cm.c 113.8 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 "iw_cxgb4.h"
H
Hariprasad S 已提交
53
#include "clip_tbl.h"
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

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

71 72 73 74 75 76 77 78
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 已提交
79
static int dack_mode = 1;
80
module_param(dack_mode, int, 0644);
S
Steve Wise 已提交
81
MODULE_PARM_DESC(dack_mode, "Delayed ack mode (default=1)");
82

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

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

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

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

int c4iw_debug;
module_param(c4iw_debug, int, 0644);
MODULE_PARM_DESC(c4iw_debug, "Enable debug logging (default=0)");

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

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

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

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

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

static struct workqueue_struct *workq;

static struct sk_buff_head rxq;

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

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

153 154 155 156 157 158 159 160 161 162 163 164 165
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);
}

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

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

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

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

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);
		PDBG("%s - device in error state - dropping\n", __func__);
		return -EIO;
	}
	error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
	if (error < 0)
		kfree_skb(skb);
220
	return error < 0 ? error : 0;
221 222 223 224 225 226 227 228 229 230 231 232 233 234
}

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

	if (c4iw_fatal_error(rdev)) {
		kfree_skb(skb);
		PDBG("%s - device in error state - dropping\n", __func__);
		return -EIO;
	}
	error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
	if (error < 0)
		kfree_skb(skb);
235
	return error < 0 ? error : 0;
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
}

static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
{
	struct cpl_tid_release *req;

	skb = get_skb(skb, sizeof *req, GFP_KERNEL);
	if (!skb)
		return;
	req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
	INIT_TP_WR(req, hwtid);
	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
	set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
	c4iw_ofld_send(rdev, skb);
	return;
}

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

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

275
	mutex_lock(&epc->mutex);
276
	state = epc->state;
277
	mutex_unlock(&epc->mutex);
278 279 280 281 282 283 284 285 286 287
	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)
{
288
	mutex_lock(&epc->mutex);
289 290
	PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
	__state_set(epc, new);
291
	mutex_unlock(&epc->mutex);
292 293 294 295 296 297 298 299 300 301
	return;
}

static void *alloc_ep(int size, gfp_t gfp)
{
	struct c4iw_ep_common *epc;

	epc = kzalloc(size, gfp);
	if (epc) {
		kref_init(&epc->kref);
302
		mutex_init(&epc->mutex);
303
		c4iw_init_wr_wait(&epc->wr_wait);
304 305 306 307 308 309 310 311 312 313
	}
	PDBG("%s alloc ep %p\n", __func__, epc);
	return epc;
}

void _c4iw_free_ep(struct kref *kref)
{
	struct c4iw_ep *ep;

	ep = container_of(kref, struct c4iw_ep, com.kref);
314
	PDBG("%s ep %p state %s\n", __func__, ep, states[ep->com.state]);
315 316
	if (test_bit(QP_REFERENCED, &ep->com.flags))
		deref_qp(ep);
317
	if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
H
Hariprasad S 已提交
318 319 320
		if (ep->com.remote_addr.ss_family == AF_INET6) {
			struct sockaddr_in6 *sin6 =
					(struct sockaddr_in6 *)
321
					&ep->com.local_addr;
H
Hariprasad S 已提交
322 323 324 325 326 327

			cxgb4_clip_release(
					ep->com.dev->rdev.lldi.ports[0],
					(const u32 *)&sin6->sin6_addr.s6_addr,
					1);
		}
328
		remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid);
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
		dst_release(ep->dst);
		cxgb4_l2t_release(ep->l2t);
	}
	kfree(ep);
}

static void release_ep_resources(struct c4iw_ep *ep)
{
	set_bit(RELEASE_RESOURCES, &ep->com.flags);
	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);
	}
374
	t4_set_arp_err_handler(skb, NULL, NULL);
375 376 377
	return skb;
}

378 379
static struct net_device *get_real_dev(struct net_device *egress_dev)
{
S
Steve Wise 已提交
380
	return rdma_vlan_dev_real_dev(egress_dev) ? : egress_dev;
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
}

static int our_interface(struct c4iw_dev *dev, struct net_device *egress_dev)
{
	int i;

	egress_dev = get_real_dev(egress_dev);
	for (i = 0; i < dev->rdev.lldi.nports; i++)
		if (dev->rdev.lldi.ports[i] == egress_dev)
			return 1;
	return 0;
}

static struct dst_entry *find_route6(struct c4iw_dev *dev, __u8 *local_ip,
				     __u8 *peer_ip, __be16 local_port,
				     __be16 peer_port, u8 tos,
				     __u32 sin6_scope_id)
{
	struct dst_entry *dst = NULL;

	if (IS_ENABLED(CONFIG_IPV6)) {
		struct flowi6 fl6;

		memset(&fl6, 0, sizeof(fl6));
		memcpy(&fl6.daddr, peer_ip, 16);
		memcpy(&fl6.saddr, local_ip, 16);
		if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
			fl6.flowi6_oif = sin6_scope_id;
		dst = ip6_route_output(&init_net, NULL, &fl6);
		if (!dst)
			goto out;
		if (!our_interface(dev, ip6_dst_idev(dst)->dev) &&
		    !(ip6_dst_idev(dst)->dev->flags & IFF_LOOPBACK)) {
			dst_release(dst);
			dst = NULL;
		}
	}

out:
	return dst;
}

static struct dst_entry *find_route(struct c4iw_dev *dev, __be32 local_ip,
424 425 426 427
				 __be32 peer_ip, __be16 local_port,
				 __be16 peer_port, u8 tos)
{
	struct rtable *rt;
428
	struct flowi4 fl4;
429
	struct neighbour *n;
430

431
	rt = ip_route_output_ports(&init_net, &fl4, NULL, peer_ip, local_ip,
432 433
				   peer_port, local_port, IPPROTO_TCP,
				   tos, 0);
434
	if (IS_ERR(rt))
435
		return NULL;
436 437 438
	n = dst_neigh_lookup(&rt->dst, &peer_ip);
	if (!n)
		return NULL;
439 440
	if (!our_interface(dev, n->dev) &&
	    !(n->dev->flags & IFF_LOOPBACK)) {
441
		neigh_release(n);
442 443 444 445 446
		dst_release(&rt->dst);
		return NULL;
	}
	neigh_release(n);
	return &rt->dst;
447 448 449 450
}

static void arp_failure_discard(void *handle, struct sk_buff *skb)
{
451
	pr_err(MOD "ARP failure\n");
452 453 454
	kfree_skb(skb);
}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
enum {
	NUM_FAKE_CPLS = 1,
	FAKE_CPL_PUT_EP_SAFE = NUM_CPL_CMDS + 0,
};

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);
	return 0;
}

/*
 * 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.
 */
static void queue_arp_failure_cpl(struct c4iw_ep *ep, struct sk_buff *skb)
{
	struct cpl_act_establish *rpl = cplhdr(skb);

	/* Set our special ARP_FAILURE opcode */
	rpl->ot.opcode = FAKE_CPL_PUT_EP_SAFE;

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

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

	__state_set(&ep->com, DEAD);
	queue_arp_failure_cpl(ep, skb);
}

502 503 504 505 506
/*
 * Handle an ARP failure for an active open.
 */
static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
{
507 508
	struct c4iw_ep *ep = handle;

M
Masanari Iida 已提交
509
	printk(KERN_ERR MOD "ARP failure during connect\n");
510
	connect_reply_upcall(ep, -EHOSTUNREACH);
511
	__state_set(&ep->com, DEAD);
H
Hariprasad S 已提交
512 513
	if (ep->com.remote_addr.ss_family == AF_INET6) {
		struct sockaddr_in6 *sin6 =
514
			(struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
515 516 517
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
518 519
	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
	cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
520
	queue_arp_failure_cpl(ep, skb);
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
}

/*
 * 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)
{
	struct c4iw_rdev *rdev = handle;
	struct cpl_abort_req *req = cplhdr(skb);

	PDBG("%s rdev %p\n", __func__, rdev);
	req->cmd = CPL_ABORT_NO_RST;
	c4iw_ofld_send(rdev, skb);
}

537
static int send_flowc(struct c4iw_ep *ep, struct sk_buff *skb)
538 539 540 541
{
	unsigned int flowclen = 80;
	struct fw_flowc_wr *flowc;
	int i;
H
Hariprasad S 已提交
542 543 544 545 546 547 548
	u16 vlan = ep->l2t->vlan;
	int nparams;

	if (vlan == CPL_L2T_VLAN_NONE)
		nparams = 8;
	else
		nparams = 9;
549 550 551 552

	skb = get_skb(skb, flowclen, GFP_KERNEL);
	flowc = (struct fw_flowc_wr *)__skb_put(skb, flowclen);

553
	flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
H
Hariprasad S 已提交
554
					   FW_FLOWC_WR_NPARAMS_V(nparams));
555 556
	flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(DIV_ROUND_UP(flowclen,
					  16)) | FW_WR_FLOWID_V(ep->hwtid));
557 558

	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
559
	flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
560
					    (ep->com.dev->rdev.lldi.pf));
561 562 563 564 565 566 567 568 569 570 571
	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;
572
	flowc->mnemval[6].val = cpu_to_be32(ep->snd_win);
573 574
	flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
	flowc->mnemval[7].val = cpu_to_be32(ep->emss);
H
Hariprasad S 已提交
575 576 577 578 579 580 581 582 583 584 585
	if (nparams == 9) {
		u16 pri;

		pri = (vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
		flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
		flowc->mnemval[8].val = cpu_to_be32(pri);
	} else {
		/* Pad WR to 16 byte boundary */
		flowc->mnemval[8].mnemonic = 0;
		flowc->mnemval[8].val = 0;
	}
586 587 588 589 590 591 592
	for (i = 0; i < 9; i++) {
		flowc->mnemval[i].r4[0] = 0;
		flowc->mnemval[i].r4[1] = 0;
		flowc->mnemval[i].r4[2] = 0;
	}

	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
593
	return c4iw_ofld_send(&ep->com.dev->rdev, skb);
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
}

static int send_halfclose(struct c4iw_ep *ep, gfp_t gfp)
{
	struct cpl_close_con_req *req;
	struct sk_buff *skb;
	int wrlen = roundup(sizeof *req, 16);

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	skb = get_skb(NULL, wrlen, gfp);
	if (!skb) {
		printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
		return -ENOMEM;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
	t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
	req = (struct cpl_close_con_req *) skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	INIT_TP_WR(req, ep->hwtid);
	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ,
						    ep->hwtid));
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
}

static int send_abort(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
{
	struct cpl_abort_req *req;
	int wrlen = roundup(sizeof *req, 16);

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	skb = get_skb(skb, wrlen, gfp);
	if (!skb) {
		printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
		       __func__);
		return -ENOMEM;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
	t4_set_arp_err_handler(skb, &ep->com.dev->rdev, abort_arp_failure);
	req = (struct cpl_abort_req *) skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	INIT_TP_WR(req, ep->hwtid);
	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
	req->cmd = CPL_ABORT_SEND_RST;
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
}

640
static void best_mtu(const unsigned short *mtus, unsigned short mtu,
641
		     unsigned int *idx, int use_ts, int ipv6)
642
{
643 644 645
	unsigned short hdr_size = (ipv6 ?
				   sizeof(struct ipv6hdr) :
				   sizeof(struct iphdr)) +
646
				  sizeof(struct tcphdr) +
647 648
				  (use_ts ?
				   round_up(TCPOLEN_TIMESTAMP, 4) : 0);
649 650 651 652 653
	unsigned short data_size = mtu - hdr_size;

	cxgb4_best_aligned_mtu(mtus, hdr_size, data_size, 8, idx);
}

654 655
static int send_connect(struct c4iw_ep *ep)
{
656 657 658 659 660 661
	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;
662 663 664 665 666
	struct sk_buff *skb;
	u64 opt0;
	u32 opt2;
	unsigned int mtu_idx;
	int wscale;
667
	int win, sizev4, sizev6, wrlen;
668
	struct sockaddr_in *la = (struct sockaddr_in *)
669
				 &ep->com.local_addr;
670
	struct sockaddr_in *ra = (struct sockaddr_in *)
671
				 &ep->com.remote_addr;
672
	struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)
673
				   &ep->com.local_addr;
674
	struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)
675
				   &ep->com.remote_addr;
H
Hariprasad S 已提交
676
	int ret;
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
	u32 isn = (prandom_u32() & ~7UL) - 1;

	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;
	}
698 699 700 701

	wrlen = (ep->com.remote_addr.ss_family == AF_INET) ?
			roundup(sizev4, 16) :
			roundup(sizev6, 16);
702 703 704 705 706 707 708 709 710

	PDBG("%s ep %p atid %u\n", __func__, ep, ep->atid);

	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
		printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
		       __func__);
		return -ENOMEM;
	}
S
Steve Wise 已提交
711
	set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
712

713
	best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
714 715
		 enable_tcp_timestamps,
		 (AF_INET == ep->com.remote_addr.ss_family) ? 0 : 1);
716
	wscale = compute_wscale(rcv_win);
717 718 719 720 721 722

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

726
	opt0 = (nocong ? NO_CONG_F : 0) |
727
	       KEEP_ALIVE_F |
728
	       DELACK_F |
729 730 731 732 733
	       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 已提交
734
	       DSCP_V(ep->tos >> 2) |
