cm.c 118.0 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
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)");

102
static int peer2peer = 1;
103
module_param(peer2peer, int, 0644);
104
MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=1)");
105 106 107 108 109 110 111 112 113 114 115

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

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

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

134
static int snd_win = 128 * 1024;
135
module_param(snd_win, int, 0644);
136
MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=128KB)");
137 138 139 140 141 142

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);
143
static void ep_timeout(struct timer_list *t);
144
static void connect_reply_upcall(struct c4iw_ep *ep, int status);
145
static int sched(struct c4iw_dev *dev, struct sk_buff *skb);
146

147 148 149
static LIST_HEAD(timeout_list);
static spinlock_t timeout_lock;

150 151 152 153 154 155 156 157 158 159 160 161 162
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);
}

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

static void ref_qp(struct c4iw_ep *ep)
{
	set_bit(QP_REFERENCED, &ep->com.flags);
173
	set_bit(QP_REFED, &ep->com.history);
174 175 176
	c4iw_qp_add_ref(&ep->com.qp->ibqp);
}

177 178
static void start_ep_timer(struct c4iw_ep *ep)
{
179
	pr_debug("ep %p\n", ep);
180
	if (timer_pending(&ep->timer)) {
181 182 183 184 185 186
		pr_err("%s timer already started! ep %p\n",
		       __func__, ep);
		return;
	}
	clear_bit(TIMEOUT, &ep->com.flags);
	c4iw_get_ep(&ep->com);
187 188 189 190
	ep->timer.expires = jiffies + ep_timeout_secs * HZ;
	add_timer(&ep->timer);
}

S
Steve Wise 已提交
191
static int stop_ep_timer(struct c4iw_ep *ep)
192
{
193
	pr_debug("ep %p stopping\n", ep);
194
	del_timer_sync(&ep->timer);
S
Steve Wise 已提交
195
	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
196
		c4iw_put_ep(&ep->com);
S
Steve Wise 已提交
197 198 199
		return 0;
	}
	return 1;
200 201 202 203 204 205 206 207 208
}

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);
209
		pr_err("%s - device in error state - dropping\n", __func__);
210 211 212 213 214
		return -EIO;
	}
	error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
	if (error < 0)
		kfree_skb(skb);
215 216
	else if (error == NET_XMIT_DROP)
		return -ENOMEM;
217
	return error < 0 ? error : 0;
218 219 220 221 222 223 224 225
}

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

	if (c4iw_fatal_error(rdev)) {
		kfree_skb(skb);
226
		pr_err("%s - device in error state - dropping\n", __func__);
227 228 229 230 231
		return -EIO;
	}
	error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
	if (error < 0)
		kfree_skb(skb);
232
	return error < 0 ? error : 0;
233 234 235 236
}

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

239
	skb = get_skb(skb, len, GFP_KERNEL);
240 241
	if (!skb)
		return;
242 243

	cxgb_mk_tid_release(skb, len, hwtid, 0);
244 245 246 247 248 249
	c4iw_ofld_send(rdev, skb);
	return;
}

static void set_emss(struct c4iw_ep *ep, u16 opt)
{
250
	ep->emss = ep->com.dev->rdev.lldi.mtus[TCPOPT_MSS_G(opt)] -
251 252 253
		   ((AF_INET == ep->com.remote_addr.ss_family) ?
		    sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
		   sizeof(struct tcphdr);
254
	ep->mss = ep->emss;
255
	if (TCPOPT_TSTAMP_G(opt))
256
		ep->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
257 258
	if (ep->emss < 128)
		ep->emss = 128;
259
	if (ep->emss & 7)
260 261
		pr_debug("Warning: misaligned mtu idx %u mss %u emss=%u\n",
			 TCPOPT_MSS_G(opt), ep->mss, ep->emss);
262 263
	pr_debug("mss_idx %u mss %u emss=%u\n", TCPOPT_MSS_G(opt), ep->mss,
		 ep->emss);
264 265 266 267 268 269
}

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

270
	mutex_lock(&epc->mutex);
271
	state = epc->state;
272
	mutex_unlock(&epc->mutex);
273 274 275 276 277 278 279 280 281 282
	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)
{
283
	mutex_lock(&epc->mutex);
284
	pr_debug("%s -> %s\n", states[epc->state], states[new]);
285
	__state_set(epc, new);
286
	mutex_unlock(&epc->mutex);
287 288 289
	return;
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
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;
}

309 310 311 312 313 314
static void *alloc_ep(int size, gfp_t gfp)
{
	struct c4iw_ep_common *epc;

	epc = kzalloc(size, gfp);
	if (epc) {
315
		epc->wr_waitp = c4iw_alloc_wr_wait(gfp);
316 317 318 319 320
		if (!epc->wr_waitp) {
			kfree(epc);
			epc = NULL;
			goto out;
		}
321
		kref_init(&epc->kref);
322
		mutex_init(&epc->mutex);
323
		c4iw_init_wr_wait(epc->wr_waitp);
324
	}
325
	pr_debug("alloc ep %p\n", epc);
326
out:
327 328 329
	return epc;
}

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

334 335 336
	xa_lock_irqsave(&ep->com.dev->hwtids, flags);
	__xa_erase(&ep->com.dev->hwtids, ep->hwtid);
	if (xa_empty(&ep->com.dev->hwtids))
337
		wake_up(&ep->com.dev->wait);
338
	xa_unlock_irqrestore(&ep->com.dev->hwtids, flags);
339 340
}

341
static int insert_ep_tid(struct c4iw_ep *ep)
342 343
{
	unsigned long flags;
344 345 346 347 348
	int err;

	xa_lock_irqsave(&ep->com.dev->hwtids, flags);
	err = __xa_insert(&ep->com.dev->hwtids, ep->hwtid, ep, GFP_KERNEL);
	xa_unlock_irqrestore(&ep->com.dev->hwtids, flags);
349

350
	return err;
351 352 353 354 355 356 357 358 359 360
}

/*
 * 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;

361 362
	xa_lock_irqsave(&dev->hwtids, flags);
	ep = xa_load(&dev->hwtids, tid);
363 364
	if (ep)
		c4iw_get_ep(&ep->com);
365
	xa_unlock_irqrestore(&dev->hwtids, flags);
366 367 368
	return ep;
}

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
/*
 * 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;
}

386 387 388 389 390
void _c4iw_free_ep(struct kref *kref)
{
	struct c4iw_ep *ep;

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

			cxgb4_clip_release(
					ep->com.dev->rdev.lldi.ports[0],
					(const u32 *)&sin6->sin6_addr.s6_addr,
					1);
		}
405 406
		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid,
				 ep->com.local_addr.ss_family);
407 408
		dst_release(ep->dst);
		cxgb4_l2t_release(ep->l2t);
409
		kfree_skb(ep->mpa_skb);
410
	}
411 412
	if (!skb_queue_empty(&ep->com.ep_skb_list))
		skb_queue_purge(&ep->com.ep_skb_list);
413
	c4iw_put_wr_wait(ep->com.wr_waitp);
414 415 416 417 418 419
	kfree(ep);
}

static void release_ep_resources(struct c4iw_ep *ep)
{
	set_bit(RELEASE_RESOURCES, &ep->com.flags);
420 421 422 423 424 425 426 427 428

	/*
	 * 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);
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 459 460 461 462 463
	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);
	}
464
	t4_set_arp_err_handler(skb, NULL, NULL);
465 466 467
	return skb;
}

468 469
static struct net_device *get_real_dev(struct net_device *egress_dev)
{
S
Steve Wise 已提交
470
	return rdma_vlan_dev_real_dev(egress_dev) ? : egress_dev;
471 472
}

473 474
static void arp_failure_discard(void *handle, struct sk_buff *skb)
{
475
	pr_err("ARP failure\n");
476 477 478
	kfree_skb(skb);
}

479 480 481 482 483
static void mpa_start_arp_failure(void *handle, struct sk_buff *skb)
{
	pr_err("ARP failure during MPA Negotiation - Closing Connection\n");
}

484
enum {
485
	NUM_FAKE_CPLS = 2,
486
	FAKE_CPL_PUT_EP_SAFE = NUM_CPL_CMDS + 0,
487
	FAKE_CPL_PASS_PUT_EP_SAFE = NUM_CPL_CMDS + 1,
488 489 490 491 492 493 494 495
};

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);
496
	kfree_skb(skb);
497 498 499
	return 0;
}

500 501 502 503 504 505 506
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);
507
	kfree_skb(skb);
508 509 510
	return 0;
}

511 512 513 514 515 516
/*
 * 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.
 */
517 518
static void queue_arp_failure_cpl(struct c4iw_ep *ep, struct sk_buff *skb,
				  int cpl)
519 520 521 522
{
	struct cpl_act_establish *rpl = cplhdr(skb);

	/* Set our special ARP_FAILURE opcode */
523
	rpl->ot.opcode = cpl;
524 525 526 527 528 529 530 531 532 533 534 535 536 537

	/*
	 * 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;

538
	pr_err("ARP failure during accept - tid %u - dropping connection\n",
539 540 541
	       ep->hwtid);

	__state_set(&ep->com, DEAD);
542
	queue_arp_failure_cpl(ep, skb, FAKE_CPL_PASS_PUT_EP_SAFE);
543 544
}

545 546 547 548 549
/*
 * Handle an ARP failure for an active open.
 */
static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
{
550 551
	struct c4iw_ep *ep = handle;

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

/*
 * 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)
{
572 573 574
	int ret;
	struct c4iw_ep *ep = handle;
	struct c4iw_rdev *rdev = &ep->com.dev->rdev;
575 576
	struct cpl_abort_req *req = cplhdr(skb);

577
	pr_debug("rdev %p\n", rdev);
578
	req->cmd = CPL_ABORT_NO_RST;
579
	skb_get(skb);
580 581 582 583
	ret = c4iw_ofld_send(rdev, skb);
	if (ret) {
		__state_set(&ep->com, DEAD);
		queue_arp_failure_cpl(ep, skb, FAKE_CPL_PUT_EP_SAFE);
584 585
	} else
		kfree_skb(skb);
586 587
}

588
static int send_flowc(struct c4iw_ep *ep)
589 590
{
	struct fw_flowc_wr *flowc;
591
	struct sk_buff *skb = skb_dequeue(&ep->com.ep_skb_list);
H
Hariprasad S 已提交
592 593
	u16 vlan = ep->l2t->vlan;
	int nparams;
594
	int flowclen, flowclen16;
H
Hariprasad S 已提交
595

596 597 598
	if (WARN_ON(!skb))
		return -ENOMEM;

H
Hariprasad S 已提交
599 600
	if (vlan == CPL_L2T_VLAN_NONE)
		nparams = 9;
601 602 603 604 605 606
	else
		nparams = 10;

	flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
	flowclen16 = DIV_ROUND_UP(flowclen, 16);
	flowclen = flowclen16 * 16;
607

608 609
	flowc = __skb_put(skb, flowclen);
	memset(flowc, 0, flowclen);
610

611
	flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
H
Hariprasad S 已提交
612
					   FW_FLOWC_WR_NPARAMS_V(nparams));
613 614
	flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(flowclen16) |
					  FW_WR_FLOWID_V(ep->hwtid));
615 616

	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
617
	flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
618
					    (ep->com.dev->rdev.lldi.pf));
619 620 621 622 623 624 625 626 627 628 629
	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;
630
	flowc->mnemval[6].val = cpu_to_be32(ep->snd_win);
631 632
	flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
	flowc->mnemval[7].val = cpu_to_be32(ep->emss);
633 634 635
	flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_RCV_SCALE;
	flowc->mnemval[8].val = cpu_to_be32(ep->snd_wscale);
	if (nparams == 10) {
H
Hariprasad S 已提交
636 637
		u16 pri;
		pri = (vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
638 639
		flowc->mnemval[9].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
		flowc->mnemval[9].val = cpu_to_be32(pri);
640 641 642
	}

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

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

651
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
652
	if (WARN_ON(!skb))
653
		return -ENOMEM;
654

655 656 657
	cxgb_mk_close_con_req(skb, wrlen, ep->hwtid, ep->txq_idx,
			      NULL, arp_failure_discard);

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

661
static void read_tcb(struct c4iw_ep *ep)
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
{
	struct sk_buff *skb;
	struct cpl_get_tcb *req;
	int wrlen = roundup(sizeof(*req), 16);

	skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
	if (WARN_ON(!skb))
		return;

	set_wr_txq(skb, CPL_PRIORITY_CONTROL, ep->ctrlq_idx);
	req = (struct cpl_get_tcb *) skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	INIT_TP_WR(req, ep->hwtid);
	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_GET_TCB, ep->hwtid));
	req->reply_ctrl = htons(REPLY_CHAN_V(0) | QUEUENO_V(ep->rss_qid));

	/*
	 * keep a ref on the ep so the tcb is not unlocked before this
	 * cpl completes. The ref is released in read_tcb_rpl().
	 */
	c4iw_get_ep(&ep->com);
	if (WARN_ON(c4iw_ofld_send(&ep->com.dev->rdev, skb)))
		c4iw_put_ep(&ep->com);
}

static int send_abort_req(struct c4iw_ep *ep)
688
{
689
	u32 wrlen = roundup(sizeof(struct cpl_abort_req), 16);
690
	struct sk_buff *req_skb = skb_dequeue(&ep->com.ep_skb_list);
691

692
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
693
	if (WARN_ON(!req_skb))
694
		return -ENOMEM;
695

696 697 698
	cxgb_mk_abort_req(req_skb, wrlen, ep->hwtid, ep->txq_idx,
			  ep, abort_arp_failure);

699
	return c4iw_l2t_send(&ep->com.dev->rdev, req_skb, ep->l2t);
700 701
}

702 703 704 705 706 707 708 709 710 711 712
static int send_abort(struct c4iw_ep *ep)
{
	if (!ep->com.qp || !ep->com.qp->srq) {
		send_abort_req(ep);
		return 0;
	}
	set_bit(ABORT_REQ_IN_PROGRESS, &ep->com.flags);
	read_tcb(ep);
	return 0;
}

713 714
static int send_connect(struct c4iw_ep *ep)
{
715 716 717 718 719 720
	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;
721 722 723 724
	struct sk_buff *skb;
	u64 opt0;
	u32 opt2;
	unsigned int mtu_idx;
725
	u32 wscale;
726
	int win, sizev4, sizev6, wrlen;
727
	struct sockaddr_in *la = (struct sockaddr_in *)
728
				 &ep->com.local_addr;
729
	struct sockaddr_in *ra = (struct sockaddr_in *)
730
				 &ep->com.remote_addr;
731
	struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)
732
				   &ep->com.local_addr;
733
	struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)
