cm.c 130.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2
/*
3
 * Copyright (c) 2004-2007 Intel Corporation.  All rights reserved.
4 5 6
 * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
 * Copyright (c) 2004, 2005 Voltaire Corporation.  All rights reserved.
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
7
 * Copyright (c) 2019, Mellanox Technologies inc.  All rights reserved.
8
 */
S
Sean Hefty 已提交
9 10

#include <linux/completion.h>
11
#include <linux/dma-mapping.h>
12
#include <linux/device.h>
13
#include <linux/module.h>
14 15 16
#include <linux/err.h>
#include <linux/idr.h>
#include <linux/interrupt.h>
S
Sean Hefty 已提交
17
#include <linux/random.h>
18 19
#include <linux/rbtree.h>
#include <linux/spinlock.h>
20
#include <linux/slab.h>
21
#include <linux/sysfs.h>
22
#include <linux/workqueue.h>
23
#include <linux/kdev_t.h>
24
#include <linux/etherdevice.h>
25

26 27
#include <rdma/ib_cache.h>
#include <rdma/ib_cm.h>
28
#include "cm_msgs.h"
29
#include "core_priv.h"
30 31 32 33 34

MODULE_AUTHOR("Sean Hefty");
MODULE_DESCRIPTION("InfiniBand CM");
MODULE_LICENSE("Dual BSD/GPL");

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static const char * const ibcm_rej_reason_strs[] = {
	[IB_CM_REJ_NO_QP]			= "no QP",
	[IB_CM_REJ_NO_EEC]			= "no EEC",
	[IB_CM_REJ_NO_RESOURCES]		= "no resources",
	[IB_CM_REJ_TIMEOUT]			= "timeout",
	[IB_CM_REJ_UNSUPPORTED]			= "unsupported",
	[IB_CM_REJ_INVALID_COMM_ID]		= "invalid comm ID",
	[IB_CM_REJ_INVALID_COMM_INSTANCE]	= "invalid comm instance",
	[IB_CM_REJ_INVALID_SERVICE_ID]		= "invalid service ID",
	[IB_CM_REJ_INVALID_TRANSPORT_TYPE]	= "invalid transport type",
	[IB_CM_REJ_STALE_CONN]			= "stale conn",
	[IB_CM_REJ_RDC_NOT_EXIST]		= "RDC not exist",
	[IB_CM_REJ_INVALID_GID]			= "invalid GID",
	[IB_CM_REJ_INVALID_LID]			= "invalid LID",
	[IB_CM_REJ_INVALID_SL]			= "invalid SL",
	[IB_CM_REJ_INVALID_TRAFFIC_CLASS]	= "invalid traffic class",
	[IB_CM_REJ_INVALID_HOP_LIMIT]		= "invalid hop limit",
	[IB_CM_REJ_INVALID_PACKET_RATE]		= "invalid packet rate",
	[IB_CM_REJ_INVALID_ALT_GID]		= "invalid alt GID",
	[IB_CM_REJ_INVALID_ALT_LID]		= "invalid alt LID",
	[IB_CM_REJ_INVALID_ALT_SL]		= "invalid alt SL",
	[IB_CM_REJ_INVALID_ALT_TRAFFIC_CLASS]	= "invalid alt traffic class",
	[IB_CM_REJ_INVALID_ALT_HOP_LIMIT]	= "invalid alt hop limit",
	[IB_CM_REJ_INVALID_ALT_PACKET_RATE]	= "invalid alt packet rate",
	[IB_CM_REJ_PORT_CM_REDIRECT]		= "port CM redirect",
	[IB_CM_REJ_PORT_REDIRECT]		= "port redirect",
	[IB_CM_REJ_INVALID_MTU]			= "invalid MTU",
	[IB_CM_REJ_INSUFFICIENT_RESP_RESOURCES]	= "insufficient resp resources",
	[IB_CM_REJ_CONSUMER_DEFINED]		= "consumer defined",
	[IB_CM_REJ_INVALID_RNR_RETRY]		= "invalid RNR retry",
	[IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID]	= "duplicate local comm ID",
	[IB_CM_REJ_INVALID_CLASS_VERSION]	= "invalid class version",
	[IB_CM_REJ_INVALID_FLOW_LABEL]		= "invalid flow label",
	[IB_CM_REJ_INVALID_ALT_FLOW_LABEL]	= "invalid alt flow label",
};

const char *__attribute_const__ ibcm_reject_msg(int reason)
{
	size_t index = reason;

	if (index < ARRAY_SIZE(ibcm_rej_reason_strs) &&
	    ibcm_rej_reason_strs[index])
		return ibcm_rej_reason_strs[index];
	else
		return "unrecognized reason";
}
EXPORT_SYMBOL(ibcm_reject_msg);

83
static void cm_add_one(struct ib_device *device);
84
static void cm_remove_one(struct ib_device *device, void *client_data);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

static struct ib_client cm_client = {
	.name   = "cm",
	.add    = cm_add_one,
	.remove = cm_remove_one
};

static struct ib_cm {
	spinlock_t lock;
	struct list_head device_list;
	rwlock_t device_lock;
	struct rb_root listen_service_table;
	u64 listen_service_id;
	/* struct rb_root peer_service_table; todo: fix peer to peer */
	struct rb_root remote_qp_table;
	struct rb_root remote_id_table;
	struct rb_root remote_sidr_table;
102 103
	struct xarray local_id_table;
	u32 local_id_next;
S
Sean Hefty 已提交
104
	__be32 random_id_operand;
105
	struct list_head timewait_list;
106
	struct workqueue_struct *wq;
107 108
	/* Sync on cm change port state */
	spinlock_t state_lock;
109 110
} cm;

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/* Counter indexes ordered by attribute ID */
enum {
	CM_REQ_COUNTER,
	CM_MRA_COUNTER,
	CM_REJ_COUNTER,
	CM_REP_COUNTER,
	CM_RTU_COUNTER,
	CM_DREQ_COUNTER,
	CM_DREP_COUNTER,
	CM_SIDR_REQ_COUNTER,
	CM_SIDR_REP_COUNTER,
	CM_LAP_COUNTER,
	CM_APR_COUNTER,
	CM_ATTR_COUNT,
	CM_ATTR_ID_OFFSET = 0x0010,
};

enum {
	CM_XMIT,
	CM_XMIT_RETRIES,
	CM_RECV,
	CM_RECV_DUPLICATES,
	CM_COUNTER_GROUPS
};

static char const counter_group_names[CM_COUNTER_GROUPS]
				     [sizeof("cm_rx_duplicates")] = {
	"cm_tx_msgs", "cm_tx_retries",
	"cm_rx_msgs", "cm_rx_duplicates"
};

struct cm_counter_group {
	struct kobject obj;
	atomic_long_t counter[CM_ATTR_COUNT];
};

struct cm_counter_attribute {
	struct attribute attr;
	int index;
};

#define CM_COUNTER_ATTR(_name, _index) \
struct cm_counter_attribute cm_##_name##_counter_attr = { \
154
	.attr = { .name = __stringify(_name), .mode = 0444 }, \
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	.index = _index \
}

static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);

static struct attribute *cm_counter_default_attrs[] = {
	&cm_req_counter_attr.attr,
	&cm_mra_counter_attr.attr,
	&cm_rej_counter_attr.attr,
	&cm_rep_counter_attr.attr,
	&cm_rtu_counter_attr.attr,
	&cm_dreq_counter_attr.attr,
	&cm_drep_counter_attr.attr,
	&cm_sidr_req_counter_attr.attr,
	&cm_sidr_rep_counter_attr.attr,
	&cm_lap_counter_attr.attr,
	&cm_apr_counter_attr.attr,
	NULL
};

185 186 187
struct cm_port {
	struct cm_device *cm_dev;
	struct ib_mad_agent *mad_agent;
188
	struct kobject port_obj;
189
	u8 port_num;
190 191
	struct list_head cm_priv_prim_list;
	struct list_head cm_priv_altr_list;
192
	struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
193 194 195 196
};

struct cm_device {
	struct list_head list;
197
	struct ib_device *ib_device;
198
	u8 ack_delay;
199
	int going_down;
200
	struct cm_port *port[0];
201 202 203 204 205
};

struct cm_av {
	struct cm_port *port;
	union ib_gid dgid;
206
	struct rdma_ah_attr ah_attr;
207
	u16 pkey_index;
208
	u8 timeout;
209 210 211
};

struct cm_work {
D
David Howells 已提交
212
	struct delayed_work work;
213 214 215
	struct list_head list;
	struct cm_port *port;
	struct ib_mad_recv_wc *mad_recv_wc;	/* Received MADs */
216 217
	__be32 local_id;			/* Established / timewait */
	__be32 remote_id;
218
	struct ib_cm_event cm_event;
219
	struct sa_path_rec path[0];
220 221 222
};

struct cm_timewait_info {
223
	struct cm_work work;
224
	struct list_head list;
225 226
	struct rb_node remote_qp_node;
	struct rb_node remote_id_node;
227 228
	__be64 remote_ca_guid;
	__be32 remote_qpn;
229 230 231 232 233 234 235 236 237
	u8 inserted_remote_qp;
	u8 inserted_remote_id;
};

struct cm_id_private {
	struct ib_cm_id	id;

	struct rb_node service_node;
	struct rb_node sidr_id_node;
238
	spinlock_t lock;	/* Do not acquire inside cm.lock */
S
Sean Hefty 已提交
239
	struct completion comp;
240
	refcount_t refcount;
H
Haggai Eran 已提交
241 242 243
	/* Number of clients sharing this ib_cm_id. Only valid for listeners.
	 * Protected by the cm.lock spinlock. */
	int listen_sharecount;
244
	struct rcu_head rcu;
245 246 247 248 249 250 251 252

	struct ib_mad_send_buf *msg;
	struct cm_timewait_info *timewait_info;
	/* todo: use alternate port on send failure */
	struct cm_av av;
	struct cm_av alt_av;

	void *private_data;
253 254 255
	__be64 tid;
	__be32 local_qpn;
	__be32 remote_qpn;
256
	enum ib_qp_type qp_type;
257 258
	__be32 sq_psn;
	__be32 rq_psn;
259 260
	int timeout_ms;
	enum ib_mtu path_mtu;
261
	__be16 pkey;
262 263 264 265 266 267 268 269
	u8 private_data_len;
	u8 max_cm_retries;
	u8 peer_to_peer;
	u8 responder_resources;
	u8 initiator_depth;
	u8 retry_count;
	u8 rnr_retry_count;
	u8 service_timeout;
270
	u8 target_ack_delay;
271

272 273 274 275 276 277
	struct list_head prim_list;
	struct list_head altr_list;
	/* Indicates that the send port mad is registered and av is set */
	int prim_send_port_not_ready;
	int altr_send_port_not_ready;

278 279 280 281
	struct list_head work_list;
	atomic_t work_count;
};

D
David Howells 已提交
282
static void cm_work_handler(struct work_struct *work);
283 284 285

static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
{
286
	if (refcount_dec_and_test(&cm_id_priv->refcount))
S
Sean Hefty 已提交
287
		complete(&cm_id_priv->comp);
288 289 290 291 292 293 294 295
}

static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
			struct ib_mad_send_buf **msg)
{
	struct ib_mad_agent *mad_agent;
	struct ib_mad_send_buf *m;
	struct ib_ah *ah;
296 297 298
	struct cm_av *av;
	unsigned long flags, flags2;
	int ret = 0;
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	/* don't let the port to be released till the agent is down */
	spin_lock_irqsave(&cm.state_lock, flags2);
	spin_lock_irqsave(&cm.lock, flags);
	if (!cm_id_priv->prim_send_port_not_ready)
		av = &cm_id_priv->av;
	else if (!cm_id_priv->altr_send_port_not_ready &&
		 (cm_id_priv->alt_av.port))
		av = &cm_id_priv->alt_av;
	else {
		pr_info("%s: not valid CM id\n", __func__);
		ret = -ENODEV;
		spin_unlock_irqrestore(&cm.lock, flags);
		goto out;
	}
	spin_unlock_irqrestore(&cm.lock, flags);
	/* Make sure the port haven't released the mad yet */
316
	mad_agent = cm_id_priv->av.port->mad_agent;
317 318 319 320 321
	if (!mad_agent) {
		pr_info("%s: not a valid MAD agent\n", __func__);
		ret = -ENODEV;
		goto out;
	}
322
	ah = rdma_create_ah(mad_agent->qp->pd, &av->ah_attr, 0);
323 324 325 326
	if (IS_ERR(ah)) {
		ret = PTR_ERR(ah);
		goto out;
	}
327

R
Roland Dreier 已提交
328
	m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
329
			       av->pkey_index,
330
			       0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
331 332
			       GFP_ATOMIC,
			       IB_MGMT_BASE_VERSION);
333
	if (IS_ERR(m)) {
334
		rdma_destroy_ah(ah, 0);
335 336
		ret = PTR_ERR(m);
		goto out;
337 338 339
	}

	/* Timeout set by caller if response is expected. */
340 341
	m->ah = ah;
	m->retries = cm_id_priv->max_cm_retries;
342

343
	refcount_inc(&cm_id_priv->refcount);
344 345
	m->context[0] = cm_id_priv;
	*msg = m;
346 347 348 349

out:
	spin_unlock_irqrestore(&cm.state_lock, flags2);
	return ret;
350 351
}

352 353 354 355 356 357 358 359 360 361 362 363
static struct ib_mad_send_buf *cm_alloc_response_msg_no_ah(struct cm_port *port,
							   struct ib_mad_recv_wc *mad_recv_wc)
{
	return ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
				  0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
				  GFP_ATOMIC,
				  IB_MGMT_BASE_VERSION);
}

static int cm_create_response_msg_ah(struct cm_port *port,
				     struct ib_mad_recv_wc *mad_recv_wc,
				     struct ib_mad_send_buf *msg)
364 365 366 367 368 369 370 371
{
	struct ib_ah *ah;

	ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
				  mad_recv_wc->recv_buf.grh, port->port_num);
	if (IS_ERR(ah))
		return PTR_ERR(ah);

372
	msg->ah = ah;
373 374 375 376 377
	return 0;
}

static void cm_free_msg(struct ib_mad_send_buf *msg)
{
378
	if (msg->ah)
379
		rdma_destroy_ah(msg->ah, 0);
380 381 382 383 384
	if (msg->context[0])
		cm_deref_id(msg->context[0]);
	ib_free_send_mad(msg);
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
static int cm_alloc_response_msg(struct cm_port *port,
				 struct ib_mad_recv_wc *mad_recv_wc,
				 struct ib_mad_send_buf **msg)
{
	struct ib_mad_send_buf *m;
	int ret;

	m = cm_alloc_response_msg_no_ah(port, mad_recv_wc);
	if (IS_ERR(m))
		return PTR_ERR(m);

	ret = cm_create_response_msg_ah(port, mad_recv_wc, m);
	if (ret) {
		cm_free_msg(m);
		return ret;
	}

	*msg = m;
	return 0;
}

406 407 408 409 410 411 412 413
static void * cm_copy_private_data(const void *private_data,
				   u8 private_data_len)
{
	void *data;

	if (!private_data || !private_data_len)
		return NULL;

E
Eric Sesterhenn 已提交
414
	data = kmemdup(private_data, private_data_len, GFP_KERNEL);
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	if (!data)
		return ERR_PTR(-ENOMEM);

	return data;
}

static void cm_set_private_data(struct cm_id_private *cm_id_priv,
				 void *private_data, u8 private_data_len)
{
	if (cm_id_priv->private_data && cm_id_priv->private_data_len)
		kfree(cm_id_priv->private_data);

	cm_id_priv->private_data = private_data;
	cm_id_priv->private_data_len = private_data_len;
}

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
static int cm_init_av_for_lap(struct cm_port *port, struct ib_wc *wc,
			      struct ib_grh *grh, struct cm_av *av)
{
	struct rdma_ah_attr new_ah_attr;
	int ret;

	av->port = port;
	av->pkey_index = wc->pkey_index;

	/*
	 * av->ah_attr might be initialized based on past wc during incoming
	 * connect request or while sending out connect request. So initialize
	 * a new ah_attr on stack. If initialization fails, old ah_attr is
	 * used for sending any responses. If initialization is successful,
	 * than new ah_attr is used by overwriting old one.
	 */
	ret = ib_init_ah_attr_from_wc(port->cm_dev->ib_device,
				      port->port_num, wc,
				      grh, &new_ah_attr);
	if (ret)
		return ret;

453
	rdma_move_ah_attr(&av->ah_attr, &new_ah_attr);
454 455 456
	return 0;
}

P
Parav Pandit 已提交
457 458
static int cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
				   struct ib_grh *grh, struct cm_av *av)
459 460 461
{
	av->port = port;
	av->pkey_index = wc->pkey_index;
462 463 464
	return ib_init_ah_attr_from_wc(port->cm_dev->ib_device,
				       port->port_num, wc,
				       grh, &av->ah_attr);
465 466
}

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
static int add_cm_id_to_port_list(struct cm_id_private *cm_id_priv,
				  struct cm_av *av,
				  struct cm_port *port)
{
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&cm.lock, flags);

	if (&cm_id_priv->av == av)
		list_add_tail(&cm_id_priv->prim_list, &port->cm_priv_prim_list);
	else if (&cm_id_priv->alt_av == av)
		list_add_tail(&cm_id_priv->altr_list, &port->cm_priv_altr_list);
	else
		ret = -EINVAL;

	spin_unlock_irqrestore(&cm.lock, flags);
	return ret;
}

487 488
static struct cm_port *
get_cm_port_from_path(struct sa_path_rec *path, const struct ib_gid_attr *attr)
489 490 491 492
{
	struct cm_device *cm_dev;
	struct cm_port *port = NULL;
	unsigned long flags;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

	if (attr) {
		read_lock_irqsave(&cm.device_lock, flags);
		list_for_each_entry(cm_dev, &cm.device_list, list) {
			if (cm_dev->ib_device == attr->device) {
				port = cm_dev->port[attr->port_num - 1];
				break;
			}
		}
		read_unlock_irqrestore(&cm.device_lock, flags);
	} else {
		/* SGID attribute can be NULL in following
		 * conditions.
		 * (a) Alternative path
		 * (b) IB link layer without GRH
		 * (c) LAP send messages
		 */
		read_lock_irqsave(&cm.device_lock, flags);
		list_for_each_entry(cm_dev, &cm.device_list, list) {
			attr = rdma_find_gid(cm_dev->ib_device,
					     &path->sgid,
					     sa_conv_pathrec_to_gid_type(path),
					     NULL);
			if (!IS_ERR(attr)) {
				port = cm_dev->port[attr->port_num - 1];
				break;
			}
520
		}
521 522 523
		read_unlock_irqrestore(&cm.device_lock, flags);
		if (port)
			rdma_put_gid_attr(attr);
524
	}
525 526 527
	return port;
}

528 529 530
static int cm_init_av_by_path(struct sa_path_rec *path,
			      const struct ib_gid_attr *sgid_attr,
			      struct cm_av *av,
531 532
			      struct cm_id_private *cm_id_priv)
{
533
	struct rdma_ah_attr new_ah_attr;
534 535 536
	struct cm_device *cm_dev;
	struct cm_port *port;
	int ret;
537

538
	port = get_cm_port_from_path(path, sgid_attr);
539 540
	if (!port)
		return -EINVAL;
541
	cm_dev = port->cm_dev;
542

543
	ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
544 545 546 547 548
				  be16_to_cpu(path->pkey), &av->pkey_index);
	if (ret)
		return ret;

	av->port = port;
549 550 551

	/*
	 * av->ah_attr might be initialized based on wc or during
552 553
	 * request processing time which might have reference to sgid_attr.
	 * So initialize a new ah_attr on stack.
554 555
	 * If initialization fails, old ah_attr is used for sending any
	 * responses. If initialization is successful, than new ah_attr
556 557
	 * is used by overwriting the old one. So that right ah_attr
	 * can be used to return an error response.
558
	 */
559
	ret = ib_init_ah_attr_from_path(cm_dev->ib_device, port->port_num, path,
560
					&new_ah_attr, sgid_attr);
561 562 563
	if (ret)
		return ret;

564
	av->timeout = path->packet_life_time + 1;
565

566
	ret = add_cm_id_to_port_list(cm_id_priv, av, port);
567 568
	if (ret) {
		rdma_destroy_ah_attr(&new_ah_attr);
569
		return ret;
570
	}
