nes_cm.c 101.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2006 - 2009 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 35 36
 *
 * 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.
 *
 */


#define TCPOPT_TIMESTAMP 8

A
Arun Sharma 已提交
37
#include <linux/atomic.h>
38 39 40 41 42
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/init.h>
#include <linux/if_arp.h>
43
#include <linux/if_vlan.h>
44 45 46 47 48 49 50 51 52 53 54
#include <linux/notifier.h>
#include <linux/net.h>
#include <linux/types.h>
#include <linux/timer.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/etherdevice.h>
#include <linux/netdevice.h>
#include <linux/random.h>
#include <linux/list.h>
#include <linux/threads.h>
F
Faisal Latif 已提交
55
#include <linux/highmem.h>
56
#include <linux/slab.h>
B
Bob Sharp 已提交
57
#include <net/arp.h>
58 59 60
#include <net/neighbour.h>
#include <net/route.h>
#include <net/ip_fib.h>
61
#include <net/tcp.h>
62 63 64 65 66 67 68 69 70

#include "nes.h"

u32 cm_packets_sent;
u32 cm_packets_bounced;
u32 cm_packets_dropped;
u32 cm_packets_retrans;
u32 cm_packets_created;
u32 cm_packets_received;
71 72
atomic_t cm_listens_created;
atomic_t cm_listens_destroyed;
73 74 75 76 77 78 79
u32 cm_backlog_drops;
atomic_t cm_loopbacks;
atomic_t cm_nodes_created;
atomic_t cm_nodes_destroyed;
atomic_t cm_accel_dropped_pkts;
atomic_t cm_resets_recvd;

80 81
static inline int mini_cm_accelerated(struct nes_cm_core *,
	struct nes_cm_node *);
82
static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
83
	struct nes_vnic *, struct nes_cm_info *);
84
static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
85
static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
86 87
	struct nes_vnic *, u16, void *, struct nes_cm_info *);
static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
88
static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
89
	struct nes_cm_node *);
90
static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
91
	struct nes_cm_node *);
92
static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
93
	struct sk_buff *);
94 95 96
static int mini_cm_dealloc_core(struct nes_cm_core *);
static int mini_cm_get(struct nes_cm_core *);
static int mini_cm_set(struct nes_cm_core *, u32, u32);
97

C
Chien Tung 已提交
98
static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
99 100 101 102
	void *, u32, void *, u32, u8);
static int add_ref_cm_node(struct nes_cm_node *);
static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);

103 104 105 106
static int nes_cm_disconn_true(struct nes_qp *);
static int nes_cm_post_event(struct nes_cm_event *event);
static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
static void nes_disconnect_worker(struct work_struct *work);
107 108

static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
109
static int send_mpa_reject(struct nes_cm_node *);
110 111 112
static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
static int send_reset(struct nes_cm_node *, struct sk_buff *);
static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
113
static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
114 115 116 117 118 119
static void process_packet(struct nes_cm_node *, struct sk_buff *,
	struct nes_cm_core *);

static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
static void cleanup_retrans_entry(struct nes_cm_node *);
120
static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
121 122 123 124 125 126 127 128 129
static void free_retrans_entry(struct nes_cm_node *cm_node);
static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
	struct sk_buff *skb, int optionsize, int passive);

/* CM event handler functions */
static void cm_event_connected(struct nes_cm_event *);
static void cm_event_connect_error(struct nes_cm_event *);
static void cm_event_reset(struct nes_cm_event *);
static void cm_event_mpa_req(struct nes_cm_event *);
130 131
static void cm_event_mpa_reject(struct nes_cm_event *);
static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
132 133

static void print_core(struct nes_cm_core *core);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

/* External CM API Interface */
/* instance of function pointers for client API */
/* set address of this instance to cm_core->cm_ops at cm_core alloc */
static struct nes_cm_ops nes_cm_api = {
	mini_cm_accelerated,
	mini_cm_listen,
	mini_cm_del_listen,
	mini_cm_connect,
	mini_cm_close,
	mini_cm_accept,
	mini_cm_reject,
	mini_cm_recv_pkt,
	mini_cm_dealloc_core,
	mini_cm_get,
	mini_cm_set
};

152
static struct nes_cm_core *g_cm_core;
153 154 155 156 157 158 159 160 161

atomic_t cm_connects;
atomic_t cm_accepts;
atomic_t cm_disconnects;
atomic_t cm_closes;
atomic_t cm_connecteds;
atomic_t cm_connect_reqs;
atomic_t cm_rejects;

162 163 164 165 166 167 168 169 170
int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
{
	return add_ref_cm_node(cm_node);
}

int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
{
	return rem_ref_cm_node(cm_node->cm_core, cm_node);
}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

/**
 * create_event
 */
static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
		enum nes_cm_event_type type)
{
	struct nes_cm_event *event;

	if (!cm_node->cm_id)
		return NULL;

	/* allocate an empty event */
	event = kzalloc(sizeof(*event), GFP_ATOMIC);

	if (!event)
		return NULL;

	event->type = type;
	event->cm_node = cm_node;
	event->cm_info.rem_addr = cm_node->rem_addr;
	event->cm_info.loc_addr = cm_node->loc_addr;
	event->cm_info.rem_port = cm_node->rem_port;
	event->cm_info.loc_port = cm_node->loc_port;
	event->cm_info.cm_id = cm_node->cm_id;

197 198 199 200 201
	nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
		"dst_addr=%08x[%x], src_addr=%08x[%x]\n",
		cm_node, event, type, event->cm_info.loc_addr,
		event->cm_info.loc_port, event->cm_info.rem_addr,
		event->cm_info.rem_port);
202 203 204 205 206 207 208 209 210

	nes_cm_post_event(event);
	return event;
}


/**
 * send_mpa_request
 */
211
static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
212 213
{
	if (!skb) {
214
		nes_debug(NES_DBG_CM, "skb set to NULL\n");
215 216 217 218 219 220 221
		return -1;
	}

	/* send an MPA Request frame */
	form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
			cm_node->mpa_frame_size, SET_ACK);

222 223
	return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
}
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242


static int send_mpa_reject(struct nes_cm_node *cm_node)
{
	struct sk_buff  *skb = NULL;

	skb = dev_alloc_skb(MAX_CM_BUFFER);
	if (!skb) {
		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
		return -ENOMEM;
	}

	/* send an MPA reject frame */
	form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
			cm_node->mpa_frame_size, SET_ACK | SET_FIN);

	cm_node->state = NES_CM_STATE_FIN_WAIT1;
	return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
243 244 245 246 247 248 249
}


/**
 * recv_mpa - process a received TCP pkt, we are expecting an
 * IETF MPA frame
 */
250 251
static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
		u32 len)
252 253 254
{
	struct ietf_mpa_frame *mpa_frame;

255 256
	*type = NES_MPA_REQUEST_ACCEPT;

257 258 259
	/* assume req frame is in tcp data payload */
	if (len < sizeof(struct ietf_mpa_frame)) {
		nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
260
		return -EINVAL;
261 262 263 264
	}

	mpa_frame = (struct ietf_mpa_frame *)buffer;
	cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	/* make sure mpa private data len is less than 512 bytes */
	if (cm_node->mpa_frame_size > IETF_MAX_PRIV_DATA_LEN) {
		nes_debug(NES_DBG_CM, "The received Length of Private"
			" Data field exceeds 512 octets\n");
		return -EINVAL;
	}
	/*
	 * make sure MPA receiver interoperate with the
	 * received MPA version and MPA key information
	 *
	 */
	if (mpa_frame->rev != mpa_version) {
		nes_debug(NES_DBG_CM, "The received mpa version"
				" can not be interoperated\n");
		return -EINVAL;
	}
	if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
		if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
			nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
			return -EINVAL;
		}
	} else {
		if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
			nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
			return -EINVAL;
		}
	}
292 293 294 295

	if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
		nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
				" complete (%x + %x != %x)\n",
296 297 298 299 300 301 302 303 304 305 306
				cm_node->mpa_frame_size,
				(u32)sizeof(struct ietf_mpa_frame), len);
		return -EINVAL;
	}
	/* make sure it does not exceed the max size */
	if (len > MAX_CM_BUFFER) {
		nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
				" (%x + %x != %x)\n",
				cm_node->mpa_frame_size,
				(u32)sizeof(struct ietf_mpa_frame), len);
		return -EINVAL;
307 308 309 310 311 312
	}

	/* copy entire MPA frame to our cm_node's frame */
	memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
			cm_node->mpa_frame_size);

313 314
	if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
		*type = NES_MPA_REQUEST_REJECT;
315 316 317 318 319 320 321 322
	return 0;
}


/**
 * form_cm_frame - get a free packet and build empty frame Use
 * node info to build.
 */
C
Chien Tung 已提交
323
static void form_cm_frame(struct sk_buff *skb,
324 325
	struct nes_cm_node *cm_node, void *options, u32 optionsize,
	void *data, u32 datasize, u8 flags)
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
{
	struct tcphdr *tcph;
	struct iphdr *iph;
	struct ethhdr *ethh;
	u8 *buf;
	u16 packetsize = sizeof(*iph);

	packetsize += sizeof(*tcph);
	packetsize +=  optionsize + datasize;

	memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));

	skb->len = 0;
	buf = skb_put(skb, packetsize + ETH_HLEN);

	ethh = (struct ethhdr *) buf;
	buf += ETH_HLEN;

	iph = (struct iphdr *)buf;
	buf += sizeof(*iph);
	tcph = (struct tcphdr *)buf;
	skb_reset_mac_header(skb);
	skb_set_network_header(skb, ETH_HLEN);
	skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
	buf += sizeof(*tcph);

	skb->ip_summed = CHECKSUM_PARTIAL;
	skb->protocol = htons(0x800);
	skb->data_len = 0;
	skb->mac_len = ETH_HLEN;

	memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
	memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
	ethh->h_proto = htons(0x0800);

	iph->version = IPVERSION;
	iph->ihl = 5;		/* 5 * 4Byte words, IP headr len */
	iph->tos = 0;
	iph->tot_len = htons(packetsize);
	iph->id = htons(++cm_node->tcp_cntxt.loc_id);

	iph->frag_off = htons(0x4000);
	iph->ttl = 0x40;
	iph->protocol = 0x06;	/* IPPROTO_TCP */

	iph->saddr = htonl(cm_node->loc_addr);
	iph->daddr = htonl(cm_node->rem_addr);

	tcph->source = htons(cm_node->loc_port);
	tcph->dest = htons(cm_node->rem_port);
	tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);

	if (flags & SET_ACK) {
		cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
		tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
		tcph->ack = 1;
	} else
		tcph->ack_seq = 0;

	if (flags & SET_SYN) {
		cm_node->tcp_cntxt.loc_seq_num++;
		tcph->syn = 1;
	} else
389
		cm_node->tcp_cntxt.loc_seq_num += datasize;
390

391 392
	if (flags & SET_FIN) {
		cm_node->tcp_cntxt.loc_seq_num++;
393
		tcph->fin = 1;
394
	}
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

	if (flags & SET_RST)
		tcph->rst = 1;

	tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
	tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
	tcph->urg_ptr = 0;
	if (optionsize)
		memcpy(buf, options, optionsize);
	buf += optionsize;
	if (datasize)
		memcpy(buf, data, datasize);

	skb_shinfo(skb)->nr_frags = 0;
	cm_packets_created++;

}


/**
 * print_core - dump a cm core
 */
static void print_core(struct nes_cm_core *core)
{
	nes_debug(NES_DBG_CM, "---------------------------------------------\n");
	nes_debug(NES_DBG_CM, "CM Core  -- (core = %p )\n", core);
	if (!core)
		return;
	nes_debug(NES_DBG_CM, "---------------------------------------------\n");

	nes_debug(NES_DBG_CM, "State         : %u \n",  core->state);

	nes_debug(NES_DBG_CM, "Listen Nodes  : %u \n", atomic_read(&core->listen_node_cnt));
	nes_debug(NES_DBG_CM, "Active Nodes  : %u \n", atomic_read(&core->node_cnt));

	nes_debug(NES_DBG_CM, "core          : %p \n", core);

	nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
}


/**
 * schedule_nes_timer
 * note - cm_node needs to be protected before calling this. Encase in:
 *			rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
 */
int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
		enum nes_timer_type type, int send_retrans,
		int close_when_complete)
{
	unsigned long  flags;
446
	struct nes_cm_core *cm_core = cm_node->cm_core;
447 448 449 450 451 452
	struct nes_timer_entry *new_send;
	int ret = 0;
	u32 was_timer_set;

	new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
	if (!new_send)
453
		return -ENOMEM;
454 455 456 457 458 459 460 461 462 463 464 465

	/* new_send->timetosend = currenttime */
	new_send->retrycount = NES_DEFAULT_RETRYS;
	new_send->retranscount = NES_DEFAULT_RETRANS;
	new_send->skb = skb;
	new_send->timetosend = jiffies;
	new_send->type = type;
	new_send->netdev = cm_node->netdev;
	new_send->send_retrans = send_retrans;
	new_send->close_when_complete = close_when_complete;

	if (type == NES_TIMER_TYPE_CLOSE) {
466
		new_send->timetosend += (HZ/10);
467
		if (cm_node->recv_entry) {
468
			kfree(new_send);
469 470 471 472
			WARN_ON(1);
			return -EINVAL;
		}
		cm_node->recv_entry = new_send;
473 474 475
	}

	if (type == NES_TIMER_TYPE_SEND) {
476
		new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
477
		atomic_inc(&new_send->skb->users);
478 479 480 481 482
		spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
		cm_node->send_entry = new_send;
		add_ref_cm_node(cm_node);
		spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
		new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
483 484 485

		ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
		if (ret != NETDEV_TX_OK) {
486 487
			nes_debug(NES_DBG_CM, "Error sending packet %p "
				"(jiffies = %lu)\n", new_send, jiffies);
488
			new_send->timetosend = jiffies;
489
			ret = NETDEV_TX_OK;
490 491 492
		} else {
			cm_packets_sent++;
			if (!send_retrans) {
493
				cleanup_retrans_entry(cm_node);
494
				if (close_when_complete)
495
					rem_ref_cm_node(cm_core, cm_node);
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
				return ret;
			}
		}
	}

	was_timer_set = timer_pending(&cm_core->tcp_timer);

	if (!was_timer_set) {
		cm_core->tcp_timer.expires = new_send->timetosend;
		add_timer(&cm_core->tcp_timer);
	}

	return ret;
}