734
				   &ep->com.remote_addr;
H
Hariprasad S 已提交
735
	int ret;
736 737
	enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
	u32 isn = (prandom_u32() & ~7UL) - 1;
738 739 740 741
	struct net_device *netdev;
	u64 params;

	netdev = ep->com.dev->rdev.lldi.ports[0];
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

	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;
	}
761 762 763 764

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

766
	pr_debug("ep %p atid %u\n", ep, ep->atid);
767 768 769

	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
770
		pr_err("%s - failed to alloc skb\n", __func__);
771 772
		return -ENOMEM;
	}
S
Steve Wise 已提交
773
	set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
774

775 776 777
	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);
778
	wscale = cxgb_compute_wscale(rcv_win);
779 780 781 782 783 784

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

788
	opt0 = (nocong ? NO_CONG_F : 0) |
789
	       KEEP_ALIVE_F |
790
	       DELACK_F |
791 792 793 794 795
	       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 已提交
796
	       DSCP_V(ep->tos >> 2) |
797 798 799
	       ULP_MODE_V(ULP_MODE_TCPDDP) |
	       RCV_BUFSIZ_V(win);
	opt2 = RX_CHANNEL_V(0) |
800
	       CCTRL_ECN_V(enable_ecn) |
801
	       RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
802
	if (enable_tcp_timestamps)
803
		opt2 |= TSTAMPS_EN_F;
804
	if (enable_tcp_sack)
805
		opt2 |= SACK_EN_F;
806
	if (wscale && enable_tcp_window_scaling)
807
		opt2 |= WND_SCALE_EN_F;
808 809 810 811
	if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
		if (peer2peer)
			isn += 4;

812
		opt2 |= T5_OPT_2_VALID_F;
813
		opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
814
		opt2 |= T5_ISS_F;
815
	}
H
Hariprasad S 已提交
816

817 818
	params = cxgb4_select_ntuple(netdev, ep->l2t);

H
Hariprasad S 已提交
819 820 821 822
	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);

823
	t4_set_arp_err_handler(skb, ep, act_open_req_arp_failure);
824

825 826 827
	if (ep->com.remote_addr.ss_family == AF_INET) {
		switch (CHELSIO_CHIP_VERSION(adapter_type)) {
		case CHELSIO_T4:
828
			req = skb_put(skb, wrlen);
829
			INIT_TP_WR(req, 0);
830 831
			break;
		case CHELSIO_T5:
832
			t5req = skb_put(skb, wrlen);
833 834 835 836
			INIT_TP_WR(t5req, 0);
			req = (struct cpl_act_open_req *)t5req;
			break;
		case CHELSIO_T6:
837
			t6req = skb_put(skb, wrlen);
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
			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)) {
858
			req->params = cpu_to_be32(params);
859 860
			req->opt2 = cpu_to_be32(opt2);
		} else {
861 862 863 864
			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);
B
Bart Van Assche 已提交
865
				pr_debug("snd_isn %u\n", t5req->rsvd);
866 867 868 869 870
				t5req->opt2 = cpu_to_be32(opt2);
			} else {
				t6req->params =
					  cpu_to_be64(FILTER_TUPLE_V(params));
				t6req->rsvd = cpu_to_be32(isn);
B
Bart Van Assche 已提交
871
				pr_debug("snd_isn %u\n", t6req->rsvd);
872 873
				t6req->opt2 = cpu_to_be32(opt2);
			}
874 875 876 877
		}
	} else {
		switch (CHELSIO_CHIP_VERSION(adapter_type)) {
		case CHELSIO_T4:
878
			req6 = skb_put(skb, wrlen);
879
			INIT_TP_WR(req6, 0);
880 881
			break;
		case CHELSIO_T5:
882
			t5req6 = skb_put(skb, wrlen);
883 884 885 886
			INIT_TP_WR(t5req6, 0);
			req6 = (struct cpl_act_open_req6 *)t5req6;
			break;
		case CHELSIO_T6:
887
			t6req6 = skb_put(skb, wrlen);
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
			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)) {
910 911
			req6->params = cpu_to_be32(cxgb4_select_ntuple(netdev,
								      ep->l2t));
912 913
			req6->opt2 = cpu_to_be32(opt2);
		} else {
914 915 916 917
			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);
B
Bart Van Assche 已提交
918
				pr_debug("snd_isn %u\n", t5req6->rsvd);
919 920 921 922 923
				t5req6->opt2 = cpu_to_be32(opt2);
			} else {
				t6req6->params =
					    cpu_to_be64(FILTER_TUPLE_V(params));
				t6req6->rsvd = cpu_to_be32(isn);
B
Bart Van Assche 已提交
924
				pr_debug("snd_isn %u\n", t6req6->rsvd);
925 926 927
				t6req6->opt2 = cpu_to_be32(opt2);
			}

928
		}
929 930
	}

931
	set_bit(ACT_OPEN_REQ, &ep->com.history);
H
Hariprasad S 已提交
932
	ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
933
clip_release:
H
Hariprasad S 已提交
934 935 936 937
	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;
938 939
}

940 941
static int send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb,
			u8 mpa_rev_to_use)
942
{
943
	int mpalen, wrlen, ret;
944 945
	struct fw_ofld_tx_data_wr *req;
	struct mpa_message *mpa;
946
	struct mpa_v2_conn_params mpa_v2_params;
947

948 949
	pr_debug("ep %p tid %u pd_len %d\n",
		 ep, ep->hwtid, ep->plen);
950 951

	mpalen = sizeof(*mpa) + ep->plen;
952 953
	if (mpa_rev_to_use == 2)
		mpalen += sizeof(struct mpa_v2_conn_params);
954 955 956 957
	wrlen = roundup(mpalen + sizeof *req, 16);
	skb = get_skb(skb, wrlen, GFP_KERNEL);
	if (!skb) {
		connect_reply_upcall(ep, -ENOMEM);
958
		return -ENOMEM;
959 960 961
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);

962
	req = skb_put_zero(skb, wrlen);
963
	req->op_to_immdlen = cpu_to_be32(
964 965 966
		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
		FW_WR_COMPL_F |
		FW_WR_IMMDLEN_V(mpalen));
967
	req->flowid_len16 = cpu_to_be32(
968 969
		FW_WR_FLOWID_V(ep->hwtid) |
		FW_WR_LEN16_V(wrlen >> 4));
970 971
	req->plen = cpu_to_be32(mpalen);
	req->tunnel_to_proxy = cpu_to_be32(
972 973
		FW_OFLD_TX_DATA_WR_FLUSH_F |
		FW_OFLD_TX_DATA_WR_SHOVE_F);
974 975 976

	mpa = (struct mpa_message *)(req + 1);
	memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
977 978 979 980 981 982 983 984 985 986 987 988 989

	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;

990
	mpa->private_data_size = htons(ep->plen);
991
	mpa->revision = mpa_rev_to_use;
992
	if (mpa_rev_to_use == 1) {
993
		ep->tried_with_mpa_v1 = 1;
994 995
		ep->retry_with_mpa_v1 = 0;
	}
996 997

	if (mpa_rev_to_use == 2) {
998 999
		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
					       sizeof (struct mpa_v2_conn_params));
1000
		pr_debug("initiator ird %u ord %u\n", ep->ird,
J
Joe Perches 已提交
1001
			 ep->ord);
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
		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));
1016

1017 1018 1019 1020 1021 1022 1023 1024
		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);
1025 1026 1027 1028 1029 1030 1031 1032 1033

	/*
	 * 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);
	ep->mpa_skb = skb;
1034 1035 1036
	ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
	if (ret)
		return ret;
1037
	start_ep_timer(ep);
1038
	__state_set(&ep->com, MPA_REQ_SENT);
1039
	ep->mpa_attr.initiator = 1;
1040
	ep->snd_seq += mpalen;
1041
	return ret;
1042 1043 1044 1045 1046 1047 1048 1049
}

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;
1050
	struct mpa_v2_conn_params mpa_v2_params;
1051

1052 1053
	pr_debug("ep %p tid %u pd_len %d\n",
		 ep, ep->hwtid, ep->plen);
1054 1055

	mpalen = sizeof(*mpa) + plen;
1056 1057
	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
		mpalen += sizeof(struct mpa_v2_conn_params);
1058 1059 1060 1061
	wrlen = roundup(mpalen + sizeof *req, 16);

	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
1062
		pr_err("%s - cannot alloc skb!\n", __func__);
1063 1064 1065 1066
		return -ENOMEM;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);

1067
	req = skb_put_zero(skb, wrlen);
1068
	req->op_to_immdlen = cpu_to_be32(
1069 1070 1071
		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
		FW_WR_COMPL_F |
		FW_WR_IMMDLEN_V(mpalen));
1072
	req->flowid_len16 = cpu_to_be32(
1073 1074
		FW_WR_FLOWID_V(ep->hwtid) |
		FW_WR_LEN16_V(wrlen >> 4));
1075 1076
	req->plen = cpu_to_be32(mpalen);
	req->tunnel_to_proxy = cpu_to_be32(
1077 1078
		FW_OFLD_TX_DATA_WR_FLUSH_F |
		FW_OFLD_TX_DATA_WR_SHOVE_F);
1079 1080 1081 1082 1083

	mpa = (struct mpa_message *)(req + 1);
	memset(mpa, 0, sizeof(*mpa));
	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
	mpa->flags = MPA_REJECT;
1084
	mpa->revision = ep->mpa_attr.version;
1085
	mpa->private_data_size = htons(plen);
1086 1087 1088

	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1089 1090
		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
					       sizeof (struct mpa_v2_conn_params));
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
		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);
1109 1110 1111 1112 1113 1114 1115 1116

	/*
	 * 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);
1117
	t4_set_arp_err_handler(skb, NULL, mpa_start_arp_failure);
1118
	ep->mpa_skb = skb;
1119
	ep->snd_seq += mpalen;
1120 1121 1122 1123 1124 1125 1126 1127 1128
	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;
1129
	struct mpa_v2_conn_params mpa_v2_params;
1130

1131 1132
	pr_debug("ep %p tid %u pd_len %d\n",
		 ep, ep->hwtid, ep->plen);
1133 1134

	mpalen = sizeof(*mpa) + plen;
1135 1136
	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
		mpalen += sizeof(struct mpa_v2_conn_params);
1137 1138 1139 1140
	wrlen = roundup(mpalen + sizeof *req, 16);

	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
1141
		pr_err("%s - cannot alloc skb!\n", __func__);
1142 1143 1144 1145
		return -ENOMEM;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);

1146
	req = skb_put_zero(skb, wrlen);
1147
	req->op_to_immdlen = cpu_to_be32(
1148 1149 1150
		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
		FW_WR_COMPL_F |
		FW_WR_IMMDLEN_V(mpalen));
1151
	req->flowid_len16 = cpu_to_be32(
1152 1153
		FW_WR_FLOWID_V(ep->hwtid) |
		FW_WR_LEN16_V(wrlen >> 4));
1154 1155
	req->plen = cpu_to_be32(mpalen);
	req->tunnel_to_proxy = cpu_to_be32(
1156 1157
		FW_OFLD_TX_DATA_WR_FLUSH_F |
		FW_OFLD_TX_DATA_WR_SHOVE_F);
1158 1159 1160 1161

	mpa = (struct mpa_message *)(req + 1);
	memset(mpa, 0, sizeof(*mpa));
	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1162 1163 1164 1165 1166
	mpa->flags = 0;
	if (ep->mpa_attr.crc_enabled)
		mpa->flags |= MPA_CRC;
	if (ep->mpa_attr.recv_marker_enabled)
		mpa->flags |= MPA_MARKERS;
1167
	mpa->revision = ep->mpa_attr.version;
1168
	mpa->private_data_size = htons(plen);
1169 1170 1171

	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1172 1173
		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
					       sizeof (struct mpa_v2_conn_params));
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
		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);
1197 1198 1199 1200 1201 1202 1203

	/*
	 * 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);
1204
	t4_set_arp_err_handler(skb, NULL, mpa_start_arp_failure);
1205
	ep->mpa_skb = skb;
1206
	__state_set(&ep->com, MPA_REP_SENT);
1207
	ep->snd_seq += mpalen;
1208 1209 1210 1211 1212 1213 1214
	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);
1215
	unsigned short tcp_opt = ntohs(req->tcp_opt);
1216
	unsigned int tid = GET_TID(req);
1217
	unsigned int atid = TID_TID_G(ntohl(req->tos_atid));
1218
	struct tid_info *t = dev->rdev.lldi.tids;
1219
	int ret;
1220 1221 1222

	ep = lookup_atid(t, atid);

1223
	pr_debug("ep %p tid %u snd_isn %u rcv_isn %u\n", ep, tid,
J
Joe Perches 已提交
1224
		 be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));
1225

1226
	mutex_lock(&ep->com.mutex);
1227 1228 1229 1230
	dst_confirm(ep->dst);

	/* setup the hwtid for this connection */
	ep->hwtid = tid;
1231
	cxgb4_insert_tid(t, ep, tid, ep->com.local_addr.ss_family);
1232
	insert_ep_tid(ep);
1233 1234 1235

	ep->snd_seq = be32_to_cpu(req->snd_isn);
	ep->rcv_seq = be32_to_cpu(req->rcv_isn);
1236
	ep->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
1237

1238
	set_emss(ep, tcp_opt);
1239 1240

	/* dealloc the atid */
1241
	xa_erase_irq(&ep->com.dev->atids, atid);
1242
	cxgb4_free_atid(t, atid);
1243
	set_bit(ACT_ESTAB, &ep->com.history);
1244 1245

	/* start MPA negotiation */
1246
	ret = send_flowc(ep);
1247 1248
	if (ret)
		goto err;
1249
	if (ep->retry_with_mpa_v1)
1250
		ret = send_mpa_req(ep, skb, 1);
1251
	else
1252 1253 1254
		ret = send_mpa_req(ep, skb, mpa_rev);
	if (ret)
		goto err;
1255
	mutex_unlock(&ep->com.mutex);
1256
	return 0;
1257 1258 1259 1260 1261
err:
	mutex_unlock(&ep->com.mutex);
	connect_reply_upcall(ep, -ENOMEM);
	c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
	return 0;
1262 1263
}

