cm.c 114.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2009-2014 Chelsio, Inc. 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 37 38 39 40
 *
 * 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.
 */
#include <linux/module.h>
#include <linux/list.h>
#include <linux/workqueue.h>
#include <linux/skbuff.h>
#include <linux/timer.h>
#include <linux/notifier.h>
#include <linux/inetdevice.h>
#include <linux/ip.h>
#include <linux/tcp.h>
41
#include <linux/if_vlan.h>
42 43 44 45

#include <net/neighbour.h>
#include <net/netevent.h>
#include <net/route.h>
46
#include <net/tcp.h>
47 48
#include <net/ip6_route.h>
#include <net/addrconf.h>
49

S
Steve Wise 已提交
50 51
#include <rdma/ib_addr.h>

52
#include <libcxgb_cm.h>
53
#include "iw_cxgb4.h"
H
Hariprasad S 已提交
54
#include "clip_tbl.h"
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

static char *states[] = {
	"idle",
	"listen",
	"connecting",
	"mpa_wait_req",
	"mpa_req_sent",
	"mpa_req_rcvd",
	"mpa_rep_sent",
	"fpdu_mode",
	"aborting",
	"closing",
	"moribund",
	"dead",
	NULL,
};

72 73 74 75 76 77 78 79
static int nocong;
module_param(nocong, int, 0644);
MODULE_PARM_DESC(nocong, "Turn of congestion control (default=0)");

static int enable_ecn;
module_param(enable_ecn, int, 0644);
MODULE_PARM_DESC(enable_ecn, "Enable ECN (default=0/disabled)");

S
Steve Wise 已提交
80
static int dack_mode = 1;
81
module_param(dack_mode, int, 0644);
S
Steve Wise 已提交
82
MODULE_PARM_DESC(dack_mode, "Delayed ack mode (default=1)");
83

84
uint c4iw_max_read_depth = 32;
85
module_param(c4iw_max_read_depth, int, 0644);
86 87
MODULE_PARM_DESC(c4iw_max_read_depth,
		 "Per-connection max ORD/IRD (default=32)");
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static int enable_tcp_timestamps;
module_param(enable_tcp_timestamps, int, 0644);
MODULE_PARM_DESC(enable_tcp_timestamps, "Enable tcp timestamps (default=0)");

static int enable_tcp_sack;
module_param(enable_tcp_sack, int, 0644);
MODULE_PARM_DESC(enable_tcp_sack, "Enable tcp SACK (default=0)");

static int enable_tcp_window_scaling = 1;
module_param(enable_tcp_window_scaling, int, 0644);
MODULE_PARM_DESC(enable_tcp_window_scaling,
		 "Enable tcp window scaling (default=1)");

int c4iw_debug;
module_param(c4iw_debug, int, 0644);
J
Joe Perches 已提交
104
MODULE_PARM_DESC(c4iw_debug, "obsolete");
105

106
static int peer2peer = 1;
107
module_param(peer2peer, int, 0644);
108
MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=1)");
109 110 111 112 113 114 115 116 117 118 119

static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
module_param(p2p_type, int, 0644);
MODULE_PARM_DESC(p2p_type, "RDMAP opcode to use for the RTR message: "
			   "1=RDMA_READ 0=RDMA_WRITE (default 1)");

static int ep_timeout_secs = 60;
module_param(ep_timeout_secs, int, 0644);
MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
				   "in seconds (default=60)");

120
static int mpa_rev = 2;
121 122
module_param(mpa_rev, int, 0644);
MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
123
		"1 is RFC5044 spec compliant, 2 is IETF MPA Peer Connect Draft"
124
		" compliant (default=2)");
125 126 127 128 129 130 131 132 133 134 135 136 137

static int markers_enabled;
module_param(markers_enabled, int, 0644);
MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");

static int crc_enabled = 1;
module_param(crc_enabled, int, 0644);
MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");

static int rcv_win = 256 * 1024;
module_param(rcv_win, int, 0644);
MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256KB)");

138
static int snd_win = 128 * 1024;
139
module_param(snd_win, int, 0644);
140
MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=128KB)");
141 142 143 144 145 146 147 148

static struct workqueue_struct *workq;

static struct sk_buff_head rxq;

static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
static void ep_timeout(unsigned long arg);
static void connect_reply_upcall(struct c4iw_ep *ep, int status);
149
static int sched(struct c4iw_dev *dev, struct sk_buff *skb);
150

151 152 153
static LIST_HEAD(timeout_list);
static spinlock_t timeout_lock;

154 155 156 157 158 159 160 161 162 163 164 165 166
static void deref_cm_id(struct c4iw_ep_common *epc)
{
	epc->cm_id->rem_ref(epc->cm_id);
	epc->cm_id = NULL;
	set_bit(CM_ID_DEREFED, &epc->history);
}

static void ref_cm_id(struct c4iw_ep_common *epc)
{
	set_bit(CM_ID_REFED, &epc->history);
	epc->cm_id->add_ref(epc->cm_id);
}

167 168 169 170
static void deref_qp(struct c4iw_ep *ep)
{
	c4iw_qp_rem_ref(&ep->com.qp->ibqp);
	clear_bit(QP_REFERENCED, &ep->com.flags);
171
	set_bit(QP_DEREFED, &ep->com.history);
172 173 174 175 176
}

static void ref_qp(struct c4iw_ep *ep)
{
	set_bit(QP_REFERENCED, &ep->com.flags);
177
	set_bit(QP_REFED, &ep->com.history);
178 179 180
	c4iw_qp_add_ref(&ep->com.qp->ibqp);
}

181 182
static void start_ep_timer(struct c4iw_ep *ep)
{
J
Joe Perches 已提交
183
	pr_debug("%s ep %p\n", __func__, ep);
184
	if (timer_pending(&ep->timer)) {
185 186 187 188 189 190
		pr_err("%s timer already started! ep %p\n",
		       __func__, ep);
		return;
	}
	clear_bit(TIMEOUT, &ep->com.flags);
	c4iw_get_ep(&ep->com);
191 192 193 194 195 196
	ep->timer.expires = jiffies + ep_timeout_secs * HZ;
	ep->timer.data = (unsigned long)ep;
	ep->timer.function = ep_timeout;
	add_timer(&ep->timer);
}

S
Steve Wise 已提交
197
static int stop_ep_timer(struct c4iw_ep *ep)
198
{
J
Joe Perches 已提交
199
	pr_debug("%s ep %p stopping\n", __func__, ep);
200
	del_timer_sync(&ep->timer);
S
Steve Wise 已提交
201
	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
202
		c4iw_put_ep(&ep->com);
S
Steve Wise 已提交
203 204 205
		return 0;
	}
	return 1;
206 207 208 209 210 211 212 213 214
}

static int c4iw_l2t_send(struct c4iw_rdev *rdev, struct sk_buff *skb,
		  struct l2t_entry *l2e)
{
	int	error = 0;

	if (c4iw_fatal_error(rdev)) {
		kfree_skb(skb);
J
Joe Perches 已提交
215
		pr_debug("%s - device in error state - dropping\n", __func__);
216 217 218 219 220
		return -EIO;
	}
	error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
	if (error < 0)
		kfree_skb(skb);
221 222
	else if (error == NET_XMIT_DROP)
		return -ENOMEM;
223
	return error < 0 ? error : 0;
224 225 226 227 228 229 230 231
}

int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
{
	int	error = 0;

	if (c4iw_fatal_error(rdev)) {
		kfree_skb(skb);
J
Joe Perches 已提交
232
		pr_debug("%s - device in error state - dropping\n", __func__);
233 234 235 236 237
		return -EIO;
	}
	error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
	if (error < 0)
		kfree_skb(skb);
238
	return error < 0 ? error : 0;
239 240 241 242
}

static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
{
243
	u32 len = roundup(sizeof(struct cpl_tid_release), 16);
244

245
	skb = get_skb(skb, len, GFP_KERNEL);
246 247
	if (!skb)
		return;
248 249

	cxgb_mk_tid_release(skb, len, hwtid, 0);
250 251 252 253 254 255
	c4iw_ofld_send(rdev, skb);
	return;
}

static void set_emss(struct c4iw_ep *ep, u16 opt)
{
256
	ep->emss = ep->com.dev->rdev.lldi.mtus[TCPOPT_MSS_G(opt)] -
257 258 259
		   ((AF_INET == ep->com.remote_addr.ss_family) ?
		    sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
		   sizeof(struct tcphdr);
260
	ep->mss = ep->emss;
261
	if (TCPOPT_TSTAMP_G(opt))
262
		ep->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
263 264
	if (ep->emss < 128)
		ep->emss = 128;
265
	if (ep->emss & 7)
J
Joe Perches 已提交
266 267 268 269
		pr_debug("Warning: misaligned mtu idx %u mss %u emss=%u\n",
			 TCPOPT_MSS_G(opt), ep->mss, ep->emss);
	pr_debug("%s mss_idx %u mss %u emss=%u\n", __func__, TCPOPT_MSS_G(opt),
		 ep->mss, ep->emss);
270 271 272 273 274 275
}

static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc)
{
	enum c4iw_ep_state state;

276
	mutex_lock(&epc->mutex);
277
	state = epc->state;
278
	mutex_unlock(&epc->mutex);
279 280 281 282 283 284 285 286 287 288
	return state;
}

static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
{
	epc->state = new;
}

static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
{
289
	mutex_lock(&epc->mutex);
J
Joe Perches 已提交
290
	pr_debug("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
291
	__state_set(epc, new);
292
	mutex_unlock(&epc->mutex);
293 294 295
	return;
}

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
static int alloc_ep_skb_list(struct sk_buff_head *ep_skb_list, int size)
{
	struct sk_buff *skb;
	unsigned int i;
	size_t len;

	len = roundup(sizeof(union cpl_wr_size), 16);
	for (i = 0; i < size; i++) {
		skb = alloc_skb(len, GFP_KERNEL);
		if (!skb)
			goto fail;
		skb_queue_tail(ep_skb_list, skb);
	}
	return 0;
fail:
	skb_queue_purge(ep_skb_list);
	return -ENOMEM;
}

315 316 317 318 319 320 321
static void *alloc_ep(int size, gfp_t gfp)
{
	struct c4iw_ep_common *epc;

	epc = kzalloc(size, gfp);
	if (epc) {
		kref_init(&epc->kref);
322
		mutex_init(&epc->mutex);
323
		c4iw_init_wr_wait(&epc->wr_wait);
324
	}
J
Joe Perches 已提交
325
	pr_debug("%s alloc ep %p\n", __func__, epc);
326 327 328
	return epc;
}

329 330 331 332 333 334
static void remove_ep_tid(struct c4iw_ep *ep)
{
	unsigned long flags;

	spin_lock_irqsave(&ep->com.dev->lock, flags);
	_remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid, 0);
335 336
	if (idr_is_empty(&ep->com.dev->hwtid_idr))
		wake_up(&ep->com.dev->wait);
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
	spin_unlock_irqrestore(&ep->com.dev->lock, flags);
}

static void insert_ep_tid(struct c4iw_ep *ep)
{
	unsigned long flags;

	spin_lock_irqsave(&ep->com.dev->lock, flags);
	_insert_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep, ep->hwtid, 0);
	spin_unlock_irqrestore(&ep->com.dev->lock, flags);
}

/*
 * Atomically lookup the ep ptr given the tid and grab a reference on the ep.
 */
static struct c4iw_ep *get_ep_from_tid(struct c4iw_dev *dev, unsigned int tid)
{
	struct c4iw_ep *ep;
	unsigned long flags;

	spin_lock_irqsave(&dev->lock, flags);
	ep = idr_find(&dev->hwtid_idr, tid);
	if (ep)
		c4iw_get_ep(&ep->com);
	spin_unlock_irqrestore(&dev->lock, flags);
	return ep;
}

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/*
 * Atomically lookup the ep ptr given the stid and grab a reference on the ep.
 */
static struct c4iw_listen_ep *get_ep_from_stid(struct c4iw_dev *dev,
					       unsigned int stid)
{
	struct c4iw_listen_ep *ep;
	unsigned long flags;

	spin_lock_irqsave(&dev->lock, flags);
	ep = idr_find(&dev->stid_idr, stid);
	if (ep)
		c4iw_get_ep(&ep->com);
	spin_unlock_irqrestore(&dev->lock, flags);
	return ep;
}

382 383 384 385 386
void _c4iw_free_ep(struct kref *kref)
{
	struct c4iw_ep *ep;

	ep = container_of(kref, struct c4iw_ep, com.kref);
J
Joe Perches 已提交
387
	pr_debug("%s ep %p state %s\n", __func__, ep, states[ep->com.state]);
388 389
	if (test_bit(QP_REFERENCED, &ep->com.flags))
		deref_qp(ep);
390
	if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
H
Hariprasad S 已提交
391 392 393
		if (ep->com.remote_addr.ss_family == AF_INET6) {
			struct sockaddr_in6 *sin6 =
					(struct sockaddr_in6 *)
394
					&ep->com.local_addr;
H
Hariprasad S 已提交
395 396 397 398 399 400

			cxgb4_clip_release(
					ep->com.dev->rdev.lldi.ports[0],
					(const u32 *)&sin6->sin6_addr.s6_addr,
					1);
		}
401 402 403
		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
		dst_release(ep->dst);
		cxgb4_l2t_release(ep->l2t);
404 405
		if (ep->mpa_skb)
			kfree_skb(ep->mpa_skb);
406
	}
407 408
	if (!skb_queue_empty(&ep->com.ep_skb_list))
		skb_queue_purge(&ep->com.ep_skb_list);
409 410 411 412 413 414
	kfree(ep);
}

static void release_ep_resources(struct c4iw_ep *ep)
{
	set_bit(RELEASE_RESOURCES, &ep->com.flags);
415 416 417 418 419 420 421 422 423

	/*
	 * If we have a hwtid, then remove it from the idr table
	 * so lookups will no longer find this endpoint.  Otherwise
	 * we have a race where one thread finds the ep ptr just
	 * before the other thread is freeing the ep memory.
	 */
	if (ep->hwtid != -1)
		remove_ep_tid(ep);
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	c4iw_put_ep(&ep->com);
}

static int status2errno(int status)
{
	switch (status) {
	case CPL_ERR_NONE:
		return 0;
	case CPL_ERR_CONN_RESET:
		return -ECONNRESET;
	case CPL_ERR_ARP_MISS:
		return -EHOSTUNREACH;
	case CPL_ERR_CONN_TIMEDOUT:
		return -ETIMEDOUT;
	case CPL_ERR_TCAM_FULL:
		return -ENOMEM;
	case CPL_ERR_CONN_EXIST:
		return -EADDRINUSE;
	default:
		return -EIO;
	}
}

/*
 * Try and reuse skbs already allocated...
 */
static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
{
	if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
		skb_trim(skb, 0);
		skb_get(skb);
		skb_reset_transport_header(skb);
	} else {
		skb = alloc_skb(len, gfp);
	}
459
	t4_set_arp_err_handler(skb, NULL, NULL);
460 461 462
	return skb;
}

463 464
static struct net_device *get_real_dev(struct net_device *egress_dev)
{
S
Steve Wise 已提交
465
	return rdma_vlan_dev_real_dev(egress_dev) ? : egress_dev;
466 467
}

468 469
static void arp_failure_discard(void *handle, struct sk_buff *skb)
{
470
	pr_err("ARP failure\n");
471 472 473
	kfree_skb(skb);
}

474 475 476 477 478
static void mpa_start_arp_failure(void *handle, struct sk_buff *skb)
{
	pr_err("ARP failure during MPA Negotiation - Closing Connection\n");
}

479
enum {
480
	NUM_FAKE_CPLS = 2,
481
	FAKE_CPL_PUT_EP_SAFE = NUM_CPL_CMDS + 0,
482
	FAKE_CPL_PASS_PUT_EP_SAFE = NUM_CPL_CMDS + 1,
483 484 485 486 487 488 489 490
};

static int _put_ep_safe(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;

	ep = *((struct c4iw_ep **)(skb->cb + 2 * sizeof(void *)));
	release_ep_resources(ep);
491
	kfree_skb(skb);
492 493 494
	return 0;
}

495 496 497 498 499 500 501
static int _put_pass_ep_safe(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;

	ep = *((struct c4iw_ep **)(skb->cb + 2 * sizeof(void *)));
	c4iw_put_ep(&ep->parent_ep->com);
	release_ep_resources(ep);
502
	kfree_skb(skb);
503 504 505
	return 0;
}

506 507 508 509 510 511
/*
 * Fake up a special CPL opcode and call sched() so process_work() will call
 * _put_ep_safe() in a safe context to free the ep resources.  This is needed
 * because ARP error handlers are called in an ATOMIC context, and
 * _c4iw_free_ep() needs to block.
 */
512 513
static void queue_arp_failure_cpl(struct c4iw_ep *ep, struct sk_buff *skb,
				  int cpl)
514 515 516 517
{
	struct cpl_act_establish *rpl = cplhdr(skb);

	/* Set our special ARP_FAILURE opcode */
518
	rpl->ot.opcode = cpl;
519 520 521 522 523 524 525 526 527 528 529 530 531 532

	/*
	 * Save ep in the skb->cb area, after where sched() will save the dev
	 * ptr.
	 */
	*((struct c4iw_ep **)(skb->cb + 2 * sizeof(void *))) = ep;
	sched(ep->com.dev, skb);
}

/* Handle an ARP failure for an accept */
static void pass_accept_rpl_arp_failure(void *handle, struct sk_buff *skb)
{
	struct c4iw_ep *ep = handle;

533
	pr_err("ARP failure during accept - tid %u - dropping connection\n",
534 535 536
	       ep->hwtid);

	__state_set(&ep->com, DEAD);
537
	queue_arp_failure_cpl(ep, skb, FAKE_CPL_PASS_PUT_EP_SAFE);
538 539
}

540 541 542 543 544
/*
 * Handle an ARP failure for an active open.
 */
static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
{
545 546
	struct c4iw_ep *ep = handle;

547
	pr_err("ARP failure during connect\n");
548
	connect_reply_upcall(ep, -EHOSTUNREACH);
549
	__state_set(&ep->com, DEAD);
H
Hariprasad S 已提交
550 551
	if (ep->com.remote_addr.ss_family == AF_INET6) {
		struct sockaddr_in6 *sin6 =
552
			(struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
553 554 555
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
556 557
	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
	cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
558
	queue_arp_failure_cpl(ep, skb, FAKE_CPL_PUT_EP_SAFE);
559 560 561 562 563 564 565 566
}

/*
 * Handle an ARP failure for a CPL_ABORT_REQ.  Change it into a no RST variant
 * and send it along.
 */
static void abort_arp_failure(void *handle, struct sk_buff *skb)
{
567 568 569
	int ret;
	struct c4iw_ep *ep = handle;
	struct c4iw_rdev *rdev = &ep->com.dev->rdev;
570 571
	struct cpl_abort_req *req = cplhdr(skb);

J
Joe Perches 已提交
572
	pr_debug("%s rdev %p\n", __func__, rdev);
573
	req->cmd = CPL_ABORT_NO_RST;
574
	skb_get(skb);
575 576 577 578
	ret = c4iw_ofld_send(rdev, skb);
	if (ret) {
		__state_set(&ep->com, DEAD);
		queue_arp_failure_cpl(ep, skb, FAKE_CPL_PUT_EP_SAFE);
579 580
	} else
		kfree_skb(skb);
581 582
}

583
static int send_flowc(struct c4iw_ep *ep)
584 585
{
	struct fw_flowc_wr *flowc;
586
	struct sk_buff *skb = skb_dequeue(&ep->com.ep_skb_list);
587
	int i;
H
Hariprasad S 已提交
588 589 590
	u16 vlan = ep->l2t->vlan;
	int nparams;

591 592 593
	if (WARN_ON(!skb))
		return -ENOMEM;

H
Hariprasad S 已提交
594 595 596 597
	if (vlan == CPL_L2T_VLAN_NONE)
		nparams = 8;
	else
		nparams = 9;
598

599
	flowc = (struct fw_flowc_wr *)__skb_put(skb, FLOWC_LEN);
600

601
	flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
H
Hariprasad S 已提交
602
					   FW_FLOWC_WR_NPARAMS_V(nparams));
603
	flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(DIV_ROUND_UP(FLOWC_LEN,
604
					  16)) | FW_WR_FLOWID_V(ep->hwtid));
605 606

	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
607
	flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
608
					    (ep->com.dev->rdev.lldi.pf));
609 610 611 612 613 614 615 616 617 618 619
	flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
	flowc->mnemval[1].val = cpu_to_be32(ep->tx_chan);
	flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
	flowc->mnemval[2].val = cpu_to_be32(ep->tx_chan);
	flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
	flowc->mnemval[3].val = cpu_to_be32(ep->rss_qid);
	flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
	flowc->mnemval[4].val = cpu_to_be32(ep->snd_seq);
	flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
	flowc->mnemval[5].val = cpu_to_be32(ep->rcv_seq);
	flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