511 512
static void nes_retrans_expired(struct nes_cm_node *cm_node)
{
513
	struct iw_cm_id *cm_id = cm_node->cm_id;
514 515 516
	enum nes_cm_node_state state = cm_node->state;
	cm_node->state = NES_CM_STATE_CLOSED;
	switch (state) {
517 518 519 520 521 522
	case NES_CM_STATE_SYN_RCVD:
	case NES_CM_STATE_CLOSING:
		rem_ref_cm_node(cm_node->cm_core, cm_node);
		break;
	case NES_CM_STATE_LAST_ACK:
	case NES_CM_STATE_FIN_WAIT1:
523 524
		if (cm_node->cm_id)
			cm_id->rem_ref(cm_id);
525 526 527
		send_reset(cm_node, NULL);
		break;
	default:
528 529
		add_ref_cm_node(cm_node);
		send_reset(cm_node, NULL);
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
		create_event(cm_node, NES_CM_EVENT_ABORTED);
	}
}

static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
{
	struct nes_timer_entry *recv_entry = cm_node->recv_entry;
	struct iw_cm_id *cm_id = cm_node->cm_id;
	struct nes_qp *nesqp;
	unsigned long qplockflags;

	if (!recv_entry)
		return;
	nesqp = (struct nes_qp *)recv_entry->skb;
	if (nesqp) {
		spin_lock_irqsave(&nesqp->lock, qplockflags);
		if (nesqp->cm_id) {
			nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
				"refcount = %d: HIT A "
				"NES_TIMER_TYPE_CLOSE with something "
				"to do!!!\n", nesqp->hwqp.qp_id, cm_id,
				atomic_read(&nesqp->refcount));
			nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
			nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
			nesqp->ibqp_state = IB_QPS_ERR;
			spin_unlock_irqrestore(&nesqp->lock, qplockflags);
			nes_cm_disconn(nesqp);
		} else {
			spin_unlock_irqrestore(&nesqp->lock, qplockflags);
			nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
				"refcount = %d: HIT A "
				"NES_TIMER_TYPE_CLOSE with nothing "
				"to do!!!\n", nesqp->hwqp.qp_id, cm_id,
				atomic_read(&nesqp->refcount));
		}
	} else if (rem_node) {
		/* TIME_WAIT state */
		rem_ref_cm_node(cm_node->cm_core, cm_node);
	}
	if (cm_node->cm_id)
		cm_id->rem_ref(cm_id);
	kfree(recv_entry);
	cm_node->recv_entry = NULL;
}
574 575 576 577

/**
 * nes_cm_timer_tick
 */
578
static void nes_cm_timer_tick(unsigned long pass)
579
{
580
	unsigned long flags;
581 582 583
	unsigned long nexttimeout = jiffies + NES_LONG_TIME;
	struct nes_cm_node *cm_node;
	struct nes_timer_entry *send_entry, *recv_entry;
584 585
	struct list_head *list_core_temp;
	struct list_head *list_node;
586 587
	struct nes_cm_core *cm_core = g_cm_core;
	u32 settimer = 0;
588
	unsigned long timetosend;
589 590
	int ret = NETDEV_TX_OK;

591 592
	struct list_head timer_list;
	INIT_LIST_HEAD(&timer_list);
593 594
	spin_lock_irqsave(&cm_core->ht_lock, flags);

595
	list_for_each_safe(list_node, list_core_temp,
596
				&cm_core->connected_nodes) {
597
		cm_node = container_of(list_node, struct nes_cm_node, list);
598
		if ((cm_node->recv_entry) || (cm_node->send_entry)) {
599 600 601 602 603 604 605 606 607
			add_ref_cm_node(cm_node);
			list_add(&cm_node->timer_entry, &timer_list);
		}
	}
	spin_unlock_irqrestore(&cm_core->ht_lock, flags);

	list_for_each_safe(list_node, list_core_temp, &timer_list) {
		cm_node = container_of(list_node, struct nes_cm_node,
					timer_entry);
608 609 610
		recv_entry = cm_node->recv_entry;

		if (recv_entry) {
611 612
			if (time_after(recv_entry->timetosend, jiffies)) {
				if (nexttimeout > recv_entry->timetosend ||
613
						!settimer) {
614 615 616
					nexttimeout = recv_entry->timetosend;
					settimer = 1;
				}
617 618
			} else
				handle_recv_entry(cm_node, 1);
619 620 621
		}

		spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
622 623 624
		do {
			send_entry = cm_node->send_entry;
			if (!send_entry)
625
				break;
626 627
			if (time_after(send_entry->timetosend, jiffies)) {
				if (cm_node->state != NES_CM_STATE_TSA) {
628 629 630 631 632
					if ((nexttimeout >
						send_entry->timetosend) ||
						!settimer) {
						nexttimeout =
							send_entry->timetosend;
633 634 635
						settimer = 1;
					}
				} else {
636
					free_retrans_entry(cm_node);
637
				}
638
				break;
639
			}
640 641 642 643

			if ((cm_node->state == NES_CM_STATE_TSA) ||
				(cm_node->state == NES_CM_STATE_CLOSED)) {
				free_retrans_entry(cm_node);
644
				break;
645 646
			}

647 648
			if (!send_entry->retranscount ||
				!send_entry->retrycount) {
649
				cm_packets_dropped++;
650
				free_retrans_entry(cm_node);
651

652 653
				spin_unlock_irqrestore(
					&cm_node->retrans_list_lock, flags);
654 655
				nes_retrans_expired(cm_node);
				cm_node->state = NES_CM_STATE_CLOSED;
656 657
				spin_lock_irqsave(&cm_node->retrans_list_lock,
					flags);
658
				break;
659 660 661
			}
			atomic_inc(&send_entry->skb->users);
			cm_packets_retrans++;
662 663 664 665 666 667 668 669 670 671 672 673
			nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
				"for node %p, jiffies = %lu, time to send = "
				"%lu, retranscount = %u, send_entry->seq_num = "
				"0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
				"0x%08X\n", send_entry, cm_node, jiffies,
				send_entry->timetosend,
				send_entry->retranscount,
				send_entry->seq_num,
				cm_node->tcp_cntxt.rem_ack_num);

			spin_unlock_irqrestore(&cm_node->retrans_list_lock,
				flags);
674
			ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
675
			spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
676
			if (ret != NETDEV_TX_OK) {
677 678
				nes_debug(NES_DBG_CM, "rexmit failed for "
					"node=%p\n", cm_node);
679 680 681 682
				cm_packets_bounced++;
				send_entry->retrycount--;
				nexttimeout = jiffies + NES_SHORT_TIME;
				settimer = 1;
683
				break;
684 685 686
			} else {
				cm_packets_sent++;
			}
687 688 689 690
			nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
				"%u, retry count = %u.\n",
				send_entry->retranscount,
				send_entry->retrycount);
691 692
			if (send_entry->send_retrans) {
				send_entry->retranscount--;
693 694 695
				timetosend = (NES_RETRY_TIMEOUT <<
					(NES_DEFAULT_RETRANS - send_entry->retranscount));

696
				send_entry->timetosend = jiffies +
697
					min(timetosend, NES_MAX_TIMEOUT);
698 699
				if (nexttimeout > send_entry->timetosend ||
					!settimer) {
700 701 702 703 704
					nexttimeout = send_entry->timetosend;
					settimer = 1;
				}
			} else {
				int close_when_complete;
705 706 707 708 709 710 711 712
				close_when_complete =
					send_entry->close_when_complete;
				nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
					cm_node, cm_node->state);
				free_retrans_entry(cm_node);
				if (close_when_complete)
					rem_ref_cm_node(cm_node->cm_core,
						cm_node);
713
			}
714
		} while (0);
715

716 717
		spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
		rem_ref_cm_node(cm_node->cm_core, cm_node);
718 719 720 721 722 723 724 725 726 727 728 729 730 731
	}

	if (settimer) {
		if (!timer_pending(&cm_core->tcp_timer)) {
			cm_core->tcp_timer.expires  = nexttimeout;
			add_timer(&cm_core->tcp_timer);
		}
	}
}


/**
 * send_syn
 */
732 733
static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
	struct sk_buff *skb)
734 735 736 737
{
	int ret;
	int flags = SET_SYN;
	char optionsbuffer[sizeof(struct option_mss) +
738 739
		sizeof(struct option_windowscale) + sizeof(struct option_base) +
		TCP_OPTIONS_PADDING];
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

	int optionssize = 0;
	/* Sending MSS option */
	union all_known_options *options;

	if (!cm_node)
		return -EINVAL;

	options = (union all_known_options *)&optionsbuffer[optionssize];
	options->as_mss.optionnum = OPTION_NUMBER_MSS;
	options->as_mss.length = sizeof(struct option_mss);
	options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
	optionssize += sizeof(struct option_mss);

	options = (union all_known_options *)&optionsbuffer[optionssize];
	options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
	options->as_windowscale.length = sizeof(struct option_windowscale);
	options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
	optionssize += sizeof(struct option_windowscale);

760
	if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
		options = (union all_known_options *)&optionsbuffer[optionssize];
		options->as_base.optionnum = OPTION_NUMBER_WRITE0;
		options->as_base.length = sizeof(struct option_base);
		optionssize += sizeof(struct option_base);
		/* we need the size to be a multiple of 4 */
		options = (union all_known_options *)&optionsbuffer[optionssize];
		options->as_end = 1;
		optionssize += 1;
		options = (union all_known_options *)&optionsbuffer[optionssize];
		options->as_end = 1;
		optionssize += 1;
	}

	options = (union all_known_options *)&optionsbuffer[optionssize];
	options->as_end = OPTION_NUMBER_END;
	optionssize += 1;

778
	if (!skb)
F
Faisal Latif 已提交
779
		skb = dev_alloc_skb(MAX_CM_BUFFER);
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
	if (!skb) {
		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
		return -1;
	}

	if (sendack)
		flags |= SET_ACK;

	form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
	ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);

	return ret;
}


/**
 * send_reset
 */
798
static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
799 800 801 802
{
	int ret;
	int flags = SET_RST | SET_ACK;

803
	if (!skb)
F
Faisal Latif 已提交
804
		skb = dev_alloc_skb(MAX_CM_BUFFER);
805 806
	if (!skb) {
		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
807
		return -ENOMEM;
808 809 810 811 812 813 814 815 816 817 818 819
	}

	form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
	ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);

	return ret;
}


/**
 * send_ack
 */
820
static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
821 822
{
	int ret;
823 824

	if (!skb)
F
Faisal Latif 已提交
825
		skb = dev_alloc_skb(MAX_CM_BUFFER);
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841

	if (!skb) {
		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
		return -1;
	}

	form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
	ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);

	return ret;
}


/**
 * send_fin
 */
842
static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
843 844 845 846 847
{
	int ret;

	/* if we didn't get a frame get one */
	if (!skb)
F
Faisal Latif 已提交
848
		skb = dev_alloc_skb(MAX_CM_BUFFER);
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876

	if (!skb) {
		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
		return -1;
	}

	form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
	ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);

	return ret;
}


/**
 * find_node - find a cm node that matches the reference cm node
 */
static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
		u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
{
	unsigned long flags;
	struct list_head *hte;
	struct nes_cm_node *cm_node;

	/* get a handle on the hte */
	hte = &cm_core->connected_nodes;

	/* walk list and find cm_node associated with this session ID */
	spin_lock_irqsave(&cm_core->ht_lock, flags);
877
	list_for_each_entry(cm_node, hte, list) {
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
		/* compare quad, return node handle if a match */
		nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
				cm_node->loc_addr, cm_node->loc_port,
				loc_addr, loc_port,
				cm_node->rem_addr, cm_node->rem_port,
				rem_addr, rem_port);
		if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
				(cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
			add_ref_cm_node(cm_node);
			spin_unlock_irqrestore(&cm_core->ht_lock, flags);
			return cm_node;
		}
	}
	spin_unlock_irqrestore(&cm_core->ht_lock, flags);

	/* no owner node */
	return NULL;
}


/**
 * find_listener - find a cm node listening on this addr-port pair
 */
static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
		nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
{
	unsigned long flags;
	struct nes_cm_listener *listen_node;

	/* walk list and find cm_node associated with this session ID */
	spin_lock_irqsave(&cm_core->listen_list_lock, flags);
909
	list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
		/* compare node pair, return node handle if a match */
		if (((listen_node->loc_addr == dst_addr) ||
				listen_node->loc_addr == 0x00000000) &&
				(listen_node->loc_port == dst_port) &&
				(listener_state & listen_node->listener_state)) {
			atomic_inc(&listen_node->ref_count);
			spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
			return listen_node;
		}
	}
	spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);

	/* no listener */
	return NULL;
}


/**
 * add_hte_node - add a cm node to the hash table
 */
static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
{
	unsigned long flags;
	struct list_head *hte;

	if (!cm_node || !cm_core)
		return -EINVAL;

938 939
	nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
		cm_node);
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957

	spin_lock_irqsave(&cm_core->ht_lock, flags);

	/* get a handle on the hash table element (list head for this slot) */
	hte = &cm_core->connected_nodes;
	list_add_tail(&cm_node->list, hte);
	atomic_inc(&cm_core->ht_node_cnt);

	spin_unlock_irqrestore(&cm_core->ht_lock, flags);

	return 0;
}


/**
 * mini_cm_dec_refcnt_listen
 */
static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
958
	struct nes_cm_listener *listener, int free_hanging_nodes)
959
{
960 961
	int ret = -EINVAL;
	int err = 0;
962
	unsigned long flags;
963 964 965
	struct list_head *list_pos = NULL;
	struct list_head *list_temp = NULL;
	struct nes_cm_node *cm_node = NULL;
966
	struct list_head reset_list;
967 968 969 970 971

	nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
		"refcnt=%d\n", listener, free_hanging_nodes,
		atomic_read(&listener->ref_count));
	/* free non-accelerated child nodes for this listener */
972
	INIT_LIST_HEAD(&reset_list);
973 974 975
	if (free_hanging_nodes) {
		spin_lock_irqsave(&cm_core->ht_lock, flags);
		list_for_each_safe(list_pos, list_temp,
976
				   &g_cm_core->connected_nodes) {
977 978 979
			cm_node = container_of(list_pos, struct nes_cm_node,
				list);
			if ((cm_node->listener == listener) &&
980 981 982
			    (!cm_node->accelerated)) {
				add_ref_cm_node(cm_node);
				list_add(&cm_node->reset_entry, &reset_list);
983 984 985 986
			}
		}
		spin_unlock_irqrestore(&cm_core->ht_lock, flags);
	}