571
	rdma_move_ah_attr(&av->ah_attr, &new_ah_attr);
572
	return 0;
573 574 575 576
}

static int cm_alloc_id(struct cm_id_private *cm_id_priv)
{
577 578
	int err;
	u32 id;
T
Tejun Heo 已提交
579

580 581
	err = xa_alloc_cyclic_irq(&cm.local_id_table, &id, cm_id_priv,
			xa_limit_32b, &cm.local_id_next, GFP_KERNEL);
S
Sean Hefty 已提交
582

583
	cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
584 585 586 587 588 589
	return err;
}

static u32 cm_local_id(__be32 local_id)
{
	return (__force u32) (local_id ^ cm.random_id_operand);
590 591
}

592
static void cm_free_id(__be32 local_id)
593
{
594
	xa_erase_irq(&cm.local_id_table, cm_local_id(local_id));
595 596
}

597
static struct cm_id_private *cm_acquire_id(__be32 local_id, __be32 remote_id)
598 599 600
{
	struct cm_id_private *cm_id_priv;

601
	rcu_read_lock();
602
	cm_id_priv = xa_load(&cm.local_id_table, cm_local_id(local_id));
603 604 605 606
	if (!cm_id_priv || cm_id_priv->id.remote_id != remote_id ||
	    !refcount_inc_not_zero(&cm_id_priv->refcount))
		cm_id_priv = NULL;
	rcu_read_unlock();
607 608 609 610

	return cm_id_priv;
}

R
Roland Dreier 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
/*
 * Trivial helpers to strip endian annotation and compare; the
 * endianness doesn't actually matter since we just need a stable
 * order for the RB tree.
 */
static int be32_lt(__be32 a, __be32 b)
{
	return (__force u32) a < (__force u32) b;
}

static int be32_gt(__be32 a, __be32 b)
{
	return (__force u32) a > (__force u32) b;
}

static int be64_lt(__be64 a, __be64 b)
{
	return (__force u64) a < (__force u64) b;
}

static int be64_gt(__be64 a, __be64 b)
{
	return (__force u64) a > (__force u64) b;
}

636 637 638 639 640
static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
{
	struct rb_node **link = &cm.listen_service_table.rb_node;
	struct rb_node *parent = NULL;
	struct cm_id_private *cur_cm_id_priv;
641 642
	__be64 service_id = cm_id_priv->id.service_id;
	__be64 service_mask = cm_id_priv->id.service_mask;
643 644 645 646 647 648

	while (*link) {
		parent = *link;
		cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
					  service_node);
		if ((cur_cm_id_priv->id.service_mask & service_id) ==
649
		    (service_mask & cur_cm_id_priv->id.service_id) &&
H
Haggai Eran 已提交
650
		    (cm_id_priv->id.device == cur_cm_id_priv->id.device))
651 652 653 654 655 656
			return cur_cm_id_priv;

		if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
			link = &(*link)->rb_left;
		else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
			link = &(*link)->rb_right;
R
Roland Dreier 已提交
657
		else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
658
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
659
		else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
660
			link = &(*link)->rb_right;
661 662 663 664 665 666 667 668
		else
			link = &(*link)->rb_right;
	}
	rb_link_node(&cm_id_priv->service_node, parent, link);
	rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
	return NULL;
}

669
static struct cm_id_private * cm_find_listen(struct ib_device *device,
H
Haggai Eran 已提交
670
					     __be64 service_id)
671 672 673 674 675 676 677
{
	struct rb_node *node = cm.listen_service_table.rb_node;
	struct cm_id_private *cm_id_priv;

	while (node) {
		cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
		if ((cm_id_priv->id.service_mask & service_id) ==
678
		     cm_id_priv->id.service_id &&
H
Haggai Eran 已提交
679
		    (cm_id_priv->id.device == device))
680
			return cm_id_priv;
681 682 683 684 685

		if (device < cm_id_priv->id.device)
			node = node->rb_left;
		else if (device > cm_id_priv->id.device)
			node = node->rb_right;
R
Roland Dreier 已提交
686
		else if (be64_lt(service_id, cm_id_priv->id.service_id))
687
			node = node->rb_left;
R
Roland Dreier 已提交
688
		else if (be64_gt(service_id, cm_id_priv->id.service_id))
689
			node = node->rb_right;
690 691 692 693 694 695 696 697 698 699 700 701
		else
			node = node->rb_right;
	}
	return NULL;
}

static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
						     *timewait_info)
{
	struct rb_node **link = &cm.remote_id_table.rb_node;
	struct rb_node *parent = NULL;
	struct cm_timewait_info *cur_timewait_info;
702 703
	__be64 remote_ca_guid = timewait_info->remote_ca_guid;
	__be32 remote_id = timewait_info->work.remote_id;
704 705 706 707 708

	while (*link) {
		parent = *link;
		cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
					     remote_id_node);
R
Roland Dreier 已提交
709
		if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
710
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
711
		else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
712
			link = &(*link)->rb_right;
R
Roland Dreier 已提交
713
		else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
714
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
715
		else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
716 717 718 719 720 721 722 723 724 725
			link = &(*link)->rb_right;
		else
			return cur_timewait_info;
	}
	timewait_info->inserted_remote_id = 1;
	rb_link_node(&timewait_info->remote_id_node, parent, link);
	rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
	return NULL;
}

726 727
static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
						   __be32 remote_id)
728 729 730 731 732 733 734
{
	struct rb_node *node = cm.remote_id_table.rb_node;
	struct cm_timewait_info *timewait_info;

	while (node) {
		timewait_info = rb_entry(node, struct cm_timewait_info,
					 remote_id_node);
R
Roland Dreier 已提交
735
		if (be32_lt(remote_id, timewait_info->work.remote_id))
736
			node = node->rb_left;
R
Roland Dreier 已提交
737
		else if (be32_gt(remote_id, timewait_info->work.remote_id))
738
			node = node->rb_right;
R
Roland Dreier 已提交
739
		else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
740
			node = node->rb_left;
R
Roland Dreier 已提交
741
		else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
742 743 744 745 746 747 748 749 750 751 752 753 754
			node = node->rb_right;
		else
			return timewait_info;
	}
	return NULL;
}

static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
						      *timewait_info)
{
	struct rb_node **link = &cm.remote_qp_table.rb_node;
	struct rb_node *parent = NULL;
	struct cm_timewait_info *cur_timewait_info;
755 756
	__be64 remote_ca_guid = timewait_info->remote_ca_guid;
	__be32 remote_qpn = timewait_info->remote_qpn;
757 758 759 760 761

	while (*link) {
		parent = *link;
		cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
					     remote_qp_node);
R
Roland Dreier 已提交
762
		if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
763
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
764
		else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
765
			link = &(*link)->rb_right;
R
Roland Dreier 已提交
766
		else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
767
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
768
		else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
			link = &(*link)->rb_right;
		else
			return cur_timewait_info;
	}
	timewait_info->inserted_remote_qp = 1;
	rb_link_node(&timewait_info->remote_qp_node, parent, link);
	rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
	return NULL;
}

static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
						    *cm_id_priv)
{
	struct rb_node **link = &cm.remote_sidr_table.rb_node;
	struct rb_node *parent = NULL;
	struct cm_id_private *cur_cm_id_priv;
	union ib_gid *port_gid = &cm_id_priv->av.dgid;
786
	__be32 remote_id = cm_id_priv->id.remote_id;
787 788 789 790 791

	while (*link) {
		parent = *link;
		cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
					  sidr_id_node);
R
Roland Dreier 已提交
792
		if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
793
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
794
		else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
			link = &(*link)->rb_right;
		else {
			int cmp;
			cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
				     sizeof *port_gid);
			if (cmp < 0)
				link = &(*link)->rb_left;
			else if (cmp > 0)
				link = &(*link)->rb_right;
			else
				return cur_cm_id_priv;
		}
	}
	rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
	rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
	return NULL;
}

static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
			       enum ib_cm_sidr_status status)
{
	struct ib_cm_sidr_rep_param param;

	memset(&param, 0, sizeof param);
	param.status = status;
	ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
}

823 824
struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
				 ib_cm_handler cm_handler,
825 826 827 828 829
				 void *context)
{
	struct cm_id_private *cm_id_priv;
	int ret;

R
Roland Dreier 已提交
830
	cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
831 832 833 834
	if (!cm_id_priv)
		return ERR_PTR(-ENOMEM);

	cm_id_priv->id.state = IB_CM_IDLE;
835
	cm_id_priv->id.device = device;
836 837
	cm_id_priv->id.cm_handler = cm_handler;
	cm_id_priv->id.context = context;
J
John Kingman 已提交
838
	cm_id_priv->id.remote_cm_qpn = 1;
839 840 841 842 843
	ret = cm_alloc_id(cm_id_priv);
	if (ret)
		goto error;

	spin_lock_init(&cm_id_priv->lock);
S
Sean Hefty 已提交
844
	init_completion(&cm_id_priv->comp);
845
	INIT_LIST_HEAD(&cm_id_priv->work_list);
846 847
	INIT_LIST_HEAD(&cm_id_priv->prim_list);
	INIT_LIST_HEAD(&cm_id_priv->altr_list);
848
	atomic_set(&cm_id_priv->work_count, -1);
849
	refcount_set(&cm_id_priv->refcount, 1);
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
	return &cm_id_priv->id;

error:
	kfree(cm_id_priv);
	return ERR_PTR(-ENOMEM);
}
EXPORT_SYMBOL(ib_create_cm_id);

static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
{
	struct cm_work *work;

	if (list_empty(&cm_id_priv->work_list))
		return NULL;

	work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
	list_del(&work->list);
	return work;
}

static void cm_free_work(struct cm_work *work)
{
	if (work->mad_recv_wc)
		ib_free_recv_mad(work->mad_recv_wc);
	kfree(work);
}

static inline int cm_convert_to_ms(int iba_time)
{
	/* approximate conversion to ms from 4.096us x 2^iba_time */
	return 1 << max(iba_time - 8, 0);
}

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
/*
 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
 * Because of how ack_timeout is stored, adding one doubles the timeout.
 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
 * increment it (round up) only if the other is within 50%.
 */
static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
{
	int ack_timeout = packet_life_time + 1;

	if (ack_timeout >= ca_ack_delay)
		ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
	else
		ack_timeout = ca_ack_delay +
			      (ack_timeout >= (ca_ack_delay - 1));

	return min(31, ack_timeout);
}

902 903 904 905 906 907 908 909 910 911 912 913 914
static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
{
	if (timewait_info->inserted_remote_id) {
		rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
		timewait_info->inserted_remote_id = 0;
	}

	if (timewait_info->inserted_remote_qp) {
		rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
		timewait_info->inserted_remote_qp = 0;
	}
}

915
static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
916 917 918
{
	struct cm_timewait_info *timewait_info;

R
Roland Dreier 已提交
919
	timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
920 921 922 923
	if (!timewait_info)
		return ERR_PTR(-ENOMEM);

	timewait_info->work.local_id = local_id;
D
David Howells 已提交
924
	INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
925 926 927 928 929 930 931
	timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
	return timewait_info;
}

static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
{
	int wait_time;
932
	unsigned long flags;
933 934 935 936 937
	struct cm_device *cm_dev;

	cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
	if (!cm_dev)
		return;
938

939
	spin_lock_irqsave(&cm.lock, flags);
940
	cm_cleanup_timewait(cm_id_priv->timewait_info);
941 942
	list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
	spin_unlock_irqrestore(&cm.lock, flags);
943

944 945 946 947 948 949
	/*
	 * The cm_id could be destroyed by the user before we exit timewait.
	 * To protect against this, we search for the cm_id after exiting
	 * timewait before notifying the user that we've exited timewait.
	 */
	cm_id_priv->id.state = IB_CM_TIMEWAIT;
950
	wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
951 952

	/* Check if the device started its remove_one */
953
	spin_lock_irqsave(&cm.lock, flags);
954 955 956
	if (!cm_dev->going_down)
		queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
				   msecs_to_jiffies(wait_time));
957
	spin_unlock_irqrestore(&cm.lock, flags);
958

959 960 961 962 963
	cm_id_priv->timewait_info = NULL;
}

static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
{
964 965
	unsigned long flags;

966 967
	cm_id_priv->id.state = IB_CM_IDLE;
	if (cm_id_priv->timewait_info) {
968
		spin_lock_irqsave(&cm.lock, flags);
969
		cm_cleanup_timewait(cm_id_priv->timewait_info);
970
		spin_unlock_irqrestore(&cm.lock, flags);
971 972 973 974 975
		kfree(cm_id_priv->timewait_info);
		cm_id_priv->timewait_info = NULL;
	}
}

976
static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
977 978 979 980 981 982
{
	struct cm_id_private *cm_id_priv;
	struct cm_work *work;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
retest:
983
	spin_lock_irq(&cm_id_priv->lock);
984 985
	switch (cm_id->state) {
	case IB_CM_LISTEN:
986
		spin_unlock_irq(&cm_id_priv->lock);
H
Haggai Eran 已提交
987

988
		spin_lock_irq(&cm.lock);
H
Haggai Eran 已提交
989 990 991 992 993 994
		if (--cm_id_priv->listen_sharecount > 0) {
			/* The id is still shared. */
			cm_deref_id(cm_id_priv);
			spin_unlock_irq(&cm.lock);
			return;
		}
995
		rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
996
		spin_unlock_irq(&cm.lock);
997 998 999
		break;
	case IB_CM_SIDR_REQ_SENT:
		cm_id->state = IB_CM_IDLE;
1000
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1001
		spin_unlock_irq(&cm_id_priv->lock);
1002 1003
		break;
	case IB_CM_SIDR_REQ_RCVD:
1004
		spin_unlock_irq(&cm_id_priv->lock);
1005
		cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
1006 1007 1008 1009 1010
		spin_lock_irq(&cm.lock);
		if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node))
			rb_erase(&cm_id_priv->sidr_id_node,
				 &cm.remote_sidr_table);
		spin_unlock_irq(&cm.lock);
1011 1012
		break;
	case IB_CM_REQ_SENT:
1013
	case IB_CM_MRA_REQ_RCVD:
S
Sean Hefty 已提交
1014
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1015
		spin_unlock_irq(&cm_id_priv->lock);
S
Sean Hefty 已提交
1016
		ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
1017 1018
			       &cm_id_priv->id.device->node_guid,
			       sizeof cm_id_priv->id.device->node_guid,
S
Sean Hefty 已提交
1019 1020
			       NULL, 0);
		break;
1021 1022 1023 1024
	case IB_CM_REQ_RCVD:
		if (err == -ENOMEM) {
			/* Do not reject to allow future retries. */
			cm_reset_to_idle(cm_id_priv);
1025
			spin_unlock_irq(&cm_id_priv->lock);
1026
		} else {
1027
			spin_unlock_irq(&cm_id_priv->lock);
1028 1029 1030 1031
			ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
				       NULL, 0, NULL, 0);
		}
		break;
1032 1033
	case IB_CM_REP_SENT:
	case IB_CM_MRA_REP_RCVD:
1034
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1035 1036 1037 1038
		/* Fall through */
	case IB_CM_MRA_REQ_SENT:
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
1039
		spin_unlock_irq(&cm_id_priv->lock);
S
Sean Hefty 已提交
1040 1041
		ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
			       NULL, 0, NULL, 0);
1042 1043
		break;
	case IB_CM_ESTABLISHED:
1044
		spin_unlock_irq(&cm_id_priv->lock);
1045 1046
		if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
			break;
1047 1048 1049
		ib_send_cm_dreq(cm_id, NULL, 0);
		goto retest;
	case IB_CM_DREQ_SENT:
1050
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1051
		cm_enter_timewait(cm_id_priv);
1052
		spin_unlock_irq(&cm_id_priv->lock);
1053 1054
		break;
	case IB_CM_DREQ_RCVD:
1055
		spin_unlock_irq(&cm_id_priv->lock);
1056 1057 1058
		ib_send_cm_drep(cm_id, NULL, 0);
		break;
	default:
1059
		spin_unlock_irq(&cm_id_priv->lock);
1060 1061 1062
		break;
	}

1063 1064 1065 1066 1067 1068 1069 1070 1071
	spin_lock_irq(&cm.lock);
	if (!list_empty(&cm_id_priv->altr_list) &&
	    (!cm_id_priv->altr_send_port_not_ready))
		list_del(&cm_id_priv->altr_list);
	if (!list_empty(&cm_id_priv->prim_list) &&
	    (!cm_id_priv->prim_send_port_not_ready))
		list_del(&cm_id_priv->prim_list);
	spin_unlock_irq(&cm.lock);

1072
	cm_free_id(cm_id->local_id);
S
Sean Hefty 已提交
1073 1074
	cm_deref_id(cm_id_priv);
	wait_for_completion(&cm_id_priv->comp);
1075 1076
	while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
		cm_free_work(work);
1077 1078 1079

	rdma_destroy_ah_attr(&cm_id_priv->av.ah_attr);
	rdma_destroy_ah_attr(&cm_id_priv->alt_av.ah_attr);
1080
	kfree(cm_id_priv->private_data);
1081
	kfree_rcu(cm_id_priv, rcu);
1082
}
1083 1084 1085 1086 1087

void ib_destroy_cm_id(struct ib_cm_id *cm_id)
{
	cm_destroy_id(cm_id, 0);
}
1088 1089
EXPORT_SYMBOL(ib_destroy_cm_id);

H
Haggai Eran 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
/**
 * __ib_cm_listen - Initiates listening on the specified service ID for
 *   connection and service ID resolution requests.
 * @cm_id: Connection identifier associated with the listen request.
 * @service_id: Service identifier matched against incoming connection
 *   and service ID resolution requests.  The service ID should be specified
 *   network-byte order.  If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
 *   assign a service ID to the caller.
 * @service_mask: Mask applied to service ID used to listen across a
 *   range of service IDs.  If set to 0, the service ID is matched
 *   exactly.  This parameter is ignored if %service_id is set to
 *   IB_CM_ASSIGN_SERVICE_ID.
 */
static int __ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id,
H
Haggai Eran 已提交
1104
			  __be64 service_mask)
1105 1106 1107 1108
{
	struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
	int ret = 0;

1109
	service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
1110 1111 1112 1113 1114 1115
	service_id &= service_mask;
	if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
	    (service_id != IB_CM_ASSIGN_SERVICE_ID))
		return -EINVAL;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1116 1117 1118
	if (cm_id->state != IB_CM_IDLE)
		return -EINVAL;

1119
	cm_id->state = IB_CM_LISTEN;
H
Haggai Eran 已提交
1120
	++cm_id_priv->listen_sharecount;
H
Haggai Eran 已提交
1121

1122
	if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
1123
		cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
1124
		cm_id->service_mask = ~cpu_to_be64(0);
1125 1126 1127 1128 1129 1130 1131 1132
	} else {
		cm_id->service_id = service_id;
		cm_id->service_mask = service_mask;
	}
	cur_cm_id_priv = cm_insert_listen(cm_id_priv);

	if (cur_cm_id_priv) {
		cm_id->state = IB_CM_IDLE;
H
Haggai Eran 已提交
1133
		--cm_id_priv->listen_sharecount;
1134 1135 1136 1137
		ret = -EBUSY;
	}
	return ret;
}
H
Haggai Eran 已提交
1138

H
Haggai Eran 已提交
1139
int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask)
H
Haggai Eran 已提交
1140
{
H
Haggai Eran 已提交
1141 1142 1143 1144 1145 1146 1147 1148
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&cm.lock, flags);
	ret = __ib_cm_listen(cm_id, service_id, service_mask);
	spin_unlock_irqrestore(&cm.lock, flags);

	return ret;
H
Haggai Eran 已提交
1149
}
1150 1151
EXPORT_SYMBOL(ib_cm_listen);

H
Haggai Eran 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
/**
 * Create a new listening ib_cm_id and listen on the given service ID.
 *
 * If there's an existing ID listening on that same device and service ID,
 * return it.
 *
 * @device: Device associated with the cm_id.  All related communication will
 * be associated with the specified device.
 * @cm_handler: Callback invoked to notify the user of CM events.
 * @service_id: Service identifier matched against incoming connection
 *   and service ID resolution requests.  The service ID should be specified
 *   network-byte order.  If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
 *   assign a service ID to the caller.
 *
 * Callers should call ib_destroy_cm_id when done with the listener ID.
 */
struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device,
				     ib_cm_handler cm_handler,
				     __be64 service_id)
{
	struct cm_id_private *cm_id_priv;
	struct ib_cm_id *cm_id;
	unsigned long flags;
	int err = 0;

	/* Create an ID in advance, since the creation may sleep */
	cm_id = ib_create_cm_id(device, cm_handler, NULL);
	if (IS_ERR(cm_id))
		return cm_id;

	spin_lock_irqsave(&cm.lock, flags);

	if (service_id == IB_CM_ASSIGN_SERVICE_ID)
		goto new_id;

	/* Find an existing ID */
H
Haggai Eran 已提交
1188
	cm_id_priv = cm_find_listen(device, service_id);
H
Haggai Eran 已提交
1189 1190 1191 1192 1193 1194 1195
	if (cm_id_priv) {
		if (cm_id->cm_handler != cm_handler || cm_id->context) {
			/* Sharing an ib_cm_id with different handlers is not
			 * supported */
			spin_unlock_irqrestore(&cm.lock, flags);
			return ERR_PTR(-EINVAL);
		}
1196
		refcount_inc(&cm_id_priv->refcount);
H
Haggai Eran 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
		++cm_id_priv->listen_sharecount;
		spin_unlock_irqrestore(&cm.lock, flags);

		ib_destroy_cm_id(cm_id);
		cm_id = &cm_id_priv->id;
		return cm_id;
	}

new_id:
	/* Use newly created ID */
H
Haggai Eran 已提交
1207
	err = __ib_cm_listen(cm_id, service_id, 0);
H
Haggai Eran 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

	spin_unlock_irqrestore(&cm.lock, flags);

	if (err) {
		ib_destroy_cm_id(cm_id);
		return ERR_PTR(err);
	}
	return cm_id;
}
EXPORT_SYMBOL(ib_cm_insert_listen);

1219
static __be64 cm_form_tid(struct cm_id_private *cm_id_priv)
1220 1221 1222 1223
{
	u64 hi_tid, low_tid;

	hi_tid   = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
1224
	low_tid  = (u64)cm_id_priv->id.local_id;
1225 1226 1227 1228
	return cpu_to_be64(hi_tid | low_tid);
}

static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
1229
			      __be16 attr_id, __be64 tid)
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
{
	hdr->base_version  = IB_MGMT_BASE_VERSION;
	hdr->mgmt_class	   = IB_MGMT_CLASS_CM;
	hdr->class_version = IB_CM_CLASS_VERSION;
	hdr->method	   = IB_MGMT_METHOD_SEND;
	hdr->attr_id	   = attr_id;
	hdr->tid	   = tid;
}

static void cm_format_req(struct cm_req_msg *req_msg,
			  struct cm_id_private *cm_id_priv,
			  struct ib_cm_req_param *param)
{
1243 1244
	struct sa_path_rec *pri_path = param->primary_path;
	struct sa_path_rec *alt_path = param->alternate_path;
1245 1246 1247 1248 1249
	bool pri_ext = false;

	if (pri_path->rec_type == SA_PATH_REC_TYPE_OPA)
		pri_ext = opa_is_extended_lid(pri_path->opa.dlid,
					      pri_path->opa.slid);
1250

1251
	cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1252
			  cm_form_tid(cm_id_priv));
1253

1254 1255 1256 1257 1258
	IBA_SET(CM_REQ_LOCAL_COMM_ID, req_msg,
		be32_to_cpu(cm_id_priv->id.local_id));
	IBA_SET(CM_REQ_SERVICE_ID, req_msg, be64_to_cpu(param->service_id));
	IBA_SET(CM_REQ_LOCAL_CA_GUID, req_msg,
		be64_to_cpu(cm_id_priv->id.device->node_guid));
1259
	IBA_SET(CM_REQ_LOCAL_QPN, req_msg, param->qp_num);
1260 1261 1262
	IBA_SET(CM_REQ_INITIATOR_DEPTH, req_msg, param->initiator_depth);
	IBA_SET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg,
		param->remote_cm_response_timeout);
1263
	cm_req_set_qp_type(req_msg, param->qp_type);
1264
	IBA_SET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg, param->flow_control);
1265
	IBA_SET(CM_REQ_STARTING_PSN, req_msg, param->starting_psn);
1266 1267
	IBA_SET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg,
		param->local_cm_response_timeout);
1268 1269
	IBA_SET(CM_REQ_PARTITION_KEY, req_msg,
		be16_to_cpu(param->primary_path->pkey));
1270 1271 1272
	IBA_SET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg,
		param->primary_path->mtu);
	IBA_SET(CM_REQ_MAX_CM_RETRIES, req_msg, param->max_cm_retries);
S
Sean Hefty 已提交
1273 1274

	if (param->qp_type != IB_QPT_XRC_INI) {
1275 1276 1277 1278 1279 1280
		IBA_SET(CM_REQ_RESPONDER_RESOURCES, req_msg,
			param->responder_resources);
		IBA_SET(CM_REQ_RETRY_COUNT, req_msg, param->retry_count);
		IBA_SET(CM_REQ_RNR_RETRY_COUNT, req_msg,
			param->rnr_retry_count);
		IBA_SET(CM_REQ_SRQ, req_msg, param->srq);
S
Sean Hefty 已提交
1281
	}
1282

1283 1284 1285 1286
	*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg) =
		pri_path->sgid;
	*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg) =
		pri_path->dgid;
1287
	if (pri_ext) {
1288 1289 1290 1291 1292 1293
		IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg)
			->global.interface_id =
			OPA_MAKE_ID(be32_to_cpu(pri_path->opa.slid));
		IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg)
			->global.interface_id =
			OPA_MAKE_ID(be32_to_cpu(pri_path->opa.dlid));
1294
	}
1295
	if (pri_path->hop_limit <= 1) {
1296 1297 1298 1299 1300 1301 1302 1303
		IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
			be16_to_cpu(pri_ext ? 0 :
					      htons(ntohl(sa_path_get_slid(
						      pri_path)))));
		IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
			be16_to_cpu(pri_ext ? 0 :
					      htons(ntohl(sa_path_get_dlid(
						      pri_path)))));
1304 1305
	} else {
		/* Work-around until there's a way to obtain remote LID info */
1306 1307 1308 1309
		IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
			be16_to_cpu(IB_LID_PERMISSIVE));
		IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
			be16_to_cpu(IB_LID_PERMISSIVE));
1310
	}
1311 1312
	IBA_SET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg,
		be32_to_cpu(pri_path->flow_label));
1313
	IBA_SET(CM_REQ_PRIMARY_PACKET_RATE, req_msg, pri_path->rate);
1314 1315
	IBA_SET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg, pri_path->traffic_class);
	IBA_SET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg, pri_path->hop_limit);
1316 1317 1318 1319
	IBA_SET(CM_REQ_PRIMARY_SL, req_msg, pri_path->sl);
	IBA_SET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg,
		(pri_path->hop_limit <= 1));
	IBA_SET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg,
1320
		cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1321
			       pri_path->packet_life_time));
1322

1323
	if (alt_path) {
1324 1325 1326 1327 1328 1329
		bool alt_ext = false;

		if (alt_path->rec_type == SA_PATH_REC_TYPE_OPA)
			alt_ext = opa_is_extended_lid(alt_path->opa.dlid,
						      alt_path->opa.slid);

1330 1331 1332 1333
		*IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg) =
			alt_path->sgid;
		*IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg) =
			alt_path->dgid;
1334
		if (alt_ext) {
1335 1336 1337 1338 1339 1340 1341 1342
			IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID,
					req_msg)
				->global.interface_id =
				OPA_MAKE_ID(be32_to_cpu(alt_path->opa.slid));
			IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID,
					req_msg)
				->global.interface_id =
				OPA_MAKE_ID(be32_to_cpu(alt_path->opa.dlid));
1343
		}
1344
		if (alt_path->hop_limit <= 1) {
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
			IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
				be16_to_cpu(
					alt_ext ? 0 :
						  htons(ntohl(sa_path_get_slid(
							  alt_path)))));
			IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
				be16_to_cpu(
					alt_ext ? 0 :
						  htons(ntohl(sa_path_get_dlid(
							  alt_path)))));
1355
		} else {
1356 1357 1358 1359
			IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
				be16_to_cpu(IB_LID_PERMISSIVE));
			IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
				be16_to_cpu(IB_LID_PERMISSIVE));
1360
		}
1361 1362
		IBA_SET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg,
			be32_to_cpu(alt_path->flow_label));
1363
		IBA_SET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg, alt_path->rate);
1364 1365 1366 1367
		IBA_SET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg,
			alt_path->traffic_class);
		IBA_SET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg,
			alt_path->hop_limit);
1368 1369 1370 1371
		IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, alt_path->sl);
		IBA_SET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg,
			(alt_path->hop_limit <= 1));
		IBA_SET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg,
1372
			cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1373
				       alt_path->packet_life_time));
1374 1375 1376
	}

	if (param->private_data && param->private_data_len)
1377 1378
		IBA_SET_MEM(CM_REQ_PRIVATE_DATA, req_msg, param->private_data,
			    param->private_data_len);
1379 1380
}

1381
static int cm_validate_req_param(struct ib_cm_req_param *param)
1382 1383 1384 1385 1386 1387 1388 1389
{
	/* peer-to-peer not supported */
	if (param->peer_to_peer)
		return -EINVAL;

	if (!param->primary_path)
		return -EINVAL;

S
Sean Hefty 已提交
1390 1391
	if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
	    param->qp_type != IB_QPT_XRC_INI)
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
		return -EINVAL;

	if (param->private_data &&
	    param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
		return -EINVAL;

	if (param->alternate_path &&
	    (param->alternate_path->pkey != param->primary_path->pkey ||
	     param->alternate_path->mtu != param->primary_path->mtu))
		return -EINVAL;

	return 0;
}

int ib_send_cm_req(struct ib_cm_id *cm_id,
		   struct ib_cm_req_param *param)
{
	struct cm_id_private *cm_id_priv;
	struct cm_req_msg *req_msg;
	unsigned long flags;
	int ret;

	ret = cm_validate_req_param(param);
	if (ret)
		return ret;

	/* Verify that we're not in timewait. */
	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	spin_lock_irqsave(&cm_id_priv->lock, flags);
	if (cm_id->state != IB_CM_IDLE) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		ret = -EINVAL;
		goto out;
	}
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);

	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
							    id.local_id);
1430 1431
	if (IS_ERR(cm_id_priv->timewait_info)) {
		ret = PTR_ERR(cm_id_priv->timewait_info);
1432
		goto out;
1433
	}
1434

1435 1436
	ret = cm_init_av_by_path(param->primary_path,
				 param->ppath_sgid_attr, &cm_id_priv->av,
1437
				 cm_id_priv);
1438 1439 1440
	if (ret)
		goto error1;
	if (param->alternate_path) {
1441
		ret = cm_init_av_by_path(param->alternate_path, NULL,
1442
					 &cm_id_priv->alt_av, cm_id_priv);
1443 1444 1445 1446
		if (ret)
			goto error1;
	}
	cm_id->service_id = param->service_id;
1447
	cm_id->service_mask = ~cpu_to_be64(0);
1448 1449 1450 1451 1452 1453 1454 1455 1456
	cm_id_priv->timeout_ms = cm_convert_to_ms(
				    param->primary_path->packet_life_time) * 2 +
				 cm_convert_to_ms(
				    param->remote_cm_response_timeout);
	cm_id_priv->max_cm_retries = param->max_cm_retries;
	cm_id_priv->initiator_depth = param->initiator_depth;
	cm_id_priv->responder_resources = param->responder_resources;
	cm_id_priv->retry_count = param->retry_count;
	cm_id_priv->path_mtu = param->primary_path->mtu;
1457
	cm_id_priv->pkey = param->primary_path->pkey;
1458
	cm_id_priv->qp_type = param->qp_type;
1459 1460 1461 1462 1463 1464 1465 1466

	ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
	if (ret)
		goto error1;

	req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
	cm_format_req(req_msg, cm_id_priv, param);
	cm_id_priv->tid = req_msg->hdr.tid;
1467
	cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1468 1469
	cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;

1470 1471
	cm_id_priv->local_qpn = cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg));
	cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg));
1472 1473

	spin_lock_irqsave(&cm_id_priv->lock, flags);
1474
	ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
	if (ret) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		goto error2;
	}
	BUG_ON(cm_id->state != IB_CM_IDLE);
	cm_id->state = IB_CM_REQ_SENT;
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return 0;

error2:	cm_free_msg(cm_id_priv->msg);
error1:	kfree(cm_id_priv->timewait_info);
out:	return ret;
}
EXPORT_SYMBOL(ib_send_cm_req);

static int cm_issue_rej(struct cm_port *port,
			struct ib_mad_recv_wc *mad_recv_wc,
			enum ib_cm_rej_reason reason,
			enum cm_msg_response msg_rejected,
			void *ari, u8 ari_length)
{
	struct ib_mad_send_buf *msg = NULL;
	struct cm_rej_msg *rej_msg, *rcv_msg;
	int ret;

	ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
	if (ret)
		return ret;

	/* We just need common CM header information.  Cast to any message. */
	rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
	rej_msg = (struct cm_rej_msg *) msg->mad;

	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1509 1510 1511 1512
	IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg,
		IBA_GET(CM_REJ_LOCAL_COMM_ID, rcv_msg));
	IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
		IBA_GET(CM_REJ_REMOTE_COMM_ID, rcv_msg));
1513
	IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, msg_rejected);
1514
	IBA_SET(CM_REJ_REASON, rej_msg, reason);
1515 1516

	if (ari && ari_length) {
1517
		IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length);
1518
		IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length);
1519 1520
	}

1521
	ret = ib_post_send_mad(msg, NULL);
1522 1523 1524 1525 1526 1527
	if (ret)
		cm_free_msg(msg);

	return ret;
}

1528 1529
static bool cm_req_has_alt_path(struct cm_req_msg *req_msg)
{
1530 1531
	return ((cpu_to_be16(
			IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg))) ||
1532 1533
		(ib_is_opa_gid(IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID,
					       req_msg))));
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
}

static void cm_path_set_rec_type(struct ib_device *ib_device, u8 port_num,
				 struct sa_path_rec *path, union ib_gid *gid)
{
	if (ib_is_opa_gid(gid) && rdma_cap_opa_ah(ib_device, port_num))
		path->rec_type = SA_PATH_REC_TYPE_OPA;
	else
		path->rec_type = SA_PATH_REC_TYPE_IB;
}

1545 1546 1547 1548 1549 1550 1551 1552
static void cm_format_path_lid_from_req(struct cm_req_msg *req_msg,
					struct sa_path_rec *primary_path,
					struct sa_path_rec *alt_path)
{
	u32 lid;

	if (primary_path->rec_type != SA_PATH_REC_TYPE_OPA) {
		sa_path_set_dlid(primary_path,
1553 1554
				 IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID,
					 req_msg));
1555
		sa_path_set_slid(primary_path,
1556 1557
				 IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID,
					 req_msg));
1558
	} else {
1559 1560
		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
			CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg));
1561
		sa_path_set_dlid(primary_path, lid);
1562

1563 1564
		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
			CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg));
1565
		sa_path_set_slid(primary_path, lid);
1566 1567 1568 1569 1570 1571
	}

	if (!cm_req_has_alt_path(req_msg))
		return;

	if (alt_path->rec_type != SA_PATH_REC_TYPE_OPA) {
1572 1573 1574 1575 1576 1577
		sa_path_set_dlid(alt_path,
				 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID,
					 req_msg));
		sa_path_set_slid(alt_path,
				 IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID,
					 req_msg));
1578
	} else {
1579 1580
		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
			CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg));
1581
		sa_path_set_dlid(alt_path, lid);
1582

1583 1584
		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
			CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg));
1585
		sa_path_set_slid(alt_path, lid);
1586 1587 1588
	}
}

1589
static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1590 1591
				     struct sa_path_rec *primary_path,
				     struct sa_path_rec *alt_path)
1592
{
1593 1594 1595 1596
	primary_path->dgid =
		*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg);
	primary_path->sgid =
		*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg);
1597 1598
	primary_path->flow_label =
		cpu_to_be32(IBA_GET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg));
1599 1600 1601
	primary_path->hop_limit = IBA_GET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg);
	primary_path->traffic_class =
		IBA_GET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg);
1602
	primary_path->reversible = 1;
1603 1604
	primary_path->pkey =
		cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
1605
	primary_path->sl = IBA_GET(CM_REQ_PRIMARY_SL, req_msg);
1606
	primary_path->mtu_selector = IB_SA_EQ;
1607
	primary_path->mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
1608
	primary_path->rate_selector = IB_SA_EQ;
1609
	primary_path->rate = IBA_GET(CM_REQ_PRIMARY_PACKET_RATE, req_msg);
1610 1611
	primary_path->packet_life_time_selector = IB_SA_EQ;
	primary_path->packet_life_time =
1612
		IBA_GET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg);
1613
	primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1614 1615
	primary_path->service_id =
		cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
1616 1617
	if (sa_path_is_roce(primary_path))
		primary_path->roce.route_resolved = false;
1618

1619
	if (cm_req_has_alt_path(req_msg)) {
1620 1621 1622 1623
		alt_path->dgid = *IBA_GET_MEM_PTR(
			CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg);
		alt_path->sgid = *IBA_GET_MEM_PTR(
			CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg);
1624 1625
		alt_path->flow_label = cpu_to_be32(
			IBA_GET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg));
1626 1627 1628 1629
		alt_path->hop_limit =
			IBA_GET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg);
		alt_path->traffic_class =
			IBA_GET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg);
1630
		alt_path->reversible = 1;
1631 1632
		alt_path->pkey =
			cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
1633
		alt_path->sl = IBA_GET(CM_REQ_ALTERNATE_SL, req_msg);
1634
		alt_path->mtu_selector = IB_SA_EQ;
1635 1636
		alt_path->mtu =
			IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
1637
		alt_path->rate_selector = IB_SA_EQ;
1638
		alt_path->rate = IBA_GET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg);
1639 1640
		alt_path->packet_life_time_selector = IB_SA_EQ;
		alt_path->packet_life_time =
1641
			IBA_GET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg);
1642
		alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1643 1644
		alt_path->service_id =
			cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
1645 1646 1647

		if (sa_path_is_roce(alt_path))
			alt_path->roce.route_resolved = false;
1648
	}
1649
	cm_format_path_lid_from_req(req_msg, primary_path, alt_path);
1650 1651
}

1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
static u16 cm_get_bth_pkey(struct cm_work *work)
{
	struct ib_device *ib_dev = work->port->cm_dev->ib_device;
	u8 port_num = work->port->port_num;
	u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
	u16 pkey;
	int ret;

	ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
	if (ret) {
		dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
				     port_num, pkey_index, ret);
		return 0;
	}

	return pkey;
}

1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
/**
 * Convert OPA SGID to IB SGID
 * ULPs (such as IPoIB) do not understand OPA GIDs and will
 * reject them as the local_gid will not match the sgid. Therefore,
 * change the pathrec's SGID to an IB SGID.
 *
 * @work: Work completion
 * @path: Path record
 */
static void cm_opa_to_ib_sgid(struct cm_work *work,
			      struct sa_path_rec *path)
{
	struct ib_device *dev = work->port->cm_dev->ib_device;
	u8 port_num = work->port->port_num;

	if (rdma_cap_opa_ah(dev, port_num) &&
	    (ib_is_opa_gid(&path->sgid))) {
		union ib_gid sgid;

1689
		if (rdma_query_gid(dev, port_num, 0, &sgid)) {
1690 1691 1692 1693 1694 1695 1696 1697 1698
			dev_warn(&dev->dev,
				 "Error updating sgid in CM request\n");
			return;
		}

		path->sgid = sgid;
	}
}

1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
static void cm_format_req_event(struct cm_work *work,
				struct cm_id_private *cm_id_priv,
				struct ib_cm_id *listen_id)
{
	struct cm_req_msg *req_msg;
	struct ib_cm_req_event_param *param;

	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
	param = &work->cm_event.param.req_rcvd;
	param->listen_id = listen_id;
1709
	param->bth_pkey = cm_get_bth_pkey(work);
1710 1711
	param->port = cm_id_priv->av.port->port_num;
	param->primary_path = &work->path[0];
1712 1713
	cm_opa_to_ib_sgid(work, param->primary_path);
	if (cm_req_has_alt_path(req_msg)) {
1714
		param->alternate_path = &work->path[1];
1715 1716
		cm_opa_to_ib_sgid(work, param->alternate_path);
	} else {
1717
		param->alternate_path = NULL;
1718
	}
1719 1720 1721
	param->remote_ca_guid =
		cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg));
	param->remote_qkey = IBA_GET(CM_REQ_LOCAL_Q_KEY, req_msg);