620
	flowc->mnemval[6].val = cpu_to_be32(ep->snd_win);
621 622
	flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
	flowc->mnemval[7].val = cpu_to_be32(ep->emss);
H
Hariprasad S 已提交
623 624 625 626 627 628 629 630 631 632 633
	if (nparams == 9) {
		u16 pri;

		pri = (vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
		flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
		flowc->mnemval[8].val = cpu_to_be32(pri);
	} else {
		/* Pad WR to 16 byte boundary */
		flowc->mnemval[8].mnemonic = 0;
		flowc->mnemval[8].val = 0;
	}
634 635 636 637 638 639 640
	for (i = 0; i < 9; i++) {
		flowc->mnemval[i].r4[0] = 0;
		flowc->mnemval[i].r4[1] = 0;
		flowc->mnemval[i].r4[2] = 0;
	}

	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
641
	return c4iw_ofld_send(&ep->com.dev->rdev, skb);
642 643
}

644
static int send_halfclose(struct c4iw_ep *ep)
645
{
646
	struct sk_buff *skb = skb_dequeue(&ep->com.ep_skb_list);
647
	u32 wrlen = roundup(sizeof(struct cpl_close_con_req), 16);
648

J
Joe Perches 已提交
649
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
650
	if (WARN_ON(!skb))
651
		return -ENOMEM;
652

653 654 655
	cxgb_mk_close_con_req(skb, wrlen, ep->hwtid, ep->txq_idx,
			      NULL, arp_failure_discard);

656 657 658
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
}

659
static int send_abort(struct c4iw_ep *ep)
660
{
661
	u32 wrlen = roundup(sizeof(struct cpl_abort_req), 16);
662
	struct sk_buff *req_skb = skb_dequeue(&ep->com.ep_skb_list);
663

J
Joe Perches 已提交
664
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
665
	if (WARN_ON(!req_skb))
666
		return -ENOMEM;
667

668 669 670
	cxgb_mk_abort_req(req_skb, wrlen, ep->hwtid, ep->txq_idx,
			  ep, abort_arp_failure);

671
	return c4iw_l2t_send(&ep->com.dev->rdev, req_skb, ep->l2t);
672 673 674 675
}

static int send_connect(struct c4iw_ep *ep)
{
676 677 678 679 680 681
	struct cpl_act_open_req *req = NULL;
	struct cpl_t5_act_open_req *t5req = NULL;
	struct cpl_t6_act_open_req *t6req = NULL;
	struct cpl_act_open_req6 *req6 = NULL;
	struct cpl_t5_act_open_req6 *t5req6 = NULL;
	struct cpl_t6_act_open_req6 *t6req6 = NULL;
682 683 684 685
	struct sk_buff *skb;
	u64 opt0;
	u32 opt2;
	unsigned int mtu_idx;
686
	u32 wscale;
687
	int win, sizev4, sizev6, wrlen;
688
	struct sockaddr_in *la = (struct sockaddr_in *)
689
				 &ep->com.local_addr;
690
	struct sockaddr_in *ra = (struct sockaddr_in *)
691
				 &ep->com.remote_addr;
692
	struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)
693
				   &ep->com.local_addr;
694
	struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)
695
				   &ep->com.remote_addr;
H
Hariprasad S 已提交
696
	int ret;
697 698
	enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
	u32 isn = (prandom_u32() & ~7UL) - 1;
699 700 701 702
	struct net_device *netdev;
	u64 params;

	netdev = ep->com.dev->rdev.lldi.ports[0];
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

	switch (CHELSIO_CHIP_VERSION(adapter_type)) {
	case CHELSIO_T4:
		sizev4 = sizeof(struct cpl_act_open_req);
		sizev6 = sizeof(struct cpl_act_open_req6);
		break;
	case CHELSIO_T5:
		sizev4 = sizeof(struct cpl_t5_act_open_req);
		sizev6 = sizeof(struct cpl_t5_act_open_req6);
		break;
	case CHELSIO_T6:
		sizev4 = sizeof(struct cpl_t6_act_open_req);
		sizev6 = sizeof(struct cpl_t6_act_open_req6);
		break;
	default:
		pr_err("T%d Chip is not supported\n",
		       CHELSIO_CHIP_VERSION(adapter_type));
		return -EINVAL;
	}
722 723 724 725

	wrlen = (ep->com.remote_addr.ss_family == AF_INET) ?
			roundup(sizev4, 16) :
			roundup(sizev6, 16);
726

J
Joe Perches 已提交
727
	pr_debug("%s ep %p atid %u\n", __func__, ep, ep->atid);
728 729 730

	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
731
		pr_err("%s - failed to alloc skb\n", __func__);
732 733
		return -ENOMEM;
	}
S
Steve Wise 已提交
734
	set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
735

736 737 738
	cxgb_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
		      enable_tcp_timestamps,
		      (ep->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
739
	wscale = cxgb_compute_wscale(rcv_win);
740 741 742 743 744 745

	/*
	 * Specify the largest window that will fit in opt0. The
	 * remainder will be specified in the rx_data_ack.
	 */
	win = ep->rcv_win >> 10;
746 747
	if (win > RCV_BUFSIZ_M)
		win = RCV_BUFSIZ_M;
748

749
	opt0 = (nocong ? NO_CONG_F : 0) |
750
	       KEEP_ALIVE_F |
751
	       DELACK_F |
752 753 754 755 756
	       WND_SCALE_V(wscale) |
	       MSS_IDX_V(mtu_idx) |
	       L2T_IDX_V(ep->l2t->idx) |
	       TX_CHAN_V(ep->tx_chan) |
	       SMAC_SEL_V(ep->smac_idx) |
H
Hariprasad S 已提交
757
	       DSCP_V(ep->tos >> 2) |
758 759 760
	       ULP_MODE_V(ULP_MODE_TCPDDP) |
	       RCV_BUFSIZ_V(win);
	opt2 = RX_CHANNEL_V(0) |
761
	       CCTRL_ECN_V(enable_ecn) |
762
	       RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
763
	if (enable_tcp_timestamps)
764
		opt2 |= TSTAMPS_EN_F;
765
	if (enable_tcp_sack)
766
		opt2 |= SACK_EN_F;
767
	if (wscale && enable_tcp_window_scaling)
768
		opt2 |= WND_SCALE_EN_F;
769 770 771 772
	if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
		if (peer2peer)
			isn += 4;

773
		opt2 |= T5_OPT_2_VALID_F;
774
		opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
775
		opt2 |= T5_ISS_F;
776
	}
H
Hariprasad S 已提交
777

778 779
	params = cxgb4_select_ntuple(netdev, ep->l2t);

H
Hariprasad S 已提交
780 781 782 783
	if (ep->com.remote_addr.ss_family == AF_INET6)
		cxgb4_clip_get(ep->com.dev->rdev.lldi.ports[0],
			       (const u32 *)&la6->sin6_addr.s6_addr, 1);

784
	t4_set_arp_err_handler(skb, ep, act_open_req_arp_failure);
785

786 787 788 789
	if (ep->com.remote_addr.ss_family == AF_INET) {
		switch (CHELSIO_CHIP_VERSION(adapter_type)) {
		case CHELSIO_T4:
			req = (struct cpl_act_open_req *)skb_put(skb, wrlen);
790
			INIT_TP_WR(req, 0);
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
			break;
		case CHELSIO_T5:
			t5req = (struct cpl_t5_act_open_req *)skb_put(skb,
					wrlen);
			INIT_TP_WR(t5req, 0);
			req = (struct cpl_act_open_req *)t5req;
			break;
		case CHELSIO_T6:
			t6req = (struct cpl_t6_act_open_req *)skb_put(skb,
					wrlen);
			INIT_TP_WR(t6req, 0);
			req = (struct cpl_act_open_req *)t6req;
			t5req = (struct cpl_t5_act_open_req *)t6req;
			break;
		default:
			pr_err("T%d Chip is not supported\n",
			       CHELSIO_CHIP_VERSION(adapter_type));
			ret = -EINVAL;
			goto clip_release;
		}

		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
					((ep->rss_qid<<14) | ep->atid)));
		req->local_port = la->sin_port;
		req->peer_port = ra->sin_port;
		req->local_ip = la->sin_addr.s_addr;
		req->peer_ip = ra->sin_addr.s_addr;
		req->opt0 = cpu_to_be64(opt0);

		if (is_t4(ep->com.dev->rdev.lldi.adapter_type)) {
821
			req->params = cpu_to_be32(params);
822 823
			req->opt2 = cpu_to_be32(opt2);
		} else {
824 825 826 827
			if (is_t5(ep->com.dev->rdev.lldi.adapter_type)) {
				t5req->params =
					  cpu_to_be64(FILTER_TUPLE_V(params));
				t5req->rsvd = cpu_to_be32(isn);
J
Joe Perches 已提交
828
			pr_debug("%s snd_isn %u\n", __func__, t5req->rsvd);
829 830 831 832 833
				t5req->opt2 = cpu_to_be32(opt2);
			} else {
				t6req->params =
					  cpu_to_be64(FILTER_TUPLE_V(params));
				t6req->rsvd = cpu_to_be32(isn);
834
			pr_debug("%s snd_isn %u\n", __func__, t6req->rsvd);
835 836
				t6req->opt2 = cpu_to_be32(opt2);
			}
837 838 839 840
		}
	} else {
		switch (CHELSIO_CHIP_VERSION(adapter_type)) {
		case CHELSIO_T4:
841 842
			req6 = (struct cpl_act_open_req6 *)skb_put(skb, wrlen);
			INIT_TP_WR(req6, 0);
843 844 845 846 847 848 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
			break;
		case CHELSIO_T5:
			t5req6 = (struct cpl_t5_act_open_req6 *)skb_put(skb,
					wrlen);
			INIT_TP_WR(t5req6, 0);
			req6 = (struct cpl_act_open_req6 *)t5req6;
			break;
		case CHELSIO_T6:
			t6req6 = (struct cpl_t6_act_open_req6 *)skb_put(skb,
					wrlen);
			INIT_TP_WR(t6req6, 0);
			req6 = (struct cpl_act_open_req6 *)t6req6;
			t5req6 = (struct cpl_t5_act_open_req6 *)t6req6;
			break;
		default:
			pr_err("T%d Chip is not supported\n",
			       CHELSIO_CHIP_VERSION(adapter_type));
			ret = -EINVAL;
			goto clip_release;
		}

		OPCODE_TID(req6) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
					((ep->rss_qid<<14)|ep->atid)));
		req6->local_port = la6->sin6_port;
		req6->peer_port = ra6->sin6_port;
		req6->local_ip_hi = *((__be64 *)(la6->sin6_addr.s6_addr));
		req6->local_ip_lo = *((__be64 *)(la6->sin6_addr.s6_addr + 8));
		req6->peer_ip_hi = *((__be64 *)(ra6->sin6_addr.s6_addr));
		req6->peer_ip_lo = *((__be64 *)(ra6->sin6_addr.s6_addr + 8));
		req6->opt0 = cpu_to_be64(opt0);

		if (is_t4(ep->com.dev->rdev.lldi.adapter_type)) {
875 876
			req6->params = cpu_to_be32(cxgb4_select_ntuple(netdev,
								      ep->l2t));
877 878
			req6->opt2 = cpu_to_be32(opt2);
		} else {
879 880 881 882
			if (is_t5(ep->com.dev->rdev.lldi.adapter_type)) {
				t5req6->params =
					    cpu_to_be64(FILTER_TUPLE_V(params));
				t5req6->rsvd = cpu_to_be32(isn);
J
Joe Perches 已提交
883
			pr_debug("%s snd_isn %u\n", __func__, t5req6->rsvd);
884 885 886 887 888
				t5req6->opt2 = cpu_to_be32(opt2);
			} else {
				t6req6->params =
					    cpu_to_be64(FILTER_TUPLE_V(params));
				t6req6->rsvd = cpu_to_be32(isn);
889
			pr_debug("%s snd_isn %u\n", __func__, t6req6->rsvd);
890 891 892
				t6req6->opt2 = cpu_to_be32(opt2);
			}

893
		}
894 895
	}

896
	set_bit(ACT_OPEN_REQ, &ep->com.history);
H
Hariprasad S 已提交
897
	ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
898
clip_release:
H
Hariprasad S 已提交
899 900 901 902
	if (ret && ep->com.remote_addr.ss_family == AF_INET6)
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&la6->sin6_addr.s6_addr, 1);
	return ret;
903 904
}

905 906
static int send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb,
			u8 mpa_rev_to_use)
907
{
908
	int mpalen, wrlen, ret;
909 910
	struct fw_ofld_tx_data_wr *req;
	struct mpa_message *mpa;
911
	struct mpa_v2_conn_params mpa_v2_params;
912

J
Joe Perches 已提交
913 914
	pr_debug("%s ep %p tid %u pd_len %d\n",
		 __func__, ep, ep->hwtid, ep->plen);
915 916 917 918

	BUG_ON(skb_cloned(skb));

	mpalen = sizeof(*mpa) + ep->plen;
919 920
	if (mpa_rev_to_use == 2)
		mpalen += sizeof(struct mpa_v2_conn_params);
921 922 923 924
	wrlen = roundup(mpalen + sizeof *req, 16);
	skb = get_skb(skb, wrlen, GFP_KERNEL);
	if (!skb) {
		connect_reply_upcall(ep, -ENOMEM);
925
		return -ENOMEM;
926 927 928 929 930 931
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);

	req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	req->op_to_immdlen = cpu_to_be32(
932 933 934
		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
		FW_WR_COMPL_F |
		FW_WR_IMMDLEN_V(mpalen));
935
	req->flowid_len16 = cpu_to_be32(
936 937
		FW_WR_FLOWID_V(ep->hwtid) |
		FW_WR_LEN16_V(wrlen >> 4));
938 939
	req->plen = cpu_to_be32(mpalen);
	req->tunnel_to_proxy = cpu_to_be32(
940 941
		FW_OFLD_TX_DATA_WR_FLUSH_F |
		FW_OFLD_TX_DATA_WR_SHOVE_F);
942 943 944

	mpa = (struct mpa_message *)(req + 1);
	memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
945 946 947 948 949 950 951 952 953 954 955 956 957

	mpa->flags = 0;
	if (crc_enabled)
		mpa->flags |= MPA_CRC;
	if (markers_enabled) {
		mpa->flags |= MPA_MARKERS;
		ep->mpa_attr.recv_marker_enabled = 1;
	} else {
		ep->mpa_attr.recv_marker_enabled = 0;
	}
	if (mpa_rev_to_use == 2)
		mpa->flags |= MPA_ENHANCED_RDMA_CONN;

958
	mpa->private_data_size = htons(ep->plen);
959
	mpa->revision = mpa_rev_to_use;
960
	if (mpa_rev_to_use == 1) {
961
		ep->tried_with_mpa_v1 = 1;
962 963
		ep->retry_with_mpa_v1 = 0;
	}
964 965

	if (mpa_rev_to_use == 2) {
966 967
		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
					       sizeof (struct mpa_v2_conn_params));
J
Joe Perches 已提交
968 969
		pr_debug("%s initiator ird %u ord %u\n", __func__, ep->ird,
			 ep->ord);
970 971 972 973 974 975 976 977 978 979 980 981 982 983
		mpa_v2_params.ird = htons((u16)ep->ird);
		mpa_v2_params.ord = htons((u16)ep->ord);

		if (peer2peer) {
			mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
			if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
				mpa_v2_params.ord |=
					htons(MPA_V2_RDMA_WRITE_RTR);
			else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
				mpa_v2_params.ord |=
					htons(MPA_V2_RDMA_READ_RTR);
		}
		memcpy(mpa->private_data, &mpa_v2_params,
		       sizeof(struct mpa_v2_conn_params));
984

985 986 987 988 989 990 991 992
		if (ep->plen)
			memcpy(mpa->private_data +
			       sizeof(struct mpa_v2_conn_params),
			       ep->mpa_pkt + sizeof(*mpa), ep->plen);
	} else
		if (ep->plen)
			memcpy(mpa->private_data,
					ep->mpa_pkt + sizeof(*mpa), ep->plen);
993 994 995 996 997 998 999 1000 1001 1002

	/*
	 * Reference the mpa skb.  This ensures the data area
	 * will remain in memory until the hw acks the tx.
	 * Function fw4_ack() will deref it.
	 */
	skb_get(skb);
	t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
	BUG_ON(ep->mpa_skb);
	ep->mpa_skb = skb;
1003 1004 1005
	ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
	if (ret)
		return ret;
1006
	start_ep_timer(ep);
1007
	__state_set(&ep->com, MPA_REQ_SENT);
1008
	ep->mpa_attr.initiator = 1;
1009
	ep->snd_seq += mpalen;
1010
	return ret;
1011 1012 1013 1014 1015 1016 1017 1018
}

static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
{
	int mpalen, wrlen;
	struct fw_ofld_tx_data_wr *req;
	struct mpa_message *mpa;
	struct sk_buff *skb;
1019
	struct mpa_v2_conn_params mpa_v2_params;
1020

J
Joe Perches 已提交
1021 1022
	pr_debug("%s ep %p tid %u pd_len %d\n",
		 __func__, ep, ep->hwtid, ep->plen);
1023 1024

	mpalen = sizeof(*mpa) + plen;
1025 1026
	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
		mpalen += sizeof(struct mpa_v2_conn_params);
1027 1028 1029 1030
	wrlen = roundup(mpalen + sizeof *req, 16);

	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
1031
		pr_err("%s - cannot alloc skb!\n", __func__);
1032 1033 1034 1035 1036 1037 1038
		return -ENOMEM;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);

	req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	req->op_to_immdlen = cpu_to_be32(
1039 1040 1041
		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
		FW_WR_COMPL_F |
		FW_WR_IMMDLEN_V(mpalen));
1042
	req->flowid_len16 = cpu_to_be32(
1043 1044
		FW_WR_FLOWID_V(ep->hwtid) |
		FW_WR_LEN16_V(wrlen >> 4));
1045 1046
	req->plen = cpu_to_be32(mpalen);
	req->tunnel_to_proxy = cpu_to_be32(
1047 1048
		FW_OFLD_TX_DATA_WR_FLUSH_F |
		FW_OFLD_TX_DATA_WR_SHOVE_F);
1049 1050 1051 1052 1053

	mpa = (struct mpa_message *)(req + 1);
	memset(mpa, 0, sizeof(*mpa));
	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
	mpa->flags = MPA_REJECT;
1054
	mpa->revision = ep->mpa_attr.version;
1055
	mpa->private_data_size = htons(plen);
1056 1057 1058

	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1059 1060
		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
					       sizeof (struct mpa_v2_conn_params));
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
		mpa_v2_params.ird = htons(((u16)ep->ird) |
					  (peer2peer ? MPA_V2_PEER2PEER_MODEL :
					   0));
		mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
					  (p2p_type ==
					   FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
					   MPA_V2_RDMA_WRITE_RTR : p2p_type ==
					   FW_RI_INIT_P2PTYPE_READ_REQ ?
					   MPA_V2_RDMA_READ_RTR : 0) : 0));
		memcpy(mpa->private_data, &mpa_v2_params,
		       sizeof(struct mpa_v2_conn_params));

		if (ep->plen)
			memcpy(mpa->private_data +
			       sizeof(struct mpa_v2_conn_params), pdata, plen);
	} else
		if (plen)
			memcpy(mpa->private_data, pdata, plen);
1079 1080 1081 1082 1083 1084 1085 1086

	/*
	 * Reference the mpa skb again.  This ensures the data area
	 * will remain in memory until the hw acks the tx.
	 * Function fw4_ack() will deref it.
	 */
	skb_get(skb);
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1087
	t4_set_arp_err_handler(skb, NULL, mpa_start_arp_failure);
1088 1089
	BUG_ON(ep->mpa_skb);
	ep->mpa_skb = skb;
1090
	ep->snd_seq += mpalen;
1091 1092 1093 1094 1095 1096 1097 1098 1099
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
}

static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
{
	int mpalen, wrlen;
	struct fw_ofld_tx_data_wr *req;
	struct mpa_message *mpa;
	struct sk_buff *skb;
1100
	struct mpa_v2_conn_params mpa_v2_params;
1101

J
Joe Perches 已提交
1102 1103
	pr_debug("%s ep %p tid %u pd_len %d\n",
		 __func__, ep, ep->hwtid, ep->plen);
1104 1105

	mpalen = sizeof(*mpa) + plen;
1106 1107
	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
		mpalen += sizeof(struct mpa_v2_conn_params);
1108 1109 1110 1111
	wrlen = roundup(mpalen + sizeof *req, 16);

	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
1112
		pr_err("%s - cannot alloc skb!\n", __func__);
1113 1114 1115 1116 1117 1118 1119
		return -ENOMEM;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);

	req = (struct fw_ofld_tx_data_wr *) skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	req->op_to_immdlen = cpu_to_be32(
1120 1121 1122
		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
		FW_WR_COMPL_F |
		FW_WR_IMMDLEN_V(mpalen));