987 988 989

	list_for_each_safe(list_pos, list_temp, &reset_list) {
		cm_node = container_of(list_pos, struct nes_cm_node,
990 991 992
				reset_entry);
		{
			struct nes_cm_node *loopback = cm_node->loopbackpartner;
F
Faisal Latif 已提交
993
			enum nes_cm_node_state old_state;
994 995 996 997 998 999 1000 1001 1002 1003 1004
			if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
				rem_ref_cm_node(cm_node->cm_core, cm_node);
			} else {
				if (!loopback) {
					cleanup_retrans_entry(cm_node);
					err = send_reset(cm_node, NULL);
					if (err) {
						cm_node->state =
							 NES_CM_STATE_CLOSED;
						WARN_ON(1);
					} else {
F
Faisal Latif 已提交
1005 1006 1007 1008 1009 1010
						old_state = cm_node->state;
						cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
						if (old_state != NES_CM_STATE_MPAREQ_RCVD)
							rem_ref_cm_node(
								cm_node->cm_core,
								cm_node);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
					}
				} else {
					struct nes_cm_event event;

					event.cm_node = loopback;
					event.cm_info.rem_addr =
							loopback->rem_addr;
					event.cm_info.loc_addr =
							loopback->loc_addr;
					event.cm_info.rem_port =
							loopback->rem_port;
					event.cm_info.loc_port =
							 loopback->loc_port;
					event.cm_info.cm_id = loopback->cm_id;
1025 1026
					add_ref_cm_node(loopback);
					loopback->state = NES_CM_STATE_CLOSED;
1027
					cm_event_connect_error(&event);
F
Faisal Latif 已提交
1028
					cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1029 1030 1031 1032 1033 1034 1035

					rem_ref_cm_node(cm_node->cm_core,
							 cm_node);

				}
			}
		}
1036 1037
	}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	spin_lock_irqsave(&cm_core->listen_list_lock, flags);
	if (!atomic_dec_return(&listener->ref_count)) {
		list_del(&listener->list);

		/* decrement our listen node count */
		atomic_dec(&cm_core->listen_node_cnt);

		spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);

		if (listener->nesvnic) {
			nes_manage_apbvt(listener->nesvnic, listener->loc_port,
					PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
		}

		nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);

		kfree(listener);
1055
		listener = NULL;
1056
		ret = 0;
1057
		atomic_inc(&cm_listens_destroyed);
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	} else {
		spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
	}
	if (listener) {
		if (atomic_read(&listener->pend_accepts_cnt) > 0)
			nes_debug(NES_DBG_CM, "destroying listener (%p)"
					" with non-zero pending accepts=%u\n",
					listener, atomic_read(&listener->pend_accepts_cnt));
	}

	return ret;
}


/**
 * mini_cm_del_listen
 */
static int mini_cm_del_listen(struct nes_cm_core *cm_core,
		struct nes_cm_listener *listener)
{
	listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
	listener->cm_id = NULL; /* going to be destroyed pretty soon */
	return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
}


/**
 * mini_cm_accelerated
 */
static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
		struct nes_cm_node *cm_node)
{
	u32 was_timer_set;
	cm_node->accelerated = 1;

	if (cm_node->accept_pend) {
		BUG_ON(!cm_node->listener);
		atomic_dec(&cm_node->listener->pend_accepts_cnt);
1096
		cm_node->accept_pend = 0;
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
		BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
	}

	was_timer_set = timer_pending(&cm_core->tcp_timer);
	if (!was_timer_set) {
		cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
		add_timer(&cm_core->tcp_timer);
	}

	return 0;
}


/**
B
Bob Sharp 已提交
1111
 * nes_addr_resolve_neigh
1112
 */
F
Faisal Latif 已提交
1113
static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
1114 1115
{
	struct rtable *rt;
B
Bob Sharp 已提交
1116
	struct neighbour *neigh;
F
Faisal Latif 已提交
1117
	int rc = arpindex;
1118
	struct net_device *netdev;
F
Faisal Latif 已提交
1119
	struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
1120

1121
	rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
1122
	if (IS_ERR(rt)) {
1123
		printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
1124
				__func__, dst_ip);
B
Bob Sharp 已提交
1125
		return rc;
1126 1127
	}

1128
	if (netif_is_bond_slave(nesvnic->netdev))
1129 1130 1131 1132 1133
		netdev = nesvnic->netdev->master;
	else
		netdev = nesvnic->netdev;

	neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
B
Bob Sharp 已提交
1134 1135 1136
	if (neigh) {
		if (neigh->nud_state & NUD_VALID) {
			nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
J
Johannes Berg 已提交
1137 1138
				  " is %pM, Gateway is 0x%08X \n", dst_ip,
				  neigh->ha, ntohl(rt->rt_gateway));
F
Faisal Latif 已提交
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

			if (arpindex >= 0) {
				if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
							neigh->ha, ETH_ALEN)){
					/* Mac address same as in nes_arp_table */
					neigh_release(neigh);
					ip_rt_put(rt);
					return rc;
				}

				nes_manage_arp_cache(nesvnic->netdev,
						nesadapter->arp_table[arpindex].mac_addr,
						dst_ip, NES_ARP_DELETE);
			}

B
Bob Sharp 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162
			nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
					     dst_ip, NES_ARP_ADD);
			rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
					   NES_ARP_RESOLVE);
		}
		neigh_release(neigh);
	}

	if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1163
		neigh_event_send(dst_get_neighbour(&rt->dst), NULL);
B
Bob Sharp 已提交
1164

1165
	ip_rt_put(rt);
B
Bob Sharp 已提交
1166
	return rc;
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
}

/**
 * make_cm_node - create a new instance of a cm node
 */
static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
		struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
		struct nes_cm_listener *listener)
{
	struct nes_cm_node *cm_node;
	struct timespec ts;
F
Faisal Latif 已提交
1178
	int oldarpindex = 0;
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
	int arpindex = 0;
	struct nes_device *nesdev;
	struct nes_adapter *nesadapter;

	/* create an hte and cm_node for this instance */
	cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
	if (!cm_node)
		return NULL;

	/* set our node specific transport info */
	cm_node->loc_addr = cm_info->loc_addr;
	cm_node->rem_addr = cm_info->rem_addr;
	cm_node->loc_port = cm_info->loc_port;
	cm_node->rem_port = cm_info->rem_port;
	cm_node->send_write0 = send_first;
H
Harvey Harrison 已提交
1194 1195 1196
	nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
		  &cm_node->loc_addr, cm_node->loc_port,
		  &cm_node->rem_addr, cm_node->rem_port);
1197 1198 1199 1200 1201
	cm_node->listener = listener;
	cm_node->netdev = nesvnic->netdev;
	cm_node->cm_id = cm_info->cm_id;
	memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);

1202 1203
	nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
			cm_node->cm_id);
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

	spin_lock_init(&cm_node->retrans_list_lock);

	cm_node->loopbackpartner = NULL;
	atomic_set(&cm_node->ref_count, 1);
	/* associate our parent CM core */
	cm_node->cm_core = cm_core;
	cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
	cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
	cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
			NES_CM_DEFAULT_RCV_WND_SCALE;
	ts = current_kernel_time();
	cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
	cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
1218
			sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	cm_node->tcp_cntxt.rcv_nxt = 0;
	/* get a unique session ID , add thread_id to an upcounter to handle race */
	atomic_inc(&cm_core->node_cnt);
	cm_node->conn_type = cm_info->conn_type;
	cm_node->apbvt_set = 0;
	cm_node->accept_pend = 0;

	cm_node->nesvnic = nesvnic;
	/* get some device handles, for arp lookup */
	nesdev = nesvnic->nesdev;
	nesadapter = nesdev->nesadapter;

	cm_node->loopbackpartner = NULL;
F
Faisal Latif 已提交
1232

1233
	/* get the mac addr for the remote node */
1234 1235
	if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
		arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
F
Faisal Latif 已提交
1236 1237 1238 1239 1240
	else {
		oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
		arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);

	}
1241
	if (arpindex < 0) {
F
Faisal Latif 已提交
1242 1243
		kfree(cm_node);
		return NULL;
1244 1245 1246 1247
	}

	/* copy the mac addr to node context */
	memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
J
Johannes Berg 已提交
1248 1249
	nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
		  cm_node->rem_mac);
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271

	add_hte_node(cm_core, cm_node);
	atomic_inc(&cm_nodes_created);

	return cm_node;
}


/**
 * add_ref_cm_node - destroy an instance of a cm node
 */
static int add_ref_cm_node(struct nes_cm_node *cm_node)
{
	atomic_inc(&cm_node->ref_count);
	return 0;
}


/**
 * rem_ref_cm_node - destroy an instance of a cm node
 */
static int rem_ref_cm_node(struct nes_cm_core *cm_core,
1272
	struct nes_cm_node *cm_node)
1273
{
1274
	unsigned long flags;
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
	struct nes_qp *nesqp;

	if (!cm_node)
		return -EINVAL;

	spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
	if (atomic_dec_return(&cm_node->ref_count)) {
		spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
		return 0;
	}
	list_del(&cm_node->list);
	atomic_dec(&cm_core->ht_node_cnt);
	spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);

	/* if the node is destroyed before connection was accelerated */
	if (!cm_node->accelerated && cm_node->accept_pend) {
		BUG_ON(!cm_node->listener);
		atomic_dec(&cm_node->listener->pend_accepts_cnt);
		BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
	}
1295 1296 1297
	WARN_ON(cm_node->send_entry);
	if (cm_node->recv_entry)
		handle_recv_entry(cm_node, 0);
1298 1299 1300 1301 1302
	if (cm_node->listener) {
		mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
	} else {
		if (cm_node->apbvt_set && cm_node->nesvnic) {
			nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
1303 1304 1305
				PCI_FUNC(
				cm_node->nesvnic->nesdev->pcidev->devfn),
				NES_MANAGE_APBVT_DEL);
1306 1307 1308 1309 1310
		}
	}

	atomic_dec(&cm_core->node_cnt);
	atomic_inc(&cm_nodes_destroyed);
1311 1312 1313 1314 1315 1316
	nesqp = cm_node->nesqp;
	if (nesqp) {
		nesqp->cm_node = NULL;
		nes_rem_ref(&nesqp->ibqp);
		cm_node->nesqp = NULL;
	}
1317

1318
	kfree(cm_node);
1319 1320 1321 1322 1323 1324
	return 0;
}

/**
 * process_options
 */
1325 1326
static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
	u32 optionsize, u32 syn_packet)
1327 1328 1329 1330 1331 1332 1333 1334 1335
{
	u32 tmp;
	u32 offset = 0;
	union all_known_options *all_options;
	char got_mss_option = 0;

	while (offset < optionsize) {
		all_options = (union all_known_options *)(optionsloc + offset);
		switch (all_options->as_base.optionnum) {
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
		case OPTION_NUMBER_END:
			offset = optionsize;
			break;
		case OPTION_NUMBER_NONE:
			offset += 1;
			continue;
		case OPTION_NUMBER_MSS:
			nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
				"Size: %d\n", __func__,
				all_options->as_mss.length, offset, optionsize);
			got_mss_option = 1;
			if (all_options->as_mss.length != 4) {
				return 1;
			} else {
				tmp = ntohs(all_options->as_mss.mss);
				if (tmp > 0 && tmp <
					cm_node->tcp_cntxt.mss)
					cm_node->tcp_cntxt.mss = tmp;
			}
			break;
		case OPTION_NUMBER_WINDOW_SCALE:
			cm_node->tcp_cntxt.snd_wscale =
				all_options->as_windowscale.shiftcount;
			break;
		case OPTION_NUMBER_WRITE0:
			cm_node->send_write0 = 1;
			break;
		default:
			nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
				all_options->as_base.optionnum);
			break;
1367 1368 1369 1370 1371 1372 1373 1374
		}
		offset += all_options->as_base.length;
	}
	if ((!got_mss_option) && (syn_packet))
		cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
	return 0;
}

1375 1376 1377 1378 1379
static void drop_packet(struct sk_buff *skb)
{
	atomic_inc(&cm_accel_dropped_pkts);
	dev_kfree_skb_any(skb);
}
1380

1381
static void handle_fin_pkt(struct nes_cm_node *cm_node)
1382
{
1383 1384 1385 1386 1387 1388 1389
	nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
		"refcnt=%d\n", cm_node, cm_node->state,
		atomic_read(&cm_node->ref_count));
	switch (cm_node->state) {
	case NES_CM_STATE_SYN_RCVD:
	case NES_CM_STATE_SYN_SENT:
	case NES_CM_STATE_ESTABLISHED:
1390
	case NES_CM_STATE_MPAREJ_RCVD:
1391 1392
		cm_node->tcp_cntxt.rcv_nxt++;
		cleanup_retrans_entry(cm_node);
1393
		cm_node->state = NES_CM_STATE_LAST_ACK;
1394
		send_fin(cm_node, NULL);
1395
		break;
1396 1397 1398 1399 1400 1401 1402 1403
	case NES_CM_STATE_MPAREQ_SENT:
		create_event(cm_node, NES_CM_EVENT_ABORTED);
		cm_node->tcp_cntxt.rcv_nxt++;
		cleanup_retrans_entry(cm_node);
		cm_node->state = NES_CM_STATE_CLOSED;
		add_ref_cm_node(cm_node);
		send_reset(cm_node, NULL);
		break;
1404
	case NES_CM_STATE_FIN_WAIT1:
1405 1406
		cm_node->tcp_cntxt.rcv_nxt++;
		cleanup_retrans_entry(cm_node);
1407
		cm_node->state = NES_CM_STATE_CLOSING;
1408
		send_ack(cm_node, NULL);
L
Lucas De Marchi 已提交
1409
		/* Wait for ACK as this is simultaneous close..
1410 1411
		* After we receive ACK, do not send anything..
		* Just rm the node.. Done.. */
1412 1413
		break;
	case NES_CM_STATE_FIN_WAIT2:
1414 1415
		cm_node->tcp_cntxt.rcv_nxt++;
		cleanup_retrans_entry(cm_node);
1416
		cm_node->state = NES_CM_STATE_TIME_WAIT;
1417 1418 1419 1420
		send_ack(cm_node, NULL);
		schedule_nes_timer(cm_node, NULL,  NES_TIMER_TYPE_CLOSE, 1, 0);
		break;
	case NES_CM_STATE_TIME_WAIT:
1421 1422
		cm_node->tcp_cntxt.rcv_nxt++;
		cleanup_retrans_entry(cm_node);
1423
		cm_node->state = NES_CM_STATE_CLOSED;
1424
		rem_ref_cm_node(cm_node->cm_core, cm_node);
1425 1426 1427 1428 1429 1430
		break;
	case NES_CM_STATE_TSA:
	default:
		nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
			cm_node, cm_node->state);
		break;