1722
	param->remote_qpn = IBA_GET(CM_REQ_LOCAL_QPN, req_msg);
1723
	param->qp_type = cm_req_get_qp_type(req_msg);
1724
	param->starting_psn = IBA_GET(CM_REQ_STARTING_PSN, req_msg);
1725 1726
	param->responder_resources = IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg);
	param->initiator_depth = IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg);
1727
	param->local_cm_response_timeout =
1728 1729
		IBA_GET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg);
	param->flow_control = IBA_GET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg);
1730
	param->remote_cm_response_timeout =
1731 1732 1733 1734
		IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg);
	param->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg);
	param->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg);
	param->srq = IBA_GET(CM_REQ_SRQ, req_msg);
1735
	param->ppath_sgid_attr = cm_id_priv->av.ah_attr.grh.sgid_attr;
1736 1737
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_REQ_PRIVATE_DATA, req_msg);
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
}

static void cm_process_work(struct cm_id_private *cm_id_priv,
			    struct cm_work *work)
{
	int ret;

	/* We will typically only have the current event to report. */
	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
	cm_free_work(work);

	while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1750
		spin_lock_irq(&cm_id_priv->lock);
1751
		work = cm_dequeue_work(cm_id_priv);
1752
		spin_unlock_irq(&cm_id_priv->lock);
1753 1754 1755
		if (!work)
			return;

1756 1757 1758 1759 1760 1761
		ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
						&work->cm_event);
		cm_free_work(work);
	}
	cm_deref_id(cm_id_priv);
	if (ret)
1762
		cm_destroy_id(&cm_id_priv->id, ret);
1763 1764 1765 1766 1767 1768 1769 1770
}

static void cm_format_mra(struct cm_mra_msg *mra_msg,
			  struct cm_id_private *cm_id_priv,
			  enum cm_msg_response msg_mraed, u8 service_timeout,
			  const void *private_data, u8 private_data_len)
{
	cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1771
	IBA_SET(CM_MRA_MESSAGE_MRAED, mra_msg, msg_mraed);
1772 1773 1774 1775
	IBA_SET(CM_MRA_LOCAL_COMM_ID, mra_msg,
		be32_to_cpu(cm_id_priv->id.local_id));
	IBA_SET(CM_MRA_REMOTE_COMM_ID, mra_msg,
		be32_to_cpu(cm_id_priv->id.remote_id));
1776
	IBA_SET(CM_MRA_SERVICE_TIMEOUT, mra_msg, service_timeout);
1777 1778

	if (private_data && private_data_len)
1779 1780
		IBA_SET_MEM(CM_MRA_PRIVATE_DATA, mra_msg, private_data,
			    private_data_len);
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
}

static void cm_format_rej(struct cm_rej_msg *rej_msg,
			  struct cm_id_private *cm_id_priv,
			  enum ib_cm_rej_reason reason,
			  void *ari,
			  u8 ari_length,
			  const void *private_data,
			  u8 private_data_len)
{
	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1792 1793
	IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg,
		be32_to_cpu(cm_id_priv->id.remote_id));
1794 1795 1796

	switch(cm_id_priv->id.state) {
	case IB_CM_REQ_RCVD:
1797
		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg, be32_to_cpu(0));
1798
		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ);
1799 1800
		break;
	case IB_CM_MRA_REQ_SENT:
1801 1802
		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
			be32_to_cpu(cm_id_priv->id.local_id));
1803
		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ);
1804 1805 1806
		break;
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
1807 1808
		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
			be32_to_cpu(cm_id_priv->id.local_id));
1809
		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REP);
1810 1811
		break;
	default:
1812 1813
		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
			be32_to_cpu(cm_id_priv->id.local_id));
1814 1815
		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg,
			CM_MSG_RESPONSE_OTHER);
1816 1817 1818
		break;
	}

1819
	IBA_SET(CM_REJ_REASON, rej_msg, reason);
1820
	if (ari && ari_length) {
1821
		IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length);
1822
		IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length);
1823 1824 1825
	}

	if (private_data && private_data_len)
1826 1827
		IBA_SET_MEM(CM_REJ_PRIVATE_DATA, rej_msg, private_data,
			    private_data_len);
1828 1829 1830 1831 1832 1833 1834 1835
}

static void cm_dup_req_handler(struct cm_work *work,
			       struct cm_id_private *cm_id_priv)
{
	struct ib_mad_send_buf *msg = NULL;
	int ret;

1836 1837 1838
	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
			counter[CM_REQ_COUNTER]);

1839 1840 1841 1842 1843 1844 1845 1846
	/* Quick state check to discard duplicate REQs. */
	if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
		return;

	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
	if (ret)
		return;

1847
	spin_lock_irq(&cm_id_priv->lock);
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
	switch (cm_id_priv->id.state) {
	case IB_CM_MRA_REQ_SENT:
		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
			      CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
			      cm_id_priv->private_data,
			      cm_id_priv->private_data_len);
		break;
	case IB_CM_TIMEWAIT:
		cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
			      IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
		break;
	default:
		goto unlock;
	}
1862
	spin_unlock_irq(&cm_id_priv->lock);
1863

1864
	ret = ib_post_send_mad(msg, NULL);
1865 1866 1867 1868
	if (ret)
		goto free;
	return;

1869
unlock:	spin_unlock_irq(&cm_id_priv->lock);
1870 1871 1872 1873 1874 1875 1876 1877 1878
free:	cm_free_msg(msg);
}

static struct cm_id_private * cm_match_req(struct cm_work *work,
					   struct cm_id_private *cm_id_priv)
{
	struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
	struct cm_timewait_info *timewait_info;
	struct cm_req_msg *req_msg;
1879
	struct ib_cm_id *cm_id;
1880 1881 1882

	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;

1883
	/* Check for possible duplicate REQ. */
1884
	spin_lock_irq(&cm.lock);
1885 1886
	timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
	if (timewait_info) {
1887
		cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
1888
					   timewait_info->work.remote_id);
1889
		spin_unlock_irq(&cm.lock);
1890 1891 1892
		if (cur_cm_id_priv) {
			cm_dup_req_handler(work, cur_cm_id_priv);
			cm_deref_id(cur_cm_id_priv);
1893 1894 1895 1896 1897 1898 1899 1900
		}
		return NULL;
	}

	/* Check for stale connections. */
	timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
	if (timewait_info) {
		cm_cleanup_timewait(cm_id_priv->timewait_info);
1901
		cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
1902 1903
					   timewait_info->work.remote_id);

1904
		spin_unlock_irq(&cm.lock);
1905 1906 1907
		cm_issue_rej(work->port, work->mad_recv_wc,
			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
			     NULL, 0);
1908 1909 1910 1911 1912
		if (cur_cm_id_priv) {
			cm_id = &cur_cm_id_priv->id;
			ib_send_cm_dreq(cm_id, NULL, 0);
			cm_deref_id(cur_cm_id_priv);
		}
1913
		return NULL;
1914 1915 1916
	}

	/* Find matching listen request. */
1917 1918 1919
	listen_cm_id_priv = cm_find_listen(
		cm_id_priv->id.device,
		cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg)));
1920
	if (!listen_cm_id_priv) {
1921
		cm_cleanup_timewait(cm_id_priv->timewait_info);
1922
		spin_unlock_irq(&cm.lock);
1923 1924 1925
		cm_issue_rej(work->port, work->mad_recv_wc,
			     IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
			     NULL, 0);
1926
		goto out;
1927
	}
1928 1929
	refcount_inc(&listen_cm_id_priv->refcount);
	refcount_inc(&cm_id_priv->refcount);
1930 1931
	cm_id_priv->id.state = IB_CM_REQ_RCVD;
	atomic_inc(&cm_id_priv->work_count);
1932
	spin_unlock_irq(&cm.lock);
1933
out:
1934 1935 1936
	return listen_cm_id_priv;
}

1937 1938 1939 1940 1941 1942 1943
/*
 * Work-around for inter-subnet connections.  If the LIDs are permissive,
 * we need to override the LID/SL data in the REQ with the LID information
 * in the work completion.
 */
static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
{
1944
	if (!IBA_GET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg)) {
1945 1946 1947 1948
		if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID,
					req_msg)) == IB_LID_PERMISSIVE) {
			IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
				be16_to_cpu(ib_lid_be16(wc->slid)));
1949
			IBA_SET(CM_REQ_PRIMARY_SL, req_msg, wc->sl);
1950 1951
		}

1952 1953 1954 1955
		if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID,
					req_msg)) == IB_LID_PERMISSIVE)
			IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
				wc->dlid_path_bits);
1956 1957
	}

1958
	if (!IBA_GET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg)) {
1959 1960 1961 1962
		if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID,
					req_msg)) == IB_LID_PERMISSIVE) {
			IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
				be16_to_cpu(ib_lid_be16(wc->slid)));
1963
			IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, wc->sl);
1964 1965
		}

1966 1967 1968 1969
		if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID,
					req_msg)) == IB_LID_PERMISSIVE)
			IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
				wc->dlid_path_bits);
1970 1971 1972
	}
}

1973 1974 1975 1976 1977
static int cm_req_handler(struct cm_work *work)
{
	struct ib_cm_id *cm_id;
	struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
	struct cm_req_msg *req_msg;
1978
	const struct ib_global_route *grh;
P
Parav Pandit 已提交
1979
	const struct ib_gid_attr *gid_attr;
1980 1981 1982 1983
	int ret;

	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;

1984
	cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
1985 1986 1987 1988
	if (IS_ERR(cm_id))
		return PTR_ERR(cm_id);

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1989 1990
	cm_id_priv->id.remote_id =
		cpu_to_be32(IBA_GET(CM_REQ_LOCAL_COMM_ID, req_msg));
P
Parav Pandit 已提交
1991 1992 1993 1994 1995
	ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
				      work->mad_recv_wc->recv_buf.grh,
				      &cm_id_priv->av);
	if (ret)
		goto destroy;
1996 1997 1998 1999
	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
							    id.local_id);
	if (IS_ERR(cm_id_priv->timewait_info)) {
		ret = PTR_ERR(cm_id_priv->timewait_info);
2000
		goto destroy;
2001
	}
2002 2003 2004 2005
	cm_id_priv->timewait_info->work.remote_id =
		cpu_to_be32(IBA_GET(CM_REQ_LOCAL_COMM_ID, req_msg));
	cm_id_priv->timewait_info->remote_ca_guid =
		cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg));
2006 2007
	cm_id_priv->timewait_info->remote_qpn =
		cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg));
2008 2009 2010

	listen_cm_id_priv = cm_match_req(work, cm_id_priv);
	if (!listen_cm_id_priv) {
D
Daniel Jurgens 已提交
2011 2012
		pr_debug("%s: local_id %d, no listen_cm_id_priv\n", __func__,
			 be32_to_cpu(cm_id->local_id));
2013
		ret = -EINVAL;
2014
		goto free_timeinfo;
2015 2016 2017 2018
	}

	cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
	cm_id_priv->id.context = listen_cm_id_priv->id.context;
2019 2020
	cm_id_priv->id.service_id =
		cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
2021
	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
2022

2023
	cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
2024

2025
	memset(&work->path[0], 0, sizeof(work->path[0]));
2026 2027
	if (cm_req_has_alt_path(req_msg))
		memset(&work->path[1], 0, sizeof(work->path[1]));
2028
	grh = rdma_ah_read_grh(&cm_id_priv->av.ah_attr);
P
Parav Pandit 已提交
2029
	gid_attr = grh->sgid_attr;
2030

2031 2032 2033
	if (gid_attr &&
	    rdma_protocol_roce(work->port->cm_dev->ib_device,
			       work->port->port_num)) {
2034
		work->path[0].rec_type =
P
Parav Pandit 已提交
2035
			sa_conv_gid_to_pathrec_type(gid_attr->gid_type);
2036
	} else {
2037 2038 2039 2040 2041
		cm_path_set_rec_type(
			work->port->cm_dev->ib_device, work->port->port_num,
			&work->path[0],
			IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID,
					req_msg));
2042 2043 2044 2045 2046 2047 2048 2049 2050
	}
	if (cm_req_has_alt_path(req_msg))
		work->path[1].rec_type = work->path[0].rec_type;
	cm_format_paths_from_req(req_msg, &work->path[0],
				 &work->path[1]);
	if (cm_id_priv->av.ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE)
		sa_path_set_dmac(&work->path[0],
				 cm_id_priv->av.ah_attr.roce.dmac);
	work->path[0].hop_limit = grh->hop_limit;
P
Parav Pandit 已提交
2051
	ret = cm_init_av_by_path(&work->path[0], gid_attr, &cm_id_priv->av,
2052
				 cm_id_priv);
2053
	if (ret) {
2054 2055
		int err;

2056 2057 2058
		err = rdma_query_gid(work->port->cm_dev->ib_device,
				     work->port->port_num, 0,
				     &work->path[0].sgid);
2059 2060 2061 2062 2063 2064 2065 2066
		if (err)
			ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
				       NULL, 0, NULL, 0);
		else
			ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
				       &work->path[0].sgid,
				       sizeof(work->path[0].sgid),
				       NULL, 0);
2067 2068
		goto rejected;
	}
2069
	if (cm_req_has_alt_path(req_msg)) {
2070 2071
		ret = cm_init_av_by_path(&work->path[1], NULL,
					 &cm_id_priv->alt_av, cm_id_priv);
2072 2073 2074
		if (ret) {
			ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
				       &work->path[0].sgid,
2075
				       sizeof(work->path[0].sgid), NULL, 0);
2076 2077
			goto rejected;
		}
2078 2079 2080
	}
	cm_id_priv->tid = req_msg->hdr.tid;
	cm_id_priv->timeout_ms = cm_convert_to_ms(
2081 2082
		IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg));
	cm_id_priv->max_cm_retries = IBA_GET(CM_REQ_MAX_CM_RETRIES, req_msg);
2083 2084
	cm_id_priv->remote_qpn =
		cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg));
2085 2086 2087 2088 2089
	cm_id_priv->initiator_depth =
		IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg);
	cm_id_priv->responder_resources =
		IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg);
	cm_id_priv->path_mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
2090
	cm_id_priv->pkey = cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
2091
	cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg));
2092 2093
	cm_id_priv->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg);
	cm_id_priv->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg);
2094
	cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
2095 2096 2097 2098 2099 2100

	cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
	cm_process_work(cm_id_priv, work);
	cm_deref_id(listen_cm_id_priv);
	return 0;

2101
rejected:
2102
	refcount_dec(&cm_id_priv->refcount);
2103
	cm_deref_id(listen_cm_id_priv);
2104 2105
free_timeinfo:
	kfree(cm_id_priv->timewait_info);
2106 2107
destroy:
	ib_destroy_cm_id(cm_id);
2108 2109 2110 2111 2112 2113 2114 2115
	return ret;
}

static void cm_format_rep(struct cm_rep_msg *rep_msg,
			  struct cm_id_private *cm_id_priv,
			  struct ib_cm_rep_param *param)
{
	cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
2116 2117 2118 2119
	IBA_SET(CM_REP_LOCAL_COMM_ID, rep_msg,
		be32_to_cpu(cm_id_priv->id.local_id));
	IBA_SET(CM_REP_REMOTE_COMM_ID, rep_msg,
		be32_to_cpu(cm_id_priv->id.remote_id));
2120
	IBA_SET(CM_REP_STARTING_PSN, rep_msg, param->starting_psn);
2121 2122
	IBA_SET(CM_REP_RESPONDER_RESOURCES, rep_msg,
		param->responder_resources);
2123 2124 2125 2126
	IBA_SET(CM_REP_TARGET_ACK_DELAY, rep_msg,
		cm_id_priv->av.port->cm_dev->ack_delay);
	IBA_SET(CM_REP_FAILOVER_ACCEPTED, rep_msg, param->failover_accepted);
	IBA_SET(CM_REP_RNR_RETRY_COUNT, rep_msg, param->rnr_retry_count);
2127 2128
	IBA_SET(CM_REP_LOCAL_CA_GUID, rep_msg,
		be64_to_cpu(cm_id_priv->id.device->node_guid));
2129

S
Sean Hefty 已提交
2130
	if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
2131 2132
		IBA_SET(CM_REP_INITIATOR_DEPTH, rep_msg,
			param->initiator_depth);
2133 2134 2135
		IBA_SET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg,
			param->flow_control);
		IBA_SET(CM_REP_SRQ, rep_msg, param->srq);
2136
		IBA_SET(CM_REP_LOCAL_QPN, rep_msg, param->qp_num);
S
Sean Hefty 已提交
2137
	} else {
2138
		IBA_SET(CM_REP_SRQ, rep_msg, 1);
2139
		IBA_SET(CM_REP_LOCAL_EE_CONTEXT_NUMBER, rep_msg, param->qp_num);
S
Sean Hefty 已提交
2140 2141
	}

2142
	if (param->private_data && param->private_data_len)
2143 2144
		IBA_SET_MEM(CM_REP_PRIVATE_DATA, rep_msg, param->private_data,
			    param->private_data_len);
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
}

int ib_send_cm_rep(struct ib_cm_id *cm_id,
		   struct ib_cm_rep_param *param)
{
	struct cm_id_private *cm_id_priv;
	struct ib_mad_send_buf *msg;
	struct cm_rep_msg *rep_msg;
	unsigned long flags;
	int ret;

	if (param->private_data &&
	    param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
		return -EINVAL;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	spin_lock_irqsave(&cm_id_priv->lock, flags);
	if (cm_id->state != IB_CM_REQ_RCVD &&
	    cm_id->state != IB_CM_MRA_REQ_SENT) {
D
Daniel Jurgens 已提交
2164 2165
		pr_debug("%s: local_comm_id %d, cm_id->state: %d\n", __func__,
			 be32_to_cpu(cm_id_priv->id.local_id), cm_id->state);
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
		ret = -EINVAL;
		goto out;
	}

	ret = cm_alloc_msg(cm_id_priv, &msg);
	if (ret)
		goto out;

	rep_msg = (struct cm_rep_msg *) msg->mad;
	cm_format_rep(rep_msg, cm_id_priv, param);
2176
	msg->timeout_ms = cm_id_priv->timeout_ms;
2177 2178
	msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;

2179
	ret = ib_post_send_mad(msg, NULL);
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
	if (ret) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		cm_free_msg(msg);
		return ret;
	}

	cm_id->state = IB_CM_REP_SENT;
	cm_id_priv->msg = msg;
	cm_id_priv->initiator_depth = param->initiator_depth;
	cm_id_priv->responder_resources = param->responder_resources;
2190
	cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg));
2191
	cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203

out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return ret;
}
EXPORT_SYMBOL(ib_send_cm_rep);

static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
			  struct cm_id_private *cm_id_priv,
			  const void *private_data,
			  u8 private_data_len)
{
	cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
2204 2205 2206 2207
	IBA_SET(CM_RTU_LOCAL_COMM_ID, rtu_msg,
		be32_to_cpu(cm_id_priv->id.local_id));
	IBA_SET(CM_RTU_REMOTE_COMM_ID, rtu_msg,
		be32_to_cpu(cm_id_priv->id.remote_id));
2208 2209

	if (private_data && private_data_len)
2210 2211
		IBA_SET_MEM(CM_RTU_PRIVATE_DATA, rtu_msg, private_data,
			    private_data_len);
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
}

int ib_send_cm_rtu(struct ib_cm_id *cm_id,
		   const void *private_data,
		   u8 private_data_len)
{
	struct cm_id_private *cm_id_priv;
	struct ib_mad_send_buf *msg;
	unsigned long flags;
	void *data;
	int ret;

	if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
		return -EINVAL;

	data = cm_copy_private_data(private_data, private_data_len);
	if (IS_ERR(data))
		return PTR_ERR(data);

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	spin_lock_irqsave(&cm_id_priv->lock, flags);
	if (cm_id->state != IB_CM_REP_RCVD &&
	    cm_id->state != IB_CM_MRA_REP_SENT) {
D
Daniel Jurgens 已提交
2235 2236
		pr_debug("%s: local_id %d, cm_id->state %d\n", __func__,
			 be32_to_cpu(cm_id->local_id), cm_id->state);
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
		ret = -EINVAL;
		goto error;
	}

	ret = cm_alloc_msg(cm_id_priv, &msg);
	if (ret)
		goto error;

	cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
		      private_data, private_data_len);

