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