735 736 737
	       ULP_MODE_V(ULP_MODE_TCPDDP) |
	       RCV_BUFSIZ_V(win);
	opt2 = RX_CHANNEL_V(0) |
738
	       CCTRL_ECN_V(enable_ecn) |
739
	       RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
740
	if (enable_tcp_timestamps)
741
		opt2 |= TSTAMPS_EN_F;
742
	if (enable_tcp_sack)
743
		opt2 |= SACK_EN_F;
744
	if (wscale && enable_tcp_window_scaling)
745
		opt2 |= WND_SCALE_EN_F;
746 747 748 749
	if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
		if (peer2peer)
			isn += 4;

750
		opt2 |= T5_OPT_2_VALID_F;
751
		opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
752
		opt2 |= T5_ISS_F;
753
	}
H
Hariprasad S 已提交
754 755 756 757 758

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

759
	t4_set_arp_err_handler(skb, ep, act_open_req_arp_failure);
760

761 762 763 764
	if (ep->com.remote_addr.ss_family == AF_INET) {
		switch (CHELSIO_CHIP_VERSION(adapter_type)) {
		case CHELSIO_T4:
			req = (struct cpl_act_open_req *)skb_put(skb, wrlen);
765
			INIT_TP_WR(req, 0);
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
			break;
		case CHELSIO_T5:
			t5req = (struct cpl_t5_act_open_req *)skb_put(skb,
					wrlen);
			INIT_TP_WR(t5req, 0);
			req = (struct cpl_act_open_req *)t5req;
			break;
		case CHELSIO_T6:
			t6req = (struct cpl_t6_act_open_req *)skb_put(skb,
					wrlen);
			INIT_TP_WR(t6req, 0);
			req = (struct cpl_act_open_req *)t6req;
			t5req = (struct cpl_t5_act_open_req *)t6req;
			break;
		default:
			pr_err("T%d Chip is not supported\n",
			       CHELSIO_CHIP_VERSION(adapter_type));
			ret = -EINVAL;
			goto clip_release;
		}

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

		if (is_t4(ep->com.dev->rdev.lldi.adapter_type)) {
796 797 798
			req->params = cpu_to_be32(cxgb4_select_ntuple(
						ep->com.dev->rdev.lldi.ports[0],
						ep->l2t));
799 800
			req->opt2 = cpu_to_be32(opt2);
		} else {
801 802 803 804 805 806 807 808 809 810 811
			t5req->params = cpu_to_be64(FILTER_TUPLE_V(
						cxgb4_select_ntuple(
						ep->com.dev->rdev.lldi.ports[0],
						ep->l2t)));
			t5req->rsvd = cpu_to_be32(isn);
			PDBG("%s snd_isn %u\n", __func__, t5req->rsvd);
			t5req->opt2 = cpu_to_be32(opt2);
		}
	} else {
		switch (CHELSIO_CHIP_VERSION(adapter_type)) {
		case CHELSIO_T4:
812 813
			req6 = (struct cpl_act_open_req6 *)skb_put(skb, wrlen);
			INIT_TP_WR(req6, 0);
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
			break;
		case CHELSIO_T5:
			t5req6 = (struct cpl_t5_act_open_req6 *)skb_put(skb,
					wrlen);
			INIT_TP_WR(t5req6, 0);
			req6 = (struct cpl_act_open_req6 *)t5req6;
			break;
		case CHELSIO_T6:
			t6req6 = (struct cpl_t6_act_open_req6 *)skb_put(skb,
					wrlen);
			INIT_TP_WR(t6req6, 0);
			req6 = (struct cpl_act_open_req6 *)t6req6;
			t5req6 = (struct cpl_t5_act_open_req6 *)t6req6;
			break;
		default:
			pr_err("T%d Chip is not supported\n",
			       CHELSIO_CHIP_VERSION(adapter_type));
			ret = -EINVAL;
			goto clip_release;
		}

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

		if (is_t4(ep->com.dev->rdev.lldi.adapter_type)) {
846 847 848
			req6->params = cpu_to_be32(cxgb4_select_ntuple(
						ep->com.dev->rdev.lldi.ports[0],
						ep->l2t));
849 850
			req6->opt2 = cpu_to_be32(opt2);
		} else {
851 852
			t5req6->params = cpu_to_be64(FILTER_TUPLE_V(
						cxgb4_select_ntuple(
853
						ep->com.dev->rdev.lldi.ports[0],
854
						ep->l2t)));
855 856 857
			t5req6->rsvd = cpu_to_be32(isn);
			PDBG("%s snd_isn %u\n", __func__, t5req6->rsvd);
			t5req6->opt2 = cpu_to_be32(opt2);
858
		}
859 860
	}

861
	set_bit(ACT_OPEN_REQ, &ep->com.history);
H
Hariprasad S 已提交
862
	ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
863
clip_release:
H
Hariprasad S 已提交
864 865 866 867
	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;
868 869
}

870 871
static void send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb,
		u8 mpa_rev_to_use)
872 873 874 875
{
	int mpalen, wrlen;
	struct fw_ofld_tx_data_wr *req;
	struct mpa_message *mpa;
876
	struct mpa_v2_conn_params mpa_v2_params;
877 878 879 880 881 882

	PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);

	BUG_ON(skb_cloned(skb));

	mpalen = sizeof(*mpa) + ep->plen;
883 884
	if (mpa_rev_to_use == 2)
		mpalen += sizeof(struct mpa_v2_conn_params);
885 886 887 888 889 890 891 892 893 894 895
	wrlen = roundup(mpalen + sizeof *req, 16);
	skb = get_skb(skb, wrlen, GFP_KERNEL);
	if (!skb) {
		connect_reply_upcall(ep, -ENOMEM);
		return;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);

	req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	req->op_to_immdlen = cpu_to_be32(
896 897 898
		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
		FW_WR_COMPL_F |
		FW_WR_IMMDLEN_V(mpalen));
899
	req->flowid_len16 = cpu_to_be32(
900 901
		FW_WR_FLOWID_V(ep->hwtid) |
		FW_WR_LEN16_V(wrlen >> 4));
902 903
	req->plen = cpu_to_be32(mpalen);
	req->tunnel_to_proxy = cpu_to_be32(
904 905
		FW_OFLD_TX_DATA_WR_FLUSH_F |
		FW_OFLD_TX_DATA_WR_SHOVE_F);
906 907 908 909

	mpa = (struct mpa_message *)(req + 1);
	memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
	mpa->flags = (crc_enabled ? MPA_CRC : 0) |
910 911
		     (markers_enabled ? MPA_MARKERS : 0) |
		     (mpa_rev_to_use == 2 ? MPA_ENHANCED_RDMA_CONN : 0);
912
	mpa->private_data_size = htons(ep->plen);
913
	mpa->revision = mpa_rev_to_use;
914
	if (mpa_rev_to_use == 1) {
915
		ep->tried_with_mpa_v1 = 1;
916 917
		ep->retry_with_mpa_v1 = 0;
	}
918 919

	if (mpa_rev_to_use == 2) {
920 921
		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
					       sizeof (struct mpa_v2_conn_params));
922 923
		PDBG("%s initiator ird %u ord %u\n", __func__, ep->ird,
		     ep->ord);
924 925 926 927 928 929 930 931 932 933 934 935 936 937
		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));
938

939 940 941 942 943 944 945 946
		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);
947 948 949 950 951 952 953 954 955 956 957 958

	/*
	 * Reference the mpa skb.  This ensures the data area
	 * will remain in memory until the hw acks the tx.
	 * Function fw4_ack() will deref it.
	 */
	skb_get(skb);
	t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
	BUG_ON(ep->mpa_skb);
	ep->mpa_skb = skb;
	c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
	start_ep_timer(ep);
959
	__state_set(&ep->com, MPA_REQ_SENT);
960
	ep->mpa_attr.initiator = 1;
961
	ep->snd_seq += mpalen;
962 963 964 965 966 967 968 969 970
	return;
}

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;
971
	struct mpa_v2_conn_params mpa_v2_params;
972 973 974 975

	PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);

	mpalen = sizeof(*mpa) + plen;
976 977
	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
		mpalen += sizeof(struct mpa_v2_conn_params);
978 979 980 981 982 983 984 985 986 987 988 989
	wrlen = roundup(mpalen + sizeof *req, 16);

	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
		printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
		return -ENOMEM;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);

	req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	req->op_to_immdlen = cpu_to_be32(
990 991 992
		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
		FW_WR_COMPL_F |
		FW_WR_IMMDLEN_V(mpalen));
993
	req->flowid_len16 = cpu_to_be32(
994 995
		FW_WR_FLOWID_V(ep->hwtid) |
		FW_WR_LEN16_V(wrlen >> 4));
996 997
	req->plen = cpu_to_be32(mpalen);
	req->tunnel_to_proxy = cpu_to_be32(
998 999
		FW_OFLD_TX_DATA_WR_FLUSH_F |
		FW_OFLD_TX_DATA_WR_SHOVE_F);
1000 1001 1002 1003 1004

	mpa = (struct mpa_message *)(req + 1);
	memset(mpa, 0, sizeof(*mpa));
	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
	mpa->flags = MPA_REJECT;
1005
	mpa->revision = ep->mpa_attr.version;
1006
	mpa->private_data_size = htons(plen);
1007 1008 1009

	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1010 1011
		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
					       sizeof (struct mpa_v2_conn_params));
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
		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);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

	/*
	 * 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);
	t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
	BUG_ON(ep->mpa_skb);
	ep->mpa_skb = skb;
1041
	ep->snd_seq += mpalen;
1042 1043 1044 1045 1046 1047 1048 1049 1050
	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;
1051
	struct mpa_v2_conn_params mpa_v2_params;
1052 1053 1054 1055

	PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);

	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 1062 1063 1064 1065 1066 1067 1068 1069
	wrlen = roundup(mpalen + sizeof *req, 16);

	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
		printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
		return -ENOMEM;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);

	req = (struct fw_ofld_tx_data_wr *) skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	req->op_to_immdlen = cpu_to_be32(
1070 1071 1072
		FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
		FW_WR_COMPL_F |
		FW_WR_IMMDLEN_V(mpalen));
1073
	req->flowid_len16 = cpu_to_be32(
1074 1075
		FW_WR_FLOWID_V(ep->hwtid) |
		FW_WR_LEN16_V(wrlen >> 4));
1076 1077
	req->plen = cpu_to_be32(mpalen);
	req->tunnel_to_proxy = cpu_to_be32(
1078 1079
		FW_OFLD_TX_DATA_WR_FLUSH_F |
		FW_OFLD_TX_DATA_WR_SHOVE_F);
1080 1081 1082 1083 1084 1085

	mpa = (struct mpa_message *)(req + 1);
	memset(mpa, 0, sizeof(*mpa));
	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
	mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
		     (markers_enabled ? MPA_MARKERS : 0);
1086
	mpa->revision = ep->mpa_attr.version;
1087
	mpa->private_data_size = htons(plen);
1088 1089 1090

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

	/*
	 * 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;
1125
	__state_set(&ep->com, MPA_REP_SENT);
1126
	ep->snd_seq += mpalen;
1127 1128 1129 1130 1131 1132 1133 1134
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
}

static int act_establish(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct cpl_act_establish *req = cplhdr(skb);
	unsigned int tid = GET_TID(req);
1135
	unsigned int atid = TID_TID_G(ntohl(req->tos_atid));
1136
	struct tid_info *t = dev->rdev.lldi.tids;
1137
	int ret;
1138 1139 1140 1141 1142 1143

	ep = lookup_atid(t, atid);

	PDBG("%s ep %p tid %u snd_isn %u rcv_isn %u\n", __func__, ep, tid,
	     be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));

1144
	mutex_lock(&ep->com.mutex);
1145 1146 1147 1148 1149
	dst_confirm(ep->dst);

	/* setup the hwtid for this connection */
	ep->hwtid = tid;
	cxgb4_insert_tid(t, ep, tid);
1150
	insert_handle(dev, &dev->hwtid_idr, ep, ep->hwtid);
1151 1152 1153 1154 1155 1156 1157

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

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

	/* dealloc the atid */
1158
	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
1159
	cxgb4_free_atid(t, atid);
1160
	set_bit(ACT_ESTAB, &ep->com.history);
1161 1162

	/* start MPA negotiation */
1163 1164 1165
	ret = send_flowc(ep, NULL);
	if (ret)
		goto err;
1166 1167 1168 1169
	if (ep->retry_with_mpa_v1)
		send_mpa_req(ep, skb, 1);
	else
		send_mpa_req(ep, skb, mpa_rev);
1170
	mutex_unlock(&ep->com.mutex);
1171
	return 0;
1172 1173 1174 1175 1176
err:
	mutex_unlock(&ep->com.mutex);
	connect_reply_upcall(ep, -ENOMEM);
	c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
	return 0;
1177 1178
}