1264
static void close_complete_upcall(struct c4iw_ep *ep, int status)
1265 1266 1267
{
	struct iw_cm_event event;

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

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

1285
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1286 1287 1288
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_DISCONNECT;
	if (ep->com.cm_id) {
J
Joe Perches 已提交
1289 1290
		pr_debug("peer close delivered ep %p cm_id %p tid %u\n",
			 ep, ep->com.cm_id, ep->hwtid);
1291
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1292
		set_bit(DISCONN_UPCALL, &ep->com.history);
1293 1294 1295 1296 1297 1298 1299
	}
}

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

1300
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1301 1302 1303 1304
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CLOSE;
	event.status = -ECONNRESET;
	if (ep->com.cm_id) {
J
Joe Perches 已提交
1305 1306
		pr_debug("abort delivered ep %p cm_id %p tid %u\n", ep,
			 ep->com.cm_id, ep->hwtid);
1307
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1308
		deref_cm_id(&ep->com);
1309
		set_bit(ABORT_UPCALL, &ep->com.history);
1310 1311 1312 1313 1314 1315 1316
	}
}

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

1317 1318
	pr_debug("ep %p tid %u status %d\n",
		 ep, ep->hwtid, status);
1319 1320 1321
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CONNECT_REPLY;
	event.status = status;
1322 1323 1324 1325
	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));
1326 1327

	if ((status == 0) || (status == -ECONNREFUSED)) {
1328 1329
		if (!ep->tried_with_mpa_v1) {
			/* this means MPA_v2 is used */
1330 1331
			event.ord = ep->ird;
			event.ird = ep->ord;
1332 1333 1334 1335 1336 1337 1338
			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 */
1339 1340
			event.ord = cur_max_read_depth(ep->com.dev);
			event.ird = cur_max_read_depth(ep->com.dev);
1341 1342 1343 1344
			event.private_data_len = ep->plen;
			event.private_data = ep->mpa_pkt +
				sizeof(struct mpa_message);
		}
1345
	}
1346

1347
	pr_debug("ep %p tid %u status %d\n", ep,
J
Joe Perches 已提交
1348
		 ep->hwtid, status);
1349
	set_bit(CONN_RPL_UPCALL, &ep->com.history);
1350 1351
	ep->com.cm_id->event_handler(ep->com.cm_id, &event);

1352 1353
	if (status < 0)
		deref_cm_id(&ep->com);
1354 1355
}

1356
static int connect_request_upcall(struct c4iw_ep *ep)
1357 1358
{
	struct iw_cm_event event;
1359
	int ret;
1360

1361
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1362 1363
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CONNECT_REQUEST;
1364 1365 1366 1367
	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));
1368
	event.provider_data = ep;
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	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 */
1379 1380
		event.ord = cur_max_read_depth(ep->com.dev);
		event.ird = cur_max_read_depth(ep->com.dev);
1381 1382 1383
		event.private_data_len = ep->plen;
		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
	}
1384 1385 1386 1387 1388
	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);
1389
	set_bit(CONNREQ_UPCALL, &ep->com.history);
1390
	c4iw_put_ep(&ep->parent_ep->com);
1391
	return ret;
1392 1393 1394 1395 1396 1397
}

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

1398
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1399 1400
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_ESTABLISHED;
1401 1402
	event.ird = ep->ord;
	event.ord = ep->ird;
1403
	if (ep->com.cm_id) {
1404
		pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1405
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1406
		set_bit(ESTAB_UPCALL, &ep->com.history);
1407 1408 1409 1410 1411 1412
	}
}

static int update_rx_credits(struct c4iw_ep *ep, u32 credits)
{
	struct sk_buff *skb;
1413 1414
	u32 wrlen = roundup(sizeof(struct cpl_rx_data_ack), 16);
	u32 credit_dack;
1415

1416 1417
	pr_debug("ep %p tid %u credits %u\n",
		 ep, ep->hwtid, credits);
1418 1419
	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
1420
		pr_err("update_rx_credits - cannot alloc skb!\n");
1421 1422 1423
		return 0;
	}

1424 1425 1426 1427 1428
	/*
	 * 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.
	 */
1429 1430
	if (ep->rcv_win > RCV_BUFSIZ_M * 1024)
		credits += ep->rcv_win - RCV_BUFSIZ_M * 1024;
1431

1432 1433 1434 1435 1436 1437
	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);

1438 1439 1440 1441
	c4iw_ofld_send(&ep->com.dev->rdev, skb);
	return credits;
}

1442 1443
#define RELAXED_IRD_NEGOTIATION 1

1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
/*
 * 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.
 */
1456
static int process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
1457 1458
{
	struct mpa_message *mpa;
1459
	struct mpa_v2_conn_params *mpa_v2_params;
1460
	u16 plen;
1461 1462
	u16 resp_ird, resp_ord;
	u8 rtr_mismatch = 0, insuff_ird = 0;
1463 1464 1465
	struct c4iw_qp_attributes attrs;
	enum c4iw_qp_attr_mask mask;
	int err;
1466
	int disconnect = 0;
1467

1468
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1469 1470 1471 1472 1473 1474 1475

	/*
	 * 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;
1476
		goto err_stop_timer;
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
	}

	/*
	 * 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))
1490
		return 0;
1491 1492 1493
	mpa = (struct mpa_message *) ep->mpa_pkt;

	/* Validate MPA header. */
1494
	if (mpa->revision > mpa_rev) {
1495 1496
		pr_err("%s MPA version mismatch. Local = %d, Received = %d\n",
		       __func__, mpa_rev, mpa->revision);
1497
		err = -EPROTO;
1498
		goto err_stop_timer;
1499 1500 1501
	}
	if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
		err = -EPROTO;
1502
		goto err_stop_timer;
1503 1504 1505 1506 1507 1508 1509 1510 1511
	}

	plen = ntohs(mpa->private_data_size);

	/*
	 * Fail if there's too much private data.
	 */
	if (plen > MPA_MAX_PRIVATE_DATA) {
		err = -EPROTO;
1512
		goto err_stop_timer;
1513 1514 1515 1516 1517 1518 1519
	}

	/*
	 * If plen does not account for pkt size
	 */
	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
		err = -EPROTO;
1520
		goto err_stop_timer;
1521 1522 1523 1524 1525 1526 1527 1528 1529
	}

	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))
1530
		return 0;
1531 1532 1533

	if (mpa->flags & MPA_REJECT) {
		err = -ECONNREFUSED;
1534
		goto err_stop_timer;
1535 1536
	}

1537 1538 1539 1540 1541 1542 1543 1544
	/*
	 * 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;

1545 1546 1547 1548 1549
	/*
	 * If we get here we have accumulated the entire mpa
	 * start reply message including private data. And
	 * the MPA header is valid.
	 */
1550
	__state_set(&ep->com, FPDU_MODE);
1551 1552
	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;
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
	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;
1566
			pr_debug("responder ird %u ord %u ep ird %u ord %u\n",
J
Joe Perches 已提交
1567
				 resp_ird, resp_ord, ep->ird, ep->ord);
1568 1569 1570 1571 1572 1573

			/*
			 * This is a double-check. Ideally, below checks are
			 * not required since ird/ord stuff has been taken
			 * care of in c4iw_accept_cr
			 */
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
			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) {
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
				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;

1611 1612
	pr_debug("crc_enabled=%d, recv_marker_enabled=%d, xmit_marker_enabled=%d, version=%d p2p_type=%d local-p2p_type = %d\n",
		 ep->mpa_attr.crc_enabled,
J
Joe Perches 已提交
1613 1614 1615
		 ep->mpa_attr.recv_marker_enabled,
		 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
		 ep->mpa_attr.p2p_type, p2p_type);
1616 1617 1618 1619 1620 1621 1622

	/*
	 * 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
	 */
1623
	if ((ep->mpa_attr.version == 2) && peer2peer &&
1624 1625 1626 1627
			(ep->mpa_attr.p2p_type != p2p_type)) {
		ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
		rtr_mismatch = 1;
	}
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643

	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;
1644 1645 1646 1647 1648 1649

	/*
	 * If responder's RTR requirement did not match with what initiator
	 * supports, generate TERM message
	 */
	if (rtr_mismatch) {
1650
		pr_err("%s: RTR mismatch, sending TERM\n", __func__);
1651 1652 1653
		attrs.layer_etype = LAYER_MPA | DDP_LLP;
		attrs.ecode = MPA_NOMATCH_RTR;
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1654
		attrs.send_term = 1;
1655
		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1656
				C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1657
		err = -ENOMEM;
1658
		disconnect = 1;
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
		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) {
1669
		pr_err("%s: Insufficient IRD, sending TERM\n", __func__);
1670 1671 1672
		attrs.layer_etype = LAYER_MPA | DDP_LLP;
		attrs.ecode = MPA_INSUFF_IRD;
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1673
		attrs.send_term = 1;
1674
		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1675
				C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1676
		err = -ENOMEM;
1677
		disconnect = 1;
1678 1679
		goto out;
	}
1680
	goto out;
1681 1682
err_stop_timer:
	stop_ep_timer(ep);
1683
err:
1684
	disconnect = 2;
1685 1686
out:
	connect_reply_upcall(ep, err);
1687
	return disconnect;
1688 1689
}

1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
/*
 * 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)
1703 1704
{
	struct mpa_message *mpa;
1705
	struct mpa_v2_conn_params *mpa_v2_params;
1706 1707
	u16 plen;

1708
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1709 1710 1711 1712 1713

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

1717
	pr_debug("enter (%s line %u)\n", __FILE__, __LINE__);
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730

	/*
	 * 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))
1731
		return 0;
1732

1733
	pr_debug("enter (%s line %u)\n", __FILE__, __LINE__);
1734 1735 1736 1737 1738
	mpa = (struct mpa_message *) ep->mpa_pkt;

	/*
	 * Validate MPA Header.
	 */
1739
	if (mpa->revision > mpa_rev) {
1740 1741
		pr_err("%s MPA version mismatch. Local = %d, Received = %d\n",
		       __func__, mpa_rev, mpa->revision);
1742
		goto err_stop_timer;
1743 1744
	}

1745 1746
	if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key)))
		goto err_stop_timer;
1747 1748 1749 1750 1751 1752

	plen = ntohs(mpa->private_data_size);

	/*
	 * Fail if there's too much private data.
	 */
1753 1754
	if (plen > MPA_MAX_PRIVATE_DATA)
		goto err_stop_timer;
1755 1756 1757 1758

	/*
	 * If plen does not account for pkt size
	 */
1759 1760
	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen))
		goto err_stop_timer;
1761 1762 1763 1764 1765 1766
	ep->plen = (u8) plen;

	/*
	 * If we don't have all the pdata yet, then bail.
	 */
	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1767
		return 0;
1768 1769 1770 1771 1772 1773 1774 1775 1776

	/*
	 * 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;
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
	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;
1790 1791
			ep->ird = min_t(u32, ep->ird,
					cur_max_read_depth(ep->com.dev));
1792 1793
			ep->ord = ntohs(mpa_v2_params->ord) &
				MPA_V2_IRD_ORD_MASK;
1794 1795
			ep->ord = min_t(u32, ep->ord,
					cur_max_read_depth(ep->com.dev));
1796 1797
			pr_debug("initiator ird %u ord %u\n",
				 ep->ird, ep->ord);
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
			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;

1814
	pr_debug("crc_enabled=%d, recv_marker_enabled=%d, xmit_marker_enabled=%d, version=%d p2p_type=%d\n",
J
Joe Perches 已提交
1815 1816 1817
		 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);
1818

1819 1820 1821 1822 1823 1824
	__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))
1825
			goto err_unlock_parent;
1826 1827
	} else {
		goto err_unlock_parent;
1828
	}
1829
	mutex_unlock(&ep->parent_ep->com.mutex);
1830 1831 1832 1833 1834 1835 1836 1837 1838
	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;
1839 1840 1841 1842 1843 1844 1845 1846
}

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);
1847
	__u8 status = hdr->status;
1848
	int disconnect = 0;
1849

1850
	ep = get_ep_from_tid(dev, tid);
1851 1852
	if (!ep)
		return 0;
1853
	pr_debug("ep %p tid %u dlen %u\n", ep, ep->hwtid, dlen);
1854 1855
	skb_pull(skb, sizeof(*hdr));
	skb_trim(skb, dlen);
1856
	mutex_lock(&ep->com.mutex);
1857

1858
	switch (ep->com.state) {
1859
	case MPA_REQ_SENT:
1860
		update_rx_credits(ep, dlen);
1861
		ep->rcv_seq += dlen;
1862
		disconnect = process_mpa_reply(ep, skb);
1863 1864
		break;
	case MPA_REQ_WAIT:
1865
		update_rx_credits(ep, dlen);
1866
		ep->rcv_seq += dlen;
1867
		disconnect = process_mpa_request(ep, skb);
1868
		break;
1869 1870
	case FPDU_MODE: {
		struct c4iw_qp_attributes attrs;
1871 1872

		update_rx_credits(ep, dlen);
1873
		if (status)
1874
			pr_err("%s Unexpected streaming data." \
1875 1876
			       " qpid %u ep %p state %d tid %u status %d\n",
			       __func__, ep->com.qp->wq.sq.qid, ep,
1877
			       ep->com.state, ep->hwtid, status);
1878
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1879
		c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1880 1881
			       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
		disconnect = 1;
1882 1883
		break;
	}
1884 1885 1886
	default:
		break;
	}
1887
	mutex_unlock(&ep->com.mutex);
1888
	if (disconnect)
1889
		c4iw_ep_disconnect(ep, disconnect == 2, GFP_KERNEL);
1890
	c4iw_put_ep(&ep->com);
1891 1892 1893
	return 0;
}

1894
static void complete_cached_srq_buffers(struct c4iw_ep *ep, u32 srqidx)
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
{
	enum chip_type adapter_type;

	adapter_type = ep->com.dev->rdev.lldi.adapter_type;

	/*
	 * If this TCB had a srq buffer cached, then we must complete
	 * it. For user mode, that means saving the srqidx in the
	 * user/kernel status page for this qp.  For kernel mode, just
	 * synthesize the CQE now.
	 */
	if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T5 && srqidx) {
		if (ep->com.qp->ibqp.uobject)
			t4_set_wq_in_error(&ep->com.qp->wq, srqidx);
		else
			c4iw_flush_srqidx(ep->com.qp, srqidx);
	}
}

1914 1915
static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
1916
	u32 srqidx;
1917
	struct c4iw_ep *ep;
1918
	struct cpl_abort_rpl_rss6 *rpl = cplhdr(skb);
1919 1920 1921
	int release = 0;
	unsigned int tid = GET_TID(rpl);