2248
	ret = ib_post_send_mad(msg, NULL);
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
	if (ret) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		cm_free_msg(msg);
		kfree(data);
		return ret;
	}

	cm_id->state = IB_CM_ESTABLISHED;
	cm_set_private_data(cm_id_priv, data, private_data_len);
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return 0;

error:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	kfree(data);
	return ret;
}
EXPORT_SYMBOL(ib_send_cm_rtu);

2267
static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
2268 2269 2270 2271 2272 2273
{
	struct cm_rep_msg *rep_msg;
	struct ib_cm_rep_event_param *param;

	rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
	param = &work->cm_event.param.rep_rcvd;
2274 2275 2276
	param->remote_ca_guid =
		cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg));
	param->remote_qkey = IBA_GET(CM_REP_LOCAL_Q_KEY, rep_msg);
2277
	param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
2278
	param->starting_psn = IBA_GET(CM_REP_STARTING_PSN, rep_msg);
2279 2280
	param->responder_resources = IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg);
	param->initiator_depth = IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg);
2281 2282 2283 2284 2285
	param->target_ack_delay = IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg);
	param->failover_accepted = IBA_GET(CM_REP_FAILOVER_ACCEPTED, rep_msg);
	param->flow_control = IBA_GET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg);
	param->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg);
	param->srq = IBA_GET(CM_REP_SRQ, rep_msg);
2286 2287
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_REP_PRIVATE_DATA, rep_msg);
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
}

static void cm_dup_rep_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	struct cm_rep_msg *rep_msg;
	struct ib_mad_send_buf *msg = NULL;
	int ret;

	rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
2298 2299 2300
	cm_id_priv = cm_acquire_id(
		cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)),
		cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg)));
2301 2302 2303
	if (!cm_id_priv)
		return;

2304 2305
	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
			counter[CM_REP_COUNTER]);
2306 2307 2308 2309
	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
	if (ret)
		goto deref;

2310
	spin_lock_irq(&cm_id_priv->lock);
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
	if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
		cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
			      cm_id_priv->private_data,
			      cm_id_priv->private_data_len);
	else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
			      CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
			      cm_id_priv->private_data,
			      cm_id_priv->private_data_len);
	else
		goto unlock;
2322
	spin_unlock_irq(&cm_id_priv->lock);
2323

2324
	ret = ib_post_send_mad(msg, NULL);
2325 2326 2327 2328
	if (ret)
		goto free;
	goto deref;

2329
unlock:	spin_unlock_irq(&cm_id_priv->lock);
2330 2331 2332 2333 2334 2335 2336 2337 2338
free:	cm_free_msg(msg);
deref:	cm_deref_id(cm_id_priv);
}

static int cm_rep_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	struct cm_rep_msg *rep_msg;
	int ret;
2339 2340 2341
	struct cm_id_private *cur_cm_id_priv;
	struct ib_cm_id *cm_id;
	struct cm_timewait_info *timewait_info;
2342 2343

	rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2344 2345
	cm_id_priv = cm_acquire_id(
		cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)), 0);
2346 2347
	if (!cm_id_priv) {
		cm_dup_rep_handler(work);
D
Daniel Jurgens 已提交
2348
		pr_debug("%s: remote_comm_id %d, no cm_id_priv\n", __func__,
2349
			 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2350 2351 2352
		return -EINVAL;
	}

2353
	cm_format_rep_event(work, cm_id_priv->qp_type);
2354

2355
	spin_lock_irq(&cm_id_priv->lock);
2356 2357 2358 2359 2360
	switch (cm_id_priv->id.state) {
	case IB_CM_REQ_SENT:
	case IB_CM_MRA_REQ_RCVD:
		break;
	default:
2361
		spin_unlock_irq(&cm_id_priv->lock);
2362
		ret = -EINVAL;
2363 2364 2365 2366 2367
		pr_debug(
			"%s: cm_id_priv->id.state: %d, local_comm_id %d, remote_comm_id %d\n",
			__func__, cm_id_priv->id.state,
			IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg),
			IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2368 2369 2370
		goto error;
	}

2371 2372 2373 2374
	cm_id_priv->timewait_info->work.remote_id =
		cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg));
	cm_id_priv->timewait_info->remote_ca_guid =
		cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg));
2375
	cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2376

2377
	spin_lock(&cm.lock);
2378 2379
	/* Check for duplicate REP. */
	if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
2380
		spin_unlock(&cm.lock);
2381
		spin_unlock_irq(&cm_id_priv->lock);
2382
		ret = -EINVAL;
D
Daniel Jurgens 已提交
2383
		pr_debug("%s: Failed to insert remote id %d\n", __func__,
2384
			 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2385 2386 2387
		goto error;
	}
	/* Check for a stale connection. */
2388 2389
	timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
	if (timewait_info) {
2390 2391 2392
		rb_erase(&cm_id_priv->timewait_info->remote_id_node,
			 &cm.remote_id_table);
		cm_id_priv->timewait_info->inserted_remote_id = 0;
2393
		cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2394 2395
					   timewait_info->work.remote_id);

2396
		spin_unlock(&cm.lock);
2397
		spin_unlock_irq(&cm_id_priv->lock);
2398 2399 2400 2401
		cm_issue_rej(work->port, work->mad_recv_wc,
			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
			     NULL, 0);
		ret = -EINVAL;
2402 2403 2404 2405
		pr_debug(
			"%s: Stale connection. local_comm_id %d, remote_comm_id %d\n",
			__func__, IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg),
			IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
D
Daniel Jurgens 已提交
2406

2407 2408 2409 2410 2411 2412
		if (cur_cm_id_priv) {
			cm_id = &cur_cm_id_priv->id;
			ib_send_cm_dreq(cm_id, NULL, 0);
			cm_deref_id(cur_cm_id_priv);
		}

2413 2414
		goto error;
	}
2415
	spin_unlock(&cm.lock);
2416 2417

	cm_id_priv->id.state = IB_CM_REP_RCVD;
2418 2419
	cm_id_priv->id.remote_id =
		cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg));
2420
	cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2421 2422 2423 2424
	cm_id_priv->initiator_depth =
		IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg);
	cm_id_priv->responder_resources =
		IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg);
2425
	cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg));
2426 2427 2428
	cm_id_priv->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg);
	cm_id_priv->target_ack_delay =
		IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg);
2429 2430 2431 2432 2433 2434
	cm_id_priv->av.timeout =
			cm_ack_timeout(cm_id_priv->target_ack_delay,
				       cm_id_priv->av.timeout - 1);
	cm_id_priv->alt_av.timeout =
			cm_ack_timeout(cm_id_priv->target_ack_delay,
				       cm_id_priv->alt_av.timeout - 1);
2435 2436 2437

	/* todo: handle peer_to_peer */

2438
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2439 2440 2441
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2442
	spin_unlock_irq(&cm_id_priv->lock);
2443 2444 2445 2446 2447 2448 2449

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;

2450
error:
2451 2452 2453 2454 2455 2456 2457 2458 2459
	cm_deref_id(cm_id_priv);
	return ret;
}

static int cm_establish_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	int ret;

2460
	/* See comment in cm_establish about lookup. */
2461 2462 2463 2464
	cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
	if (!cm_id_priv)
		return -EINVAL;

2465
	spin_lock_irq(&cm_id_priv->lock);
2466
	if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2467
		spin_unlock_irq(&cm_id_priv->lock);
2468 2469 2470
		goto out;
	}

2471
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2472 2473 2474
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2475
	spin_unlock_irq(&cm_id_priv->lock);
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;
out:
	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

static int cm_rtu_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	struct cm_rtu_msg *rtu_msg;
	int ret;

	rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
2494 2495 2496
	cm_id_priv = cm_acquire_id(
		cpu_to_be32(IBA_GET(CM_RTU_REMOTE_COMM_ID, rtu_msg)),
		cpu_to_be32(IBA_GET(CM_RTU_LOCAL_COMM_ID, rtu_msg)));
2497 2498 2499
	if (!cm_id_priv)
		return -EINVAL;

2500 2501
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_RTU_PRIVATE_DATA, rtu_msg);
2502

2503
	spin_lock_irq(&cm_id_priv->lock);
2504 2505
	if (cm_id_priv->id.state != IB_CM_REP_SENT &&
	    cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
2506
		spin_unlock_irq(&cm_id_priv->lock);
2507 2508
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_RTU_COUNTER]);
2509 2510 2511 2512
		goto out;
	}
	cm_id_priv->id.state = IB_CM_ESTABLISHED;

2513
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2514 2515 2516
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2517
	spin_unlock_irq(&cm_id_priv->lock);
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;
out:
	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
			  struct cm_id_private *cm_id_priv,
			  const void *private_data,
			  u8 private_data_len)
{
	cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2535
			  cm_form_tid(cm_id_priv));
2536 2537 2538 2539
	IBA_SET(CM_DREQ_LOCAL_COMM_ID, dreq_msg,
		be32_to_cpu(cm_id_priv->id.local_id));
	IBA_SET(CM_DREQ_REMOTE_COMM_ID, dreq_msg,
		be32_to_cpu(cm_id_priv->id.remote_id));
2540 2541
	IBA_SET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg,
		be32_to_cpu(cm_id_priv->remote_qpn));
2542 2543

	if (private_data && private_data_len)
2544 2545
		IBA_SET_MEM(CM_DREQ_PRIVATE_DATA, dreq_msg, private_data,
			    private_data_len);
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562
}

int ib_send_cm_dreq(struct ib_cm_id *cm_id,
		    const void *private_data,
		    u8 private_data_len)
{
	struct cm_id_private *cm_id_priv;
	struct ib_mad_send_buf *msg;
	unsigned long flags;
	int ret;

	if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
		return -EINVAL;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	spin_lock_irqsave(&cm_id_priv->lock, flags);
	if (cm_id->state != IB_CM_ESTABLISHED) {
D
Daniel Jurgens 已提交
2563 2564
		pr_debug("%s: local_id %d, cm_id->state: %d\n", __func__,
			 be32_to_cpu(cm_id->local_id), cm_id->state);
2565 2566 2567 2568
		ret = -EINVAL;
		goto out;
	}

2569 2570 2571 2572
	if (cm_id->lap_state == IB_CM_LAP_SENT ||
	    cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);

2573 2574 2575 2576 2577 2578 2579 2580
	ret = cm_alloc_msg(cm_id_priv, &msg);
	if (ret) {
		cm_enter_timewait(cm_id_priv);
		goto out;
	}

	cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
		       private_data, private_data_len);
2581
	msg->timeout_ms = cm_id_priv->timeout_ms;
2582 2583
	msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;

2584
	ret = ib_post_send_mad(msg, NULL);
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
	if (ret) {
		cm_enter_timewait(cm_id_priv);
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		cm_free_msg(msg);
		return ret;
	}

	cm_id->state = IB_CM_DREQ_SENT;
	cm_id_priv->msg = msg;
out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return ret;
}
EXPORT_SYMBOL(ib_send_cm_dreq);

static void cm_format_drep(struct cm_drep_msg *drep_msg,
			  struct cm_id_private *cm_id_priv,
			  const void *private_data,
			  u8 private_data_len)
{
	cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2605 2606 2607 2608
	IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg,
		be32_to_cpu(cm_id_priv->id.local_id));
	IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg,
		be32_to_cpu(cm_id_priv->id.remote_id));
2609 2610

	if (private_data && private_data_len)
2611 2612
		IBA_SET_MEM(CM_DREP_PRIVATE_DATA, drep_msg, private_data,
			    private_data_len);
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
}

int ib_send_cm_drep(struct ib_cm_id *cm_id,
		    const void *private_data,
		    u8 private_data_len)
{
	struct cm_id_private *cm_id_priv;
	struct ib_mad_send_buf *msg;
	unsigned long flags;
	void *data;
	int ret;

	if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
		return -EINVAL;

	data = cm_copy_private_data(private_data, private_data_len);
	if (IS_ERR(data))
		return PTR_ERR(data);

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	spin_lock_irqsave(&cm_id_priv->lock, flags);
	if (cm_id->state != IB_CM_DREQ_RCVD) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		kfree(data);
D
Daniel Jurgens 已提交
2637 2638
		pr_debug("%s: local_id %d, cm_idcm_id->state(%d) != IB_CM_DREQ_RCVD\n",
			 __func__, be32_to_cpu(cm_id->local_id), cm_id->state);
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
		return -EINVAL;
	}

	cm_set_private_data(cm_id_priv, data, private_data_len);
	cm_enter_timewait(cm_id_priv);

	ret = cm_alloc_msg(cm_id_priv, &msg);
	if (ret)
		goto out;

	cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
		       private_data, private_data_len);

2652
	ret = ib_post_send_mad(msg, NULL);
2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
	if (ret) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		cm_free_msg(msg);
		return ret;
	}

out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return ret;
}
EXPORT_SYMBOL(ib_send_cm_drep);

2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
static int cm_issue_drep(struct cm_port *port,
			 struct ib_mad_recv_wc *mad_recv_wc)
{
	struct ib_mad_send_buf *msg = NULL;
	struct cm_dreq_msg *dreq_msg;
	struct cm_drep_msg *drep_msg;
	int ret;

	ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
	if (ret)
		return ret;

	dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
	drep_msg = (struct cm_drep_msg *) msg->mad;

	cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2680 2681 2682 2683
	IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg,
		IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg));
	IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg,
		IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2684 2685 2686 2687 2688 2689 2690 2691

	ret = ib_post_send_mad(msg, NULL);
	if (ret)
		cm_free_msg(msg);

	return ret;
}

2692 2693 2694 2695 2696 2697 2698 2699
static int cm_dreq_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	struct cm_dreq_msg *dreq_msg;
	struct ib_mad_send_buf *msg = NULL;
	int ret;

	dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2700 2701 2702
	cm_id_priv = cm_acquire_id(
		cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg)),
		cpu_to_be32(IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg)));
2703
	if (!cm_id_priv) {
2704 2705
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_DREQ_COUNTER]);
2706
		cm_issue_drep(work->port, work->mad_recv_wc);
2707 2708 2709 2710
		pr_debug(
			"%s: no cm_id_priv, local_comm_id %d, remote_comm_id %d\n",
			__func__, IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg),
			IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2711
		return -EINVAL;
2712
	}
2713

2714 2715
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_DREQ_PRIVATE_DATA, dreq_msg);
2716

2717
	spin_lock_irq(&cm_id_priv->lock);
2718 2719
	if (cm_id_priv->local_qpn !=
	    cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg)))
2720 2721 2722 2723 2724
		goto unlock;

	switch (cm_id_priv->id.state) {
	case IB_CM_REP_SENT:
	case IB_CM_DREQ_SENT:
2725
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2726 2727
		break;
	case IB_CM_ESTABLISHED:
2728 2729 2730 2731
		if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
		    cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
			ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
		break;
2732 2733 2734
	case IB_CM_MRA_REP_RCVD:
		break;
	case IB_CM_TIMEWAIT:
2735 2736
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_DREQ_COUNTER]);
2737 2738
		msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc);
		if (IS_ERR(msg))
2739 2740 2741 2742 2743
			goto unlock;

		cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
			       cm_id_priv->private_data,
			       cm_id_priv->private_data_len);
2744
		spin_unlock_irq(&cm_id_priv->lock);
2745

2746 2747
		if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
		    ib_post_send_mad(msg, NULL))
2748 2749
			cm_free_msg(msg);
		goto deref;
2750 2751 2752 2753
	case IB_CM_DREQ_RCVD:
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_DREQ_COUNTER]);
		goto unlock;
2754
	default:
D
Daniel Jurgens 已提交
2755 2756 2757
		pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
			 __func__, be32_to_cpu(cm_id_priv->id.local_id),
			 cm_id_priv->id.state);
2758 2759 2760 2761 2762 2763 2764
		goto unlock;
	}
	cm_id_priv->id.state = IB_CM_DREQ_RCVD;
	cm_id_priv->tid = dreq_msg->hdr.tid;
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2765
	spin_unlock_irq(&cm_id_priv->lock);
2766 2767 2768 2769 2770 2771 2772

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;

2773
unlock:	spin_unlock_irq(&cm_id_priv->lock);
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
deref:	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

static int cm_drep_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	struct cm_drep_msg *drep_msg;
	int ret;

	drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2785 2786 2787
	cm_id_priv = cm_acquire_id(
		cpu_to_be32(IBA_GET(CM_DREP_REMOTE_COMM_ID, drep_msg)),
		cpu_to_be32(IBA_GET(CM_DREP_LOCAL_COMM_ID, drep_msg)));
2788 2789 2790
	if (!cm_id_priv)
		return -EINVAL;

2791 2792
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_DREP_PRIVATE_DATA, drep_msg);
2793

2794
	spin_lock_irq(&cm_id_priv->lock);
2795 2796
	if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
	    cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2797
		spin_unlock_irq(&cm_id_priv->lock);
2798 2799 2800 2801
		goto out;
	}
	cm_enter_timewait(cm_id_priv);

2802
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2803 2804 2805
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2806
	spin_unlock_irq(&cm_id_priv->lock);
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;
out:
	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

int ib_send_cm_rej(struct ib_cm_id *cm_id,
		   enum ib_cm_rej_reason reason,
		   void *ari,
		   u8 ari_length,
		   const void *private_data,
		   u8 private_data_len)
{
	struct cm_id_private *cm_id_priv;
	struct ib_mad_send_buf *msg;
	unsigned long flags;
	int ret;

	if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
	    (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
		return -EINVAL;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);

	spin_lock_irqsave(&cm_id_priv->lock, flags);
	switch (cm_id->state) {
	case IB_CM_REQ_SENT:
	case IB_CM_MRA_REQ_RCVD:
	case IB_CM_REQ_RCVD:
	case IB_CM_MRA_REQ_SENT:
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
		ret = cm_alloc_msg(cm_id_priv, &msg);
		if (!ret)
			cm_format_rej((struct cm_rej_msg *) msg->mad,
				      cm_id_priv, reason, ari, ari_length,
				      private_data, private_data_len);

		cm_reset_to_idle(cm_id_priv);
		break;
	case IB_CM_REP_SENT:
	case IB_CM_MRA_REP_RCVD:
		ret = cm_alloc_msg(cm_id_priv, &msg);
		if (!ret)
			cm_format_rej((struct cm_rej_msg *) msg->mad,
				      cm_id_priv, reason, ari, ari_length,
				      private_data, private_data_len);

		cm_enter_timewait(cm_id_priv);
		break;
	default:
D
Daniel Jurgens 已提交
2863 2864
		pr_debug("%s: local_id %d, cm_id->state: %d\n", __func__,
			 be32_to_cpu(cm_id_priv->id.local_id), cm_id->state);
2865 2866 2867 2868 2869 2870 2871
		ret = -EINVAL;
		goto out;
	}

	if (ret)
		goto out;

2872
	ret = ib_post_send_mad(msg, NULL);
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
	if (ret)
		cm_free_msg(msg);

out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return ret;
}
EXPORT_SYMBOL(ib_send_cm_rej);

static void cm_format_rej_event(struct cm_work *work)
{
	struct cm_rej_msg *rej_msg;
	struct ib_cm_rej_event_param *param;

	rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
	param = &work->cm_event.param.rej_rcvd;
2888
	param->ari = IBA_GET_MEM_PTR(CM_REJ_ARI, rej_msg);
2889
	param->ari_length = IBA_GET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg);
2890
	param->reason = IBA_GET(CM_REJ_REASON, rej_msg);
2891 2892
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_REJ_PRIVATE_DATA, rej_msg);
2893 2894 2895 2896 2897 2898
}

static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
{
	struct cm_timewait_info *timewait_info;
	struct cm_id_private *cm_id_priv;
2899
	__be32 remote_id;
2900

2901
	remote_id = cpu_to_be32(IBA_GET(CM_REJ_LOCAL_COMM_ID, rej_msg));
2902

2903
	if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_TIMEOUT) {
2904
		spin_lock_irq(&cm.lock);
2905 2906 2907
		timewait_info = cm_find_remote_id(
			*((__be64 *)IBA_GET_MEM_PTR(CM_REJ_ARI, rej_msg)),
			remote_id);
2908
		if (!timewait_info) {
2909
			spin_unlock_irq(&cm.lock);
2910 2911
			return NULL;
		}
2912 2913
		cm_id_priv =
			cm_acquire_id(timewait_info->work.local_id, remote_id);
2914
		spin_unlock_irq(&cm.lock);
2915 2916
	} else if (IBA_GET(CM_REJ_MESSAGE_REJECTED, rej_msg) ==
		   CM_MSG_RESPONSE_REQ)