1431
	}
1432
}
1433 1434


1435 1436 1437
static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
	struct tcphdr *tcph)
{
1438

1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
	int	reset = 0;	/* whether to send reset in case of err.. */
	atomic_inc(&cm_resets_recvd);
	nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
			" refcnt=%d\n", cm_node, cm_node->state,
			atomic_read(&cm_node->ref_count));
	cleanup_retrans_entry(cm_node);
	switch (cm_node->state) {
	case NES_CM_STATE_SYN_SENT:
	case NES_CM_STATE_MPAREQ_SENT:
		nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
			"listener=%p state=%d\n", __func__, __LINE__, cm_node,
			cm_node->listener, cm_node->state);
		active_open_err(cm_node, skb, reset);
		break;
1453
	case NES_CM_STATE_MPAREQ_RCVD:
1454
		atomic_inc(&cm_node->passive_state);
1455 1456
		dev_kfree_skb_any(skb);
		break;
1457 1458 1459 1460 1461 1462 1463
	case NES_CM_STATE_ESTABLISHED:
	case NES_CM_STATE_SYN_RCVD:
	case NES_CM_STATE_LISTENING:
		nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
		passive_open_err(cm_node, skb, reset);
		break;
	case NES_CM_STATE_TSA:
1464 1465 1466 1467 1468
		active_open_err(cm_node, skb, reset);
		break;
	case NES_CM_STATE_CLOSED:
		drop_packet(skb);
		break;
1469
	case NES_CM_STATE_FIN_WAIT2:
1470
	case NES_CM_STATE_FIN_WAIT1:
1471 1472
	case NES_CM_STATE_LAST_ACK:
		cm_node->cm_id->rem_ref(cm_node->cm_id);
1473 1474 1475 1476 1477
	case NES_CM_STATE_TIME_WAIT:
		cm_node->state = NES_CM_STATE_CLOSED;
		rem_ref_cm_node(cm_node->cm_core, cm_node);
		drop_packet(skb);
		break;
1478
	default:
1479
		drop_packet(skb);
1480 1481 1482
		break;
	}
}
1483

1484 1485

static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
1486 1487
{

1488
	int	ret = 0;
1489 1490
	int datasize = skb->len;
	u8 *dataloc = skb->data;
1491 1492 1493 1494 1495

	enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
	u32     res_type;
	ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
	if (ret) {
1496
		nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1497
		if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
1498 1499 1500 1501 1502 1503 1504
			nes_debug(NES_DBG_CM, "%s[%u] create abort for "
				"cm_node=%p listener=%p state=%d\n", __func__,
				__LINE__, cm_node, cm_node->listener,
				cm_node->state);
			active_open_err(cm_node, skb, 1);
		} else {
			passive_open_err(cm_node, skb, 1);
1505
		}
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
		return;
	}

	switch (cm_node->state) {
	case NES_CM_STATE_ESTABLISHED:
		if (res_type == NES_MPA_REQUEST_REJECT) {
			/*BIG problem as we are receiving the MPA.. So should
			* not be REJECT.. This is Passive Open.. We can
			* only receive it Reject for Active Open...*/
			WARN_ON(1);
		}
		cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
		type = NES_CM_EVENT_MPA_REQ;
		atomic_set(&cm_node->passive_state,
				NES_PASSIVE_STATE_INDICATED);
		break;
	case NES_CM_STATE_MPAREQ_SENT:
1523
		cleanup_retrans_entry(cm_node);
1524 1525 1526 1527 1528
		if (res_type == NES_MPA_REQUEST_REJECT) {
			type = NES_CM_EVENT_MPA_REJECT;
			cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
		} else {
			type = NES_CM_EVENT_CONNECTED;
1529
			cm_node->state = NES_CM_STATE_TSA;
1530
		}
1531

1532 1533 1534 1535
		break;
	default:
		WARN_ON(1);
		break;
1536
	}
1537 1538
	dev_kfree_skb_any(skb);
	create_event(cm_node, type);
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
}

static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
{
	switch (cm_node->state) {
	case NES_CM_STATE_SYN_SENT:
	case NES_CM_STATE_MPAREQ_SENT:
		nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
			"listener=%p state=%d\n", __func__, __LINE__, cm_node,
			cm_node->listener, cm_node->state);
		active_open_err(cm_node, skb, 1);
		break;
	case NES_CM_STATE_ESTABLISHED:
	case NES_CM_STATE_SYN_RCVD:
		passive_open_err(cm_node, skb, 1);
		break;
	case NES_CM_STATE_TSA:
	default:
		drop_packet(skb);
1558
	}
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
}

static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
	struct sk_buff *skb)
{
	int err;

	err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
	if (err)
		active_open_err(cm_node, skb, 1);

	return err;
}

static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
	struct sk_buff *skb)
{
	int err = 0;
	u32 seq;
	u32 ack_seq;
	u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
	u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
	u32 rcv_wnd;
	seq = ntohl(tcph->seq);
	ack_seq = ntohl(tcph->ack_seq);
	rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
	if (ack_seq != loc_seq_num)
		err = 1;
1587
	else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
		err = 1;
	if (err) {
		nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
			"listener=%p state=%d\n", __func__, __LINE__, cm_node,
			cm_node->listener, cm_node->state);
		indicate_pkt_err(cm_node, skb);
		nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
			"rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
			rcv_wnd);
	}
	return err;
}

/*
 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
 * is created with a listener or it may comein as rexmitted packet which in
 * that case will be just dropped.
 */

static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
	struct tcphdr *tcph)
{
	int ret;
	u32 inc_sequence;
	int optionsize;
1613 1614

	optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1615
	skb_trim(skb, 0);
1616
	inc_sequence = ntohl(tcph->seq);
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 1642
	switch (cm_node->state) {
	case NES_CM_STATE_SYN_SENT:
	case NES_CM_STATE_MPAREQ_SENT:
		/* Rcvd syn on active open connection*/
		active_open_err(cm_node, skb, 1);
		break;
	case NES_CM_STATE_LISTENING:
		/* Passive OPEN */
		if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
				cm_node->listener->backlog) {
			nes_debug(NES_DBG_CM, "drop syn due to backlog "
				"pressure \n");
			cm_backlog_drops++;
			passive_open_err(cm_node, skb, 0);
			break;
		}
		ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
			1);
		if (ret) {
			passive_open_err(cm_node, skb, 0);
			/* drop pkt */
			break;
		}
		cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
		BUG_ON(cm_node->send_entry);
1643 1644 1645
		cm_node->accept_pend = 1;
		atomic_inc(&cm_node->listener->pend_accepts_cnt);

1646 1647 1648
		cm_node->state = NES_CM_STATE_SYN_RCVD;
		send_syn(cm_node, 1, skb);
		break;
1649 1650
	case NES_CM_STATE_CLOSED:
		cleanup_retrans_entry(cm_node);
1651
		add_ref_cm_node(cm_node);
1652 1653
		send_reset(cm_node, skb);
		break;
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
	case NES_CM_STATE_TSA:
	case NES_CM_STATE_ESTABLISHED:
	case NES_CM_STATE_FIN_WAIT1:
	case NES_CM_STATE_FIN_WAIT2:
	case NES_CM_STATE_MPAREQ_RCVD:
	case NES_CM_STATE_LAST_ACK:
	case NES_CM_STATE_CLOSING:
	case NES_CM_STATE_UNKNOWN:
	default:
		drop_packet(skb);
		break;
	}
}

static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
	struct tcphdr *tcph)
{

	int ret;
	u32 inc_sequence;
	int optionsize;

	optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1677
	skb_trim(skb, 0);
1678 1679 1680
	inc_sequence = ntohl(tcph->seq);
	switch (cm_node->state) {
	case NES_CM_STATE_SYN_SENT:
1681
		cleanup_retrans_entry(cm_node);
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
		/* active open */
		if (check_syn(cm_node, tcph, skb))
			return;
		cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
		/* setup options */
		ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
		if (ret) {
			nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
				cm_node);
			break;
		}
		cleanup_retrans_entry(cm_node);
		cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
		send_mpa_request(cm_node, skb);
		cm_node->state = NES_CM_STATE_MPAREQ_SENT;
		break;
	case NES_CM_STATE_MPAREQ_RCVD:
		/* passive open, so should not be here */
		passive_open_err(cm_node, skb, 1);
		break;
1702
	case NES_CM_STATE_LISTENING:
1703 1704 1705 1706 1707
		cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
		cleanup_retrans_entry(cm_node);
		cm_node->state = NES_CM_STATE_CLOSED;
		send_reset(cm_node, skb);
		break;
1708 1709 1710
	case NES_CM_STATE_CLOSED:
		cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
		cleanup_retrans_entry(cm_node);
1711
		add_ref_cm_node(cm_node);
1712 1713
		send_reset(cm_node, skb);
		break;
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
	case NES_CM_STATE_ESTABLISHED:
	case NES_CM_STATE_FIN_WAIT1:
	case NES_CM_STATE_FIN_WAIT2:
	case NES_CM_STATE_LAST_ACK:
	case NES_CM_STATE_TSA:
	case NES_CM_STATE_CLOSING:
	case NES_CM_STATE_UNKNOWN:
	case NES_CM_STATE_MPAREQ_SENT:
	default:
		drop_packet(skb);
		break;
	}
}
1727

1728
static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1729 1730 1731 1732
	struct tcphdr *tcph)
{
	int datasize = 0;
	u32 inc_sequence;
1733
	int ret = 0;
1734 1735 1736
	int optionsize;
	optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);

1737
	if (check_seq(cm_node, tcph, skb))
1738
		return -EINVAL;
1739 1740

	skb_pull(skb, tcph->doff << 2);
1741
	inc_sequence = ntohl(tcph->seq);
1742 1743 1744 1745
	datasize = skb->len;
	switch (cm_node->state) {
	case NES_CM_STATE_SYN_RCVD:
		/* Passive OPEN */
1746
		cleanup_retrans_entry(cm_node);
1747 1748 1749
		ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
		if (ret)
			break;
1750 1751 1752 1753
		cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
		cm_node->state = NES_CM_STATE_ESTABLISHED;
		if (datasize) {
			cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1754
			handle_rcv_mpa(cm_node, skb);
1755
		} else  /* rcvd ACK only */
1756 1757 1758 1759
			dev_kfree_skb_any(skb);
		break;
	case NES_CM_STATE_ESTABLISHED:
		/* Passive OPEN */
1760
		cleanup_retrans_entry(cm_node);
1761 1762
		if (datasize) {
			cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1763
			handle_rcv_mpa(cm_node, skb);
1764 1765 1766 1767 1768 1769 1770
		} else
			drop_packet(skb);
		break;
	case NES_CM_STATE_MPAREQ_SENT:
		cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
		if (datasize) {
			cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1771
			handle_rcv_mpa(cm_node, skb);
1772
		} else  /* Could be just an ack pkt.. */
1773 1774
			dev_kfree_skb_any(skb);
		break;
1775
	case NES_CM_STATE_LISTENING:
1776 1777 1778 1779
		cleanup_retrans_entry(cm_node);
		cm_node->state = NES_CM_STATE_CLOSED;
		send_reset(cm_node, skb);
		break;
1780 1781
	case NES_CM_STATE_CLOSED:
		cleanup_retrans_entry(cm_node);
1782
		add_ref_cm_node(cm_node);
1783 1784
		send_reset(cm_node, skb);
		break;
1785
	case NES_CM_STATE_LAST_ACK:
1786
	case NES_CM_STATE_CLOSING:
1787 1788 1789 1790 1791 1792
		cleanup_retrans_entry(cm_node);
		cm_node->state = NES_CM_STATE_CLOSED;
		cm_node->cm_id->rem_ref(cm_node->cm_id);
		rem_ref_cm_node(cm_node->cm_core, cm_node);
		drop_packet(skb);
		break;
1793
	case NES_CM_STATE_FIN_WAIT1:
1794 1795 1796 1797
		cleanup_retrans_entry(cm_node);
		drop_packet(skb);
		cm_node->state = NES_CM_STATE_FIN_WAIT2;
		break;
1798 1799 1800 1801 1802 1803
	case NES_CM_STATE_SYN_SENT:
	case NES_CM_STATE_FIN_WAIT2:
	case NES_CM_STATE_TSA:
	case NES_CM_STATE_MPAREQ_RCVD:
	case NES_CM_STATE_UNKNOWN:
	default:
1804
		cleanup_retrans_entry(cm_node);
1805 1806
		drop_packet(skb);
		break;
1807
	}
1808
	return ret;
1809
}
1810 1811 1812



1813 1814 1815 1816
static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
	struct sk_buff *skb, int optionsize, int passive)
{
	u8 *optionsloc = (u8 *)&tcph[1];
1817
	if (optionsize) {
1818 1819 1820 1821 1822
		if (process_options(cm_node, optionsloc, optionsize,
			(u32)tcph->syn)) {
			nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
				__func__, cm_node);
			if (passive)
1823
				passive_open_err(cm_node, skb, 1);
1824
			else
1825
				active_open_err(cm_node, skb, 1);
1826
			return 1;
1827
		}
1828
	}
1829 1830 1831 1832

	cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
			cm_node->tcp_cntxt.snd_wscale;

1833
	if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
1834
		cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
1835 1836
	return 0;
}
1837

1838 1839 1840 1841
/*
 * active_open_err() will send reset() if flag set..
 * It will also send ABORT event.
 */
1842

1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
	int reset)
{
	cleanup_retrans_entry(cm_node);
	if (reset) {
		nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
				"state=%d\n", cm_node, cm_node->state);
		add_ref_cm_node(cm_node);
		send_reset(cm_node, skb);
	} else
		dev_kfree_skb_any(skb);
1854

1855 1856 1857
	cm_node->state = NES_CM_STATE_CLOSED;
	create_event(cm_node, NES_CM_EVENT_ABORTED);
}
1858

1859 1860 1861 1862
/*
 * passive_open_err() will either do a reset() or will free up the skb and
 * remove the cm_node.
 */
1863

