xref: /linux/drivers/scsi/cxgbi/cxgb4i/cxgb4i.c (revision 32786fdc9506aeba98278c1844d4bfb766863832)
1 /*
2  * cxgb4i.c: Chelsio T4 iSCSI driver.
3  *
4  * Copyright (c) 2010-2015 Chelsio Communications, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  *
10  * Written by:	Karen Xie (kxie@chelsio.com)
11  *		Rakesh Ranjan (rranjan@chelsio.com)
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <scsi/scsi_host.h>
20 #include <net/tcp.h>
21 #include <net/dst.h>
22 #include <linux/netdevice.h>
23 #include <net/addrconf.h>
24 
25 #include "t4_regs.h"
26 #include "t4_msg.h"
27 #include "cxgb4.h"
28 #include "cxgb4_uld.h"
29 #include "t4fw_api.h"
30 #include "l2t.h"
31 #include "cxgb4i.h"
32 #include "clip_tbl.h"
33 
34 static unsigned int dbg_level;
35 
36 #include "../libcxgbi.h"
37 
38 #define	DRV_MODULE_NAME		"cxgb4i"
39 #define DRV_MODULE_DESC		"Chelsio T4/T5 iSCSI Driver"
40 #define	DRV_MODULE_VERSION	"0.9.5-ko"
41 #define DRV_MODULE_RELDATE	"Apr. 2015"
42 
43 static char version[] =
44 	DRV_MODULE_DESC " " DRV_MODULE_NAME
45 	" v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
46 
47 MODULE_AUTHOR("Chelsio Communications, Inc.");
48 MODULE_DESCRIPTION(DRV_MODULE_DESC);
49 MODULE_VERSION(DRV_MODULE_VERSION);
50 MODULE_LICENSE("GPL");
51 
52 module_param(dbg_level, uint, 0644);
53 MODULE_PARM_DESC(dbg_level, "Debug flag (default=0)");
54 
55 #define CXGB4I_DEFAULT_10G_RCV_WIN (256 * 1024)
56 static int cxgb4i_rcv_win = -1;
57 module_param(cxgb4i_rcv_win, int, 0644);
58 MODULE_PARM_DESC(cxgb4i_rcv_win, "TCP reveive window in bytes");
59 
60 #define CXGB4I_DEFAULT_10G_SND_WIN (128 * 1024)
61 static int cxgb4i_snd_win = -1;
62 module_param(cxgb4i_snd_win, int, 0644);
63 MODULE_PARM_DESC(cxgb4i_snd_win, "TCP send window in bytes");
64 
65 static int cxgb4i_rx_credit_thres = 10 * 1024;
66 module_param(cxgb4i_rx_credit_thres, int, 0644);
67 MODULE_PARM_DESC(cxgb4i_rx_credit_thres,
68 		"RX credits return threshold in bytes (default=10KB)");
69 
70 static unsigned int cxgb4i_max_connect = (8 * 1024);
71 module_param(cxgb4i_max_connect, uint, 0644);
72 MODULE_PARM_DESC(cxgb4i_max_connect, "Maximum number of connections");
73 
74 static unsigned short cxgb4i_sport_base = 20000;
75 module_param(cxgb4i_sport_base, ushort, 0644);
76 MODULE_PARM_DESC(cxgb4i_sport_base, "Starting port number (default 20000)");
77 
78 typedef void (*cxgb4i_cplhandler_func)(struct cxgbi_device *, struct sk_buff *);
79 
80 static void *t4_uld_add(const struct cxgb4_lld_info *);
81 static int t4_uld_rx_handler(void *, const __be64 *, const struct pkt_gl *);
82 static int t4_uld_state_change(void *, enum cxgb4_state state);
83 static inline int send_tx_flowc_wr(struct cxgbi_sock *);
84 
85 static const struct cxgb4_uld_info cxgb4i_uld_info = {
86 	.name = DRV_MODULE_NAME,
87 	.nrxq = MAX_ULD_QSETS,
88 	.ntxq = MAX_ULD_QSETS,
89 	.rxq_size = 1024,
90 	.lro = false,
91 	.add = t4_uld_add,
92 	.rx_handler = t4_uld_rx_handler,
93 	.state_change = t4_uld_state_change,
94 };
95 
96 static struct scsi_host_template cxgb4i_host_template = {
97 	.module		= THIS_MODULE,
98 	.name		= DRV_MODULE_NAME,
99 	.proc_name	= DRV_MODULE_NAME,
100 	.can_queue	= CXGB4I_SCSI_HOST_QDEPTH,
101 	.queuecommand	= iscsi_queuecommand,
102 	.change_queue_depth = scsi_change_queue_depth,
103 	.sg_tablesize	= SG_ALL,
104 	.max_sectors	= 0xFFFF,
105 	.cmd_per_lun	= ISCSI_DEF_CMD_PER_LUN,
106 	.eh_abort_handler = iscsi_eh_abort,
107 	.eh_device_reset_handler = iscsi_eh_device_reset,
108 	.eh_target_reset_handler = iscsi_eh_recover_target,
109 	.target_alloc	= iscsi_target_alloc,
110 	.use_clustering	= DISABLE_CLUSTERING,
111 	.this_id	= -1,
112 	.track_queue_depth = 1,
113 };
114 
115 static struct iscsi_transport cxgb4i_iscsi_transport = {
116 	.owner		= THIS_MODULE,
117 	.name		= DRV_MODULE_NAME,
118 	.caps		= CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST |
119 				CAP_DATADGST | CAP_DIGEST_OFFLOAD |
120 				CAP_PADDING_OFFLOAD | CAP_TEXT_NEGO,
121 	.attr_is_visible	= cxgbi_attr_is_visible,
122 	.get_host_param	= cxgbi_get_host_param,
123 	.set_host_param	= cxgbi_set_host_param,
124 	/* session management */
125 	.create_session	= cxgbi_create_session,
126 	.destroy_session	= cxgbi_destroy_session,
127 	.get_session_param = iscsi_session_get_param,
128 	/* connection management */
129 	.create_conn	= cxgbi_create_conn,
130 	.bind_conn		= cxgbi_bind_conn,
131 	.destroy_conn	= iscsi_tcp_conn_teardown,
132 	.start_conn		= iscsi_conn_start,
133 	.stop_conn		= iscsi_conn_stop,
134 	.get_conn_param	= iscsi_conn_get_param,
135 	.set_param	= cxgbi_set_conn_param,
136 	.get_stats	= cxgbi_get_conn_stats,
137 	/* pdu xmit req from user space */
138 	.send_pdu	= iscsi_conn_send_pdu,
139 	/* task */
140 	.init_task	= iscsi_tcp_task_init,
141 	.xmit_task	= iscsi_tcp_task_xmit,
142 	.cleanup_task	= cxgbi_cleanup_task,
143 	/* pdu */
144 	.alloc_pdu	= cxgbi_conn_alloc_pdu,
145 	.init_pdu	= cxgbi_conn_init_pdu,
146 	.xmit_pdu	= cxgbi_conn_xmit_pdu,
147 	.parse_pdu_itt	= cxgbi_parse_pdu_itt,
148 	/* TCP connect/disconnect */
149 	.get_ep_param	= cxgbi_get_ep_param,
150 	.ep_connect	= cxgbi_ep_connect,
151 	.ep_poll	= cxgbi_ep_poll,
152 	.ep_disconnect	= cxgbi_ep_disconnect,
153 	/* Error recovery timeout call */
154 	.session_recovery_timedout = iscsi_session_recovery_timedout,
155 };
156 
157 static struct scsi_transport_template *cxgb4i_stt;
158 
159 /*
160  * CPL (Chelsio Protocol Language) defines a message passing interface between
161  * the host driver and Chelsio asic.
162  * The section below implments CPLs that related to iscsi tcp connection
163  * open/close/abort and data send/receive.
164  */
165 
166 #define RCV_BUFSIZ_MASK		0x3FFU
167 #define MAX_IMM_TX_PKT_LEN	256
168 
169 static int push_tx_frames(struct cxgbi_sock *, int);
170 
171 /*
172  * is_ofld_imm - check whether a packet can be sent as immediate data
173  * @skb: the packet
174  *
175  * Returns true if a packet can be sent as an offload WR with immediate
176  * data.  We currently use the same limit as for Ethernet packets.
177  */
178 static inline bool is_ofld_imm(const struct sk_buff *skb)
179 {
180 	int len = skb->len;
181 
182 	if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_NEED_HDR)))
183 		len += sizeof(struct fw_ofld_tx_data_wr);
184 
185 	return len <= MAX_IMM_TX_PKT_LEN;
186 }
187 
188 static void send_act_open_req(struct cxgbi_sock *csk, struct sk_buff *skb,
189 				struct l2t_entry *e)
190 {
191 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
192 	int t4 = is_t4(lldi->adapter_type);
193 	int wscale = cxgbi_sock_compute_wscale(csk->mss_idx);
194 	unsigned long long opt0;
195 	unsigned int opt2;
196 	unsigned int qid_atid = ((unsigned int)csk->atid) |
197 				 (((unsigned int)csk->rss_qid) << 14);
198 
199 	opt0 = KEEP_ALIVE_F |
200 		WND_SCALE_V(wscale) |
201 		MSS_IDX_V(csk->mss_idx) |
202 		L2T_IDX_V(((struct l2t_entry *)csk->l2t)->idx) |
203 		TX_CHAN_V(csk->tx_chan) |
204 		SMAC_SEL_V(csk->smac_idx) |
205 		ULP_MODE_V(ULP_MODE_ISCSI) |
206 		RCV_BUFSIZ_V(csk->rcv_win >> 10);
207 
208 	opt2 = RX_CHANNEL_V(0) |
209 		RSS_QUEUE_VALID_F |
210 		RSS_QUEUE_V(csk->rss_qid);
211 
212 	if (is_t4(lldi->adapter_type)) {
213 		struct cpl_act_open_req *req =
214 				(struct cpl_act_open_req *)skb->head;
215 
216 		INIT_TP_WR(req, 0);
217 		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
218 					qid_atid));
219 		req->local_port = csk->saddr.sin_port;
220 		req->peer_port = csk->daddr.sin_port;
221 		req->local_ip = csk->saddr.sin_addr.s_addr;
222 		req->peer_ip = csk->daddr.sin_addr.s_addr;
223 		req->opt0 = cpu_to_be64(opt0);
224 		req->params = cpu_to_be32(cxgb4_select_ntuple(
225 					csk->cdev->ports[csk->port_id],
226 					csk->l2t));
227 		opt2 |= RX_FC_VALID_F;
228 		req->opt2 = cpu_to_be32(opt2);
229 
230 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
231 			"csk t4 0x%p, %pI4:%u-%pI4:%u, atid %d, qid %u.\n",
232 			csk, &req->local_ip, ntohs(req->local_port),
233 			&req->peer_ip, ntohs(req->peer_port),
234 			csk->atid, csk->rss_qid);
235 	} else {
236 		struct cpl_t5_act_open_req *req =
237 				(struct cpl_t5_act_open_req *)skb->head;
238 		u32 isn = (prandom_u32() & ~7UL) - 1;
239 
240 		INIT_TP_WR(req, 0);
241 		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
242 					qid_atid));
243 		req->local_port = csk->saddr.sin_port;
244 		req->peer_port = csk->daddr.sin_port;
245 		req->local_ip = csk->saddr.sin_addr.s_addr;
246 		req->peer_ip = csk->daddr.sin_addr.s_addr;
247 		req->opt0 = cpu_to_be64(opt0);
248 		req->params = cpu_to_be64(FILTER_TUPLE_V(
249 				cxgb4_select_ntuple(
250 					csk->cdev->ports[csk->port_id],
251 					csk->l2t)));
252 		req->rsvd = cpu_to_be32(isn);
253 		opt2 |= T5_ISS_VALID;
254 		opt2 |= T5_OPT_2_VALID_F;
255 
256 		req->opt2 = cpu_to_be32(opt2);
257 
258 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
259 			"csk t5 0x%p, %pI4:%u-%pI4:%u, atid %d, qid %u.\n",
260 			csk, &req->local_ip, ntohs(req->local_port),
261 			&req->peer_ip, ntohs(req->peer_port),
262 			csk->atid, csk->rss_qid);
263 	}
264 
265 	set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);
266 
267 	pr_info_ipaddr("t%d csk 0x%p,%u,0x%lx,%u, rss_qid %u.\n",
268 		       (&csk->saddr), (&csk->daddr), t4 ? 4 : 5, csk,
269 		       csk->state, csk->flags, csk->atid, csk->rss_qid);
270 
271 	cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
272 }
273 
274 #if IS_ENABLED(CONFIG_IPV6)
275 static void send_act_open_req6(struct cxgbi_sock *csk, struct sk_buff *skb,
276 			       struct l2t_entry *e)
277 {
278 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
279 	int t4 = is_t4(lldi->adapter_type);
280 	int wscale = cxgbi_sock_compute_wscale(csk->mss_idx);
281 	unsigned long long opt0;
282 	unsigned int opt2;
283 	unsigned int qid_atid = ((unsigned int)csk->atid) |
284 				 (((unsigned int)csk->rss_qid) << 14);
285 
286 	opt0 = KEEP_ALIVE_F |
287 		WND_SCALE_V(wscale) |
288 		MSS_IDX_V(csk->mss_idx) |
289 		L2T_IDX_V(((struct l2t_entry *)csk->l2t)->idx) |
290 		TX_CHAN_V(csk->tx_chan) |
291 		SMAC_SEL_V(csk->smac_idx) |
292 		ULP_MODE_V(ULP_MODE_ISCSI) |
293 		RCV_BUFSIZ_V(csk->rcv_win >> 10);
294 
295 	opt2 = RX_CHANNEL_V(0) |
296 		RSS_QUEUE_VALID_F |
297 		RX_FC_DISABLE_F |
298 		RSS_QUEUE_V(csk->rss_qid);
299 
300 	if (t4) {
301 		struct cpl_act_open_req6 *req =
302 			    (struct cpl_act_open_req6 *)skb->head;
303 
304 		INIT_TP_WR(req, 0);
305 		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
306 							    qid_atid));
307 		req->local_port = csk->saddr6.sin6_port;
308 		req->peer_port = csk->daddr6.sin6_port;
309 
310 		req->local_ip_hi = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr);
311 		req->local_ip_lo = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr +
312 								    8);
313 		req->peer_ip_hi = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr);
314 		req->peer_ip_lo = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr +
315 								    8);
316 
317 		req->opt0 = cpu_to_be64(opt0);
318 
319 		opt2 |= RX_FC_VALID_F;
320 		req->opt2 = cpu_to_be32(opt2);
321 
322 		req->params = cpu_to_be32(cxgb4_select_ntuple(
323 					  csk->cdev->ports[csk->port_id],
324 					  csk->l2t));
325 	} else {
326 		struct cpl_t5_act_open_req6 *req =
327 				(struct cpl_t5_act_open_req6 *)skb->head;
328 
329 		INIT_TP_WR(req, 0);
330 		OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
331 							    qid_atid));
332 		req->local_port = csk->saddr6.sin6_port;
333 		req->peer_port = csk->daddr6.sin6_port;
334 		req->local_ip_hi = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr);
335 		req->local_ip_lo = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr +
336 									8);
337 		req->peer_ip_hi = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr);
338 		req->peer_ip_lo = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr +
339 									8);
340 		req->opt0 = cpu_to_be64(opt0);
341 
342 		opt2 |= T5_OPT_2_VALID_F;
343 		req->opt2 = cpu_to_be32(opt2);
344 
345 		req->params = cpu_to_be64(FILTER_TUPLE_V(cxgb4_select_ntuple(
346 					  csk->cdev->ports[csk->port_id],
347 					  csk->l2t)));
348 	}
349 
350 	set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);
351 
352 	pr_info("t%d csk 0x%p,%u,0x%lx,%u, [%pI6]:%u-[%pI6]:%u, rss_qid %u.\n",
353 		t4 ? 4 : 5, csk, csk->state, csk->flags, csk->atid,
354 		&csk->saddr6.sin6_addr, ntohs(csk->saddr.sin_port),
355 		&csk->daddr6.sin6_addr, ntohs(csk->daddr.sin_port),
356 		csk->rss_qid);
357 
358 	cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
359 }
360 #endif
361 
362 static void send_close_req(struct cxgbi_sock *csk)
363 {
364 	struct sk_buff *skb = csk->cpl_close;
365 	struct cpl_close_con_req *req = (struct cpl_close_con_req *)skb->head;
366 	unsigned int tid = csk->tid;
367 
368 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
369 		"csk 0x%p,%u,0x%lx, tid %u.\n",
370 		csk, csk->state, csk->flags, csk->tid);
371 	csk->cpl_close = NULL;
372 	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
373 	INIT_TP_WR(req, tid);
374 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
375 	req->rsvd = 0;
376 
377 	cxgbi_sock_skb_entail(csk, skb);
378 	if (csk->state >= CTP_ESTABLISHED)
379 		push_tx_frames(csk, 1);
380 }
381 
382 static void abort_arp_failure(void *handle, struct sk_buff *skb)
383 {
384 	struct cxgbi_sock *csk = (struct cxgbi_sock *)handle;
385 	struct cpl_abort_req *req;
386 
387 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
388 		"csk 0x%p,%u,0x%lx, tid %u, abort.\n",
389 		csk, csk->state, csk->flags, csk->tid);
390 	req = (struct cpl_abort_req *)skb->data;
391 	req->cmd = CPL_ABORT_NO_RST;
392 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
393 }
394 
395 static void send_abort_req(struct cxgbi_sock *csk)
396 {
397 	struct cpl_abort_req *req;
398 	struct sk_buff *skb = csk->cpl_abort_req;
399 
400 	if (unlikely(csk->state == CTP_ABORTING) || !skb || !csk->cdev)
401 		return;
402 
403 	if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
404 		send_tx_flowc_wr(csk);
405 		cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
406 	}
407 
408 	cxgbi_sock_set_state(csk, CTP_ABORTING);
409 	cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_PENDING);
410 	cxgbi_sock_purge_write_queue(csk);
411 
412 	csk->cpl_abort_req = NULL;
413 	req = (struct cpl_abort_req *)skb->head;
414 	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
415 	req->cmd = CPL_ABORT_SEND_RST;
416 	t4_set_arp_err_handler(skb, csk, abort_arp_failure);
417 	INIT_TP_WR(req, csk->tid);
418 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, csk->tid));
419 	req->rsvd0 = htonl(csk->snd_nxt);
420 	req->rsvd1 = !cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT);
421 
422 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
423 		"csk 0x%p,%u,0x%lx,%u, snd_nxt %u, 0x%x.\n",
424 		csk, csk->state, csk->flags, csk->tid, csk->snd_nxt,
425 		req->rsvd1);
426 
427 	cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
428 }
429 
430 static void send_abort_rpl(struct cxgbi_sock *csk, int rst_status)
431 {
432 	struct sk_buff *skb = csk->cpl_abort_rpl;
433 	struct cpl_abort_rpl *rpl = (struct cpl_abort_rpl *)skb->head;
434 
435 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
436 		"csk 0x%p,%u,0x%lx,%u, status %d.\n",
437 		csk, csk->state, csk->flags, csk->tid, rst_status);
438 
439 	csk->cpl_abort_rpl = NULL;
440 	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
441 	INIT_TP_WR(rpl, csk->tid);
442 	OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, csk->tid));
443 	rpl->cmd = rst_status;
444 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
445 }
446 
447 /*
448  * CPL connection rx data ack: host ->
449  * Send RX credits through an RX_DATA_ACK CPL message. Returns the number of
450  * credits sent.
451  */
452 static u32 send_rx_credits(struct cxgbi_sock *csk, u32 credits)
453 {
454 	struct sk_buff *skb;
455 	struct cpl_rx_data_ack *req;
456 
457 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
458 		"csk 0x%p,%u,0x%lx,%u, credit %u.\n",
459 		csk, csk->state, csk->flags, csk->tid, credits);
460 
461 	skb = alloc_wr(sizeof(*req), 0, GFP_ATOMIC);
462 	if (!skb) {
463 		pr_info("csk 0x%p, credit %u, OOM.\n", csk, credits);
464 		return 0;
465 	}
466 	req = (struct cpl_rx_data_ack *)skb->head;
467 
468 	set_wr_txq(skb, CPL_PRIORITY_ACK, csk->port_id);
469 	INIT_TP_WR(req, csk->tid);
470 	OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
471 				      csk->tid));
472 	req->credit_dack = cpu_to_be32(RX_CREDITS_V(credits)
473 				       | RX_FORCE_ACK_F);
474 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
475 	return credits;
476 }
477 
478 /*
479  * sgl_len - calculates the size of an SGL of the given capacity
480  * @n: the number of SGL entries
481  * Calculates the number of flits needed for a scatter/gather list that
482  * can hold the given number of entries.
483  */
484 static inline unsigned int sgl_len(unsigned int n)
485 {
486 	n--;
487 	return (3 * n) / 2 + (n & 1) + 2;
488 }
489 
490 /*
491  * calc_tx_flits_ofld - calculate # of flits for an offload packet
492  * @skb: the packet
493  *
494  * Returns the number of flits needed for the given offload packet.
495  * These packets are already fully constructed and no additional headers
496  * will be added.
497  */
498 static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
499 {
500 	unsigned int flits, cnt;
501 
502 	if (is_ofld_imm(skb))
503 		return DIV_ROUND_UP(skb->len, 8);
504 	flits = skb_transport_offset(skb) / 8;
505 	cnt = skb_shinfo(skb)->nr_frags;
506 	if (skb_tail_pointer(skb) != skb_transport_header(skb))
507 		cnt++;
508 	return flits + sgl_len(cnt);
509 }
510 
511 #define FLOWC_WR_NPARAMS_MIN	9
512 static inline int tx_flowc_wr_credits(int *nparamsp, int *flowclenp)
513 {
514 	int nparams, flowclen16, flowclen;
515 
516 	nparams = FLOWC_WR_NPARAMS_MIN;
517 	flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
518 	flowclen16 = DIV_ROUND_UP(flowclen, 16);
519 	flowclen = flowclen16 * 16;
520 	/*
521 	 * Return the number of 16-byte credits used by the FlowC request.
522 	 * Pass back the nparams and actual FlowC length if requested.
523 	 */
524 	if (nparamsp)
525 		*nparamsp = nparams;
526 	if (flowclenp)
527 		*flowclenp = flowclen;
528 
529 	return flowclen16;
530 }
531 
532 static inline int send_tx_flowc_wr(struct cxgbi_sock *csk)
533 {
534 	struct sk_buff *skb;
535 	struct fw_flowc_wr *flowc;
536 	int nparams, flowclen16, flowclen;
537 
538 	flowclen16 = tx_flowc_wr_credits(&nparams, &flowclen);
539 	skb = alloc_wr(flowclen, 0, GFP_ATOMIC);
540 	flowc = (struct fw_flowc_wr *)skb->head;
541 	flowc->op_to_nparams =
542 		htonl(FW_WR_OP_V(FW_FLOWC_WR) | FW_FLOWC_WR_NPARAMS_V(nparams));
543 	flowc->flowid_len16 =
544 		htonl(FW_WR_LEN16_V(flowclen16) | FW_WR_FLOWID_V(csk->tid));
545 	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
546 	flowc->mnemval[0].val = htonl(csk->cdev->pfvf);
547 	flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
548 	flowc->mnemval[1].val = htonl(csk->tx_chan);
549 	flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
550 	flowc->mnemval[2].val = htonl(csk->tx_chan);
551 	flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
552 	flowc->mnemval[3].val = htonl(csk->rss_qid);
553 	flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
554 	flowc->mnemval[4].val = htonl(csk->snd_nxt);
555 	flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
556 	flowc->mnemval[5].val = htonl(csk->rcv_nxt);
557 	flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
558 	flowc->mnemval[6].val = htonl(csk->snd_win);
559 	flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
560 	flowc->mnemval[7].val = htonl(csk->advmss);
561 	flowc->mnemval[8].mnemonic = 0;
562 	flowc->mnemval[8].val = 0;
563 	flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_TXDATAPLEN_MAX;
564 	flowc->mnemval[8].val = 16384;
565 
566 	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
567 
568 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
569 		"csk 0x%p, tid 0x%x, %u,%u,%u,%u,%u,%u,%u.\n",
570 		csk, csk->tid, 0, csk->tx_chan, csk->rss_qid,
571 		csk->snd_nxt, csk->rcv_nxt, csk->snd_win,
572 		csk->advmss);
573 
574 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
575 
576 	return flowclen16;
577 }
578 
579 static inline void make_tx_data_wr(struct cxgbi_sock *csk, struct sk_buff *skb,
580 				   int dlen, int len, u32 credits, int compl)
581 {
582 	struct fw_ofld_tx_data_wr *req;
583 	unsigned int submode = cxgbi_skcb_ulp_mode(skb) & 3;
584 	unsigned int wr_ulp_mode = 0, val;
585 	bool imm = is_ofld_imm(skb);
586 
587 	req = (struct fw_ofld_tx_data_wr *)__skb_push(skb, sizeof(*req));
588 
589 	if (imm) {
590 		req->op_to_immdlen = htonl(FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
591 					FW_WR_COMPL_F |
592 					FW_WR_IMMDLEN_V(dlen));
593 		req->flowid_len16 = htonl(FW_WR_FLOWID_V(csk->tid) |
594 						FW_WR_LEN16_V(credits));
595 	} else {
596 		req->op_to_immdlen =
597 			cpu_to_be32(FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
598 					FW_WR_COMPL_F |
599 					FW_WR_IMMDLEN_V(0));
600 		req->flowid_len16 =
601 			cpu_to_be32(FW_WR_FLOWID_V(csk->tid) |
602 					FW_WR_LEN16_V(credits));
603 	}
604 	if (submode)
605 		wr_ulp_mode = FW_OFLD_TX_DATA_WR_ULPMODE_V(ULP2_MODE_ISCSI) |
606 				FW_OFLD_TX_DATA_WR_ULPSUBMODE_V(submode);
607 	val = skb_peek(&csk->write_queue) ? 0 : 1;
608 	req->tunnel_to_proxy = htonl(wr_ulp_mode |
609 				     FW_OFLD_TX_DATA_WR_SHOVE_V(val));
610 	req->plen = htonl(len);
611 	if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT))
612 		cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
613 }
614 
615 static void arp_failure_skb_discard(void *handle, struct sk_buff *skb)
616 {
617 	kfree_skb(skb);
618 }
619 
620 static int push_tx_frames(struct cxgbi_sock *csk, int req_completion)
621 {
622 	int total_size = 0;
623 	struct sk_buff *skb;
624 
625 	if (unlikely(csk->state < CTP_ESTABLISHED ||
626 		csk->state == CTP_CLOSE_WAIT_1 || csk->state >= CTP_ABORTING)) {
627 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK |
628 			 1 << CXGBI_DBG_PDU_TX,
629 			"csk 0x%p,%u,0x%lx,%u, in closing state.\n",
630 			csk, csk->state, csk->flags, csk->tid);
631 		return 0;
632 	}
633 
634 	while (csk->wr_cred && (skb = skb_peek(&csk->write_queue)) != NULL) {
635 		int dlen = skb->len;
636 		int len = skb->len;
637 		unsigned int credits_needed;
638 		int flowclen16 = 0;
639 
640 		skb_reset_transport_header(skb);
641 		if (is_ofld_imm(skb))
642 			credits_needed = DIV_ROUND_UP(dlen, 16);
643 		else
644 			credits_needed = DIV_ROUND_UP(
645 						8 * calc_tx_flits_ofld(skb),
646 						16);
647 
648 		if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_NEED_HDR)))
649 			credits_needed += DIV_ROUND_UP(
650 					sizeof(struct fw_ofld_tx_data_wr),
651 					16);
652 
653 		/*
654 		 * Assumes the initial credits is large enough to support
655 		 * fw_flowc_wr plus largest possible first payload
656 		 */
657 		if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
658 			flowclen16 = send_tx_flowc_wr(csk);
659 			csk->wr_cred -= flowclen16;
660 			csk->wr_una_cred += flowclen16;
661 			cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
662 		}
663 
664 		if (csk->wr_cred < credits_needed) {
665 			log_debug(1 << CXGBI_DBG_PDU_TX,
666 				"csk 0x%p, skb %u/%u, wr %d < %u.\n",
667 				csk, skb->len, skb->data_len,
668 				credits_needed, csk->wr_cred);
669 			break;
670 		}
671 		__skb_unlink(skb, &csk->write_queue);
672 		set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
673 		skb->csum = credits_needed + flowclen16;
674 		csk->wr_cred -= credits_needed;
675 		csk->wr_una_cred += credits_needed;
676 		cxgbi_sock_enqueue_wr(csk, skb);
677 
678 		log_debug(1 << CXGBI_DBG_PDU_TX,
679 			"csk 0x%p, skb %u/%u, wr %d, left %u, unack %u.\n",
680 			csk, skb->len, skb->data_len, credits_needed,
681 			csk->wr_cred, csk->wr_una_cred);
682 
683 		if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_NEED_HDR))) {
684 			len += cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
685 			make_tx_data_wr(csk, skb, dlen, len, credits_needed,
686 					req_completion);
687 			csk->snd_nxt += len;
688 			cxgbi_skcb_clear_flag(skb, SKCBF_TX_NEED_HDR);
689 		} else if (cxgbi_skcb_test_flag(skb, SKCBF_TX_FLAG_COMPL) &&
690 			   (csk->wr_una_cred >= (csk->wr_max_cred / 2))) {
691 			struct cpl_close_con_req *req =
692 				(struct cpl_close_con_req *)skb->data;
693 			req->wr.wr_hi |= htonl(FW_WR_COMPL_F);
694 		}
695 		total_size += skb->truesize;
696 		t4_set_arp_err_handler(skb, csk, arp_failure_skb_discard);
697 
698 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_TX,
699 			"csk 0x%p,%u,0x%lx,%u, skb 0x%p, %u.\n",
700 			csk, csk->state, csk->flags, csk->tid, skb, len);
701 
702 		cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
703 	}
704 	return total_size;
705 }
706 
707 static inline void free_atid(struct cxgbi_sock *csk)
708 {
709 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
710 
711 	if (cxgbi_sock_flag(csk, CTPF_HAS_ATID)) {
712 		cxgb4_free_atid(lldi->tids, csk->atid);
713 		cxgbi_sock_clear_flag(csk, CTPF_HAS_ATID);
714 		cxgbi_sock_put(csk);
715 	}
716 }
717 
718 static void do_act_establish(struct cxgbi_device *cdev, struct sk_buff *skb)
719 {
720 	struct cxgbi_sock *csk;
721 	struct cpl_act_establish *req = (struct cpl_act_establish *)skb->data;
722 	unsigned short tcp_opt = ntohs(req->tcp_opt);
723 	unsigned int tid = GET_TID(req);
724 	unsigned int atid = TID_TID_G(ntohl(req->tos_atid));
725 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
726 	struct tid_info *t = lldi->tids;
727 	u32 rcv_isn = be32_to_cpu(req->rcv_isn);
728 
729 	csk = lookup_atid(t, atid);
730 	if (unlikely(!csk)) {
731 		pr_err("NO conn. for atid %u, cdev 0x%p.\n", atid, cdev);
732 		goto rel_skb;
733 	}
734 
735 	if (csk->atid != atid) {
736 		pr_err("bad conn atid %u, csk 0x%p,%u,0x%lx,tid %u, atid %u.\n",
737 			atid, csk, csk->state, csk->flags, csk->tid, csk->atid);
738 		goto rel_skb;
739 	}
740 
741 	pr_info_ipaddr("atid 0x%x, tid 0x%x, csk 0x%p,%u,0x%lx, isn %u.\n",
742 		       (&csk->saddr), (&csk->daddr),
743 		       atid, tid, csk, csk->state, csk->flags, rcv_isn);
744 
745 	module_put(THIS_MODULE);
746 
747 	cxgbi_sock_get(csk);
748 	csk->tid = tid;
749 	cxgb4_insert_tid(lldi->tids, csk, tid);
750 	cxgbi_sock_set_flag(csk, CTPF_HAS_TID);
751 
752 	free_atid(csk);
753 
754 	spin_lock_bh(&csk->lock);
755 	if (unlikely(csk->state != CTP_ACTIVE_OPEN))
756 		pr_info("csk 0x%p,%u,0x%lx,%u, got EST.\n",
757 			csk, csk->state, csk->flags, csk->tid);
758 
759 	if (csk->retry_timer.function) {
760 		del_timer(&csk->retry_timer);
761 		csk->retry_timer.function = NULL;
762 	}
763 
764 	csk->copied_seq = csk->rcv_wup = csk->rcv_nxt = rcv_isn;
765 	/*
766 	 * Causes the first RX_DATA_ACK to supply any Rx credits we couldn't
767 	 * pass through opt0.
768 	 */
769 	if (csk->rcv_win > (RCV_BUFSIZ_MASK << 10))
770 		csk->rcv_wup -= csk->rcv_win - (RCV_BUFSIZ_MASK << 10);
771 
772 	csk->advmss = lldi->mtus[TCPOPT_MSS_G(tcp_opt)] - 40;
773 	if (TCPOPT_TSTAMP_G(tcp_opt))
774 		csk->advmss -= 12;
775 	if (csk->advmss < 128)
776 		csk->advmss = 128;
777 
778 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
779 		"csk 0x%p, mss_idx %u, advmss %u.\n",
780 			csk, TCPOPT_MSS_G(tcp_opt), csk->advmss);
781 
782 	cxgbi_sock_established(csk, ntohl(req->snd_isn), ntohs(req->tcp_opt));
783 
784 	if (unlikely(cxgbi_sock_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED)))
785 		send_abort_req(csk);
786 	else {
787 		if (skb_queue_len(&csk->write_queue))
788 			push_tx_frames(csk, 0);
789 		cxgbi_conn_tx_open(csk);
790 	}
791 	spin_unlock_bh(&csk->lock);
792 
793 rel_skb:
794 	__kfree_skb(skb);
795 }
796 
797 static int act_open_rpl_status_to_errno(int status)
798 {
799 	switch (status) {
800 	case CPL_ERR_CONN_RESET:
801 		return -ECONNREFUSED;
802 	case CPL_ERR_ARP_MISS:
803 		return -EHOSTUNREACH;
804 	case CPL_ERR_CONN_TIMEDOUT:
805 		return -ETIMEDOUT;
806 	case CPL_ERR_TCAM_FULL:
807 		return -ENOMEM;
808 	case CPL_ERR_CONN_EXIST:
809 		return -EADDRINUSE;
810 	default:
811 		return -EIO;
812 	}
813 }
814 
815 static void csk_act_open_retry_timer(unsigned long data)
816 {
817 	struct sk_buff *skb = NULL;
818 	struct cxgbi_sock *csk = (struct cxgbi_sock *)data;
819 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
820 	void (*send_act_open_func)(struct cxgbi_sock *, struct sk_buff *,
821 				   struct l2t_entry *);
822 	int t4 = is_t4(lldi->adapter_type), size, size6;
823 
824 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
825 		"csk 0x%p,%u,0x%lx,%u.\n",
826 		csk, csk->state, csk->flags, csk->tid);
827 
828 	cxgbi_sock_get(csk);
829 	spin_lock_bh(&csk->lock);
830 
831 	if (t4) {
832 		size = sizeof(struct cpl_act_open_req);
833 		size6 = sizeof(struct cpl_act_open_req6);
834 	} else {
835 		size = sizeof(struct cpl_t5_act_open_req);
836 		size6 = sizeof(struct cpl_t5_act_open_req6);
837 	}
838 
839 	if (csk->csk_family == AF_INET) {
840 		send_act_open_func = send_act_open_req;
841 		skb = alloc_wr(size, 0, GFP_ATOMIC);
842 #if IS_ENABLED(CONFIG_IPV6)
843 	} else {
844 		send_act_open_func = send_act_open_req6;
845 		skb = alloc_wr(size6, 0, GFP_ATOMIC);
846 #endif
847 	}
848 
849 	if (!skb)
850 		cxgbi_sock_fail_act_open(csk, -ENOMEM);
851 	else {
852 		skb->sk = (struct sock *)csk;
853 		t4_set_arp_err_handler(skb, csk,
854 				       cxgbi_sock_act_open_req_arp_failure);
855 		send_act_open_func(csk, skb, csk->l2t);
856 	}
857 
858 	spin_unlock_bh(&csk->lock);
859 	cxgbi_sock_put(csk);
860 
861 }
862 
863 static inline bool is_neg_adv(unsigned int status)
864 {
865 	return status == CPL_ERR_RTX_NEG_ADVICE ||
866 		status == CPL_ERR_KEEPALV_NEG_ADVICE ||
867 		status == CPL_ERR_PERSIST_NEG_ADVICE;
868 }
869 
870 static void do_act_open_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
871 {
872 	struct cxgbi_sock *csk;
873 	struct cpl_act_open_rpl *rpl = (struct cpl_act_open_rpl *)skb->data;
874 	unsigned int tid = GET_TID(rpl);
875 	unsigned int atid =
876 		TID_TID_G(AOPEN_ATID_G(be32_to_cpu(rpl->atid_status)));
877 	unsigned int status = AOPEN_STATUS_G(be32_to_cpu(rpl->atid_status));
878 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
879 	struct tid_info *t = lldi->tids;
880 
881 	csk = lookup_atid(t, atid);
882 	if (unlikely(!csk)) {
883 		pr_err("NO matching conn. atid %u, tid %u.\n", atid, tid);
884 		goto rel_skb;
885 	}
886 
887 	pr_info_ipaddr("tid %u/%u, status %u.\n"
888 		       "csk 0x%p,%u,0x%lx. ", (&csk->saddr), (&csk->daddr),
889 		       atid, tid, status, csk, csk->state, csk->flags);
890 
891 	if (is_neg_adv(status))
892 		goto rel_skb;
893 
894 	module_put(THIS_MODULE);
895 
896 	if (status && status != CPL_ERR_TCAM_FULL &&
897 	    status != CPL_ERR_CONN_EXIST &&
898 	    status != CPL_ERR_ARP_MISS)
899 		cxgb4_remove_tid(lldi->tids, csk->port_id, GET_TID(rpl));
900 
901 	cxgbi_sock_get(csk);
902 	spin_lock_bh(&csk->lock);
903 
904 	if (status == CPL_ERR_CONN_EXIST &&
905 	    csk->retry_timer.function != csk_act_open_retry_timer) {
906 		csk->retry_timer.function = csk_act_open_retry_timer;
907 		mod_timer(&csk->retry_timer, jiffies + HZ / 2);
908 	} else
909 		cxgbi_sock_fail_act_open(csk,
910 					act_open_rpl_status_to_errno(status));
911 
912 	spin_unlock_bh(&csk->lock);
913 	cxgbi_sock_put(csk);
914 rel_skb:
915 	__kfree_skb(skb);
916 }
917 
918 static void do_peer_close(struct cxgbi_device *cdev, struct sk_buff *skb)
919 {
920 	struct cxgbi_sock *csk;
921 	struct cpl_peer_close *req = (struct cpl_peer_close *)skb->data;
922 	unsigned int tid = GET_TID(req);
923 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
924 	struct tid_info *t = lldi->tids;
925 
926 	csk = lookup_tid(t, tid);
927 	if (unlikely(!csk)) {
928 		pr_err("can't find connection for tid %u.\n", tid);
929 		goto rel_skb;
930 	}
931 	pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u.\n",
932 		       (&csk->saddr), (&csk->daddr),
933 		       csk, csk->state, csk->flags, csk->tid);
934 	cxgbi_sock_rcv_peer_close(csk);
935 rel_skb:
936 	__kfree_skb(skb);
937 }
938 
939 static void do_close_con_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
940 {
941 	struct cxgbi_sock *csk;
942 	struct cpl_close_con_rpl *rpl = (struct cpl_close_con_rpl *)skb->data;
943 	unsigned int tid = GET_TID(rpl);
944 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
945 	struct tid_info *t = lldi->tids;
946 
947 	csk = lookup_tid(t, tid);
948 	if (unlikely(!csk)) {
949 		pr_err("can't find connection for tid %u.\n", tid);
950 		goto rel_skb;
951 	}
952 	pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u.\n",
953 		       (&csk->saddr), (&csk->daddr),
954 		       csk, csk->state, csk->flags, csk->tid);
955 	cxgbi_sock_rcv_close_conn_rpl(csk, ntohl(rpl->snd_nxt));
956 rel_skb:
957 	__kfree_skb(skb);
958 }
959 
960 static int abort_status_to_errno(struct cxgbi_sock *csk, int abort_reason,
961 								int *need_rst)
962 {
963 	switch (abort_reason) {
964 	case CPL_ERR_BAD_SYN: /* fall through */
965 	case CPL_ERR_CONN_RESET:
966 		return csk->state > CTP_ESTABLISHED ?
967 			-EPIPE : -ECONNRESET;
968 	case CPL_ERR_XMIT_TIMEDOUT:
969 	case CPL_ERR_PERSIST_TIMEDOUT:
970 	case CPL_ERR_FINWAIT2_TIMEDOUT:
971 	case CPL_ERR_KEEPALIVE_TIMEDOUT:
972 		return -ETIMEDOUT;
973 	default:
974 		return -EIO;
975 	}
976 }
977 
978 static void do_abort_req_rss(struct cxgbi_device *cdev, struct sk_buff *skb)
979 {
980 	struct cxgbi_sock *csk;
981 	struct cpl_abort_req_rss *req = (struct cpl_abort_req_rss *)skb->data;
982 	unsigned int tid = GET_TID(req);
983 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
984 	struct tid_info *t = lldi->tids;
985 	int rst_status = CPL_ABORT_NO_RST;
986 
987 	csk = lookup_tid(t, tid);
988 	if (unlikely(!csk)) {
989 		pr_err("can't find connection for tid %u.\n", tid);
990 		goto rel_skb;
991 	}
992 
993 	pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u, status %u.\n",
994 		       (&csk->saddr), (&csk->daddr),
995 		       csk, csk->state, csk->flags, csk->tid, req->status);
996 
997 	if (is_neg_adv(req->status))
998 		goto rel_skb;
999 
1000 	cxgbi_sock_get(csk);
1001 	spin_lock_bh(&csk->lock);
1002 
1003 	cxgbi_sock_clear_flag(csk, CTPF_ABORT_REQ_RCVD);
1004 
1005 	if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
1006 		send_tx_flowc_wr(csk);
1007 		cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
1008 	}
1009 
1010 	cxgbi_sock_set_flag(csk, CTPF_ABORT_REQ_RCVD);
1011 	cxgbi_sock_set_state(csk, CTP_ABORTING);
1012 
1013 	send_abort_rpl(csk, rst_status);
1014 
1015 	if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
1016 		csk->err = abort_status_to_errno(csk, req->status, &rst_status);
1017 		cxgbi_sock_closed(csk);
1018 	}
1019 
1020 	spin_unlock_bh(&csk->lock);
1021 	cxgbi_sock_put(csk);
1022 rel_skb:
1023 	__kfree_skb(skb);
1024 }
1025 
1026 static void do_abort_rpl_rss(struct cxgbi_device *cdev, struct sk_buff *skb)
1027 {
1028 	struct cxgbi_sock *csk;
1029 	struct cpl_abort_rpl_rss *rpl = (struct cpl_abort_rpl_rss *)skb->data;
1030 	unsigned int tid = GET_TID(rpl);
1031 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
1032 	struct tid_info *t = lldi->tids;
1033 
1034 	csk = lookup_tid(t, tid);
1035 	if (!csk)
1036 		goto rel_skb;
1037 
1038 	if (csk)
1039 		pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u, status %u.\n",
1040 			       (&csk->saddr), (&csk->daddr), csk,
1041 			       csk->state, csk->flags, csk->tid, rpl->status);
1042 
1043 	if (rpl->status == CPL_ERR_ABORT_FAILED)
1044 		goto rel_skb;
1045 
1046 	cxgbi_sock_rcv_abort_rpl(csk);
1047 rel_skb:
1048 	__kfree_skb(skb);
1049 }
1050 
1051 static void do_rx_data(struct cxgbi_device *cdev, struct sk_buff *skb)
1052 {
1053 	struct cxgbi_sock *csk;
1054 	struct cpl_rx_data *cpl = (struct cpl_rx_data *)skb->data;
1055 	unsigned int tid = GET_TID(cpl);
1056 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
1057 	struct tid_info *t = lldi->tids;
1058 
1059 	csk = lookup_tid(t, tid);
1060 	if (!csk) {
1061 		pr_err("can't find connection for tid %u.\n", tid);
1062 	} else {
1063 		/* not expecting this, reset the connection. */
1064 		pr_err("csk 0x%p, tid %u, rcv cpl_rx_data.\n", csk, tid);
1065 		spin_lock_bh(&csk->lock);
1066 		send_abort_req(csk);
1067 		spin_unlock_bh(&csk->lock);
1068 	}
1069 	__kfree_skb(skb);
1070 }
1071 
1072 static void do_rx_iscsi_hdr(struct cxgbi_device *cdev, struct sk_buff *skb)
1073 {
1074 	struct cxgbi_sock *csk;
1075 	struct cpl_iscsi_hdr *cpl = (struct cpl_iscsi_hdr *)skb->data;
1076 	unsigned short pdu_len_ddp = be16_to_cpu(cpl->pdu_len_ddp);
1077 	unsigned int tid = GET_TID(cpl);
1078 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
1079 	struct tid_info *t = lldi->tids;
1080 
1081 	csk = lookup_tid(t, tid);
1082 	if (unlikely(!csk)) {
1083 		pr_err("can't find conn. for tid %u.\n", tid);
1084 		goto rel_skb;
1085 	}
1086 
1087 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
1088 		"csk 0x%p,%u,0x%lx, tid %u, skb 0x%p,%u, 0x%x.\n",
1089 		csk, csk->state, csk->flags, csk->tid, skb, skb->len,
1090 		pdu_len_ddp);
1091 
1092 	spin_lock_bh(&csk->lock);
1093 
1094 	if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
1095 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1096 			"csk 0x%p,%u,0x%lx,%u, bad state.\n",
1097 			csk, csk->state, csk->flags, csk->tid);
1098 		if (csk->state != CTP_ABORTING)
1099 			goto abort_conn;
1100 		else
1101 			goto discard;
1102 	}
1103 
1104 	cxgbi_skcb_tcp_seq(skb) = ntohl(cpl->seq);
1105 	cxgbi_skcb_flags(skb) = 0;
1106 
1107 	skb_reset_transport_header(skb);
1108 	__skb_pull(skb, sizeof(*cpl));
1109 	__pskb_trim(skb, ntohs(cpl->len));
1110 
1111 	if (!csk->skb_ulp_lhdr) {
1112 		unsigned char *bhs;
1113 		unsigned int hlen, dlen, plen;
1114 
1115 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
1116 			"csk 0x%p,%u,0x%lx, tid %u, skb 0x%p header.\n",
1117 			csk, csk->state, csk->flags, csk->tid, skb);
1118 		csk->skb_ulp_lhdr = skb;
1119 		cxgbi_skcb_set_flag(skb, SKCBF_RX_HDR);
1120 
1121 		if (cxgbi_skcb_tcp_seq(skb) != csk->rcv_nxt) {
1122 			pr_info("tid %u, CPL_ISCSI_HDR, bad seq, 0x%x/0x%x.\n",
1123 				csk->tid, cxgbi_skcb_tcp_seq(skb),
1124 				csk->rcv_nxt);
1125 			goto abort_conn;
1126 		}
1127 
1128 		bhs = skb->data;
1129 		hlen = ntohs(cpl->len);
1130 		dlen = ntohl(*(unsigned int *)(bhs + 4)) & 0xFFFFFF;
1131 
1132 		plen = ISCSI_PDU_LEN_G(pdu_len_ddp);
1133 		if (is_t4(lldi->adapter_type))
1134 			plen -= 40;
1135 
1136 		if ((hlen + dlen) != plen) {
1137 			pr_info("tid 0x%x, CPL_ISCSI_HDR, pdu len "
1138 				"mismatch %u != %u + %u, seq 0x%x.\n",
1139 				csk->tid, plen, hlen, dlen,
1140 				cxgbi_skcb_tcp_seq(skb));
1141 			goto abort_conn;
1142 		}
1143 
1144 		cxgbi_skcb_rx_pdulen(skb) = (hlen + dlen + 3) & (~0x3);
1145 		if (dlen)
1146 			cxgbi_skcb_rx_pdulen(skb) += csk->dcrc_len;
1147 		csk->rcv_nxt += cxgbi_skcb_rx_pdulen(skb);
1148 
1149 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
1150 			"csk 0x%p, skb 0x%p, 0x%x,%u+%u,0x%x,0x%x.\n",
1151 			csk, skb, *bhs, hlen, dlen,
1152 			ntohl(*((unsigned int *)(bhs + 16))),
1153 			ntohl(*((unsigned int *)(bhs + 24))));
1154 
1155 	} else {
1156 		struct sk_buff *lskb = csk->skb_ulp_lhdr;
1157 
1158 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_DATA);
1159 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
1160 			"csk 0x%p,%u,0x%lx, skb 0x%p data, 0x%p.\n",
1161 			csk, csk->state, csk->flags, skb, lskb);
1162 	}
1163 
1164 	__skb_queue_tail(&csk->receive_queue, skb);
1165 	spin_unlock_bh(&csk->lock);
1166 	return;
1167 
1168 abort_conn:
1169 	send_abort_req(csk);
1170 discard:
1171 	spin_unlock_bh(&csk->lock);
1172 rel_skb:
1173 	__kfree_skb(skb);
1174 }
1175 
1176 static void do_rx_data_ddp(struct cxgbi_device *cdev,
1177 				  struct sk_buff *skb)
1178 {
1179 	struct cxgbi_sock *csk;
1180 	struct sk_buff *lskb;
1181 	struct cpl_rx_data_ddp *rpl = (struct cpl_rx_data_ddp *)skb->data;
1182 	unsigned int tid = GET_TID(rpl);
1183 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
1184 	struct tid_info *t = lldi->tids;
1185 	unsigned int status = ntohl(rpl->ddpvld);
1186 
1187 	csk = lookup_tid(t, tid);
1188 	if (unlikely(!csk)) {
1189 		pr_err("can't find connection for tid %u.\n", tid);
1190 		goto rel_skb;
1191 	}
1192 
1193 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
1194 		"csk 0x%p,%u,0x%lx, skb 0x%p,0x%x, lhdr 0x%p.\n",
1195 		csk, csk->state, csk->flags, skb, status, csk->skb_ulp_lhdr);
1196 
1197 	spin_lock_bh(&csk->lock);
1198 
1199 	if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
1200 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1201 			"csk 0x%p,%u,0x%lx,%u, bad state.\n",
1202 			csk, csk->state, csk->flags, csk->tid);
1203 		if (csk->state != CTP_ABORTING)
1204 			goto abort_conn;
1205 		else
1206 			goto discard;
1207 	}
1208 
1209 	if (!csk->skb_ulp_lhdr) {
1210 		pr_err("tid 0x%x, rcv RX_DATA_DDP w/o pdu bhs.\n", csk->tid);
1211 		goto abort_conn;
1212 	}
1213 
1214 	lskb = csk->skb_ulp_lhdr;
1215 	csk->skb_ulp_lhdr = NULL;
1216 
1217 	cxgbi_skcb_rx_ddigest(lskb) = ntohl(rpl->ulp_crc);
1218 
1219 	if (ntohs(rpl->len) != cxgbi_skcb_rx_pdulen(lskb))
1220 		pr_info("tid 0x%x, RX_DATA_DDP pdulen %u != %u.\n",
1221 			csk->tid, ntohs(rpl->len), cxgbi_skcb_rx_pdulen(lskb));
1222 
1223 	if (status & (1 << CPL_RX_DDP_STATUS_HCRC_SHIFT)) {
1224 		pr_info("csk 0x%p, lhdr 0x%p, status 0x%x, hcrc bad 0x%lx.\n",
1225 			csk, lskb, status, cxgbi_skcb_flags(lskb));
1226 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_HCRC_ERR);
1227 	}
1228 	if (status & (1 << CPL_RX_DDP_STATUS_DCRC_SHIFT)) {
1229 		pr_info("csk 0x%p, lhdr 0x%p, status 0x%x, dcrc bad 0x%lx.\n",
1230 			csk, lskb, status, cxgbi_skcb_flags(lskb));
1231 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_DCRC_ERR);
1232 	}
1233 	if (status & (1 << CPL_RX_DDP_STATUS_PAD_SHIFT)) {
1234 		log_debug(1 << CXGBI_DBG_PDU_RX,
1235 			"csk 0x%p, lhdr 0x%p, status 0x%x, pad bad.\n",
1236 			csk, lskb, status);
1237 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_PAD_ERR);
1238 	}
1239 	if ((status & (1 << CPL_RX_DDP_STATUS_DDP_SHIFT)) &&
1240 		!cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA)) {
1241 		log_debug(1 << CXGBI_DBG_PDU_RX,
1242 			"csk 0x%p, lhdr 0x%p, 0x%x, data ddp'ed.\n",
1243 			csk, lskb, status);
1244 		cxgbi_skcb_set_flag(lskb, SKCBF_RX_DATA_DDPD);
1245 	}
1246 	log_debug(1 << CXGBI_DBG_PDU_RX,
1247 		"csk 0x%p, lskb 0x%p, f 0x%lx.\n",
1248 		csk, lskb, cxgbi_skcb_flags(lskb));
1249 
1250 	cxgbi_skcb_set_flag(lskb, SKCBF_RX_STATUS);
1251 	cxgbi_conn_pdu_ready(csk);
1252 	spin_unlock_bh(&csk->lock);
1253 	goto rel_skb;
1254 
1255 abort_conn:
1256 	send_abort_req(csk);
1257 discard:
1258 	spin_unlock_bh(&csk->lock);
1259 rel_skb:
1260 	__kfree_skb(skb);
1261 }
1262 
1263 static void do_fw4_ack(struct cxgbi_device *cdev, struct sk_buff *skb)
1264 {
1265 	struct cxgbi_sock *csk;
1266 	struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)skb->data;
1267 	unsigned int tid = GET_TID(rpl);
1268 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
1269 	struct tid_info *t = lldi->tids;
1270 
1271 	csk = lookup_tid(t, tid);
1272 	if (unlikely(!csk))
1273 		pr_err("can't find connection for tid %u.\n", tid);
1274 	else {
1275 		log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1276 			"csk 0x%p,%u,0x%lx,%u.\n",
1277 			csk, csk->state, csk->flags, csk->tid);
1278 		cxgbi_sock_rcv_wr_ack(csk, rpl->credits, ntohl(rpl->snd_una),
1279 					rpl->seq_vld);
1280 	}
1281 	__kfree_skb(skb);
1282 }
1283 
1284 static void do_set_tcb_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
1285 {
1286 	struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
1287 	unsigned int tid = GET_TID(rpl);
1288 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
1289 	struct tid_info *t = lldi->tids;
1290 	struct cxgbi_sock *csk;
1291 
1292 	csk = lookup_tid(t, tid);
1293 	if (!csk)
1294 		pr_err("can't find conn. for tid %u.\n", tid);
1295 
1296 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1297 		"csk 0x%p,%u,%lx,%u, status 0x%x.\n",
1298 		csk, csk->state, csk->flags, csk->tid, rpl->status);
1299 
1300 	if (rpl->status != CPL_ERR_NONE)
1301 		pr_err("csk 0x%p,%u, SET_TCB_RPL status %u.\n",
1302 			csk, tid, rpl->status);
1303 
1304 	__kfree_skb(skb);
1305 }
1306 
1307 static int alloc_cpls(struct cxgbi_sock *csk)
1308 {
1309 	csk->cpl_close = alloc_wr(sizeof(struct cpl_close_con_req),
1310 					0, GFP_KERNEL);
1311 	if (!csk->cpl_close)
1312 		return -ENOMEM;
1313 
1314 	csk->cpl_abort_req = alloc_wr(sizeof(struct cpl_abort_req),
1315 					0, GFP_KERNEL);
1316 	if (!csk->cpl_abort_req)
1317 		goto free_cpls;
1318 
1319 	csk->cpl_abort_rpl = alloc_wr(sizeof(struct cpl_abort_rpl),
1320 					0, GFP_KERNEL);
1321 	if (!csk->cpl_abort_rpl)
1322 		goto free_cpls;
1323 	return 0;
1324 
1325 free_cpls:
1326 	cxgbi_sock_free_cpl_skbs(csk);
1327 	return -ENOMEM;
1328 }
1329 
1330 static inline void l2t_put(struct cxgbi_sock *csk)
1331 {
1332 	if (csk->l2t) {
1333 		cxgb4_l2t_release(csk->l2t);
1334 		csk->l2t = NULL;
1335 		cxgbi_sock_put(csk);
1336 	}
1337 }
1338 
1339 static void release_offload_resources(struct cxgbi_sock *csk)
1340 {
1341 	struct cxgb4_lld_info *lldi;
1342 #if IS_ENABLED(CONFIG_IPV6)
1343 	struct net_device *ndev = csk->cdev->ports[csk->port_id];
1344 #endif
1345 
1346 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1347 		"csk 0x%p,%u,0x%lx,%u.\n",
1348 		csk, csk->state, csk->flags, csk->tid);
1349 
1350 	cxgbi_sock_free_cpl_skbs(csk);
1351 	if (csk->wr_cred != csk->wr_max_cred) {
1352 		cxgbi_sock_purge_wr_queue(csk);
1353 		cxgbi_sock_reset_wr_list(csk);
1354 	}
1355 
1356 	l2t_put(csk);
1357 #if IS_ENABLED(CONFIG_IPV6)
1358 	if (csk->csk_family == AF_INET6)
1359 		cxgb4_clip_release(ndev,
1360 				   (const u32 *)&csk->saddr6.sin6_addr, 1);
1361 #endif
1362 
1363 	if (cxgbi_sock_flag(csk, CTPF_HAS_ATID))
1364 		free_atid(csk);
1365 	else if (cxgbi_sock_flag(csk, CTPF_HAS_TID)) {
1366 		lldi = cxgbi_cdev_priv(csk->cdev);
1367 		cxgb4_remove_tid(lldi->tids, 0, csk->tid);
1368 		cxgbi_sock_clear_flag(csk, CTPF_HAS_TID);
1369 		cxgbi_sock_put(csk);
1370 	}
1371 	csk->dst = NULL;
1372 	csk->cdev = NULL;
1373 }
1374 
1375 static int init_act_open(struct cxgbi_sock *csk)
1376 {
1377 	struct cxgbi_device *cdev = csk->cdev;
1378 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
1379 	struct net_device *ndev = cdev->ports[csk->port_id];
1380 	struct sk_buff *skb = NULL;
1381 	struct neighbour *n = NULL;
1382 	void *daddr;
1383 	unsigned int step;
1384 	unsigned int size, size6;
1385 	int t4 = is_t4(lldi->adapter_type);
1386 	unsigned int linkspeed;
1387 	unsigned int rcv_winf, snd_winf;
1388 
1389 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1390 		"csk 0x%p,%u,0x%lx,%u.\n",
1391 		csk, csk->state, csk->flags, csk->tid);
1392 
1393 	if (csk->csk_family == AF_INET)
1394 		daddr = &csk->daddr.sin_addr.s_addr;
1395 #if IS_ENABLED(CONFIG_IPV6)
1396 	else if (csk->csk_family == AF_INET6)
1397 		daddr = &csk->daddr6.sin6_addr;
1398 #endif
1399 	else {
1400 		pr_err("address family 0x%x not supported\n", csk->csk_family);
1401 		goto rel_resource;
1402 	}
1403 
1404 	n = dst_neigh_lookup(csk->dst, daddr);
1405 
1406 	if (!n) {
1407 		pr_err("%s, can't get neighbour of csk->dst.\n", ndev->name);
1408 		goto rel_resource;
1409 	}
1410 
1411 	csk->atid = cxgb4_alloc_atid(lldi->tids, csk);
1412 	if (csk->atid < 0) {
1413 		pr_err("%s, NO atid available.\n", ndev->name);
1414 		goto rel_resource_without_clip;
1415 	}
1416 	cxgbi_sock_set_flag(csk, CTPF_HAS_ATID);
1417 	cxgbi_sock_get(csk);
1418 
1419 	csk->l2t = cxgb4_l2t_get(lldi->l2t, n, ndev, 0);
1420 	if (!csk->l2t) {
1421 		pr_err("%s, cannot alloc l2t.\n", ndev->name);
1422 		goto rel_resource_without_clip;
1423 	}
1424 	cxgbi_sock_get(csk);
1425 
1426 #if IS_ENABLED(CONFIG_IPV6)
1427 	if (csk->csk_family == AF_INET6)
1428 		cxgb4_clip_get(ndev, (const u32 *)&csk->saddr6.sin6_addr, 1);
1429 #endif
1430 
1431 	if (t4) {
1432 		size = sizeof(struct cpl_act_open_req);
1433 		size6 = sizeof(struct cpl_act_open_req6);
1434 	} else {
1435 		size = sizeof(struct cpl_t5_act_open_req);
1436 		size6 = sizeof(struct cpl_t5_act_open_req6);
1437 	}
1438 
1439 	if (csk->csk_family == AF_INET)
1440 		skb = alloc_wr(size, 0, GFP_NOIO);
1441 #if IS_ENABLED(CONFIG_IPV6)
1442 	else
1443 		skb = alloc_wr(size6, 0, GFP_NOIO);
1444 #endif
1445 
1446 	if (!skb)
1447 		goto rel_resource;
1448 	skb->sk = (struct sock *)csk;
1449 	t4_set_arp_err_handler(skb, csk, cxgbi_sock_act_open_req_arp_failure);
1450 
1451 	if (!csk->mtu)
1452 		csk->mtu = dst_mtu(csk->dst);
1453 	cxgb4_best_mtu(lldi->mtus, csk->mtu, &csk->mss_idx);
1454 	csk->tx_chan = cxgb4_port_chan(ndev);
1455 	/* SMT two entries per row */
1456 	csk->smac_idx = ((cxgb4_port_viid(ndev) & 0x7F)) << 1;
1457 	step = lldi->ntxq / lldi->nchan;
1458 	csk->txq_idx = cxgb4_port_idx(ndev) * step;
1459 	step = lldi->nrxq / lldi->nchan;
1460 	csk->rss_qid = lldi->rxq_ids[cxgb4_port_idx(ndev) * step];
1461 	linkspeed = ((struct port_info *)netdev_priv(ndev))->link_cfg.speed;
1462 	csk->snd_win = cxgb4i_snd_win;
1463 	csk->rcv_win = cxgb4i_rcv_win;
1464 	if (cxgb4i_rcv_win <= 0) {
1465 		csk->rcv_win = CXGB4I_DEFAULT_10G_RCV_WIN;
1466 		rcv_winf = linkspeed / SPEED_10000;
1467 		if (rcv_winf)
1468 			csk->rcv_win *= rcv_winf;
1469 	}
1470 	if (cxgb4i_snd_win <= 0) {
1471 		csk->snd_win = CXGB4I_DEFAULT_10G_SND_WIN;
1472 		snd_winf = linkspeed / SPEED_10000;
1473 		if (snd_winf)
1474 			csk->snd_win *= snd_winf;
1475 	}
1476 	csk->wr_cred = lldi->wr_cred -
1477 		       DIV_ROUND_UP(sizeof(struct cpl_abort_req), 16);
1478 	csk->wr_max_cred = csk->wr_cred;
1479 	csk->wr_una_cred = 0;
1480 	cxgbi_sock_reset_wr_list(csk);
1481 	csk->err = 0;
1482 
1483 	pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u,%u,%u, mtu %u,%u, smac %u.\n",
1484 		       (&csk->saddr), (&csk->daddr), csk, csk->state,
1485 		       csk->flags, csk->tx_chan, csk->txq_idx, csk->rss_qid,
1486 		       csk->mtu, csk->mss_idx, csk->smac_idx);
1487 
1488 	/* must wait for either a act_open_rpl or act_open_establish */
1489 	try_module_get(THIS_MODULE);
1490 	cxgbi_sock_set_state(csk, CTP_ACTIVE_OPEN);
1491 	if (csk->csk_family == AF_INET)
1492 		send_act_open_req(csk, skb, csk->l2t);
1493 #if IS_ENABLED(CONFIG_IPV6)
1494 	else
1495 		send_act_open_req6(csk, skb, csk->l2t);
1496 #endif
1497 	neigh_release(n);
1498 
1499 	return 0;
1500 
1501 rel_resource:
1502 #if IS_ENABLED(CONFIG_IPV6)
1503 	if (csk->csk_family == AF_INET6)
1504 		cxgb4_clip_release(ndev,
1505 				   (const u32 *)&csk->saddr6.sin6_addr, 1);
1506 #endif
1507 rel_resource_without_clip:
1508 	if (n)
1509 		neigh_release(n);
1510 	if (skb)
1511 		__kfree_skb(skb);
1512 	return -EINVAL;
1513 }
1514 
1515 static cxgb4i_cplhandler_func cxgb4i_cplhandlers[NUM_CPL_CMDS] = {
1516 	[CPL_ACT_ESTABLISH] = do_act_establish,
1517 	[CPL_ACT_OPEN_RPL] = do_act_open_rpl,
1518 	[CPL_PEER_CLOSE] = do_peer_close,
1519 	[CPL_ABORT_REQ_RSS] = do_abort_req_rss,
1520 	[CPL_ABORT_RPL_RSS] = do_abort_rpl_rss,
1521 	[CPL_CLOSE_CON_RPL] = do_close_con_rpl,
1522 	[CPL_FW4_ACK] = do_fw4_ack,
1523 	[CPL_ISCSI_HDR] = do_rx_iscsi_hdr,
1524 	[CPL_ISCSI_DATA] = do_rx_iscsi_hdr,
1525 	[CPL_SET_TCB_RPL] = do_set_tcb_rpl,
1526 	[CPL_RX_DATA_DDP] = do_rx_data_ddp,
1527 	[CPL_RX_ISCSI_DDP] = do_rx_data_ddp,
1528 	[CPL_RX_DATA] = do_rx_data,
1529 };
1530 
1531 static int cxgb4i_ofld_init(struct cxgbi_device *cdev)
1532 {
1533 	int rc;
1534 
1535 	if (cxgb4i_max_connect > CXGB4I_MAX_CONN)
1536 		cxgb4i_max_connect = CXGB4I_MAX_CONN;
1537 
1538 	rc = cxgbi_device_portmap_create(cdev, cxgb4i_sport_base,
1539 					cxgb4i_max_connect);
1540 	if (rc < 0)
1541 		return rc;
1542 
1543 	cdev->csk_release_offload_resources = release_offload_resources;
1544 	cdev->csk_push_tx_frames = push_tx_frames;
1545 	cdev->csk_send_abort_req = send_abort_req;
1546 	cdev->csk_send_close_req = send_close_req;
1547 	cdev->csk_send_rx_credits = send_rx_credits;
1548 	cdev->csk_alloc_cpls = alloc_cpls;
1549 	cdev->csk_init_act_open = init_act_open;
1550 
1551 	pr_info("cdev 0x%p, offload up, added.\n", cdev);
1552 	return 0;
1553 }
1554 
1555 static inline void
1556 ulp_mem_io_set_hdr(struct cxgbi_device *cdev,
1557 		   struct ulp_mem_io *req,
1558 		   unsigned int wr_len, unsigned int dlen,
1559 		   unsigned int pm_addr,
1560 		   int tid)
1561 {
1562 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
1563 	struct ulptx_idata *idata = (struct ulptx_idata *)(req + 1);
1564 
1565 	INIT_ULPTX_WR(req, wr_len, 0, tid);
1566 	req->wr.wr_hi = htonl(FW_WR_OP_V(FW_ULPTX_WR) |
1567 		FW_WR_ATOMIC_V(0));
1568 	req->cmd = htonl(ULPTX_CMD_V(ULP_TX_MEM_WRITE) |
1569 		ULP_MEMIO_ORDER_V(is_t4(lldi->adapter_type)) |
1570 		T5_ULP_MEMIO_IMM_V(!is_t4(lldi->adapter_type)));
1571 	req->dlen = htonl(ULP_MEMIO_DATA_LEN_V(dlen >> 5));
1572 	req->lock_addr = htonl(ULP_MEMIO_ADDR_V(pm_addr >> 5));
1573 	req->len16 = htonl(DIV_ROUND_UP(wr_len - sizeof(req->wr), 16));
1574 
1575 	idata->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM));
1576 	idata->len = htonl(dlen);
1577 }
1578 
1579 static struct sk_buff *
1580 ddp_ppod_init_idata(struct cxgbi_device *cdev,
1581 		    struct cxgbi_ppm *ppm,
1582 		    unsigned int idx, unsigned int npods,
1583 		    unsigned int tid)
1584 {
1585 	unsigned int pm_addr = (idx << PPOD_SIZE_SHIFT) + ppm->llimit;
1586 	unsigned int dlen = npods << PPOD_SIZE_SHIFT;
1587 	unsigned int wr_len = roundup(sizeof(struct ulp_mem_io) +
1588 				sizeof(struct ulptx_idata) + dlen, 16);
1589 	struct sk_buff *skb = alloc_wr(wr_len, 0, GFP_ATOMIC);
1590 
1591 	if (!skb) {
1592 		pr_err("%s: %s idx %u, npods %u, OOM.\n",
1593 		       __func__, ppm->ndev->name, idx, npods);
1594 		return NULL;
1595 	}
1596 
1597 	ulp_mem_io_set_hdr(cdev, (struct ulp_mem_io *)skb->head, wr_len, dlen,
1598 			   pm_addr, tid);
1599 
1600 	return skb;
1601 }
1602 
1603 static int ddp_ppod_write_idata(struct cxgbi_ppm *ppm, struct cxgbi_sock *csk,
1604 				struct cxgbi_task_tag_info *ttinfo,
1605 				unsigned int idx, unsigned int npods,
1606 				struct scatterlist **sg_pp,
1607 				unsigned int *sg_off)
1608 {
1609 	struct cxgbi_device *cdev = csk->cdev;
1610 	struct sk_buff *skb = ddp_ppod_init_idata(cdev, ppm, idx, npods,
1611 						  csk->tid);
1612 	struct ulp_mem_io *req;
1613 	struct ulptx_idata *idata;
1614 	struct cxgbi_pagepod *ppod;
1615 	int i;
1616 
1617 	if (!skb)
1618 		return -ENOMEM;
1619 
1620 	req = (struct ulp_mem_io *)skb->head;
1621 	idata = (struct ulptx_idata *)(req + 1);
1622 	ppod = (struct cxgbi_pagepod *)(idata + 1);
1623 
1624 	for (i = 0; i < npods; i++, ppod++)
1625 		cxgbi_ddp_set_one_ppod(ppod, ttinfo, sg_pp, sg_off);
1626 
1627 	cxgbi_skcb_set_flag(skb, SKCBF_TX_MEM_WRITE);
1628 	cxgbi_skcb_set_flag(skb, SKCBF_TX_FLAG_COMPL);
1629 	set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
1630 
1631 	spin_lock_bh(&csk->lock);
1632 	cxgbi_sock_skb_entail(csk, skb);
1633 	spin_unlock_bh(&csk->lock);
1634 
1635 	return 0;
1636 }
1637 
1638 static int ddp_set_map(struct cxgbi_ppm *ppm, struct cxgbi_sock *csk,
1639 		       struct cxgbi_task_tag_info *ttinfo)
1640 {
1641 	unsigned int pidx = ttinfo->idx;
1642 	unsigned int npods = ttinfo->npods;
1643 	unsigned int i, cnt;
1644 	int err = 0;
1645 	struct scatterlist *sg = ttinfo->sgl;
1646 	unsigned int offset = 0;
1647 
1648 	ttinfo->cid = csk->port_id;
1649 
1650 	for (i = 0; i < npods; i += cnt, pidx += cnt) {
1651 		cnt = npods - i;
1652 
1653 		if (cnt > ULPMEM_IDATA_MAX_NPPODS)
1654 			cnt = ULPMEM_IDATA_MAX_NPPODS;
1655 		err = ddp_ppod_write_idata(ppm, csk, ttinfo, pidx, cnt,
1656 					   &sg, &offset);
1657 		if (err < 0)
1658 			break;
1659 	}
1660 
1661 	return err;
1662 }
1663 
1664 static int ddp_setup_conn_pgidx(struct cxgbi_sock *csk, unsigned int tid,
1665 				int pg_idx, bool reply)
1666 {
1667 	struct sk_buff *skb;
1668 	struct cpl_set_tcb_field *req;
1669 
1670 	if (!pg_idx || pg_idx >= DDP_PGIDX_MAX)
1671 		return 0;
1672 
1673 	skb = alloc_wr(sizeof(*req), 0, GFP_KERNEL);
1674 	if (!skb)
1675 		return -ENOMEM;
1676 
1677 	/*  set up ulp page size */
1678 	req = (struct cpl_set_tcb_field *)skb->head;
1679 	INIT_TP_WR(req, csk->tid);
1680 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
1681 	req->reply_ctrl = htons(NO_REPLY_V(reply) | QUEUENO_V(csk->rss_qid));
1682 	req->word_cookie = htons(0);
1683 	req->mask = cpu_to_be64(0x3 << 8);
1684 	req->val = cpu_to_be64(pg_idx << 8);
1685 	set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->port_id);
1686 
1687 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1688 		"csk 0x%p, tid 0x%x, pg_idx %u.\n", csk, csk->tid, pg_idx);
1689 
1690 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
1691 	return 0;
1692 }
1693 
1694 static int ddp_setup_conn_digest(struct cxgbi_sock *csk, unsigned int tid,
1695 				 int hcrc, int dcrc, int reply)
1696 {
1697 	struct sk_buff *skb;
1698 	struct cpl_set_tcb_field *req;
1699 
1700 	if (!hcrc && !dcrc)
1701 		return 0;
1702 
1703 	skb = alloc_wr(sizeof(*req), 0, GFP_KERNEL);
1704 	if (!skb)
1705 		return -ENOMEM;
1706 
1707 	csk->hcrc_len = (hcrc ? 4 : 0);
1708 	csk->dcrc_len = (dcrc ? 4 : 0);
1709 	/*  set up ulp submode */
1710 	req = (struct cpl_set_tcb_field *)skb->head;
1711 	INIT_TP_WR(req, tid);
1712 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
1713 	req->reply_ctrl = htons(NO_REPLY_V(reply) | QUEUENO_V(csk->rss_qid));
1714 	req->word_cookie = htons(0);
1715 	req->mask = cpu_to_be64(0x3 << 4);
1716 	req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
1717 				(dcrc ? ULP_CRC_DATA : 0)) << 4);
1718 	set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->port_id);
1719 
1720 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1721 		"csk 0x%p, tid 0x%x, crc %d,%d.\n", csk, csk->tid, hcrc, dcrc);
1722 
1723 	cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
1724 	return 0;
1725 }
1726 
1727 static struct cxgbi_ppm *cdev2ppm(struct cxgbi_device *cdev)
1728 {
1729 	return (struct cxgbi_ppm *)(*((struct cxgb4_lld_info *)
1730 				       (cxgbi_cdev_priv(cdev)))->iscsi_ppm);
1731 }
1732 
1733 static int cxgb4i_ddp_init(struct cxgbi_device *cdev)
1734 {
1735 	struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
1736 	struct net_device *ndev = cdev->ports[0];
1737 	struct cxgbi_tag_format tformat;
1738 	unsigned int ppmax;
1739 	int i;
1740 
1741 	if (!lldi->vr->iscsi.size) {
1742 		pr_warn("%s, iscsi NOT enabled, check config!\n", ndev->name);
1743 		return -EACCES;
1744 	}
1745 
1746 	cdev->flags |= CXGBI_FLAG_USE_PPOD_OFLDQ;
1747 	ppmax = lldi->vr->iscsi.size >> PPOD_SIZE_SHIFT;
1748 
1749 	memset(&tformat, 0, sizeof(struct cxgbi_tag_format));
1750 	for (i = 0; i < 4; i++)
1751 		tformat.pgsz_order[i] = (lldi->iscsi_pgsz_order >> (i << 3))
1752 					 & 0xF;
1753 	cxgbi_tagmask_check(lldi->iscsi_tagmask, &tformat);
1754 
1755 	cxgbi_ddp_ppm_setup(lldi->iscsi_ppm, cdev, &tformat, ppmax,
1756 			    lldi->iscsi_llimit, lldi->vr->iscsi.start, 2);
1757 
1758 	cdev->csk_ddp_setup_digest = ddp_setup_conn_digest;
1759 	cdev->csk_ddp_setup_pgidx = ddp_setup_conn_pgidx;
1760 	cdev->csk_ddp_set_map = ddp_set_map;
1761 	cdev->tx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1762 				  lldi->iscsi_iolen - ISCSI_PDU_NONPAYLOAD_LEN);
1763 	cdev->rx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1764 				  lldi->iscsi_iolen - ISCSI_PDU_NONPAYLOAD_LEN);
1765 	cdev->cdev2ppm = cdev2ppm;
1766 
1767 	return 0;
1768 }
1769 
1770 static void *t4_uld_add(const struct cxgb4_lld_info *lldi)
1771 {
1772 	struct cxgbi_device *cdev;
1773 	struct port_info *pi;
1774 	int i, rc;
1775 
1776 	cdev = cxgbi_device_register(sizeof(*lldi), lldi->nports);
1777 	if (!cdev) {
1778 		pr_info("t4 device 0x%p, register failed.\n", lldi);
1779 		return NULL;
1780 	}
1781 	pr_info("0x%p,0x%x, ports %u,%s, chan %u, q %u,%u, wr %u.\n",
1782 		cdev, lldi->adapter_type, lldi->nports,
1783 		lldi->ports[0]->name, lldi->nchan, lldi->ntxq,
1784 		lldi->nrxq, lldi->wr_cred);
1785 	for (i = 0; i < lldi->nrxq; i++)
1786 		log_debug(1 << CXGBI_DBG_DEV,
1787 			"t4 0x%p, rxq id #%d: %u.\n",
1788 			cdev, i, lldi->rxq_ids[i]);
1789 
1790 	memcpy(cxgbi_cdev_priv(cdev), lldi, sizeof(*lldi));
1791 	cdev->flags = CXGBI_FLAG_DEV_T4;
1792 	cdev->pdev = lldi->pdev;
1793 	cdev->ports = lldi->ports;
1794 	cdev->nports = lldi->nports;
1795 	cdev->mtus = lldi->mtus;
1796 	cdev->nmtus = NMTUS;
1797 	cdev->rx_credit_thres = cxgb4i_rx_credit_thres;
1798 	cdev->skb_tx_rsvd = CXGB4I_TX_HEADER_LEN;
1799 	cdev->skb_rx_extra = sizeof(struct cpl_iscsi_hdr);
1800 	cdev->itp = &cxgb4i_iscsi_transport;
1801 
1802 	cdev->pfvf = FW_VIID_PFN_G(cxgb4_port_viid(lldi->ports[0]))
1803 			<< FW_VIID_PFN_S;
1804 	pr_info("cdev 0x%p,%s, pfvf %u.\n",
1805 		cdev, lldi->ports[0]->name, cdev->pfvf);
1806 
1807 	rc = cxgb4i_ddp_init(cdev);
1808 	if (rc) {
1809 		pr_info("t4 0x%p ddp init failed.\n", cdev);
1810 		goto err_out;
1811 	}
1812 	rc = cxgb4i_ofld_init(cdev);
1813 	if (rc) {
1814 		pr_info("t4 0x%p ofld init failed.\n", cdev);
1815 		goto err_out;
1816 	}
1817 
1818 	rc = cxgbi_hbas_add(cdev, CXGB4I_MAX_LUN, CXGBI_MAX_CONN,
1819 				&cxgb4i_host_template, cxgb4i_stt);
1820 	if (rc)
1821 		goto err_out;
1822 
1823 	for (i = 0; i < cdev->nports; i++) {
1824 		pi = netdev_priv(lldi->ports[i]);
1825 		cdev->hbas[i]->port_id = pi->port_id;
1826 	}
1827 	return cdev;
1828 
1829 err_out:
1830 	cxgbi_device_unregister(cdev);
1831 	return ERR_PTR(-ENOMEM);
1832 }
1833 
1834 #define RX_PULL_LEN	128
1835 static int t4_uld_rx_handler(void *handle, const __be64 *rsp,
1836 				const struct pkt_gl *pgl)
1837 {
1838 	const struct cpl_act_establish *rpl;
1839 	struct sk_buff *skb;
1840 	unsigned int opc;
1841 	struct cxgbi_device *cdev = handle;
1842 
1843 	if (pgl == NULL) {
1844 		unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;
1845 
1846 		skb = alloc_wr(len, 0, GFP_ATOMIC);
1847 		if (!skb)
1848 			goto nomem;
1849 		skb_copy_to_linear_data(skb, &rsp[1], len);
1850 	} else {
1851 		if (unlikely(*(u8 *)rsp != *(u8 *)pgl->va)) {
1852 			pr_info("? FL 0x%p,RSS%#llx,FL %#llx,len %u.\n",
1853 				pgl->va, be64_to_cpu(*rsp),
1854 				be64_to_cpu(*(u64 *)pgl->va),
1855 				pgl->tot_len);
1856 			return 0;
1857 		}
1858 		skb = cxgb4_pktgl_to_skb(pgl, RX_PULL_LEN, RX_PULL_LEN);
1859 		if (unlikely(!skb))
1860 			goto nomem;
1861 	}
1862 
1863 	rpl = (struct cpl_act_establish *)skb->data;
1864 	opc = rpl->ot.opcode;
1865 	log_debug(1 << CXGBI_DBG_TOE,
1866 		"cdev %p, opcode 0x%x(0x%x,0x%x), skb %p.\n",
1867 		 cdev, opc, rpl->ot.opcode_tid, ntohl(rpl->ot.opcode_tid), skb);
1868 	if (cxgb4i_cplhandlers[opc])
1869 		cxgb4i_cplhandlers[opc](cdev, skb);
1870 	else {
1871 		pr_err("No handler for opcode 0x%x.\n", opc);
1872 		__kfree_skb(skb);
1873 	}
1874 	return 0;
1875 nomem:
1876 	log_debug(1 << CXGBI_DBG_TOE, "OOM bailing out.\n");
1877 	return 1;
1878 }
1879 
1880 static int t4_uld_state_change(void *handle, enum cxgb4_state state)
1881 {
1882 	struct cxgbi_device *cdev = handle;
1883 
1884 	switch (state) {
1885 	case CXGB4_STATE_UP:
1886 		pr_info("cdev 0x%p, UP.\n", cdev);
1887 		break;
1888 	case CXGB4_STATE_START_RECOVERY:
1889 		pr_info("cdev 0x%p, RECOVERY.\n", cdev);
1890 		/* close all connections */
1891 		break;
1892 	case CXGB4_STATE_DOWN:
1893 		pr_info("cdev 0x%p, DOWN.\n", cdev);
1894 		break;
1895 	case CXGB4_STATE_DETACH:
1896 		pr_info("cdev 0x%p, DETACH.\n", cdev);
1897 		cxgbi_device_unregister(cdev);
1898 		break;
1899 	default:
1900 		pr_info("cdev 0x%p, unknown state %d.\n", cdev, state);
1901 		break;
1902 	}
1903 	return 0;
1904 }
1905 
1906 static int __init cxgb4i_init_module(void)
1907 {
1908 	int rc;
1909 
1910 	printk(KERN_INFO "%s", version);
1911 
1912 	rc = cxgbi_iscsi_init(&cxgb4i_iscsi_transport, &cxgb4i_stt);
1913 	if (rc < 0)
1914 		return rc;
1915 	cxgb4_register_uld(CXGB4_ULD_ISCSI, &cxgb4i_uld_info);
1916 
1917 	return 0;
1918 }
1919 
1920 static void __exit cxgb4i_exit_module(void)
1921 {
1922 	cxgb4_unregister_uld(CXGB4_ULD_ISCSI);
1923 	cxgbi_device_unregister_all(CXGBI_FLAG_DEV_T4);
1924 	cxgbi_iscsi_cleanup(&cxgb4i_iscsi_transport, &cxgb4i_stt);
1925 }
1926 
1927 module_init(cxgb4i_init_module);
1928 module_exit(cxgb4i_exit_module);
1929