1922
	ep = get_ep_from_tid(dev, tid);
1923
	if (!ep) {
1924
		pr_warn("Abort rpl to freed endpoint\n");
1925 1926
		return 0;
	}
1927

1928 1929 1930 1931
	if (ep->com.qp && ep->com.qp->srq) {
		srqidx = ABORT_RSS_SRQIDX_G(be32_to_cpu(rpl->srqidx_status));
		complete_cached_srq_buffers(ep, srqidx ? srqidx : ep->srqe_idx);
	}
1932

1933
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
1934
	mutex_lock(&ep->com.mutex);
1935 1936
	switch (ep->com.state) {
	case ABORTING:
1937
		c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
1938 1939 1940 1941
		__state_set(&ep->com, DEAD);
		release = 1;
		break;
	default:
1942
		pr_err("%s ep %p state %d\n", __func__, ep, ep->com.state);
1943 1944
		break;
	}
1945
	mutex_unlock(&ep->com.mutex);
1946

1947 1948
	if (release) {
		close_complete_upcall(ep, -ECONNRESET);
1949
		release_ep_resources(ep);
1950
	}
1951
	c4iw_put_ep(&ep->com);
1952 1953 1954
	return 0;
}

1955
static int send_fw_act_open_req(struct c4iw_ep *ep, unsigned int atid)
1956 1957 1958 1959
{
	struct sk_buff *skb;
	struct fw_ofld_connection_wr *req;
	unsigned int mtu_idx;
1960
	u32 wscale;
1961
	struct sockaddr_in *sin;
1962
	int win;
1963 1964

	skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1965
	req = __skb_put_zero(skb, sizeof(*req));
1966
	req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR));
1967
	req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
1968 1969
	req->le.filter = cpu_to_be32(cxgb4_select_ntuple(
				     ep->com.dev->rdev.lldi.ports[0],
1970
				     ep->l2t));
1971
	sin = (struct sockaddr_in *)&ep->com.local_addr;
1972 1973
	req->le.lport = sin->sin_port;
	req->le.u.ipv4.lip = sin->sin_addr.s_addr;
1974
	sin = (struct sockaddr_in *)&ep->com.remote_addr;
1975 1976
	req->le.pport = sin->sin_port;
	req->le.u.ipv4.pip = sin->sin_addr.s_addr;
1977
	req->tcb.t_state_to_astid =
1978 1979
			htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_SENT) |
			FW_OFLD_CONNECTION_WR_ASTID_V(atid));
1980
	req->tcb.cplrxdataack_cplpassacceptrpl =
1981
			htons(FW_OFLD_CONNECTION_WR_CPLRXDATAACK_F);
1982
	req->tcb.tx_max = (__force __be32) jiffies;
1983
	req->tcb.rcv_adv = htons(1);
1984 1985 1986
	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);
1987
	wscale = cxgb_compute_wscale(rcv_win);
1988 1989 1990 1991 1992 1993

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

1997 1998
	req->tcb.opt0 = (__force __be64) (TCAM_BYPASS_F |
		(nocong ? NO_CONG_F : 0) |
1999
		KEEP_ALIVE_F |
2000
		DELACK_F |
2001 2002 2003 2004 2005
		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 已提交
2006
		DSCP_V(ep->tos >> 2) |
2007 2008
		ULP_MODE_V(ULP_MODE_TCPDDP) |
		RCV_BUFSIZ_V(win));
2009 2010
	req->tcb.opt2 = (__force __be32) (PACE_V(1) |
		TX_QUEUE_V(ep->com.dev->rdev.lldi.tx_modq[ep->tx_chan]) |
2011
		RX_CHANNEL_V(0) |
2012
		CCTRL_ECN_V(enable_ecn) |
2013
		RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid));
2014
	if (enable_tcp_timestamps)
2015
		req->tcb.opt2 |= (__force __be32)TSTAMPS_EN_F;
2016
	if (enable_tcp_sack)
2017
		req->tcb.opt2 |= (__force __be32)SACK_EN_F;
2018
	if (wscale && enable_tcp_window_scaling)
2019 2020 2021
		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);
2022 2023
	set_wr_txq(skb, CPL_PRIORITY_CONTROL, ep->ctrlq_idx);
	set_bit(ACT_OFLD_CONN, &ep->com.history);
2024
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
2025 2026
}

2027
/*
2028 2029 2030
 * 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.
2031 2032 2033
 */
static inline int act_open_has_tid(int status)
{
2034 2035 2036 2037 2038
	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);
2039 2040
}

2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
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";
	}
}

2055 2056 2057 2058
static void set_tcp_window(struct c4iw_ep *ep, struct port_info *pi)
{
	ep->snd_win = snd_win;
	ep->rcv_win = rcv_win;
2059 2060
	pr_debug("snd_win %d rcv_win %d\n",
		 ep->snd_win, ep->rcv_win);
2061 2062
}

2063 2064
#define ACT_OPEN_RETRY_COUNT 2

2065 2066
static int import_ep(struct c4iw_ep *ep, int iptype, __u8 *peer_ip,
		     struct dst_entry *dst, struct c4iw_dev *cdev,
H
Hariprasad S 已提交
2067
		     bool clear_mpa_v1, enum chip_type adapter_type, u8 tos)
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
{
	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 已提交
2097
					n, pdev, rt_tos2priority(tos));
2098 2099
		if (!ep->l2t) {
			dev_put(pdev);
2100
			goto out;
2101
		}
2102 2103
		ep->mtu = pdev->mtu;
		ep->tx_chan = cxgb4_port_chan(pdev);
2104
		ep->smac_idx = ((struct port_info *)netdev_priv(pdev))->smt_idx;
2105 2106 2107 2108 2109 2110 2111 2112
		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];
2113
		set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2114 2115 2116 2117
		dev_put(pdev);
	} else {
		pdev = get_real_dev(n->dev);
		ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
2118
					n, pdev, rt_tos2priority(tos));
2119 2120 2121
		if (!ep->l2t)
			goto out;
		ep->mtu = dst_mtu(dst);
S
Steve Wise 已提交
2122
		ep->tx_chan = cxgb4_port_chan(pdev);
2123
		ep->smac_idx = ((struct port_info *)netdev_priv(pdev))->smt_idx;
2124 2125
		step = cdev->rdev.lldi.ntxq /
			cdev->rdev.lldi.nchan;
S
Steve Wise 已提交
2126 2127
		ep->txq_idx = cxgb4_port_idx(pdev) * step;
		ep->ctrlq_idx = cxgb4_port_idx(pdev);
2128 2129 2130
		step = cdev->rdev.lldi.nrxq /
			cdev->rdev.lldi.nchan;
		ep->rss_qid = cdev->rdev.lldi.rxq_ids[
S
Steve Wise 已提交
2131
			cxgb4_port_idx(pdev) * step];
2132
		set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147

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

2148 2149 2150
static int c4iw_reconnect(struct c4iw_ep *ep)
{
	int err = 0;
2151
	int size = 0;
2152
	struct sockaddr_in *laddr = (struct sockaddr_in *)
2153
				    &ep->com.cm_id->m_local_addr;
2154
	struct sockaddr_in *raddr = (struct sockaddr_in *)
2155
				    &ep->com.cm_id->m_remote_addr;
2156
	struct sockaddr_in6 *laddr6 = (struct sockaddr_in6 *)
2157
				      &ep->com.cm_id->m_local_addr;
2158
	struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)
2159
				      &ep->com.cm_id->m_remote_addr;
2160 2161
	int iptype;
	__u8 *ra;
2162

2163
	pr_debug("qp %p cm_id %p\n", ep->com.qp, ep->com.cm_id);
2164
	c4iw_init_wr_wait(ep->com.wr_waitp);
2165

2166 2167 2168 2169 2170 2171
	/* 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
S
Steve Wise 已提交
2172
	 * c4iw_reconnect(), Further causing a crash due to an empty
2173 2174 2175 2176 2177 2178 2179 2180
	 * 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;
	}

2181 2182 2183 2184 2185
	/*
	 * 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) {
2186
		pr_err("%s - cannot alloc atid\n", __func__);
2187 2188 2189
		err = -ENOMEM;
		goto fail2;
	}
2190 2191 2192
	err = xa_insert_irq(&ep->com.dev->atids, ep->atid, ep, GFP_KERNEL);
	if (err)
		goto fail2a;
2193 2194

	/* find a route */
2195
	if (ep->com.cm_id->m_local_addr.ss_family == AF_INET) {
2196 2197 2198 2199 2200
		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);
2201 2202 2203
		iptype = 4;
		ra = (__u8 *)&raddr->sin_addr;
	} else {
2204 2205 2206 2207 2208
		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,
2209 2210
					   raddr6->sin6_port,
					   ep->com.cm_id->tos,
2211
					   raddr6->sin6_scope_id);
2212 2213 2214 2215
		iptype = 6;
		ra = (__u8 *)&raddr6->sin6_addr;
	}
	if (!ep->dst) {
2216
		pr_err("%s - cannot find route\n", __func__);
2217 2218 2219
		err = -EHOSTUNREACH;
		goto fail3;
	}
2220
	err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, false,
H
Hariprasad S 已提交
2221 2222
			ep->com.dev->rdev.lldi.adapter_type,
			ep->com.cm_id->tos);
2223
	if (err) {
2224
		pr_err("%s - cannot alloc l2e\n", __func__);
2225 2226 2227
		goto fail4;
	}

2228 2229
	pr_debug("txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
		 ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
J
Joe Perches 已提交
2230
		 ep->l2t->idx);
2231 2232

	state_set(&ep->com, CONNECTING);
H
Hariprasad S 已提交
2233
	ep->tos = ep->com.cm_id->tos;
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243

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

	cxgb4_l2t_release(ep->l2t);
fail4:
	dst_release(ep->dst);
fail3:
2244 2245
	xa_erase_irq(&ep->com.dev->atids, ep->atid);
fail2a:
2246 2247 2248 2249 2250 2251 2252 2253 2254
	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);
2255
fail1:
2256 2257 2258 2259 2260
	c4iw_put_ep(&ep->com);
out:
	return err;
}

2261 2262 2263 2264
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);
2265 2266
	unsigned int atid = TID_TID_G(AOPEN_ATID_G(
				      ntohl(rpl->atid_status)));
2267
	struct tid_info *t = dev->rdev.lldi.tids;
2268
	int status = AOPEN_STATUS_G(ntohl(rpl->atid_status));
2269 2270 2271 2272
	struct sockaddr_in *la;
	struct sockaddr_in *ra;
	struct sockaddr_in6 *la6;
	struct sockaddr_in6 *ra6;
2273
	int ret = 0;
2274 2275

	ep = lookup_atid(t, atid);
2276 2277 2278 2279
	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;
2280

2281
	pr_debug("ep %p atid %u status %u errno %d\n", ep, atid,
J
Joe Perches 已提交
2282
		 status, status2errno(status));
2283

2284
	if (cxgb_is_neg_adv(status)) {
2285 2286
		pr_debug("Connection problems for atid %u status %u (%s)\n",
			 atid, status, neg_adv_str(status));
2287 2288 2289 2290
		ep->stats.connect_neg_adv++;
		mutex_lock(&dev->rdev.stats.lock);
		dev->rdev.stats.neg_adv++;
		mutex_unlock(&dev->rdev.stats.lock);
2291 2292 2293
		return 0;
	}

2294 2295
	set_bit(ACT_OPEN_RPL, &ep->com.history);

2296 2297 2298 2299 2300 2301 2302
	/*
	 * Log interesting failures.
	 */
	switch (status) {
	case CPL_ERR_CONN_RESET:
	case CPL_ERR_CONN_TIMEDOUT:
		break;
2303
	case CPL_ERR_TCAM_FULL:
2304
		mutex_lock(&dev->rdev.stats.lock);
2305
		dev->rdev.stats.tcam_full++;
2306 2307 2308
		mutex_unlock(&dev->rdev.stats.lock);
		if (ep->com.local_addr.ss_family == AF_INET &&
		    dev->rdev.lldi.enable_fw_ofld_conn) {
2309 2310 2311 2312
			ret = send_fw_act_open_req(ep, TID_TID_G(AOPEN_ATID_G(
						   ntohl(rpl->atid_status))));
			if (ret)
				goto fail;
2313 2314 2315 2316 2317 2318
			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 已提交
2319 2320 2321
			if (ep->com.remote_addr.ss_family == AF_INET6) {
				struct sockaddr_in6 *sin6 =
						(struct sockaddr_in6 *)
2322
						&ep->com.local_addr;
H
Hariprasad S 已提交
2323 2324 2325 2326 2327
				cxgb4_clip_release(
						ep->com.dev->rdev.lldi.ports[0],
						(const u32 *)
						&sin6->sin6_addr.s6_addr, 1);
			}
2328
			xa_erase_irq(&ep->com.dev->atids, atid);
2329 2330 2331 2332 2333 2334
			cxgb4_free_atid(t, atid);
			dst_release(ep->dst);
			cxgb4_l2t_release(ep->l2t);
			c4iw_reconnect(ep);
			return 0;
		}
2335
		break;
2336
	default:
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
		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));
		}
2348 2349 2350
		break;
	}

2351
fail:
2352 2353 2354
	connect_reply_upcall(ep, status2errno(status));
	state_set(&ep->com, DEAD);

H
Hariprasad S 已提交
2355 2356
	if (ep->com.remote_addr.ss_family == AF_INET6) {
		struct sockaddr_in6 *sin6 =
2357
			(struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
2358 2359 2360
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
2361
	if (status && act_open_has_tid(status))
2362 2363
		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl),
				 ep->com.local_addr.ss_family);
2364

2365
	xa_erase_irq(&ep->com.dev->atids, atid);
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
	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);
2378
	struct c4iw_listen_ep *ep = get_ep_from_stid(dev, stid);
2379 2380

	if (!ep) {
2381
		pr_warn("%s stid %d lookup failure!\n", __func__, stid);
2382
		goto out;
2383
	}
2384
	pr_debug("ep %p status %d error %d\n", ep,
J
Joe Perches 已提交
2385
		 rpl->status, status2errno(rpl->status));
2386
	c4iw_wake_up_noref(ep->com.wr_waitp, status2errno(rpl->status));
2387
	c4iw_put_ep(&ep->com);
2388
out:
2389 2390 2391 2392 2393 2394 2395
	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);
2396
	struct c4iw_listen_ep *ep = get_ep_from_stid(dev, stid);
2397

2398
	if (!ep) {
2399
		pr_warn("%s stid %d lookup failure!\n", __func__, stid);
2400 2401
		goto out;
	}
