cm.c 125.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2004-2007 Intel Corporation.  All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * 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.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
S
Sean Hefty 已提交
35 36

#include <linux/completion.h>
37
#include <linux/dma-mapping.h>
38
#include <linux/device.h>
39
#include <linux/module.h>
40 41 42
#include <linux/err.h>
#include <linux/idr.h>
#include <linux/interrupt.h>
S
Sean Hefty 已提交
43
#include <linux/random.h>
44 45
#include <linux/rbtree.h>
#include <linux/spinlock.h>
46
#include <linux/slab.h>
47
#include <linux/sysfs.h>
48
#include <linux/workqueue.h>
49
#include <linux/kdev_t.h>
50
#include <linux/etherdevice.h>
51

52 53
#include <rdma/ib_cache.h>
#include <rdma/ib_cm.h>
54 55 56 57 58 59
#include "cm_msgs.h"

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

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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);

108
static void cm_add_one(struct ib_device *device);
109
static void cm_remove_one(struct ib_device *device, void *client_data);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

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;
	struct idr local_id_table;
S
Sean Hefty 已提交
128
	__be32 random_id_operand;
129
	struct list_head timewait_list;
130
	struct workqueue_struct *wq;
131 132
	/* Sync on cm change port state */
	spinlock_t state_lock;
133 134
} cm;

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/* 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 = { \
178
	.attr = { .name = __stringify(_name), .mode = 0444 }, \
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	.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
};

209 210 211
struct cm_port {
	struct cm_device *cm_dev;
	struct ib_mad_agent *mad_agent;
212
	struct kobject port_obj;
213
	u8 port_num;
214 215
	struct list_head cm_priv_prim_list;
	struct list_head cm_priv_altr_list;
216
	struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
217 218 219 220
};

struct cm_device {
	struct list_head list;
221
	struct ib_device *ib_device;
222
	struct device *device;
223
	u8 ack_delay;
224
	int going_down;
225
	struct cm_port *port[0];
226 227 228 229 230
};

struct cm_av {
	struct cm_port *port;
	union ib_gid dgid;
231
	struct rdma_ah_attr ah_attr;
232
	u16 pkey_index;
233
	u8 timeout;
234 235 236
};

struct cm_work {
D
David Howells 已提交
237
	struct delayed_work work;
238 239 240
	struct list_head list;
	struct cm_port *port;
	struct ib_mad_recv_wc *mad_recv_wc;	/* Received MADs */
241 242
	__be32 local_id;			/* Established / timewait */
	__be32 remote_id;
243
	struct ib_cm_event cm_event;
244
	struct sa_path_rec path[0];
245 246 247 248
};

struct cm_timewait_info {
	struct cm_work work;			/* Must be first. */
249
	struct list_head list;
250 251
	struct rb_node remote_qp_node;
	struct rb_node remote_id_node;
252 253
	__be64 remote_ca_guid;
	__be32 remote_qpn;
254 255 256 257 258 259 260 261 262
	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;
263
	spinlock_t lock;	/* Do not acquire inside cm.lock */
S
Sean Hefty 已提交
264
	struct completion comp;
265
	atomic_t refcount;
H
Haggai Eran 已提交
266 267 268
	/* Number of clients sharing this ib_cm_id. Only valid for listeners.
	 * Protected by the cm.lock spinlock. */
	int listen_sharecount;
269 270 271 272 273 274 275 276

	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;
277 278 279
	__be64 tid;
	__be32 local_qpn;
	__be32 remote_qpn;
280
	enum ib_qp_type qp_type;
281 282
	__be32 sq_psn;
	__be32 rq_psn;
283 284
	int timeout_ms;
	enum ib_mtu path_mtu;
285
	__be16 pkey;
286 287 288 289 290 291 292 293
	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;
294
	u8 target_ack_delay;
295

296 297 298 299 300 301
	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;

302 303 304 305
	struct list_head work_list;
	atomic_t work_count;
};

D
David Howells 已提交
306
static void cm_work_handler(struct work_struct *work);
307 308 309 310

static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
{
	if (atomic_dec_and_test(&cm_id_priv->refcount))
S
Sean Hefty 已提交
311
		complete(&cm_id_priv->comp);
312 313 314 315 316 317 318 319
}

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;
320 321 322
	struct cm_av *av;
	unsigned long flags, flags2;
	int ret = 0;
323

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	/* 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 */
340
	mad_agent = cm_id_priv->av.port->mad_agent;
341 342 343 344 345
	if (!mad_agent) {
		pr_info("%s: not a valid MAD agent\n", __func__);
		ret = -ENODEV;
		goto out;
	}
346
	ah = rdma_create_ah(mad_agent->qp->pd, &av->ah_attr);
347 348 349 350
	if (IS_ERR(ah)) {
		ret = PTR_ERR(ah);
		goto out;
	}
351

R
Roland Dreier 已提交
352
	m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
353
			       av->pkey_index,
354
			       0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
355 356
			       GFP_ATOMIC,
			       IB_MGMT_BASE_VERSION);
357
	if (IS_ERR(m)) {
358
		rdma_destroy_ah(ah);
359 360
		ret = PTR_ERR(m);
		goto out;
361 362 363
	}

	/* Timeout set by caller if response is expected. */
364 365
	m->ah = ah;
	m->retries = cm_id_priv->max_cm_retries;
366 367 368 369

	atomic_inc(&cm_id_priv->refcount);
	m->context[0] = cm_id_priv;
	*msg = m;
370 371 372 373

out:
	spin_unlock_irqrestore(&cm.state_lock, flags2);
	return ret;
374 375
}

376 377 378 379 380 381 382 383 384 385 386 387
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)
388 389 390 391 392 393 394 395
{
	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);

396
	msg->ah = ah;
397 398 399 400 401
	return 0;
}

static void cm_free_msg(struct ib_mad_send_buf *msg)
{
402 403
	if (msg->ah)
		rdma_destroy_ah(msg->ah);
404 405 406 407 408
	if (msg->context[0])
		cm_deref_id(msg->context[0]);
	ib_free_send_mad(msg);
}

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
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;
}

430 431 432 433 434 435 436 437
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 已提交
438
	data = kmemdup(private_data, private_data_len, GFP_KERNEL);
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	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;
}

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

464
static int cm_init_av_by_path(struct sa_path_rec *path, struct cm_av *av,
465
			      struct cm_id_private *cm_id_priv)
466 467 468 469 470 471
{
	struct cm_device *cm_dev;
	struct cm_port *port = NULL;
	unsigned long flags;
	int ret;
	u8 p;
472
	struct net_device *ndev = ib_get_ndev_from_path(path);
473 474 475

	read_lock_irqsave(&cm.device_lock, flags);
	list_for_each_entry(cm_dev, &cm.device_list, list) {
476
		if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
477 478
					sa_conv_pathrec_to_gid_type(path),
					ndev, &p, NULL)) {
479
			port = cm_dev->port[p-1];
480 481 482 483 484
			break;
		}
	}
	read_unlock_irqrestore(&cm.device_lock, flags);

485 486 487
	if (ndev)
		dev_put(ndev);

488 489 490
	if (!port)
		return -EINVAL;

491
	ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
492 493 494 495 496
				  be16_to_cpu(path->pkey), &av->pkey_index);
	if (ret)
		return ret;

	av->port = port;
497
	ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
S
Sean Hefty 已提交
498
			     &av->ah_attr);
499
	av->timeout = path->packet_life_time + 1;
500

501 502 503 504 505 506 507 508 509 510 511
	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;
512 513 514 515 516
}

static int cm_alloc_id(struct cm_id_private *cm_id_priv)
{
	unsigned long flags;
T
Tejun Heo 已提交
517
	int id;
518

T
Tejun Heo 已提交
519 520 521
	idr_preload(GFP_KERNEL);
	spin_lock_irqsave(&cm.lock, flags);

522
	id = idr_alloc_cyclic(&cm.local_id_table, cm_id_priv, 0, 0, GFP_NOWAIT);
T
Tejun Heo 已提交
523 524 525

	spin_unlock_irqrestore(&cm.lock, flags);
	idr_preload_end();
S
Sean Hefty 已提交
526

527
	cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
T
Tejun Heo 已提交
528
	return id < 0 ? id : 0;
529 530
}

531
static void cm_free_id(__be32 local_id)
532
{
533
	spin_lock_irq(&cm.lock);
S
Sean Hefty 已提交
534 535
	idr_remove(&cm.local_id_table,
		   (__force int) (local_id ^ cm.random_id_operand));
536
	spin_unlock_irq(&cm.lock);
537 538
}

539
static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
540 541 542
{
	struct cm_id_private *cm_id_priv;

S
Sean Hefty 已提交
543 544
	cm_id_priv = idr_find(&cm.local_id_table,
			      (__force int) (local_id ^ cm.random_id_operand));
545 546 547 548 549 550 551 552 553 554
	if (cm_id_priv) {
		if (cm_id_priv->id.remote_id == remote_id)
			atomic_inc(&cm_id_priv->refcount);
		else
			cm_id_priv = NULL;
	}

	return cm_id_priv;
}

555
static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
556 557 558
{
	struct cm_id_private *cm_id_priv;

559
	spin_lock_irq(&cm.lock);
560
	cm_id_priv = cm_get_id(local_id, remote_id);
561
	spin_unlock_irq(&cm.lock);
562 563 564 565

	return cm_id_priv;
}

