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