cxgb4i.c 68.8 KB
Newer Older
1 2 3
/*
 * cxgb4i.c: Chelsio T4 iSCSI driver.
 *
K
Karen Xie 已提交
4
 * Copyright (c) 2010-2015 Chelsio Communications, Inc.
5 6 7 8 9 10 11 12 13 14 15
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Written by:	Karen Xie (kxie@chelsio.com)
 *		Rakesh Ranjan (rranjan@chelsio.com)
 */

#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__

16
#include <linux/kernel.h>
17 18 19 20 21 22
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <scsi/scsi_host.h>
#include <net/tcp.h>
#include <net/dst.h>
#include <linux/netdevice.h>
23
#include <net/addrconf.h>
24

25
#include "t4_regs.h"
26 27 28 29 30 31
#include "t4_msg.h"
#include "cxgb4.h"
#include "cxgb4_uld.h"
#include "t4fw_api.h"
#include "l2t.h"
#include "cxgb4i.h"
32
#include "clip_tbl.h"
33 34 35 36 37

static unsigned int dbg_level;

#include "../libcxgbi.h"

38 39 40 41 42
#ifdef CONFIG_CHELSIO_T4_DCB
#include <net/dcbevent.h>
#include "cxgb4_dcb.h"
#endif

43
#define	DRV_MODULE_NAME		"cxgb4i"
44
#define DRV_MODULE_DESC		"Chelsio T4-T6 iSCSI Driver"
K
Karen Xie 已提交
45 46
#define	DRV_MODULE_VERSION	"0.9.5-ko"
#define DRV_MODULE_RELDATE	"Apr. 2015"
47 48 49

static char version[] =
	DRV_MODULE_DESC " " DRV_MODULE_NAME
K
Karen Xie 已提交
50
	" v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
51 52 53 54 55 56 57 58 59

MODULE_AUTHOR("Chelsio Communications, Inc.");
MODULE_DESCRIPTION(DRV_MODULE_DESC);
MODULE_VERSION(DRV_MODULE_VERSION);
MODULE_LICENSE("GPL");

module_param(dbg_level, uint, 0644);
MODULE_PARM_DESC(dbg_level, "Debug flag (default=0)");

60 61
#define CXGB4I_DEFAULT_10G_RCV_WIN (256 * 1024)
static int cxgb4i_rcv_win = -1;
62
module_param(cxgb4i_rcv_win, int, 0644);
63
MODULE_PARM_DESC(cxgb4i_rcv_win, "TCP receive window in bytes");
64

65 66
#define CXGB4I_DEFAULT_10G_SND_WIN (128 * 1024)
static int cxgb4i_snd_win = -1;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
module_param(cxgb4i_snd_win, int, 0644);
MODULE_PARM_DESC(cxgb4i_snd_win, "TCP send window in bytes");

static int cxgb4i_rx_credit_thres = 10 * 1024;
module_param(cxgb4i_rx_credit_thres, int, 0644);
MODULE_PARM_DESC(cxgb4i_rx_credit_thres,
		"RX credits return threshold in bytes (default=10KB)");

static unsigned int cxgb4i_max_connect = (8 * 1024);
module_param(cxgb4i_max_connect, uint, 0644);
MODULE_PARM_DESC(cxgb4i_max_connect, "Maximum number of connections");

static unsigned short cxgb4i_sport_base = 20000;
module_param(cxgb4i_sport_base, ushort, 0644);
MODULE_PARM_DESC(cxgb4i_sport_base, "Starting port number (default 20000)");

typedef void (*cxgb4i_cplhandler_func)(struct cxgbi_device *, struct sk_buff *);

static void *t4_uld_add(const struct cxgb4_lld_info *);
static int t4_uld_rx_handler(void *, const __be64 *, const struct pkt_gl *);
static int t4_uld_state_change(void *, enum cxgb4_state state);
88
static inline int send_tx_flowc_wr(struct cxgbi_sock *);
89 90 91

static const struct cxgb4_uld_info cxgb4i_uld_info = {
	.name = DRV_MODULE_NAME,
92
	.nrxq = MAX_ULD_QSETS,
93
	.ntxq = MAX_ULD_QSETS,
94 95
	.rxq_size = 1024,
	.lro = false,
96 97 98 99 100 101 102 103 104 105 106
	.add = t4_uld_add,
	.rx_handler = t4_uld_rx_handler,
	.state_change = t4_uld_state_change,
};

static struct scsi_host_template cxgb4i_host_template = {
	.module		= THIS_MODULE,
	.name		= DRV_MODULE_NAME,
	.proc_name	= DRV_MODULE_NAME,
	.can_queue	= CXGB4I_SCSI_HOST_QDEPTH,
	.queuecommand	= iscsi_queuecommand,
107
	.change_queue_depth = scsi_change_queue_depth,
108 109 110
	.sg_tablesize	= SG_ALL,
	.max_sectors	= 0xFFFF,
	.cmd_per_lun	= ISCSI_DEF_CMD_PER_LUN,
111
	.eh_timed_out	= iscsi_eh_cmd_timed_out,
112 113 114 115
	.eh_abort_handler = iscsi_eh_abort,
	.eh_device_reset_handler = iscsi_eh_device_reset,
	.eh_target_reset_handler = iscsi_eh_recover_target,
	.target_alloc	= iscsi_target_alloc,
116
	.dma_boundary	= PAGE_SIZE - 1,
117
	.this_id	= -1,
118
	.track_queue_depth = 1,
119 120 121 122 123 124 125
};

static struct iscsi_transport cxgb4i_iscsi_transport = {
	.owner		= THIS_MODULE,
	.name		= DRV_MODULE_NAME,
	.caps		= CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST |
				CAP_DATADGST | CAP_DIGEST_OFFLOAD |
126
				CAP_PADDING_OFFLOAD | CAP_TEXT_NEGO,
127
	.attr_is_visible	= cxgbi_attr_is_visible,
128 129 130 131 132 133 134 135 136
	.get_host_param	= cxgbi_get_host_param,
	.set_host_param	= cxgbi_set_host_param,
	/* session management */
	.create_session	= cxgbi_create_session,
	.destroy_session	= cxgbi_destroy_session,
	.get_session_param = iscsi_session_get_param,
	/* connection management */
	.create_conn	= cxgbi_create_conn,
	.bind_conn		= cxgbi_bind_conn,
137
	.unbind_conn	= iscsi_conn_unbind,
138 139 140
	.destroy_conn	= iscsi_tcp_conn_teardown,
	.start_conn		= iscsi_conn_start,
	.stop_conn		= iscsi_conn_stop,
141
	.get_conn_param	= iscsi_conn_get_param,
142 143 144 145 146 147 148 149 150 151 152 153 154 155
	.set_param	= cxgbi_set_conn_param,
	.get_stats	= cxgbi_get_conn_stats,
	/* pdu xmit req from user space */
	.send_pdu	= iscsi_conn_send_pdu,
	/* task */
	.init_task	= iscsi_tcp_task_init,
	.xmit_task	= iscsi_tcp_task_xmit,
	.cleanup_task	= cxgbi_cleanup_task,
	/* pdu */
	.alloc_pdu	= cxgbi_conn_alloc_pdu,
	.init_pdu	= cxgbi_conn_init_pdu,
	.xmit_pdu	= cxgbi_conn_xmit_pdu,
	.parse_pdu_itt	= cxgbi_parse_pdu_itt,
	/* TCP connect/disconnect */
156
	.get_ep_param	= cxgbi_get_ep_param,
157 158 159 160 161 162 163
	.ep_connect	= cxgbi_ep_connect,
	.ep_poll	= cxgbi_ep_poll,
	.ep_disconnect	= cxgbi_ep_disconnect,
	/* Error recovery timeout call */
	.session_recovery_timedout = iscsi_session_recovery_timedout,
};

164 165 166 167 168 169 170 171 172
#ifdef CONFIG_CHELSIO_T4_DCB
static int
cxgb4_dcb_change_notify(struct notifier_block *, unsigned long, void *);

static struct notifier_block cxgb4_dcb_change = {
	.notifier_call = cxgb4_dcb_change_notify,
};
#endif

173 174 175 176 177 178 179 180
static struct scsi_transport_template *cxgb4i_stt;

/*
 * CPL (Chelsio Protocol Language) defines a message passing interface between
 * the host driver and Chelsio asic.
 * The section below implments CPLs that related to iscsi tcp connection
 * open/close/abort and data send/receive.
 */
181

182
#define RCV_BUFSIZ_MASK		0x3FFU
183
#define MAX_IMM_TX_PKT_LEN	256
184 185 186 187 188 189 190 191 192 193

static int push_tx_frames(struct cxgbi_sock *, int);

/*
 * is_ofld_imm - check whether a packet can be sent as immediate data
 * @skb: the packet
 *
 * Returns true if a packet can be sent as an offload WR with immediate
 * data.  We currently use the same limit as for Ethernet packets.
 */
194
static inline bool is_ofld_imm(const struct sk_buff *skb)
195
{
196 197 198 199 200
	int len = skb->len;

	if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_NEED_HDR)))
		len += sizeof(struct fw_ofld_tx_data_wr);

201 202 203 204
	if  (likely(cxgbi_skcb_test_flag((struct sk_buff *)skb, SKCBF_TX_ISO)))
		len += sizeof(struct cpl_tx_data_iso);

	return (len <= MAX_IMM_OFLD_TX_DATA_WR_LEN);
205 206 207 208 209
}

static void send_act_open_req(struct cxgbi_sock *csk, struct sk_buff *skb,
				struct l2t_entry *e)
{
210
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
211 212 213 214 215 216
	int wscale = cxgbi_sock_compute_wscale(csk->mss_idx);
	unsigned long long opt0;
	unsigned int opt2;
	unsigned int qid_atid = ((unsigned int)csk->atid) |
				 (((unsigned int)csk->rss_qid) << 14);

217 218 219 220 221 222 223
	opt0 = KEEP_ALIVE_F |
		WND_SCALE_V(wscale) |
		MSS_IDX_V(csk->mss_idx) |
		L2T_IDX_V(((struct l2t_entry *)csk->l2t)->idx) |
		TX_CHAN_V(csk->tx_chan) |
		SMAC_SEL_V(csk->smac_idx) |
		ULP_MODE_V(ULP_MODE_ISCSI) |
224
		RCV_BUFSIZ_V(csk->rcv_win >> 10);
225

226 227 228
	opt2 = RX_CHANNEL_V(0) |
		RSS_QUEUE_VALID_F |
		RSS_QUEUE_V(csk->rss_qid);
229

230 231 232 233 234 235
	if (is_t4(lldi->adapter_type)) {
		struct cpl_act_open_req *req =
				(struct cpl_act_open_req *)skb->head;

		INIT_TP_WR(req, 0);
		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
236
					qid_atid));
237 238 239 240 241
		req->local_port = csk->saddr.sin_port;
		req->peer_port = csk->daddr.sin_port;
		req->local_ip = csk->saddr.sin_addr.s_addr;
		req->peer_ip = csk->daddr.sin_addr.s_addr;
		req->opt0 = cpu_to_be64(opt0);
242 243 244
		req->params = cpu_to_be32(cxgb4_select_ntuple(
					csk->cdev->ports[csk->port_id],
					csk->l2t));
245
		opt2 |= RX_FC_VALID_F;
246
		req->opt2 = cpu_to_be32(opt2);
247

248 249 250 251 252
		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
			"csk t4 0x%p, %pI4:%u-%pI4:%u, atid %d, qid %u.\n",
			csk, &req->local_ip, ntohs(req->local_port),
			&req->peer_ip, ntohs(req->peer_port),
			csk->atid, csk->rss_qid);
253
	} else if (is_t5(lldi->adapter_type)) {
254 255
		struct cpl_t5_act_open_req *req =
				(struct cpl_t5_act_open_req *)skb->head;
256
		u32 isn = (prandom_u32() & ~7UL) - 1;
257 258 259 260 261 262 263 264 265

		INIT_TP_WR(req, 0);
		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
					qid_atid));
		req->local_port = csk->saddr.sin_port;
		req->peer_port = csk->daddr.sin_port;
		req->local_ip = csk->saddr.sin_addr.s_addr;
		req->peer_ip = csk->daddr.sin_addr.s_addr;
		req->opt0 = cpu_to_be64(opt0);
266
		req->params = cpu_to_be64(FILTER_TUPLE_V(
267 268 269
				cxgb4_select_ntuple(
					csk->cdev->ports[csk->port_id],
					csk->l2t)));
270 271 272 273
		req->rsvd = cpu_to_be32(isn);
		opt2 |= T5_ISS_VALID;
		opt2 |= T5_OPT_2_VALID_F;