R
Roland Dreier 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
/*
 * 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;
}

591 592 593 594 595
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;
596 597
	__be64 service_id = cm_id_priv->id.service_id;
	__be64 service_mask = cm_id_priv->id.service_mask;
598 599 600 601 602 603

	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) ==
604
		    (service_mask & cur_cm_id_priv->id.service_id) &&
H
Haggai Eran 已提交
605
		    (cm_id_priv->id.device == cur_cm_id_priv->id.device))
606 607 608 609 610 611
			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 已提交
612
		else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
613
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
614
		else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
615
			link = &(*link)->rb_right;
616 617 618 619 620 621 622 623
		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;
}

624
static struct cm_id_private * cm_find_listen(struct ib_device *device,
H
Haggai Eran 已提交
625
					     __be64 service_id)
626 627 628 629 630 631 632
{
	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) ==
633
		     cm_id_priv->id.service_id &&
H
Haggai Eran 已提交
634
		    (cm_id_priv->id.device == device))
635
			return cm_id_priv;
636 637 638 639 640

		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 已提交
641
		else if (be64_lt(service_id, cm_id_priv->id.service_id))
642
			node = node->rb_left;
R
Roland Dreier 已提交
643
		else if (be64_gt(service_id, cm_id_priv->id.service_id))
644
			node = node->rb_right;
645 646 647 648 649 650 651 652 653 654 655 656
		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;
657 658
	__be64 remote_ca_guid = timewait_info->remote_ca_guid;
	__be32 remote_id = timewait_info->work.remote_id;
659 660 661 662 663

	while (*link) {
		parent = *link;
		cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
					     remote_id_node);
R
Roland Dreier 已提交
664
		if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
665
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
666
		else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
667
			link = &(*link)->rb_right;
R
Roland Dreier 已提交
668
		else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
669
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
670
		else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
671 672 673 674 675 676 677 678 679 680
			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;
}

681 682
static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
						   __be32 remote_id)
683 684 685 686 687 688 689
{
	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 已提交
690
		if (be32_lt(remote_id, timewait_info->work.remote_id))
691
			node = node->rb_left;
R
Roland Dreier 已提交
692
		else if (be32_gt(remote_id, timewait_info->work.remote_id))
693
			node = node->rb_right;
R
Roland Dreier 已提交
694
		else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
695
			node = node->rb_left;
R
Roland Dreier 已提交
696
		else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
697 698 699 700 701 702 703 704 705 706 707 708 709
			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;
710 711
	__be64 remote_ca_guid = timewait_info->remote_ca_guid;
	__be32 remote_qpn = timewait_info->remote_qpn;
712 713 714 715 716

	while (*link) {
		parent = *link;
		cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
					     remote_qp_node);
R
Roland Dreier 已提交
717
		if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
718
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
719
		else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
720
			link = &(*link)->rb_right;
R
Roland Dreier 已提交
721
		else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
722
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
723
		else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
			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;
741
	__be32 remote_id = cm_id_priv->id.remote_id;
742 743 744 745 746

	while (*link) {
		parent = *link;
		cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
					  sidr_id_node);
R
Roland Dreier 已提交
747
		if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
748
			link = &(*link)->rb_left;
R
Roland Dreier 已提交
749
		else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
			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);
}

778 779
struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
				 ib_cm_handler cm_handler,
780 781 782 783 784
				 void *context)
{
	struct cm_id_private *cm_id_priv;
	int ret;

R
Roland Dreier 已提交
785
	cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
786 787 788 789
	if (!cm_id_priv)
		return ERR_PTR(-ENOMEM);

	cm_id_priv->id.state = IB_CM_IDLE;
790
	cm_id_priv->id.device = device;
791 792
	cm_id_priv->id.cm_handler = cm_handler;
	cm_id_priv->id.context = context;
J
John Kingman 已提交
793
	cm_id_priv->id.remote_cm_qpn = 1;
794 795 796 797 798
	ret = cm_alloc_id(cm_id_priv);
	if (ret)
		goto error;

	spin_lock_init(&cm_id_priv->lock);
S
Sean Hefty 已提交
799
	init_completion(&cm_id_priv->comp);
800
	INIT_LIST_HEAD(&cm_id_priv->work_list);
801 802
	INIT_LIST_HEAD(&cm_id_priv->prim_list);
	INIT_LIST_HEAD(&cm_id_priv->altr_list);
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	atomic_set(&cm_id_priv->work_count, -1);
	atomic_set(&cm_id_priv->refcount, 1);
	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);
}

838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
/*
 * 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);
}

857 858 859 860 861 862 863 864 865 866 867 868 869
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;
	}
}

870
static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
871 872 873
{
	struct cm_timewait_info *timewait_info;

R
Roland Dreier 已提交
874
	timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
875 876 877 878
	if (!timewait_info)
		return ERR_PTR(-ENOMEM);

	timewait_info->work.local_id = local_id;
D
David Howells 已提交
879
	INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
880 881 882 883 884 885 886
	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;
887
	unsigned long flags;
888 889 890 891 892
	struct cm_device *cm_dev;

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

894
	spin_lock_irqsave(&cm.lock, flags);
895
	cm_cleanup_timewait(cm_id_priv->timewait_info);
896 897
	list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
	spin_unlock_irqrestore(&cm.lock, flags);
898

899 900 901 902 903 904
	/*
	 * 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;
905
	wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
906 907

	/* Check if the device started its remove_one */
908
	spin_lock_irqsave(&cm.lock, flags);
909 910 911
	if (!cm_dev->going_down)
		queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
				   msecs_to_jiffies(wait_time));
912
	spin_unlock_irqrestore(&cm.lock, flags);
913

914 915 916 917 918
	cm_id_priv->timewait_info = NULL;
}

static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
{
919 920
	unsigned long flags;

921 922
	cm_id_priv->id.state = IB_CM_IDLE;
	if (cm_id_priv->timewait_info) {
923
		spin_lock_irqsave(&cm.lock, flags);
924
		cm_cleanup_timewait(cm_id_priv->timewait_info);
925
		spin_unlock_irqrestore(&cm.lock, flags);
926 927 928 929 930
		kfree(cm_id_priv->timewait_info);
		cm_id_priv->timewait_info = NULL;
	}
}

931
static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
932 933 934 935 936 937
{
	struct cm_id_private *cm_id_priv;
	struct cm_work *work;

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
retest:
938
	spin_lock_irq(&cm_id_priv->lock);
939 940
	switch (cm_id->state) {
	case IB_CM_LISTEN:
941
		spin_unlock_irq(&cm_id_priv->lock);
H
Haggai Eran 已提交
942

943
		spin_lock_irq(&cm.lock);
H
Haggai Eran 已提交
944 945 946 947 948 949
		if (--cm_id_priv->listen_sharecount > 0) {
			/* The id is still shared. */
			cm_deref_id(cm_id_priv);
			spin_unlock_irq(&cm.lock);
			return;
		}
950
		rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
951
		spin_unlock_irq(&cm.lock);
952 953 954
		break;
	case IB_CM_SIDR_REQ_SENT:
		cm_id->state = IB_CM_IDLE;
955
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
956
		spin_unlock_irq(&cm_id_priv->lock);
957 958
		break;
	case IB_CM_SIDR_REQ_RCVD:
959
		spin_unlock_irq(&cm_id_priv->lock);
960
		cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
961 962 963 964 965
		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);
966 967
		break;
	case IB_CM_REQ_SENT:
968
	case IB_CM_MRA_REQ_RCVD:
S
Sean Hefty 已提交
969
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
970
		spin_unlock_irq(&cm_id_priv->lock);
S
Sean Hefty 已提交
971
		ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
972 973
			       &cm_id_priv->id.device->node_guid,
			       sizeof cm_id_priv->id.device->node_guid,
S
Sean Hefty 已提交
974 975
			       NULL, 0);
		break;
976 977 978 979
	case IB_CM_REQ_RCVD:
		if (err == -ENOMEM) {
			/* Do not reject to allow future retries. */
			cm_reset_to_idle(cm_id_priv);
980
			spin_unlock_irq(&cm_id_priv->lock);
981
		} else {
982
			spin_unlock_irq(&cm_id_priv->lock);
983 984 985 986
			ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
				       NULL, 0, NULL, 0);
		}
		break;
987 988
	case IB_CM_REP_SENT:
	case IB_CM_MRA_REP_RCVD:
989
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
990 991 992 993
		/* Fall through */
	case IB_CM_MRA_REQ_SENT:
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
994
		spin_unlock_irq(&cm_id_priv->lock);
S
Sean Hefty 已提交
995 996
		ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
			       NULL, 0, NULL, 0);
997 998
		break;
	case IB_CM_ESTABLISHED:
999
		spin_unlock_irq(&cm_id_priv->lock);
1000 1001
		if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
			break;
1002 1003 1004
		ib_send_cm_dreq(cm_id, NULL, 0);
		goto retest;
	case IB_CM_DREQ_SENT:
1005
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1006
		cm_enter_timewait(cm_id_priv);
1007
		spin_unlock_irq(&cm_id_priv->lock);
1008 1009
		break;
	case IB_CM_DREQ_RCVD:
1010
		spin_unlock_irq(&cm_id_priv->lock);
1011 1012 1013
		ib_send_cm_drep(cm_id, NULL, 0);
		break;
	default:
1014
		spin_unlock_irq(&cm_id_priv->lock);
1015 1016 1017
		break;
	}

1018 1019 1020 1021 1022 1023 1024 1025 1026
	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);

1027
	cm_free_id(cm_id->local_id);
S
Sean Hefty 已提交
1028 1029
	cm_deref_id(cm_id_priv);
	wait_for_completion(&cm_id_priv->comp);
1030 1031
	while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
		cm_free_work(work);
1032
	kfree(cm_id_priv->private_data);
1033 1034
	kfree(cm_id_priv);
}
1035 1036 1037 1038 1039

void ib_destroy_cm_id(struct ib_cm_id *cm_id)
{
	cm_destroy_id(cm_id, 0);
}
1040 1041
EXPORT_SYMBOL(ib_destroy_cm_id);

H
Haggai Eran 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
/**
 * __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 已提交
1056
			  __be64 service_mask)
1057 1058 1059 1060
{
	struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
	int ret = 0;

1061
	service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
1062 1063 1064 1065 1066 1067
	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);
1068 1069 1070
	if (cm_id->state != IB_CM_IDLE)
		return -EINVAL;

1071
	cm_id->state = IB_CM_LISTEN;
H
Haggai Eran 已提交
1072
	++cm_id_priv->listen_sharecount;
H
Haggai Eran 已提交
1073

1074
	if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
1075
		cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
1076
		cm_id->service_mask = ~cpu_to_be64(0);
1077 1078 1079 1080 1081 1082 1083 1084
	} 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 已提交
1085
		--cm_id_priv->listen_sharecount;
1086 1087 1088 1089
		ret = -EBUSY;
	}
	return ret;
}
H
Haggai Eran 已提交
1090

H
Haggai Eran 已提交
1091
int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask)
H
Haggai Eran 已提交
1092
{
H
Haggai Eran 已提交
1093 1094 1095 1096 1097 1098 1099 1100
	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 已提交
1101
}
1102 1103
EXPORT_SYMBOL(ib_cm_listen);

H
Haggai Eran 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
/**
 * 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 已提交
1140
	cm_id_priv = cm_find_listen(device, service_id);
H
Haggai Eran 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
	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);
		}
		atomic_inc(&cm_id_priv->refcount);
		++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 已提交
1159
	err = __ib_cm_listen(cm_id, service_id, 0);
H
Haggai Eran 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

	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);

1171 1172
static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
			  enum cm_msg_sequence msg_seq)
1173 1174 1175 1176
{
	u64 hi_tid, low_tid;

	hi_tid   = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
1177 1178
	low_tid  = (u64) ((__force u32)cm_id_priv->id.local_id |
			  (msg_seq << 30));
1179 1180 1181 1182
	return cpu_to_be64(hi_tid | low_tid);
}

static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
1183
			      __be16 attr_id, __be64 tid)
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
{
	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)
{
1197 1198
	struct sa_path_rec *pri_path = param->primary_path;
	struct sa_path_rec *alt_path = param->alternate_path;
1199 1200 1201 1202 1203
	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);
1204

1205 1206 1207 1208 1209
	cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
			  cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));

	req_msg->local_comm_id = cm_id_priv->id.local_id;
	req_msg->service_id = param->service_id;
1210
	req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
	cm_req_set_init_depth(req_msg, param->initiator_depth);
	cm_req_set_remote_resp_timeout(req_msg,
				       param->remote_cm_response_timeout);
	cm_req_set_qp_type(req_msg, param->qp_type);
	cm_req_set_flow_ctrl(req_msg, param->flow_control);
	cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
	cm_req_set_local_resp_timeout(req_msg,
				      param->local_cm_response_timeout);
	req_msg->pkey = param->primary_path->pkey;
	cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
	cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
S
Sean Hefty 已提交
1223 1224 1225 1226 1227 1228 1229

	if (param->qp_type != IB_QPT_XRC_INI) {
		cm_req_set_resp_res(req_msg, param->responder_resources);
		cm_req_set_retry_count(req_msg, param->retry_count);
		cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
		cm_req_set_srq(req_msg, param->srq);
	}
1230

1231 1232 1233 1234 1235 1236 1237 1238
	req_msg->primary_local_gid = pri_path->sgid;
	req_msg->primary_remote_gid = pri_path->dgid;
	if (pri_ext) {
		req_msg->primary_local_gid.global.interface_id
			= OPA_MAKE_ID(be32_to_cpu(pri_path->opa.slid));
		req_msg->primary_remote_gid.global.interface_id
			= OPA_MAKE_ID(be32_to_cpu(pri_path->opa.dlid));
	}
1239
	if (pri_path->hop_limit <= 1) {
1240
		req_msg->primary_local_lid = pri_ext ? 0 :
1241
			htons(ntohl(sa_path_get_slid(pri_path)));
1242
		req_msg->primary_remote_lid = pri_ext ? 0 :
1243
			htons(ntohl(sa_path_get_dlid(pri_path)));
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	} else {
		/* Work-around until there's a way to obtain remote LID info */
		req_msg->primary_local_lid = IB_LID_PERMISSIVE;
		req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
	}
	cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
	cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
	req_msg->primary_traffic_class = pri_path->traffic_class;
	req_msg->primary_hop_limit = pri_path->hop_limit;
	cm_req_set_primary_sl(req_msg, pri_path->sl);
	cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
