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