1123
	req->flowid_len16 = cpu_to_be32(
1124 1125
		FW_WR_FLOWID_V(ep->hwtid) |
		FW_WR_LEN16_V(wrlen >> 4));
1126 1127
	req->plen = cpu_to_be32(mpalen);
	req->tunnel_to_proxy = cpu_to_be32(
1128 1129
		FW_OFLD_TX_DATA_WR_FLUSH_F |
		FW_OFLD_TX_DATA_WR_SHOVE_F);
1130 1131 1132 1133

	mpa = (struct mpa_message *)(req + 1);
	memset(mpa, 0, sizeof(*mpa));
	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1134 1135 1136 1137 1138
	mpa->flags = 0;
	if (ep->mpa_attr.crc_enabled)
		mpa->flags |= MPA_CRC;
	if (ep->mpa_attr.recv_marker_enabled)
		mpa->flags |= MPA_MARKERS;
1139
	mpa->revision = ep->mpa_attr.version;
1140
	mpa->private_data_size = htons(plen);
1141 1142 1143

	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1144 1145
		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
					       sizeof (struct mpa_v2_conn_params));
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
		mpa_v2_params.ird = htons((u16)ep->ird);
		mpa_v2_params.ord = htons((u16)ep->ord);
		if (peer2peer && (ep->mpa_attr.p2p_type !=
					FW_RI_INIT_P2PTYPE_DISABLED)) {
			mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);

			if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
				mpa_v2_params.ord |=
					htons(MPA_V2_RDMA_WRITE_RTR);
			else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
				mpa_v2_params.ord |=
					htons(MPA_V2_RDMA_READ_RTR);
		}

		memcpy(mpa->private_data, &mpa_v2_params,
		       sizeof(struct mpa_v2_conn_params));

		if (ep->plen)
			memcpy(mpa->private_data +
			       sizeof(struct mpa_v2_conn_params), pdata, plen);
	} else
		if (plen)
			memcpy(mpa->private_data, pdata, plen);
1169 1170 1171 1172 1173 1174 1175

	/*
	 * Reference the mpa skb.  This ensures the data area
	 * will remain in memory until the hw acks the tx.
	 * Function fw4_ack() will deref it.
	 */
	skb_get(skb);
1176
	t4_set_arp_err_handler(skb, NULL, mpa_start_arp_failure);
1177
	ep->mpa_skb = skb;
1178
	__state_set(&ep->com, MPA_REP_SENT);
1179
	ep->snd_seq += mpalen;
1180 1181 1182 1183 1184 1185 1186 1187
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
}

static int act_establish(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct cpl_act_establish *req = cplhdr(skb);
	unsigned int tid = GET_TID(req);
1188
	unsigned int atid = TID_TID_G(ntohl(req->tos_atid));
1189
	struct tid_info *t = dev->rdev.lldi.tids;
1190
	int ret;
1191 1192 1193

	ep = lookup_atid(t, atid);

J
Joe Perches 已提交
1194 1195
	pr_debug("%s ep %p tid %u snd_isn %u rcv_isn %u\n", __func__, ep, tid,
		 be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));
1196

1197
	mutex_lock(&ep->com.mutex);
1198 1199 1200 1201 1202
	dst_confirm(ep->dst);

	/* setup the hwtid for this connection */
	ep->hwtid = tid;
	cxgb4_insert_tid(t, ep, tid);
1203
	insert_ep_tid(ep);
1204 1205 1206 1207 1208 1209 1210

	ep->snd_seq = be32_to_cpu(req->snd_isn);
	ep->rcv_seq = be32_to_cpu(req->rcv_isn);

	set_emss(ep, ntohs(req->tcp_opt));

	/* dealloc the atid */
1211
	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
1212
	cxgb4_free_atid(t, atid);
1213
	set_bit(ACT_ESTAB, &ep->com.history);
1214 1215

	/* start MPA negotiation */
1216
	ret = send_flowc(ep);
1217 1218
	if (ret)
		goto err;
1219
	if (ep->retry_with_mpa_v1)
1220
		ret = send_mpa_req(ep, skb, 1);
1221
	else
1222 1223 1224
		ret = send_mpa_req(ep, skb, mpa_rev);
	if (ret)
		goto err;
1225
	mutex_unlock(&ep->com.mutex);
1226
	return 0;
1227 1228 1229 1230 1231
err:
	mutex_unlock(&ep->com.mutex);
	connect_reply_upcall(ep, -ENOMEM);
	c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
	return 0;
1232 1233
}

1234
static void close_complete_upcall(struct c4iw_ep *ep, int status)
1235 1236 1237
{
	struct iw_cm_event event;

J
Joe Perches 已提交
1238
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1239 1240
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CLOSE;
1241
	event.status = status;
1242
	if (ep->com.cm_id) {
J
Joe Perches 已提交
1243 1244
		pr_debug("close complete delivered ep %p cm_id %p tid %u\n",
			 ep, ep->com.cm_id, ep->hwtid);
1245
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1246
		deref_cm_id(&ep->com);
1247
		set_bit(CLOSE_UPCALL, &ep->com.history);
1248 1249 1250 1251 1252 1253 1254
	}
}

static void peer_close_upcall(struct c4iw_ep *ep)
{
	struct iw_cm_event event;

J
Joe Perches 已提交
1255
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1256 1257 1258
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_DISCONNECT;
	if (ep->com.cm_id) {
J
Joe Perches 已提交
1259 1260
		pr_debug("peer close delivered ep %p cm_id %p tid %u\n",
			 ep, ep->com.cm_id, ep->hwtid);
1261
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1262
		set_bit(DISCONN_UPCALL, &ep->com.history);
1263 1264 1265 1266 1267 1268 1269
	}
}

static void peer_abort_upcall(struct c4iw_ep *ep)
{
	struct iw_cm_event event;

J
Joe Perches 已提交
1270
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1271 1272 1273 1274
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CLOSE;
	event.status = -ECONNRESET;
	if (ep->com.cm_id) {
J
Joe Perches 已提交
1275 1276
		pr_debug("abort delivered ep %p cm_id %p tid %u\n", ep,
			 ep->com.cm_id, ep->hwtid);
1277
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1278
		deref_cm_id(&ep->com);
1279
		set_bit(ABORT_UPCALL, &ep->com.history);
1280 1281 1282 1283 1284 1285 1286
	}
}

static void connect_reply_upcall(struct c4iw_ep *ep, int status)
{
	struct iw_cm_event event;

J
Joe Perches 已提交
1287 1288
	pr_debug("%s ep %p tid %u status %d\n",
		 __func__, ep, ep->hwtid, status);
1289 1290 1291
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CONNECT_REPLY;
	event.status = status;
1292 1293 1294 1295
	memcpy(&event.local_addr, &ep->com.local_addr,
	       sizeof(ep->com.local_addr));
	memcpy(&event.remote_addr, &ep->com.remote_addr,
	       sizeof(ep->com.remote_addr));
1296 1297

	if ((status == 0) || (status == -ECONNREFUSED)) {
1298 1299
		if (!ep->tried_with_mpa_v1) {
			/* this means MPA_v2 is used */
1300 1301
			event.ord = ep->ird;
			event.ird = ep->ord;
1302 1303 1304 1305 1306 1307 1308
			event.private_data_len = ep->plen -
				sizeof(struct mpa_v2_conn_params);
			event.private_data = ep->mpa_pkt +
				sizeof(struct mpa_message) +
				sizeof(struct mpa_v2_conn_params);
		} else {
			/* this means MPA_v1 is used */
1309 1310
			event.ord = cur_max_read_depth(ep->com.dev);
			event.ird = cur_max_read_depth(ep->com.dev);
1311 1312 1313 1314
			event.private_data_len = ep->plen;
			event.private_data = ep->mpa_pkt +
				sizeof(struct mpa_message);
		}
1315
	}
1316

J
Joe Perches 已提交
1317 1318
	pr_debug("%s ep %p tid %u status %d\n", __func__, ep,
		 ep->hwtid, status);
1319
	set_bit(CONN_RPL_UPCALL, &ep->com.history);
1320 1321
	ep->com.cm_id->event_handler(ep->com.cm_id, &event);

1322 1323
	if (status < 0)
		deref_cm_id(&ep->com);
1324 1325
}

1326
static int connect_request_upcall(struct c4iw_ep *ep)
1327 1328
{
	struct iw_cm_event event;
1329
	int ret;
1330

J
Joe Perches 已提交
1331
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1332 1333
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CONNECT_REQUEST;
1334 1335 1336 1337
	memcpy(&event.local_addr, &ep->com.local_addr,
	       sizeof(ep->com.local_addr));
	memcpy(&event.remote_addr, &ep->com.remote_addr,
	       sizeof(ep->com.remote_addr));
1338
	event.provider_data = ep;
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
	if (!ep->tried_with_mpa_v1) {
		/* this means MPA_v2 is used */
		event.ord = ep->ord;
		event.ird = ep->ird;
		event.private_data_len = ep->plen -
			sizeof(struct mpa_v2_conn_params);
		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
			sizeof(struct mpa_v2_conn_params);
	} else {
		/* this means MPA_v1 is used. Send max supported */
1349 1350
		event.ord = cur_max_read_depth(ep->com.dev);
		event.ird = cur_max_read_depth(ep->com.dev);
1351 1352 1353
		event.private_data_len = ep->plen;
		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
	}
1354 1355 1356 1357 1358
	c4iw_get_ep(&ep->com);
	ret = ep->parent_ep->com.cm_id->event_handler(ep->parent_ep->com.cm_id,
						      &event);
	if (ret)
		c4iw_put_ep(&ep->com);
1359
	set_bit(CONNREQ_UPCALL, &ep->com.history);
1360
	c4iw_put_ep(&ep->parent_ep->com);
1361
	return ret;
1362 1363 1364 1365 1366 1367
}

static void established_upcall(struct c4iw_ep *ep)
{
	struct iw_cm_event event;

J
Joe Perches 已提交
1368
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1369 1370
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_ESTABLISHED;
1371 1372
	event.ird = ep->ord;
	event.ord = ep->ird;
1373
	if (ep->com.cm_id) {
J
Joe Perches 已提交
1374
		pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1375
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1376
		set_bit(ESTAB_UPCALL, &ep->com.history);
1377 1378 1379 1380 1381 1382
	}
}

static int update_rx_credits(struct c4iw_ep *ep, u32 credits)
{
	struct sk_buff *skb;
1383 1384
	u32 wrlen = roundup(sizeof(struct cpl_rx_data_ack), 16);
	u32 credit_dack;
1385

J
Joe Perches 已提交
1386 1387
	pr_debug("%s ep %p tid %u credits %u\n",
		 __func__, ep, ep->hwtid, credits);
1388 1389
	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
1390
		pr_err("update_rx_credits - cannot alloc skb!\n");
1391 1392 1393
		return 0;
	}

1394 1395 1396 1397 1398
	/*
	 * If we couldn't specify the entire rcv window at connection setup
	 * due to the limit in the number of bits in the RCV_BUFSIZ field,
	 * then add the overage in to the credits returned.
	 */
1399 1400
	if (ep->rcv_win > RCV_BUFSIZ_M * 1024)
		credits += ep->rcv_win - RCV_BUFSIZ_M * 1024;
1401

1402 1403 1404 1405 1406 1407
	credit_dack = credits | RX_FORCE_ACK_F | RX_DACK_CHANGE_F |
		      RX_DACK_MODE_V(dack_mode);

	cxgb_mk_rx_data_ack(skb, wrlen, ep->hwtid, ep->ctrlq_idx,
			    credit_dack);

1408 1409 1410 1411
	c4iw_ofld_send(&ep->com.dev->rdev, skb);
	return credits;
}

1412 1413
#define RELAXED_IRD_NEGOTIATION 1

1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
/*
 * process_mpa_reply - process streaming mode MPA reply
 *
 * Returns:
 *
 * 0 upon success indicating a connect request was delivered to the ULP
 * or the mpa request is incomplete but valid so far.
 *
 * 1 if a failure requires the caller to close the connection.
 *
 * 2 if a failure requires the caller to abort the connection.
 */
1426
static int process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
1427 1428
{
	struct mpa_message *mpa;
1429
	struct mpa_v2_conn_params *mpa_v2_params;
1430
	u16 plen;
1431 1432
	u16 resp_ird, resp_ord;
	u8 rtr_mismatch = 0, insuff_ird = 0;
1433 1434 1435
	struct c4iw_qp_attributes attrs;
	enum c4iw_qp_attr_mask mask;
	int err;
1436
	int disconnect = 0;
1437

J
Joe Perches 已提交
1438
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1439 1440 1441 1442 1443 1444 1445

	/*
	 * If we get more than the supported amount of private data
	 * then we must fail this connection.
	 */
	if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
		err = -EINVAL;
1446
		goto err_stop_timer;
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
	}

	/*
	 * copy the new data into our accumulation buffer.
	 */
	skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
				  skb->len);
	ep->mpa_pkt_len += skb->len;

	/*
	 * if we don't even have the mpa message, then bail.
	 */
	if (ep->mpa_pkt_len < sizeof(*mpa))
1460
		return 0;
1461 1462 1463
	mpa = (struct mpa_message *) ep->mpa_pkt;

	/* Validate MPA header. */
1464
	if (mpa->revision > mpa_rev) {
1465 1466
		pr_err("%s MPA version mismatch. Local = %d, Received = %d\n",
		       __func__, mpa_rev, mpa->revision);
1467
		err = -EPROTO;
1468
		goto err_stop_timer;
1469 1470 1471
	}
	if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
		err = -EPROTO;
1472
		goto err_stop_timer;
1473 1474 1475 1476 1477 1478 1479 1480 1481
	}

	plen = ntohs(mpa->private_data_size);

	/*
	 * Fail if there's too much private data.
	 */
	if (plen > MPA_MAX_PRIVATE_DATA) {
		err = -EPROTO;
1482
		goto err_stop_timer;
1483 1484 1485 1486 1487 1488 1489
	}

	/*
	 * If plen does not account for pkt size
	 */
	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
		err = -EPROTO;
1490
		goto err_stop_timer;
1491 1492 1493 1494 1495 1496 1497 1498 1499
	}

	ep->plen = (u8) plen;

	/*
	 * If we don't have all the pdata yet, then bail.
	 * We'll continue process when more data arrives.
	 */
	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1500
		return 0;
1501 1502 1503

	if (mpa->flags & MPA_REJECT) {
		err = -ECONNREFUSED;
1504
		goto err_stop_timer;
1505 1506
	}

1507 1508 1509 1510 1511 1512 1513 1514
	/*
	 * Stop mpa timer.  If it expired, then
	 * we ignore the MPA reply.  process_timeout()
	 * will abort the connection.
	 */
	if (stop_ep_timer(ep))
		return 0;

1515 1516 1517 1518 1519
	/*
	 * If we get here we have accumulated the entire mpa
	 * start reply message including private data. And
	 * the MPA header is valid.
	 */
1520
	__state_set(&ep->com, FPDU_MODE);
1521 1522
	ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
	ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
	ep->mpa_attr.version = mpa->revision;
	ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;

	if (mpa->revision == 2) {
		ep->mpa_attr.enhanced_rdma_conn =
			mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
		if (ep->mpa_attr.enhanced_rdma_conn) {
			mpa_v2_params = (struct mpa_v2_conn_params *)
				(ep->mpa_pkt + sizeof(*mpa));
			resp_ird = ntohs(mpa_v2_params->ird) &
				MPA_V2_IRD_ORD_MASK;
			resp_ord = ntohs(mpa_v2_params->ord) &
				MPA_V2_IRD_ORD_MASK;
J
Joe Perches 已提交
1536 1537 1538
			pr_debug("%s responder ird %u ord %u ep ird %u ord %u\n",
				 __func__,
				 resp_ird, resp_ord, ep->ird, ep->ord);
1539 1540 1541 1542 1543 1544

			/*
			 * This is a double-check. Ideally, below checks are
			 * not required since ird/ord stuff has been taken
			 * care of in c4iw_accept_cr
			 */
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
			if (ep->ird < resp_ord) {
				if (RELAXED_IRD_NEGOTIATION && resp_ord <=
				    ep->com.dev->rdev.lldi.max_ordird_qp)
					ep->ird = resp_ord;
				else
					insuff_ird = 1;
			} else if (ep->ird > resp_ord) {
				ep->ird = resp_ord;
			}
			if (ep->ord > resp_ird) {
				if (RELAXED_IRD_NEGOTIATION)
					ep->ord = resp_ird;
				else
					insuff_ird = 1;
			}
			if (insuff_ird) {
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
				err = -ENOMEM;
				ep->ird = resp_ord;
				ep->ord = resp_ird;
			}

			if (ntohs(mpa_v2_params->ird) &
					MPA_V2_PEER2PEER_MODEL) {
				if (ntohs(mpa_v2_params->ord) &
						MPA_V2_RDMA_WRITE_RTR)
					ep->mpa_attr.p2p_type =
						FW_RI_INIT_P2PTYPE_RDMA_WRITE;
				else if (ntohs(mpa_v2_params->ord) &
						MPA_V2_RDMA_READ_RTR)
					ep->mpa_attr.p2p_type =
						FW_RI_INIT_P2PTYPE_READ_REQ;
			}
		}
	} else if (mpa->revision == 1)
		if (peer2peer)
			ep->mpa_attr.p2p_type = p2p_type;

J
Joe Perches 已提交
1582 1583 1584 1585 1586
	pr_debug("%s - crc_enabled=%d, recv_marker_enabled=%d, xmit_marker_enabled=%d, version=%d p2p_type=%d local-p2p_type = %d\n",
		 __func__, ep->mpa_attr.crc_enabled,
		 ep->mpa_attr.recv_marker_enabled,
		 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
		 ep->mpa_attr.p2p_type, p2p_type);
1587 1588 1589 1590 1591 1592 1593

	/*
	 * If responder's RTR does not match with that of initiator, assign
	 * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
	 * generated when moving QP to RTS state.
	 * A TERM message will be sent after QP has moved to RTS state
	 */
1594
	if ((ep->mpa_attr.version == 2) && peer2peer &&
1595 1596 1597 1598
			(ep->mpa_attr.p2p_type != p2p_type)) {
		ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
		rtr_mismatch = 1;
	}
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614

	attrs.mpa_attr = ep->mpa_attr;
	attrs.max_ird = ep->ird;
	attrs.max_ord = ep->ord;
	attrs.llp_stream_handle = ep;
	attrs.next_state = C4IW_QP_STATE_RTS;

	mask = C4IW_QP_ATTR_NEXT_STATE |
	    C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
	    C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;

	/* bind QP and TID with INIT_WR */
	err = c4iw_modify_qp(ep->com.qp->rhp,
			     ep->com.qp, mask, &attrs, 1);
	if (err)
		goto err;
1615 1616 1617 1618 1619 1620

	/*
	 * If responder's RTR requirement did not match with what initiator
	 * supports, generate TERM message
	 */
	if (rtr_mismatch) {
1621
		pr_err("%s: RTR mismatch, sending TERM\n", __func__);
1622 1623 1624
		attrs.layer_etype = LAYER_MPA | DDP_LLP;
		attrs.ecode = MPA_NOMATCH_RTR;
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1625
		attrs.send_term = 1;
1626
		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1627
				C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1628
		err = -ENOMEM;
1629
		disconnect = 1;
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
		goto out;
	}

	/*
	 * Generate TERM if initiator IRD is not sufficient for responder
	 * provided ORD. Currently, we do the same behaviour even when
	 * responder provided IRD is also not sufficient as regards to
	 * initiator ORD.
	 */
	if (insuff_ird) {
1640
		pr_err("%s: Insufficient IRD, sending TERM\n", __func__);
1641 1642 1643
		attrs.layer_etype = LAYER_MPA | DDP_LLP;
		attrs.ecode = MPA_INSUFF_IRD;
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1644
		attrs.send_term = 1;
1645
		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1646
				C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1647
		err = -ENOMEM;
1648
		disconnect = 1;
1649 1650
		goto out;
	}
1651
	goto out;
1652 1653
err_stop_timer:
	stop_ep_timer(ep);
1654
err:
1655
	disconnect = 2;
1656 1657
out:
	connect_reply_upcall(ep, err);
1658
	return disconnect;
1659 1660
}

1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
/*
 * process_mpa_request - process streaming mode MPA request
 *
 * Returns:
 *
 * 0 upon success indicating a connect request was delivered to the ULP
 * or the mpa request is incomplete but valid so far.
 *
 * 1 if a failure requires the caller to close the connection.
 *
 * 2 if a failure requires the caller to abort the connection.
 */