1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
	int reset)
{
	cleanup_retrans_entry(cm_node);
	cm_node->state = NES_CM_STATE_CLOSED;
	if (reset) {
		nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
			"cm_node=%p state =%d\n", cm_node, cm_node->state);
		send_reset(cm_node, skb);
	} else {
		dev_kfree_skb_any(skb);
		rem_ref_cm_node(cm_node->cm_core, cm_node);
1876
	}
1877
}
1878

1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
/*
 * free_retrans_entry() routines assumes that the retrans_list_lock has
 * been acquired before calling.
 */
static void free_retrans_entry(struct nes_cm_node *cm_node)
{
	struct nes_timer_entry *send_entry;
	send_entry = cm_node->send_entry;
	if (send_entry) {
		cm_node->send_entry = NULL;
		dev_kfree_skb_any(send_entry->skb);
		kfree(send_entry);
		rem_ref_cm_node(cm_node->cm_core, cm_node);
1892
	}
1893
}
1894

1895 1896 1897
static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
{
	unsigned long flags;
1898

1899 1900 1901
	spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
	free_retrans_entry(cm_node);
	spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1902 1903
}

1904 1905 1906 1907 1908 1909 1910 1911 1912
/**
 * process_packet
 * Returns skb if to be freed, else it will return NULL if already used..
 */
static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
	struct nes_cm_core *cm_core)
{
	enum nes_tcpip_pkt_type	pkt_type = NES_PKT_TYPE_UNKNOWN;
	struct tcphdr *tcph = tcp_hdr(skb);
1913
	u32     fin_set = 0;
1914
	int ret = 0;
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
	skb_pull(skb, ip_hdr(skb)->ihl << 2);

	nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
		"ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
		tcph->ack, tcph->rst, tcph->fin);

	if (tcph->rst)
		pkt_type = NES_PKT_TYPE_RST;
	else if (tcph->syn) {
		pkt_type = NES_PKT_TYPE_SYN;
		if (tcph->ack)
			pkt_type = NES_PKT_TYPE_SYNACK;
1927
	} else if (tcph->ack)
1928
		pkt_type = NES_PKT_TYPE_ACK;
1929 1930
	if (tcph->fin)
		fin_set = 1;
1931 1932 1933 1934 1935 1936 1937 1938 1939

	switch (pkt_type) {
	case NES_PKT_TYPE_SYN:
		handle_syn_pkt(cm_node, skb, tcph);
		break;
	case NES_PKT_TYPE_SYNACK:
		handle_synack_pkt(cm_node, skb, tcph);
		break;
	case NES_PKT_TYPE_ACK:
1940 1941
		ret = handle_ack_pkt(cm_node, skb, tcph);
		if (fin_set && !ret)
1942
			handle_fin_pkt(cm_node);
1943 1944 1945 1946 1947
		break;
	case NES_PKT_TYPE_RST:
		handle_rst_pkt(cm_node, skb, tcph);
		break;
	default:
1948
		if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
1949
			handle_fin_pkt(cm_node);
1950
		drop_packet(skb);
1951 1952 1953
		break;
	}
}
1954 1955 1956 1957 1958

/**
 * mini_cm_listen - create a listen node with params
 */
static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
1959
	struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
{
	struct nes_cm_listener *listener;
	unsigned long flags;

	nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
		cm_info->loc_addr, cm_info->loc_port);

	/* cannot have multiple matching listeners */
	listener = find_listener(cm_core, htonl(cm_info->loc_addr),
			htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
	if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
		/* find automatically incs ref count ??? */
		atomic_dec(&listener->ref_count);
		nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
		return NULL;
	}

	if (!listener) {
		/* create a CM listen node (1/2 node to compare incoming traffic to) */
		listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
		if (!listener) {
			nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
			return NULL;
		}

		listener->loc_addr = htonl(cm_info->loc_addr);
		listener->loc_port = htons(cm_info->loc_port);
		listener->reused_node = 0;

		atomic_set(&listener->ref_count, 1);
	}
	/* pasive case */
	/* find already inc'ed the ref count */
	else {
		listener->reused_node = 1;
	}

	listener->cm_id = cm_info->cm_id;
	atomic_set(&listener->pend_accepts_cnt, 0);
	listener->cm_core = cm_core;
	listener->nesvnic = nesvnic;
	atomic_inc(&cm_core->node_cnt);

	listener->conn_type = cm_info->conn_type;
	listener->backlog = cm_info->backlog;
	listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;

	if (!listener->reused_node) {
		spin_lock_irqsave(&cm_core->listen_list_lock, flags);
		list_add(&listener->list, &cm_core->listen_list.list);
		spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
		atomic_inc(&cm_core->listen_node_cnt);
	}

	nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
			" listener = %p, backlog = %d, cm_id = %p.\n",
			cm_info->loc_addr, cm_info->loc_port,
			listener, listener->backlog, listener->cm_id);

	return listener;
}


/**
 * mini_cm_connect - make a connection node with params
 */
2026
static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
2027 2028
	struct nes_vnic *nesvnic, u16 private_data_len,
	void *private_data, struct nes_cm_info *cm_info)
2029 2030 2031 2032 2033 2034
{
	int ret = 0;
	struct nes_cm_node *cm_node;
	struct nes_cm_listener *loopbackremotelistener;
	struct nes_cm_node *loopbackremotenode;
	struct nes_cm_info loopback_cm_info;
2035 2036
	u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
	struct ietf_mpa_frame *mpa_frame = NULL;
2037 2038 2039 2040 2041

	/* create a CM connection node */
	cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
	if (!cm_node)
		return NULL;
2042
	mpa_frame = &cm_node->mpa_frame;
2043
	memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
2044 2045 2046
	mpa_frame->flags = IETF_MPA_FLAGS_CRC;
	mpa_frame->rev =  IETF_MPA_VERSION;
	mpa_frame->priv_data_len = htons(private_data_len);
2047

G
Glenn Streiff 已提交
2048
	/* set our node side to client (active) side */
2049 2050 2051 2052
	cm_node->tcp_cntxt.client = 1;
	cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;

	if (cm_info->loc_addr == cm_info->rem_addr) {
2053 2054 2055
		loopbackremotelistener = find_listener(cm_core,
				ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
				NES_CM_LISTENER_ACTIVE_STATE);
2056 2057 2058 2059 2060 2061 2062
		if (loopbackremotelistener == NULL) {
			create_event(cm_node, NES_CM_EVENT_ABORTED);
		} else {
			loopback_cm_info = *cm_info;
			loopback_cm_info.loc_port = cm_info->rem_port;
			loopback_cm_info.rem_port = cm_info->loc_port;
			loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
2063 2064
			loopbackremotenode = make_cm_node(cm_core, nesvnic,
				&loopback_cm_info, loopbackremotelistener);
2065 2066 2067 2068 2069
			if (!loopbackremotenode) {
				rem_ref_cm_node(cm_node->cm_core, cm_node);
				return NULL;
			}
			atomic_inc(&cm_loopbacks);
2070
			loopbackremotenode->loopbackpartner = cm_node;
2071 2072
			loopbackremotenode->tcp_cntxt.rcv_wscale =
				NES_CM_DEFAULT_RCV_WND_SCALE;
2073
			cm_node->loopbackpartner = loopbackremotenode;
2074 2075 2076
			memcpy(loopbackremotenode->mpa_frame_buf, private_data,
				private_data_len);
			loopbackremotenode->mpa_frame_size = private_data_len;
2077

2078 2079
			/* we are done handling this state. */
			/* set node to a TSA state */
2080
			cm_node->state = NES_CM_STATE_TSA;
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
			cm_node->tcp_cntxt.rcv_nxt =
				loopbackremotenode->tcp_cntxt.loc_seq_num;
			loopbackremotenode->tcp_cntxt.rcv_nxt =
				cm_node->tcp_cntxt.loc_seq_num;
			cm_node->tcp_cntxt.max_snd_wnd =
				loopbackremotenode->tcp_cntxt.rcv_wnd;
			loopbackremotenode->tcp_cntxt.max_snd_wnd =
				cm_node->tcp_cntxt.rcv_wnd;
			cm_node->tcp_cntxt.snd_wnd =
				loopbackremotenode->tcp_cntxt.rcv_wnd;
			loopbackremotenode->tcp_cntxt.snd_wnd =
				cm_node->tcp_cntxt.rcv_wnd;
			cm_node->tcp_cntxt.snd_wscale =
				loopbackremotenode->tcp_cntxt.rcv_wscale;
			loopbackremotenode->tcp_cntxt.snd_wscale =
				cm_node->tcp_cntxt.rcv_wscale;
2097
			loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
2098 2099 2100 2101 2102 2103 2104 2105
			create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
		}
		return cm_node;
	}

	/* set our node side to client (active) side */
	cm_node->tcp_cntxt.client = 1;
	/* init our MPA frame ptr */
2106 2107
	memcpy(mpa_frame->priv_data, private_data, private_data_len);

2108 2109 2110 2111
	cm_node->mpa_frame_size = mpa_frame_size;

	/* send a syn and goto syn sent state */
	cm_node->state = NES_CM_STATE_SYN_SENT;
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
	ret = send_syn(cm_node, 0, NULL);

	if (ret) {
		/* error in sending the syn free up the cm_node struct */
		nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
			"addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
			cm_node->rem_addr, cm_node->rem_port, cm_node,
			cm_node->cm_id);
		rem_ref_cm_node(cm_node->cm_core, cm_node);
		cm_node = NULL;
	}
2123

2124 2125 2126 2127 2128
	if (cm_node)
		nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
			"port=0x%04x, cm_node=%p, cm_id = %p.\n",
			cm_node->rem_addr, cm_node->rem_port, cm_node,
			cm_node->cm_id);
2129 2130 2131 2132 2133 2134 2135 2136 2137

	return cm_node;
}


/**
 * mini_cm_accept - accept a connection
 * This function is never called
 */
2138 2139
static int mini_cm_accept(struct nes_cm_core *cm_core,
	struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
2140 2141 2142 2143 2144 2145 2146 2147
{
	return 0;
}


/**
 * mini_cm_reject - reject and teardown a connection
 */
2148
static int mini_cm_reject(struct nes_cm_core *cm_core,
2149
	struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
2150 2151
{
	int ret = 0;
2152
	int err = 0;
2153
	int passive_state;
2154 2155 2156
	struct nes_cm_event event;
	struct iw_cm_id *cm_id = cm_node->cm_id;
	struct nes_cm_node *loopback = cm_node->loopbackpartner;
2157

2158 2159
	nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
		__func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
2160

2161 2162 2163
	if (cm_node->tcp_cntxt.client)
		return ret;
	cleanup_retrans_entry(cm_node);
2164

2165 2166 2167 2168 2169 2170
	if (!loopback) {
		passive_state = atomic_add_return(1, &cm_node->passive_state);
		if (passive_state == NES_SEND_RESET_EVENT) {
			cm_node->state = NES_CM_STATE_CLOSED;
			rem_ref_cm_node(cm_core, cm_node);
		} else {
F
Faisal Latif 已提交
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
			if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
				rem_ref_cm_node(cm_core, cm_node);
			} else {
				ret = send_mpa_reject(cm_node);
				if (ret) {
					cm_node->state = NES_CM_STATE_CLOSED;
					err = send_reset(cm_node, NULL);
					if (err)
						WARN_ON(1);
				} else
					cm_id->add_ref(cm_id);
			}
2183 2184 2185
		}
	} else {
		cm_node->cm_id = NULL;
F
Faisal Latif 已提交
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
		if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
			rem_ref_cm_node(cm_core, cm_node);
			rem_ref_cm_node(cm_core, loopback);
		} else {
			event.cm_node = loopback;
			event.cm_info.rem_addr = loopback->rem_addr;
			event.cm_info.loc_addr = loopback->loc_addr;
			event.cm_info.rem_port = loopback->rem_port;
			event.cm_info.loc_port = loopback->loc_port;
			event.cm_info.cm_id = loopback->cm_id;
			cm_event_mpa_reject(&event);
			rem_ref_cm_node(cm_core, cm_node);
			loopback->state = NES_CM_STATE_CLOSING;
2199

F
Faisal Latif 已提交
2200 2201 2202 2203
			cm_id = loopback->cm_id;
			rem_ref_cm_node(cm_core, loopback);
			cm_id->rem_ref(cm_id);
		}
2204 2205
	}

2206 2207 2208 2209 2210 2211 2212
	return ret;
}


/**
 * mini_cm_close
 */
2213
static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2214 2215 2216 2217 2218 2219 2220
{
	int ret = 0;

	if (!cm_core || !cm_node)
		return -EINVAL;

	switch (cm_node->state) {
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
	case NES_CM_STATE_SYN_RCVD:
	case NES_CM_STATE_SYN_SENT:
	case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
	case NES_CM_STATE_ESTABLISHED:
	case NES_CM_STATE_ACCEPTING:
	case NES_CM_STATE_MPAREQ_SENT:
	case NES_CM_STATE_MPAREQ_RCVD:
		cleanup_retrans_entry(cm_node);
		send_reset(cm_node, NULL);
		break;
	case NES_CM_STATE_CLOSE_WAIT:
		cm_node->state = NES_CM_STATE_LAST_ACK;
		send_fin(cm_node, NULL);
		break;
	case NES_CM_STATE_FIN_WAIT1:
	case NES_CM_STATE_FIN_WAIT2:
	case NES_CM_STATE_LAST_ACK:
	case NES_CM_STATE_TIME_WAIT:
	case NES_CM_STATE_CLOSING:
		ret = -1;
		break;
	case NES_CM_STATE_LISTENING:
2243 2244 2245 2246
		cleanup_retrans_entry(cm_node);
		send_reset(cm_node, NULL);
		break;
	case NES_CM_STATE_MPAREJ_RCVD:
2247 2248 2249
	case NES_CM_STATE_UNKNOWN:
	case NES_CM_STATE_INITED:
	case NES_CM_STATE_CLOSED:
F
Faisal Latif 已提交
2250
	case NES_CM_STATE_LISTENER_DESTROYED:
2251 2252 2253 2254 2255 2256 2257 2258
		ret = rem_ref_cm_node(cm_core, cm_node);
		break;
	case NES_CM_STATE_TSA:
		if (cm_node->send_entry)
			printk(KERN_ERR "ERROR Close got called from STATE_TSA "
				"send_entry=%p\n", cm_node->send_entry);
		ret = rem_ref_cm_node(cm_core, cm_node);
		break;
2259 2260 2261 2262 2263 2264 2265 2266 2267
	}
	return ret;
}