2402
	pr_debug("ep %p\n", ep);
2403
	c4iw_wake_up_noref(ep->com.wr_waitp, status2errno(rpl->status));
2404
	c4iw_put_ep(&ep->com);
2405
out:
2406 2407 2408
	return 0;
}

2409 2410
static int accept_cr(struct c4iw_ep *ep, struct sk_buff *skb,
		     struct cpl_pass_accept_req *req)
2411 2412 2413 2414 2415
{
	struct cpl_pass_accept_rpl *rpl;
	unsigned int mtu_idx;
	u64 opt0;
	u32 opt2;
2416
	u32 wscale;
2417
	struct cpl_t5_pass_accept_rpl *rpl5 = NULL;
2418
	int win;
2419
	enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
2420

2421
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
2422

2423
	skb_get(skb);
2424
	rpl = cplhdr(skb);
2425
	if (!is_t4(adapter_type)) {
2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
		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));

2436 2437 2438
	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);
2439
	wscale = cxgb_compute_wscale(rcv_win);
2440 2441 2442 2443 2444 2445

	/*
	 * Specify the largest window that will fit in opt0. The
	 * remainder will be specified in the rx_data_ack.
	 */
	win = ep->rcv_win >> 10;
2446 2447
	if (win > RCV_BUFSIZ_M)
		win = RCV_BUFSIZ_M;
2448
	opt0 = (nocong ? NO_CONG_F : 0) |
2449
	       KEEP_ALIVE_F |
2450
	       DELACK_F |
2451 2452 2453 2454 2455
	       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) |
2456
	       DSCP_V(ep->tos >> 2) |
2457 2458 2459 2460
	       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);
2461 2462

	if (enable_tcp_timestamps && req->tcpopt.tstamp)
2463
		opt2 |= TSTAMPS_EN_F;
2464
	if (enable_tcp_sack && req->tcpopt.sack)
2465
		opt2 |= SACK_EN_F;
2466
	if (wscale && enable_tcp_window_scaling)
2467
		opt2 |= WND_SCALE_EN_F;
2468 2469 2470 2471
	if (enable_ecn) {
		const struct tcphdr *tcph;
		u32 hlen = ntohl(req->hdr_len);

2472 2473 2474 2475 2476 2477
		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);
2478
		if (tcph->ece && tcph->cwr)
2479
			opt2 |= CCTRL_ECN_V(1);
2480
	}
2481
	if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
2482
		u32 isn = (prandom_u32() & ~7UL) - 1;
2483
		opt2 |= T5_OPT_2_VALID_F;
2484
		opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
2485
		opt2 |= T5_ISS_F;
2486 2487 2488 2489 2490
		rpl5 = (void *)rpl;
		memset(&rpl5->iss, 0, roundup(sizeof(*rpl5)-sizeof(*rpl), 16));
		if (peer2peer)
			isn += 4;
		rpl5->iss = cpu_to_be32(isn);
2491
		pr_debug("iss %u\n", be32_to_cpu(rpl5->iss));
2492
	}
2493 2494 2495

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

2499
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
2500 2501
}

2502
static void reject_cr(struct c4iw_dev *dev, u32 hwtid, struct sk_buff *skb)
2503
{
2504
	pr_debug("c4iw_dev %p tid %u\n", dev, hwtid);
2505 2506 2507 2508 2509 2510 2511
	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)
{
2512
	struct c4iw_ep *child_ep = NULL, *parent_ep;
2513
	struct cpl_pass_accept_req *req = cplhdr(skb);
2514
	unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
2515 2516 2517
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int hwtid = GET_TID(req);
	struct dst_entry *dst;
2518
	__u8 local_ip[16], peer_ip[16];
2519
	__be16 local_port, peer_port;
H
Hariprasad S 已提交
2520
	struct sockaddr_in6 *sin6;
2521
	int err;
2522
	u16 peer_mss = ntohs(req->tcpopt.mss);
2523
	int iptype;
2524
	unsigned short hdrs;
2525
	u8 tos;
2526

2527
	parent_ep = (struct c4iw_ep *)get_ep_from_stid(dev, stid);
2528
	if (!parent_ep) {
2529 2530
		pr_err("%s connect request on invalid stid %d\n",
		       __func__, stid);
2531 2532 2533
		goto reject;
	}

2534
	if (state_read(&parent_ep->com) != LISTEN) {
2535
		pr_err("%s - listening ep not in LISTEN\n", __func__);
2536 2537 2538
		goto reject;
	}

2539 2540 2541 2542 2543
	if (parent_ep->com.cm_id->tos_set)
		tos = parent_ep->com.cm_id->tos;
	else
		tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));

2544 2545
	cxgb_get_4tuple(req, parent_ep->com.dev->rdev.lldi.adapter_type,
			&iptype, local_ip, peer_ip, &local_port, &peer_port);
2546

2547
	/* Find output route */
2548
	if (iptype == 4)  {
2549 2550
		pr_debug("parent ep %p hwtid %u laddr %pI4 raddr %pI4 lport %d rport %d peer_mss %d\n"
			 , parent_ep, hwtid,
J
Joe Perches 已提交
2551 2552
			 local_ip, peer_ip, ntohs(local_port),
			 ntohs(peer_port), peer_mss);
2553 2554 2555
		dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
				      *(__be32 *)local_ip, *(__be32 *)peer_ip,
				      local_port, peer_port, tos);
2556
	} else {
2557 2558
		pr_debug("parent ep %p hwtid %u laddr %pI6 raddr %pI6 lport %d rport %d peer_mss %d\n"
			 , parent_ep, hwtid,
J
Joe Perches 已提交
2559 2560
			 local_ip, peer_ip, ntohs(local_port),
			 ntohs(peer_port), peer_mss);
2561 2562
		dst = cxgb_find_route6(&dev->rdev.lldi, get_real_dev,
				local_ip, peer_ip, local_port, peer_port,
2563
				tos,
2564 2565
				((struct sockaddr_in6 *)
				 &parent_ep->com.local_addr)->sin6_scope_id);
2566 2567
	}
	if (!dst) {
2568
		pr_err("%s - failed to find dst entry!\n", __func__);
2569 2570
		goto reject;
	}
2571 2572 2573

	child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
	if (!child_ep) {
2574
		pr_err("%s - failed to allocate ep entry!\n", __func__);
2575 2576 2577 2578
		dst_release(dst);
		goto reject;
	}

2579
	err = import_ep(child_ep, iptype, peer_ip, dst, dev, false,
H
Hariprasad S 已提交
2580
			parent_ep->com.dev->rdev.lldi.adapter_type, tos);
2581
	if (err) {
2582
		pr_err("%s - failed to allocate l2t entry!\n", __func__);
2583
		dst_release(dst);
2584
		kfree(child_ep);
2585 2586
		goto reject;
	}
2587

2588 2589
	hdrs = ((iptype == 4) ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)) +
	       sizeof(struct tcphdr) +
2590 2591 2592
	       ((enable_tcp_timestamps && req->tcpopt.tstamp) ? 12 : 0);
	if (peer_mss && child_ep->mtu > (peer_mss + hdrs))
		child_ep->mtu = peer_mss + hdrs;
2593

2594 2595 2596 2597
	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;

2598 2599 2600
	state_set(&child_ep->com, CONNECTING);
	child_ep->com.dev = dev;
	child_ep->com.cm_id = NULL;
2601

2602 2603
	if (iptype == 4) {
		struct sockaddr_in *sin = (struct sockaddr_in *)
2604
			&child_ep->com.local_addr;
2605

2606
		sin->sin_family = AF_INET;
2607 2608
		sin->sin_port = local_port;
		sin->sin_addr.s_addr = *(__be32 *)local_ip;
2609 2610

		sin = (struct sockaddr_in *)&child_ep->com.local_addr;
2611
		sin->sin_family = AF_INET;
2612 2613 2614 2615
		sin->sin_port = ((struct sockaddr_in *)
				 &parent_ep->com.local_addr)->sin_port;
		sin->sin_addr.s_addr = *(__be32 *)local_ip;

2616
		sin = (struct sockaddr_in *)&child_ep->com.remote_addr;
2617
		sin->sin_family = AF_INET;
2618 2619 2620
		sin->sin_port = peer_port;
		sin->sin_addr.s_addr = *(__be32 *)peer_ip;
	} else {
2621
		sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2622 2623 2624
		sin6->sin6_family = PF_INET6;
		sin6->sin6_port = local_port;
		memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
2625 2626 2627 2628 2629 2630 2631

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

2632
		sin6 = (struct sockaddr_in6 *)&child_ep->com.remote_addr;
2633 2634 2635 2636
		sin6->sin6_family = PF_INET6;
		sin6->sin6_port = peer_port;
		memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
	}
2637

2638 2639
	c4iw_get_ep(&parent_ep->com);
	child_ep->parent_ep = parent_ep;
H
Hariprasad S 已提交
2640
	child_ep->tos = tos;
2641 2642 2643
	child_ep->dst = dst;
	child_ep->hwtid = hwtid;

2644
	pr_debug("tx_chan %u smac_idx %u rss_qid %u\n",
J
Joe Perches 已提交
2645
		 child_ep->tx_chan, child_ep->smac_idx, child_ep->rss_qid);
2646

2647
	timer_setup(&child_ep->timer, ep_timeout, 0);
2648 2649
	cxgb4_insert_tid(t, child_ep, hwtid,
			 child_ep->com.local_addr.ss_family);
2650
	insert_ep_tid(child_ep);
2651 2652 2653 2654 2655 2656
	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 已提交
2657
	if (iptype == 6) {
2658
		sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
H
Hariprasad S 已提交
2659 2660 2661
		cxgb4_clip_get(child_ep->com.dev->rdev.lldi.ports[0],
			       (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
2662
	goto out;
2663 2664
fail:
	c4iw_put_ep(&child_ep->com);
2665
reject:
2666
	reject_cr(dev, hwtid, skb);
2667
out:
2668 2669
	if (parent_ep)
		c4iw_put_ep(&parent_ep->com);
2670 2671 2672 2673 2674 2675 2676 2677
	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);
2678
	int ret;
2679
	u16 tcp_opt = ntohs(req->tcp_opt);
2680

2681
	ep = get_ep_from_tid(dev, tid);
2682
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
2683 2684
	ep->snd_seq = be32_to_cpu(req->snd_isn);
	ep->rcv_seq = be32_to_cpu(req->rcv_isn);
2685
	ep->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
2686

2687
	pr_debug("ep %p hwtid %u tcp_opt 0x%02x\n", ep, tid, tcp_opt);
2688

2689
	set_emss(ep, tcp_opt);
2690 2691

	dst_confirm(ep->dst);
2692 2693
	mutex_lock(&ep->com.mutex);
	ep->com.state = MPA_REQ_WAIT;
2694
	start_ep_timer(ep);
2695
	set_bit(PASS_ESTAB, &ep->com.history);
2696
	ret = send_flowc(ep);
2697 2698 2699
	mutex_unlock(&ep->com.mutex);
	if (ret)
		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
2700
	c4iw_put_ep(&ep->com);
2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712

	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 已提交
2713
	int ret;
2714

2715 2716 2717 2718
	ep = get_ep_from_tid(dev, tid);
	if (!ep)
		return 0;

2719
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
2720 2721
	dst_confirm(ep->dst);

2722
	set_bit(PEER_CLOSE, &ep->com.history);
2723
	mutex_lock(&ep->com.mutex);
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
	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 已提交
2741
		pr_debug("waking up ep %p tid %u\n", ep, ep->hwtid);
2742
		c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
2743 2744 2745
		break;
	case MPA_REP_SENT:
		__state_set(&ep->com, CLOSING);
J
Joe Perches 已提交
2746
		pr_debug("waking up ep %p tid %u\n", ep, ep->hwtid);
2747
		c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
2748 2749
		break;
	case FPDU_MODE:
2750
		start_ep_timer(ep);
2751
		__state_set(&ep->com, CLOSING);
2752
		attrs.next_state = C4IW_QP_STATE_CLOSING;
S
Steve Wise 已提交
2753
		ret = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2754
				       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
S
Steve Wise 已提交
2755 2756 2757 2758
		if (ret != -ECONNRESET) {
			peer_close_upcall(ep);
			disconnect = 1;
		}
2759 2760 2761 2762 2763 2764 2765 2766 2767
		break;
	case ABORTING:
		disconnect = 0;
		break;
	case CLOSING:
		__state_set(&ep->com, MORIBUND);
		disconnect = 0;
		break;
	case MORIBUND:
S
Steve Wise 已提交
2768
		(void)stop_ep_timer(ep);
2769 2770 2771 2772 2773
		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);
		}
2774
		close_complete_upcall(ep, 0);
2775 2776 2777 2778 2779 2780 2781 2782
		__state_set(&ep->com, DEAD);
		release = 1;
		disconnect = 0;
		break;
	case DEAD:
		disconnect = 0;
		break;
	default:
S
Steve Wise 已提交
2783
		WARN_ONCE(1, "Bad endpoint state %u\n", ep->com.state);
2784
	}
2785
	mutex_unlock(&ep->com.mutex);
2786 2787 2788 2789
	if (disconnect)
		c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
	if (release)
		release_ep_resources(ep);
2790
	c4iw_put_ep(&ep->com);
2791 2792 2793
	return 0;
}

2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808
static void finish_peer_abort(struct c4iw_dev *dev, struct c4iw_ep *ep)
{
	complete_cached_srq_buffers(ep, ep->srqe_idx);
	if (ep->com.cm_id && ep->com.qp) {
		struct c4iw_qp_attributes attrs;

		attrs.next_state = C4IW_QP_STATE_ERROR;
		c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
			       C4IW_QP_ATTR_NEXT_STATE,	&attrs, 1);
	}
	peer_abort_upcall(ep);
	release_ep_resources(ep);
	c4iw_put_ep(&ep->com);
}

2809 2810
static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
{
2811
	struct cpl_abort_req_rss6 *req = cplhdr(skb);
2812 2813 2814 2815 2816 2817
	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);
2818
	u8 status;
2819
	u32 srqidx;
2820

2821
	u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
2822

2823 2824 2825 2826
	ep = get_ep_from_tid(dev, tid);
	if (!ep)
		return 0;