2917 2918 2919
		cm_id_priv = cm_acquire_id(
			cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)),
			0);
2920
	else
2921 2922 2923
		cm_id_priv = cm_acquire_id(
			cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)),
			remote_id);
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940

	return cm_id_priv;
}

static int cm_rej_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	struct cm_rej_msg *rej_msg;
	int ret;

	rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
	cm_id_priv = cm_acquire_rejected_id(rej_msg);
	if (!cm_id_priv)
		return -EINVAL;

	cm_format_rej_event(work);

2941
	spin_lock_irq(&cm_id_priv->lock);
2942 2943 2944 2945 2946
	switch (cm_id_priv->id.state) {
	case IB_CM_REQ_SENT:
	case IB_CM_MRA_REQ_RCVD:
	case IB_CM_REP_SENT:
	case IB_CM_MRA_REP_RCVD:
2947
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2948 2949 2950
		/* fall through */
	case IB_CM_REQ_RCVD:
	case IB_CM_MRA_REQ_SENT:
2951
		if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_STALE_CONN)
2952 2953 2954 2955 2956
			cm_enter_timewait(cm_id_priv);
		else
			cm_reset_to_idle(cm_id_priv);
		break;
	case IB_CM_DREQ_SENT:
2957
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2958 2959 2960 2961 2962
		/* fall through */
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
		cm_enter_timewait(cm_id_priv);
		break;
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
	case IB_CM_ESTABLISHED:
		if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
		    cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
			if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
				ib_cancel_mad(cm_id_priv->av.port->mad_agent,
					      cm_id_priv->msg);
			cm_enter_timewait(cm_id_priv);
			break;
		}
		/* fall through */
2973
	default:
2974
		spin_unlock_irq(&cm_id_priv->lock);
D
Daniel Jurgens 已提交
2975 2976 2977
		pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
			 __func__, be32_to_cpu(cm_id_priv->id.local_id),
			 cm_id_priv->id.state);
2978 2979 2980 2981 2982 2983 2984
		ret = -EINVAL;
		goto out;
	}

	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2985
	spin_unlock_irq(&cm_id_priv->lock);
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;
out:
	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

int ib_send_cm_mra(struct ib_cm_id *cm_id,
		   u8 service_timeout,
		   const void *private_data,
		   u8 private_data_len)
{
	struct cm_id_private *cm_id_priv;
	struct ib_mad_send_buf *msg;
3004 3005 3006
	enum ib_cm_state cm_state;
	enum ib_cm_lap_state lap_state;
	enum cm_msg_response msg_response;
3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
	void *data;
	unsigned long flags;
	int ret;

	if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
		return -EINVAL;

	data = cm_copy_private_data(private_data, private_data_len);
	if (IS_ERR(data))
		return PTR_ERR(data);

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);

	spin_lock_irqsave(&cm_id_priv->lock, flags);
	switch(cm_id_priv->id.state) {
	case IB_CM_REQ_RCVD:
3023 3024 3025
		cm_state = IB_CM_MRA_REQ_SENT;
		lap_state = cm_id->lap_state;
		msg_response = CM_MSG_RESPONSE_REQ;
3026 3027
		break;
	case IB_CM_REP_RCVD:
3028 3029 3030
		cm_state = IB_CM_MRA_REP_SENT;
		lap_state = cm_id->lap_state;
		msg_response = CM_MSG_RESPONSE_REP;
3031 3032
		break;
	case IB_CM_ESTABLISHED:
3033 3034 3035 3036 3037 3038
		if (cm_id->lap_state == IB_CM_LAP_RCVD) {
			cm_state = cm_id->state;
			lap_state = IB_CM_MRA_LAP_SENT;
			msg_response = CM_MSG_RESPONSE_OTHER;
			break;
		}
3039
		/* fall through */
3040
	default:
D
Daniel Jurgens 已提交
3041 3042 3043
		pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
			 __func__, be32_to_cpu(cm_id_priv->id.local_id),
			 cm_id_priv->id.state);
3044 3045 3046 3047 3048
		ret = -EINVAL;
		goto error1;
	}

	if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
3049 3050 3051 3052 3053
		ret = cm_alloc_msg(cm_id_priv, &msg);
		if (ret)
			goto error1;

		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
3054
			      msg_response, service_timeout,
3055
			      private_data, private_data_len);
3056
		ret = ib_post_send_mad(msg, NULL);
3057 3058 3059
		if (ret)
			goto error2;
	}
3060 3061 3062

	cm_id->state = cm_state;
	cm_id->lap_state = lap_state;
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
	cm_id_priv->service_timeout = service_timeout;
	cm_set_private_data(cm_id_priv, data, private_data_len);
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return 0;

error1:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	kfree(data);
	return ret;

error2:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	kfree(data);
	cm_free_msg(msg);
	return ret;
}
EXPORT_SYMBOL(ib_send_cm_mra);

static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
{
3081
	switch (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg)) {
3082
	case CM_MSG_RESPONSE_REQ:
3083 3084 3085
		return cm_acquire_id(
			cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)),
			0);
3086 3087
	case CM_MSG_RESPONSE_REP:
	case CM_MSG_RESPONSE_OTHER:
3088 3089 3090
		return cm_acquire_id(
			cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)),
			cpu_to_be32(IBA_GET(CM_MRA_LOCAL_COMM_ID, mra_msg)));
3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106
	default:
		return NULL;
	}
}

static int cm_mra_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	struct cm_mra_msg *mra_msg;
	int timeout, ret;

	mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
	cm_id_priv = cm_acquire_mraed_id(mra_msg);
	if (!cm_id_priv)
		return -EINVAL;

3107 3108
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_MRA_PRIVATE_DATA, mra_msg);
3109
	work->cm_event.param.mra_rcvd.service_timeout =
3110 3111
		IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg);
	timeout = cm_convert_to_ms(IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg)) +
3112
		  cm_convert_to_ms(cm_id_priv->av.timeout);
3113

3114
	spin_lock_irq(&cm_id_priv->lock);
3115 3116
	switch (cm_id_priv->id.state) {
	case IB_CM_REQ_SENT:
3117 3118
		if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
			    CM_MSG_RESPONSE_REQ ||
3119
		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
3120
				  cm_id_priv->msg, timeout))
3121 3122 3123 3124
			goto out;
		cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
		break;
	case IB_CM_REP_SENT:
3125 3126
		if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
			    CM_MSG_RESPONSE_REP ||
3127
		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
3128
				  cm_id_priv->msg, timeout))
3129 3130 3131 3132
			goto out;
		cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
		break;
	case IB_CM_ESTABLISHED:
3133 3134
		if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
			    CM_MSG_RESPONSE_OTHER ||
3135 3136
		    cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
3137 3138 3139 3140 3141
				  cm_id_priv->msg, timeout)) {
			if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
				atomic_long_inc(&work->port->
						counter_group[CM_RECV_DUPLICATES].
						counter[CM_MRA_COUNTER]);
3142
			goto out;
3143
		}
3144 3145
		cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
		break;
3146 3147 3148 3149 3150
	case IB_CM_MRA_REQ_RCVD:
	case IB_CM_MRA_REP_RCVD:
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_MRA_COUNTER]);
		/* fall through */
3151
	default:
D
Daniel Jurgens 已提交
3152 3153 3154
		pr_debug("%s local_id %d, cm_id_priv->id.state: %d\n",
			 __func__, be32_to_cpu(cm_id_priv->id.local_id),
			 cm_id_priv->id.state);
3155 3156 3157 3158 3159 3160 3161 3162
		goto out;
	}

	cm_id_priv->msg->context[1] = (void *) (unsigned long)
				      cm_id_priv->id.state;
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
3163
	spin_unlock_irq(&cm_id_priv->lock);
3164 3165 3166 3167 3168 3169 3170

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;
out:
3171
	spin_unlock_irq(&cm_id_priv->lock);
3172 3173 3174 3175
	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

3176 3177 3178 3179 3180 3181
static void cm_format_path_lid_from_lap(struct cm_lap_msg *lap_msg,
					struct sa_path_rec *path)
{
	u32 lid;

	if (path->rec_type != SA_PATH_REC_TYPE_OPA) {
3182 3183 3184 3185
		sa_path_set_dlid(path, IBA_GET(CM_LAP_ALTERNATE_LOCAL_PORT_LID,
					       lap_msg));
		sa_path_set_slid(path, IBA_GET(CM_LAP_ALTERNATE_REMOTE_PORT_LID,
					       lap_msg));
3186
	} else {
3187 3188
		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
			CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg));
3189
		sa_path_set_dlid(path, lid);
3190

3191 3192
		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
			CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg));
3193
		sa_path_set_slid(path, lid);
3194 3195 3196
	}
}

3197
static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
3198
				    struct sa_path_rec *path,
3199 3200
				    struct cm_lap_msg *lap_msg)
{
3201 3202 3203
	path->dgid = *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg);
	path->sgid =
		*IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg);
3204 3205
	path->flow_label =
		cpu_to_be32(IBA_GET(CM_LAP_ALTERNATE_FLOW_LABEL, lap_msg));
3206
	path->hop_limit = IBA_GET(CM_LAP_ALTERNATE_HOP_LIMIT, lap_msg);
3207
	path->traffic_class = IBA_GET(CM_LAP_ALTERNATE_TRAFFIC_CLASS, lap_msg);
3208
	path->reversible = 1;
3209
	path->pkey = cm_id_priv->pkey;
3210
	path->sl = IBA_GET(CM_LAP_ALTERNATE_SL, lap_msg);
3211
	path->mtu_selector = IB_SA_EQ;
3212
	path->mtu = cm_id_priv->path_mtu;
3213
	path->rate_selector = IB_SA_EQ;
3214
	path->rate = IBA_GET(CM_LAP_ALTERNATE_PACKET_RATE, lap_msg);
3215
	path->packet_life_time_selector = IB_SA_EQ;
3216 3217
	path->packet_life_time =
		IBA_GET(CM_LAP_ALTERNATE_LOCAL_ACK_TIMEOUT, lap_msg);
3218
	path->packet_life_time -= (path->packet_life_time > 0);
3219
	cm_format_path_lid_from_lap(lap_msg, path);
3220 3221 3222 3223 3224 3225 3226 3227 3228 3229
}

static int cm_lap_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	struct cm_lap_msg *lap_msg;
	struct ib_cm_lap_event_param *param;
	struct ib_mad_send_buf *msg = NULL;
	int ret;

3230 3231 3232 3233 3234 3235 3236
	/* Currently Alternate path messages are not supported for
	 * RoCE link layer.
	 */
	if (rdma_protocol_roce(work->port->cm_dev->ib_device,
			       work->port->port_num))
		return -EINVAL;

3237 3238
	/* todo: verify LAP request and send reject APR if invalid. */
	lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
3239 3240 3241
	cm_id_priv = cm_acquire_id(
		cpu_to_be32(IBA_GET(CM_LAP_REMOTE_COMM_ID, lap_msg)),
		cpu_to_be32(IBA_GET(CM_LAP_LOCAL_COMM_ID, lap_msg)));
3242 3243 3244 3245
	if (!cm_id_priv)
		return -EINVAL;

	param = &work->cm_event.param.lap_rcvd;
3246 3247
	memset(&work->path[0], 0, sizeof(work->path[1]));
	cm_path_set_rec_type(work->port->cm_dev->ib_device,
3248 3249 3250
			     work->port->port_num, &work->path[0],
			     IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID,
					     lap_msg));
3251
	param->alternate_path = &work->path[0];
3252
	cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
3253 3254
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_LAP_PRIVATE_DATA, lap_msg);
3255

3256
	spin_lock_irq(&cm_id_priv->lock);
3257 3258 3259 3260
	if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
		goto unlock;

	switch (cm_id_priv->id.lap_state) {
3261
	case IB_CM_LAP_UNINIT:
3262 3263 3264
	case IB_CM_LAP_IDLE:
		break;
	case IB_CM_MRA_LAP_SENT:
3265 3266
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_LAP_COUNTER]);
3267 3268
		msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc);
		if (IS_ERR(msg))
3269 3270 3271 3272 3273 3274 3275
			goto unlock;

		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
			      CM_MSG_RESPONSE_OTHER,
			      cm_id_priv->service_timeout,
			      cm_id_priv->private_data,
			      cm_id_priv->private_data_len);
3276
		spin_unlock_irq(&cm_id_priv->lock);
3277

3278 3279
		if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
		    ib_post_send_mad(msg, NULL))
3280 3281
			cm_free_msg(msg);
		goto deref;
3282 3283 3284 3285
	case IB_CM_LAP_RCVD:
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_LAP_COUNTER]);
		goto unlock;
3286 3287 3288 3289
	default:
		goto unlock;
	}

3290 3291 3292
	ret = cm_init_av_for_lap(work->port, work->mad_recv_wc->wc,
				 work->mad_recv_wc->recv_buf.grh,
				 &cm_id_priv->av);
3293 3294 3295
	if (ret)
		goto unlock;

3296 3297 3298 3299 3300
	ret = cm_init_av_by_path(param->alternate_path, NULL,
				 &cm_id_priv->alt_av, cm_id_priv);
	if (ret)
		goto unlock;

3301 3302
	cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
	cm_id_priv->tid = lap_msg->hdr.tid;
3303 3304 3305
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
3306
	spin_unlock_irq(&cm_id_priv->lock);
3307 3308 3309 3310 3311 3312 3313

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;

3314
unlock:	spin_unlock_irq(&cm_id_priv->lock);
3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
deref:	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

static int cm_apr_handler(struct cm_work *work)
{
	struct cm_id_private *cm_id_priv;
	struct cm_apr_msg *apr_msg;
	int ret;

3325 3326 3327 3328 3329 3330 3331
	/* Currently Alternate path messages are not supported for
	 * RoCE link layer.
	 */
	if (rdma_protocol_roce(work->port->cm_dev->ib_device,
			       work->port->port_num))
		return -EINVAL;

3332
	apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
3333 3334 3335
	cm_id_priv = cm_acquire_id(
		cpu_to_be32(IBA_GET(CM_APR_REMOTE_COMM_ID, apr_msg)),
		cpu_to_be32(IBA_GET(CM_APR_LOCAL_COMM_ID, apr_msg)));
3336 3337 3338
	if (!cm_id_priv)
		return -EINVAL; /* Unmatched reply. */

3339 3340
	work->cm_event.param.apr_rcvd.ap_status =
		IBA_GET(CM_APR_AR_STATUS, apr_msg);
3341 3342
	work->cm_event.param.apr_rcvd.apr_info =
		IBA_GET_MEM_PTR(CM_APR_ADDITIONAL_INFORMATION, apr_msg);
3343 3344
	work->cm_event.param.apr_rcvd.info_len =
		IBA_GET(CM_APR_ADDITIONAL_INFORMATION_LENGTH, apr_msg);
3345 3346
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_APR_PRIVATE_DATA, apr_msg);
3347

3348
	spin_lock_irq(&cm_id_priv->lock);
3349 3350 3351
	if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
	    (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
	     cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
3352
		spin_unlock_irq(&cm_id_priv->lock);
3353 3354 3355
		goto out;
	}
	cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
3356
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3357 3358 3359 3360 3361
	cm_id_priv->msg = NULL;

	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
3362
	spin_unlock_irq(&cm_id_priv->lock);
3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;
out:
	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

static int cm_timewait_handler(struct cm_work *work)
{
	struct cm_timewait_info *timewait_info;
	struct cm_id_private *cm_id_priv;
	int ret;

3380
	timewait_info = container_of(work, struct cm_timewait_info, work);
3381 3382 3383
	spin_lock_irq(&cm.lock);
	list_del(&timewait_info->list);
	spin_unlock_irq(&cm.lock);
3384 3385 3386 3387 3388 3389

	cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
				   timewait_info->work.remote_id);
	if (!cm_id_priv)
		return -EINVAL;

3390
	spin_lock_irq(&cm_id_priv->lock);
3391 3392
	if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
	    cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
3393
		spin_unlock_irq(&cm_id_priv->lock);
3394 3395 3396 3397 3398 3399
		goto out;
	}
	cm_id_priv->id.state = IB_CM_IDLE;
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
3400
	spin_unlock_irq(&cm_id_priv->lock);
3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;
out:
	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
			       struct cm_id_private *cm_id_priv,
			       struct ib_cm_sidr_req_param *param)
{
	cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
3417
			  cm_form_tid(cm_id_priv));
3418 3419 3420 3421 3422 3423
	IBA_SET(CM_SIDR_REQ_REQUESTID, sidr_req_msg,
		be32_to_cpu(cm_id_priv->id.local_id));
	IBA_SET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg,
		be16_to_cpu(param->path->pkey));
	IBA_SET(CM_SIDR_REQ_SERVICEID, sidr_req_msg,
		be64_to_cpu(param->service_id));
3424 3425

	if (param->private_data && param->private_data_len)
3426 3427
		IBA_SET_MEM(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg,
			    param->private_data, param->private_data_len);
3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442
}

int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
			struct ib_cm_sidr_req_param *param)
{
	struct cm_id_private *cm_id_priv;
	struct ib_mad_send_buf *msg;
	unsigned long flags;
	int ret;

	if (!param->path || (param->private_data &&
	     param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
		return -EINVAL;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3443 3444 3445
	ret = cm_init_av_by_path(param->path, param->sgid_attr,
				 &cm_id_priv->av,
				 cm_id_priv);
3446 3447 3448 3449
	if (ret)
		goto out;

	cm_id->service_id = param->service_id;
3450
	cm_id->service_mask = ~cpu_to_be64(0);
3451 3452 3453 3454 3455 3456 3457 3458
	cm_id_priv->timeout_ms = param->timeout_ms;
	cm_id_priv->max_cm_retries = param->max_cm_retries;
	ret = cm_alloc_msg(cm_id_priv, &msg);
	if (ret)
		goto out;

	cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
			   param);
3459
	msg->timeout_ms = cm_id_priv->timeout_ms;
3460 3461 3462 3463
	msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;

	spin_lock_irqsave(&cm_id_priv->lock, flags);
	if (cm_id->state == IB_CM_IDLE)
3464
		ret = ib_post_send_mad(msg, NULL);
3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481
	else
		ret = -EINVAL;

	if (ret) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		cm_free_msg(msg);
		goto out;
	}
	cm_id->state = IB_CM_SIDR_REQ_SENT;
	cm_id_priv->msg = msg;
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
out:
	return ret;
}
EXPORT_SYMBOL(ib_send_cm_sidr_req);

static void cm_format_sidr_req_event(struct cm_work *work,
3482
				     const struct cm_id_private *rx_cm_id,
3483 3484 3485 3486 3487 3488 3489 3490
				     struct ib_cm_id *listen_id)
{
	struct cm_sidr_req_msg *sidr_req_msg;
	struct ib_cm_sidr_req_event_param *param;

	sidr_req_msg = (struct cm_sidr_req_msg *)
				work->mad_recv_wc->recv_buf.mad;
	param = &work->cm_event.param.sidr_req_rcvd;
3491
	param->pkey = IBA_GET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg);
3492
	param->listen_id = listen_id;
3493 3494
	param->service_id =
		cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg));
3495
	param->bth_pkey = cm_get_bth_pkey(work);
3496
	param->port = work->port->port_num;
3497
	param->sgid_attr = rx_cm_id->av.ah_attr.grh.sgid_attr;
3498 3499
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg);
3500 3501 3502 3503 3504 3505 3506 3507
}

static int cm_sidr_req_handler(struct cm_work *work)
{
	struct ib_cm_id *cm_id;
	struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
	struct cm_sidr_req_msg *sidr_req_msg;
	struct ib_wc *wc;
P
Parav Pandit 已提交
3508
	int ret;
3509

3510
	cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
3511 3512 3513 3514 3515 3516 3517 3518
	if (IS_ERR(cm_id))
		return PTR_ERR(cm_id);
	cm_id_priv = container_of(cm_id, struct cm_id_private, id);

	/* Record SGID/SLID and request ID for lookup. */
	sidr_req_msg = (struct cm_sidr_req_msg *)
				work->mad_recv_wc->recv_buf.mad;
	wc = work->mad_recv_wc->wc;
3519
	cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
3520
	cm_id_priv->av.dgid.global.interface_id = 0;
P
Parav Pandit 已提交
3521 3522 3523 3524 3525 3526
	ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
				      work->mad_recv_wc->recv_buf.grh,
				      &cm_id_priv->av);
	if (ret)
		goto out;