1255
	cm_req_set_primary_local_ack_timeout(req_msg,
1256
		cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1257
			       pri_path->packet_life_time));
1258

1259
	if (alt_path) {
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
		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);

		req_msg->alt_local_gid = alt_path->sgid;
		req_msg->alt_remote_gid = alt_path->dgid;
		if (alt_ext) {
			req_msg->alt_local_gid.global.interface_id
				= OPA_MAKE_ID(be32_to_cpu(alt_path->opa.slid));
			req_msg->alt_remote_gid.global.interface_id
				= OPA_MAKE_ID(be32_to_cpu(alt_path->opa.dlid));
		}
1274
		if (alt_path->hop_limit <= 1) {
1275
			req_msg->alt_local_lid = alt_ext ? 0 :
1276
				htons(ntohl(sa_path_get_slid(alt_path)));
1277
			req_msg->alt_remote_lid = alt_ext ? 0 :
1278
				htons(ntohl(sa_path_get_dlid(alt_path)));
1279 1280 1281 1282
		} else {
			req_msg->alt_local_lid = IB_LID_PERMISSIVE;
			req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
		}
1283
		cm_req_set_alt_flow_label(req_msg,
1284 1285 1286 1287 1288 1289
					  alt_path->flow_label);
		cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
		req_msg->alt_traffic_class = alt_path->traffic_class;
		req_msg->alt_hop_limit = alt_path->hop_limit;
		cm_req_set_alt_sl(req_msg, alt_path->sl);
		cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
1290
		cm_req_set_alt_local_ack_timeout(req_msg,
1291
			cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1292
				       alt_path->packet_life_time));
1293 1294 1295 1296 1297 1298 1299
	}

	if (param->private_data && param->private_data_len)
		memcpy(req_msg->private_data, param->private_data,
		       param->private_data_len);
}

1300
static int cm_validate_req_param(struct ib_cm_req_param *param)
1301 1302 1303 1304 1305 1306 1307 1308
{
	/* peer-to-peer not supported */
	if (param->peer_to_peer)
		return -EINVAL;

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

S
Sean Hefty 已提交
1309 1310
	if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
	    param->qp_type != IB_QPT_XRC_INI)
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		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);
1349 1350
	if (IS_ERR(cm_id_priv->timewait_info)) {
		ret = PTR_ERR(cm_id_priv->timewait_info);
1351
		goto out;
1352
	}
1353

1354 1355
	ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av,
				 cm_id_priv);
1356 1357 1358 1359
	if (ret)
		goto error1;
	if (param->alternate_path) {
		ret = cm_init_av_by_path(param->alternate_path,
1360
					 &cm_id_priv->alt_av, cm_id_priv);
1361 1362 1363 1364
		if (ret)
			goto error1;
	}
	cm_id->service_id = param->service_id;
1365
	cm_id->service_mask = ~cpu_to_be64(0);
1366 1367 1368 1369 1370 1371 1372 1373 1374
	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;
1375
	cm_id_priv->pkey = param->primary_path->pkey;
1376
	cm_id_priv->qp_type = param->qp_type;
1377 1378 1379 1380 1381 1382 1383 1384

	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;
1385
	cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1386 1387 1388 1389 1390 1391
	cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;

	cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
	cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);

	spin_lock_irqsave(&cm_id_priv->lock, flags);
1392
	ret = ib_post_send_mad(cm_id_priv->msg, NULL);
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
	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);
	rej_msg->remote_comm_id = rcv_msg->local_comm_id;
	rej_msg->local_comm_id = rcv_msg->remote_comm_id;
	cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1430
	rej_msg->reason = cpu_to_be16(reason);
1431 1432 1433 1434 1435 1436

	if (ari && ari_length) {
		cm_rej_set_reject_info_len(rej_msg, ari_length);
		memcpy(rej_msg->ari, ari, ari_length);
	}

1437
	ret = ib_post_send_mad(msg, NULL);
1438 1439 1440 1441 1442 1443
	if (ret)
		cm_free_msg(msg);

	return ret;
}

1444 1445
static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
				    __be32 local_qpn, __be32 remote_qpn)
1446 1447 1448 1449 1450 1451
{
	return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
		((local_ca_guid == remote_ca_guid) &&
		 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
}

1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
static bool cm_req_has_alt_path(struct cm_req_msg *req_msg)
{
	return ((req_msg->alt_local_lid) ||
		(ib_is_opa_gid(&req_msg->alt_local_gid)));
}

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;
}

1467 1468 1469 1470 1471 1472 1473 1474
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,
1475
				 ntohs(req_msg->primary_local_lid));
1476
		sa_path_set_slid(primary_path,
1477
				 ntohs(req_msg->primary_remote_lid));
1478 1479
	} else {
		lid = opa_get_lid_from_gid(&req_msg->primary_local_gid);
1480
		sa_path_set_dlid(primary_path, lid);
1481 1482

		lid = opa_get_lid_from_gid(&req_msg->primary_remote_gid);
1483
		sa_path_set_slid(primary_path, lid);
1484 1485 1486 1487 1488 1489
	}

	if (!cm_req_has_alt_path(req_msg))
		return;

	if (alt_path->rec_type != SA_PATH_REC_TYPE_OPA) {
1490 1491
		sa_path_set_dlid(alt_path, ntohs(req_msg->alt_local_lid));
		sa_path_set_slid(alt_path, ntohs(req_msg->alt_remote_lid));
1492 1493
	} else {
		lid = opa_get_lid_from_gid(&req_msg->alt_local_gid);
1494
		sa_path_set_dlid(alt_path, lid);
1495 1496

		lid = opa_get_lid_from_gid(&req_msg->alt_remote_gid);
1497
		sa_path_set_slid(alt_path, lid);
1498 1499 1500
	}
}

1501
static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1502 1503
				     struct sa_path_rec *primary_path,
				     struct sa_path_rec *alt_path)
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
{
	primary_path->dgid = req_msg->primary_local_gid;
	primary_path->sgid = req_msg->primary_remote_gid;
	primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
	primary_path->hop_limit = req_msg->primary_hop_limit;
	primary_path->traffic_class = req_msg->primary_traffic_class;
	primary_path->reversible = 1;
	primary_path->pkey = req_msg->pkey;
	primary_path->sl = cm_req_get_primary_sl(req_msg);
	primary_path->mtu_selector = IB_SA_EQ;
	primary_path->mtu = cm_req_get_path_mtu(req_msg);
	primary_path->rate_selector = IB_SA_EQ;
	primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
	primary_path->packet_life_time_selector = IB_SA_EQ;
	primary_path->packet_life_time =
		cm_req_get_primary_local_ack_timeout(req_msg);
	primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1521
	primary_path->service_id = req_msg->service_id;
1522

1523
	if (cm_req_has_alt_path(req_msg)) {
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
		alt_path->dgid = req_msg->alt_local_gid;
		alt_path->sgid = req_msg->alt_remote_gid;
		alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
		alt_path->hop_limit = req_msg->alt_hop_limit;
		alt_path->traffic_class = req_msg->alt_traffic_class;
		alt_path->reversible = 1;
		alt_path->pkey = req_msg->pkey;
		alt_path->sl = cm_req_get_alt_sl(req_msg);
		alt_path->mtu_selector = IB_SA_EQ;
		alt_path->mtu = cm_req_get_path_mtu(req_msg);
		alt_path->rate_selector = IB_SA_EQ;
		alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
		alt_path->packet_life_time_selector = IB_SA_EQ;
		alt_path->packet_life_time =
			cm_req_get_alt_local_ack_timeout(req_msg);
		alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1540
		alt_path->service_id = req_msg->service_id;
1541
	}
1542
	cm_format_path_lid_from_req(req_msg, primary_path, alt_path);
1543 1544
}

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
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;
}

1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
/**
 * 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;
	struct ib_gid_attr gid_attr;
	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;

		if (ib_get_cached_gid(dev, port_num, 0,
				      &sgid, &gid_attr)) {
			dev_warn(&dev->dev,
				 "Error updating sgid in CM request\n");
			return;
		}

		path->sgid = sgid;
	}
}

1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
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;
1604
	param->bth_pkey = cm_get_bth_pkey(work);
1605 1606
	param->port = cm_id_priv->av.port->port_num;
	param->primary_path = &work->path[0];
1607 1608
	cm_opa_to_ib_sgid(work, param->primary_path);
	if (cm_req_has_alt_path(req_msg)) {
1609
		param->alternate_path = &work->path[1];
1610 1611
		cm_opa_to_ib_sgid(work, param->alternate_path);
	} else {
1612
		param->alternate_path = NULL;
1613
	}
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
	param->remote_ca_guid = req_msg->local_ca_guid;
	param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
	param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
	param->qp_type = cm_req_get_qp_type(req_msg);
	param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
	param->responder_resources = cm_req_get_init_depth(req_msg);
	param->initiator_depth = cm_req_get_resp_res(req_msg);
	param->local_cm_response_timeout =
					cm_req_get_remote_resp_timeout(req_msg);
	param->flow_control = cm_req_get_flow_ctrl(req_msg);
	param->remote_cm_response_timeout =
					cm_req_get_local_resp_timeout(req_msg);
	param->retry_count = cm_req_get_retry_count(req_msg);
	param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
	param->srq = cm_req_get_srq(req_msg);
	work->cm_event.private_data = &req_msg->private_data;
}

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)) {
1642
		spin_lock_irq(&cm_id_priv->lock);
1643
		work = cm_dequeue_work(cm_id_priv);
1644
		spin_unlock_irq(&cm_id_priv->lock);
1645 1646 1647 1648 1649 1650 1651
		BUG_ON(!work);
		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)
1652
		cm_destroy_id(&cm_id_priv->id, ret);
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
}

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);
	cm_mra_set_msg_mraed(mra_msg, msg_mraed);
	mra_msg->local_comm_id = cm_id_priv->id.local_id;
	mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
	cm_mra_set_service_timeout(mra_msg, service_timeout);

	if (private_data && private_data_len)
		memcpy(mra_msg->private_data, private_data, private_data_len);
}

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);
	rej_msg->remote_comm_id = cm_id_priv->id.remote_id;

	switch(cm_id_priv->id.state) {
	case IB_CM_REQ_RCVD:
		rej_msg->local_comm_id = 0;
		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
		break;
	case IB_CM_MRA_REQ_SENT:
		rej_msg->local_comm_id = cm_id_priv->id.local_id;
		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
		break;
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
		rej_msg->local_comm_id = cm_id_priv->id.local_id;
		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
		break;
	default:
		rej_msg->local_comm_id = cm_id_priv->id.local_id;
		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
		break;
	}

1701
	rej_msg->reason = cpu_to_be16(reason);
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
	if (ari && ari_length) {
		cm_rej_set_reject_info_len(rej_msg, ari_length);
		memcpy(rej_msg->ari, ari, ari_length);
	}

	if (private_data && private_data_len)
		memcpy(rej_msg->private_data, private_data, private_data_len);
}

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;

1717 1718 1719
	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
			counter[CM_REQ_COUNTER]);

1720 1721 1722 1723 1724 1725 1726 1727
	/* 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;

1728
	spin_lock_irq(&cm_id_priv->lock);
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
	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;
	}
1743
	spin_unlock_irq(&cm_id_priv->lock);
1744

1745
	ret = ib_post_send_mad(msg, NULL);
1746 1747 1748 1749
	if (ret)
		goto free;
	return;

1750
unlock:	spin_unlock_irq(&cm_id_priv->lock);
1751 1752 1753 1754 1755 1756 1757 1758 1759
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;
1760
	struct ib_cm_id *cm_id;
1761 1762 1763

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

1764
	/* Check for possible duplicate REQ. */