274 275 276 277 278 279 280
		req->opt2 = cpu_to_be32(opt2);

		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
			"csk t5 0x%p, %pI4:%u-%pI4:%u, atid %d, qid %u.\n",
			csk, &req->local_ip, ntohs(req->local_port),
			&req->peer_ip, ntohs(req->peer_port),
			csk->atid, csk->rss_qid);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	} else {
		struct cpl_t6_act_open_req *req =
				(struct cpl_t6_act_open_req *)skb->head;
		u32 isn = (prandom_u32() & ~7UL) - 1;

		INIT_TP_WR(req, 0);
		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
							    qid_atid));
		req->local_port = csk->saddr.sin_port;
		req->peer_port = csk->daddr.sin_port;
		req->local_ip = csk->saddr.sin_addr.s_addr;
		req->peer_ip = csk->daddr.sin_addr.s_addr;
		req->opt0 = cpu_to_be64(opt0);
		req->params = cpu_to_be64(FILTER_TUPLE_V(
				cxgb4_select_ntuple(
					csk->cdev->ports[csk->port_id],
					csk->l2t)));
		req->rsvd = cpu_to_be32(isn);

		opt2 |= T5_ISS_VALID;
		opt2 |= RX_FC_DISABLE_F;
		opt2 |= T5_OPT_2_VALID_F;

		req->opt2 = cpu_to_be32(opt2);
		req->rsvd2 = cpu_to_be32(0);
		req->opt3 = cpu_to_be32(0);

		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
			  "csk t6 0x%p, %pI4:%u-%pI4:%u, atid %d, qid %u.\n",
			  csk, &req->local_ip, ntohs(req->local_port),
			  &req->peer_ip, ntohs(req->peer_port),
			  csk->atid, csk->rss_qid);
313 314 315
	}

	set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);
316 317

	pr_info_ipaddr("t%d csk 0x%p,%u,0x%lx,%u, rss_qid %u.\n",
318 319
		       (&csk->saddr), (&csk->daddr),
		       CHELSIO_CHIP_VERSION(lldi->adapter_type), csk,
320 321 322 323 324
		       csk->state, csk->flags, csk->atid, csk->rss_qid);

	cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
}

325
#if IS_ENABLED(CONFIG_IPV6)
326 327 328 329 330 331 332 333 334 335
static void send_act_open_req6(struct cxgbi_sock *csk, struct sk_buff *skb,
			       struct l2t_entry *e)
{
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
	int wscale = cxgbi_sock_compute_wscale(csk->mss_idx);
	unsigned long long opt0;
	unsigned int opt2;
	unsigned int qid_atid = ((unsigned int)csk->atid) |
				 (((unsigned int)csk->rss_qid) << 14);

336 337 338 339 340 341 342
	opt0 = KEEP_ALIVE_F |
		WND_SCALE_V(wscale) |
		MSS_IDX_V(csk->mss_idx) |
		L2T_IDX_V(((struct l2t_entry *)csk->l2t)->idx) |
		TX_CHAN_V(csk->tx_chan) |
		SMAC_SEL_V(csk->smac_idx) |
		ULP_MODE_V(ULP_MODE_ISCSI) |
343
		RCV_BUFSIZ_V(csk->rcv_win >> 10);
344

345 346 347
	opt2 = RX_CHANNEL_V(0) |
		RSS_QUEUE_VALID_F |
		RSS_QUEUE_V(csk->rss_qid);
348

349
	if (is_t4(lldi->adapter_type)) {
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
		struct cpl_act_open_req6 *req =
			    (struct cpl_act_open_req6 *)skb->head;

		INIT_TP_WR(req, 0);
		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
							    qid_atid));
		req->local_port = csk->saddr6.sin6_port;
		req->peer_port = csk->daddr6.sin6_port;

		req->local_ip_hi = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr);
		req->local_ip_lo = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr +
								    8);
		req->peer_ip_hi = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr);
		req->peer_ip_lo = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr +
								    8);

		req->opt0 = cpu_to_be64(opt0);

368
		opt2 |= RX_FC_VALID_F;
369 370 371 372 373
		req->opt2 = cpu_to_be32(opt2);

		req->params = cpu_to_be32(cxgb4_select_ntuple(
					  csk->cdev->ports[csk->port_id],
					  csk->l2t));
374
	} else if (is_t5(lldi->adapter_type)) {
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		struct cpl_t5_act_open_req6 *req =
				(struct cpl_t5_act_open_req6 *)skb->head;

		INIT_TP_WR(req, 0);
		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
							    qid_atid));
		req->local_port = csk->saddr6.sin6_port;
		req->peer_port = csk->daddr6.sin6_port;
		req->local_ip_hi = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr);
		req->local_ip_lo = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr +
									8);
		req->peer_ip_hi = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr);
		req->peer_ip_lo = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr +
									8);
		req->opt0 = cpu_to_be64(opt0);

391
		opt2 |= T5_OPT_2_VALID_F;
392 393
		req->opt2 = cpu_to_be32(opt2);

394
		req->params = cpu_to_be64(FILTER_TUPLE_V(cxgb4_select_ntuple(
395 396
					  csk->cdev->ports[csk->port_id],
					  csk->l2t)));
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	} else {
		struct cpl_t6_act_open_req6 *req =
				(struct cpl_t6_act_open_req6 *)skb->head;

		INIT_TP_WR(req, 0);
		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
							    qid_atid));
		req->local_port = csk->saddr6.sin6_port;
		req->peer_port = csk->daddr6.sin6_port;
		req->local_ip_hi = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr);
		req->local_ip_lo = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr +
									8);
		req->peer_ip_hi = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr);
		req->peer_ip_lo = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr +
									8);
		req->opt0 = cpu_to_be64(opt0);

		opt2 |= RX_FC_DISABLE_F;
		opt2 |= T5_OPT_2_VALID_F;

		req->opt2 = cpu_to_be32(opt2);

		req->params = cpu_to_be64(FILTER_TUPLE_V(cxgb4_select_ntuple(
					  csk->cdev->ports[csk->port_id],
					  csk->l2t)));

		req->rsvd2 = cpu_to_be32(0);
		req->opt3 = cpu_to_be32(0);
425 426 427 428 429
	}

	set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);

	pr_info("t%d csk 0x%p,%u,0x%lx,%u, [%pI6]:%u-[%pI6]:%u, rss_qid %u.\n",
430 431
		CHELSIO_CHIP_VERSION(lldi->adapter_type), csk, csk->state,
		csk->flags, csk->atid,
432 433 434 435
		&csk->saddr6.sin6_addr, ntohs(csk->saddr.sin_port),
		&csk->daddr6.sin6_addr, ntohs(csk->daddr.sin_port),
		csk->rss_qid);

436 437
	cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
}
438
#endif
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

static void send_close_req(struct cxgbi_sock *csk)
{
	struct sk_buff *skb = csk->cpl_close;
	struct cpl_close_con_req *req = (struct cpl_close_con_req *)skb->head;
	unsigned int tid = csk->tid;

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p,%u,0x%lx, tid %u.\n",
		csk, csk->state, csk->flags, csk->tid);
	csk->cpl_close = NULL;
	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
	INIT_TP_WR(req, tid);
	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
	req->rsvd = 0;

	cxgbi_sock_skb_entail(csk, skb);
	if (csk->state >= CTP_ESTABLISHED)
		push_tx_frames(csk, 1);
}

static void abort_arp_failure(void *handle, struct sk_buff *skb)
{
	struct cxgbi_sock *csk = (struct cxgbi_sock *)handle;
	struct cpl_abort_req *req;

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p,%u,0x%lx, tid %u, abort.\n",
		csk, csk->state, csk->flags, csk->tid);
	req = (struct cpl_abort_req *)skb->data;
	req->cmd = CPL_ABORT_NO_RST;
	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
}

static void send_abort_req(struct cxgbi_sock *csk)
{
	struct cpl_abort_req *req;
	struct sk_buff *skb = csk->cpl_abort_req;

	if (unlikely(csk->state == CTP_ABORTING) || !skb || !csk->cdev)
		return;
480 481 482 483 484 485

	if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
		send_tx_flowc_wr(csk);
		cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
	}

486 487 488 489 490 491
	cxgbi_sock_set_state(csk, CTP_ABORTING);
	cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_PENDING);
	cxgbi_sock_purge_write_queue(csk);

	csk->cpl_abort_req = NULL;
	req = (struct cpl_abort_req *)skb->head;
492
	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	req->cmd = CPL_ABORT_SEND_RST;
	t4_set_arp_err_handler(skb, csk, abort_arp_failure);
	INIT_TP_WR(req, csk->tid);
	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, csk->tid));
	req->rsvd0 = htonl(csk->snd_nxt);
	req->rsvd1 = !cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT);

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p,%u,0x%lx,%u, snd_nxt %u, 0x%x.\n",
		csk, csk->state, csk->flags, csk->tid, csk->snd_nxt,
		req->rsvd1);

	cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
}

static void send_abort_rpl(struct cxgbi_sock *csk, int rst_status)
{
	struct sk_buff *skb = csk->cpl_abort_rpl;
	struct cpl_abort_rpl *rpl = (struct cpl_abort_rpl *)skb->head;

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p,%u,0x%lx,%u, status %d.\n",
		csk, csk->state, csk->flags, csk->tid, rst_status);

	csk->cpl_abort_rpl = NULL;
518
	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	INIT_TP_WR(rpl, csk->tid);
	OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, csk->tid));
	rpl->cmd = rst_status;
	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
}

/*
 * CPL connection rx data ack: host ->
 * Send RX credits through an RX_DATA_ACK CPL message. Returns the number of
 * credits sent.
 */
static u32 send_rx_credits(struct cxgbi_sock *csk, u32 credits)
{
	struct sk_buff *skb;
	struct cpl_rx_data_ack *req;

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
		"csk 0x%p,%u,0x%lx,%u, credit %u.\n",
		csk, csk->state, csk->flags, csk->tid, credits);

539
	skb = alloc_wr(sizeof(*req), 0, GFP_ATOMIC);
540 541 542 543 544 545 546 547 548 549
	if (!skb) {
		pr_info("csk 0x%p, credit %u, OOM.\n", csk, credits);
		return 0;
	}
	req = (struct cpl_rx_data_ack *)skb->head;

	set_wr_txq(skb, CPL_PRIORITY_ACK, csk->port_id);
	INIT_TP_WR(req, csk->tid);
	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
				      csk->tid));
550 551
	req->credit_dack = cpu_to_be32(RX_CREDITS_V(credits)
				       | RX_FORCE_ACK_F);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
	return credits;
}

/*
 * sgl_len - calculates the size of an SGL of the given capacity
 * @n: the number of SGL entries
 * Calculates the number of flits needed for a scatter/gather list that
 * can hold the given number of entries.
 */
static inline unsigned int sgl_len(unsigned int n)
{
	n--;
	return (3 * n) / 2 + (n & 1) + 2;
}

/*
 * calc_tx_flits_ofld - calculate # of flits for an offload packet
 * @skb: the packet
 *
 * Returns the number of flits needed for the given offload packet.
 * These packets are already fully constructed and no additional headers
 * will be added.
 */
static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
{
	unsigned int flits, cnt;

	if (is_ofld_imm(skb))
		return DIV_ROUND_UP(skb->len, 8);
	flits = skb_transport_offset(skb) / 8;
	cnt = skb_shinfo(skb)->nr_frags;
584
	if (skb_tail_pointer(skb) != skb_transport_header(skb))
585 586 587 588
		cnt++;
	return flits + sgl_len(cnt);
}

589 590 591 592 593 594
#define FLOWC_WR_NPARAMS_MIN	9
static inline int tx_flowc_wr_credits(int *nparamsp, int *flowclenp)
{
	int nparams, flowclen16, flowclen;

	nparams = FLOWC_WR_NPARAMS_MIN;
595 596 597
#ifdef CONFIG_CHELSIO_T4_DCB
	nparams++;
#endif
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
	flowclen16 = DIV_ROUND_UP(flowclen, 16);
	flowclen = flowclen16 * 16;
	/*
	 * Return the number of 16-byte credits used by the FlowC request.
	 * Pass back the nparams and actual FlowC length if requested.
	 */
	if (nparamsp)
		*nparamsp = nparams;
	if (flowclenp)
		*flowclenp = flowclen;

	return flowclen16;
}