1179
static void close_complete_upcall(struct c4iw_ep *ep, int status)
1180 1181 1182 1183 1184 1185
{
	struct iw_cm_event event;

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CLOSE;
1186
	event.status = status;
1187 1188 1189 1190
	if (ep->com.cm_id) {
		PDBG("close complete delivered ep %p cm_id %p tid %u\n",
		     ep, ep->com.cm_id, ep->hwtid);
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1191
		deref_cm_id(&ep->com);
1192
		set_bit(CLOSE_UPCALL, &ep->com.history);
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	}
}

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

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_DISCONNECT;
	if (ep->com.cm_id) {
		PDBG("peer close delivered ep %p cm_id %p tid %u\n",
		     ep, ep->com.cm_id, ep->hwtid);
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1207
		set_bit(DISCONN_UPCALL, &ep->com.history);
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	}
}

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

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CLOSE;
	event.status = -ECONNRESET;
	if (ep->com.cm_id) {
		PDBG("abort delivered ep %p cm_id %p tid %u\n", ep,
		     ep->com.cm_id, ep->hwtid);
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1223
		deref_cm_id(&ep->com);
1224
		set_bit(ABORT_UPCALL, &ep->com.history);
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	}
}

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

	PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid, status);
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CONNECT_REPLY;
	event.status = status;
1236 1237 1238 1239
	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));
1240 1241

	if ((status == 0) || (status == -ECONNREFUSED)) {
1242 1243
		if (!ep->tried_with_mpa_v1) {
			/* this means MPA_v2 is used */
1244 1245
			event.ord = ep->ird;
			event.ird = ep->ord;
1246 1247 1248 1249 1250 1251 1252
			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 */
1253 1254
			event.ord = cur_max_read_depth(ep->com.dev);
			event.ird = cur_max_read_depth(ep->com.dev);
1255 1256 1257 1258
			event.private_data_len = ep->plen;
			event.private_data = ep->mpa_pkt +
				sizeof(struct mpa_message);
		}
1259
	}
1260 1261 1262

	PDBG("%s ep %p tid %u status %d\n", __func__, ep,
	     ep->hwtid, status);
1263
	set_bit(CONN_RPL_UPCALL, &ep->com.history);
1264 1265
	ep->com.cm_id->event_handler(ep->com.cm_id, &event);

1266 1267
	if (status < 0)
		deref_cm_id(&ep->com);
1268 1269
}

1270
static int connect_request_upcall(struct c4iw_ep *ep)
1271 1272
{
	struct iw_cm_event event;
1273
	int ret;
1274 1275 1276 1277

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_CONNECT_REQUEST;
1278 1279 1280 1281
	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));
1282
	event.provider_data = ep;
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
	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 */
1293 1294
		event.ord = cur_max_read_depth(ep->com.dev);
		event.ird = cur_max_read_depth(ep->com.dev);
1295 1296 1297
		event.private_data_len = ep->plen;
		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
	}
1298 1299 1300 1301 1302
	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);
1303
	set_bit(CONNREQ_UPCALL, &ep->com.history);
1304
	c4iw_put_ep(&ep->parent_ep->com);
1305
	return ret;
1306 1307 1308 1309 1310 1311 1312 1313 1314
}

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

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	memset(&event, 0, sizeof(event));
	event.event = IW_CM_EVENT_ESTABLISHED;
1315 1316
	event.ird = ep->ord;
	event.ord = ep->ird;
1317 1318 1319
	if (ep->com.cm_id) {
		PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1320
		set_bit(ESTAB_UPCALL, &ep->com.history);
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	}
}

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

	PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
	skb = get_skb(NULL, wrlen, GFP_KERNEL);
	if (!skb) {
		printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
		return 0;
	}

1337 1338 1339 1340 1341
	/*
	 * 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.
	 */
1342 1343
	if (ep->rcv_win > RCV_BUFSIZ_M * 1024)
		credits += ep->rcv_win - RCV_BUFSIZ_M * 1024;
1344

1345 1346 1347 1348 1349
	req = (struct cpl_rx_data_ack *) skb_put(skb, wrlen);
	memset(req, 0, wrlen);
	INIT_TP_WR(req, ep->hwtid);
	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
						    ep->hwtid));
1350
	req->credit_dack = cpu_to_be32(credits | RX_FORCE_ACK_F |
1351 1352
				       RX_DACK_CHANGE_F |
				       RX_DACK_MODE_V(dack_mode));
S
Steve Wise 已提交
1353
	set_wr_txq(skb, CPL_PRIORITY_ACK, ep->ctrlq_idx);
1354 1355 1356 1357
	c4iw_ofld_send(&ep->com.dev->rdev, skb);
	return credits;
}

1358 1359
#define RELAXED_IRD_NEGOTIATION 1

1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
/*
 * 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.
 */
1372
static int process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
1373 1374
{
	struct mpa_message *mpa;
1375
	struct mpa_v2_conn_params *mpa_v2_params;
1376
	u16 plen;
1377 1378
	u16 resp_ird, resp_ord;
	u8 rtr_mismatch = 0, insuff_ird = 0;
1379 1380 1381
	struct c4iw_qp_attributes attrs;
	enum c4iw_qp_attr_mask mask;
	int err;
1382
	int disconnect = 0;
1383 1384 1385 1386

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);

	/*
S
Steve Wise 已提交
1387 1388 1389
	 * Stop mpa timer.  If it expired, then
	 * we ignore the MPA reply.  process_timeout()
	 * will abort the connection.
1390
	 */
S
Steve Wise 已提交
1391
	if (stop_ep_timer(ep))
1392
		return 0;
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413

	/*
	 * 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;
		goto err;
	}

	/*
	 * 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))
1414
		return 0;
1415 1416 1417
	mpa = (struct mpa_message *) ep->mpa_pkt;

	/* Validate MPA header. */
1418 1419 1420
	if (mpa->revision > mpa_rev) {
		printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d,"
		       " Received = %d\n", __func__, mpa_rev, mpa->revision);
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
		err = -EPROTO;
		goto err;
	}
	if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
		err = -EPROTO;
		goto err;
	}

	plen = ntohs(mpa->private_data_size);

	/*
	 * Fail if there's too much private data.
	 */
	if (plen > MPA_MAX_PRIVATE_DATA) {
		err = -EPROTO;
		goto err;
	}

	/*
	 * If plen does not account for pkt size
	 */
	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
		err = -EPROTO;
		goto err;
	}

	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))
1454
		return 0;
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

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

	/*
	 * If we get here we have accumulated the entire mpa
	 * start reply message including private data. And
	 * the MPA header is valid.
	 */
1466
	__state_set(&ep->com, FPDU_MODE);
1467 1468 1469
	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;
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
	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;
1483 1484
			PDBG("%s responder ird %u ord %u ep ird %u ord %u\n",
			     __func__, resp_ird, resp_ord, ep->ird, ep->ord);
1485 1486 1487 1488 1489 1490

			/*
			 * This is a double-check. Ideally, below checks are
			 * not required since ird/ord stuff has been taken
			 * care of in c4iw_accept_cr
			 */
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
			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) {
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
				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;

1528
	PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
	     "xmit_marker_enabled=%d, version=%d p2p_type=%d local-p2p_type = "
	     "%d\n", __func__, ep->mpa_attr.crc_enabled,
	     ep->mpa_attr.recv_marker_enabled,
	     ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
	     ep->mpa_attr.p2p_type, p2p_type);

	/*
	 * 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
	 */
1541
	if ((ep->mpa_attr.version == 2) && peer2peer &&
1542 1543 1544 1545
			(ep->mpa_attr.p2p_type != p2p_type)) {
		ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
		rtr_mismatch = 1;
	}
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561

	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;
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571

	/*
	 * If responder's RTR requirement did not match with what initiator
	 * supports, generate TERM message
	 */
	if (rtr_mismatch) {
		printk(KERN_ERR "%s: RTR mismatch, sending TERM\n", __func__);
		attrs.layer_etype = LAYER_MPA | DDP_LLP;
		attrs.ecode = MPA_NOMATCH_RTR;
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1572
		attrs.send_term = 1;
1573
		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1574
				C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1575
		err = -ENOMEM;
1576
		disconnect = 1;
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
		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) {
		printk(KERN_ERR "%s: Insufficient IRD, sending TERM\n",
				__func__);
		attrs.layer_etype = LAYER_MPA | DDP_LLP;
		attrs.ecode = MPA_INSUFF_IRD;
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1592
		attrs.send_term = 1;
1593
		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1594
				C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1595
		err = -ENOMEM;
1596
		disconnect = 1;
1597 1598
		goto out;
	}
1599 1600
	goto out;
err:
1601
	disconnect = 2;
1602 1603
out:
	connect_reply_upcall(ep, err);
1604
	return disconnect;
1605 1606
}

1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
/*
 * 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)
1620 1621
{
	struct mpa_message *mpa;
1622
	struct mpa_v2_conn_params *mpa_v2_params;
1623 1624 1625 1626 1627 1628 1629 1630
	u16 plen;

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);

	/*
	 * If we get more than the supported amount of private data
	 * then we must fail this connection.
	 */
1631 1632
	if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt))
		goto err_stop_timer;
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647

	PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);

	/*
	 * 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))
1648
		return 0;
1649 1650 1651 1652 1653 1654 1655

	PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
	mpa = (struct mpa_message *) ep->mpa_pkt;

	/*
	 * Validate MPA Header.
	 */
1656 1657 1658
	if (mpa->revision > mpa_rev) {
		printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d,"
		       " Received = %d\n", __func__, mpa_rev, mpa->revision);
1659
		goto err_stop_timer;
1660 1661
	}

1662 1663
	if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key)))
		goto err_stop_timer;
1664 1665 1666 1667 1668 1669

	plen = ntohs(mpa->private_data_size);

	/*
	 * Fail if there's too much private data.
	 */
1670 1671
	if (plen > MPA_MAX_PRIVATE_DATA)
		goto err_stop_timer;
1672 1673 1674 1675

	/*
	 * If plen does not account for pkt size
	 */
1676 1677
	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen))
		goto err_stop_timer;
1678 1679 1680 1681 1682 1683
	ep->plen = (u8) plen;

	/*
	 * If we don't have all the pdata yet, then bail.
	 */
	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1684
		return 0;
1685 1686 1687 1688 1689 1690 1691 1692 1693

	/*
	 * 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;
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
	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;
			ep->ord = ntohs(mpa_v2_params->ord) &
				MPA_V2_IRD_ORD_MASK;
1709 1710
			PDBG("%s initiator ird %u ord %u\n", __func__, ep->ird,
			     ep->ord);
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
			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;

1727 1728 1729 1730 1731 1732
	PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
	     "xmit_marker_enabled=%d, version=%d p2p_type=%d\n", __func__,
	     ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
	     ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
	     ep->mpa_attr.p2p_type);

S
Steve Wise 已提交
1733 1734 1735 1736 1737 1738 1739 1740 1741
	/*
	 * If the endpoint timer already expired, then we ignore
	 * the start request.  process_timeout() will abort
	 * the connection.
	 */
	if (!stop_ep_timer(ep)) {
		__state_set(&ep->com, MPA_REQ_RCVD);

		/* drive upcall */
1742 1743
		mutex_lock_nested(&ep->parent_ep->com.mutex,
				  SINGLE_DEPTH_NESTING);
S
Steve Wise 已提交
1744 1745
		if (ep->parent_ep->com.state != DEAD) {
			if (connect_request_upcall(ep))
1746
				goto err_unlock_parent;
S
Steve Wise 已提交
1747
		} else {
1748
			goto err_unlock_parent;
S
Steve Wise 已提交
1749 1750
		}
		mutex_unlock(&ep->parent_ep->com.mutex);
1751
	}
1752 1753 1754 1755 1756 1757 1758 1759 1760
	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;
1761 1762 1763 1764 1765 1766 1767 1768 1769
}

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);
	struct tid_info *t = dev->rdev.lldi.tids;
1770
	__u8 status = hdr->status;
1771
	int disconnect = 0;
1772 1773

	ep = lookup_tid(t, tid);
1774 1775
	if (!ep)
		return 0;
1776 1777 1778
	PDBG("%s ep %p tid %u dlen %u\n", __func__, ep, ep->hwtid, dlen);
	skb_pull(skb, sizeof(*hdr));
	skb_trim(skb, dlen);
1779
	mutex_lock(&ep->com.mutex);
1780 1781 1782 1783

	/* update RX credits */
	update_rx_credits(ep, dlen);

1784
	switch (ep->com.state) {
1785
	case MPA_REQ_SENT:
1786
		ep->rcv_seq += dlen;
1787
		disconnect = process_mpa_reply(ep, skb);
1788 1789
		break;
	case MPA_REQ_WAIT:
1790
		ep->rcv_seq += dlen;
1791 1792
		process_mpa_request(ep, skb);
		break;
1793 1794 1795
	case FPDU_MODE: {
		struct c4iw_qp_attributes attrs;
		BUG_ON(!ep->com.qp);
1796
		if (status)
1797
			pr_err("%s Unexpected streaming data." \
1798 1799
			       " qpid %u ep %p state %d tid %u status %d\n",
			       __func__, ep->com.qp->wq.sq.qid, ep,
1800
			       ep->com.state, ep->hwtid, status);
1801
		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1802
		c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1803 1804
			       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
		disconnect = 1;
1805 1806
		break;
	}
1807 1808 1809
	default:
		break;
	}
1810
	mutex_unlock(&ep->com.mutex);
1811 1812
	if (disconnect)
		c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
	return 0;
}

static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
	int release = 0;
	unsigned int tid = GET_TID(rpl);
	struct tid_info *t = dev->rdev.lldi.tids;

	ep = lookup_tid(t, tid);
1825 1826 1827 1828
	if (!ep) {
		printk(KERN_WARNING MOD "Abort rpl to freed endpoint\n");
		return 0;
	}
1829
	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1830
	mutex_lock(&ep->com.mutex);