static int process_mpa_request(struct c4iw_ep *ep, struct sk_buff *skb)
1674 1675
{
	struct mpa_message *mpa;
1676
	struct mpa_v2_conn_params *mpa_v2_params;
1677 1678
	u16 plen;

J
Joe Perches 已提交
1679
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1680 1681 1682 1683 1684

	/*
	 * If we get more than the supported amount of private data
	 * then we must fail this connection.
	 */
1685 1686
	if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt))
		goto err_stop_timer;
1687

J
Joe Perches 已提交
1688
	pr_debug("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701

	/*
	 * Copy the new data into our accumulation buffer.
	 */
	skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
				  skb->len);
	ep->mpa_pkt_len += skb->len;

	/*
	 * If we don't even have the mpa message, then bail.
	 * We'll continue process when more data arrives.
	 */
	if (ep->mpa_pkt_len < sizeof(*mpa))
1702
		return 0;
1703

J
Joe Perches 已提交
1704
	pr_debug("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1705 1706 1707 1708 1709
	mpa = (struct mpa_message *) ep->mpa_pkt;

	/*
	 * Validate MPA Header.
	 */
1710
	if (mpa->revision > mpa_rev) {
1711 1712
		pr_err("%s MPA version mismatch. Local = %d, Received = %d\n",
		       __func__, mpa_rev, mpa->revision);
1713
		goto err_stop_timer;
1714 1715
	}

1716 1717
	if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key)))
		goto err_stop_timer;
1718 1719 1720 1721 1722 1723

	plen = ntohs(mpa->private_data_size);

	/*
	 * Fail if there's too much private data.
	 */
1724 1725
	if (plen > MPA_MAX_PRIVATE_DATA)
		goto err_stop_timer;
1726 1727 1728 1729

	/*
	 * If plen does not account for pkt size
	 */
1730 1731
	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen))
		goto err_stop_timer;
1732 1733 1734 1735 1736 1737
	ep->plen = (u8) plen;

	/*
	 * If we don't have all the pdata yet, then bail.
	 */
	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1738
		return 0;
1739 1740 1741 1742 1743 1744 1745 1746 1747

	/*
	 * If we get here we have accumulated the entire mpa
	 * start reply message including private data.
	 */
	ep->mpa_attr.initiator = 0;
	ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
	ep->mpa_attr.recv_marker_enabled = markers_enabled;
	ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
	ep->mpa_attr.version = mpa->revision;
	if (mpa->revision == 1)
		ep->tried_with_mpa_v1 = 1;
	ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;

	if (mpa->revision == 2) {
		ep->mpa_attr.enhanced_rdma_conn =
			mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
		if (ep->mpa_attr.enhanced_rdma_conn) {
			mpa_v2_params = (struct mpa_v2_conn_params *)
				(ep->mpa_pkt + sizeof(*mpa));
			ep->ird = ntohs(mpa_v2_params->ird) &
				MPA_V2_IRD_ORD_MASK;
1761 1762
			ep->ird = min_t(u32, ep->ird,
					cur_max_read_depth(ep->com.dev));
1763 1764
			ep->ord = ntohs(mpa_v2_params->ord) &
				MPA_V2_IRD_ORD_MASK;
1765 1766
			ep->ord = min_t(u32, ep->ord,
					cur_max_read_depth(ep->com.dev));
J
Joe Perches 已提交
1767 1768
			pr_debug("%s initiator ird %u ord %u\n",
				 __func__, ep->ird, ep->ord);
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
			if (ntohs(mpa_v2_params->ird) & MPA_V2_PEER2PEER_MODEL)
				if (peer2peer) {
					if (ntohs(mpa_v2_params->ord) &
							MPA_V2_RDMA_WRITE_RTR)
						ep->mpa_attr.p2p_type =
						FW_RI_INIT_P2PTYPE_RDMA_WRITE;
					else if (ntohs(mpa_v2_params->ord) &
							MPA_V2_RDMA_READ_RTR)
						ep->mpa_attr.p2p_type =
						FW_RI_INIT_P2PTYPE_READ_REQ;
				}
		}
	} else if (mpa->revision == 1)
		if (peer2peer)
			ep->mpa_attr.p2p_type = p2p_type;

J
Joe Perches 已提交
1785 1786 1787 1788 1789
	pr_debug("%s - crc_enabled=%d, recv_marker_enabled=%d, xmit_marker_enabled=%d, version=%d p2p_type=%d\n",
		 __func__,
		 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
		 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
		 ep->mpa_attr.p2p_type);
1790

1791 1792 1793 1794 1795 1796
	__state_set(&ep->com, MPA_REQ_RCVD);

	/* drive upcall */
	mutex_lock_nested(&ep->parent_ep->com.mutex, SINGLE_DEPTH_NESTING);
	if (ep->parent_ep->com.state != DEAD) {
		if (connect_request_upcall(ep))
1797
			goto err_unlock_parent;
1798 1799
	} else {
		goto err_unlock_parent;
1800
	}
1801
	mutex_unlock(&ep->parent_ep->com.mutex);
1802 1803 1804 1805 1806 1807 1808 1809 1810
	return 0;

err_unlock_parent:
	mutex_unlock(&ep->parent_ep->com.mutex);
	goto err_out;
err_stop_timer:
	(void)stop_ep_timer(ep);
err_out:
	return 2;
1811 1812 1813 1814 1815 1816 1817 1818
}

static int rx_data(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct cpl_rx_data *hdr = cplhdr(skb);
	unsigned int dlen = ntohs(hdr->len);
	unsigned int tid = GET_TID(hdr);
1819
	__u8 status = hdr->status;
1820
	int disconnect = 0;
1821

1822
	ep = get_ep_from_tid(dev, tid);
1823 1824
	if (!ep)
		return 0;
J
Joe Perches 已提交
1825
	pr_debug("%s ep %p tid %u dlen %u\n", __func__, ep, ep->hwtid, dlen);
1826 1827
	skb_pull(skb, sizeof(*hdr));
	skb_trim(skb, dlen);
1828
	mutex_lock(&ep->com.mutex);
1829

1830
	switch (ep->com.state) {
1831
	case MPA_REQ_SENT:
1832
		update_rx_credits(ep, dlen);
1833
		ep->rcv_seq += dlen;
1834
		disconnect = process_mpa_reply(ep, skb);
1835 1836
		break;
	case MPA_REQ_WAIT:
1837
		update_rx_credits(ep, dlen);
1838
		ep->rcv_seq += dlen;
1839
		disconnect = process_mpa_request(ep, skb);
1840
		break;
1841 1842
	case FPDU_MODE: {
		struct c4iw_qp_attributes attrs;
1843 1844

		update_rx_credits(ep, dlen);
1845
		BUG_ON(!ep->com.qp);
1846
		if (status)
1847
			pr_err("%s Unexpected streaming data." \
1848 1849
			       " qpid %u ep %p state %d tid %u status %d\n",
			       __func__, ep->com.qp->wq.sq.qid, ep,
1850
			       ep->com.state, ep->hwtid, status);
1851
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1852
		c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1853 1854
			       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
		disconnect = 1;
1855 1856
		break;
	}
1857 1858 1859
	default:
		break;
	}
1860
	mutex_unlock(&ep->com.mutex);
1861
	if (disconnect)
1862
		c4iw_ep_disconnect(ep, disconnect == 2, GFP_KERNEL);
1863
	c4iw_put_ep(&ep->com);
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
	return 0;
}

static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
	int release = 0;
	unsigned int tid = GET_TID(rpl);

1874
	ep = get_ep_from_tid(dev, tid);
1875
	if (!ep) {
1876
		pr_warn("Abort rpl to freed endpoint\n");
1877 1878
		return 0;
	}
J
Joe Perches 已提交
1879
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1880
	mutex_lock(&ep->com.mutex);
1881 1882
	switch (ep->com.state) {
	case ABORTING:
1883
		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1884 1885 1886 1887
		__state_set(&ep->com, DEAD);
		release = 1;
		break;
	default:
1888
		pr_err("%s ep %p state %d\n", __func__, ep, ep->com.state);
1889 1890
		break;
	}
1891
	mutex_unlock(&ep->com.mutex);
1892 1893 1894

	if (release)
		release_ep_resources(ep);
1895
	c4iw_put_ep(&ep->com);
1896 1897 1898
	return 0;
}

1899
static int send_fw_act_open_req(struct c4iw_ep *ep, unsigned int atid)
1900 1901 1902 1903
{
	struct sk_buff *skb;
	struct fw_ofld_connection_wr *req;
	unsigned int mtu_idx;
1904
	u32 wscale;
1905
	struct sockaddr_in *sin;
1906
	int win;
1907 1908 1909 1910

	skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
	req = (struct fw_ofld_connection_wr *)__skb_put(skb, sizeof(*req));
	memset(req, 0, sizeof(*req));
1911
	req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR));
1912
	req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
1913 1914
	req->le.filter = cpu_to_be32(cxgb4_select_ntuple(
				     ep->com.dev->rdev.lldi.ports[0],
1915
				     ep->l2t));
1916
	sin = (struct sockaddr_in *)&ep->com.local_addr;
1917 1918
	req->le.lport = sin->sin_port;
	req->le.u.ipv4.lip = sin->sin_addr.s_addr;
1919
	sin = (struct sockaddr_in *)&ep->com.remote_addr;
1920 1921
	req->le.pport = sin->sin_port;
	req->le.u.ipv4.pip = sin->sin_addr.s_addr;
1922
	req->tcb.t_state_to_astid =
1923 1924
			htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_SENT) |
			FW_OFLD_CONNECTION_WR_ASTID_V(atid));
1925
	req->tcb.cplrxdataack_cplpassacceptrpl =
1926
			htons(FW_OFLD_CONNECTION_WR_CPLRXDATAACK_F);
1927
	req->tcb.tx_max = (__force __be32) jiffies;
1928
	req->tcb.rcv_adv = htons(1);
1929 1930 1931
	cxgb_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
		      enable_tcp_timestamps,
		      (ep->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
1932
	wscale = cxgb_compute_wscale(rcv_win);
1933 1934 1935 1936 1937 1938

	/*
	 * Specify the largest window that will fit in opt0. The
	 * remainder will be specified in the rx_data_ack.
	 */
	win = ep->rcv_win >> 10;
1939 1940
	if (win > RCV_BUFSIZ_M)
		win = RCV_BUFSIZ_M;
1941

1942 1943
	req->tcb.opt0 = (__force __be64) (TCAM_BYPASS_F |
		(nocong ? NO_CONG_F : 0) |
1944
		KEEP_ALIVE_F |
1945
		DELACK_F |
1946 1947 1948 1949 1950
		WND_SCALE_V(wscale) |
		MSS_IDX_V(mtu_idx) |
		L2T_IDX_V(ep->l2t->idx) |
		TX_CHAN_V(ep->tx_chan) |
		SMAC_SEL_V(ep->smac_idx) |
H
Hariprasad S 已提交
1951
		DSCP_V(ep->tos >> 2) |
1952 1953
		ULP_MODE_V(ULP_MODE_TCPDDP) |
		RCV_BUFSIZ_V(win));
1954 1955
	req->tcb.opt2 = (__force __be32) (PACE_V(1) |
		TX_QUEUE_V(ep->com.dev->rdev.lldi.tx_modq[ep->tx_chan]) |
1956
		RX_CHANNEL_V(0) |
1957
		CCTRL_ECN_V(enable_ecn) |
1958
		RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid));
1959
	if (enable_tcp_timestamps)
1960
		req->tcb.opt2 |= (__force __be32)TSTAMPS_EN_F;
1961
	if (enable_tcp_sack)
1962
		req->tcb.opt2 |= (__force __be32)SACK_EN_F;
1963
	if (wscale && enable_tcp_window_scaling)
1964 1965 1966
		req->tcb.opt2 |= (__force __be32)WND_SCALE_EN_F;
	req->tcb.opt0 = cpu_to_be64((__force u64)req->tcb.opt0);
	req->tcb.opt2 = cpu_to_be32((__force u32)req->tcb.opt2);
1967 1968
	set_wr_txq(skb, CPL_PRIORITY_CONTROL, ep->ctrlq_idx);
	set_bit(ACT_OFLD_CONN, &ep->com.history);
1969
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1970 1971
}

1972
/*
1973 1974 1975
 * Some of the error codes above implicitly indicate that there is no TID
 * allocated with the result of an ACT_OPEN.  We use this predicate to make
 * that explicit.
1976 1977 1978
 */
static inline int act_open_has_tid(int status)
{
1979 1980 1981 1982 1983
	return (status != CPL_ERR_TCAM_PARITY &&
		status != CPL_ERR_TCAM_MISS &&
		status != CPL_ERR_TCAM_FULL &&
		status != CPL_ERR_CONN_EXIST_SYNRECV &&
		status != CPL_ERR_CONN_EXIST);
1984 1985
}

1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
static char *neg_adv_str(unsigned int status)
{
	switch (status) {
	case CPL_ERR_RTX_NEG_ADVICE:
		return "Retransmit timeout";
	case CPL_ERR_PERSIST_NEG_ADVICE:
		return "Persist timeout";
	case CPL_ERR_KEEPALV_NEG_ADVICE:
		return "Keepalive timeout";
	default:
		return "Unknown";
	}
}

2000 2001 2002 2003
static void set_tcp_window(struct c4iw_ep *ep, struct port_info *pi)
{
	ep->snd_win = snd_win;
	ep->rcv_win = rcv_win;
J
Joe Perches 已提交
2004 2005
	pr_debug("%s snd_win %d rcv_win %d\n",
		 __func__, ep->snd_win, ep->rcv_win);
2006 2007
}

2008 2009
#define ACT_OPEN_RETRY_COUNT 2

2010 2011
static int import_ep(struct c4iw_ep *ep, int iptype, __u8 *peer_ip,
		     struct dst_entry *dst, struct c4iw_dev *cdev,
H
Hariprasad S 已提交
2012
		     bool clear_mpa_v1, enum chip_type adapter_type, u8 tos)
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
{
	struct neighbour *n;
	int err, step;
	struct net_device *pdev;

	n = dst_neigh_lookup(dst, peer_ip);
	if (!n)
		return -ENODEV;

	rcu_read_lock();
	err = -ENOMEM;
	if (n->dev->flags & IFF_LOOPBACK) {
		if (iptype == 4)
			pdev = ip_dev_find(&init_net, *(__be32 *)peer_ip);
		else if (IS_ENABLED(CONFIG_IPV6))
			for_each_netdev(&init_net, pdev) {
				if (ipv6_chk_addr(&init_net,
						  (struct in6_addr *)peer_ip,
						  pdev, 1))
					break;
			}
		else
			pdev = NULL;

		if (!pdev) {
			err = -ENODEV;
			goto out;
		}
		ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
H
Hariprasad S 已提交
2042
					n, pdev, rt_tos2priority(tos));
2043 2044
		if (!ep->l2t) {
			dev_put(pdev);
2045
			goto out;
2046
		}
2047 2048
		ep->mtu = pdev->mtu;
		ep->tx_chan = cxgb4_port_chan(pdev);
2049 2050
		ep->smac_idx = cxgb4_tp_smt_idx(adapter_type,
						cxgb4_port_viid(pdev));
2051 2052 2053 2054 2055 2056 2057 2058
		step = cdev->rdev.lldi.ntxq /
			cdev->rdev.lldi.nchan;
		ep->txq_idx = cxgb4_port_idx(pdev) * step;
		step = cdev->rdev.lldi.nrxq /
			cdev->rdev.lldi.nchan;
		ep->ctrlq_idx = cxgb4_port_idx(pdev);
		ep->rss_qid = cdev->rdev.lldi.rxq_ids[
			cxgb4_port_idx(pdev) * step];
2059
		set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2060 2061 2062 2063 2064 2065 2066 2067
		dev_put(pdev);
	} else {
		pdev = get_real_dev(n->dev);
		ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
					n, pdev, 0);
		if (!ep->l2t)
			goto out;
		ep->mtu = dst_mtu(dst);
S
Steve Wise 已提交
2068
		ep->tx_chan = cxgb4_port_chan(pdev);
2069 2070
		ep->smac_idx = cxgb4_tp_smt_idx(adapter_type,
						cxgb4_port_viid(pdev));
2071 2072
		step = cdev->rdev.lldi.ntxq /
			cdev->rdev.lldi.nchan;
S
Steve Wise 已提交
2073 2074
		ep->txq_idx = cxgb4_port_idx(pdev) * step;
		ep->ctrlq_idx = cxgb4_port_idx(pdev);
2075 2076 2077
		step = cdev->rdev.lldi.nrxq /
			cdev->rdev.lldi.nchan;
		ep->rss_qid = cdev->rdev.lldi.rxq_ids[
S
Steve Wise 已提交
2078
			cxgb4_port_idx(pdev) * step];
2079
		set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094

		if (clear_mpa_v1) {
			ep->retry_with_mpa_v1 = 0;
			ep->tried_with_mpa_v1 = 0;
		}
	}
	err = 0;
out:
	rcu_read_unlock();

	neigh_release(n);

	return err;
}

2095 2096 2097
static int c4iw_reconnect(struct c4iw_ep *ep)
{
	int err = 0;
2098
	int size = 0;
2099
	struct sockaddr_in *laddr = (struct sockaddr_in *)
2100
				    &ep->com.cm_id->m_local_addr;
2101
	struct sockaddr_in *raddr = (struct sockaddr_in *)
2102
				    &ep->com.cm_id->m_remote_addr;
2103
	struct sockaddr_in6 *laddr6 = (struct sockaddr_in6 *)
2104
				      &ep->com.cm_id->m_local_addr;
2105
	struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)
2106
				      &ep->com.cm_id->m_remote_addr;
2107 2108
	int iptype;
	__u8 *ra;
2109

J
Joe Perches 已提交
2110
	pr_debug("%s qp %p cm_id %p\n", __func__, ep->com.qp, ep->com.cm_id);
2111
	init_timer(&ep->timer);
2112
	c4iw_init_wr_wait(&ep->com.wr_wait);
2113

2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
	/* When MPA revision is different on nodes, the node with MPA_rev=2
	 * tries to reconnect with MPA_rev 1 for the same EP through
	 * c4iw_reconnect(), where the same EP is assigned with new tid for
	 * further connection establishment. As we are using the same EP pointer
	 * for reconnect, few skbs are used during the previous c4iw_connect(),
	 * which leaves the EP with inadequate skbs for further
	 * c4iw_reconnect(), Further causing an assert BUG_ON() due to empty
	 * skb_list() during peer_abort(). Allocate skbs which is already used.
	 */
	size = (CN_MAX_CON_BUF - skb_queue_len(&ep->com.ep_skb_list));
	if (alloc_ep_skb_list(&ep->com.ep_skb_list, size)) {
		err = -ENOMEM;
		goto fail1;
	}

2129 2130 2131 2132 2133
	/*
	 * Allocate an active TID to initiate a TCP connection.
	 */
	ep->atid = cxgb4_alloc_atid(ep->com.dev->rdev.lldi.tids, ep);
	if (ep->atid == -1) {
2134
		pr_err("%s - cannot alloc atid\n", __func__);
2135 2136 2137 2138 2139 2140
		err = -ENOMEM;
		goto fail2;
	}
	insert_handle(ep->com.dev, &ep->com.dev->atid_idr, ep, ep->atid);

	/* find a route */
2141
	if (ep->com.cm_id->m_local_addr.ss_family == AF_INET) {
2142 2143 2144 2145 2146
		ep->dst = cxgb_find_route(&ep->com.dev->rdev.lldi, get_real_dev,
					  laddr->sin_addr.s_addr,
					  raddr->sin_addr.s_addr,
					  laddr->sin_port,
					  raddr->sin_port, ep->com.cm_id->tos);
2147 2148 2149
		iptype = 4;
		ra = (__u8 *)&raddr->sin_addr;
	} else {
2150 2151 2152 2153 2154 2155 2156
		ep->dst = cxgb_find_route6(&ep->com.dev->rdev.lldi,
					   get_real_dev,
					   laddr6->sin6_addr.s6_addr,
					   raddr6->sin6_addr.s6_addr,
					   laddr6->sin6_port,
					   raddr6->sin6_port, 0,
					   raddr6->sin6_scope_id);
2157 2158 2159 2160
		iptype = 6;
		ra = (__u8 *)&raddr6->sin6_addr;
	}
	if (!ep->dst) {
2161
		pr_err("%s - cannot find route\n", __func__);
2162 2163 2164
		err = -EHOSTUNREACH;
		goto fail3;
	}
2165
	err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, false,
H
Hariprasad S 已提交
2166 2167
			ep->com.dev->rdev.lldi.adapter_type,
			ep->com.cm_id->tos);
2168
	if (err) {
2169
		pr_err("%s - cannot alloc l2e\n", __func__);
2170 2171 2172
		goto fail4;
	}

J
Joe Perches 已提交
2173 2174 2175
	pr_debug("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
		 __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
		 ep->l2t->idx);
2176 2177

	state_set(&ep->com, CONNECTING);
