xref: /openbmc/linux/drivers/scsi/cxgbi/cxgb4i/cxgb4i.c (revision c4737377)
17b36b6e0Skxie@chelsio.com /*
27b36b6e0Skxie@chelsio.com  * cxgb4i.c: Chelsio T4 iSCSI driver.
37b36b6e0Skxie@chelsio.com  *
47b36b6e0Skxie@chelsio.com  * Copyright (c) 2010 Chelsio Communications, Inc.
57b36b6e0Skxie@chelsio.com  *
67b36b6e0Skxie@chelsio.com  * This program is free software; you can redistribute it and/or modify
77b36b6e0Skxie@chelsio.com  * it under the terms of the GNU General Public License as published by
87b36b6e0Skxie@chelsio.com  * the Free Software Foundation.
97b36b6e0Skxie@chelsio.com  *
107b36b6e0Skxie@chelsio.com  * Written by:	Karen Xie (kxie@chelsio.com)
117b36b6e0Skxie@chelsio.com  *		Rakesh Ranjan (rranjan@chelsio.com)
127b36b6e0Skxie@chelsio.com  */
137b36b6e0Skxie@chelsio.com 
147b36b6e0Skxie@chelsio.com #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
157b36b6e0Skxie@chelsio.com 
167b36b6e0Skxie@chelsio.com #include <linux/module.h>
177b36b6e0Skxie@chelsio.com #include <linux/moduleparam.h>
187b36b6e0Skxie@chelsio.com #include <scsi/scsi_host.h>
197b36b6e0Skxie@chelsio.com #include <net/tcp.h>
207b36b6e0Skxie@chelsio.com #include <net/dst.h>
217b36b6e0Skxie@chelsio.com #include <linux/netdevice.h>
227b36b6e0Skxie@chelsio.com 
237b36b6e0Skxie@chelsio.com #include "t4_msg.h"
247b36b6e0Skxie@chelsio.com #include "cxgb4.h"
257b36b6e0Skxie@chelsio.com #include "cxgb4_uld.h"
267b36b6e0Skxie@chelsio.com #include "t4fw_api.h"
277b36b6e0Skxie@chelsio.com #include "l2t.h"
287b36b6e0Skxie@chelsio.com #include "cxgb4i.h"
297b36b6e0Skxie@chelsio.com 
307b36b6e0Skxie@chelsio.com static unsigned int dbg_level;
317b36b6e0Skxie@chelsio.com 
327b36b6e0Skxie@chelsio.com #include "../libcxgbi.h"
337b36b6e0Skxie@chelsio.com 
347b36b6e0Skxie@chelsio.com #define	DRV_MODULE_NAME		"cxgb4i"
357b36b6e0Skxie@chelsio.com #define DRV_MODULE_DESC		"Chelsio T4 iSCSI Driver"
36e27d6169Skxie@chelsio.com #define	DRV_MODULE_VERSION	"0.9.1"
37e27d6169Skxie@chelsio.com #define	DRV_MODULE_RELDATE	"Aug. 2010"
387b36b6e0Skxie@chelsio.com 
397b36b6e0Skxie@chelsio.com static char version[] =
407b36b6e0Skxie@chelsio.com 	DRV_MODULE_DESC " " DRV_MODULE_NAME
417b36b6e0Skxie@chelsio.com 	" v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
427b36b6e0Skxie@chelsio.com 
437b36b6e0Skxie@chelsio.com MODULE_AUTHOR("Chelsio Communications, Inc.");
447b36b6e0Skxie@chelsio.com MODULE_DESCRIPTION(DRV_MODULE_DESC);
457b36b6e0Skxie@chelsio.com MODULE_VERSION(DRV_MODULE_VERSION);
467b36b6e0Skxie@chelsio.com MODULE_LICENSE("GPL");
477b36b6e0Skxie@chelsio.com 
487b36b6e0Skxie@chelsio.com module_param(dbg_level, uint, 0644);
497b36b6e0Skxie@chelsio.com MODULE_PARM_DESC(dbg_level, "Debug flag (default=0)");
507b36b6e0Skxie@chelsio.com 
517b36b6e0Skxie@chelsio.com static int cxgb4i_rcv_win = 256 * 1024;
527b36b6e0Skxie@chelsio.com module_param(cxgb4i_rcv_win, int, 0644);
537b36b6e0Skxie@chelsio.com MODULE_PARM_DESC(cxgb4i_rcv_win, "TCP reveive window in bytes");
547b36b6e0Skxie@chelsio.com 
557b36b6e0Skxie@chelsio.com static int cxgb4i_snd_win = 128 * 1024;
567b36b6e0Skxie@chelsio.com module_param(cxgb4i_snd_win, int, 0644);
577b36b6e0Skxie@chelsio.com MODULE_PARM_DESC(cxgb4i_snd_win, "TCP send window in bytes");
587b36b6e0Skxie@chelsio.com 
597b36b6e0Skxie@chelsio.com static int cxgb4i_rx_credit_thres = 10 * 1024;
607b36b6e0Skxie@chelsio.com module_param(cxgb4i_rx_credit_thres, int, 0644);
617b36b6e0Skxie@chelsio.com MODULE_PARM_DESC(cxgb4i_rx_credit_thres,
627b36b6e0Skxie@chelsio.com 		"RX credits return threshold in bytes (default=10KB)");
637b36b6e0Skxie@chelsio.com 
647b36b6e0Skxie@chelsio.com static unsigned int cxgb4i_max_connect = (8 * 1024);
657b36b6e0Skxie@chelsio.com module_param(cxgb4i_max_connect, uint, 0644);
667b36b6e0Skxie@chelsio.com MODULE_PARM_DESC(cxgb4i_max_connect, "Maximum number of connections");
677b36b6e0Skxie@chelsio.com 
687b36b6e0Skxie@chelsio.com static unsigned short cxgb4i_sport_base = 20000;
697b36b6e0Skxie@chelsio.com module_param(cxgb4i_sport_base, ushort, 0644);
707b36b6e0Skxie@chelsio.com MODULE_PARM_DESC(cxgb4i_sport_base, "Starting port number (default 20000)");
717b36b6e0Skxie@chelsio.com 
727b36b6e0Skxie@chelsio.com typedef void (*cxgb4i_cplhandler_func)(struct cxgbi_device *, struct sk_buff *);
737b36b6e0Skxie@chelsio.com 
747b36b6e0Skxie@chelsio.com static void *t4_uld_add(const struct cxgb4_lld_info *);
757b36b6e0Skxie@chelsio.com static int t4_uld_rx_handler(void *, const __be64 *, const struct pkt_gl *);
767b36b6e0Skxie@chelsio.com static int t4_uld_state_change(void *, enum cxgb4_state state);
777b36b6e0Skxie@chelsio.com 
787b36b6e0Skxie@chelsio.com static const struct cxgb4_uld_info cxgb4i_uld_info = {
797b36b6e0Skxie@chelsio.com 	.name = DRV_MODULE_NAME,
807b36b6e0Skxie@chelsio.com 	.add = t4_uld_add,
817b36b6e0Skxie@chelsio.com 	.rx_handler = t4_uld_rx_handler,
827b36b6e0Skxie@chelsio.com 	.state_change = t4_uld_state_change,
837b36b6e0Skxie@chelsio.com };
847b36b6e0Skxie@chelsio.com 
857b36b6e0Skxie@chelsio.com static struct scsi_host_template cxgb4i_host_template = {
867b36b6e0Skxie@chelsio.com 	.module		= THIS_MODULE,
877b36b6e0Skxie@chelsio.com 	.name		= DRV_MODULE_NAME,
887b36b6e0Skxie@chelsio.com 	.proc_name	= DRV_MODULE_NAME,
897b36b6e0Skxie@chelsio.com 	.can_queue	= CXGB4I_SCSI_HOST_QDEPTH,
907b36b6e0Skxie@chelsio.com 	.queuecommand	= iscsi_queuecommand,
917b36b6e0Skxie@chelsio.com 	.change_queue_depth = iscsi_change_queue_depth,
927b36b6e0Skxie@chelsio.com 	.sg_tablesize	= SG_ALL,
937b36b6e0Skxie@chelsio.com 	.max_sectors	= 0xFFFF,
947b36b6e0Skxie@chelsio.com 	.cmd_per_lun	= ISCSI_DEF_CMD_PER_LUN,
957b36b6e0Skxie@chelsio.com 	.eh_abort_handler = iscsi_eh_abort,
967b36b6e0Skxie@chelsio.com 	.eh_device_reset_handler = iscsi_eh_device_reset,
977b36b6e0Skxie@chelsio.com 	.eh_target_reset_handler = iscsi_eh_recover_target,
987b36b6e0Skxie@chelsio.com 	.target_alloc	= iscsi_target_alloc,
997b36b6e0Skxie@chelsio.com 	.use_clustering	= DISABLE_CLUSTERING,
1007b36b6e0Skxie@chelsio.com 	.this_id	= -1,
1017b36b6e0Skxie@chelsio.com };
1027b36b6e0Skxie@chelsio.com 
1037b36b6e0Skxie@chelsio.com static struct iscsi_transport cxgb4i_iscsi_transport = {
1047b36b6e0Skxie@chelsio.com 	.owner		= THIS_MODULE,
1057b36b6e0Skxie@chelsio.com 	.name		= DRV_MODULE_NAME,
1067b36b6e0Skxie@chelsio.com 	.caps		= CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST |
1077b36b6e0Skxie@chelsio.com 				CAP_DATADGST | CAP_DIGEST_OFFLOAD |
108fdafd4dfSMike Christie 				CAP_PADDING_OFFLOAD | CAP_TEXT_NEGO,
1093128c6c7SMike Christie 	.attr_is_visible	= cxgbi_attr_is_visible,
1107b36b6e0Skxie@chelsio.com 	.get_host_param	= cxgbi_get_host_param,
1117b36b6e0Skxie@chelsio.com 	.set_host_param	= cxgbi_set_host_param,
1127b36b6e0Skxie@chelsio.com 	/* session management */
1137b36b6e0Skxie@chelsio.com 	.create_session	= cxgbi_create_session,
1147b36b6e0Skxie@chelsio.com 	.destroy_session	= cxgbi_destroy_session,
1157b36b6e0Skxie@chelsio.com 	.get_session_param = iscsi_session_get_param,
1167b36b6e0Skxie@chelsio.com 	/* connection management */
1177b36b6e0Skxie@chelsio.com 	.create_conn	= cxgbi_create_conn,
1187b36b6e0Skxie@chelsio.com 	.bind_conn		= cxgbi_bind_conn,
1197b36b6e0Skxie@chelsio.com 	.destroy_conn	= iscsi_tcp_conn_teardown,
1207b36b6e0Skxie@chelsio.com 	.start_conn		= iscsi_conn_start,
1217b36b6e0Skxie@chelsio.com 	.stop_conn		= iscsi_conn_stop,
122c71b9b66SMike Christie 	.get_conn_param	= iscsi_conn_get_param,
1237b36b6e0Skxie@chelsio.com 	.set_param	= cxgbi_set_conn_param,
1247b36b6e0Skxie@chelsio.com 	.get_stats	= cxgbi_get_conn_stats,
1257b36b6e0Skxie@chelsio.com 	/* pdu xmit req from user space */
1267b36b6e0Skxie@chelsio.com 	.send_pdu	= iscsi_conn_send_pdu,
1277b36b6e0Skxie@chelsio.com 	/* task */
1287b36b6e0Skxie@chelsio.com 	.init_task	= iscsi_tcp_task_init,
1297b36b6e0Skxie@chelsio.com 	.xmit_task	= iscsi_tcp_task_xmit,
1307b36b6e0Skxie@chelsio.com 	.cleanup_task	= cxgbi_cleanup_task,
1317b36b6e0Skxie@chelsio.com 	/* pdu */
1327b36b6e0Skxie@chelsio.com 	.alloc_pdu	= cxgbi_conn_alloc_pdu,
1337b36b6e0Skxie@chelsio.com 	.init_pdu	= cxgbi_conn_init_pdu,
1347b36b6e0Skxie@chelsio.com 	.xmit_pdu	= cxgbi_conn_xmit_pdu,
1357b36b6e0Skxie@chelsio.com 	.parse_pdu_itt	= cxgbi_parse_pdu_itt,
1367b36b6e0Skxie@chelsio.com 	/* TCP connect/disconnect */
137c71b9b66SMike Christie 	.get_ep_param	= cxgbi_get_ep_param,
1387b36b6e0Skxie@chelsio.com 	.ep_connect	= cxgbi_ep_connect,
1397b36b6e0Skxie@chelsio.com 	.ep_poll	= cxgbi_ep_poll,
1407b36b6e0Skxie@chelsio.com 	.ep_disconnect	= cxgbi_ep_disconnect,
1417b36b6e0Skxie@chelsio.com 	/* Error recovery timeout call */
1427b36b6e0Skxie@chelsio.com 	.session_recovery_timedout = iscsi_session_recovery_timedout,
1437b36b6e0Skxie@chelsio.com };
1447b36b6e0Skxie@chelsio.com 
1457b36b6e0Skxie@chelsio.com static struct scsi_transport_template *cxgb4i_stt;
1467b36b6e0Skxie@chelsio.com 
1477b36b6e0Skxie@chelsio.com /*
1487b36b6e0Skxie@chelsio.com  * CPL (Chelsio Protocol Language) defines a message passing interface between
1497b36b6e0Skxie@chelsio.com  * the host driver and Chelsio asic.
1507b36b6e0Skxie@chelsio.com  * The section below implments CPLs that related to iscsi tcp connection
1517b36b6e0Skxie@chelsio.com  * open/close/abort and data send/receive.
1527b36b6e0Skxie@chelsio.com  */
1537b36b6e0Skxie@chelsio.com #define DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
1547b36b6e0Skxie@chelsio.com #define RCV_BUFSIZ_MASK		0x3FFU
1557b36b6e0Skxie@chelsio.com #define MAX_IMM_TX_PKT_LEN	128
1567b36b6e0Skxie@chelsio.com 
1577b36b6e0Skxie@chelsio.com static inline void set_queue(struct sk_buff *skb, unsigned int queue,
1587b36b6e0Skxie@chelsio.com 				const struct cxgbi_sock *csk)
1597b36b6e0Skxie@chelsio.com {
1607b36b6e0Skxie@chelsio.com 	skb->queue_mapping = queue;
1617b36b6e0Skxie@chelsio.com }
1627b36b6e0Skxie@chelsio.com 
1637b36b6e0Skxie@chelsio.com static int push_tx_frames(struct cxgbi_sock *, int);
1647b36b6e0Skxie@chelsio.com 
1657b36b6e0Skxie@chelsio.com /*
1667b36b6e0Skxie@chelsio.com  * is_ofld_imm - check whether a packet can be sent as immediate data
1677b36b6e0Skxie@chelsio.com  * @skb: the packet
1687b36b6e0Skxie@chelsio.com  *
1697b36b6e0Skxie@chelsio.com  * Returns true if a packet can be sent as an offload WR with immediate
1707b36b6e0Skxie@chelsio.com  * data.  We currently use the same limit as for Ethernet packets.
1717b36b6e0Skxie@chelsio.com  */
1727b36b6e0Skxie@chelsio.com static inline int is_ofld_imm(const struct sk_buff *skb)
1737b36b6e0Skxie@chelsio.com {
1747b36b6e0Skxie@chelsio.com 	return skb->len <= (MAX_IMM_TX_PKT_LEN -
1757b36b6e0Skxie@chelsio.com 			sizeof(struct fw_ofld_tx_data_wr));
1767b36b6e0Skxie@chelsio.com }
1777b36b6e0Skxie@chelsio.com 
1787b36b6e0Skxie@chelsio.com static void send_act_open_req(struct cxgbi_sock *csk, struct sk_buff *skb,
1797b36b6e0Skxie@chelsio.com 				struct l2t_entry *e)
1807b36b6e0Skxie@chelsio.com {
1817b36b6e0Skxie@chelsio.com 	struct cpl_act_open_req *req;
1827b36b6e0Skxie@chelsio.com 	int wscale = cxgbi_sock_compute_wscale(csk->mss_idx);
1837b36b6e0Skxie@chelsio.com 	unsigned long long opt0;
1847b36b6e0Skxie@chelsio.com 	unsigned int opt2;
1857b36b6e0Skxie@chelsio.com 	unsigned int qid_atid = ((unsigned int)csk->atid) |
1867b36b6e0Skxie@chelsio.com 				 (((unsigned int)csk->rss_qid) << 14);
1877b36b6e0Skxie@chelsio.com 
1887b36b6e0Skxie@chelsio.com 	opt0 = KEEP_ALIVE(1) |
1897b36b6e0Skxie@chelsio.com 		WND_SCALE(wscale) |
1907b36b6e0Skxie@chelsio.com 		MSS_IDX(csk->mss_idx) |
1917b36b6e0Skxie@chelsio.com 		L2T_IDX(((struct l2t_entry *)csk->l2t)->idx) |
1927b36b6e0Skxie@chelsio.com 		TX_CHAN(csk->tx_chan) |
1937b36b6e0Skxie@chelsio.com 		SMAC_SEL(csk->smac_idx) |
1947b36b6e0Skxie@chelsio.com 		ULP_MODE(ULP_MODE_ISCSI) |
1957b36b6e0Skxie@chelsio.com 		RCV_BUFSIZ(cxgb4i_rcv_win >> 10);
1967b36b6e0Skxie@chelsio.com 	opt2 = RX_CHANNEL(0) |
1977b36b6e0Skxie@chelsio.com 		RSS_QUEUE_VALID |
1987b36b6e0Skxie@chelsio.com 		(1 << 20) | (1 << 22) |
1997b36b6e0Skxie@chelsio.com 		RSS_QUEUE(csk->rss_qid);
2007b36b6e0Skxie@chelsio.com 
2017b36b6e0Skxie@chelsio.com 	set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);
2027b36b6e0Skxie@chelsio.com 	req = (struct cpl_act_open_req *)skb->head;
2037b36b6e0Skxie@chelsio.com 
2047b36b6e0Skxie@chelsio.com 	INIT_TP_WR(req, 0);
2057b36b6e0Skxie@chelsio.com 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
2067b36b6e0Skxie@chelsio.com 					qid_atid));
2077b36b6e0Skxie@chelsio.com 	req->local_port = csk->saddr.sin_port;
2087b36b6e0Skxie@chelsio.com 	req->peer_port = csk->daddr.sin_port;
2097b36b6e0Skxie@chelsio.com 	req->local_ip = csk->saddr.sin_addr.s_addr;
2107b36b6e0Skxie@chelsio.com 	req->peer_ip = csk->daddr.sin_addr.s_addr;
2117b36b6e0Skxie@chelsio.com 	req->opt0 = cpu_to_be64(opt0);
2127b36b6e0Skxie@chelsio.com 	req->params = 0;
2137b36b6e0Skxie@chelsio.com 	req->opt2 = cpu_to_be32(opt2);
2147b36b6e0Skxie@chelsio.com 
2157b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
2167b36b6e0Skxie@chelsio.com 		"csk 0x%p, %pI4:%u-%pI4:%u, atid %d, qid %u.\n",
2177b36b6e0Skxie@chelsio.com 		csk, &req->local_ip, ntohs(req->local_port),
2187b36b6e0Skxie@chelsio.com 		&req->peer_ip, ntohs(req->peer_port),
2197b36b6e0Skxie@chelsio.com 		csk->atid, csk->rss_qid);
2207b36b6e0Skxie@chelsio.com 
2217b36b6e0Skxie@chelsio.com 	cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
2227b36b6e0Skxie@chelsio.com }
2237b36b6e0Skxie@chelsio.com 
2247b36b6e0Skxie@chelsio.com static void send_close_req(struct cxgbi_sock *csk)
2257b36b6e0Skxie@chelsio.com {
2267b36b6e0Skxie@chelsio.com 	struct sk_buff *skb = csk->cpl_close;
2277b36b6e0Skxie@chelsio.com 	struct cpl_close_con_req *req = (struct cpl_close_con_req *)skb->head;
2287b36b6e0Skxie@chelsio.com 	unsigned int tid = csk->tid;
2297b36b6e0Skxie@chelsio.com 
2307b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
2317b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx, tid %u.\n",
2327b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid);
2337b36b6e0Skxie@chelsio.com 	csk->cpl_close = NULL;
2347b36b6e0Skxie@chelsio.com 	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
2357b36b6e0Skxie@chelsio.com 	INIT_TP_WR(req, tid);
2367b36b6e0Skxie@chelsio.com 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
2377b36b6e0Skxie@chelsio.com 	req->rsvd = 0;
2387b36b6e0Skxie@chelsio.com 
2397b36b6e0Skxie@chelsio.com 	cxgbi_sock_skb_entail(csk, skb);
2407b36b6e0Skxie@chelsio.com 	if (csk->state >= CTP_ESTABLISHED)
2417b36b6e0Skxie@chelsio.com 		push_tx_frames(csk, 1);
2427b36b6e0Skxie@chelsio.com }
2437b36b6e0Skxie@chelsio.com 
2447b36b6e0Skxie@chelsio.com static void abort_arp_failure(void *handle, struct sk_buff *skb)
2457b36b6e0Skxie@chelsio.com {
2467b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk = (struct cxgbi_sock *)handle;
2477b36b6e0Skxie@chelsio.com 	struct cpl_abort_req *req;
2487b36b6e0Skxie@chelsio.com 
2497b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
2507b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx, tid %u, abort.\n",
2517b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid);
2527b36b6e0Skxie@chelsio.com 	req = (struct cpl_abort_req *)skb->data;
2537b36b6e0Skxie@chelsio.com 	req->cmd = CPL_ABORT_NO_RST;
2547b36b6e0Skxie@chelsio.com 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
2557b36b6e0Skxie@chelsio.com }
2567b36b6e0Skxie@chelsio.com 
2577b36b6e0Skxie@chelsio.com static void send_abort_req(struct cxgbi_sock *csk)
2587b36b6e0Skxie@chelsio.com {
2597b36b6e0Skxie@chelsio.com 	struct cpl_abort_req *req;
2607b36b6e0Skxie@chelsio.com 	struct sk_buff *skb = csk->cpl_abort_req;
2617b36b6e0Skxie@chelsio.com 
2627b36b6e0Skxie@chelsio.com 	if (unlikely(csk->state == CTP_ABORTING) || !skb || !csk->cdev)
2637b36b6e0Skxie@chelsio.com 		return;
2647b36b6e0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_ABORTING);
2657b36b6e0Skxie@chelsio.com 	cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_PENDING);
2667b36b6e0Skxie@chelsio.com 	cxgbi_sock_purge_write_queue(csk);
2677b36b6e0Skxie@chelsio.com 
2687b36b6e0Skxie@chelsio.com 	csk->cpl_abort_req = NULL;
2697b36b6e0Skxie@chelsio.com 	req = (struct cpl_abort_req *)skb->head;
2707b36b6e0Skxie@chelsio.com 	set_queue(skb, CPL_PRIORITY_DATA, csk);
2717b36b6e0Skxie@chelsio.com 	req->cmd = CPL_ABORT_SEND_RST;
2727b36b6e0Skxie@chelsio.com 	t4_set_arp_err_handler(skb, csk, abort_arp_failure);
2737b36b6e0Skxie@chelsio.com 	INIT_TP_WR(req, csk->tid);
2747b36b6e0Skxie@chelsio.com 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, csk->tid));
2757b36b6e0Skxie@chelsio.com 	req->rsvd0 = htonl(csk->snd_nxt);
2767b36b6e0Skxie@chelsio.com 	req->rsvd1 = !cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT);
2777b36b6e0Skxie@chelsio.com 
2787b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
2797b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx,%u, snd_nxt %u, 0x%x.\n",
2807b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid, csk->snd_nxt,
2817b36b6e0Skxie@chelsio.com 		req->rsvd1);
2827b36b6e0Skxie@chelsio.com 
2837b36b6e0Skxie@chelsio.com 	cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
2847b36b6e0Skxie@chelsio.com }
2857b36b6e0Skxie@chelsio.com 
2867b36b6e0Skxie@chelsio.com static void send_abort_rpl(struct cxgbi_sock *csk, int rst_status)
2877b36b6e0Skxie@chelsio.com {
2887b36b6e0Skxie@chelsio.com 	struct sk_buff *skb = csk->cpl_abort_rpl;
2897b36b6e0Skxie@chelsio.com 	struct cpl_abort_rpl *rpl = (struct cpl_abort_rpl *)skb->head;
2907b36b6e0Skxie@chelsio.com 
2917b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
2927b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx,%u, status %d.\n",
2937b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid, rst_status);
2947b36b6e0Skxie@chelsio.com 
2957b36b6e0Skxie@chelsio.com 	csk->cpl_abort_rpl = NULL;
2967b36b6e0Skxie@chelsio.com 	set_queue(skb, CPL_PRIORITY_DATA, csk);
2977b36b6e0Skxie@chelsio.com 	INIT_TP_WR(rpl, csk->tid);
2987b36b6e0Skxie@chelsio.com 	OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, csk->tid));
2997b36b6e0Skxie@chelsio.com 	rpl->cmd = rst_status;
3007b36b6e0Skxie@chelsio.com 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
3017b36b6e0Skxie@chelsio.com }
3027b36b6e0Skxie@chelsio.com 
3037b36b6e0Skxie@chelsio.com /*
3047b36b6e0Skxie@chelsio.com  * CPL connection rx data ack: host ->
3057b36b6e0Skxie@chelsio.com  * Send RX credits through an RX_DATA_ACK CPL message. Returns the number of
3067b36b6e0Skxie@chelsio.com  * credits sent.
3077b36b6e0Skxie@chelsio.com  */
3087b36b6e0Skxie@chelsio.com static u32 send_rx_credits(struct cxgbi_sock *csk, u32 credits)
3097b36b6e0Skxie@chelsio.com {
3107b36b6e0Skxie@chelsio.com 	struct sk_buff *skb;
3117b36b6e0Skxie@chelsio.com 	struct cpl_rx_data_ack *req;
3127b36b6e0Skxie@chelsio.com 
3137b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
3147b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx,%u, credit %u.\n",
3157b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid, credits);
3167b36b6e0Skxie@chelsio.com 
31724d3f95aSkxie@chelsio.com 	skb = alloc_wr(sizeof(*req), 0, GFP_ATOMIC);
3187b36b6e0Skxie@chelsio.com 	if (!skb) {
3197b36b6e0Skxie@chelsio.com 		pr_info("csk 0x%p, credit %u, OOM.\n", csk, credits);
3207b36b6e0Skxie@chelsio.com 		return 0;
3217b36b6e0Skxie@chelsio.com 	}
3227b36b6e0Skxie@chelsio.com 	req = (struct cpl_rx_data_ack *)skb->head;
3237b36b6e0Skxie@chelsio.com 
3247b36b6e0Skxie@chelsio.com 	set_wr_txq(skb, CPL_PRIORITY_ACK, csk->port_id);
3257b36b6e0Skxie@chelsio.com 	INIT_TP_WR(req, csk->tid);
3267b36b6e0Skxie@chelsio.com 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
3277b36b6e0Skxie@chelsio.com 				      csk->tid));
3287b36b6e0Skxie@chelsio.com 	req->credit_dack = cpu_to_be32(RX_CREDITS(credits) | RX_FORCE_ACK(1));
3297b36b6e0Skxie@chelsio.com 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
3307b36b6e0Skxie@chelsio.com 	return credits;
3317b36b6e0Skxie@chelsio.com }
3327b36b6e0Skxie@chelsio.com 
3337b36b6e0Skxie@chelsio.com /*
3347b36b6e0Skxie@chelsio.com  * sgl_len - calculates the size of an SGL of the given capacity
3357b36b6e0Skxie@chelsio.com  * @n: the number of SGL entries
3367b36b6e0Skxie@chelsio.com  * Calculates the number of flits needed for a scatter/gather list that
3377b36b6e0Skxie@chelsio.com  * can hold the given number of entries.
3387b36b6e0Skxie@chelsio.com  */
3397b36b6e0Skxie@chelsio.com static inline unsigned int sgl_len(unsigned int n)
3407b36b6e0Skxie@chelsio.com {
3417b36b6e0Skxie@chelsio.com 	n--;
3427b36b6e0Skxie@chelsio.com 	return (3 * n) / 2 + (n & 1) + 2;
3437b36b6e0Skxie@chelsio.com }
3447b36b6e0Skxie@chelsio.com 
3457b36b6e0Skxie@chelsio.com /*
3467b36b6e0Skxie@chelsio.com  * calc_tx_flits_ofld - calculate # of flits for an offload packet
3477b36b6e0Skxie@chelsio.com  * @skb: the packet
3487b36b6e0Skxie@chelsio.com  *
3497b36b6e0Skxie@chelsio.com  * Returns the number of flits needed for the given offload packet.
3507b36b6e0Skxie@chelsio.com  * These packets are already fully constructed and no additional headers
3517b36b6e0Skxie@chelsio.com  * will be added.
3527b36b6e0Skxie@chelsio.com  */
3537b36b6e0Skxie@chelsio.com static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
3547b36b6e0Skxie@chelsio.com {
3557b36b6e0Skxie@chelsio.com 	unsigned int flits, cnt;
3567b36b6e0Skxie@chelsio.com 
3577b36b6e0Skxie@chelsio.com 	if (is_ofld_imm(skb))
3587b36b6e0Skxie@chelsio.com 		return DIV_ROUND_UP(skb->len, 8);
3597b36b6e0Skxie@chelsio.com 	flits = skb_transport_offset(skb) / 8;
3607b36b6e0Skxie@chelsio.com 	cnt = skb_shinfo(skb)->nr_frags;
3617b36b6e0Skxie@chelsio.com 	if (skb->tail != skb->transport_header)
3627b36b6e0Skxie@chelsio.com 		cnt++;
3637b36b6e0Skxie@chelsio.com 	return flits + sgl_len(cnt);
3647b36b6e0Skxie@chelsio.com }
3657b36b6e0Skxie@chelsio.com 
3667b36b6e0Skxie@chelsio.com static inline void send_tx_flowc_wr(struct cxgbi_sock *csk)
3677b36b6e0Skxie@chelsio.com {
3687b36b6e0Skxie@chelsio.com 	struct sk_buff *skb;
3697b36b6e0Skxie@chelsio.com 	struct fw_flowc_wr *flowc;
3707b36b6e0Skxie@chelsio.com 	int flowclen, i;
3717b36b6e0Skxie@chelsio.com 
3727b36b6e0Skxie@chelsio.com 	flowclen = 80;
37324d3f95aSkxie@chelsio.com 	skb = alloc_wr(flowclen, 0, GFP_ATOMIC);
3747b36b6e0Skxie@chelsio.com 	flowc = (struct fw_flowc_wr *)skb->head;
3757b36b6e0Skxie@chelsio.com 	flowc->op_to_nparams =
3767b36b6e0Skxie@chelsio.com 		htonl(FW_WR_OP(FW_FLOWC_WR) | FW_FLOWC_WR_NPARAMS(8));
3777b36b6e0Skxie@chelsio.com 	flowc->flowid_len16 =
3787b36b6e0Skxie@chelsio.com 		htonl(FW_WR_LEN16(DIV_ROUND_UP(72, 16)) |
3797b36b6e0Skxie@chelsio.com 				FW_WR_FLOWID(csk->tid));
3807b36b6e0Skxie@chelsio.com 	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
381e27d6169Skxie@chelsio.com 	flowc->mnemval[0].val = htonl(csk->cdev->pfvf);
3827b36b6e0Skxie@chelsio.com 	flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
3837b36b6e0Skxie@chelsio.com 	flowc->mnemval[1].val = htonl(csk->tx_chan);
3847b36b6e0Skxie@chelsio.com 	flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
3857b36b6e0Skxie@chelsio.com 	flowc->mnemval[2].val = htonl(csk->tx_chan);
3867b36b6e0Skxie@chelsio.com 	flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
3877b36b6e0Skxie@chelsio.com 	flowc->mnemval[3].val = htonl(csk->rss_qid);
3887b36b6e0Skxie@chelsio.com 	flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
3897b36b6e0Skxie@chelsio.com 	flowc->mnemval[4].val = htonl(csk->snd_nxt);
3907b36b6e0Skxie@chelsio.com 	flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
3917b36b6e0Skxie@chelsio.com 	flowc->mnemval[5].val = htonl(csk->rcv_nxt);
3927b36b6e0Skxie@chelsio.com 	flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
3937b36b6e0Skxie@chelsio.com 	flowc->mnemval[6].val = htonl(cxgb4i_snd_win);
3947b36b6e0Skxie@chelsio.com 	flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
3957b36b6e0Skxie@chelsio.com 	flowc->mnemval[7].val = htonl(csk->advmss);
3967b36b6e0Skxie@chelsio.com 	flowc->mnemval[8].mnemonic = 0;
3977b36b6e0Skxie@chelsio.com 	flowc->mnemval[8].val = 0;
3987b36b6e0Skxie@chelsio.com 	for (i = 0; i < 9; i++) {
3997b36b6e0Skxie@chelsio.com 		flowc->mnemval[i].r4[0] = 0;
4007b36b6e0Skxie@chelsio.com 		flowc->mnemval[i].r4[1] = 0;
4017b36b6e0Skxie@chelsio.com 		flowc->mnemval[i].r4[2] = 0;
4027b36b6e0Skxie@chelsio.com 	}
4037b36b6e0Skxie@chelsio.com 	set_queue(skb, CPL_PRIORITY_DATA, csk);
4047b36b6e0Skxie@chelsio.com 
4057b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
4067b36b6e0Skxie@chelsio.com 		"csk 0x%p, tid 0x%x, %u,%u,%u,%u,%u,%u,%u.\n",
4077b36b6e0Skxie@chelsio.com 		csk, csk->tid, 0, csk->tx_chan, csk->rss_qid,
4087b36b6e0Skxie@chelsio.com 		csk->snd_nxt, csk->rcv_nxt, cxgb4i_snd_win,
4097b36b6e0Skxie@chelsio.com 		csk->advmss);
4107b36b6e0Skxie@chelsio.com 
4117b36b6e0Skxie@chelsio.com 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
4127b36b6e0Skxie@chelsio.com }
4137b36b6e0Skxie@chelsio.com 
4147b36b6e0Skxie@chelsio.com static inline void make_tx_data_wr(struct cxgbi_sock *csk, struct sk_buff *skb,
4157b36b6e0Skxie@chelsio.com 				   int dlen, int len, u32 credits, int compl)
4167b36b6e0Skxie@chelsio.com {
4177b36b6e0Skxie@chelsio.com 	struct fw_ofld_tx_data_wr *req;
4187b36b6e0Skxie@chelsio.com 	unsigned int submode = cxgbi_skcb_ulp_mode(skb) & 3;
4197b36b6e0Skxie@chelsio.com 	unsigned int wr_ulp_mode = 0;
4207b36b6e0Skxie@chelsio.com 
4217b36b6e0Skxie@chelsio.com 	req = (struct fw_ofld_tx_data_wr *)__skb_push(skb, sizeof(*req));
4227b36b6e0Skxie@chelsio.com 
4237b36b6e0Skxie@chelsio.com 	if (is_ofld_imm(skb)) {
4247b36b6e0Skxie@chelsio.com 		req->op_to_immdlen = htonl(FW_WR_OP(FW_OFLD_TX_DATA_WR) |
4257b36b6e0Skxie@chelsio.com 					FW_WR_COMPL(1) |
4267b36b6e0Skxie@chelsio.com 					FW_WR_IMMDLEN(dlen));
4277b36b6e0Skxie@chelsio.com 		req->flowid_len16 = htonl(FW_WR_FLOWID(csk->tid) |
4287b36b6e0Skxie@chelsio.com 						FW_WR_LEN16(credits));
4297b36b6e0Skxie@chelsio.com 	} else {
4307b36b6e0Skxie@chelsio.com 		req->op_to_immdlen =
4317b36b6e0Skxie@chelsio.com 			cpu_to_be32(FW_WR_OP(FW_OFLD_TX_DATA_WR) |
4327b36b6e0Skxie@chelsio.com 					FW_WR_COMPL(1) |
4337b36b6e0Skxie@chelsio.com 					FW_WR_IMMDLEN(0));
4347b36b6e0Skxie@chelsio.com 		req->flowid_len16 =
4357b36b6e0Skxie@chelsio.com 			cpu_to_be32(FW_WR_FLOWID(csk->tid) |
4367b36b6e0Skxie@chelsio.com 					FW_WR_LEN16(credits));
4377b36b6e0Skxie@chelsio.com 	}
4387b36b6e0Skxie@chelsio.com 	if (submode)
4397b36b6e0Skxie@chelsio.com 		wr_ulp_mode = FW_OFLD_TX_DATA_WR_ULPMODE(ULP2_MODE_ISCSI) |
4407b36b6e0Skxie@chelsio.com 				FW_OFLD_TX_DATA_WR_ULPSUBMODE(submode);
4417b36b6e0Skxie@chelsio.com 	req->tunnel_to_proxy = htonl(wr_ulp_mode) |
4427b36b6e0Skxie@chelsio.com 		 FW_OFLD_TX_DATA_WR_SHOVE(skb_peek(&csk->write_queue) ? 0 : 1);
4437b36b6e0Skxie@chelsio.com 	req->plen = htonl(len);
4447b36b6e0Skxie@chelsio.com 	if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT))
4457b36b6e0Skxie@chelsio.com 		cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
4467b36b6e0Skxie@chelsio.com }
4477b36b6e0Skxie@chelsio.com 
4487b36b6e0Skxie@chelsio.com static void arp_failure_skb_discard(void *handle, struct sk_buff *skb)
4497b36b6e0Skxie@chelsio.com {
4507b36b6e0Skxie@chelsio.com 	kfree_skb(skb);
4517b36b6e0Skxie@chelsio.com }
4527b36b6e0Skxie@chelsio.com 
4537b36b6e0Skxie@chelsio.com static int push_tx_frames(struct cxgbi_sock *csk, int req_completion)
4547b36b6e0Skxie@chelsio.com {
4557b36b6e0Skxie@chelsio.com 	int total_size = 0;
4567b36b6e0Skxie@chelsio.com 	struct sk_buff *skb;
4577b36b6e0Skxie@chelsio.com 
4587b36b6e0Skxie@chelsio.com 	if (unlikely(csk->state < CTP_ESTABLISHED ||
4597b36b6e0Skxie@chelsio.com 		csk->state == CTP_CLOSE_WAIT_1 || csk->state >= CTP_ABORTING)) {
4607b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK |
4617b36b6e0Skxie@chelsio.com 			 1 << CXGBI_DBG_PDU_TX,
4627b36b6e0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, in closing state.\n",
4637b36b6e0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
4647b36b6e0Skxie@chelsio.com 		return 0;
4657b36b6e0Skxie@chelsio.com 	}
4667b36b6e0Skxie@chelsio.com 
4677b36b6e0Skxie@chelsio.com 	while (csk->wr_cred && (skb = skb_peek(&csk->write_queue)) != NULL) {
4687b36b6e0Skxie@chelsio.com 		int dlen = skb->len;
4697b36b6e0Skxie@chelsio.com 		int len = skb->len;
4707b36b6e0Skxie@chelsio.com 		unsigned int credits_needed;
4717b36b6e0Skxie@chelsio.com 
4727b36b6e0Skxie@chelsio.com 		skb_reset_transport_header(skb);
4737b36b6e0Skxie@chelsio.com 		if (is_ofld_imm(skb))
4747b36b6e0Skxie@chelsio.com 			credits_needed = DIV_ROUND_UP(dlen +
4757b36b6e0Skxie@chelsio.com 					sizeof(struct fw_ofld_tx_data_wr), 16);
4767b36b6e0Skxie@chelsio.com 		else
4777b36b6e0Skxie@chelsio.com 			credits_needed = DIV_ROUND_UP(8*calc_tx_flits_ofld(skb)
4787b36b6e0Skxie@chelsio.com 					+ sizeof(struct fw_ofld_tx_data_wr),
4797b36b6e0Skxie@chelsio.com 					16);
4807b36b6e0Skxie@chelsio.com 
4817b36b6e0Skxie@chelsio.com 		if (csk->wr_cred < credits_needed) {
4827b36b6e0Skxie@chelsio.com 			log_debug(1 << CXGBI_DBG_PDU_TX,
4837b36b6e0Skxie@chelsio.com 				"csk 0x%p, skb %u/%u, wr %d < %u.\n",
4847b36b6e0Skxie@chelsio.com 				csk, skb->len, skb->data_len,
4857b36b6e0Skxie@chelsio.com 				credits_needed, csk->wr_cred);
4867b36b6e0Skxie@chelsio.com 			break;
4877b36b6e0Skxie@chelsio.com 		}
4887b36b6e0Skxie@chelsio.com 		__skb_unlink(skb, &csk->write_queue);
4897b36b6e0Skxie@chelsio.com 		set_queue(skb, CPL_PRIORITY_DATA, csk);
4907b36b6e0Skxie@chelsio.com 		skb->csum = credits_needed;
4917b36b6e0Skxie@chelsio.com 		csk->wr_cred -= credits_needed;
4927b36b6e0Skxie@chelsio.com 		csk->wr_una_cred += credits_needed;
4937b36b6e0Skxie@chelsio.com 		cxgbi_sock_enqueue_wr(csk, skb);
4947b36b6e0Skxie@chelsio.com 
4957b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
4967b36b6e0Skxie@chelsio.com 			"csk 0x%p, skb %u/%u, wr %d, left %u, unack %u.\n",
4977b36b6e0Skxie@chelsio.com 			csk, skb->len, skb->data_len, credits_needed,
4987b36b6e0Skxie@chelsio.com 			csk->wr_cred, csk->wr_una_cred);
4997b36b6e0Skxie@chelsio.com 
5007b36b6e0Skxie@chelsio.com 		if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_NEED_HDR))) {
5017b36b6e0Skxie@chelsio.com 			if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
5027b36b6e0Skxie@chelsio.com 				send_tx_flowc_wr(csk);
5037b36b6e0Skxie@chelsio.com 				skb->csum += 5;
5047b36b6e0Skxie@chelsio.com 				csk->wr_cred -= 5;
5057b36b6e0Skxie@chelsio.com 				csk->wr_una_cred += 5;
5067b36b6e0Skxie@chelsio.com 			}
5077b36b6e0Skxie@chelsio.com 			len += cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
5087b36b6e0Skxie@chelsio.com 			make_tx_data_wr(csk, skb, dlen, len, credits_needed,
5097b36b6e0Skxie@chelsio.com 					req_completion);
5107b36b6e0Skxie@chelsio.com 			csk->snd_nxt += len;
5117b36b6e0Skxie@chelsio.com 			cxgbi_skcb_clear_flag(skb, SKCBF_TX_NEED_HDR);
5127b36b6e0Skxie@chelsio.com 		}
5137b36b6e0Skxie@chelsio.com 		total_size += skb->truesize;
5147b36b6e0Skxie@chelsio.com 		t4_set_arp_err_handler(skb, csk, arp_failure_skb_discard);
5157b36b6e0Skxie@chelsio.com 
5167b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_TX,
5177b36b6e0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, skb 0x%p, %u.\n",
5187b36b6e0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid, skb, len);
5197b36b6e0Skxie@chelsio.com 
5207b36b6e0Skxie@chelsio.com 		cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
5217b36b6e0Skxie@chelsio.com 	}
5227b36b6e0Skxie@chelsio.com 	return total_size;
5237b36b6e0Skxie@chelsio.com }
5247b36b6e0Skxie@chelsio.com 
5257b36b6e0Skxie@chelsio.com static inline void free_atid(struct cxgbi_sock *csk)
5267b36b6e0Skxie@chelsio.com {
5277b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
5287b36b6e0Skxie@chelsio.com 
5297b36b6e0Skxie@chelsio.com 	if (cxgbi_sock_flag(csk, CTPF_HAS_ATID)) {
5307b36b6e0Skxie@chelsio.com 		cxgb4_free_atid(lldi->tids, csk->atid);
5317b36b6e0Skxie@chelsio.com 		cxgbi_sock_clear_flag(csk, CTPF_HAS_ATID);
5327b36b6e0Skxie@chelsio.com 		cxgbi_sock_put(csk);
5337b36b6e0Skxie@chelsio.com 	}
5347b36b6e0Skxie@chelsio.com }
5357b36b6e0Skxie@chelsio.com 
5367b36b6e0Skxie@chelsio.com static void do_act_establish(struct cxgbi_device *cdev, struct sk_buff *skb)
5377b36b6e0Skxie@chelsio.com {
5387b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
5397b36b6e0Skxie@chelsio.com 	struct cpl_act_establish *req = (struct cpl_act_establish *)skb->data;
5407b36b6e0Skxie@chelsio.com 	unsigned short tcp_opt = ntohs(req->tcp_opt);
5417b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(req);
5427b36b6e0Skxie@chelsio.com 	unsigned int atid = GET_TID_TID(ntohl(req->tos_atid));
5437b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
5447b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
5457b36b6e0Skxie@chelsio.com 	u32 rcv_isn = be32_to_cpu(req->rcv_isn);
5467b36b6e0Skxie@chelsio.com 
5477b36b6e0Skxie@chelsio.com 	csk = lookup_atid(t, atid);
5487b36b6e0Skxie@chelsio.com 	if (unlikely(!csk)) {
5497b36b6e0Skxie@chelsio.com 		pr_err("NO conn. for atid %u, cdev 0x%p.\n", atid, cdev);
5507b36b6e0Skxie@chelsio.com 		goto rel_skb;
5517b36b6e0Skxie@chelsio.com 	}
5527b36b6e0Skxie@chelsio.com 
553e27d6169Skxie@chelsio.com 	if (csk->atid != atid) {
554e27d6169Skxie@chelsio.com 		pr_err("bad conn atid %u, csk 0x%p,%u,0x%lx,tid %u, atid %u.\n",
555e27d6169Skxie@chelsio.com 			atid, csk, csk->state, csk->flags, csk->tid, csk->atid);
556e27d6169Skxie@chelsio.com 		goto rel_skb;
557e27d6169Skxie@chelsio.com 	}
558e27d6169Skxie@chelsio.com 
5597b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
5607b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx, tid %u, atid %u, rseq %u.\n",
5617b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, tid, atid, rcv_isn);
5627b36b6e0Skxie@chelsio.com 
5637b36b6e0Skxie@chelsio.com 	cxgbi_sock_get(csk);
5647b36b6e0Skxie@chelsio.com 	csk->tid = tid;
5657b36b6e0Skxie@chelsio.com 	cxgb4_insert_tid(lldi->tids, csk, tid);
5667b36b6e0Skxie@chelsio.com 	cxgbi_sock_set_flag(csk, CTPF_HAS_TID);
5677b36b6e0Skxie@chelsio.com 
5687b36b6e0Skxie@chelsio.com 	free_atid(csk);
5697b36b6e0Skxie@chelsio.com 
5707b36b6e0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
5717b36b6e0Skxie@chelsio.com 	if (unlikely(csk->state != CTP_ACTIVE_OPEN))
5727b36b6e0Skxie@chelsio.com 		pr_info("csk 0x%p,%u,0x%lx,%u, got EST.\n",
5737b36b6e0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
5747b36b6e0Skxie@chelsio.com 
5757b36b6e0Skxie@chelsio.com 	if (csk->retry_timer.function) {
5767b36b6e0Skxie@chelsio.com 		del_timer(&csk->retry_timer);
5777b36b6e0Skxie@chelsio.com 		csk->retry_timer.function = NULL;
5787b36b6e0Skxie@chelsio.com 	}
5797b36b6e0Skxie@chelsio.com 
5807b36b6e0Skxie@chelsio.com 	csk->copied_seq = csk->rcv_wup = csk->rcv_nxt = rcv_isn;
5817b36b6e0Skxie@chelsio.com 	/*
5827b36b6e0Skxie@chelsio.com 	 * Causes the first RX_DATA_ACK to supply any Rx credits we couldn't
5837b36b6e0Skxie@chelsio.com 	 * pass through opt0.
5847b36b6e0Skxie@chelsio.com 	 */
5857b36b6e0Skxie@chelsio.com 	if (cxgb4i_rcv_win > (RCV_BUFSIZ_MASK << 10))
5867b36b6e0Skxie@chelsio.com 		csk->rcv_wup -= cxgb4i_rcv_win - (RCV_BUFSIZ_MASK << 10);
5877b36b6e0Skxie@chelsio.com 
5887b36b6e0Skxie@chelsio.com 	csk->advmss = lldi->mtus[GET_TCPOPT_MSS(tcp_opt)] - 40;
5897b36b6e0Skxie@chelsio.com 	if (GET_TCPOPT_TSTAMP(tcp_opt))
5907b36b6e0Skxie@chelsio.com 		csk->advmss -= 12;
5917b36b6e0Skxie@chelsio.com 	if (csk->advmss < 128)
5927b36b6e0Skxie@chelsio.com 		csk->advmss = 128;
5937b36b6e0Skxie@chelsio.com 
5947b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
5957b36b6e0Skxie@chelsio.com 		"csk 0x%p, mss_idx %u, advmss %u.\n",
5967b36b6e0Skxie@chelsio.com 			csk, GET_TCPOPT_MSS(tcp_opt), csk->advmss);
5977b36b6e0Skxie@chelsio.com 
5987b36b6e0Skxie@chelsio.com 	cxgbi_sock_established(csk, ntohl(req->snd_isn), ntohs(req->tcp_opt));
5997b36b6e0Skxie@chelsio.com 
6007b36b6e0Skxie@chelsio.com 	if (unlikely(cxgbi_sock_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED)))
6017b36b6e0Skxie@chelsio.com 		send_abort_req(csk);
6027b36b6e0Skxie@chelsio.com 	else {
6037b36b6e0Skxie@chelsio.com 		if (skb_queue_len(&csk->write_queue))
6047b36b6e0Skxie@chelsio.com 			push_tx_frames(csk, 0);
6057b36b6e0Skxie@chelsio.com 		cxgbi_conn_tx_open(csk);
6067b36b6e0Skxie@chelsio.com 	}
6077b36b6e0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
6087b36b6e0Skxie@chelsio.com 
6097b36b6e0Skxie@chelsio.com rel_skb:
6107b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
6117b36b6e0Skxie@chelsio.com }
6127b36b6e0Skxie@chelsio.com 
6137b36b6e0Skxie@chelsio.com static int act_open_rpl_status_to_errno(int status)
6147b36b6e0Skxie@chelsio.com {
6157b36b6e0Skxie@chelsio.com 	switch (status) {
6167b36b6e0Skxie@chelsio.com 	case CPL_ERR_CONN_RESET:
6177b36b6e0Skxie@chelsio.com 		return -ECONNREFUSED;
6187b36b6e0Skxie@chelsio.com 	case CPL_ERR_ARP_MISS:
6197b36b6e0Skxie@chelsio.com 		return -EHOSTUNREACH;
6207b36b6e0Skxie@chelsio.com 	case CPL_ERR_CONN_TIMEDOUT:
6217b36b6e0Skxie@chelsio.com 		return -ETIMEDOUT;
6227b36b6e0Skxie@chelsio.com 	case CPL_ERR_TCAM_FULL:
6237b36b6e0Skxie@chelsio.com 		return -ENOMEM;
6247b36b6e0Skxie@chelsio.com 	case CPL_ERR_CONN_EXIST:
6257b36b6e0Skxie@chelsio.com 		return -EADDRINUSE;
6267b36b6e0Skxie@chelsio.com 	default:
6277b36b6e0Skxie@chelsio.com 		return -EIO;
6287b36b6e0Skxie@chelsio.com 	}
6297b36b6e0Skxie@chelsio.com }
6307b36b6e0Skxie@chelsio.com 
6317b36b6e0Skxie@chelsio.com static void csk_act_open_retry_timer(unsigned long data)
6327b36b6e0Skxie@chelsio.com {
6337b36b6e0Skxie@chelsio.com 	struct sk_buff *skb;
6347b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk = (struct cxgbi_sock *)data;
6357b36b6e0Skxie@chelsio.com 
6367b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
6377b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx,%u.\n",
6387b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid);
6397b36b6e0Skxie@chelsio.com 
6407b36b6e0Skxie@chelsio.com 	cxgbi_sock_get(csk);
6417b36b6e0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
64224d3f95aSkxie@chelsio.com 	skb = alloc_wr(sizeof(struct cpl_act_open_req), 0, GFP_ATOMIC);
6437b36b6e0Skxie@chelsio.com 	if (!skb)
6447b36b6e0Skxie@chelsio.com 		cxgbi_sock_fail_act_open(csk, -ENOMEM);
6457b36b6e0Skxie@chelsio.com 	else {
6467b36b6e0Skxie@chelsio.com 		skb->sk = (struct sock *)csk;
6477b36b6e0Skxie@chelsio.com 		t4_set_arp_err_handler(skb, csk,
6487b36b6e0Skxie@chelsio.com 					cxgbi_sock_act_open_req_arp_failure);
6497b36b6e0Skxie@chelsio.com 		send_act_open_req(csk, skb, csk->l2t);
6507b36b6e0Skxie@chelsio.com 	}
6517b36b6e0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
6527b36b6e0Skxie@chelsio.com 	cxgbi_sock_put(csk);
6537b36b6e0Skxie@chelsio.com }
6547b36b6e0Skxie@chelsio.com 
6557b36b6e0Skxie@chelsio.com static void do_act_open_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
6567b36b6e0Skxie@chelsio.com {
6577b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
6587b36b6e0Skxie@chelsio.com 	struct cpl_act_open_rpl *rpl = (struct cpl_act_open_rpl *)skb->data;
6597b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(rpl);
6607b36b6e0Skxie@chelsio.com 	unsigned int atid =
6617b36b6e0Skxie@chelsio.com 		GET_TID_TID(GET_AOPEN_ATID(be32_to_cpu(rpl->atid_status)));
6627b36b6e0Skxie@chelsio.com 	unsigned int status = GET_AOPEN_STATUS(be32_to_cpu(rpl->atid_status));
6637b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
6647b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
6657b36b6e0Skxie@chelsio.com 
6667b36b6e0Skxie@chelsio.com 	csk = lookup_atid(t, atid);
6677b36b6e0Skxie@chelsio.com 	if (unlikely(!csk)) {
6687b36b6e0Skxie@chelsio.com 		pr_err("NO matching conn. atid %u, tid %u.\n", atid, tid);
6697b36b6e0Skxie@chelsio.com 		goto rel_skb;
6707b36b6e0Skxie@chelsio.com 	}
6717b36b6e0Skxie@chelsio.com 
672e27d6169Skxie@chelsio.com 	pr_info("%pI4:%u-%pI4:%u, atid %u,%u, status %u, csk 0x%p,%u,0x%lx.\n",
673e27d6169Skxie@chelsio.com 		&csk->saddr.sin_addr.s_addr, ntohs(csk->saddr.sin_port),
674e27d6169Skxie@chelsio.com 		&csk->daddr.sin_addr.s_addr, ntohs(csk->daddr.sin_port),
675e27d6169Skxie@chelsio.com 		atid, tid, status, csk, csk->state, csk->flags);
6767b36b6e0Skxie@chelsio.com 
677150cca7cSKaren Xie 	if (status == CPL_ERR_RTX_NEG_ADVICE)
678150cca7cSKaren Xie 		goto rel_skb;
679150cca7cSKaren Xie 
6807b36b6e0Skxie@chelsio.com 	if (status && status != CPL_ERR_TCAM_FULL &&
6817b36b6e0Skxie@chelsio.com 	    status != CPL_ERR_CONN_EXIST &&
6827b36b6e0Skxie@chelsio.com 	    status != CPL_ERR_ARP_MISS)
6837b36b6e0Skxie@chelsio.com 		cxgb4_remove_tid(lldi->tids, csk->port_id, GET_TID(rpl));
6847b36b6e0Skxie@chelsio.com 
6857b36b6e0Skxie@chelsio.com 	cxgbi_sock_get(csk);
6867b36b6e0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
6877b36b6e0Skxie@chelsio.com 
6887b36b6e0Skxie@chelsio.com 	if (status == CPL_ERR_CONN_EXIST &&
6897b36b6e0Skxie@chelsio.com 	    csk->retry_timer.function != csk_act_open_retry_timer) {
6907b36b6e0Skxie@chelsio.com 		csk->retry_timer.function = csk_act_open_retry_timer;
6917b36b6e0Skxie@chelsio.com 		mod_timer(&csk->retry_timer, jiffies + HZ / 2);
6927b36b6e0Skxie@chelsio.com 	} else
6937b36b6e0Skxie@chelsio.com 		cxgbi_sock_fail_act_open(csk,
6947b36b6e0Skxie@chelsio.com 					act_open_rpl_status_to_errno(status));
6957b36b6e0Skxie@chelsio.com 
6967b36b6e0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
6977b36b6e0Skxie@chelsio.com 	cxgbi_sock_put(csk);
6987b36b6e0Skxie@chelsio.com rel_skb:
6997b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
7007b36b6e0Skxie@chelsio.com }
7017b36b6e0Skxie@chelsio.com 
7027b36b6e0Skxie@chelsio.com static void do_peer_close(struct cxgbi_device *cdev, struct sk_buff *skb)
7037b36b6e0Skxie@chelsio.com {
7047b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
7057b36b6e0Skxie@chelsio.com 	struct cpl_peer_close *req = (struct cpl_peer_close *)skb->data;
7067b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(req);
7077b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
7087b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
7097b36b6e0Skxie@chelsio.com 
7107b36b6e0Skxie@chelsio.com 	csk = lookup_tid(t, tid);
7117b36b6e0Skxie@chelsio.com 	if (unlikely(!csk)) {
7127b36b6e0Skxie@chelsio.com 		pr_err("can't find connection for tid %u.\n", tid);
7137b36b6e0Skxie@chelsio.com 		goto rel_skb;
7147b36b6e0Skxie@chelsio.com 	}
7157b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
7167b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx,%u.\n",
7177b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid);
7187b36b6e0Skxie@chelsio.com 	cxgbi_sock_rcv_peer_close(csk);
7197b36b6e0Skxie@chelsio.com rel_skb:
7207b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
7217b36b6e0Skxie@chelsio.com }
7227b36b6e0Skxie@chelsio.com 
7237b36b6e0Skxie@chelsio.com static void do_close_con_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
7247b36b6e0Skxie@chelsio.com {
7257b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
7267b36b6e0Skxie@chelsio.com 	struct cpl_close_con_rpl *rpl = (struct cpl_close_con_rpl *)skb->data;
7277b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(rpl);
7287b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
7297b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
7307b36b6e0Skxie@chelsio.com 
7317b36b6e0Skxie@chelsio.com 	csk = lookup_tid(t, tid);
7327b36b6e0Skxie@chelsio.com 	if (unlikely(!csk)) {
7337b36b6e0Skxie@chelsio.com 		pr_err("can't find connection for tid %u.\n", tid);
7347b36b6e0Skxie@chelsio.com 		goto rel_skb;
7357b36b6e0Skxie@chelsio.com 	}
7367b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
7377b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx,%u.\n",
7387b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid);
7397b36b6e0Skxie@chelsio.com 	cxgbi_sock_rcv_close_conn_rpl(csk, ntohl(rpl->snd_nxt));
7407b36b6e0Skxie@chelsio.com rel_skb:
7417b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
7427b36b6e0Skxie@chelsio.com }
7437b36b6e0Skxie@chelsio.com 
7447b36b6e0Skxie@chelsio.com static int abort_status_to_errno(struct cxgbi_sock *csk, int abort_reason,
7457b36b6e0Skxie@chelsio.com 								int *need_rst)
7467b36b6e0Skxie@chelsio.com {
7477b36b6e0Skxie@chelsio.com 	switch (abort_reason) {
7487b36b6e0Skxie@chelsio.com 	case CPL_ERR_BAD_SYN: /* fall through */
7497b36b6e0Skxie@chelsio.com 	case CPL_ERR_CONN_RESET:
7507b36b6e0Skxie@chelsio.com 		return csk->state > CTP_ESTABLISHED ?
7517b36b6e0Skxie@chelsio.com 			-EPIPE : -ECONNRESET;
7527b36b6e0Skxie@chelsio.com 	case CPL_ERR_XMIT_TIMEDOUT:
7537b36b6e0Skxie@chelsio.com 	case CPL_ERR_PERSIST_TIMEDOUT:
7547b36b6e0Skxie@chelsio.com 	case CPL_ERR_FINWAIT2_TIMEDOUT:
7557b36b6e0Skxie@chelsio.com 	case CPL_ERR_KEEPALIVE_TIMEDOUT:
7567b36b6e0Skxie@chelsio.com 		return -ETIMEDOUT;
7577b36b6e0Skxie@chelsio.com 	default:
7587b36b6e0Skxie@chelsio.com 		return -EIO;
7597b36b6e0Skxie@chelsio.com 	}
7607b36b6e0Skxie@chelsio.com }
7617b36b6e0Skxie@chelsio.com 
7627b36b6e0Skxie@chelsio.com static void do_abort_req_rss(struct cxgbi_device *cdev, struct sk_buff *skb)
7637b36b6e0Skxie@chelsio.com {
7647b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
7657b36b6e0Skxie@chelsio.com 	struct cpl_abort_req_rss *req = (struct cpl_abort_req_rss *)skb->data;
7667b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(req);
7677b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
7687b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
7697b36b6e0Skxie@chelsio.com 	int rst_status = CPL_ABORT_NO_RST;
7707b36b6e0Skxie@chelsio.com 
7717b36b6e0Skxie@chelsio.com 	csk = lookup_tid(t, tid);
7727b36b6e0Skxie@chelsio.com 	if (unlikely(!csk)) {
7737b36b6e0Skxie@chelsio.com 		pr_err("can't find connection for tid %u.\n", tid);
7747b36b6e0Skxie@chelsio.com 		goto rel_skb;
7757b36b6e0Skxie@chelsio.com 	}
7767b36b6e0Skxie@chelsio.com 
7777b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
7787b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx, tid %u, status 0x%x.\n",
7797b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid, req->status);
7807b36b6e0Skxie@chelsio.com 
7817b36b6e0Skxie@chelsio.com 	if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
7827b36b6e0Skxie@chelsio.com 	    req->status == CPL_ERR_PERSIST_NEG_ADVICE)
7837b36b6e0Skxie@chelsio.com 		goto rel_skb;
7847b36b6e0Skxie@chelsio.com 
7857b36b6e0Skxie@chelsio.com 	cxgbi_sock_get(csk);
7867b36b6e0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
7877b36b6e0Skxie@chelsio.com 
7887b36b6e0Skxie@chelsio.com 	if (!cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD)) {
7897b36b6e0Skxie@chelsio.com 		cxgbi_sock_set_flag(csk, CTPF_ABORT_REQ_RCVD);
7907b36b6e0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_ABORTING);
7917b36b6e0Skxie@chelsio.com 		goto done;
7927b36b6e0Skxie@chelsio.com 	}
7937b36b6e0Skxie@chelsio.com 
7947b36b6e0Skxie@chelsio.com 	cxgbi_sock_clear_flag(csk, CTPF_ABORT_REQ_RCVD);
7957b36b6e0Skxie@chelsio.com 	send_abort_rpl(csk, rst_status);
7967b36b6e0Skxie@chelsio.com 
7977b36b6e0Skxie@chelsio.com 	if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
7987b36b6e0Skxie@chelsio.com 		csk->err = abort_status_to_errno(csk, req->status, &rst_status);
7997b36b6e0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
8007b36b6e0Skxie@chelsio.com 	}
8017b36b6e0Skxie@chelsio.com done:
8027b36b6e0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
8037b36b6e0Skxie@chelsio.com 	cxgbi_sock_put(csk);
8047b36b6e0Skxie@chelsio.com rel_skb:
8057b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
8067b36b6e0Skxie@chelsio.com }
8077b36b6e0Skxie@chelsio.com 
8087b36b6e0Skxie@chelsio.com static void do_abort_rpl_rss(struct cxgbi_device *cdev, struct sk_buff *skb)
8097b36b6e0Skxie@chelsio.com {
8107b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
8117b36b6e0Skxie@chelsio.com 	struct cpl_abort_rpl_rss *rpl = (struct cpl_abort_rpl_rss *)skb->data;
8127b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(rpl);
8137b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
8147b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
8157b36b6e0Skxie@chelsio.com 
8167b36b6e0Skxie@chelsio.com 	csk = lookup_tid(t, tid);
8177b36b6e0Skxie@chelsio.com 	if (!csk)
8187b36b6e0Skxie@chelsio.com 		goto rel_skb;
8197b36b6e0Skxie@chelsio.com 
8207b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
8217b36b6e0Skxie@chelsio.com 		"status 0x%x, csk 0x%p, s %u, 0x%lx.\n",
8227b36b6e0Skxie@chelsio.com 		rpl->status, csk, csk ? csk->state : 0,
8237b36b6e0Skxie@chelsio.com 		csk ? csk->flags : 0UL);
8247b36b6e0Skxie@chelsio.com 
8257b36b6e0Skxie@chelsio.com 	if (rpl->status == CPL_ERR_ABORT_FAILED)
8267b36b6e0Skxie@chelsio.com 		goto rel_skb;
8277b36b6e0Skxie@chelsio.com 
8287b36b6e0Skxie@chelsio.com 	cxgbi_sock_rcv_abort_rpl(csk);
8297b36b6e0Skxie@chelsio.com rel_skb:
8307b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
8317b36b6e0Skxie@chelsio.com }
8327b36b6e0Skxie@chelsio.com 
8337b36b6e0Skxie@chelsio.com static void do_rx_iscsi_hdr(struct cxgbi_device *cdev, struct sk_buff *skb)
8347b36b6e0Skxie@chelsio.com {
8357b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
8367b36b6e0Skxie@chelsio.com 	struct cpl_iscsi_hdr *cpl = (struct cpl_iscsi_hdr *)skb->data;
8377b36b6e0Skxie@chelsio.com 	unsigned short pdu_len_ddp = be16_to_cpu(cpl->pdu_len_ddp);
8387b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(cpl);
8397b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
8407b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
8417b36b6e0Skxie@chelsio.com 
8427b36b6e0Skxie@chelsio.com 	csk = lookup_tid(t, tid);
8437b36b6e0Skxie@chelsio.com 	if (unlikely(!csk)) {
8447b36b6e0Skxie@chelsio.com 		pr_err("can't find conn. for tid %u.\n", tid);
8457b36b6e0Skxie@chelsio.com 		goto rel_skb;
8467b36b6e0Skxie@chelsio.com 	}
8477b36b6e0Skxie@chelsio.com 
8487b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
8497b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx, tid %u, skb 0x%p,%u, 0x%x.\n",
8507b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid, skb, skb->len,
8517b36b6e0Skxie@chelsio.com 		pdu_len_ddp);
8527b36b6e0Skxie@chelsio.com 
8537b36b6e0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
8547b36b6e0Skxie@chelsio.com 
8557b36b6e0Skxie@chelsio.com 	if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
8567b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
8577b36b6e0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, bad state.\n",
8587b36b6e0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
8597b36b6e0Skxie@chelsio.com 		if (csk->state != CTP_ABORTING)
8607b36b6e0Skxie@chelsio.com 			goto abort_conn;
8617b36b6e0Skxie@chelsio.com 		else
8627b36b6e0Skxie@chelsio.com 			goto discard;
8637b36b6e0Skxie@chelsio.com 	}
8647b36b6e0Skxie@chelsio.com 
8657b36b6e0Skxie@chelsio.com 	cxgbi_skcb_tcp_seq(skb) = ntohl(cpl->seq);
866e27d6169Skxie@chelsio.com 	cxgbi_skcb_flags(skb) = 0;
867e27d6169Skxie@chelsio.com 
8687b36b6e0Skxie@chelsio.com 	skb_reset_transport_header(skb);
8697b36b6e0Skxie@chelsio.com 	__skb_pull(skb, sizeof(*cpl));
8707b36b6e0Skxie@chelsio.com 	__pskb_trim(skb, ntohs(cpl->len));
8717b36b6e0Skxie@chelsio.com 
8727b36b6e0Skxie@chelsio.com 	if (!csk->skb_ulp_lhdr) {
8737b36b6e0Skxie@chelsio.com 		unsigned char *bhs;
8747b36b6e0Skxie@chelsio.com 		unsigned int hlen, dlen;
8757b36b6e0Skxie@chelsio.com 
8767b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
8777b36b6e0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx, tid %u, skb 0x%p header.\n",
8787b36b6e0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid, skb);
8797b36b6e0Skxie@chelsio.com 		csk->skb_ulp_lhdr = skb;
880e27d6169Skxie@chelsio.com 		cxgbi_skcb_set_flag(skb, SKCBF_RX_HDR);
8817b36b6e0Skxie@chelsio.com 
882e27d6169Skxie@chelsio.com 		if (cxgbi_skcb_tcp_seq(skb) != csk->rcv_nxt) {
8837b36b6e0Skxie@chelsio.com 			pr_info("tid %u, CPL_ISCSI_HDR, bad seq, 0x%x/0x%x.\n",
884e27d6169Skxie@chelsio.com 				csk->tid, cxgbi_skcb_tcp_seq(skb),
8857b36b6e0Skxie@chelsio.com 				csk->rcv_nxt);
8867b36b6e0Skxie@chelsio.com 			goto abort_conn;
8877b36b6e0Skxie@chelsio.com 		}
8887b36b6e0Skxie@chelsio.com 
889e27d6169Skxie@chelsio.com 		bhs = skb->data;
8907b36b6e0Skxie@chelsio.com 		hlen = ntohs(cpl->len);
8917b36b6e0Skxie@chelsio.com 		dlen = ntohl(*(unsigned int *)(bhs + 4)) & 0xFFFFFF;
8927b36b6e0Skxie@chelsio.com 
8937b36b6e0Skxie@chelsio.com 		if ((hlen + dlen) != ISCSI_PDU_LEN(pdu_len_ddp) - 40) {
8947b36b6e0Skxie@chelsio.com 			pr_info("tid 0x%x, CPL_ISCSI_HDR, pdu len "
8957b36b6e0Skxie@chelsio.com 				"mismatch %u != %u + %u, seq 0x%x.\n",
8967b36b6e0Skxie@chelsio.com 				csk->tid, ISCSI_PDU_LEN(pdu_len_ddp) - 40,
8977b36b6e0Skxie@chelsio.com 				hlen, dlen, cxgbi_skcb_tcp_seq(skb));
8987b36b6e0Skxie@chelsio.com 			goto abort_conn;
8997b36b6e0Skxie@chelsio.com 		}
9007b36b6e0Skxie@chelsio.com 
9017b36b6e0Skxie@chelsio.com 		cxgbi_skcb_rx_pdulen(skb) = (hlen + dlen + 3) & (~0x3);
9027b36b6e0Skxie@chelsio.com 		if (dlen)
9037b36b6e0Skxie@chelsio.com 			cxgbi_skcb_rx_pdulen(skb) += csk->dcrc_len;
9047b36b6e0Skxie@chelsio.com 		csk->rcv_nxt += cxgbi_skcb_rx_pdulen(skb);
9057b36b6e0Skxie@chelsio.com 
9067b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
9077b36b6e0Skxie@chelsio.com 			"csk 0x%p, skb 0x%p, 0x%x,%u+%u,0x%x,0x%x.\n",
9087b36b6e0Skxie@chelsio.com 			csk, skb, *bhs, hlen, dlen,
9097b36b6e0Skxie@chelsio.com 			ntohl(*((unsigned int *)(bhs + 16))),
9107b36b6e0Skxie@chelsio.com 			ntohl(*((unsigned int *)(bhs + 24))));
9117b36b6e0Skxie@chelsio.com 
9127b36b6e0Skxie@chelsio.com 	} else {
913e27d6169Skxie@chelsio.com 		struct sk_buff *lskb = csk->skb_ulp_lhdr;
9147b36b6e0Skxie@chelsio.com 
915e27d6169Skxie@chelsio.com 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_DATA);
9167b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
9177b36b6e0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx, skb 0x%p data, 0x%p.\n",
9187b36b6e0Skxie@chelsio.com 			csk, csk->state, csk->flags, skb, lskb);
9197b36b6e0Skxie@chelsio.com 	}
9207b36b6e0Skxie@chelsio.com 
9217b36b6e0Skxie@chelsio.com 	__skb_queue_tail(&csk->receive_queue, skb);
9227b36b6e0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9237b36b6e0Skxie@chelsio.com 	return;
9247b36b6e0Skxie@chelsio.com 
9257b36b6e0Skxie@chelsio.com abort_conn:
9267b36b6e0Skxie@chelsio.com 	send_abort_req(csk);
9277b36b6e0Skxie@chelsio.com discard:
9287b36b6e0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9297b36b6e0Skxie@chelsio.com rel_skb:
9307b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
9317b36b6e0Skxie@chelsio.com }
9327b36b6e0Skxie@chelsio.com 
9337b36b6e0Skxie@chelsio.com static void do_rx_data_ddp(struct cxgbi_device *cdev,
9347b36b6e0Skxie@chelsio.com 				  struct sk_buff *skb)
9357b36b6e0Skxie@chelsio.com {
9367b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
9377b36b6e0Skxie@chelsio.com 	struct sk_buff *lskb;
9387b36b6e0Skxie@chelsio.com 	struct cpl_rx_data_ddp *rpl = (struct cpl_rx_data_ddp *)skb->data;
9397b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(rpl);
9407b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
9417b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
9427b36b6e0Skxie@chelsio.com 	unsigned int status = ntohl(rpl->ddpvld);
9437b36b6e0Skxie@chelsio.com 
9447b36b6e0Skxie@chelsio.com 	csk = lookup_tid(t, tid);
9457b36b6e0Skxie@chelsio.com 	if (unlikely(!csk)) {
9467b36b6e0Skxie@chelsio.com 		pr_err("can't find connection for tid %u.\n", tid);
9477b36b6e0Skxie@chelsio.com 		goto rel_skb;
9487b36b6e0Skxie@chelsio.com 	}
9497b36b6e0Skxie@chelsio.com 
9507b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
9517b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx, skb 0x%p,0x%x, lhdr 0x%p.\n",
9527b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, skb, status, csk->skb_ulp_lhdr);
9537b36b6e0Skxie@chelsio.com 
9547b36b6e0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9557b36b6e0Skxie@chelsio.com 
9567b36b6e0Skxie@chelsio.com 	if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
9577b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
9587b36b6e0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, bad state.\n",
9597b36b6e0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
9607b36b6e0Skxie@chelsio.com 		if (csk->state != CTP_ABORTING)
9617b36b6e0Skxie@chelsio.com 			goto abort_conn;
9627b36b6e0Skxie@chelsio.com 		else
9637b36b6e0Skxie@chelsio.com 			goto discard;
9647b36b6e0Skxie@chelsio.com 	}
9657b36b6e0Skxie@chelsio.com 
9667b36b6e0Skxie@chelsio.com 	if (!csk->skb_ulp_lhdr) {
9677b36b6e0Skxie@chelsio.com 		pr_err("tid 0x%x, rcv RX_DATA_DDP w/o pdu bhs.\n", csk->tid);
9687b36b6e0Skxie@chelsio.com 		goto abort_conn;
9697b36b6e0Skxie@chelsio.com 	}
9707b36b6e0Skxie@chelsio.com 
9717b36b6e0Skxie@chelsio.com 	lskb = csk->skb_ulp_lhdr;
9727b36b6e0Skxie@chelsio.com 	csk->skb_ulp_lhdr = NULL;
9737b36b6e0Skxie@chelsio.com 
9747b36b6e0Skxie@chelsio.com 	cxgbi_skcb_rx_ddigest(lskb) = ntohl(rpl->ulp_crc);
9757b36b6e0Skxie@chelsio.com 
9767b36b6e0Skxie@chelsio.com 	if (ntohs(rpl->len) != cxgbi_skcb_rx_pdulen(lskb))
9777b36b6e0Skxie@chelsio.com 		pr_info("tid 0x%x, RX_DATA_DDP pdulen %u != %u.\n",
9787b36b6e0Skxie@chelsio.com 			csk->tid, ntohs(rpl->len), cxgbi_skcb_rx_pdulen(lskb));
9797b36b6e0Skxie@chelsio.com 
9807b36b6e0Skxie@chelsio.com 	if (status & (1 << CPL_RX_DDP_STATUS_HCRC_SHIFT)) {
981e27d6169Skxie@chelsio.com 		pr_info("csk 0x%p, lhdr 0x%p, status 0x%x, hcrc bad 0x%lx.\n",
982e27d6169Skxie@chelsio.com 			csk, lskb, status, cxgbi_skcb_flags(lskb));
9837b36b6e0Skxie@chelsio.com 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_HCRC_ERR);
9847b36b6e0Skxie@chelsio.com 	}
9857b36b6e0Skxie@chelsio.com 	if (status & (1 << CPL_RX_DDP_STATUS_DCRC_SHIFT)) {
986e27d6169Skxie@chelsio.com 		pr_info("csk 0x%p, lhdr 0x%p, status 0x%x, dcrc bad 0x%lx.\n",
987e27d6169Skxie@chelsio.com 			csk, lskb, status, cxgbi_skcb_flags(lskb));
9887b36b6e0Skxie@chelsio.com 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_DCRC_ERR);
9897b36b6e0Skxie@chelsio.com 	}
9907b36b6e0Skxie@chelsio.com 	if (status & (1 << CPL_RX_DDP_STATUS_PAD_SHIFT)) {
9917b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
9927b36b6e0Skxie@chelsio.com 			"csk 0x%p, lhdr 0x%p, status 0x%x, pad bad.\n",
9937b36b6e0Skxie@chelsio.com 			csk, lskb, status);
9947b36b6e0Skxie@chelsio.com 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_PAD_ERR);
9957b36b6e0Skxie@chelsio.com 	}
9967b36b6e0Skxie@chelsio.com 	if ((status & (1 << CPL_RX_DDP_STATUS_DDP_SHIFT)) &&
9977b36b6e0Skxie@chelsio.com 		!cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA)) {
9987b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
9997b36b6e0Skxie@chelsio.com 			"csk 0x%p, lhdr 0x%p, 0x%x, data ddp'ed.\n",
10007b36b6e0Skxie@chelsio.com 			csk, lskb, status);
10017b36b6e0Skxie@chelsio.com 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_DATA_DDPD);
10027b36b6e0Skxie@chelsio.com 	}
10037b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
10047b36b6e0Skxie@chelsio.com 		"csk 0x%p, lskb 0x%p, f 0x%lx.\n",
10057b36b6e0Skxie@chelsio.com 		csk, lskb, cxgbi_skcb_flags(lskb));
10067b36b6e0Skxie@chelsio.com 
1007e27d6169Skxie@chelsio.com 	cxgbi_skcb_set_flag(lskb, SKCBF_RX_STATUS);
10087b36b6e0Skxie@chelsio.com 	cxgbi_conn_pdu_ready(csk);
10097b36b6e0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
10107b36b6e0Skxie@chelsio.com 	goto rel_skb;
10117b36b6e0Skxie@chelsio.com 
10127b36b6e0Skxie@chelsio.com abort_conn:
10137b36b6e0Skxie@chelsio.com 	send_abort_req(csk);
10147b36b6e0Skxie@chelsio.com discard:
10157b36b6e0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
10167b36b6e0Skxie@chelsio.com rel_skb:
10177b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
10187b36b6e0Skxie@chelsio.com }
10197b36b6e0Skxie@chelsio.com 
10207b36b6e0Skxie@chelsio.com static void do_fw4_ack(struct cxgbi_device *cdev, struct sk_buff *skb)
10217b36b6e0Skxie@chelsio.com {
10227b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
10237b36b6e0Skxie@chelsio.com 	struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)skb->data;
10247b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(rpl);
10257b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
10267b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
10277b36b6e0Skxie@chelsio.com 
10287b36b6e0Skxie@chelsio.com 	csk = lookup_tid(t, tid);
10297b36b6e0Skxie@chelsio.com 	if (unlikely(!csk))
10307b36b6e0Skxie@chelsio.com 		pr_err("can't find connection for tid %u.\n", tid);
10317b36b6e0Skxie@chelsio.com 	else {
10327b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
10337b36b6e0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u.\n",
10347b36b6e0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
10357b36b6e0Skxie@chelsio.com 		cxgbi_sock_rcv_wr_ack(csk, rpl->credits, ntohl(rpl->snd_una),
10367b36b6e0Skxie@chelsio.com 					rpl->seq_vld);
10377b36b6e0Skxie@chelsio.com 	}
10387b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
10397b36b6e0Skxie@chelsio.com }
10407b36b6e0Skxie@chelsio.com 
10417b36b6e0Skxie@chelsio.com static void do_set_tcb_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
10427b36b6e0Skxie@chelsio.com {
10437b36b6e0Skxie@chelsio.com 	struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
10447b36b6e0Skxie@chelsio.com 	unsigned int tid = GET_TID(rpl);
10457b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
10467b36b6e0Skxie@chelsio.com 	struct tid_info *t = lldi->tids;
10477b36b6e0Skxie@chelsio.com 	struct cxgbi_sock *csk;
10487b36b6e0Skxie@chelsio.com 
10497b36b6e0Skxie@chelsio.com 	csk = lookup_tid(t, tid);
10507b36b6e0Skxie@chelsio.com 	if (!csk)
10517b36b6e0Skxie@chelsio.com 		pr_err("can't find conn. for tid %u.\n", tid);
10527b36b6e0Skxie@chelsio.com 
10537b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
10547b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,%lx,%u, status 0x%x.\n",
10557b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid, rpl->status);
10567b36b6e0Skxie@chelsio.com 
10577b36b6e0Skxie@chelsio.com 	if (rpl->status != CPL_ERR_NONE)
10587b36b6e0Skxie@chelsio.com 		pr_err("csk 0x%p,%u, SET_TCB_RPL status %u.\n",
10597b36b6e0Skxie@chelsio.com 			csk, tid, rpl->status);
10607b36b6e0Skxie@chelsio.com 
10617b36b6e0Skxie@chelsio.com 	__kfree_skb(skb);
10627b36b6e0Skxie@chelsio.com }
10637b36b6e0Skxie@chelsio.com 
10647b36b6e0Skxie@chelsio.com static int alloc_cpls(struct cxgbi_sock *csk)
10657b36b6e0Skxie@chelsio.com {
106624d3f95aSkxie@chelsio.com 	csk->cpl_close = alloc_wr(sizeof(struct cpl_close_con_req),
106724d3f95aSkxie@chelsio.com 					0, GFP_KERNEL);
10687b36b6e0Skxie@chelsio.com 	if (!csk->cpl_close)
10697b36b6e0Skxie@chelsio.com 		return -ENOMEM;
10707b36b6e0Skxie@chelsio.com 
107124d3f95aSkxie@chelsio.com 	csk->cpl_abort_req = alloc_wr(sizeof(struct cpl_abort_req),
107224d3f95aSkxie@chelsio.com 					0, GFP_KERNEL);
10737b36b6e0Skxie@chelsio.com 	if (!csk->cpl_abort_req)
10747b36b6e0Skxie@chelsio.com 		goto free_cpls;
10757b36b6e0Skxie@chelsio.com 
107624d3f95aSkxie@chelsio.com 	csk->cpl_abort_rpl = alloc_wr(sizeof(struct cpl_abort_rpl),
107724d3f95aSkxie@chelsio.com 					0, GFP_KERNEL);
10787b36b6e0Skxie@chelsio.com 	if (!csk->cpl_abort_rpl)
10797b36b6e0Skxie@chelsio.com 		goto free_cpls;
10807b36b6e0Skxie@chelsio.com 	return 0;
10817b36b6e0Skxie@chelsio.com 
10827b36b6e0Skxie@chelsio.com free_cpls:
10837b36b6e0Skxie@chelsio.com 	cxgbi_sock_free_cpl_skbs(csk);
10847b36b6e0Skxie@chelsio.com 	return -ENOMEM;
10857b36b6e0Skxie@chelsio.com }
10867b36b6e0Skxie@chelsio.com 
10877b36b6e0Skxie@chelsio.com static inline void l2t_put(struct cxgbi_sock *csk)
10887b36b6e0Skxie@chelsio.com {
10897b36b6e0Skxie@chelsio.com 	if (csk->l2t) {
10907b36b6e0Skxie@chelsio.com 		cxgb4_l2t_release(csk->l2t);
10917b36b6e0Skxie@chelsio.com 		csk->l2t = NULL;
10927b36b6e0Skxie@chelsio.com 		cxgbi_sock_put(csk);
10937b36b6e0Skxie@chelsio.com 	}
10947b36b6e0Skxie@chelsio.com }
10957b36b6e0Skxie@chelsio.com 
10967b36b6e0Skxie@chelsio.com static void release_offload_resources(struct cxgbi_sock *csk)
10977b36b6e0Skxie@chelsio.com {
10987b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi;
10997b36b6e0Skxie@chelsio.com 
11007b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
11017b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx,%u.\n",
11027b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid);
11037b36b6e0Skxie@chelsio.com 
11047b36b6e0Skxie@chelsio.com 	cxgbi_sock_free_cpl_skbs(csk);
11057b36b6e0Skxie@chelsio.com 	if (csk->wr_cred != csk->wr_max_cred) {
11067b36b6e0Skxie@chelsio.com 		cxgbi_sock_purge_wr_queue(csk);
11077b36b6e0Skxie@chelsio.com 		cxgbi_sock_reset_wr_list(csk);
11087b36b6e0Skxie@chelsio.com 	}
11097b36b6e0Skxie@chelsio.com 
11107b36b6e0Skxie@chelsio.com 	l2t_put(csk);
11117b36b6e0Skxie@chelsio.com 	if (cxgbi_sock_flag(csk, CTPF_HAS_ATID))
11127b36b6e0Skxie@chelsio.com 		free_atid(csk);
11137b36b6e0Skxie@chelsio.com 	else if (cxgbi_sock_flag(csk, CTPF_HAS_TID)) {
11147b36b6e0Skxie@chelsio.com 		lldi = cxgbi_cdev_priv(csk->cdev);
11157b36b6e0Skxie@chelsio.com 		cxgb4_remove_tid(lldi->tids, 0, csk->tid);
11167b36b6e0Skxie@chelsio.com 		cxgbi_sock_clear_flag(csk, CTPF_HAS_TID);
11177b36b6e0Skxie@chelsio.com 		cxgbi_sock_put(csk);
11187b36b6e0Skxie@chelsio.com 	}
11197b36b6e0Skxie@chelsio.com 	csk->dst = NULL;
11207b36b6e0Skxie@chelsio.com 	csk->cdev = NULL;
11217b36b6e0Skxie@chelsio.com }
11227b36b6e0Skxie@chelsio.com 
11237b36b6e0Skxie@chelsio.com static int init_act_open(struct cxgbi_sock *csk)
11247b36b6e0Skxie@chelsio.com {
11257b36b6e0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
11267b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
11277b36b6e0Skxie@chelsio.com 	struct net_device *ndev = cdev->ports[csk->port_id];
11287b36b6e0Skxie@chelsio.com 	struct port_info *pi = netdev_priv(ndev);
11297b36b6e0Skxie@chelsio.com 	struct sk_buff *skb = NULL;
113051e059bdSDavid Miller 	struct neighbour *n;
11317b36b6e0Skxie@chelsio.com 	unsigned int step;
11327b36b6e0Skxie@chelsio.com 
11337b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
11347b36b6e0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lx,%u.\n",
11357b36b6e0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid);
11367b36b6e0Skxie@chelsio.com 
11377b36b6e0Skxie@chelsio.com 	csk->atid = cxgb4_alloc_atid(lldi->tids, csk);
11387b36b6e0Skxie@chelsio.com 	if (csk->atid < 0) {
11397b36b6e0Skxie@chelsio.com 		pr_err("%s, NO atid available.\n", ndev->name);
11407b36b6e0Skxie@chelsio.com 		return -EINVAL;
11417b36b6e0Skxie@chelsio.com 	}
11427b36b6e0Skxie@chelsio.com 	cxgbi_sock_set_flag(csk, CTPF_HAS_ATID);
11437b36b6e0Skxie@chelsio.com 	cxgbi_sock_get(csk);
11447b36b6e0Skxie@chelsio.com 
1145c4737377SDavid S. Miller 	n = dst_neigh_lookup(csk->dst, &csk->daddr.sin_addr.s_addr);
114651e059bdSDavid Miller 	if (!n) {
114751e059bdSDavid Miller 		pr_err("%s, can't get neighbour of csk->dst.\n", ndev->name);
114851e059bdSDavid Miller 		goto rel_resource;
114951e059bdSDavid Miller 	}
115051e059bdSDavid Miller 	csk->l2t = cxgb4_l2t_get(lldi->l2t, n, ndev, 0);
11517b36b6e0Skxie@chelsio.com 	if (!csk->l2t) {
11527b36b6e0Skxie@chelsio.com 		pr_err("%s, cannot alloc l2t.\n", ndev->name);
11537b36b6e0Skxie@chelsio.com 		goto rel_resource;
11547b36b6e0Skxie@chelsio.com 	}
11557b36b6e0Skxie@chelsio.com 	cxgbi_sock_get(csk);
11567b36b6e0Skxie@chelsio.com 
115724d3f95aSkxie@chelsio.com 	skb = alloc_wr(sizeof(struct cpl_act_open_req), 0, GFP_KERNEL);
11587b36b6e0Skxie@chelsio.com 	if (!skb)
11597b36b6e0Skxie@chelsio.com 		goto rel_resource;
11607b36b6e0Skxie@chelsio.com 	skb->sk = (struct sock *)csk;
11617b36b6e0Skxie@chelsio.com 	t4_set_arp_err_handler(skb, csk, cxgbi_sock_act_open_req_arp_failure);
11627b36b6e0Skxie@chelsio.com 
11637b36b6e0Skxie@chelsio.com 	if (!csk->mtu)
11647b36b6e0Skxie@chelsio.com 		csk->mtu = dst_mtu(csk->dst);
11657b36b6e0Skxie@chelsio.com 	cxgb4_best_mtu(lldi->mtus, csk->mtu, &csk->mss_idx);
11667b36b6e0Skxie@chelsio.com 	csk->tx_chan = cxgb4_port_chan(ndev);
11677b36b6e0Skxie@chelsio.com 	/* SMT two entries per row */
11687b36b6e0Skxie@chelsio.com 	csk->smac_idx = ((cxgb4_port_viid(ndev) & 0x7F)) << 1;
11697b36b6e0Skxie@chelsio.com 	step = lldi->ntxq / lldi->nchan;
11707b36b6e0Skxie@chelsio.com 	csk->txq_idx = cxgb4_port_idx(ndev) * step;
11717b36b6e0Skxie@chelsio.com 	step = lldi->nrxq / lldi->nchan;
11727b36b6e0Skxie@chelsio.com 	csk->rss_qid = lldi->rxq_ids[cxgb4_port_idx(ndev) * step];
11737b36b6e0Skxie@chelsio.com 	csk->wr_max_cred = csk->wr_cred = lldi->wr_cred;
11747b36b6e0Skxie@chelsio.com 	csk->wr_una_cred = 0;
11757b36b6e0Skxie@chelsio.com 	cxgbi_sock_reset_wr_list(csk);
11767b36b6e0Skxie@chelsio.com 	csk->err = 0;
11777b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
11787b36b6e0Skxie@chelsio.com 		"csk 0x%p,p%d,%s, %u,%u,%u, mss %u,%u, smac %u.\n",
11797b36b6e0Skxie@chelsio.com 		csk, pi->port_id, ndev->name, csk->tx_chan,
11807b36b6e0Skxie@chelsio.com 		csk->txq_idx, csk->rss_qid, csk->mtu, csk->mss_idx,
11817b36b6e0Skxie@chelsio.com 		csk->smac_idx);
11827b36b6e0Skxie@chelsio.com 
11837b36b6e0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_ACTIVE_OPEN);
11847b36b6e0Skxie@chelsio.com 	send_act_open_req(csk, skb, csk->l2t);
1185c4737377SDavid S. Miller 	neigh_release(n);
11867b36b6e0Skxie@chelsio.com 	return 0;
11877b36b6e0Skxie@chelsio.com 
11887b36b6e0Skxie@chelsio.com rel_resource:
1189c4737377SDavid S. Miller 	if (n)
1190c4737377SDavid S. Miller 		neigh_release(n);
11917b36b6e0Skxie@chelsio.com 	if (skb)
11927b36b6e0Skxie@chelsio.com 		__kfree_skb(skb);
11937b36b6e0Skxie@chelsio.com 	return -EINVAL;
11947b36b6e0Skxie@chelsio.com }
11957b36b6e0Skxie@chelsio.com 
11967b36b6e0Skxie@chelsio.com cxgb4i_cplhandler_func cxgb4i_cplhandlers[NUM_CPL_CMDS] = {
11977b36b6e0Skxie@chelsio.com 	[CPL_ACT_ESTABLISH] = do_act_establish,
11987b36b6e0Skxie@chelsio.com 	[CPL_ACT_OPEN_RPL] = do_act_open_rpl,
11997b36b6e0Skxie@chelsio.com 	[CPL_PEER_CLOSE] = do_peer_close,
12007b36b6e0Skxie@chelsio.com 	[CPL_ABORT_REQ_RSS] = do_abort_req_rss,
12017b36b6e0Skxie@chelsio.com 	[CPL_ABORT_RPL_RSS] = do_abort_rpl_rss,
12027b36b6e0Skxie@chelsio.com 	[CPL_CLOSE_CON_RPL] = do_close_con_rpl,
12037b36b6e0Skxie@chelsio.com 	[CPL_FW4_ACK] = do_fw4_ack,
12047b36b6e0Skxie@chelsio.com 	[CPL_ISCSI_HDR] = do_rx_iscsi_hdr,
12057b36b6e0Skxie@chelsio.com 	[CPL_SET_TCB_RPL] = do_set_tcb_rpl,
12067b36b6e0Skxie@chelsio.com 	[CPL_RX_DATA_DDP] = do_rx_data_ddp,
12077b36b6e0Skxie@chelsio.com };
12087b36b6e0Skxie@chelsio.com 
12097b36b6e0Skxie@chelsio.com int cxgb4i_ofld_init(struct cxgbi_device *cdev)
12107b36b6e0Skxie@chelsio.com {
12117b36b6e0Skxie@chelsio.com 	int rc;
12127b36b6e0Skxie@chelsio.com 
12137b36b6e0Skxie@chelsio.com 	if (cxgb4i_max_connect > CXGB4I_MAX_CONN)
12147b36b6e0Skxie@chelsio.com 		cxgb4i_max_connect = CXGB4I_MAX_CONN;
12157b36b6e0Skxie@chelsio.com 
12167b36b6e0Skxie@chelsio.com 	rc = cxgbi_device_portmap_create(cdev, cxgb4i_sport_base,
12177b36b6e0Skxie@chelsio.com 					cxgb4i_max_connect);
12187b36b6e0Skxie@chelsio.com 	if (rc < 0)
12197b36b6e0Skxie@chelsio.com 		return rc;
12207b36b6e0Skxie@chelsio.com 
12217b36b6e0Skxie@chelsio.com 	cdev->csk_release_offload_resources = release_offload_resources;
12227b36b6e0Skxie@chelsio.com 	cdev->csk_push_tx_frames = push_tx_frames;
12237b36b6e0Skxie@chelsio.com 	cdev->csk_send_abort_req = send_abort_req;
12247b36b6e0Skxie@chelsio.com 	cdev->csk_send_close_req = send_close_req;
12257b36b6e0Skxie@chelsio.com 	cdev->csk_send_rx_credits = send_rx_credits;
12267b36b6e0Skxie@chelsio.com 	cdev->csk_alloc_cpls = alloc_cpls;
12277b36b6e0Skxie@chelsio.com 	cdev->csk_init_act_open = init_act_open;
12287b36b6e0Skxie@chelsio.com 
12297b36b6e0Skxie@chelsio.com 	pr_info("cdev 0x%p, offload up, added.\n", cdev);
12307b36b6e0Skxie@chelsio.com 	return 0;
12317b36b6e0Skxie@chelsio.com }
12327b36b6e0Skxie@chelsio.com 
12337b36b6e0Skxie@chelsio.com /*
12347b36b6e0Skxie@chelsio.com  * functions to program the pagepod in h/w
12357b36b6e0Skxie@chelsio.com  */
1236e27d6169Skxie@chelsio.com #define ULPMEM_IDATA_MAX_NPPODS	4 /* 256/PPOD_SIZE */
12377b36b6e0Skxie@chelsio.com static inline void ulp_mem_io_set_hdr(struct ulp_mem_io *req,
1238e27d6169Skxie@chelsio.com 				unsigned int wr_len, unsigned int dlen,
1239e27d6169Skxie@chelsio.com 				unsigned int pm_addr)
12407b36b6e0Skxie@chelsio.com {
1241e27d6169Skxie@chelsio.com 	struct ulptx_idata *idata = (struct ulptx_idata *)(req + 1);
12427b36b6e0Skxie@chelsio.com 
12437b36b6e0Skxie@chelsio.com 	INIT_ULPTX_WR(req, wr_len, 0, 0);
1244e27d6169Skxie@chelsio.com 	req->cmd = htonl(ULPTX_CMD(ULP_TX_MEM_WRITE) | (1 << 23));
12457b36b6e0Skxie@chelsio.com 	req->dlen = htonl(ULP_MEMIO_DATA_LEN(dlen >> 5));
12467b36b6e0Skxie@chelsio.com 	req->lock_addr = htonl(ULP_MEMIO_ADDR(pm_addr >> 5));
12477b36b6e0Skxie@chelsio.com 	req->len16 = htonl(DIV_ROUND_UP(wr_len - sizeof(req->wr), 16));
1248e27d6169Skxie@chelsio.com 
1249e27d6169Skxie@chelsio.com 	idata->cmd_more = htonl(ULPTX_CMD(ULP_TX_SC_IMM));
1250e27d6169Skxie@chelsio.com 	idata->len = htonl(dlen);
12517b36b6e0Skxie@chelsio.com }
12527b36b6e0Skxie@chelsio.com 
1253e27d6169Skxie@chelsio.com static int ddp_ppod_write_idata(struct cxgbi_device *cdev, unsigned int port_id,
12547b36b6e0Skxie@chelsio.com 				struct cxgbi_pagepod_hdr *hdr, unsigned int idx,
12557b36b6e0Skxie@chelsio.com 				unsigned int npods,
12567b36b6e0Skxie@chelsio.com 				struct cxgbi_gather_list *gl,
12577b36b6e0Skxie@chelsio.com 				unsigned int gl_pidx)
12587b36b6e0Skxie@chelsio.com {
12597b36b6e0Skxie@chelsio.com 	struct cxgbi_ddp_info *ddp = cdev->ddp;
12607b36b6e0Skxie@chelsio.com 	struct sk_buff *skb;
12617b36b6e0Skxie@chelsio.com 	struct ulp_mem_io *req;
1262e27d6169Skxie@chelsio.com 	struct ulptx_idata *idata;
12637b36b6e0Skxie@chelsio.com 	struct cxgbi_pagepod *ppod;
1264e27d6169Skxie@chelsio.com 	unsigned int pm_addr = idx * PPOD_SIZE + ddp->llimit;
1265e27d6169Skxie@chelsio.com 	unsigned int dlen = PPOD_SIZE * npods;
1266e27d6169Skxie@chelsio.com 	unsigned int wr_len = roundup(sizeof(struct ulp_mem_io) +
1267e27d6169Skxie@chelsio.com 				sizeof(struct ulptx_idata) + dlen, 16);
12687b36b6e0Skxie@chelsio.com 	unsigned int i;
12697b36b6e0Skxie@chelsio.com 
1270e27d6169Skxie@chelsio.com 	skb = alloc_wr(wr_len, 0, GFP_ATOMIC);
12717b36b6e0Skxie@chelsio.com 	if (!skb) {
12727b36b6e0Skxie@chelsio.com 		pr_err("cdev 0x%p, idx %u, npods %u, OOM.\n",
12737b36b6e0Skxie@chelsio.com 			cdev, idx, npods);
12747b36b6e0Skxie@chelsio.com 		return -ENOMEM;
12757b36b6e0Skxie@chelsio.com 	}
12767b36b6e0Skxie@chelsio.com 	req = (struct ulp_mem_io *)skb->head;
12777b36b6e0Skxie@chelsio.com 	set_queue(skb, CPL_PRIORITY_CONTROL, NULL);
12787b36b6e0Skxie@chelsio.com 
1279e27d6169Skxie@chelsio.com 	ulp_mem_io_set_hdr(req, wr_len, dlen, pm_addr);
1280e27d6169Skxie@chelsio.com 	idata = (struct ulptx_idata *)(req + 1);
1281e27d6169Skxie@chelsio.com 	ppod = (struct cxgbi_pagepod *)(idata + 1);
12827b36b6e0Skxie@chelsio.com 
12837b36b6e0Skxie@chelsio.com 	for (i = 0; i < npods; i++, ppod++, gl_pidx += PPOD_PAGES_MAX) {
12847b36b6e0Skxie@chelsio.com 		if (!hdr && !gl)
12857b36b6e0Skxie@chelsio.com 			cxgbi_ddp_ppod_clear(ppod);
12867b36b6e0Skxie@chelsio.com 		else
12877b36b6e0Skxie@chelsio.com 			cxgbi_ddp_ppod_set(ppod, hdr, gl, gl_pidx);
12887b36b6e0Skxie@chelsio.com 	}
12897b36b6e0Skxie@chelsio.com 
12907b36b6e0Skxie@chelsio.com 	cxgb4_ofld_send(cdev->ports[port_id], skb);
12917b36b6e0Skxie@chelsio.com 	return 0;
12927b36b6e0Skxie@chelsio.com }
12937b36b6e0Skxie@chelsio.com 
12947b36b6e0Skxie@chelsio.com static int ddp_set_map(struct cxgbi_sock *csk, struct cxgbi_pagepod_hdr *hdr,
12957b36b6e0Skxie@chelsio.com 			unsigned int idx, unsigned int npods,
12967b36b6e0Skxie@chelsio.com 			struct cxgbi_gather_list *gl)
12977b36b6e0Skxie@chelsio.com {
12987b36b6e0Skxie@chelsio.com 	unsigned int i, cnt;
12997b36b6e0Skxie@chelsio.com 	int err = 0;
13007b36b6e0Skxie@chelsio.com 
13017b36b6e0Skxie@chelsio.com 	for (i = 0; i < npods; i += cnt, idx += cnt) {
13027b36b6e0Skxie@chelsio.com 		cnt = npods - i;
1303e27d6169Skxie@chelsio.com 		if (cnt > ULPMEM_IDATA_MAX_NPPODS)
1304e27d6169Skxie@chelsio.com 			cnt = ULPMEM_IDATA_MAX_NPPODS;
1305e27d6169Skxie@chelsio.com 		err = ddp_ppod_write_idata(csk->cdev, csk->port_id, hdr,
13067b36b6e0Skxie@chelsio.com 					idx, cnt, gl, 4 * i);
13077b36b6e0Skxie@chelsio.com 		if (err < 0)
13087b36b6e0Skxie@chelsio.com 			break;
13097b36b6e0Skxie@chelsio.com 	}
13107b36b6e0Skxie@chelsio.com 	return err;
13117b36b6e0Skxie@chelsio.com }
13127b36b6e0Skxie@chelsio.com 
13137b36b6e0Skxie@chelsio.com static void ddp_clear_map(struct cxgbi_hba *chba, unsigned int tag,
13147b36b6e0Skxie@chelsio.com 			  unsigned int idx, unsigned int npods)
13157b36b6e0Skxie@chelsio.com {
13167b36b6e0Skxie@chelsio.com 	unsigned int i, cnt;
13177b36b6e0Skxie@chelsio.com 	int err;
13187b36b6e0Skxie@chelsio.com 
13197b36b6e0Skxie@chelsio.com 	for (i = 0; i < npods; i += cnt, idx += cnt) {
13207b36b6e0Skxie@chelsio.com 		cnt = npods - i;
1321e27d6169Skxie@chelsio.com 		if (cnt > ULPMEM_IDATA_MAX_NPPODS)
1322e27d6169Skxie@chelsio.com 			cnt = ULPMEM_IDATA_MAX_NPPODS;
1323e27d6169Skxie@chelsio.com 		err = ddp_ppod_write_idata(chba->cdev, chba->port_id, NULL,
13247b36b6e0Skxie@chelsio.com 					idx, cnt, NULL, 0);
13257b36b6e0Skxie@chelsio.com 		if (err < 0)
13267b36b6e0Skxie@chelsio.com 			break;
13277b36b6e0Skxie@chelsio.com 	}
13287b36b6e0Skxie@chelsio.com }
13297b36b6e0Skxie@chelsio.com 
13307b36b6e0Skxie@chelsio.com static int ddp_setup_conn_pgidx(struct cxgbi_sock *csk, unsigned int tid,
13317b36b6e0Skxie@chelsio.com 				int pg_idx, bool reply)
13327b36b6e0Skxie@chelsio.com {
13337b36b6e0Skxie@chelsio.com 	struct sk_buff *skb;
13347b36b6e0Skxie@chelsio.com 	struct cpl_set_tcb_field *req;
13357b36b6e0Skxie@chelsio.com 
1336e27d6169Skxie@chelsio.com 	if (!pg_idx || pg_idx >= DDP_PGIDX_MAX)
13377b36b6e0Skxie@chelsio.com 		return 0;
13387b36b6e0Skxie@chelsio.com 
133924d3f95aSkxie@chelsio.com 	skb = alloc_wr(sizeof(*req), 0, GFP_KERNEL);
13407b36b6e0Skxie@chelsio.com 	if (!skb)
13417b36b6e0Skxie@chelsio.com 		return -ENOMEM;
13427b36b6e0Skxie@chelsio.com 
1343e27d6169Skxie@chelsio.com 	/*  set up ulp page size */
13447b36b6e0Skxie@chelsio.com 	req = (struct cpl_set_tcb_field *)skb->head;
13457b36b6e0Skxie@chelsio.com 	INIT_TP_WR(req, csk->tid);
13467b36b6e0Skxie@chelsio.com 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
13477b36b6e0Skxie@chelsio.com 	req->reply_ctrl = htons(NO_REPLY(reply) | QUEUENO(csk->rss_qid));
1348e27d6169Skxie@chelsio.com 	req->word_cookie = htons(0);
1349e27d6169Skxie@chelsio.com 	req->mask = cpu_to_be64(0x3 << 8);
1350e27d6169Skxie@chelsio.com 	req->val = cpu_to_be64(pg_idx << 8);
13517b36b6e0Skxie@chelsio.com 	set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->port_id);
13527b36b6e0Skxie@chelsio.com 
13537b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
13547b36b6e0Skxie@chelsio.com 		"csk 0x%p, tid 0x%x, pg_idx %u.\n", csk, csk->tid, pg_idx);
13557b36b6e0Skxie@chelsio.com 
13567b36b6e0Skxie@chelsio.com 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
13577b36b6e0Skxie@chelsio.com 	return 0;
13587b36b6e0Skxie@chelsio.com }
13597b36b6e0Skxie@chelsio.com 
13607b36b6e0Skxie@chelsio.com static int ddp_setup_conn_digest(struct cxgbi_sock *csk, unsigned int tid,
13617b36b6e0Skxie@chelsio.com 				 int hcrc, int dcrc, int reply)
13627b36b6e0Skxie@chelsio.com {
13637b36b6e0Skxie@chelsio.com 	struct sk_buff *skb;
13647b36b6e0Skxie@chelsio.com 	struct cpl_set_tcb_field *req;
13657b36b6e0Skxie@chelsio.com 
1366e27d6169Skxie@chelsio.com 	if (!hcrc && !dcrc)
1367e27d6169Skxie@chelsio.com 		return 0;
13687b36b6e0Skxie@chelsio.com 
136924d3f95aSkxie@chelsio.com 	skb = alloc_wr(sizeof(*req), 0, GFP_KERNEL);
13707b36b6e0Skxie@chelsio.com 	if (!skb)
13717b36b6e0Skxie@chelsio.com 		return -ENOMEM;
13727b36b6e0Skxie@chelsio.com 
13737b36b6e0Skxie@chelsio.com 	csk->hcrc_len = (hcrc ? 4 : 0);
13747b36b6e0Skxie@chelsio.com 	csk->dcrc_len = (dcrc ? 4 : 0);
1375e27d6169Skxie@chelsio.com 	/*  set up ulp submode */
13767b36b6e0Skxie@chelsio.com 	req = (struct cpl_set_tcb_field *)skb->head;
13777b36b6e0Skxie@chelsio.com 	INIT_TP_WR(req, tid);
13787b36b6e0Skxie@chelsio.com 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
13797b36b6e0Skxie@chelsio.com 	req->reply_ctrl = htons(NO_REPLY(reply) | QUEUENO(csk->rss_qid));
1380e27d6169Skxie@chelsio.com 	req->word_cookie = htons(0);
1381e27d6169Skxie@chelsio.com 	req->mask = cpu_to_be64(0x3 << 4);
1382e27d6169Skxie@chelsio.com 	req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
1383e27d6169Skxie@chelsio.com 				(dcrc ? ULP_CRC_DATA : 0)) << 4);
13847b36b6e0Skxie@chelsio.com 	set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->port_id);
13857b36b6e0Skxie@chelsio.com 
13867b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
13877b36b6e0Skxie@chelsio.com 		"csk 0x%p, tid 0x%x, crc %d,%d.\n", csk, csk->tid, hcrc, dcrc);
13887b36b6e0Skxie@chelsio.com 
13897b36b6e0Skxie@chelsio.com 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
13907b36b6e0Skxie@chelsio.com 	return 0;
13917b36b6e0Skxie@chelsio.com }
13927b36b6e0Skxie@chelsio.com 
13937b36b6e0Skxie@chelsio.com static int cxgb4i_ddp_init(struct cxgbi_device *cdev)
13947b36b6e0Skxie@chelsio.com {
13957b36b6e0Skxie@chelsio.com 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
13967b36b6e0Skxie@chelsio.com 	struct cxgbi_ddp_info *ddp = cdev->ddp;
13977b36b6e0Skxie@chelsio.com 	unsigned int tagmask, pgsz_factor[4];
13987b36b6e0Skxie@chelsio.com 	int err;
13997b36b6e0Skxie@chelsio.com 
14007b36b6e0Skxie@chelsio.com 	if (ddp) {
14017b36b6e0Skxie@chelsio.com 		kref_get(&ddp->refcnt);
14027b36b6e0Skxie@chelsio.com 		pr_warn("cdev 0x%p, ddp 0x%p already set up.\n",
14037b36b6e0Skxie@chelsio.com 			cdev, cdev->ddp);
14047b36b6e0Skxie@chelsio.com 		return -EALREADY;
14057b36b6e0Skxie@chelsio.com 	}
14067b36b6e0Skxie@chelsio.com 
14077b36b6e0Skxie@chelsio.com 	err = cxgbi_ddp_init(cdev, lldi->vr->iscsi.start,
14087b36b6e0Skxie@chelsio.com 			lldi->vr->iscsi.start + lldi->vr->iscsi.size - 1,
14097b36b6e0Skxie@chelsio.com 			lldi->iscsi_iolen, lldi->iscsi_iolen);
14107b36b6e0Skxie@chelsio.com 	if (err < 0)
14117b36b6e0Skxie@chelsio.com 		return err;
14127b36b6e0Skxie@chelsio.com 
14137b36b6e0Skxie@chelsio.com 	ddp = cdev->ddp;
14147b36b6e0Skxie@chelsio.com 
14157b36b6e0Skxie@chelsio.com 	tagmask = ddp->idx_mask << PPOD_IDX_SHIFT;
14167b36b6e0Skxie@chelsio.com 	cxgbi_ddp_page_size_factor(pgsz_factor);
14177b36b6e0Skxie@chelsio.com 	cxgb4_iscsi_init(lldi->ports[0], tagmask, pgsz_factor);
14187b36b6e0Skxie@chelsio.com 
14197b36b6e0Skxie@chelsio.com 	cdev->csk_ddp_setup_digest = ddp_setup_conn_digest;
14207b36b6e0Skxie@chelsio.com 	cdev->csk_ddp_setup_pgidx = ddp_setup_conn_pgidx;
14217b36b6e0Skxie@chelsio.com 	cdev->csk_ddp_set = ddp_set_map;
14227b36b6e0Skxie@chelsio.com 	cdev->csk_ddp_clear = ddp_clear_map;
14237b36b6e0Skxie@chelsio.com 
14247b36b6e0Skxie@chelsio.com 	pr_info("cxgb4i 0x%p tag: sw %u, rsvd %u,%u, mask 0x%x.\n",
14257b36b6e0Skxie@chelsio.com 		cdev, cdev->tag_format.sw_bits, cdev->tag_format.rsvd_bits,
14267b36b6e0Skxie@chelsio.com 		cdev->tag_format.rsvd_shift, cdev->tag_format.rsvd_mask);
14277b36b6e0Skxie@chelsio.com 	pr_info("cxgb4i 0x%p, nppods %u, bits %u, mask 0x%x,0x%x pkt %u/%u, "
14287b36b6e0Skxie@chelsio.com 		" %u/%u.\n",
14297b36b6e0Skxie@chelsio.com 		cdev, ddp->nppods, ddp->idx_bits, ddp->idx_mask,
14307b36b6e0Skxie@chelsio.com 		ddp->rsvd_tag_mask, ddp->max_txsz, lldi->iscsi_iolen,
14317b36b6e0Skxie@chelsio.com 		ddp->max_rxsz, lldi->iscsi_iolen);
14327b36b6e0Skxie@chelsio.com 	pr_info("cxgb4i 0x%p max payload size: %u/%u, %u/%u.\n",
14337b36b6e0Skxie@chelsio.com 		cdev, cdev->tx_max_size, ddp->max_txsz, cdev->rx_max_size,
14347b36b6e0Skxie@chelsio.com 		ddp->max_rxsz);
14357b36b6e0Skxie@chelsio.com 	return 0;
14367b36b6e0Skxie@chelsio.com }
14377b36b6e0Skxie@chelsio.com 
14387b36b6e0Skxie@chelsio.com static void *t4_uld_add(const struct cxgb4_lld_info *lldi)
14397b36b6e0Skxie@chelsio.com {
14407b36b6e0Skxie@chelsio.com 	struct cxgbi_device *cdev;
14417b36b6e0Skxie@chelsio.com 	struct port_info *pi;
14427b36b6e0Skxie@chelsio.com 	int i, rc;
14437b36b6e0Skxie@chelsio.com 
14447b36b6e0Skxie@chelsio.com 	cdev = cxgbi_device_register(sizeof(*lldi), lldi->nports);
14457b36b6e0Skxie@chelsio.com 	if (!cdev) {
14467b36b6e0Skxie@chelsio.com 		pr_info("t4 device 0x%p, register failed.\n", lldi);
14477b36b6e0Skxie@chelsio.com 		return NULL;
14487b36b6e0Skxie@chelsio.com 	}
14497b36b6e0Skxie@chelsio.com 	pr_info("0x%p,0x%x, ports %u,%s, chan %u, q %u,%u, wr %u.\n",
14507b36b6e0Skxie@chelsio.com 		cdev, lldi->adapter_type, lldi->nports,
14517b36b6e0Skxie@chelsio.com 		lldi->ports[0]->name, lldi->nchan, lldi->ntxq,
14527b36b6e0Skxie@chelsio.com 		lldi->nrxq, lldi->wr_cred);
14537b36b6e0Skxie@chelsio.com 	for (i = 0; i < lldi->nrxq; i++)
14547b36b6e0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DEV,
14557b36b6e0Skxie@chelsio.com 			"t4 0x%p, rxq id #%d: %u.\n",
14567b36b6e0Skxie@chelsio.com 			cdev, i, lldi->rxq_ids[i]);
14577b36b6e0Skxie@chelsio.com 
14587b36b6e0Skxie@chelsio.com 	memcpy(cxgbi_cdev_priv(cdev), lldi, sizeof(*lldi));
14597b36b6e0Skxie@chelsio.com 	cdev->flags = CXGBI_FLAG_DEV_T4;
14607b36b6e0Skxie@chelsio.com 	cdev->pdev = lldi->pdev;
14617b36b6e0Skxie@chelsio.com 	cdev->ports = lldi->ports;
14627b36b6e0Skxie@chelsio.com 	cdev->nports = lldi->nports;
14637b36b6e0Skxie@chelsio.com 	cdev->mtus = lldi->mtus;
14647b36b6e0Skxie@chelsio.com 	cdev->nmtus = NMTUS;
14657b36b6e0Skxie@chelsio.com 	cdev->snd_win = cxgb4i_snd_win;
14667b36b6e0Skxie@chelsio.com 	cdev->rcv_win = cxgb4i_rcv_win;
14677b36b6e0Skxie@chelsio.com 	cdev->rx_credit_thres = cxgb4i_rx_credit_thres;
14687b36b6e0Skxie@chelsio.com 	cdev->skb_tx_rsvd = CXGB4I_TX_HEADER_LEN;
14697b36b6e0Skxie@chelsio.com 	cdev->skb_rx_extra = sizeof(struct cpl_iscsi_hdr);
14707b36b6e0Skxie@chelsio.com 	cdev->itp = &cxgb4i_iscsi_transport;
14717b36b6e0Skxie@chelsio.com 
1472e27d6169Skxie@chelsio.com 	cdev->pfvf = FW_VIID_PFN_GET(cxgb4_port_viid(lldi->ports[0])) << 8;
1473e27d6169Skxie@chelsio.com 	pr_info("cdev 0x%p,%s, pfvf %u.\n",
1474e27d6169Skxie@chelsio.com 		cdev, lldi->ports[0]->name, cdev->pfvf);
1475e27d6169Skxie@chelsio.com 
14767b36b6e0Skxie@chelsio.com 	rc = cxgb4i_ddp_init(cdev);
14777b36b6e0Skxie@chelsio.com 	if (rc) {
14787b36b6e0Skxie@chelsio.com 		pr_info("t4 0x%p ddp init failed.\n", cdev);
14797b36b6e0Skxie@chelsio.com 		goto err_out;
14807b36b6e0Skxie@chelsio.com 	}
14817b36b6e0Skxie@chelsio.com 	rc = cxgb4i_ofld_init(cdev);
14827b36b6e0Skxie@chelsio.com 	if (rc) {
14837b36b6e0Skxie@chelsio.com 		pr_info("t4 0x%p ofld init failed.\n", cdev);
14847b36b6e0Skxie@chelsio.com 		goto err_out;
14857b36b6e0Skxie@chelsio.com 	}
14867b36b6e0Skxie@chelsio.com 
14877b36b6e0Skxie@chelsio.com 	rc = cxgbi_hbas_add(cdev, CXGB4I_MAX_LUN, CXGBI_MAX_CONN,
14887b36b6e0Skxie@chelsio.com 				&cxgb4i_host_template, cxgb4i_stt);
14897b36b6e0Skxie@chelsio.com 	if (rc)
14907b36b6e0Skxie@chelsio.com 		goto err_out;
14917b36b6e0Skxie@chelsio.com 
14927b36b6e0Skxie@chelsio.com 	for (i = 0; i < cdev->nports; i++) {
14937b36b6e0Skxie@chelsio.com 		pi = netdev_priv(lldi->ports[i]);
14947b36b6e0Skxie@chelsio.com 		cdev->hbas[i]->port_id = pi->port_id;
14957b36b6e0Skxie@chelsio.com 	}
14967b36b6e0Skxie@chelsio.com 	return cdev;
14977b36b6e0Skxie@chelsio.com 
14987b36b6e0Skxie@chelsio.com err_out:
14997b36b6e0Skxie@chelsio.com 	cxgbi_device_unregister(cdev);
15007b36b6e0Skxie@chelsio.com 	return ERR_PTR(-ENOMEM);
15017b36b6e0Skxie@chelsio.com }
15027b36b6e0Skxie@chelsio.com 
15037b36b6e0Skxie@chelsio.com #define RX_PULL_LEN	128
15047b36b6e0Skxie@chelsio.com static int t4_uld_rx_handler(void *handle, const __be64 *rsp,
15057b36b6e0Skxie@chelsio.com 				const struct pkt_gl *pgl)
15067b36b6e0Skxie@chelsio.com {
15077b36b6e0Skxie@chelsio.com 	const struct cpl_act_establish *rpl;
15087b36b6e0Skxie@chelsio.com 	struct sk_buff *skb;
15097b36b6e0Skxie@chelsio.com 	unsigned int opc;
15107b36b6e0Skxie@chelsio.com 	struct cxgbi_device *cdev = handle;
15117b36b6e0Skxie@chelsio.com 
15127b36b6e0Skxie@chelsio.com 	if (pgl == NULL) {
15137b36b6e0Skxie@chelsio.com 		unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;
15147b36b6e0Skxie@chelsio.com 
151524d3f95aSkxie@chelsio.com 		skb = alloc_wr(len, 0, GFP_ATOMIC);
15167b36b6e0Skxie@chelsio.com 		if (!skb)
15177b36b6e0Skxie@chelsio.com 			goto nomem;
15187b36b6e0Skxie@chelsio.com 		skb_copy_to_linear_data(skb, &rsp[1], len);
15197b36b6e0Skxie@chelsio.com 	} else {
15207b36b6e0Skxie@chelsio.com 		if (unlikely(*(u8 *)rsp != *(u8 *)pgl->va)) {
15217b36b6e0Skxie@chelsio.com 			pr_info("? FL 0x%p,RSS%#llx,FL %#llx,len %u.\n",
15227b36b6e0Skxie@chelsio.com 				pgl->va, be64_to_cpu(*rsp),
15237b36b6e0Skxie@chelsio.com 				be64_to_cpu(*(u64 *)pgl->va),
15247b36b6e0Skxie@chelsio.com 				pgl->tot_len);
15257b36b6e0Skxie@chelsio.com 			return 0;
15267b36b6e0Skxie@chelsio.com 		}
15277b36b6e0Skxie@chelsio.com 		skb = cxgb4_pktgl_to_skb(pgl, RX_PULL_LEN, RX_PULL_LEN);
15287b36b6e0Skxie@chelsio.com 		if (unlikely(!skb))
15297b36b6e0Skxie@chelsio.com 			goto nomem;
15307b36b6e0Skxie@chelsio.com 	}
15317b36b6e0Skxie@chelsio.com 
15327b36b6e0Skxie@chelsio.com 	rpl = (struct cpl_act_establish *)skb->data;
15337b36b6e0Skxie@chelsio.com 	opc = rpl->ot.opcode;
15347b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE,
15357b36b6e0Skxie@chelsio.com 		"cdev %p, opcode 0x%x(0x%x,0x%x), skb %p.\n",
15367b36b6e0Skxie@chelsio.com 		 cdev, opc, rpl->ot.opcode_tid, ntohl(rpl->ot.opcode_tid), skb);
15377b36b6e0Skxie@chelsio.com 	if (cxgb4i_cplhandlers[opc])
15387b36b6e0Skxie@chelsio.com 		cxgb4i_cplhandlers[opc](cdev, skb);
15397b36b6e0Skxie@chelsio.com 	else {
15407b36b6e0Skxie@chelsio.com 		pr_err("No handler for opcode 0x%x.\n", opc);
15417b36b6e0Skxie@chelsio.com 		__kfree_skb(skb);
15427b36b6e0Skxie@chelsio.com 	}
15437b36b6e0Skxie@chelsio.com 	return 0;
15447b36b6e0Skxie@chelsio.com nomem:
15457b36b6e0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE, "OOM bailing out.\n");
15467b36b6e0Skxie@chelsio.com 	return 1;
15477b36b6e0Skxie@chelsio.com }
15487b36b6e0Skxie@chelsio.com 
15497b36b6e0Skxie@chelsio.com static int t4_uld_state_change(void *handle, enum cxgb4_state state)
15507b36b6e0Skxie@chelsio.com {
15517b36b6e0Skxie@chelsio.com 	struct cxgbi_device *cdev = handle;
15527b36b6e0Skxie@chelsio.com 
15537b36b6e0Skxie@chelsio.com 	switch (state) {
15547b36b6e0Skxie@chelsio.com 	case CXGB4_STATE_UP:
15557b36b6e0Skxie@chelsio.com 		pr_info("cdev 0x%p, UP.\n", cdev);
15567b36b6e0Skxie@chelsio.com 		/* re-initialize */
15577b36b6e0Skxie@chelsio.com 		break;
15587b36b6e0Skxie@chelsio.com 	case CXGB4_STATE_START_RECOVERY:
15597b36b6e0Skxie@chelsio.com 		pr_info("cdev 0x%p, RECOVERY.\n", cdev);
15607b36b6e0Skxie@chelsio.com 		/* close all connections */
15617b36b6e0Skxie@chelsio.com 		break;
15627b36b6e0Skxie@chelsio.com 	case CXGB4_STATE_DOWN:
15637b36b6e0Skxie@chelsio.com 		pr_info("cdev 0x%p, DOWN.\n", cdev);
15647b36b6e0Skxie@chelsio.com 		break;
15657b36b6e0Skxie@chelsio.com 	case CXGB4_STATE_DETACH:
15667b36b6e0Skxie@chelsio.com 		pr_info("cdev 0x%p, DETACH.\n", cdev);
15677b36b6e0Skxie@chelsio.com 		break;
15687b36b6e0Skxie@chelsio.com 	default:
15697b36b6e0Skxie@chelsio.com 		pr_info("cdev 0x%p, unknown state %d.\n", cdev, state);
15707b36b6e0Skxie@chelsio.com 		break;
15717b36b6e0Skxie@chelsio.com 	}
15727b36b6e0Skxie@chelsio.com 	return 0;
15737b36b6e0Skxie@chelsio.com }
15747b36b6e0Skxie@chelsio.com 
15757b36b6e0Skxie@chelsio.com static int __init cxgb4i_init_module(void)
15767b36b6e0Skxie@chelsio.com {
15777b36b6e0Skxie@chelsio.com 	int rc;
15787b36b6e0Skxie@chelsio.com 
15797b36b6e0Skxie@chelsio.com 	printk(KERN_INFO "%s", version);
15807b36b6e0Skxie@chelsio.com 
15817b36b6e0Skxie@chelsio.com 	rc = cxgbi_iscsi_init(&cxgb4i_iscsi_transport, &cxgb4i_stt);
15827b36b6e0Skxie@chelsio.com 	if (rc < 0)
15837b36b6e0Skxie@chelsio.com 		return rc;
15847b36b6e0Skxie@chelsio.com 	cxgb4_register_uld(CXGB4_ULD_ISCSI, &cxgb4i_uld_info);
15857b36b6e0Skxie@chelsio.com 	return 0;
15867b36b6e0Skxie@chelsio.com }
15877b36b6e0Skxie@chelsio.com 
15887b36b6e0Skxie@chelsio.com static void __exit cxgb4i_exit_module(void)
15897b36b6e0Skxie@chelsio.com {
15907b36b6e0Skxie@chelsio.com 	cxgb4_unregister_uld(CXGB4_ULD_ISCSI);
15917b36b6e0Skxie@chelsio.com 	cxgbi_device_unregister_all(CXGBI_FLAG_DEV_T4);
15927b36b6e0Skxie@chelsio.com 	cxgbi_iscsi_cleanup(&cxgb4i_iscsi_transport, &cxgb4i_stt);
15937b36b6e0Skxie@chelsio.com }
15947b36b6e0Skxie@chelsio.com 
15957b36b6e0Skxie@chelsio.com module_init(cxgb4i_init_module);
15967b36b6e0Skxie@chelsio.com module_exit(cxgb4i_exit_module);
1597