1831 1832
	switch (ep->com.state) {
	case ABORTING:
1833
		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1834 1835 1836 1837 1838 1839 1840 1841
		__state_set(&ep->com, DEAD);
		release = 1;
		break;
	default:
		printk(KERN_ERR "%s ep %p state %d\n",
		     __func__, ep, ep->com.state);
		break;
	}
1842
	mutex_unlock(&ep->com.mutex);
1843 1844 1845 1846 1847 1848

	if (release)
		release_ep_resources(ep);
	return 0;
}

1849 1850 1851 1852 1853 1854
static void send_fw_act_open_req(struct c4iw_ep *ep, unsigned int atid)
{
	struct sk_buff *skb;
	struct fw_ofld_connection_wr *req;
	unsigned int mtu_idx;
	int wscale;
1855
	struct sockaddr_in *sin;
1856
	int win;
1857 1858 1859 1860

	skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
	req = (struct fw_ofld_connection_wr *)__skb_put(skb, sizeof(*req));
	memset(req, 0, sizeof(*req));
1861
	req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR));
1862
	req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
1863 1864
	req->le.filter = cpu_to_be32(cxgb4_select_ntuple(
				     ep->com.dev->rdev.lldi.ports[0],
1865
				     ep->l2t));
1866
	sin = (struct sockaddr_in *)&ep->com.local_addr;
1867 1868
	req->le.lport = sin->sin_port;
	req->le.u.ipv4.lip = sin->sin_addr.s_addr;
1869
	sin = (struct sockaddr_in *)&ep->com.remote_addr;
1870 1871
	req->le.pport = sin->sin_port;
	req->le.u.ipv4.pip = sin->sin_addr.s_addr;
1872
	req->tcb.t_state_to_astid =
1873 1874
			htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_SENT) |
			FW_OFLD_CONNECTION_WR_ASTID_V(atid));
1875
	req->tcb.cplrxdataack_cplpassacceptrpl =
1876
			htons(FW_OFLD_CONNECTION_WR_CPLRXDATAACK_F);
1877
	req->tcb.tx_max = (__force __be32) jiffies;
1878
	req->tcb.rcv_adv = htons(1);
1879
	best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
1880 1881
		 enable_tcp_timestamps,
		 (AF_INET == ep->com.remote_addr.ss_family) ? 0 : 1);
1882
	wscale = compute_wscale(rcv_win);
1883 1884 1885 1886 1887 1888

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

1892 1893
	req->tcb.opt0 = (__force __be64) (TCAM_BYPASS_F |
		(nocong ? NO_CONG_F : 0) |
1894
		KEEP_ALIVE_F |
1895
		DELACK_F |
1896 1897 1898 1899 1900
		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 已提交
1901
		DSCP_V(ep->tos >> 2) |
1902 1903
		ULP_MODE_V(ULP_MODE_TCPDDP) |
		RCV_BUFSIZ_V(win));
1904 1905
	req->tcb.opt2 = (__force __be32) (PACE_V(1) |
		TX_QUEUE_V(ep->com.dev->rdev.lldi.tx_modq[ep->tx_chan]) |
1906
		RX_CHANNEL_V(0) |
1907
		CCTRL_ECN_V(enable_ecn) |
1908
		RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid));
1909
	if (enable_tcp_timestamps)
1910
		req->tcb.opt2 |= (__force __be32)TSTAMPS_EN_F;
1911
	if (enable_tcp_sack)
1912
		req->tcb.opt2 |= (__force __be32)SACK_EN_F;
1913
	if (wscale && enable_tcp_window_scaling)
1914 1915 1916
		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);
1917 1918
	set_wr_txq(skb, CPL_PRIORITY_CONTROL, ep->ctrlq_idx);
	set_bit(ACT_OFLD_CONN, &ep->com.history);
1919 1920 1921
	c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
}

1922 1923 1924 1925 1926 1927 1928 1929 1930
/*
 * Return whether a failed active open has allocated a TID
 */
static inline int act_open_has_tid(int status)
{
	return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
	       status != CPL_ERR_ARP_MISS;
}

1931 1932 1933 1934 1935 1936 1937 1938 1939
/* Returns whether a CPL status conveys negative advice.
 */
static int is_neg_adv(unsigned int status)
{
	return status == CPL_ERR_RTX_NEG_ADVICE ||
	       status == CPL_ERR_PERSIST_NEG_ADVICE ||
	       status == CPL_ERR_KEEPALV_NEG_ADVICE;
}

1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
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";
	}
}

1954 1955 1956 1957 1958 1959 1960
static void set_tcp_window(struct c4iw_ep *ep, struct port_info *pi)
{
	ep->snd_win = snd_win;
	ep->rcv_win = rcv_win;
	PDBG("%s snd_win %d rcv_win %d\n", __func__, ep->snd_win, ep->rcv_win);
}

1961 1962
#define ACT_OPEN_RETRY_COUNT 2

1963 1964
static int import_ep(struct c4iw_ep *ep, int iptype, __u8 *peer_ip,
		     struct dst_entry *dst, struct c4iw_dev *cdev,
H
Hariprasad S 已提交
1965
		     bool clear_mpa_v1, enum chip_type adapter_type, u8 tos)
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
{
	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 已提交
1995
					n, pdev, rt_tos2priority(tos));
1996 1997 1998 1999
		if (!ep->l2t)
			goto out;
		ep->mtu = pdev->mtu;
		ep->tx_chan = cxgb4_port_chan(pdev);
2000 2001
		ep->smac_idx = cxgb4_tp_smt_idx(adapter_type,
						cxgb4_port_viid(pdev));
2002 2003 2004 2005 2006 2007 2008 2009
		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];
2010
		set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2011 2012 2013 2014 2015 2016 2017 2018
		dev_put(pdev);
	} else {
		pdev = get_real_dev(n->dev);
		ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
					n, pdev, 0);
		if (!ep->l2t)
			goto out;
		ep->mtu = dst_mtu(dst);
S
Steve Wise 已提交
2019
		ep->tx_chan = cxgb4_port_chan(pdev);
2020 2021
		ep->smac_idx = cxgb4_tp_smt_idx(adapter_type,
						cxgb4_port_viid(pdev));
2022 2023
		step = cdev->rdev.lldi.ntxq /
			cdev->rdev.lldi.nchan;
S
Steve Wise 已提交
2024 2025
		ep->txq_idx = cxgb4_port_idx(pdev) * step;
		ep->ctrlq_idx = cxgb4_port_idx(pdev);
2026 2027 2028
		step = cdev->rdev.lldi.nrxq /
			cdev->rdev.lldi.nchan;
		ep->rss_qid = cdev->rdev.lldi.rxq_ids[
S
Steve Wise 已提交
2029
			cxgb4_port_idx(pdev) * step];
2030
		set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045

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

2046 2047 2048
static int c4iw_reconnect(struct c4iw_ep *ep)
{
	int err = 0;
2049
	struct sockaddr_in *laddr = (struct sockaddr_in *)
2050
				    &ep->com.cm_id->m_local_addr;
2051
	struct sockaddr_in *raddr = (struct sockaddr_in *)
2052
				    &ep->com.cm_id->m_remote_addr;
2053
	struct sockaddr_in6 *laddr6 = (struct sockaddr_in6 *)
2054
				      &ep->com.cm_id->m_local_addr;
2055
	struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)
2056
				      &ep->com.cm_id->m_remote_addr;
2057 2058
	int iptype;
	__u8 *ra;
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074

	PDBG("%s qp %p cm_id %p\n", __func__, ep->com.qp, ep->com.cm_id);
	init_timer(&ep->timer);

	/*
	 * 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) {
		pr_err("%s - cannot alloc atid.\n", __func__);
		err = -ENOMEM;
		goto fail2;
	}
	insert_handle(ep->com.dev, &ep->com.dev->atid_idr, ep, ep->atid);

	/* find a route */
2075
	if (ep->com.cm_id->m_local_addr.ss_family == AF_INET) {
2076 2077
		ep->dst = find_route(ep->com.dev, laddr->sin_addr.s_addr,
				     raddr->sin_addr.s_addr, laddr->sin_port,
H
Hariprasad S 已提交
2078
				     raddr->sin_port, ep->com.cm_id->tos);
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
		iptype = 4;
		ra = (__u8 *)&raddr->sin_addr;
	} else {
		ep->dst = find_route6(ep->com.dev, laddr6->sin6_addr.s6_addr,
				      raddr6->sin6_addr.s6_addr,
				      laddr6->sin6_port, raddr6->sin6_port, 0,
				      raddr6->sin6_scope_id);
		iptype = 6;
		ra = (__u8 *)&raddr6->sin6_addr;
	}
	if (!ep->dst) {
2090 2091 2092 2093
		pr_err("%s - cannot find route.\n", __func__);
		err = -EHOSTUNREACH;
		goto fail3;
	}
2094
	err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, false,
H
Hariprasad S 已提交
2095 2096
			ep->com.dev->rdev.lldi.adapter_type,
			ep->com.cm_id->tos);
2097
	if (err) {
2098 2099 2100 2101 2102 2103 2104 2105 2106
		pr_err("%s - cannot alloc l2e.\n", __func__);
		goto fail4;
	}

	PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
	     __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
	     ep->l2t->idx);

	state_set(&ep->com, CONNECTING);
H
Hariprasad S 已提交
2107
	ep->tos = ep->com.cm_id->tos;
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

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

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

2133 2134 2135 2136
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);
2137 2138
	unsigned int atid = TID_TID_G(AOPEN_ATID_G(
				      ntohl(rpl->atid_status)));
2139
	struct tid_info *t = dev->rdev.lldi.tids;
2140
	int status = AOPEN_STATUS_G(ntohl(rpl->atid_status));
2141 2142 2143 2144
	struct sockaddr_in *la;
	struct sockaddr_in *ra;
	struct sockaddr_in6 *la6;
	struct sockaddr_in6 *ra6;
2145 2146

	ep = lookup_atid(t, atid);
2147 2148 2149 2150
	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;
2151 2152 2153 2154

	PDBG("%s ep %p atid %u status %u errno %d\n", __func__, ep, atid,
	     status, status2errno(status));

2155
	if (is_neg_adv(status)) {
2156 2157 2158 2159 2160 2161
		PDBG("%s Connection problems for atid %u status %u (%s)\n",
		     __func__, atid, status, neg_adv_str(status));
		ep->stats.connect_neg_adv++;
		mutex_lock(&dev->rdev.stats.lock);
		dev->rdev.stats.neg_adv++;
		mutex_unlock(&dev->rdev.stats.lock);
2162 2163 2164
		return 0;
	}

2165 2166
	set_bit(ACT_OPEN_RPL, &ep->com.history);

2167 2168 2169 2170 2171 2172 2173
	/*
	 * Log interesting failures.
	 */
	switch (status) {
	case CPL_ERR_CONN_RESET:
	case CPL_ERR_CONN_TIMEDOUT:
		break;
2174
	case CPL_ERR_TCAM_FULL:
2175
		mutex_lock(&dev->rdev.stats.lock);
2176
		dev->rdev.stats.tcam_full++;
2177 2178 2179
		mutex_unlock(&dev->rdev.stats.lock);
		if (ep->com.local_addr.ss_family == AF_INET &&
		    dev->rdev.lldi.enable_fw_ofld_conn) {
2180
			send_fw_act_open_req(ep,
2181
					     TID_TID_G(AOPEN_ATID_G(
2182 2183 2184 2185 2186 2187 2188
					     ntohl(rpl->atid_status))));
			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 已提交
2189 2190 2191
			if (ep->com.remote_addr.ss_family == AF_INET6) {
				struct sockaddr_in6 *sin6 =
						(struct sockaddr_in6 *)
2192
						&ep->com.local_addr;
H
Hariprasad S 已提交
2193 2194 2195 2196 2197
				cxgb4_clip_release(
						ep->com.dev->rdev.lldi.ports[0],
						(const u32 *)
						&sin6->sin6_addr.s6_addr, 1);
			}
2198 2199 2200 2201 2202 2203 2204 2205
			remove_handle(ep->com.dev, &ep->com.dev->atid_idr,
					atid);
			cxgb4_free_atid(t, atid);
			dst_release(ep->dst);
			cxgb4_l2t_release(ep->l2t);
			c4iw_reconnect(ep);
			return 0;
		}
2206
		break;
2207
	default:
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
		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));
		}
2219 2220 2221
		break;
	}

2222 2223 2224
	connect_reply_upcall(ep, status2errno(status));
	state_set(&ep->com, DEAD);

H
Hariprasad S 已提交
2225 2226
	if (ep->com.remote_addr.ss_family == AF_INET6) {
		struct sockaddr_in6 *sin6 =
2227
			(struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
2228 2229 2230
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
2231 2232 2233
	if (status && act_open_has_tid(status))
		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl));

2234
	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
	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);
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int stid = GET_TID(rpl);
	struct c4iw_listen_ep *ep = lookup_stid(t, stid);

	if (!ep) {
2251 2252
		PDBG("%s stid %d lookup failure!\n", __func__, stid);
		goto out;
2253 2254 2255
	}
	PDBG("%s ep %p status %d error %d\n", __func__, ep,
	     rpl->status, status2errno(rpl->status));
2256
	c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
2257

2258
out:
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
	return 0;
}

