Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
   2/*
   3 * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
   4 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
   5 * Copyright (c) 1999-2019, Mellanox Technologies, Inc. All rights reserved.
   6 * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   7 */
   8
   9#include <linux/completion.h>
  10#include <linux/in.h>
  11#include <linux/in6.h>
  12#include <linux/mutex.h>
  13#include <linux/random.h>
  14#include <linux/rbtree.h>
  15#include <linux/igmp.h>
  16#include <linux/xarray.h>
  17#include <linux/inetdevice.h>
  18#include <linux/slab.h>
  19#include <linux/module.h>
  20#include <net/route.h>
  21
  22#include <net/net_namespace.h>
  23#include <net/netns/generic.h>
  24#include <net/netevent.h>
  25#include <net/tcp.h>
  26#include <net/ipv6.h>
  27#include <net/ip_fib.h>
  28#include <net/ip6_route.h>
  29
  30#include <rdma/rdma_cm.h>
  31#include <rdma/rdma_cm_ib.h>
  32#include <rdma/rdma_netlink.h>
  33#include <rdma/ib.h>
  34#include <rdma/ib_cache.h>
  35#include <rdma/ib_cm.h>
  36#include <rdma/ib_sa.h>
  37#include <rdma/iw_cm.h>
  38
  39#include "core_priv.h"
  40#include "cma_priv.h"
  41#include "cma_trace.h"
  42
  43MODULE_AUTHOR("Sean Hefty");
  44MODULE_DESCRIPTION("Generic RDMA CM Agent");
  45MODULE_LICENSE("Dual BSD/GPL");
  46
  47#define CMA_CM_RESPONSE_TIMEOUT 20
 
  48#define CMA_MAX_CM_RETRIES 15
  49#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
  50#define CMA_IBOE_PACKET_LIFETIME 16
  51#define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
  52
  53static const char * const cma_events[] = {
  54	[RDMA_CM_EVENT_ADDR_RESOLVED]	 = "address resolved",
  55	[RDMA_CM_EVENT_ADDR_ERROR]	 = "address error",
  56	[RDMA_CM_EVENT_ROUTE_RESOLVED]	 = "route resolved ",
  57	[RDMA_CM_EVENT_ROUTE_ERROR]	 = "route error",
  58	[RDMA_CM_EVENT_CONNECT_REQUEST]	 = "connect request",
  59	[RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
  60	[RDMA_CM_EVENT_CONNECT_ERROR]	 = "connect error",
  61	[RDMA_CM_EVENT_UNREACHABLE]	 = "unreachable",
  62	[RDMA_CM_EVENT_REJECTED]	 = "rejected",
  63	[RDMA_CM_EVENT_ESTABLISHED]	 = "established",
  64	[RDMA_CM_EVENT_DISCONNECTED]	 = "disconnected",
  65	[RDMA_CM_EVENT_DEVICE_REMOVAL]	 = "device removal",
  66	[RDMA_CM_EVENT_MULTICAST_JOIN]	 = "multicast join",
  67	[RDMA_CM_EVENT_MULTICAST_ERROR]	 = "multicast error",
  68	[RDMA_CM_EVENT_ADDR_CHANGE]	 = "address change",
  69	[RDMA_CM_EVENT_TIMEWAIT_EXIT]	 = "timewait exit",
  70};
  71
  72static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
  73			      enum ib_gid_type gid_type);
  74
  75const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
  76{
  77	size_t index = event;
  78
  79	return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
  80			cma_events[index] : "unrecognized event";
  81}
  82EXPORT_SYMBOL(rdma_event_msg);
  83
  84const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,
  85						int reason)
  86{
  87	if (rdma_ib_or_roce(id->device, id->port_num))
  88		return ibcm_reject_msg(reason);
  89
  90	if (rdma_protocol_iwarp(id->device, id->port_num))
  91		return iwcm_reject_msg(reason);
  92
  93	WARN_ON_ONCE(1);
  94	return "unrecognized transport";
  95}
  96EXPORT_SYMBOL(rdma_reject_msg);
  97
  98/**
  99 * rdma_is_consumer_reject - return true if the consumer rejected the connect
 100 *                           request.
 101 * @id: Communication identifier that received the REJECT event.
 102 * @reason: Value returned in the REJECT event status field.
 103 */
 104static bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason)
 105{
 106	if (rdma_ib_or_roce(id->device, id->port_num))
 107		return reason == IB_CM_REJ_CONSUMER_DEFINED;
 108
 109	if (rdma_protocol_iwarp(id->device, id->port_num))
 110		return reason == -ECONNREFUSED;
 111
 112	WARN_ON_ONCE(1);
 113	return false;
 114}
 
 115
 116const void *rdma_consumer_reject_data(struct rdma_cm_id *id,
 117				      struct rdma_cm_event *ev, u8 *data_len)
 118{
 119	const void *p;
 120
 121	if (rdma_is_consumer_reject(id, ev->status)) {
 122		*data_len = ev->param.conn.private_data_len;
 123		p = ev->param.conn.private_data;
 124	} else {
 125		*data_len = 0;
 126		p = NULL;
 127	}
 128	return p;
 129}
 130EXPORT_SYMBOL(rdma_consumer_reject_data);
 131
 132/**
 133 * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
 134 * @id: Communication Identifier
 135 */
 136struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *id)
 137{
 138	struct rdma_id_private *id_priv;
 139
 140	id_priv = container_of(id, struct rdma_id_private, id);
 141	if (id->device->node_type == RDMA_NODE_RNIC)
 142		return id_priv->cm_id.iw;
 143	return NULL;
 144}
 145EXPORT_SYMBOL(rdma_iw_cm_id);
 146
 147/**
 148 * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
 149 * @res: rdma resource tracking entry pointer
 150 */
 151struct rdma_cm_id *rdma_res_to_id(struct rdma_restrack_entry *res)
 152{
 153	struct rdma_id_private *id_priv =
 154		container_of(res, struct rdma_id_private, res);
 155
 156	return &id_priv->id;
 157}
 158EXPORT_SYMBOL(rdma_res_to_id);
 159
 160static int cma_add_one(struct ib_device *device);
 161static void cma_remove_one(struct ib_device *device, void *client_data);
 162
 163static struct ib_client cma_client = {
 164	.name   = "cma",
 165	.add    = cma_add_one,
 166	.remove = cma_remove_one
 167};
 168
 169static struct ib_sa_client sa_client;
 
 170static LIST_HEAD(dev_list);
 171static LIST_HEAD(listen_any_list);
 172static DEFINE_MUTEX(lock);
 173static struct rb_root id_table = RB_ROOT;
 174/* Serialize operations of id_table tree */
 175static DEFINE_SPINLOCK(id_table_lock);
 176static struct workqueue_struct *cma_wq;
 177static unsigned int cma_pernet_id;
 178
 179struct cma_pernet {
 180	struct xarray tcp_ps;
 181	struct xarray udp_ps;
 182	struct xarray ipoib_ps;
 183	struct xarray ib_ps;
 184};
 185
 186static struct cma_pernet *cma_pernet(struct net *net)
 187{
 188	return net_generic(net, cma_pernet_id);
 189}
 190
 191static
 192struct xarray *cma_pernet_xa(struct net *net, enum rdma_ucm_port_space ps)
 193{
 194	struct cma_pernet *pernet = cma_pernet(net);
 195
 196	switch (ps) {
 197	case RDMA_PS_TCP:
 198		return &pernet->tcp_ps;
 199	case RDMA_PS_UDP:
 200		return &pernet->udp_ps;
 201	case RDMA_PS_IPOIB:
 202		return &pernet->ipoib_ps;
 203	case RDMA_PS_IB:
 204		return &pernet->ib_ps;
 205	default:
 206		return NULL;
 207	}
 208}
 209
 210struct id_table_entry {
 211	struct list_head id_list;
 212	struct rb_node rb_node;
 213};
 214
 215struct cma_device {
 216	struct list_head	list;
 217	struct ib_device	*device;
 218	struct completion	comp;
 219	refcount_t refcount;
 220	struct list_head	id_list;
 221	enum ib_gid_type	*default_gid_type;
 222	u8			*default_roce_tos;
 223};
 224
 225struct rdma_bind_list {
 226	enum rdma_ucm_port_space ps;
 227	struct hlist_head	owners;
 228	unsigned short		port;
 229};
 230
 231static int cma_ps_alloc(struct net *net, enum rdma_ucm_port_space ps,
 
 
 
 
 
 
 
 
 232			struct rdma_bind_list *bind_list, int snum)
 233{
 234	struct xarray *xa = cma_pernet_xa(net, ps);
 235
 236	return xa_insert(xa, snum, bind_list, GFP_KERNEL);
 237}
 238
 239static struct rdma_bind_list *cma_ps_find(struct net *net,
 240					  enum rdma_ucm_port_space ps, int snum)
 241{
 242	struct xarray *xa = cma_pernet_xa(net, ps);
 243
 244	return xa_load(xa, snum);
 245}
 246
 247static void cma_ps_remove(struct net *net, enum rdma_ucm_port_space ps,
 248			  int snum)
 249{
 250	struct xarray *xa = cma_pernet_xa(net, ps);
 251
 252	xa_erase(xa, snum);
 253}
 254
 255enum {
 256	CMA_OPTION_AFONLY,
 257};
 258
 259void cma_dev_get(struct cma_device *cma_dev)
 260{
 261	refcount_inc(&cma_dev->refcount);
 262}
 263
 264void cma_dev_put(struct cma_device *cma_dev)
 265{
 266	if (refcount_dec_and_test(&cma_dev->refcount))
 267		complete(&cma_dev->comp);
 268}
 269
 270struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter	filter,
 271					     void		*cookie)
 272{
 273	struct cma_device *cma_dev;
 274	struct cma_device *found_cma_dev = NULL;
 275
 276	mutex_lock(&lock);
 277
 278	list_for_each_entry(cma_dev, &dev_list, list)
 279		if (filter(cma_dev->device, cookie)) {
 280			found_cma_dev = cma_dev;
 281			break;
 282		}
 283
 284	if (found_cma_dev)
 285		cma_dev_get(found_cma_dev);
 286	mutex_unlock(&lock);
 287	return found_cma_dev;
 288}
 289
 290int cma_get_default_gid_type(struct cma_device *cma_dev,
 291			     u32 port)
 292{
 293	if (!rdma_is_port_valid(cma_dev->device, port))
 
 294		return -EINVAL;
 295
 296	return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
 297}
 298
 299int cma_set_default_gid_type(struct cma_device *cma_dev,
 300			     u32 port,
 301			     enum ib_gid_type default_gid_type)
 302{
 303	unsigned long supported_gids;
 304
 305	if (!rdma_is_port_valid(cma_dev->device, port))
 
 306		return -EINVAL;
 307
 308	if (default_gid_type == IB_GID_TYPE_IB &&
 309	    rdma_protocol_roce_eth_encap(cma_dev->device, port))
 310		default_gid_type = IB_GID_TYPE_ROCE;
 311
 312	supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
 313
 314	if (!(supported_gids & 1 << default_gid_type))
 315		return -EINVAL;
 316
 317	cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
 318		default_gid_type;
 319
 320	return 0;
 321}
 322
 323int cma_get_default_roce_tos(struct cma_device *cma_dev, u32 port)
 324{
 325	if (!rdma_is_port_valid(cma_dev->device, port))
 326		return -EINVAL;
 327
 328	return cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)];
 329}
 330
 331int cma_set_default_roce_tos(struct cma_device *cma_dev, u32 port,
 332			     u8 default_roce_tos)
 333{
 334	if (!rdma_is_port_valid(cma_dev->device, port))
 335		return -EINVAL;
 336
 337	cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)] =
 338		 default_roce_tos;
 339
 340	return 0;
 341}
 342struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
 343{
 344	return cma_dev->device;
 345}
 346
 347/*
 348 * Device removal can occur at anytime, so we need extra handling to
 349 * serialize notifying the user of device removal with other callbacks.
 350 * We do this by disabling removal notification while a callback is in process,
 351 * and reporting it after the callback completes.
 352 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 353
 354struct cma_multicast {
 355	struct rdma_id_private *id_priv;
 356	union {
 357		struct ib_sa_multicast *sa_mc;
 358		struct {
 359			struct work_struct work;
 360			struct rdma_cm_event event;
 361		} iboe_join;
 362	};
 363	struct list_head	list;
 364	void			*context;
 365	struct sockaddr_storage	addr;
 
 
 366	u8			join_state;
 367};
 368
 369struct cma_work {
 370	struct work_struct	work;
 371	struct rdma_id_private	*id;
 372	enum rdma_cm_state	old_state;
 373	enum rdma_cm_state	new_state;
 374	struct rdma_cm_event	event;
 375};
 376
 
 
 
 
 
 
 
 
 
 
 
 
 377union cma_ip_addr {
 378	struct in6_addr ip6;
 379	struct {
 380		__be32 pad[3];
 381		__be32 addr;
 382	} ip4;
 383};
 384
 385struct cma_hdr {
 386	u8 cma_version;
 387	u8 ip_version;	/* IP version: 7:4 */
 388	__be16 port;
 389	union cma_ip_addr src_addr;
 390	union cma_ip_addr dst_addr;
 391};
 392
 393#define CMA_VERSION 0x00
 394
 395struct cma_req_info {
 396	struct sockaddr_storage listen_addr_storage;
 397	struct sockaddr_storage src_addr_storage;
 398	struct ib_device *device;
 
 399	union ib_gid local_gid;
 400	__be64 service_id;
 401	int port;
 402	bool has_gid;
 403	u16 pkey;
 
 404};
 405
 
 
 
 
 
 
 
 
 
 
 
 406static int cma_comp_exch(struct rdma_id_private *id_priv,
 407			 enum rdma_cm_state comp, enum rdma_cm_state exch)
 408{
 409	unsigned long flags;
 410	int ret;
 411
 412	/*
 413	 * The FSM uses a funny double locking where state is protected by both
 414	 * the handler_mutex and the spinlock. State is not allowed to change
 415	 * to/from a handler_mutex protected value without also holding
 416	 * handler_mutex.
 417	 */
 418	if (comp == RDMA_CM_CONNECT || exch == RDMA_CM_CONNECT)
 419		lockdep_assert_held(&id_priv->handler_mutex);
 420
 421	spin_lock_irqsave(&id_priv->lock, flags);
 422	if ((ret = (id_priv->state == comp)))
 423		id_priv->state = exch;
 424	spin_unlock_irqrestore(&id_priv->lock, flags);
 425	return ret;
 426}
 427
 428static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
 
 429{
 430	return hdr->ip_version >> 4;
 431}
 432
 433static void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
 434{
 435	hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
 
 
 436}
 437
 438static struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
 439{
 440	return (struct sockaddr *)&id_priv->id.route.addr.src_addr;
 441}
 442
 443static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
 444{
 445	return (struct sockaddr *)&id_priv->id.route.addr.dst_addr;
 446}
 447
 448static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
 449{
 450	struct in_device *in_dev = NULL;
 451
 452	if (ndev) {
 453		rtnl_lock();
 454		in_dev = __in_dev_get_rtnl(ndev);
 455		if (in_dev) {
 456			if (join)
 457				ip_mc_inc_group(in_dev,
 458						*(__be32 *)(mgid->raw + 12));
 459			else
 460				ip_mc_dec_group(in_dev,
 461						*(__be32 *)(mgid->raw + 12));
 462		}
 463		rtnl_unlock();
 464	}
 465	return (in_dev) ? 0 : -ENODEV;
 466}
 467
 468static int compare_netdev_and_ip(int ifindex_a, struct sockaddr *sa,
 469				 struct id_table_entry *entry_b)
 470{
 471	struct rdma_id_private *id_priv = list_first_entry(
 472		&entry_b->id_list, struct rdma_id_private, id_list_entry);
 473	int ifindex_b = id_priv->id.route.addr.dev_addr.bound_dev_if;
 474	struct sockaddr *sb = cma_dst_addr(id_priv);
 475
 476	if (ifindex_a != ifindex_b)
 477		return (ifindex_a > ifindex_b) ? 1 : -1;
 478
 479	if (sa->sa_family != sb->sa_family)
 480		return sa->sa_family - sb->sa_family;
 481
 482	if (sa->sa_family == AF_INET &&
 483	    __builtin_object_size(sa, 0) >= sizeof(struct sockaddr_in)) {
 484		return memcmp(&((struct sockaddr_in *)sa)->sin_addr,
 485			      &((struct sockaddr_in *)sb)->sin_addr,
 486			      sizeof(((struct sockaddr_in *)sa)->sin_addr));
 487	}
 488
 489	if (sa->sa_family == AF_INET6 &&
 490	    __builtin_object_size(sa, 0) >= sizeof(struct sockaddr_in6)) {
 491		return ipv6_addr_cmp(&((struct sockaddr_in6 *)sa)->sin6_addr,
 492				     &((struct sockaddr_in6 *)sb)->sin6_addr);
 493	}
 494
 495	return -1;
 496}
 497
 498static int cma_add_id_to_tree(struct rdma_id_private *node_id_priv)
 499{
 500	struct rb_node **new, *parent = NULL;
 501	struct id_table_entry *this, *node;
 502	unsigned long flags;
 503	int result;
 504
 505	node = kzalloc(sizeof(*node), GFP_KERNEL);
 506	if (!node)
 507		return -ENOMEM;
 508
 509	spin_lock_irqsave(&id_table_lock, flags);
 510	new = &id_table.rb_node;
 511	while (*new) {
 512		this = container_of(*new, struct id_table_entry, rb_node);
 513		result = compare_netdev_and_ip(
 514			node_id_priv->id.route.addr.dev_addr.bound_dev_if,
 515			cma_dst_addr(node_id_priv), this);
 516
 517		parent = *new;
 518		if (result < 0)
 519			new = &((*new)->rb_left);
 520		else if (result > 0)
 521			new = &((*new)->rb_right);
 522		else {
 523			list_add_tail(&node_id_priv->id_list_entry,
 524				      &this->id_list);
 525			kfree(node);
 526			goto unlock;
 527		}
 528	}
 529
 530	INIT_LIST_HEAD(&node->id_list);
 531	list_add_tail(&node_id_priv->id_list_entry, &node->id_list);
 532
 533	rb_link_node(&node->rb_node, parent, new);
 534	rb_insert_color(&node->rb_node, &id_table);
 535
 536unlock:
 537	spin_unlock_irqrestore(&id_table_lock, flags);
 538	return 0;
 539}
 540
 541static struct id_table_entry *
 542node_from_ndev_ip(struct rb_root *root, int ifindex, struct sockaddr *sa)
 543{
 544	struct rb_node *node = root->rb_node;
 545	struct id_table_entry *data;
 546	int result;
 547
 548	while (node) {
 549		data = container_of(node, struct id_table_entry, rb_node);
 550		result = compare_netdev_and_ip(ifindex, sa, data);
 551		if (result < 0)
 552			node = node->rb_left;
 553		else if (result > 0)
 554			node = node->rb_right;
 555		else
 556			return data;
 557	}
 558
 559	return NULL;
 560}
 561
 562static void cma_remove_id_from_tree(struct rdma_id_private *id_priv)
 563{
 564	struct id_table_entry *data;
 565	unsigned long flags;
 566
 567	spin_lock_irqsave(&id_table_lock, flags);
 568	if (list_empty(&id_priv->id_list_entry))
 569		goto out;
 570
 571	data = node_from_ndev_ip(&id_table,
 572				 id_priv->id.route.addr.dev_addr.bound_dev_if,
 573				 cma_dst_addr(id_priv));
 574	if (!data)
 575		goto out;
 576
 577	list_del_init(&id_priv->id_list_entry);
 578	if (list_empty(&data->id_list)) {
 579		rb_erase(&data->rb_node, &id_table);
 580		kfree(data);
 581	}
 582out:
 583	spin_unlock_irqrestore(&id_table_lock, flags);
 584}
 585
 586static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
 587			       struct cma_device *cma_dev)
 588{
 589	cma_dev_get(cma_dev);
 590	id_priv->cma_dev = cma_dev;
 
 591	id_priv->id.device = cma_dev->device;
 592	id_priv->id.route.addr.dev_addr.transport =
 593		rdma_node_get_transport(cma_dev->device->node_type);
 594	list_add_tail(&id_priv->device_item, &cma_dev->id_list);
 595
 596	trace_cm_id_attach(id_priv, cma_dev->device);
 597}
 598
 599static void cma_attach_to_dev(struct rdma_id_private *id_priv,
 600			      struct cma_device *cma_dev)
 601{
 602	_cma_attach_to_dev(id_priv, cma_dev);
 603	id_priv->gid_type =
 604		cma_dev->default_gid_type[id_priv->id.port_num -
 605					  rdma_start_port(cma_dev->device)];
 606}
 607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 608static void cma_release_dev(struct rdma_id_private *id_priv)
 609{
 610	mutex_lock(&lock);
 611	list_del_init(&id_priv->device_item);
 612	cma_dev_put(id_priv->cma_dev);
 613	id_priv->cma_dev = NULL;
 614	id_priv->id.device = NULL;
 615	if (id_priv->id.route.addr.dev_addr.sgid_attr) {
 616		rdma_put_gid_attr(id_priv->id.route.addr.dev_addr.sgid_attr);
 617		id_priv->id.route.addr.dev_addr.sgid_attr = NULL;
 618	}
 619	mutex_unlock(&lock);
 620}
 621
 
 
 
 
 
 
 
 
 
 
 622static inline unsigned short cma_family(struct rdma_id_private *id_priv)
 623{
 624	return id_priv->id.route.addr.src_addr.ss_family;
 625}
 626
 627static int cma_set_default_qkey(struct rdma_id_private *id_priv)
 628{
 629	struct ib_sa_mcmember_rec rec;
 630	int ret = 0;
 631
 
 
 
 
 
 
 
 
 
 
 
 632	switch (id_priv->id.ps) {
 633	case RDMA_PS_UDP:
 634	case RDMA_PS_IB:
 635		id_priv->qkey = RDMA_UDP_QKEY;
 636		break;
 637	case RDMA_PS_IPOIB:
 638		ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
 639		ret = ib_sa_get_mcmember_rec(id_priv->id.device,
 640					     id_priv->id.port_num, &rec.mgid,
 641					     &rec);
 642		if (!ret)
 643			id_priv->qkey = be32_to_cpu(rec.qkey);
 644		break;
 645	default:
 646		break;
 647	}
 648	return ret;
 649}
 650
 651static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
 652{
 653	if (!qkey ||
 654	    (id_priv->qkey && (id_priv->qkey != qkey)))
 655		return -EINVAL;
 656
 657	id_priv->qkey = qkey;
 658	return 0;
 659}
 660
 661static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
 662{
 663	dev_addr->dev_type = ARPHRD_INFINIBAND;
 664	rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
 665	ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
 666}
 667
 668static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
 669{
 670	int ret;
 671
 672	if (addr->sa_family != AF_IB) {
 673		ret = rdma_translate_ip(addr, dev_addr);
 674	} else {
 675		cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
 676		ret = 0;
 677	}
 678
 679	return ret;
 680}
 681
 682static const struct ib_gid_attr *
 683cma_validate_port(struct ib_device *device, u32 port,
 684		  enum ib_gid_type gid_type,
 685		  union ib_gid *gid,
 686		  struct rdma_id_private *id_priv)
 687{
 688	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
 689	const struct ib_gid_attr *sgid_attr = ERR_PTR(-ENODEV);
 690	int bound_if_index = dev_addr->bound_dev_if;
 691	int dev_type = dev_addr->dev_type;
 692	struct net_device *ndev = NULL;
 693
 694	if (!rdma_dev_access_netns(device, id_priv->id.route.addr.dev_addr.net))
 695		goto out;
 696
 697	if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
 698		goto out;
 699
 700	if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
 701		goto out;
 702
 703	/*
 704	 * For drivers that do not associate more than one net device with
 705	 * their gid tables, such as iWARP drivers, it is sufficient to
 706	 * return the first table entry.
 707	 *
 708	 * Other driver classes might be included in the future.
 709	 */
 710	if (rdma_protocol_iwarp(device, port)) {
 711		sgid_attr = rdma_get_gid_attr(device, port, 0);
 712		if (IS_ERR(sgid_attr))
 713			goto out;
 714
 715		rcu_read_lock();
 716		ndev = rcu_dereference(sgid_attr->ndev);
 717		if (!net_eq(dev_net(ndev), dev_addr->net) ||
 718		    ndev->ifindex != bound_if_index)
 719			sgid_attr = ERR_PTR(-ENODEV);
 720		rcu_read_unlock();
 721		goto out;
 722	}
 723
 724	if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
 725		ndev = dev_get_by_index(dev_addr->net, bound_if_index);
 726		if (!ndev)
 727			goto out;
 
 
 
 
 
 
 
 
 
 728	} else {
 729		gid_type = IB_GID_TYPE_IB;
 730	}
 731
 732	sgid_attr = rdma_find_gid_by_port(device, gid, gid_type, port, ndev);
 733	dev_put(ndev);
 734out:
 735	return sgid_attr;
 736}
 737
 738static void cma_bind_sgid_attr(struct rdma_id_private *id_priv,
 739			       const struct ib_gid_attr *sgid_attr)
 740{
 741	WARN_ON(id_priv->id.route.addr.dev_addr.sgid_attr);
 742	id_priv->id.route.addr.dev_addr.sgid_attr = sgid_attr;
 743}
 744
 745/**
 746 * cma_acquire_dev_by_src_ip - Acquire cma device, port, gid attribute
 747 * based on source ip address.
 748 * @id_priv:	cm_id which should be bound to cma device
 749 *
 750 * cma_acquire_dev_by_src_ip() binds cm id to cma device, port and GID attribute
 751 * based on source IP address. It returns 0 on success or error code otherwise.
 752 * It is applicable to active and passive side cm_id.
 753 */
 754static int cma_acquire_dev_by_src_ip(struct rdma_id_private *id_priv)
 755{
 756	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
 757	const struct ib_gid_attr *sgid_attr;
 758	union ib_gid gid, iboe_gid, *gidp;
 759	struct cma_device *cma_dev;
 760	enum ib_gid_type gid_type;
 761	int ret = -ENODEV;
 762	u32 port;
 763
 764	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
 765	    id_priv->id.ps == RDMA_PS_IPOIB)
 766		return -EINVAL;
 767
 
 768	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
 769		    &iboe_gid);
 770
 771	memcpy(&gid, dev_addr->src_dev_addr +
 772	       rdma_addr_gid_offset(dev_addr), sizeof(gid));
 773
 774	mutex_lock(&lock);
 775	list_for_each_entry(cma_dev, &dev_list, list) {
 776		rdma_for_each_port (cma_dev->device, port) {
 777			gidp = rdma_protocol_roce(cma_dev->device, port) ?
 778			       &iboe_gid : &gid;
 779			gid_type = cma_dev->default_gid_type[port - 1];
 780			sgid_attr = cma_validate_port(cma_dev->device, port,
 781						      gid_type, gidp, id_priv);
 782			if (!IS_ERR(sgid_attr)) {
 783				id_priv->id.port_num = port;
 784				cma_bind_sgid_attr(id_priv, sgid_attr);
 785				cma_attach_to_dev(id_priv, cma_dev);
 786				ret = 0;
 787				goto out;
 788			}
 789		}
 790	}
 791out:
 792	mutex_unlock(&lock);
 793	return ret;
 794}
 795
 796/**
 797 * cma_ib_acquire_dev - Acquire cma device, port and SGID attribute
 798 * @id_priv:		cm id to bind to cma device
 799 * @listen_id_priv:	listener cm id to match against
 800 * @req:		Pointer to req structure containaining incoming
 801 *			request information
 802 * cma_ib_acquire_dev() acquires cma device, port and SGID attribute when
 803 * rdma device matches for listen_id and incoming request. It also verifies
 804 * that a GID table entry is present for the source address.
 805 * Returns 0 on success, or returns error code otherwise.
 806 */
 807static int cma_ib_acquire_dev(struct rdma_id_private *id_priv,
 808			      const struct rdma_id_private *listen_id_priv,
 809			      struct cma_req_info *req)
 810{
 811	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
 812	const struct ib_gid_attr *sgid_attr;
 813	enum ib_gid_type gid_type;
 814	union ib_gid gid;
 815
 816	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
 817	    id_priv->id.ps == RDMA_PS_IPOIB)
 818		return -EINVAL;
 819
 820	if (rdma_protocol_roce(req->device, req->port))
 821		rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
 822			    &gid);
 823	else
 824		memcpy(&gid, dev_addr->src_dev_addr +
 825		       rdma_addr_gid_offset(dev_addr), sizeof(gid));
 826
 827	gid_type = listen_id_priv->cma_dev->default_gid_type[req->port - 1];
 828	sgid_attr = cma_validate_port(req->device, req->port,
 829				      gid_type, &gid, id_priv);
 830	if (IS_ERR(sgid_attr))
 831		return PTR_ERR(sgid_attr);
 832
 833	id_priv->id.port_num = req->port;
 834	cma_bind_sgid_attr(id_priv, sgid_attr);
 835	/* Need to acquire lock to protect against reader
 836	 * of cma_dev->id_list such as cma_netdev_callback() and
 837	 * cma_process_remove().
 838	 */
 839	mutex_lock(&lock);
 840	cma_attach_to_dev(id_priv, listen_id_priv->cma_dev);
 841	mutex_unlock(&lock);
 842	rdma_restrack_add(&id_priv->res);
 843	return 0;
 844}
 845
 846static int cma_iw_acquire_dev(struct rdma_id_private *id_priv,
 847			      const struct rdma_id_private *listen_id_priv)
 848{
 849	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
 850	const struct ib_gid_attr *sgid_attr;
 851	struct cma_device *cma_dev;
 852	enum ib_gid_type gid_type;
 853	int ret = -ENODEV;
 854	union ib_gid gid;
 855	u32 port;
 856
 857	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
 858	    id_priv->id.ps == RDMA_PS_IPOIB)
 859		return -EINVAL;
 860
 861	memcpy(&gid, dev_addr->src_dev_addr +
 862	       rdma_addr_gid_offset(dev_addr), sizeof(gid));
 863
 864	mutex_lock(&lock);
 865
 866	cma_dev = listen_id_priv->cma_dev;
 867	port = listen_id_priv->id.port_num;
 868	gid_type = listen_id_priv->gid_type;
 869	sgid_attr = cma_validate_port(cma_dev->device, port,
 870				      gid_type, &gid, id_priv);
 871	if (!IS_ERR(sgid_attr)) {
 872		id_priv->id.port_num = port;
 873		cma_bind_sgid_attr(id_priv, sgid_attr);
 874		ret = 0;
 875		goto out;
 876	}
 877
 878	list_for_each_entry(cma_dev, &dev_list, list) {
 879		rdma_for_each_port (cma_dev->device, port) {
 880			if (listen_id_priv->cma_dev == cma_dev &&
 
 881			    listen_id_priv->id.port_num == port)
 882				continue;
 883
 884			gid_type = cma_dev->default_gid_type[port - 1];
 885			sgid_attr = cma_validate_port(cma_dev->device, port,
 886						      gid_type, &gid, id_priv);
 887			if (!IS_ERR(sgid_attr)) {
 
 
 
 
 
 
 888				id_priv->id.port_num = port;
 889				cma_bind_sgid_attr(id_priv, sgid_attr);
 890				ret = 0;
 891				goto out;
 892			}
 893		}
 894	}
 895
 896out:
 897	if (!ret) {
 898		cma_attach_to_dev(id_priv, cma_dev);
 899		rdma_restrack_add(&id_priv->res);
 900	}
 901
 902	mutex_unlock(&lock);
 903	return ret;
 904}
 905
 906/*
 907 * Select the source IB device and address to reach the destination IB address.
 908 */
 909static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
 910{
 911	struct cma_device *cma_dev, *cur_dev;
 912	struct sockaddr_ib *addr;
 913	union ib_gid gid, sgid, *dgid;
 914	unsigned int p;
 915	u16 pkey, index;
 916	enum ib_port_state port_state;
 917	int ret;
 918	int i;
 919
 920	cma_dev = NULL;
 921	addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
 922	dgid = (union ib_gid *) &addr->sib_addr;
 923	pkey = ntohs(addr->sib_pkey);
 924
 925	mutex_lock(&lock);
 926	list_for_each_entry(cur_dev, &dev_list, list) {
 927		rdma_for_each_port (cur_dev->device, p) {
 928			if (!rdma_cap_af_ib(cur_dev->device, p))
 929				continue;
 930
 931			if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
 932				continue;
 933
 934			if (ib_get_cached_port_state(cur_dev->device, p, &port_state))
 935				continue;
 936
 937			for (i = 0; i < cur_dev->device->port_data[p].immutable.gid_tbl_len;
 938			     ++i) {
 939				ret = rdma_query_gid(cur_dev->device, p, i,
 940						     &gid);
 941				if (ret)
 942					continue;
 943
 944				if (!memcmp(&gid, dgid, sizeof(gid))) {
 945					cma_dev = cur_dev;
 946					sgid = gid;
 947					id_priv->id.port_num = p;
 948					goto found;
 949				}
 950
 951				if (!cma_dev && (gid.global.subnet_prefix ==
 952				    dgid->global.subnet_prefix) &&
 953				    port_state == IB_PORT_ACTIVE) {
 954					cma_dev = cur_dev;
 955					sgid = gid;
 956					id_priv->id.port_num = p;
 957					goto found;
 958				}
 959			}
 960		}
 961	}
 962	mutex_unlock(&lock);
 963	return -ENODEV;
 
 964
 965found:
 966	cma_attach_to_dev(id_priv, cma_dev);
 967	rdma_restrack_add(&id_priv->res);
 968	mutex_unlock(&lock);
 969	addr = (struct sockaddr_ib *)cma_src_addr(id_priv);
 970	memcpy(&addr->sib_addr, &sgid, sizeof(sgid));
 971	cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
 972	return 0;
 973}
 974
 975static void cma_id_get(struct rdma_id_private *id_priv)
 976{
 977	refcount_inc(&id_priv->refcount);
 978}
 979
 980static void cma_id_put(struct rdma_id_private *id_priv)
 981{
 982	if (refcount_dec_and_test(&id_priv->refcount))
 983		complete(&id_priv->comp);
 984}
 985
 986static struct rdma_id_private *
 987__rdma_create_id(struct net *net, rdma_cm_event_handler event_handler,
 988		 void *context, enum rdma_ucm_port_space ps,
 989		 enum ib_qp_type qp_type, const struct rdma_id_private *parent)
 990{
 991	struct rdma_id_private *id_priv;
 992
 993	id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
 994	if (!id_priv)
 995		return ERR_PTR(-ENOMEM);
 996
 
 997	id_priv->state = RDMA_CM_IDLE;
 998	id_priv->id.context = context;
 999	id_priv->id.event_handler = event_handler;
1000	id_priv->id.ps = ps;
1001	id_priv->id.qp_type = qp_type;
1002	id_priv->tos_set = false;
1003	id_priv->timeout_set = false;
1004	id_priv->min_rnr_timer_set = false;
1005	id_priv->gid_type = IB_GID_TYPE_IB;
1006	spin_lock_init(&id_priv->lock);
1007	mutex_init(&id_priv->qp_mutex);
1008	init_completion(&id_priv->comp);
1009	refcount_set(&id_priv->refcount, 1);
1010	mutex_init(&id_priv->handler_mutex);
1011	INIT_LIST_HEAD(&id_priv->device_item);
1012	INIT_LIST_HEAD(&id_priv->id_list_entry);
1013	INIT_LIST_HEAD(&id_priv->listen_list);
1014	INIT_LIST_HEAD(&id_priv->mc_list);
1015	get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
1016	id_priv->id.route.addr.dev_addr.net = get_net(net);
1017	id_priv->seq_num &= 0x00ffffff;
1018
1019	rdma_restrack_new(&id_priv->res, RDMA_RESTRACK_CM_ID);
1020	if (parent)
1021		rdma_restrack_parent_name(&id_priv->res, &parent->res);
1022
1023	return id_priv;
1024}
1025
1026struct rdma_cm_id *
1027__rdma_create_kernel_id(struct net *net, rdma_cm_event_handler event_handler,
1028			void *context, enum rdma_ucm_port_space ps,
1029			enum ib_qp_type qp_type, const char *caller)
1030{
1031	struct rdma_id_private *ret;
1032
1033	ret = __rdma_create_id(net, event_handler, context, ps, qp_type, NULL);
1034	if (IS_ERR(ret))
1035		return ERR_CAST(ret);
1036
1037	rdma_restrack_set_name(&ret->res, caller);
1038	return &ret->id;
1039}
1040EXPORT_SYMBOL(__rdma_create_kernel_id);
1041
1042struct rdma_cm_id *rdma_create_user_id(rdma_cm_event_handler event_handler,
1043				       void *context,
1044				       enum rdma_ucm_port_space ps,
1045				       enum ib_qp_type qp_type)
1046{
1047	struct rdma_id_private *ret;
1048
1049	ret = __rdma_create_id(current->nsproxy->net_ns, event_handler, context,
1050			       ps, qp_type, NULL);
1051	if (IS_ERR(ret))
1052		return ERR_CAST(ret);
1053
1054	rdma_restrack_set_name(&ret->res, NULL);
1055	return &ret->id;
1056}
1057EXPORT_SYMBOL(rdma_create_user_id);
1058
1059static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
1060{
1061	struct ib_qp_attr qp_attr;
1062	int qp_attr_mask, ret;
1063
1064	qp_attr.qp_state = IB_QPS_INIT;
1065	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1066	if (ret)
1067		return ret;
1068
1069	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1070	if (ret)
1071		return ret;
1072
1073	qp_attr.qp_state = IB_QPS_RTR;
1074	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
1075	if (ret)
1076		return ret;
1077
1078	qp_attr.qp_state = IB_QPS_RTS;
1079	qp_attr.sq_psn = 0;
1080	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
1081
1082	return ret;
1083}
1084
1085static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
1086{
1087	struct ib_qp_attr qp_attr;
1088	int qp_attr_mask, ret;
1089
1090	qp_attr.qp_state = IB_QPS_INIT;
1091	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1092	if (ret)
1093		return ret;
1094
1095	return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1096}
1097
1098int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
1099		   struct ib_qp_init_attr *qp_init_attr)
1100{
1101	struct rdma_id_private *id_priv;
1102	struct ib_qp *qp;
1103	int ret;
1104
1105	id_priv = container_of(id, struct rdma_id_private, id);
1106	if (id->device != pd->device) {
1107		ret = -EINVAL;
1108		goto out_err;
1109	}
1110
1111	qp_init_attr->port_num = id->port_num;
1112	qp = ib_create_qp(pd, qp_init_attr);
1113	if (IS_ERR(qp)) {
1114		ret = PTR_ERR(qp);
1115		goto out_err;
1116	}
1117
1118	if (id->qp_type == IB_QPT_UD)
1119		ret = cma_init_ud_qp(id_priv, qp);
1120	else
1121		ret = cma_init_conn_qp(id_priv, qp);
1122	if (ret)
1123		goto out_destroy;
1124
1125	id->qp = qp;
1126	id_priv->qp_num = qp->qp_num;
1127	id_priv->srq = (qp->srq != NULL);
1128	trace_cm_qp_create(id_priv, pd, qp_init_attr, 0);
1129	return 0;
1130out_destroy:
1131	ib_destroy_qp(qp);
1132out_err:
1133	trace_cm_qp_create(id_priv, pd, qp_init_attr, ret);
1134	return ret;
1135}
1136EXPORT_SYMBOL(rdma_create_qp);
1137
1138void rdma_destroy_qp(struct rdma_cm_id *id)
1139{
1140	struct rdma_id_private *id_priv;
1141
1142	id_priv = container_of(id, struct rdma_id_private, id);
1143	trace_cm_qp_destroy(id_priv);
1144	mutex_lock(&id_priv->qp_mutex);
1145	ib_destroy_qp(id_priv->id.qp);
1146	id_priv->id.qp = NULL;
1147	mutex_unlock(&id_priv->qp_mutex);
1148}
1149EXPORT_SYMBOL(rdma_destroy_qp);
1150
1151static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
1152			     struct rdma_conn_param *conn_param)
1153{
1154	struct ib_qp_attr qp_attr;
1155	int qp_attr_mask, ret;
 
1156
1157	mutex_lock(&id_priv->qp_mutex);
1158	if (!id_priv->id.qp) {
1159		ret = 0;
1160		goto out;
1161	}
1162
1163	/* Need to update QP attributes from default values. */
1164	qp_attr.qp_state = IB_QPS_INIT;
1165	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1166	if (ret)
1167		goto out;
1168
1169	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1170	if (ret)
1171		goto out;
1172
1173	qp_attr.qp_state = IB_QPS_RTR;
1174	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1175	if (ret)
1176		goto out;
1177
 
 
 
 
 
1178	BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
1179
1180	if (conn_param)
1181		qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
1182	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1183out:
1184	mutex_unlock(&id_priv->qp_mutex);
1185	return ret;
1186}
1187
1188static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
1189			     struct rdma_conn_param *conn_param)
1190{
1191	struct ib_qp_attr qp_attr;
1192	int qp_attr_mask, ret;
1193
1194	mutex_lock(&id_priv->qp_mutex);
1195	if (!id_priv->id.qp) {
1196		ret = 0;
1197		goto out;
1198	}
1199
1200	qp_attr.qp_state = IB_QPS_RTS;
1201	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1202	if (ret)
1203		goto out;
1204
1205	if (conn_param)
1206		qp_attr.max_rd_atomic = conn_param->initiator_depth;
1207	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1208out:
1209	mutex_unlock(&id_priv->qp_mutex);
1210	return ret;
1211}
1212
1213static int cma_modify_qp_err(struct rdma_id_private *id_priv)
1214{
1215	struct ib_qp_attr qp_attr;
1216	int ret;
1217
1218	mutex_lock(&id_priv->qp_mutex);
1219	if (!id_priv->id.qp) {
1220		ret = 0;
1221		goto out;
1222	}
1223
1224	qp_attr.qp_state = IB_QPS_ERR;
1225	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
1226out:
1227	mutex_unlock(&id_priv->qp_mutex);
1228	return ret;
1229}
1230
1231static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
1232			       struct ib_qp_attr *qp_attr, int *qp_attr_mask)
1233{
1234	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
1235	int ret;
1236	u16 pkey;
1237
1238	if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
1239		pkey = 0xffff;
1240	else
1241		pkey = ib_addr_get_pkey(dev_addr);
1242
1243	ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
1244				  pkey, &qp_attr->pkey_index);
1245	if (ret)
1246		return ret;
1247
1248	qp_attr->port_num = id_priv->id.port_num;
1249	*qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
1250
1251	if (id_priv->id.qp_type == IB_QPT_UD) {
1252		ret = cma_set_default_qkey(id_priv);
1253		if (ret)
1254			return ret;
1255
1256		qp_attr->qkey = id_priv->qkey;
1257		*qp_attr_mask |= IB_QP_QKEY;
1258	} else {
1259		qp_attr->qp_access_flags = 0;
1260		*qp_attr_mask |= IB_QP_ACCESS_FLAGS;
1261	}
1262	return 0;
1263}
1264
1265int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
1266		       int *qp_attr_mask)
1267{
1268	struct rdma_id_private *id_priv;
1269	int ret = 0;
1270
1271	id_priv = container_of(id, struct rdma_id_private, id);
1272	if (rdma_cap_ib_cm(id->device, id->port_num)) {
1273		if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
1274			ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
1275		else
1276			ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
1277						 qp_attr_mask);
1278
1279		if (qp_attr->qp_state == IB_QPS_RTR)
1280			qp_attr->rq_psn = id_priv->seq_num;
1281	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
1282		if (!id_priv->cm_id.iw) {
1283			qp_attr->qp_access_flags = 0;
1284			*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1285		} else
1286			ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
1287						 qp_attr_mask);
1288		qp_attr->port_num = id_priv->id.port_num;
1289		*qp_attr_mask |= IB_QP_PORT;
1290	} else {
1291		ret = -ENOSYS;
1292	}
1293
1294	if ((*qp_attr_mask & IB_QP_TIMEOUT) && id_priv->timeout_set)
1295		qp_attr->timeout = id_priv->timeout;
1296
1297	if ((*qp_attr_mask & IB_QP_MIN_RNR_TIMER) && id_priv->min_rnr_timer_set)
1298		qp_attr->min_rnr_timer = id_priv->min_rnr_timer;
1299
1300	return ret;
1301}
1302EXPORT_SYMBOL(rdma_init_qp_attr);
1303
1304static inline bool cma_zero_addr(const struct sockaddr *addr)
1305{
1306	switch (addr->sa_family) {
1307	case AF_INET:
1308		return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
1309	case AF_INET6:
1310		return ipv6_addr_any(&((struct sockaddr_in6 *)addr)->sin6_addr);
1311	case AF_IB:
1312		return ib_addr_any(&((struct sockaddr_ib *)addr)->sib_addr);
1313	default:
1314		return false;
1315	}
1316}
1317
1318static inline bool cma_loopback_addr(const struct sockaddr *addr)
1319{
1320	switch (addr->sa_family) {
1321	case AF_INET:
1322		return ipv4_is_loopback(
1323			((struct sockaddr_in *)addr)->sin_addr.s_addr);
1324	case AF_INET6:
1325		return ipv6_addr_loopback(
1326			&((struct sockaddr_in6 *)addr)->sin6_addr);
1327	case AF_IB:
1328		return ib_addr_loopback(
1329			&((struct sockaddr_ib *)addr)->sib_addr);
1330	default:
1331		return false;
1332	}
1333}
1334
1335static inline bool cma_any_addr(const struct sockaddr *addr)
1336{
1337	return cma_zero_addr(addr) || cma_loopback_addr(addr);
1338}
1339
1340static int cma_addr_cmp(const struct sockaddr *src, const struct sockaddr *dst)
1341{
1342	if (src->sa_family != dst->sa_family)
1343		return -1;
1344
1345	switch (src->sa_family) {
1346	case AF_INET:
1347		return ((struct sockaddr_in *)src)->sin_addr.s_addr !=
1348		       ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1349	case AF_INET6: {
1350		struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *)src;
1351		struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *)dst;
1352		bool link_local;
1353
1354		if (ipv6_addr_cmp(&src_addr6->sin6_addr,
1355					  &dst_addr6->sin6_addr))
1356			return 1;
1357		link_local = ipv6_addr_type(&dst_addr6->sin6_addr) &
1358			     IPV6_ADDR_LINKLOCAL;
1359		/* Link local must match their scope_ids */
1360		return link_local ? (src_addr6->sin6_scope_id !=
1361				     dst_addr6->sin6_scope_id) :
1362				    0;
1363	}
1364
1365	default:
1366		return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1367				   &((struct sockaddr_ib *) dst)->sib_addr);
1368	}
1369}
1370
1371static __be16 cma_port(const struct sockaddr *addr)
1372{
1373	struct sockaddr_ib *sib;
1374
1375	switch (addr->sa_family) {
1376	case AF_INET:
1377		return ((struct sockaddr_in *) addr)->sin_port;
1378	case AF_INET6:
1379		return ((struct sockaddr_in6 *) addr)->sin6_port;
1380	case AF_IB:
1381		sib = (struct sockaddr_ib *) addr;
1382		return htons((u16) (be64_to_cpu(sib->sib_sid) &
1383				    be64_to_cpu(sib->sib_sid_mask)));
1384	default:
1385		return 0;
1386	}
1387}
1388
1389static inline int cma_any_port(const struct sockaddr *addr)
1390{
1391	return !cma_port(addr);
1392}
1393
1394static void cma_save_ib_info(struct sockaddr *src_addr,
1395			     struct sockaddr *dst_addr,
1396			     const struct rdma_cm_id *listen_id,
1397			     const struct sa_path_rec *path)
1398{
1399	struct sockaddr_ib *listen_ib, *ib;
1400
1401	listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
1402	if (src_addr) {
1403		ib = (struct sockaddr_ib *)src_addr;
1404		ib->sib_family = AF_IB;
1405		if (path) {
1406			ib->sib_pkey = path->pkey;
1407			ib->sib_flowinfo = path->flow_label;
1408			memcpy(&ib->sib_addr, &path->sgid, 16);
1409			ib->sib_sid = path->service_id;
1410			ib->sib_scope_id = 0;
1411		} else {
1412			ib->sib_pkey = listen_ib->sib_pkey;
1413			ib->sib_flowinfo = listen_ib->sib_flowinfo;
1414			ib->sib_addr = listen_ib->sib_addr;
1415			ib->sib_sid = listen_ib->sib_sid;
1416			ib->sib_scope_id = listen_ib->sib_scope_id;
1417		}
1418		ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
1419	}
1420	if (dst_addr) {
1421		ib = (struct sockaddr_ib *)dst_addr;
1422		ib->sib_family = AF_IB;
1423		if (path) {
1424			ib->sib_pkey = path->pkey;
1425			ib->sib_flowinfo = path->flow_label;
1426			memcpy(&ib->sib_addr, &path->dgid, 16);
1427		}
1428	}
1429}
1430
1431static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1432			      struct sockaddr_in *dst_addr,
1433			      struct cma_hdr *hdr,
1434			      __be16 local_port)
1435{
1436	if (src_addr) {
1437		*src_addr = (struct sockaddr_in) {
1438			.sin_family = AF_INET,
1439			.sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1440			.sin_port = local_port,
1441		};
1442	}
1443
1444	if (dst_addr) {
1445		*dst_addr = (struct sockaddr_in) {
1446			.sin_family = AF_INET,
1447			.sin_addr.s_addr = hdr->src_addr.ip4.addr,
1448			.sin_port = hdr->port,
1449		};
1450	}
1451}
1452
1453static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1454			      struct sockaddr_in6 *dst_addr,
1455			      struct cma_hdr *hdr,
1456			      __be16 local_port)
1457{
1458	if (src_addr) {
1459		*src_addr = (struct sockaddr_in6) {
1460			.sin6_family = AF_INET6,
1461			.sin6_addr = hdr->dst_addr.ip6,
1462			.sin6_port = local_port,
1463		};
1464	}
1465
1466	if (dst_addr) {
1467		*dst_addr = (struct sockaddr_in6) {
1468			.sin6_family = AF_INET6,
1469			.sin6_addr = hdr->src_addr.ip6,
1470			.sin6_port = hdr->port,
1471		};
1472	}
1473}
1474
1475static u16 cma_port_from_service_id(__be64 service_id)
1476{
1477	return (u16)be64_to_cpu(service_id);
1478}
1479
1480static int cma_save_ip_info(struct sockaddr *src_addr,
1481			    struct sockaddr *dst_addr,
1482			    const struct ib_cm_event *ib_event,
1483			    __be64 service_id)
1484{
1485	struct cma_hdr *hdr;
1486	__be16 port;
1487
1488	hdr = ib_event->private_data;
1489	if (hdr->cma_version != CMA_VERSION)
1490		return -EINVAL;
1491
1492	port = htons(cma_port_from_service_id(service_id));
1493
1494	switch (cma_get_ip_ver(hdr)) {
1495	case 4:
1496		cma_save_ip4_info((struct sockaddr_in *)src_addr,
1497				  (struct sockaddr_in *)dst_addr, hdr, port);
1498		break;
1499	case 6:
1500		cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1501				  (struct sockaddr_in6 *)dst_addr, hdr, port);
1502		break;
1503	default:
1504		return -EAFNOSUPPORT;
1505	}
1506
1507	return 0;
1508}
1509
1510static int cma_save_net_info(struct sockaddr *src_addr,
1511			     struct sockaddr *dst_addr,
1512			     const struct rdma_cm_id *listen_id,
1513			     const struct ib_cm_event *ib_event,
1514			     sa_family_t sa_family, __be64 service_id)
1515{
1516	if (sa_family == AF_IB) {
1517		if (ib_event->event == IB_CM_REQ_RECEIVED)
1518			cma_save_ib_info(src_addr, dst_addr, listen_id,
1519					 ib_event->param.req_rcvd.primary_path);
1520		else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1521			cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1522		return 0;
1523	}
1524
1525	return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1526}
1527
1528static int cma_save_req_info(const struct ib_cm_event *ib_event,
1529			     struct cma_req_info *req)
1530{
1531	const struct ib_cm_req_event_param *req_param =
1532		&ib_event->param.req_rcvd;
1533	const struct ib_cm_sidr_req_event_param *sidr_param =
1534		&ib_event->param.sidr_req_rcvd;
1535
1536	switch (ib_event->event) {
1537	case IB_CM_REQ_RECEIVED:
1538		req->device	= req_param->listen_id->device;
1539		req->port	= req_param->port;
1540		memcpy(&req->local_gid, &req_param->primary_path->sgid,
1541		       sizeof(req->local_gid));
1542		req->has_gid	= true;
1543		req->service_id = req_param->primary_path->service_id;
1544		req->pkey	= be16_to_cpu(req_param->primary_path->pkey);
1545		if (req->pkey != req_param->bth_pkey)
1546			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1547					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1548					    req_param->bth_pkey, req->pkey);
1549		break;
1550	case IB_CM_SIDR_REQ_RECEIVED:
1551		req->device	= sidr_param->listen_id->device;
1552		req->port	= sidr_param->port;
1553		req->has_gid	= false;
1554		req->service_id	= sidr_param->service_id;
1555		req->pkey	= sidr_param->pkey;
1556		if (req->pkey != sidr_param->bth_pkey)
1557			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1558					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1559					    sidr_param->bth_pkey, req->pkey);
1560		break;
1561	default:
1562		return -EINVAL;
1563	}
1564
1565	return 0;
1566}
1567
1568static bool validate_ipv4_net_dev(struct net_device *net_dev,
1569				  const struct sockaddr_in *dst_addr,
1570				  const struct sockaddr_in *src_addr)
1571{
1572	__be32 daddr = dst_addr->sin_addr.s_addr,
1573	       saddr = src_addr->sin_addr.s_addr;
1574	struct fib_result res;
1575	struct flowi4 fl4;
1576	int err;
1577	bool ret;
1578
1579	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1580	    ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1581	    ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1582	    ipv4_is_loopback(saddr))
1583		return false;
1584
1585	memset(&fl4, 0, sizeof(fl4));
1586	fl4.flowi4_oif = net_dev->ifindex;
1587	fl4.daddr = daddr;
1588	fl4.saddr = saddr;
1589
1590	rcu_read_lock();
1591	err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1592	ret = err == 0 && FIB_RES_DEV(res) == net_dev;
1593	rcu_read_unlock();
1594
1595	return ret;
1596}
1597
1598static bool validate_ipv6_net_dev(struct net_device *net_dev,
1599				  const struct sockaddr_in6 *dst_addr,
1600				  const struct sockaddr_in6 *src_addr)
1601{
1602#if IS_ENABLED(CONFIG_IPV6)
1603	const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1604			   IPV6_ADDR_LINKLOCAL;
1605	struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1606					 &src_addr->sin6_addr, net_dev->ifindex,
1607					 NULL, strict);
1608	bool ret;
1609
1610	if (!rt)
1611		return false;
1612
1613	ret = rt->rt6i_idev->dev == net_dev;
1614	ip6_rt_put(rt);
1615
1616	return ret;
1617#else
1618	return false;
1619#endif
1620}
1621
1622static bool validate_net_dev(struct net_device *net_dev,
1623			     const struct sockaddr *daddr,
1624			     const struct sockaddr *saddr)
1625{
1626	const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1627	const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1628	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1629	const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1630
1631	switch (daddr->sa_family) {
1632	case AF_INET:
1633		return saddr->sa_family == AF_INET &&
1634		       validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1635
1636	case AF_INET6:
1637		return saddr->sa_family == AF_INET6 &&
1638		       validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1639
1640	default:
1641		return false;
1642	}
1643}
1644
1645static struct net_device *
1646roce_get_net_dev_by_cm_event(const struct ib_cm_event *ib_event)
1647{
1648	const struct ib_gid_attr *sgid_attr = NULL;
1649	struct net_device *ndev;
1650
1651	if (ib_event->event == IB_CM_REQ_RECEIVED)
1652		sgid_attr = ib_event->param.req_rcvd.ppath_sgid_attr;
1653	else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1654		sgid_attr = ib_event->param.sidr_req_rcvd.sgid_attr;
1655
1656	if (!sgid_attr)
1657		return NULL;
1658
1659	rcu_read_lock();
1660	ndev = rdma_read_gid_attr_ndev_rcu(sgid_attr);
1661	if (IS_ERR(ndev))
1662		ndev = NULL;
1663	else
1664		dev_hold(ndev);
1665	rcu_read_unlock();
1666	return ndev;
1667}
1668
1669static struct net_device *cma_get_net_dev(const struct ib_cm_event *ib_event,
1670					  struct cma_req_info *req)
1671{
1672	struct sockaddr *listen_addr =
1673			(struct sockaddr *)&req->listen_addr_storage;
1674	struct sockaddr *src_addr = (struct sockaddr *)&req->src_addr_storage;
1675	struct net_device *net_dev;
1676	const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1677	int err;
1678
1679	err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1680			       req->service_id);
1681	if (err)
1682		return ERR_PTR(err);
1683
1684	if (rdma_protocol_roce(req->device, req->port))
1685		net_dev = roce_get_net_dev_by_cm_event(ib_event);
1686	else
1687		net_dev = ib_get_net_dev_by_params(req->device, req->port,
1688						   req->pkey,
1689						   gid, listen_addr);
1690	if (!net_dev)
1691		return ERR_PTR(-ENODEV);
1692
 
 
 
 
 
1693	return net_dev;
1694}
1695
1696static enum rdma_ucm_port_space rdma_ps_from_service_id(__be64 service_id)
1697{
1698	return (be64_to_cpu(service_id) >> 16) & 0xffff;
1699}
1700
1701static bool cma_match_private_data(struct rdma_id_private *id_priv,
1702				   const struct cma_hdr *hdr)
1703{
1704	struct sockaddr *addr = cma_src_addr(id_priv);
1705	__be32 ip4_addr;
1706	struct in6_addr ip6_addr;
1707
1708	if (cma_any_addr(addr) && !id_priv->afonly)
1709		return true;
1710
1711	switch (addr->sa_family) {
1712	case AF_INET:
1713		ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1714		if (cma_get_ip_ver(hdr) != 4)
1715			return false;
1716		if (!cma_any_addr(addr) &&
1717		    hdr->dst_addr.ip4.addr != ip4_addr)
1718			return false;
1719		break;
1720	case AF_INET6:
1721		ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1722		if (cma_get_ip_ver(hdr) != 6)
1723			return false;
1724		if (!cma_any_addr(addr) &&
1725		    memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1726			return false;
1727		break;
1728	case AF_IB:
1729		return true;
1730	default:
1731		return false;
1732	}
1733
1734	return true;
1735}
1736
1737static bool cma_protocol_roce(const struct rdma_cm_id *id)
1738{
1739	struct ib_device *device = id->device;
1740	const u32 port_num = id->port_num ?: rdma_start_port(device);
 
1741
1742	return rdma_protocol_roce(device, port_num);
1743}
1744
1745static bool cma_is_req_ipv6_ll(const struct cma_req_info *req)
1746{
1747	const struct sockaddr *daddr =
1748			(const struct sockaddr *)&req->listen_addr_storage;
1749	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1750
1751	/* Returns true if the req is for IPv6 link local */
1752	return (daddr->sa_family == AF_INET6 &&
1753		(ipv6_addr_type(&daddr6->sin6_addr) & IPV6_ADDR_LINKLOCAL));
1754}
1755
1756static bool cma_match_net_dev(const struct rdma_cm_id *id,
1757			      const struct net_device *net_dev,
1758			      const struct cma_req_info *req)
1759{
1760	const struct rdma_addr *addr = &id->route.addr;
1761
1762	if (!net_dev)
1763		/* This request is an AF_IB request */
1764		return (!id->port_num || id->port_num == req->port) &&
1765		       (addr->src_addr.ss_family == AF_IB);
1766
1767	/*
1768	 * If the request is not for IPv6 link local, allow matching
1769	 * request to any netdevice of the one or multiport rdma device.
1770	 */
1771	if (!cma_is_req_ipv6_ll(req))
1772		return true;
1773	/*
1774	 * Net namespaces must match, and if the listner is listening
1775	 * on a specific netdevice than netdevice must match as well.
1776	 */
1777	if (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1778	    (!!addr->dev_addr.bound_dev_if ==
1779	     (addr->dev_addr.bound_dev_if == net_dev->ifindex)))
1780		return true;
1781	else
1782		return false;
1783}
1784
1785static struct rdma_id_private *cma_find_listener(
1786		const struct rdma_bind_list *bind_list,
1787		const struct ib_cm_id *cm_id,
1788		const struct ib_cm_event *ib_event,
1789		const struct cma_req_info *req,
1790		const struct net_device *net_dev)
1791{
1792	struct rdma_id_private *id_priv, *id_priv_dev;
1793
1794	lockdep_assert_held(&lock);
1795
1796	if (!bind_list)
1797		return ERR_PTR(-EINVAL);
1798
1799	hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1800		if (cma_match_private_data(id_priv, ib_event->private_data)) {
1801			if (id_priv->id.device == cm_id->device &&
1802			    cma_match_net_dev(&id_priv->id, net_dev, req))
1803				return id_priv;
1804			list_for_each_entry(id_priv_dev,
1805					    &id_priv->listen_list,
1806					    listen_item) {
1807				if (id_priv_dev->id.device == cm_id->device &&
1808				    cma_match_net_dev(&id_priv_dev->id,
1809						      net_dev, req))
1810					return id_priv_dev;
1811			}
1812		}
1813	}
1814
1815	return ERR_PTR(-EINVAL);
1816}
1817
1818static struct rdma_id_private *
1819cma_ib_id_from_event(struct ib_cm_id *cm_id,
1820		     const struct ib_cm_event *ib_event,
1821		     struct cma_req_info *req,
1822		     struct net_device **net_dev)
1823{
 
1824	struct rdma_bind_list *bind_list;
1825	struct rdma_id_private *id_priv;
1826	int err;
1827
1828	err = cma_save_req_info(ib_event, req);
1829	if (err)
1830		return ERR_PTR(err);
1831
1832	*net_dev = cma_get_net_dev(ib_event, req);
1833	if (IS_ERR(*net_dev)) {
1834		if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1835			/* Assuming the protocol is AF_IB */
1836			*net_dev = NULL;
 
 
 
 
1837		} else {
1838			return ERR_CAST(*net_dev);
1839		}
1840	}
1841
1842	mutex_lock(&lock);
1843	/*
1844	 * Net namespace might be getting deleted while route lookup,
1845	 * cm_id lookup is in progress. Therefore, perform netdevice
1846	 * validation, cm_id lookup under rcu lock.
1847	 * RCU lock along with netdevice state check, synchronizes with
1848	 * netdevice migrating to different net namespace and also avoids
1849	 * case where net namespace doesn't get deleted while lookup is in
1850	 * progress.
1851	 * If the device state is not IFF_UP, its properties such as ifindex
1852	 * and nd_net cannot be trusted to remain valid without rcu lock.
1853	 * net/core/dev.c change_net_namespace() ensures to synchronize with
1854	 * ongoing operations on net device after device is closed using
1855	 * synchronize_net().
1856	 */
1857	rcu_read_lock();
1858	if (*net_dev) {
1859		/*
1860		 * If netdevice is down, it is likely that it is administratively
1861		 * down or it might be migrating to different namespace.
1862		 * In that case avoid further processing, as the net namespace
1863		 * or ifindex may change.
1864		 */
1865		if (((*net_dev)->flags & IFF_UP) == 0) {
1866			id_priv = ERR_PTR(-EHOSTUNREACH);
1867			goto err;
1868		}
1869
1870		if (!validate_net_dev(*net_dev,
1871				 (struct sockaddr *)&req->src_addr_storage,
1872				 (struct sockaddr *)&req->listen_addr_storage)) {
1873			id_priv = ERR_PTR(-EHOSTUNREACH);
1874			goto err;
1875		}
1876	}
1877
1878	bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1879				rdma_ps_from_service_id(req->service_id),
1880				cma_port_from_service_id(req->service_id));
1881	id_priv = cma_find_listener(bind_list, cm_id, ib_event, req, *net_dev);
1882err:
1883	rcu_read_unlock();
1884	mutex_unlock(&lock);
1885	if (IS_ERR(id_priv) && *net_dev) {
1886		dev_put(*net_dev);
1887		*net_dev = NULL;
1888	}
 
1889	return id_priv;
1890}
1891
1892static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv)
1893{
1894	return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1895}
1896
1897static void cma_cancel_route(struct rdma_id_private *id_priv)
1898{
1899	if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1900		if (id_priv->query)
1901			ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1902	}
1903}
1904
1905static void _cma_cancel_listens(struct rdma_id_private *id_priv)
1906{
1907	struct rdma_id_private *dev_id_priv;
1908
1909	lockdep_assert_held(&lock);
1910
1911	/*
1912	 * Remove from listen_any_list to prevent added devices from spawning
1913	 * additional listen requests.
1914	 */
1915	list_del_init(&id_priv->listen_any_item);
 
1916
1917	while (!list_empty(&id_priv->listen_list)) {
1918		dev_id_priv =
1919			list_first_entry(&id_priv->listen_list,
1920					 struct rdma_id_private, listen_item);
1921		/* sync with device removal to avoid duplicate destruction */
1922		list_del_init(&dev_id_priv->device_item);
1923		list_del_init(&dev_id_priv->listen_item);
1924		mutex_unlock(&lock);
1925
1926		rdma_destroy_id(&dev_id_priv->id);
1927		mutex_lock(&lock);
1928	}
1929}
1930
1931static void cma_cancel_listens(struct rdma_id_private *id_priv)
1932{
1933	mutex_lock(&lock);
1934	_cma_cancel_listens(id_priv);
1935	mutex_unlock(&lock);
1936}
1937
1938static void cma_cancel_operation(struct rdma_id_private *id_priv,
1939				 enum rdma_cm_state state)
1940{
1941	switch (state) {
1942	case RDMA_CM_ADDR_QUERY:
1943		/*
1944		 * We can avoid doing the rdma_addr_cancel() based on state,
1945		 * only RDMA_CM_ADDR_QUERY has a work that could still execute.
1946		 * Notice that the addr_handler work could still be exiting
1947		 * outside this state, however due to the interaction with the
1948		 * handler_mutex the work is guaranteed not to touch id_priv
1949		 * during exit.
1950		 */
1951		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1952		break;
1953	case RDMA_CM_ROUTE_QUERY:
1954		cma_cancel_route(id_priv);
1955		break;
1956	case RDMA_CM_LISTEN:
1957		if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1958			cma_cancel_listens(id_priv);
1959		break;
1960	default:
1961		break;
1962	}
1963}
1964
1965static void cma_release_port(struct rdma_id_private *id_priv)
1966{
1967	struct rdma_bind_list *bind_list = id_priv->bind_list;
1968	struct net *net = id_priv->id.route.addr.dev_addr.net;
1969
1970	if (!bind_list)
1971		return;
1972
1973	mutex_lock(&lock);
1974	hlist_del(&id_priv->node);
1975	if (hlist_empty(&bind_list->owners)) {
1976		cma_ps_remove(net, bind_list->ps, bind_list->port);
1977		kfree(bind_list);
1978	}
1979	mutex_unlock(&lock);
1980}
1981
1982static void destroy_mc(struct rdma_id_private *id_priv,
1983		       struct cma_multicast *mc)
1984{
1985	bool send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
1986
1987	if (rdma_cap_ib_mcast(id_priv->id.device, id_priv->id.port_num))
1988		ib_sa_free_multicast(mc->sa_mc);
1989
1990	if (rdma_protocol_roce(id_priv->id.device, id_priv->id.port_num)) {
1991		struct rdma_dev_addr *dev_addr =
1992			&id_priv->id.route.addr.dev_addr;
1993		struct net_device *ndev = NULL;
1994
1995		if (dev_addr->bound_dev_if)
1996			ndev = dev_get_by_index(dev_addr->net,
1997						dev_addr->bound_dev_if);
1998		if (ndev && !send_only) {
1999			enum ib_gid_type gid_type;
2000			union ib_gid mgid;
2001
2002			gid_type = id_priv->cma_dev->default_gid_type
2003					   [id_priv->id.port_num -
2004					    rdma_start_port(
2005						    id_priv->cma_dev->device)];
2006			cma_iboe_set_mgid((struct sockaddr *)&mc->addr, &mgid,
2007					  gid_type);
2008			cma_igmp_send(ndev, &mgid, false);
2009		}
2010		dev_put(ndev);
2011
2012		cancel_work_sync(&mc->iboe_join.work);
2013	}
2014	kfree(mc);
2015}
2016
2017static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
2018{
2019	struct cma_multicast *mc;
2020
2021	while (!list_empty(&id_priv->mc_list)) {
2022		mc = list_first_entry(&id_priv->mc_list, struct cma_multicast,
2023				      list);
2024		list_del(&mc->list);
2025		destroy_mc(id_priv, mc);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2026	}
2027}
2028
2029static void _destroy_id(struct rdma_id_private *id_priv,
2030			enum rdma_cm_state state)
2031{
 
 
 
 
 
2032	cma_cancel_operation(id_priv, state);
2033
2034	rdma_restrack_del(&id_priv->res);
2035	cma_remove_id_from_tree(id_priv);
 
 
 
 
 
2036	if (id_priv->cma_dev) {
2037		if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
2038			if (id_priv->cm_id.ib)
2039				ib_destroy_cm_id(id_priv->cm_id.ib);
2040		} else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
2041			if (id_priv->cm_id.iw)
2042				iw_destroy_cm_id(id_priv->cm_id.iw);
2043		}
2044		cma_leave_mc_groups(id_priv);
2045		cma_release_dev(id_priv);
2046	}
2047
2048	cma_release_port(id_priv);
2049	cma_id_put(id_priv);
2050	wait_for_completion(&id_priv->comp);
2051
2052	if (id_priv->internal_id)
2053		cma_id_put(id_priv->id.context);
2054
2055	kfree(id_priv->id.route.path_rec);
2056	kfree(id_priv->id.route.path_rec_inbound);
2057	kfree(id_priv->id.route.path_rec_outbound);
2058
2059	put_net(id_priv->id.route.addr.dev_addr.net);
2060	kfree(id_priv);
2061}
2062
2063/*
2064 * destroy an ID from within the handler_mutex. This ensures that no other
2065 * handlers can start running concurrently.
2066 */
2067static void destroy_id_handler_unlock(struct rdma_id_private *id_priv)
2068	__releases(&idprv->handler_mutex)
2069{
2070	enum rdma_cm_state state;
2071	unsigned long flags;
2072
2073	trace_cm_id_destroy(id_priv);
2074
2075	/*
2076	 * Setting the state to destroyed under the handler mutex provides a
2077	 * fence against calling handler callbacks. If this is invoked due to
2078	 * the failure of a handler callback then it guarentees that no future
2079	 * handlers will be called.
2080	 */
2081	lockdep_assert_held(&id_priv->handler_mutex);
2082	spin_lock_irqsave(&id_priv->lock, flags);
2083	state = id_priv->state;
2084	id_priv->state = RDMA_CM_DESTROYING;
2085	spin_unlock_irqrestore(&id_priv->lock, flags);
2086	mutex_unlock(&id_priv->handler_mutex);
2087	_destroy_id(id_priv, state);
2088}
2089
2090void rdma_destroy_id(struct rdma_cm_id *id)
2091{
2092	struct rdma_id_private *id_priv =
2093		container_of(id, struct rdma_id_private, id);
2094
2095	mutex_lock(&id_priv->handler_mutex);
2096	destroy_id_handler_unlock(id_priv);
2097}
2098EXPORT_SYMBOL(rdma_destroy_id);
2099
2100static int cma_rep_recv(struct rdma_id_private *id_priv)
2101{
2102	int ret;
2103
2104	ret = cma_modify_qp_rtr(id_priv, NULL);
2105	if (ret)
2106		goto reject;
2107
2108	ret = cma_modify_qp_rts(id_priv, NULL);
2109	if (ret)
2110		goto reject;
2111
2112	trace_cm_send_rtu(id_priv);
2113	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
2114	if (ret)
2115		goto reject;
2116
2117	return 0;
2118reject:
2119	pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret);
2120	cma_modify_qp_err(id_priv);
2121	trace_cm_send_rej(id_priv);
2122	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
2123		       NULL, 0, NULL, 0);
2124	return ret;
2125}
2126
2127static void cma_set_rep_event_data(struct rdma_cm_event *event,
2128				   const struct ib_cm_rep_event_param *rep_data,
2129				   void *private_data)
2130{
2131	event->param.conn.private_data = private_data;
2132	event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
2133	event->param.conn.responder_resources = rep_data->responder_resources;
2134	event->param.conn.initiator_depth = rep_data->initiator_depth;
2135	event->param.conn.flow_control = rep_data->flow_control;
2136	event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
2137	event->param.conn.srq = rep_data->srq;
2138	event->param.conn.qp_num = rep_data->remote_qpn;
2139
2140	event->ece.vendor_id = rep_data->ece.vendor_id;
2141	event->ece.attr_mod = rep_data->ece.attr_mod;
2142}
2143
2144static int cma_cm_event_handler(struct rdma_id_private *id_priv,
2145				struct rdma_cm_event *event)
2146{
2147	int ret;
2148
2149	lockdep_assert_held(&id_priv->handler_mutex);
2150
2151	trace_cm_event_handler(id_priv, event);
2152	ret = id_priv->id.event_handler(&id_priv->id, event);
2153	trace_cm_event_done(id_priv, event, ret);
2154	return ret;
2155}
2156
2157static int cma_ib_handler(struct ib_cm_id *cm_id,
2158			  const struct ib_cm_event *ib_event)
2159{
2160	struct rdma_id_private *id_priv = cm_id->context;
2161	struct rdma_cm_event event = {};
2162	enum rdma_cm_state state;
2163	int ret;
2164
2165	mutex_lock(&id_priv->handler_mutex);
2166	state = READ_ONCE(id_priv->state);
2167	if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
2168	     state != RDMA_CM_CONNECT) ||
2169	    (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
2170	     state != RDMA_CM_DISCONNECT))
2171		goto out;
2172
 
2173	switch (ib_event->event) {
2174	case IB_CM_REQ_ERROR:
2175	case IB_CM_REP_ERROR:
2176		event.event = RDMA_CM_EVENT_UNREACHABLE;
2177		event.status = -ETIMEDOUT;
2178		break;
2179	case IB_CM_REP_RECEIVED:
2180		if (state == RDMA_CM_CONNECT &&
2181		    (id_priv->id.qp_type != IB_QPT_UD)) {
2182			trace_cm_send_mra(id_priv);
2183			ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2184		}
2185		if (id_priv->id.qp) {
2186			event.status = cma_rep_recv(id_priv);
2187			event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
2188						     RDMA_CM_EVENT_ESTABLISHED;
2189		} else {
2190			event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
2191		}
2192		cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
2193				       ib_event->private_data);
2194		break;
2195	case IB_CM_RTU_RECEIVED:
2196	case IB_CM_USER_ESTABLISHED:
2197		event.event = RDMA_CM_EVENT_ESTABLISHED;
2198		break;
2199	case IB_CM_DREQ_ERROR:
2200		event.status = -ETIMEDOUT;
2201		fallthrough;
2202	case IB_CM_DREQ_RECEIVED:
2203	case IB_CM_DREP_RECEIVED:
2204		if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
2205				   RDMA_CM_DISCONNECT))
2206			goto out;
2207		event.event = RDMA_CM_EVENT_DISCONNECTED;
2208		break;
2209	case IB_CM_TIMEWAIT_EXIT:
2210		event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
2211		break;
2212	case IB_CM_MRA_RECEIVED:
2213		/* ignore event */
2214		goto out;
2215	case IB_CM_REJ_RECEIVED:
2216		pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id,
2217										ib_event->param.rej_rcvd.reason));
2218		cma_modify_qp_err(id_priv);
2219		event.status = ib_event->param.rej_rcvd.reason;
2220		event.event = RDMA_CM_EVENT_REJECTED;
2221		event.param.conn.private_data = ib_event->private_data;
2222		event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
2223		break;
2224	default:
2225		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
2226		       ib_event->event);
2227		goto out;
2228	}
2229
2230	ret = cma_cm_event_handler(id_priv, &event);
2231	if (ret) {
2232		/* Destroy the CM ID by returning a non-zero value. */
2233		id_priv->cm_id.ib = NULL;
2234		destroy_id_handler_unlock(id_priv);
 
 
2235		return ret;
2236	}
2237out:
2238	mutex_unlock(&id_priv->handler_mutex);
2239	return 0;
2240}
2241
2242static struct rdma_id_private *
2243cma_ib_new_conn_id(const struct rdma_cm_id *listen_id,
2244		   const struct ib_cm_event *ib_event,
2245		   struct net_device *net_dev)
2246{
2247	struct rdma_id_private *listen_id_priv;
2248	struct rdma_id_private *id_priv;
2249	struct rdma_cm_id *id;
2250	struct rdma_route *rt;
2251	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2252	struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path;
2253	const __be64 service_id =
2254		ib_event->param.req_rcvd.primary_path->service_id;
2255	int ret;
2256
2257	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2258	id_priv = __rdma_create_id(listen_id->route.addr.dev_addr.net,
2259				   listen_id->event_handler, listen_id->context,
2260				   listen_id->ps,
2261				   ib_event->param.req_rcvd.qp_type,
2262				   listen_id_priv);
2263	if (IS_ERR(id_priv))
2264		return NULL;
2265
2266	id = &id_priv->id;
2267	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2268			      (struct sockaddr *)&id->route.addr.dst_addr,
2269			      listen_id, ib_event, ss_family, service_id))
2270		goto err;
2271
2272	rt = &id->route;
2273	rt->num_pri_alt_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
2274	rt->path_rec = kmalloc_array(rt->num_pri_alt_paths,
2275				     sizeof(*rt->path_rec), GFP_KERNEL);
2276	if (!rt->path_rec)
2277		goto err;
2278
2279	rt->path_rec[0] = *path;
2280	if (rt->num_pri_alt_paths == 2)
2281		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
2282
2283	if (net_dev) {
2284		rdma_copy_src_l2_addr(&rt->addr.dev_addr, net_dev);
 
 
2285	} else {
2286		if (!cma_protocol_roce(listen_id) &&
2287		    cma_any_addr(cma_src_addr(id_priv))) {
2288			rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
2289			rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
2290			ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
2291		} else if (!cma_any_addr(cma_src_addr(id_priv))) {
2292			ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
2293			if (ret)
2294				goto err;
2295		}
2296	}
2297	rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
2298
2299	id_priv->state = RDMA_CM_CONNECT;
2300	return id_priv;
2301
2302err:
2303	rdma_destroy_id(id);
2304	return NULL;
2305}
2306
2307static struct rdma_id_private *
2308cma_ib_new_udp_id(const struct rdma_cm_id *listen_id,
2309		  const struct ib_cm_event *ib_event,
2310		  struct net_device *net_dev)
2311{
2312	const struct rdma_id_private *listen_id_priv;
2313	struct rdma_id_private *id_priv;
2314	struct rdma_cm_id *id;
2315	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2316	struct net *net = listen_id->route.addr.dev_addr.net;
2317	int ret;
2318
2319	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2320	id_priv = __rdma_create_id(net, listen_id->event_handler,
2321				   listen_id->context, listen_id->ps, IB_QPT_UD,
2322				   listen_id_priv);
2323	if (IS_ERR(id_priv))
2324		return NULL;
2325
2326	id = &id_priv->id;
2327	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2328			      (struct sockaddr *)&id->route.addr.dst_addr,
2329			      listen_id, ib_event, ss_family,
2330			      ib_event->param.sidr_req_rcvd.service_id))
2331		goto err;
2332
2333	if (net_dev) {
2334		rdma_copy_src_l2_addr(&id->route.addr.dev_addr, net_dev);
 
 
2335	} else {
2336		if (!cma_any_addr(cma_src_addr(id_priv))) {
2337			ret = cma_translate_addr(cma_src_addr(id_priv),
2338						 &id->route.addr.dev_addr);
2339			if (ret)
2340				goto err;
2341		}
2342	}
2343
2344	id_priv->state = RDMA_CM_CONNECT;
2345	return id_priv;
2346err:
2347	rdma_destroy_id(id);
2348	return NULL;
2349}
2350
2351static void cma_set_req_event_data(struct rdma_cm_event *event,
2352				   const struct ib_cm_req_event_param *req_data,
2353				   void *private_data, int offset)
2354{
2355	event->param.conn.private_data = private_data + offset;
2356	event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
2357	event->param.conn.responder_resources = req_data->responder_resources;
2358	event->param.conn.initiator_depth = req_data->initiator_depth;
2359	event->param.conn.flow_control = req_data->flow_control;
2360	event->param.conn.retry_count = req_data->retry_count;
2361	event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
2362	event->param.conn.srq = req_data->srq;
2363	event->param.conn.qp_num = req_data->remote_qpn;
2364
2365	event->ece.vendor_id = req_data->ece.vendor_id;
2366	event->ece.attr_mod = req_data->ece.attr_mod;
2367}
2368
2369static int cma_ib_check_req_qp_type(const struct rdma_cm_id *id,
2370				    const struct ib_cm_event *ib_event)
2371{
2372	return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
2373		 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
2374		((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
2375		 (id->qp_type == IB_QPT_UD)) ||
2376		(!id->qp_type));
2377}
2378
2379static int cma_ib_req_handler(struct ib_cm_id *cm_id,
2380			      const struct ib_cm_event *ib_event)
2381{
2382	struct rdma_id_private *listen_id, *conn_id = NULL;
2383	struct rdma_cm_event event = {};
2384	struct cma_req_info req = {};
2385	struct net_device *net_dev;
2386	u8 offset;
2387	int ret;
2388
2389	listen_id = cma_ib_id_from_event(cm_id, ib_event, &req, &net_dev);
2390	if (IS_ERR(listen_id))
2391		return PTR_ERR(listen_id);
2392
2393	trace_cm_req_handler(listen_id, ib_event->event);
2394	if (!cma_ib_check_req_qp_type(&listen_id->id, ib_event)) {
2395		ret = -EINVAL;
2396		goto net_dev_put;
2397	}
2398
2399	mutex_lock(&listen_id->handler_mutex);
2400	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN) {
2401		ret = -ECONNABORTED;
2402		goto err_unlock;
2403	}
2404
 
2405	offset = cma_user_data_offset(listen_id);
2406	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2407	if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
2408		conn_id = cma_ib_new_udp_id(&listen_id->id, ib_event, net_dev);
2409		event.param.ud.private_data = ib_event->private_data + offset;
2410		event.param.ud.private_data_len =
2411				IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
2412	} else {
2413		conn_id = cma_ib_new_conn_id(&listen_id->id, ib_event, net_dev);
2414		cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
2415				       ib_event->private_data, offset);
2416	}
2417	if (!conn_id) {
2418		ret = -ENOMEM;
2419		goto err_unlock;
2420	}
2421
2422	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2423	ret = cma_ib_acquire_dev(conn_id, listen_id, &req);
2424	if (ret) {
2425		destroy_id_handler_unlock(conn_id);
2426		goto err_unlock;
2427	}
2428
2429	conn_id->cm_id.ib = cm_id;
2430	cm_id->context = conn_id;
2431	cm_id->cm_handler = cma_ib_handler;
2432
2433	ret = cma_cm_event_handler(conn_id, &event);
2434	if (ret) {
2435		/* Destroy the CM ID by returning a non-zero value. */
2436		conn_id->cm_id.ib = NULL;
2437		mutex_unlock(&listen_id->handler_mutex);
2438		destroy_id_handler_unlock(conn_id);
2439		goto net_dev_put;
2440	}
2441
2442	if (READ_ONCE(conn_id->state) == RDMA_CM_CONNECT &&
2443	    conn_id->id.qp_type != IB_QPT_UD) {
2444		trace_cm_send_mra(cm_id->context);
 
 
 
2445		ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2446	}
2447	mutex_unlock(&conn_id->handler_mutex);
 
 
 
 
 
2448
2449err_unlock:
 
 
 
 
 
 
 
2450	mutex_unlock(&listen_id->handler_mutex);
 
 
2451
2452net_dev_put:
2453	dev_put(net_dev);
 
2454
2455	return ret;
2456}
2457
2458__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2459{
2460	if (addr->sa_family == AF_IB)
2461		return ((struct sockaddr_ib *) addr)->sib_sid;
2462
2463	return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2464}
2465EXPORT_SYMBOL(rdma_get_service_id);
2466
2467void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid,
2468		    union ib_gid *dgid)
2469{
2470	struct rdma_addr *addr = &cm_id->route.addr;
2471
2472	if (!cm_id->device) {
2473		if (sgid)
2474			memset(sgid, 0, sizeof(*sgid));
2475		if (dgid)
2476			memset(dgid, 0, sizeof(*dgid));
2477		return;
2478	}
2479
2480	if (rdma_protocol_roce(cm_id->device, cm_id->port_num)) {
2481		if (sgid)
2482			rdma_ip2gid((struct sockaddr *)&addr->src_addr, sgid);
2483		if (dgid)
2484			rdma_ip2gid((struct sockaddr *)&addr->dst_addr, dgid);
2485	} else {
2486		if (sgid)
2487			rdma_addr_get_sgid(&addr->dev_addr, sgid);
2488		if (dgid)
2489			rdma_addr_get_dgid(&addr->dev_addr, dgid);
2490	}
2491}
2492EXPORT_SYMBOL(rdma_read_gids);
2493
2494static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2495{
2496	struct rdma_id_private *id_priv = iw_id->context;
2497	struct rdma_cm_event event = {};
2498	int ret = 0;
2499	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2500	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2501
2502	mutex_lock(&id_priv->handler_mutex);
2503	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
2504		goto out;
2505
 
2506	switch (iw_event->event) {
2507	case IW_CM_EVENT_CLOSE:
2508		event.event = RDMA_CM_EVENT_DISCONNECTED;
2509		break;
2510	case IW_CM_EVENT_CONNECT_REPLY:
2511		memcpy(cma_src_addr(id_priv), laddr,
2512		       rdma_addr_size(laddr));
2513		memcpy(cma_dst_addr(id_priv), raddr,
2514		       rdma_addr_size(raddr));
2515		switch (iw_event->status) {
2516		case 0:
2517			event.event = RDMA_CM_EVENT_ESTABLISHED;
2518			event.param.conn.initiator_depth = iw_event->ird;
2519			event.param.conn.responder_resources = iw_event->ord;
2520			break;
2521		case -ECONNRESET:
2522		case -ECONNREFUSED:
2523			event.event = RDMA_CM_EVENT_REJECTED;
2524			break;
2525		case -ETIMEDOUT:
2526			event.event = RDMA_CM_EVENT_UNREACHABLE;
2527			break;
2528		default:
2529			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2530			break;
2531		}
2532		break;
2533	case IW_CM_EVENT_ESTABLISHED:
2534		event.event = RDMA_CM_EVENT_ESTABLISHED;
2535		event.param.conn.initiator_depth = iw_event->ird;
2536		event.param.conn.responder_resources = iw_event->ord;
2537		break;
2538	default:
2539		goto out;
2540	}
2541
2542	event.status = iw_event->status;
2543	event.param.conn.private_data = iw_event->private_data;
2544	event.param.conn.private_data_len = iw_event->private_data_len;
2545	ret = cma_cm_event_handler(id_priv, &event);
2546	if (ret) {
2547		/* Destroy the CM ID by returning a non-zero value. */
2548		id_priv->cm_id.iw = NULL;
2549		destroy_id_handler_unlock(id_priv);
 
 
2550		return ret;
2551	}
2552
2553out:
2554	mutex_unlock(&id_priv->handler_mutex);
2555	return ret;
2556}
2557
2558static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2559			       struct iw_cm_event *iw_event)
2560{
 
2561	struct rdma_id_private *listen_id, *conn_id;
2562	struct rdma_cm_event event = {};
2563	int ret = -ECONNABORTED;
2564	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2565	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2566
2567	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2568	event.param.conn.private_data = iw_event->private_data;
2569	event.param.conn.private_data_len = iw_event->private_data_len;
2570	event.param.conn.initiator_depth = iw_event->ird;
2571	event.param.conn.responder_resources = iw_event->ord;
2572
2573	listen_id = cm_id->context;
2574
2575	mutex_lock(&listen_id->handler_mutex);
2576	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN)
2577		goto out;
2578
2579	/* Create a new RDMA id for the new IW CM ID */
2580	conn_id = __rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2581				   listen_id->id.event_handler,
2582				   listen_id->id.context, RDMA_PS_TCP,
2583				   IB_QPT_RC, listen_id);
2584	if (IS_ERR(conn_id)) {
2585		ret = -ENOMEM;
2586		goto out;
2587	}
 
2588	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2589	conn_id->state = RDMA_CM_CONNECT;
2590
2591	ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr);
2592	if (ret) {
2593		mutex_unlock(&listen_id->handler_mutex);
2594		destroy_id_handler_unlock(conn_id);
2595		return ret;
2596	}
2597
2598	ret = cma_iw_acquire_dev(conn_id, listen_id);
2599	if (ret) {
2600		mutex_unlock(&listen_id->handler_mutex);
2601		destroy_id_handler_unlock(conn_id);
2602		return ret;
2603	}
2604
2605	conn_id->cm_id.iw = cm_id;
2606	cm_id->context = conn_id;
2607	cm_id->cm_handler = cma_iw_handler;
2608
2609	memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2610	memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2611
2612	ret = cma_cm_event_handler(conn_id, &event);
 
 
 
 
 
 
 
 
 
 
 
 
2613	if (ret) {
2614		/* User wants to destroy the CM ID */
2615		conn_id->cm_id.iw = NULL;
2616		mutex_unlock(&listen_id->handler_mutex);
2617		destroy_id_handler_unlock(conn_id);
2618		return ret;
 
 
2619	}
2620
2621	mutex_unlock(&conn_id->handler_mutex);
 
2622
2623out:
2624	mutex_unlock(&listen_id->handler_mutex);
2625	return ret;
2626}
2627
2628static int cma_ib_listen(struct rdma_id_private *id_priv)
2629{
2630	struct sockaddr *addr;
2631	struct ib_cm_id	*id;
2632	__be64 svc_id;
2633
2634	addr = cma_src_addr(id_priv);
2635	svc_id = rdma_get_service_id(&id_priv->id, addr);
2636	id = ib_cm_insert_listen(id_priv->id.device,
2637				 cma_ib_req_handler, svc_id);
2638	if (IS_ERR(id))
2639		return PTR_ERR(id);
2640	id_priv->cm_id.ib = id;
2641
2642	return 0;
2643}
2644
2645static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2646{
2647	int ret;
2648	struct iw_cm_id	*id;
2649
2650	id = iw_create_cm_id(id_priv->id.device,
2651			     iw_conn_req_handler,
2652			     id_priv);
2653	if (IS_ERR(id))
2654		return PTR_ERR(id);
2655
2656	mutex_lock(&id_priv->qp_mutex);
2657	id->tos = id_priv->tos;
2658	id->tos_set = id_priv->tos_set;
2659	mutex_unlock(&id_priv->qp_mutex);
2660	id->afonly = id_priv->afonly;
2661	id_priv->cm_id.iw = id;
2662
2663	memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2664	       rdma_addr_size(cma_src_addr(id_priv)));
2665
2666	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2667
2668	if (ret) {
2669		iw_destroy_cm_id(id_priv->cm_id.iw);
2670		id_priv->cm_id.iw = NULL;
2671	}
2672
2673	return ret;
2674}
2675
2676static int cma_listen_handler(struct rdma_cm_id *id,
2677			      struct rdma_cm_event *event)
2678{
2679	struct rdma_id_private *id_priv = id->context;
2680
2681	/* Listening IDs are always destroyed on removal */
2682	if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
2683		return -1;
2684
2685	id->context = id_priv->id.context;
2686	id->event_handler = id_priv->id.event_handler;
2687	trace_cm_event_handler(id_priv, event);
2688	return id_priv->id.event_handler(id, event);
2689}
2690
2691static int cma_listen_on_dev(struct rdma_id_private *id_priv,
2692			     struct cma_device *cma_dev,
2693			     struct rdma_id_private **to_destroy)
2694{
2695	struct rdma_id_private *dev_id_priv;
 
2696	struct net *net = id_priv->id.route.addr.dev_addr.net;
2697	int ret;
2698
2699	lockdep_assert_held(&lock);
2700
2701	*to_destroy = NULL;
2702	if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2703		return 0;
 
 
 
 
 
2704
2705	dev_id_priv =
2706		__rdma_create_id(net, cma_listen_handler, id_priv,
2707				 id_priv->id.ps, id_priv->id.qp_type, id_priv);
2708	if (IS_ERR(dev_id_priv))
2709		return PTR_ERR(dev_id_priv);
2710
2711	dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2712	memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2713	       rdma_addr_size(cma_src_addr(id_priv)));
2714
2715	_cma_attach_to_dev(dev_id_priv, cma_dev);
2716	rdma_restrack_add(&dev_id_priv->res);
2717	cma_id_get(id_priv);
2718	dev_id_priv->internal_id = 1;
2719	dev_id_priv->afonly = id_priv->afonly;
2720	mutex_lock(&id_priv->qp_mutex);
2721	dev_id_priv->tos_set = id_priv->tos_set;
2722	dev_id_priv->tos = id_priv->tos;
2723	mutex_unlock(&id_priv->qp_mutex);
2724
2725	ret = rdma_listen(&dev_id_priv->id, id_priv->backlog);
2726	if (ret)
2727		goto err_listen;
2728	list_add_tail(&dev_id_priv->listen_item, &id_priv->listen_list);
2729	return 0;
2730err_listen:
2731	/* Caller must destroy this after releasing lock */
2732	*to_destroy = dev_id_priv;
2733	dev_warn(&cma_dev->device->dev, "RDMA CMA: %s, error %d\n", __func__, ret);
2734	return ret;
2735}
2736
2737static int cma_listen_on_all(struct rdma_id_private *id_priv)
2738{
2739	struct rdma_id_private *to_destroy;
2740	struct cma_device *cma_dev;
2741	int ret;
2742
2743	mutex_lock(&lock);
2744	list_add_tail(&id_priv->listen_any_item, &listen_any_list);
2745	list_for_each_entry(cma_dev, &dev_list, list) {
2746		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
2747		if (ret) {
2748			/* Prevent racing with cma_process_remove() */
2749			if (to_destroy)
2750				list_del_init(&to_destroy->device_item);
2751			goto err_listen;
2752		}
2753	}
2754	mutex_unlock(&lock);
2755	return 0;
2756
2757err_listen:
2758	_cma_cancel_listens(id_priv);
2759	mutex_unlock(&lock);
2760	if (to_destroy)
2761		rdma_destroy_id(&to_destroy->id);
2762	return ret;
2763}
2764
2765void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2766{
2767	struct rdma_id_private *id_priv;
2768
2769	id_priv = container_of(id, struct rdma_id_private, id);
2770	mutex_lock(&id_priv->qp_mutex);
2771	id_priv->tos = (u8) tos;
2772	id_priv->tos_set = true;
2773	mutex_unlock(&id_priv->qp_mutex);
2774}
2775EXPORT_SYMBOL(rdma_set_service_type);
2776
2777/**
2778 * rdma_set_ack_timeout() - Set the ack timeout of QP associated
2779 *                          with a connection identifier.
2780 * @id: Communication identifier to associated with service type.
2781 * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
2782 *
2783 * This function should be called before rdma_connect() on active side,
2784 * and on passive side before rdma_accept(). It is applicable to primary
2785 * path only. The timeout will affect the local side of the QP, it is not
2786 * negotiated with remote side and zero disables the timer. In case it is
2787 * set before rdma_resolve_route, the value will also be used to determine
2788 * PacketLifeTime for RoCE.
2789 *
2790 * Return: 0 for success
2791 */
2792int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout)
2793{
2794	struct rdma_id_private *id_priv;
2795
2796	if (id->qp_type != IB_QPT_RC && id->qp_type != IB_QPT_XRC_INI)
2797		return -EINVAL;
2798
2799	id_priv = container_of(id, struct rdma_id_private, id);
2800	mutex_lock(&id_priv->qp_mutex);
2801	id_priv->timeout = timeout;
2802	id_priv->timeout_set = true;
2803	mutex_unlock(&id_priv->qp_mutex);
2804
2805	return 0;
2806}
2807EXPORT_SYMBOL(rdma_set_ack_timeout);
2808
2809/**
2810 * rdma_set_min_rnr_timer() - Set the minimum RNR Retry timer of the
2811 *			      QP associated with a connection identifier.
2812 * @id: Communication identifier to associated with service type.
2813 * @min_rnr_timer: 5-bit value encoded as Table 45: "Encoding for RNR NAK
2814 *		   Timer Field" in the IBTA specification.
2815 *
2816 * This function should be called before rdma_connect() on active
2817 * side, and on passive side before rdma_accept(). The timer value
2818 * will be associated with the local QP. When it receives a send it is
2819 * not read to handle, typically if the receive queue is empty, an RNR
2820 * Retry NAK is returned to the requester with the min_rnr_timer
2821 * encoded. The requester will then wait at least the time specified
2822 * in the NAK before retrying. The default is zero, which translates
2823 * to a minimum RNR Timer value of 655 ms.
2824 *
2825 * Return: 0 for success
2826 */
2827int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer)
2828{
2829	struct rdma_id_private *id_priv;
2830
2831	/* It is a five-bit value */
2832	if (min_rnr_timer & 0xe0)
2833		return -EINVAL;
2834
2835	if (WARN_ON(id->qp_type != IB_QPT_RC && id->qp_type != IB_QPT_XRC_TGT))
2836		return -EINVAL;
2837
2838	id_priv = container_of(id, struct rdma_id_private, id);
2839	mutex_lock(&id_priv->qp_mutex);
2840	id_priv->min_rnr_timer = min_rnr_timer;
2841	id_priv->min_rnr_timer_set = true;
2842	mutex_unlock(&id_priv->qp_mutex);
2843
2844	return 0;
2845}
2846EXPORT_SYMBOL(rdma_set_min_rnr_timer);
2847
2848static int route_set_path_rec_inbound(struct cma_work *work,
2849				      struct sa_path_rec *path_rec)
2850{
2851	struct rdma_route *route = &work->id->id.route;
2852
2853	if (!route->path_rec_inbound) {
2854		route->path_rec_inbound =
2855			kzalloc(sizeof(*route->path_rec_inbound), GFP_KERNEL);
2856		if (!route->path_rec_inbound)
2857			return -ENOMEM;
2858	}
2859
2860	*route->path_rec_inbound = *path_rec;
2861	return 0;
2862}
2863
2864static int route_set_path_rec_outbound(struct cma_work *work,
2865				       struct sa_path_rec *path_rec)
2866{
2867	struct rdma_route *route = &work->id->id.route;
2868
2869	if (!route->path_rec_outbound) {
2870		route->path_rec_outbound =
2871			kzalloc(sizeof(*route->path_rec_outbound), GFP_KERNEL);
2872		if (!route->path_rec_outbound)
2873			return -ENOMEM;
2874	}
2875
2876	*route->path_rec_outbound = *path_rec;
2877	return 0;
2878}
2879
2880static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2881			      unsigned int num_prs, void *context)
2882{
2883	struct cma_work *work = context;
2884	struct rdma_route *route;
2885	int i;
2886
2887	route = &work->id->id.route;
2888
2889	if (status)
2890		goto fail;
2891
2892	for (i = 0; i < num_prs; i++) {
2893		if (!path_rec[i].flags || (path_rec[i].flags & IB_PATH_GMP))
2894			*route->path_rec = path_rec[i];
2895		else if (path_rec[i].flags & IB_PATH_INBOUND)
2896			status = route_set_path_rec_inbound(work, &path_rec[i]);
2897		else if (path_rec[i].flags & IB_PATH_OUTBOUND)
2898			status = route_set_path_rec_outbound(work,
2899							     &path_rec[i]);
2900		else
2901			status = -EINVAL;
2902
2903		if (status)
2904			goto fail;
2905	}
2906
2907	route->num_pri_alt_paths = 1;
2908	queue_work(cma_wq, &work->work);
2909	return;
2910
2911fail:
2912	work->old_state = RDMA_CM_ROUTE_QUERY;
2913	work->new_state = RDMA_CM_ADDR_RESOLVED;
2914	work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2915	work->event.status = status;
2916	pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2917			     status);
2918	queue_work(cma_wq, &work->work);
2919}
2920
2921static int cma_query_ib_route(struct rdma_id_private *id_priv,
2922			      unsigned long timeout_ms, struct cma_work *work)
2923{
2924	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2925	struct sa_path_rec path_rec;
2926	ib_sa_comp_mask comp_mask;
2927	struct sockaddr_in6 *sin6;
2928	struct sockaddr_ib *sib;
2929
2930	memset(&path_rec, 0, sizeof path_rec);
2931
2932	if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
2933		path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
2934	else
2935		path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2936	rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2937	rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2938	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2939	path_rec.numb_path = 1;
2940	path_rec.reversible = 1;
2941	path_rec.service_id = rdma_get_service_id(&id_priv->id,
2942						  cma_dst_addr(id_priv));
2943
2944	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2945		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2946		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2947
2948	switch (cma_family(id_priv)) {
2949	case AF_INET:
2950		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2951		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2952		break;
2953	case AF_INET6:
2954		sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2955		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2956		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2957		break;
2958	case AF_IB:
2959		sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2960		path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2961		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2962		break;
2963	}
2964
2965	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2966					       id_priv->id.port_num, &path_rec,
2967					       comp_mask, timeout_ms,
2968					       GFP_KERNEL, cma_query_handler,
2969					       work, &id_priv->query);
2970
2971	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2972}
2973
2974static void cma_iboe_join_work_handler(struct work_struct *work)
2975{
2976	struct cma_multicast *mc =
2977		container_of(work, struct cma_multicast, iboe_join.work);
2978	struct rdma_cm_event *event = &mc->iboe_join.event;
2979	struct rdma_id_private *id_priv = mc->id_priv;
2980	int ret;
2981
2982	mutex_lock(&id_priv->handler_mutex);
2983	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2984	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2985		goto out_unlock;
2986
2987	ret = cma_cm_event_handler(id_priv, event);
2988	WARN_ON(ret);
2989
2990out_unlock:
2991	mutex_unlock(&id_priv->handler_mutex);
2992	if (event->event == RDMA_CM_EVENT_MULTICAST_JOIN)
2993		rdma_destroy_ah_attr(&event->param.ud.ah_attr);
2994}
2995
2996static void cma_work_handler(struct work_struct *_work)
2997{
2998	struct cma_work *work = container_of(_work, struct cma_work, work);
2999	struct rdma_id_private *id_priv = work->id;
 
3000
3001	mutex_lock(&id_priv->handler_mutex);
3002	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
3003	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
3004		goto out_unlock;
3005	if (work->old_state != 0 || work->new_state != 0) {
3006		if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
3007			goto out_unlock;
3008	}
3009
3010	if (cma_cm_event_handler(id_priv, &work->event)) {
3011		cma_id_put(id_priv);
3012		destroy_id_handler_unlock(id_priv);
3013		goto out_free;
3014	}
3015
3016out_unlock:
3017	mutex_unlock(&id_priv->handler_mutex);
3018	cma_id_put(id_priv);
3019out_free:
3020	if (work->event.event == RDMA_CM_EVENT_MULTICAST_JOIN)
3021		rdma_destroy_ah_attr(&work->event.param.ud.ah_attr);
3022	kfree(work);
3023}
3024
3025static void cma_init_resolve_route_work(struct cma_work *work,
3026					struct rdma_id_private *id_priv)
3027{
3028	work->id = id_priv;
3029	INIT_WORK(&work->work, cma_work_handler);
3030	work->old_state = RDMA_CM_ROUTE_QUERY;
3031	work->new_state = RDMA_CM_ROUTE_RESOLVED;
3032	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
3033}
3034
3035static void enqueue_resolve_addr_work(struct cma_work *work,
3036				      struct rdma_id_private *id_priv)
3037{
3038	/* Balances with cma_id_put() in cma_work_handler */
3039	cma_id_get(id_priv);
3040
3041	work->id = id_priv;
3042	INIT_WORK(&work->work, cma_work_handler);
3043	work->old_state = RDMA_CM_ADDR_QUERY;
3044	work->new_state = RDMA_CM_ADDR_RESOLVED;
3045	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3046
3047	queue_work(cma_wq, &work->work);
 
 
 
 
 
3048}
3049
3050static int cma_resolve_ib_route(struct rdma_id_private *id_priv,
3051				unsigned long timeout_ms)
3052{
3053	struct rdma_route *route = &id_priv->id.route;
3054	struct cma_work *work;
3055	int ret;
3056
3057	work = kzalloc(sizeof *work, GFP_KERNEL);
3058	if (!work)
3059		return -ENOMEM;
3060
3061	cma_init_resolve_route_work(work, id_priv);
 
 
 
 
3062
3063	if (!route->path_rec)
3064		route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
3065	if (!route->path_rec) {
3066		ret = -ENOMEM;
3067		goto err1;
3068	}
3069
3070	ret = cma_query_ib_route(id_priv, timeout_ms, work);
3071	if (ret)
3072		goto err2;
3073
3074	return 0;
3075err2:
3076	kfree(route->path_rec);
3077	route->path_rec = NULL;
3078err1:
3079	kfree(work);
3080	return ret;
3081}
3082
3083static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
3084					   unsigned long supported_gids,
3085					   enum ib_gid_type default_gid)
3086{
3087	if ((network_type == RDMA_NETWORK_IPV4 ||
3088	     network_type == RDMA_NETWORK_IPV6) &&
3089	    test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
3090		return IB_GID_TYPE_ROCE_UDP_ENCAP;
3091
3092	return default_gid;
3093}
3094
3095/*
3096 * cma_iboe_set_path_rec_l2_fields() is helper function which sets
3097 * path record type based on GID type.
3098 * It also sets up other L2 fields which includes destination mac address
3099 * netdev ifindex, of the path record.
3100 * It returns the netdev of the bound interface for this path record entry.
3101 */
3102static struct net_device *
3103cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv)
3104{
3105	struct rdma_route *route = &id_priv->id.route;
3106	enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
3107	struct rdma_addr *addr = &route->addr;
3108	unsigned long supported_gids;
3109	struct net_device *ndev;
3110
3111	if (!addr->dev_addr.bound_dev_if)
3112		return NULL;
3113
3114	ndev = dev_get_by_index(addr->dev_addr.net,
3115				addr->dev_addr.bound_dev_if);
3116	if (!ndev)
3117		return NULL;
3118
3119	supported_gids = roce_gid_type_mask_support(id_priv->id.device,
3120						    id_priv->id.port_num);
3121	gid_type = cma_route_gid_type(addr->dev_addr.network,
3122				      supported_gids,
3123				      id_priv->gid_type);
3124	/* Use the hint from IP Stack to select GID Type */
3125	if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
3126		gid_type = ib_network_to_gid_type(addr->dev_addr.network);
3127	route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
3128
3129	route->path_rec->roce.route_resolved = true;
3130	sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
3131	return ndev;
3132}
3133
3134int rdma_set_ib_path(struct rdma_cm_id *id,
3135		     struct sa_path_rec *path_rec)
3136{
3137	struct rdma_id_private *id_priv;
3138	struct net_device *ndev;
3139	int ret;
3140
3141	id_priv = container_of(id, struct rdma_id_private, id);
3142	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3143			   RDMA_CM_ROUTE_RESOLVED))
3144		return -EINVAL;
3145
3146	id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec),
3147				     GFP_KERNEL);
3148	if (!id->route.path_rec) {
3149		ret = -ENOMEM;
3150		goto err;
3151	}
3152
3153	if (rdma_protocol_roce(id->device, id->port_num)) {
3154		ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
3155		if (!ndev) {
3156			ret = -ENODEV;
3157			goto err_free;
3158		}
3159		dev_put(ndev);
3160	}
3161
3162	id->route.num_pri_alt_paths = 1;
3163	return 0;
3164
3165err_free:
3166	kfree(id->route.path_rec);
3167	id->route.path_rec = NULL;
3168err:
3169	cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
3170	return ret;
3171}
3172EXPORT_SYMBOL(rdma_set_ib_path);
3173
3174static int cma_resolve_iw_route(struct rdma_id_private *id_priv)
3175{
3176	struct cma_work *work;
3177
3178	work = kzalloc(sizeof *work, GFP_KERNEL);
3179	if (!work)
3180		return -ENOMEM;
3181
3182	cma_init_resolve_route_work(work, id_priv);
 
 
 
 
3183	queue_work(cma_wq, &work->work);
3184	return 0;
3185}
3186
3187static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio)
3188{
 
3189	struct net_device *dev;
3190
3191	dev = vlan_dev_real_dev(vlan_ndev);
 
 
 
3192	if (dev->num_tc)
3193		return netdev_get_prio_tc_map(dev, prio);
3194
3195	return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) &
3196		VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
3197}
3198
3199struct iboe_prio_tc_map {
3200	int input_prio;
3201	int output_tc;
3202	bool found;
3203};
3204
3205static int get_lower_vlan_dev_tc(struct net_device *dev,
3206				 struct netdev_nested_priv *priv)
3207{
3208	struct iboe_prio_tc_map *map = (struct iboe_prio_tc_map *)priv->data;
3209
3210	if (is_vlan_dev(dev))
3211		map->output_tc = get_vlan_ndev_tc(dev, map->input_prio);
3212	else if (dev->num_tc)
3213		map->output_tc = netdev_get_prio_tc_map(dev, map->input_prio);
3214	else
3215		map->output_tc = 0;
3216	/* We are interested only in first level VLAN device, so always
3217	 * return 1 to stop iterating over next level devices.
3218	 */
3219	map->found = true;
3220	return 1;
3221}
3222
3223static int iboe_tos_to_sl(struct net_device *ndev, int tos)
3224{
3225	struct iboe_prio_tc_map prio_tc_map = {};
3226	int prio = rt_tos2priority(tos);
3227	struct netdev_nested_priv priv;
3228
3229	/* If VLAN device, get it directly from the VLAN netdev */
3230	if (is_vlan_dev(ndev))
3231		return get_vlan_ndev_tc(ndev, prio);
3232
3233	prio_tc_map.input_prio = prio;
3234	priv.data = (void *)&prio_tc_map;
3235	rcu_read_lock();
3236	netdev_walk_all_lower_dev_rcu(ndev,
3237				      get_lower_vlan_dev_tc,
3238				      &priv);
3239	rcu_read_unlock();
3240	/* If map is found from lower device, use it; Otherwise
3241	 * continue with the current netdevice to get priority to tc map.
3242	 */
3243	if (prio_tc_map.found)
3244		return prio_tc_map.output_tc;
3245	else if (ndev->num_tc)
3246		return netdev_get_prio_tc_map(ndev, prio);
3247	else
3248		return 0;
3249}
3250
3251static __be32 cma_get_roce_udp_flow_label(struct rdma_id_private *id_priv)
 
 
3252{
3253	struct sockaddr_in6 *addr6;
3254	u16 dport, sport;
3255	u32 hash, fl;
3256
3257	addr6 = (struct sockaddr_in6 *)cma_src_addr(id_priv);
3258	fl = be32_to_cpu(addr6->sin6_flowinfo) & IB_GRH_FLOWLABEL_MASK;
3259	if ((cma_family(id_priv) != AF_INET6) || !fl) {
3260		dport = be16_to_cpu(cma_port(cma_dst_addr(id_priv)));
3261		sport = be16_to_cpu(cma_port(cma_src_addr(id_priv)));
3262		hash = (u32)sport * 31 + dport;
3263		fl = hash & IB_GRH_FLOWLABEL_MASK;
3264	}
3265
3266	return cpu_to_be32(fl);
3267}
3268
3269static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
3270{
3271	struct rdma_route *route = &id_priv->id.route;
3272	struct rdma_addr *addr = &route->addr;
3273	struct cma_work *work;
3274	int ret;
3275	struct net_device *ndev;
3276
3277	u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
3278					rdma_start_port(id_priv->cma_dev->device)];
3279	u8 tos;
3280
3281	mutex_lock(&id_priv->qp_mutex);
3282	tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
3283	mutex_unlock(&id_priv->qp_mutex);
3284
3285	work = kzalloc(sizeof *work, GFP_KERNEL);
3286	if (!work)
3287		return -ENOMEM;
3288
 
 
 
3289	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
3290	if (!route->path_rec) {
3291		ret = -ENOMEM;
3292		goto err1;
3293	}
3294
3295	route->num_pri_alt_paths = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3296
3297	ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
 
 
 
 
 
 
 
 
3298	if (!ndev) {
3299		ret = -ENODEV;
3300		goto err2;
3301	}
3302
 
 
3303	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3304		    &route->path_rec->sgid);
3305	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
3306		    &route->path_rec->dgid);
3307
 
 
 
3308	if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
3309		/* TODO: get the hoplimit from the inet/inet6 device */
3310		route->path_rec->hop_limit = addr->dev_addr.hoplimit;
3311	else
3312		route->path_rec->hop_limit = 1;
3313	route->path_rec->reversible = 1;
3314	route->path_rec->pkey = cpu_to_be16(0xffff);
3315	route->path_rec->mtu_selector = IB_SA_EQ;
3316	route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
3317	route->path_rec->traffic_class = tos;
3318	route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
3319	route->path_rec->rate_selector = IB_SA_EQ;
3320	route->path_rec->rate = IB_RATE_PORT_CURRENT;
3321	dev_put(ndev);
3322	route->path_rec->packet_life_time_selector = IB_SA_EQ;
3323	/* In case ACK timeout is set, use this value to calculate
3324	 * PacketLifeTime.  As per IBTA 12.7.34,
3325	 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
3326	 * Assuming a negligible local ACK delay, we can use
3327	 * PacketLifeTime = local ACK timeout/2
3328	 * as a reasonable approximation for RoCE networks.
3329	 */
3330	mutex_lock(&id_priv->qp_mutex);
3331	if (id_priv->timeout_set && id_priv->timeout)
3332		route->path_rec->packet_life_time = id_priv->timeout - 1;
3333	else
3334		route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
3335	mutex_unlock(&id_priv->qp_mutex);
3336
3337	if (!route->path_rec->mtu) {
3338		ret = -EINVAL;
3339		goto err2;
3340	}
3341
3342	if (rdma_protocol_roce_udp_encap(id_priv->id.device,
3343					 id_priv->id.port_num))
3344		route->path_rec->flow_label =
3345			cma_get_roce_udp_flow_label(id_priv);
3346
3347	cma_init_resolve_route_work(work, id_priv);
3348	queue_work(cma_wq, &work->work);
3349
3350	return 0;
3351
3352err2:
3353	kfree(route->path_rec);
3354	route->path_rec = NULL;
3355	route->num_pri_alt_paths = 0;
3356err1:
3357	kfree(work);
3358	return ret;
3359}
3360
3361int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms)
3362{
3363	struct rdma_id_private *id_priv;
3364	int ret;
3365
3366	if (!timeout_ms)
3367		return -EINVAL;
3368
3369	id_priv = container_of(id, struct rdma_id_private, id);
3370	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
3371		return -EINVAL;
3372
3373	cma_id_get(id_priv);
3374	if (rdma_cap_ib_sa(id->device, id->port_num))
3375		ret = cma_resolve_ib_route(id_priv, timeout_ms);
3376	else if (rdma_protocol_roce(id->device, id->port_num)) {
3377		ret = cma_resolve_iboe_route(id_priv);
3378		if (!ret)
3379			cma_add_id_to_tree(id_priv);
3380	}
3381	else if (rdma_protocol_iwarp(id->device, id->port_num))
3382		ret = cma_resolve_iw_route(id_priv);
3383	else
3384		ret = -ENOSYS;
3385
3386	if (ret)
3387		goto err;
3388
3389	return 0;
3390err:
3391	cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
3392	cma_id_put(id_priv);
3393	return ret;
3394}
3395EXPORT_SYMBOL(rdma_resolve_route);
3396
3397static void cma_set_loopback(struct sockaddr *addr)
3398{
3399	switch (addr->sa_family) {
3400	case AF_INET:
3401		((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3402		break;
3403	case AF_INET6:
3404		ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
3405			      0, 0, 0, htonl(1));
3406		break;
3407	default:
3408		ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
3409			    0, 0, 0, htonl(1));
3410		break;
3411	}
3412}
3413
3414static int cma_bind_loopback(struct rdma_id_private *id_priv)
3415{
3416	struct cma_device *cma_dev, *cur_dev;
 
3417	union ib_gid gid;
3418	enum ib_port_state port_state;
3419	unsigned int p;
3420	u16 pkey;
3421	int ret;
 
3422
3423	cma_dev = NULL;
3424	mutex_lock(&lock);
3425	list_for_each_entry(cur_dev, &dev_list, list) {
3426		if (cma_family(id_priv) == AF_IB &&
3427		    !rdma_cap_ib_cm(cur_dev->device, 1))
3428			continue;
3429
3430		if (!cma_dev)
3431			cma_dev = cur_dev;
3432
3433		rdma_for_each_port (cur_dev->device, p) {
3434			if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
3435			    port_state == IB_PORT_ACTIVE) {
3436				cma_dev = cur_dev;
3437				goto port_found;
3438			}
3439		}
3440	}
3441
3442	if (!cma_dev) {
3443		ret = -ENODEV;
3444		goto out;
3445	}
3446
3447	p = 1;
3448
3449port_found:
3450	ret = rdma_query_gid(cma_dev->device, p, 0, &gid);
3451	if (ret)
3452		goto out;
3453
3454	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
3455	if (ret)
3456		goto out;
3457
3458	id_priv->id.route.addr.dev_addr.dev_type =
3459		(rdma_protocol_ib(cma_dev->device, p)) ?
3460		ARPHRD_INFINIBAND : ARPHRD_ETHER;
3461
3462	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3463	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
3464	id_priv->id.port_num = p;
3465	cma_attach_to_dev(id_priv, cma_dev);
3466	rdma_restrack_add(&id_priv->res);
3467	cma_set_loopback(cma_src_addr(id_priv));
3468out:
3469	mutex_unlock(&lock);
3470	return ret;
3471}
3472
3473static void addr_handler(int status, struct sockaddr *src_addr,
3474			 struct rdma_dev_addr *dev_addr, void *context)
3475{
3476	struct rdma_id_private *id_priv = context;
3477	struct rdma_cm_event event = {};
3478	struct sockaddr *addr;
3479	struct sockaddr_storage old_addr;
3480
 
3481	mutex_lock(&id_priv->handler_mutex);
3482	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
3483			   RDMA_CM_ADDR_RESOLVED))
3484		goto out;
3485
3486	/*
3487	 * Store the previous src address, so that if we fail to acquire
3488	 * matching rdma device, old address can be restored back, which helps
3489	 * to cancel the cma listen operation correctly.
3490	 */
3491	addr = cma_src_addr(id_priv);
3492	memcpy(&old_addr, addr, rdma_addr_size(addr));
3493	memcpy(addr, src_addr, rdma_addr_size(src_addr));
3494	if (!status && !id_priv->cma_dev) {
3495		status = cma_acquire_dev_by_src_ip(id_priv);
3496		if (status)
3497			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3498					     status);
3499		rdma_restrack_add(&id_priv->res);
3500	} else if (status) {
3501		pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
3502	}
3503
3504	if (status) {
3505		memcpy(addr, &old_addr,
3506		       rdma_addr_size((struct sockaddr *)&old_addr));
3507		if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3508				   RDMA_CM_ADDR_BOUND))
3509			goto out;
3510		event.event = RDMA_CM_EVENT_ADDR_ERROR;
3511		event.status = status;
3512	} else
3513		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3514
3515	if (cma_cm_event_handler(id_priv, &event)) {
3516		destroy_id_handler_unlock(id_priv);
 
 
 
3517		return;
3518	}
3519out:
3520	mutex_unlock(&id_priv->handler_mutex);
 
3521}
3522
3523static int cma_resolve_loopback(struct rdma_id_private *id_priv)
3524{
3525	struct cma_work *work;
3526	union ib_gid gid;
3527	int ret;
3528
3529	work = kzalloc(sizeof *work, GFP_KERNEL);
3530	if (!work)
3531		return -ENOMEM;
3532
3533	if (!id_priv->cma_dev) {
3534		ret = cma_bind_loopback(id_priv);
3535		if (ret)
3536			goto err;
3537	}
3538
3539	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3540	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
3541
3542	enqueue_resolve_addr_work(work, id_priv);
 
 
 
 
 
3543	return 0;
3544err:
3545	kfree(work);
3546	return ret;
3547}
3548
3549static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
3550{
3551	struct cma_work *work;
3552	int ret;
3553
3554	work = kzalloc(sizeof *work, GFP_KERNEL);
3555	if (!work)
3556		return -ENOMEM;
3557
3558	if (!id_priv->cma_dev) {
3559		ret = cma_resolve_ib_dev(id_priv);
3560		if (ret)
3561			goto err;
3562	}
3563
3564	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
3565		&(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
3566
3567	enqueue_resolve_addr_work(work, id_priv);
 
 
 
 
 
3568	return 0;
3569err:
3570	kfree(work);
3571	return ret;
3572}
3573
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3574int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
3575{
3576	struct rdma_id_private *id_priv;
3577	unsigned long flags;
3578	int ret;
3579
3580	id_priv = container_of(id, struct rdma_id_private, id);
3581	spin_lock_irqsave(&id_priv->lock, flags);
3582	if ((reuse && id_priv->state != RDMA_CM_LISTEN) ||
3583	    id_priv->state == RDMA_CM_IDLE) {
3584		id_priv->reuseaddr = reuse;
3585		ret = 0;
3586	} else {
3587		ret = -EINVAL;
3588	}
3589	spin_unlock_irqrestore(&id_priv->lock, flags);
3590	return ret;
3591}
3592EXPORT_SYMBOL(rdma_set_reuseaddr);
3593
3594int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
3595{
3596	struct rdma_id_private *id_priv;
3597	unsigned long flags;
3598	int ret;
3599
3600	id_priv = container_of(id, struct rdma_id_private, id);
3601	spin_lock_irqsave(&id_priv->lock, flags);
3602	if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
3603		id_priv->options |= (1 << CMA_OPTION_AFONLY);
3604		id_priv->afonly = afonly;
3605		ret = 0;
3606	} else {
3607		ret = -EINVAL;
3608	}
3609	spin_unlock_irqrestore(&id_priv->lock, flags);
3610	return ret;
3611}
3612EXPORT_SYMBOL(rdma_set_afonly);
3613
3614static void cma_bind_port(struct rdma_bind_list *bind_list,
3615			  struct rdma_id_private *id_priv)
3616{
3617	struct sockaddr *addr;
3618	struct sockaddr_ib *sib;
3619	u64 sid, mask;
3620	__be16 port;
3621
3622	lockdep_assert_held(&lock);
3623
3624	addr = cma_src_addr(id_priv);
3625	port = htons(bind_list->port);
3626
3627	switch (addr->sa_family) {
3628	case AF_INET:
3629		((struct sockaddr_in *) addr)->sin_port = port;
3630		break;
3631	case AF_INET6:
3632		((struct sockaddr_in6 *) addr)->sin6_port = port;
3633		break;
3634	case AF_IB:
3635		sib = (struct sockaddr_ib *) addr;
3636		sid = be64_to_cpu(sib->sib_sid);
3637		mask = be64_to_cpu(sib->sib_sid_mask);
3638		sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
3639		sib->sib_sid_mask = cpu_to_be64(~0ULL);
3640		break;
3641	}
3642	id_priv->bind_list = bind_list;
3643	hlist_add_head(&id_priv->node, &bind_list->owners);
3644}
3645
3646static int cma_alloc_port(enum rdma_ucm_port_space ps,
3647			  struct rdma_id_private *id_priv, unsigned short snum)
3648{
3649	struct rdma_bind_list *bind_list;
3650	int ret;
3651
3652	lockdep_assert_held(&lock);
3653
3654	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3655	if (!bind_list)
3656		return -ENOMEM;
3657
3658	ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3659			   snum);
3660	if (ret < 0)
3661		goto err;
3662
3663	bind_list->ps = ps;
3664	bind_list->port = snum;
3665	cma_bind_port(bind_list, id_priv);
3666	return 0;
3667err:
3668	kfree(bind_list);
3669	return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3670}
3671
3672static int cma_port_is_unique(struct rdma_bind_list *bind_list,
3673			      struct rdma_id_private *id_priv)
3674{
3675	struct rdma_id_private *cur_id;
3676	struct sockaddr  *daddr = cma_dst_addr(id_priv);
3677	struct sockaddr  *saddr = cma_src_addr(id_priv);
3678	__be16 dport = cma_port(daddr);
3679
3680	lockdep_assert_held(&lock);
3681
3682	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3683		struct sockaddr  *cur_daddr = cma_dst_addr(cur_id);
3684		struct sockaddr  *cur_saddr = cma_src_addr(cur_id);
3685		__be16 cur_dport = cma_port(cur_daddr);
3686
3687		if (id_priv == cur_id)
3688			continue;
3689
3690		/* different dest port -> unique */
3691		if (!cma_any_port(daddr) &&
3692		    !cma_any_port(cur_daddr) &&
3693		    (dport != cur_dport))
3694			continue;
3695
3696		/* different src address -> unique */
3697		if (!cma_any_addr(saddr) &&
3698		    !cma_any_addr(cur_saddr) &&
3699		    cma_addr_cmp(saddr, cur_saddr))
3700			continue;
3701
3702		/* different dst address -> unique */
3703		if (!cma_any_addr(daddr) &&
3704		    !cma_any_addr(cur_daddr) &&
3705		    cma_addr_cmp(daddr, cur_daddr))
3706			continue;
3707
3708		return -EADDRNOTAVAIL;
3709	}
3710	return 0;
3711}
3712
3713static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
3714			      struct rdma_id_private *id_priv)
3715{
3716	static unsigned int last_used_port;
3717	int low, high, remaining;
3718	unsigned int rover;
3719	struct net *net = id_priv->id.route.addr.dev_addr.net;
3720
3721	lockdep_assert_held(&lock);
3722
3723	inet_get_local_port_range(net, &low, &high);
3724	remaining = (high - low) + 1;
3725	rover = get_random_u32_inclusive(low, remaining + low - 1);
3726retry:
3727	if (last_used_port != rover) {
3728		struct rdma_bind_list *bind_list;
3729		int ret;
3730
3731		bind_list = cma_ps_find(net, ps, (unsigned short)rover);
3732
3733		if (!bind_list) {
3734			ret = cma_alloc_port(ps, id_priv, rover);
3735		} else {
3736			ret = cma_port_is_unique(bind_list, id_priv);
3737			if (!ret)
3738				cma_bind_port(bind_list, id_priv);
3739		}
3740		/*
3741		 * Remember previously used port number in order to avoid
3742		 * re-using same port immediately after it is closed.
3743		 */
3744		if (!ret)
3745			last_used_port = rover;
3746		if (ret != -EADDRNOTAVAIL)
3747			return ret;
3748	}
3749	if (--remaining) {
3750		rover++;
3751		if ((rover < low) || (rover > high))
3752			rover = low;
3753		goto retry;
3754	}
3755	return -EADDRNOTAVAIL;
3756}
3757
3758/*
3759 * Check that the requested port is available.  This is called when trying to
3760 * bind to a specific port, or when trying to listen on a bound port.  In
3761 * the latter case, the provided id_priv may already be on the bind_list, but
3762 * we still need to check that it's okay to start listening.
3763 */
3764static int cma_check_port(struct rdma_bind_list *bind_list,
3765			  struct rdma_id_private *id_priv, uint8_t reuseaddr)
3766{
3767	struct rdma_id_private *cur_id;
3768	struct sockaddr *addr, *cur_addr;
3769
3770	lockdep_assert_held(&lock);
3771
3772	addr = cma_src_addr(id_priv);
3773	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3774		if (id_priv == cur_id)
3775			continue;
3776
3777		if (reuseaddr && cur_id->reuseaddr)
 
3778			continue;
3779
3780		cur_addr = cma_src_addr(cur_id);
3781		if (id_priv->afonly && cur_id->afonly &&
3782		    (addr->sa_family != cur_addr->sa_family))
3783			continue;
3784
3785		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3786			return -EADDRNOTAVAIL;
3787
3788		if (!cma_addr_cmp(addr, cur_addr))
3789			return -EADDRINUSE;
3790	}
3791	return 0;
3792}
3793
3794static int cma_use_port(enum rdma_ucm_port_space ps,
3795			struct rdma_id_private *id_priv)
3796{
3797	struct rdma_bind_list *bind_list;
3798	unsigned short snum;
3799	int ret;
3800
3801	lockdep_assert_held(&lock);
3802
3803	snum = ntohs(cma_port(cma_src_addr(id_priv)));
3804	if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3805		return -EACCES;
3806
3807	bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3808	if (!bind_list) {
3809		ret = cma_alloc_port(ps, id_priv, snum);
3810	} else {
3811		ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3812		if (!ret)
3813			cma_bind_port(bind_list, id_priv);
3814	}
3815	return ret;
3816}
3817
3818static enum rdma_ucm_port_space
3819cma_select_inet_ps(struct rdma_id_private *id_priv)
 
 
 
 
 
 
 
 
 
 
 
 
3820{
3821	switch (id_priv->id.ps) {
3822	case RDMA_PS_TCP:
3823	case RDMA_PS_UDP:
3824	case RDMA_PS_IPOIB:
3825	case RDMA_PS_IB:
3826		return id_priv->id.ps;
3827	default:
3828
3829		return 0;
3830	}
3831}
3832
3833static enum rdma_ucm_port_space
3834cma_select_ib_ps(struct rdma_id_private *id_priv)
3835{
3836	enum rdma_ucm_port_space ps = 0;
3837	struct sockaddr_ib *sib;
3838	u64 sid_ps, mask, sid;
3839
3840	sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3841	mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3842	sid = be64_to_cpu(sib->sib_sid) & mask;
3843
3844	if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3845		sid_ps = RDMA_IB_IP_PS_IB;
3846		ps = RDMA_PS_IB;
3847	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3848		   (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3849		sid_ps = RDMA_IB_IP_PS_TCP;
3850		ps = RDMA_PS_TCP;
3851	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3852		   (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3853		sid_ps = RDMA_IB_IP_PS_UDP;
3854		ps = RDMA_PS_UDP;
3855	}
3856
3857	if (ps) {
3858		sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3859		sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3860						be64_to_cpu(sib->sib_sid_mask));
3861	}
3862	return ps;
3863}
3864
3865static int cma_get_port(struct rdma_id_private *id_priv)
3866{
3867	enum rdma_ucm_port_space ps;
3868	int ret;
3869
3870	if (cma_family(id_priv) != AF_IB)
3871		ps = cma_select_inet_ps(id_priv);
3872	else
3873		ps = cma_select_ib_ps(id_priv);
3874	if (!ps)
3875		return -EPROTONOSUPPORT;
3876
3877	mutex_lock(&lock);
3878	if (cma_any_port(cma_src_addr(id_priv)))
3879		ret = cma_alloc_any_port(ps, id_priv);
3880	else
3881		ret = cma_use_port(ps, id_priv);
3882	mutex_unlock(&lock);
3883
3884	return ret;
3885}
3886
3887static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3888			       struct sockaddr *addr)
3889{
3890#if IS_ENABLED(CONFIG_IPV6)
3891	struct sockaddr_in6 *sin6;
3892
3893	if (addr->sa_family != AF_INET6)
3894		return 0;
3895
3896	sin6 = (struct sockaddr_in6 *) addr;
3897
3898	if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3899		return 0;
3900
3901	if (!sin6->sin6_scope_id)
3902			return -EINVAL;
3903
3904	dev_addr->bound_dev_if = sin6->sin6_scope_id;
3905#endif
3906	return 0;
3907}
3908
3909int rdma_listen(struct rdma_cm_id *id, int backlog)
3910{
3911	struct rdma_id_private *id_priv =
3912		container_of(id, struct rdma_id_private, id);
3913	int ret;
3914
3915	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) {
3916		struct sockaddr_in any_in = {
3917			.sin_family = AF_INET,
3918			.sin_addr.s_addr = htonl(INADDR_ANY),
3919		};
3920
3921		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3922		ret = rdma_bind_addr(id, (struct sockaddr *)&any_in);
3923		if (ret)
3924			return ret;
3925		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3926					   RDMA_CM_LISTEN)))
3927			return -EINVAL;
3928	}
3929
3930	/*
3931	 * Once the ID reaches RDMA_CM_LISTEN it is not allowed to be reusable
3932	 * any more, and has to be unique in the bind list.
3933	 */
3934	if (id_priv->reuseaddr) {
3935		mutex_lock(&lock);
3936		ret = cma_check_port(id_priv->bind_list, id_priv, 0);
3937		if (!ret)
3938			id_priv->reuseaddr = 0;
3939		mutex_unlock(&lock);
3940		if (ret)
3941			goto err;
3942	}
3943
3944	id_priv->backlog = backlog;
3945	if (id_priv->cma_dev) {
3946		if (rdma_cap_ib_cm(id->device, 1)) {
3947			ret = cma_ib_listen(id_priv);
3948			if (ret)
3949				goto err;
3950		} else if (rdma_cap_iw_cm(id->device, 1)) {
3951			ret = cma_iw_listen(id_priv, backlog);
3952			if (ret)
3953				goto err;
3954		} else {
3955			ret = -ENOSYS;
3956			goto err;
3957		}
3958	} else {
3959		ret = cma_listen_on_all(id_priv);
3960		if (ret)
3961			goto err;
3962	}
3963
3964	return 0;
3965err:
3966	id_priv->backlog = 0;
3967	/*
3968	 * All the failure paths that lead here will not allow the req_handler's
3969	 * to have run.
3970	 */
3971	cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3972	return ret;
3973}
3974EXPORT_SYMBOL(rdma_listen);
3975
3976static int rdma_bind_addr_dst(struct rdma_id_private *id_priv,
3977			      struct sockaddr *addr, const struct sockaddr *daddr)
3978{
3979	struct sockaddr *id_daddr;
3980	int ret;
3981
3982	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3983	    addr->sa_family != AF_IB)
3984		return -EAFNOSUPPORT;
3985
 
3986	if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3987		return -EINVAL;
3988
3989	ret = cma_check_linklocal(&id_priv->id.route.addr.dev_addr, addr);
3990	if (ret)
3991		goto err1;
3992
3993	memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3994	if (!cma_any_addr(addr)) {
3995		ret = cma_translate_addr(addr, &id_priv->id.route.addr.dev_addr);
3996		if (ret)
3997			goto err1;
3998
3999		ret = cma_acquire_dev_by_src_ip(id_priv);
4000		if (ret)
4001			goto err1;
4002	}
4003
4004	if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
4005		if (addr->sa_family == AF_INET)
4006			id_priv->afonly = 1;
4007#if IS_ENABLED(CONFIG_IPV6)
4008		else if (addr->sa_family == AF_INET6) {
4009			struct net *net = id_priv->id.route.addr.dev_addr.net;
4010
4011			id_priv->afonly = net->ipv6.sysctl.bindv6only;
4012		}
4013#endif
4014	}
4015	id_daddr = cma_dst_addr(id_priv);
4016	if (daddr != id_daddr)
4017		memcpy(id_daddr, daddr, rdma_addr_size(addr));
4018	id_daddr->sa_family = addr->sa_family;
4019
4020	ret = cma_get_port(id_priv);
4021	if (ret)
4022		goto err2;
4023
4024	if (!cma_any_addr(addr))
4025		rdma_restrack_add(&id_priv->res);
4026	return 0;
4027err2:
4028	if (id_priv->cma_dev)
4029		cma_release_dev(id_priv);
4030err1:
4031	cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
4032	return ret;
4033}
4034
4035static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
4036			 const struct sockaddr *dst_addr)
4037{
4038	struct rdma_id_private *id_priv =
4039		container_of(id, struct rdma_id_private, id);
4040	struct sockaddr_storage zero_sock = {};
4041
4042	if (src_addr && src_addr->sa_family)
4043		return rdma_bind_addr_dst(id_priv, src_addr, dst_addr);
4044
4045	/*
4046	 * When the src_addr is not specified, automatically supply an any addr
4047	 */
4048	zero_sock.ss_family = dst_addr->sa_family;
4049	if (IS_ENABLED(CONFIG_IPV6) && dst_addr->sa_family == AF_INET6) {
4050		struct sockaddr_in6 *src_addr6 =
4051			(struct sockaddr_in6 *)&zero_sock;
4052		struct sockaddr_in6 *dst_addr6 =
4053			(struct sockaddr_in6 *)dst_addr;
4054
4055		src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
4056		if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
4057			id->route.addr.dev_addr.bound_dev_if =
4058				dst_addr6->sin6_scope_id;
4059	} else if (dst_addr->sa_family == AF_IB) {
4060		((struct sockaddr_ib *)&zero_sock)->sib_pkey =
4061			((struct sockaddr_ib *)dst_addr)->sib_pkey;
4062	}
4063	return rdma_bind_addr_dst(id_priv, (struct sockaddr *)&zero_sock, dst_addr);
4064}
4065
4066/*
4067 * If required, resolve the source address for bind and leave the id_priv in
4068 * state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
4069 * calls made by ULP, a previously bound ID will not be re-bound and src_addr is
4070 * ignored.
4071 */
4072static int resolve_prepare_src(struct rdma_id_private *id_priv,
4073			       struct sockaddr *src_addr,
4074			       const struct sockaddr *dst_addr)
4075{
4076	int ret;
4077
4078	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
4079		/* For a well behaved ULP state will be RDMA_CM_IDLE */
4080		ret = cma_bind_addr(&id_priv->id, src_addr, dst_addr);
4081		if (ret)
4082			return ret;
4083		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
4084					   RDMA_CM_ADDR_QUERY)))
4085			return -EINVAL;
4086
4087	} else {
4088		memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
4089	}
4090
4091	if (cma_family(id_priv) != dst_addr->sa_family) {
4092		ret = -EINVAL;
4093		goto err_state;
4094	}
4095	return 0;
4096
4097err_state:
4098	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
4099	return ret;
4100}
4101
4102int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
4103		      const struct sockaddr *dst_addr, unsigned long timeout_ms)
4104{
4105	struct rdma_id_private *id_priv =
4106		container_of(id, struct rdma_id_private, id);
4107	int ret;
4108
4109	ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
4110	if (ret)
4111		return ret;
4112
4113	if (cma_any_addr(dst_addr)) {
4114		ret = cma_resolve_loopback(id_priv);
4115	} else {
4116		if (dst_addr->sa_family == AF_IB) {
4117			ret = cma_resolve_ib_addr(id_priv);
4118		} else {
4119			/*
4120			 * The FSM can return back to RDMA_CM_ADDR_BOUND after
4121			 * rdma_resolve_ip() is called, eg through the error
4122			 * path in addr_handler(). If this happens the existing
4123			 * request must be canceled before issuing a new one.
4124			 * Since canceling a request is a bit slow and this
4125			 * oddball path is rare, keep track once a request has
4126			 * been issued. The track turns out to be a permanent
4127			 * state since this is the only cancel as it is
4128			 * immediately before rdma_resolve_ip().
4129			 */
4130			if (id_priv->used_resolve_ip)
4131				rdma_addr_cancel(&id->route.addr.dev_addr);
4132			else
4133				id_priv->used_resolve_ip = 1;
4134			ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr,
4135					      &id->route.addr.dev_addr,
4136					      timeout_ms, addr_handler,
4137					      false, id_priv);
4138		}
4139	}
4140	if (ret)
4141		goto err;
4142
4143	return 0;
4144err:
4145	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
4146	return ret;
4147}
4148EXPORT_SYMBOL(rdma_resolve_addr);
4149
4150int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
4151{
4152	struct rdma_id_private *id_priv =
4153		container_of(id, struct rdma_id_private, id);
4154
4155	return rdma_bind_addr_dst(id_priv, addr, cma_dst_addr(id_priv));
4156}
4157EXPORT_SYMBOL(rdma_bind_addr);
4158
4159static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
4160{
4161	struct cma_hdr *cma_hdr;
4162
4163	cma_hdr = hdr;
4164	cma_hdr->cma_version = CMA_VERSION;
4165	if (cma_family(id_priv) == AF_INET) {
4166		struct sockaddr_in *src4, *dst4;
4167
4168		src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
4169		dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
4170
4171		cma_set_ip_ver(cma_hdr, 4);
4172		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
4173		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
4174		cma_hdr->port = src4->sin_port;
4175	} else if (cma_family(id_priv) == AF_INET6) {
4176		struct sockaddr_in6 *src6, *dst6;
4177
4178		src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
4179		dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
4180
4181		cma_set_ip_ver(cma_hdr, 6);
4182		cma_hdr->src_addr.ip6 = src6->sin6_addr;
4183		cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
4184		cma_hdr->port = src6->sin6_port;
4185	}
4186	return 0;
4187}
4188
4189static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
4190				const struct ib_cm_event *ib_event)
4191{
4192	struct rdma_id_private *id_priv = cm_id->context;
4193	struct rdma_cm_event event = {};
4194	const struct ib_cm_sidr_rep_event_param *rep =
4195				&ib_event->param.sidr_rep_rcvd;
4196	int ret;
4197
4198	mutex_lock(&id_priv->handler_mutex);
4199	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4200		goto out;
4201
 
4202	switch (ib_event->event) {
4203	case IB_CM_SIDR_REQ_ERROR:
4204		event.event = RDMA_CM_EVENT_UNREACHABLE;
4205		event.status = -ETIMEDOUT;
4206		break;
4207	case IB_CM_SIDR_REP_RECEIVED:
4208		event.param.ud.private_data = ib_event->private_data;
4209		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
4210		if (rep->status != IB_SIDR_SUCCESS) {
4211			event.event = RDMA_CM_EVENT_UNREACHABLE;
4212			event.status = ib_event->param.sidr_rep_rcvd.status;
4213			pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
4214					     event.status);
4215			break;
4216		}
4217		ret = cma_set_qkey(id_priv, rep->qkey);
4218		if (ret) {
4219			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
4220			event.event = RDMA_CM_EVENT_ADDR_ERROR;
4221			event.status = ret;
4222			break;
4223		}
4224		ib_init_ah_attr_from_path(id_priv->id.device,
4225					  id_priv->id.port_num,
4226					  id_priv->id.route.path_rec,
4227					  &event.param.ud.ah_attr,
4228					  rep->sgid_attr);
4229		event.param.ud.qp_num = rep->qpn;
4230		event.param.ud.qkey = rep->qkey;
4231		event.event = RDMA_CM_EVENT_ESTABLISHED;
4232		event.status = 0;
4233		break;
4234	default:
4235		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
4236		       ib_event->event);
4237		goto out;
4238	}
4239
4240	ret = cma_cm_event_handler(id_priv, &event);
4241
4242	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4243	if (ret) {
4244		/* Destroy the CM ID by returning a non-zero value. */
4245		id_priv->cm_id.ib = NULL;
4246		destroy_id_handler_unlock(id_priv);
 
 
4247		return ret;
4248	}
4249out:
4250	mutex_unlock(&id_priv->handler_mutex);
4251	return 0;
4252}
4253
4254static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
4255			      struct rdma_conn_param *conn_param)
4256{
4257	struct ib_cm_sidr_req_param req;
4258	struct ib_cm_id	*id;
4259	void *private_data;
4260	u8 offset;
4261	int ret;
4262
4263	memset(&req, 0, sizeof req);
4264	offset = cma_user_data_offset(id_priv);
4265	if (check_add_overflow(offset, conn_param->private_data_len, &req.private_data_len))
 
4266		return -EINVAL;
4267
4268	if (req.private_data_len) {
4269		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4270		if (!private_data)
4271			return -ENOMEM;
4272	} else {
4273		private_data = NULL;
4274	}
4275
4276	if (conn_param->private_data && conn_param->private_data_len)
4277		memcpy(private_data + offset, conn_param->private_data,
4278		       conn_param->private_data_len);
4279
4280	if (private_data) {
4281		ret = cma_format_hdr(private_data, id_priv);
4282		if (ret)
4283			goto out;
4284		req.private_data = private_data;
4285	}
4286
4287	id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
4288			     id_priv);
4289	if (IS_ERR(id)) {
4290		ret = PTR_ERR(id);
4291		goto out;
4292	}
4293	id_priv->cm_id.ib = id;
4294
4295	req.path = id_priv->id.route.path_rec;
4296	req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4297	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4298	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
4299	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4300
4301	trace_cm_send_sidr_req(id_priv);
4302	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
4303	if (ret) {
4304		ib_destroy_cm_id(id_priv->cm_id.ib);
4305		id_priv->cm_id.ib = NULL;
4306	}
4307out:
4308	kfree(private_data);
4309	return ret;
4310}
4311
4312static int cma_connect_ib(struct rdma_id_private *id_priv,
4313			  struct rdma_conn_param *conn_param)
4314{
4315	struct ib_cm_req_param req;
4316	struct rdma_route *route;
4317	void *private_data;
4318	struct ib_cm_id	*id;
4319	u8 offset;
4320	int ret;
4321
4322	memset(&req, 0, sizeof req);
4323	offset = cma_user_data_offset(id_priv);
4324	if (check_add_overflow(offset, conn_param->private_data_len, &req.private_data_len))
 
4325		return -EINVAL;
4326
4327	if (req.private_data_len) {
4328		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4329		if (!private_data)
4330			return -ENOMEM;
4331	} else {
4332		private_data = NULL;
4333	}
4334
4335	if (conn_param->private_data && conn_param->private_data_len)
4336		memcpy(private_data + offset, conn_param->private_data,
4337		       conn_param->private_data_len);
4338
4339	id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
4340	if (IS_ERR(id)) {
4341		ret = PTR_ERR(id);
4342		goto out;
4343	}
4344	id_priv->cm_id.ib = id;
4345
4346	route = &id_priv->id.route;
4347	if (private_data) {
4348		ret = cma_format_hdr(private_data, id_priv);
4349		if (ret)
4350			goto out;
4351		req.private_data = private_data;
4352	}
4353
4354	req.primary_path = &route->path_rec[0];
4355	req.primary_path_inbound = route->path_rec_inbound;
4356	req.primary_path_outbound = route->path_rec_outbound;
4357	if (route->num_pri_alt_paths == 2)
4358		req.alternate_path = &route->path_rec[1];
4359
4360	req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4361	/* Alternate path SGID attribute currently unsupported */
4362	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4363	req.qp_num = id_priv->qp_num;
4364	req.qp_type = id_priv->id.qp_type;
4365	req.starting_psn = id_priv->seq_num;
4366	req.responder_resources = conn_param->responder_resources;
4367	req.initiator_depth = conn_param->initiator_depth;
4368	req.flow_control = conn_param->flow_control;
4369	req.retry_count = min_t(u8, 7, conn_param->retry_count);
4370	req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4371	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4372	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4373	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4374	req.srq = id_priv->srq ? 1 : 0;
4375	req.ece.vendor_id = id_priv->ece.vendor_id;
4376	req.ece.attr_mod = id_priv->ece.attr_mod;
4377
4378	trace_cm_send_req(id_priv);
4379	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
4380out:
4381	if (ret && !IS_ERR(id)) {
4382		ib_destroy_cm_id(id);
4383		id_priv->cm_id.ib = NULL;
4384	}
4385
4386	kfree(private_data);
4387	return ret;
4388}
4389
4390static int cma_connect_iw(struct rdma_id_private *id_priv,
4391			  struct rdma_conn_param *conn_param)
4392{
4393	struct iw_cm_id *cm_id;
4394	int ret;
4395	struct iw_cm_conn_param iw_param;
4396
4397	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
4398	if (IS_ERR(cm_id))
4399		return PTR_ERR(cm_id);
4400
4401	mutex_lock(&id_priv->qp_mutex);
4402	cm_id->tos = id_priv->tos;
4403	cm_id->tos_set = id_priv->tos_set;
4404	mutex_unlock(&id_priv->qp_mutex);
4405
4406	id_priv->cm_id.iw = cm_id;
4407
4408	memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
4409	       rdma_addr_size(cma_src_addr(id_priv)));
4410	memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
4411	       rdma_addr_size(cma_dst_addr(id_priv)));
4412
4413	ret = cma_modify_qp_rtr(id_priv, conn_param);
4414	if (ret)
4415		goto out;
4416
4417	if (conn_param) {
4418		iw_param.ord = conn_param->initiator_depth;
4419		iw_param.ird = conn_param->responder_resources;
4420		iw_param.private_data = conn_param->private_data;
4421		iw_param.private_data_len = conn_param->private_data_len;
4422		iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
4423	} else {
4424		memset(&iw_param, 0, sizeof iw_param);
4425		iw_param.qpn = id_priv->qp_num;
4426	}
4427	ret = iw_cm_connect(cm_id, &iw_param);
4428out:
4429	if (ret) {
4430		iw_destroy_cm_id(cm_id);
4431		id_priv->cm_id.iw = NULL;
4432	}
4433	return ret;
4434}
4435
4436/**
4437 * rdma_connect_locked - Initiate an active connection request.
4438 * @id: Connection identifier to connect.
4439 * @conn_param: Connection information used for connected QPs.
4440 *
4441 * Same as rdma_connect() but can only be called from the
4442 * RDMA_CM_EVENT_ROUTE_RESOLVED handler callback.
4443 */
4444int rdma_connect_locked(struct rdma_cm_id *id,
4445			struct rdma_conn_param *conn_param)
4446{
4447	struct rdma_id_private *id_priv =
4448		container_of(id, struct rdma_id_private, id);
4449	int ret;
4450
 
4451	if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
4452		return -EINVAL;
4453
4454	if (!id->qp) {
4455		id_priv->qp_num = conn_param->qp_num;
4456		id_priv->srq = conn_param->srq;
4457	}
4458
4459	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4460		if (id->qp_type == IB_QPT_UD)
4461			ret = cma_resolve_ib_udp(id_priv, conn_param);
4462		else
4463			ret = cma_connect_ib(id_priv, conn_param);
4464	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4465		ret = cma_connect_iw(id_priv, conn_param);
4466	} else {
4467		ret = -ENOSYS;
4468	}
4469	if (ret)
4470		goto err_state;
 
4471	return 0;
4472err_state:
4473	cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4474	return ret;
4475}
4476EXPORT_SYMBOL(rdma_connect_locked);
4477
4478/**
4479 * rdma_connect - Initiate an active connection request.
4480 * @id: Connection identifier to connect.
4481 * @conn_param: Connection information used for connected QPs.
4482 *
4483 * Users must have resolved a route for the rdma_cm_id to connect with by having
4484 * called rdma_resolve_route before calling this routine.
4485 *
4486 * This call will either connect to a remote QP or obtain remote QP information
4487 * for unconnected rdma_cm_id's.  The actual operation is based on the
4488 * rdma_cm_id's port space.
4489 */
4490int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4491{
4492	struct rdma_id_private *id_priv =
4493		container_of(id, struct rdma_id_private, id);
4494	int ret;
4495
4496	mutex_lock(&id_priv->handler_mutex);
4497	ret = rdma_connect_locked(id, conn_param);
4498	mutex_unlock(&id_priv->handler_mutex);
4499	return ret;
4500}
4501EXPORT_SYMBOL(rdma_connect);
4502
4503/**
4504 * rdma_connect_ece - Initiate an active connection request with ECE data.
4505 * @id: Connection identifier to connect.
4506 * @conn_param: Connection information used for connected QPs.
4507 * @ece: ECE parameters
4508 *
4509 * See rdma_connect() explanation.
4510 */
4511int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4512		     struct rdma_ucm_ece *ece)
4513{
4514	struct rdma_id_private *id_priv =
4515		container_of(id, struct rdma_id_private, id);
4516
4517	id_priv->ece.vendor_id = ece->vendor_id;
4518	id_priv->ece.attr_mod = ece->attr_mod;
4519
4520	return rdma_connect(id, conn_param);
4521}
4522EXPORT_SYMBOL(rdma_connect_ece);
4523
4524static int cma_accept_ib(struct rdma_id_private *id_priv,
4525			 struct rdma_conn_param *conn_param)
4526{
4527	struct ib_cm_rep_param rep;
4528	int ret;
4529
4530	ret = cma_modify_qp_rtr(id_priv, conn_param);
4531	if (ret)
4532		goto out;
4533
4534	ret = cma_modify_qp_rts(id_priv, conn_param);
4535	if (ret)
4536		goto out;
4537
4538	memset(&rep, 0, sizeof rep);
4539	rep.qp_num = id_priv->qp_num;
4540	rep.starting_psn = id_priv->seq_num;
4541	rep.private_data = conn_param->private_data;
4542	rep.private_data_len = conn_param->private_data_len;
4543	rep.responder_resources = conn_param->responder_resources;
4544	rep.initiator_depth = conn_param->initiator_depth;
4545	rep.failover_accepted = 0;
4546	rep.flow_control = conn_param->flow_control;
4547	rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4548	rep.srq = id_priv->srq ? 1 : 0;
4549	rep.ece.vendor_id = id_priv->ece.vendor_id;
4550	rep.ece.attr_mod = id_priv->ece.attr_mod;
4551
4552	trace_cm_send_rep(id_priv);
4553	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
4554out:
4555	return ret;
4556}
4557
4558static int cma_accept_iw(struct rdma_id_private *id_priv,
4559		  struct rdma_conn_param *conn_param)
4560{
4561	struct iw_cm_conn_param iw_param;
4562	int ret;
4563
4564	if (!conn_param)
4565		return -EINVAL;
4566
4567	ret = cma_modify_qp_rtr(id_priv, conn_param);
4568	if (ret)
4569		return ret;
4570
4571	iw_param.ord = conn_param->initiator_depth;
4572	iw_param.ird = conn_param->responder_resources;
4573	iw_param.private_data = conn_param->private_data;
4574	iw_param.private_data_len = conn_param->private_data_len;
4575	if (id_priv->id.qp)
4576		iw_param.qpn = id_priv->qp_num;
4577	else
4578		iw_param.qpn = conn_param->qp_num;
4579
4580	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
4581}
4582
4583static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
4584			     enum ib_cm_sidr_status status, u32 qkey,
4585			     const void *private_data, int private_data_len)
4586{
4587	struct ib_cm_sidr_rep_param rep;
4588	int ret;
4589
4590	memset(&rep, 0, sizeof rep);
4591	rep.status = status;
4592	if (status == IB_SIDR_SUCCESS) {
4593		if (qkey)
4594			ret = cma_set_qkey(id_priv, qkey);
4595		else
4596			ret = cma_set_default_qkey(id_priv);
4597		if (ret)
4598			return ret;
4599		rep.qp_num = id_priv->qp_num;
4600		rep.qkey = id_priv->qkey;
4601
4602		rep.ece.vendor_id = id_priv->ece.vendor_id;
4603		rep.ece.attr_mod = id_priv->ece.attr_mod;
4604	}
4605
4606	rep.private_data = private_data;
4607	rep.private_data_len = private_data_len;
4608
4609	trace_cm_send_sidr_rep(id_priv);
4610	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
4611}
4612
4613/**
4614 * rdma_accept - Called to accept a connection request or response.
4615 * @id: Connection identifier associated with the request.
4616 * @conn_param: Information needed to establish the connection.  This must be
4617 *   provided if accepting a connection request.  If accepting a connection
4618 *   response, this parameter must be NULL.
4619 *
4620 * Typically, this routine is only called by the listener to accept a connection
4621 * request.  It must also be called on the active side of a connection if the
4622 * user is performing their own QP transitions.
4623 *
4624 * In the case of error, a reject message is sent to the remote side and the
4625 * state of the qp associated with the id is modified to error, such that any
4626 * previously posted receive buffers would be flushed.
4627 *
4628 * This function is for use by kernel ULPs and must be called from under the
4629 * handler callback.
4630 */
4631int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4632{
4633	struct rdma_id_private *id_priv =
4634		container_of(id, struct rdma_id_private, id);
4635	int ret;
4636
4637	lockdep_assert_held(&id_priv->handler_mutex);
4638
4639	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
 
 
4640		return -EINVAL;
4641
4642	if (!id->qp && conn_param) {
4643		id_priv->qp_num = conn_param->qp_num;
4644		id_priv->srq = conn_param->srq;
4645	}
4646
4647	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4648		if (id->qp_type == IB_QPT_UD) {
4649			if (conn_param)
4650				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4651							conn_param->qkey,
4652							conn_param->private_data,
4653							conn_param->private_data_len);
4654			else
4655				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4656							0, NULL, 0);
4657		} else {
4658			if (conn_param)
4659				ret = cma_accept_ib(id_priv, conn_param);
4660			else
4661				ret = cma_rep_recv(id_priv);
4662		}
4663	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4664		ret = cma_accept_iw(id_priv, conn_param);
4665	} else {
4666		ret = -ENOSYS;
4667	}
4668	if (ret)
4669		goto reject;
4670
4671	return 0;
4672reject:
4673	cma_modify_qp_err(id_priv);
4674	rdma_reject(id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
4675	return ret;
4676}
4677EXPORT_SYMBOL(rdma_accept);
4678
4679int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4680		    struct rdma_ucm_ece *ece)
4681{
4682	struct rdma_id_private *id_priv =
4683		container_of(id, struct rdma_id_private, id);
4684
4685	id_priv->ece.vendor_id = ece->vendor_id;
4686	id_priv->ece.attr_mod = ece->attr_mod;
4687
4688	return rdma_accept(id, conn_param);
4689}
4690EXPORT_SYMBOL(rdma_accept_ece);
4691
4692void rdma_lock_handler(struct rdma_cm_id *id)
4693{
4694	struct rdma_id_private *id_priv =
4695		container_of(id, struct rdma_id_private, id);
4696
4697	mutex_lock(&id_priv->handler_mutex);
4698}
4699EXPORT_SYMBOL(rdma_lock_handler);
4700
4701void rdma_unlock_handler(struct rdma_cm_id *id)
4702{
4703	struct rdma_id_private *id_priv =
4704		container_of(id, struct rdma_id_private, id);
4705
4706	mutex_unlock(&id_priv->handler_mutex);
4707}
4708EXPORT_SYMBOL(rdma_unlock_handler);
4709
4710int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
4711{
4712	struct rdma_id_private *id_priv;
4713	int ret;
4714
4715	id_priv = container_of(id, struct rdma_id_private, id);
4716	if (!id_priv->cm_id.ib)
4717		return -EINVAL;
4718
4719	switch (id->device->node_type) {
4720	case RDMA_NODE_IB_CA:
4721		ret = ib_cm_notify(id_priv->cm_id.ib, event);
4722		break;
4723	default:
4724		ret = 0;
4725		break;
4726	}
4727	return ret;
4728}
4729EXPORT_SYMBOL(rdma_notify);
4730
4731int rdma_reject(struct rdma_cm_id *id, const void *private_data,
4732		u8 private_data_len, u8 reason)
4733{
4734	struct rdma_id_private *id_priv;
4735	int ret;
4736
4737	id_priv = container_of(id, struct rdma_id_private, id);
4738	if (!id_priv->cm_id.ib)
4739		return -EINVAL;
4740
4741	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4742		if (id->qp_type == IB_QPT_UD) {
4743			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
4744						private_data, private_data_len);
4745		} else {
4746			trace_cm_send_rej(id_priv);
4747			ret = ib_send_cm_rej(id_priv->cm_id.ib, reason, NULL, 0,
4748					     private_data, private_data_len);
4749		}
4750	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4751		ret = iw_cm_reject(id_priv->cm_id.iw,
4752				   private_data, private_data_len);
4753	} else {
4754		ret = -ENOSYS;
4755	}
4756
4757	return ret;
4758}
4759EXPORT_SYMBOL(rdma_reject);
4760
4761int rdma_disconnect(struct rdma_cm_id *id)
4762{
4763	struct rdma_id_private *id_priv;
4764	int ret;
4765
4766	id_priv = container_of(id, struct rdma_id_private, id);
4767	if (!id_priv->cm_id.ib)
4768		return -EINVAL;
4769
4770	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4771		ret = cma_modify_qp_err(id_priv);
4772		if (ret)
4773			goto out;
4774		/* Initiate or respond to a disconnect. */
4775		trace_cm_disconnect(id_priv);
4776		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
4777			if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0))
4778				trace_cm_sent_drep(id_priv);
4779		} else {
4780			trace_cm_sent_dreq(id_priv);
4781		}
4782	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4783		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
4784	} else
4785		ret = -EINVAL;
4786
4787out:
4788	return ret;
4789}
4790EXPORT_SYMBOL(rdma_disconnect);
4791
4792static void cma_make_mc_event(int status, struct rdma_id_private *id_priv,
4793			      struct ib_sa_multicast *multicast,
4794			      struct rdma_cm_event *event,
4795			      struct cma_multicast *mc)
4796{
4797	struct rdma_dev_addr *dev_addr;
4798	enum ib_gid_type gid_type;
4799	struct net_device *ndev;
4800
4801	if (status)
4802		pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4803				     status);
4804
4805	event->status = status;
4806	event->param.ud.private_data = mc->context;
4807	if (status) {
4808		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4809		return;
4810	}
4811
4812	dev_addr = &id_priv->id.route.addr.dev_addr;
4813	ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4814	gid_type =
4815		id_priv->cma_dev
4816			->default_gid_type[id_priv->id.port_num -
4817					   rdma_start_port(
4818						   id_priv->cma_dev->device)];
4819
4820	event->event = RDMA_CM_EVENT_MULTICAST_JOIN;
4821	if (ib_init_ah_from_mcmember(id_priv->id.device, id_priv->id.port_num,
4822				     &multicast->rec, ndev, gid_type,
4823				     &event->param.ud.ah_attr)) {
4824		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4825		goto out;
4826	}
4827
4828	event->param.ud.qp_num = 0xFFFFFF;
4829	event->param.ud.qkey = id_priv->qkey;
4830
4831out:
4832	dev_put(ndev);
4833}
4834
4835static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
4836{
 
4837	struct cma_multicast *mc = multicast->context;
4838	struct rdma_id_private *id_priv = mc->id_priv;
4839	struct rdma_cm_event event = {};
4840	int ret = 0;
4841
 
4842	mutex_lock(&id_priv->handler_mutex);
4843	if (READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL ||
4844	    READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING)
4845		goto out;
4846
4847	ret = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
4848	if (!ret) {
4849		cma_make_mc_event(status, id_priv, multicast, &event, mc);
4850		ret = cma_cm_event_handler(id_priv, &event);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4851	}
4852	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4853	WARN_ON(ret);
4854
4855out:
4856	mutex_unlock(&id_priv->handler_mutex);
4857	return 0;
4858}
4859
4860static void cma_set_mgid(struct rdma_id_private *id_priv,
4861			 struct sockaddr *addr, union ib_gid *mgid)
4862{
4863	unsigned char mc_map[MAX_ADDR_LEN];
4864	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4865	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
4866	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
4867
4868	if (cma_any_addr(addr)) {
4869		memset(mgid, 0, sizeof *mgid);
4870	} else if ((addr->sa_family == AF_INET6) &&
4871		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
4872								 0xFF10A01B)) {
4873		/* IPv6 address is an SA assigned MGID. */
4874		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4875	} else if (addr->sa_family == AF_IB) {
4876		memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
4877	} else if (addr->sa_family == AF_INET6) {
4878		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
4879		if (id_priv->id.ps == RDMA_PS_UDP)
4880			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4881		*mgid = *(union ib_gid *) (mc_map + 4);
4882	} else {
4883		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
4884		if (id_priv->id.ps == RDMA_PS_UDP)
4885			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4886		*mgid = *(union ib_gid *) (mc_map + 4);
4887	}
4888}
4889
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4890static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
4891				 struct cma_multicast *mc)
4892{
4893	struct ib_sa_mcmember_rec rec;
 
4894	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4895	ib_sa_comp_mask comp_mask;
4896	int ret;
4897
4898	ib_addr_get_mgid(dev_addr, &rec.mgid);
4899	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
4900				     &rec.mgid, &rec);
4901	if (ret)
4902		return ret;
4903
4904	if (!id_priv->qkey) {
4905		ret = cma_set_default_qkey(id_priv);
4906		if (ret)
4907			return ret;
4908	}
4909
4910	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
4911	rec.qkey = cpu_to_be32(id_priv->qkey);
4912	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
4913	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
4914	rec.join_state = mc->join_state;
4915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4916	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
4917		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
4918		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
4919		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
4920		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
4921
4922	if (id_priv->id.ps == RDMA_PS_IPOIB)
4923		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
4924			     IB_SA_MCMEMBER_REC_RATE_SELECTOR |
4925			     IB_SA_MCMEMBER_REC_MTU_SELECTOR |
4926			     IB_SA_MCMEMBER_REC_MTU |
4927			     IB_SA_MCMEMBER_REC_HOP_LIMIT;
4928
4929	mc->sa_mc = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4930					 id_priv->id.port_num, &rec, comp_mask,
4931					 GFP_KERNEL, cma_ib_mc_handler, mc);
4932	return PTR_ERR_OR_ZERO(mc->sa_mc);
 
 
 
 
 
 
 
 
 
 
 
 
 
4933}
4934
4935static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4936			      enum ib_gid_type gid_type)
4937{
4938	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4939	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4940
4941	if (cma_any_addr(addr)) {
4942		memset(mgid, 0, sizeof *mgid);
4943	} else if (addr->sa_family == AF_INET6) {
4944		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4945	} else {
4946		mgid->raw[0] =
4947			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
4948		mgid->raw[1] =
4949			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
4950		mgid->raw[2] = 0;
4951		mgid->raw[3] = 0;
4952		mgid->raw[4] = 0;
4953		mgid->raw[5] = 0;
4954		mgid->raw[6] = 0;
4955		mgid->raw[7] = 0;
4956		mgid->raw[8] = 0;
4957		mgid->raw[9] = 0;
4958		mgid->raw[10] = 0xff;
4959		mgid->raw[11] = 0xff;
4960		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4961	}
4962}
4963
4964static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4965				   struct cma_multicast *mc)
4966{
 
4967	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4968	int err = 0;
4969	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4970	struct net_device *ndev = NULL;
4971	struct ib_sa_multicast ib = {};
4972	enum ib_gid_type gid_type;
4973	bool send_only;
4974
4975	send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4976
4977	if (cma_zero_addr(addr))
4978		return -EINVAL;
4979
4980	gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4981		   rdma_start_port(id_priv->cma_dev->device)];
4982	cma_iboe_set_mgid(addr, &ib.rec.mgid, gid_type);
4983
4984	ib.rec.pkey = cpu_to_be16(0xffff);
4985	if (dev_addr->bound_dev_if)
4986		ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4987	if (!ndev)
4988		return -ENODEV;
 
 
4989
4990	ib.rec.rate = IB_RATE_PORT_CURRENT;
4991	ib.rec.hop_limit = 1;
4992	ib.rec.mtu = iboe_get_mtu(ndev->mtu);
4993
 
 
 
 
 
 
 
 
 
 
 
 
4994	if (addr->sa_family == AF_INET) {
4995		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4996			ib.rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4997			if (!send_only) {
4998				err = cma_igmp_send(ndev, &ib.rec.mgid,
4999						    true);
 
 
5000			}
5001		}
5002	} else {
5003		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
5004			err = -ENOTSUPP;
5005	}
5006	dev_put(ndev);
5007	if (err || !ib.rec.mtu)
5008		return err ?: -EINVAL;
5009
5010	if (!id_priv->qkey)
5011		cma_set_default_qkey(id_priv);
5012
5013	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
5014		    &ib.rec.port_gid);
5015	INIT_WORK(&mc->iboe_join.work, cma_iboe_join_work_handler);
5016	cma_make_mc_event(0, id_priv, &ib, &mc->iboe_join.event, mc);
5017	queue_work(cma_wq, &mc->iboe_join.work);
 
 
 
5018	return 0;
 
 
 
 
 
 
5019}
5020
5021int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
5022			u8 join_state, void *context)
5023{
5024	struct rdma_id_private *id_priv =
5025		container_of(id, struct rdma_id_private, id);
5026	struct cma_multicast *mc;
5027	int ret;
5028
5029	/* Not supported for kernel QPs */
5030	if (WARN_ON(id->qp))
5031		return -EINVAL;
5032
5033	/* ULP is calling this wrong. */
5034	if (!id->device || (READ_ONCE(id_priv->state) != RDMA_CM_ADDR_BOUND &&
5035			    READ_ONCE(id_priv->state) != RDMA_CM_ADDR_RESOLVED))
5036		return -EINVAL;
5037
5038	if (id_priv->id.qp_type != IB_QPT_UD)
5039		return -EINVAL;
5040
5041	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
5042	if (!mc)
5043		return -ENOMEM;
5044
5045	memcpy(&mc->addr, addr, rdma_addr_size(addr));
5046	mc->context = context;
5047	mc->id_priv = id_priv;
 
5048	mc->join_state = join_state;
 
 
 
5049
5050	if (rdma_protocol_roce(id->device, id->port_num)) {
 
5051		ret = cma_iboe_join_multicast(id_priv, mc);
5052		if (ret)
5053			goto out_err;
5054	} else if (rdma_cap_ib_mcast(id->device, id->port_num)) {
5055		ret = cma_join_ib_multicast(id_priv, mc);
5056		if (ret)
5057			goto out_err;
5058	} else {
5059		ret = -ENOSYS;
5060		goto out_err;
5061	}
5062
5063	spin_lock(&id_priv->lock);
5064	list_add(&mc->list, &id_priv->mc_list);
5065	spin_unlock(&id_priv->lock);
5066
5067	return 0;
5068out_err:
5069	kfree(mc);
 
 
 
5070	return ret;
5071}
5072EXPORT_SYMBOL(rdma_join_multicast);
5073
5074void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
5075{
5076	struct rdma_id_private *id_priv;
5077	struct cma_multicast *mc;
5078
5079	id_priv = container_of(id, struct rdma_id_private, id);
5080	spin_lock_irq(&id_priv->lock);
5081	list_for_each_entry(mc, &id_priv->mc_list, list) {
5082		if (memcmp(&mc->addr, addr, rdma_addr_size(addr)) != 0)
5083			continue;
5084		list_del(&mc->list);
5085		spin_unlock_irq(&id_priv->lock);
5086
5087		WARN_ON(id_priv->cma_dev->device != id->device);
5088		destroy_mc(id_priv, mc);
5089		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5090	}
5091	spin_unlock_irq(&id_priv->lock);
5092}
5093EXPORT_SYMBOL(rdma_leave_multicast);
5094
5095static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
5096{
5097	struct rdma_dev_addr *dev_addr;
5098	struct cma_work *work;
5099
5100	dev_addr = &id_priv->id.route.addr.dev_addr;
5101
5102	if ((dev_addr->bound_dev_if == ndev->ifindex) &&
5103	    (net_eq(dev_net(ndev), dev_addr->net)) &&
5104	    memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
5105		pr_info("RDMA CM addr change for ndev %s used by id %p\n",
5106			ndev->name, &id_priv->id);
5107		work = kzalloc(sizeof *work, GFP_KERNEL);
5108		if (!work)
5109			return -ENOMEM;
5110
5111		INIT_WORK(&work->work, cma_work_handler);
5112		work->id = id_priv;
5113		work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
5114		cma_id_get(id_priv);
5115		queue_work(cma_wq, &work->work);
5116	}
5117
5118	return 0;
5119}
5120
5121static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
5122			       void *ptr)
5123{
5124	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
5125	struct cma_device *cma_dev;
5126	struct rdma_id_private *id_priv;
5127	int ret = NOTIFY_DONE;
5128
5129	if (event != NETDEV_BONDING_FAILOVER)
5130		return NOTIFY_DONE;
5131
5132	if (!netif_is_bond_master(ndev))
5133		return NOTIFY_DONE;
5134
5135	mutex_lock(&lock);
5136	list_for_each_entry(cma_dev, &dev_list, list)
5137		list_for_each_entry(id_priv, &cma_dev->id_list, device_item) {
5138			ret = cma_netdev_change(ndev, id_priv);
5139			if (ret)
5140				goto out;
5141		}
5142
5143out:
5144	mutex_unlock(&lock);
5145	return ret;
5146}
5147
5148static void cma_netevent_work_handler(struct work_struct *_work)
5149{
5150	struct rdma_id_private *id_priv =
5151		container_of(_work, struct rdma_id_private, id.net_work);
5152	struct rdma_cm_event event = {};
5153
5154	mutex_lock(&id_priv->handler_mutex);
5155
5156	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
5157	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
5158		goto out_unlock;
 
 
 
5159
5160	event.event = RDMA_CM_EVENT_UNREACHABLE;
5161	event.status = -ETIMEDOUT;
 
5162
5163	if (cma_cm_event_handler(id_priv, &event)) {
5164		__acquire(&id_priv->handler_mutex);
5165		id_priv->cm_id.ib = NULL;
5166		cma_id_put(id_priv);
5167		destroy_id_handler_unlock(id_priv);
 
5168		return;
5169	}
 
 
 
 
 
 
5170
5171out_unlock:
5172	mutex_unlock(&id_priv->handler_mutex);
5173	cma_id_put(id_priv);
 
 
 
 
 
 
 
5174}
5175
5176static int cma_netevent_callback(struct notifier_block *self,
5177				 unsigned long event, void *ctx)
5178{
5179	struct id_table_entry *ips_node = NULL;
5180	struct rdma_id_private *current_id;
5181	struct neighbour *neigh = ctx;
5182	unsigned long flags;
5183
5184	if (event != NETEVENT_NEIGH_UPDATE)
5185		return NOTIFY_DONE;
 
 
5186
5187	spin_lock_irqsave(&id_table_lock, flags);
5188	if (neigh->tbl->family == AF_INET6) {
5189		struct sockaddr_in6 neigh_sock_6;
5190
5191		neigh_sock_6.sin6_family = AF_INET6;
5192		neigh_sock_6.sin6_addr = *(struct in6_addr *)neigh->primary_key;
5193		ips_node = node_from_ndev_ip(&id_table, neigh->dev->ifindex,
5194					     (struct sockaddr *)&neigh_sock_6);
5195	} else if (neigh->tbl->family == AF_INET) {
5196		struct sockaddr_in neigh_sock_4;
5197
5198		neigh_sock_4.sin_family = AF_INET;
5199		neigh_sock_4.sin_addr.s_addr = *(__be32 *)(neigh->primary_key);
5200		ips_node = node_from_ndev_ip(&id_table, neigh->dev->ifindex,
5201					     (struct sockaddr *)&neigh_sock_4);
5202	} else
5203		goto out;
5204
5205	if (!ips_node)
 
5206		goto out;
5207
5208	list_for_each_entry(current_id, &ips_node->id_list, id_list_entry) {
5209		if (!memcmp(current_id->id.route.addr.dev_addr.dst_dev_addr,
5210			   neigh->ha, ETH_ALEN))
5211			continue;
5212		INIT_WORK(&current_id->id.net_work, cma_netevent_work_handler);
5213		cma_id_get(current_id);
5214		queue_work(cma_wq, &current_id->id.net_work);
5215	}
5216out:
5217	spin_unlock_irqrestore(&id_table_lock, flags);
5218	return NOTIFY_DONE;
5219}
5220
5221static struct notifier_block cma_nb = {
5222	.notifier_call = cma_netdev_callback
5223};
5224
5225static struct notifier_block cma_netevent_cb = {
5226	.notifier_call = cma_netevent_callback
5227};
5228
5229static void cma_send_device_removal_put(struct rdma_id_private *id_priv)
5230{
5231	struct rdma_cm_event event = { .event = RDMA_CM_EVENT_DEVICE_REMOVAL };
5232	enum rdma_cm_state state;
5233	unsigned long flags;
5234
5235	mutex_lock(&id_priv->handler_mutex);
5236	/* Record that we want to remove the device */
5237	spin_lock_irqsave(&id_priv->lock, flags);
5238	state = id_priv->state;
5239	if (state == RDMA_CM_DESTROYING || state == RDMA_CM_DEVICE_REMOVAL) {
5240		spin_unlock_irqrestore(&id_priv->lock, flags);
5241		mutex_unlock(&id_priv->handler_mutex);
5242		cma_id_put(id_priv);
5243		return;
5244	}
5245	id_priv->state = RDMA_CM_DEVICE_REMOVAL;
5246	spin_unlock_irqrestore(&id_priv->lock, flags);
5247
5248	if (cma_cm_event_handler(id_priv, &event)) {
5249		/*
5250		 * At this point the ULP promises it won't call
5251		 * rdma_destroy_id() concurrently
5252		 */
5253		cma_id_put(id_priv);
5254		mutex_unlock(&id_priv->handler_mutex);
5255		trace_cm_id_destroy(id_priv);
5256		_destroy_id(id_priv, state);
5257		return;
5258	}
5259	mutex_unlock(&id_priv->handler_mutex);
5260
5261	/*
5262	 * If this races with destroy then the thread that first assigns state
5263	 * to a destroying does the cancel.
5264	 */
5265	cma_cancel_operation(id_priv, state);
5266	cma_id_put(id_priv);
5267}
5268
5269static void cma_process_remove(struct cma_device *cma_dev)
5270{
 
 
 
5271	mutex_lock(&lock);
5272	while (!list_empty(&cma_dev->id_list)) {
5273		struct rdma_id_private *id_priv = list_first_entry(
5274			&cma_dev->id_list, struct rdma_id_private, device_item);
5275
5276		list_del_init(&id_priv->listen_item);
5277		list_del_init(&id_priv->device_item);
5278		cma_id_get(id_priv);
5279		mutex_unlock(&lock);
5280
5281		cma_send_device_removal_put(id_priv);
 
 
 
5282
5283		mutex_lock(&lock);
5284	}
5285	mutex_unlock(&lock);
5286
5287	cma_dev_put(cma_dev);
5288	wait_for_completion(&cma_dev->comp);
5289}
5290
5291static bool cma_supported(struct ib_device *device)
5292{
5293	u32 i;
5294
5295	rdma_for_each_port(device, i) {
5296		if (rdma_cap_ib_cm(device, i) || rdma_cap_iw_cm(device, i))
5297			return true;
5298	}
5299	return false;
5300}
5301
5302static int cma_add_one(struct ib_device *device)
5303{
5304	struct rdma_id_private *to_destroy;
5305	struct cma_device *cma_dev;
5306	struct rdma_id_private *id_priv;
5307	unsigned long supported_gids = 0;
5308	int ret;
5309	u32 i;
5310
5311	if (!cma_supported(device))
5312		return -EOPNOTSUPP;
5313
5314	cma_dev = kmalloc(sizeof(*cma_dev), GFP_KERNEL);
5315	if (!cma_dev)
5316		return -ENOMEM;
5317
5318	cma_dev->device = device;
5319	cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
5320					    sizeof(*cma_dev->default_gid_type),
5321					    GFP_KERNEL);
5322	if (!cma_dev->default_gid_type) {
5323		ret = -ENOMEM;
5324		goto free_cma_dev;
5325	}
5326
5327	cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
5328					    sizeof(*cma_dev->default_roce_tos),
5329					    GFP_KERNEL);
5330	if (!cma_dev->default_roce_tos) {
5331		ret = -ENOMEM;
5332		goto free_gid_type;
5333	}
5334
5335	rdma_for_each_port (device, i) {
5336		supported_gids = roce_gid_type_mask_support(device, i);
5337		WARN_ON(!supported_gids);
5338		if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))
5339			cma_dev->default_gid_type[i - rdma_start_port(device)] =
5340				CMA_PREFERRED_ROCE_GID_TYPE;
5341		else
5342			cma_dev->default_gid_type[i - rdma_start_port(device)] =
5343				find_first_bit(&supported_gids, BITS_PER_LONG);
5344		cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
5345	}
5346
5347	init_completion(&cma_dev->comp);
5348	refcount_set(&cma_dev->refcount, 1);
5349	INIT_LIST_HEAD(&cma_dev->id_list);
5350	ib_set_client_data(device, &cma_client, cma_dev);
5351
5352	mutex_lock(&lock);
5353	list_add_tail(&cma_dev->list, &dev_list);
5354	list_for_each_entry(id_priv, &listen_any_list, listen_any_item) {
5355		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
5356		if (ret)
5357			goto free_listen;
5358	}
5359	mutex_unlock(&lock);
5360
5361	trace_cm_add_one(device);
5362	return 0;
5363
5364free_listen:
5365	list_del(&cma_dev->list);
5366	mutex_unlock(&lock);
5367
5368	/* cma_process_remove() will delete to_destroy */
5369	cma_process_remove(cma_dev);
5370	kfree(cma_dev->default_roce_tos);
5371free_gid_type:
5372	kfree(cma_dev->default_gid_type);
5373
5374free_cma_dev:
5375	kfree(cma_dev);
5376	return ret;
5377}
5378
5379static void cma_remove_one(struct ib_device *device, void *client_data)
5380{
5381	struct cma_device *cma_dev = client_data;
5382
5383	trace_cm_remove_one(device);
 
 
 
5384
 
 
 
 
5385	mutex_lock(&lock);
5386	list_del(&cma_dev->list);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5387	mutex_unlock(&lock);
 
 
5388
5389	cma_process_remove(cma_dev);
5390	kfree(cma_dev->default_roce_tos);
5391	kfree(cma_dev->default_gid_type);
5392	kfree(cma_dev);
5393}
5394
 
 
 
 
 
5395static int cma_init_net(struct net *net)
5396{
5397	struct cma_pernet *pernet = cma_pernet(net);
5398
5399	xa_init(&pernet->tcp_ps);
5400	xa_init(&pernet->udp_ps);
5401	xa_init(&pernet->ipoib_ps);
5402	xa_init(&pernet->ib_ps);
5403
5404	return 0;
5405}
5406
5407static void cma_exit_net(struct net *net)
5408{
5409	struct cma_pernet *pernet = cma_pernet(net);
5410
5411	WARN_ON(!xa_empty(&pernet->tcp_ps));
5412	WARN_ON(!xa_empty(&pernet->udp_ps));
5413	WARN_ON(!xa_empty(&pernet->ipoib_ps));
5414	WARN_ON(!xa_empty(&pernet->ib_ps));
5415}
5416
5417static struct pernet_operations cma_pernet_operations = {
5418	.init = cma_init_net,
5419	.exit = cma_exit_net,
5420	.id = &cma_pernet_id,
5421	.size = sizeof(struct cma_pernet),
5422};
5423
5424static int __init cma_init(void)
5425{
5426	int ret;
5427
5428	/*
5429	 * There is a rare lock ordering dependency in cma_netdev_callback()
5430	 * that only happens when bonding is enabled. Teach lockdep that rtnl
5431	 * must never be nested under lock so it can find these without having
5432	 * to test with bonding.
5433	 */
5434	if (IS_ENABLED(CONFIG_LOCKDEP)) {
5435		rtnl_lock();
5436		mutex_lock(&lock);
5437		mutex_unlock(&lock);
5438		rtnl_unlock();
5439	}
5440
5441	cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
5442	if (!cma_wq)
5443		return -ENOMEM;
5444
5445	ret = register_pernet_subsys(&cma_pernet_operations);
5446	if (ret)
5447		goto err_wq;
5448
5449	ib_sa_register_client(&sa_client);
 
5450	register_netdevice_notifier(&cma_nb);
5451	register_netevent_notifier(&cma_netevent_cb);
5452
5453	ret = ib_register_client(&cma_client);
5454	if (ret)
5455		goto err;
5456
5457	ret = cma_configfs_init();
5458	if (ret)
5459		goto err_ib;
 
5460
5461	return 0;
5462
5463err_ib:
5464	ib_unregister_client(&cma_client);
5465err:
5466	unregister_netevent_notifier(&cma_netevent_cb);
5467	unregister_netdevice_notifier(&cma_nb);
 
5468	ib_sa_unregister_client(&sa_client);
5469	unregister_pernet_subsys(&cma_pernet_operations);
5470err_wq:
5471	destroy_workqueue(cma_wq);
5472	return ret;
5473}
5474
5475static void __exit cma_cleanup(void)
5476{
5477	cma_configfs_exit();
 
5478	ib_unregister_client(&cma_client);
5479	unregister_netevent_notifier(&cma_netevent_cb);
5480	unregister_netdevice_notifier(&cma_nb);
 
5481	ib_sa_unregister_client(&sa_client);
5482	unregister_pernet_subsys(&cma_pernet_operations);
5483	destroy_workqueue(cma_wq);
5484}
5485
5486module_init(cma_init);
5487module_exit(cma_cleanup);
v4.10.11
 
   1/*
   2 * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
   3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
   4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
   5 * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
   6 *
   7 * This software is available to you under a choice of one of two
   8 * licenses.  You may choose to be licensed under the terms of the GNU
   9 * General Public License (GPL) Version 2, available from the file
  10 * COPYING in the main directory of this source tree, or the
  11 * OpenIB.org BSD license below:
  12 *
  13 *     Redistribution and use in source and binary forms, with or
  14 *     without modification, are permitted provided that the following
  15 *     conditions are met:
  16 *
  17 *      - Redistributions of source code must retain the above
  18 *        copyright notice, this list of conditions and the following
  19 *        disclaimer.
  20 *
  21 *      - Redistributions in binary form must reproduce the above
  22 *        copyright notice, this list of conditions and the following
  23 *        disclaimer in the documentation and/or other materials
  24 *        provided with the distribution.
  25 *
  26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33 * SOFTWARE.
  34 */
  35
  36#include <linux/completion.h>
  37#include <linux/in.h>
  38#include <linux/in6.h>
  39#include <linux/mutex.h>
  40#include <linux/random.h>
 
  41#include <linux/igmp.h>
  42#include <linux/idr.h>
  43#include <linux/inetdevice.h>
  44#include <linux/slab.h>
  45#include <linux/module.h>
  46#include <net/route.h>
  47
  48#include <net/net_namespace.h>
  49#include <net/netns/generic.h>
 
  50#include <net/tcp.h>
  51#include <net/ipv6.h>
  52#include <net/ip_fib.h>
  53#include <net/ip6_route.h>
  54
  55#include <rdma/rdma_cm.h>
  56#include <rdma/rdma_cm_ib.h>
  57#include <rdma/rdma_netlink.h>
  58#include <rdma/ib.h>
  59#include <rdma/ib_cache.h>
  60#include <rdma/ib_cm.h>
  61#include <rdma/ib_sa.h>
  62#include <rdma/iw_cm.h>
  63
  64#include "core_priv.h"
 
 
  65
  66MODULE_AUTHOR("Sean Hefty");
  67MODULE_DESCRIPTION("Generic RDMA CM Agent");
  68MODULE_LICENSE("Dual BSD/GPL");
  69
  70#define CMA_CM_RESPONSE_TIMEOUT 20
  71#define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
  72#define CMA_MAX_CM_RETRIES 15
  73#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
  74#define CMA_IBOE_PACKET_LIFETIME 18
 
  75
  76static const char * const cma_events[] = {
  77	[RDMA_CM_EVENT_ADDR_RESOLVED]	 = "address resolved",
  78	[RDMA_CM_EVENT_ADDR_ERROR]	 = "address error",
  79	[RDMA_CM_EVENT_ROUTE_RESOLVED]	 = "route resolved ",
  80	[RDMA_CM_EVENT_ROUTE_ERROR]	 = "route error",
  81	[RDMA_CM_EVENT_CONNECT_REQUEST]	 = "connect request",
  82	[RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
  83	[RDMA_CM_EVENT_CONNECT_ERROR]	 = "connect error",
  84	[RDMA_CM_EVENT_UNREACHABLE]	 = "unreachable",
  85	[RDMA_CM_EVENT_REJECTED]	 = "rejected",
  86	[RDMA_CM_EVENT_ESTABLISHED]	 = "established",
  87	[RDMA_CM_EVENT_DISCONNECTED]	 = "disconnected",
  88	[RDMA_CM_EVENT_DEVICE_REMOVAL]	 = "device removal",
  89	[RDMA_CM_EVENT_MULTICAST_JOIN]	 = "multicast join",
  90	[RDMA_CM_EVENT_MULTICAST_ERROR]	 = "multicast error",
  91	[RDMA_CM_EVENT_ADDR_CHANGE]	 = "address change",
  92	[RDMA_CM_EVENT_TIMEWAIT_EXIT]	 = "timewait exit",
  93};
  94
 
 
 
  95const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
  96{
  97	size_t index = event;
  98
  99	return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
 100			cma_events[index] : "unrecognized event";
 101}
 102EXPORT_SYMBOL(rdma_event_msg);
 103
 104const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,
 105						int reason)
 106{
 107	if (rdma_ib_or_roce(id->device, id->port_num))
 108		return ibcm_reject_msg(reason);
 109
 110	if (rdma_protocol_iwarp(id->device, id->port_num))
 111		return iwcm_reject_msg(reason);
 112
 113	WARN_ON_ONCE(1);
 114	return "unrecognized transport";
 115}
 116EXPORT_SYMBOL(rdma_reject_msg);
 117
 118bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason)
 
 
 
 
 
 
 119{
 120	if (rdma_ib_or_roce(id->device, id->port_num))
 121		return reason == IB_CM_REJ_CONSUMER_DEFINED;
 122
 123	if (rdma_protocol_iwarp(id->device, id->port_num))
 124		return reason == -ECONNREFUSED;
 125
 126	WARN_ON_ONCE(1);
 127	return false;
 128}
 129EXPORT_SYMBOL(rdma_is_consumer_reject);
 130
 131const void *rdma_consumer_reject_data(struct rdma_cm_id *id,
 132				      struct rdma_cm_event *ev, u8 *data_len)
 133{
 134	const void *p;
 135
 136	if (rdma_is_consumer_reject(id, ev->status)) {
 137		*data_len = ev->param.conn.private_data_len;
 138		p = ev->param.conn.private_data;
 139	} else {
 140		*data_len = 0;
 141		p = NULL;
 142	}
 143	return p;
 144}
 145EXPORT_SYMBOL(rdma_consumer_reject_data);
 146
 147static void cma_add_one(struct ib_device *device);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 148static void cma_remove_one(struct ib_device *device, void *client_data);
 149
 150static struct ib_client cma_client = {
 151	.name   = "cma",
 152	.add    = cma_add_one,
 153	.remove = cma_remove_one
 154};
 155
 156static struct ib_sa_client sa_client;
 157static struct rdma_addr_client addr_client;
 158static LIST_HEAD(dev_list);
 159static LIST_HEAD(listen_any_list);
 160static DEFINE_MUTEX(lock);
 
 
 
 161static struct workqueue_struct *cma_wq;
 162static unsigned int cma_pernet_id;
 163
 164struct cma_pernet {
 165	struct idr tcp_ps;
 166	struct idr udp_ps;
 167	struct idr ipoib_ps;
 168	struct idr ib_ps;
 169};
 170
 171static struct cma_pernet *cma_pernet(struct net *net)
 172{
 173	return net_generic(net, cma_pernet_id);
 174}
 175
 176static struct idr *cma_pernet_idr(struct net *net, enum rdma_port_space ps)
 
 177{
 178	struct cma_pernet *pernet = cma_pernet(net);
 179
 180	switch (ps) {
 181	case RDMA_PS_TCP:
 182		return &pernet->tcp_ps;
 183	case RDMA_PS_UDP:
 184		return &pernet->udp_ps;
 185	case RDMA_PS_IPOIB:
 186		return &pernet->ipoib_ps;
 187	case RDMA_PS_IB:
 188		return &pernet->ib_ps;
 189	default:
 190		return NULL;
 191	}
 192}
 193
 
 
 
 
 
 194struct cma_device {
 195	struct list_head	list;
 196	struct ib_device	*device;
 197	struct completion	comp;
 198	atomic_t		refcount;
 199	struct list_head	id_list;
 200	enum ib_gid_type	*default_gid_type;
 
 201};
 202
 203struct rdma_bind_list {
 204	enum rdma_port_space	ps;
 205	struct hlist_head	owners;
 206	unsigned short		port;
 207};
 208
 209struct class_port_info_context {
 210	struct ib_class_port_info	*class_port_info;
 211	struct ib_device		*device;
 212	struct completion		done;
 213	struct ib_sa_query		*sa_query;
 214	u8				port_num;
 215};
 216
 217static int cma_ps_alloc(struct net *net, enum rdma_port_space ps,
 218			struct rdma_bind_list *bind_list, int snum)
 219{
 220	struct idr *idr = cma_pernet_idr(net, ps);
 221
 222	return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL);
 223}
 224
 225static struct rdma_bind_list *cma_ps_find(struct net *net,
 226					  enum rdma_port_space ps, int snum)
 227{
 228	struct idr *idr = cma_pernet_idr(net, ps);
 229
 230	return idr_find(idr, snum);
 231}
 232
 233static void cma_ps_remove(struct net *net, enum rdma_port_space ps, int snum)
 
 234{
 235	struct idr *idr = cma_pernet_idr(net, ps);
 236
 237	idr_remove(idr, snum);
 238}
 239
 240enum {
 241	CMA_OPTION_AFONLY,
 242};
 243
 244void cma_ref_dev(struct cma_device *cma_dev)
 245{
 246	atomic_inc(&cma_dev->refcount);
 
 
 
 
 
 
 247}
 248
 249struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter	filter,
 250					     void		*cookie)
 251{
 252	struct cma_device *cma_dev;
 253	struct cma_device *found_cma_dev = NULL;
 254
 255	mutex_lock(&lock);
 256
 257	list_for_each_entry(cma_dev, &dev_list, list)
 258		if (filter(cma_dev->device, cookie)) {
 259			found_cma_dev = cma_dev;
 260			break;
 261		}
 262
 263	if (found_cma_dev)
 264		cma_ref_dev(found_cma_dev);
 265	mutex_unlock(&lock);
 266	return found_cma_dev;
 267}
 268
 269int cma_get_default_gid_type(struct cma_device *cma_dev,
 270			     unsigned int port)
 271{
 272	if (port < rdma_start_port(cma_dev->device) ||
 273	    port > rdma_end_port(cma_dev->device))
 274		return -EINVAL;
 275
 276	return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
 277}
 278
 279int cma_set_default_gid_type(struct cma_device *cma_dev,
 280			     unsigned int port,
 281			     enum ib_gid_type default_gid_type)
 282{
 283	unsigned long supported_gids;
 284
 285	if (port < rdma_start_port(cma_dev->device) ||
 286	    port > rdma_end_port(cma_dev->device))
 287		return -EINVAL;
 288
 
 
 
 
 289	supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
 290
 291	if (!(supported_gids & 1 << default_gid_type))
 292		return -EINVAL;
 293
 294	cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
 295		default_gid_type;
 296
 297	return 0;
 298}
 299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 300struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
 301{
 302	return cma_dev->device;
 303}
 304
 305/*
 306 * Device removal can occur at anytime, so we need extra handling to
 307 * serialize notifying the user of device removal with other callbacks.
 308 * We do this by disabling removal notification while a callback is in process,
 309 * and reporting it after the callback completes.
 310 */
 311struct rdma_id_private {
 312	struct rdma_cm_id	id;
 313
 314	struct rdma_bind_list	*bind_list;
 315	struct hlist_node	node;
 316	struct list_head	list; /* listen_any_list or cma_device.list */
 317	struct list_head	listen_list; /* per device listens */
 318	struct cma_device	*cma_dev;
 319	struct list_head	mc_list;
 320
 321	int			internal_id;
 322	enum rdma_cm_state	state;
 323	spinlock_t		lock;
 324	struct mutex		qp_mutex;
 325
 326	struct completion	comp;
 327	atomic_t		refcount;
 328	struct mutex		handler_mutex;
 329
 330	int			backlog;
 331	int			timeout_ms;
 332	struct ib_sa_query	*query;
 333	int			query_id;
 334	union {
 335		struct ib_cm_id	*ib;
 336		struct iw_cm_id	*iw;
 337	} cm_id;
 338
 339	u32			seq_num;
 340	u32			qkey;
 341	u32			qp_num;
 342	pid_t			owner;
 343	u32			options;
 344	u8			srq;
 345	u8			tos;
 346	u8			reuseaddr;
 347	u8			afonly;
 348	enum ib_gid_type	gid_type;
 349};
 350
 351struct cma_multicast {
 352	struct rdma_id_private *id_priv;
 353	union {
 354		struct ib_sa_multicast *ib;
 355	} multicast;
 
 
 
 
 356	struct list_head	list;
 357	void			*context;
 358	struct sockaddr_storage	addr;
 359	struct kref		mcref;
 360	bool			igmp_joined;
 361	u8			join_state;
 362};
 363
 364struct cma_work {
 365	struct work_struct	work;
 366	struct rdma_id_private	*id;
 367	enum rdma_cm_state	old_state;
 368	enum rdma_cm_state	new_state;
 369	struct rdma_cm_event	event;
 370};
 371
 372struct cma_ndev_work {
 373	struct work_struct	work;
 374	struct rdma_id_private	*id;
 375	struct rdma_cm_event	event;
 376};
 377
 378struct iboe_mcast_work {
 379	struct work_struct	 work;
 380	struct rdma_id_private	*id;
 381	struct cma_multicast	*mc;
 382};
 383
 384union cma_ip_addr {
 385	struct in6_addr ip6;
 386	struct {
 387		__be32 pad[3];
 388		__be32 addr;
 389	} ip4;
 390};
 391
 392struct cma_hdr {
 393	u8 cma_version;
 394	u8 ip_version;	/* IP version: 7:4 */
 395	__be16 port;
 396	union cma_ip_addr src_addr;
 397	union cma_ip_addr dst_addr;
 398};
 399
 400#define CMA_VERSION 0x00
 401
 402struct cma_req_info {
 
 
 403	struct ib_device *device;
 404	int port;
 405	union ib_gid local_gid;
 406	__be64 service_id;
 
 
 407	u16 pkey;
 408	bool has_gid:1;
 409};
 410
 411static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
 412{
 413	unsigned long flags;
 414	int ret;
 415
 416	spin_lock_irqsave(&id_priv->lock, flags);
 417	ret = (id_priv->state == comp);
 418	spin_unlock_irqrestore(&id_priv->lock, flags);
 419	return ret;
 420}
 421
 422static int cma_comp_exch(struct rdma_id_private *id_priv,
 423			 enum rdma_cm_state comp, enum rdma_cm_state exch)
 424{
 425	unsigned long flags;
 426	int ret;
 427
 
 
 
 
 
 
 
 
 
 428	spin_lock_irqsave(&id_priv->lock, flags);
 429	if ((ret = (id_priv->state == comp)))
 430		id_priv->state = exch;
 431	spin_unlock_irqrestore(&id_priv->lock, flags);
 432	return ret;
 433}
 434
 435static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
 436				   enum rdma_cm_state exch)
 437{
 438	unsigned long flags;
 439	enum rdma_cm_state old;
 440
 441	spin_lock_irqsave(&id_priv->lock, flags);
 442	old = id_priv->state;
 443	id_priv->state = exch;
 444	spin_unlock_irqrestore(&id_priv->lock, flags);
 445	return old;
 446}
 447
 448static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
 449{
 450	return hdr->ip_version >> 4;
 451}
 452
 453static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
 454{
 455	hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
 456}
 457
 458static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
 459{
 460	struct in_device *in_dev = NULL;
 461
 462	if (ndev) {
 463		rtnl_lock();
 464		in_dev = __in_dev_get_rtnl(ndev);
 465		if (in_dev) {
 466			if (join)
 467				ip_mc_inc_group(in_dev,
 468						*(__be32 *)(mgid->raw + 12));
 469			else
 470				ip_mc_dec_group(in_dev,
 471						*(__be32 *)(mgid->raw + 12));
 472		}
 473		rtnl_unlock();
 474	}
 475	return (in_dev) ? 0 : -ENODEV;
 476}
 477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 478static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
 479			       struct cma_device *cma_dev)
 480{
 481	cma_ref_dev(cma_dev);
 482	id_priv->cma_dev = cma_dev;
 483	id_priv->gid_type = 0;
 484	id_priv->id.device = cma_dev->device;
 485	id_priv->id.route.addr.dev_addr.transport =
 486		rdma_node_get_transport(cma_dev->device->node_type);
 487	list_add_tail(&id_priv->list, &cma_dev->id_list);
 
 
 488}
 489
 490static void cma_attach_to_dev(struct rdma_id_private *id_priv,
 491			      struct cma_device *cma_dev)
 492{
 493	_cma_attach_to_dev(id_priv, cma_dev);
 494	id_priv->gid_type =
 495		cma_dev->default_gid_type[id_priv->id.port_num -
 496					  rdma_start_port(cma_dev->device)];
 497}
 498
 499void cma_deref_dev(struct cma_device *cma_dev)
 500{
 501	if (atomic_dec_and_test(&cma_dev->refcount))
 502		complete(&cma_dev->comp);
 503}
 504
 505static inline void release_mc(struct kref *kref)
 506{
 507	struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
 508
 509	kfree(mc->multicast.ib);
 510	kfree(mc);
 511}
 512
 513static void cma_release_dev(struct rdma_id_private *id_priv)
 514{
 515	mutex_lock(&lock);
 516	list_del(&id_priv->list);
 517	cma_deref_dev(id_priv->cma_dev);
 518	id_priv->cma_dev = NULL;
 
 
 
 
 
 519	mutex_unlock(&lock);
 520}
 521
 522static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
 523{
 524	return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
 525}
 526
 527static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
 528{
 529	return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
 530}
 531
 532static inline unsigned short cma_family(struct rdma_id_private *id_priv)
 533{
 534	return id_priv->id.route.addr.src_addr.ss_family;
 535}
 536
 537static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
 538{
 539	struct ib_sa_mcmember_rec rec;
 540	int ret = 0;
 541
 542	if (id_priv->qkey) {
 543		if (qkey && id_priv->qkey != qkey)
 544			return -EINVAL;
 545		return 0;
 546	}
 547
 548	if (qkey) {
 549		id_priv->qkey = qkey;
 550		return 0;
 551	}
 552
 553	switch (id_priv->id.ps) {
 554	case RDMA_PS_UDP:
 555	case RDMA_PS_IB:
 556		id_priv->qkey = RDMA_UDP_QKEY;
 557		break;
 558	case RDMA_PS_IPOIB:
 559		ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
 560		ret = ib_sa_get_mcmember_rec(id_priv->id.device,
 561					     id_priv->id.port_num, &rec.mgid,
 562					     &rec);
 563		if (!ret)
 564			id_priv->qkey = be32_to_cpu(rec.qkey);
 565		break;
 566	default:
 567		break;
 568	}
 569	return ret;
 570}
 571
 
 
 
 
 
 
 
 
 
 
 572static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
 573{
 574	dev_addr->dev_type = ARPHRD_INFINIBAND;
 575	rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
 576	ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
 577}
 578
 579static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
 580{
 581	int ret;
 582
 583	if (addr->sa_family != AF_IB) {
 584		ret = rdma_translate_ip(addr, dev_addr, NULL);
 585	} else {
 586		cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
 587		ret = 0;
 588	}
 589
 590	return ret;
 591}
 592
 593static inline int cma_validate_port(struct ib_device *device, u8 port,
 594				    enum ib_gid_type gid_type,
 595				      union ib_gid *gid, int dev_type,
 596				      int bound_if_index)
 
 597{
 598	int ret = -ENODEV;
 
 
 
 599	struct net_device *ndev = NULL;
 600
 
 
 
 601	if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
 602		return ret;
 603
 604	if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
 605		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 606
 607	if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
 608		ndev = dev_get_by_index(&init_net, bound_if_index);
 609		if (ndev && ndev->flags & IFF_LOOPBACK) {
 610			pr_info("detected loopback device\n");
 611			dev_put(ndev);
 612
 613			if (!device->get_netdev)
 614				return -EOPNOTSUPP;
 615
 616			ndev = device->get_netdev(device, port);
 617			if (!ndev)
 618				return -ENODEV;
 619		}
 620	} else {
 621		gid_type = IB_GID_TYPE_IB;
 622	}
 623
 624	ret = ib_find_cached_gid_by_port(device, gid, gid_type, port,
 625					 ndev, NULL);
 
 
 
 626
 627	if (ndev)
 628		dev_put(ndev);
 629
 630	return ret;
 
 631}
 632
 633static int cma_acquire_dev(struct rdma_id_private *id_priv,
 634			   struct rdma_id_private *listen_id_priv)
 
 
 
 
 
 
 
 
 635{
 636	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
 
 
 637	struct cma_device *cma_dev;
 638	union ib_gid gid, iboe_gid, *gidp;
 639	int ret = -ENODEV;
 640	u8 port;
 641
 642	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
 643	    id_priv->id.ps == RDMA_PS_IPOIB)
 644		return -EINVAL;
 645
 646	mutex_lock(&lock);
 647	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
 648		    &iboe_gid);
 649
 650	memcpy(&gid, dev_addr->src_dev_addr +
 651	       rdma_addr_gid_offset(dev_addr), sizeof gid);
 652
 653	if (listen_id_priv) {
 654		cma_dev = listen_id_priv->cma_dev;
 655		port = listen_id_priv->id.port_num;
 656		gidp = rdma_protocol_roce(cma_dev->device, port) ?
 657		       &iboe_gid : &gid;
 658
 659		ret = cma_validate_port(cma_dev->device, port,
 660					rdma_protocol_ib(cma_dev->device, port) ?
 661					IB_GID_TYPE_IB :
 662					listen_id_priv->gid_type, gidp,
 663					dev_addr->dev_type,
 664					dev_addr->bound_dev_if);
 665		if (!ret) {
 666			id_priv->id.port_num = port;
 667			goto out;
 668		}
 669	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 670
 671	list_for_each_entry(cma_dev, &dev_list, list) {
 672		for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
 673			if (listen_id_priv &&
 674			    listen_id_priv->cma_dev == cma_dev &&
 675			    listen_id_priv->id.port_num == port)
 676				continue;
 677
 678			gidp = rdma_protocol_roce(cma_dev->device, port) ?
 679			       &iboe_gid : &gid;
 680
 681			ret = cma_validate_port(cma_dev->device, port,
 682						rdma_protocol_ib(cma_dev->device, port) ?
 683						IB_GID_TYPE_IB :
 684						cma_dev->default_gid_type[port - 1],
 685						gidp, dev_addr->dev_type,
 686						dev_addr->bound_dev_if);
 687			if (!ret) {
 688				id_priv->id.port_num = port;
 
 
 689				goto out;
 690			}
 691		}
 692	}
 693
 694out:
 695	if (!ret)
 696		cma_attach_to_dev(id_priv, cma_dev);
 
 
 697
 698	mutex_unlock(&lock);
 699	return ret;
 700}
 701
 702/*
 703 * Select the source IB device and address to reach the destination IB address.
 704 */
 705static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
 706{
 707	struct cma_device *cma_dev, *cur_dev;
 708	struct sockaddr_ib *addr;
 709	union ib_gid gid, sgid, *dgid;
 
 710	u16 pkey, index;
 711	u8 p;
 
 712	int i;
 713
 714	cma_dev = NULL;
 715	addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
 716	dgid = (union ib_gid *) &addr->sib_addr;
 717	pkey = ntohs(addr->sib_pkey);
 718
 
 719	list_for_each_entry(cur_dev, &dev_list, list) {
 720		for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
 721			if (!rdma_cap_af_ib(cur_dev->device, p))
 722				continue;
 723
 724			if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
 725				continue;
 726
 727			for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i,
 728						       &gid, NULL);
 729			     i++) {
 
 
 
 
 
 
 
 730				if (!memcmp(&gid, dgid, sizeof(gid))) {
 731					cma_dev = cur_dev;
 732					sgid = gid;
 733					id_priv->id.port_num = p;
 734					goto found;
 735				}
 736
 737				if (!cma_dev && (gid.global.subnet_prefix ==
 738						 dgid->global.subnet_prefix)) {
 
 739					cma_dev = cur_dev;
 740					sgid = gid;
 741					id_priv->id.port_num = p;
 
 742				}
 743			}
 744		}
 745	}
 746
 747	if (!cma_dev)
 748		return -ENODEV;
 749
 750found:
 751	cma_attach_to_dev(id_priv, cma_dev);
 752	addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
 753	memcpy(&addr->sib_addr, &sgid, sizeof sgid);
 
 
 754	cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
 755	return 0;
 756}
 757
 758static void cma_deref_id(struct rdma_id_private *id_priv)
 759{
 760	if (atomic_dec_and_test(&id_priv->refcount))
 
 
 
 
 
 761		complete(&id_priv->comp);
 762}
 763
 764struct rdma_cm_id *rdma_create_id(struct net *net,
 765				  rdma_cm_event_handler event_handler,
 766				  void *context, enum rdma_port_space ps,
 767				  enum ib_qp_type qp_type)
 768{
 769	struct rdma_id_private *id_priv;
 770
 771	id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
 772	if (!id_priv)
 773		return ERR_PTR(-ENOMEM);
 774
 775	id_priv->owner = task_pid_nr(current);
 776	id_priv->state = RDMA_CM_IDLE;
 777	id_priv->id.context = context;
 778	id_priv->id.event_handler = event_handler;
 779	id_priv->id.ps = ps;
 780	id_priv->id.qp_type = qp_type;
 
 
 
 
 781	spin_lock_init(&id_priv->lock);
 782	mutex_init(&id_priv->qp_mutex);
 783	init_completion(&id_priv->comp);
 784	atomic_set(&id_priv->refcount, 1);
 785	mutex_init(&id_priv->handler_mutex);
 
 
 786	INIT_LIST_HEAD(&id_priv->listen_list);
 787	INIT_LIST_HEAD(&id_priv->mc_list);
 788	get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
 789	id_priv->id.route.addr.dev_addr.net = get_net(net);
 
 790
 791	return &id_priv->id;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 792}
 793EXPORT_SYMBOL(rdma_create_id);
 794
 795static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
 796{
 797	struct ib_qp_attr qp_attr;
 798	int qp_attr_mask, ret;
 799
 800	qp_attr.qp_state = IB_QPS_INIT;
 801	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
 802	if (ret)
 803		return ret;
 804
 805	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
 806	if (ret)
 807		return ret;
 808
 809	qp_attr.qp_state = IB_QPS_RTR;
 810	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
 811	if (ret)
 812		return ret;
 813
 814	qp_attr.qp_state = IB_QPS_RTS;
 815	qp_attr.sq_psn = 0;
 816	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
 817
 818	return ret;
 819}
 820
 821static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
 822{
 823	struct ib_qp_attr qp_attr;
 824	int qp_attr_mask, ret;
 825
 826	qp_attr.qp_state = IB_QPS_INIT;
 827	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
 828	if (ret)
 829		return ret;
 830
 831	return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
 832}
 833
 834int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
 835		   struct ib_qp_init_attr *qp_init_attr)
 836{
 837	struct rdma_id_private *id_priv;
 838	struct ib_qp *qp;
 839	int ret;
 840
 841	id_priv = container_of(id, struct rdma_id_private, id);
 842	if (id->device != pd->device)
 843		return -EINVAL;
 
 
 844
 845	qp_init_attr->port_num = id->port_num;
 846	qp = ib_create_qp(pd, qp_init_attr);
 847	if (IS_ERR(qp))
 848		return PTR_ERR(qp);
 
 
 849
 850	if (id->qp_type == IB_QPT_UD)
 851		ret = cma_init_ud_qp(id_priv, qp);
 852	else
 853		ret = cma_init_conn_qp(id_priv, qp);
 854	if (ret)
 855		goto err;
 856
 857	id->qp = qp;
 858	id_priv->qp_num = qp->qp_num;
 859	id_priv->srq = (qp->srq != NULL);
 
 860	return 0;
 861err:
 862	ib_destroy_qp(qp);
 
 
 863	return ret;
 864}
 865EXPORT_SYMBOL(rdma_create_qp);
 866
 867void rdma_destroy_qp(struct rdma_cm_id *id)
 868{
 869	struct rdma_id_private *id_priv;
 870
 871	id_priv = container_of(id, struct rdma_id_private, id);
 
 872	mutex_lock(&id_priv->qp_mutex);
 873	ib_destroy_qp(id_priv->id.qp);
 874	id_priv->id.qp = NULL;
 875	mutex_unlock(&id_priv->qp_mutex);
 876}
 877EXPORT_SYMBOL(rdma_destroy_qp);
 878
 879static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
 880			     struct rdma_conn_param *conn_param)
 881{
 882	struct ib_qp_attr qp_attr;
 883	int qp_attr_mask, ret;
 884	union ib_gid sgid;
 885
 886	mutex_lock(&id_priv->qp_mutex);
 887	if (!id_priv->id.qp) {
 888		ret = 0;
 889		goto out;
 890	}
 891
 892	/* Need to update QP attributes from default values. */
 893	qp_attr.qp_state = IB_QPS_INIT;
 894	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
 895	if (ret)
 896		goto out;
 897
 898	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
 899	if (ret)
 900		goto out;
 901
 902	qp_attr.qp_state = IB_QPS_RTR;
 903	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
 904	if (ret)
 905		goto out;
 906
 907	ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
 908			   qp_attr.ah_attr.grh.sgid_index, &sgid, NULL);
 909	if (ret)
 910		goto out;
 911
 912	BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
 913
 914	if (conn_param)
 915		qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
 916	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
 917out:
 918	mutex_unlock(&id_priv->qp_mutex);
 919	return ret;
 920}
 921
 922static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
 923			     struct rdma_conn_param *conn_param)
 924{
 925	struct ib_qp_attr qp_attr;
 926	int qp_attr_mask, ret;
 927
 928	mutex_lock(&id_priv->qp_mutex);
 929	if (!id_priv->id.qp) {
 930		ret = 0;
 931		goto out;
 932	}
 933
 934	qp_attr.qp_state = IB_QPS_RTS;
 935	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
 936	if (ret)
 937		goto out;
 938
 939	if (conn_param)
 940		qp_attr.max_rd_atomic = conn_param->initiator_depth;
 941	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
 942out:
 943	mutex_unlock(&id_priv->qp_mutex);
 944	return ret;
 945}
 946
 947static int cma_modify_qp_err(struct rdma_id_private *id_priv)
 948{
 949	struct ib_qp_attr qp_attr;
 950	int ret;
 951
 952	mutex_lock(&id_priv->qp_mutex);
 953	if (!id_priv->id.qp) {
 954		ret = 0;
 955		goto out;
 956	}
 957
 958	qp_attr.qp_state = IB_QPS_ERR;
 959	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
 960out:
 961	mutex_unlock(&id_priv->qp_mutex);
 962	return ret;
 963}
 964
 965static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
 966			       struct ib_qp_attr *qp_attr, int *qp_attr_mask)
 967{
 968	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
 969	int ret;
 970	u16 pkey;
 971
 972	if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
 973		pkey = 0xffff;
 974	else
 975		pkey = ib_addr_get_pkey(dev_addr);
 976
 977	ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
 978				  pkey, &qp_attr->pkey_index);
 979	if (ret)
 980		return ret;
 981
 982	qp_attr->port_num = id_priv->id.port_num;
 983	*qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
 984
 985	if (id_priv->id.qp_type == IB_QPT_UD) {
 986		ret = cma_set_qkey(id_priv, 0);
 987		if (ret)
 988			return ret;
 989
 990		qp_attr->qkey = id_priv->qkey;
 991		*qp_attr_mask |= IB_QP_QKEY;
 992	} else {
 993		qp_attr->qp_access_flags = 0;
 994		*qp_attr_mask |= IB_QP_ACCESS_FLAGS;
 995	}
 996	return 0;
 997}
 998
 999int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
1000		       int *qp_attr_mask)
1001{
1002	struct rdma_id_private *id_priv;
1003	int ret = 0;
1004
1005	id_priv = container_of(id, struct rdma_id_private, id);
1006	if (rdma_cap_ib_cm(id->device, id->port_num)) {
1007		if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
1008			ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
1009		else
1010			ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
1011						 qp_attr_mask);
1012
1013		if (qp_attr->qp_state == IB_QPS_RTR)
1014			qp_attr->rq_psn = id_priv->seq_num;
1015	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
1016		if (!id_priv->cm_id.iw) {
1017			qp_attr->qp_access_flags = 0;
1018			*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1019		} else
1020			ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
1021						 qp_attr_mask);
1022	} else
 
 
1023		ret = -ENOSYS;
 
 
 
 
 
 
 
1024
1025	return ret;
1026}
1027EXPORT_SYMBOL(rdma_init_qp_attr);
1028
1029static inline int cma_zero_addr(struct sockaddr *addr)
1030{
1031	switch (addr->sa_family) {
1032	case AF_INET:
1033		return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
1034	case AF_INET6:
1035		return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
1036	case AF_IB:
1037		return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
1038	default:
1039		return 0;
1040	}
1041}
1042
1043static inline int cma_loopback_addr(struct sockaddr *addr)
1044{
1045	switch (addr->sa_family) {
1046	case AF_INET:
1047		return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
 
1048	case AF_INET6:
1049		return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
 
1050	case AF_IB:
1051		return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
 
1052	default:
1053		return 0;
1054	}
1055}
1056
1057static inline int cma_any_addr(struct sockaddr *addr)
1058{
1059	return cma_zero_addr(addr) || cma_loopback_addr(addr);
1060}
1061
1062static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
1063{
1064	if (src->sa_family != dst->sa_family)
1065		return -1;
1066
1067	switch (src->sa_family) {
1068	case AF_INET:
1069		return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
1070		       ((struct sockaddr_in *) dst)->sin_addr.s_addr;
1071	case AF_INET6:
1072		return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
1073				     &((struct sockaddr_in6 *) dst)->sin6_addr);
 
 
 
 
 
 
 
 
 
 
 
 
 
1074	default:
1075		return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1076				   &((struct sockaddr_ib *) dst)->sib_addr);
1077	}
1078}
1079
1080static __be16 cma_port(struct sockaddr *addr)
1081{
1082	struct sockaddr_ib *sib;
1083
1084	switch (addr->sa_family) {
1085	case AF_INET:
1086		return ((struct sockaddr_in *) addr)->sin_port;
1087	case AF_INET6:
1088		return ((struct sockaddr_in6 *) addr)->sin6_port;
1089	case AF_IB:
1090		sib = (struct sockaddr_ib *) addr;
1091		return htons((u16) (be64_to_cpu(sib->sib_sid) &
1092				    be64_to_cpu(sib->sib_sid_mask)));
1093	default:
1094		return 0;
1095	}
1096}
1097
1098static inline int cma_any_port(struct sockaddr *addr)
1099{
1100	return !cma_port(addr);
1101}
1102
1103static void cma_save_ib_info(struct sockaddr *src_addr,
1104			     struct sockaddr *dst_addr,
1105			     struct rdma_cm_id *listen_id,
1106			     struct ib_sa_path_rec *path)
1107{
1108	struct sockaddr_ib *listen_ib, *ib;
1109
1110	listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
1111	if (src_addr) {
1112		ib = (struct sockaddr_ib *)src_addr;
1113		ib->sib_family = AF_IB;
1114		if (path) {
1115			ib->sib_pkey = path->pkey;
1116			ib->sib_flowinfo = path->flow_label;
1117			memcpy(&ib->sib_addr, &path->sgid, 16);
1118			ib->sib_sid = path->service_id;
1119			ib->sib_scope_id = 0;
1120		} else {
1121			ib->sib_pkey = listen_ib->sib_pkey;
1122			ib->sib_flowinfo = listen_ib->sib_flowinfo;
1123			ib->sib_addr = listen_ib->sib_addr;
1124			ib->sib_sid = listen_ib->sib_sid;
1125			ib->sib_scope_id = listen_ib->sib_scope_id;
1126		}
1127		ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
1128	}
1129	if (dst_addr) {
1130		ib = (struct sockaddr_ib *)dst_addr;
1131		ib->sib_family = AF_IB;
1132		if (path) {
1133			ib->sib_pkey = path->pkey;
1134			ib->sib_flowinfo = path->flow_label;
1135			memcpy(&ib->sib_addr, &path->dgid, 16);
1136		}
1137	}
1138}
1139
1140static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1141			      struct sockaddr_in *dst_addr,
1142			      struct cma_hdr *hdr,
1143			      __be16 local_port)
1144{
1145	if (src_addr) {
1146		*src_addr = (struct sockaddr_in) {
1147			.sin_family = AF_INET,
1148			.sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1149			.sin_port = local_port,
1150		};
1151	}
1152
1153	if (dst_addr) {
1154		*dst_addr = (struct sockaddr_in) {
1155			.sin_family = AF_INET,
1156			.sin_addr.s_addr = hdr->src_addr.ip4.addr,
1157			.sin_port = hdr->port,
1158		};
1159	}
1160}
1161
1162static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1163			      struct sockaddr_in6 *dst_addr,
1164			      struct cma_hdr *hdr,
1165			      __be16 local_port)
1166{
1167	if (src_addr) {
1168		*src_addr = (struct sockaddr_in6) {
1169			.sin6_family = AF_INET6,
1170			.sin6_addr = hdr->dst_addr.ip6,
1171			.sin6_port = local_port,
1172		};
1173	}
1174
1175	if (dst_addr) {
1176		*dst_addr = (struct sockaddr_in6) {
1177			.sin6_family = AF_INET6,
1178			.sin6_addr = hdr->src_addr.ip6,
1179			.sin6_port = hdr->port,
1180		};
1181	}
1182}
1183
1184static u16 cma_port_from_service_id(__be64 service_id)
1185{
1186	return (u16)be64_to_cpu(service_id);
1187}
1188
1189static int cma_save_ip_info(struct sockaddr *src_addr,
1190			    struct sockaddr *dst_addr,
1191			    struct ib_cm_event *ib_event,
1192			    __be64 service_id)
1193{
1194	struct cma_hdr *hdr;
1195	__be16 port;
1196
1197	hdr = ib_event->private_data;
1198	if (hdr->cma_version != CMA_VERSION)
1199		return -EINVAL;
1200
1201	port = htons(cma_port_from_service_id(service_id));
1202
1203	switch (cma_get_ip_ver(hdr)) {
1204	case 4:
1205		cma_save_ip4_info((struct sockaddr_in *)src_addr,
1206				  (struct sockaddr_in *)dst_addr, hdr, port);
1207		break;
1208	case 6:
1209		cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1210				  (struct sockaddr_in6 *)dst_addr, hdr, port);
1211		break;
1212	default:
1213		return -EAFNOSUPPORT;
1214	}
1215
1216	return 0;
1217}
1218
1219static int cma_save_net_info(struct sockaddr *src_addr,
1220			     struct sockaddr *dst_addr,
1221			     struct rdma_cm_id *listen_id,
1222			     struct ib_cm_event *ib_event,
1223			     sa_family_t sa_family, __be64 service_id)
1224{
1225	if (sa_family == AF_IB) {
1226		if (ib_event->event == IB_CM_REQ_RECEIVED)
1227			cma_save_ib_info(src_addr, dst_addr, listen_id,
1228					 ib_event->param.req_rcvd.primary_path);
1229		else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1230			cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1231		return 0;
1232	}
1233
1234	return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1235}
1236
1237static int cma_save_req_info(const struct ib_cm_event *ib_event,
1238			     struct cma_req_info *req)
1239{
1240	const struct ib_cm_req_event_param *req_param =
1241		&ib_event->param.req_rcvd;
1242	const struct ib_cm_sidr_req_event_param *sidr_param =
1243		&ib_event->param.sidr_req_rcvd;
1244
1245	switch (ib_event->event) {
1246	case IB_CM_REQ_RECEIVED:
1247		req->device	= req_param->listen_id->device;
1248		req->port	= req_param->port;
1249		memcpy(&req->local_gid, &req_param->primary_path->sgid,
1250		       sizeof(req->local_gid));
1251		req->has_gid	= true;
1252		req->service_id	= req_param->primary_path->service_id;
1253		req->pkey	= be16_to_cpu(req_param->primary_path->pkey);
1254		if (req->pkey != req_param->bth_pkey)
1255			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1256					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1257					    req_param->bth_pkey, req->pkey);
1258		break;
1259	case IB_CM_SIDR_REQ_RECEIVED:
1260		req->device	= sidr_param->listen_id->device;
1261		req->port	= sidr_param->port;
1262		req->has_gid	= false;
1263		req->service_id	= sidr_param->service_id;
1264		req->pkey	= sidr_param->pkey;
1265		if (req->pkey != sidr_param->bth_pkey)
1266			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1267					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1268					    sidr_param->bth_pkey, req->pkey);
1269		break;
1270	default:
1271		return -EINVAL;
1272	}
1273
1274	return 0;
1275}
1276
1277static bool validate_ipv4_net_dev(struct net_device *net_dev,
1278				  const struct sockaddr_in *dst_addr,
1279				  const struct sockaddr_in *src_addr)
1280{
1281	__be32 daddr = dst_addr->sin_addr.s_addr,
1282	       saddr = src_addr->sin_addr.s_addr;
1283	struct fib_result res;
1284	struct flowi4 fl4;
1285	int err;
1286	bool ret;
1287
1288	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1289	    ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1290	    ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1291	    ipv4_is_loopback(saddr))
1292		return false;
1293
1294	memset(&fl4, 0, sizeof(fl4));
1295	fl4.flowi4_iif = net_dev->ifindex;
1296	fl4.daddr = daddr;
1297	fl4.saddr = saddr;
1298
1299	rcu_read_lock();
1300	err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1301	ret = err == 0 && FIB_RES_DEV(res) == net_dev;
1302	rcu_read_unlock();
1303
1304	return ret;
1305}
1306
1307static bool validate_ipv6_net_dev(struct net_device *net_dev,
1308				  const struct sockaddr_in6 *dst_addr,
1309				  const struct sockaddr_in6 *src_addr)
1310{
1311#if IS_ENABLED(CONFIG_IPV6)
1312	const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1313			   IPV6_ADDR_LINKLOCAL;
1314	struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1315					 &src_addr->sin6_addr, net_dev->ifindex,
1316					 strict);
1317	bool ret;
1318
1319	if (!rt)
1320		return false;
1321
1322	ret = rt->rt6i_idev->dev == net_dev;
1323	ip6_rt_put(rt);
1324
1325	return ret;
1326#else
1327	return false;
1328#endif
1329}
1330
1331static bool validate_net_dev(struct net_device *net_dev,
1332			     const struct sockaddr *daddr,
1333			     const struct sockaddr *saddr)
1334{
1335	const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1336	const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1337	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1338	const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1339
1340	switch (daddr->sa_family) {
1341	case AF_INET:
1342		return saddr->sa_family == AF_INET &&
1343		       validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1344
1345	case AF_INET6:
1346		return saddr->sa_family == AF_INET6 &&
1347		       validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1348
1349	default:
1350		return false;
1351	}
1352}
1353
1354static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1355					  const struct cma_req_info *req)
1356{
1357	struct sockaddr_storage listen_addr_storage, src_addr_storage;
1358	struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1359			*src_addr = (struct sockaddr *)&src_addr_storage;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1360	struct net_device *net_dev;
1361	const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1362	int err;
1363
1364	err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1365			       req->service_id);
1366	if (err)
1367		return ERR_PTR(err);
1368
1369	net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
1370					   gid, listen_addr);
 
 
 
 
1371	if (!net_dev)
1372		return ERR_PTR(-ENODEV);
1373
1374	if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1375		dev_put(net_dev);
1376		return ERR_PTR(-EHOSTUNREACH);
1377	}
1378
1379	return net_dev;
1380}
1381
1382static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1383{
1384	return (be64_to_cpu(service_id) >> 16) & 0xffff;
1385}
1386
1387static bool cma_match_private_data(struct rdma_id_private *id_priv,
1388				   const struct cma_hdr *hdr)
1389{
1390	struct sockaddr *addr = cma_src_addr(id_priv);
1391	__be32 ip4_addr;
1392	struct in6_addr ip6_addr;
1393
1394	if (cma_any_addr(addr) && !id_priv->afonly)
1395		return true;
1396
1397	switch (addr->sa_family) {
1398	case AF_INET:
1399		ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1400		if (cma_get_ip_ver(hdr) != 4)
1401			return false;
1402		if (!cma_any_addr(addr) &&
1403		    hdr->dst_addr.ip4.addr != ip4_addr)
1404			return false;
1405		break;
1406	case AF_INET6:
1407		ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1408		if (cma_get_ip_ver(hdr) != 6)
1409			return false;
1410		if (!cma_any_addr(addr) &&
1411		    memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1412			return false;
1413		break;
1414	case AF_IB:
1415		return true;
1416	default:
1417		return false;
1418	}
1419
1420	return true;
1421}
1422
1423static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1424{
1425	enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1426	enum rdma_transport_type transport =
1427		rdma_node_get_transport(device->node_type);
1428
1429	return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1430}
1431
1432static bool cma_protocol_roce(const struct rdma_cm_id *id)
1433{
1434	struct ib_device *device = id->device;
1435	const int port_num = id->port_num ?: rdma_start_port(device);
 
1436
1437	return cma_protocol_roce_dev_port(device, port_num);
 
 
1438}
1439
1440static bool cma_match_net_dev(const struct rdma_cm_id *id,
1441			      const struct net_device *net_dev,
1442			      u8 port_num)
1443{
1444	const struct rdma_addr *addr = &id->route.addr;
1445
1446	if (!net_dev)
1447		/* This request is an AF_IB request or a RoCE request */
1448		return (!id->port_num || id->port_num == port_num) &&
1449		       (addr->src_addr.ss_family == AF_IB ||
1450			cma_protocol_roce_dev_port(id->device, port_num));
1451
1452	return !addr->dev_addr.bound_dev_if ||
1453	       (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1454		addr->dev_addr.bound_dev_if == net_dev->ifindex);
 
 
 
 
 
 
 
 
 
 
 
 
1455}
1456
1457static struct rdma_id_private *cma_find_listener(
1458		const struct rdma_bind_list *bind_list,
1459		const struct ib_cm_id *cm_id,
1460		const struct ib_cm_event *ib_event,
1461		const struct cma_req_info *req,
1462		const struct net_device *net_dev)
1463{
1464	struct rdma_id_private *id_priv, *id_priv_dev;
1465
 
 
1466	if (!bind_list)
1467		return ERR_PTR(-EINVAL);
1468
1469	hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1470		if (cma_match_private_data(id_priv, ib_event->private_data)) {
1471			if (id_priv->id.device == cm_id->device &&
1472			    cma_match_net_dev(&id_priv->id, net_dev, req->port))
1473				return id_priv;
1474			list_for_each_entry(id_priv_dev,
1475					    &id_priv->listen_list,
1476					    listen_list) {
1477				if (id_priv_dev->id.device == cm_id->device &&
1478				    cma_match_net_dev(&id_priv_dev->id, net_dev, req->port))
 
1479					return id_priv_dev;
1480			}
1481		}
1482	}
1483
1484	return ERR_PTR(-EINVAL);
1485}
1486
1487static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
1488						 struct ib_cm_event *ib_event,
1489						 struct net_device **net_dev)
 
 
1490{
1491	struct cma_req_info req;
1492	struct rdma_bind_list *bind_list;
1493	struct rdma_id_private *id_priv;
1494	int err;
1495
1496	err = cma_save_req_info(ib_event, &req);
1497	if (err)
1498		return ERR_PTR(err);
1499
1500	*net_dev = cma_get_net_dev(ib_event, &req);
1501	if (IS_ERR(*net_dev)) {
1502		if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1503			/* Assuming the protocol is AF_IB */
1504			*net_dev = NULL;
1505		} else if (cma_protocol_roce_dev_port(req.device, req.port)) {
1506			/* TODO find the net dev matching the request parameters
1507			 * through the RoCE GID table */
1508			*net_dev = NULL;
1509		} else {
1510			return ERR_CAST(*net_dev);
1511		}
1512	}
1513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1514	bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1515				rdma_ps_from_service_id(req.service_id),
1516				cma_port_from_service_id(req.service_id));
1517	id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
 
 
 
1518	if (IS_ERR(id_priv) && *net_dev) {
1519		dev_put(*net_dev);
1520		*net_dev = NULL;
1521	}
1522
1523	return id_priv;
1524}
1525
1526static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
1527{
1528	return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1529}
1530
1531static void cma_cancel_route(struct rdma_id_private *id_priv)
1532{
1533	if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1534		if (id_priv->query)
1535			ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1536	}
1537}
1538
1539static void cma_cancel_listens(struct rdma_id_private *id_priv)
1540{
1541	struct rdma_id_private *dev_id_priv;
1542
 
 
1543	/*
1544	 * Remove from listen_any_list to prevent added devices from spawning
1545	 * additional listen requests.
1546	 */
1547	mutex_lock(&lock);
1548	list_del(&id_priv->list);
1549
1550	while (!list_empty(&id_priv->listen_list)) {
1551		dev_id_priv = list_entry(id_priv->listen_list.next,
1552					 struct rdma_id_private, listen_list);
 
1553		/* sync with device removal to avoid duplicate destruction */
1554		list_del_init(&dev_id_priv->list);
1555		list_del(&dev_id_priv->listen_list);
1556		mutex_unlock(&lock);
1557
1558		rdma_destroy_id(&dev_id_priv->id);
1559		mutex_lock(&lock);
1560	}
 
 
 
 
 
 
1561	mutex_unlock(&lock);
1562}
1563
1564static void cma_cancel_operation(struct rdma_id_private *id_priv,
1565				 enum rdma_cm_state state)
1566{
1567	switch (state) {
1568	case RDMA_CM_ADDR_QUERY:
 
 
 
 
 
 
 
 
1569		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1570		break;
1571	case RDMA_CM_ROUTE_QUERY:
1572		cma_cancel_route(id_priv);
1573		break;
1574	case RDMA_CM_LISTEN:
1575		if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1576			cma_cancel_listens(id_priv);
1577		break;
1578	default:
1579		break;
1580	}
1581}
1582
1583static void cma_release_port(struct rdma_id_private *id_priv)
1584{
1585	struct rdma_bind_list *bind_list = id_priv->bind_list;
1586	struct net *net = id_priv->id.route.addr.dev_addr.net;
1587
1588	if (!bind_list)
1589		return;
1590
1591	mutex_lock(&lock);
1592	hlist_del(&id_priv->node);
1593	if (hlist_empty(&bind_list->owners)) {
1594		cma_ps_remove(net, bind_list->ps, bind_list->port);
1595		kfree(bind_list);
1596	}
1597	mutex_unlock(&lock);
1598}
1599
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1600static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1601{
1602	struct cma_multicast *mc;
1603
1604	while (!list_empty(&id_priv->mc_list)) {
1605		mc = container_of(id_priv->mc_list.next,
1606				  struct cma_multicast, list);
1607		list_del(&mc->list);
1608		if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
1609				      id_priv->id.port_num)) {
1610			ib_sa_free_multicast(mc->multicast.ib);
1611			kfree(mc);
1612		} else {
1613			if (mc->igmp_joined) {
1614				struct rdma_dev_addr *dev_addr =
1615					&id_priv->id.route.addr.dev_addr;
1616				struct net_device *ndev = NULL;
1617
1618				if (dev_addr->bound_dev_if)
1619					ndev = dev_get_by_index(&init_net,
1620								dev_addr->bound_dev_if);
1621				if (ndev) {
1622					cma_igmp_send(ndev,
1623						      &mc->multicast.ib->rec.mgid,
1624						      false);
1625					dev_put(ndev);
1626				}
1627			}
1628			kref_put(&mc->mcref, release_mc);
1629		}
1630	}
1631}
1632
1633void rdma_destroy_id(struct rdma_cm_id *id)
 
1634{
1635	struct rdma_id_private *id_priv;
1636	enum rdma_cm_state state;
1637
1638	id_priv = container_of(id, struct rdma_id_private, id);
1639	state = cma_exch(id_priv, RDMA_CM_DESTROYING);
1640	cma_cancel_operation(id_priv, state);
1641
1642	/*
1643	 * Wait for any active callback to finish.  New callbacks will find
1644	 * the id_priv state set to destroying and abort.
1645	 */
1646	mutex_lock(&id_priv->handler_mutex);
1647	mutex_unlock(&id_priv->handler_mutex);
1648
1649	if (id_priv->cma_dev) {
1650		if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1651			if (id_priv->cm_id.ib)
1652				ib_destroy_cm_id(id_priv->cm_id.ib);
1653		} else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1654			if (id_priv->cm_id.iw)
1655				iw_destroy_cm_id(id_priv->cm_id.iw);
1656		}
1657		cma_leave_mc_groups(id_priv);
1658		cma_release_dev(id_priv);
1659	}
1660
1661	cma_release_port(id_priv);
1662	cma_deref_id(id_priv);
1663	wait_for_completion(&id_priv->comp);
1664
1665	if (id_priv->internal_id)
1666		cma_deref_id(id_priv->id.context);
1667
1668	kfree(id_priv->id.route.path_rec);
 
 
 
1669	put_net(id_priv->id.route.addr.dev_addr.net);
1670	kfree(id_priv);
1671}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1672EXPORT_SYMBOL(rdma_destroy_id);
1673
1674static int cma_rep_recv(struct rdma_id_private *id_priv)
1675{
1676	int ret;
1677
1678	ret = cma_modify_qp_rtr(id_priv, NULL);
1679	if (ret)
1680		goto reject;
1681
1682	ret = cma_modify_qp_rts(id_priv, NULL);
1683	if (ret)
1684		goto reject;
1685
 
1686	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1687	if (ret)
1688		goto reject;
1689
1690	return 0;
1691reject:
 
1692	cma_modify_qp_err(id_priv);
 
1693	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1694		       NULL, 0, NULL, 0);
1695	return ret;
1696}
1697
1698static void cma_set_rep_event_data(struct rdma_cm_event *event,
1699				   struct ib_cm_rep_event_param *rep_data,
1700				   void *private_data)
1701{
1702	event->param.conn.private_data = private_data;
1703	event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1704	event->param.conn.responder_resources = rep_data->responder_resources;
1705	event->param.conn.initiator_depth = rep_data->initiator_depth;
1706	event->param.conn.flow_control = rep_data->flow_control;
1707	event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1708	event->param.conn.srq = rep_data->srq;
1709	event->param.conn.qp_num = rep_data->remote_qpn;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1710}
1711
1712static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
 
1713{
1714	struct rdma_id_private *id_priv = cm_id->context;
1715	struct rdma_cm_event event;
1716	int ret = 0;
 
1717
1718	mutex_lock(&id_priv->handler_mutex);
 
1719	if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1720	     id_priv->state != RDMA_CM_CONNECT) ||
1721	    (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1722	     id_priv->state != RDMA_CM_DISCONNECT))
1723		goto out;
1724
1725	memset(&event, 0, sizeof event);
1726	switch (ib_event->event) {
1727	case IB_CM_REQ_ERROR:
1728	case IB_CM_REP_ERROR:
1729		event.event = RDMA_CM_EVENT_UNREACHABLE;
1730		event.status = -ETIMEDOUT;
1731		break;
1732	case IB_CM_REP_RECEIVED:
 
 
 
 
 
1733		if (id_priv->id.qp) {
1734			event.status = cma_rep_recv(id_priv);
1735			event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1736						     RDMA_CM_EVENT_ESTABLISHED;
1737		} else {
1738			event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1739		}
1740		cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1741				       ib_event->private_data);
1742		break;
1743	case IB_CM_RTU_RECEIVED:
1744	case IB_CM_USER_ESTABLISHED:
1745		event.event = RDMA_CM_EVENT_ESTABLISHED;
1746		break;
1747	case IB_CM_DREQ_ERROR:
1748		event.status = -ETIMEDOUT; /* fall through */
 
1749	case IB_CM_DREQ_RECEIVED:
1750	case IB_CM_DREP_RECEIVED:
1751		if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1752				   RDMA_CM_DISCONNECT))
1753			goto out;
1754		event.event = RDMA_CM_EVENT_DISCONNECTED;
1755		break;
1756	case IB_CM_TIMEWAIT_EXIT:
1757		event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1758		break;
1759	case IB_CM_MRA_RECEIVED:
1760		/* ignore event */
1761		goto out;
1762	case IB_CM_REJ_RECEIVED:
 
 
1763		cma_modify_qp_err(id_priv);
1764		event.status = ib_event->param.rej_rcvd.reason;
1765		event.event = RDMA_CM_EVENT_REJECTED;
1766		event.param.conn.private_data = ib_event->private_data;
1767		event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1768		break;
1769	default:
1770		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1771		       ib_event->event);
1772		goto out;
1773	}
1774
1775	ret = id_priv->id.event_handler(&id_priv->id, &event);
1776	if (ret) {
1777		/* Destroy the CM ID by returning a non-zero value. */
1778		id_priv->cm_id.ib = NULL;
1779		cma_exch(id_priv, RDMA_CM_DESTROYING);
1780		mutex_unlock(&id_priv->handler_mutex);
1781		rdma_destroy_id(&id_priv->id);
1782		return ret;
1783	}
1784out:
1785	mutex_unlock(&id_priv->handler_mutex);
1786	return ret;
1787}
1788
1789static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1790					       struct ib_cm_event *ib_event,
1791					       struct net_device *net_dev)
 
1792{
 
1793	struct rdma_id_private *id_priv;
1794	struct rdma_cm_id *id;
1795	struct rdma_route *rt;
1796	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
 
1797	const __be64 service_id =
1798		      ib_event->param.req_rcvd.primary_path->service_id;
1799	int ret;
1800
1801	id = rdma_create_id(listen_id->route.addr.dev_addr.net,
1802			    listen_id->event_handler, listen_id->context,
1803			    listen_id->ps, ib_event->param.req_rcvd.qp_type);
1804	if (IS_ERR(id))
 
 
 
1805		return NULL;
1806
1807	id_priv = container_of(id, struct rdma_id_private, id);
1808	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1809			      (struct sockaddr *)&id->route.addr.dst_addr,
1810			      listen_id, ib_event, ss_family, service_id))
1811		goto err;
1812
1813	rt = &id->route;
1814	rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1815	rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1816			       GFP_KERNEL);
1817	if (!rt->path_rec)
1818		goto err;
1819
1820	rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1821	if (rt->num_paths == 2)
1822		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1823
1824	if (net_dev) {
1825		ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
1826		if (ret)
1827			goto err;
1828	} else {
1829		if (!cma_protocol_roce(listen_id) &&
1830		    cma_any_addr(cma_src_addr(id_priv))) {
1831			rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1832			rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1833			ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1834		} else if (!cma_any_addr(cma_src_addr(id_priv))) {
1835			ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1836			if (ret)
1837				goto err;
1838		}
1839	}
1840	rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1841
1842	id_priv->state = RDMA_CM_CONNECT;
1843	return id_priv;
1844
1845err:
1846	rdma_destroy_id(id);
1847	return NULL;
1848}
1849
1850static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1851					      struct ib_cm_event *ib_event,
1852					      struct net_device *net_dev)
 
1853{
 
1854	struct rdma_id_private *id_priv;
1855	struct rdma_cm_id *id;
1856	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1857	struct net *net = listen_id->route.addr.dev_addr.net;
1858	int ret;
1859
1860	id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
1861			    listen_id->ps, IB_QPT_UD);
1862	if (IS_ERR(id))
 
 
1863		return NULL;
1864
1865	id_priv = container_of(id, struct rdma_id_private, id);
1866	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1867			      (struct sockaddr *)&id->route.addr.dst_addr,
1868			      listen_id, ib_event, ss_family,
1869			      ib_event->param.sidr_req_rcvd.service_id))
1870		goto err;
1871
1872	if (net_dev) {
1873		ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
1874		if (ret)
1875			goto err;
1876	} else {
1877		if (!cma_any_addr(cma_src_addr(id_priv))) {
1878			ret = cma_translate_addr(cma_src_addr(id_priv),
1879						 &id->route.addr.dev_addr);
1880			if (ret)
1881				goto err;
1882		}
1883	}
1884
1885	id_priv->state = RDMA_CM_CONNECT;
1886	return id_priv;
1887err:
1888	rdma_destroy_id(id);
1889	return NULL;
1890}
1891
1892static void cma_set_req_event_data(struct rdma_cm_event *event,
1893				   struct ib_cm_req_event_param *req_data,
1894				   void *private_data, int offset)
1895{
1896	event->param.conn.private_data = private_data + offset;
1897	event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1898	event->param.conn.responder_resources = req_data->responder_resources;
1899	event->param.conn.initiator_depth = req_data->initiator_depth;
1900	event->param.conn.flow_control = req_data->flow_control;
1901	event->param.conn.retry_count = req_data->retry_count;
1902	event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1903	event->param.conn.srq = req_data->srq;
1904	event->param.conn.qp_num = req_data->remote_qpn;
 
 
 
1905}
1906
1907static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
 
1908{
1909	return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
1910		 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1911		((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1912		 (id->qp_type == IB_QPT_UD)) ||
1913		(!id->qp_type));
1914}
1915
1916static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
 
1917{
1918	struct rdma_id_private *listen_id, *conn_id = NULL;
1919	struct rdma_cm_event event;
 
1920	struct net_device *net_dev;
1921	int offset, ret;
 
1922
1923	listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
1924	if (IS_ERR(listen_id))
1925		return PTR_ERR(listen_id);
1926
1927	if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
 
1928		ret = -EINVAL;
1929		goto net_dev_put;
1930	}
1931
1932	mutex_lock(&listen_id->handler_mutex);
1933	if (listen_id->state != RDMA_CM_LISTEN) {
1934		ret = -ECONNABORTED;
1935		goto err1;
1936	}
1937
1938	memset(&event, 0, sizeof event);
1939	offset = cma_user_data_offset(listen_id);
1940	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1941	if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
1942		conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
1943		event.param.ud.private_data = ib_event->private_data + offset;
1944		event.param.ud.private_data_len =
1945				IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1946	} else {
1947		conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
1948		cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1949				       ib_event->private_data, offset);
1950	}
1951	if (!conn_id) {
1952		ret = -ENOMEM;
1953		goto err1;
1954	}
1955
1956	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1957	ret = cma_acquire_dev(conn_id, listen_id);
1958	if (ret)
1959		goto err2;
 
 
1960
1961	conn_id->cm_id.ib = cm_id;
1962	cm_id->context = conn_id;
1963	cm_id->cm_handler = cma_ib_handler;
1964
1965	/*
1966	 * Protect against the user destroying conn_id from another thread
1967	 * until we're done accessing it.
1968	 */
1969	atomic_inc(&conn_id->refcount);
1970	ret = conn_id->id.event_handler(&conn_id->id, &event);
1971	if (ret)
1972		goto err3;
1973	/*
1974	 * Acquire mutex to prevent user executing rdma_destroy_id()
1975	 * while we're accessing the cm_id.
1976	 */
1977	mutex_lock(&lock);
1978	if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1979	    (conn_id->id.qp_type != IB_QPT_UD))
1980		ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1981	mutex_unlock(&lock);
1982	mutex_unlock(&conn_id->handler_mutex);
1983	mutex_unlock(&listen_id->handler_mutex);
1984	cma_deref_id(conn_id);
1985	if (net_dev)
1986		dev_put(net_dev);
1987	return 0;
1988
1989err3:
1990	cma_deref_id(conn_id);
1991	/* Destroy the CM ID by returning a non-zero value. */
1992	conn_id->cm_id.ib = NULL;
1993err2:
1994	cma_exch(conn_id, RDMA_CM_DESTROYING);
1995	mutex_unlock(&conn_id->handler_mutex);
1996err1:
1997	mutex_unlock(&listen_id->handler_mutex);
1998	if (conn_id)
1999		rdma_destroy_id(&conn_id->id);
2000
2001net_dev_put:
2002	if (net_dev)
2003		dev_put(net_dev);
2004
2005	return ret;
2006}
2007
2008__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2009{
2010	if (addr->sa_family == AF_IB)
2011		return ((struct sockaddr_ib *) addr)->sib_sid;
2012
2013	return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2014}
2015EXPORT_SYMBOL(rdma_get_service_id);
2016
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2017static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2018{
2019	struct rdma_id_private *id_priv = iw_id->context;
2020	struct rdma_cm_event event;
2021	int ret = 0;
2022	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2023	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2024
2025	mutex_lock(&id_priv->handler_mutex);
2026	if (id_priv->state != RDMA_CM_CONNECT)
2027		goto out;
2028
2029	memset(&event, 0, sizeof event);
2030	switch (iw_event->event) {
2031	case IW_CM_EVENT_CLOSE:
2032		event.event = RDMA_CM_EVENT_DISCONNECTED;
2033		break;
2034	case IW_CM_EVENT_CONNECT_REPLY:
2035		memcpy(cma_src_addr(id_priv), laddr,
2036		       rdma_addr_size(laddr));
2037		memcpy(cma_dst_addr(id_priv), raddr,
2038		       rdma_addr_size(raddr));
2039		switch (iw_event->status) {
2040		case 0:
2041			event.event = RDMA_CM_EVENT_ESTABLISHED;
2042			event.param.conn.initiator_depth = iw_event->ird;
2043			event.param.conn.responder_resources = iw_event->ord;
2044			break;
2045		case -ECONNRESET:
2046		case -ECONNREFUSED:
2047			event.event = RDMA_CM_EVENT_REJECTED;
2048			break;
2049		case -ETIMEDOUT:
2050			event.event = RDMA_CM_EVENT_UNREACHABLE;
2051			break;
2052		default:
2053			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2054			break;
2055		}
2056		break;
2057	case IW_CM_EVENT_ESTABLISHED:
2058		event.event = RDMA_CM_EVENT_ESTABLISHED;
2059		event.param.conn.initiator_depth = iw_event->ird;
2060		event.param.conn.responder_resources = iw_event->ord;
2061		break;
2062	default:
2063		BUG_ON(1);
2064	}
2065
2066	event.status = iw_event->status;
2067	event.param.conn.private_data = iw_event->private_data;
2068	event.param.conn.private_data_len = iw_event->private_data_len;
2069	ret = id_priv->id.event_handler(&id_priv->id, &event);
2070	if (ret) {
2071		/* Destroy the CM ID by returning a non-zero value. */
2072		id_priv->cm_id.iw = NULL;
2073		cma_exch(id_priv, RDMA_CM_DESTROYING);
2074		mutex_unlock(&id_priv->handler_mutex);
2075		rdma_destroy_id(&id_priv->id);
2076		return ret;
2077	}
2078
2079out:
2080	mutex_unlock(&id_priv->handler_mutex);
2081	return ret;
2082}
2083
2084static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2085			       struct iw_cm_event *iw_event)
2086{
2087	struct rdma_cm_id *new_cm_id;
2088	struct rdma_id_private *listen_id, *conn_id;
2089	struct rdma_cm_event event;
2090	int ret = -ECONNABORTED;
2091	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2092	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2093
 
 
 
 
 
 
2094	listen_id = cm_id->context;
2095
2096	mutex_lock(&listen_id->handler_mutex);
2097	if (listen_id->state != RDMA_CM_LISTEN)
2098		goto out;
2099
2100	/* Create a new RDMA id for the new IW CM ID */
2101	new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2102				   listen_id->id.event_handler,
2103				   listen_id->id.context,
2104				   RDMA_PS_TCP, IB_QPT_RC);
2105	if (IS_ERR(new_cm_id)) {
2106		ret = -ENOMEM;
2107		goto out;
2108	}
2109	conn_id = container_of(new_cm_id, struct rdma_id_private, id);
2110	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2111	conn_id->state = RDMA_CM_CONNECT;
2112
2113	ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
2114	if (ret) {
2115		mutex_unlock(&conn_id->handler_mutex);
2116		rdma_destroy_id(new_cm_id);
2117		goto out;
2118	}
2119
2120	ret = cma_acquire_dev(conn_id, listen_id);
2121	if (ret) {
2122		mutex_unlock(&conn_id->handler_mutex);
2123		rdma_destroy_id(new_cm_id);
2124		goto out;
2125	}
2126
2127	conn_id->cm_id.iw = cm_id;
2128	cm_id->context = conn_id;
2129	cm_id->cm_handler = cma_iw_handler;
2130
2131	memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2132	memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2133
2134	memset(&event, 0, sizeof event);
2135	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2136	event.param.conn.private_data = iw_event->private_data;
2137	event.param.conn.private_data_len = iw_event->private_data_len;
2138	event.param.conn.initiator_depth = iw_event->ird;
2139	event.param.conn.responder_resources = iw_event->ord;
2140
2141	/*
2142	 * Protect against the user destroying conn_id from another thread
2143	 * until we're done accessing it.
2144	 */
2145	atomic_inc(&conn_id->refcount);
2146	ret = conn_id->id.event_handler(&conn_id->id, &event);
2147	if (ret) {
2148		/* User wants to destroy the CM ID */
2149		conn_id->cm_id.iw = NULL;
2150		cma_exch(conn_id, RDMA_CM_DESTROYING);
2151		mutex_unlock(&conn_id->handler_mutex);
2152		cma_deref_id(conn_id);
2153		rdma_destroy_id(&conn_id->id);
2154		goto out;
2155	}
2156
2157	mutex_unlock(&conn_id->handler_mutex);
2158	cma_deref_id(conn_id);
2159
2160out:
2161	mutex_unlock(&listen_id->handler_mutex);
2162	return ret;
2163}
2164
2165static int cma_ib_listen(struct rdma_id_private *id_priv)
2166{
2167	struct sockaddr *addr;
2168	struct ib_cm_id	*id;
2169	__be64 svc_id;
2170
2171	addr = cma_src_addr(id_priv);
2172	svc_id = rdma_get_service_id(&id_priv->id, addr);
2173	id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
 
2174	if (IS_ERR(id))
2175		return PTR_ERR(id);
2176	id_priv->cm_id.ib = id;
2177
2178	return 0;
2179}
2180
2181static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2182{
2183	int ret;
2184	struct iw_cm_id	*id;
2185
2186	id = iw_create_cm_id(id_priv->id.device,
2187			     iw_conn_req_handler,
2188			     id_priv);
2189	if (IS_ERR(id))
2190		return PTR_ERR(id);
2191
 
2192	id->tos = id_priv->tos;
 
 
 
2193	id_priv->cm_id.iw = id;
2194
2195	memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2196	       rdma_addr_size(cma_src_addr(id_priv)));
2197
2198	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2199
2200	if (ret) {
2201		iw_destroy_cm_id(id_priv->cm_id.iw);
2202		id_priv->cm_id.iw = NULL;
2203	}
2204
2205	return ret;
2206}
2207
2208static int cma_listen_handler(struct rdma_cm_id *id,
2209			      struct rdma_cm_event *event)
2210{
2211	struct rdma_id_private *id_priv = id->context;
2212
 
 
 
 
2213	id->context = id_priv->id.context;
2214	id->event_handler = id_priv->id.event_handler;
 
2215	return id_priv->id.event_handler(id, event);
2216}
2217
2218static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2219			      struct cma_device *cma_dev)
 
2220{
2221	struct rdma_id_private *dev_id_priv;
2222	struct rdma_cm_id *id;
2223	struct net *net = id_priv->id.route.addr.dev_addr.net;
2224	int ret;
2225
 
 
 
2226	if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2227		return;
2228
2229	id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
2230			    id_priv->id.qp_type);
2231	if (IS_ERR(id))
2232		return;
2233
2234	dev_id_priv = container_of(id, struct rdma_id_private, id);
 
 
 
 
2235
2236	dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2237	memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2238	       rdma_addr_size(cma_src_addr(id_priv)));
2239
2240	_cma_attach_to_dev(dev_id_priv, cma_dev);
2241	list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2242	atomic_inc(&id_priv->refcount);
2243	dev_id_priv->internal_id = 1;
2244	dev_id_priv->afonly = id_priv->afonly;
 
 
 
 
2245
2246	ret = rdma_listen(id, id_priv->backlog);
2247	if (ret)
2248		pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2249			ret, cma_dev->device->name);
 
 
 
 
 
 
2250}
2251
2252static void cma_listen_on_all(struct rdma_id_private *id_priv)
2253{
 
2254	struct cma_device *cma_dev;
 
2255
2256	mutex_lock(&lock);
2257	list_add_tail(&id_priv->list, &listen_any_list);
2258	list_for_each_entry(cma_dev, &dev_list, list)
2259		cma_listen_on_dev(id_priv, cma_dev);
 
 
 
 
 
 
 
 
 
 
 
 
2260	mutex_unlock(&lock);
 
 
 
2261}
2262
2263void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2264{
2265	struct rdma_id_private *id_priv;
2266
2267	id_priv = container_of(id, struct rdma_id_private, id);
 
2268	id_priv->tos = (u8) tos;
 
 
2269}
2270EXPORT_SYMBOL(rdma_set_service_type);
2271
2272static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
2273			      void *context)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2274{
2275	struct cma_work *work = context;
2276	struct rdma_route *route;
 
2277
2278	route = &work->id->id.route;
2279
2280	if (!status) {
2281		route->num_paths = 1;
2282		*route->path_rec = *path_rec;
2283	} else {
2284		work->old_state = RDMA_CM_ROUTE_QUERY;
2285		work->new_state = RDMA_CM_ADDR_RESOLVED;
2286		work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2287		work->event.status = status;
 
 
 
 
 
 
 
 
2288	}
2289
 
 
 
 
 
 
 
 
 
 
 
2290	queue_work(cma_wq, &work->work);
2291}
2292
2293static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2294			      struct cma_work *work)
2295{
2296	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2297	struct ib_sa_path_rec path_rec;
2298	ib_sa_comp_mask comp_mask;
2299	struct sockaddr_in6 *sin6;
2300	struct sockaddr_ib *sib;
2301
2302	memset(&path_rec, 0, sizeof path_rec);
 
 
 
 
 
2303	rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2304	rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2305	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2306	path_rec.numb_path = 1;
2307	path_rec.reversible = 1;
2308	path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
 
2309
2310	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2311		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2312		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2313
2314	switch (cma_family(id_priv)) {
2315	case AF_INET:
2316		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2317		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2318		break;
2319	case AF_INET6:
2320		sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2321		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2322		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2323		break;
2324	case AF_IB:
2325		sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2326		path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2327		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2328		break;
2329	}
2330
2331	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2332					       id_priv->id.port_num, &path_rec,
2333					       comp_mask, timeout_ms,
2334					       GFP_KERNEL, cma_query_handler,
2335					       work, &id_priv->query);
2336
2337	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2338}
2339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2340static void cma_work_handler(struct work_struct *_work)
2341{
2342	struct cma_work *work = container_of(_work, struct cma_work, work);
2343	struct rdma_id_private *id_priv = work->id;
2344	int destroy = 0;
2345
2346	mutex_lock(&id_priv->handler_mutex);
2347	if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2348		goto out;
 
 
 
 
 
2349
2350	if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
2351		cma_exch(id_priv, RDMA_CM_DESTROYING);
2352		destroy = 1;
 
2353	}
2354out:
 
2355	mutex_unlock(&id_priv->handler_mutex);
2356	cma_deref_id(id_priv);
2357	if (destroy)
2358		rdma_destroy_id(&id_priv->id);
 
2359	kfree(work);
2360}
2361
2362static void cma_ndev_work_handler(struct work_struct *_work)
 
2363{
2364	struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
2365	struct rdma_id_private *id_priv = work->id;
2366	int destroy = 0;
 
 
 
2367
2368	mutex_lock(&id_priv->handler_mutex);
2369	if (id_priv->state == RDMA_CM_DESTROYING ||
2370	    id_priv->state == RDMA_CM_DEVICE_REMOVAL)
2371		goto out;
 
2372
2373	if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
2374		cma_exch(id_priv, RDMA_CM_DESTROYING);
2375		destroy = 1;
2376	}
 
2377
2378out:
2379	mutex_unlock(&id_priv->handler_mutex);
2380	cma_deref_id(id_priv);
2381	if (destroy)
2382		rdma_destroy_id(&id_priv->id);
2383	kfree(work);
2384}
2385
2386static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
 
2387{
2388	struct rdma_route *route = &id_priv->id.route;
2389	struct cma_work *work;
2390	int ret;
2391
2392	work = kzalloc(sizeof *work, GFP_KERNEL);
2393	if (!work)
2394		return -ENOMEM;
2395
2396	work->id = id_priv;
2397	INIT_WORK(&work->work, cma_work_handler);
2398	work->old_state = RDMA_CM_ROUTE_QUERY;
2399	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2400	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2401
2402	route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
 
2403	if (!route->path_rec) {
2404		ret = -ENOMEM;
2405		goto err1;
2406	}
2407
2408	ret = cma_query_ib_route(id_priv, timeout_ms, work);
2409	if (ret)
2410		goto err2;
2411
2412	return 0;
2413err2:
2414	kfree(route->path_rec);
2415	route->path_rec = NULL;
2416err1:
2417	kfree(work);
2418	return ret;
2419}
2420
2421int rdma_set_ib_paths(struct rdma_cm_id *id,
2422		      struct ib_sa_path_rec *path_rec, int num_paths)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2423{
2424	struct rdma_id_private *id_priv;
 
2425	int ret;
2426
2427	id_priv = container_of(id, struct rdma_id_private, id);
2428	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2429			   RDMA_CM_ROUTE_RESOLVED))
2430		return -EINVAL;
2431
2432	id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2433				     GFP_KERNEL);
2434	if (!id->route.path_rec) {
2435		ret = -ENOMEM;
2436		goto err;
2437	}
2438
2439	id->route.num_paths = num_paths;
 
 
 
 
 
 
 
 
 
2440	return 0;
 
 
 
 
2441err:
2442	cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2443	return ret;
2444}
2445EXPORT_SYMBOL(rdma_set_ib_paths);
2446
2447static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2448{
2449	struct cma_work *work;
2450
2451	work = kzalloc(sizeof *work, GFP_KERNEL);
2452	if (!work)
2453		return -ENOMEM;
2454
2455	work->id = id_priv;
2456	INIT_WORK(&work->work, cma_work_handler);
2457	work->old_state = RDMA_CM_ROUTE_QUERY;
2458	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2459	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2460	queue_work(cma_wq, &work->work);
2461	return 0;
2462}
2463
2464static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2465{
2466	int prio;
2467	struct net_device *dev;
2468
2469	prio = rt_tos2priority(tos);
2470	dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
2471		vlan_dev_real_dev(ndev) : ndev;
2472
2473	if (dev->num_tc)
2474		return netdev_get_prio_tc_map(dev, prio);
2475
2476#if IS_ENABLED(CONFIG_VLAN_8021Q)
2477	if (ndev->priv_flags & IFF_802_1Q_VLAN)
2478		return (vlan_dev_get_egress_qos_mask(ndev, prio) &
2479			VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2480#endif
2481	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2482}
2483
2484static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2485					   unsigned long supported_gids,
2486					   enum ib_gid_type default_gid)
2487{
2488	if ((network_type == RDMA_NETWORK_IPV4 ||
2489	     network_type == RDMA_NETWORK_IPV6) &&
2490	    test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2491		return IB_GID_TYPE_ROCE_UDP_ENCAP;
 
 
 
 
 
 
 
 
2492
2493	return default_gid;
2494}
2495
2496static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2497{
2498	struct rdma_route *route = &id_priv->id.route;
2499	struct rdma_addr *addr = &route->addr;
2500	struct cma_work *work;
2501	int ret;
2502	struct net_device *ndev = NULL;
 
 
 
 
2503
 
 
 
2504
2505	work = kzalloc(sizeof *work, GFP_KERNEL);
2506	if (!work)
2507		return -ENOMEM;
2508
2509	work->id = id_priv;
2510	INIT_WORK(&work->work, cma_work_handler);
2511
2512	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2513	if (!route->path_rec) {
2514		ret = -ENOMEM;
2515		goto err1;
2516	}
2517
2518	route->num_paths = 1;
2519
2520	if (addr->dev_addr.bound_dev_if) {
2521		unsigned long supported_gids;
2522
2523		ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
2524		if (!ndev) {
2525			ret = -ENODEV;
2526			goto err2;
2527		}
2528
2529		if (ndev->flags & IFF_LOOPBACK) {
2530			dev_put(ndev);
2531			if (!id_priv->id.device->get_netdev) {
2532				ret = -EOPNOTSUPP;
2533				goto err2;
2534			}
2535
2536			ndev = id_priv->id.device->get_netdev(id_priv->id.device,
2537							      id_priv->id.port_num);
2538			if (!ndev) {
2539				ret = -ENODEV;
2540				goto err2;
2541			}
2542		}
2543
2544		route->path_rec->net = &init_net;
2545		route->path_rec->ifindex = ndev->ifindex;
2546		supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2547							    id_priv->id.port_num);
2548		route->path_rec->gid_type =
2549			cma_route_gid_type(addr->dev_addr.network,
2550					   supported_gids,
2551					   id_priv->gid_type);
2552	}
2553	if (!ndev) {
2554		ret = -ENODEV;
2555		goto err2;
2556	}
2557
2558	memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
2559
2560	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2561		    &route->path_rec->sgid);
2562	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2563		    &route->path_rec->dgid);
2564
2565	/* Use the hint from IP Stack to select GID Type */
2566	if (route->path_rec->gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2567		route->path_rec->gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2568	if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
2569		/* TODO: get the hoplimit from the inet/inet6 device */
2570		route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2571	else
2572		route->path_rec->hop_limit = 1;
2573	route->path_rec->reversible = 1;
2574	route->path_rec->pkey = cpu_to_be16(0xffff);
2575	route->path_rec->mtu_selector = IB_SA_EQ;
2576	route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
 
2577	route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2578	route->path_rec->rate_selector = IB_SA_EQ;
2579	route->path_rec->rate = iboe_get_rate(ndev);
2580	dev_put(ndev);
2581	route->path_rec->packet_life_time_selector = IB_SA_EQ;
2582	route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
 
 
 
 
 
 
 
 
 
 
 
 
 
2583	if (!route->path_rec->mtu) {
2584		ret = -EINVAL;
2585		goto err2;
2586	}
2587
2588	work->old_state = RDMA_CM_ROUTE_QUERY;
2589	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2590	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2591	work->event.status = 0;
2592
 
2593	queue_work(cma_wq, &work->work);
2594
2595	return 0;
2596
2597err2:
2598	kfree(route->path_rec);
2599	route->path_rec = NULL;
 
2600err1:
2601	kfree(work);
2602	return ret;
2603}
2604
2605int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2606{
2607	struct rdma_id_private *id_priv;
2608	int ret;
2609
 
 
 
2610	id_priv = container_of(id, struct rdma_id_private, id);
2611	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
2612		return -EINVAL;
2613
2614	atomic_inc(&id_priv->refcount);
2615	if (rdma_cap_ib_sa(id->device, id->port_num))
2616		ret = cma_resolve_ib_route(id_priv, timeout_ms);
2617	else if (rdma_protocol_roce(id->device, id->port_num))
2618		ret = cma_resolve_iboe_route(id_priv);
 
 
 
2619	else if (rdma_protocol_iwarp(id->device, id->port_num))
2620		ret = cma_resolve_iw_route(id_priv, timeout_ms);
2621	else
2622		ret = -ENOSYS;
2623
2624	if (ret)
2625		goto err;
2626
2627	return 0;
2628err:
2629	cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
2630	cma_deref_id(id_priv);
2631	return ret;
2632}
2633EXPORT_SYMBOL(rdma_resolve_route);
2634
2635static void cma_set_loopback(struct sockaddr *addr)
2636{
2637	switch (addr->sa_family) {
2638	case AF_INET:
2639		((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2640		break;
2641	case AF_INET6:
2642		ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2643			      0, 0, 0, htonl(1));
2644		break;
2645	default:
2646		ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2647			    0, 0, 0, htonl(1));
2648		break;
2649	}
2650}
2651
2652static int cma_bind_loopback(struct rdma_id_private *id_priv)
2653{
2654	struct cma_device *cma_dev, *cur_dev;
2655	struct ib_port_attr port_attr;
2656	union ib_gid gid;
 
 
2657	u16 pkey;
2658	int ret;
2659	u8 p;
2660
2661	cma_dev = NULL;
2662	mutex_lock(&lock);
2663	list_for_each_entry(cur_dev, &dev_list, list) {
2664		if (cma_family(id_priv) == AF_IB &&
2665		    !rdma_cap_ib_cm(cur_dev->device, 1))
2666			continue;
2667
2668		if (!cma_dev)
2669			cma_dev = cur_dev;
2670
2671		for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2672			if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2673			    port_attr.state == IB_PORT_ACTIVE) {
2674				cma_dev = cur_dev;
2675				goto port_found;
2676			}
2677		}
2678	}
2679
2680	if (!cma_dev) {
2681		ret = -ENODEV;
2682		goto out;
2683	}
2684
2685	p = 1;
2686
2687port_found:
2688	ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
2689	if (ret)
2690		goto out;
2691
2692	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2693	if (ret)
2694		goto out;
2695
2696	id_priv->id.route.addr.dev_addr.dev_type =
2697		(rdma_protocol_ib(cma_dev->device, p)) ?
2698		ARPHRD_INFINIBAND : ARPHRD_ETHER;
2699
2700	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2701	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2702	id_priv->id.port_num = p;
2703	cma_attach_to_dev(id_priv, cma_dev);
 
2704	cma_set_loopback(cma_src_addr(id_priv));
2705out:
2706	mutex_unlock(&lock);
2707	return ret;
2708}
2709
2710static void addr_handler(int status, struct sockaddr *src_addr,
2711			 struct rdma_dev_addr *dev_addr, void *context)
2712{
2713	struct rdma_id_private *id_priv = context;
2714	struct rdma_cm_event event;
 
 
2715
2716	memset(&event, 0, sizeof event);
2717	mutex_lock(&id_priv->handler_mutex);
2718	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2719			   RDMA_CM_ADDR_RESOLVED))
2720		goto out;
2721
2722	memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
2723	if (!status && !id_priv->cma_dev)
2724		status = cma_acquire_dev(id_priv, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2725
2726	if (status) {
 
 
2727		if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2728				   RDMA_CM_ADDR_BOUND))
2729			goto out;
2730		event.event = RDMA_CM_EVENT_ADDR_ERROR;
2731		event.status = status;
2732	} else
2733		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2734
2735	if (id_priv->id.event_handler(&id_priv->id, &event)) {
2736		cma_exch(id_priv, RDMA_CM_DESTROYING);
2737		mutex_unlock(&id_priv->handler_mutex);
2738		cma_deref_id(id_priv);
2739		rdma_destroy_id(&id_priv->id);
2740		return;
2741	}
2742out:
2743	mutex_unlock(&id_priv->handler_mutex);
2744	cma_deref_id(id_priv);
2745}
2746
2747static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2748{
2749	struct cma_work *work;
2750	union ib_gid gid;
2751	int ret;
2752
2753	work = kzalloc(sizeof *work, GFP_KERNEL);
2754	if (!work)
2755		return -ENOMEM;
2756
2757	if (!id_priv->cma_dev) {
2758		ret = cma_bind_loopback(id_priv);
2759		if (ret)
2760			goto err;
2761	}
2762
2763	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2764	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
2765
2766	work->id = id_priv;
2767	INIT_WORK(&work->work, cma_work_handler);
2768	work->old_state = RDMA_CM_ADDR_QUERY;
2769	work->new_state = RDMA_CM_ADDR_RESOLVED;
2770	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2771	queue_work(cma_wq, &work->work);
2772	return 0;
2773err:
2774	kfree(work);
2775	return ret;
2776}
2777
2778static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2779{
2780	struct cma_work *work;
2781	int ret;
2782
2783	work = kzalloc(sizeof *work, GFP_KERNEL);
2784	if (!work)
2785		return -ENOMEM;
2786
2787	if (!id_priv->cma_dev) {
2788		ret = cma_resolve_ib_dev(id_priv);
2789		if (ret)
2790			goto err;
2791	}
2792
2793	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2794		&(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2795
2796	work->id = id_priv;
2797	INIT_WORK(&work->work, cma_work_handler);
2798	work->old_state = RDMA_CM_ADDR_QUERY;
2799	work->new_state = RDMA_CM_ADDR_RESOLVED;
2800	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2801	queue_work(cma_wq, &work->work);
2802	return 0;
2803err:
2804	kfree(work);
2805	return ret;
2806}
2807
2808static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2809			 struct sockaddr *dst_addr)
2810{
2811	if (!src_addr || !src_addr->sa_family) {
2812		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2813		src_addr->sa_family = dst_addr->sa_family;
2814		if (IS_ENABLED(CONFIG_IPV6) &&
2815		    dst_addr->sa_family == AF_INET6) {
2816			struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2817			struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2818			src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2819			if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
2820				id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
2821		} else if (dst_addr->sa_family == AF_IB) {
2822			((struct sockaddr_ib *) src_addr)->sib_pkey =
2823				((struct sockaddr_ib *) dst_addr)->sib_pkey;
2824		}
2825	}
2826	return rdma_bind_addr(id, src_addr);
2827}
2828
2829int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2830		      struct sockaddr *dst_addr, int timeout_ms)
2831{
2832	struct rdma_id_private *id_priv;
2833	int ret;
2834
2835	id_priv = container_of(id, struct rdma_id_private, id);
2836	if (id_priv->state == RDMA_CM_IDLE) {
2837		ret = cma_bind_addr(id, src_addr, dst_addr);
2838		if (ret)
2839			return ret;
2840	}
2841
2842	if (cma_family(id_priv) != dst_addr->sa_family)
2843		return -EINVAL;
2844
2845	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
2846		return -EINVAL;
2847
2848	atomic_inc(&id_priv->refcount);
2849	memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
2850	if (cma_any_addr(dst_addr)) {
2851		ret = cma_resolve_loopback(id_priv);
2852	} else {
2853		if (dst_addr->sa_family == AF_IB) {
2854			ret = cma_resolve_ib_addr(id_priv);
2855		} else {
2856			ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2857					      dst_addr, &id->route.addr.dev_addr,
2858					      timeout_ms, addr_handler, id_priv);
2859		}
2860	}
2861	if (ret)
2862		goto err;
2863
2864	return 0;
2865err:
2866	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
2867	cma_deref_id(id_priv);
2868	return ret;
2869}
2870EXPORT_SYMBOL(rdma_resolve_addr);
2871
2872int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2873{
2874	struct rdma_id_private *id_priv;
2875	unsigned long flags;
2876	int ret;
2877
2878	id_priv = container_of(id, struct rdma_id_private, id);
2879	spin_lock_irqsave(&id_priv->lock, flags);
2880	if (reuse || id_priv->state == RDMA_CM_IDLE) {
 
2881		id_priv->reuseaddr = reuse;
2882		ret = 0;
2883	} else {
2884		ret = -EINVAL;
2885	}
2886	spin_unlock_irqrestore(&id_priv->lock, flags);
2887	return ret;
2888}
2889EXPORT_SYMBOL(rdma_set_reuseaddr);
2890
2891int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2892{
2893	struct rdma_id_private *id_priv;
2894	unsigned long flags;
2895	int ret;
2896
2897	id_priv = container_of(id, struct rdma_id_private, id);
2898	spin_lock_irqsave(&id_priv->lock, flags);
2899	if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2900		id_priv->options |= (1 << CMA_OPTION_AFONLY);
2901		id_priv->afonly = afonly;
2902		ret = 0;
2903	} else {
2904		ret = -EINVAL;
2905	}
2906	spin_unlock_irqrestore(&id_priv->lock, flags);
2907	return ret;
2908}
2909EXPORT_SYMBOL(rdma_set_afonly);
2910
2911static void cma_bind_port(struct rdma_bind_list *bind_list,
2912			  struct rdma_id_private *id_priv)
2913{
2914	struct sockaddr *addr;
2915	struct sockaddr_ib *sib;
2916	u64 sid, mask;
2917	__be16 port;
2918
 
 
2919	addr = cma_src_addr(id_priv);
2920	port = htons(bind_list->port);
2921
2922	switch (addr->sa_family) {
2923	case AF_INET:
2924		((struct sockaddr_in *) addr)->sin_port = port;
2925		break;
2926	case AF_INET6:
2927		((struct sockaddr_in6 *) addr)->sin6_port = port;
2928		break;
2929	case AF_IB:
2930		sib = (struct sockaddr_ib *) addr;
2931		sid = be64_to_cpu(sib->sib_sid);
2932		mask = be64_to_cpu(sib->sib_sid_mask);
2933		sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2934		sib->sib_sid_mask = cpu_to_be64(~0ULL);
2935		break;
2936	}
2937	id_priv->bind_list = bind_list;
2938	hlist_add_head(&id_priv->node, &bind_list->owners);
2939}
2940
2941static int cma_alloc_port(enum rdma_port_space ps,
2942			  struct rdma_id_private *id_priv, unsigned short snum)
2943{
2944	struct rdma_bind_list *bind_list;
2945	int ret;
2946
 
 
2947	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
2948	if (!bind_list)
2949		return -ENOMEM;
2950
2951	ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
2952			   snum);
2953	if (ret < 0)
2954		goto err;
2955
2956	bind_list->ps = ps;
2957	bind_list->port = (unsigned short)ret;
2958	cma_bind_port(bind_list, id_priv);
2959	return 0;
2960err:
2961	kfree(bind_list);
2962	return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
2963}
2964
2965static int cma_alloc_any_port(enum rdma_port_space ps,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2966			      struct rdma_id_private *id_priv)
2967{
2968	static unsigned int last_used_port;
2969	int low, high, remaining;
2970	unsigned int rover;
2971	struct net *net = id_priv->id.route.addr.dev_addr.net;
2972
 
 
2973	inet_get_local_port_range(net, &low, &high);
2974	remaining = (high - low) + 1;
2975	rover = prandom_u32() % remaining + low;
2976retry:
2977	if (last_used_port != rover &&
2978	    !cma_ps_find(net, ps, (unsigned short)rover)) {
2979		int ret = cma_alloc_port(ps, id_priv, rover);
 
 
 
 
 
 
 
 
 
 
2980		/*
2981		 * Remember previously used port number in order to avoid
2982		 * re-using same port immediately after it is closed.
2983		 */
2984		if (!ret)
2985			last_used_port = rover;
2986		if (ret != -EADDRNOTAVAIL)
2987			return ret;
2988	}
2989	if (--remaining) {
2990		rover++;
2991		if ((rover < low) || (rover > high))
2992			rover = low;
2993		goto retry;
2994	}
2995	return -EADDRNOTAVAIL;
2996}
2997
2998/*
2999 * Check that the requested port is available.  This is called when trying to
3000 * bind to a specific port, or when trying to listen on a bound port.  In
3001 * the latter case, the provided id_priv may already be on the bind_list, but
3002 * we still need to check that it's okay to start listening.
3003 */
3004static int cma_check_port(struct rdma_bind_list *bind_list,
3005			  struct rdma_id_private *id_priv, uint8_t reuseaddr)
3006{
3007	struct rdma_id_private *cur_id;
3008	struct sockaddr *addr, *cur_addr;
3009
 
 
3010	addr = cma_src_addr(id_priv);
3011	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3012		if (id_priv == cur_id)
3013			continue;
3014
3015		if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
3016		    cur_id->reuseaddr)
3017			continue;
3018
3019		cur_addr = cma_src_addr(cur_id);
3020		if (id_priv->afonly && cur_id->afonly &&
3021		    (addr->sa_family != cur_addr->sa_family))
3022			continue;
3023
3024		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3025			return -EADDRNOTAVAIL;
3026
3027		if (!cma_addr_cmp(addr, cur_addr))
3028			return -EADDRINUSE;
3029	}
3030	return 0;
3031}
3032
3033static int cma_use_port(enum rdma_port_space ps,
3034			struct rdma_id_private *id_priv)
3035{
3036	struct rdma_bind_list *bind_list;
3037	unsigned short snum;
3038	int ret;
3039
 
 
3040	snum = ntohs(cma_port(cma_src_addr(id_priv)));
3041	if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3042		return -EACCES;
3043
3044	bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3045	if (!bind_list) {
3046		ret = cma_alloc_port(ps, id_priv, snum);
3047	} else {
3048		ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3049		if (!ret)
3050			cma_bind_port(bind_list, id_priv);
3051	}
3052	return ret;
3053}
3054
3055static int cma_bind_listen(struct rdma_id_private *id_priv)
3056{
3057	struct rdma_bind_list *bind_list = id_priv->bind_list;
3058	int ret = 0;
3059
3060	mutex_lock(&lock);
3061	if (bind_list->owners.first->next)
3062		ret = cma_check_port(bind_list, id_priv, 0);
3063	mutex_unlock(&lock);
3064	return ret;
3065}
3066
3067static enum rdma_port_space cma_select_inet_ps(
3068		struct rdma_id_private *id_priv)
3069{
3070	switch (id_priv->id.ps) {
3071	case RDMA_PS_TCP:
3072	case RDMA_PS_UDP:
3073	case RDMA_PS_IPOIB:
3074	case RDMA_PS_IB:
3075		return id_priv->id.ps;
3076	default:
3077
3078		return 0;
3079	}
3080}
3081
3082static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
 
3083{
3084	enum rdma_port_space ps = 0;
3085	struct sockaddr_ib *sib;
3086	u64 sid_ps, mask, sid;
3087
3088	sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3089	mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3090	sid = be64_to_cpu(sib->sib_sid) & mask;
3091
3092	if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3093		sid_ps = RDMA_IB_IP_PS_IB;
3094		ps = RDMA_PS_IB;
3095	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3096		   (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3097		sid_ps = RDMA_IB_IP_PS_TCP;
3098		ps = RDMA_PS_TCP;
3099	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3100		   (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3101		sid_ps = RDMA_IB_IP_PS_UDP;
3102		ps = RDMA_PS_UDP;
3103	}
3104
3105	if (ps) {
3106		sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3107		sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3108						be64_to_cpu(sib->sib_sid_mask));
3109	}
3110	return ps;
3111}
3112
3113static int cma_get_port(struct rdma_id_private *id_priv)
3114{
3115	enum rdma_port_space ps;
3116	int ret;
3117
3118	if (cma_family(id_priv) != AF_IB)
3119		ps = cma_select_inet_ps(id_priv);
3120	else
3121		ps = cma_select_ib_ps(id_priv);
3122	if (!ps)
3123		return -EPROTONOSUPPORT;
3124
3125	mutex_lock(&lock);
3126	if (cma_any_port(cma_src_addr(id_priv)))
3127		ret = cma_alloc_any_port(ps, id_priv);
3128	else
3129		ret = cma_use_port(ps, id_priv);
3130	mutex_unlock(&lock);
3131
3132	return ret;
3133}
3134
3135static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3136			       struct sockaddr *addr)
3137{
3138#if IS_ENABLED(CONFIG_IPV6)
3139	struct sockaddr_in6 *sin6;
3140
3141	if (addr->sa_family != AF_INET6)
3142		return 0;
3143
3144	sin6 = (struct sockaddr_in6 *) addr;
3145
3146	if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3147		return 0;
3148
3149	if (!sin6->sin6_scope_id)
3150			return -EINVAL;
3151
3152	dev_addr->bound_dev_if = sin6->sin6_scope_id;
3153#endif
3154	return 0;
3155}
3156
3157int rdma_listen(struct rdma_cm_id *id, int backlog)
3158{
3159	struct rdma_id_private *id_priv;
 
3160	int ret;
3161
3162	id_priv = container_of(id, struct rdma_id_private, id);
3163	if (id_priv->state == RDMA_CM_IDLE) {
3164		id->route.addr.src_addr.ss_family = AF_INET;
3165		ret = rdma_bind_addr(id, cma_src_addr(id_priv));
 
 
 
 
3166		if (ret)
3167			return ret;
 
 
 
3168	}
3169
3170	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
3171		return -EINVAL;
3172
 
3173	if (id_priv->reuseaddr) {
3174		ret = cma_bind_listen(id_priv);
 
 
 
 
3175		if (ret)
3176			goto err;
3177	}
3178
3179	id_priv->backlog = backlog;
3180	if (id->device) {
3181		if (rdma_cap_ib_cm(id->device, 1)) {
3182			ret = cma_ib_listen(id_priv);
3183			if (ret)
3184				goto err;
3185		} else if (rdma_cap_iw_cm(id->device, 1)) {
3186			ret = cma_iw_listen(id_priv, backlog);
3187			if (ret)
3188				goto err;
3189		} else {
3190			ret = -ENOSYS;
3191			goto err;
3192		}
3193	} else
3194		cma_listen_on_all(id_priv);
 
 
 
3195
3196	return 0;
3197err:
3198	id_priv->backlog = 0;
 
 
 
 
3199	cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3200	return ret;
3201}
3202EXPORT_SYMBOL(rdma_listen);
3203
3204int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
 
3205{
3206	struct rdma_id_private *id_priv;
3207	int ret;
3208
3209	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3210	    addr->sa_family != AF_IB)
3211		return -EAFNOSUPPORT;
3212
3213	id_priv = container_of(id, struct rdma_id_private, id);
3214	if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3215		return -EINVAL;
3216
3217	ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3218	if (ret)
3219		goto err1;
3220
3221	memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3222	if (!cma_any_addr(addr)) {
3223		ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3224		if (ret)
3225			goto err1;
3226
3227		ret = cma_acquire_dev(id_priv, NULL);
3228		if (ret)
3229			goto err1;
3230	}
3231
3232	if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3233		if (addr->sa_family == AF_INET)
3234			id_priv->afonly = 1;
3235#if IS_ENABLED(CONFIG_IPV6)
3236		else if (addr->sa_family == AF_INET6) {
3237			struct net *net = id_priv->id.route.addr.dev_addr.net;
3238
3239			id_priv->afonly = net->ipv6.sysctl.bindv6only;
3240		}
3241#endif
3242	}
 
 
 
 
 
3243	ret = cma_get_port(id_priv);
3244	if (ret)
3245		goto err2;
3246
 
 
3247	return 0;
3248err2:
3249	if (id_priv->cma_dev)
3250		cma_release_dev(id_priv);
3251err1:
3252	cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3253	return ret;
3254}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3255EXPORT_SYMBOL(rdma_bind_addr);
3256
3257static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3258{
3259	struct cma_hdr *cma_hdr;
3260
3261	cma_hdr = hdr;
3262	cma_hdr->cma_version = CMA_VERSION;
3263	if (cma_family(id_priv) == AF_INET) {
3264		struct sockaddr_in *src4, *dst4;
3265
3266		src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3267		dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3268
3269		cma_set_ip_ver(cma_hdr, 4);
3270		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3271		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3272		cma_hdr->port = src4->sin_port;
3273	} else if (cma_family(id_priv) == AF_INET6) {
3274		struct sockaddr_in6 *src6, *dst6;
3275
3276		src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3277		dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3278
3279		cma_set_ip_ver(cma_hdr, 6);
3280		cma_hdr->src_addr.ip6 = src6->sin6_addr;
3281		cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3282		cma_hdr->port = src6->sin6_port;
3283	}
3284	return 0;
3285}
3286
3287static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3288				struct ib_cm_event *ib_event)
3289{
3290	struct rdma_id_private *id_priv = cm_id->context;
3291	struct rdma_cm_event event;
3292	struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3293	int ret = 0;
 
3294
3295	mutex_lock(&id_priv->handler_mutex);
3296	if (id_priv->state != RDMA_CM_CONNECT)
3297		goto out;
3298
3299	memset(&event, 0, sizeof event);
3300	switch (ib_event->event) {
3301	case IB_CM_SIDR_REQ_ERROR:
3302		event.event = RDMA_CM_EVENT_UNREACHABLE;
3303		event.status = -ETIMEDOUT;
3304		break;
3305	case IB_CM_SIDR_REP_RECEIVED:
3306		event.param.ud.private_data = ib_event->private_data;
3307		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3308		if (rep->status != IB_SIDR_SUCCESS) {
3309			event.event = RDMA_CM_EVENT_UNREACHABLE;
3310			event.status = ib_event->param.sidr_rep_rcvd.status;
 
 
3311			break;
3312		}
3313		ret = cma_set_qkey(id_priv, rep->qkey);
3314		if (ret) {
 
3315			event.event = RDMA_CM_EVENT_ADDR_ERROR;
3316			event.status = ret;
3317			break;
3318		}
3319		ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
3320				     id_priv->id.route.path_rec,
3321				     &event.param.ud.ah_attr);
 
 
3322		event.param.ud.qp_num = rep->qpn;
3323		event.param.ud.qkey = rep->qkey;
3324		event.event = RDMA_CM_EVENT_ESTABLISHED;
3325		event.status = 0;
3326		break;
3327	default:
3328		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3329		       ib_event->event);
3330		goto out;
3331	}
3332
3333	ret = id_priv->id.event_handler(&id_priv->id, &event);
 
 
3334	if (ret) {
3335		/* Destroy the CM ID by returning a non-zero value. */
3336		id_priv->cm_id.ib = NULL;
3337		cma_exch(id_priv, RDMA_CM_DESTROYING);
3338		mutex_unlock(&id_priv->handler_mutex);
3339		rdma_destroy_id(&id_priv->id);
3340		return ret;
3341	}
3342out:
3343	mutex_unlock(&id_priv->handler_mutex);
3344	return ret;
3345}
3346
3347static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3348			      struct rdma_conn_param *conn_param)
3349{
3350	struct ib_cm_sidr_req_param req;
3351	struct ib_cm_id	*id;
3352	void *private_data;
3353	int offset, ret;
 
3354
3355	memset(&req, 0, sizeof req);
3356	offset = cma_user_data_offset(id_priv);
3357	req.private_data_len = offset + conn_param->private_data_len;
3358	if (req.private_data_len < conn_param->private_data_len)
3359		return -EINVAL;
3360
3361	if (req.private_data_len) {
3362		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3363		if (!private_data)
3364			return -ENOMEM;
3365	} else {
3366		private_data = NULL;
3367	}
3368
3369	if (conn_param->private_data && conn_param->private_data_len)
3370		memcpy(private_data + offset, conn_param->private_data,
3371		       conn_param->private_data_len);
3372
3373	if (private_data) {
3374		ret = cma_format_hdr(private_data, id_priv);
3375		if (ret)
3376			goto out;
3377		req.private_data = private_data;
3378	}
3379
3380	id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3381			     id_priv);
3382	if (IS_ERR(id)) {
3383		ret = PTR_ERR(id);
3384		goto out;
3385	}
3386	id_priv->cm_id.ib = id;
3387
3388	req.path = id_priv->id.route.path_rec;
 
3389	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3390	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3391	req.max_cm_retries = CMA_MAX_CM_RETRIES;
3392
 
3393	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3394	if (ret) {
3395		ib_destroy_cm_id(id_priv->cm_id.ib);
3396		id_priv->cm_id.ib = NULL;
3397	}
3398out:
3399	kfree(private_data);
3400	return ret;
3401}
3402
3403static int cma_connect_ib(struct rdma_id_private *id_priv,
3404			  struct rdma_conn_param *conn_param)
3405{
3406	struct ib_cm_req_param req;
3407	struct rdma_route *route;
3408	void *private_data;
3409	struct ib_cm_id	*id;
3410	int offset, ret;
 
3411
3412	memset(&req, 0, sizeof req);
3413	offset = cma_user_data_offset(id_priv);
3414	req.private_data_len = offset + conn_param->private_data_len;
3415	if (req.private_data_len < conn_param->private_data_len)
3416		return -EINVAL;
3417
3418	if (req.private_data_len) {
3419		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3420		if (!private_data)
3421			return -ENOMEM;
3422	} else {
3423		private_data = NULL;
3424	}
3425
3426	if (conn_param->private_data && conn_param->private_data_len)
3427		memcpy(private_data + offset, conn_param->private_data,
3428		       conn_param->private_data_len);
3429
3430	id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3431	if (IS_ERR(id)) {
3432		ret = PTR_ERR(id);
3433		goto out;
3434	}
3435	id_priv->cm_id.ib = id;
3436
3437	route = &id_priv->id.route;
3438	if (private_data) {
3439		ret = cma_format_hdr(private_data, id_priv);
3440		if (ret)
3441			goto out;
3442		req.private_data = private_data;
3443	}
3444
3445	req.primary_path = &route->path_rec[0];
3446	if (route->num_paths == 2)
 
 
3447		req.alternate_path = &route->path_rec[1];
3448
 
 
3449	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3450	req.qp_num = id_priv->qp_num;
3451	req.qp_type = id_priv->id.qp_type;
3452	req.starting_psn = id_priv->seq_num;
3453	req.responder_resources = conn_param->responder_resources;
3454	req.initiator_depth = conn_param->initiator_depth;
3455	req.flow_control = conn_param->flow_control;
3456	req.retry_count = min_t(u8, 7, conn_param->retry_count);
3457	req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3458	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3459	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3460	req.max_cm_retries = CMA_MAX_CM_RETRIES;
3461	req.srq = id_priv->srq ? 1 : 0;
 
 
3462
 
3463	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3464out:
3465	if (ret && !IS_ERR(id)) {
3466		ib_destroy_cm_id(id);
3467		id_priv->cm_id.ib = NULL;
3468	}
3469
3470	kfree(private_data);
3471	return ret;
3472}
3473
3474static int cma_connect_iw(struct rdma_id_private *id_priv,
3475			  struct rdma_conn_param *conn_param)
3476{
3477	struct iw_cm_id *cm_id;
3478	int ret;
3479	struct iw_cm_conn_param iw_param;
3480
3481	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
3482	if (IS_ERR(cm_id))
3483		return PTR_ERR(cm_id);
3484
 
3485	cm_id->tos = id_priv->tos;
 
 
 
3486	id_priv->cm_id.iw = cm_id;
3487
3488	memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3489	       rdma_addr_size(cma_src_addr(id_priv)));
3490	memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3491	       rdma_addr_size(cma_dst_addr(id_priv)));
3492
3493	ret = cma_modify_qp_rtr(id_priv, conn_param);
3494	if (ret)
3495		goto out;
3496
3497	if (conn_param) {
3498		iw_param.ord = conn_param->initiator_depth;
3499		iw_param.ird = conn_param->responder_resources;
3500		iw_param.private_data = conn_param->private_data;
3501		iw_param.private_data_len = conn_param->private_data_len;
3502		iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3503	} else {
3504		memset(&iw_param, 0, sizeof iw_param);
3505		iw_param.qpn = id_priv->qp_num;
3506	}
3507	ret = iw_cm_connect(cm_id, &iw_param);
3508out:
3509	if (ret) {
3510		iw_destroy_cm_id(cm_id);
3511		id_priv->cm_id.iw = NULL;
3512	}
3513	return ret;
3514}
3515
3516int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
 
 
 
 
 
 
 
 
 
3517{
3518	struct rdma_id_private *id_priv;
 
3519	int ret;
3520
3521	id_priv = container_of(id, struct rdma_id_private, id);
3522	if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
3523		return -EINVAL;
3524
3525	if (!id->qp) {
3526		id_priv->qp_num = conn_param->qp_num;
3527		id_priv->srq = conn_param->srq;
3528	}
3529
3530	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3531		if (id->qp_type == IB_QPT_UD)
3532			ret = cma_resolve_ib_udp(id_priv, conn_param);
3533		else
3534			ret = cma_connect_ib(id_priv, conn_param);
3535	} else if (rdma_cap_iw_cm(id->device, id->port_num))
3536		ret = cma_connect_iw(id_priv, conn_param);
3537	else
3538		ret = -ENOSYS;
 
3539	if (ret)
3540		goto err;
3541
3542	return 0;
3543err:
3544	cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
3545	return ret;
3546}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3547EXPORT_SYMBOL(rdma_connect);
3548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3549static int cma_accept_ib(struct rdma_id_private *id_priv,
3550			 struct rdma_conn_param *conn_param)
3551{
3552	struct ib_cm_rep_param rep;
3553	int ret;
3554
3555	ret = cma_modify_qp_rtr(id_priv, conn_param);
3556	if (ret)
3557		goto out;
3558
3559	ret = cma_modify_qp_rts(id_priv, conn_param);
3560	if (ret)
3561		goto out;
3562
3563	memset(&rep, 0, sizeof rep);
3564	rep.qp_num = id_priv->qp_num;
3565	rep.starting_psn = id_priv->seq_num;
3566	rep.private_data = conn_param->private_data;
3567	rep.private_data_len = conn_param->private_data_len;
3568	rep.responder_resources = conn_param->responder_resources;
3569	rep.initiator_depth = conn_param->initiator_depth;
3570	rep.failover_accepted = 0;
3571	rep.flow_control = conn_param->flow_control;
3572	rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3573	rep.srq = id_priv->srq ? 1 : 0;
 
 
3574
 
3575	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3576out:
3577	return ret;
3578}
3579
3580static int cma_accept_iw(struct rdma_id_private *id_priv,
3581		  struct rdma_conn_param *conn_param)
3582{
3583	struct iw_cm_conn_param iw_param;
3584	int ret;
3585
3586	if (!conn_param)
3587		return -EINVAL;
3588
3589	ret = cma_modify_qp_rtr(id_priv, conn_param);
3590	if (ret)
3591		return ret;
3592
3593	iw_param.ord = conn_param->initiator_depth;
3594	iw_param.ird = conn_param->responder_resources;
3595	iw_param.private_data = conn_param->private_data;
3596	iw_param.private_data_len = conn_param->private_data_len;
3597	if (id_priv->id.qp) {
3598		iw_param.qpn = id_priv->qp_num;
3599	} else
3600		iw_param.qpn = conn_param->qp_num;
3601
3602	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3603}
3604
3605static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
3606			     enum ib_cm_sidr_status status, u32 qkey,
3607			     const void *private_data, int private_data_len)
3608{
3609	struct ib_cm_sidr_rep_param rep;
3610	int ret;
3611
3612	memset(&rep, 0, sizeof rep);
3613	rep.status = status;
3614	if (status == IB_SIDR_SUCCESS) {
3615		ret = cma_set_qkey(id_priv, qkey);
 
 
 
3616		if (ret)
3617			return ret;
3618		rep.qp_num = id_priv->qp_num;
3619		rep.qkey = id_priv->qkey;
 
 
 
3620	}
 
3621	rep.private_data = private_data;
3622	rep.private_data_len = private_data_len;
3623
 
3624	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3625}
3626
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3627int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3628{
3629	struct rdma_id_private *id_priv;
 
3630	int ret;
3631
3632	id_priv = container_of(id, struct rdma_id_private, id);
3633
3634	id_priv->owner = task_pid_nr(current);
3635
3636	if (!cma_comp(id_priv, RDMA_CM_CONNECT))
3637		return -EINVAL;
3638
3639	if (!id->qp && conn_param) {
3640		id_priv->qp_num = conn_param->qp_num;
3641		id_priv->srq = conn_param->srq;
3642	}
3643
3644	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3645		if (id->qp_type == IB_QPT_UD) {
3646			if (conn_param)
3647				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3648							conn_param->qkey,
3649							conn_param->private_data,
3650							conn_param->private_data_len);
3651			else
3652				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3653							0, NULL, 0);
3654		} else {
3655			if (conn_param)
3656				ret = cma_accept_ib(id_priv, conn_param);
3657			else
3658				ret = cma_rep_recv(id_priv);
3659		}
3660	} else if (rdma_cap_iw_cm(id->device, id->port_num))
3661		ret = cma_accept_iw(id_priv, conn_param);
3662	else
3663		ret = -ENOSYS;
3664
3665	if (ret)
3666		goto reject;
3667
3668	return 0;
3669reject:
3670	cma_modify_qp_err(id_priv);
3671	rdma_reject(id, NULL, 0);
3672	return ret;
3673}
3674EXPORT_SYMBOL(rdma_accept);
3675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3676int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3677{
3678	struct rdma_id_private *id_priv;
3679	int ret;
3680
3681	id_priv = container_of(id, struct rdma_id_private, id);
3682	if (!id_priv->cm_id.ib)
3683		return -EINVAL;
3684
3685	switch (id->device->node_type) {
3686	case RDMA_NODE_IB_CA:
3687		ret = ib_cm_notify(id_priv->cm_id.ib, event);
3688		break;
3689	default:
3690		ret = 0;
3691		break;
3692	}
3693	return ret;
3694}
3695EXPORT_SYMBOL(rdma_notify);
3696
3697int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3698		u8 private_data_len)
3699{
3700	struct rdma_id_private *id_priv;
3701	int ret;
3702
3703	id_priv = container_of(id, struct rdma_id_private, id);
3704	if (!id_priv->cm_id.ib)
3705		return -EINVAL;
3706
3707	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3708		if (id->qp_type == IB_QPT_UD)
3709			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
3710						private_data, private_data_len);
3711		else
3712			ret = ib_send_cm_rej(id_priv->cm_id.ib,
3713					     IB_CM_REJ_CONSUMER_DEFINED, NULL,
3714					     0, private_data, private_data_len);
 
3715	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3716		ret = iw_cm_reject(id_priv->cm_id.iw,
3717				   private_data, private_data_len);
3718	} else
3719		ret = -ENOSYS;
 
3720
3721	return ret;
3722}
3723EXPORT_SYMBOL(rdma_reject);
3724
3725int rdma_disconnect(struct rdma_cm_id *id)
3726{
3727	struct rdma_id_private *id_priv;
3728	int ret;
3729
3730	id_priv = container_of(id, struct rdma_id_private, id);
3731	if (!id_priv->cm_id.ib)
3732		return -EINVAL;
3733
3734	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3735		ret = cma_modify_qp_err(id_priv);
3736		if (ret)
3737			goto out;
3738		/* Initiate or respond to a disconnect. */
3739		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3740			ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
 
 
 
 
 
3741	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3742		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
3743	} else
3744		ret = -EINVAL;
3745
3746out:
3747	return ret;
3748}
3749EXPORT_SYMBOL(rdma_disconnect);
3750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3751static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3752{
3753	struct rdma_id_private *id_priv;
3754	struct cma_multicast *mc = multicast->context;
3755	struct rdma_cm_event event;
 
3756	int ret = 0;
3757
3758	id_priv = mc->id_priv;
3759	mutex_lock(&id_priv->handler_mutex);
3760	if (id_priv->state != RDMA_CM_ADDR_BOUND &&
3761	    id_priv->state != RDMA_CM_ADDR_RESOLVED)
3762		goto out;
3763
3764	if (!status)
3765		status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
3766	mutex_lock(&id_priv->qp_mutex);
3767	if (!status && id_priv->id.qp)
3768		status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
3769					 be16_to_cpu(multicast->rec.mlid));
3770	mutex_unlock(&id_priv->qp_mutex);
3771
3772	memset(&event, 0, sizeof event);
3773	event.status = status;
3774	event.param.ud.private_data = mc->context;
3775	if (!status) {
3776		struct rdma_dev_addr *dev_addr =
3777			&id_priv->id.route.addr.dev_addr;
3778		struct net_device *ndev =
3779			dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3780		enum ib_gid_type gid_type =
3781			id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3782			rdma_start_port(id_priv->cma_dev->device)];
3783
3784		event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3785		ib_init_ah_from_mcmember(id_priv->id.device,
3786					 id_priv->id.port_num, &multicast->rec,
3787					 ndev, gid_type,
3788					 &event.param.ud.ah_attr);
3789		event.param.ud.qp_num = 0xFFFFFF;
3790		event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3791		if (ndev)
3792			dev_put(ndev);
3793	} else
3794		event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3795
3796	ret = id_priv->id.event_handler(&id_priv->id, &event);
3797	if (ret) {
3798		cma_exch(id_priv, RDMA_CM_DESTROYING);
3799		mutex_unlock(&id_priv->handler_mutex);
3800		rdma_destroy_id(&id_priv->id);
3801		return 0;
3802	}
 
 
3803
3804out:
3805	mutex_unlock(&id_priv->handler_mutex);
3806	return 0;
3807}
3808
3809static void cma_set_mgid(struct rdma_id_private *id_priv,
3810			 struct sockaddr *addr, union ib_gid *mgid)
3811{
3812	unsigned char mc_map[MAX_ADDR_LEN];
3813	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3814	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3815	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3816
3817	if (cma_any_addr(addr)) {
3818		memset(mgid, 0, sizeof *mgid);
3819	} else if ((addr->sa_family == AF_INET6) &&
3820		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
3821								 0xFF10A01B)) {
3822		/* IPv6 address is an SA assigned MGID. */
3823		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3824	} else if (addr->sa_family == AF_IB) {
3825		memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
3826	} else if ((addr->sa_family == AF_INET6)) {
3827		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3828		if (id_priv->id.ps == RDMA_PS_UDP)
3829			mc_map[7] = 0x01;	/* Use RDMA CM signature */
3830		*mgid = *(union ib_gid *) (mc_map + 4);
3831	} else {
3832		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
3833		if (id_priv->id.ps == RDMA_PS_UDP)
3834			mc_map[7] = 0x01;	/* Use RDMA CM signature */
3835		*mgid = *(union ib_gid *) (mc_map + 4);
3836	}
3837}
3838
3839static void cma_query_sa_classport_info_cb(int status,
3840					   struct ib_class_port_info *rec,
3841					   void *context)
3842{
3843	struct class_port_info_context *cb_ctx = context;
3844
3845	WARN_ON(!context);
3846
3847	if (status || !rec) {
3848		pr_debug("RDMA CM: %s port %u failed query ClassPortInfo status: %d\n",
3849			 cb_ctx->device->name, cb_ctx->port_num, status);
3850		goto out;
3851	}
3852
3853	memcpy(cb_ctx->class_port_info, rec, sizeof(struct ib_class_port_info));
3854
3855out:
3856	complete(&cb_ctx->done);
3857}
3858
3859static int cma_query_sa_classport_info(struct ib_device *device, u8 port_num,
3860				       struct ib_class_port_info *class_port_info)
3861{
3862	struct class_port_info_context *cb_ctx;
3863	int ret;
3864
3865	cb_ctx = kmalloc(sizeof(*cb_ctx), GFP_KERNEL);
3866	if (!cb_ctx)
3867		return -ENOMEM;
3868
3869	cb_ctx->device = device;
3870	cb_ctx->class_port_info = class_port_info;
3871	cb_ctx->port_num = port_num;
3872	init_completion(&cb_ctx->done);
3873
3874	ret = ib_sa_classport_info_rec_query(&sa_client, device, port_num,
3875					     CMA_QUERY_CLASSPORT_INFO_TIMEOUT,
3876					     GFP_KERNEL, cma_query_sa_classport_info_cb,
3877					     cb_ctx, &cb_ctx->sa_query);
3878	if (ret < 0) {
3879		pr_err("RDMA CM: %s port %u failed to send ClassPortInfo query, ret: %d\n",
3880		       device->name, port_num, ret);
3881		goto out;
3882	}
3883
3884	wait_for_completion(&cb_ctx->done);
3885
3886out:
3887	kfree(cb_ctx);
3888	return ret;
3889}
3890
3891static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3892				 struct cma_multicast *mc)
3893{
3894	struct ib_sa_mcmember_rec rec;
3895	struct ib_class_port_info class_port_info;
3896	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3897	ib_sa_comp_mask comp_mask;
3898	int ret;
3899
3900	ib_addr_get_mgid(dev_addr, &rec.mgid);
3901	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3902				     &rec.mgid, &rec);
3903	if (ret)
3904		return ret;
3905
3906	ret = cma_set_qkey(id_priv, 0);
3907	if (ret)
3908		return ret;
 
 
3909
3910	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
3911	rec.qkey = cpu_to_be32(id_priv->qkey);
3912	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
3913	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3914	rec.join_state = mc->join_state;
3915
3916	if (rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) {
3917		ret = cma_query_sa_classport_info(id_priv->id.device,
3918						  id_priv->id.port_num,
3919						  &class_port_info);
3920
3921		if (ret)
3922			return ret;
3923
3924		if (!(ib_get_cpi_capmask2(&class_port_info) &
3925		      IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT)) {
3926			pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
3927				"RDMA CM: SM doesn't support Send Only Full Member option\n",
3928				id_priv->id.device->name, id_priv->id.port_num);
3929			return -EOPNOTSUPP;
3930		}
3931	}
3932
3933	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3934		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3935		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3936		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
3937		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3938
3939	if (id_priv->id.ps == RDMA_PS_IPOIB)
3940		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
3941			     IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3942			     IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3943			     IB_SA_MCMEMBER_REC_MTU |
3944			     IB_SA_MCMEMBER_REC_HOP_LIMIT;
3945
3946	mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3947						id_priv->id.port_num, &rec,
3948						comp_mask, GFP_KERNEL,
3949						cma_ib_mc_handler, mc);
3950	return PTR_ERR_OR_ZERO(mc->multicast.ib);
3951}
3952
3953static void iboe_mcast_work_handler(struct work_struct *work)
3954{
3955	struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3956	struct cma_multicast *mc = mw->mc;
3957	struct ib_sa_multicast *m = mc->multicast.ib;
3958
3959	mc->multicast.ib->context = mc;
3960	cma_ib_mc_handler(0, m);
3961	kref_put(&mc->mcref, release_mc);
3962	kfree(mw);
3963}
3964
3965static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
 
3966{
3967	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3968	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3969
3970	if (cma_any_addr(addr)) {
3971		memset(mgid, 0, sizeof *mgid);
3972	} else if (addr->sa_family == AF_INET6) {
3973		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3974	} else {
3975		mgid->raw[0] = 0xff;
3976		mgid->raw[1] = 0x0e;
 
 
3977		mgid->raw[2] = 0;
3978		mgid->raw[3] = 0;
3979		mgid->raw[4] = 0;
3980		mgid->raw[5] = 0;
3981		mgid->raw[6] = 0;
3982		mgid->raw[7] = 0;
3983		mgid->raw[8] = 0;
3984		mgid->raw[9] = 0;
3985		mgid->raw[10] = 0xff;
3986		mgid->raw[11] = 0xff;
3987		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3988	}
3989}
3990
3991static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3992				   struct cma_multicast *mc)
3993{
3994	struct iboe_mcast_work *work;
3995	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3996	int err = 0;
3997	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3998	struct net_device *ndev = NULL;
 
3999	enum ib_gid_type gid_type;
4000	bool send_only;
4001
4002	send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4003
4004	if (cma_zero_addr((struct sockaddr *)&mc->addr))
4005		return -EINVAL;
4006
4007	work = kzalloc(sizeof *work, GFP_KERNEL);
4008	if (!work)
4009		return -ENOMEM;
4010
4011	mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
4012	if (!mc->multicast.ib) {
4013		err = -ENOMEM;
4014		goto out1;
4015	}
4016
4017	cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
4018
4019	mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
4020	if (id_priv->id.ps == RDMA_PS_UDP)
4021		mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
4022
4023	if (dev_addr->bound_dev_if)
4024		ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
4025	if (!ndev) {
4026		err = -ENODEV;
4027		goto out2;
4028	}
4029	mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
4030	mc->multicast.ib->rec.hop_limit = 1;
4031	mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
4032
4033	gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4034		   rdma_start_port(id_priv->cma_dev->device)];
4035	if (addr->sa_family == AF_INET) {
4036		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4037			mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4038			if (!send_only) {
4039				err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid,
4040						    true);
4041				if (!err)
4042					mc->igmp_joined = true;
4043			}
4044		}
4045	} else {
4046		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4047			err = -ENOTSUPP;
4048	}
4049	dev_put(ndev);
4050	if (err || !mc->multicast.ib->rec.mtu) {
4051		if (!err)
4052			err = -EINVAL;
4053		goto out2;
4054	}
 
4055	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4056		    &mc->multicast.ib->rec.port_gid);
4057	work->id = id_priv;
4058	work->mc = mc;
4059	INIT_WORK(&work->work, iboe_mcast_work_handler);
4060	kref_get(&mc->mcref);
4061	queue_work(cma_wq, &work->work);
4062
4063	return 0;
4064
4065out2:
4066	kfree(mc->multicast.ib);
4067out1:
4068	kfree(work);
4069	return err;
4070}
4071
4072int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4073			u8 join_state, void *context)
4074{
4075	struct rdma_id_private *id_priv;
 
4076	struct cma_multicast *mc;
4077	int ret;
4078
4079	id_priv = container_of(id, struct rdma_id_private, id);
4080	if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
4081	    !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
 
 
 
 
 
 
 
4082		return -EINVAL;
4083
4084	mc = kmalloc(sizeof *mc, GFP_KERNEL);
4085	if (!mc)
4086		return -ENOMEM;
4087
4088	memcpy(&mc->addr, addr, rdma_addr_size(addr));
4089	mc->context = context;
4090	mc->id_priv = id_priv;
4091	mc->igmp_joined = false;
4092	mc->join_state = join_state;
4093	spin_lock(&id_priv->lock);
4094	list_add(&mc->list, &id_priv->mc_list);
4095	spin_unlock(&id_priv->lock);
4096
4097	if (rdma_protocol_roce(id->device, id->port_num)) {
4098		kref_init(&mc->mcref);
4099		ret = cma_iboe_join_multicast(id_priv, mc);
4100	} else if (rdma_cap_ib_mcast(id->device, id->port_num))
 
 
4101		ret = cma_join_ib_multicast(id_priv, mc);
4102	else
 
 
4103		ret = -ENOSYS;
 
 
 
 
 
 
4104
4105	if (ret) {
4106		spin_lock_irq(&id_priv->lock);
4107		list_del(&mc->list);
4108		spin_unlock_irq(&id_priv->lock);
4109		kfree(mc);
4110	}
4111	return ret;
4112}
4113EXPORT_SYMBOL(rdma_join_multicast);
4114
4115void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4116{
4117	struct rdma_id_private *id_priv;
4118	struct cma_multicast *mc;
4119
4120	id_priv = container_of(id, struct rdma_id_private, id);
4121	spin_lock_irq(&id_priv->lock);
4122	list_for_each_entry(mc, &id_priv->mc_list, list) {
4123		if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
4124			list_del(&mc->list);
4125			spin_unlock_irq(&id_priv->lock);
4126
4127			if (id->qp)
4128				ib_detach_mcast(id->qp,
4129						&mc->multicast.ib->rec.mgid,
4130						be16_to_cpu(mc->multicast.ib->rec.mlid));
4131
4132			BUG_ON(id_priv->cma_dev->device != id->device);
4133
4134			if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4135				ib_sa_free_multicast(mc->multicast.ib);
4136				kfree(mc);
4137			} else if (rdma_protocol_roce(id->device, id->port_num)) {
4138				if (mc->igmp_joined) {
4139					struct rdma_dev_addr *dev_addr =
4140						&id->route.addr.dev_addr;
4141					struct net_device *ndev = NULL;
4142
4143					if (dev_addr->bound_dev_if)
4144						ndev = dev_get_by_index(&init_net,
4145									dev_addr->bound_dev_if);
4146					if (ndev) {
4147						cma_igmp_send(ndev,
4148							      &mc->multicast.ib->rec.mgid,
4149							      false);
4150						dev_put(ndev);
4151					}
4152					mc->igmp_joined = false;
4153				}
4154				kref_put(&mc->mcref, release_mc);
4155			}
4156			return;
4157		}
4158	}
4159	spin_unlock_irq(&id_priv->lock);
4160}
4161EXPORT_SYMBOL(rdma_leave_multicast);
4162
4163static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4164{
4165	struct rdma_dev_addr *dev_addr;
4166	struct cma_ndev_work *work;
4167
4168	dev_addr = &id_priv->id.route.addr.dev_addr;
4169
4170	if ((dev_addr->bound_dev_if == ndev->ifindex) &&
4171	    (net_eq(dev_net(ndev), dev_addr->net)) &&
4172	    memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
4173		pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4174			ndev->name, &id_priv->id);
4175		work = kzalloc(sizeof *work, GFP_KERNEL);
4176		if (!work)
4177			return -ENOMEM;
4178
4179		INIT_WORK(&work->work, cma_ndev_work_handler);
4180		work->id = id_priv;
4181		work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4182		atomic_inc(&id_priv->refcount);
4183		queue_work(cma_wq, &work->work);
4184	}
4185
4186	return 0;
4187}
4188
4189static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
4190			       void *ptr)
4191{
4192	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4193	struct cma_device *cma_dev;
4194	struct rdma_id_private *id_priv;
4195	int ret = NOTIFY_DONE;
4196
4197	if (event != NETDEV_BONDING_FAILOVER)
4198		return NOTIFY_DONE;
4199
4200	if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
4201		return NOTIFY_DONE;
4202
4203	mutex_lock(&lock);
4204	list_for_each_entry(cma_dev, &dev_list, list)
4205		list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4206			ret = cma_netdev_change(ndev, id_priv);
4207			if (ret)
4208				goto out;
4209		}
4210
4211out:
4212	mutex_unlock(&lock);
4213	return ret;
4214}
4215
4216static struct notifier_block cma_nb = {
4217	.notifier_call = cma_netdev_callback
4218};
 
 
 
 
4219
4220static void cma_add_one(struct ib_device *device)
4221{
4222	struct cma_device *cma_dev;
4223	struct rdma_id_private *id_priv;
4224	unsigned int i;
4225	unsigned long supported_gids = 0;
4226
4227	cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4228	if (!cma_dev)
4229		return;
4230
4231	cma_dev->device = device;
4232	cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4233					    sizeof(*cma_dev->default_gid_type),
4234					    GFP_KERNEL);
4235	if (!cma_dev->default_gid_type) {
4236		kfree(cma_dev);
4237		return;
4238	}
4239	for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4240		supported_gids = roce_gid_type_mask_support(device, i);
4241		WARN_ON(!supported_gids);
4242		cma_dev->default_gid_type[i - rdma_start_port(device)] =
4243			find_first_bit(&supported_gids, BITS_PER_LONG);
4244	}
4245
4246	init_completion(&cma_dev->comp);
4247	atomic_set(&cma_dev->refcount, 1);
4248	INIT_LIST_HEAD(&cma_dev->id_list);
4249	ib_set_client_data(device, &cma_client, cma_dev);
4250
4251	mutex_lock(&lock);
4252	list_add_tail(&cma_dev->list, &dev_list);
4253	list_for_each_entry(id_priv, &listen_any_list, list)
4254		cma_listen_on_dev(id_priv, cma_dev);
4255	mutex_unlock(&lock);
4256}
4257
4258static int cma_remove_id_dev(struct rdma_id_private *id_priv)
 
4259{
4260	struct rdma_cm_event event;
4261	enum rdma_cm_state state;
4262	int ret = 0;
 
4263
4264	/* Record that we want to remove the device */
4265	state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
4266	if (state == RDMA_CM_DESTROYING)
4267		return 0;
4268
4269	cma_cancel_operation(id_priv, state);
4270	mutex_lock(&id_priv->handler_mutex);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4271
4272	/* Check for destruction from another callback. */
4273	if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
4274		goto out;
4275
4276	memset(&event, 0, sizeof event);
4277	event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
4278	ret = id_priv->id.event_handler(&id_priv->id, &event);
 
 
 
 
 
4279out:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4280	mutex_unlock(&id_priv->handler_mutex);
4281	return ret;
 
 
 
 
 
 
4282}
4283
4284static void cma_process_remove(struct cma_device *cma_dev)
4285{
4286	struct rdma_id_private *id_priv;
4287	int ret;
4288
4289	mutex_lock(&lock);
4290	while (!list_empty(&cma_dev->id_list)) {
4291		id_priv = list_entry(cma_dev->id_list.next,
4292				     struct rdma_id_private, list);
4293
4294		list_del(&id_priv->listen_list);
4295		list_del_init(&id_priv->list);
4296		atomic_inc(&id_priv->refcount);
4297		mutex_unlock(&lock);
4298
4299		ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
4300		cma_deref_id(id_priv);
4301		if (ret)
4302			rdma_destroy_id(&id_priv->id);
4303
4304		mutex_lock(&lock);
4305	}
4306	mutex_unlock(&lock);
4307
4308	cma_deref_dev(cma_dev);
4309	wait_for_completion(&cma_dev->comp);
4310}
4311
4312static void cma_remove_one(struct ib_device *device, void *client_data)
 
 
 
 
 
 
 
 
 
 
 
4313{
4314	struct cma_device *cma_dev = client_data;
 
 
 
 
 
 
 
 
4315
 
4316	if (!cma_dev)
4317		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4318
4319	mutex_lock(&lock);
 
 
 
 
 
 
 
 
 
 
 
 
4320	list_del(&cma_dev->list);
4321	mutex_unlock(&lock);
4322
 
4323	cma_process_remove(cma_dev);
 
 
4324	kfree(cma_dev->default_gid_type);
 
 
4325	kfree(cma_dev);
 
4326}
4327
4328static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
4329{
4330	struct nlmsghdr *nlh;
4331	struct rdma_cm_id_stats *id_stats;
4332	struct rdma_id_private *id_priv;
4333	struct rdma_cm_id *id = NULL;
4334	struct cma_device *cma_dev;
4335	int i_dev = 0, i_id = 0;
4336
4337	/*
4338	 * We export all of the IDs as a sequence of messages.  Each
4339	 * ID gets its own netlink message.
4340	 */
4341	mutex_lock(&lock);
4342
4343	list_for_each_entry(cma_dev, &dev_list, list) {
4344		if (i_dev < cb->args[0]) {
4345			i_dev++;
4346			continue;
4347		}
4348
4349		i_id = 0;
4350		list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4351			if (i_id < cb->args[1]) {
4352				i_id++;
4353				continue;
4354			}
4355
4356			id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
4357						sizeof *id_stats, RDMA_NL_RDMA_CM,
4358						RDMA_NL_RDMA_CM_ID_STATS,
4359						NLM_F_MULTI);
4360			if (!id_stats)
4361				goto out;
4362
4363			memset(id_stats, 0, sizeof *id_stats);
4364			id = &id_priv->id;
4365			id_stats->node_type = id->route.addr.dev_addr.dev_type;
4366			id_stats->port_num = id->port_num;
4367			id_stats->bound_dev_if =
4368				id->route.addr.dev_addr.bound_dev_if;
4369
4370			if (ibnl_put_attr(skb, nlh,
4371					  rdma_addr_size(cma_src_addr(id_priv)),
4372					  cma_src_addr(id_priv),
4373					  RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
4374				goto out;
4375			if (ibnl_put_attr(skb, nlh,
4376					  rdma_addr_size(cma_src_addr(id_priv)),
4377					  cma_dst_addr(id_priv),
4378					  RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
4379				goto out;
4380
4381			id_stats->pid		= id_priv->owner;
4382			id_stats->port_space	= id->ps;
4383			id_stats->cm_state	= id_priv->state;
4384			id_stats->qp_num	= id_priv->qp_num;
4385			id_stats->qp_type	= id->qp_type;
4386
4387			i_id++;
4388		}
4389
4390		cb->args[1] = 0;
4391		i_dev++;
4392	}
4393
4394out:
4395	mutex_unlock(&lock);
4396	cb->args[0] = i_dev;
4397	cb->args[1] = i_id;
4398
4399	return skb->len;
 
 
 
4400}
4401
4402static const struct ibnl_client_cbs cma_cb_table[] = {
4403	[RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
4404				       .module = THIS_MODULE },
4405};
4406
4407static int cma_init_net(struct net *net)
4408{
4409	struct cma_pernet *pernet = cma_pernet(net);
4410
4411	idr_init(&pernet->tcp_ps);
4412	idr_init(&pernet->udp_ps);
4413	idr_init(&pernet->ipoib_ps);
4414	idr_init(&pernet->ib_ps);
4415
4416	return 0;
4417}
4418
4419static void cma_exit_net(struct net *net)
4420{
4421	struct cma_pernet *pernet = cma_pernet(net);
4422
4423	idr_destroy(&pernet->tcp_ps);
4424	idr_destroy(&pernet->udp_ps);
4425	idr_destroy(&pernet->ipoib_ps);
4426	idr_destroy(&pernet->ib_ps);
4427}
4428
4429static struct pernet_operations cma_pernet_operations = {
4430	.init = cma_init_net,
4431	.exit = cma_exit_net,
4432	.id = &cma_pernet_id,
4433	.size = sizeof(struct cma_pernet),
4434};
4435
4436static int __init cma_init(void)
4437{
4438	int ret;
4439
 
 
 
 
 
 
 
 
 
 
 
 
 
4440	cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
4441	if (!cma_wq)
4442		return -ENOMEM;
4443
4444	ret = register_pernet_subsys(&cma_pernet_operations);
4445	if (ret)
4446		goto err_wq;
4447
4448	ib_sa_register_client(&sa_client);
4449	rdma_addr_register_client(&addr_client);
4450	register_netdevice_notifier(&cma_nb);
 
4451
4452	ret = ib_register_client(&cma_client);
4453	if (ret)
4454		goto err;
4455
4456	if (ibnl_add_client(RDMA_NL_RDMA_CM, ARRAY_SIZE(cma_cb_table),
4457			    cma_cb_table))
4458		pr_warn("RDMA CMA: failed to add netlink callback\n");
4459	cma_configfs_init();
4460
4461	return 0;
4462
 
 
4463err:
 
4464	unregister_netdevice_notifier(&cma_nb);
4465	rdma_addr_unregister_client(&addr_client);
4466	ib_sa_unregister_client(&sa_client);
 
4467err_wq:
4468	destroy_workqueue(cma_wq);
4469	return ret;
4470}
4471
4472static void __exit cma_cleanup(void)
4473{
4474	cma_configfs_exit();
4475	ibnl_remove_client(RDMA_NL_RDMA_CM);
4476	ib_unregister_client(&cma_client);
 
4477	unregister_netdevice_notifier(&cma_nb);
4478	rdma_addr_unregister_client(&addr_client);
4479	ib_sa_unregister_client(&sa_client);
4480	unregister_pernet_subsys(&cma_pernet_operations);
4481	destroy_workqueue(cma_wq);
4482}
4483
4484module_init(cma_init);
4485module_exit(cma_cleanup);