static inline int send_tx_flowc_wr(struct cxgbi_sock *csk)
614 615 616
{
	struct sk_buff *skb;
	struct fw_flowc_wr *flowc;
617
	int nparams, flowclen16, flowclen;
618

619 620 621
#ifdef CONFIG_CHELSIO_T4_DCB
	u16 vlan = ((struct l2t_entry *)csk->l2t)->vlan;
#endif
622
	flowclen16 = tx_flowc_wr_credits(&nparams, &flowclen);
623
	skb = alloc_wr(flowclen, 0, GFP_ATOMIC);
624 625
	flowc = (struct fw_flowc_wr *)skb->head;
	flowc->op_to_nparams =
626
		htonl(FW_WR_OP_V(FW_FLOWC_WR) | FW_FLOWC_WR_NPARAMS_V(nparams));
627
	flowc->flowid_len16 =
628
		htonl(FW_WR_LEN16_V(flowclen16) | FW_WR_FLOWID_V(csk->tid));
629
	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
630
	flowc->mnemval[0].val = htonl(csk->cdev->pfvf);
631 632 633 634 635 636 637 638 639 640 641
	flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
	flowc->mnemval[1].val = htonl(csk->tx_chan);
	flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
	flowc->mnemval[2].val = htonl(csk->tx_chan);
	flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
	flowc->mnemval[3].val = htonl(csk->rss_qid);
	flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
	flowc->mnemval[4].val = htonl(csk->snd_nxt);
	flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
	flowc->mnemval[5].val = htonl(csk->rcv_nxt);
	flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
642
	flowc->mnemval[6].val = htonl(csk->snd_win);
643 644 645 646
	flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
	flowc->mnemval[7].val = htonl(csk->advmss);
	flowc->mnemval[8].mnemonic = 0;
	flowc->mnemval[8].val = 0;
647
	flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_TXDATAPLEN_MAX;
648 649 650 651
	if (csk->cdev->skb_iso_txhdr)
		flowc->mnemval[8].val = cpu_to_be32(CXGBI_MAX_ISO_DATA_IN_SKB);
	else
		flowc->mnemval[8].val = cpu_to_be32(16128);
652 653 654 655 656 657 658 659 660 661 662
#ifdef CONFIG_CHELSIO_T4_DCB
	flowc->mnemval[9].mnemonic = FW_FLOWC_MNEM_DCBPRIO;
	if (vlan == CPL_L2T_VLAN_NONE) {
		pr_warn_ratelimited("csk %u without VLAN Tag on DCB Link\n",
				    csk->tid);
		flowc->mnemval[9].val = cpu_to_be32(0);
	} else {
		flowc->mnemval[9].val = cpu_to_be32((vlan & VLAN_PRIO_MASK) >>
					VLAN_PRIO_SHIFT);
	}
#endif
663

664
	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
665 666 667 668

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p, tid 0x%x, %u,%u,%u,%u,%u,%u,%u.\n",
		csk, csk->tid, 0, csk->tx_chan, csk->rss_qid,
669
		csk->snd_nxt, csk->rcv_nxt, csk->snd_win,
670 671 672
		csk->advmss);

	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
673 674

	return flowclen16;
675 676
}

677 678
static void
cxgb4i_make_tx_iso_cpl(struct sk_buff *skb, struct cpl_tx_data_iso *cpl)
679
{
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
	struct cxgbi_iso_info *info = (struct cxgbi_iso_info *)skb->head;
	u32 imm_en = !!(info->flags & CXGBI_ISO_INFO_IMM_ENABLE);
	u32 fslice = !!(info->flags & CXGBI_ISO_INFO_FSLICE);
	u32 lslice = !!(info->flags & CXGBI_ISO_INFO_LSLICE);
	u32 pdu_type = (info->op == ISCSI_OP_SCSI_CMD) ? 0 : 1;
	u32 submode = cxgbi_skcb_tx_ulp_mode(skb) & 0x3;

	cpl->op_to_scsi = cpu_to_be32(CPL_TX_DATA_ISO_OP_V(CPL_TX_DATA_ISO) |
				CPL_TX_DATA_ISO_FIRST_V(fslice) |
				CPL_TX_DATA_ISO_LAST_V(lslice) |
				CPL_TX_DATA_ISO_CPLHDRLEN_V(0) |
				CPL_TX_DATA_ISO_HDRCRC_V(submode & 1) |
				CPL_TX_DATA_ISO_PLDCRC_V(((submode >> 1) & 1)) |
				CPL_TX_DATA_ISO_IMMEDIATE_V(imm_en) |
				CPL_TX_DATA_ISO_SCSI_V(pdu_type));

	cpl->ahs_len = info->ahs;
	cpl->mpdu = cpu_to_be16(DIV_ROUND_UP(info->mpdu, 4));
	cpl->burst_size = cpu_to_be32(info->burst_size);
	cpl->len = cpu_to_be32(info->len);
	cpl->reserved2_seglen_offset =
	     cpu_to_be32(CPL_TX_DATA_ISO_SEGLEN_OFFSET_V(info->segment_offset));
	cpl->datasn_offset = cpu_to_be32(info->datasn_offset);
	cpl->buffer_offset = cpu_to_be32(info->buffer_offset);
	cpl->reserved3 = cpu_to_be32(0);
	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
		  "iso: flags 0x%x, op %u, ahs %u, num_pdu %u, mpdu %u, "
		  "burst_size %u, iso_len %u\n",
		  info->flags, info->op, info->ahs, info->num_pdu,
		  info->mpdu, info->burst_size << 2, info->len);
}

static void
cxgb4i_make_tx_data_wr(struct cxgbi_sock *csk, struct sk_buff *skb, int dlen,
		       int len, u32 credits, int compl)
{
	struct cxgbi_device *cdev = csk->cdev;
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
718
	struct fw_ofld_tx_data_wr *req;
719 720 721 722 723 724 725 726 727 728 729 730 731 732
	struct cpl_tx_data_iso *cpl;
	u32 submode = cxgbi_skcb_tx_ulp_mode(skb) & 0x3;
	u32 wr_ulp_mode = 0;
	u32 hdr_size = sizeof(*req);
	u32 opcode = FW_OFLD_TX_DATA_WR;
	u32 immlen = 0;
	u32 force = is_t5(lldi->adapter_type) ? TX_FORCE_V(!submode) :
						T6_TX_FORCE_F;

	if (cxgbi_skcb_test_flag(skb, SKCBF_TX_ISO)) {
		hdr_size += sizeof(struct cpl_tx_data_iso);
		opcode = FW_ISCSI_TX_DATA_WR;
		immlen += sizeof(struct cpl_tx_data_iso);
		submode |= 8;
733
	}
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

	if (is_ofld_imm(skb))
		immlen += dlen;

	req = (struct fw_ofld_tx_data_wr *)__skb_push(skb, hdr_size);
	req->op_to_immdlen = cpu_to_be32(FW_WR_OP_V(opcode) |
					 FW_WR_COMPL_V(compl) |
					 FW_WR_IMMDLEN_V(immlen));
	req->flowid_len16 = cpu_to_be32(FW_WR_FLOWID_V(csk->tid) |
					FW_WR_LEN16_V(credits));
	req->plen = cpu_to_be32(len);
	cpl =  (struct cpl_tx_data_iso *)(req + 1);

	if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_ISO)))
		cxgb4i_make_tx_iso_cpl(skb, cpl);

750
	if (submode)
751
		wr_ulp_mode = FW_OFLD_TX_DATA_WR_ULPMODE_V(ULP2_MODE_ISCSI) |
752 753 754 755 756
			      FW_OFLD_TX_DATA_WR_ULPSUBMODE_V(submode);

	req->tunnel_to_proxy = cpu_to_be32(wr_ulp_mode | force |
					   FW_OFLD_TX_DATA_WR_SHOVE_V(1U));

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT))
		cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
}

static void arp_failure_skb_discard(void *handle, struct sk_buff *skb)
{
	kfree_skb(skb);
}

static int push_tx_frames(struct cxgbi_sock *csk, int req_completion)
{
	int total_size = 0;
	struct sk_buff *skb;

	if (unlikely(csk->state < CTP_ESTABLISHED ||
		csk->state == CTP_CLOSE_WAIT_1 || csk->state >= CTP_ABORTING)) {
		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK |
774 775 776
			  1 << CXGBI_DBG_PDU_TX,
			  "csk 0x%p,%u,0x%lx,%u, in closing state.\n",
			  csk, csk->state, csk->flags, csk->tid);
777 778 779
		return 0;
	}

780 781 782 783 784 785 786 787 788 789 790
	while (csk->wr_cred && ((skb = skb_peek(&csk->write_queue)) != NULL)) {
		struct cxgbi_iso_info *iso_cpl;
		u32 dlen = skb->len;
		u32 len = skb->len;
		u32 iso_cpl_len = 0;
		u32 flowclen16 = 0;
		u32 credits_needed;
		u32 num_pdu = 1, hdr_len;

		if (cxgbi_skcb_test_flag(skb, SKCBF_TX_ISO))
			iso_cpl_len = sizeof(struct cpl_tx_data_iso);
791 792

		if (is_ofld_imm(skb))
793
			credits_needed = DIV_ROUND_UP(dlen + iso_cpl_len, 16);
794
		else
795 796 797
			credits_needed =
				DIV_ROUND_UP((8 * calc_tx_flits_ofld(skb)) +
					     iso_cpl_len, 16);
798 799

		if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_NEED_HDR)))
800 801
			credits_needed +=
			   DIV_ROUND_UP(sizeof(struct fw_ofld_tx_data_wr), 16);
802

803 804 805 806 807 808 809 810 811 812 813
		/*
		 * Assumes the initial credits is large enough to support
		 * fw_flowc_wr plus largest possible first payload
		 */
		if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
			flowclen16 = send_tx_flowc_wr(csk);
			csk->wr_cred -= flowclen16;
			csk->wr_una_cred += flowclen16;
			cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
		}

814 815
		if (csk->wr_cred < credits_needed) {
			log_debug(1 << CXGBI_DBG_PDU_TX,
816 817 818 819 820
				  "csk 0x%p, skb %u/%u, wr %d < %u.\n",
				  csk, skb->len, skb->data_len,
				  credits_needed, csk->wr_cred);

			csk->no_tx_credits++;
821 822
			break;
		}
823 824 825

		csk->no_tx_credits = 0;

826
		__skb_unlink(skb, &csk->write_queue);
827
		set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
828
		skb->csum = (__force __wsum)(credits_needed + flowclen16);
829 830 831 832 833 834 835 836 837
		csk->wr_cred -= credits_needed;
		csk->wr_una_cred += credits_needed;
		cxgbi_sock_enqueue_wr(csk, skb);

		log_debug(1 << CXGBI_DBG_PDU_TX,
			"csk 0x%p, skb %u/%u, wr %d, left %u, unack %u.\n",
			csk, skb->len, skb->data_len, credits_needed,
			csk->wr_cred, csk->wr_una_cred);

838 839 840 841 842
		if (!req_completion &&
		    ((csk->wr_una_cred >= (csk->wr_max_cred / 2)) ||
		     after(csk->write_seq, (csk->snd_una + csk->snd_win / 2))))
			req_completion = 1;

843
		if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_NEED_HDR))) {
844 845 846 847 848 849 850 851 852 853 854 855 856 857
			u32 ulp_mode = cxgbi_skcb_tx_ulp_mode(skb);

			if (cxgbi_skcb_test_flag(skb, SKCBF_TX_ISO)) {
				iso_cpl = (struct cxgbi_iso_info *)skb->head;
				num_pdu = iso_cpl->num_pdu;
				hdr_len = cxgbi_skcb_tx_iscsi_hdrlen(skb);
				len += (cxgbi_ulp_extra_len(ulp_mode) * num_pdu) +
				       (hdr_len * (num_pdu - 1));
			} else {
				len += cxgbi_ulp_extra_len(ulp_mode);
			}

			cxgb4i_make_tx_data_wr(csk, skb, dlen, len,
					       credits_needed, req_completion);
858 859
			csk->snd_nxt += len;
			cxgbi_skcb_clear_flag(skb, SKCBF_TX_NEED_HDR);
860 861 862 863
		} else if (cxgbi_skcb_test_flag(skb, SKCBF_TX_FLAG_COMPL) &&
			   (csk->wr_una_cred >= (csk->wr_max_cred / 2))) {
			struct cpl_close_con_req *req =
				(struct cpl_close_con_req *)skb->data;
864 865

			req->wr.wr_hi |= cpu_to_be32(FW_WR_COMPL_F);
866
		}
867

868 869 870 871
		total_size += skb->truesize;
		t4_set_arp_err_handler(skb, csk, arp_failure_skb_discard);

		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_TX,
872 873
			  "csk 0x%p,%u,0x%lx,%u, skb 0x%p, %u.\n",
			  csk, csk->state, csk->flags, csk->tid, skb, len);
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
		cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
	}
	return total_size;
}