H
Hariprasad S 已提交
2178
	ep->tos = ep->com.cm_id->tos;
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198

	/* send connect request to rnic */
	err = send_connect(ep);
	if (!err)
		goto out;

	cxgb4_l2t_release(ep->l2t);
fail4:
	dst_release(ep->dst);
fail3:
	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
	cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
fail2:
	/*
	 * remember to send notification to upper layer.
	 * We are in here so the upper layer is not aware that this is
	 * re-connect attempt and so, upper layer is still waiting for
	 * response of 1st connect request.
	 */
	connect_reply_upcall(ep, -ECONNRESET);
2199
fail1:
2200 2201 2202 2203 2204
	c4iw_put_ep(&ep->com);
out:
	return err;
}

2205 2206 2207 2208
static int act_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct cpl_act_open_rpl *rpl = cplhdr(skb);
2209 2210
	unsigned int atid = TID_TID_G(AOPEN_ATID_G(
				      ntohl(rpl->atid_status)));
2211
	struct tid_info *t = dev->rdev.lldi.tids;
2212
	int status = AOPEN_STATUS_G(ntohl(rpl->atid_status));
2213 2214 2215 2216
	struct sockaddr_in *la;
	struct sockaddr_in *ra;
	struct sockaddr_in6 *la6;
	struct sockaddr_in6 *ra6;
2217
	int ret = 0;
2218 2219

	ep = lookup_atid(t, atid);
2220 2221 2222 2223
	la = (struct sockaddr_in *)&ep->com.local_addr;
	ra = (struct sockaddr_in *)&ep->com.remote_addr;
	la6 = (struct sockaddr_in6 *)&ep->com.local_addr;
	ra6 = (struct sockaddr_in6 *)&ep->com.remote_addr;
2224

J
Joe Perches 已提交
2225 2226
	pr_debug("%s ep %p atid %u status %u errno %d\n", __func__, ep, atid,
		 status, status2errno(status));
2227

2228
	if (cxgb_is_neg_adv(status)) {
J
Joe Perches 已提交
2229 2230
		pr_debug("%s Connection problems for atid %u status %u (%s)\n",
			 __func__, atid, status, neg_adv_str(status));
2231 2232 2233 2234
		ep->stats.connect_neg_adv++;
		mutex_lock(&dev->rdev.stats.lock);
		dev->rdev.stats.neg_adv++;
		mutex_unlock(&dev->rdev.stats.lock);
2235 2236 2237
		return 0;
	}

2238 2239
	set_bit(ACT_OPEN_RPL, &ep->com.history);

2240 2241 2242 2243 2244 2245 2246
	/*
	 * Log interesting failures.
	 */
	switch (status) {
	case CPL_ERR_CONN_RESET:
	case CPL_ERR_CONN_TIMEDOUT:
		break;
2247
	case CPL_ERR_TCAM_FULL:
2248
		mutex_lock(&dev->rdev.stats.lock);
2249
		dev->rdev.stats.tcam_full++;
2250 2251 2252
		mutex_unlock(&dev->rdev.stats.lock);
		if (ep->com.local_addr.ss_family == AF_INET &&
		    dev->rdev.lldi.enable_fw_ofld_conn) {
2253 2254 2255 2256
			ret = send_fw_act_open_req(ep, TID_TID_G(AOPEN_ATID_G(
						   ntohl(rpl->atid_status))));
			if (ret)
				goto fail;
2257 2258 2259 2260 2261 2262
			return 0;
		}
		break;
	case CPL_ERR_CONN_EXIST:
		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
			set_bit(ACT_RETRY_INUSE, &ep->com.history);
H
Hariprasad S 已提交
2263 2264 2265
			if (ep->com.remote_addr.ss_family == AF_INET6) {
				struct sockaddr_in6 *sin6 =
						(struct sockaddr_in6 *)
2266
						&ep->com.local_addr;
H
Hariprasad S 已提交
2267 2268 2269 2270 2271
				cxgb4_clip_release(
						ep->com.dev->rdev.lldi.ports[0],
						(const u32 *)
						&sin6->sin6_addr.s6_addr, 1);
			}
2272 2273 2274 2275 2276 2277 2278 2279
			remove_handle(ep->com.dev, &ep->com.dev->atid_idr,
					atid);
			cxgb4_free_atid(t, atid);
			dst_release(ep->dst);
			cxgb4_l2t_release(ep->l2t);
			c4iw_reconnect(ep);
			return 0;
		}
2280
		break;
2281
	default:
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
		if (ep->com.local_addr.ss_family == AF_INET) {
			pr_info("Active open failure - atid %u status %u errno %d %pI4:%u->%pI4:%u\n",
				atid, status, status2errno(status),
				&la->sin_addr.s_addr, ntohs(la->sin_port),
				&ra->sin_addr.s_addr, ntohs(ra->sin_port));
		} else {
			pr_info("Active open failure - atid %u status %u errno %d %pI6:%u->%pI6:%u\n",
				atid, status, status2errno(status),
				la6->sin6_addr.s6_addr, ntohs(la6->sin6_port),
				ra6->sin6_addr.s6_addr, ntohs(ra6->sin6_port));
		}
2293 2294 2295
		break;
	}

2296
fail:
2297 2298 2299
	connect_reply_upcall(ep, status2errno(status));
	state_set(&ep->com, DEAD);

H
Hariprasad S 已提交
2300 2301
	if (ep->com.remote_addr.ss_family == AF_INET6) {
		struct sockaddr_in6 *sin6 =
2302
			(struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
2303 2304 2305
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
2306 2307 2308
	if (status && act_open_has_tid(status))
		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl));

2309
	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
	cxgb4_free_atid(t, atid);
	dst_release(ep->dst);
	cxgb4_l2t_release(ep->l2t);
	c4iw_put_ep(&ep->com);

	return 0;
}

static int pass_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_pass_open_rpl *rpl = cplhdr(skb);
	unsigned int stid = GET_TID(rpl);
2322
	struct c4iw_listen_ep *ep = get_ep_from_stid(dev, stid);
2323 2324

	if (!ep) {
J
Joe Perches 已提交
2325
		pr_debug("%s stid %d lookup failure!\n", __func__, stid);
2326
		goto out;
2327
	}
J
Joe Perches 已提交
2328 2329
	pr_debug("%s ep %p status %d error %d\n", __func__, ep,
		 rpl->status, status2errno(rpl->status));
2330
	c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
2331
	c4iw_put_ep(&ep->com);
2332
out:
2333 2334 2335 2336 2337 2338 2339
	return 0;
}

static int close_listsrv_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
	unsigned int stid = GET_TID(rpl);
2340
	struct c4iw_listen_ep *ep = get_ep_from_stid(dev, stid);
2341

J
Joe Perches 已提交
2342
	pr_debug("%s ep %p\n", __func__, ep);
2343
	c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
2344
	c4iw_put_ep(&ep->com);
2345 2346 2347
	return 0;
}

2348 2349
static int accept_cr(struct c4iw_ep *ep, struct sk_buff *skb,
		     struct cpl_pass_accept_req *req)
2350 2351 2352 2353 2354
{
	struct cpl_pass_accept_rpl *rpl;
	unsigned int mtu_idx;
	u64 opt0;
	u32 opt2;
2355
	u32 wscale;
2356
	struct cpl_t5_pass_accept_rpl *rpl5 = NULL;
2357
	int win;
2358
	enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
2359

J
Joe Perches 已提交
2360
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2361
	BUG_ON(skb_cloned(skb));
2362

2363
	skb_get(skb);
2364
	rpl = cplhdr(skb);
2365
	if (!is_t4(adapter_type)) {
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
		skb_trim(skb, roundup(sizeof(*rpl5), 16));
		rpl5 = (void *)rpl;
		INIT_TP_WR(rpl5, ep->hwtid);
	} else {
		skb_trim(skb, sizeof(*rpl));
		INIT_TP_WR(rpl, ep->hwtid);
	}
	OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
						    ep->hwtid));

2376 2377 2378
	cxgb_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
		      enable_tcp_timestamps && req->tcpopt.tstamp,
		      (ep->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
2379
	wscale = cxgb_compute_wscale(rcv_win);
2380 2381 2382 2383 2384 2385

	/*
	 * Specify the largest window that will fit in opt0. The
	 * remainder will be specified in the rx_data_ack.
	 */
	win = ep->rcv_win >> 10;
2386 2387
	if (win > RCV_BUFSIZ_M)
		win = RCV_BUFSIZ_M;
2388
	opt0 = (nocong ? NO_CONG_F : 0) |
2389
	       KEEP_ALIVE_F |
2390
	       DELACK_F |
2391 2392 2393 2394 2395
	       WND_SCALE_V(wscale) |
	       MSS_IDX_V(mtu_idx) |
	       L2T_IDX_V(ep->l2t->idx) |
	       TX_CHAN_V(ep->tx_chan) |
	       SMAC_SEL_V(ep->smac_idx) |
2396
	       DSCP_V(ep->tos >> 2) |
2397 2398 2399 2400
	       ULP_MODE_V(ULP_MODE_TCPDDP) |
	       RCV_BUFSIZ_V(win);
	opt2 = RX_CHANNEL_V(0) |
	       RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
2401 2402

	if (enable_tcp_timestamps && req->tcpopt.tstamp)
2403
		opt2 |= TSTAMPS_EN_F;
2404
	if (enable_tcp_sack && req->tcpopt.sack)
2405
		opt2 |= SACK_EN_F;
2406
	if (wscale && enable_tcp_window_scaling)
2407
		opt2 |= WND_SCALE_EN_F;
2408 2409 2410 2411
	if (enable_ecn) {
		const struct tcphdr *tcph;
		u32 hlen = ntohl(req->hdr_len);

2412 2413 2414 2415 2416 2417
		if (CHELSIO_CHIP_VERSION(adapter_type) <= CHELSIO_T5)
			tcph = (const void *)(req + 1) + ETH_HDR_LEN_G(hlen) +
				IP_HDR_LEN_G(hlen);
		else
			tcph = (const void *)(req + 1) +
				T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen);
2418
		if (tcph->ece && tcph->cwr)
2419
			opt2 |= CCTRL_ECN_V(1);
2420
	}
2421
	if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
2422
		u32 isn = (prandom_u32() & ~7UL) - 1;
2423
		opt2 |= T5_OPT_2_VALID_F;
2424
		opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
2425
		opt2 |= T5_ISS_F;
2426 2427 2428 2429 2430
		rpl5 = (void *)rpl;
		memset(&rpl5->iss, 0, roundup(sizeof(*rpl5)-sizeof(*rpl), 16));
		if (peer2peer)
			isn += 4;
		rpl5->iss = cpu_to_be32(isn);
J
Joe Perches 已提交
2431
		pr_debug("%s iss %u\n", __func__, be32_to_cpu(rpl5->iss));
2432
	}
2433 2434 2435

	rpl->opt0 = cpu_to_be64(opt0);
	rpl->opt2 = cpu_to_be32(opt2);
S
Steve Wise 已提交
2436
	set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
2437
	t4_set_arp_err_handler(skb, ep, pass_accept_rpl_arp_failure);
2438

2439
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
2440 2441
}

2442
static void reject_cr(struct c4iw_dev *dev, u32 hwtid, struct sk_buff *skb)
2443
{
J
Joe Perches 已提交
2444
	pr_debug("%s c4iw_dev %p tid %u\n", __func__, dev, hwtid);
2445 2446 2447 2448 2449 2450 2451 2452
	BUG_ON(skb_cloned(skb));
	skb_trim(skb, sizeof(struct cpl_tid_release));
	release_tid(&dev->rdev, hwtid, skb);
	return;
}

static int pass_accept_req(struct c4iw_dev *dev, struct sk_buff *skb)
{
2453
	struct c4iw_ep *child_ep = NULL, *parent_ep;
2454
	struct cpl_pass_accept_req *req = cplhdr(skb);
2455
	unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
2456 2457 2458
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int hwtid = GET_TID(req);
	struct dst_entry *dst;
2459
	__u8 local_ip[16], peer_ip[16];
2460
	__be16 local_port, peer_port;
H
Hariprasad S 已提交
2461
	struct sockaddr_in6 *sin6;
2462
	int err;
2463
	u16 peer_mss = ntohs(req->tcpopt.mss);
2464
	int iptype;
2465
	unsigned short hdrs;
H
Hariprasad S 已提交
2466
	u8 tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
2467

2468
	parent_ep = (struct c4iw_ep *)get_ep_from_stid(dev, stid);
2469
	if (!parent_ep) {
J
Joe Perches 已提交
2470 2471
		pr_debug("%s connect request on invalid stid %d\n",
			 __func__, stid);
2472 2473 2474
		goto reject;
	}

2475
	if (state_read(&parent_ep->com) != LISTEN) {
J
Joe Perches 已提交
2476
		pr_debug("%s - listening ep not in LISTEN\n", __func__);
2477 2478 2479
		goto reject;
	}

2480 2481
	cxgb_get_4tuple(req, parent_ep->com.dev->rdev.lldi.adapter_type,
			&iptype, local_ip, peer_ip, &local_port, &peer_port);
2482

2483
	/* Find output route */
2484
	if (iptype == 4)  {
J
Joe Perches 已提交
2485 2486 2487 2488
		pr_debug("%s parent ep %p hwtid %u laddr %pI4 raddr %pI4 lport %d rport %d peer_mss %d\n"
			 , __func__, parent_ep, hwtid,
			 local_ip, peer_ip, ntohs(local_port),
			 ntohs(peer_port), peer_mss);
2489 2490 2491
		dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
				      *(__be32 *)local_ip, *(__be32 *)peer_ip,
				      local_port, peer_port, tos);
2492
	} else {
J
Joe Perches 已提交
2493 2494 2495 2496
		pr_debug("%s parent ep %p hwtid %u laddr %pI6 raddr %pI6 lport %d rport %d peer_mss %d\n"
			 , __func__, parent_ep, hwtid,
			 local_ip, peer_ip, ntohs(local_port),
			 ntohs(peer_port), peer_mss);
2497 2498 2499 2500 2501
		dst = cxgb_find_route6(&dev->rdev.lldi, get_real_dev,
				local_ip, peer_ip, local_port, peer_port,
				PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
				((struct sockaddr_in6 *)
				 &parent_ep->com.local_addr)->sin6_scope_id);
2502 2503
	}
	if (!dst) {
2504
		pr_err("%s - failed to find dst entry!\n", __func__);
2505 2506
		goto reject;
	}
2507 2508 2509

	child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
	if (!child_ep) {
2510
		pr_err("%s - failed to allocate ep entry!\n", __func__);
2511 2512 2513 2514
		dst_release(dst);
		goto reject;
	}

2515
	err = import_ep(child_ep, iptype, peer_ip, dst, dev, false,
H
Hariprasad S 已提交
2516
			parent_ep->com.dev->rdev.lldi.adapter_type, tos);
2517
	if (err) {
2518
		pr_err("%s - failed to allocate l2t entry!\n", __func__);
2519
		dst_release(dst);
2520
		kfree(child_ep);
2521 2522
		goto reject;
	}
2523

2524 2525 2526 2527
	hdrs = sizeof(struct iphdr) + sizeof(struct tcphdr) +
	       ((enable_tcp_timestamps && req->tcpopt.tstamp) ? 12 : 0);
	if (peer_mss && child_ep->mtu > (peer_mss + hdrs))
		child_ep->mtu = peer_mss + hdrs;
2528

2529 2530 2531 2532
	skb_queue_head_init(&child_ep->com.ep_skb_list);
	if (alloc_ep_skb_list(&child_ep->com.ep_skb_list, CN_MAX_CON_BUF))
		goto fail;

2533 2534 2535
	state_set(&child_ep->com, CONNECTING);
	child_ep->com.dev = dev;
	child_ep->com.cm_id = NULL;
2536

2537 2538
	if (iptype == 4) {
		struct sockaddr_in *sin = (struct sockaddr_in *)
2539
			&child_ep->com.local_addr;
2540

2541
		sin->sin_family = AF_INET;
2542 2543
		sin->sin_port = local_port;
		sin->sin_addr.s_addr = *(__be32 *)local_ip;
2544 2545

		sin = (struct sockaddr_in *)&child_ep->com.local_addr;
2546
		sin->sin_family = AF_INET;
2547 2548 2549 2550
		sin->sin_port = ((struct sockaddr_in *)
				 &parent_ep->com.local_addr)->sin_port;
		sin->sin_addr.s_addr = *(__be32 *)local_ip;

2551
		sin = (struct sockaddr_in *)&child_ep->com.remote_addr;
2552
		sin->sin_family = AF_INET;
2553 2554 2555
		sin->sin_port = peer_port;
		sin->sin_addr.s_addr = *(__be32 *)peer_ip;
	} else {
2556
		sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2557 2558 2559
		sin6->sin6_family = PF_INET6;
		sin6->sin6_port = local_port;
		memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
2560 2561 2562 2563 2564 2565 2566

		sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
		sin6->sin6_family = PF_INET6;
		sin6->sin6_port = ((struct sockaddr_in6 *)
				   &parent_ep->com.local_addr)->sin6_port;
		memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);

2567
		sin6 = (struct sockaddr_in6 *)&child_ep->com.remote_addr;
2568 2569 2570 2571
		sin6->sin6_family = PF_INET6;
		sin6->sin6_port = peer_port;
		memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
	}
2572

2573 2574
	c4iw_get_ep(&parent_ep->com);
	child_ep->parent_ep = parent_ep;
H
Hariprasad S 已提交
2575
	child_ep->tos = tos;
2576 2577 2578
	child_ep->dst = dst;
	child_ep->hwtid = hwtid;

J
Joe Perches 已提交
2579 2580
	pr_debug("%s tx_chan %u smac_idx %u rss_qid %u\n", __func__,
		 child_ep->tx_chan, child_ep->smac_idx, child_ep->rss_qid);
2581 2582 2583

	init_timer(&child_ep->timer);
	cxgb4_insert_tid(t, child_ep, hwtid);
2584
	insert_ep_tid(child_ep);
2585 2586 2587 2588 2589 2590
	if (accept_cr(child_ep, skb, req)) {
		c4iw_put_ep(&parent_ep->com);
		release_ep_resources(child_ep);
	} else {
		set_bit(PASS_ACCEPT_REQ, &child_ep->com.history);
	}