1765
	spin_lock_irq(&cm.lock);
1766 1767 1768 1769
	timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
	if (timewait_info) {
		cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
					   timewait_info->work.remote_id);
1770
		spin_unlock_irq(&cm.lock);
1771 1772 1773
		if (cur_cm_id_priv) {
			cm_dup_req_handler(work, cur_cm_id_priv);
			cm_deref_id(cur_cm_id_priv);
1774 1775 1776 1777 1778 1779 1780 1781
		}
		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);
1782 1783 1784
		cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
					   timewait_info->work.remote_id);

1785
		spin_unlock_irq(&cm.lock);
1786 1787 1788
		cm_issue_rej(work->port, work->mad_recv_wc,
			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
			     NULL, 0);
1789 1790 1791 1792 1793
		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);
		}
1794
		return NULL;
1795 1796 1797
	}

	/* Find matching listen request. */
1798
	listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
H
Haggai Eran 已提交
1799
					   req_msg->service_id);
1800
	if (!listen_cm_id_priv) {
1801
		cm_cleanup_timewait(cm_id_priv->timewait_info);
1802
		spin_unlock_irq(&cm.lock);
1803 1804 1805
		cm_issue_rej(work->port, work->mad_recv_wc,
			     IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
			     NULL, 0);
1806
		goto out;
1807 1808 1809 1810 1811
	}
	atomic_inc(&listen_cm_id_priv->refcount);
	atomic_inc(&cm_id_priv->refcount);
	cm_id_priv->id.state = IB_CM_REQ_RCVD;
	atomic_inc(&cm_id_priv->work_count);
1812
	spin_unlock_irq(&cm.lock);
1813
out:
1814 1815 1816
	return listen_cm_id_priv;
}

1817 1818 1819 1820 1821 1822 1823 1824 1825
/*
 * 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)
{
	if (!cm_req_get_primary_subnet_local(req_msg)) {
		if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
H
Hiatt, Don 已提交
1826
			req_msg->primary_local_lid = ib_lid_be16(wc->slid);
1827 1828 1829 1830 1831 1832 1833 1834 1835
			cm_req_set_primary_sl(req_msg, wc->sl);
		}

		if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
			req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
	}

	if (!cm_req_get_alt_subnet_local(req_msg)) {
		if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
H
Hiatt, Don 已提交
1836
			req_msg->alt_local_lid = ib_lid_be16(wc->slid);
1837 1838 1839 1840 1841 1842 1843 1844
			cm_req_set_alt_sl(req_msg, wc->sl);
		}

		if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
			req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
	}
}

1845 1846 1847 1848 1849
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;
M
Matan Barak 已提交
1850 1851
	union ib_gid gid;
	struct ib_gid_attr gid_attr;
1852
	const struct ib_global_route *grh;
1853 1854 1855 1856
	int ret;

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

1857
	cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
1858 1859 1860 1861 1862
	if (IS_ERR(cm_id))
		return PTR_ERR(cm_id);

	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
	cm_id_priv->id.remote_id = req_msg->local_comm_id;
P
Parav Pandit 已提交
1863 1864 1865 1866 1867
	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;
1868 1869 1870 1871
	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);
1872
		goto destroy;
1873 1874 1875 1876 1877 1878 1879
	}
	cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
	cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
	cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);

	listen_cm_id_priv = cm_match_req(work, cm_id_priv);
	if (!listen_cm_id_priv) {
D
Daniel Jurgens 已提交
1880 1881
		pr_debug("%s: local_id %d, no listen_cm_id_priv\n", __func__,
			 be32_to_cpu(cm_id->local_id));
1882
		ret = -EINVAL;
1883
		goto free_timeinfo;
1884 1885 1886 1887 1888
	}

	cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
	cm_id_priv->id.context = listen_cm_id_priv->id.context;
	cm_id_priv->id.service_id = req_msg->service_id;
1889
	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
1890

1891
	cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
1892

1893
	memset(&work->path[0], 0, sizeof(work->path[0]));
1894 1895
	if (cm_req_has_alt_path(req_msg))
		memset(&work->path[1], 0, sizeof(work->path[1]));
1896
	grh = rdma_ah_read_grh(&cm_id_priv->av.ah_attr);
M
Matan Barak 已提交
1897 1898
	ret = ib_get_cached_gid(work->port->cm_dev->ib_device,
				work->port->port_num,
1899
				grh->sgid_index,
M
Matan Barak 已提交
1900 1901
				&gid, &gid_attr);
	if (!ret) {
1902
		if (gid_attr.ndev) {
1903 1904
			work->path[0].rec_type =
				sa_conv_gid_to_pathrec_type(gid_attr.gid_type);
1905 1906 1907 1908 1909
			sa_path_set_ifindex(&work->path[0],
					    gid_attr.ndev->ifindex);
			sa_path_set_ndev(&work->path[0],
					 dev_net(gid_attr.ndev));
			dev_put(gid_attr.ndev);
1910
		} else {
1911 1912 1913 1914
			cm_path_set_rec_type(work->port->cm_dev->ib_device,
					     work->port->port_num,
					     &work->path[0],
					     &req_msg->primary_local_gid);
1915
		}
1916
		if (cm_req_has_alt_path(req_msg))
1917 1918 1919 1920 1921 1922 1923
			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;
1924 1925
		ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av,
					 cm_id_priv);
M
Matan Barak 已提交
1926
	}
1927
	if (ret) {
M
Matan Barak 已提交
1928 1929 1930 1931
		int err = ib_get_cached_gid(work->port->cm_dev->ib_device,
					    work->port->port_num, 0,
					    &work->path[0].sgid,
					    &gid_attr);
1932
		if (!err && gid_attr.ndev) {
1933 1934
			work->path[0].rec_type =
				sa_conv_gid_to_pathrec_type(gid_attr.gid_type);
1935 1936 1937 1938 1939
			sa_path_set_ifindex(&work->path[0],
					    gid_attr.ndev->ifindex);
			sa_path_set_ndev(&work->path[0],
					 dev_net(gid_attr.ndev));
			dev_put(gid_attr.ndev);
1940
		} else {
1941 1942 1943 1944
			cm_path_set_rec_type(work->port->cm_dev->ib_device,
					     work->port->port_num,
					     &work->path[0],
					     &req_msg->primary_local_gid);
1945
		}
1946
		if (cm_req_has_alt_path(req_msg))
1947
			work->path[1].rec_type = work->path[0].rec_type;
1948 1949 1950 1951 1952
		ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
			       &work->path[0].sgid, sizeof work->path[0].sgid,
			       NULL, 0);
		goto rejected;
	}
1953
	if (cm_req_has_alt_path(req_msg)) {
1954 1955
		ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av,
					 cm_id_priv);
1956 1957 1958 1959 1960 1961
		if (ret) {
			ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
				       &work->path[0].sgid,
				       sizeof work->path[0].sgid, NULL, 0);
			goto rejected;
		}
1962 1963 1964 1965 1966 1967 1968 1969 1970
	}
	cm_id_priv->tid = req_msg->hdr.tid;
	cm_id_priv->timeout_ms = cm_convert_to_ms(
					cm_req_get_local_resp_timeout(req_msg));
	cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
	cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
	cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
	cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
	cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1971
	cm_id_priv->pkey = req_msg->pkey;
1972 1973 1974
	cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
	cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
	cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1975
	cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1976 1977 1978 1979 1980 1981

	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;

1982 1983
rejected:
	atomic_dec(&cm_id_priv->refcount);
1984
	cm_deref_id(listen_cm_id_priv);
1985 1986
free_timeinfo:
	kfree(cm_id_priv->timewait_info);
1987 1988
destroy:
	ib_destroy_cm_id(cm_id);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
	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);
	rep_msg->local_comm_id = cm_id_priv->id.local_id;
	rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
	cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
	rep_msg->resp_resources = param->responder_resources;
2001 2002
	cm_rep_set_target_ack_delay(rep_msg,
				    cm_id_priv->av.port->cm_dev->ack_delay);
2003 2004
	cm_rep_set_failover(rep_msg, param->failover_accepted);
	cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
2005
	rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
2006

S
Sean Hefty 已提交
2007 2008 2009 2010
	if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
		rep_msg->initiator_depth = param->initiator_depth;
		cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
		cm_rep_set_srq(rep_msg, param->srq);
2011
		cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
S
Sean Hefty 已提交
2012 2013
	} else {
		cm_rep_set_srq(rep_msg, 1);
2014
		cm_rep_set_local_eecn(rep_msg, cpu_to_be32(param->qp_num));
S
Sean Hefty 已提交
2015 2016
	}

2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
	if (param->private_data && param->private_data_len)
		memcpy(rep_msg->private_data, param->private_data,
		       param->private_data_len);
}

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 已提交
2039 2040
		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);
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
		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);
2051
	msg->timeout_ms = cm_id_priv->timeout_ms;
2052 2053
	msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;

2054
	ret = ib_post_send_mad(msg, NULL);
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
	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;
	cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
2066
	cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106

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);
	rtu_msg->local_comm_id = cm_id_priv->id.local_id;
	rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;

	if (private_data && private_data_len)
		memcpy(rtu_msg->private_data, private_data, private_data_len);
}

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 已提交
2107 2108
		pr_debug("%s: local_id %d, cm_id->state %d\n", __func__,
			 be32_to_cpu(cm_id->local_id), cm_id->state);
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
		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);

2120
	ret = ib_post_send_mad(msg, NULL);
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
	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);

2139
static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
2140 2141 2142 2143 2144 2145 2146 2147
{
	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;
	param->remote_ca_guid = rep_msg->local_ca_guid;
	param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
2148
	param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
	param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
	param->responder_resources = rep_msg->initiator_depth;
	param->initiator_depth = rep_msg->resp_resources;
	param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
	param->failover_accepted = cm_rep_get_failover(rep_msg);
	param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
	param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
	param->srq = cm_rep_get_srq(rep_msg);
	work->cm_event.private_data = &rep_msg->private_data;
}

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;
	cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
				   rep_msg->local_comm_id);
	if (!cm_id_priv)
		return;

2173 2174
	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
			counter[CM_REP_COUNTER]);
2175 2176 2177 2178
	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
	if (ret)
		goto deref;

2179
	spin_lock_irq(&cm_id_priv->lock);
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
	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;
2191
	spin_unlock_irq(&cm_id_priv->lock);
2192

2193
	ret = ib_post_send_mad(msg, NULL);
2194 2195 2196 2197
	if (ret)
		goto free;
	goto deref;

2198
unlock:	spin_unlock_irq(&cm_id_priv->lock);
2199 2200 2201 2202 2203 2204 2205 2206 2207
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;
2208 2209 2210
	struct cm_id_private *cur_cm_id_priv;
	struct ib_cm_id *cm_id;
	struct cm_timewait_info *timewait_info;
2211 2212 2213 2214 2215

	rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
	cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
	if (!cm_id_priv) {
		cm_dup_rep_handler(work);
D
Daniel Jurgens 已提交
2216 2217
		pr_debug("%s: remote_comm_id %d, no cm_id_priv\n", __func__,
			 be32_to_cpu(rep_msg->remote_comm_id));
2218 2219 2220
		return -EINVAL;
	}

2221
	cm_format_rep_event(work, cm_id_priv->qp_type);
2222

2223
	spin_lock_irq(&cm_id_priv->lock);
2224 2225 2226 2227 2228
	switch (cm_id_priv->id.state) {
	case IB_CM_REQ_SENT:
	case IB_CM_MRA_REQ_RCVD:
		break;
	default:
2229
		spin_unlock_irq(&cm_id_priv->lock);
2230
		ret = -EINVAL;
D
Daniel Jurgens 已提交
2231 2232 2233 2234
		pr_debug("%s: cm_id_priv->id.state: %d, local_comm_id %d, remote_comm_id %d\n",
			 __func__, cm_id_priv->id.state,
			 be32_to_cpu(rep_msg->local_comm_id),
			 be32_to_cpu(rep_msg->remote_comm_id));
2235 2236 2237
		goto error;
	}

2238 2239
	cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
	cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
2240
	cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2241

2242
	spin_lock(&cm.lock);
2243 2244
	/* Check for duplicate REP. */
	if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