static inline void free_atid(struct cxgbi_sock *csk)
{
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);

	if (cxgbi_sock_flag(csk, CTPF_HAS_ATID)) {
		cxgb4_free_atid(lldi->tids, csk->atid);
		cxgbi_sock_clear_flag(csk, CTPF_HAS_ATID);
		cxgbi_sock_put(csk);
	}
}

static void do_act_establish(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_act_establish *req = (struct cpl_act_establish *)skb->data;
	unsigned short tcp_opt = ntohs(req->tcp_opt);
	unsigned int tid = GET_TID(req);
896
	unsigned int atid = TID_TID_G(ntohl(req->tos_atid));
897 898 899 900 901 902 903 904 905 906
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;
	u32 rcv_isn = be32_to_cpu(req->rcv_isn);

	csk = lookup_atid(t, atid);
	if (unlikely(!csk)) {
		pr_err("NO conn. for atid %u, cdev 0x%p.\n", atid, cdev);
		goto rel_skb;
	}

907 908 909 910 911 912
	if (csk->atid != atid) {
		pr_err("bad conn atid %u, csk 0x%p,%u,0x%lx,tid %u, atid %u.\n",
			atid, csk, csk->state, csk->flags, csk->tid, csk->atid);
		goto rel_skb;
	}

913 914 915 916
	pr_info_ipaddr("atid 0x%x, tid 0x%x, csk 0x%p,%u,0x%lx, isn %u.\n",
		       (&csk->saddr), (&csk->daddr),
		       atid, tid, csk, csk->state, csk->flags, rcv_isn);

917
	module_put(cdev->owner);
918 919 920

	cxgbi_sock_get(csk);
	csk->tid = tid;
921
	cxgb4_insert_tid(lldi->tids, csk, tid, csk->csk_family);
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	cxgbi_sock_set_flag(csk, CTPF_HAS_TID);

	free_atid(csk);

	spin_lock_bh(&csk->lock);
	if (unlikely(csk->state != CTP_ACTIVE_OPEN))
		pr_info("csk 0x%p,%u,0x%lx,%u, got EST.\n",
			csk, csk->state, csk->flags, csk->tid);

	if (csk->retry_timer.function) {
		del_timer(&csk->retry_timer);
		csk->retry_timer.function = NULL;
	}

	csk->copied_seq = csk->rcv_wup = csk->rcv_nxt = rcv_isn;
	/*
	 * Causes the first RX_DATA_ACK to supply any Rx credits we couldn't
	 * pass through opt0.
	 */
941 942
	if (csk->rcv_win > (RCV_BUFSIZ_MASK << 10))
		csk->rcv_wup -= csk->rcv_win - (RCV_BUFSIZ_MASK << 10);
943

944 945
	csk->advmss = lldi->mtus[TCPOPT_MSS_G(tcp_opt)] - 40;
	if (TCPOPT_TSTAMP_G(tcp_opt))
946 947 948 949 950 951
		csk->advmss -= 12;
	if (csk->advmss < 128)
		csk->advmss = 128;

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p, mss_idx %u, advmss %u.\n",
952
			csk, TCPOPT_MSS_G(tcp_opt), csk->advmss);
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

	cxgbi_sock_established(csk, ntohl(req->snd_isn), ntohs(req->tcp_opt));

	if (unlikely(cxgbi_sock_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED)))
		send_abort_req(csk);
	else {
		if (skb_queue_len(&csk->write_queue))
			push_tx_frames(csk, 0);
		cxgbi_conn_tx_open(csk);
	}
	spin_unlock_bh(&csk->lock);

rel_skb:
	__kfree_skb(skb);
}

static int act_open_rpl_status_to_errno(int status)
{
	switch (status) {
	case CPL_ERR_CONN_RESET:
		return -ECONNREFUSED;
	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;
	}
}

987
static void csk_act_open_retry_timer(struct timer_list *t)
988
{
989
	struct sk_buff *skb = NULL;
990
	struct cxgbi_sock *csk = from_timer(csk, t, retry_timer);
991
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
992 993 994
	void (*send_act_open_func)(struct cxgbi_sock *, struct sk_buff *,
				   struct l2t_entry *);
	int t4 = is_t4(lldi->adapter_type), size, size6;
995 996 997 998 999 1000 1001

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p,%u,0x%lx,%u.\n",
		csk, csk->state, csk->flags, csk->tid);

	cxgbi_sock_get(csk);
	spin_lock_bh(&csk->lock);
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

	if (t4) {
		size = sizeof(struct cpl_act_open_req);
		size6 = sizeof(struct cpl_act_open_req6);
	} else {
		size = sizeof(struct cpl_t5_act_open_req);
		size6 = sizeof(struct cpl_t5_act_open_req6);
	}

	if (csk->csk_family == AF_INET) {
		send_act_open_func = send_act_open_req;
		skb = alloc_wr(size, 0, GFP_ATOMIC);
1014
#if IS_ENABLED(CONFIG_IPV6)
1015 1016 1017
	} else {
		send_act_open_func = send_act_open_req6;
		skb = alloc_wr(size6, 0, GFP_ATOMIC);
1018
#endif
1019 1020
	}

1021 1022 1023 1024 1025
	if (!skb)
		cxgbi_sock_fail_act_open(csk, -ENOMEM);
	else {
		skb->sk = (struct sock *)csk;
		t4_set_arp_err_handler(skb, csk,
1026 1027
				       cxgbi_sock_act_open_req_arp_failure);
		send_act_open_func(csk, skb, csk->l2t);
1028
	}
1029

1030 1031
	spin_unlock_bh(&csk->lock);
	cxgbi_sock_put(csk);
1032

1033 1034
}

1035 1036 1037 1038 1039 1040 1041
static inline bool is_neg_adv(unsigned int status)
{
	return status == CPL_ERR_RTX_NEG_ADVICE ||
		status == CPL_ERR_KEEPALV_NEG_ADVICE ||
		status == CPL_ERR_PERSIST_NEG_ADVICE;
}

1042 1043 1044 1045 1046 1047
static void do_act_open_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_act_open_rpl *rpl = (struct cpl_act_open_rpl *)skb->data;
	unsigned int tid = GET_TID(rpl);
	unsigned int atid =
1048 1049
		TID_TID_G(AOPEN_ATID_G(be32_to_cpu(rpl->atid_status)));
	unsigned int status = AOPEN_STATUS_G(be32_to_cpu(rpl->atid_status));
1050 1051 1052 1053 1054 1055 1056 1057 1058
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;

	csk = lookup_atid(t, atid);
	if (unlikely(!csk)) {
		pr_err("NO matching conn. atid %u, tid %u.\n", atid, tid);
		goto rel_skb;
	}

1059 1060 1061
	pr_info_ipaddr("tid %u/%u, status %u.\n"
		       "csk 0x%p,%u,0x%lx. ", (&csk->saddr), (&csk->daddr),
		       atid, tid, status, csk, csk->state, csk->flags);
1062

1063
	if (is_neg_adv(status))
1064 1065
		goto rel_skb;

1066
	module_put(cdev->owner);
1067

1068 1069 1070
	if (status && status != CPL_ERR_TCAM_FULL &&
	    status != CPL_ERR_CONN_EXIST &&
	    status != CPL_ERR_ARP_MISS)
1071 1072
		cxgb4_remove_tid(lldi->tids, csk->port_id, GET_TID(rpl),
				 csk->csk_family);
1073 1074 1075 1076 1077

	cxgbi_sock_get(csk);
	spin_lock_bh(&csk->lock);

	if (status == CPL_ERR_CONN_EXIST &&
1078 1079
	    csk->retry_timer.function != csk_act_open_retry_timer) {
		csk->retry_timer.function = csk_act_open_retry_timer;
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
		mod_timer(&csk->retry_timer, jiffies + HZ / 2);
	} else
		cxgbi_sock_fail_act_open(csk,
					act_open_rpl_status_to_errno(status));

	spin_unlock_bh(&csk->lock);
	cxgbi_sock_put(csk);
rel_skb:
	__kfree_skb(skb);
}

static void do_peer_close(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_peer_close *req = (struct cpl_peer_close *)skb->data;
	unsigned int tid = GET_TID(req);
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;

	csk = lookup_tid(t, tid);
	if (unlikely(!csk)) {
		pr_err("can't find connection for tid %u.\n", tid);
		goto rel_skb;
	}
1104 1105 1106
	pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u.\n",
		       (&csk->saddr), (&csk->daddr),
		       csk, csk->state, csk->flags, csk->tid);
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	cxgbi_sock_rcv_peer_close(csk);
rel_skb:
	__kfree_skb(skb);
}

static void do_close_con_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_close_con_rpl *rpl = (struct cpl_close_con_rpl *)skb->data;
	unsigned int tid = GET_TID(rpl);
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;

	csk = lookup_tid(t, tid);
	if (unlikely(!csk)) {
		pr_err("can't find connection for tid %u.\n", tid);
		goto rel_skb;
	}
1125 1126 1127
	pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u.\n",
		       (&csk->saddr), (&csk->daddr),
		       csk, csk->state, csk->flags, csk->tid);
1128 1129 1130 1131 1132 1133 1134 1135 1136
	cxgbi_sock_rcv_close_conn_rpl(csk, ntohl(rpl->snd_nxt));
rel_skb:
	__kfree_skb(skb);
}

static int abort_status_to_errno(struct cxgbi_sock *csk, int abort_reason,
								int *need_rst)
{
	switch (abort_reason) {
1137
	case CPL_ERR_BAD_SYN:
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
	case CPL_ERR_CONN_RESET:
		return csk->state > CTP_ESTABLISHED ?
			-EPIPE : -ECONNRESET;
	case CPL_ERR_XMIT_TIMEDOUT:
	case CPL_ERR_PERSIST_TIMEDOUT:
	case CPL_ERR_FINWAIT2_TIMEDOUT:
	case CPL_ERR_KEEPALIVE_TIMEDOUT:
		return -ETIMEDOUT;
	default:
		return -EIO;
	}
}

static void do_abort_req_rss(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_abort_req_rss *req = (struct cpl_abort_req_rss *)skb->data;
	unsigned int tid = GET_TID(req);
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;
	int rst_status = CPL_ABORT_NO_RST;

	csk = lookup_tid(t, tid);
	if (unlikely(!csk)) {
		pr_err("can't find connection for tid %u.\n", tid);
		goto rel_skb;
	}

1166 1167 1168
	pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u, status %u.\n",
		       (&csk->saddr), (&csk->daddr),
		       csk, csk->state, csk->flags, csk->tid, req->status);
1169

1170
	if (is_neg_adv(req->status))
1171 1172 1173 1174 1175
		goto rel_skb;

	cxgbi_sock_get(csk);
	spin_lock_bh(&csk->lock);

A
Anish Bhatt 已提交
1176 1177 1178 1179 1180
	cxgbi_sock_clear_flag(csk, CTPF_ABORT_REQ_RCVD);

	if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
		send_tx_flowc_wr(csk);
		cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
1181 1182
	}

A
Anish Bhatt 已提交
1183 1184 1185
	cxgbi_sock_set_flag(csk, CTPF_ABORT_REQ_RCVD);
	cxgbi_sock_set_state(csk, CTP_ABORTING);

1186 1187 1188 1189 1190 1191
	send_abort_rpl(csk, rst_status);

	if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
		csk->err = abort_status_to_errno(csk, req->status, &rst_status);
		cxgbi_sock_closed(csk);
	}
A
Anish Bhatt 已提交
1192

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	spin_unlock_bh(&csk->lock);
	cxgbi_sock_put(csk);
rel_skb:
	__kfree_skb(skb);
}

static void do_abort_rpl_rss(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_abort_rpl_rss *rpl = (struct cpl_abort_rpl_rss *)skb->data;
	unsigned int tid = GET_TID(rpl);
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;

	csk = lookup_tid(t, tid);
	if (!csk)
		goto rel_skb;

1211 1212 1213
	pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u, status %u.\n",
		       (&csk->saddr), (&csk->daddr), csk,
		       csk->state, csk->flags, csk->tid, rpl->status);
1214 1215 1216 1217 1218 1219 1220 1221 1222

	if (rpl->status == CPL_ERR_ABORT_FAILED)
		goto rel_skb;

	cxgbi_sock_rcv_abort_rpl(csk);
rel_skb:
	__kfree_skb(skb);
}