H
Hariprasad S 已提交
2591
	if (iptype == 6) {
2592
		sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
H
Hariprasad S 已提交
2593 2594 2595
		cxgb4_clip_get(child_ep->com.dev->rdev.lldi.ports[0],
			       (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
2596
	goto out;
2597 2598
fail:
	c4iw_put_ep(&child_ep->com);
2599
reject:
2600
	reject_cr(dev, hwtid, skb);
2601 2602
	if (parent_ep)
		c4iw_put_ep(&parent_ep->com);
2603 2604 2605 2606 2607 2608 2609 2610 2611
out:
	return 0;
}

static int pass_establish(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct cpl_pass_establish *req = cplhdr(skb);
	unsigned int tid = GET_TID(req);
2612
	int ret;
2613

2614
	ep = get_ep_from_tid(dev, tid);
J
Joe Perches 已提交
2615
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2616 2617 2618
	ep->snd_seq = be32_to_cpu(req->snd_isn);
	ep->rcv_seq = be32_to_cpu(req->rcv_isn);

J
Joe Perches 已提交
2619 2620
	pr_debug("%s ep %p hwtid %u tcp_opt 0x%02x\n", __func__, ep, tid,
		 ntohs(req->tcp_opt));
2621

2622 2623 2624
	set_emss(ep, ntohs(req->tcp_opt));

	dst_confirm(ep->dst);
2625 2626
	mutex_lock(&ep->com.mutex);
	ep->com.state = MPA_REQ_WAIT;
2627
	start_ep_timer(ep);
2628
	set_bit(PASS_ESTAB, &ep->com.history);
2629
	ret = send_flowc(ep);
2630 2631 2632
	mutex_unlock(&ep->com.mutex);
	if (ret)
		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
2633
	c4iw_put_ep(&ep->com);
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645

	return 0;
}

static int peer_close(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_peer_close *hdr = cplhdr(skb);
	struct c4iw_ep *ep;
	struct c4iw_qp_attributes attrs;
	int disconnect = 1;
	int release = 0;
	unsigned int tid = GET_TID(hdr);
S
Steve Wise 已提交
2646
	int ret;
2647

2648 2649 2650 2651
	ep = get_ep_from_tid(dev, tid);
	if (!ep)
		return 0;

J
Joe Perches 已提交
2652
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2653 2654
	dst_confirm(ep->dst);

2655
	set_bit(PEER_CLOSE, &ep->com.history);
2656
	mutex_lock(&ep->com.mutex);
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
	switch (ep->com.state) {
	case MPA_REQ_WAIT:
		__state_set(&ep->com, CLOSING);
		break;
	case MPA_REQ_SENT:
		__state_set(&ep->com, CLOSING);
		connect_reply_upcall(ep, -ECONNRESET);
		break;
	case MPA_REQ_RCVD:

		/*
		 * We're gonna mark this puppy DEAD, but keep
		 * the reference on it until the ULP accepts or
		 * rejects the CR. Also wake up anyone waiting
		 * in rdma connection migration (see c4iw_accept_cr()).
		 */
		__state_set(&ep->com, CLOSING);
J
Joe Perches 已提交
2674
		pr_debug("waking up ep %p tid %u\n", ep, ep->hwtid);
2675
		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2676 2677 2678
		break;
	case MPA_REP_SENT:
		__state_set(&ep->com, CLOSING);
J
Joe Perches 已提交
2679
		pr_debug("waking up ep %p tid %u\n", ep, ep->hwtid);
2680
		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2681 2682
		break;
	case FPDU_MODE:
2683
		start_ep_timer(ep);
2684
		__state_set(&ep->com, CLOSING);
2685
		attrs.next_state = C4IW_QP_STATE_CLOSING;
S
Steve Wise 已提交
2686
		ret = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2687
				       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
S
Steve Wise 已提交
2688 2689 2690 2691
		if (ret != -ECONNRESET) {
			peer_close_upcall(ep);
			disconnect = 1;
		}
2692 2693 2694 2695 2696 2697 2698 2699 2700
		break;
	case ABORTING:
		disconnect = 0;
		break;
	case CLOSING:
		__state_set(&ep->com, MORIBUND);
		disconnect = 0;
		break;
	case MORIBUND:
S
Steve Wise 已提交
2701
		(void)stop_ep_timer(ep);
2702 2703 2704 2705 2706
		if (ep->com.cm_id && ep->com.qp) {
			attrs.next_state = C4IW_QP_STATE_IDLE;
			c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
				       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
		}
2707
		close_complete_upcall(ep, 0);
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
		__state_set(&ep->com, DEAD);
		release = 1;
		disconnect = 0;
		break;
	case DEAD:
		disconnect = 0;
		break;
	default:
		BUG_ON(1);
	}
2718
	mutex_unlock(&ep->com.mutex);
2719 2720 2721 2722
	if (disconnect)
		c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
	if (release)
		release_ep_resources(ep);
2723
	c4iw_put_ep(&ep->com);
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
	return 0;
}

static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_abort_req_rss *req = cplhdr(skb);
	struct c4iw_ep *ep;
	struct sk_buff *rpl_skb;
	struct c4iw_qp_attributes attrs;
	int ret;
	int release = 0;
	unsigned int tid = GET_TID(req);
2736
	u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
2737

2738 2739 2740 2741
	ep = get_ep_from_tid(dev, tid);
	if (!ep)
		return 0;

2742
	if (cxgb_is_neg_adv(req->status)) {
J
Joe Perches 已提交
2743 2744 2745
		pr_debug("%s Negative advice on abort- tid %u status %d (%s)\n",
			 __func__, ep->hwtid, req->status,
			 neg_adv_str(req->status));
2746 2747 2748 2749
		ep->stats.abort_neg_adv++;
		mutex_lock(&dev->rdev.stats.lock);
		dev->rdev.stats.neg_adv++;
		mutex_unlock(&dev->rdev.stats.lock);
2750
		goto deref_ep;
2751
	}
J
Joe Perches 已提交
2752 2753
	pr_debug("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
		 ep->com.state);
2754
	set_bit(PEER_ABORT, &ep->com.history);
2755 2756 2757

	/*
	 * Wake up any threads in rdma_init() or rdma_fini().
2758 2759
	 * However, this is not needed if com state is just
	 * MPA_REQ_SENT
2760
	 */
2761 2762
	if (ep->com.state != MPA_REQ_SENT)
		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2763 2764

	mutex_lock(&ep->com.mutex);
2765 2766
	switch (ep->com.state) {
	case CONNECTING:
2767
		c4iw_put_ep(&ep->parent_ep->com);
2768 2769
		break;
	case MPA_REQ_WAIT:
S
Steve Wise 已提交
2770
		(void)stop_ep_timer(ep);
2771 2772
		break;
	case MPA_REQ_SENT:
S
Steve Wise 已提交
2773
		(void)stop_ep_timer(ep);
2774
		if (mpa_rev == 1 || (mpa_rev == 2 && ep->tried_with_mpa_v1))
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
			connect_reply_upcall(ep, -ECONNRESET);
		else {
			/*
			 * we just don't send notification upwards because we
			 * want to retry with mpa_v1 without upper layers even
			 * knowing it.
			 *
			 * do some housekeeping so as to re-initiate the
			 * connection
			 */
J
Joe Perches 已提交
2785 2786
			pr_debug("%s: mpa_rev=%d. Retrying with mpav1\n",
				 __func__, mpa_rev);
2787 2788
			ep->retry_with_mpa_v1 = 1;
		}
2789 2790 2791 2792 2793 2794 2795
		break;
	case MPA_REP_SENT:
		break;
	case MPA_REQ_RCVD:
		break;
	case MORIBUND:
	case CLOSING:
2796
		stop_ep_timer(ep);
2797 2798 2799 2800 2801 2802 2803 2804
		/*FALLTHROUGH*/
	case FPDU_MODE:
		if (ep->com.cm_id && ep->com.qp) {
			attrs.next_state = C4IW_QP_STATE_ERROR;
			ret = c4iw_modify_qp(ep->com.qp->rhp,
				     ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
				     &attrs, 1);
			if (ret)
2805
				pr_err("%s - qp <- error failed!\n", __func__);
2806 2807 2808 2809 2810 2811
		}
		peer_abort_upcall(ep);
		break;
	case ABORTING:
		break;
	case DEAD:
J
Joe Perches 已提交
2812
		pr_debug("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
2813
		mutex_unlock(&ep->com.mutex);
2814
		goto deref_ep;
2815 2816 2817 2818 2819 2820 2821
	default:
		BUG_ON(1);
		break;
	}
	dst_confirm(ep->dst);
	if (ep->com.state != ABORTING) {
		__state_set(&ep->com, DEAD);
2822 2823 2824
		/* we don't release if we want to retry with mpa_v1 */
		if (!ep->retry_with_mpa_v1)
			release = 1;
2825
	}
2826
	mutex_unlock(&ep->com.mutex);
2827

2828 2829
	rpl_skb = skb_dequeue(&ep->com.ep_skb_list);
	if (WARN_ON(!rpl_skb)) {
2830 2831 2832
		release = 1;
		goto out;
	}
2833 2834 2835

	cxgb_mk_abort_rpl(rpl_skb, len, ep->hwtid, ep->txq_idx);

2836 2837 2838 2839
	c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
out:
	if (release)
		release_ep_resources(ep);
2840
	else if (ep->retry_with_mpa_v1) {
H
Hariprasad S 已提交
2841 2842 2843
		if (ep->com.remote_addr.ss_family == AF_INET6) {
			struct sockaddr_in6 *sin6 =
					(struct sockaddr_in6 *)
2844
					&ep->com.local_addr;
H
Hariprasad S 已提交
2845 2846 2847 2848 2849
			cxgb4_clip_release(
					ep->com.dev->rdev.lldi.ports[0],
					(const u32 *)&sin6->sin6_addr.s6_addr,
					1);
		}
2850
		remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid);
2851 2852 2853 2854 2855 2856
		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
		dst_release(ep->dst);
		cxgb4_l2t_release(ep->l2t);
		c4iw_reconnect(ep);
	}

2857 2858 2859 2860
deref_ep:
	c4iw_put_ep(&ep->com);
	/* Dereferencing ep, referenced in peer_abort_intr() */
	c4iw_put_ep(&ep->com);
2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
	return 0;
}

static int close_con_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct c4iw_qp_attributes attrs;
	struct cpl_close_con_rpl *rpl = cplhdr(skb);
	int release = 0;
	unsigned int tid = GET_TID(rpl);

2872 2873 2874
	ep = get_ep_from_tid(dev, tid);
	if (!ep)
		return 0;
2875

J
Joe Perches 已提交
2876
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2877 2878 2879
	BUG_ON(!ep);

	/* The cm_id may be null if we failed to connect */
2880
	mutex_lock(&ep->com.mutex);
2881
	set_bit(CLOSE_CON_RPL, &ep->com.history);
2882 2883 2884 2885 2886
	switch (ep->com.state) {
	case CLOSING:
		__state_set(&ep->com, MORIBUND);
		break;
	case MORIBUND:
S
Steve Wise 已提交
2887
		(void)stop_ep_timer(ep);
2888 2889 2890 2891 2892 2893 2894
		if ((ep->com.cm_id) && (ep->com.qp)) {
			attrs.next_state = C4IW_QP_STATE_IDLE;
			c4iw_modify_qp(ep->com.qp->rhp,
					     ep->com.qp,
					     C4IW_QP_ATTR_NEXT_STATE,
					     &attrs, 1);
		}
2895
		close_complete_upcall(ep, 0);
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905
		__state_set(&ep->com, DEAD);
		release = 1;
		break;
	case ABORTING:
	case DEAD:
		break;
	default:
		BUG_ON(1);
		break;
	}
2906
	mutex_unlock(&ep->com.mutex);
2907 2908
	if (release)
		release_ep_resources(ep);
2909
	c4iw_put_ep(&ep->com);
2910 2911 2912 2913 2914
	return 0;
}

static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
{
2915 2916 2917 2918
	struct cpl_rdma_terminate *rpl = cplhdr(skb);
	unsigned int tid = GET_TID(rpl);
	struct c4iw_ep *ep;
	struct c4iw_qp_attributes attrs;
2919

2920
	ep = get_ep_from_tid(dev, tid);
2921
	BUG_ON(!ep);
2922

2923
	if (ep && ep->com.qp) {
2924 2925
		pr_warn("TERM received tid %u qpid %u\n",
			tid, ep->com.qp->wq.sq.qid);
2926 2927 2928 2929
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
		c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
			       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
	} else
2930
		pr_warn("TERM received tid %u no ep/qp\n", tid);
2931
	c4iw_put_ep(&ep->com);
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948

	return 0;
}

/*
 * Upcall from the adapter indicating data has been transmitted.
 * For us its just the single MPA request or reply.  We can now free
 * the skb holding the mpa message.
 */
static int fw4_ack(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct cpl_fw4_ack *hdr = cplhdr(skb);
	u8 credits = hdr->credits;
	unsigned int tid = GET_TID(hdr);


2949 2950 2951
	ep = get_ep_from_tid(dev, tid);
	if (!ep)
		return 0;
J
Joe Perches 已提交
2952 2953
	pr_debug("%s ep %p tid %u credits %u\n",
		 __func__, ep, ep->hwtid, credits);
2954
	if (credits == 0) {
J
Joe Perches 已提交
2955 2956
		pr_debug("%s 0 credit ack ep %p tid %u state %u\n",
			 __func__, ep, ep->hwtid, state_read(&ep->com));
2957
		goto out;
2958 2959 2960 2961
	}

	dst_confirm(ep->dst);
	if (ep->mpa_skb) {
J
Joe Perches 已提交
2962 2963 2964
		pr_debug("%s last streaming msg ack ep %p tid %u state %u initiator %u freeing skb\n",
			 __func__, ep, ep->hwtid,
			 state_read(&ep->com), ep->mpa_attr.initiator ? 1 : 0);
2965
		mutex_lock(&ep->com.mutex);
2966 2967
		kfree_skb(ep->mpa_skb);
		ep->mpa_skb = NULL;
2968 2969 2970
		if (test_bit(STOP_MPA_TIMER, &ep->com.flags))
			stop_ep_timer(ep);
		mutex_unlock(&ep->com.mutex);
2971
	}
2972 2973
out:
	c4iw_put_ep(&ep->com);
2974 2975 2976 2977 2978
	return 0;
}

int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
{
2979
	int abort;
2980
	struct c4iw_ep *ep = to_ep(cm_id);
2981

J
Joe Perches 已提交
2982
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2983

2984
	mutex_lock(&ep->com.mutex);
2985
	if (ep->com.state != MPA_REQ_RCVD) {
2986
		mutex_unlock(&ep->com.mutex);
2987 2988 2989
		c4iw_put_ep(&ep->com);
		return -ECONNRESET;
	}
2990
	set_bit(ULP_REJECT, &ep->com.history);
2991
	if (mpa_rev == 0)
2992 2993 2994
		abort = 1;
	else
		abort = send_mpa_reject(ep, pdata, pdata_len);
2995
	mutex_unlock(&ep->com.mutex);
2996 2997 2998

	stop_ep_timer(ep);
	c4iw_ep_disconnect(ep, abort != 0, GFP_KERNEL);
2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010
	c4iw_put_ep(&ep->com);
	return 0;
}

int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
{
	int err;
	struct c4iw_qp_attributes attrs;
	enum c4iw_qp_attr_mask mask;
	struct c4iw_ep *ep = to_ep(cm_id);
	struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
	struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
3011
	int abort = 0;
3012

J
Joe Perches 已提交
3013
	pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
3014 3015

	mutex_lock(&ep->com.mutex);
3016
	if (ep->com.state != MPA_REQ_RCVD) {
3017
		err = -ECONNRESET;
3018
		goto err_out;
3019 3020 3021 3022
	}

	BUG_ON(!qp);

3023
	set_bit(ULP_ACCEPT, &ep->com.history);
3024 3025
	if ((conn_param->ord > cur_max_read_depth(ep->com.dev)) ||
	    (conn_param->ird > cur_max_read_depth(ep->com.dev))) {
3026
		err = -EINVAL;
3027
		goto err_abort;
3028 3029
	}

3030 3031
	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
		if (conn_param->ord > ep->ird) {
3032
			if (RELAXED_IRD_NEGOTIATION) {
3033
				conn_param->ord = ep->ird;
3034 3035 3036 3037 3038 3039
			} else {
				ep->ird = conn_param->ird;
				ep->ord = conn_param->ord;
				send_mpa_reject(ep, conn_param->private_data,
						conn_param->private_data_len);
				err = -ENOMEM;
3040
				goto err_abort;
3041
			}
3042
		}
3043 3044 3045 3046 3047
		if (conn_param->ird < ep->ord) {
			if (RELAXED_IRD_NEGOTIATION &&
			    ep->ord <= h->rdev.lldi.max_ordird_qp) {
				conn_param->ird = ep->ord;
			} else {
3048
				err = -ENOMEM;
3049
				goto err_abort;
3050 3051 3052
			}
		}
	}
3053 3054 3055
	ep->ird = conn_param->ird;
	ep->ord = conn_param->ord;

3056
	if (ep->mpa_attr.version == 1) {
3057 3058
		if (peer2peer && ep->ird == 0)
			ep->ird = 1;
3059 3060 3061
	} else {
		if (peer2peer &&
		    (ep->mpa_attr.p2p_type != FW_RI_INIT_P2PTYPE_DISABLED) &&
3062
		    (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) && ep->ird == 0)
3063 3064
			ep->ird = 1;
	}
3065

J
Joe Perches 已提交
3066
	pr_debug("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
3067

3068
	ep->com.cm_id = cm_id;
3069
	ref_cm_id(&ep->com);
3070
	ep->com.qp = qp;
3071
	ref_qp(ep);
3072

3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089
	/* bind QP to EP and move to RTS */
	attrs.mpa_attr = ep->mpa_attr;
	attrs.max_ird = ep->ird;
	attrs.max_ord = ep->ord;
	attrs.llp_stream_handle = ep;
	attrs.next_state = C4IW_QP_STATE_RTS;

	/* bind QP and TID with INIT_WR */
	mask = C4IW_QP_ATTR_NEXT_STATE |
			     C4IW_QP_ATTR_LLP_STREAM_HANDLE |
			     C4IW_QP_ATTR_MPA_ATTR |
			     C4IW_QP_ATTR_MAX_IRD |
			     C4IW_QP_ATTR_MAX_ORD;

	err = c4iw_modify_qp(ep->com.qp->rhp,
			     ep->com.qp, mask, &attrs, 1);
	if (err)
3090
		goto err_deref_cm_id;
3091 3092

	set_bit(STOP_MPA_TIMER, &ep->com.flags);
3093 3094 3095
	err = send_mpa_reply(ep, conn_param->private_data,
			     conn_param->private_data_len);
	if (err)
3096
		goto err_deref_cm_id;
3097

3098
	__state_set(&ep->com, FPDU_MODE);
3099
	established_upcall(ep);
3100
	mutex_unlock(&ep->com.mutex);
3101 3102
	c4iw_put_ep(&ep->com);
	return 0;
3103
err_deref_cm_id:
3104
	deref_cm_id(&ep->com);
3105 3106 3107
err_abort:
	abort = 1;
err_out:
3108
	mutex_unlock(&ep->com.mutex);
3109 3110
	if (abort)
		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
3111 3112 3113 3114
	c4iw_put_ep(&ep->com);
	return err;
}

3115 3116 3117 3118
static int pick_local_ipaddrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
{
	struct in_device *ind;
	int found = 0;
3119 3120
	struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
	struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164

	ind = in_dev_get(dev->rdev.lldi.ports[0]);
	if (!ind)
		return -EADDRNOTAVAIL;
	for_primary_ifa(ind) {
		laddr->sin_addr.s_addr = ifa->ifa_address;
		raddr->sin_addr.s_addr = ifa->ifa_address;
		found = 1;
		break;
	}
	endfor_ifa(ind);
	in_dev_put(ind);
	return found ? 0 : -EADDRNOTAVAIL;
}

static int get_lladdr(struct net_device *dev, struct in6_addr *addr,
		      unsigned char banned_flags)
{
	struct inet6_dev *idev;
	int err = -EADDRNOTAVAIL;

	rcu_read_lock();
	idev = __in6_dev_get(dev);
	if (idev != NULL) {
		struct inet6_ifaddr *ifp;

		read_lock_bh(&idev->lock);
		list_for_each_entry(ifp, &idev->addr_list, if_list) {
			if (ifp->scope == IFA_LINK &&
			    !(ifp->flags & banned_flags)) {
				memcpy(addr, &ifp->addr, 16);
				err = 0;
				break;
			}
		}
		read_unlock_bh(&idev->lock);
	}
	rcu_read_unlock();
	return err;
}

static int pick_local_ip6addrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
{
	struct in6_addr uninitialized_var(addr);
3165 3166
	struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)&cm_id->m_local_addr;
	struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)&cm_id->m_remote_addr;
3167

3168
	if (!get_lladdr(dev->rdev.lldi.ports[0], &addr, IFA_F_TENTATIVE)) {
3169 3170 3171 3172 3173 3174 3175
		memcpy(la6->sin6_addr.s6_addr, &addr, 16);
		memcpy(ra6->sin6_addr.s6_addr, &addr, 16);
		return 0;
	}
	return -EADDRNOTAVAIL;
}

3176 3177 3178 3179
int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
{
	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
	struct c4iw_ep *ep;
3180
	int err = 0;
3181 3182 3183 3184
	struct sockaddr_in *laddr;
	struct sockaddr_in *raddr;
	struct sockaddr_in6 *laddr6;
	struct sockaddr_in6 *raddr6;
3185 3186
	__u8 *ra;
	int iptype;
3187

3188 3189
	if ((conn_param->ord > cur_max_read_depth(dev)) ||
	    (conn_param->ird > cur_max_read_depth(dev))) {
3190 3191 3192
		err = -EINVAL;
		goto out;
	}
3193 3194
	ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
	if (!ep) {
3195
		pr_err("%s - cannot alloc ep\n", __func__);
3196 3197 3198
		err = -ENOMEM;
		goto out;
	}
3199 3200 3201 3202 3203 3204 3205

	skb_queue_head_init(&ep->com.ep_skb_list);
	if (alloc_ep_skb_list(&ep->com.ep_skb_list, CN_MAX_CON_BUF)) {
		err = -ENOMEM;
		goto fail1;
	}

3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217
	init_timer(&ep->timer);
	ep->plen = conn_param->private_data_len;
	if (ep->plen)
		memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
		       conn_param->private_data, ep->plen);
	ep->ird = conn_param->ird;
	ep->ord = conn_param->ord;

	if (peer2peer && ep->ord == 0)
		ep->ord = 1;

	ep->com.cm_id = cm_id;
3218 3219
	ref_cm_id(&ep->com);
	ep->com.dev = dev;
3220
	ep->com.qp = get_qhp(dev, conn_param->qpn);
3221
	if (!ep->com.qp) {
J
Joe Perches 已提交
3222
		pr_debug("%s qpn 0x%x not found!\n", __func__, conn_param->qpn);
3223
		err = -EINVAL;
3224
		goto fail2;
3225
	}
3226
	ref_qp(ep);
J
Joe Perches 已提交
3227 3228
	pr_debug("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
		 ep->com.qp, cm_id);
3229 3230 3231 3232 3233 3234

	/*
	 * Allocate an active TID to initiate a TCP connection.
	 */
	ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
	if (ep->atid == -1) {
3235
		pr_err("%s - cannot alloc atid\n", __func__);
3236
		err = -ENOMEM;
3237
		goto fail2;
3238
	}