/**
 * recv_pkt - recv an ETHERNET packet, and process it through CM
 * node state machine
 */
2268
static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2269
	struct nes_vnic *nesvnic, struct sk_buff *skb)
2270 2271 2272 2273 2274 2275
{
	struct nes_cm_node *cm_node = NULL;
	struct nes_cm_listener *listener = NULL;
	struct iphdr *iph;
	struct tcphdr *tcph;
	struct nes_cm_info nfo;
2276
	int skb_handled = 1;
2277
	__be32 tmp_daddr, tmp_saddr;
2278

2279
	if (!skb)
2280
		return 0;
2281
	if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
2282
		return 0;
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
	}

	iph = (struct iphdr *)skb->data;
	tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));

	nfo.loc_addr = ntohl(iph->daddr);
	nfo.loc_port = ntohs(tcph->dest);
	nfo.rem_addr = ntohl(iph->saddr);
	nfo.rem_port = ntohs(tcph->source);

2293 2294 2295
	tmp_daddr = cpu_to_be32(iph->daddr);
	tmp_saddr = cpu_to_be32(iph->saddr);

H
Harvey Harrison 已提交
2296
	nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
2297
		  &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
2298

2299 2300
	do {
		cm_node = find_node(cm_core,
2301 2302 2303 2304
			nfo.rem_port, nfo.rem_addr,
			nfo.loc_port, nfo.loc_addr);

		if (!cm_node) {
2305 2306 2307
			/* Only type of packet accepted are for */
			/* the PASSIVE open (syn only) */
			if ((!tcph->syn) || (tcph->ack)) {
2308
				skb_handled = 0;
2309 2310 2311 2312 2313
				break;
			}
			listener = find_listener(cm_core, nfo.loc_addr,
				nfo.loc_port,
				NES_CM_LISTENER_ACTIVE_STATE);
2314 2315 2316 2317 2318
			if (!listener) {
				nfo.cm_id = NULL;
				nfo.conn_type = 0;
				nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
				skb_handled = 0;
2319 2320
				break;
			}
2321 2322
			nfo.cm_id = listener->cm_id;
			nfo.conn_type = listener->conn_type;
2323 2324 2325 2326 2327 2328
			cm_node = make_cm_node(cm_core, nesvnic, &nfo,
				listener);
			if (!cm_node) {
				nes_debug(NES_DBG_CM, "Unable to allocate "
					"node\n");
				cm_packets_dropped++;
2329
				atomic_dec(&listener->ref_count);
2330 2331
				dev_kfree_skb_any(skb);
				break;
2332
			}
2333 2334 2335 2336 2337 2338 2339
			if (!tcph->rst && !tcph->fin) {
				cm_node->state = NES_CM_STATE_LISTENING;
			} else {
				cm_packets_dropped++;
				rem_ref_cm_node(cm_core, cm_node);
				dev_kfree_skb_any(skb);
				break;
2340
			}
2341 2342
			add_ref_cm_node(cm_node);
		} else if (cm_node->state == NES_CM_STATE_TSA) {
2343 2344 2345 2346 2347 2348 2349
			if (cm_node->nesqp->pau_mode)
				nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
			else {
				rem_ref_cm_node(cm_core, cm_node);
				atomic_inc(&cm_accel_dropped_pkts);
				dev_kfree_skb_any(skb);
			}
2350
			break;
2351
		}
2352 2353 2354
		skb_reset_network_header(skb);
		skb_set_transport_header(skb, sizeof(*tcph));
		skb->len = ntohs(iph->tot_len);
2355 2356 2357
		process_packet(cm_node, skb, cm_core);
		rem_ref_cm_node(cm_core, cm_node);
	} while (0);
2358
	return skb_handled;
2359 2360 2361 2362 2363 2364
}


/**
 * nes_cm_alloc_core - allocate a top level instance of a cm core
 */
2365
static struct nes_cm_core *nes_cm_alloc_core(void)
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 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
{
	struct nes_cm_core *cm_core;

	/* setup the CM core */
	/* alloc top level core control structure */
	cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
	if (!cm_core)
		return NULL;

	INIT_LIST_HEAD(&cm_core->connected_nodes);
	init_timer(&cm_core->tcp_timer);
	cm_core->tcp_timer.function = nes_cm_timer_tick;

	cm_core->mtu   = NES_CM_DEFAULT_MTU;
	cm_core->state = NES_CM_STATE_INITED;
	cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;

	atomic_set(&cm_core->events_posted, 0);

	cm_core->api = &nes_cm_api;

	spin_lock_init(&cm_core->ht_lock);
	spin_lock_init(&cm_core->listen_list_lock);

	INIT_LIST_HEAD(&cm_core->listen_list.list);

	nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);

	nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
	cm_core->event_wq = create_singlethread_workqueue("nesewq");
	cm_core->post_event = nes_cm_post_event;
	nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
	cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");

	print_core(cm_core);
	return cm_core;
}


/**
 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
 */
2408
static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
{
	nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);

	if (!cm_core)
		return -EINVAL;

	barrier();

	if (timer_pending(&cm_core->tcp_timer)) {
		del_timer(&cm_core->tcp_timer);
	}

	destroy_workqueue(cm_core->event_wq);
	destroy_workqueue(cm_core->disconn_wq);
	nes_debug(NES_DBG_CM, "\n");
	kfree(cm_core);

	return 0;
}


/**
 * mini_cm_get
 */
2433
static int mini_cm_get(struct nes_cm_core *cm_core)
2434 2435 2436 2437 2438 2439 2440 2441
{
	return cm_core->state;
}


/**
 * mini_cm_set
 */
2442
static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
2443 2444 2445 2446
{
	int ret = 0;

	switch (type) {
2447 2448 2449 2450 2451 2452 2453 2454 2455
	case NES_CM_SET_PKT_SIZE:
		cm_core->mtu = value;
		break;
	case NES_CM_SET_FREE_PKT_Q_SIZE:
		cm_core->free_tx_pkt_max = value;
		break;
	default:
		/* unknown set option */
		ret = -EINVAL;
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 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
	}

	return ret;
}


/**
 * nes_cm_init_tsa_conn setup HW; MPA frames must be
 * successfully exchanged when this is called
 */
static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
{
	int ret = 0;

	if (!nesqp)
		return -EINVAL;

	nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
			NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
			NES_QPCONTEXT_MISC_DROS);

	if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
		nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);

	nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);

	nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);

	nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
			(u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);

	nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
			(cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
			NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);

	nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
			(cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
			NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);

	nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
	nesqp->nesqp_context->ts_recent = 0;
	nesqp->nesqp_context->ts_age = 0;
	nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
	nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
	nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
	nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
			cm_node->tcp_cntxt.rcv_wscale);
	nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
	nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
	nesqp->nesqp_context->srtt = 0;
	nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
	nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
	nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
	nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
	nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
	nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);

	nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
			" Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
			nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
			le32_to_cpu(nesqp->nesqp_context->snd_nxt),
			cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
			le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
			le32_to_cpu(nesqp->nesqp_context->misc));
	nes_debug(NES_DBG_CM, "  snd_wnd  = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
	nes_debug(NES_DBG_CM, "  snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
	nes_debug(NES_DBG_CM, "  max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));

	nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
	cm_node->state = NES_CM_STATE_TSA;

	return ret;
}


/**
 * nes_cm_disconn
 */
int nes_cm_disconn(struct nes_qp *nesqp)
{
2536
	struct disconn_work *work;
2537

2538 2539 2540
	work = kzalloc(sizeof *work, GFP_ATOMIC);
	if (!work)
		return -ENOMEM; /* Timer will clean up */
2541

2542 2543 2544 2545
	nes_add_ref(&nesqp->ibqp);
	work->nesqp = nesqp;
	INIT_WORK(&work->work, nes_disconnect_worker);
	queue_work(g_cm_core->disconn_wq, &work->work);
2546 2547 2548 2549 2550 2551 2552
	return 0;
}


/**
 * nes_disconnect_worker
 */
2553
static void nes_disconnect_worker(struct work_struct *work)
2554
{
2555 2556
	struct disconn_work *dwork = container_of(work, struct disconn_work, work);
	struct nes_qp *nesqp = dwork->nesqp;
2557

2558
	kfree(dwork);
2559 2560 2561
	nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
			nesqp->last_aeq, nesqp->hwqp.qp_id);
	nes_cm_disconn_true(nesqp);
2562
	nes_rem_ref(&nesqp->ibqp);
2563 2564 2565 2566 2567 2568
}


/**
 * nes_cm_disconn_true
 */
2569
static int nes_cm_disconn_true(struct nes_qp *nesqp)
2570 2571 2572 2573 2574 2575 2576 2577 2578
{
	unsigned long flags;
	int ret = 0;
	struct iw_cm_id *cm_id;
	struct iw_cm_event cm_event;
	struct nes_vnic *nesvnic;
	u16 last_ae;
	u8 original_hw_tcp_state;
	u8 original_ibqp_state;
2579
	int disconn_status = 0;
2580 2581 2582 2583 2584
	int issue_disconn = 0;
	int issue_close = 0;
	int issue_flush = 0;
	u32 flush_q = NES_CQP_FLUSH_RQ;
	struct ib_event ibevent;
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607

	if (!nesqp) {
		nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
		return -1;
	}

	spin_lock_irqsave(&nesqp->lock, flags);
	cm_id = nesqp->cm_id;
	/* make sure we havent already closed this connection */
	if (!cm_id) {
		nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
				nesqp->hwqp.qp_id);
		spin_unlock_irqrestore(&nesqp->lock, flags);
		return -1;
	}

	nesvnic = to_nesvnic(nesqp->ibqp.device);
	nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);

	original_hw_tcp_state = nesqp->hw_tcp_state;
	original_ibqp_state   = nesqp->ibqp_state;
	last_ae = nesqp->last_aeq;

2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
	if (nesqp->term_flags) {
		issue_disconn = 1;
		issue_close = 1;
		nesqp->cm_id = NULL;
		if (nesqp->flush_issued == 0) {
			nesqp->flush_issued = 1;
			issue_flush = 1;
		}
	} else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
			((original_ibqp_state == IB_QPS_RTS) &&
			(last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
		issue_disconn = 1;
		if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2621
			disconn_status = -ECONNRESET;
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
	}

	if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
		 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
		 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
		 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
		issue_close = 1;
		nesqp->cm_id = NULL;
		if (nesqp->flush_issued == 0) {
			nesqp->flush_issued = 1;
			issue_flush = 1;
		}
	}
2635

2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650
	spin_unlock_irqrestore(&nesqp->lock, flags);

	if ((issue_flush) && (nesqp->destroyed == 0)) {
		/* Flush the queue(s) */
		if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
			flush_q |= NES_CQP_FLUSH_SQ;
		flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);

		if (nesqp->term_flags) {
			ibevent.device = nesqp->ibqp.device;
			ibevent.event = nesqp->terminate_eventtype;
			ibevent.element.qp = &nesqp->ibqp;
			nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
		}
	}
2651

2652 2653
	if ((cm_id) && (cm_id->event_handler)) {
		if (issue_disconn) {
2654 2655
			atomic_inc(&cm_disconnects);
			cm_event.event = IW_CM_EVENT_DISCONNECT;
2656
			cm_event.status = disconn_status;
2657 2658 2659 2660 2661
			cm_event.local_addr = cm_id->local_addr;
			cm_event.remote_addr = cm_id->remote_addr;
			cm_event.private_data = NULL;
			cm_event.private_data_len = 0;

2662 2663 2664 2665 2666 2667
			nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
				" for  QP%u, SQ Head = %u, SQ Tail = %u. "
				"cm_id = %p, refcount = %u.\n",
				nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
				nesqp->hwqp.sq_tail, cm_id,
				atomic_read(&nesqp->refcount));
2668 2669 2670

			ret = cm_id->event_handler(cm_id, &cm_event);
			if (ret)
2671 2672
				nes_debug(NES_DBG_CM, "OFA CM event_handler "
					"returned, ret=%d\n", ret);
2673 2674
		}

2675
		if (issue_close) {
2676 2677 2678 2679 2680 2681
			atomic_inc(&cm_closes);
			nes_disconnect(nesqp, 1);

			cm_id->provider_data = nesqp;
			/* Send up the close complete event */
			cm_event.event = IW_CM_EVENT_CLOSE;
2682
			cm_event.status = 0;
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704
			cm_event.provider_data = cm_id->provider_data;
			cm_event.local_addr = cm_id->local_addr;
			cm_event.remote_addr = cm_id->remote_addr;
			cm_event.private_data = NULL;
			cm_event.private_data_len = 0;

			ret = cm_id->event_handler(cm_id, &cm_event);
			if (ret) {
				nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
			}

			cm_id->rem_ref(cm_id);
		}
	}

	return 0;
}


/**
 * nes_disconnect
 */
2705
static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
2706 2707 2708 2709
{
	int ret = 0;
	struct nes_vnic *nesvnic;
	struct nes_device *nesdev;
2710
	struct nes_ib_device *nesibdev;
2711 2712 2713 2714 2715 2716

	nesvnic = to_nesvnic(nesqp->ibqp.device);
	if (!nesvnic)
		return -EINVAL;

	nesdev = nesvnic->nesdev;
2717
	nesibdev = nesvnic->nesibdev;
2718 2719

	nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
E
Eric Dumazet 已提交
2720
			netdev_refcnt_read(nesvnic->netdev));
2721 2722 2723 2724 2725 2726 2727 2728

	if (nesqp->active_conn) {

		/* indicate this connection is NOT active */
		nesqp->active_conn = 0;
	} else {
		/* Need to free the Last Streaming Mode Message */
		if (nesqp->ietf_frame) {
2729 2730
			if (nesqp->lsmm_mr)
				nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763
			pci_free_consistent(nesdev->pcidev,
					nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
					nesqp->ietf_frame, nesqp->ietf_frame_pbase);
		}
	}

	/* close the CM node down if it is still active */
	if (nesqp->cm_node) {
		nes_debug(NES_DBG_CM, "Call close API\n");

		g_cm_core->api->close(g_cm_core, nesqp->cm_node);
	}

	return ret;
}


/**
 * nes_accept
 */