3527 3528
	cm_id_priv->id.remote_id =
		cpu_to_be32(IBA_GET(CM_SIDR_REQ_REQUESTID, sidr_req_msg));
3529 3530 3531
	cm_id_priv->tid = sidr_req_msg->hdr.tid;
	atomic_inc(&cm_id_priv->work_count);

3532
	spin_lock_irq(&cm.lock);
3533 3534
	cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
	if (cur_cm_id_priv) {
3535
		spin_unlock_irq(&cm.lock);
3536 3537
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_SIDR_REQ_COUNTER]);
3538 3539
		goto out; /* Duplicate message. */
	}
3540
	cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3541 3542 3543
	cur_cm_id_priv = cm_find_listen(
		cm_id->device,
		cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg)));
3544
	if (!cur_cm_id_priv) {
3545
		spin_unlock_irq(&cm.lock);
3546
		cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
3547 3548
		goto out; /* No match. */
	}
3549 3550
	refcount_inc(&cur_cm_id_priv->refcount);
	refcount_inc(&cm_id_priv->refcount);
3551
	spin_unlock_irq(&cm.lock);
3552 3553 3554

	cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
	cm_id_priv->id.context = cur_cm_id_priv->id.context;
3555 3556
	cm_id_priv->id.service_id =
		cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg));
3557
	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
3558

3559
	cm_format_sidr_req_event(work, cm_id_priv, &cur_cm_id_priv->id);
3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573
	cm_process_work(cm_id_priv, work);
	cm_deref_id(cur_cm_id_priv);
	return 0;
out:
	ib_destroy_cm_id(&cm_id_priv->id);
	return -EINVAL;
}

static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
			       struct cm_id_private *cm_id_priv,
			       struct ib_cm_sidr_rep_param *param)
{
	cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
			  cm_id_priv->tid);
3574 3575 3576
	IBA_SET(CM_SIDR_REP_REQUESTID, sidr_rep_msg,
		be32_to_cpu(cm_id_priv->id.remote_id));
	IBA_SET(CM_SIDR_REP_STATUS, sidr_rep_msg, param->status);
3577
	IBA_SET(CM_SIDR_REP_QPN, sidr_rep_msg, param->qp_num);
3578 3579 3580
	IBA_SET(CM_SIDR_REP_SERVICEID, sidr_rep_msg,
		be64_to_cpu(cm_id_priv->id.service_id));
	IBA_SET(CM_SIDR_REP_Q_KEY, sidr_rep_msg, param->qkey);
3581 3582

	if (param->info && param->info_length)
3583 3584
		IBA_SET_MEM(CM_SIDR_REP_ADDITIONAL_INFORMATION, sidr_rep_msg,
			    param->info, param->info_length);
3585 3586

	if (param->private_data && param->private_data_len)
3587 3588
		IBA_SET_MEM(CM_SIDR_REP_PRIVATE_DATA, sidr_rep_msg,
			    param->private_data, param->private_data_len);
3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616
}

int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
			struct ib_cm_sidr_rep_param *param)
{
	struct cm_id_private *cm_id_priv;
	struct ib_mad_send_buf *msg;
	unsigned long flags;
	int ret;

	if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
	    (param->private_data &&
	     param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
		return -EINVAL;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	spin_lock_irqsave(&cm_id_priv->lock, flags);
	if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
		ret = -EINVAL;
		goto error;
	}

	ret = cm_alloc_msg(cm_id_priv, &msg);
	if (ret)
		goto error;

	cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
			   param);
3617
	ret = ib_post_send_mad(msg, NULL);
3618 3619 3620 3621 3622 3623 3624 3625 3626
	if (ret) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		cm_free_msg(msg);
		return ret;
	}
	cm_id->state = IB_CM_IDLE;
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);

	spin_lock_irqsave(&cm.lock, flags);
3627 3628 3629 3630
	if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
		rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
		RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
	}
3631 3632 3633 3634 3635 3636 3637 3638
	spin_unlock_irqrestore(&cm.lock, flags);
	return 0;

error:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return ret;
}
EXPORT_SYMBOL(ib_send_cm_sidr_rep);

3639 3640
static void cm_format_sidr_rep_event(struct cm_work *work,
				     const struct cm_id_private *cm_id_priv)
3641 3642 3643 3644 3645 3646 3647
{
	struct cm_sidr_rep_msg *sidr_rep_msg;
	struct ib_cm_sidr_rep_event_param *param;

	sidr_rep_msg = (struct cm_sidr_rep_msg *)
				work->mad_recv_wc->recv_buf.mad;
	param = &work->cm_event.param.sidr_rep_rcvd;
3648 3649
	param->status = IBA_GET(CM_SIDR_REP_STATUS, sidr_rep_msg);
	param->qkey = IBA_GET(CM_SIDR_REP_Q_KEY, sidr_rep_msg);
3650
	param->qpn = IBA_GET(CM_SIDR_REP_QPN, sidr_rep_msg);
3651 3652
	param->info = IBA_GET_MEM_PTR(CM_SIDR_REP_ADDITIONAL_INFORMATION,
				      sidr_rep_msg);
3653 3654
	param->info_len = IBA_GET(CM_SIDR_REP_ADDITIONAL_INFORMATION_LENGTH,
				  sidr_rep_msg);
3655
	param->sgid_attr = cm_id_priv->av.ah_attr.grh.sgid_attr;
3656 3657
	work->cm_event.private_data =
		IBA_GET_MEM_PTR(CM_SIDR_REP_PRIVATE_DATA, sidr_rep_msg);
3658 3659 3660 3661 3662 3663 3664 3665 3666
}

static int cm_sidr_rep_handler(struct cm_work *work)
{
	struct cm_sidr_rep_msg *sidr_rep_msg;
	struct cm_id_private *cm_id_priv;

	sidr_rep_msg = (struct cm_sidr_rep_msg *)
				work->mad_recv_wc->recv_buf.mad;
3667 3668
	cm_id_priv = cm_acquire_id(
		cpu_to_be32(IBA_GET(CM_SIDR_REP_REQUESTID, sidr_rep_msg)), 0);
3669 3670 3671
	if (!cm_id_priv)
		return -EINVAL; /* Unmatched reply. */

3672
	spin_lock_irq(&cm_id_priv->lock);
3673
	if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3674
		spin_unlock_irq(&cm_id_priv->lock);
3675 3676 3677
		goto out;
	}
	cm_id_priv->id.state = IB_CM_IDLE;
3678
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3679
	spin_unlock_irq(&cm_id_priv->lock);
3680

3681
	cm_format_sidr_rep_event(work, cm_id_priv);
3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700
	cm_process_work(cm_id_priv, work);
	return 0;
out:
	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

static void cm_process_send_error(struct ib_mad_send_buf *msg,
				  enum ib_wc_status wc_status)
{
	struct cm_id_private *cm_id_priv;
	struct ib_cm_event cm_event;
	enum ib_cm_state state;
	int ret;

	memset(&cm_event, 0, sizeof cm_event);
	cm_id_priv = msg->context[0];

	/* Discard old sends or ones without a response. */
3701
	spin_lock_irq(&cm_id_priv->lock);
3702 3703 3704 3705
	state = (enum ib_cm_state) (unsigned long) msg->context[1];
	if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
		goto discard;

3706 3707
	pr_debug_ratelimited("CM: failed sending MAD in state %d. (%s)\n",
			     state, ib_wc_status_msg(wc_status));
3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729
	switch (state) {
	case IB_CM_REQ_SENT:
	case IB_CM_MRA_REQ_RCVD:
		cm_reset_to_idle(cm_id_priv);
		cm_event.event = IB_CM_REQ_ERROR;
		break;
	case IB_CM_REP_SENT:
	case IB_CM_MRA_REP_RCVD:
		cm_reset_to_idle(cm_id_priv);
		cm_event.event = IB_CM_REP_ERROR;
		break;
	case IB_CM_DREQ_SENT:
		cm_enter_timewait(cm_id_priv);
		cm_event.event = IB_CM_DREQ_ERROR;
		break;
	case IB_CM_SIDR_REQ_SENT:
		cm_id_priv->id.state = IB_CM_IDLE;
		cm_event.event = IB_CM_SIDR_REQ_ERROR;
		break;
	default:
		goto discard;
	}
3730
	spin_unlock_irq(&cm_id_priv->lock);
3731 3732 3733 3734 3735 3736 3737 3738 3739
	cm_event.param.send_status = wc_status;

	/* No other events can occur on the cm_id at this point. */
	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
	cm_free_msg(msg);
	if (ret)
		ib_destroy_cm_id(&cm_id_priv->id);
	return;
discard:
3740
	spin_unlock_irq(&cm_id_priv->lock);
3741 3742 3743 3744 3745 3746
	cm_free_msg(msg);
}

static void cm_send_handler(struct ib_mad_agent *mad_agent,
			    struct ib_mad_send_wc *mad_send_wc)
{
3747
	struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768
	struct cm_port *port;
	u16 attr_index;

	port = mad_agent->context;
	attr_index = be16_to_cpu(((struct ib_mad_hdr *)
				  msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;

	/*
	 * If the send was in response to a received message (context[0] is not
	 * set to a cm_id), and is not a REJ, then it is a send that was
	 * manually retried.
	 */
	if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
		msg->retries = 1;

	atomic_long_add(1 + msg->retries,
			&port->counter_group[CM_XMIT].counter[attr_index]);
	if (msg->retries)
		atomic_long_add(msg->retries,
				&port->counter_group[CM_XMIT_RETRIES].
				counter[attr_index]);
3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783

	switch (mad_send_wc->status) {
	case IB_WC_SUCCESS:
	case IB_WC_WR_FLUSH_ERR:
		cm_free_msg(msg);
		break;
	default:
		if (msg->context[0] && msg->context[1])
			cm_process_send_error(msg, mad_send_wc->status);
		else
			cm_free_msg(msg);
		break;
	}
}

D
David Howells 已提交
3784
static void cm_work_handler(struct work_struct *_work)
3785
{
D
David Howells 已提交
3786
	struct cm_work *work = container_of(_work, struct cm_work, work.work);
3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829
	int ret;

	switch (work->cm_event.event) {
	case IB_CM_REQ_RECEIVED:
		ret = cm_req_handler(work);
		break;
	case IB_CM_MRA_RECEIVED:
		ret = cm_mra_handler(work);
		break;
	case IB_CM_REJ_RECEIVED:
		ret = cm_rej_handler(work);
		break;
	case IB_CM_REP_RECEIVED:
		ret = cm_rep_handler(work);
		break;
	case IB_CM_RTU_RECEIVED:
		ret = cm_rtu_handler(work);
		break;
	case IB_CM_USER_ESTABLISHED:
		ret = cm_establish_handler(work);
		break;
	case IB_CM_DREQ_RECEIVED:
		ret = cm_dreq_handler(work);
		break;
	case IB_CM_DREP_RECEIVED:
		ret = cm_drep_handler(work);
		break;
	case IB_CM_SIDR_REQ_RECEIVED:
		ret = cm_sidr_req_handler(work);
		break;
	case IB_CM_SIDR_REP_RECEIVED:
		ret = cm_sidr_rep_handler(work);
		break;
	case IB_CM_LAP_RECEIVED:
		ret = cm_lap_handler(work);
		break;
	case IB_CM_APR_RECEIVED:
		ret = cm_apr_handler(work);
		break;
	case IB_CM_TIMEWAIT_EXIT:
		ret = cm_timewait_handler(work);
		break;
	default:
D
Daniel Jurgens 已提交
3830
		pr_debug("cm_event.event: 0x%x\n", work->cm_event.event);
3831 3832 3833 3834 3835 3836 3837
		ret = -EINVAL;
		break;
	}
	if (ret)
		cm_free_work(work);
}

3838
static int cm_establish(struct ib_cm_id *cm_id)
3839 3840 3841 3842 3843
{
	struct cm_id_private *cm_id_priv;
	struct cm_work *work;
	unsigned long flags;
	int ret = 0;
3844 3845 3846 3847 3848
	struct cm_device *cm_dev;

	cm_dev = ib_get_client_data(cm_id->device, &cm_client);
	if (!cm_dev)
		return -ENODEV;
3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865

	work = kmalloc(sizeof *work, GFP_ATOMIC);
	if (!work)
		return -ENOMEM;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	spin_lock_irqsave(&cm_id_priv->lock, flags);
	switch (cm_id->state)
	{
	case IB_CM_REP_SENT:
	case IB_CM_MRA_REP_RCVD:
		cm_id->state = IB_CM_ESTABLISHED;
		break;
	case IB_CM_ESTABLISHED:
		ret = -EISCONN;
		break;
	default:
D
Daniel Jurgens 已提交
3866 3867
		pr_debug("%s: local_id %d, cm_id->state: %d\n", __func__,
			 be32_to_cpu(cm_id->local_id), cm_id->state);
3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
		ret = -EINVAL;
		break;
	}
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);

	if (ret) {
		kfree(work);
		goto out;
	}

	/*
	 * The CM worker thread may try to destroy the cm_id before it
	 * can execute this work item.  To prevent potential deadlock,
	 * we need to find the cm_id once we're in the context of the
	 * worker thread, rather than holding a reference on it.
	 */
D
David Howells 已提交
3884
	INIT_DELAYED_WORK(&work->work, cm_work_handler);
3885 3886 3887 3888
	work->local_id = cm_id->local_id;
	work->remote_id = cm_id->remote_id;
	work->mad_recv_wc = NULL;
	work->cm_event.event = IB_CM_USER_ESTABLISHED;
3889 3890

	/* Check if the device started its remove_one */
3891
	spin_lock_irqsave(&cm.lock, flags);
3892 3893 3894 3895 3896 3897
	if (!cm_dev->going_down) {
		queue_delayed_work(cm.wq, &work->work, 0);
	} else {
		kfree(work);
		ret = -ENODEV;
	}
3898
	spin_unlock_irqrestore(&cm.lock, flags);
3899

3900 3901 3902
out:
	return ret;
}
3903 3904 3905 3906

static int cm_migrate(struct ib_cm_id *cm_id)
{
	struct cm_id_private *cm_id_priv;
3907
	struct cm_av tmp_av;
3908
	unsigned long flags;
3909
	int tmp_send_port_not_ready;
3910 3911 3912 3913 3914 3915 3916 3917
	int ret = 0;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	spin_lock_irqsave(&cm_id_priv->lock, flags);
	if (cm_id->state == IB_CM_ESTABLISHED &&
	    (cm_id->lap_state == IB_CM_LAP_UNINIT ||
	     cm_id->lap_state == IB_CM_LAP_IDLE)) {
		cm_id->lap_state = IB_CM_LAP_IDLE;
3918 3919
		/* Swap address vector */
		tmp_av = cm_id_priv->av;
3920
		cm_id_priv->av = cm_id_priv->alt_av;
3921 3922 3923 3924 3925
		cm_id_priv->alt_av = tmp_av;
		/* Swap port send ready state */
		tmp_send_port_not_ready = cm_id_priv->prim_send_port_not_ready;
		cm_id_priv->prim_send_port_not_ready = cm_id_priv->altr_send_port_not_ready;
		cm_id_priv->altr_send_port_not_ready = tmp_send_port_not_ready;
3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949
	} else
		ret = -EINVAL;
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);

	return ret;
}

int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
{
	int ret;

	switch (event) {
	case IB_EVENT_COMM_EST:
		ret = cm_establish(cm_id);
		break;
	case IB_EVENT_PATH_MIG:
		ret = cm_migrate(cm_id);
		break;
	default:
		ret = -EINVAL;
	}
	return ret;
}
EXPORT_SYMBOL(ib_cm_notify);
3950 3951

static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3952
			    struct ib_mad_send_buf *send_buf,
3953 3954
			    struct ib_mad_recv_wc *mad_recv_wc)
{
3955
	struct cm_port *port = mad_agent->context;
3956 3957
	struct cm_work *work;
	enum ib_cm_event_type event;
3958
	bool alt_path = false;
3959
	u16 attr_id;
3960
	int paths = 0;
3961
	int going_down = 0;
3962 3963 3964

	switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
	case CM_REQ_ATTR_ID:
3965 3966 3967
		alt_path = cm_req_has_alt_path((struct cm_req_msg *)
						mad_recv_wc->recv_buf.mad);
		paths = 1 + (alt_path != 0);
3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005
		event = IB_CM_REQ_RECEIVED;
		break;
	case CM_MRA_ATTR_ID:
		event = IB_CM_MRA_RECEIVED;
		break;
	case CM_REJ_ATTR_ID:
		event = IB_CM_REJ_RECEIVED;
		break;
	case CM_REP_ATTR_ID:
		event = IB_CM_REP_RECEIVED;
		break;
	case CM_RTU_ATTR_ID:
		event = IB_CM_RTU_RECEIVED;
		break;
	case CM_DREQ_ATTR_ID:
		event = IB_CM_DREQ_RECEIVED;
		break;
	case CM_DREP_ATTR_ID:
		event = IB_CM_DREP_RECEIVED;
		break;
	case CM_SIDR_REQ_ATTR_ID:
		event = IB_CM_SIDR_REQ_RECEIVED;
		break;
	case CM_SIDR_REP_ATTR_ID:
		event = IB_CM_SIDR_REP_RECEIVED;
		break;
	case CM_LAP_ATTR_ID:
		paths = 1;
		event = IB_CM_LAP_RECEIVED;
		break;
	case CM_APR_ATTR_ID:
		event = IB_CM_APR_RECEIVED;
		break;
	default:
		ib_free_recv_mad(mad_recv_wc);
		return;
	}

4006 4007 4008 4009
	attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
	atomic_long_inc(&port->counter_group[CM_RECV].
			counter[attr_id - CM_ATTR_ID_OFFSET]);

4010
	work = kmalloc(struct_size(work, path, paths), GFP_KERNEL);
4011 4012 4013 4014 4015
	if (!work) {
		ib_free_recv_mad(mad_recv_wc);
		return;
	}

D
David Howells 已提交
4016
	INIT_DELAYED_WORK(&work->work, cm_work_handler);
4017 4018
	work->cm_event.event = event;
	work->mad_recv_wc = mad_recv_wc;
4019
	work->port = port;
4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032

	/* Check if the device started its remove_one */
	spin_lock_irq(&cm.lock);
	if (!port->cm_dev->going_down)
		queue_delayed_work(cm.wq, &work->work, 0);
	else
		going_down = 1;
	spin_unlock_irq(&cm.lock);

	if (going_down) {
		kfree(work);
		ib_free_recv_mad(mad_recv_wc);
	}
4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054
}

static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
				struct ib_qp_attr *qp_attr,
				int *qp_attr_mask)
{
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&cm_id_priv->lock, flags);
	switch (cm_id_priv->id.state) {
	case IB_CM_REQ_SENT:
	case IB_CM_MRA_REQ_RCVD:
	case IB_CM_REQ_RCVD:
	case IB_CM_MRA_REQ_SENT:
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
	case IB_CM_REP_SENT:
	case IB_CM_MRA_REP_RCVD:
	case IB_CM_ESTABLISHED:
		*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
				IB_QP_PKEY_INDEX | IB_QP_PORT;
4055
		qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
4056
		if (cm_id_priv->responder_resources)
4057 4058
			qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
						    IB_ACCESS_REMOTE_ATOMIC;
4059 4060 4061 4062 4063
		qp_attr->pkey_index = cm_id_priv->av.pkey_index;
		qp_attr->port_num = cm_id_priv->av.port->port_num;
		ret = 0;
		break;
	default:
D
Daniel Jurgens 已提交
4064 4065 4066
		pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
			 __func__, be32_to_cpu(cm_id_priv->id.local_id),
			 cm_id_priv->id.state);
4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090
		ret = -EINVAL;
		break;
	}
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return ret;
}