2245
		spin_unlock(&cm.lock);
2246
		spin_unlock_irq(&cm_id_priv->lock);
2247
		ret = -EINVAL;
D
Daniel Jurgens 已提交
2248 2249
		pr_debug("%s: Failed to insert remote id %d\n", __func__,
			 be32_to_cpu(rep_msg->remote_comm_id));
2250 2251 2252
		goto error;
	}
	/* Check for a stale connection. */
2253 2254
	timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
	if (timewait_info) {
2255 2256 2257
		rb_erase(&cm_id_priv->timewait_info->remote_id_node,
			 &cm.remote_id_table);
		cm_id_priv->timewait_info->inserted_remote_id = 0;
2258 2259 2260
		cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
					   timewait_info->work.remote_id);

2261
		spin_unlock(&cm.lock);
2262
		spin_unlock_irq(&cm_id_priv->lock);
2263 2264 2265 2266
		cm_issue_rej(work->port, work->mad_recv_wc,
			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
			     NULL, 0);
		ret = -EINVAL;
D
Daniel Jurgens 已提交
2267 2268 2269 2270
		pr_debug("%s: Stale connection. local_comm_id %d, remote_comm_id %d\n",
			 __func__, be32_to_cpu(rep_msg->local_comm_id),
			 be32_to_cpu(rep_msg->remote_comm_id));

2271 2272 2273 2274 2275 2276
		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);
		}

2277 2278
		goto error;
	}
2279
	spin_unlock(&cm.lock);
2280 2281 2282

	cm_id_priv->id.state = IB_CM_REP_RCVD;
	cm_id_priv->id.remote_id = rep_msg->local_comm_id;
2283
	cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2284 2285 2286 2287
	cm_id_priv->initiator_depth = rep_msg->resp_resources;
	cm_id_priv->responder_resources = rep_msg->initiator_depth;
	cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
	cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
2288 2289 2290 2291 2292 2293 2294
	cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
	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);
2295 2296 2297

	/* todo: handle peer_to_peer */

2298
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2299 2300 2301
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2302
	spin_unlock_irq(&cm_id_priv->lock);
2303 2304 2305 2306 2307 2308 2309

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

2310
error:
2311 2312 2313 2314 2315 2316 2317 2318 2319
	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;

2320
	/* See comment in cm_establish about lookup. */
2321 2322 2323 2324
	cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
	if (!cm_id_priv)
		return -EINVAL;

2325
	spin_lock_irq(&cm_id_priv->lock);
2326
	if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2327
		spin_unlock_irq(&cm_id_priv->lock);
2328 2329 2330
		goto out;
	}

2331
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2332 2333 2334
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2335
	spin_unlock_irq(&cm_id_priv->lock);
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360

	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;
	cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
				   rtu_msg->local_comm_id);
	if (!cm_id_priv)
		return -EINVAL;

	work->cm_event.private_data = &rtu_msg->private_data;

2361
	spin_lock_irq(&cm_id_priv->lock);
2362 2363
	if (cm_id_priv->id.state != IB_CM_REP_SENT &&
	    cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
2364
		spin_unlock_irq(&cm_id_priv->lock);
2365 2366
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_RTU_COUNTER]);
2367 2368 2369 2370
		goto out;
	}
	cm_id_priv->id.state = IB_CM_ESTABLISHED;

2371
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2372 2373 2374
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2375
	spin_unlock_irq(&cm_id_priv->lock);
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416

	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,
			  cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
	dreq_msg->local_comm_id = cm_id_priv->id.local_id;
	dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
	cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);

	if (private_data && private_data_len)
		memcpy(dreq_msg->private_data, private_data, private_data_len);
}

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 已提交
2417 2418
		pr_debug("%s: local_id %d, cm_id->state: %d\n", __func__,
			 be32_to_cpu(cm_id->local_id), cm_id->state);
2419 2420 2421 2422
		ret = -EINVAL;
		goto out;
	}

2423 2424 2425 2426
	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);

2427 2428 2429 2430 2431 2432 2433 2434
	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);
2435
	msg->timeout_ms = cm_id_priv->timeout_ms;
2436 2437
	msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;

2438
	ret = ib_post_send_mad(msg, NULL);
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
	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);
	drep_msg->local_comm_id = cm_id_priv->id.local_id;
	drep_msg->remote_comm_id = cm_id_priv->id.remote_id;

	if (private_data && private_data_len)
		memcpy(drep_msg->private_data, private_data, private_data_len);
}

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 已提交
2488 2489
		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);
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
		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);

2503
	ret = ib_post_send_mad(msg, NULL);
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
	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);

2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540
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);
	drep_msg->remote_comm_id = dreq_msg->local_comm_id;
	drep_msg->local_comm_id = dreq_msg->remote_comm_id;

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

	return ret;
}

2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
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;
	cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
				   dreq_msg->local_comm_id);
2551
	if (!cm_id_priv) {
2552 2553
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_DREQ_COUNTER]);
2554
		cm_issue_drep(work->port, work->mad_recv_wc);
D
Daniel Jurgens 已提交
2555 2556 2557
		pr_debug("%s: no cm_id_priv, local_comm_id %d, remote_comm_id %d\n",
			 __func__, be32_to_cpu(dreq_msg->local_comm_id),
			 be32_to_cpu(dreq_msg->remote_comm_id));
2558
		return -EINVAL;
2559
	}
2560 2561 2562

	work->cm_event.private_data = &dreq_msg->private_data;

2563
	spin_lock_irq(&cm_id_priv->lock);
2564 2565 2566 2567 2568 2569
	if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
		goto unlock;

	switch (cm_id_priv->id.state) {
	case IB_CM_REP_SENT:
	case IB_CM_DREQ_SENT:
2570
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2571 2572
		break;
	case IB_CM_ESTABLISHED:
2573 2574 2575 2576
		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;
2577 2578 2579
	case IB_CM_MRA_REP_RCVD:
		break;
	case IB_CM_TIMEWAIT:
2580 2581
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_DREQ_COUNTER]);
2582 2583
		msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc);
		if (IS_ERR(msg))
2584 2585 2586 2587 2588
			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);
2589
		spin_unlock_irq(&cm_id_priv->lock);
2590

2591 2592
		if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
		    ib_post_send_mad(msg, NULL))
2593 2594
			cm_free_msg(msg);
		goto deref;
2595 2596 2597 2598
	case IB_CM_DREQ_RCVD:
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_DREQ_COUNTER]);
		goto unlock;
2599
	default:
D
Daniel Jurgens 已提交
2600 2601 2602
		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);
2603 2604 2605 2606 2607 2608 2609
		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);
2610
	spin_unlock_irq(&cm_id_priv->lock);
2611 2612 2613 2614 2615 2616 2617

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

2618
unlock:	spin_unlock_irq(&cm_id_priv->lock);
2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
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;
	cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
				   drep_msg->local_comm_id);
	if (!cm_id_priv)
		return -EINVAL;

	work->cm_event.private_data = &drep_msg->private_data;

2637
	spin_lock_irq(&cm_id_priv->lock);
2638 2639
	if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
	    cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2640
		spin_unlock_irq(&cm_id_priv->lock);
2641 2642 2643 2644
		goto out;
	}
	cm_enter_timewait(cm_id_priv);

2645
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2646 2647 2648
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
2649
	spin_unlock_irq(&cm_id_priv->lock);
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705

	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 已提交
2706 2707
		pr_debug("%s: local_id %d, cm_id->state: %d\n", __func__,
			 be32_to_cpu(cm_id_priv->id.local_id), cm_id->state);
2708 2709 2710 2711 2712 2713 2714
		ret = -EINVAL;
		goto out;
	}

	if (ret)
		goto out;

2715
	ret = ib_post_send_mad(msg, NULL);
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
	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;
	param->ari = rej_msg->ari;
	param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2733
	param->reason = __be16_to_cpu(rej_msg->reason);
2734 2735 2736 2737 2738 2739 2740
	work->cm_event.private_data = &rej_msg->private_data;
}

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;
2741
	__be32 remote_id;
2742 2743 2744

	remote_id = rej_msg->local_comm_id;

2745
	if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2746
		spin_lock_irq(&cm.lock);
2747
		timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2748 2749
						  remote_id);
		if (!timewait_info) {
2750
			spin_unlock_irq(&cm.lock);
2751 2752
			return NULL;
		}