static int close_listsrv_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int stid = GET_TID(rpl);
	struct c4iw_listen_ep *ep = lookup_stid(t, stid);

	PDBG("%s ep %p\n", __func__, ep);
2270
	c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
2271 2272 2273
	return 0;
}

2274 2275
static int accept_cr(struct c4iw_ep *ep, struct sk_buff *skb,
		     struct cpl_pass_accept_req *req)
2276 2277 2278 2279 2280 2281
{
	struct cpl_pass_accept_rpl *rpl;
	unsigned int mtu_idx;
	u64 opt0;
	u32 opt2;
	int wscale;
2282
	struct cpl_t5_pass_accept_rpl *rpl5 = NULL;
2283
	int win;
2284
	enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
2285 2286 2287

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	BUG_ON(skb_cloned(skb));
2288

2289
	skb_get(skb);
2290
	rpl = cplhdr(skb);
2291
	if (!is_t4(adapter_type)) {
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
		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));

	best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
2303 2304
		 enable_tcp_timestamps && req->tcpopt.tstamp,
		 (AF_INET == ep->com.remote_addr.ss_family) ? 0 : 1);
2305
	wscale = compute_wscale(rcv_win);
2306 2307 2308 2309 2310 2311

	/*
	 * Specify the largest window that will fit in opt0. The
	 * remainder will be specified in the rx_data_ack.
	 */
	win = ep->rcv_win >> 10;
2312 2313
	if (win > RCV_BUFSIZ_M)
		win = RCV_BUFSIZ_M;
2314
	opt0 = (nocong ? NO_CONG_F : 0) |
2315
	       KEEP_ALIVE_F |
2316
	       DELACK_F |
2317 2318 2319 2320 2321
	       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) |
2322
	       DSCP_V(ep->tos >> 2) |
2323 2324 2325 2326
	       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);
2327 2328

	if (enable_tcp_timestamps && req->tcpopt.tstamp)
2329
		opt2 |= TSTAMPS_EN_F;
2330
	if (enable_tcp_sack && req->tcpopt.sack)
2331
		opt2 |= SACK_EN_F;
2332
	if (wscale && enable_tcp_window_scaling)
2333
		opt2 |= WND_SCALE_EN_F;
2334 2335 2336 2337
	if (enable_ecn) {
		const struct tcphdr *tcph;
		u32 hlen = ntohl(req->hdr_len);

2338 2339 2340 2341 2342 2343
		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);
2344
		if (tcph->ece && tcph->cwr)
2345
			opt2 |= CCTRL_ECN_V(1);
2346
	}
2347
	if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
2348
		u32 isn = (prandom_u32() & ~7UL) - 1;
2349
		opt2 |= T5_OPT_2_VALID_F;
2350
		opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
2351
		opt2 |= T5_ISS_F;
2352 2353 2354 2355 2356 2357
		rpl5 = (void *)rpl;
		memset(&rpl5->iss, 0, roundup(sizeof(*rpl5)-sizeof(*rpl), 16));
		if (peer2peer)
			isn += 4;
		rpl5->iss = cpu_to_be32(isn);
		PDBG("%s iss %u\n", __func__, be32_to_cpu(rpl5->iss));
2358
	}
2359 2360 2361

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

2365
	return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
2366 2367
}

2368
static void reject_cr(struct c4iw_dev *dev, u32 hwtid, struct sk_buff *skb)
2369
{
2370
	PDBG("%s c4iw_dev %p tid %u\n", __func__, dev, hwtid);
2371 2372 2373 2374 2375 2376
	BUG_ON(skb_cloned(skb));
	skb_trim(skb, sizeof(struct cpl_tid_release));
	release_tid(&dev->rdev, hwtid, skb);
	return;
}

2377 2378
static void get_4tuple(struct cpl_pass_accept_req *req, enum chip_type type,
		       int *iptype, __u8 *local_ip, __u8 *peer_ip,
2379 2380
		       __be16 *local_port, __be16 *peer_port)
{
2381 2382 2383 2384 2385 2386
	int eth_len = (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) ?
		      ETH_HDR_LEN_G(be32_to_cpu(req->hdr_len)) :
		      T6_ETH_HDR_LEN_G(be32_to_cpu(req->hdr_len));
	int ip_len = (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) ?
		     IP_HDR_LEN_G(be32_to_cpu(req->hdr_len)) :
		     T6_IP_HDR_LEN_G(be32_to_cpu(req->hdr_len));
2387
	struct iphdr *ip = (struct iphdr *)((u8 *)(req + 1) + eth_len);
2388
	struct ipv6hdr *ip6 = (struct ipv6hdr *)((u8 *)(req + 1) + eth_len);
2389 2390 2391
	struct tcphdr *tcp = (struct tcphdr *)
			     ((u8 *)(req + 1) + eth_len + ip_len);

2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
	if (ip->version == 4) {
		PDBG("%s saddr 0x%x daddr 0x%x sport %u dport %u\n", __func__,
		     ntohl(ip->saddr), ntohl(ip->daddr), ntohs(tcp->source),
		     ntohs(tcp->dest));
		*iptype = 4;
		memcpy(peer_ip, &ip->saddr, 4);
		memcpy(local_ip, &ip->daddr, 4);
	} else {
		PDBG("%s saddr %pI6 daddr %pI6 sport %u dport %u\n", __func__,
		     ip6->saddr.s6_addr, ip6->daddr.s6_addr, ntohs(tcp->source),
		     ntohs(tcp->dest));
		*iptype = 6;
		memcpy(peer_ip, ip6->saddr.s6_addr, 16);
		memcpy(local_ip, ip6->daddr.s6_addr, 16);
	}
2407 2408 2409 2410 2411 2412 2413 2414
	*peer_port = tcp->source;
	*local_port = tcp->dest;

	return;
}

static int pass_accept_req(struct c4iw_dev *dev, struct sk_buff *skb)
{
2415
	struct c4iw_ep *child_ep = NULL, *parent_ep;
2416
	struct cpl_pass_accept_req *req = cplhdr(skb);
2417
	unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
2418 2419 2420
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int hwtid = GET_TID(req);
	struct dst_entry *dst;
2421
	__u8 local_ip[16], peer_ip[16];
2422
	__be16 local_port, peer_port;
H
Hariprasad S 已提交
2423
	struct sockaddr_in6 *sin6;
2424
	int err;
2425
	u16 peer_mss = ntohs(req->tcpopt.mss);
2426
	int iptype;
2427
	unsigned short hdrs;
H
Hariprasad S 已提交
2428
	u8 tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
2429 2430

	parent_ep = lookup_stid(t, stid);
2431 2432 2433 2434 2435
	if (!parent_ep) {
		PDBG("%s connect request on invalid stid %d\n", __func__, stid);
		goto reject;
	}

2436
	if (state_read(&parent_ep->com) != LISTEN) {
2437
		PDBG("%s - listening ep not in LISTEN\n", __func__);
2438 2439 2440
		goto reject;
	}

2441 2442
	get_4tuple(req, parent_ep->com.dev->rdev.lldi.adapter_type, &iptype,
		   local_ip, peer_ip, &local_port, &peer_port);
2443

2444
	/* Find output route */
2445 2446 2447 2448 2449 2450 2451
	if (iptype == 4)  {
		PDBG("%s parent ep %p hwtid %u laddr %pI4 raddr %pI4 lport %d rport %d peer_mss %d\n"
		     , __func__, parent_ep, hwtid,
		     local_ip, peer_ip, ntohs(local_port),
		     ntohs(peer_port), peer_mss);
		dst = find_route(dev, *(__be32 *)local_ip, *(__be32 *)peer_ip,
				 local_port, peer_port,
H
Hariprasad S 已提交
2452
				 tos);
2453 2454 2455 2456 2457 2458
	} else {
		PDBG("%s parent ep %p hwtid %u laddr %pI6 raddr %pI6 lport %d rport %d peer_mss %d\n"
		     , __func__, parent_ep, hwtid,
		     local_ip, peer_ip, ntohs(local_port),
		     ntohs(peer_port), peer_mss);
		dst = find_route6(dev, local_ip, peer_ip, local_port, peer_port,
2459
				  PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
2460 2461 2462 2463
				  ((struct sockaddr_in6 *)
				  &parent_ep->com.local_addr)->sin6_scope_id);
	}
	if (!dst) {
2464 2465 2466 2467
		printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
		       __func__);
		goto reject;
	}
2468 2469 2470 2471

	child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
	if (!child_ep) {
		printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
2472 2473 2474 2475 2476
		       __func__);
		dst_release(dst);
		goto reject;
	}

2477
	err = import_ep(child_ep, iptype, peer_ip, dst, dev, false,
H
Hariprasad S 已提交
2478
			parent_ep->com.dev->rdev.lldi.adapter_type, tos);
2479 2480
	if (err) {
		printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
2481 2482
		       __func__);
		dst_release(dst);
2483
		kfree(child_ep);
2484 2485
		goto reject;
	}
2486

2487 2488 2489 2490
	hdrs = sizeof(struct iphdr) + sizeof(struct tcphdr) +
	       ((enable_tcp_timestamps && req->tcpopt.tstamp) ? 12 : 0);
	if (peer_mss && child_ep->mtu > (peer_mss + hdrs))
		child_ep->mtu = peer_mss + hdrs;
2491

2492 2493 2494
	state_set(&child_ep->com, CONNECTING);
	child_ep->com.dev = dev;
	child_ep->com.cm_id = NULL;
2495

2496 2497
	if (iptype == 4) {
		struct sockaddr_in *sin = (struct sockaddr_in *)
2498
			&child_ep->com.local_addr;
2499

2500 2501 2502
		sin->sin_family = PF_INET;
		sin->sin_port = local_port;
		sin->sin_addr.s_addr = *(__be32 *)local_ip;
2503 2504 2505 2506 2507 2508 2509

		sin = (struct sockaddr_in *)&child_ep->com.local_addr;
		sin->sin_family = PF_INET;
		sin->sin_port = ((struct sockaddr_in *)
				 &parent_ep->com.local_addr)->sin_port;
		sin->sin_addr.s_addr = *(__be32 *)local_ip;

2510
		sin = (struct sockaddr_in *)&child_ep->com.remote_addr;
2511 2512 2513 2514
		sin->sin_family = PF_INET;
		sin->sin_port = peer_port;
		sin->sin_addr.s_addr = *(__be32 *)peer_ip;
	} else {
2515
		sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2516 2517 2518
		sin6->sin6_family = PF_INET6;
		sin6->sin6_port = local_port;
		memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
2519 2520 2521 2522 2523 2524 2525

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

2526
		sin6 = (struct sockaddr_in6 *)&child_ep->com.remote_addr;
2527 2528 2529 2530
		sin6->sin6_family = PF_INET6;
		sin6->sin6_port = peer_port;
		memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
	}
2531

2532 2533
	c4iw_get_ep(&parent_ep->com);
	child_ep->parent_ep = parent_ep;
H
Hariprasad S 已提交
2534
	child_ep->tos = tos;
2535 2536 2537 2538
	child_ep->dst = dst;
	child_ep->hwtid = hwtid;

	PDBG("%s tx_chan %u smac_idx %u rss_qid %u\n", __func__,
2539
	     child_ep->tx_chan, child_ep->smac_idx, child_ep->rss_qid);
2540 2541 2542

	init_timer(&child_ep->timer);
	cxgb4_insert_tid(t, child_ep, hwtid);
2543
	insert_handle(dev, &dev->hwtid_idr, child_ep, child_ep->hwtid);
2544 2545 2546 2547 2548 2549
	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 已提交
2550
	if (iptype == 6) {
2551
		sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
H
Hariprasad S 已提交
2552 2553 2554
		cxgb4_clip_get(child_ep->com.dev->rdev.lldi.ports[0],
			       (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
2555 2556
	goto out;
reject:
2557
	reject_cr(dev, hwtid, skb);
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567
out:
	return 0;
}

static int pass_establish(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct c4iw_ep *ep;
	struct cpl_pass_establish *req = cplhdr(skb);
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int tid = GET_TID(req);
2568
	int ret;
2569 2570 2571 2572 2573 2574

	ep = lookup_tid(t, tid);
	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	ep->snd_seq = be32_to_cpu(req->snd_isn);
	ep->rcv_seq = be32_to_cpu(req->rcv_isn);

2575 2576 2577
	PDBG("%s ep %p hwtid %u tcp_opt 0x%02x\n", __func__, ep, tid,
	     ntohs(req->tcp_opt));

2578 2579 2580
	set_emss(ep, ntohs(req->tcp_opt));

	dst_confirm(ep->dst);
2581 2582
	mutex_lock(&ep->com.mutex);
	ep->com.state = MPA_REQ_WAIT;
2583
	start_ep_timer(ep);
2584
	set_bit(PASS_ESTAB, &ep->com.history);
2585 2586 2587 2588
	ret = send_flowc(ep, skb);
	mutex_unlock(&ep->com.mutex);
	if (ret)
		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601

	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;
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int tid = GET_TID(hdr);
S
Steve Wise 已提交
2602
	int ret;
2603 2604 2605 2606 2607

	ep = lookup_tid(t, tid);
	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	dst_confirm(ep->dst);

2608
	set_bit(PEER_CLOSE, &ep->com.history);
2609
	mutex_lock(&ep->com.mutex);
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627
	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);
		PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
2628
		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2629 2630 2631 2632
		break;
	case MPA_REP_SENT:
		__state_set(&ep->com, CLOSING);
		PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
2633
		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2634 2635
		break;
	case FPDU_MODE:
2636
		start_ep_timer(ep);
2637
		__state_set(&ep->com, CLOSING);
2638
		attrs.next_state = C4IW_QP_STATE_CLOSING;
S
Steve Wise 已提交
2639
		ret = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2640
				       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
S
Steve Wise 已提交
2641 2642 2643 2644
		if (ret != -ECONNRESET) {
			peer_close_upcall(ep);
			disconnect = 1;
		}
2645 2646 2647 2648 2649 2650 2651 2652 2653
		break;
	case ABORTING:
		disconnect = 0;
		break;
	case CLOSING:
		__state_set(&ep->com, MORIBUND);
		disconnect = 0;
		break;
	case MORIBUND:
S
Steve Wise 已提交
2654
		(void)stop_ep_timer(ep);
2655 2656 2657 2658 2659
		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);
		}