static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
			       struct ib_qp_attr *qp_attr,
			       int *qp_attr_mask)
{
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&cm_id_priv->lock, flags);
	switch (cm_id_priv->id.state) {
	case IB_CM_REQ_RCVD:
	case IB_CM_MRA_REQ_SENT:
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
	case IB_CM_REP_SENT:
	case IB_CM_MRA_REP_RCVD:
	case IB_CM_ESTABLISHED:
		*qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
4091
				IB_QP_DEST_QPN | IB_QP_RQ_PSN;
4092 4093 4094 4095
		qp_attr->ah_attr = cm_id_priv->av.ah_attr;
		qp_attr->path_mtu = cm_id_priv->path_mtu;
		qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
		qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
S
Sean Hefty 已提交
4096 4097
		if (cm_id_priv->qp_type == IB_QPT_RC ||
		    cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
4098 4099 4100 4101 4102 4103
			*qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
					 IB_QP_MIN_RNR_TIMER;
			qp_attr->max_dest_rd_atomic =
					cm_id_priv->responder_resources;
			qp_attr->min_rnr_timer = 0;
		}
4104
		if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
4105
			*qp_attr_mask |= IB_QP_ALT_PATH;
4106
			qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
4107
			qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
4108
			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4109 4110 4111 4112 4113
			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
		}
		ret = 0;
		break;
	default:
D
Daniel Jurgens 已提交
4114 4115 4116
		pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
			 __func__, be32_to_cpu(cm_id_priv->id.local_id),
			 cm_id_priv->id.state);
4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132
		ret = -EINVAL;
		break;
	}
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return ret;
}

static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
			       struct ib_qp_attr *qp_attr,
			       int *qp_attr_mask)
{
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&cm_id_priv->lock, flags);
	switch (cm_id_priv->id.state) {
4133 4134 4135 4136
	/* Allow transition to RTS before sending REP */
	case IB_CM_REQ_RCVD:
	case IB_CM_MRA_REQ_SENT:

4137 4138 4139 4140 4141
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
	case IB_CM_REP_SENT:
	case IB_CM_MRA_REP_RCVD:
	case IB_CM_ESTABLISHED:
4142 4143 4144
		if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
			*qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
			qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
S
Sean Hefty 已提交
4145 4146 4147 4148
			switch (cm_id_priv->qp_type) {
			case IB_QPT_RC:
			case IB_QPT_XRC_INI:
				*qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
4149 4150 4151
						 IB_QP_MAX_QP_RD_ATOMIC;
				qp_attr->retry_cnt = cm_id_priv->retry_count;
				qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
S
Sean Hefty 已提交
4152 4153 4154 4155 4156 4157 4158 4159
				qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
				/* fall through */
			case IB_QPT_XRC_TGT:
				*qp_attr_mask |= IB_QP_TIMEOUT;
				qp_attr->timeout = cm_id_priv->av.timeout;
				break;
			default:
				break;
4160
			}
4161
			if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
4162 4163 4164 4165 4166 4167 4168
				*qp_attr_mask |= IB_QP_PATH_MIG_STATE;
				qp_attr->path_mig_state = IB_MIG_REARM;
			}
		} else {
			*qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
			qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
			qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
4169
			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4170
			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
4171 4172 4173 4174 4175
			qp_attr->path_mig_state = IB_MIG_REARM;
		}
		ret = 0;
		break;
	default:
D
Daniel Jurgens 已提交
4176 4177 4178
		pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
			 __func__, be32_to_cpu(cm_id_priv->id.local_id),
			 cm_id_priv->id.state);
4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211
		ret = -EINVAL;
		break;
	}
	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return ret;
}

int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
		       struct ib_qp_attr *qp_attr,
		       int *qp_attr_mask)
{
	struct cm_id_private *cm_id_priv;
	int ret;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	switch (qp_attr->qp_state) {
	case IB_QPS_INIT:
		ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
		break;
	case IB_QPS_RTR:
		ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
		break;
	case IB_QPS_RTS:
		ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
		break;
	default:
		ret = -EINVAL;
		break;
	}
	return ret;
}
EXPORT_SYMBOL(ib_cm_init_qp_attr);

4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224
static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
			       char *buf)
{
	struct cm_counter_group *group;
	struct cm_counter_attribute *cm_attr;

	group = container_of(obj, struct cm_counter_group, obj);
	cm_attr = container_of(attr, struct cm_counter_attribute, attr);

	return sprintf(buf, "%ld\n",
		       atomic_long_read(&group->counter[cm_attr->index]));
}

4225
static const struct sysfs_ops cm_counter_ops = {
4226 4227 4228 4229 4230 4231 4232 4233
	.show = cm_show_counter
};

static struct kobj_type cm_counter_obj_type = {
	.sysfs_ops = &cm_counter_ops,
	.default_attrs = cm_counter_default_attrs
};

4234
static char *cm_devnode(struct device *dev, umode_t *mode)
4235
{
4236 4237
	if (mode)
		*mode = 0666;
4238 4239 4240
	return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
}

4241
struct class cm_class = {
4242
	.owner   = THIS_MODULE,
4243
	.name    = "infiniband_cm",
4244
	.devnode = cm_devnode,
4245 4246 4247 4248 4249 4250 4251 4252
};
EXPORT_SYMBOL(cm_class);

static int cm_create_port_fs(struct cm_port *port)
{
	int i, ret;

	for (i = 0; i < CM_COUNTER_GROUPS; i++) {
4253 4254 4255 4256 4257
		ret = ib_port_register_module_stat(port->cm_dev->ib_device,
						   port->port_num,
						   &port->counter_group[i].obj,
						   &cm_counter_obj_type,
						   counter_group_names[i]);
4258 4259 4260 4261 4262 4263 4264 4265
		if (ret)
			goto error;
	}

	return 0;

error:
	while (i--)
4266
		ib_port_unregister_module_stat(&port->counter_group[i].obj);
4267 4268 4269 4270 4271 4272 4273 4274 4275
	return ret;

}

static void cm_remove_port_fs(struct cm_port *port)
{
	int i;

	for (i = 0; i < CM_COUNTER_GROUPS; i++)
4276
		ib_port_unregister_module_stat(&port->counter_group[i].obj);
4277 4278 4279

}

4280
static void cm_add_one(struct ib_device *ib_device)
4281 4282 4283 4284 4285
{
	struct cm_device *cm_dev;
	struct cm_port *port;
	struct ib_mad_reg_req reg_req = {
		.mgmt_class = IB_MGMT_CLASS_CM,
4286
		.mgmt_class_version = IB_CM_CLASS_VERSION,
4287 4288 4289 4290 4291 4292
	};
	struct ib_port_modify port_modify = {
		.set_port_cap_mask = IB_PORT_CM_SUP
	};
	unsigned long flags;
	int ret;
M
Michael Wang 已提交
4293
	int count = 0;
4294 4295
	u8 i;

4296 4297
	cm_dev = kzalloc(struct_size(cm_dev, port, ib_device->phys_port_cnt),
			 GFP_KERNEL);
4298 4299 4300
	if (!cm_dev)
		return;

4301
	cm_dev->ib_device = ib_device;
4302
	cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
4303
	cm_dev->going_down = 0;
4304

4305
	set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
4306
	for (i = 1; i <= ib_device->phys_port_cnt; i++) {
4307
		if (!rdma_cap_ib_cm(ib_device, i))
M
Michael Wang 已提交
4308 4309
			continue;

4310 4311 4312 4313 4314
		port = kzalloc(sizeof *port, GFP_KERNEL);
		if (!port)
			goto error1;

		cm_dev->port[i-1] = port;
4315 4316
		port->cm_dev = cm_dev;
		port->port_num = i;
4317

4318 4319 4320
		INIT_LIST_HEAD(&port->cm_priv_prim_list);
		INIT_LIST_HEAD(&port->cm_priv_altr_list);

4321 4322 4323 4324
		ret = cm_create_port_fs(port);
		if (ret)
			goto error1;

4325
		port->mad_agent = ib_register_mad_agent(ib_device, i,
4326 4327 4328 4329 4330
							IB_QPT_GSI,
							&reg_req,
							0,
							cm_send_handler,
							cm_recv_handler,
4331 4332
							port,
							0);
4333
		if (IS_ERR(port->mad_agent))
4334
			goto error2;
4335

4336
		ret = ib_modify_port(ib_device, i, 0, &port_modify);
4337
		if (ret)
4338
			goto error3;
M
Michael Wang 已提交
4339 4340

		count++;
4341
	}
M
Michael Wang 已提交
4342 4343 4344 4345

	if (!count)
		goto free;

4346
	ib_set_client_data(ib_device, &cm_client, cm_dev);
4347 4348 4349 4350 4351 4352

	write_lock_irqsave(&cm.device_lock, flags);
	list_add_tail(&cm_dev->list, &cm.device_list);
	write_unlock_irqrestore(&cm.device_lock, flags);
	return;

4353
error3:
4354
	ib_unregister_mad_agent(port->mad_agent);
4355 4356
error2:
	cm_remove_port_fs(port);
4357
error1:
4358 4359
	port_modify.set_port_cap_mask = 0;
	port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
4360
	kfree(port);
4361
	while (--i) {
4362
		if (!rdma_cap_ib_cm(ib_device, i))
M
Michael Wang 已提交
4363 4364
			continue;

4365
		port = cm_dev->port[i-1];
4366
		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4367
		ib_unregister_mad_agent(port->mad_agent);
4368
		cm_remove_port_fs(port);
4369
		kfree(port);
4370
	}
M
Michael Wang 已提交
4371
free:
4372
	kfree(cm_dev);
4373 4374
}

4375
static void cm_remove_one(struct ib_device *ib_device, void *client_data)
4376
{
4377
	struct cm_device *cm_dev = client_data;
4378
	struct cm_port *port;
4379 4380
	struct cm_id_private *cm_id_priv;
	struct ib_mad_agent *cur_mad_agent;
4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393
	struct ib_port_modify port_modify = {
		.clr_port_cap_mask = IB_PORT_CM_SUP
	};
	unsigned long flags;
	int i;

	if (!cm_dev)
		return;

	write_lock_irqsave(&cm.device_lock, flags);
	list_del(&cm_dev->list);
	write_unlock_irqrestore(&cm.device_lock, flags);

4394 4395 4396 4397
	spin_lock_irq(&cm.lock);
	cm_dev->going_down = 1;
	spin_unlock_irq(&cm.lock);

4398
	for (i = 1; i <= ib_device->phys_port_cnt; i++) {
4399
		if (!rdma_cap_ib_cm(ib_device, i))
M
Michael Wang 已提交
4400 4401
			continue;

4402
		port = cm_dev->port[i-1];
4403
		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4404 4405 4406 4407 4408 4409 4410
		/* Mark all the cm_id's as not valid */
		spin_lock_irq(&cm.lock);
		list_for_each_entry(cm_id_priv, &port->cm_priv_altr_list, altr_list)
			cm_id_priv->altr_send_port_not_ready = 1;
		list_for_each_entry(cm_id_priv, &port->cm_priv_prim_list, prim_list)
			cm_id_priv->prim_send_port_not_ready = 1;
		spin_unlock_irq(&cm.lock);
4411 4412 4413 4414 4415
		/*
		 * We flush the queue here after the going_down set, this
		 * verify that no new works will be queued in the recv handler,
		 * after that we can call the unregister_mad_agent
		 */
4416
		flush_workqueue(cm.wq);
4417 4418 4419 4420 4421
		spin_lock_irq(&cm.state_lock);
		cur_mad_agent = port->mad_agent;
		port->mad_agent = NULL;
		spin_unlock_irq(&cm.state_lock);
		ib_unregister_mad_agent(cur_mad_agent);
4422
		cm_remove_port_fs(port);
4423
		kfree(port);
4424
	}
4425

4426
	kfree(cm_dev);
4427 4428
}

4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505
/*
 * Check at compile time that the byte offset and length of field old_name in
 * the struct matches the byte offset and length in the new macro.
 */
#define _IBA_CHECK_OFF(old_name, field_struct, field_offset, mask, bits) \
	static_assert(offsetof(field_struct, old_name) == (field_offset));     \
	static_assert(bits == sizeof(((field_struct *)0)->old_name) * 8)
#define IBA_CHECK_OFF(field, old_name) _IBA_CHECK_OFF(old_name, field)

IBA_CHECK_OFF(CM_REQ_LOCAL_COMM_ID, local_comm_id);
IBA_CHECK_OFF(CM_REQ_SERVICE_ID, service_id);
IBA_CHECK_OFF(CM_REQ_LOCAL_CA_GUID, local_ca_guid);
IBA_CHECK_OFF(CM_REQ_LOCAL_Q_KEY, local_qkey);
IBA_CHECK_OFF(CM_REQ_PARTITION_KEY, pkey);
IBA_CHECK_OFF(CM_REQ_PRIMARY_LOCAL_PORT_LID, primary_local_lid);
IBA_CHECK_OFF(CM_REQ_PRIMARY_REMOTE_PORT_LID, primary_remote_lid);
IBA_CHECK_OFF(CM_REQ_PRIMARY_LOCAL_PORT_GID, primary_local_gid);
IBA_CHECK_OFF(CM_REQ_PRIMARY_REMOTE_PORT_GID, primary_remote_gid);
IBA_CHECK_OFF(CM_REQ_PRIMARY_TRAFFIC_CLASS, primary_traffic_class);
IBA_CHECK_OFF(CM_REQ_PRIMARY_HOP_LIMIT, primary_hop_limit);
IBA_CHECK_OFF(CM_REQ_ALTERNATE_LOCAL_PORT_LID, alt_local_lid);
IBA_CHECK_OFF(CM_REQ_ALTERNATE_REMOTE_PORT_LID, alt_remote_lid);
IBA_CHECK_OFF(CM_REQ_ALTERNATE_LOCAL_PORT_GID, alt_local_gid);
IBA_CHECK_OFF(CM_REQ_ALTERNATE_REMOTE_PORT_GID, alt_remote_gid);
IBA_CHECK_OFF(CM_REQ_ALTERNATE_TRAFFIC_CLASS, alt_traffic_class);
IBA_CHECK_OFF(CM_REQ_ALTERNATE_HOP_LIMIT, alt_hop_limit);
IBA_CHECK_OFF(CM_REQ_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_MRA_LOCAL_COMM_ID, local_comm_id);
IBA_CHECK_OFF(CM_MRA_REMOTE_COMM_ID, remote_comm_id);
IBA_CHECK_OFF(CM_MRA_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_REJ_LOCAL_COMM_ID, local_comm_id);
IBA_CHECK_OFF(CM_REJ_REMOTE_COMM_ID, remote_comm_id);
IBA_CHECK_OFF(CM_REJ_REASON, reason);
IBA_CHECK_OFF(CM_REJ_ARI, ari);
IBA_CHECK_OFF(CM_REJ_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_REP_LOCAL_COMM_ID, local_comm_id);
IBA_CHECK_OFF(CM_REP_REMOTE_COMM_ID, remote_comm_id);
IBA_CHECK_OFF(CM_REP_LOCAL_Q_KEY, local_qkey);
IBA_CHECK_OFF(CM_REP_RESPONDER_RESOURCES, resp_resources);
IBA_CHECK_OFF(CM_REP_INITIATOR_DEPTH, initiator_depth);
IBA_CHECK_OFF(CM_REP_LOCAL_CA_GUID, local_ca_guid);
IBA_CHECK_OFF(CM_REP_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_RTU_LOCAL_COMM_ID, local_comm_id);
IBA_CHECK_OFF(CM_RTU_REMOTE_COMM_ID, remote_comm_id);
IBA_CHECK_OFF(CM_RTU_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_DREQ_LOCAL_COMM_ID, local_comm_id);
IBA_CHECK_OFF(CM_DREQ_REMOTE_COMM_ID, remote_comm_id);
IBA_CHECK_OFF(CM_DREQ_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_DREP_LOCAL_COMM_ID, local_comm_id);
IBA_CHECK_OFF(CM_DREP_REMOTE_COMM_ID, remote_comm_id);
IBA_CHECK_OFF(CM_DREP_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_LAP_LOCAL_COMM_ID, local_comm_id);
IBA_CHECK_OFF(CM_LAP_REMOTE_COMM_ID, remote_comm_id);
IBA_CHECK_OFF(CM_LAP_ALTERNATE_LOCAL_PORT_LID, alt_local_lid);
IBA_CHECK_OFF(CM_LAP_ALTERNATE_REMOTE_PORT_LID, alt_remote_lid);
IBA_CHECK_OFF(CM_LAP_ALTERNATE_LOCAL_PORT_GID, alt_local_gid);
IBA_CHECK_OFF(CM_LAP_ALTERNATE_REMOTE_PORT_GID, alt_remote_gid);
IBA_CHECK_OFF(CM_LAP_ALTERNATE_HOP_LIMIT, alt_hop_limit);
IBA_CHECK_OFF(CM_LAP_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_APR_LOCAL_COMM_ID, local_comm_id);
IBA_CHECK_OFF(CM_APR_REMOTE_COMM_ID, remote_comm_id);
IBA_CHECK_OFF(CM_APR_ADDITIONAL_INFORMATION_LENGTH, info_length);
IBA_CHECK_OFF(CM_APR_AR_STATUS, ap_status);
IBA_CHECK_OFF(CM_APR_ADDITIONAL_INFORMATION, info);
IBA_CHECK_OFF(CM_APR_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_SIDR_REQ_REQUESTID, request_id);
IBA_CHECK_OFF(CM_SIDR_REQ_PARTITION_KEY, pkey);
IBA_CHECK_OFF(CM_SIDR_REQ_SERVICEID, service_id);
IBA_CHECK_OFF(CM_SIDR_REQ_PRIVATE_DATA, private_data);
IBA_CHECK_OFF(CM_SIDR_REP_REQUESTID, request_id);
IBA_CHECK_OFF(CM_SIDR_REP_STATUS, status);
IBA_CHECK_OFF(CM_SIDR_REP_ADDITIONAL_INFORMATION_LENGTH, info_length);
IBA_CHECK_OFF(CM_SIDR_REP_SERVICEID, service_id);
IBA_CHECK_OFF(CM_SIDR_REP_Q_KEY, qkey);
IBA_CHECK_OFF(CM_SIDR_REP_ADDITIONAL_INFORMATION, info);
IBA_CHECK_OFF(CM_SIDR_REP_PRIVATE_DATA, private_data);

4506 4507 4508 4509 4510 4511 4512
static int __init ib_cm_init(void)
{
	int ret;

	INIT_LIST_HEAD(&cm.device_list);
	rwlock_init(&cm.device_lock);
	spin_lock_init(&cm.lock);
4513
	spin_lock_init(&cm.state_lock);
4514
	cm.listen_service_table = RB_ROOT;
4515
	cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
4516 4517 4518
	cm.remote_id_table = RB_ROOT;
	cm.remote_qp_table = RB_ROOT;
	cm.remote_sidr_table = RB_ROOT;
4519
	xa_init_flags(&cm.local_id_table, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
S
Sean Hefty 已提交
4520
	get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
4521
	INIT_LIST_HEAD(&cm.timewait_list);
4522

4523
	ret = class_register(&cm_class);
4524 4525 4526 4527
	if (ret) {
		ret = -ENOMEM;
		goto error1;
	}
4528

4529
	cm.wq = alloc_workqueue("ib_cm", 0, 1);
4530 4531
	if (!cm.wq) {
		ret = -ENOMEM;
4532
		goto error2;
4533 4534
	}

4535 4536
	ret = ib_register_client(&cm_client);
	if (ret)
4537
		goto error3;
4538 4539

	return 0;
4540
error3:
4541
	destroy_workqueue(cm.wq);
4542
error2:
4543
	class_unregister(&cm_class);
4544
error1:
4545 4546 4547 4548 4549
	return ret;
}

static void __exit ib_cm_cleanup(void)
{
4550 4551 4552 4553 4554 4555 4556
	struct cm_timewait_info *timewait_info, *tmp;

	spin_lock_irq(&cm.lock);
	list_for_each_entry(timewait_info, &cm.timewait_list, list)
		cancel_delayed_work(&timewait_info->work.work);
	spin_unlock_irq(&cm.lock);

4557
	ib_unregister_client(&cm_client);
4558
	destroy_workqueue(cm.wq);
4559 4560 4561 4562 4563 4564

	list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
		list_del(&timewait_info->list);
		kfree(timewait_info);
	}

4565
	class_unregister(&cm_class);
4566
	WARN_ON(!xa_empty(&cm.local_id_table));
4567 4568 4569 4570
}

module_init(ib_cm_init);
module_exit(ib_cm_cleanup);