PageRenderTime 115ms CodeModel.GetById 19ms app.highlight 86ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/infiniband/core/cm.c

https://bitbucket.org/ndreys/linux-sunxi
C | 3875 lines | 3297 code | 482 blank | 96 comment | 439 complexity | 9ba594543d5101f200f1c1152ab8cdaf MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * Copyright (c) 2004-2007 Intel Corporation.  All rights reserved.
   3 * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
   4 * Copyright (c) 2004, 2005 Voltaire Corporation.  All rights reserved.
   5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
   6 *
   7 * This software is available to you under a choice of one of two
   8 * licenses.  You may choose to be licensed under the terms of the GNU
   9 * General Public License (GPL) Version 2, available from the file
  10 * COPYING in the main directory of this source tree, or the
  11 * OpenIB.org BSD license below:
  12 *
  13 *     Redistribution and use in source and binary forms, with or
  14 *     without modification, are permitted provided that the following
  15 *     conditions are met:
  16 *
  17 *      - Redistributions of source code must retain the above
  18 *        copyright notice, this list of conditions and the following
  19 *        disclaimer.
  20 *
  21 *      - Redistributions in binary form must reproduce the above
  22 *        copyright notice, this list of conditions and the following
  23 *        disclaimer in the documentation and/or other materials
  24 *        provided with the distribution.
  25 *
  26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33 * SOFTWARE.
  34 */
  35
  36#include <linux/completion.h>
  37#include <linux/dma-mapping.h>
  38#include <linux/device.h>
  39#include <linux/err.h>
  40#include <linux/idr.h>
  41#include <linux/interrupt.h>
  42#include <linux/random.h>
  43#include <linux/rbtree.h>
  44#include <linux/spinlock.h>
  45#include <linux/slab.h>
  46#include <linux/sysfs.h>
  47#include <linux/workqueue.h>
  48#include <linux/kdev_t.h>
  49
  50#include <rdma/ib_cache.h>
  51#include <rdma/ib_cm.h>
  52#include "cm_msgs.h"
  53
  54MODULE_AUTHOR("Sean Hefty");
  55MODULE_DESCRIPTION("InfiniBand CM");
  56MODULE_LICENSE("Dual BSD/GPL");
  57
  58static void cm_add_one(struct ib_device *device);
  59static void cm_remove_one(struct ib_device *device);
  60
  61static struct ib_client cm_client = {
  62	.name   = "cm",
  63	.add    = cm_add_one,
  64	.remove = cm_remove_one
  65};
  66
  67static struct ib_cm {
  68	spinlock_t lock;
  69	struct list_head device_list;
  70	rwlock_t device_lock;
  71	struct rb_root listen_service_table;
  72	u64 listen_service_id;
  73	/* struct rb_root peer_service_table; todo: fix peer to peer */
  74	struct rb_root remote_qp_table;
  75	struct rb_root remote_id_table;
  76	struct rb_root remote_sidr_table;
  77	struct idr local_id_table;
  78	__be32 random_id_operand;
  79	struct list_head timewait_list;
  80	struct workqueue_struct *wq;
  81} cm;
  82
  83/* Counter indexes ordered by attribute ID */
  84enum {
  85	CM_REQ_COUNTER,
  86	CM_MRA_COUNTER,
  87	CM_REJ_COUNTER,
  88	CM_REP_COUNTER,
  89	CM_RTU_COUNTER,
  90	CM_DREQ_COUNTER,
  91	CM_DREP_COUNTER,
  92	CM_SIDR_REQ_COUNTER,
  93	CM_SIDR_REP_COUNTER,
  94	CM_LAP_COUNTER,
  95	CM_APR_COUNTER,
  96	CM_ATTR_COUNT,
  97	CM_ATTR_ID_OFFSET = 0x0010,
  98};
  99
 100enum {
 101	CM_XMIT,
 102	CM_XMIT_RETRIES,
 103	CM_RECV,
 104	CM_RECV_DUPLICATES,
 105	CM_COUNTER_GROUPS
 106};
 107
 108static char const counter_group_names[CM_COUNTER_GROUPS]
 109				     [sizeof("cm_rx_duplicates")] = {
 110	"cm_tx_msgs", "cm_tx_retries",
 111	"cm_rx_msgs", "cm_rx_duplicates"
 112};
 113
 114struct cm_counter_group {
 115	struct kobject obj;
 116	atomic_long_t counter[CM_ATTR_COUNT];
 117};
 118
 119struct cm_counter_attribute {
 120	struct attribute attr;
 121	int index;
 122};
 123
 124#define CM_COUNTER_ATTR(_name, _index) \
 125struct cm_counter_attribute cm_##_name##_counter_attr = { \
 126	.attr = { .name = __stringify(_name), .mode = 0444 }, \
 127	.index = _index \
 128}
 129
 130static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
 131static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
 132static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
 133static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
 134static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
 135static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
 136static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
 137static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
 138static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
 139static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
 140static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
 141
 142static struct attribute *cm_counter_default_attrs[] = {
 143	&cm_req_counter_attr.attr,
 144	&cm_mra_counter_attr.attr,
 145	&cm_rej_counter_attr.attr,
 146	&cm_rep_counter_attr.attr,
 147	&cm_rtu_counter_attr.attr,
 148	&cm_dreq_counter_attr.attr,
 149	&cm_drep_counter_attr.attr,
 150	&cm_sidr_req_counter_attr.attr,
 151	&cm_sidr_rep_counter_attr.attr,
 152	&cm_lap_counter_attr.attr,
 153	&cm_apr_counter_attr.attr,
 154	NULL
 155};
 156
 157struct cm_port {
 158	struct cm_device *cm_dev;
 159	struct ib_mad_agent *mad_agent;
 160	struct kobject port_obj;
 161	u8 port_num;
 162	struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
 163};
 164
 165struct cm_device {
 166	struct list_head list;
 167	struct ib_device *ib_device;
 168	struct device *device;
 169	u8 ack_delay;
 170	struct cm_port *port[0];
 171};
 172
 173struct cm_av {
 174	struct cm_port *port;
 175	union ib_gid dgid;
 176	struct ib_ah_attr ah_attr;
 177	u16 pkey_index;
 178	u8 timeout;
 179};
 180
 181struct cm_work {
 182	struct delayed_work work;
 183	struct list_head list;
 184	struct cm_port *port;
 185	struct ib_mad_recv_wc *mad_recv_wc;	/* Received MADs */
 186	__be32 local_id;			/* Established / timewait */
 187	__be32 remote_id;
 188	struct ib_cm_event cm_event;
 189	struct ib_sa_path_rec path[0];
 190};
 191
 192struct cm_timewait_info {
 193	struct cm_work work;			/* Must be first. */
 194	struct list_head list;
 195	struct rb_node remote_qp_node;
 196	struct rb_node remote_id_node;
 197	__be64 remote_ca_guid;
 198	__be32 remote_qpn;
 199	u8 inserted_remote_qp;
 200	u8 inserted_remote_id;
 201};
 202
 203struct cm_id_private {
 204	struct ib_cm_id	id;
 205
 206	struct rb_node service_node;
 207	struct rb_node sidr_id_node;
 208	spinlock_t lock;	/* Do not acquire inside cm.lock */
 209	struct completion comp;
 210	atomic_t refcount;
 211
 212	struct ib_mad_send_buf *msg;
 213	struct cm_timewait_info *timewait_info;
 214	/* todo: use alternate port on send failure */
 215	struct cm_av av;
 216	struct cm_av alt_av;
 217	struct ib_cm_compare_data *compare_data;
 218
 219	void *private_data;
 220	__be64 tid;
 221	__be32 local_qpn;
 222	__be32 remote_qpn;
 223	enum ib_qp_type qp_type;
 224	__be32 sq_psn;
 225	__be32 rq_psn;
 226	int timeout_ms;
 227	enum ib_mtu path_mtu;
 228	__be16 pkey;
 229	u8 private_data_len;
 230	u8 max_cm_retries;
 231	u8 peer_to_peer;
 232	u8 responder_resources;
 233	u8 initiator_depth;
 234	u8 retry_count;
 235	u8 rnr_retry_count;
 236	u8 service_timeout;
 237	u8 target_ack_delay;
 238
 239	struct list_head work_list;
 240	atomic_t work_count;
 241};
 242
 243static void cm_work_handler(struct work_struct *work);
 244
 245static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
 246{
 247	if (atomic_dec_and_test(&cm_id_priv->refcount))
 248		complete(&cm_id_priv->comp);
 249}
 250
 251static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
 252			struct ib_mad_send_buf **msg)
 253{
 254	struct ib_mad_agent *mad_agent;
 255	struct ib_mad_send_buf *m;
 256	struct ib_ah *ah;
 257
 258	mad_agent = cm_id_priv->av.port->mad_agent;
 259	ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
 260	if (IS_ERR(ah))
 261		return PTR_ERR(ah);
 262
 263	m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
 264			       cm_id_priv->av.pkey_index,
 265			       0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
 266			       GFP_ATOMIC);
 267	if (IS_ERR(m)) {
 268		ib_destroy_ah(ah);
 269		return PTR_ERR(m);
 270	}
 271
 272	/* Timeout set by caller if response is expected. */
 273	m->ah = ah;
 274	m->retries = cm_id_priv->max_cm_retries;
 275
 276	atomic_inc(&cm_id_priv->refcount);
 277	m->context[0] = cm_id_priv;
 278	*msg = m;
 279	return 0;
 280}
 281
 282static int cm_alloc_response_msg(struct cm_port *port,
 283				 struct ib_mad_recv_wc *mad_recv_wc,
 284				 struct ib_mad_send_buf **msg)
 285{
 286	struct ib_mad_send_buf *m;
 287	struct ib_ah *ah;
 288
 289	ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
 290				  mad_recv_wc->recv_buf.grh, port->port_num);
 291	if (IS_ERR(ah))
 292		return PTR_ERR(ah);
 293
 294	m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
 295			       0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
 296			       GFP_ATOMIC);
 297	if (IS_ERR(m)) {
 298		ib_destroy_ah(ah);
 299		return PTR_ERR(m);
 300	}
 301	m->ah = ah;
 302	*msg = m;
 303	return 0;
 304}
 305
 306static void cm_free_msg(struct ib_mad_send_buf *msg)
 307{
 308	ib_destroy_ah(msg->ah);
 309	if (msg->context[0])
 310		cm_deref_id(msg->context[0]);
 311	ib_free_send_mad(msg);
 312}
 313
 314static void * cm_copy_private_data(const void *private_data,
 315				   u8 private_data_len)
 316{
 317	void *data;
 318
 319	if (!private_data || !private_data_len)
 320		return NULL;
 321
 322	data = kmemdup(private_data, private_data_len, GFP_KERNEL);
 323	if (!data)
 324		return ERR_PTR(-ENOMEM);
 325
 326	return data;
 327}
 328
 329static void cm_set_private_data(struct cm_id_private *cm_id_priv,
 330				 void *private_data, u8 private_data_len)
 331{
 332	if (cm_id_priv->private_data && cm_id_priv->private_data_len)
 333		kfree(cm_id_priv->private_data);
 334
 335	cm_id_priv->private_data = private_data;
 336	cm_id_priv->private_data_len = private_data_len;
 337}
 338
 339static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
 340				    struct ib_grh *grh, struct cm_av *av)
 341{
 342	av->port = port;
 343	av->pkey_index = wc->pkey_index;
 344	ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
 345			   grh, &av->ah_attr);
 346}
 347
 348static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
 349{
 350	struct cm_device *cm_dev;
 351	struct cm_port *port = NULL;
 352	unsigned long flags;
 353	int ret;
 354	u8 p;
 355
 356	read_lock_irqsave(&cm.device_lock, flags);
 357	list_for_each_entry(cm_dev, &cm.device_list, list) {
 358		if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
 359					&p, NULL)) {
 360			port = cm_dev->port[p-1];
 361			break;
 362		}
 363	}
 364	read_unlock_irqrestore(&cm.device_lock, flags);
 365
 366	if (!port)
 367		return -EINVAL;
 368
 369	ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
 370				  be16_to_cpu(path->pkey), &av->pkey_index);
 371	if (ret)
 372		return ret;
 373
 374	av->port = port;
 375	ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
 376			     &av->ah_attr);
 377	av->timeout = path->packet_life_time + 1;
 378	return 0;
 379}
 380
 381static int cm_alloc_id(struct cm_id_private *cm_id_priv)
 382{
 383	unsigned long flags;
 384	int ret, id;
 385	static int next_id;
 386
 387	do {
 388		spin_lock_irqsave(&cm.lock, flags);
 389		ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
 390					next_id, &id);
 391		if (!ret)
 392			next_id = ((unsigned) id + 1) & MAX_ID_MASK;
 393		spin_unlock_irqrestore(&cm.lock, flags);
 394	} while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
 395
 396	cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
 397	return ret;
 398}
 399
 400static void cm_free_id(__be32 local_id)
 401{
 402	spin_lock_irq(&cm.lock);
 403	idr_remove(&cm.local_id_table,
 404		   (__force int) (local_id ^ cm.random_id_operand));
 405	spin_unlock_irq(&cm.lock);
 406}
 407
 408static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
 409{
 410	struct cm_id_private *cm_id_priv;
 411
 412	cm_id_priv = idr_find(&cm.local_id_table,
 413			      (__force int) (local_id ^ cm.random_id_operand));
 414	if (cm_id_priv) {
 415		if (cm_id_priv->id.remote_id == remote_id)
 416			atomic_inc(&cm_id_priv->refcount);
 417		else
 418			cm_id_priv = NULL;
 419	}
 420
 421	return cm_id_priv;
 422}
 423
 424static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
 425{
 426	struct cm_id_private *cm_id_priv;
 427
 428	spin_lock_irq(&cm.lock);
 429	cm_id_priv = cm_get_id(local_id, remote_id);
 430	spin_unlock_irq(&cm.lock);
 431
 432	return cm_id_priv;
 433}
 434
 435static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
 436{
 437	int i;
 438
 439	for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
 440		((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
 441					     ((unsigned long *) mask)[i];
 442}
 443
 444static int cm_compare_data(struct ib_cm_compare_data *src_data,
 445			   struct ib_cm_compare_data *dst_data)
 446{
 447	u8 src[IB_CM_COMPARE_SIZE];
 448	u8 dst[IB_CM_COMPARE_SIZE];
 449
 450	if (!src_data || !dst_data)
 451		return 0;
 452
 453	cm_mask_copy(src, src_data->data, dst_data->mask);
 454	cm_mask_copy(dst, dst_data->data, src_data->mask);
 455	return memcmp(src, dst, IB_CM_COMPARE_SIZE);
 456}
 457
 458static int cm_compare_private_data(u8 *private_data,
 459				   struct ib_cm_compare_data *dst_data)
 460{
 461	u8 src[IB_CM_COMPARE_SIZE];
 462
 463	if (!dst_data)
 464		return 0;
 465
 466	cm_mask_copy(src, private_data, dst_data->mask);
 467	return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
 468}
 469
 470/*
 471 * Trivial helpers to strip endian annotation and compare; the
 472 * endianness doesn't actually matter since we just need a stable
 473 * order for the RB tree.
 474 */
 475static int be32_lt(__be32 a, __be32 b)
 476{
 477	return (__force u32) a < (__force u32) b;
 478}
 479
 480static int be32_gt(__be32 a, __be32 b)
 481{
 482	return (__force u32) a > (__force u32) b;
 483}
 484
 485static int be64_lt(__be64 a, __be64 b)
 486{
 487	return (__force u64) a < (__force u64) b;
 488}
 489
 490static int be64_gt(__be64 a, __be64 b)
 491{
 492	return (__force u64) a > (__force u64) b;
 493}
 494
 495static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
 496{
 497	struct rb_node **link = &cm.listen_service_table.rb_node;
 498	struct rb_node *parent = NULL;
 499	struct cm_id_private *cur_cm_id_priv;
 500	__be64 service_id = cm_id_priv->id.service_id;
 501	__be64 service_mask = cm_id_priv->id.service_mask;
 502	int data_cmp;
 503
 504	while (*link) {
 505		parent = *link;
 506		cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
 507					  service_node);
 508		data_cmp = cm_compare_data(cm_id_priv->compare_data,
 509					   cur_cm_id_priv->compare_data);
 510		if ((cur_cm_id_priv->id.service_mask & service_id) ==
 511		    (service_mask & cur_cm_id_priv->id.service_id) &&
 512		    (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
 513		    !data_cmp)
 514			return cur_cm_id_priv;
 515
 516		if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
 517			link = &(*link)->rb_left;
 518		else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
 519			link = &(*link)->rb_right;
 520		else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
 521			link = &(*link)->rb_left;
 522		else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
 523			link = &(*link)->rb_right;
 524		else if (data_cmp < 0)
 525			link = &(*link)->rb_left;
 526		else
 527			link = &(*link)->rb_right;
 528	}
 529	rb_link_node(&cm_id_priv->service_node, parent, link);
 530	rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
 531	return NULL;
 532}
 533
 534static struct cm_id_private * cm_find_listen(struct ib_device *device,
 535					     __be64 service_id,
 536					     u8 *private_data)
 537{
 538	struct rb_node *node = cm.listen_service_table.rb_node;
 539	struct cm_id_private *cm_id_priv;
 540	int data_cmp;
 541
 542	while (node) {
 543		cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
 544		data_cmp = cm_compare_private_data(private_data,
 545						   cm_id_priv->compare_data);
 546		if ((cm_id_priv->id.service_mask & service_id) ==
 547		     cm_id_priv->id.service_id &&
 548		    (cm_id_priv->id.device == device) && !data_cmp)
 549			return cm_id_priv;
 550
 551		if (device < cm_id_priv->id.device)
 552			node = node->rb_left;
 553		else if (device > cm_id_priv->id.device)
 554			node = node->rb_right;
 555		else if (be64_lt(service_id, cm_id_priv->id.service_id))
 556			node = node->rb_left;
 557		else if (be64_gt(service_id, cm_id_priv->id.service_id))
 558			node = node->rb_right;
 559		else if (data_cmp < 0)
 560			node = node->rb_left;
 561		else
 562			node = node->rb_right;
 563	}
 564	return NULL;
 565}
 566
 567static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
 568						     *timewait_info)
 569{
 570	struct rb_node **link = &cm.remote_id_table.rb_node;
 571	struct rb_node *parent = NULL;
 572	struct cm_timewait_info *cur_timewait_info;
 573	__be64 remote_ca_guid = timewait_info->remote_ca_guid;
 574	__be32 remote_id = timewait_info->work.remote_id;
 575
 576	while (*link) {
 577		parent = *link;
 578		cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
 579					     remote_id_node);
 580		if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
 581			link = &(*link)->rb_left;
 582		else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
 583			link = &(*link)->rb_right;
 584		else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
 585			link = &(*link)->rb_left;
 586		else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
 587			link = &(*link)->rb_right;
 588		else
 589			return cur_timewait_info;
 590	}
 591	timewait_info->inserted_remote_id = 1;
 592	rb_link_node(&timewait_info->remote_id_node, parent, link);
 593	rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
 594	return NULL;
 595}
 596
 597static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
 598						   __be32 remote_id)
 599{
 600	struct rb_node *node = cm.remote_id_table.rb_node;
 601	struct cm_timewait_info *timewait_info;
 602
 603	while (node) {
 604		timewait_info = rb_entry(node, struct cm_timewait_info,
 605					 remote_id_node);
 606		if (be32_lt(remote_id, timewait_info->work.remote_id))
 607			node = node->rb_left;
 608		else if (be32_gt(remote_id, timewait_info->work.remote_id))
 609			node = node->rb_right;
 610		else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
 611			node = node->rb_left;
 612		else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
 613			node = node->rb_right;
 614		else
 615			return timewait_info;
 616	}
 617	return NULL;
 618}
 619
 620static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
 621						      *timewait_info)
 622{
 623	struct rb_node **link = &cm.remote_qp_table.rb_node;
 624	struct rb_node *parent = NULL;
 625	struct cm_timewait_info *cur_timewait_info;
 626	__be64 remote_ca_guid = timewait_info->remote_ca_guid;
 627	__be32 remote_qpn = timewait_info->remote_qpn;
 628
 629	while (*link) {
 630		parent = *link;
 631		cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
 632					     remote_qp_node);
 633		if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
 634			link = &(*link)->rb_left;
 635		else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
 636			link = &(*link)->rb_right;
 637		else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
 638			link = &(*link)->rb_left;
 639		else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
 640			link = &(*link)->rb_right;
 641		else
 642			return cur_timewait_info;
 643	}
 644	timewait_info->inserted_remote_qp = 1;
 645	rb_link_node(&timewait_info->remote_qp_node, parent, link);
 646	rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
 647	return NULL;
 648}
 649
 650static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
 651						    *cm_id_priv)
 652{
 653	struct rb_node **link = &cm.remote_sidr_table.rb_node;
 654	struct rb_node *parent = NULL;
 655	struct cm_id_private *cur_cm_id_priv;
 656	union ib_gid *port_gid = &cm_id_priv->av.dgid;
 657	__be32 remote_id = cm_id_priv->id.remote_id;
 658
 659	while (*link) {
 660		parent = *link;
 661		cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
 662					  sidr_id_node);
 663		if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
 664			link = &(*link)->rb_left;
 665		else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
 666			link = &(*link)->rb_right;
 667		else {
 668			int cmp;
 669			cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
 670				     sizeof *port_gid);
 671			if (cmp < 0)
 672				link = &(*link)->rb_left;
 673			else if (cmp > 0)
 674				link = &(*link)->rb_right;
 675			else
 676				return cur_cm_id_priv;
 677		}
 678	}
 679	rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
 680	rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
 681	return NULL;
 682}
 683
 684static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
 685			       enum ib_cm_sidr_status status)
 686{
 687	struct ib_cm_sidr_rep_param param;
 688
 689	memset(&param, 0, sizeof param);
 690	param.status = status;
 691	ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
 692}
 693
 694struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
 695				 ib_cm_handler cm_handler,
 696				 void *context)
 697{
 698	struct cm_id_private *cm_id_priv;
 699	int ret;
 700
 701	cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
 702	if (!cm_id_priv)
 703		return ERR_PTR(-ENOMEM);
 704
 705	cm_id_priv->id.state = IB_CM_IDLE;
 706	cm_id_priv->id.device = device;
 707	cm_id_priv->id.cm_handler = cm_handler;
 708	cm_id_priv->id.context = context;
 709	cm_id_priv->id.remote_cm_qpn = 1;
 710	ret = cm_alloc_id(cm_id_priv);
 711	if (ret)
 712		goto error;
 713
 714	spin_lock_init(&cm_id_priv->lock);
 715	init_completion(&cm_id_priv->comp);
 716	INIT_LIST_HEAD(&cm_id_priv->work_list);
 717	atomic_set(&cm_id_priv->work_count, -1);
 718	atomic_set(&cm_id_priv->refcount, 1);
 719	return &cm_id_priv->id;
 720
 721error:
 722	kfree(cm_id_priv);
 723	return ERR_PTR(-ENOMEM);
 724}
 725EXPORT_SYMBOL(ib_create_cm_id);
 726
 727static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
 728{
 729	struct cm_work *work;
 730
 731	if (list_empty(&cm_id_priv->work_list))
 732		return NULL;
 733
 734	work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
 735	list_del(&work->list);
 736	return work;
 737}
 738
 739static void cm_free_work(struct cm_work *work)
 740{
 741	if (work->mad_recv_wc)
 742		ib_free_recv_mad(work->mad_recv_wc);
 743	kfree(work);
 744}
 745
 746static inline int cm_convert_to_ms(int iba_time)
 747{
 748	/* approximate conversion to ms from 4.096us x 2^iba_time */
 749	return 1 << max(iba_time - 8, 0);
 750}
 751
 752/*
 753 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
 754 * Because of how ack_timeout is stored, adding one doubles the timeout.
 755 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
 756 * increment it (round up) only if the other is within 50%.
 757 */
 758static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
 759{
 760	int ack_timeout = packet_life_time + 1;
 761
 762	if (ack_timeout >= ca_ack_delay)
 763		ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
 764	else
 765		ack_timeout = ca_ack_delay +
 766			      (ack_timeout >= (ca_ack_delay - 1));
 767
 768	return min(31, ack_timeout);
 769}
 770
 771static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
 772{
 773	if (timewait_info->inserted_remote_id) {
 774		rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
 775		timewait_info->inserted_remote_id = 0;
 776	}
 777
 778	if (timewait_info->inserted_remote_qp) {
 779		rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
 780		timewait_info->inserted_remote_qp = 0;
 781	}
 782}
 783
 784static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
 785{
 786	struct cm_timewait_info *timewait_info;
 787
 788	timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
 789	if (!timewait_info)
 790		return ERR_PTR(-ENOMEM);
 791
 792	timewait_info->work.local_id = local_id;
 793	INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
 794	timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
 795	return timewait_info;
 796}
 797
 798static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
 799{
 800	int wait_time;
 801	unsigned long flags;
 802
 803	spin_lock_irqsave(&cm.lock, flags);
 804	cm_cleanup_timewait(cm_id_priv->timewait_info);
 805	list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
 806	spin_unlock_irqrestore(&cm.lock, flags);
 807
 808	/*
 809	 * The cm_id could be destroyed by the user before we exit timewait.
 810	 * To protect against this, we search for the cm_id after exiting
 811	 * timewait before notifying the user that we've exited timewait.
 812	 */
 813	cm_id_priv->id.state = IB_CM_TIMEWAIT;
 814	wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
 815	queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
 816			   msecs_to_jiffies(wait_time));
 817	cm_id_priv->timewait_info = NULL;
 818}
 819
 820static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
 821{
 822	unsigned long flags;
 823
 824	cm_id_priv->id.state = IB_CM_IDLE;
 825	if (cm_id_priv->timewait_info) {
 826		spin_lock_irqsave(&cm.lock, flags);
 827		cm_cleanup_timewait(cm_id_priv->timewait_info);
 828		spin_unlock_irqrestore(&cm.lock, flags);
 829		kfree(cm_id_priv->timewait_info);
 830		cm_id_priv->timewait_info = NULL;
 831	}
 832}
 833
 834static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
 835{
 836	struct cm_id_private *cm_id_priv;
 837	struct cm_work *work;
 838
 839	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
 840retest:
 841	spin_lock_irq(&cm_id_priv->lock);
 842	switch (cm_id->state) {
 843	case IB_CM_LISTEN:
 844		cm_id->state = IB_CM_IDLE;
 845		spin_unlock_irq(&cm_id_priv->lock);
 846		spin_lock_irq(&cm.lock);
 847		rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
 848		spin_unlock_irq(&cm.lock);
 849		break;
 850	case IB_CM_SIDR_REQ_SENT:
 851		cm_id->state = IB_CM_IDLE;
 852		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
 853		spin_unlock_irq(&cm_id_priv->lock);
 854		break;
 855	case IB_CM_SIDR_REQ_RCVD:
 856		spin_unlock_irq(&cm_id_priv->lock);
 857		cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
 858		break;
 859	case IB_CM_REQ_SENT:
 860		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
 861		spin_unlock_irq(&cm_id_priv->lock);
 862		ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
 863			       &cm_id_priv->id.device->node_guid,
 864			       sizeof cm_id_priv->id.device->node_guid,
 865			       NULL, 0);
 866		break;
 867	case IB_CM_REQ_RCVD:
 868		if (err == -ENOMEM) {
 869			/* Do not reject to allow future retries. */
 870			cm_reset_to_idle(cm_id_priv);
 871			spin_unlock_irq(&cm_id_priv->lock);
 872		} else {
 873			spin_unlock_irq(&cm_id_priv->lock);
 874			ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
 875				       NULL, 0, NULL, 0);
 876		}
 877		break;
 878	case IB_CM_MRA_REQ_RCVD:
 879	case IB_CM_REP_SENT:
 880	case IB_CM_MRA_REP_RCVD:
 881		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
 882		/* Fall through */
 883	case IB_CM_MRA_REQ_SENT:
 884	case IB_CM_REP_RCVD:
 885	case IB_CM_MRA_REP_SENT:
 886		spin_unlock_irq(&cm_id_priv->lock);
 887		ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
 888			       NULL, 0, NULL, 0);
 889		break;
 890	case IB_CM_ESTABLISHED:
 891		spin_unlock_irq(&cm_id_priv->lock);
 892		ib_send_cm_dreq(cm_id, NULL, 0);
 893		goto retest;
 894	case IB_CM_DREQ_SENT:
 895		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
 896		cm_enter_timewait(cm_id_priv);
 897		spin_unlock_irq(&cm_id_priv->lock);
 898		break;
 899	case IB_CM_DREQ_RCVD:
 900		spin_unlock_irq(&cm_id_priv->lock);
 901		ib_send_cm_drep(cm_id, NULL, 0);
 902		break;
 903	default:
 904		spin_unlock_irq(&cm_id_priv->lock);
 905		break;
 906	}
 907
 908	cm_free_id(cm_id->local_id);
 909	cm_deref_id(cm_id_priv);
 910	wait_for_completion(&cm_id_priv->comp);
 911	while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
 912		cm_free_work(work);
 913	kfree(cm_id_priv->compare_data);
 914	kfree(cm_id_priv->private_data);
 915	kfree(cm_id_priv);
 916}
 917
 918void ib_destroy_cm_id(struct ib_cm_id *cm_id)
 919{
 920	cm_destroy_id(cm_id, 0);
 921}
 922EXPORT_SYMBOL(ib_destroy_cm_id);
 923
 924int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
 925		 struct ib_cm_compare_data *compare_data)
 926{
 927	struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
 928	unsigned long flags;
 929	int ret = 0;
 930
 931	service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
 932	service_id &= service_mask;
 933	if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
 934	    (service_id != IB_CM_ASSIGN_SERVICE_ID))
 935		return -EINVAL;
 936
 937	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
 938	if (cm_id->state != IB_CM_IDLE)
 939		return -EINVAL;
 940
 941	if (compare_data) {
 942		cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
 943						   GFP_KERNEL);
 944		if (!cm_id_priv->compare_data)
 945			return -ENOMEM;
 946		cm_mask_copy(cm_id_priv->compare_data->data,
 947			     compare_data->data, compare_data->mask);
 948		memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
 949		       IB_CM_COMPARE_SIZE);
 950	}
 951
 952	cm_id->state = IB_CM_LISTEN;
 953
 954	spin_lock_irqsave(&cm.lock, flags);
 955	if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
 956		cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
 957		cm_id->service_mask = ~cpu_to_be64(0);
 958	} else {
 959		cm_id->service_id = service_id;
 960		cm_id->service_mask = service_mask;
 961	}
 962	cur_cm_id_priv = cm_insert_listen(cm_id_priv);
 963	spin_unlock_irqrestore(&cm.lock, flags);
 964
 965	if (cur_cm_id_priv) {
 966		cm_id->state = IB_CM_IDLE;
 967		kfree(cm_id_priv->compare_data);
 968		cm_id_priv->compare_data = NULL;
 969		ret = -EBUSY;
 970	}
 971	return ret;
 972}
 973EXPORT_SYMBOL(ib_cm_listen);
 974
 975static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
 976			  enum cm_msg_sequence msg_seq)
 977{
 978	u64 hi_tid, low_tid;
 979
 980	hi_tid   = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
 981	low_tid  = (u64) ((__force u32)cm_id_priv->id.local_id |
 982			  (msg_seq << 30));
 983	return cpu_to_be64(hi_tid | low_tid);
 984}
 985
 986static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
 987			      __be16 attr_id, __be64 tid)
 988{
 989	hdr->base_version  = IB_MGMT_BASE_VERSION;
 990	hdr->mgmt_class	   = IB_MGMT_CLASS_CM;
 991	hdr->class_version = IB_CM_CLASS_VERSION;
 992	hdr->method	   = IB_MGMT_METHOD_SEND;
 993	hdr->attr_id	   = attr_id;
 994	hdr->tid	   = tid;
 995}
 996
 997static void cm_format_req(struct cm_req_msg *req_msg,
 998			  struct cm_id_private *cm_id_priv,
 999			  struct ib_cm_req_param *param)