2660
		close_complete_upcall(ep, 0);
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670
		__state_set(&ep->com, DEAD);
		release = 1;
		disconnect = 0;
		break;
	case DEAD:
		disconnect = 0;
		break;
	default:
		BUG_ON(1);
	}
2671
	mutex_unlock(&ep->com.mutex);
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
	if (disconnect)
		c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
	if (release)
		release_ep_resources(ep);
	return 0;
}

static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
{
	struct cpl_abort_req_rss *req = cplhdr(skb);
	struct c4iw_ep *ep;
	struct cpl_abort_rpl *rpl;
	struct sk_buff *rpl_skb;
	struct c4iw_qp_attributes attrs;
	int ret;
	int release = 0;
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int tid = GET_TID(req);

	ep = lookup_tid(t, tid);
2692
	if (is_neg_adv(req->status)) {
2693 2694 2695 2696 2697 2698 2699
		PDBG("%s Negative advice on abort- tid %u status %d (%s)\n",
		     __func__, ep->hwtid, req->status,
		     neg_adv_str(req->status));
		ep->stats.abort_neg_adv++;
		mutex_lock(&dev->rdev.stats.lock);
		dev->rdev.stats.neg_adv++;
		mutex_unlock(&dev->rdev.stats.lock);
2700 2701 2702 2703
		return 0;
	}
	PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
	     ep->com.state);
2704
	set_bit(PEER_ABORT, &ep->com.history);
2705 2706 2707

	/*
	 * Wake up any threads in rdma_init() or rdma_fini().
2708 2709
	 * However, this is not needed if com state is just
	 * MPA_REQ_SENT
2710
	 */
2711 2712
	if (ep->com.state != MPA_REQ_SENT)
		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2713 2714

	mutex_lock(&ep->com.mutex);
2715 2716
	switch (ep->com.state) {
	case CONNECTING:
2717
		c4iw_put_ep(&ep->parent_ep->com);
2718 2719
		break;
	case MPA_REQ_WAIT:
S
Steve Wise 已提交
2720
		(void)stop_ep_timer(ep);
2721 2722
		break;
	case MPA_REQ_SENT:
S
Steve Wise 已提交
2723
		(void)stop_ep_timer(ep);
2724
		if (mpa_rev == 1 || (mpa_rev == 2 && ep->tried_with_mpa_v1))
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
			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
			 */
			PDBG("%s: mpa_rev=%d. Retrying with mpav1\n", __func__,
			     mpa_rev);
			ep->retry_with_mpa_v1 = 1;
		}
2739 2740 2741 2742 2743 2744 2745
		break;
	case MPA_REP_SENT:
		break;
	case MPA_REQ_RCVD:
		break;
	case MORIBUND:
	case CLOSING:
2746
		stop_ep_timer(ep);
2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764
		/*FALLTHROUGH*/
	case FPDU_MODE:
		if (ep->com.cm_id && ep->com.qp) {
			attrs.next_state = C4IW_QP_STATE_ERROR;
			ret = c4iw_modify_qp(ep->com.qp->rhp,
				     ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
				     &attrs, 1);
			if (ret)
				printk(KERN_ERR MOD
				       "%s - qp <- error failed!\n",
				       __func__);
		}
		peer_abort_upcall(ep);
		break;
	case ABORTING:
		break;
	case DEAD:
		PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
2765
		mutex_unlock(&ep->com.mutex);
2766 2767 2768 2769 2770 2771 2772 2773
		return 0;
	default:
		BUG_ON(1);
		break;
	}
	dst_confirm(ep->dst);
	if (ep->com.state != ABORTING) {
		__state_set(&ep->com, DEAD);
2774 2775 2776
		/* we don't release if we want to retry with mpa_v1 */
		if (!ep->retry_with_mpa_v1)
			release = 1;
2777
	}
2778
	mutex_unlock(&ep->com.mutex);
2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795

	rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
	if (!rpl_skb) {
		printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
		       __func__);
		release = 1;
		goto out;
	}
	set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
	rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
	INIT_TP_WR(rpl, ep->hwtid);
	OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
	rpl->cmd = CPL_ABORT_NO_RST;
	c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
out:
	if (release)
		release_ep_resources(ep);
2796
	else if (ep->retry_with_mpa_v1) {
H
Hariprasad S 已提交
2797 2798 2799
		if (ep->com.remote_addr.ss_family == AF_INET6) {
			struct sockaddr_in6 *sin6 =
					(struct sockaddr_in6 *)
2800
					&ep->com.local_addr;
H
Hariprasad S 已提交
2801 2802 2803 2804 2805
			cxgb4_clip_release(
					ep->com.dev->rdev.lldi.ports[0],
					(const u32 *)&sin6->sin6_addr.s6_addr,
					1);
		}
2806
		remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid);
2807 2808 2809 2810 2811 2812
		cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
		dst_release(ep->dst);
		cxgb4_l2t_release(ep->l2t);
		c4iw_reconnect(ep);
	}

2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
	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;
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int tid = GET_TID(rpl);

	ep = lookup_tid(t, tid);

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
	BUG_ON(!ep);

	/* The cm_id may be null if we failed to connect */
2831
	mutex_lock(&ep->com.mutex);
2832
	set_bit(CLOSE_CON_RPL, &ep->com.history);
2833 2834 2835 2836 2837
	switch (ep->com.state) {
	case CLOSING:
		__state_set(&ep->com, MORIBUND);
		break;
	case MORIBUND:
S
Steve Wise 已提交
2838
		(void)stop_ep_timer(ep);
2839 2840 2841 2842 2843 2844 2845
		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);
		}
2846
		close_complete_upcall(ep, 0);
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
		__state_set(&ep->com, DEAD);
		release = 1;
		break;
	case ABORTING:
	case DEAD:
		break;
	default:
		BUG_ON(1);
		break;
	}
2857
	mutex_unlock(&ep->com.mutex);
2858 2859 2860 2861 2862 2863 2864
	if (release)
		release_ep_resources(ep);
	return 0;
}

static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
{
2865
	struct cpl_rdma_terminate *rpl = cplhdr(skb);
2866
	struct tid_info *t = dev->rdev.lldi.tids;
2867 2868 2869
	unsigned int tid = GET_TID(rpl);
	struct c4iw_ep *ep;
	struct c4iw_qp_attributes attrs;
2870 2871

	ep = lookup_tid(t, tid);
2872
	BUG_ON(!ep);
2873

2874
	if (ep && ep->com.qp) {
2875 2876 2877 2878 2879 2880
		printk(KERN_WARNING MOD "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);
	} else
2881
		printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n", tid);
2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902

	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);
	struct tid_info *t = dev->rdev.lldi.tids;


	ep = lookup_tid(t, tid);
	PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
	if (credits == 0) {
2903 2904
		PDBG("%s 0 credit ack ep %p tid %u state %u\n",
		     __func__, ep, ep->hwtid, state_read(&ep->com));
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
		return 0;
	}

	dst_confirm(ep->dst);
	if (ep->mpa_skb) {
		PDBG("%s last streaming msg ack ep %p tid %u state %u "
		     "initiator %u freeing skb\n", __func__, ep, ep->hwtid,
		     state_read(&ep->com), ep->mpa_attr.initiator ? 1 : 0);
		kfree_skb(ep->mpa_skb);
		ep->mpa_skb = NULL;
	}
	return 0;
}

int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
{
2921 2922
	int err = 0;
	int disconnect = 0;
2923 2924 2925
	struct c4iw_ep *ep = to_ep(cm_id);
	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);

2926 2927 2928
	mutex_lock(&ep->com.mutex);
	if (ep->com.state == DEAD) {
		mutex_unlock(&ep->com.mutex);
2929 2930 2931
		c4iw_put_ep(&ep->com);
		return -ECONNRESET;
	}
2932
	set_bit(ULP_REJECT, &ep->com.history);
2933
	BUG_ON(ep->com.state != MPA_REQ_RCVD);
2934
	if (mpa_rev == 0)
2935
		disconnect = 2;
2936 2937
	else {
		err = send_mpa_reject(ep, pdata, pdata_len);
2938
		disconnect = 1;
2939
	}
2940 2941
	mutex_unlock(&ep->com.mutex);
	if (disconnect)
2942
		err = c4iw_ep_disconnect(ep, disconnect == 2, GFP_KERNEL);
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
	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);
2955
	int abort = 0;
2956 2957

	PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2958 2959 2960

	mutex_lock(&ep->com.mutex);
	if (ep->com.state == DEAD) {
2961
		err = -ECONNRESET;
2962
		goto err_out;
2963 2964
	}

2965
	BUG_ON(ep->com.state != MPA_REQ_RCVD);
2966 2967
	BUG_ON(!qp);

2968
	set_bit(ULP_ACCEPT, &ep->com.history);
2969 2970
	if ((conn_param->ord > cur_max_read_depth(ep->com.dev)) ||
	    (conn_param->ird > cur_max_read_depth(ep->com.dev))) {
2971
		err = -EINVAL;
2972
		goto err_abort;
2973 2974
	}

2975 2976
	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
		if (conn_param->ord > ep->ird) {
2977 2978 2979 2980 2981 2982 2983 2984
			if (RELAXED_IRD_NEGOTIATION) {
				ep->ord = ep->ird;
			} 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;
2985
				goto err_abort;
2986
			}
2987
		}
2988 2989 2990 2991 2992
		if (conn_param->ird < ep->ord) {
			if (RELAXED_IRD_NEGOTIATION &&
			    ep->ord <= h->rdev.lldi.max_ordird_qp) {
				conn_param->ird = ep->ord;
			} else {
2993
				err = -ENOMEM;
2994
				goto err_abort;
2995 2996 2997
			}
		}
	}
2998 2999 3000
	ep->ird = conn_param->ird;
	ep->ord = conn_param->ord;

3001
	if (ep->mpa_attr.version == 1) {
3002 3003
		if (peer2peer && ep->ird == 0)
			ep->ird = 1;
3004 3005 3006
	} else {
		if (peer2peer &&
		    (ep->mpa_attr.p2p_type != FW_RI_INIT_P2PTYPE_DISABLED) &&
3007
		    (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) && ep->ird == 0)
3008 3009
			ep->ird = 1;
	}
3010 3011 3012

	PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);

3013
	ep->com.cm_id = cm_id;
3014
	ref_cm_id(&ep->com);
3015
	ep->com.qp = qp;
3016
	ref_qp(ep);
3017

3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
	/* 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)
3035
		goto err_deref_cm_id;
3036 3037 3038
	err = send_mpa_reply(ep, conn_param->private_data,
			     conn_param->private_data_len);
	if (err)
3039
		goto err_deref_cm_id;
3040

3041
	__state_set(&ep->com, FPDU_MODE);
3042
	established_upcall(ep);
3043
	mutex_unlock(&ep->com.mutex);
3044 3045
	c4iw_put_ep(&ep->com);
	return 0;
3046
err_deref_cm_id:
3047
	deref_cm_id(&ep->com);
3048 3049 3050
err_abort:
	abort = 1;
err_out:
3051
	mutex_unlock(&ep->com.mutex);
3052 3053
	if (abort)
		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
3054 3055 3056 3057
	c4iw_put_ep(&ep->com);
	return err;
}

3058 3059 3060 3061
static int pick_local_ipaddrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
{
	struct in_device *ind;
	int found = 0;
3062 3063
	struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
	struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107

	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);
3108 3109
	struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)&cm_id->m_local_addr;
	struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)&cm_id->m_remote_addr;
3110

3111
	if (!get_lladdr(dev->rdev.lldi.ports[0], &addr, IFA_F_TENTATIVE)) {
3112 3113 3114 3115 3116 3117 3118
		memcpy(la6->sin6_addr.s6_addr, &addr, 16);
		memcpy(ra6->sin6_addr.s6_addr, &addr, 16);
		return 0;
	}
	return -EADDRNOTAVAIL;
}

3119 3120 3121 3122
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;
3123
	int err = 0;
3124 3125 3126 3127
	struct sockaddr_in *laddr;
	struct sockaddr_in *raddr;
	struct sockaddr_in6 *laddr6;
	struct sockaddr_in6 *raddr6;
3128 3129
	__u8 *ra;
	int iptype;
3130

3131 3132
	if ((conn_param->ord > cur_max_read_depth(dev)) ||
	    (conn_param->ird > cur_max_read_depth(dev))) {
3133 3134 3135
		err = -EINVAL;
		goto out;
	}
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
	ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
	if (!ep) {
		printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
		err = -ENOMEM;
		goto out;
	}
	init_timer(&ep->timer);
	ep->plen = conn_param->private_data_len;
	if (ep->plen)
		memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
		       conn_param->private_data, ep->plen);
	ep->ird = conn_param->ird;
	ep->ord = conn_param->ord;

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

	ep->com.cm_id = cm_id;
3154 3155
	ref_cm_id(&ep->com);
	ep->com.dev = dev;
3156
	ep->com.qp = get_qhp(dev, conn_param->qpn);
3157 3158 3159
	if (!ep->com.qp) {
		PDBG("%s qpn 0x%x not found!\n", __func__, conn_param->qpn);
		err = -EINVAL;
3160
		goto fail1;
3161
	}
3162
	ref_qp(ep);
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172
	PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
	     ep->com.qp, cm_id);

	/*
	 * Allocate an active TID to initiate a TCP connection.
	 */
	ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
	if (ep->atid == -1) {
		printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
		err = -ENOMEM;
3173
		goto fail1;
3174
	}
