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