int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
{
	u64 u64temp;
	struct ib_qp *ibqp;
	struct nes_qp *nesqp;
	struct nes_vnic *nesvnic;
	struct nes_device *nesdev;
	struct nes_cm_node *cm_node;
	struct nes_adapter *adapter;
	struct ib_qp_attr attr;
	struct iw_cm_event cm_event;
	struct nes_hw_qp_wqe *wqe;
	struct nes_v4_quad nes_quad;
2764
	u32 crc_value;
2765
	int ret;
2766
	int passive_state;
2767 2768 2769 2770
	struct nes_ib_device *nesibdev;
	struct ib_mr *ibmr = NULL;
	struct ib_phys_buf ibphysbuf;
	struct nes_pd *nespd;
2771
	u64 tagged_offset;
2772

2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
	ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
	if (!ibqp)
		return -EINVAL;

	/* get all our handles */
	nesqp = to_nesqp(ibqp);
	nesvnic = to_nesvnic(nesqp->ibqp.device);
	nesdev = nesvnic->nesdev;
	adapter = nesdev->nesadapter;

	cm_node = (struct nes_cm_node *)cm_id->provider_data;
2784 2785 2786
	nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
		"%s\n", cm_node, nesvnic, nesvnic->netdev,
		nesvnic->netdev->name);
2787

F
Faisal Latif 已提交
2788 2789 2790 2791 2792 2793 2794
	if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
		if (cm_node->loopbackpartner)
			rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
		rem_ref_cm_node(cm_node->cm_core, cm_node);
		return -EINVAL;
	}

2795 2796 2797 2798 2799 2800
	passive_state = atomic_add_return(1, &cm_node->passive_state);
	if (passive_state == NES_SEND_RESET_EVENT) {
		rem_ref_cm_node(cm_node->cm_core, cm_node);
		return -ECONNRESET;
	}

2801 2802
	/* associate the node with the QP */
	nesqp->cm_node = (void *)cm_node;
2803
	cm_node->nesqp = nesqp;
2804

2805 2806
	nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
		nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
2807 2808 2809
	atomic_inc(&cm_accepts);

	nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
E
Eric Dumazet 已提交
2810
			netdev_refcnt_read(nesvnic->netdev));
2811

2812 2813 2814 2815
	/* allocate the ietf frame and space for private data */
	nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
		sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
		&nesqp->ietf_frame_pbase);
2816

2817 2818 2819 2820 2821
	if (!nesqp->ietf_frame) {
		nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
			"data\n");
		return -ENOMEM;
	}
2822 2823


2824 2825 2826
	/* setup the MPA frame */
	nesqp->private_data_len = conn_param->private_data_len;
	memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
2827

2828 2829
	memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
			conn_param->private_data_len);
2830

2831 2832 2833 2834
	nesqp->ietf_frame->priv_data_len =
		cpu_to_be16(conn_param->private_data_len);
	nesqp->ietf_frame->rev = mpa_version;
	nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
2835

2836 2837 2838 2839 2840 2841
	/* setup our first outgoing iWarp send WQE (the IETF frame response) */
	wqe = &nesqp->hwqp.sq_vbase[0];

	if (cm_id->remote_addr.sin_addr.s_addr !=
			cm_id->local_addr.sin_addr.s_addr) {
		u64temp = (unsigned long)nesqp;
2842 2843 2844 2845 2846
		nesibdev = nesvnic->nesibdev;
		nespd = nesqp->nespd;
		ibphysbuf.addr = nesqp->ietf_frame_pbase;
		ibphysbuf.size = conn_param->private_data_len +
					sizeof(struct ietf_mpa_frame);
2847
		tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
2848 2849 2850
		ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
						&ibphysbuf, 1,
						IB_ACCESS_LOCAL_WRITE,
2851
						&tagged_offset);
2852 2853 2854 2855
		if (!ibmr) {
			nes_debug(NES_DBG_CM, "Unable to register memory region"
					"for lSMM for cm_node = %p \n",
					cm_node);
2856 2857 2858
			pci_free_consistent(nesdev->pcidev,
				nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
				nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2859 2860 2861 2862 2863 2864 2865
			return -ENOMEM;
		}

		ibmr->pd = &nespd->ibpd;
		ibmr->device = nespd->ibpd.device;
		nesqp->lsmm_mr = ibmr;

2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
		u64temp |= NES_SW_CONTEXT_ALIGN>>1;
		set_wqe_64bit_value(wqe->wqe_words,
			NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
			u64temp);
		wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
			cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
			NES_IWARP_SQ_WQE_WRPDU);
		wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
			cpu_to_le32(conn_param->private_data_len +
			sizeof(struct ietf_mpa_frame));
2876 2877
		set_wqe_64bit_value(wqe->wqe_words,
					NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
2878
					(u64)(unsigned long)nesqp->ietf_frame);
2879 2880 2881
		wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
			cpu_to_le32(conn_param->private_data_len +
			sizeof(struct ietf_mpa_frame));
2882
		wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
F
Faisal Latif 已提交
2883 2884 2885 2886
		if (nesqp->sq_kmapped) {
			nesqp->sq_kmapped = 0;
			kunmap(nesqp->page);
		}
2887 2888 2889 2890 2891 2892

		nesqp->nesqp_context->ird_ord_sizes |=
			cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
			NES_QPCONTEXT_ORDIRD_WRPDU);
	} else {
		nesqp->nesqp_context->ird_ord_sizes |=
2893
			cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
2894 2895
	}
	nesqp->skip_lsmm = 1;
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905


	/* Cache the cm_id in the qp */
	nesqp->cm_id = cm_id;
	cm_node->cm_id = cm_id;

	/*  nesqp->cm_node = (void *)cm_id->provider_data; */
	cm_id->provider_data = nesqp;
	nesqp->active_conn   = 0;

2906 2907 2908 2909
	if (cm_node->state == NES_CM_STATE_TSA)
		nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
			cm_node);

2910 2911
	nes_cm_init_tsa_conn(nesqp, cm_node);

2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922
	nesqp->nesqp_context->tcpPorts[0] =
		cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
	nesqp->nesqp_context->tcpPorts[1] =
		cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));

	if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
		nesqp->nesqp_context->ip0 =
			cpu_to_le32(ntohl(nesvnic->local_ipaddr));
	else
		nesqp->nesqp_context->ip0 =
			cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
2923 2924

	nesqp->nesqp_context->misc2 |= cpu_to_le32(
2925 2926
			(u32)PCI_FUNC(nesdev->pcidev->devfn) <<
			NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
2927

2928 2929 2930
	nesqp->nesqp_context->arp_index_vlan |=
		cpu_to_le32(nes_arp_table(nesdev,
			le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
2931 2932 2933
			NES_ARP_RESOLVE) << 16);

	nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
2934
		jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
2935 2936 2937 2938

	nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);

	nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
2939 2940 2941
		((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
	nesqp->nesqp_context->ird_ord_sizes |=
		cpu_to_le32((u32)conn_param->ord);
2942 2943

	memset(&nes_quad, 0, sizeof(nes_quad));
2944 2945 2946 2947 2948 2949 2950 2951
	nes_quad.DstIpAdrIndex =
		cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
	if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
		nes_quad.SrcIpadr = nesvnic->local_ipaddr;
	else
		nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
	nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
	nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
2952 2953

	/* Produce hash key */
2954 2955
	crc_value = get_crc_value(&nes_quad);
	nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
2956
	nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
2957
		nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
2958 2959 2960 2961 2962 2963

	nesqp->hte_index &= adapter->hte_index_mask;
	nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);

	cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);

2964 2965 2966
	nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
			"0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
			"private data length=%zu.\n", nesqp->hwqp.qp_id,
2967 2968 2969 2970 2971 2972
			ntohl(cm_id->remote_addr.sin_addr.s_addr),
			ntohs(cm_id->remote_addr.sin_port),
			ntohl(cm_id->local_addr.sin_addr.s_addr),
			ntohs(cm_id->local_addr.sin_port),
			le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
			le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2973 2974
			conn_param->private_data_len +
			sizeof(struct ietf_mpa_frame));
2975 2976


C
Coly Li 已提交
2977
	/* notify OF layer that accept event was successful */
2978
	cm_id->add_ref(cm_id);
2979
	nes_add_ref(&nesqp->ibqp);
2980 2981

	cm_event.event = IW_CM_EVENT_ESTABLISHED;
2982
	cm_event.status = 0;
2983 2984 2985 2986 2987 2988
	cm_event.provider_data = (void *)nesqp;
	cm_event.local_addr = cm_id->local_addr;
	cm_event.remote_addr = cm_id->remote_addr;
	cm_event.private_data = NULL;
	cm_event.private_data_len = 0;
	ret = cm_id->event_handler(cm_id, &cm_event);
2989 2990
	attr.qp_state = IB_QPS_RTS;
	nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2991
	if (cm_node->loopbackpartner) {
2992 2993
		cm_node->loopbackpartner->mpa_frame_size =
			nesqp->private_data_len;
2994
		/* copy entire MPA frame to our cm_node's frame */
2995 2996
		memcpy(cm_node->loopbackpartner->mpa_frame_buf,
			nesqp->ietf_frame->priv_data, nesqp->private_data_len);
2997 2998 2999
		create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
	}
	if (ret)
3000 3001
		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
			"ret=%d\n", __func__, __LINE__, ret);
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012

	return 0;
}


/**
 * nes_reject
 */
int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
{
	struct nes_cm_node *cm_node;
3013 3014
	struct nes_cm_node *loopback;

3015 3016 3017 3018
	struct nes_cm_core *cm_core;

	atomic_inc(&cm_rejects);
	cm_node = (struct nes_cm_node *) cm_id->provider_data;
3019
	loopback = cm_node->loopbackpartner;
3020
	cm_core = cm_node->cm_core;
3021
	cm_node->cm_id = cm_id;
3022 3023
	cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;

3024 3025 3026
	if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
		return -EINVAL;

3027
	memcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
3028 3029 3030 3031 3032 3033 3034 3035 3036
	if (loopback) {
		memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
		loopback->mpa_frame.priv_data_len = pdata_len;
		loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
				pdata_len;
	} else {
		memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
		cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
	}
3037 3038 3039 3040

	cm_node->mpa_frame.rev = mpa_version;
	cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;

3041
	return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
}


/**
 * nes_connect
 * setup and launch cm connect node
 */
int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
{
	struct ib_qp *ibqp;
	struct nes_qp *nesqp;
	struct nes_vnic *nesvnic;
	struct nes_device *nesdev;
	struct nes_cm_node *cm_node;
	struct nes_cm_info cm_info;
3057
	int apbvt_set = 0;
3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071

	ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
	if (!ibqp)
		return -EINVAL;
	nesqp = to_nesqp(ibqp);
	if (!nesqp)
		return -EINVAL;
	nesvnic = to_nesvnic(nesqp->ibqp.device);
	if (!nesvnic)
		return -EINVAL;
	nesdev  = nesvnic->nesdev;
	if (!nesdev)
		return -EINVAL;

F
Faisal Latif 已提交
3072 3073 3074
	if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
		return -EINVAL;

3075 3076 3077 3078 3079 3080 3081
	nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
		"0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
		ntohl(nesvnic->local_ipaddr),
		ntohl(cm_id->remote_addr.sin_addr.s_addr),
		ntohs(cm_id->remote_addr.sin_port),
		ntohl(cm_id->local_addr.sin_addr.s_addr),
		ntohs(cm_id->local_addr.sin_port));
3082

3083
	atomic_inc(&cm_connects);
3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
	nesqp->active_conn = 1;

	/* cache the cm_id in the qp */
	nesqp->cm_id = cm_id;

	cm_id->provider_data = nesqp;

	nesqp->private_data_len = conn_param->private_data_len;
	nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
	nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
3094 3095
	nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
		conn_param->private_data_len);
3096

3097
	if (cm_id->local_addr.sin_addr.s_addr !=
3098
		cm_id->remote_addr.sin_addr.s_addr) {
3099
		nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
3100
			PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
3101 3102
		apbvt_set = 1;
	}
3103 3104

	/* set up the connection params for the node */
3105 3106 3107 3108
	cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
	cm_info.loc_port = htons(cm_id->local_addr.sin_port);
	cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
	cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
3109 3110 3111 3112 3113 3114
	cm_info.cm_id = cm_id;
	cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;

	cm_id->add_ref(cm_id);

	/* create a connect CM node connection */
3115 3116 3117
	cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
		conn_param->private_data_len, (void *)conn_param->private_data,
		&cm_info);
3118
	if (!cm_node) {
3119
		if (apbvt_set)
3120
			nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
3121 3122 3123
				PCI_FUNC(nesdev->pcidev->devfn),
				NES_MANAGE_APBVT_DEL);

3124 3125 3126 3127
		cm_id->rem_ref(cm_id);
		return -ENOMEM;
	}

3128
	cm_node->apbvt_set = apbvt_set;
3129
	nesqp->cm_node = cm_node;
3130
	cm_node->nesqp = nesqp;
3131
	nes_add_ref(&nesqp->ibqp);
3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152

	return 0;
}


/**
 * nes_create_listen
 */
int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
{
	struct nes_vnic *nesvnic;
	struct nes_cm_listener *cm_node;
	struct nes_cm_info cm_info;
	int err;

	nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
			cm_id, ntohs(cm_id->local_addr.sin_port));

	nesvnic = to_nesvnic(cm_id->device);
	if (!nesvnic)
		return -EINVAL;
3153

3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
	nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
			nesvnic, nesvnic->netdev, nesvnic->netdev->name);

	nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
			nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);

	/* setup listen params in our api call struct */
	cm_info.loc_addr = nesvnic->local_ipaddr;
	cm_info.loc_port = cm_id->local_addr.sin_port;
	cm_info.backlog = backlog;
	cm_info.cm_id = cm_id;

	cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;


	cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
	if (!cm_node) {
3171
		printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
3172
				__func__, __LINE__);
3173 3174 3175 3176 3177 3178
		return -ENOMEM;
	}

	cm_id->provider_data = cm_node;

	if (!cm_node->reused_node) {
3179 3180 3181 3182
		err = nes_manage_apbvt(nesvnic,
			ntohs(cm_id->local_addr.sin_port),
			PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
			NES_MANAGE_APBVT_ADD);
3183
		if (err) {
3184 3185
			printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
				err);
3186 3187 3188
			g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
			return err;
		}
3189
		atomic_inc(&cm_listens_created);
3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
	}

	cm_id->add_ref(cm_id);
	cm_id->provider_data = (void *)cm_node;


	return 0;
}


/**
 * nes_destroy_listen
 */
int nes_destroy_listen(struct iw_cm_id *cm_id)
{
	if (cm_id->provider_data)
		g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
	else
		nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");

	cm_id->rem_ref(cm_id);

	return 0;
}


/**
 * nes_cm_recv
 */