3175
	insert_handle(dev, &dev->atid_idr, ep, ep->atid);
3176

3177
	memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3178
	       sizeof(ep->com.local_addr));
3179
	memcpy(&ep->com.remote_addr, &cm_id->m_remote_addr,
3180 3181
	       sizeof(ep->com.remote_addr));

3182 3183 3184 3185
	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;
3186

3187
	if (cm_id->m_remote_addr.ss_family == AF_INET) {
3188 3189
		iptype = 4;
		ra = (__u8 *)&raddr->sin_addr;
3190

3191 3192 3193 3194 3195 3196
		/*
		 * Handle loopback requests to INADDR_ANY.
		 */
		if ((__force int)raddr->sin_addr.s_addr == INADDR_ANY) {
			err = pick_local_ipaddrs(dev, cm_id);
			if (err)
3197
				goto fail1;
3198 3199 3200 3201 3202 3203 3204 3205
		}

		/* find a route */
		PDBG("%s saddr %pI4 sport 0x%x raddr %pI4 rport 0x%x\n",
		     __func__, &laddr->sin_addr, ntohs(laddr->sin_port),
		     ra, ntohs(raddr->sin_port));
		ep->dst = find_route(dev, laddr->sin_addr.s_addr,
				     raddr->sin_addr.s_addr, laddr->sin_port,
H
Hariprasad S 已提交
3206
				     raddr->sin_port, cm_id->tos);
3207 3208 3209 3210 3211 3212 3213 3214 3215 3216
	} 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)
3217
				goto fail1;
3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230
		}

		/* find a route */
		PDBG("%s saddr %pI6 sport 0x%x raddr %pI6 rport 0x%x\n",
		     __func__, laddr6->sin6_addr.s6_addr,
		     ntohs(laddr6->sin6_port),
		     raddr6->sin6_addr.s6_addr, ntohs(raddr6->sin6_port));
		ep->dst = find_route6(dev, laddr6->sin6_addr.s6_addr,
				      raddr6->sin6_addr.s6_addr,
				      laddr6->sin6_port, raddr6->sin6_port, 0,
				      raddr6->sin6_scope_id);
	}
	if (!ep->dst) {
3231 3232
		printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
		err = -EHOSTUNREACH;
3233
		goto fail2;
3234 3235
	}

3236
	err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, true,
H
Hariprasad S 已提交
3237
			ep->com.dev->rdev.lldi.adapter_type, cm_id->tos);
3238
	if (err) {
3239
		printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
3240
		goto fail3;
3241 3242 3243 3244 3245 3246 3247
	}

	PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
		__func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
		ep->l2t->idx);

	state_set(&ep->com, CONNECTING);
H
Hariprasad S 已提交
3248
	ep->tos = cm_id->tos;
3249 3250 3251 3252 3253 3254 3255 3256

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

	cxgb4_l2t_release(ep->l2t);
fail3:
3257 3258
	dst_release(ep->dst);
fail2:
3259
	remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
3260
	cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
3261
fail1:
3262
	deref_cm_id(&ep->com);
3263 3264 3265 3266 3267
	c4iw_put_ep(&ep->com);
out:
	return err;
}

3268 3269 3270
static int create_server6(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
{
	int err;
3271
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
3272
				    &ep->com.local_addr;
3273

3274 3275 3276 3277 3278 3279
	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;
	}
3280 3281 3282 3283 3284 3285 3286 3287 3288
	c4iw_init_wr_wait(&ep->com.wr_wait);
	err = cxgb4_create_server6(ep->com.dev->rdev.lldi.ports[0],
				   ep->stid, &sin6->sin6_addr,
				   sin6->sin6_port,
				   ep->com.dev->rdev.lldi.rxq_ids[0]);
	if (!err)
		err = c4iw_wait_for_reply(&ep->com.dev->rdev,
					  &ep->com.wr_wait,
					  0, 0, __func__);
3289 3290
	else if (err > 0)
		err = net_xmit_errno(err);
3291 3292 3293
	if (err) {
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3294 3295 3296
		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));
3297
	}
3298 3299 3300 3301 3302 3303
	return err;
}

static int create_server4(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
{
	int err;
3304
	struct sockaddr_in *sin = (struct sockaddr_in *)
3305
				  &ep->com.local_addr;
3306 3307 3308 3309 3310 3311 3312 3313

	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) {
3314 3315 3316 3317
				if (c4iw_fatal_error(&ep->com.dev->rdev)) {
					err = -EIO;
					break;
				}
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330
				set_current_state(TASK_UNINTERRUPTIBLE);
				schedule_timeout(usecs_to_jiffies(100));
			}
		} while (err == -EBUSY);
	} else {
		c4iw_init_wr_wait(&ep->com.wr_wait);
		err = cxgb4_create_server(ep->com.dev->rdev.lldi.ports[0],
				ep->stid, sin->sin_addr.s_addr, sin->sin_port,
				0, ep->com.dev->rdev.lldi.rxq_ids[0]);
		if (!err)
			err = c4iw_wait_for_reply(&ep->com.dev->rdev,
						  &ep->com.wr_wait,
						  0, 0, __func__);
3331 3332
		else if (err > 0)
			err = net_xmit_errno(err);
3333 3334 3335 3336 3337 3338 3339 3340
	}
	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;
}

3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356
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) {
		printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
		err = -ENOMEM;
		goto fail1;
	}
	PDBG("%s ep %p\n", __func__, ep);
	ep->com.cm_id = cm_id;
3357
	ref_cm_id(&ep->com);
3358 3359
	ep->com.dev = dev;
	ep->backlog = backlog;
3360
	memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3361
	       sizeof(ep->com.local_addr));
3362 3363 3364 3365

	/*
	 * Allocate a server TID.
	 */
3366 3367
	if (dev->rdev.lldi.enable_fw_ofld_conn &&
	    ep->com.local_addr.ss_family == AF_INET)
3368
		ep->stid = cxgb4_alloc_sftid(dev->rdev.lldi.tids,
3369
					     cm_id->m_local_addr.ss_family, ep);
3370
	else
3371
		ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids,
3372
					    cm_id->m_local_addr.ss_family, ep);
3373

3374
	if (ep->stid == -1) {
3375
		printk(KERN_ERR MOD "%s - cannot alloc stid.\n", __func__);
3376 3377 3378
		err = -ENOMEM;
		goto fail2;
	}
3379
	insert_handle(dev, &dev->stid_idr, ep, ep->stid);
3380

3381 3382
	memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
	       sizeof(ep->com.local_addr));
3383

3384
	state_set(&ep->com, LISTEN);
3385 3386 3387 3388
	if (ep->com.local_addr.ss_family == AF_INET)
		err = create_server4(dev, ep);
	else
		err = create_server6(dev, ep);
3389 3390 3391 3392
	if (!err) {
		cm_id->provider_data = ep;
		goto out;
	}
3393

3394 3395
	cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
			ep->com.local_addr.ss_family);
3396
fail2:
3397
	deref_cm_id(&ep->com);
3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
	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);

	PDBG("%s ep %p\n", __func__, ep);

	might_sleep();
	state_set(&ep->com, DEAD);
3413 3414
	if (ep->com.dev->rdev.lldi.enable_fw_ofld_conn &&
	    ep->com.local_addr.ss_family == AF_INET) {
3415 3416 3417 3418
		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 已提交
3419
		struct sockaddr_in6 *sin6;
3420
		c4iw_init_wr_wait(&ep->com.wr_wait);
3421 3422 3423
		err = cxgb4_remove_server(
				ep->com.dev->rdev.lldi.ports[0], ep->stid,
				ep->com.dev->rdev.lldi.rxq_ids[0], 0);
3424 3425 3426 3427
		if (err)
			goto done;
		err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait,
					  0, 0, __func__);
3428
		sin6 = (struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
3429 3430
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3431
	}
3432
	remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
3433 3434
	cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
			ep->com.local_addr.ss_family);
3435
done:
3436
	deref_cm_id(&ep->com);
3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447
	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;

3448
	mutex_lock(&ep->com.mutex);
3449 3450 3451 3452

	PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
	     states[ep->com.state], abrupt);

3453 3454 3455 3456 3457 3458
	/*
	 * Ref the ep here in case we have fatal errors causing the
	 * ep to be released and freed.
	 */
	c4iw_get_ep(&ep->com);

3459 3460 3461
	rdev = &ep->com.dev->rdev;
	if (c4iw_fatal_error(rdev)) {
		fatal = 1;
3462
		close_complete_upcall(ep, -EIO);
3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475
		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:
		close = 1;
		if (abrupt)
			ep->com.state = ABORTING;
		else {
			ep->com.state = CLOSING;
3476
			start_ep_timer(ep);
3477 3478 3479 3480 3481 3482 3483
		}
		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 已提交
3484
				(void)stop_ep_timer(ep);
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501
				ep->com.state = ABORTING;
			} else
				ep->com.state = MORIBUND;
		}
		break;
	case MORIBUND:
	case ABORTING:
	case DEAD:
		PDBG("%s ignoring disconnect ep %p state %u\n",
		     __func__, ep, ep->com.state);
		break;
	default:
		BUG();
		break;
	}

	if (close) {
S
Steve Wise 已提交
3502
		if (abrupt) {
3503
			set_bit(EP_DISC_ABORT, &ep->com.history);
3504
			close_complete_upcall(ep, -ECONNRESET);
S
Steve Wise 已提交
3505
			ret = send_abort(ep, NULL, gfp);
3506 3507
		} else {
			set_bit(EP_DISC_CLOSE, &ep->com.history);
3508
			ret = send_halfclose(ep, gfp);
3509
		}
3510
		if (ret) {
3511
			set_bit(EP_DISC_FAIL, &ep->com.history);
3512 3513 3514 3515
			if (!abrupt) {
				stop_ep_timer(ep);
				close_complete_upcall(ep, -EIO);
			}
3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
			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)
					pr_err(MOD
					       "%s - qp <- error failed!\n",
					       __func__);
			}
3529
			fatal = 1;
3530
		}
3531
	}
S
Steve Wise 已提交
3532
	mutex_unlock(&ep->com.mutex);
3533
	c4iw_put_ep(&ep->com);
3534 3535 3536 3537 3538
	if (fatal)
		release_ep_resources(ep);
	return ret;
}

3539 3540 3541 3542
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;
3543
	int atid = be32_to_cpu(req->tid);
3544

3545 3546
	ep = (struct c4iw_ep *)lookup_atid(dev->rdev.lldi.tids,
					   (__force u32) req->tid);
3547 3548 3549 3550 3551
	if (!ep)
		return;

	switch (req->retval) {
	case FW_ENOMEM:
3552 3553 3554 3555 3556
		set_bit(ACT_RETRY_NOMEM, &ep->com.history);
		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
			send_fw_act_open_req(ep, atid);
			return;
		}
3557
	case FW_EADDRINUSE:
3558 3559 3560 3561 3562
		set_bit(ACT_RETRY_INUSE, &ep->com.history);
		if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
			send_fw_act_open_req(ep, atid);
			return;
		}
3563 3564 3565 3566 3567 3568
		break;
	default:
		pr_info("%s unexpected ofld conn wr retval %d\n",
		       __func__, req->retval);
		break;
	}
3569 3570 3571 3572 3573
	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);
3574
	connect_reply_upcall(ep, status2errno(req->retval));
3575
	state_set(&ep->com, DEAD);
H
Hariprasad S 已提交
3576 3577
	if (ep->com.remote_addr.ss_family == AF_INET6) {
		struct sockaddr_in6 *sin6 =
3578
			(struct sockaddr_in6 *)&ep->com.local_addr;
H
Hariprasad S 已提交
3579 3580 3581
		cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
				   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
	}
3582 3583 3584 3585 3586
	remove_handle(dev, &dev->atid_idr, atid);
	cxgb4_free_atid(dev->rdev.lldi.tids, atid);
	dst_release(ep->dst);
	cxgb4_l2t_release(ep->l2t);
	c4iw_put_ep(&ep->com);
3587 3588 3589 3590 3591 3592 3593 3594 3595
}

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;

3596
	rpl_skb = (struct sk_buff *)(unsigned long)req->cookie;
