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