int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
{
3221
	int rc = 0;
3222 3223
	cm_packets_received++;
	if ((g_cm_core) && (g_cm_core->api)) {
3224
		rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
3225 3226 3227 3228 3229
	} else {
		nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
				" cm is not setup properly.\n");
	}

3230
	return rc;
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
}


/**
 * nes_cm_start
 * Start and init a cm core module
 */
int nes_cm_start(void)
{
	nes_debug(NES_DBG_CM, "\n");
	/* create the primary CM core, pass this handle to subsequent core inits */
	g_cm_core = nes_cm_alloc_core();
	if (g_cm_core) {
		return 0;
	} else {
		return -ENOMEM;
	}
}


/**
 * nes_cm_stop
 * stop and dealloc all cm core instances
 */
int nes_cm_stop(void)
{
	g_cm_core->api->destroy_cm_core(g_cm_core);
	return 0;
}


/**
 * cm_event_connected
 * handle a connected event, setup QPs and HW
 */
3266
static void cm_event_connected(struct nes_cm_event *event)
3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278
{
	u64 u64temp;
	struct nes_qp *nesqp;
	struct nes_vnic *nesvnic;
	struct nes_device *nesdev;
	struct nes_cm_node *cm_node;
	struct nes_adapter *nesadapter;
	struct ib_qp_attr attr;
	struct iw_cm_id *cm_id;
	struct iw_cm_event cm_event;
	struct nes_hw_qp_wqe *wqe;
	struct nes_v4_quad nes_quad;
3279
	u32 crc_value;
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
	int ret;

	/* get all our handles */
	cm_node = event->cm_node;
	cm_id = cm_node->cm_id;
	nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
	nesqp = (struct nes_qp *)cm_id->provider_data;
	nesvnic = to_nesvnic(nesqp->ibqp.device);
	nesdev = nesvnic->nesdev;
	nesadapter = nesdev->nesadapter;

	if (nesqp->destroyed) {
		return;
	}
	atomic_inc(&cm_connecteds);
	nes_debug(NES_DBG_CM, "QP%u attempting to connect to  0x%08X:0x%04X on"
			" local port 0x%04X. jiffies = %lu.\n",
			nesqp->hwqp.qp_id,
			ntohl(cm_id->remote_addr.sin_addr.s_addr),
			ntohs(cm_id->remote_addr.sin_port),
			ntohs(cm_id->local_addr.sin_port),
			jiffies);

	nes_cm_init_tsa_conn(nesqp, cm_node);

	/* set the QP tsa context */
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
	nesqp->nesqp_context->tcpPorts[0] =
		cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
	nesqp->nesqp_context->tcpPorts[1] =
		cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
	if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
		nesqp->nesqp_context->ip0 =
			cpu_to_le32(ntohl(nesvnic->local_ipaddr));
	else
		nesqp->nesqp_context->ip0 =
			cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
3316 3317

	nesqp->nesqp_context->misc2 |= cpu_to_le32(
3318 3319
			(u32)PCI_FUNC(nesdev->pcidev->devfn) <<
			NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
3320
	nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
3321 3322
			nes_arp_table(nesdev,
			le32_to_cpu(nesqp->nesqp_context->ip0),
3323 3324 3325 3326 3327
			NULL, NES_ARP_RESOLVE) << 16);
	nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
			jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
	nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
	nesqp->nesqp_context->ird_ord_sizes |=
3328 3329
			cpu_to_le32((u32)1 <<
			NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
3330 3331 3332 3333 3334

	/* Adjust tail for not having a LSMM */
	nesqp->hwqp.sq_tail = 1;

#if defined(NES_SEND_FIRST_WRITE)
3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349
	if (cm_node->send_write0) {
		nes_debug(NES_DBG_CM, "Sending first write.\n");
		wqe = &nesqp->hwqp.sq_vbase[0];
		u64temp = (unsigned long)nesqp;
		u64temp |= NES_SW_CONTEXT_ALIGN>>1;
		set_wqe_64bit_value(wqe->wqe_words,
				NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
		wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
			cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
		wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
		wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
		wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
		wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
		wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;

F
Faisal Latif 已提交
3350 3351 3352 3353 3354
		if (nesqp->sq_kmapped) {
			nesqp->sq_kmapped = 0;
			kunmap(nesqp->page);
		}

3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
		/* use the reserved spot on the WQ for the extra first WQE */
		nesqp->nesqp_context->ird_ord_sizes &=
			cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
						NES_QPCONTEXT_ORDIRD_WRPDU |
						NES_QPCONTEXT_ORDIRD_ALSMM));
		nesqp->skip_lsmm = 1;
		nesqp->hwqp.sq_tail = 0;
		nes_write32(nesdev->regs + NES_WQE_ALLOC,
				(1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
	}
3365 3366 3367 3368
#endif

	memset(&nes_quad, 0, sizeof(nes_quad));

3369 3370 3371 3372 3373 3374
	nes_quad.DstIpAdrIndex =
		cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
	if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
		nes_quad.SrcIpadr = nesvnic->local_ipaddr;
	else
		nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3375 3376 3377 3378
	nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
	nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;

	/* Produce hash key */
3379 3380
	crc_value = get_crc_value(&nes_quad);
	nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
	nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
			nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);

	nesqp->hte_index &= nesadapter->hte_index_mask;
	nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);

	nesqp->ietf_frame = &cm_node->mpa_frame;
	nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
	cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);

	/* notify OF layer we successfully created the requested connection */
	cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3393
	cm_event.status = 0;
3394 3395 3396 3397 3398
	cm_event.provider_data = cm_id->provider_data;
	cm_event.local_addr.sin_family = AF_INET;
	cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
	cm_event.remote_addr = cm_id->remote_addr;

3399 3400
	cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
	cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
3401 3402 3403 3404 3405 3406

	cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
	ret = cm_id->event_handler(cm_id, &cm_event);
	nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);

	if (ret)
3407 3408 3409 3410
		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
			"ret=%d\n", __func__, __LINE__, ret);
	attr.qp_state = IB_QPS_RTS;
	nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
3411

3412 3413
	nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
		"%lu\n", nesqp->hwqp.qp_id, jiffies);
3414 3415 3416 3417 3418 3419 3420 3421

	return;
}


/**
 * cm_event_connect_error
 */
3422
static void cm_event_connect_error(struct nes_cm_event *event)
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
{
	struct nes_qp *nesqp;
	struct iw_cm_id *cm_id;
	struct iw_cm_event cm_event;
	/* struct nes_cm_info cm_info; */
	int ret;

	if (!event->cm_node)
		return;

	cm_id = event->cm_node->cm_id;
	if (!cm_id) {
		return;
	}

	nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
	nesqp = cm_id->provider_data;

	if (!nesqp) {
		return;
	}

	/* notify OF layer about this connection error event */
	/* cm_id->rem_ref(cm_id); */
	nesqp->cm_id = NULL;
	cm_id->provider_data = NULL;
	cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
F
Faisal Latif 已提交
3450
	cm_event.status = -ECONNRESET;
3451 3452 3453 3454 3455 3456
	cm_event.provider_data = cm_id->provider_data;
	cm_event.local_addr = cm_id->local_addr;
	cm_event.remote_addr = cm_id->remote_addr;
	cm_event.private_data = NULL;
	cm_event.private_data_len = 0;

3457 3458 3459
	nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
		"remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
		cm_event.remote_addr.sin_addr.s_addr);
3460 3461 3462 3463

	ret = cm_id->event_handler(cm_id, &cm_event);
	nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
	if (ret)
3464 3465 3466
		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
			"ret=%d\n", __func__, __LINE__, ret);
	cm_id->rem_ref(cm_id);
3467

3468
	rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
3469 3470 3471 3472 3473 3474 3475
	return;
}


/**
 * cm_event_reset
 */
3476
static void cm_event_reset(struct nes_cm_event *event)
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493
{
	struct nes_qp *nesqp;
	struct iw_cm_id *cm_id;
	struct iw_cm_event cm_event;
	/* struct nes_cm_info cm_info; */
	int ret;

	if (!event->cm_node)
		return;

	if (!event->cm_node->cm_id)
		return;

	cm_id = event->cm_node->cm_id;

	nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
	nesqp = cm_id->provider_data;
3494 3495
	if (!nesqp)
		return;
3496 3497 3498 3499

	nesqp->cm_id = NULL;
	/* cm_id->provider_data = NULL; */
	cm_event.event = IW_CM_EVENT_DISCONNECT;
3500
	cm_event.status = -ECONNRESET;
3501 3502 3503 3504 3505 3506
	cm_event.provider_data = cm_id->provider_data;
	cm_event.local_addr = cm_id->local_addr;
	cm_event.remote_addr = cm_id->remote_addr;
	cm_event.private_data = NULL;
	cm_event.private_data_len = 0;

3507
	cm_id->add_ref(cm_id);
3508
	ret = cm_id->event_handler(cm_id, &cm_event);
3509 3510
	atomic_inc(&cm_closes);
	cm_event.event = IW_CM_EVENT_CLOSE;
3511
	cm_event.status = 0;
3512 3513 3514 3515 3516 3517 3518 3519
	cm_event.provider_data = cm_id->provider_data;
	cm_event.local_addr = cm_id->local_addr;
	cm_event.remote_addr = cm_id->remote_addr;
	cm_event.private_data = NULL;
	cm_event.private_data_len = 0;
	nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
	ret = cm_id->event_handler(cm_id, &cm_event);

3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
	nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);


	/* notify OF layer about this connection error event */
	cm_id->rem_ref(cm_id);

	return;
}


/**
 * cm_event_mpa_req
 */
3533
static void cm_event_mpa_req(struct nes_cm_event *event)
3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
{
	struct iw_cm_id   *cm_id;
	struct iw_cm_event cm_event;
	int ret;
	struct nes_cm_node *cm_node;

	cm_node = event->cm_node;
	if (!cm_node)
		return;
	cm_id = cm_node->cm_id;

	atomic_inc(&cm_connect_reqs);
	nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
			cm_node, cm_id, jiffies);

	cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3550
	cm_event.status = 0;
3551 3552 3553 3554 3555 3556
	cm_event.provider_data = (void *)cm_node;

	cm_event.local_addr.sin_family = AF_INET;
	cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
	cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);

3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594
	cm_event.remote_addr.sin_family = AF_INET;
	cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
	cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
	cm_event.private_data = cm_node->mpa_frame_buf;
	cm_event.private_data_len  = (u8) cm_node->mpa_frame_size;

	ret = cm_id->event_handler(cm_id, &cm_event);
	if (ret)
		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
				__func__, __LINE__, ret);
	return;
}


static void cm_event_mpa_reject(struct nes_cm_event *event)
{
	struct iw_cm_id   *cm_id;
	struct iw_cm_event cm_event;
	struct nes_cm_node *cm_node;
	int ret;

	cm_node = event->cm_node;
	if (!cm_node)
		return;
	cm_id = cm_node->cm_id;

	atomic_inc(&cm_connect_reqs);
	nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
			cm_node, cm_id, jiffies);

	cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
	cm_event.status = -ECONNREFUSED;
	cm_event.provider_data = cm_id->provider_data;

	cm_event.local_addr.sin_family = AF_INET;
	cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
	cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);

3595 3596 3597 3598
	cm_event.remote_addr.sin_family = AF_INET;
	cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
	cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);

3599 3600 3601 3602 3603 3604 3605
	cm_event.private_data = cm_node->mpa_frame_buf;
	cm_event.private_data_len = (u8) cm_node->mpa_frame_size;

	nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
			"remove_addr=%08x\n",
			cm_event.local_addr.sin_addr.s_addr,
			cm_event.remote_addr.sin_addr.s_addr);
3606 3607 3608

	ret = cm_id->event_handler(cm_id, &cm_event);
	if (ret)
3609
		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3610
				__func__, __LINE__, ret);
3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621

	return;
}


static void nes_cm_event_handler(struct work_struct *);

/**
 * nes_cm_post_event
 * post an event to the cm event handler
 */
3622
static int nes_cm_post_event(struct nes_cm_event *event)
3623 3624 3625 3626 3627
{
	atomic_inc(&event->cm_node->cm_core->events_posted);
	add_ref_cm_node(event->cm_node);
	event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
	INIT_WORK(&event->event_work, nes_cm_event_handler);
3628 3629
	nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
		event->cm_node, event);
3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644

	queue_work(event->cm_node->cm_core->event_wq, &event->event_work);

	nes_debug(NES_DBG_CM, "Exit\n");
	return 0;
}


/**
 * nes_cm_event_handler
 * worker function to handle cm events
 * will free instance of nes_cm_event
 */
static void nes_cm_event_handler(struct work_struct *work)
{
3645 3646
	struct nes_cm_event *event = container_of(work, struct nes_cm_event,
			event_work);
3647 3648
	struct nes_cm_core *cm_core;

3649
	if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
3650
		return;
3651

3652 3653
	cm_core = event->cm_node->cm_core;
	nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
3654
		event, event->type, atomic_read(&cm_core->events_posted));
3655 3656

	switch (event->type) {
3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669
	case NES_CM_EVENT_MPA_REQ:
		cm_event_mpa_req(event);
		nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
			event->cm_node);
		break;
	case NES_CM_EVENT_RESET:
		nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
			event->cm_node);
		cm_event_reset(event);
		break;
	case NES_CM_EVENT_CONNECTED:
		if ((!event->cm_node->cm_id) ||
			(event->cm_node->state != NES_CM_STATE_TSA))
3670
			break;
3671 3672 3673
		cm_event_connected(event);
		nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
		break;
3674 3675 3676 3677 3678 3679 3680 3681
	case NES_CM_EVENT_MPA_REJECT:
		if ((!event->cm_node->cm_id) ||
				(event->cm_node->state == NES_CM_STATE_TSA))
			break;
		cm_event_mpa_reject(event);
		nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
		break;

3682 3683 3684
	case NES_CM_EVENT_ABORTED:
		if ((!event->cm_node->cm_id) ||
			(event->cm_node->state == NES_CM_STATE_TSA))
3685
			break;
3686 3687 3688 3689 3690 3691 3692 3693 3694
		cm_event_connect_error(event);
		nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
		break;
	case NES_CM_EVENT_DROPPED_PKT:
		nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
		break;
	default:
		nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
		break;
3695 3696 3697 3698 3699 3700 3701 3702 3703
	}

	atomic_dec(&cm_core->events_posted);
	event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
	rem_ref_cm_node(cm_core, event->cm_node);
	kfree(event);

	return;
}