K
Karen Xie 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
static void do_rx_data(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_rx_data *cpl = (struct cpl_rx_data *)skb->data;
	unsigned int tid = GET_TID(cpl);
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;

	csk = lookup_tid(t, tid);
	if (!csk) {
		pr_err("can't find connection for tid %u.\n", tid);
	} else {
		/* not expecting this, reset the connection. */
		pr_err("csk 0x%p, tid %u, rcv cpl_rx_data.\n", csk, tid);
		spin_lock_bh(&csk->lock);
		send_abort_req(csk);
		spin_unlock_bh(&csk->lock);
	}
	__kfree_skb(skb);
}

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
static void do_rx_iscsi_hdr(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_iscsi_hdr *cpl = (struct cpl_iscsi_hdr *)skb->data;
	unsigned short pdu_len_ddp = be16_to_cpu(cpl->pdu_len_ddp);
	unsigned int tid = GET_TID(cpl);
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;

	csk = lookup_tid(t, tid);
	if (unlikely(!csk)) {
		pr_err("can't find conn. for tid %u.\n", tid);
		goto rel_skb;
	}

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
		"csk 0x%p,%u,0x%lx, tid %u, skb 0x%p,%u, 0x%x.\n",
		csk, csk->state, csk->flags, csk->tid, skb, skb->len,
		pdu_len_ddp);

	spin_lock_bh(&csk->lock);

	if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
			"csk 0x%p,%u,0x%lx,%u, bad state.\n",
			csk, csk->state, csk->flags, csk->tid);
		if (csk->state != CTP_ABORTING)
			goto abort_conn;
		else
			goto discard;
	}

	cxgbi_skcb_tcp_seq(skb) = ntohl(cpl->seq);
1277 1278
	cxgbi_skcb_flags(skb) = 0;

1279 1280 1281 1282 1283 1284
	skb_reset_transport_header(skb);
	__skb_pull(skb, sizeof(*cpl));
	__pskb_trim(skb, ntohs(cpl->len));

	if (!csk->skb_ulp_lhdr) {
		unsigned char *bhs;
1285
		unsigned int hlen, dlen, plen;
1286 1287 1288 1289 1290

		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
			"csk 0x%p,%u,0x%lx, tid %u, skb 0x%p header.\n",
			csk, csk->state, csk->flags, csk->tid, skb);
		csk->skb_ulp_lhdr = skb;
1291
		cxgbi_skcb_set_flag(skb, SKCBF_RX_HDR);
1292

1293 1294
		if ((CHELSIO_CHIP_VERSION(lldi->adapter_type) <= CHELSIO_T5) &&
		    (cxgbi_skcb_tcp_seq(skb) != csk->rcv_nxt)) {
1295
			pr_info("tid %u, CPL_ISCSI_HDR, bad seq, 0x%x/0x%x.\n",
1296
				csk->tid, cxgbi_skcb_tcp_seq(skb),
1297 1298 1299 1300
				csk->rcv_nxt);
			goto abort_conn;
		}

1301
		bhs = skb->data;
1302 1303 1304
		hlen = ntohs(cpl->len);
		dlen = ntohl(*(unsigned int *)(bhs + 4)) & 0xFFFFFF;

1305
		plen = ISCSI_PDU_LEN_G(pdu_len_ddp);
1306 1307 1308 1309
		if (is_t4(lldi->adapter_type))
			plen -= 40;

		if ((hlen + dlen) != plen) {
1310 1311
			pr_info("tid 0x%x, CPL_ISCSI_HDR, pdu len "
				"mismatch %u != %u + %u, seq 0x%x.\n",
1312 1313
				csk->tid, plen, hlen, dlen,
				cxgbi_skcb_tcp_seq(skb));
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
			goto abort_conn;
		}

		cxgbi_skcb_rx_pdulen(skb) = (hlen + dlen + 3) & (~0x3);
		if (dlen)
			cxgbi_skcb_rx_pdulen(skb) += csk->dcrc_len;
		csk->rcv_nxt += cxgbi_skcb_rx_pdulen(skb);

		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
			"csk 0x%p, skb 0x%p, 0x%x,%u+%u,0x%x,0x%x.\n",
			csk, skb, *bhs, hlen, dlen,
			ntohl(*((unsigned int *)(bhs + 16))),
			ntohl(*((unsigned int *)(bhs + 24))));

	} else {
1329
		struct sk_buff *lskb = csk->skb_ulp_lhdr;
1330

1331
		cxgbi_skcb_set_flag(lskb, SKCBF_RX_DATA);
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
			"csk 0x%p,%u,0x%lx, skb 0x%p data, 0x%p.\n",
			csk, csk->state, csk->flags, skb, lskb);
	}

	__skb_queue_tail(&csk->receive_queue, skb);
	spin_unlock_bh(&csk->lock);
	return;

abort_conn:
	send_abort_req(csk);
discard:
	spin_unlock_bh(&csk->lock);
rel_skb:
	__kfree_skb(skb);
}

1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
static void do_rx_iscsi_data(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_iscsi_hdr *cpl = (struct cpl_iscsi_hdr *)skb->data;
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;
	struct sk_buff *lskb;
	u32 tid = GET_TID(cpl);
	u16 pdu_len_ddp = be16_to_cpu(cpl->pdu_len_ddp);

	csk = lookup_tid(t, tid);
	if (unlikely(!csk)) {
		pr_err("can't find conn. for tid %u.\n", tid);
		goto rel_skb;
	}

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
		  "csk 0x%p,%u,0x%lx, tid %u, skb 0x%p,%u, 0x%x.\n",
		  csk, csk->state, csk->flags, csk->tid, skb,
		  skb->len, pdu_len_ddp);

	spin_lock_bh(&csk->lock);

	if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
			  "csk 0x%p,%u,0x%lx,%u, bad state.\n",
			  csk, csk->state, csk->flags, csk->tid);

		if (csk->state != CTP_ABORTING)
			goto abort_conn;
		else
			goto discard;
	}

	cxgbi_skcb_tcp_seq(skb) = be32_to_cpu(cpl->seq);
	cxgbi_skcb_flags(skb) = 0;

	skb_reset_transport_header(skb);
	__skb_pull(skb, sizeof(*cpl));
	__pskb_trim(skb, ntohs(cpl->len));

	if (!csk->skb_ulp_lhdr)
		csk->skb_ulp_lhdr = skb;

	lskb = csk->skb_ulp_lhdr;
	cxgbi_skcb_set_flag(lskb, SKCBF_RX_DATA);

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
		  "csk 0x%p,%u,0x%lx, skb 0x%p data, 0x%p.\n",
		  csk, csk->state, csk->flags, skb, lskb);

	__skb_queue_tail(&csk->receive_queue, skb);
	spin_unlock_bh(&csk->lock);
	return;

abort_conn:
	send_abort_req(csk);
discard:
	spin_unlock_bh(&csk->lock);
rel_skb:
	__kfree_skb(skb);
}

static void
cxgb4i_process_ddpvld(struct cxgbi_sock *csk,
		      struct sk_buff *skb, u32 ddpvld)
{
	if (ddpvld & (1 << CPL_RX_DDP_STATUS_HCRC_SHIFT)) {
		pr_info("csk 0x%p, lhdr 0x%p, status 0x%x, hcrc bad 0x%lx.\n",
			csk, skb, ddpvld, cxgbi_skcb_flags(skb));
		cxgbi_skcb_set_flag(skb, SKCBF_RX_HCRC_ERR);
	}

	if (ddpvld & (1 << CPL_RX_DDP_STATUS_DCRC_SHIFT)) {
		pr_info("csk 0x%p, lhdr 0x%p, status 0x%x, dcrc bad 0x%lx.\n",
			csk, skb, ddpvld, cxgbi_skcb_flags(skb));
		cxgbi_skcb_set_flag(skb, SKCBF_RX_DCRC_ERR);
	}

	if (ddpvld & (1 << CPL_RX_DDP_STATUS_PAD_SHIFT)) {
		log_debug(1 << CXGBI_DBG_PDU_RX,
			  "csk 0x%p, lhdr 0x%p, status 0x%x, pad bad.\n",
			  csk, skb, ddpvld);
		cxgbi_skcb_set_flag(skb, SKCBF_RX_PAD_ERR);
	}

	if ((ddpvld & (1 << CPL_RX_DDP_STATUS_DDP_SHIFT)) &&
	    !cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
		log_debug(1 << CXGBI_DBG_PDU_RX,
			  "csk 0x%p, lhdr 0x%p, 0x%x, data ddp'ed.\n",
			  csk, skb, ddpvld);
		cxgbi_skcb_set_flag(skb, SKCBF_RX_DATA_DDPD);
	}
}

1444 1445 1446 1447 1448 1449 1450 1451 1452
static void do_rx_data_ddp(struct cxgbi_device *cdev,
				  struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct sk_buff *lskb;
	struct cpl_rx_data_ddp *rpl = (struct cpl_rx_data_ddp *)skb->data;
	unsigned int tid = GET_TID(rpl);
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;
1453
	u32 ddpvld = be32_to_cpu(rpl->ddpvld);
1454 1455 1456 1457 1458 1459 1460 1461 1462

	csk = lookup_tid(t, tid);
	if (unlikely(!csk)) {
		pr_err("can't find connection for tid %u.\n", tid);
		goto rel_skb;
	}

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
		"csk 0x%p,%u,0x%lx, skb 0x%p,0x%x, lhdr 0x%p.\n",
1463
		csk, csk->state, csk->flags, skb, ddpvld, csk->skb_ulp_lhdr);
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

	spin_lock_bh(&csk->lock);

	if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
			"csk 0x%p,%u,0x%lx,%u, bad state.\n",
			csk, csk->state, csk->flags, csk->tid);
		if (csk->state != CTP_ABORTING)
			goto abort_conn;
		else
			goto discard;
	}

	if (!csk->skb_ulp_lhdr) {
		pr_err("tid 0x%x, rcv RX_DATA_DDP w/o pdu bhs.\n", csk->tid);
		goto abort_conn;
	}

	lskb = csk->skb_ulp_lhdr;
	csk->skb_ulp_lhdr = NULL;

	cxgbi_skcb_rx_ddigest(lskb) = ntohl(rpl->ulp_crc);

	if (ntohs(rpl->len) != cxgbi_skcb_rx_pdulen(lskb))
		pr_info("tid 0x%x, RX_DATA_DDP pdulen %u != %u.\n",
			csk->tid, ntohs(rpl->len), cxgbi_skcb_rx_pdulen(lskb));

1491 1492
	cxgb4i_process_ddpvld(csk, lskb, ddpvld);

1493 1494 1495 1496
	log_debug(1 << CXGBI_DBG_PDU_RX,
		"csk 0x%p, lskb 0x%p, f 0x%lx.\n",
		csk, lskb, cxgbi_skcb_flags(lskb));

1497
	cxgbi_skcb_set_flag(lskb, SKCBF_RX_STATUS);
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
	cxgbi_conn_pdu_ready(csk);
	spin_unlock_bh(&csk->lock);
	goto rel_skb;

abort_conn:
	send_abort_req(csk);
discard:
	spin_unlock_bh(&csk->lock);
rel_skb:
	__kfree_skb(skb);
}

1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
static void
do_rx_iscsi_cmp(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_rx_iscsi_cmp *rpl = (struct cpl_rx_iscsi_cmp *)skb->data;
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;
	struct sk_buff *data_skb = NULL;
	u32 tid = GET_TID(rpl);
	u32 ddpvld = be32_to_cpu(rpl->ddpvld);
	u32 seq = be32_to_cpu(rpl->seq);
	u16 pdu_len_ddp = be16_to_cpu(rpl->pdu_len_ddp);

	csk = lookup_tid(t, tid);
	if (unlikely(!csk)) {
		pr_err("can't find connection for tid %u.\n", tid);
		goto rel_skb;
	}

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
		  "csk 0x%p,%u,0x%lx, skb 0x%p,0x%x, lhdr 0x%p, len %u, "
		  "pdu_len_ddp %u, status %u.\n",
		  csk, csk->state, csk->flags, skb, ddpvld, csk->skb_ulp_lhdr,
		  ntohs(rpl->len), pdu_len_ddp,  rpl->status);

	spin_lock_bh(&csk->lock);

	if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
			  "csk 0x%p,%u,0x%lx,%u, bad state.\n",
			  csk, csk->state, csk->flags, csk->tid);

		if (csk->state != CTP_ABORTING)
			goto abort_conn;
		else
			goto discard;
	}

	cxgbi_skcb_tcp_seq(skb) = seq;
	cxgbi_skcb_flags(skb) = 0;
	cxgbi_skcb_rx_pdulen(skb) = 0;

	skb_reset_transport_header(skb);
	__skb_pull(skb, sizeof(*rpl));
	__pskb_trim(skb, be16_to_cpu(rpl->len));

	csk->rcv_nxt = seq + pdu_len_ddp;

	if (csk->skb_ulp_lhdr) {
		data_skb = skb_peek(&csk->receive_queue);
		if (!data_skb ||
		    !cxgbi_skcb_test_flag(data_skb, SKCBF_RX_DATA)) {
			pr_err("Error! freelist data not found 0x%p, tid %u\n",
			       data_skb, tid);

			goto abort_conn;
		}
		__skb_unlink(data_skb, &csk->receive_queue);

		cxgbi_skcb_set_flag(skb, SKCBF_RX_DATA);

		__skb_queue_tail(&csk->receive_queue, skb);
		__skb_queue_tail(&csk->receive_queue, data_skb);
	} else {
		 __skb_queue_tail(&csk->receive_queue, skb);
	}

	csk->skb_ulp_lhdr = NULL;

	cxgbi_skcb_set_flag(skb, SKCBF_RX_HDR);
	cxgbi_skcb_set_flag(skb, SKCBF_RX_STATUS);
	cxgbi_skcb_set_flag(skb, SKCBF_RX_ISCSI_COMPL);
	cxgbi_skcb_rx_ddigest(skb) = be32_to_cpu(rpl->ulp_crc);

	cxgb4i_process_ddpvld(csk, skb, ddpvld);

	log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, skb 0x%p, f 0x%lx.\n",
		  csk, skb, cxgbi_skcb_flags(skb));

	cxgbi_conn_pdu_ready(csk);
	spin_unlock_bh(&csk->lock);

	return;

