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