3239
	insert_handle(dev, &dev->atid_idr, ep, ep->atid);
3240

3241
	memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3242
	       sizeof(ep->com.local_addr));
3243
	memcpy(&ep->com.remote_addr, &cm_id->m_remote_addr,
3244 3245
	       sizeof(ep->com.remote_addr));

3246 3247 3248 3249
	laddr = (struct sockaddr_in *)&ep->com.local_addr;
	raddr = (struct sockaddr_in *)&ep->com.remote_addr;
	laddr6 = (struct sockaddr_in6 *)&ep->com.local_addr;
	raddr6 = (struct sockaddr_in6 *) &ep->com.remote_addr;
3250

3251
	if (cm_id->m_remote_addr.ss_family == AF_INET) {
3252 3253
		iptype = 4;
		ra = (__u8 *)&raddr->sin_addr;
3254

3255 3256 3257
		/*
		 * Handle loopback requests to INADDR_ANY.
		 */
3258
		if (raddr->sin_addr.s_addr == htonl(INADDR_ANY)) {
3259 3260
			err = pick_local_ipaddrs(dev, cm_id);
			if (err)
3261
				goto fail2;
3262 3263 3264
		}

		/* find a route */
J
Joe Perches 已提交
3265 3266 3267
		pr_debug("%s saddr %pI4 sport 0x%x raddr %pI4 rport 0x%x\n",
			 __func__, &laddr->sin_addr, ntohs(laddr->sin_port),
			 ra, ntohs(raddr->sin_port));
3268 3269 3270 3271 3272
		ep->dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
					  laddr->sin_addr.s_addr,
					  raddr->sin_addr.s_addr,
					  laddr->sin_port,
					  raddr->sin_port, cm_id->tos);
3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
	} else {
		iptype = 6;
		ra = (__u8 *)&raddr6->sin6_addr;

		/*
		 * Handle loopback requests to INADDR_ANY.
		 */
		if (ipv6_addr_type(&raddr6->sin6_addr) == IPV6_ADDR_ANY) {
			err = pick_local_ip6addrs(dev, cm_id);
			if (err)
3283
				goto fail2;
3284 3285 3286
		}

		/* find a route */
J
Joe Perches 已提交
3287 3288 3289 3290
		pr_debug("%s saddr %pI6 sport 0x%x raddr %pI6 rport 0x%x\n",
			 __func__, laddr6->sin6_addr.s6_addr,
			 ntohs(laddr6->sin6_port),
			 raddr6->sin6_addr.s6_addr, ntohs(raddr6->sin6_port));
3291 3292 3293 3294 3295 3296
		ep->dst = cxgb_find_route6(&dev->rdev.lldi, get_real_dev,
					   laddr6->sin6_addr.s6_addr,
					   raddr6->sin6_addr.s6_addr,
					   laddr6->sin6_port,
					   raddr6->sin6_port, 0,
					   raddr6->sin6_scope_id);
3297 3298
	}
	if (!ep->dst) {
3299
		pr_err("%s - cannot find route\n", __func__);
3300
		err = -EHOSTUNREACH;
3301
		goto fail3;
3302 3303
	}

3304
	err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, true,
H
Hariprasad S 已提交
3305
			ep->com.dev->rdev.lldi.adapter_type, cm_id->tos);
3306
	if (err) {
3307
		pr_err("%s - cannot alloc l2e\n", __func__);
3308
		goto fail4;
3309 3310
	}

J
Joe Perches 已提交
3311 3312 3313
	pr_debug("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
		 __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
		 ep->l2t->idx);
3314 3315

	state_set(&ep->com, CONNECTING);
H
Hariprasad S 已提交
3316
	ep->tos = cm_id->tos;
3317 3318 3319 3320 3321 3322 3323

	/* send connect request to rnic */
	err = send_connect(ep);
	if (!err)
		goto out;

	cxgb4_l2t_release(ep->l2t);
3324
fail4:
3325
	dst_release(ep->dst);
3326
fail3:
3327
	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
3328
	cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
3329 3330
fail2:
	skb_queue_purge(&ep->com.ep_skb_list);
3331
	deref_cm_id(&ep->com);
3332
fail1:
3333 3334 3335 3336 3337
	c4iw_put_ep(&ep->com);
out:
	return err;
}

3338 3339 3340
static int create_server6(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
{
	int err;
3341
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
3342
				    &ep->com.local_addr;
3343

3344 3345 3346 3347 3348 3349
	if (ipv6_addr_type(&sin6->sin6_addr) != IPV6_ADDR_ANY) {
		err = cxgb4_clip_get(ep->com.dev->rdev.lldi.ports[0],
				     (const u32 *)&sin6->sin6_addr.s6_addr, 1);
		if (err)
			return err;
	}
3350 3351 3352 3353 3354 3355 3356 3357 3358
	c4iw_init_wr_wait(&ep->com.wr_wait);
	err = cxgb4_create_server6(ep->com.dev->rdev.lldi.ports[0],
				   ep->stid, &sin6->sin6_addr,
				   sin6->sin6_port,
				   ep->com.dev->rdev.lldi.rxq_ids[0]);
	if (!err)
		err = c4iw_wait_for_reply(&ep->com.dev->rdev,
					  &ep->com.wr_wait,
					  0, 0, __func__);
3359 3360
	else if (err > 0)
		err = net_xmit_errno(err);
3361 3362 3363
	if (err) {
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3364 3365 3366
		pr_err("cxgb4_create_server6/filter failed err %d stid %d laddr %pI6 lport %d\n",
		       err, ep->stid,
		       sin6->sin6_addr.s6_addr, ntohs(sin6->sin6_port));
3367
	}
3368 3369 3370 3371 3372 3373
	return err;
}

static int create_server4(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
{
	int err;
3374
	struct sockaddr_in *sin = (struct sockaddr_in *)
3375
				  &ep->com.local_addr;
3376 3377 3378 3379 3380 3381 3382 3383

	if (dev->rdev.lldi.enable_fw_ofld_conn) {
		do {
			err = cxgb4_create_server_filter(
				ep->com.dev->rdev.lldi.ports[0], ep->stid,
				sin->sin_addr.s_addr, sin->sin_port, 0,
				ep->com.dev->rdev.lldi.rxq_ids[0], 0, 0);
			if (err == -EBUSY) {
3384 3385 3386 3387
				if (c4iw_fatal_error(&ep->com.dev->rdev)) {
					err = -EIO;
					break;
				}
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400
				set_current_state(TASK_UNINTERRUPTIBLE);
				schedule_timeout(usecs_to_jiffies(100));
			}
		} while (err == -EBUSY);
	} else {
		c4iw_init_wr_wait(&ep->com.wr_wait);
		err = cxgb4_create_server(ep->com.dev->rdev.lldi.ports[0],
				ep->stid, sin->sin_addr.s_addr, sin->sin_port,
				0, ep->com.dev->rdev.lldi.rxq_ids[0]);
		if (!err)
			err = c4iw_wait_for_reply(&ep->com.dev->rdev,
						  &ep->com.wr_wait,
						  0, 0, __func__);
3401 3402
		else if (err > 0)
			err = net_xmit_errno(err);
3403 3404 3405 3406 3407 3408 3409 3410
	}
	if (err)
		pr_err("cxgb4_create_server/filter failed err %d stid %d laddr %pI4 lport %d\n"
		       , err, ep->stid,
		       &sin->sin_addr, ntohs(sin->sin_port));
	return err;
}

3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
int c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
{
	int err = 0;
	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
	struct c4iw_listen_ep *ep;

	might_sleep();

	ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
	if (!ep) {
3421
		pr_err("%s - cannot alloc ep\n", __func__);
3422 3423 3424
		err = -ENOMEM;
		goto fail1;
	}
3425
	skb_queue_head_init(&ep->com.ep_skb_list);
J
Joe Perches 已提交
3426
	pr_debug("%s ep %p\n", __func__, ep);
3427
	ep->com.cm_id = cm_id;
3428
	ref_cm_id(&ep->com);
3429 3430
	ep->com.dev = dev;
	ep->backlog = backlog;
3431
	memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3432
	       sizeof(ep->com.local_addr));
3433 3434 3435 3436

	/*
	 * Allocate a server TID.
	 */
3437 3438
	if (dev->rdev.lldi.enable_fw_ofld_conn &&
	    ep->com.local_addr.ss_family == AF_INET)
3439
		ep->stid = cxgb4_alloc_sftid(dev->rdev.lldi.tids,
3440
					     cm_id->m_local_addr.ss_family, ep);
3441
	else
3442
		ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids,
3443
					    cm_id->m_local_addr.ss_family, ep);
3444

3445
	if (ep->stid == -1) {
3446
		pr_err("%s - cannot alloc stid\n", __func__);
3447 3448 3449
		err = -ENOMEM;
		goto fail2;
	}
3450
	insert_handle(dev, &dev->stid_idr, ep, ep->stid);
3451

3452 3453
	memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
	       sizeof(ep->com.local_addr));
3454

3455
	state_set(&ep->com, LISTEN);
3456 3457 3458 3459
	if (ep->com.local_addr.ss_family == AF_INET)
		err = create_server4(dev, ep);
	else
		err = create_server6(dev, ep);
3460 3461 3462 3463
	if (!err) {
		cm_id->provider_data = ep;
		goto out;
	}
3464

3465 3466
	cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
			ep->com.local_addr.ss_family);
3467
fail2:
3468
	deref_cm_id(&ep->com);
3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479
	c4iw_put_ep(&ep->com);
fail1:
out:
	return err;
}

int c4iw_destroy_listen(struct iw_cm_id *cm_id)
{
	int err;
	struct c4iw_listen_ep *ep = to_listen_ep(cm_id);

J
Joe Perches 已提交
3480
	pr_debug("%s ep %p\n", __func__, ep);
3481 3482 3483

	might_sleep();
	state_set(&ep->com, DEAD);
3484 3485
	if (ep->com.dev->rdev.lldi.enable_fw_ofld_conn &&
	    ep->com.local_addr.ss_family == AF_INET) {
3486 3487 3488 3489
		err = cxgb4_remove_server_filter(
			ep->com.dev->rdev.lldi.ports[0], ep->stid,
			ep->com.dev->rdev.lldi.rxq_ids[0], 0);
	} else {
H
Hariprasad S 已提交
3490
		struct sockaddr_in6 *sin6;
3491
		c4iw_init_wr_wait(&ep->com.wr_wait);
3492 3493 3494
		err = cxgb4_remove_server(
				ep->com.dev->rdev.lldi.ports[0], ep->stid,
				ep->com.dev->rdev.lldi.rxq_ids[0], 0);
3495 3496 3497 3498
		if (err)
			goto done;
		err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait,
					  0, 0, __func__);
3499
		sin6 = (struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
3500 3501
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3502
	}
3503
	remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
3504 3505
	cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
			ep->com.local_addr.ss_family);
3506
done:
3507
	deref_cm_id(&ep->com);
3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518
	c4iw_put_ep(&ep->com);
	return err;
}

int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
{
	int ret = 0;
	int close = 0;
	int fatal = 0;
	struct c4iw_rdev *rdev;

3519
	mutex_lock(&ep->com.mutex);
3520

J
Joe Perches 已提交
3521 3522
	pr_debug("%s ep %p state %s, abrupt %d\n", __func__, ep,
		 states[ep->com.state], abrupt);
3523

3524 3525 3526 3527 3528 3529
	/*
	 * Ref the ep here in case we have fatal errors causing the
	 * ep to be released and freed.
	 */
	c4iw_get_ep(&ep->com);

3530 3531 3532
	rdev = &ep->com.dev->rdev;
	if (c4iw_fatal_error(rdev)) {
		fatal = 1;
3533
		close_complete_upcall(ep, -EIO);
3534 3535 3536 3537 3538 3539 3540 3541
		ep->com.state = DEAD;
	}
	switch (ep->com.state) {
	case MPA_REQ_WAIT:
	case MPA_REQ_SENT:
	case MPA_REQ_RCVD:
	case MPA_REP_SENT:
	case FPDU_MODE:
3542
	case CONNECTING:
3543 3544 3545 3546 3547
		close = 1;
		if (abrupt)
			ep->com.state = ABORTING;
		else {
			ep->com.state = CLOSING;
3548 3549 3550 3551 3552 3553 3554 3555 3556 3557

			/*
			 * if we close before we see the fw4_ack() then we fix
			 * up the timer state since we're reusing it.
			 */
			if (ep->mpa_skb &&
			    test_bit(STOP_MPA_TIMER, &ep->com.flags)) {
				clear_bit(STOP_MPA_TIMER, &ep->com.flags);
				stop_ep_timer(ep);
			}
3558
			start_ep_timer(ep);
3559 3560 3561 3562 3563 3564 3565
		}
		set_bit(CLOSE_SENT, &ep->com.flags);
		break;
	case CLOSING:
		if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
			close = 1;
			if (abrupt) {
S
Steve Wise 已提交
3566
				(void)stop_ep_timer(ep);
3567 3568 3569 3570 3571 3572 3573 3574
				ep->com.state = ABORTING;
			} else
				ep->com.state = MORIBUND;
		}
		break;
	case MORIBUND:
	case ABORTING:
	case DEAD:
J
Joe Perches 已提交
3575 3576
		pr_debug("%s ignoring disconnect ep %p state %u\n",
			 __func__, ep, ep->com.state);
3577 3578 3579 3580 3581 3582 3583
		break;
	default:
		BUG();
		break;
	}

	if (close) {
S
Steve Wise 已提交
3584
		if (abrupt) {
3585
			set_bit(EP_DISC_ABORT, &ep->com.history);
3586
			close_complete_upcall(ep, -ECONNRESET);
3587
			ret = send_abort(ep);
3588 3589
		} else {
			set_bit(EP_DISC_CLOSE, &ep->com.history);
3590
			ret = send_halfclose(ep);
3591
		}
3592
		if (ret) {
3593
			set_bit(EP_DISC_FAIL, &ep->com.history);
3594 3595 3596 3597
			if (!abrupt) {
				stop_ep_timer(ep);
				close_complete_upcall(ep, -EIO);
			}
3598 3599 3600 3601 3602 3603 3604 3605 3606
			if (ep->com.qp) {
				struct c4iw_qp_attributes attrs;

				attrs.next_state = C4IW_QP_STATE_ERROR;
				ret = c4iw_modify_qp(ep->com.qp->rhp,
						     ep->com.qp,
						     C4IW_QP_ATTR_NEXT_STATE,
						     &attrs, 1);
				if (ret)
3607
					pr_err("%s - qp <- error failed!\n",
3608 3609
					       __func__);
			}
3610
			fatal = 1;
3611
		}
3612
	}
S
Steve Wise 已提交
3613
	mutex_unlock(&ep->com.mutex);
3614
	c4iw_put_ep(&ep->com);
3615 3616 3617 3618 3619
	if (fatal)
		release_ep_resources(ep);
	return ret;
}

3620 3621 3622 3623
static void active_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
			struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
{
	struct c4iw_ep *ep;
3624
	int atid = be32_to_cpu(req->tid);
3625

3626 3627
	ep = (struct c4iw_ep *)lookup_atid(dev->rdev.lldi.tids,
					   (__force u32) req->tid);
3628 3629 3630 3631 3632
	if (!ep)
		return;

	switch (req->retval) {
	case FW_ENOMEM:
3633 3634 3635 3636 3637
		set_bit(ACT_RETRY_NOMEM, &ep->com.history);
		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
			send_fw_act_open_req(ep, atid);
			return;
		}
3638
	case FW_EADDRINUSE:
3639 3640 3641 3642 3643
		set_bit(ACT_RETRY_INUSE, &ep->com.history);
		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
			send_fw_act_open_req(ep, atid);
			return;
		}
3644 3645 3646 3647 3648 3649
		break;
	default:
		pr_info("%s unexpected ofld conn wr retval %d\n",
		       __func__, req->retval);
		break;
	}
3650 3651 3652 3653 3654
	pr_err("active ofld_connect_wr failure %d atid %d\n",
	       req->retval, atid);
	mutex_lock(&dev->rdev.stats.lock);
	dev->rdev.stats.act_ofld_conn_fails++;
	mutex_unlock(&dev->rdev.stats.lock);
3655
	connect_reply_upcall(ep, status2errno(req->retval));
3656
	state_set(&ep->com, DEAD);
H
Hariprasad S 已提交
3657 3658
	if (ep->com.remote_addr.ss_family == AF_INET6) {
		struct sockaddr_in6 *sin6 =
3659
			(struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
3660 3661 3662
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
3663 3664 3665 3666 3667
	remove_handle(dev, &dev->atid_idr, atid);
	cxgb4_free_atid(dev->rdev.lldi.tids, atid);
	dst_release(ep->dst);
	cxgb4_l2t_release(ep->l2t);
	c4iw_put_ep(&ep->com);
3668 3669 3670 3671 3672 3673 3674 3675 3676
}

static void passive_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
			struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
{
	struct sk_buff *rpl_skb;
	struct cpl_pass_accept_req *cpl;
	int ret;

3677
	rpl_skb = (struct sk_buff *)(unsigned long)req->cookie;
3678 3679
	BUG_ON(!rpl_skb);
	if (req->retval) {
J
Joe Perches 已提交
3680
		pr_debug("%s passive open failure %d\n", __func__, req->retval);
3681 3682 3683
		mutex_lock(&dev->rdev.stats.lock);
		dev->rdev.stats.pas_ofld_conn_fails++;
		mutex_unlock(&dev->rdev.stats.lock);
3684 3685 3686 3687
		kfree_skb(rpl_skb);
	} else {
		cpl = (struct cpl_pass_accept_req *)cplhdr(rpl_skb);
		OPCODE_TID(cpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ,
3688 3689
					(__force u32) htonl(
					(__force u32) req->tid)));
3690 3691 3692 3693 3694 3695 3696 3697
		ret = pass_accept_req(dev, rpl_skb);
		if (!ret)
			kfree_skb(rpl_skb);
	}
	return;
}

static int deferred_fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
3698 3699
{
	struct cpl_fw6_msg *rpl = cplhdr(skb);
3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
	struct cpl_fw6_msg_ofld_connection_wr_rpl *req;

	switch (rpl->type) {
	case FW6_TYPE_CQE:
		c4iw_ev_dispatch(dev, (struct t4_cqe *)&rpl->data[0]);
		break;
	case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
		req = (struct cpl_fw6_msg_ofld_connection_wr_rpl *)rpl->data;
		switch (req->t_state) {
		case TCP_SYN_SENT:
			active_ofld_conn_reply(dev, skb, req);
			break;
		case TCP_SYN_RECV:
			passive_ofld_conn_reply(dev, skb, req);
			break;
		default:
			pr_err("%s unexpected ofld conn wr state %d\n",
			       __func__, req->t_state);
			break;
		}
		break;
	}
	return 0;
}

static void build_cpl_pass_accept_req(struct sk_buff *skb, int stid , u8 tos)
{
3727 3728 3729 3730
	__be32 l2info;
	__be16 hdr_len, vlantag, len;
	u16 eth_hdr_len;
	int tcp_hdr_len, ip_hdr_len;
3731 3732 3733 3734
	u8 intf;
	struct cpl_rx_pkt *cpl = cplhdr(skb);
	struct cpl_pass_accept_req *req;
	struct tcp_options_received tmp_opt;
3735
	struct c4iw_dev *dev;
3736
	enum chip_type type;
3737

3738
	dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
3739
	/* Store values from cpl_rx_pkt in temporary location. */
3740 3741 3742 3743
	vlantag = cpl->vlan;
	len = cpl->len;
	l2info  = cpl->l2info;
	hdr_len = cpl->hdr_len;
3744 3745 3746 3747 3748 3749 3750 3751 3752 3753
	intf = cpl->iff;

	__skb_pull(skb, sizeof(*req) + sizeof(struct rss_header));

	/*
	 * We need to parse the TCP options from SYN packet.
	 * to generate cpl_pass_accept_req.
	 */
	memset(&tmp_opt, 0, sizeof(tmp_opt));
	tcp_clear_options(&tmp_opt);
C
Christoph Paasch 已提交
3754
	tcp_parse_options(skb, &tmp_opt, 0, NULL);
3755 3756 3757

	req = (struct cpl_pass_accept_req *)__skb_push(skb, sizeof(*req));
	memset(req, 0, sizeof(*req));
3758 3759
	req->l2info = cpu_to_be16(SYN_INTF_V(intf) |
			 SYN_MAC_IDX_V(RX_MACIDX_G(
3760
			 be32_to_cpu(l2info))) |
3761
			 SYN_XACT_MATCH_F);
3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781
	type = dev->rdev.lldi.adapter_type;
	tcp_hdr_len = RX_TCPHDR_LEN_G(be16_to_cpu(hdr_len));
	ip_hdr_len = RX_IPHDR_LEN_G(be16_to_cpu(hdr_len));
	req->hdr_len =
		cpu_to_be32(SYN_RX_CHAN_V(RX_CHAN_G(be32_to_cpu(l2info))));
	if (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) {
		eth_hdr_len = is_t4(type) ?
				RX_ETHHDR_LEN_G(be32_to_cpu(l2info)) :
				RX_T5_ETHHDR_LEN_G(be32_to_cpu(l2info));
		req->hdr_len |= cpu_to_be32(TCP_HDR_LEN_V(tcp_hdr_len) |
					    IP_HDR_LEN_V(ip_hdr_len) |
					    ETH_HDR_LEN_V(eth_hdr_len));
	} else { /* T6 and later */
		eth_hdr_len = RX_T6_ETHHDR_LEN_G(be32_to_cpu(l2info));
		req->hdr_len |= cpu_to_be32(T6_TCP_HDR_LEN_V(tcp_hdr_len) |
					    T6_IP_HDR_LEN_V(ip_hdr_len) |
					    T6_ETH_HDR_LEN_V(eth_hdr_len));
	}
	req->vlan = vlantag;
	req->len = len;
3782 3783
	req->tos_stid = cpu_to_be32(PASS_OPEN_TID_V(stid) |
				    PASS_OPEN_TOS_V(tos));
3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802
	req->tcpopt.mss = htons(tmp_opt.mss_clamp);
	if (tmp_opt.wscale_ok)
		req->tcpopt.wsf = tmp_opt.snd_wscale;
	req->tcpopt.tstamp = tmp_opt.saw_tstamp;
	if (tmp_opt.sack_ok)
		req->tcpopt.sack = 1;
	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ, 0));
	return;
}