abort_conn:
	send_abort_req(csk);
discard:
	spin_unlock_bh(&csk->lock);
rel_skb:
	__kfree_skb(skb);
}

1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
static void do_fw4_ack(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cxgbi_sock *csk;
	struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)skb->data;
	unsigned int tid = GET_TID(rpl);
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;

	csk = lookup_tid(t, tid);
	if (unlikely(!csk))
		pr_err("can't find connection for tid %u.\n", tid);
	else {
		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
			"csk 0x%p,%u,0x%lx,%u.\n",
			csk, csk->state, csk->flags, csk->tid);
		cxgbi_sock_rcv_wr_ack(csk, rpl->credits, ntohl(rpl->snd_una),
					rpl->seq_vld);
	}
	__kfree_skb(skb);
}

static void do_set_tcb_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
{
	struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
	unsigned int tid = GET_TID(rpl);
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct tid_info *t = lldi->tids;
	struct cxgbi_sock *csk;

	csk = lookup_tid(t, tid);
1632
	if (!csk) {
1633
		pr_err("can't find conn. for tid %u.\n", tid);
1634 1635
		return;
	}
1636 1637 1638 1639 1640

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p,%u,%lx,%u, status 0x%x.\n",
		csk, csk->state, csk->flags, csk->tid, rpl->status);

1641
	if (rpl->status != CPL_ERR_NONE) {
1642 1643
		pr_err("csk 0x%p,%u, SET_TCB_RPL status %u.\n",
			csk, tid, rpl->status);
1644 1645 1646 1647
		csk->err = -EINVAL;
	}

	complete(&csk->cmpl);
1648 1649 1650 1651 1652 1653

	__kfree_skb(skb);
}

static int alloc_cpls(struct cxgbi_sock *csk)
{
1654 1655
	csk->cpl_close = alloc_wr(sizeof(struct cpl_close_con_req),
					0, GFP_KERNEL);
1656 1657 1658
	if (!csk->cpl_close)
		return -ENOMEM;

1659 1660
	csk->cpl_abort_req = alloc_wr(sizeof(struct cpl_abort_req),
					0, GFP_KERNEL);
1661 1662 1663
	if (!csk->cpl_abort_req)
		goto free_cpls;

1664 1665
	csk->cpl_abort_rpl = alloc_wr(sizeof(struct cpl_abort_rpl),
					0, GFP_KERNEL);
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
	if (!csk->cpl_abort_rpl)
		goto free_cpls;
	return 0;

free_cpls:
	cxgbi_sock_free_cpl_skbs(csk);
	return -ENOMEM;
}

static inline void l2t_put(struct cxgbi_sock *csk)
{
	if (csk->l2t) {
		cxgb4_l2t_release(csk->l2t);
		csk->l2t = NULL;
		cxgbi_sock_put(csk);
	}
}

static void release_offload_resources(struct cxgbi_sock *csk)
{
	struct cxgb4_lld_info *lldi;
1687 1688 1689
#if IS_ENABLED(CONFIG_IPV6)
	struct net_device *ndev = csk->cdev->ports[csk->port_id];
#endif
1690 1691 1692 1693 1694 1695

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p,%u,0x%lx,%u.\n",
		csk, csk->state, csk->flags, csk->tid);

	cxgbi_sock_free_cpl_skbs(csk);
V
Varun Prakash 已提交
1696
	cxgbi_sock_purge_write_queue(csk);
1697 1698 1699 1700 1701 1702
	if (csk->wr_cred != csk->wr_max_cred) {
		cxgbi_sock_purge_wr_queue(csk);
		cxgbi_sock_reset_wr_list(csk);
	}

	l2t_put(csk);
1703 1704 1705 1706 1707 1708
#if IS_ENABLED(CONFIG_IPV6)
	if (csk->csk_family == AF_INET6)
		cxgb4_clip_release(ndev,
				   (const u32 *)&csk->saddr6.sin6_addr, 1);
#endif

1709 1710 1711 1712
	if (cxgbi_sock_flag(csk, CTPF_HAS_ATID))
		free_atid(csk);
	else if (cxgbi_sock_flag(csk, CTPF_HAS_TID)) {
		lldi = cxgbi_cdev_priv(csk->cdev);
1713 1714
		cxgb4_remove_tid(lldi->tids, 0, csk->tid,
				 csk->csk_family);
1715 1716 1717 1718 1719 1720
		cxgbi_sock_clear_flag(csk, CTPF_HAS_TID);
		cxgbi_sock_put(csk);
	}
	csk->dst = NULL;
}

1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
#ifdef CONFIG_CHELSIO_T4_DCB
static inline u8 get_iscsi_dcb_state(struct net_device *ndev)
{
	return ndev->dcbnl_ops->getstate(ndev);
}

static int select_priority(int pri_mask)
{
	if (!pri_mask)
		return 0;
	return (ffs(pri_mask) - 1);
}

static u8 get_iscsi_dcb_priority(struct net_device *ndev)
{
	int rv;
	u8 caps;

	struct dcb_app iscsi_dcb_app = {
		.protocol = 3260
	};

	rv = (int)ndev->dcbnl_ops->getcap(ndev, DCB_CAP_ATTR_DCBX, &caps);
	if (rv)
		return 0;

	if (caps & DCB_CAP_DCBX_VER_IEEE) {
1748
		iscsi_dcb_app.selector = IEEE_8021QAZ_APP_SEL_STREAM;
1749
		rv = dcb_ieee_getapp_mask(ndev, &iscsi_dcb_app);
1750 1751 1752 1753
		if (!rv) {
			iscsi_dcb_app.selector = IEEE_8021QAZ_APP_SEL_ANY;
			rv = dcb_ieee_getapp_mask(ndev, &iscsi_dcb_app);
		}
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
	} else if (caps & DCB_CAP_DCBX_VER_CEE) {
		iscsi_dcb_app.selector = DCB_APP_IDTYPE_PORTNUM;
		rv = dcb_getapp(ndev, &iscsi_dcb_app);
	}

	log_debug(1 << CXGBI_DBG_ISCSI,
		  "iSCSI priority is set to %u\n", select_priority(rv));
	return select_priority(rv);
}
#endif

1765 1766 1767 1768 1769 1770
static int init_act_open(struct cxgbi_sock *csk)
{
	struct cxgbi_device *cdev = csk->cdev;
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct net_device *ndev = cdev->ports[csk->port_id];
	struct sk_buff *skb = NULL;
1771 1772
	struct neighbour *n = NULL;
	void *daddr;
1773
	unsigned int step;
1774
	unsigned int rxq_idx;
1775
	unsigned int size, size6;
1776 1777
	unsigned int linkspeed;
	unsigned int rcv_winf, snd_winf;
1778 1779 1780
#ifdef CONFIG_CHELSIO_T4_DCB
	u8 priority = 0;
#endif
1781 1782 1783 1784
	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p,%u,0x%lx,%u.\n",
		csk, csk->state, csk->flags, csk->tid);

1785 1786
	if (csk->csk_family == AF_INET)
		daddr = &csk->daddr.sin_addr.s_addr;
1787 1788
#if IS_ENABLED(CONFIG_IPV6)
	else if (csk->csk_family == AF_INET6)
1789
		daddr = &csk->daddr6.sin6_addr;
1790 1791 1792 1793 1794
#endif
	else {
		pr_err("address family 0x%x not supported\n", csk->csk_family);
		goto rel_resource;
	}
1795 1796 1797 1798 1799 1800 1801 1802

	n = dst_neigh_lookup(csk->dst, daddr);

	if (!n) {
		pr_err("%s, can't get neighbour of csk->dst.\n", ndev->name);
		goto rel_resource;
	}

1803 1804 1805
	if (!(n->nud_state & NUD_VALID))
		neigh_event_send(n, NULL);

1806 1807 1808
	csk->atid = cxgb4_alloc_atid(lldi->tids, csk);
	if (csk->atid < 0) {
		pr_err("%s, NO atid available.\n", ndev->name);
1809
		goto rel_resource_without_clip;
1810 1811 1812 1813
	}
	cxgbi_sock_set_flag(csk, CTPF_HAS_ATID);
	cxgbi_sock_get(csk);

1814 1815 1816 1817 1818 1819 1820
#ifdef CONFIG_CHELSIO_T4_DCB
	if (get_iscsi_dcb_state(ndev))
		priority = get_iscsi_dcb_priority(ndev);

	csk->dcb_priority = priority;
	csk->l2t = cxgb4_l2t_get(lldi->l2t, n, ndev, priority);
#else
1821
	csk->l2t = cxgb4_l2t_get(lldi->l2t, n, ndev, 0);
1822
#endif
1823 1824
	if (!csk->l2t) {
		pr_err("%s, cannot alloc l2t.\n", ndev->name);
1825
		goto rel_resource_without_clip;
1826 1827 1828
	}
	cxgbi_sock_get(csk);

1829 1830 1831 1832 1833
#if IS_ENABLED(CONFIG_IPV6)
	if (csk->csk_family == AF_INET6)
		cxgb4_clip_get(ndev, (const u32 *)&csk->saddr6.sin6_addr, 1);
#endif

1834
	if (is_t4(lldi->adapter_type)) {
1835 1836
		size = sizeof(struct cpl_act_open_req);
		size6 = sizeof(struct cpl_act_open_req6);
1837
	} else if (is_t5(lldi->adapter_type)) {
1838 1839
		size = sizeof(struct cpl_t5_act_open_req);
		size6 = sizeof(struct cpl_t5_act_open_req6);
1840 1841 1842
	} else {
		size = sizeof(struct cpl_t6_act_open_req);
		size6 = sizeof(struct cpl_t6_act_open_req6);
1843 1844 1845 1846
	}

	if (csk->csk_family == AF_INET)
		skb = alloc_wr(size, 0, GFP_NOIO);
1847
#if IS_ENABLED(CONFIG_IPV6)
1848 1849
	else
		skb = alloc_wr(size6, 0, GFP_NOIO);
1850
#endif
1851

1852 1853 1854 1855 1856 1857 1858 1859 1860
	if (!skb)
		goto rel_resource;
	skb->sk = (struct sock *)csk;
	t4_set_arp_err_handler(skb, csk, cxgbi_sock_act_open_req_arp_failure);

	if (!csk->mtu)
		csk->mtu = dst_mtu(csk->dst);
	cxgb4_best_mtu(lldi->mtus, csk->mtu, &csk->mss_idx);
	csk->tx_chan = cxgb4_port_chan(ndev);
1861
	csk->smac_idx = ((struct port_info *)netdev_priv(ndev))->smt_idx;
1862 1863 1864
	step = lldi->ntxq / lldi->nchan;
	csk->txq_idx = cxgb4_port_idx(ndev) * step;
	step = lldi->nrxq / lldi->nchan;
