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