S
Sean Hefty 已提交
2753 2754 2755
		cm_id_priv = idr_find(&cm.local_id_table, (__force int)
				      (timewait_info->work.local_id ^
				       cm.random_id_operand));
2756 2757 2758 2759 2760 2761
		if (cm_id_priv) {
			if (cm_id_priv->id.remote_id == remote_id)
				atomic_inc(&cm_id_priv->refcount);
			else
				cm_id_priv = NULL;
		}
2762
		spin_unlock_irq(&cm.lock);
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
	} else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
		cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
	else
		cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);

	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);

2784
	spin_lock_irq(&cm_id_priv->lock);
2785 2786 2787 2788 2789
	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:
2790
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2791 2792 2793
		/* fall through */
	case IB_CM_REQ_RCVD:
	case IB_CM_MRA_REQ_SENT:
2794
		if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2795 2796 2797 2798 2799
			cm_enter_timewait(cm_id_priv);
		else
			cm_reset_to_idle(cm_id_priv);
		break;
	case IB_CM_DREQ_SENT:
2800
		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2801 2802 2803 2804 2805
		/* fall through */
	case IB_CM_REP_RCVD:
	case IB_CM_MRA_REP_SENT:
		cm_enter_timewait(cm_id_priv);
		break;
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
	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 */
2816
	default:
2817
		spin_unlock_irq(&cm_id_priv->lock);
D
Daniel Jurgens 已提交
2818 2819 2820
		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);
2821 2822 2823 2824 2825 2826 2827
		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);
2828
	spin_unlock_irq(&cm_id_priv->lock);
2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846

	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;
2847 2848 2849
	enum ib_cm_state cm_state;
	enum ib_cm_lap_state lap_state;
	enum cm_msg_response msg_response;
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865
	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:
2866 2867 2868
		cm_state = IB_CM_MRA_REQ_SENT;
		lap_state = cm_id->lap_state;
		msg_response = CM_MSG_RESPONSE_REQ;
2869 2870
		break;
	case IB_CM_REP_RCVD:
2871 2872 2873
		cm_state = IB_CM_MRA_REP_SENT;
		lap_state = cm_id->lap_state;
		msg_response = CM_MSG_RESPONSE_REP;
2874 2875
		break;
	case IB_CM_ESTABLISHED:
2876 2877 2878 2879 2880 2881
		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;
		}
2882
		/* fall through */
2883
	default:
D
Daniel Jurgens 已提交
2884 2885 2886
		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);
2887 2888 2889 2890 2891
		ret = -EINVAL;
		goto error1;
	}

	if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2892 2893 2894 2895 2896
		ret = cm_alloc_msg(cm_id_priv, &msg);
		if (ret)
			goto error1;

		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2897
			      msg_response, service_timeout,
2898
			      private_data, private_data_len);
2899
		ret = ib_post_send_mad(msg, NULL);
2900 2901 2902
		if (ret)
			goto error2;
	}
2903 2904 2905

	cm_id->state = cm_state;
	cm_id->lap_state = lap_state;
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
	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)
{
	switch (cm_mra_get_msg_mraed(mra_msg)) {
	case CM_MSG_RESPONSE_REQ:
		return cm_acquire_id(mra_msg->remote_comm_id, 0);
	case CM_MSG_RESPONSE_REP:
	case CM_MSG_RESPONSE_OTHER:
		return cm_acquire_id(mra_msg->remote_comm_id,
				     mra_msg->local_comm_id);
	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;

	work->cm_event.private_data = &mra_msg->private_data;
	work->cm_event.param.mra_rcvd.service_timeout =
					cm_mra_get_service_timeout(mra_msg);
	timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2951
		  cm_convert_to_ms(cm_id_priv->av.timeout);
2952

2953
	spin_lock_irq(&cm_id_priv->lock);
2954 2955 2956 2957
	switch (cm_id_priv->id.state) {
	case IB_CM_REQ_SENT:
		if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
2958
				  cm_id_priv->msg, timeout))
2959 2960 2961 2962 2963 2964
			goto out;
		cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
		break;
	case IB_CM_REP_SENT:
		if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
2965
				  cm_id_priv->msg, timeout))
2966 2967 2968 2969 2970 2971 2972
			goto out;
		cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
		break;
	case IB_CM_ESTABLISHED:
		if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
		    cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
2973 2974 2975 2976 2977
				  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]);
2978
			goto out;
2979
		}
2980 2981
		cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
		break;
2982 2983 2984 2985 2986
	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 */
2987
	default:
D
Daniel Jurgens 已提交
2988 2989 2990
		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);
2991 2992 2993 2994 2995 2996 2997 2998
		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);
2999
	spin_unlock_irq(&cm_id_priv->lock);
3000 3001 3002 3003 3004 3005 3006

	if (ret)
		cm_process_work(cm_id_priv, work);
	else
		cm_deref_id(cm_id_priv);
	return 0;
out:
3007
	spin_unlock_irq(&cm_id_priv->lock);
3008 3009 3010 3011 3012 3013
	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

static void cm_format_lap(struct cm_lap_msg *lap_msg,
			  struct cm_id_private *cm_id_priv,
3014
			  struct sa_path_rec *alternate_path,
3015 3016 3017
			  const void *private_data,
			  u8 private_data_len)
{
3018 3019 3020 3021 3022
	bool alt_ext = false;

	if (alternate_path->rec_type == SA_PATH_REC_TYPE_OPA)
		alt_ext = opa_is_extended_lid(alternate_path->opa.dlid,
					      alternate_path->opa.slid);
3023 3024 3025 3026 3027 3028 3029
	cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
			  cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
	lap_msg->local_comm_id = cm_id_priv->id.local_id;
	lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
	cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
	/* todo: need remote CM response timeout */
	cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
3030 3031 3032 3033
	lap_msg->alt_local_lid =
		htons(ntohl(sa_path_get_slid(alternate_path)));
	lap_msg->alt_remote_lid =
		htons(ntohl(sa_path_get_dlid(alternate_path)));
3034 3035
	lap_msg->alt_local_gid = alternate_path->sgid;
	lap_msg->alt_remote_gid = alternate_path->dgid;
3036 3037 3038 3039 3040 3041
	if (alt_ext) {
		lap_msg->alt_local_gid.global.interface_id
			= OPA_MAKE_ID(be32_to_cpu(alternate_path->opa.slid));
		lap_msg->alt_remote_gid.global.interface_id
			= OPA_MAKE_ID(be32_to_cpu(alternate_path->opa.dlid));
	}
3042 3043 3044 3045 3046 3047 3048
	cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
	cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
	lap_msg->alt_hop_limit = alternate_path->hop_limit;
	cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
	cm_lap_set_sl(lap_msg, alternate_path->sl);
	cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
	cm_lap_set_local_ack_timeout(lap_msg,
3049 3050
		cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
			       alternate_path->packet_life_time));
3051 3052 3053 3054 3055 3056

	if (private_data && private_data_len)
		memcpy(lap_msg->private_data, private_data, private_data_len);
}

int ib_send_cm_lap(struct ib_cm_id *cm_id,
3057
		   struct sa_path_rec *alternate_path,
3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071
		   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_LAP_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 ||
3072 3073
	    (cm_id->lap_state != IB_CM_LAP_UNINIT &&
	     cm_id->lap_state != IB_CM_LAP_IDLE)) {
3074 3075 3076 3077
		ret = -EINVAL;
		goto out;
	}

3078 3079
	ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av,
				 cm_id_priv);
3080 3081
	if (ret)
		goto out;
3082 3083 3084
	cm_id_priv->alt_av.timeout =
			cm_ack_timeout(cm_id_priv->target_ack_delay,
				       cm_id_priv->alt_av.timeout - 1);
3085

3086 3087 3088 3089 3090 3091
	ret = cm_alloc_msg(cm_id_priv, &msg);
	if (ret)
		goto out;

	cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
		      alternate_path, private_data, private_data_len);
3092
	msg->timeout_ms = cm_id_priv->timeout_ms;
3093 3094
	msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;

3095
	ret = ib_post_send_mad(msg, NULL);
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109
	if (ret) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		cm_free_msg(msg);
		return ret;
	}

	cm_id->lap_state = IB_CM_LAP_SENT;
	cm_id_priv->msg = msg;

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

3110 3111 3112 3113 3114 3115
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) {
3116 3117
		sa_path_set_dlid(path, ntohs(lap_msg->alt_local_lid));
		sa_path_set_slid(path, ntohs(lap_msg->alt_remote_lid));
3118 3119
	} else {
		lid = opa_get_lid_from_gid(&lap_msg->alt_local_gid);
3120
		sa_path_set_dlid(path, lid);
3121 3122

		lid = opa_get_lid_from_gid(&lap_msg->alt_remote_gid);
3123
		sa_path_set_slid(path, lid);
3124 3125 3126
	}
}

3127
static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
3128
				    struct sa_path_rec *path,
3129 3130 3131 3132 3133 3134 3135 3136
				    struct cm_lap_msg *lap_msg)
{
	path->dgid = lap_msg->alt_local_gid;
	path->sgid = lap_msg->alt_remote_gid;
	path->flow_label = cm_lap_get_flow_label(lap_msg);
	path->hop_limit = lap_msg->alt_hop_limit;
	path->traffic_class = cm_lap_get_traffic_class(lap_msg);
	path->reversible = 1;
3137
	path->pkey = cm_id_priv->pkey;
3138 3139
	path->sl = cm_lap_get_sl(lap_msg);
	path->mtu_selector = IB_SA_EQ;
3140
	path->mtu = cm_id_priv->path_mtu;
3141 3142 3143 3144 3145
	path->rate_selector = IB_SA_EQ;
	path->rate = cm_lap_get_packet_rate(lap_msg);
	path->packet_life_time_selector = IB_SA_EQ;
	path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
	path->packet_life_time -= (path->packet_life_time > 0);
3146
	cm_format_path_lid_from_lap(lap_msg, path);
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164
}

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;

	/* todo: verify LAP request and send reject APR if invalid. */
	lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
	cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
				   lap_msg->local_comm_id);
	if (!cm_id_priv)
		return -EINVAL;

	param = &work->cm_event.param.lap_rcvd;
3165 3166 3167 3168 3169
	memset(&work->path[0], 0, sizeof(work->path[1]));
	cm_path_set_rec_type(work->port->cm_dev->ib_device,
			     work->port->port_num,
			     &work->path[0],
			     &lap_msg->alt_local_gid);
3170
	param->alternate_path = &work->path[0];
3171
	cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
3172 3173
	work->cm_event.private_data = &lap_msg->private_data;

3174
	spin_lock_irq(&cm_id_priv->lock);
3175 3176 3177 3178
	if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
		goto unlock;

	switch (cm_id_priv->id.lap_state) {
3179
	case IB_CM_LAP_UNINIT:
3180 3181 3182
	case IB_CM_LAP_IDLE:
		break;
	case IB_CM_MRA_LAP_SENT:
3183 3184
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_LAP_COUNTER]);
3185 3186
		msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc);
		if (IS_ERR(msg))
3187 3188 3189 3190 3191 3192 3193
			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);
3194
		spin_unlock_irq(&cm_id_priv->lock);
3195

3196 3197
		if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
		    ib_post_send_mad(msg, NULL))
3198 3199
			cm_free_msg(msg);
		goto deref;