1865 1866 1867
	rxq_idx = (cxgb4_port_idx(ndev) * step) + (cdev->rxq_idx_cntr % step);
	cdev->rxq_idx_cntr++;
	csk->rss_qid = lldi->rxq_ids[rxq_idx];
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
	linkspeed = ((struct port_info *)netdev_priv(ndev))->link_cfg.speed;
	csk->snd_win = cxgb4i_snd_win;
	csk->rcv_win = cxgb4i_rcv_win;
	if (cxgb4i_rcv_win <= 0) {
		csk->rcv_win = CXGB4I_DEFAULT_10G_RCV_WIN;
		rcv_winf = linkspeed / SPEED_10000;
		if (rcv_winf)
			csk->rcv_win *= rcv_winf;
	}
	if (cxgb4i_snd_win <= 0) {
		csk->snd_win = CXGB4I_DEFAULT_10G_SND_WIN;
		snd_winf = linkspeed / SPEED_10000;
		if (snd_winf)
			csk->snd_win *= snd_winf;
	}
1883 1884 1885
	csk->wr_cred = lldi->wr_cred -
		       DIV_ROUND_UP(sizeof(struct cpl_abort_req), 16);
	csk->wr_max_cred = csk->wr_cred;
1886 1887 1888 1889
	csk->wr_una_cred = 0;
	cxgbi_sock_reset_wr_list(csk);
	csk->err = 0;

1890 1891 1892 1893 1894 1895
	pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u,%u,%u, mtu %u,%u, smac %u.\n",
		       (&csk->saddr), (&csk->daddr), csk, csk->state,
		       csk->flags, csk->tx_chan, csk->txq_idx, csk->rss_qid,
		       csk->mtu, csk->mss_idx, csk->smac_idx);

	/* must wait for either a act_open_rpl or act_open_establish */
1896 1897 1898 1899 1900
	if (!try_module_get(cdev->owner)) {
		pr_err("%s, try_module_get failed.\n", ndev->name);
		goto rel_resource;
	}

1901
	cxgbi_sock_set_state(csk, CTP_ACTIVE_OPEN);
1902 1903
	if (csk->csk_family == AF_INET)
		send_act_open_req(csk, skb, csk->l2t);
1904
#if IS_ENABLED(CONFIG_IPV6)
1905 1906
	else
		send_act_open_req6(csk, skb, csk->l2t);
1907
#endif
1908
	neigh_release(n);
1909

1910 1911 1912
	return 0;

rel_resource:
1913 1914 1915 1916 1917 1918
#if IS_ENABLED(CONFIG_IPV6)
	if (csk->csk_family == AF_INET6)
		cxgb4_clip_release(ndev,
				   (const u32 *)&csk->saddr6.sin6_addr, 1);
#endif
rel_resource_without_clip:
1919 1920
	if (n)
		neigh_release(n);
1921 1922 1923 1924 1925
	if (skb)
		__kfree_skb(skb);
	return -EINVAL;
}

1926
static cxgb4i_cplhandler_func cxgb4i_cplhandlers[NUM_CPL_CMDS] = {
1927 1928 1929 1930 1931 1932 1933 1934
	[CPL_ACT_ESTABLISH] = do_act_establish,
	[CPL_ACT_OPEN_RPL] = do_act_open_rpl,
	[CPL_PEER_CLOSE] = do_peer_close,
	[CPL_ABORT_REQ_RSS] = do_abort_req_rss,
	[CPL_ABORT_RPL_RSS] = do_abort_rpl_rss,
	[CPL_CLOSE_CON_RPL] = do_close_con_rpl,
	[CPL_FW4_ACK] = do_fw4_ack,
	[CPL_ISCSI_HDR] = do_rx_iscsi_hdr,
1935
	[CPL_ISCSI_DATA] = do_rx_iscsi_data,
1936 1937
	[CPL_SET_TCB_RPL] = do_set_tcb_rpl,
	[CPL_RX_DATA_DDP] = do_rx_data_ddp,
1938
	[CPL_RX_ISCSI_DDP] = do_rx_data_ddp,
1939
	[CPL_RX_ISCSI_CMP] = do_rx_iscsi_cmp,
K
Karen Xie 已提交
1940
	[CPL_RX_DATA] = do_rx_data,
1941 1942
};

1943
static int cxgb4i_ofld_init(struct cxgbi_device *cdev)
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
{
	int rc;

	if (cxgb4i_max_connect > CXGB4I_MAX_CONN)
		cxgb4i_max_connect = CXGB4I_MAX_CONN;

	rc = cxgbi_device_portmap_create(cdev, cxgb4i_sport_base,
					cxgb4i_max_connect);
	if (rc < 0)
		return rc;

	cdev->csk_release_offload_resources = release_offload_resources;
	cdev->csk_push_tx_frames = push_tx_frames;
	cdev->csk_send_abort_req = send_abort_req;
	cdev->csk_send_close_req = send_close_req;
	cdev->csk_send_rx_credits = send_rx_credits;
	cdev->csk_alloc_cpls = alloc_cpls;
	cdev->csk_init_act_open = init_act_open;

	pr_info("cdev 0x%p, offload up, added.\n", cdev);
	return 0;
}

1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
static inline void
ulp_mem_io_set_hdr(struct cxgbi_device *cdev,
		   struct ulp_mem_io *req,
		   unsigned int wr_len, unsigned int dlen,
		   unsigned int pm_addr,
		   int tid)
{
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct ulptx_idata *idata = (struct ulptx_idata *)(req + 1);

	INIT_ULPTX_WR(req, wr_len, 0, tid);
	req->wr.wr_hi = htonl(FW_WR_OP_V(FW_ULPTX_WR) |
		FW_WR_ATOMIC_V(0));
	req->cmd = htonl(ULPTX_CMD_V(ULP_TX_MEM_WRITE) |
		ULP_MEMIO_ORDER_V(is_t4(lldi->adapter_type)) |
		T5_ULP_MEMIO_IMM_V(!is_t4(lldi->adapter_type)));
	req->dlen = htonl(ULP_MEMIO_DATA_LEN_V(dlen >> 5));
	req->lock_addr = htonl(ULP_MEMIO_ADDR_V(pm_addr >> 5));
	req->len16 = htonl(DIV_ROUND_UP(wr_len - sizeof(req->wr), 16));

	idata->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM));
	idata->len = htonl(dlen);
}

static struct sk_buff *
ddp_ppod_init_idata(struct cxgbi_device *cdev,
		    struct cxgbi_ppm *ppm,
		    unsigned int idx, unsigned int npods,
		    unsigned int tid)
{
	unsigned int pm_addr = (idx << PPOD_SIZE_SHIFT) + ppm->llimit;
	unsigned int dlen = npods << PPOD_SIZE_SHIFT;
	unsigned int wr_len = roundup(sizeof(struct ulp_mem_io) +
				sizeof(struct ulptx_idata) + dlen, 16);
	struct sk_buff *skb = alloc_wr(wr_len, 0, GFP_ATOMIC);

	if (!skb) {
		pr_err("%s: %s idx %u, npods %u, OOM.\n",
		       __func__, ppm->ndev->name, idx, npods);
		return NULL;
	}

	ulp_mem_io_set_hdr(cdev, (struct ulp_mem_io *)skb->head, wr_len, dlen,
			   pm_addr, tid);

	return skb;
}

static int ddp_ppod_write_idata(struct cxgbi_ppm *ppm, struct cxgbi_sock *csk,
				struct cxgbi_task_tag_info *ttinfo,
				unsigned int idx, unsigned int npods,
				struct scatterlist **sg_pp,
				unsigned int *sg_off)
{
	struct cxgbi_device *cdev = csk->cdev;
	struct sk_buff *skb = ddp_ppod_init_idata(cdev, ppm, idx, npods,
						  csk->tid);
	struct ulp_mem_io *req;
	struct ulptx_idata *idata;
	struct cxgbi_pagepod *ppod;
	int i;

	if (!skb)
		return -ENOMEM;

	req = (struct ulp_mem_io *)skb->head;
	idata = (struct ulptx_idata *)(req + 1);
	ppod = (struct cxgbi_pagepod *)(idata + 1);

	for (i = 0; i < npods; i++, ppod++)
		cxgbi_ddp_set_one_ppod(ppod, ttinfo, sg_pp, sg_off);

	cxgbi_skcb_set_flag(skb, SKCBF_TX_MEM_WRITE);
	cxgbi_skcb_set_flag(skb, SKCBF_TX_FLAG_COMPL);
	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);

	spin_lock_bh(&csk->lock);
	cxgbi_sock_skb_entail(csk, skb);
	spin_unlock_bh(&csk->lock);

	return 0;
}

static int ddp_set_map(struct cxgbi_ppm *ppm, struct cxgbi_sock *csk,
		       struct cxgbi_task_tag_info *ttinfo)
{
	unsigned int pidx = ttinfo->idx;
	unsigned int npods = ttinfo->npods;
	unsigned int i, cnt;
	int err = 0;
	struct scatterlist *sg = ttinfo->sgl;
	unsigned int offset = 0;

	ttinfo->cid = csk->port_id;

	for (i = 0; i < npods; i += cnt, pidx += cnt) {
		cnt = npods - i;

		if (cnt > ULPMEM_IDATA_MAX_NPPODS)
			cnt = ULPMEM_IDATA_MAX_NPPODS;
		err = ddp_ppod_write_idata(ppm, csk, ttinfo, pidx, cnt,
					   &sg, &offset);
		if (err < 0)
			break;
	}

	return err;
}

2076
static int ddp_setup_conn_pgidx(struct cxgbi_sock *csk, unsigned int tid,
2077
				int pg_idx)
2078 2079 2080 2081
{
	struct sk_buff *skb;
	struct cpl_set_tcb_field *req;

2082
	if (!pg_idx || pg_idx >= DDP_PGIDX_MAX)
2083 2084
		return 0;

2085
	skb = alloc_wr(sizeof(*req), 0, GFP_KERNEL);
2086 2087 2088
	if (!skb)
		return -ENOMEM;

2089
	/*  set up ulp page size */
2090 2091 2092
	req = (struct cpl_set_tcb_field *)skb->head;
	INIT_TP_WR(req, csk->tid);
	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
2093
	req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
2094 2095 2096
	req->word_cookie = htons(0);
	req->mask = cpu_to_be64(0x3 << 8);
	req->val = cpu_to_be64(pg_idx << 8);
2097 2098 2099 2100 2101
	set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->port_id);

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p, tid 0x%x, pg_idx %u.\n", csk, csk->tid, pg_idx);

2102
	reinit_completion(&csk->cmpl);
2103
	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
2104 2105 2106
	wait_for_completion(&csk->cmpl);

	return csk->err;
2107 2108 2109
}

static int ddp_setup_conn_digest(struct cxgbi_sock *csk, unsigned int tid,
2110
				 int hcrc, int dcrc)
2111 2112 2113 2114
{
	struct sk_buff *skb;
	struct cpl_set_tcb_field *req;

2115 2116
	if (!hcrc && !dcrc)
		return 0;
2117

2118
	skb = alloc_wr(sizeof(*req), 0, GFP_KERNEL);
2119 2120 2121 2122 2123
	if (!skb)
		return -ENOMEM;

	csk->hcrc_len = (hcrc ? 4 : 0);
	csk->dcrc_len = (dcrc ? 4 : 0);
2124
	/*  set up ulp submode */
2125 2126 2127
	req = (struct cpl_set_tcb_field *)skb->head;
	INIT_TP_WR(req, tid);
	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
2128
	req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
2129 2130 2131 2132
	req->word_cookie = htons(0);
	req->mask = cpu_to_be64(0x3 << 4);
	req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
				(dcrc ? ULP_CRC_DATA : 0)) << 4);
2133 2134 2135 2136 2137
	set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->port_id);

	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
		"csk 0x%p, tid 0x%x, crc %d,%d.\n", csk, csk->tid, hcrc, dcrc);

2138
	reinit_completion(&csk->cmpl);
2139
	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
2140 2141 2142
	wait_for_completion(&csk->cmpl);

	return csk->err;
2143 2144
}

2145 2146 2147 2148 2149 2150
static struct cxgbi_ppm *cdev2ppm(struct cxgbi_device *cdev)
{
	return (struct cxgbi_ppm *)(*((struct cxgb4_lld_info *)
				       (cxgbi_cdev_priv(cdev)))->iscsi_ppm);
}