static void send_fw_pass_open_req(struct c4iw_dev *dev, struct sk_buff *skb,
				  __be32 laddr, __be16 lport,
				  __be32 raddr, __be16 rport,
				  u32 rcv_isn, u32 filter, u16 window,
				  u32 rss_qid, u8 port_id)
{
	struct sk_buff *req_skb;
	struct fw_ofld_connection_wr *req;
	struct cpl_pass_accept_req *cpl = cplhdr(skb);
3803
	int ret;
3804 3805

	req_skb = alloc_skb(sizeof(struct fw_ofld_connection_wr), GFP_KERNEL);
3806 3807
	if (!req_skb)
		return;
3808 3809
	req = (struct fw_ofld_connection_wr *)__skb_put(req_skb, sizeof(*req));
	memset(req, 0, sizeof(*req));
3810
	req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR) | FW_WR_COMPL_F);
3811
	req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
3812
	req->le.version_cpl = htonl(FW_OFLD_CONNECTION_WR_CPL_F);
3813
	req->le.filter = (__force __be32) filter;
3814 3815 3816 3817 3818 3819 3820
	req->le.lport = lport;
	req->le.pport = rport;
	req->le.u.ipv4.lip = laddr;
	req->le.u.ipv4.pip = raddr;
	req->tcb.rcv_nxt = htonl(rcv_isn + 1);
	req->tcb.rcv_adv = htons(window);
	req->tcb.t_state_to_astid =
3821 3822 3823
		 htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_RECV) |
			FW_OFLD_CONNECTION_WR_RCV_SCALE_V(cpl->tcpopt.wsf) |
			FW_OFLD_CONNECTION_WR_ASTID_V(
3824
			PASS_OPEN_TID_G(ntohl(cpl->tos_stid))));
3825 3826 3827 3828 3829

	/*
	 * We store the qid in opt2 which will be used by the firmware
	 * to send us the wr response.
	 */
3830
	req->tcb.opt2 = htonl(RSS_QUEUE_V(rss_qid));
3831 3832 3833 3834 3835 3836 3837

	/*
	 * We initialize the MSS index in TCB to 0xF.
	 * So that when driver sends cpl_pass_accept_rpl
	 * TCB picks up the correct value. If this was 0
	 * TP will ignore any value > 0 for MSS index.
	 */
3838
	req->tcb.opt0 = cpu_to_be64(MSS_IDX_V(0xF));
H
Hariprasad S 已提交
3839
	req->cookie = (uintptr_t)skb;
3840 3841

	set_wr_txq(req_skb, CPL_PRIORITY_CONTROL, port_id);
3842 3843 3844 3845 3846 3847 3848
	ret = cxgb4_ofld_send(dev->rdev.lldi.ports[0], req_skb);
	if (ret < 0) {
		pr_err("%s - cxgb4_ofld_send error %d - dropping\n", __func__,
		       ret);
		kfree_skb(skb);
		kfree_skb(req_skb);
	}
3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870
}

/*
 * Handler for CPL_RX_PKT message. Need to handle cpl_rx_pkt
 * messages when a filter is being used instead of server to
 * redirect a syn packet. When packets hit filter they are redirected
 * to the offload queue and driver tries to establish the connection
 * using firmware work request.
 */
static int rx_pkt(struct c4iw_dev *dev, struct sk_buff *skb)
{
	int stid;
	unsigned int filter;
	struct ethhdr *eh = NULL;
	struct vlan_ethhdr *vlan_eh = NULL;
	struct iphdr *iph;
	struct tcphdr *tcph;
	struct rss_header *rss = (void *)skb->data;
	struct cpl_rx_pkt *cpl = (void *)skb->data;
	struct cpl_pass_accept_req *req = (void *)(rss + 1);
	struct l2t_entry *e;
	struct dst_entry *dst;
3871
	struct c4iw_ep *lep = NULL;
3872 3873 3874
	u16 window;
	struct port_info *pi;
	struct net_device *pdev;
3875
	u16 rss_qid, eth_hdr_len;
3876 3877 3878 3879 3880
	int step;
	u32 tx_chan;
	struct neighbour *neigh;

	/* Drop all non-SYN packets */
3881
	if (!(cpl->l2info & cpu_to_be32(RXF_SYN_F)))
3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893
		goto reject;

	/*
	 * Drop all packets which did not hit the filter.
	 * Unlikely to happen.
	 */
	if (!(rss->filter_hit && rss->filter_tid))
		goto reject;

	/*
	 * Calculate the server tid from filter hit index from cpl_rx_pkt.
	 */
3894
	stid = (__force int) cpu_to_be32((__force u32) rss->hash_val);
3895

3896
	lep = (struct c4iw_ep *)get_ep_from_stid(dev, stid);
3897
	if (!lep) {
J
Joe Perches 已提交
3898 3899
		pr_debug("%s connect request on invalid stid %d\n",
			 __func__, stid);
3900 3901 3902
		goto reject;
	}

3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918
	switch (CHELSIO_CHIP_VERSION(dev->rdev.lldi.adapter_type)) {
	case CHELSIO_T4:
		eth_hdr_len = RX_ETHHDR_LEN_G(be32_to_cpu(cpl->l2info));
		break;
	case CHELSIO_T5:
		eth_hdr_len = RX_T5_ETHHDR_LEN_G(be32_to_cpu(cpl->l2info));
		break;
	case CHELSIO_T6:
		eth_hdr_len = RX_T6_ETHHDR_LEN_G(be32_to_cpu(cpl->l2info));
		break;
	default:
		pr_err("T%d Chip is not supported\n",
		       CHELSIO_CHIP_VERSION(dev->rdev.lldi.adapter_type));
		goto reject;
	}

3919
	if (eth_hdr_len == ETH_HLEN) {
3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935
		eh = (struct ethhdr *)(req + 1);
		iph = (struct iphdr *)(eh + 1);
	} else {
		vlan_eh = (struct vlan_ethhdr *)(req + 1);
		iph = (struct iphdr *)(vlan_eh + 1);
		skb->vlan_tci = ntohs(cpl->vlan);
	}

	if (iph->version != 0x4)
		goto reject;

	tcph = (struct tcphdr *)(iph + 1);
	skb_set_network_header(skb, (void *)iph - (void *)rss);
	skb_set_transport_header(skb, (void *)tcph - (void *)rss);
	skb_get(skb);

J
Joe Perches 已提交
3936 3937 3938
	pr_debug("%s lip 0x%x lport %u pip 0x%x pport %u tos %d\n", __func__,
		 ntohl(iph->daddr), ntohs(tcph->dest), ntohl(iph->saddr),
		 ntohs(tcph->source), iph->tos);
3939

3940 3941 3942
	dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
			      iph->daddr, iph->saddr, tcph->dest,
			      tcph->source, iph->tos);
3943
	if (!dst) {
3944 3945 3946 3947 3948 3949
		pr_err("%s - failed to find dst entry!\n",
		       __func__);
		goto reject;
	}
	neigh = dst_neigh_lookup_skb(dst, skb);

3950 3951 3952 3953 3954 3955
	if (!neigh) {
		pr_err("%s - failed to allocate neigh!\n",
		       __func__);
		goto free_dst;
	}

3956 3957 3958 3959 3960 3961 3962 3963
	if (neigh->dev->flags & IFF_LOOPBACK) {
		pdev = ip_dev_find(&init_net, iph->daddr);
		e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
				    pdev, 0);
		pi = (struct port_info *)netdev_priv(pdev);
		tx_chan = cxgb4_port_chan(pdev);
		dev_put(pdev);
	} else {
3964
		pdev = get_real_dev(neigh->dev);
3965
		e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3966 3967 3968
					pdev, 0);
		pi = (struct port_info *)netdev_priv(pdev);
		tx_chan = cxgb4_port_chan(pdev);
3969
	}
3970
	neigh_release(neigh);
3971 3972 3973 3974 3975 3976 3977 3978
	if (!e) {
		pr_err("%s - failed to allocate l2t entry!\n",
		       __func__);
		goto free_dst;
	}

	step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
	rss_qid = dev->rdev.lldi.rxq_ids[pi->port_id * step];
3979
	window = (__force u16) htons((__force u16)tcph->window);
3980 3981

	/* Calcuate filter portion for LE region. */
3982 3983 3984
	filter = (__force unsigned int) cpu_to_be32(cxgb4_select_ntuple(
						    dev->rdev.lldi.ports[0],
						    e));
3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998

	/*
	 * Synthesize the cpl_pass_accept_req. We have everything except the
	 * TID. Once firmware sends a reply with TID we update the TID field
	 * in cpl and pass it through the regular cpl_pass_accept_req path.
	 */
	build_cpl_pass_accept_req(skb, stid, iph->tos);
	send_fw_pass_open_req(dev, skb, iph->daddr, tcph->dest, iph->saddr,
			      tcph->source, ntohl(tcph->seq), filter, window,
			      rss_qid, pi->port_id);
	cxgb4_l2t_release(e);
free_dst:
	dst_release(dst);
reject:
3999 4000
	if (lep)
		c4iw_put_ep(&lep->com);
4001 4002 4003
	return 0;
}

4004 4005 4006 4007
/*
 * These are the real handlers that are called from a
 * work queue.
 */
4008
static c4iw_handler_func work_handlers[NUM_CPL_CMDS + NUM_FAKE_CPLS] = {
4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021
	[CPL_ACT_ESTABLISH] = act_establish,
	[CPL_ACT_OPEN_RPL] = act_open_rpl,
	[CPL_RX_DATA] = rx_data,
	[CPL_ABORT_RPL_RSS] = abort_rpl,
	[CPL_ABORT_RPL] = abort_rpl,
	[CPL_PASS_OPEN_RPL] = pass_open_rpl,
	[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl,
	[CPL_PASS_ACCEPT_REQ] = pass_accept_req,
	[CPL_PASS_ESTABLISH] = pass_establish,
	[CPL_PEER_CLOSE] = peer_close,
	[CPL_ABORT_REQ_RSS] = peer_abort,
	[CPL_CLOSE_CON_RPL] = close_con_rpl,
	[CPL_RDMA_TERMINATE] = terminate,
4022
	[CPL_FW4_ACK] = fw4_ack,
4023
	[CPL_FW6_MSG] = deferred_fw6_msg,
4024
	[CPL_RX_PKT] = rx_pkt,
4025 4026
	[FAKE_CPL_PUT_EP_SAFE] = _put_ep_safe,
	[FAKE_CPL_PASS_PUT_EP_SAFE] = _put_pass_ep_safe
4027 4028 4029 4030 4031 4032 4033
};

static void process_timeout(struct c4iw_ep *ep)
{
	struct c4iw_qp_attributes attrs;
	int abort = 1;

4034
	mutex_lock(&ep->com.mutex);
J
Joe Perches 已提交
4035 4036
	pr_debug("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
		 ep->com.state);
4037
	set_bit(TIMEDOUT, &ep->com.history);
4038 4039 4040 4041 4042
	switch (ep->com.state) {
	case MPA_REQ_SENT:
		connect_reply_upcall(ep, -ETIMEDOUT);
		break;
	case MPA_REQ_WAIT:
4043
	case MPA_REQ_RCVD:
4044
	case MPA_REP_SENT:
4045
	case FPDU_MODE:
4046 4047 4048 4049 4050 4051 4052 4053 4054
		break;
	case CLOSING:
	case MORIBUND:
		if (ep->com.cm_id && ep->com.qp) {
			attrs.next_state = C4IW_QP_STATE_ERROR;
			c4iw_modify_qp(ep->com.qp->rhp,
				     ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
				     &attrs, 1);
		}
4055
		close_complete_upcall(ep, -ETIMEDOUT);
4056
		break;
S
Steve Wise 已提交
4057 4058 4059 4060 4061 4062 4063 4064 4065 4066
	case ABORTING:
	case DEAD:

		/*
		 * These states are expected if the ep timed out at the same
		 * time as another thread was calling stop_ep_timer().
		 * So we silently do nothing for these states.
		 */
		abort = 0;
		break;
4067
	default:
J
Julia Lawall 已提交
4068
		WARN(1, "%s unexpected state ep %p tid %u state %u\n",
4069 4070 4071
			__func__, ep, ep->hwtid, ep->com.state);
		abort = 0;
	}
4072
	mutex_unlock(&ep->com.mutex);
4073 4074
	if (abort)
		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087
	c4iw_put_ep(&ep->com);
}

static void process_timedout_eps(void)
{
	struct c4iw_ep *ep;

	spin_lock_irq(&timeout_lock);
	while (!list_empty(&timeout_list)) {
		struct list_head *tmp;

		tmp = timeout_list.next;
		list_del(tmp);
S
Steve Wise 已提交
4088 4089
		tmp->next = NULL;
		tmp->prev = NULL;
4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101
		spin_unlock_irq(&timeout_lock);
		ep = list_entry(tmp, struct c4iw_ep, entry);
		process_timeout(ep);
		spin_lock_irq(&timeout_lock);
	}
	spin_unlock_irq(&timeout_lock);
}

static void process_work(struct work_struct *work)
{
	struct sk_buff *skb = NULL;
	struct c4iw_dev *dev;
4102
	struct cpl_act_establish *rpl;
4103 4104 4105
	unsigned int opcode;
	int ret;

S
Steve Wise 已提交
4106
	process_timedout_eps();
4107 4108 4109 4110 4111 4112 4113 4114 4115
	while ((skb = skb_dequeue(&rxq))) {
		rpl = cplhdr(skb);
		dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
		opcode = rpl->ot.opcode;

		BUG_ON(!work_handlers[opcode]);
		ret = work_handlers[opcode](dev, skb);
		if (!ret)
			kfree_skb(skb);
S
Steve Wise 已提交
4116
		process_timedout_eps();
4117 4118 4119 4120 4121 4122 4123 4124
	}
}

static DECLARE_WORK(skb_work, process_work);

static void ep_timeout(unsigned long arg)
{
	struct c4iw_ep *ep = (struct c4iw_ep *)arg;
4125
	int kickit = 0;
4126 4127

	spin_lock(&timeout_lock);
4128
	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
S
Steve Wise 已提交
4129 4130 4131 4132 4133 4134 4135
		/*
		 * Only insert if it is not already on the list.
		 */
		if (!ep->entry.next) {
			list_add_tail(&ep->entry, &timeout_list);
			kickit = 1;
		}
4136
	}
4137
	spin_unlock(&timeout_lock);
4138 4139
	if (kickit)
		queue_work(workq, &skb_work);
4140 4141
}

4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165
/*
 * All the CM events are handled on a work queue to have a safe context.
 */
static int sched(struct c4iw_dev *dev, struct sk_buff *skb)
{

	/*
	 * Save dev in the skb->cb area.
	 */
	*((struct c4iw_dev **) (skb->cb + sizeof(void *))) = dev;

	/*
	 * Queue the skb and schedule the worker thread.
	 */
	skb_queue_tail(&rxq, skb);
	queue_work(workq, &skb_work);
	return 0;
}

static int set_tcb_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_set_tcb_rpl *rpl = cplhdr(skb);

	if (rpl->status != CPL_ERR_NONE) {
4166 4167
		pr_err("Unexpected SET_TCB_RPL status %u for tid %u\n",
		       rpl->status, GET_TID(rpl));
4168
	}
4169
	kfree_skb(skb);
4170 4171 4172
	return 0;
}

4173 4174 4175 4176 4177 4178
static int fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_fw6_msg *rpl = cplhdr(skb);
	struct c4iw_wr_wait *wr_waitp;
	int ret;

J
Joe Perches 已提交
4179
	pr_debug("%s type %u\n", __func__, rpl->type);
4180 4181

	switch (rpl->type) {
4182
	case FW6_TYPE_WR_RPL:
4183
		ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
4184
		wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
J
Joe Perches 已提交
4185
		pr_debug("%s wr_waitp %p ret %u\n", __func__, wr_waitp, ret);
4186 4187
		if (wr_waitp)
			c4iw_wake_up(wr_waitp, ret ? -ret : 0);
4188
		kfree_skb(skb);
4189
		break;
4190 4191
	case FW6_TYPE_CQE:
	case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
4192
		sched(dev, skb);
4193
		break;
4194
	default:
4195 4196
		pr_err("%s unexpected fw6 msg type %u\n",
		       __func__, rpl->type);
4197
		kfree_skb(skb);
4198 4199 4200 4201 4202
		break;
	}
	return 0;
}

S
Steve Wise 已提交
4203 4204 4205 4206 4207 4208
static int peer_abort_intr(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_abort_req_rss *req = cplhdr(skb);
	struct c4iw_ep *ep;
	unsigned int tid = GET_TID(req);

4209 4210
	ep = get_ep_from_tid(dev, tid);
	/* This EP will be dereferenced in peer_abort() */
4211
	if (!ep) {
4212
		pr_warn("Abort on non-existent endpoint, tid %d\n", tid);
4213 4214 4215
		kfree_skb(skb);
		return 0;
	}
4216
	if (cxgb_is_neg_adv(req->status)) {
J
Joe Perches 已提交
4217 4218 4219
		pr_debug("%s Negative advice on abort- tid %u status %d (%s)\n",
			 __func__, ep->hwtid, req->status,
			 neg_adv_str(req->status));
4220
		goto out;
S
Steve Wise 已提交
4221
	}
J
Joe Perches 已提交
4222 4223
	pr_debug("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
		 ep->com.state);
S
Steve Wise 已提交
4224

4225
	c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
4226
out:
S
Steve Wise 已提交
4227 4228 4229 4230
	sched(dev, skb);
	return 0;
}

4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246
/*
 * Most upcalls from the T4 Core go to sched() to
 * schedule the processing on a work queue.
 */
c4iw_handler_func c4iw_handlers[NUM_CPL_CMDS] = {
	[CPL_ACT_ESTABLISH] = sched,
	[CPL_ACT_OPEN_RPL] = sched,
	[CPL_RX_DATA] = sched,
	[CPL_ABORT_RPL_RSS] = sched,
	[CPL_ABORT_RPL] = sched,
	[CPL_PASS_OPEN_RPL] = sched,
	[CPL_CLOSE_LISTSRV_RPL] = sched,
	[CPL_PASS_ACCEPT_REQ] = sched,
	[CPL_PASS_ESTABLISH] = sched,
	[CPL_PEER_CLOSE] = sched,
	[CPL_CLOSE_CON_RPL] = sched,
S
Steve Wise 已提交
4247
	[CPL_ABORT_REQ_RSS] = peer_abort_intr,
4248 4249 4250
	[CPL_RDMA_TERMINATE] = sched,
	[CPL_FW4_ACK] = sched,
	[CPL_SET_TCB_RPL] = set_tcb_rpl,
4251 4252
	[CPL_FW6_MSG] = fw6_msg,
	[CPL_RX_PKT] = sched
4253 4254
};

4255 4256
int __init c4iw_cm_init(void)
{
4257
	spin_lock_init(&timeout_lock);
4258 4259
	skb_queue_head_init(&rxq);

4260
	workq = alloc_ordered_workqueue("iw_cxgb4", WQ_MEM_RECLAIM);
4261 4262 4263 4264 4265 4266
	if (!workq)
		return -ENOMEM;

	return 0;
}

4267
void c4iw_cm_term(void)
4268
{
4269
	WARN_ON(!list_empty(&timeout_list));
4270 4271 4272
	flush_workqueue(workq);
	destroy_workqueue(workq);
}