1000{
1001	struct ib_sa_path_rec *pri_path = param->primary_path;
1002	struct ib_sa_path_rec *alt_path = param->alternate_path;
1003
1004	cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1005			  cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1006
1007	req_msg->local_comm_id = cm_id_priv->id.local_id;
1008	req_msg->service_id = param->service_id;
1009	req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1010	cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
1011	cm_req_set_resp_res(req_msg, param->responder_resources);
1012	cm_req_set_init_depth(req_msg, param->initiator_depth);
1013	cm_req_set_remote_resp_timeout(req_msg,
1014				       param->remote_cm_response_timeout);
1015	cm_req_set_qp_type(req_msg, param->qp_type);
1016	cm_req_set_flow_ctrl(req_msg, param->flow_control);
1017	cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1018	cm_req_set_local_resp_timeout(req_msg,
1019				      param->local_cm_response_timeout);
1020	cm_req_set_retry_count(req_msg, param->retry_count);
1021	req_msg->pkey = param->primary_path->pkey;
1022	cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
1023	cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1024	cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1025	cm_req_set_srq(req_msg, param->srq);
1026
1027	if (pri_path->hop_limit <= 1) {
1028		req_msg->primary_local_lid = pri_path->slid;
1029		req_msg->primary_remote_lid = pri_path->dlid;
1030	} else {
1031		/* Work-around until there's a way to obtain remote LID info */
1032		req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1033		req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1034	}
1035	req_msg->primary_local_gid = pri_path->sgid;
1036	req_msg->primary_remote_gid = pri_path->dgid;
1037	cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1038	cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1039	req_msg->primary_traffic_class = pri_path->traffic_class;
1040	req_msg->primary_hop_limit = pri_path->hop_limit;
1041	cm_req_set_primary_sl(req_msg, pri_path->sl);
1042	cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
1043	cm_req_set_primary_local_ack_timeout(req_msg,
1044		cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1045			       pri_path->packet_life_time));
1046
1047	if (alt_path) {
1048		if (alt_path->hop_limit <= 1) {
1049			req_msg->alt_local_lid = alt_path->slid;
1050			req_msg->alt_remote_lid = alt_path->dlid;
1051		} else {
1052			req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1053			req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1054		}
1055		req_msg->alt_local_gid = alt_path->sgid;
1056		req_msg->alt_remote_gid = alt_path->dgid;
1057		cm_req_set_alt_flow_label(req_msg,
1058					  alt_path->flow_label);
1059		cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1060		req_msg->alt_traffic_class = alt_path->traffic_class;
1061		req_msg->alt_hop_limit = alt_path->hop_limit;
1062		cm_req_set_alt_sl(req_msg, alt_path->sl);
1063		cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
1064		cm_req_set_alt_local_ack_timeout(req_msg,
1065			cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1066				       alt_path->packet_life_time));
1067	}
1068
1069	if (param->private_data && param->private_data_len)
1070		memcpy(req_msg->private_data, param->private_data,
1071		       param->private_data_len);
1072}
1073
1074static int cm_validate_req_param(struct ib_cm_req_param *param)
1075{
1076	/* peer-to-peer not supported */
1077	if (param->peer_to_peer)
1078		return -EINVAL;
1079
1080	if (!param->primary_path)
1081		return -EINVAL;
1082
1083	if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
1084		return -EINVAL;
1085
1086	if (param->private_data &&
1087	    param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1088		return -EINVAL;
1089
1090	if (param->alternate_path &&
1091	    (param->alternate_path->pkey != param->primary_path->pkey ||
1092	     param->alternate_path->mtu != param->primary_path->mtu))
1093		return -EINVAL;
1094
1095	return 0;
1096}
1097
1098int ib_send_cm_req(struct ib_cm_id *cm_id,
1099		   struct ib_cm_req_param *param)
1100{
1101	struct cm_id_private *cm_id_priv;
1102	struct cm_req_msg *req_msg;
1103	unsigned long flags;
1104	int ret;
1105
1106	ret = cm_validate_req_param(param);
1107	if (ret)
1108		return ret;
1109
1110	/* Verify that we're not in timewait. */
1111	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1112	spin_lock_irqsave(&cm_id_priv->lock, flags);
1113	if (cm_id->state != IB_CM_IDLE) {
1114		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1115		ret = -EINVAL;
1116		goto out;
1117	}
1118	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1119
1120	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1121							    id.local_id);
1122	if (IS_ERR(cm_id_priv->timewait_info)) {
1123		ret = PTR_ERR(cm_id_priv->timewait_info);
1124		goto out;
1125	}
1126
1127	ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1128	if (ret)
1129		goto error1;
1130	if (param->alternate_path) {
1131		ret = cm_init_av_by_path(param->alternate_path,
1132					 &cm_id_priv->alt_av);
1133		if (ret)
1134			goto error1;
1135	}
1136	cm_id->service_id = param->service_id;
1137	cm_id->service_mask = ~cpu_to_be64(0);
1138	cm_id_priv->timeout_ms = cm_convert_to_ms(
1139				    param->primary_path->packet_life_time) * 2 +
1140				 cm_convert_to_ms(
1141				    param->remote_cm_response_timeout);
1142	cm_id_priv->max_cm_retries = param->max_cm_retries;
1143	cm_id_priv->initiator_depth = param->initiator_depth;
1144	cm_id_priv->responder_resources = param->responder_resources;
1145	cm_id_priv->retry_count = param->retry_count;
1146	cm_id_priv->path_mtu = param->primary_path->mtu;
1147	cm_id_priv->pkey = param->primary_path->pkey;
1148	cm_id_priv->qp_type = param->qp_type;
1149
1150	ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1151	if (ret)
1152		goto error1;
1153
1154	req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1155	cm_format_req(req_msg, cm_id_priv, param);
1156	cm_id_priv->tid = req_msg->hdr.tid;
1157	cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1158	cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1159
1160	cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1161	cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1162
1163	spin_lock_irqsave(&cm_id_priv->lock, flags);
1164	ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1165	if (ret) {
1166		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1167		goto error2;
1168	}
1169	BUG_ON(cm_id->state != IB_CM_IDLE);
1170	cm_id->state = IB_CM_REQ_SENT;
1171	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1172	return 0;
1173
1174error2:	cm_free_msg(cm_id_priv->msg);
1175error1:	kfree(cm_id_priv->timewait_info);
1176out:	return ret;
1177}
1178EXPORT_SYMBOL(ib_send_cm_req);
1179
1180static int cm_issue_rej(struct cm_port *port,
1181			struct ib_mad_recv_wc *mad_recv_wc,
1182			enum ib_cm_rej_reason reason,
1183			enum cm_msg_response msg_rejected,
1184			void *ari, u8 ari_length)
1185{
1186	struct ib_mad_send_buf *msg = NULL;
1187	struct cm_rej_msg *rej_msg, *rcv_msg;
1188	int ret;
1189
1190	ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1191	if (ret)
1192		return ret;
1193
1194	/* We just need common CM header information.  Cast to any message. */
1195	rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1196	rej_msg = (struct cm_rej_msg *) msg->mad;
1197
1198	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1199	rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1200	rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1201	cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1202	rej_msg->reason = cpu_to_be16(reason);
1203
1204	if (ari && ari_length) {
1205		cm_rej_set_reject_info_len(rej_msg, ari_length);
1206		memcpy(rej_msg->ari, ari, ari_length);
1207	}
1208
1209	ret = ib_post_send_mad(msg, NULL);
1210	if (ret)
1211		cm_free_msg(msg);
1212
1213	return ret;
1214}
1215
1216static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1217				    __be32 local_qpn, __be32 remote_qpn)
1218{
1219	return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1220		((local_ca_guid == remote_ca_guid) &&
1221		 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1222}
1223
1224static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1225					    struct ib_sa_path_rec *primary_path,
1226					    struct ib_sa_path_rec *alt_path)
1227{
1228	memset(primary_path, 0, sizeof *primary_path);
1229	primary_path->dgid = req_msg->primary_local_gid;
1230	primary_path->sgid = req_msg->primary_remote_gid;
1231	primary_path->dlid = req_msg->primary_local_lid;
1232	primary_path->slid = req_msg->primary_remote_lid;
1233	primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1234	primary_path->hop_limit = req_msg->primary_hop_limit;
1235	primary_path->traffic_class = req_msg->primary_traffic_class;
1236	primary_path->reversible = 1;
1237	primary_path->pkey = req_msg->pkey;
1238	primary_path->sl = cm_req_get_primary_sl(req_msg);
1239	primary_path->mtu_selector = IB_SA_EQ;
1240	primary_path->mtu = cm_req_get_path_mtu(req_msg);
1241	primary_path->rate_selector = IB_SA_EQ;
1242	primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1243	primary_path->packet_life_time_selector = IB_SA_EQ;
1244	primary_path->packet_life_time =
1245		cm_req_get_primary_local_ack_timeout(req_msg);
1246	primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1247
1248	if (req_msg->alt_local_lid) {
1249		memset(alt_path, 0, sizeof *alt_path);
1250		alt_path->dgid = req_msg->alt_local_gid;
1251		alt_path->sgid = req_msg->alt_remote_gid;
1252		alt_path->dlid = req_msg->alt_local_lid;
1253		alt_path->slid = req_msg->alt_remote_lid;
1254		alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1255		alt_path->hop_limit = req_msg->alt_hop_limit;
1256		alt_path->traffic_class = req_msg->alt_traffic_class;
1257		alt_path->reversible = 1;
1258		alt_path->pkey = req_msg->pkey;
1259		alt_path->sl = cm_req_get_alt_sl(req_msg);
1260		alt_path->mtu_selector = IB_SA_EQ;
1261		alt_path->mtu = cm_req_get_path_mtu(req_msg);
1262		alt_path->rate_selector = IB_SA_EQ;
1263		alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1264		alt_path->packet_life_time_selector = IB_SA_EQ;
1265		alt_path->packet_life_time =
1266			cm_req_get_alt_local_ack_timeout(req_msg);
1267		alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1268	}
1269}
1270
1271static void cm_format_req_event(struct cm_work *work,
1272				struct cm_id_private *cm_id_priv,
1273				struct ib_cm_id *listen_id)
1274{
1275	struct cm_req_msg *req_msg;
1276	struct ib_cm_req_event_param *param;
1277
1278	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1279	param = &work->cm_event.param.req_rcvd;
1280	param->listen_id = listen_id;
1281	param->port = cm_id_priv->av.port->port_num;
1282	param->primary_path = &work->path[0];
1283	if (req_msg->alt_local_lid)
1284		param->alternate_path = &work->path[1];
1285	else
1286		param->alternate_path = NULL;
1287	param->remote_ca_guid = req_msg->local_ca_guid;
1288	param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1289	param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1290	param->qp_type = cm_req_get_qp_type(req_msg);
1291	param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1292	param->responder_resources = cm_req_get_init_depth(req_msg);
1293	param->initiator_depth = cm_req_get_resp_res(req_msg);
1294	param->local_cm_response_timeout =
1295					cm_req_get_remote_resp_timeout(req_msg);
1296	param->flow_control = cm_req_get_flow_ctrl(req_msg);
1297	param->remote_cm_response_timeout =
1298					cm_req_get_local_resp_timeout(req_msg);
1299	param->retry_count = cm_req_get_retry_count(req_msg);
1300	param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1301	param->srq = cm_req_get_srq(req_msg);
1302	work->cm_event.private_data = &req_msg->private_data;
1303}
1304
1305static void cm_process_work(struct cm_id_private *cm_id_priv,
1306			    struct cm_work *work)
1307{
1308	int ret;
1309
1310	/* We will typically only have the current event to report. */
1311	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1312	cm_free_work(work);
1313
1314	while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1315		spin_lock_irq(&cm_id_priv->lock);
1316		work = cm_dequeue_work(cm_id_priv);
1317		spin_unlock_irq(&cm_id_priv->lock);
1318		BUG_ON(!work);
1319		ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1320						&work->cm_event);
1321		cm_free_work(work);
1322	}
1323	cm_deref_id(cm_id_priv);
1324	if (ret)
1325		cm_destroy_id(&cm_id_priv->id, ret);
1326}
1327
1328static void cm_format_mra(struct cm_mra_msg *mra_msg,
1329			  struct cm_id_private *cm_id_priv,
1330			  enum cm_msg_response msg_mraed, u8 service_timeout,
1331			  const void *private_data, u8 private_data_len)
1332{
1333	cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1334	cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1335	mra_msg->local_comm_id = cm_id_priv->id.local_id;
1336	mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1337	cm_mra_set_service_timeout(mra_msg, service_timeout);
1338
1339	if (private_data && private_data_len)
1340		memcpy(mra_msg->private_data, private_data, private_data_len);
1341}
1342
1343static void cm_format_rej(struct cm_rej_msg *rej_msg,
1344			  struct cm_id_private *cm_id_priv,
1345			  enum ib_cm_rej_reason reason,
1346			  void *ari,
1347			  u8 ari_length,
1348			  const void *private_data,
1349			  u8 private_data_len)
1350{
1351	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1352	rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1353
1354	switch(cm_id_priv->id.state) {
1355	case IB_CM_REQ_RCVD:
1356		rej_msg->local_comm_id = 0;
1357		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1358		break;
1359	case IB_CM_MRA_REQ_SENT:
1360		rej_msg->local_comm_id = cm_id_priv->id.local_id;
1361		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1362		break;
1363	case IB_CM_REP_RCVD:
1364	case IB_CM_MRA_REP_SENT:
1365		rej_msg->local_comm_id = cm_id_priv->id.local_id;
1366		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1367		break;
1368	default:
1369		rej_msg->local_comm_id = cm_id_priv->id.local_id;
1370		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1371		break;
1372	}
1373
1374	rej_msg->reason = cpu_to_be16(reason);
1375	if (ari && ari_length) {
1376		cm_rej_set_reject_info_len(rej_msg, ari_length);
1377		memcpy(rej_msg->ari, ari, ari_length);
1378	}
1379
1380	if (private_data && private_data_len)
1381		memcpy(rej_msg->private_data, private_data, private_data_len);
1382}
1383
1384static void cm_dup_req_handler(struct cm_work *work,
1385			       struct cm_id_private *cm_id_priv)
1386{
1387	struct ib_mad_send_buf *msg = NULL;
1388	int ret;
1389
1390	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1391			counter[CM_REQ_COUNTER]);
1392
1393	/* Quick state check to discard duplicate REQs. */
1394	if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1395		return;
1396
1397	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1398	if (ret)
1399		return;
1400
1401	spin_lock_irq(&cm_id_priv->lock);
1402	switch (cm_id_priv->id.state) {
1403	case IB_CM_MRA_REQ_SENT:
1404		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1405			      CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1406			      cm_id_priv->private_data,
1407			      cm_id_priv->private_data_len);
1408		break;
1409	case IB_CM_TIMEWAIT:
1410		cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1411			      IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1412		break;
1413	default:
1414		goto unlock;
1415	}
1416	spin_unlock_irq(&cm_id_priv->lock);
1417
1418	ret = ib_post_send_mad(msg, NULL);
1419	if (ret)
1420		goto free;
1421	return;
1422
1423unlock:	spin_unlock_irq(&cm_id_priv->lock);
1424free:	cm_free_msg(msg);
1425}
1426
1427static struct cm_id_private * cm_match_req(struct cm_work *work,
1428					   struct cm_id_private *cm_id_priv)
1429{
1430	struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1431	struct cm_timewait_info *timewait_info;
1432	struct cm_req_msg *req_msg;
1433
1434	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1435
1436	/* Check for possible duplicate REQ. */
1437	spin_lock_irq(&cm.lock);
1438	timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1439	if (timewait_info) {
1440		cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1441					   timewait_info->work.remote_id);
1442		spin_unlock_irq(&cm.lock);
1443		if (cur_cm_id_priv) {
1444			cm_dup_req_handler(work, cur_cm_id_priv);
1445			cm_deref_id(cur_cm_id_priv);
1446		}
1447		return NULL;
1448	}
1449
1450	/* Check for stale connections. */
1451	timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1452	if (timewait_info) {
1453		cm_cleanup_timewait(cm_id_priv->timewait_info);
1454		spin_unlock_irq(&cm.lock);
1455		cm_issue_rej(work->port, work->mad_recv_wc,
1456			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1457			     NULL, 0);
1458		return NULL;
1459	}
1460
1461	/* Find matching listen request. */
1462	listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1463					   req_msg->service_id,
1464					   req_msg->private_data);
1465	if (!listen_cm_id_priv) {
1466		cm_cleanup_timewait(cm_id_priv->timewait_info);
1467		spin_unlock_irq(&cm.lock);
1468		cm_issue_rej(work->port, work->mad_recv_wc,
1469			     IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1470			     NULL, 0);
1471		goto out;
1472	}
1473	atomic_inc(&listen_cm_id_priv->refcount);
1474	atomic_inc(&cm_id_priv->refcount);
1475	cm_id_priv->id.state = IB_CM_REQ_RCVD;
1476	atomic_inc(&cm_id_priv->work_count);
1477	spin_unlock_irq(&cm.lock);
1478out:
1479	return listen_cm_id_priv;
1480}
1481
1482/*
1483 * Work-around for inter-subnet connections.  If the LIDs are permissive,
1484 * we need to override the LID/SL data in the REQ with the LID information
1485 * in the work completion.
1486 */
1487static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1488{
1489	if (!cm_req_get_primary_subnet_local(req_msg)) {
1490		if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1491			req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1492			cm_req_set_primary_sl(req_msg, wc->sl);
1493		}
1494
1495		if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1496			req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1497	}
1498
1499	if (!cm_req_get_alt_subnet_local(req_msg)) {
1500		if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1501			req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1502			cm_req_set_alt_sl(req_msg, wc->sl);
1503		}
1504
1505		if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1506			req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1507	}
1508}
1509
1510static int cm_req_handler(struct cm_work *work)
1511{
1512	struct ib_cm_id *cm_id;
1513	struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1514	struct cm_req_msg *req_msg;
1515	int ret;
1516
1517	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1518
1519	cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
1520	if (IS_ERR(cm_id))
1521		return PTR_ERR(cm_id);
1522
1523	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1524	cm_id_priv->id.remote_id = req_msg->local_comm_id;
1525	cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1526				work->mad_recv_wc->recv_buf.grh,
1527				&cm_id_priv->av);
1528	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1529							    id.local_id);
1530	if (IS_ERR(cm_id_priv->timewait_info)) {
1531		ret = PTR_ERR(cm_id_priv->timewait_info);
1532		goto destroy;
1533	}
1534	cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1535	cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1536	cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1537
1538	listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1539	if (!listen_cm_id_priv) {
1540		ret = -EINVAL;
1541		kfree(cm_id_priv->timewait_info);
1542		goto destroy;
1543	}
1544
1545	cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1546	cm_id_priv->id.context = listen_cm_id_priv->id.context;
1547	cm_id_priv->id.service_id = req_msg->service_id;
1548	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
1549
1550	cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
1551	cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1552	ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1553	if (ret) {
1554		ib_get_cached_gid(work->port->cm_dev->ib_device,
1555				  work->port->port_num, 0, &work->path[0].sgid);
1556		ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1557			       &work->path[0].sgid, sizeof work->path[0].sgid,
1558			       NULL, 0);
1559		goto rejected;
1560	}
1561	if (req_msg->alt_local_lid) {
1562		ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1563		if (ret) {
1564			ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1565				       &work->path[0].sgid,
1566				       sizeof work->path[0].sgid, NULL, 0);
1567			goto rejected;
1568		}
1569	}
1570	cm_id_priv->tid = req_msg->hdr.tid;
1571	cm_id_priv->timeout_ms = cm_convert_to_ms(
1572					cm_req_get_local_resp_timeout(req_msg));
1573	cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1574	cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1575	cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1576	cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1577	cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1578	cm_id_priv->pkey = req_msg->pkey;
1579	cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1580	cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1581	cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1582	cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1583
1584	cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1585	cm_process_work(cm_id_priv, work);
1586	cm_deref_id(listen_cm_id_priv);
1587	return 0;
1588
1589rejected:
1590	atomic_dec(&cm_id_priv->refcount);
1591	cm_deref_id(listen_cm_id_priv);
1592destroy:
1593	ib_destroy_cm_id(cm_id);
1594	return ret;
1595}
1596
1597static void cm_format_rep(struct cm_rep_msg *rep_msg,
1598			  struct cm_id_private *cm_id_priv,
1599			  struct ib_cm_rep_param *param)
1600{
1601	cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1602	rep_msg->local_comm_id = cm_id_priv->id.local_id;
1603	rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1604	cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1605	cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1606	rep_msg->resp_resources = param->responder_resources;
1607	rep_msg->initiator_depth = param->initiator_depth;
1608	cm_rep_set_target_ack_delay(rep_msg,
1609				    cm_id_priv->av.port->cm_dev->ack_delay);
1610	cm_rep_set_failover(rep_msg, param->failover_accepted);
1611	cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1612	cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1613	cm_rep_set_srq(rep_msg, param->srq);
1614	rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1615
1616	if (param->private_data && param->private_data_len)
1617		memcpy(rep_msg->private_data, param->private_data,
1618		       param->private_data_len);
1619}
1620
1621int ib_send_cm_rep(struct ib_cm_id *cm_id,
1622		   struct ib_cm_rep_param *param)
1623{
1624	struct cm_id_private *cm_id_priv;
1625	struct ib_mad_send_buf *msg;
1626	struct cm_rep_msg *rep_msg;
1627	unsigned long flags;
1628	int ret;
1629
1630	if (param->private_data &&
1631	    param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1632		return -EINVAL;
1633
1634	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1635	spin_lock_irqsave(&cm_id_priv->lock, flags);
1636	if (cm_id->state != IB_CM_REQ_RCVD &&
1637	    cm_id->state != IB_CM_MRA_REQ_SENT) {
1638		ret = -EINVAL;
1639		goto out;
1640	}
1641
1642	ret = cm_alloc_msg(cm_id_priv, &msg);
1643	if (ret)
1644		goto out;
1645
1646	rep_msg = (struct cm_rep_msg *) msg->mad;
1647	cm_format_rep(rep_msg, cm_id_priv, param);
1648	msg->timeout_ms = cm_id_priv->timeout_ms;
1649	msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1650
1651	ret = ib_post_send_mad(msg, NULL);
1652	if (ret) {
1653		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1654		cm_free_msg(msg);
1655		return ret;
1656	}
1657
1658	cm_id->state = IB_CM_REP_SENT;
1659	cm_id_priv->msg = msg;
1660	cm_id_priv->initiator_depth = param->initiator_depth;
1661	cm_id_priv->responder_resources = param->responder_resources;
1662	cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1663	cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1664
1665out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1666	return ret;
1667}
1668EXPORT_SYMBOL(ib_send_cm_rep);
1669
1670static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1671			  struct cm_id_private *cm_id_priv,
1672			  const void *private_data,
1673			  u8 private_data_len)
1674{
1675	cm_format_m

Large files files are truncated, but you can click here to view the full file