2151 2152
static int cxgb4i_ddp_init(struct cxgbi_device *cdev)
{
2153 2154 2155
	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
	struct net_device *ndev = cdev->ports[0];
	struct cxgbi_tag_format tformat;
2156
	int i, err;
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170

	if (!lldi->vr->iscsi.size) {
		pr_warn("%s, iscsi NOT enabled, check config!\n", ndev->name);
		return -EACCES;
	}

	cdev->flags |= CXGBI_FLAG_USE_PPOD_OFLDQ;

	memset(&tformat, 0, sizeof(struct cxgbi_tag_format));
	for (i = 0; i < 4; i++)
		tformat.pgsz_order[i] = (lldi->iscsi_pgsz_order >> (i << 3))
					 & 0xF;
	cxgbi_tagmask_check(lldi->iscsi_tagmask, &tformat);

2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
	pr_info("iscsi_edram.start 0x%x iscsi_edram.size 0x%x",
		lldi->vr->ppod_edram.start, lldi->vr->ppod_edram.size);

	err = cxgbi_ddp_ppm_setup(lldi->iscsi_ppm, cdev, &tformat,
				  lldi->vr->iscsi.size, lldi->iscsi_llimit,
				  lldi->vr->iscsi.start, 2,
				  lldi->vr->ppod_edram.start,
				  lldi->vr->ppod_edram.size);

	if (err < 0)
		return err;
2182

2183 2184
	cdev->csk_ddp_setup_digest = ddp_setup_conn_digest;
	cdev->csk_ddp_setup_pgidx = ddp_setup_conn_pgidx;
2185 2186 2187 2188 2189 2190 2191
	cdev->csk_ddp_set_map = ddp_set_map;
	cdev->tx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
				  lldi->iscsi_iolen - ISCSI_PDU_NONPAYLOAD_LEN);
	cdev->rx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
				  lldi->iscsi_iolen - ISCSI_PDU_NONPAYLOAD_LEN);
	cdev->cdev2ppm = cdev2ppm;

2192 2193 2194
	return 0;
}

2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
static bool is_memfree(struct adapter *adap)
{
	u32 io;

	io = t4_read_reg(adap, MA_TARGET_MEM_ENABLE_A);
	if (is_t5(adap->params.chip)) {
		if ((io & EXT_MEM0_ENABLE_F) || (io & EXT_MEM1_ENABLE_F))
			return false;
	} else if (io & EXT_MEM_ENABLE_F) {
		return false;
	}

	return true;
}

2210 2211 2212 2213
static void *t4_uld_add(const struct cxgb4_lld_info *lldi)
{
	struct cxgbi_device *cdev;
	struct port_info *pi;
2214 2215 2216 2217 2218
	struct net_device *ndev;
	struct adapter *adap;
	struct tid_info *t;
	u32 max_cmds = CXGB4I_SCSI_HOST_QDEPTH;
	u32 max_conn = CXGBI_MAX_CONN;
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
	int i, rc;

	cdev = cxgbi_device_register(sizeof(*lldi), lldi->nports);
	if (!cdev) {
		pr_info("t4 device 0x%p, register failed.\n", lldi);
		return NULL;
	}
	pr_info("0x%p,0x%x, ports %u,%s, chan %u, q %u,%u, wr %u.\n",
		cdev, lldi->adapter_type, lldi->nports,
		lldi->ports[0]->name, lldi->nchan, lldi->ntxq,
		lldi->nrxq, lldi->wr_cred);
	for (i = 0; i < lldi->nrxq; i++)
		log_debug(1 << CXGBI_DBG_DEV,
			"t4 0x%p, rxq id #%d: %u.\n",
			cdev, i, lldi->rxq_ids[i]);

	memcpy(cxgbi_cdev_priv(cdev), lldi, sizeof(*lldi));
	cdev->flags = CXGBI_FLAG_DEV_T4;
	cdev->pdev = lldi->pdev;
	cdev->ports = lldi->ports;
	cdev->nports = lldi->nports;
	cdev->mtus = lldi->mtus;
	cdev->nmtus = NMTUS;
2242 2243
	cdev->rx_credit_thres = (CHELSIO_CHIP_VERSION(lldi->adapter_type) <=
				 CHELSIO_T5) ? cxgb4i_rx_credit_thres : 0;
2244 2245 2246
	cdev->skb_tx_rsvd = CXGB4I_TX_HEADER_LEN;
	cdev->skb_rx_extra = sizeof(struct cpl_iscsi_hdr);
	cdev->itp = &cxgb4i_iscsi_transport;
2247
	cdev->owner = THIS_MODULE;
2248

2249
	cdev->pfvf = FW_PFVF_CMD_PFN_V(lldi->pf);
2250 2251 2252
	pr_info("cdev 0x%p,%s, pfvf %u.\n",
		cdev, lldi->ports[0]->name, cdev->pfvf);

2253 2254
	rc = cxgb4i_ddp_init(cdev);
	if (rc) {
2255
		pr_info("t4 0x%p ddp init failed %d.\n", cdev, rc);
2256 2257
		goto err_out;
	}
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282

	ndev = cdev->ports[0];
	adap = netdev2adap(ndev);
	if (adap) {
		t = &adap->tids;
		if (t->ntids <= CXGBI_MAX_CONN)
			max_conn = t->ntids;

		if (is_memfree(adap)) {
			cdev->flags |=	CXGBI_FLAG_DEV_ISO_OFF;
			max_cmds = CXGB4I_SCSI_HOST_QDEPTH >> 2;

			pr_info("%s: 0x%p, tid %u, SO adapter.\n",
				ndev->name, cdev, t->ntids);
		}
	} else {
		pr_info("%s, 0x%p, NO adapter struct.\n", ndev->name, cdev);
	}

	/* ISO is enabled in T5/T6 firmware version >= 1.13.43.0 */
	if (!is_t4(lldi->adapter_type) &&
	    (lldi->fw_vers >= 0x10d2b00) &&
	    !(cdev->flags & CXGBI_FLAG_DEV_ISO_OFF))
		cdev->skb_iso_txhdr = sizeof(struct cpl_tx_data_iso);

2283 2284 2285 2286 2287 2288
	rc = cxgb4i_ofld_init(cdev);
	if (rc) {
		pr_info("t4 0x%p ofld init failed.\n", cdev);
		goto err_out;
	}

2289 2290 2291
	cxgb4i_host_template.can_queue = max_cmds;
	rc = cxgbi_hbas_add(cdev, CXGB4I_MAX_LUN, max_conn,
			    &cxgb4i_host_template, cxgb4i_stt);
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
	if (rc)
		goto err_out;

	for (i = 0; i < cdev->nports; i++) {
		pi = netdev_priv(lldi->ports[i]);
		cdev->hbas[i]->port_id = pi->port_id;
	}
	return cdev;

err_out:
	cxgbi_device_unregister(cdev);
	return ERR_PTR(-ENOMEM);
}

#define RX_PULL_LEN	128
static int t4_uld_rx_handler(void *handle, const __be64 *rsp,
				const struct pkt_gl *pgl)
{
	const struct cpl_act_establish *rpl;
	struct sk_buff *skb;
	unsigned int opc;
	struct cxgbi_device *cdev = handle;

	if (pgl == NULL) {
		unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;

2318
		skb = alloc_wr(len, 0, GFP_ATOMIC);
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
		if (!skb)
			goto nomem;
		skb_copy_to_linear_data(skb, &rsp[1], len);
	} else {
		if (unlikely(*(u8 *)rsp != *(u8 *)pgl->va)) {
			pr_info("? FL 0x%p,RSS%#llx,FL %#llx,len %u.\n",
				pgl->va, be64_to_cpu(*rsp),
				be64_to_cpu(*(u64 *)pgl->va),
				pgl->tot_len);
			return 0;
		}
		skb = cxgb4_pktgl_to_skb(pgl, RX_PULL_LEN, RX_PULL_LEN);
		if (unlikely(!skb))
			goto nomem;
	}

	rpl = (struct cpl_act_establish *)skb->data;
	opc = rpl->ot.opcode;
	log_debug(1 << CXGBI_DBG_TOE,
		"cdev %p, opcode 0x%x(0x%x,0x%x), skb %p.\n",
		 cdev, opc, rpl->ot.opcode_tid, ntohl(rpl->ot.opcode_tid), skb);
2340
	if (opc >= ARRAY_SIZE(cxgb4i_cplhandlers) || !cxgb4i_cplhandlers[opc]) {
2341 2342
		pr_err("No handler for opcode 0x%x.\n", opc);
		__kfree_skb(skb);
2343 2344 2345
	} else
		cxgb4i_cplhandlers[opc](cdev, skb);

2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
	return 0;
nomem:
	log_debug(1 << CXGBI_DBG_TOE, "OOM bailing out.\n");
	return 1;
}

static int t4_uld_state_change(void *handle, enum cxgb4_state state)
{
	struct cxgbi_device *cdev = handle;

	switch (state) {
	case CXGB4_STATE_UP:
		pr_info("cdev 0x%p, UP.\n", cdev);
		break;
	case CXGB4_STATE_START_RECOVERY:
		pr_info("cdev 0x%p, RECOVERY.\n", cdev);
		/* close all connections */
		break;
	case CXGB4_STATE_DOWN:
		pr_info("cdev 0x%p, DOWN.\n", cdev);
		break;
	case CXGB4_STATE_DETACH:
		pr_info("cdev 0x%p, DETACH.\n", cdev);
2369
		cxgbi_device_unregister(cdev);
2370 2371 2372 2373 2374 2375 2376 2377
		break;
	default:
		pr_info("cdev 0x%p, unknown state %d.\n", cdev, state);
		break;
	}
	return 0;
}

2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
#ifdef CONFIG_CHELSIO_T4_DCB
static int
cxgb4_dcb_change_notify(struct notifier_block *self, unsigned long val,
			void *data)
{
	int i, port = 0xFF;
	struct net_device *ndev;
	struct cxgbi_device *cdev = NULL;
	struct dcb_app_type *iscsi_app = data;
	struct cxgbi_ports_map *pmap;
	u8 priority;

	if (iscsi_app->dcbx & DCB_CAP_DCBX_VER_IEEE) {
2391 2392
		if ((iscsi_app->app.selector != IEEE_8021QAZ_APP_SEL_STREAM) &&
		    (iscsi_app->app.selector != IEEE_8021QAZ_APP_SEL_ANY))
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
			return NOTIFY_DONE;

		priority = iscsi_app->app.priority;
	} else if (iscsi_app->dcbx & DCB_CAP_DCBX_VER_CEE) {
		if (iscsi_app->app.selector != DCB_APP_IDTYPE_PORTNUM)
			return NOTIFY_DONE;

		if (!iscsi_app->app.priority)
			return NOTIFY_DONE;

		priority = ffs(iscsi_app->app.priority) - 1;
	} else {
		return NOTIFY_DONE;
	}

	if (iscsi_app->app.protocol != 3260)
		return NOTIFY_DONE;

	log_debug(1 << CXGBI_DBG_ISCSI, "iSCSI priority for ifid %d is %u\n",
		  iscsi_app->ifindex, priority);

	ndev = dev_get_by_index(&init_net, iscsi_app->ifindex);
	if (!ndev)
		return NOTIFY_DONE;

	cdev = cxgbi_device_find_by_netdev_rcu(ndev, &port);

	dev_put(ndev);
	if (!cdev)
		return NOTIFY_DONE;

	pmap = &cdev->pmap;

	for (i = 0; i < pmap->used; i++) {
		if (pmap->port_csk[i]) {
			struct cxgbi_sock *csk = pmap->port_csk[i];

			if (csk->dcb_priority != priority) {
				iscsi_conn_failure(csk->user_data,
						   ISCSI_ERR_CONN_FAILED);
				pr_info("Restarting iSCSI connection %p with "
					"priority %u->%u.\n", csk,
					csk->dcb_priority, priority);
			}
		}
	}
	return NOTIFY_OK;
}
#endif

2443 2444 2445 2446 2447 2448 2449 2450 2451 2452
static int __init cxgb4i_init_module(void)
{
	int rc;

	printk(KERN_INFO "%s", version);

	rc = cxgbi_iscsi_init(&cxgb4i_iscsi_transport, &cxgb4i_stt);
	if (rc < 0)
		return rc;
	cxgb4_register_uld(CXGB4_ULD_ISCSI, &cxgb4i_uld_info);
2453

2454 2455 2456 2457
#ifdef CONFIG_CHELSIO_T4_DCB
	pr_info("%s dcb enabled.\n", DRV_MODULE_NAME);
	register_dcbevent_notifier(&cxgb4_dcb_change);
#endif
2458 2459 2460 2461 2462
	return 0;
}

static void __exit cxgb4i_exit_module(void)
{
2463 2464 2465
#ifdef CONFIG_CHELSIO_T4_DCB
	unregister_dcbevent_notifier(&cxgb4_dcb_change);
#endif
2466 2467 2468 2469 2470 2471 2472
	cxgb4_unregister_uld(CXGB4_ULD_ISCSI);
	cxgbi_device_unregister_all(CXGBI_FLAG_DEV_T4);
	cxgbi_iscsi_cleanup(&cxgb4i_iscsi_transport, &cxgb4i_stt);
}

module_init(cxgb4i_init_module);
module_exit(cxgb4i_exit_module);