3200 3201 3202 3203
	case IB_CM_LAP_RCVD:
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_LAP_COUNTER]);
		goto unlock;
3204 3205 3206 3207 3208 3209
	default:
		goto unlock;
	}

	cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
	cm_id_priv->tid = lap_msg->hdr.tid;
P
Parav Pandit 已提交
3210 3211 3212 3213 3214
	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 unlock;
3215 3216
	cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av,
			   cm_id_priv);
3217 3218 3219
	ret = atomic_inc_and_test(&cm_id_priv->work_count);
	if (!ret)
		list_add_tail(&work->list, &cm_id_priv->work_list);
3220
	spin_unlock_irq(&cm_id_priv->lock);
3221 3222 3223 3224 3225 3226 3227

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

3228
unlock:	spin_unlock_irq(&cm_id_priv->lock);
3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285
deref:	cm_deref_id(cm_id_priv);
	return -EINVAL;
}

static void cm_format_apr(struct cm_apr_msg *apr_msg,
			  struct cm_id_private *cm_id_priv,
			  enum ib_cm_apr_status status,
			  void *info,
			  u8 info_length,
			  const void *private_data,
			  u8 private_data_len)
{
	cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
	apr_msg->local_comm_id = cm_id_priv->id.local_id;
	apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
	apr_msg->ap_status = (u8) status;

	if (info && info_length) {
		apr_msg->info_length = info_length;
		memcpy(apr_msg->info, info, info_length);
	}

	if (private_data && private_data_len)
		memcpy(apr_msg->private_data, private_data, private_data_len);
}

int ib_send_cm_apr(struct ib_cm_id *cm_id,
		   enum ib_cm_apr_status status,
		   void *info,
		   u8 info_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_APR_PRIVATE_DATA_SIZE) ||
	    (info && info_length > IB_CM_APR_INFO_LENGTH))
		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 ||
	    (cm_id->lap_state != IB_CM_LAP_RCVD &&
	     cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
		ret = -EINVAL;
		goto out;
	}

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

	cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
		      info, info_length, private_data, private_data_len);
3286
	ret = ib_post_send_mad(msg, NULL);
3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
	if (ret) {
		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
		cm_free_msg(msg);
		return ret;
	}

	cm_id->lap_state = IB_CM_LAP_IDLE;
out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
	return ret;
}
EXPORT_SYMBOL(ib_send_cm_apr);

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

	apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
	cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
				   apr_msg->local_comm_id);
	if (!cm_id_priv)
		return -EINVAL; /* Unmatched reply. */

	work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
	work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
	work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
	work->cm_event.private_data = &apr_msg->private_data;

3316
	spin_lock_irq(&cm_id_priv->lock);
3317 3318 3319
	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)) {
3320
		spin_unlock_irq(&cm_id_priv->lock);
3321 3322 3323
		goto out;
	}
	cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
3324
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3325 3326 3327 3328 3329
	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);
3330
	spin_unlock_irq(&cm_id_priv->lock);
3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348

	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;

	timewait_info = (struct cm_timewait_info *)work;
3349 3350 3351
	spin_lock_irq(&cm.lock);
	list_del(&timewait_info->list);
	spin_unlock_irq(&cm.lock);
3352 3353 3354 3355 3356 3357

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

3358
	spin_lock_irq(&cm_id_priv->lock);
3359 3360
	if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
	    cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
3361
		spin_unlock_irq(&cm_id_priv->lock);
3362 3363 3364 3365 3366 3367
		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);
3368
	spin_unlock_irq(&cm_id_priv->lock);
3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386

	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,
			  cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
	sidr_req_msg->request_id = cm_id_priv->id.local_id;
R
Roland Dreier 已提交
3387
	sidr_req_msg->pkey = param->path->pkey;
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
	sidr_req_msg->service_id = param->service_id;

	if (param->private_data && param->private_data_len)
		memcpy(sidr_req_msg->private_data, param->private_data,
		       param->private_data_len);
}

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);
3408
	ret = cm_init_av_by_path(param->path, &cm_id_priv->av, cm_id_priv);
3409 3410 3411 3412
	if (ret)
		goto out;

	cm_id->service_id = param->service_id;
3413
	cm_id->service_mask = ~cpu_to_be64(0);
3414 3415 3416 3417 3418 3419 3420 3421
	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);
3422
	msg->timeout_ms = cm_id_priv->timeout_ms;
3423 3424 3425 3426
	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)
3427
		ret = ib_post_send_mad(msg, NULL);
3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452
	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,
				     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;
3453
	param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
3454
	param->listen_id = listen_id;
3455
	param->service_id = sidr_req_msg->service_id;
3456
	param->bth_pkey = cm_get_bth_pkey(work);
3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
	param->port = work->port->port_num;
	work->cm_event.private_data = &sidr_req_msg->private_data;
}

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 已提交
3467
	int ret;
3468

3469
	cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
3470 3471 3472 3473 3474 3475 3476 3477
	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;
3478
	cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
3479
	cm_id_priv->av.dgid.global.interface_id = 0;
P
Parav Pandit 已提交
3480 3481 3482 3483 3484 3485
	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;

3486 3487 3488 3489
	cm_id_priv->id.remote_id = sidr_req_msg->request_id;
	cm_id_priv->tid = sidr_req_msg->hdr.tid;
	atomic_inc(&cm_id_priv->work_count);

3490
	spin_lock_irq(&cm.lock);
3491 3492
	cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
	if (cur_cm_id_priv) {
3493
		spin_unlock_irq(&cm.lock);
3494 3495
		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
				counter[CM_SIDR_REQ_COUNTER]);
3496 3497
		goto out; /* Duplicate message. */
	}
3498
	cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3499
	cur_cm_id_priv = cm_find_listen(cm_id->device,
H
Haggai Eran 已提交
3500
					sidr_req_msg->service_id);
3501
	if (!cur_cm_id_priv) {
3502
		spin_unlock_irq(&cm.lock);
3503
		cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
3504 3505 3506
		goto out; /* No match. */
	}
	atomic_inc(&cur_cm_id_priv->refcount);
3507
	atomic_inc(&cm_id_priv->refcount);
3508
	spin_unlock_irq(&cm.lock);
3509 3510 3511 3512

	cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
	cm_id_priv->id.context = cur_cm_id_priv->id.context;
	cm_id_priv->id.service_id = sidr_req_msg->service_id;
3513
	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569

	cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
	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);
	sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
	sidr_rep_msg->status = param->status;
	cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
	sidr_rep_msg->service_id = cm_id_priv->id.service_id;
	sidr_rep_msg->qkey = cpu_to_be32(param->qkey);

	if (param->info && param->info_length)
		memcpy(sidr_rep_msg->info, param->info, param->info_length);

	if (param->private_data && param->private_data_len)
		memcpy(sidr_rep_msg->private_data, param->private_data,
		       param->private_data_len);
}

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);
3570
	ret = ib_post_send_mad(msg, NULL);
3571 3572 3573 3574 3575 3576 3577 3578 3579
	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);
3580 3581 3582 3583
	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);
	}
3584 3585 3586 3587 3588 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 3617 3618
	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);

static void cm_format_sidr_rep_event(struct cm_work *work)
{
	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;
	param->status = sidr_rep_msg->status;
	param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
	param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
	param->info = &sidr_rep_msg->info;
	param->info_len = sidr_rep_msg->info_length;
	work->cm_event.private_data = &sidr_rep_msg->private_data;
}

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;
	cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
	if (!cm_id_priv)
		return -EINVAL; /* Unmatched reply. */

3619
	spin_lock_irq(&cm_id_priv->lock);
3620
	if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3621
		spin_unlock_irq(&cm_id_priv->lock);
3622 3623 3624
		goto out;
	}
	cm_id_priv->id.state = IB_CM_IDLE;
3625
	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3626
	spin_unlock_irq(&cm_id_priv->lock);
3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647

	cm_format_sidr_rep_event(work);
	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. */
3648
	spin_lock_irq(&cm_id_priv->lock);
3649 3650 3651 3652
	state = (enum ib_cm_state) (unsigned long) msg->context[1];
	if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
		goto discard;

3653 3654
	pr_debug_ratelimited("CM: failed sending MAD in state %d. (%s)\n",
			     state, ib_wc_status_msg(wc_status));
3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676
	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;
	}
3677
	spin_unlock_irq(&cm_id_priv->lock);
3678 3679 3680 3681 3682 3683 3684 3685 3686
	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:
3687
	spin_unlock_irq(&cm_id_priv->lock);
3688 3689 3690 3691 3692 3693
	cm_free_msg(msg);
}

static void cm_send_handler(struct ib_mad_agent *mad_agent,
			    struct ib_mad_send_wc *mad_send_wc)
{
3694
	struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
	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]);
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730

	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 已提交
3731
static void cm_work_handler(struct work_struct *_work)
3732
{
D
David Howells 已提交
3733
	struct cm_work *work = container_of(_work, struct cm_work, work.work);
3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776
	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 已提交
3777
		pr_debug("cm_event.event: 0x%x\n", work->cm_event.event);
3778 3779 3780 3781 3782 3783 3784
		ret = -EINVAL;
		break;
	}
	if (ret)
		cm_free_work(work);
}

3785
static int cm_establish(struct ib_cm_id *cm_id)
3786 3787 3788 3789 3790
{
	struct cm_id_private *cm_id_priv;
	struct cm_work *work;
	unsigned long flags;
	int ret = 0;
3791 3792 3793 3794 3795
	struct cm_device *cm_dev;

	cm_dev = ib_get_client_data(cm_id->device, &cm_client);
	if (!cm_dev)
		return -ENODEV;
3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812

	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 已提交
3813 3814
		pr_debug("%s: local_id %d, cm_id->state: %d\n", __func__,
			 be32_to_cpu(cm_id->local_id), cm_id->state);
3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830
		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 已提交
3831
	INIT_DELAYED_WORK(&work->work, cm_work_handler);
3832 3833 3834 3835
	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;
3836 3837

	/* Check if the device started its remove_one */
3838
	spin_lock_irqsave(&cm.lock, flags);
3839 3840 3841 3842 3843 3844
	if (!cm_dev->going_down) {
		queue_delayed_work(cm.wq, &work->work, 0);
	} else {
		kfree(work);
		ret = -ENODEV;
	}
3845
	spin_unlock_irqrestore(&cm.lock, flags);
3846

3847 3848 3849
out:
	return ret;
}
3850 3851 3852 3853

static int cm_migrate(struct ib_cm_id *cm_id)
{
	struct cm_id_private *cm_id_priv;
3854
	struct cm_av tmp_av;
3855
	unsigned long flags;
3856
	int tmp_send_port_not_ready;
3857 3858 3859 3860 3861 3862 3863 3864
	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;
3865 3866
		/* Swap address vector */
		tmp_av = cm_id_priv->av;
3867
		cm_id_priv->av = cm_id_priv->alt_av;
3868 3869 3870 3871 3872
		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;
3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896
	} 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);
3897 3898

static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3899
			    struct ib_mad_send_buf *send_buf,
3900 3901
			    struct ib_mad_recv_wc *mad_recv_wc)
{
3902
	struct cm_port *port = mad_agent->context;
3903 3904
	struct cm_work *work;
	enum ib_cm_event_type event;
3905
	bool alt_path = false;
3906
	u16 attr_id;
3907
	int paths = 0;
3908
	int going_down = 0;
3909 3910 3911

	switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
	case CM_REQ_ATTR_ID:
3912 3913 3914
		alt_path = cm_req_has_alt_path((struct cm_req_msg *)
						mad_recv_wc->recv_buf.mad);
		paths = 1 + (alt_path != 0);
3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952
		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;
	}

3953 3954 3955 3956
	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]);

3957
	work = kmalloc(sizeof(*work) + sizeof(struct sa_path_rec) * paths,
3958 3959 3960 3961 3962 3963
		       GFP_KERNEL);
	if (!work) {
		ib_free_recv_mad(mad_recv_wc);
		return;
	}

D
David Howells 已提交
3964
	INIT_DELAYED_WORK(&work->work, cm_work_handler);
3965 3966
	work->cm_event.event = event;
	work->mad_recv_wc = mad_recv_wc;
3967
	work->port = port;
3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980

	/* 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);
	}
3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
}

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;
4003
		qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
4004
		if (cm_id_priv->responder_resources)
4005 4006
			qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
						    IB_ACCESS_REMOTE_ATOMIC;
4007 4008 4009 4010 4011
		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 已提交
4012 4013 4014
		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);
4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038
		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 |
4039
				IB_QP_DEST_QPN | IB_QP_RQ_PSN;
4040 4041 4042 4043
		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 已提交
4044 4045
		if (cm_id_priv->qp_type == IB_QPT_RC ||
		    cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
4046 4047 4048 4049 4050 4051
			*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;
		}
4052
		if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
4053
			*qp_attr_mask |= IB_QP_ALT_PATH;
4054
			qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
4055
			qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
4056
			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4057 4058 4059 4060 4061
			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
		}
		ret = 0;
		break;
	default:
D
Daniel Jurgens 已提交
4062 4063 4064
		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);
4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080
		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) {
4081 4082 4083 4084
	/* Allow transition to RTS before sending REP */
	case IB_CM_REQ_RCVD:
	case IB_CM_MRA_REQ_SENT:

4085 4086 4087 4088 4089
	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:
4090 4091 4092
		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 已提交
4093 4094 4095 4096
			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 |
4097 4098 4099
						 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 已提交
4100 4101 4102 4103 4104 4105 4106 4107
				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;
4108
			}
4109
			if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
4110 4111 4112 4113 4114 4115 4116
				*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;
4117
			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4118
			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
4119 4120 4121 4122 4123
			qp_attr->path_mig_state = IB_MIG_REARM;
		}
		ret = 0;
		break;
	default:
D
Daniel Jurgens 已提交
4124 4125 4126
		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);
4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159
		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);

4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172
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]));
}

4173
static const struct sysfs_ops cm_counter_ops = {
4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193
	.show = cm_show_counter
};

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

static void cm_release_port_obj(struct kobject *obj)
{
	struct cm_port *cm_port;

	cm_port = container_of(obj, struct cm_port, port_obj);
	kfree(cm_port);
}

static struct kobj_type cm_port_obj_type = {
	.release = cm_release_port_obj
};

4194
static char *cm_devnode(struct device *dev, umode_t *mode)
4195
{
4196 4197
	if (mode)
		*mode = 0666;
4198 4199 4200
	return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
}

4201
struct class cm_class = {
4202
	.owner   = THIS_MODULE,
4203
	.name    = "infiniband_cm",
4204
	.devnode = cm_devnode,
4205 4206 4207 4208 4209 4210 4211 4212
};
EXPORT_SYMBOL(cm_class);

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

	ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
4213
				   &port->cm_dev->device->kobj,
4214 4215 4216 4217 4218 4219 4220 4221 4222
				   "%d", port->port_num);
	if (ret) {
		kfree(port);
		return ret;
	}

	for (i = 0; i < CM_COUNTER_GROUPS; i++) {
		ret = kobject_init_and_add(&port->counter_group[i].obj,
					   &cm_counter_obj_type,
4223
					   &port->port_obj,
4224 4225 4226 4227 4228 4229 4230 4231 4232
					   "%s", counter_group_names[i]);
		if (ret)
			goto error;
	}

	return 0;

error:
	while (i--)
4233 4234
		kobject_put(&port->counter_group[i].obj);
	kobject_put(&port->port_obj);
4235 4236 4237 4238 4239 4240 4241 4242 4243
	return ret;

}

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

	for (i = 0; i < CM_COUNTER_GROUPS; i++)
4244
		kobject_put(&port->counter_group[i].obj);
4245

4246
	kobject_put(&port->port_obj);
4247 4248
}

4249
static void cm_add_one(struct ib_device *ib_device)
4250 4251 4252 4253 4254
{
	struct cm_device *cm_dev;
	struct cm_port *port;
	struct ib_mad_reg_req reg_req = {
		.mgmt_class = IB_MGMT_CLASS_CM,
4255
		.mgmt_class_version = IB_CM_CLASS_VERSION,
4256 4257 4258 4259 4260 4261
	};
	struct ib_port_modify port_modify = {
		.set_port_cap_mask = IB_PORT_CM_SUP
	};
	unsigned long flags;
	int ret;
M
Michael Wang 已提交
4262
	int count = 0;
4263 4264
	u8 i;

4265
	cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
4266
			 ib_device->phys_port_cnt, GFP_KERNEL);
4267 4268 4269
	if (!cm_dev)
		return;

4270
	cm_dev->ib_device = ib_device;
4271
	cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
4272
	cm_dev->going_down = 0;
4273 4274 4275
	cm_dev->device = device_create(&cm_class, &ib_device->dev,
				       MKDEV(0, 0), NULL,
				       "%s", ib_device->name);
4276
	if (IS_ERR(cm_dev->device)) {
4277 4278 4279 4280
		kfree(cm_dev);
		return;
	}

4281
	set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
4282
	for (i = 1; i <= ib_device->phys_port_cnt; i++) {
4283
		if (!rdma_cap_ib_cm(ib_device, i))
M
Michael Wang 已提交
4284 4285
			continue;

4286 4287 4288 4289 4290
		port = kzalloc(sizeof *port, GFP_KERNEL);
		if (!port)
			goto error1;

		cm_dev->port[i-1] = port;
4291 4292
		port->cm_dev = cm_dev;
		port->port_num = i;
4293

4294 4295 4296
		INIT_LIST_HEAD(&port->cm_priv_prim_list);
		INIT_LIST_HEAD(&port->cm_priv_altr_list);

4297 4298 4299 4300
		ret = cm_create_port_fs(port);
		if (ret)
			goto error1;

4301
		port->mad_agent = ib_register_mad_agent(ib_device, i,
4302 4303 4304 4305 4306
							IB_QPT_GSI,
							&reg_req,
							0,
							cm_send_handler,
							cm_recv_handler,
4307 4308
							port,
							0);
4309
		if (IS_ERR(port->mad_agent))
4310
			goto error2;
4311

4312
		ret = ib_modify_port(ib_device, i, 0, &port_modify);
4313
		if (ret)
4314
			goto error3;
M
Michael Wang 已提交
4315 4316

		count++;
4317
	}
M
Michael Wang 已提交
4318 4319 4320 4321

	if (!count)
		goto free;

4322
	ib_set_client_data(ib_device, &cm_client, cm_dev);
4323 4324 4325 4326 4327 4328

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

4329
error3:
4330
	ib_unregister_mad_agent(port->mad_agent);
4331 4332
error2:
	cm_remove_port_fs(port);
4333
error1:
4334 4335 4336
	port_modify.set_port_cap_mask = 0;
	port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
	while (--i) {
4337
		if (!rdma_cap_ib_cm(ib_device, i))
M
Michael Wang 已提交
4338 4339
			continue;

4340
		port = cm_dev->port[i-1];
4341
		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4342
		ib_unregister_mad_agent(port->mad_agent);
4343
		cm_remove_port_fs(port);
4344
	}
M
Michael Wang 已提交
4345
free:
4346
	device_unregister(cm_dev->device);
4347
	kfree(cm_dev);
4348 4349
}

4350
static void cm_remove_one(struct ib_device *ib_device, void *client_data)
4351
{
4352
	struct cm_device *cm_dev = client_data;
4353
	struct cm_port *port;
4354 4355
	struct cm_id_private *cm_id_priv;
	struct ib_mad_agent *cur_mad_agent;
4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368
	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);

4369 4370 4371 4372
	spin_lock_irq(&cm.lock);
	cm_dev->going_down = 1;
	spin_unlock_irq(&cm.lock);

4373
	for (i = 1; i <= ib_device->phys_port_cnt; i++) {
4374
		if (!rdma_cap_ib_cm(ib_device, i))
M
Michael Wang 已提交
4375 4376
			continue;

4377
		port = cm_dev->port[i-1];
4378
		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4379 4380 4381 4382 4383 4384 4385
		/* 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);
4386 4387 4388 4389 4390
		/*
		 * 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
		 */
4391
		flush_workqueue(cm.wq);
4392 4393 4394 4395 4396
		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);
4397
		cm_remove_port_fs(port);
4398
	}
4399

4400
	device_unregister(cm_dev->device);
4401
	kfree(cm_dev);
4402 4403 4404 4405 4406 4407 4408 4409 4410 4411
}

static int __init ib_cm_init(void)
{
	int ret;

	memset(&cm, 0, sizeof cm);
	INIT_LIST_HEAD(&cm.device_list);
	rwlock_init(&cm.device_lock);
	spin_lock_init(&cm.lock);
4412
	spin_lock_init(&cm.state_lock);
4413
	cm.listen_service_table = RB_ROOT;
4414
	cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
4415 4416 4417 4418
	cm.remote_id_table = RB_ROOT;
	cm.remote_qp_table = RB_ROOT;
	cm.remote_sidr_table = RB_ROOT;
	idr_init(&cm.local_id_table);
S
Sean Hefty 已提交
4419
	get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
4420
	INIT_LIST_HEAD(&cm.timewait_list);
4421

4422
	ret = class_register(&cm_class);
4423 4424 4425 4426
	if (ret) {
		ret = -ENOMEM;
		goto error1;
	}
4427

4428
	cm.wq = alloc_workqueue("ib_cm", 0, 1);
4429 4430
	if (!cm.wq) {
		ret = -ENOMEM;
4431
		goto error2;
4432 4433
	}

4434 4435
	ret = ib_register_client(&cm_client);
	if (ret)
4436
		goto error3;
4437 4438

	return 0;
4439
error3:
4440
	destroy_workqueue(cm.wq);
4441
error2:
4442
	class_unregister(&cm_class);
4443 4444
error1:
	idr_destroy(&cm.local_id_table);
4445 4446 4447 4448 4449
	return ret;
}

static void __exit ib_cm_cleanup(void)
{
4450 4451 4452 4453 4454 4455 4456
	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);

4457
	ib_unregister_client(&cm_client);
4458
	destroy_workqueue(cm.wq);
4459 4460 4461 4462 4463 4464

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

4465
	class_unregister(&cm_class);
4466
	idr_destroy(&cm.local_id_table);
4467 4468 4469 4470 4471
}

module_init(ib_cm_init);
module_exit(ib_cm_cleanup);