3597 3598 3599
	BUG_ON(!rpl_skb);
	if (req->retval) {
		PDBG("%s passive open failure %d\n", __func__, req->retval);
3600 3601 3602
		mutex_lock(&dev->rdev.stats.lock);
		dev->rdev.stats.pas_ofld_conn_fails++;
		mutex_unlock(&dev->rdev.stats.lock);
3603 3604 3605 3606
		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,
3607 3608
					(__force u32) htonl(
					(__force u32) req->tid)));
3609 3610 3611 3612 3613 3614 3615 3616
		ret = pass_accept_req(dev, rpl_skb);
		if (!ret)
			kfree_skb(rpl_skb);
	}
	return;
}

static int deferred_fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
3617 3618
{
	struct cpl_fw6_msg *rpl = cplhdr(skb);
3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645
	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)
{
3646 3647 3648 3649
	__be32 l2info;
	__be16 hdr_len, vlantag, len;
	u16 eth_hdr_len;
	int tcp_hdr_len, ip_hdr_len;
3650 3651 3652 3653
	u8 intf;
	struct cpl_rx_pkt *cpl = cplhdr(skb);
	struct cpl_pass_accept_req *req;
	struct tcp_options_received tmp_opt;
3654
	struct c4iw_dev *dev;
3655
	enum chip_type type;
3656

3657
	dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
3658
	/* Store values from cpl_rx_pkt in temporary location. */
3659 3660 3661 3662
	vlantag = cpl->vlan;
	len = cpl->len;
	l2info  = cpl->l2info;
	hdr_len = cpl->hdr_len;
3663 3664 3665 3666 3667 3668 3669 3670 3671 3672
	intf = cpl->iff;

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

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

	req = (struct cpl_pass_accept_req *)__skb_push(skb, sizeof(*req));
	memset(req, 0, sizeof(*req));
3677 3678
	req->l2info = cpu_to_be16(SYN_INTF_V(intf) |
			 SYN_MAC_IDX_V(RX_MACIDX_G(
3679
			 be32_to_cpu(l2info))) |
3680
			 SYN_XACT_MATCH_F);
3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700
	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;
3701 3702
	req->tos_stid = cpu_to_be32(PASS_OPEN_TID_V(stid) |
				    PASS_OPEN_TOS_V(tos));
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721
	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);
3722
	int ret;
3723 3724 3725 3726

	req_skb = alloc_skb(sizeof(struct fw_ofld_connection_wr), GFP_KERNEL);
	req = (struct fw_ofld_connection_wr *)__skb_put(req_skb, sizeof(*req));
	memset(req, 0, sizeof(*req));
3727
	req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR) | FW_WR_COMPL_F);
3728
	req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
3729
	req->le.version_cpl = htonl(FW_OFLD_CONNECTION_WR_CPL_F);
3730
	req->le.filter = (__force __be32) filter;
3731 3732 3733 3734 3735 3736 3737
	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 =
3738 3739 3740
		 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(
3741
			PASS_OPEN_TID_G(ntohl(cpl->tos_stid))));
3742 3743 3744 3745 3746

	/*
	 * We store the qid in opt2 which will be used by the firmware
	 * to send us the wr response.
	 */
3747
	req->tcb.opt2 = htonl(RSS_QUEUE_V(rss_qid));
3748 3749 3750 3751 3752 3753 3754

	/*
	 * 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.
	 */
3755
	req->tcb.opt0 = cpu_to_be64(MSS_IDX_V(0xF));
H
Hariprasad S 已提交
3756
	req->cookie = (uintptr_t)skb;
3757 3758

	set_wr_txq(req_skb, CPL_PRIORITY_CONTROL, port_id);
3759 3760 3761 3762 3763 3764 3765
	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);
	}
3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791
}

/*
 * 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;
	struct c4iw_ep *lep;
	u16 window;
	struct port_info *pi;
	struct net_device *pdev;
3792
	u16 rss_qid, eth_hdr_len;
3793 3794 3795 3796 3797
	int step;
	u32 tx_chan;
	struct neighbour *neigh;

	/* Drop all non-SYN packets */
3798
	if (!(cpl->l2info & cpu_to_be32(RXF_SYN_F)))
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810
		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.
	 */
3811
	stid = (__force int) cpu_to_be32((__force u32) rss->hash_val);
3812 3813 3814 3815 3816 3817 3818

	lep = (struct c4iw_ep *)lookup_stid(dev->rdev.lldi.tids, stid);
	if (!lep) {
		PDBG("%s connect request on invalid stid %d\n", __func__, stid);
		goto reject;
	}

3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834
	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;
	}

3835
	if (eth_hdr_len == ETH_HLEN) {
3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855
		eh = (struct ethhdr *)(req + 1);
		iph = (struct iphdr *)(eh + 1);
	} else {
		vlan_eh = (struct vlan_ethhdr *)(req + 1);
		iph = (struct iphdr *)(vlan_eh + 1);
		skb->vlan_tci = ntohs(cpl->vlan);
	}

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

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

	PDBG("%s lip 0x%x lport %u pip 0x%x pport %u tos %d\n", __func__,
	     ntohl(iph->daddr), ntohs(tcph->dest), ntohl(iph->saddr),
	     ntohs(tcph->source), iph->tos);

3856 3857 3858
	dst = find_route(dev, iph->daddr, iph->saddr, tcph->dest, tcph->source,
			 iph->tos);
	if (!dst) {
3859 3860 3861 3862 3863 3864
		pr_err("%s - failed to find dst entry!\n",
		       __func__);
		goto reject;
	}
	neigh = dst_neigh_lookup_skb(dst, skb);

3865 3866 3867 3868 3869 3870
	if (!neigh) {
		pr_err("%s - failed to allocate neigh!\n",
		       __func__);
		goto free_dst;
	}

3871 3872 3873 3874 3875 3876 3877 3878
	if (neigh->dev->flags & IFF_LOOPBACK) {
		pdev = ip_dev_find(&init_net, iph->daddr);
		e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
				    pdev, 0);
		pi = (struct port_info *)netdev_priv(pdev);
		tx_chan = cxgb4_port_chan(pdev);
		dev_put(pdev);
	} else {
3879
		pdev = get_real_dev(neigh->dev);
3880
		e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3881 3882 3883
					pdev, 0);
		pi = (struct port_info *)netdev_priv(pdev);
		tx_chan = cxgb4_port_chan(pdev);
3884
	}
3885
	neigh_release(neigh);
3886 3887 3888 3889 3890 3891 3892 3893
	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];
3894
	window = (__force u16) htons((__force u16)tcph->window);
3895 3896

	/* Calcuate filter portion for LE region. */
3897 3898 3899
	filter = (__force unsigned int) cpu_to_be32(cxgb4_select_ntuple(
						    dev->rdev.lldi.ports[0],
						    e));
3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913

	/*
	 * 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:
3914 3915 3916
	return 0;
}

3917 3918 3919 3920
/*
 * These are the real handlers that are called from a
 * work queue.
 */
3921
static c4iw_handler_func work_handlers[NUM_CPL_CMDS + NUM_FAKE_CPLS] = {
3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934
	[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,
3935
	[CPL_FW4_ACK] = fw4_ack,
3936
	[CPL_FW6_MSG] = deferred_fw6_msg,
3937 3938
	[CPL_RX_PKT] = rx_pkt,
	[FAKE_CPL_PUT_EP_SAFE] = _put_ep_safe
3939 3940 3941 3942 3943 3944 3945
};

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

3946
	mutex_lock(&ep->com.mutex);
3947 3948
	PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
	     ep->com.state);
3949
	set_bit(TIMEDOUT, &ep->com.history);
3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966
	switch (ep->com.state) {
	case MPA_REQ_SENT:
		__state_set(&ep->com, ABORTING);
		connect_reply_upcall(ep, -ETIMEDOUT);
		break;
	case MPA_REQ_WAIT:
		__state_set(&ep->com, ABORTING);
		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);
		}
		__state_set(&ep->com, ABORTING);
3967
		close_complete_upcall(ep, -ETIMEDOUT);
3968
		break;
S
Steve Wise 已提交
3969 3970 3971 3972 3973 3974 3975 3976 3977 3978
	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;
3979
	default:
J
Julia Lawall 已提交
3980
		WARN(1, "%s unexpected state ep %p tid %u state %u\n",
3981 3982 3983
			__func__, ep, ep->hwtid, ep->com.state);
		abort = 0;
	}
3984
	mutex_unlock(&ep->com.mutex);
3985 3986
	if (abort)
		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999
	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 已提交
4000 4001
		tmp->next = NULL;
		tmp->prev = NULL;
4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013
		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;
4014
	struct cpl_act_establish *rpl;
4015 4016 4017
	unsigned int opcode;
	int ret;

S
Steve Wise 已提交
4018
	process_timedout_eps();
4019 4020 4021 4022 4023 4024 4025 4026 4027
	while ((skb = skb_dequeue(&rxq))) {
		rpl = cplhdr(skb);
		dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
		opcode = rpl->ot.opcode;

		BUG_ON(!work_handlers[opcode]);
		ret = work_handlers[opcode](dev, skb);
		if (!ret)
			kfree_skb(skb);
S
Steve Wise 已提交
4028
		process_timedout_eps();
4029 4030 4031 4032 4033 4034 4035 4036
	}
}

static DECLARE_WORK(skb_work, process_work);

static void ep_timeout(unsigned long arg)
{
	struct c4iw_ep *ep = (struct c4iw_ep *)arg;
4037
	int kickit = 0;
4038 4039

	spin_lock(&timeout_lock);
4040
	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
S
Steve Wise 已提交
4041 4042 4043 4044 4045 4046 4047
		/*
		 * Only insert if it is not already on the list.
		 */
		if (!ep->entry.next) {
			list_add_tail(&ep->entry, &timeout_list);
			kickit = 1;
		}
4048
	}
4049
	spin_unlock(&timeout_lock);
4050 4051
	if (kickit)
		queue_work(workq, &skb_work);
4052 4053
}

4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080
/*
 * 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) {
		printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
		       "for tid %u\n", rpl->status, GET_TID(rpl));
	}
4081
	kfree_skb(skb);
4082 4083 4084
	return 0;
}

4085 4086 4087 4088 4089 4090 4091 4092 4093
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;

	PDBG("%s type %u\n", __func__, rpl->type);

	switch (rpl->type) {
4094
	case FW6_TYPE_WR_RPL:
4095
		ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
4096
		wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
4097
		PDBG("%s wr_waitp %p ret %u\n", __func__, wr_waitp, ret);
4098 4099
		if (wr_waitp)
			c4iw_wake_up(wr_waitp, ret ? -ret : 0);
4100
		kfree_skb(skb);
4101
		break;
4102 4103
	case FW6_TYPE_CQE:
	case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
4104
		sched(dev, skb);
4105
		break;
4106 4107 4108
	default:
		printk(KERN_ERR MOD "%s unexpected fw6 msg type %u\n", __func__,
		       rpl->type);
4109
		kfree_skb(skb);
4110 4111 4112 4113 4114
		break;
	}
	return 0;
}

S
Steve Wise 已提交
4115 4116 4117 4118 4119 4120 4121 4122
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;
	struct tid_info *t = dev->rdev.lldi.tids;
	unsigned int tid = GET_TID(req);

	ep = lookup_tid(t, tid);
4123 4124 4125 4126 4127 4128
	if (!ep) {
		printk(KERN_WARNING MOD
		       "Abort on non-existent endpoint, tid %d\n", tid);
		kfree_skb(skb);
		return 0;
	}
4129
	if (is_neg_adv(req->status)) {
4130 4131 4132 4133 4134
		PDBG("%s Negative advice on abort- tid %u status %d (%s)\n",
		     __func__, ep->hwtid, req->status,
		     neg_adv_str(req->status));
		ep->stats.abort_neg_adv++;
		dev->rdev.stats.neg_adv++;
S
Steve Wise 已提交
4135 4136 4137 4138 4139 4140 4141 4142
		kfree_skb(skb);
		return 0;
	}
	PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
	     ep->com.state);

	/*
	 * Wake up any threads in rdma_init() or rdma_fini().
4143 4144
	 * However, if we are on MPAv2 and want to retry with MPAv1
	 * then, don't wake up yet.
S
Steve Wise 已提交
4145
	 */
4146 4147 4148 4149 4150
	if (mpa_rev == 2 && !ep->tried_with_mpa_v1) {
		if (ep->com.state != MPA_REQ_SENT)
			c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
	} else
		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
S
Steve Wise 已提交
4151 4152 4153 4154
	sched(dev, skb);
	return 0;
}

4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170
/*
 * 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 已提交
4171
	[CPL_ABORT_REQ_RSS] = peer_abort_intr,
4172 4173 4174
	[CPL_RDMA_TERMINATE] = sched,
	[CPL_FW4_ACK] = sched,
	[CPL_SET_TCB_RPL] = set_tcb_rpl,
4175 4176
	[CPL_FW6_MSG] = fw6_msg,
	[CPL_RX_PKT] = sched
4177 4178
};

4179 4180
int __init c4iw_cm_init(void)
{
4181
	spin_lock_init(&timeout_lock);
4182 4183 4184 4185 4186 4187 4188 4189 4190
	skb_queue_head_init(&rxq);

	workq = create_singlethread_workqueue("iw_cxgb4");
	if (!workq)
		return -ENOMEM;

	return 0;
}

4191
void c4iw_cm_term(void)
4192
{
4193
	WARN_ON(!list_empty(&timeout_list));
4194 4195 4196
	flush_workqueue(workq);
	destroy_workqueue(workq);
}