2827 2828 2829
	status = ABORT_RSS_STATUS_G(be32_to_cpu(req->srqidx_status));

	if (cxgb_is_neg_adv(status)) {
2830
		pr_debug("Negative advice on abort- tid %u status %d (%s)\n",
2831
			 ep->hwtid, status, neg_adv_str(status));
2832 2833 2834 2835
		ep->stats.abort_neg_adv++;
		mutex_lock(&dev->rdev.stats.lock);
		dev->rdev.stats.neg_adv++;
		mutex_unlock(&dev->rdev.stats.lock);
2836
		goto deref_ep;
2837
	}
2838

2839
	pr_debug("ep %p tid %u state %u\n", ep, ep->hwtid,
J
Joe Perches 已提交
2840
		 ep->com.state);
2841
	set_bit(PEER_ABORT, &ep->com.history);
2842 2843 2844

	/*
	 * Wake up any threads in rdma_init() or rdma_fini().
2845 2846
	 * However, this is not needed if com state is just
	 * MPA_REQ_SENT
2847
	 */
2848
	if (ep->com.state != MPA_REQ_SENT)
2849
		c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
2850 2851

	mutex_lock(&ep->com.mutex);
2852 2853
	switch (ep->com.state) {
	case CONNECTING:
2854
		c4iw_put_ep(&ep->parent_ep->com);
2855 2856
		break;
	case MPA_REQ_WAIT:
S
Steve Wise 已提交
2857
		(void)stop_ep_timer(ep);
2858 2859
		break;
	case MPA_REQ_SENT:
S
Steve Wise 已提交
2860
		(void)stop_ep_timer(ep);
2861 2862
		if (status != CPL_ERR_CONN_RESET || mpa_rev == 1 ||
		    (mpa_rev == 2 && ep->tried_with_mpa_v1))
2863 2864 2865 2866 2867 2868 2869 2870 2871 2872
			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
			 */
2873 2874
			pr_info("%s: mpa_rev=%d. Retrying with mpav1\n",
				__func__, mpa_rev);
2875 2876
			ep->retry_with_mpa_v1 = 1;
		}
2877 2878 2879 2880 2881 2882 2883
		break;
	case MPA_REP_SENT:
		break;
	case MPA_REQ_RCVD:
		break;
	case MORIBUND:
	case CLOSING:
2884
		stop_ep_timer(ep);
2885 2886
		/*FALLTHROUGH*/
	case FPDU_MODE:
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
		if (ep->com.qp && ep->com.qp->srq) {
			srqidx = ABORT_RSS_SRQIDX_G(
					be32_to_cpu(req->srqidx_status));
			if (srqidx) {
				complete_cached_srq_buffers(ep,
							    req->srqidx_status);
			} else {
				/* Hold ep ref until finish_peer_abort() */
				c4iw_get_ep(&ep->com);
				__state_set(&ep->com, ABORTING);
				set_bit(PEER_ABORT_IN_PROGRESS, &ep->com.flags);
				read_tcb(ep);
				break;

			}
		}

2904 2905 2906 2907 2908 2909
		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)
2910
				pr_err("%s - qp <- error failed!\n", __func__);
2911 2912 2913 2914 2915 2916
		}
		peer_abort_upcall(ep);
		break;
	case ABORTING:
		break;
	case DEAD:
2917
		pr_warn("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
2918
		mutex_unlock(&ep->com.mutex);
2919
		goto deref_ep;
2920
	default:
S
Steve Wise 已提交
2921
		WARN_ONCE(1, "Bad endpoint state %u\n", ep->com.state);
2922 2923 2924 2925 2926
		break;
	}
	dst_confirm(ep->dst);
	if (ep->com.state != ABORTING) {
		__state_set(&ep->com, DEAD);
2927 2928 2929
		/* we don't release if we want to retry with mpa_v1 */
		if (!ep->retry_with_mpa_v1)
			release = 1;
2930
	}
2931
	mutex_unlock(&ep->com.mutex);
2932

2933 2934
	rpl_skb = skb_dequeue(&ep->com.ep_skb_list);
	if (WARN_ON(!rpl_skb)) {
2935 2936 2937
		release = 1;
		goto out;
	}
2938 2939 2940

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

2941 2942 2943 2944
	c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
out:
	if (release)
		release_ep_resources(ep);
2945
	else if (ep->retry_with_mpa_v1) {
H
Hariprasad S 已提交
2946 2947 2948
		if (ep->com.remote_addr.ss_family == AF_INET6) {
			struct sockaddr_in6 *sin6 =
					(struct sockaddr_in6 *)
2949
					&ep->com.local_addr;
H
Hariprasad S 已提交
2950 2951 2952 2953 2954
			cxgb4_clip_release(
					ep->com.dev->rdev.lldi.ports[0],
					(const u32 *)&sin6->sin6_addr.s6_addr,
					1);
		}
2955
		xa_erase_irq(&ep->com.dev->hwtids, ep->hwtid);
2956 2957
		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid,
				 ep->com.local_addr.ss_family);
2958 2959 2960 2961 2962
		dst_release(ep->dst);
		cxgb4_l2t_release(ep->l2t);
		c4iw_reconnect(ep);
	}

2963 2964 2965 2966
deref_ep:
	c4iw_put_ep(&ep->com);
	/* Dereferencing ep, referenced in peer_abort_intr() */
	c4iw_put_ep(&ep->com);
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977
	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);

2978 2979 2980
	ep = get_ep_from_tid(dev, tid);
	if (!ep)
		return 0;
2981

2982
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
2983 2984

	/* The cm_id may be null if we failed to connect */
2985
	mutex_lock(&ep->com.mutex);
2986
	set_bit(CLOSE_CON_RPL, &ep->com.history);
2987 2988 2989 2990 2991
	switch (ep->com.state) {
	case CLOSING:
		__state_set(&ep->com, MORIBUND);
		break;
	case MORIBUND:
S
Steve Wise 已提交
2992
		(void)stop_ep_timer(ep);
2993 2994 2995 2996 2997 2998 2999
		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);
		}
3000
		close_complete_upcall(ep, 0);
3001 3002 3003 3004 3005 3006 3007
		__state_set(&ep->com, DEAD);
		release = 1;
		break;
	case ABORTING:
	case DEAD:
		break;
	default:
S
Steve Wise 已提交
3008
		WARN_ONCE(1, "Bad endpoint state %u\n", ep->com.state);
3009 3010
		break;
	}
3011
	mutex_unlock(&ep->com.mutex);
3012 3013
	if (release)
		release_ep_resources(ep);
3014
	c4iw_put_ep(&ep->com);
3015 3016 3017 3018 3019
	return 0;
}

static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
{
3020 3021 3022 3023
	struct cpl_rdma_terminate *rpl = cplhdr(skb);
	unsigned int tid = GET_TID(rpl);
	struct c4iw_ep *ep;
	struct c4iw_qp_attributes attrs;
3024

3025
	ep = get_ep_from_tid(dev, tid);
3026

3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
	if (ep) {
		if (ep->com.qp) {
			pr_warn("TERM received tid %u qpid %u\n", tid,
				ep->com.qp->wq.sq.qid);
			attrs.next_state = C4IW_QP_STATE_TERMINATE;
			c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
				       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
		}

		c4iw_put_ep(&ep->com);
3037
	} else
3038
		pr_warn("TERM received tid %u no ep/qp\n", tid);
3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055

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


3056 3057 3058
	ep = get_ep_from_tid(dev, tid);
	if (!ep)
		return 0;
3059 3060
	pr_debug("ep %p tid %u credits %u\n",
		 ep, ep->hwtid, credits);
3061
	if (credits == 0) {
3062 3063
		pr_debug("0 credit ack ep %p tid %u state %u\n",
			 ep, ep->hwtid, state_read(&ep->com));
3064
		goto out;
3065 3066 3067 3068
	}

	dst_confirm(ep->dst);
	if (ep->mpa_skb) {
3069 3070 3071
		pr_debug("last streaming msg ack ep %p tid %u state %u initiator %u freeing skb\n",
			 ep, ep->hwtid, state_read(&ep->com),
			 ep->mpa_attr.initiator ? 1 : 0);
3072
		mutex_lock(&ep->com.mutex);
3073 3074
		kfree_skb(ep->mpa_skb);
		ep->mpa_skb = NULL;
3075 3076 3077
		if (test_bit(STOP_MPA_TIMER, &ep->com.flags))
			stop_ep_timer(ep);
		mutex_unlock(&ep->com.mutex);
3078
	}
3079 3080
out:
	c4iw_put_ep(&ep->com);
3081 3082 3083 3084 3085
	return 0;
}

int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
{
3086
	int abort;
3087
	struct c4iw_ep *ep = to_ep(cm_id);
3088

3089
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
3090

3091
	mutex_lock(&ep->com.mutex);
3092
	if (ep->com.state != MPA_REQ_RCVD) {
3093
		mutex_unlock(&ep->com.mutex);
3094 3095 3096
		c4iw_put_ep(&ep->com);
		return -ECONNRESET;
	}
3097
	set_bit(ULP_REJECT, &ep->com.history);
3098
	if (mpa_rev == 0)
3099 3100 3101
		abort = 1;
	else
		abort = send_mpa_reject(ep, pdata, pdata_len);
3102
	mutex_unlock(&ep->com.mutex);
3103 3104 3105

	stop_ep_timer(ep);
	c4iw_ep_disconnect(ep, abort != 0, GFP_KERNEL);
3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117
	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);
3118
	int abort = 0;
3119

3120
	pr_debug("ep %p tid %u\n", ep, ep->hwtid);
3121 3122

	mutex_lock(&ep->com.mutex);
3123
	if (ep->com.state != MPA_REQ_RCVD) {
3124
		err = -ECONNRESET;
3125
		goto err_out;
3126 3127
	}

S
Steve Wise 已提交
3128 3129 3130 3131
	if (!qp) {
		err = -EINVAL;
		goto err_out;
	}
3132

3133
	set_bit(ULP_ACCEPT, &ep->com.history);
3134 3135
	if ((conn_param->ord > cur_max_read_depth(ep->com.dev)) ||
	    (conn_param->ird > cur_max_read_depth(ep->com.dev))) {
3136
		err = -EINVAL;
3137
		goto err_abort;
3138 3139
	}

3140 3141
	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
		if (conn_param->ord > ep->ird) {
3142
			if (RELAXED_IRD_NEGOTIATION) {
3143
				conn_param->ord = ep->ird;
3144 3145 3146 3147 3148 3149
			} 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;
3150
				goto err_abort;
3151
			}
3152
		}
3153 3154 3155 3156 3157
		if (conn_param->ird < ep->ord) {
			if (RELAXED_IRD_NEGOTIATION &&
			    ep->ord <= h->rdev.lldi.max_ordird_qp) {
				conn_param->ird = ep->ord;
			} else {
3158
				err = -ENOMEM;
3159
				goto err_abort;
3160 3161 3162
			}
		}
	}
3163 3164 3165
	ep->ird = conn_param->ird;
	ep->ord = conn_param->ord;

3166
	if (ep->mpa_attr.version == 1) {
3167 3168
		if (peer2peer && ep->ird == 0)
			ep->ird = 1;
3169 3170 3171
	} else {
		if (peer2peer &&
		    (ep->mpa_attr.p2p_type != FW_RI_INIT_P2PTYPE_DISABLED) &&
3172
		    (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) && ep->ird == 0)
3173 3174
			ep->ird = 1;
	}
3175

3176
	pr_debug("ird %d ord %d\n", ep->ird, ep->ord);
3177

3178
	ep->com.cm_id = cm_id;
3179
	ref_cm_id(&ep->com);
3180
	ep->com.qp = qp;
3181
	ref_qp(ep);
3182

3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199
	/* 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)
3200
		goto err_deref_cm_id;
3201 3202

	set_bit(STOP_MPA_TIMER, &ep->com.flags);
3203 3204 3205
	err = send_mpa_reply(ep, conn_param->private_data,
			     conn_param->private_data_len);
	if (err)
3206
		goto err_deref_cm_id;
3207

3208
	__state_set(&ep->com, FPDU_MODE);
3209
	established_upcall(ep);
3210
	mutex_unlock(&ep->com.mutex);
3211 3212
	c4iw_put_ep(&ep->com);
	return 0;
3213
err_deref_cm_id:
3214
	deref_cm_id(&ep->com);
3215 3216 3217
err_abort:
	abort = 1;
err_out:
3218
	mutex_unlock(&ep->com.mutex);
3219 3220
	if (abort)
		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
3221 3222 3223 3224
	c4iw_put_ep(&ep->com);
	return err;
}

3225 3226 3227 3228
static int pick_local_ipaddrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
{
	struct in_device *ind;
	int found = 0;
3229 3230
	struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
	struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274

	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);
3275 3276
	struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)&cm_id->m_local_addr;
	struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)&cm_id->m_remote_addr;
3277

3278
	if (!get_lladdr(dev->rdev.lldi.ports[0], &addr, IFA_F_TENTATIVE)) {
3279 3280 3281 3282 3283 3284 3285
		memcpy(la6->sin6_addr.s6_addr, &addr, 16);
		memcpy(ra6->sin6_addr.s6_addr, &addr, 16);
		return 0;
	}
	return -EADDRNOTAVAIL;
}

3286 3287 3288 3289
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;
3290
	int err = 0;
3291 3292 3293 3294
	struct sockaddr_in *laddr;
	struct sockaddr_in *raddr;
	struct sockaddr_in6 *laddr6;
	struct sockaddr_in6 *raddr6;
3295 3296
	__u8 *ra;
	int iptype;
3297

3298 3299
	if ((conn_param->ord > cur_max_read_depth(dev)) ||
	    (conn_param->ird > cur_max_read_depth(dev))) {
3300 3301 3302
		err = -EINVAL;
		goto out;
	}
3303 3304
	ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
	if (!ep) {
3305
		pr_err("%s - cannot alloc ep\n", __func__);
3306 3307 3308
		err = -ENOMEM;
		goto out;
	}
3309 3310 3311 3312 3313 3314 3315

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

3316
	timer_setup(&ep->timer, ep_timeout, 0);
3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327
	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;
3328
	ref_cm_id(&ep->com);
3329
	cm_id->provider_data = ep;
3330
	ep->com.dev = dev;
3331
	ep->com.qp = get_qhp(dev, conn_param->qpn);
3332
	if (!ep->com.qp) {
3333
		pr_warn("%s qpn 0x%x not found!\n", __func__, conn_param->qpn);
3334
		err = -EINVAL;
3335
		goto fail2;
3336
	}
3337
	ref_qp(ep);
3338
	pr_debug("qpn 0x%x qp %p cm_id %p\n", conn_param->qpn,
J
Joe Perches 已提交
3339
		 ep->com.qp, cm_id);
3340 3341 3342 3343 3344 3345

	/*
	 * Allocate an active TID to initiate a TCP connection.
	 */
	ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
	if (ep->atid == -1) {
3346
		pr_err("%s - cannot alloc atid\n", __func__);
3347
		err = -ENOMEM;
3348
		goto fail2;
3349
	}
3350 3351 3352
	err = xa_insert_irq(&dev->atids, ep->atid, ep, GFP_KERNEL);
	if (err)
		goto fail5;
3353

3354
	memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3355
	       sizeof(ep->com.local_addr));
3356
	memcpy(&ep->com.remote_addr, &cm_id->m_remote_addr,
3357 3358
	       sizeof(ep->com.remote_addr));

3359 3360 3361 3362
	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;
3363

3364
	if (cm_id->m_remote_addr.ss_family == AF_INET) {
3365 3366
		iptype = 4;
		ra = (__u8 *)&raddr->sin_addr;
3367

3368 3369 3370
		/*
		 * Handle loopback requests to INADDR_ANY.
		 */
3371
		if (raddr->sin_addr.s_addr == htonl(INADDR_ANY)) {
3372 3373
			err = pick_local_ipaddrs(dev, cm_id);
			if (err)
3374
				goto fail2;
3375 3376 3377
		}

		/* find a route */
3378 3379
		pr_debug("saddr %pI4 sport 0x%x raddr %pI4 rport 0x%x\n",
			 &laddr->sin_addr, ntohs(laddr->sin_port),
J
Joe Perches 已提交
3380
			 ra, ntohs(raddr->sin_port));
3381 3382 3383 3384 3385
		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);
3386 3387 3388 3389 3390 3391 3392 3393 3394 3395
	} 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)
3396
				goto fail2;
3397 3398 3399
		}

		/* find a route */
3400 3401
		pr_debug("saddr %pI6 sport 0x%x raddr %pI6 rport 0x%x\n",
			 laddr6->sin6_addr.s6_addr,
J
Joe Perches 已提交
3402 3403
			 ntohs(laddr6->sin6_port),
			 raddr6->sin6_addr.s6_addr, ntohs(raddr6->sin6_port));
3404 3405 3406 3407
		ep->dst = cxgb_find_route6(&dev->rdev.lldi, get_real_dev,
					   laddr6->sin6_addr.s6_addr,
					   raddr6->sin6_addr.s6_addr,
					   laddr6->sin6_port,
3408
					   raddr6->sin6_port, cm_id->tos,
3409
					   raddr6->sin6_scope_id);
3410 3411
	}
	if (!ep->dst) {
3412
		pr_err("%s - cannot find route\n", __func__);
3413
		err = -EHOSTUNREACH;
3414
		goto fail3;
3415 3416
	}

3417
	err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, true,
H
Hariprasad S 已提交
3418
			ep->com.dev->rdev.lldi.adapter_type, cm_id->tos);
3419
	if (err) {
3420
		pr_err("%s - cannot alloc l2e\n", __func__);
3421
		goto fail4;
3422 3423
	}

3424 3425
	pr_debug("txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
		 ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
J
Joe Perches 已提交
3426
		 ep->l2t->idx);
3427 3428

	state_set(&ep->com, CONNECTING);
H
Hariprasad S 已提交
3429
	ep->tos = cm_id->tos;
3430 3431 3432 3433 3434 3435 3436

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

	cxgb4_l2t_release(ep->l2t);
3437
fail4:
3438
	dst_release(ep->dst);
3439
fail3:
3440 3441
	xa_erase_irq(&ep->com.dev->atids, ep->atid);
fail5:
3442
	cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
3443 3444
fail2:
	skb_queue_purge(&ep->com.ep_skb_list);
3445
	deref_cm_id(&ep->com);
3446
fail1:
3447 3448 3449 3450 3451
	c4iw_put_ep(&ep->com);
out:
	return err;
}

3452 3453 3454
static int create_server6(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
{
	int err;
3455
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
3456
				    &ep->com.local_addr;
3457

3458 3459 3460 3461 3462 3463
	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;
	}
3464
	c4iw_init_wr_wait(ep->com.wr_waitp);
3465 3466 3467 3468 3469 3470
	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,
3471
					  ep->com.wr_waitp,
3472
					  0, 0, __func__);
3473 3474
	else if (err > 0)
		err = net_xmit_errno(err);
3475 3476 3477
	if (err) {
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3478 3479 3480
		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));
3481
	}
3482 3483 3484 3485 3486 3487
	return err;
}

static int create_server4(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
{
	int err;
3488
	struct sockaddr_in *sin = (struct sockaddr_in *)
3489
				  &ep->com.local_addr;
3490 3491 3492 3493 3494 3495 3496 3497

	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) {
3498 3499 3500 3501
				if (c4iw_fatal_error(&ep->com.dev->rdev)) {
					err = -EIO;
					break;
				}
3502 3503 3504 3505 3506
				set_current_state(TASK_UNINTERRUPTIBLE);
				schedule_timeout(usecs_to_jiffies(100));
			}
		} while (err == -EBUSY);
	} else {
3507
		c4iw_init_wr_wait(ep->com.wr_waitp);
3508 3509 3510 3511 3512
		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,
3513
						  ep->com.wr_waitp,
3514
						  0, 0, __func__);
3515 3516
		else if (err > 0)
			err = net_xmit_errno(err);
3517 3518 3519 3520 3521 3522 3523 3524
	}
	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;
}

3525 3526 3527 3528 3529 3530 3531 3532 3533 3534
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) {
3535
		pr_err("%s - cannot alloc ep\n", __func__);
3536 3537 3538
		err = -ENOMEM;
		goto fail1;
	}
3539
	skb_queue_head_init(&ep->com.ep_skb_list);
3540
	pr_debug("ep %p\n", ep);
3541
	ep->com.cm_id = cm_id;
3542
	ref_cm_id(&ep->com);
3543 3544
	ep->com.dev = dev;
	ep->backlog = backlog;
3545
	memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3546
	       sizeof(ep->com.local_addr));
3547 3548 3549 3550

	/*
	 * Allocate a server TID.
	 */
3551 3552
	if (dev->rdev.lldi.enable_fw_ofld_conn &&
	    ep->com.local_addr.ss_family == AF_INET)
3553
		ep->stid = cxgb4_alloc_sftid(dev->rdev.lldi.tids,
3554
					     cm_id->m_local_addr.ss_family, ep);
3555
	else
3556
		ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids,
3557
					    cm_id->m_local_addr.ss_family, ep);
3558

3559
	if (ep->stid == -1) {
3560
		pr_err("%s - cannot alloc stid\n", __func__);
3561 3562 3563
		err = -ENOMEM;
		goto fail2;
	}
3564
	insert_handle(dev, &dev->stid_idr, ep, ep->stid);
3565

3566
	state_set(&ep->com, LISTEN);
3567 3568 3569 3570
	if (ep->com.local_addr.ss_family == AF_INET)
		err = create_server4(dev, ep);
	else
		err = create_server6(dev, ep);
3571 3572 3573 3574
	if (!err) {
		cm_id->provider_data = ep;
		goto out;
	}
3575
	remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
3576 3577
	cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
			ep->com.local_addr.ss_family);
3578
fail2:
3579
	deref_cm_id(&ep->com);
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590
	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);

3591
	pr_debug("ep %p\n", ep);
3592 3593 3594

	might_sleep();
	state_set(&ep->com, DEAD);
3595 3596
	if (ep->com.dev->rdev.lldi.enable_fw_ofld_conn &&
	    ep->com.local_addr.ss_family == AF_INET) {
3597 3598 3599 3600
		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 已提交
3601
		struct sockaddr_in6 *sin6;
3602
		c4iw_init_wr_wait(ep->com.wr_waitp);
3603 3604 3605
		err = cxgb4_remove_server(
				ep->com.dev->rdev.lldi.ports[0], ep->stid,
				ep->com.dev->rdev.lldi.rxq_ids[0], 0);
3606 3607
		if (err)
			goto done;
3608
		err = c4iw_wait_for_reply(&ep->com.dev->rdev, ep->com.wr_waitp,
3609
					  0, 0, __func__);
3610
		sin6 = (struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
3611 3612
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3613
	}
3614
	remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
3615 3616
	cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
			ep->com.local_addr.ss_family);
3617
done:
3618
	deref_cm_id(&ep->com);
3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629
	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;

3630
	mutex_lock(&ep->com.mutex);
3631

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

3635 3636 3637 3638 3639 3640
	/*
	 * Ref the ep here in case we have fatal errors causing the
	 * ep to be released and freed.
	 */
	c4iw_get_ep(&ep->com);

3641 3642 3643
	rdev = &ep->com.dev->rdev;
	if (c4iw_fatal_error(rdev)) {
		fatal = 1;
3644
		close_complete_upcall(ep, -EIO);
3645 3646 3647 3648 3649 3650 3651 3652
		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:
3653
	case CONNECTING:
3654 3655 3656 3657 3658
		close = 1;
		if (abrupt)
			ep->com.state = ABORTING;
		else {
			ep->com.state = CLOSING;
3659 3660 3661 3662 3663 3664 3665 3666 3667 3668

			/*
			 * 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);
			}
3669
			start_ep_timer(ep);
3670 3671 3672 3673 3674 3675 3676
		}
		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 已提交
3677
				(void)stop_ep_timer(ep);
3678 3679 3680 3681 3682 3683 3684 3685
				ep->com.state = ABORTING;
			} else
				ep->com.state = MORIBUND;
		}
		break;
	case MORIBUND:
	case ABORTING:
	case DEAD:
3686 3687
		pr_debug("ignoring disconnect ep %p state %u\n",
			 ep, ep->com.state);
3688 3689
		break;
	default:
S
Steve Wise 已提交
3690
		WARN_ONCE(1, "Bad endpoint state %u\n", ep->com.state);
3691 3692 3693 3694
		break;
	}

	if (close) {
S
Steve Wise 已提交
3695
		if (abrupt) {
3696
			set_bit(EP_DISC_ABORT, &ep->com.history);
3697
			ret = send_abort(ep);
3698 3699
		} else {
			set_bit(EP_DISC_CLOSE, &ep->com.history);
3700
			ret = send_halfclose(ep);
3701
		}
3702
		if (ret) {
3703
			set_bit(EP_DISC_FAIL, &ep->com.history);
3704 3705 3706 3707
			if (!abrupt) {
				stop_ep_timer(ep);
				close_complete_upcall(ep, -EIO);
			}
3708 3709 3710 3711 3712 3713 3714 3715 3716
			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)
3717
					pr_err("%s - qp <- error failed!\n",
3718 3719
					       __func__);
			}
3720
			fatal = 1;
3721
		}
3722
	}
S
Steve Wise 已提交
3723
	mutex_unlock(&ep->com.mutex);
3724
	c4iw_put_ep(&ep->com);
3725 3726 3727 3728 3729
	if (fatal)
		release_ep_resources(ep);
	return ret;
}

3730 3731 3732 3733
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;
3734
	int atid = be32_to_cpu(req->tid);
3735

3736 3737
	ep = (struct c4iw_ep *)lookup_atid(dev->rdev.lldi.tids,
					   (__force u32) req->tid);
3738 3739 3740 3741 3742
	if (!ep)
		return;

	switch (req->retval) {
	case FW_ENOMEM:
3743 3744 3745 3746 3747
		set_bit(ACT_RETRY_NOMEM, &ep->com.history);
		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
			send_fw_act_open_req(ep, atid);
			return;
		}
3748
		/* fall through */
3749
	case FW_EADDRINUSE:
3750 3751 3752 3753 3754
		set_bit(ACT_RETRY_INUSE, &ep->com.history);
		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
			send_fw_act_open_req(ep, atid);
			return;
		}
3755 3756 3757 3758 3759 3760
		break;
	default:
		pr_info("%s unexpected ofld conn wr retval %d\n",
		       __func__, req->retval);
		break;
	}
3761 3762 3763 3764 3765
	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);
3766
	connect_reply_upcall(ep, status2errno(req->retval));
3767
	state_set(&ep->com, DEAD);
H
Hariprasad S 已提交
3768 3769
	if (ep->com.remote_addr.ss_family == AF_INET6) {
		struct sockaddr_in6 *sin6 =
3770
			(struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
3771 3772 3773
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
3774
	xa_erase_irq(&dev->atids, atid);
3775 3776 3777 3778
	cxgb4_free_atid(dev->rdev.lldi.tids, atid);
	dst_release(ep->dst);
	cxgb4_l2t_release(ep->l2t);
	c4iw_put_ep(&ep->com);
3779 3780 3781 3782 3783 3784 3785 3786 3787
}

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;

3788
	rpl_skb = (struct sk_buff *)(unsigned long)req->cookie;
3789
	if (req->retval) {
3790
		pr_err("%s passive open failure %d\n", __func__, req->retval);
3791 3792 3793
		mutex_lock(&dev->rdev.stats.lock);
		dev->rdev.stats.pas_ofld_conn_fails++;
		mutex_unlock(&dev->rdev.stats.lock);
3794 3795 3796 3797
		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,
3798 3799
					(__force u32) htonl(
					(__force u32) req->tid)));
3800 3801 3802 3803 3804 3805 3806
		ret = pass_accept_req(dev, rpl_skb);
		if (!ret)
			kfree_skb(rpl_skb);
	}
	return;
}

3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880
static inline u64 t4_tcb_get_field64(__be64 *tcb, u16 word)
{
	u64 tlo = be64_to_cpu(tcb[((31 - word) / 2)]);
	u64 thi = be64_to_cpu(tcb[((31 - word) / 2) - 1]);
	u64 t;
	u32 shift = 32;

	t = (thi << shift) | (tlo >> shift);

	return t;
}

static inline u32 t4_tcb_get_field32(__be64 *tcb, u16 word, u32 mask, u32 shift)
{
	u32 v;
	u64 t = be64_to_cpu(tcb[(31 - word) / 2]);

	if (word & 0x1)
		shift += 32;
	v = (t >> shift) & mask;
	return v;
}

static int read_tcb_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_get_tcb_rpl *rpl = cplhdr(skb);
	__be64 *tcb = (__be64 *)(rpl + 1);
	unsigned int tid = GET_TID(rpl);
	struct c4iw_ep *ep;
	u64 t_flags_64;
	u32 rx_pdu_out;

	ep = get_ep_from_tid(dev, tid);
	if (!ep)
		return 0;
	/* Examine the TF_RX_PDU_OUT (bit 49 of the t_flags) in order to
	 * determine if there's a rx PDU feedback event pending.
	 *
	 * If that bit is set, it means we'll need to re-read the TCB's
	 * rq_start value. The final value is the one present in a TCB
	 * with the TF_RX_PDU_OUT bit cleared.
	 */

	t_flags_64 = t4_tcb_get_field64(tcb, TCB_T_FLAGS_W);
	rx_pdu_out = (t_flags_64 & TF_RX_PDU_OUT_V(1)) >> TF_RX_PDU_OUT_S;

	c4iw_put_ep(&ep->com); /* from get_ep_from_tid() */
	c4iw_put_ep(&ep->com); /* from read_tcb() */

	/* If TF_RX_PDU_OUT bit is set, re-read the TCB */
	if (rx_pdu_out) {
		if (++ep->rx_pdu_out_cnt >= 2) {
			WARN_ONCE(1, "tcb re-read() reached the guard limit, finishing the cleanup\n");
			goto cleanup;
		}
		read_tcb(ep);
		return 0;
	}

	ep->srqe_idx = t4_tcb_get_field32(tcb, TCB_RQ_START_W, TCB_RQ_START_W,
			TCB_RQ_START_S);
cleanup:
	pr_debug("ep %p tid %u %016x\n", ep, ep->hwtid, ep->srqe_idx);

	if (test_bit(PEER_ABORT_IN_PROGRESS, &ep->com.flags))
		finish_peer_abort(dev, ep);
	else if (test_bit(ABORT_REQ_IN_PROGRESS, &ep->com.flags))
		send_abort_req(ep);
	else
		WARN_ONCE(1, "unexpected state!");

	return 0;
}

3881
static int deferred_fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
3882 3883
{
	struct cpl_fw6_msg *rpl = cplhdr(skb);
3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910
	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)
{
3911 3912 3913 3914
	__be32 l2info;
	__be16 hdr_len, vlantag, len;
	u16 eth_hdr_len;
	int tcp_hdr_len, ip_hdr_len;
3915 3916 3917 3918
	u8 intf;
	struct cpl_rx_pkt *cpl = cplhdr(skb);
	struct cpl_pass_accept_req *req;
	struct tcp_options_received tmp_opt;
3919
	struct c4iw_dev *dev;
3920
	enum chip_type type;
3921

3922
	dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
3923
	/* Store values from cpl_rx_pkt in temporary location. */
3924 3925 3926 3927
	vlantag = cpl->vlan;
	len = cpl->len;
	l2info  = cpl->l2info;
	hdr_len = cpl->hdr_len;
3928 3929 3930 3931 3932 3933 3934 3935 3936 3937
	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);
3938
	tcp_parse_options(&init_net, skb, &tmp_opt, 0, NULL);
3939

3940
	req = __skb_push(skb, sizeof(*req));
3941
	memset(req, 0, sizeof(*req));
3942 3943
	req->l2info = cpu_to_be16(SYN_INTF_V(intf) |
			 SYN_MAC_IDX_V(RX_MACIDX_G(
3944
			 be32_to_cpu(l2info))) |
3945
			 SYN_XACT_MATCH_F);
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965
	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;
3966 3967
	req->tos_stid = cpu_to_be32(PASS_OPEN_TID_V(stid) |
				    PASS_OPEN_TOS_V(tos));
3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986
	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);
3987
	int ret;
3988 3989

	req_skb = alloc_skb(sizeof(struct fw_ofld_connection_wr), GFP_KERNEL);
3990 3991
	if (!req_skb)
		return;
3992
	req = __skb_put_zero(req_skb, sizeof(*req));
3993
	req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR) | FW_WR_COMPL_F);
3994
	req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
3995
	req->le.version_cpl = htonl(FW_OFLD_CONNECTION_WR_CPL_F);
3996
	req->le.filter = (__force __be32) filter;
3997 3998 3999 4000 4001 4002 4003
	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 =
4004 4005 4006
		 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(
4007
			PASS_OPEN_TID_G(ntohl(cpl->tos_stid))));
4008 4009 4010 4011 4012

	/*
	 * We store the qid in opt2 which will be used by the firmware
	 * to send us the wr response.
	 */
4013
	req->tcb.opt2 = htonl(RSS_QUEUE_V(rss_qid));
4014 4015 4016 4017 4018 4019 4020

	/*
	 * 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.
	 */
4021
	req->tcb.opt0 = cpu_to_be64(MSS_IDX_V(0xF));
H
Hariprasad S 已提交
4022
	req->cookie = (uintptr_t)skb;
4023 4024

	set_wr_txq(req_skb, CPL_PRIORITY_CONTROL, port_id);
4025 4026 4027 4028 4029 4030 4031
	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);
	}
4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053
}

/*
 * 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;
4054
	struct c4iw_ep *lep = NULL;
4055 4056 4057
	u16 window;
	struct port_info *pi;
	struct net_device *pdev;
4058
	u16 rss_qid, eth_hdr_len;
4059 4060 4061 4062
	int step;
	struct neighbour *neigh;

	/* Drop all non-SYN packets */
4063
	if (!(cpl->l2info & cpu_to_be32(RXF_SYN_F)))
4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075
		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.
	 */
4076
	stid = (__force int) cpu_to_be32((__force u32) rss->hash_val);
4077

4078
	lep = (struct c4iw_ep *)get_ep_from_stid(dev, stid);
4079
	if (!lep) {
4080 4081
		pr_warn("%s connect request on invalid stid %d\n",
			__func__, stid);
4082 4083 4084
		goto reject;
	}

4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100
	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;
	}

4101
	if (eth_hdr_len == ETH_HLEN) {
4102 4103 4104 4105 4106
		eh = (struct ethhdr *)(req + 1);
		iph = (struct iphdr *)(eh + 1);
	} else {
		vlan_eh = (struct vlan_ethhdr *)(req + 1);
		iph = (struct iphdr *)(vlan_eh + 1);
4107
		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(cpl->vlan));
4108 4109 4110 4111 4112 4113 4114 4115 4116 4117
	}

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

4118
	pr_debug("lip 0x%x lport %u pip 0x%x pport %u tos %d\n",
J
Joe Perches 已提交
4119 4120
		 ntohl(iph->daddr), ntohs(tcph->dest), ntohl(iph->saddr),
		 ntohs(tcph->source), iph->tos);
4121

4122 4123 4124
	dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
			      iph->daddr, iph->saddr, tcph->dest,
			      tcph->source, iph->tos);
4125
	if (!dst) {
4126
		pr_err("%s - failed to find dst entry!\n", __func__);
4127 4128 4129 4130
		goto reject;
	}
	neigh = dst_neigh_lookup_skb(dst, skb);

4131
	if (!neigh) {
4132
		pr_err("%s - failed to allocate neigh!\n", __func__);
4133 4134 4135
		goto free_dst;
	}

4136 4137 4138 4139 4140 4141 4142
	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);
		dev_put(pdev);
	} else {
4143
		pdev = get_real_dev(neigh->dev);
4144
		e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
4145 4146
					pdev, 0);
		pi = (struct port_info *)netdev_priv(pdev);
4147
	}
4148
	neigh_release(neigh);
4149 4150 4151 4152 4153 4154 4155 4156
	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];
4157
	window = (__force u16) htons((__force u16)tcph->window);
4158 4159

	/* Calcuate filter portion for LE region. */
4160 4161 4162
	filter = (__force unsigned int) cpu_to_be32(cxgb4_select_ntuple(
						    dev->rdev.lldi.ports[0],
						    e));
4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176

	/*
	 * 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:
4177 4178
	if (lep)
		c4iw_put_ep(&lep->com);
4179 4180 4181
	return 0;
}

4182 4183 4184 4185
/*
 * These are the real handlers that are called from a
 * work queue.
 */
4186
static c4iw_handler_func work_handlers[NUM_CPL_CMDS + NUM_FAKE_CPLS] = {
4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199
	[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,
4200
	[CPL_FW4_ACK] = fw4_ack,
4201
	[CPL_GET_TCB_RPL] = read_tcb_rpl,
4202
	[CPL_FW6_MSG] = deferred_fw6_msg,
4203
	[CPL_RX_PKT] = rx_pkt,
4204 4205
	[FAKE_CPL_PUT_EP_SAFE] = _put_ep_safe,
	[FAKE_CPL_PASS_PUT_EP_SAFE] = _put_pass_ep_safe
4206 4207 4208 4209 4210 4211 4212
};

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

4213
	mutex_lock(&ep->com.mutex);
4214
	pr_debug("ep %p tid %u state %d\n", ep, ep->hwtid, ep->com.state);
4215
	set_bit(TIMEDOUT, &ep->com.history);
4216 4217 4218 4219 4220
	switch (ep->com.state) {
	case MPA_REQ_SENT:
		connect_reply_upcall(ep, -ETIMEDOUT);
		break;
	case MPA_REQ_WAIT:
4221
	case MPA_REQ_RCVD:
4222
	case MPA_REP_SENT:
4223
	case FPDU_MODE:
4224 4225 4226 4227 4228 4229 4230 4231 4232
		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);
		}
4233
		close_complete_upcall(ep, -ETIMEDOUT);
4234
		break;
S
Steve Wise 已提交
4235 4236 4237 4238 4239 4240 4241 4242 4243 4244
	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;
4245
	default:
J
Julia Lawall 已提交
4246
		WARN(1, "%s unexpected state ep %p tid %u state %u\n",
4247 4248 4249
			__func__, ep, ep->hwtid, ep->com.state);
		abort = 0;
	}
4250
	mutex_unlock(&ep->com.mutex);
4251 4252
	if (abort)
		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265
	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 已提交
4266 4267
		tmp->next = NULL;
		tmp->prev = NULL;
4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279
		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;
4280
	struct cpl_act_establish *rpl;
4281 4282 4283
	unsigned int opcode;
	int ret;

S
Steve Wise 已提交
4284
	process_timedout_eps();
4285 4286 4287 4288 4289
	while ((skb = skb_dequeue(&rxq))) {
		rpl = cplhdr(skb);
		dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
		opcode = rpl->ot.opcode;

4290 4291 4292
		if (opcode >= ARRAY_SIZE(work_handlers) ||
		    !work_handlers[opcode]) {
			pr_err("No handler for opcode 0x%x.\n", opcode);
4293
			kfree_skb(skb);
4294 4295 4296 4297 4298
		} else {
			ret = work_handlers[opcode](dev, skb);
			if (!ret)
				kfree_skb(skb);
		}
S
Steve Wise 已提交
4299
		process_timedout_eps();
4300 4301 4302 4303 4304
	}
}

static DECLARE_WORK(skb_work, process_work);

4305
static void ep_timeout(struct timer_list *t)
4306
{
4307
	struct c4iw_ep *ep = from_timer(ep, t, timer);
4308
	int kickit = 0;
4309 4310

	spin_lock(&timeout_lock);
4311
	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
S
Steve Wise 已提交
4312 4313 4314 4315 4316 4317 4318
		/*
		 * Only insert if it is not already on the list.
		 */
		if (!ep->entry.next) {
			list_add_tail(&ep->entry, &timeout_list);
			kickit = 1;
		}
4319
	}
4320
	spin_unlock(&timeout_lock);
4321 4322
	if (kickit)
		queue_work(workq, &skb_work);
4323 4324
}

4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348
/*
 * 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) {
4349 4350
		pr_err("Unexpected SET_TCB_RPL status %u for tid %u\n",
		       rpl->status, GET_TID(rpl));
4351
	}
4352
	kfree_skb(skb);
4353 4354 4355
	return 0;
}

4356 4357 4358 4359 4360 4361
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;

4362
	pr_debug("type %u\n", rpl->type);
4363 4364

	switch (rpl->type) {
4365
	case FW6_TYPE_WR_RPL:
4366
		ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
4367
		wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
4368
		pr_debug("wr_waitp %p ret %u\n", wr_waitp, ret);
4369
		if (wr_waitp)
4370
			c4iw_wake_up_deref(wr_waitp, ret ? -ret : 0);
4371
		kfree_skb(skb);
4372
		break;
4373 4374
	case FW6_TYPE_CQE:
	case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
4375
		sched(dev, skb);
4376
		break;
4377
	default:
4378 4379
		pr_err("%s unexpected fw6 msg type %u\n",
		       __func__, rpl->type);
4380
		kfree_skb(skb);
4381 4382 4383 4384 4385
		break;
	}
	return 0;
}

S
Steve Wise 已提交
4386 4387 4388 4389 4390 4391
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);

4392 4393
	ep = get_ep_from_tid(dev, tid);
	/* This EP will be dereferenced in peer_abort() */
4394
	if (!ep) {
4395
		pr_warn("Abort on non-existent endpoint, tid %d\n", tid);
4396 4397 4398
		kfree_skb(skb);
		return 0;
	}
4399
	if (cxgb_is_neg_adv(req->status)) {
4400 4401
		pr_debug("Negative advice on abort- tid %u status %d (%s)\n",
			 ep->hwtid, req->status,
J
Joe Perches 已提交
4402
			 neg_adv_str(req->status));
4403
		goto out;
S
Steve Wise 已提交
4404
	}
4405
	pr_debug("ep %p tid %u state %u\n", ep, ep->hwtid, ep->com.state);
S
Steve Wise 已提交
4406

4407
	c4iw_wake_up_noref(ep->com.wr_waitp, -ECONNRESET);
4408
out:
S
Steve Wise 已提交
4409 4410 4411 4412
	sched(dev, skb);
	return 0;
}

4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428
/*
 * 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 已提交
4429
	[CPL_ABORT_REQ_RSS] = peer_abort_intr,
4430 4431 4432
	[CPL_RDMA_TERMINATE] = sched,
	[CPL_FW4_ACK] = sched,
	[CPL_SET_TCB_RPL] = set_tcb_rpl,
4433
	[CPL_GET_TCB_RPL] = sched,
4434 4435
	[CPL_FW6_MSG] = fw6_msg,
	[CPL_RX_PKT] = sched
4436 4437
};

4438 4439
int __init c4iw_cm_init(void)
{
4440
	spin_lock_init(&timeout_lock);
4441 4442
	skb_queue_head_init(&rxq);

4443
	workq = alloc_ordered_workqueue("iw_cxgb4", WQ_MEM_RECLAIM);
4444 4445 4446 4447 4448 4449
	if (!workq)
		return -ENOMEM;

	return 0;
}

4450
void c4iw_cm_term(void)
4451
{
4452
	WARN_ON(!list_empty(&timeout_list));
4453 4454 4455
	flush_workqueue(workq);
	destroy_workqueue(workq);
}