Merge tag 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm
[linux-2.6-microblaze.git] / drivers / net / ethernet / chelsio / inline_crypto / ch_ktls / chcr_ktls.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2020 Chelsio Communications.  All rights reserved. */
3
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5
6 #include <linux/skbuff.h>
7 #include <linux/module.h>
8 #include <linux/highmem.h>
9 #include <linux/ip.h>
10 #include <net/ipv6.h>
11 #include <linux/netdevice.h>
12 #include "chcr_ktls.h"
13
14 static LIST_HEAD(uld_ctx_list);
15 static DEFINE_MUTEX(dev_mutex);
16
17 static int chcr_init_tcb_fields(struct chcr_ktls_info *tx_info);
18 /*
19  * chcr_ktls_save_keys: calculate and save crypto keys.
20  * @tx_info - driver specific tls info.
21  * @crypto_info - tls crypto information.
22  * @direction - TX/RX direction.
23  * return - SUCCESS/FAILURE.
24  */
25 static int chcr_ktls_save_keys(struct chcr_ktls_info *tx_info,
26                                struct tls_crypto_info *crypto_info,
27                                enum tls_offload_ctx_dir direction)
28 {
29         int ck_size, key_ctx_size, mac_key_size, keylen, ghash_size, ret;
30         unsigned char ghash_h[TLS_CIPHER_AES_GCM_256_TAG_SIZE];
31         struct tls12_crypto_info_aes_gcm_128 *info_128_gcm;
32         struct ktls_key_ctx *kctx = &tx_info->key_ctx;
33         struct crypto_cipher *cipher;
34         unsigned char *key, *salt;
35
36         switch (crypto_info->cipher_type) {
37         case TLS_CIPHER_AES_GCM_128:
38                 info_128_gcm =
39                         (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
40                 keylen = TLS_CIPHER_AES_GCM_128_KEY_SIZE;
41                 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
42                 tx_info->salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE;
43                 mac_key_size = CHCR_KEYCTX_MAC_KEY_SIZE_128;
44                 tx_info->iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
45                 tx_info->iv = be64_to_cpu(*(__be64 *)info_128_gcm->iv);
46
47                 ghash_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
48                 key = info_128_gcm->key;
49                 salt = info_128_gcm->salt;
50                 tx_info->record_no = *(u64 *)info_128_gcm->rec_seq;
51
52                 /* The SCMD fields used when encrypting a full TLS
53                  * record. Its a one time calculation till the
54                  * connection exists.
55                  */
56                 tx_info->scmd0_seqno_numivs =
57                         SCMD_SEQ_NO_CTRL_V(CHCR_SCMD_SEQ_NO_CTRL_64BIT) |
58                         SCMD_CIPH_AUTH_SEQ_CTRL_F |
59                         SCMD_PROTO_VERSION_V(CHCR_SCMD_PROTO_VERSION_TLS) |
60                         SCMD_CIPH_MODE_V(CHCR_SCMD_CIPHER_MODE_AES_GCM) |
61                         SCMD_AUTH_MODE_V(CHCR_SCMD_AUTH_MODE_GHASH) |
62                         SCMD_IV_SIZE_V(TLS_CIPHER_AES_GCM_128_IV_SIZE >> 1) |
63                         SCMD_NUM_IVS_V(1);
64
65                 /* keys will be sent inline. */
66                 tx_info->scmd0_ivgen_hdrlen = SCMD_KEY_CTX_INLINE_F;
67
68                 /* The SCMD fields used when encrypting a partial TLS
69                  * record (no trailer and possibly a truncated payload).
70                  */
71                 tx_info->scmd0_short_seqno_numivs =
72                         SCMD_CIPH_AUTH_SEQ_CTRL_F |
73                         SCMD_PROTO_VERSION_V(CHCR_SCMD_PROTO_VERSION_GENERIC) |
74                         SCMD_CIPH_MODE_V(CHCR_SCMD_CIPHER_MODE_AES_CTR) |
75                         SCMD_IV_SIZE_V(AES_BLOCK_LEN >> 1);
76
77                 tx_info->scmd0_short_ivgen_hdrlen =
78                         tx_info->scmd0_ivgen_hdrlen | SCMD_AADIVDROP_F;
79
80                 break;
81
82         default:
83                 pr_err("GCM: cipher type 0x%x not supported\n",
84                        crypto_info->cipher_type);
85                 ret = -EINVAL;
86                 goto out;
87         }
88
89         key_ctx_size = CHCR_KTLS_KEY_CTX_LEN +
90                        roundup(keylen, 16) + ghash_size;
91         /* Calculate the H = CIPH(K, 0 repeated 16 times).
92          * It will go in key context
93          */
94         cipher = crypto_alloc_cipher("aes", 0, 0);
95         if (IS_ERR(cipher)) {
96                 ret = -ENOMEM;
97                 goto out;
98         }
99
100         ret = crypto_cipher_setkey(cipher, key, keylen);
101         if (ret)
102                 goto out1;
103
104         memset(ghash_h, 0, ghash_size);
105         crypto_cipher_encrypt_one(cipher, ghash_h, ghash_h);
106
107         /* fill the Key context */
108         if (direction == TLS_OFFLOAD_CTX_DIR_TX) {
109                 kctx->ctx_hdr = FILL_KEY_CTX_HDR(ck_size,
110                                                  mac_key_size,
111                                                  key_ctx_size >> 4);
112         } else {
113                 ret = -EINVAL;
114                 goto out1;
115         }
116
117         memcpy(kctx->salt, salt, tx_info->salt_size);
118         memcpy(kctx->key, key, keylen);
119         memcpy(kctx->key + keylen, ghash_h, ghash_size);
120         tx_info->key_ctx_len = key_ctx_size;
121
122 out1:
123         crypto_free_cipher(cipher);
124 out:
125         return ret;
126 }
127
128 /*
129  * chcr_ktls_act_open_req: creates TCB entry for ipv4 connection.
130  * @sk - tcp socket.
131  * @tx_info - driver specific tls info.
132  * @atid - connection active tid.
133  * return - send success/failure.
134  */
135 static int chcr_ktls_act_open_req(struct sock *sk,
136                                   struct chcr_ktls_info *tx_info,
137                                   int atid)
138 {
139         struct inet_sock *inet = inet_sk(sk);
140         struct cpl_t6_act_open_req *cpl6;
141         struct cpl_act_open_req *cpl;
142         struct sk_buff *skb;
143         unsigned int len;
144         int qid_atid;
145         u64 options;
146
147         len = sizeof(*cpl6);
148         skb = alloc_skb(len, GFP_KERNEL);
149         if (unlikely(!skb))
150                 return -ENOMEM;
151         /* mark it a control pkt */
152         set_wr_txq(skb, CPL_PRIORITY_CONTROL, tx_info->port_id);
153
154         cpl6 = __skb_put_zero(skb, len);
155         cpl = (struct cpl_act_open_req *)cpl6;
156         INIT_TP_WR(cpl6, 0);
157         qid_atid = TID_QID_V(tx_info->rx_qid) |
158                    TID_TID_V(atid);
159         OPCODE_TID(cpl) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, qid_atid));
160         cpl->local_port = inet->inet_sport;
161         cpl->peer_port = inet->inet_dport;
162         cpl->local_ip = inet->inet_rcv_saddr;
163         cpl->peer_ip = inet->inet_daddr;
164
165         /* fill first 64 bit option field. */
166         options = TCAM_BYPASS_F | ULP_MODE_V(ULP_MODE_NONE) | NON_OFFLOAD_F |
167                   SMAC_SEL_V(tx_info->smt_idx) | TX_CHAN_V(tx_info->tx_chan);
168         cpl->opt0 = cpu_to_be64(options);
169
170         /* next 64 bit option field. */
171         options =
172                 TX_QUEUE_V(tx_info->adap->params.tp.tx_modq[tx_info->tx_chan]);
173         cpl->opt2 = htonl(options);
174
175         return cxgb4_l2t_send(tx_info->netdev, skb, tx_info->l2te);
176 }
177
178 #if IS_ENABLED(CONFIG_IPV6)
179 /*
180  * chcr_ktls_act_open_req6: creates TCB entry for ipv6 connection.
181  * @sk - tcp socket.
182  * @tx_info - driver specific tls info.
183  * @atid - connection active tid.
184  * return - send success/failure.
185  */
186 static int chcr_ktls_act_open_req6(struct sock *sk,
187                                    struct chcr_ktls_info *tx_info,
188                                    int atid)
189 {
190         struct inet_sock *inet = inet_sk(sk);
191         struct cpl_t6_act_open_req6 *cpl6;
192         struct cpl_act_open_req6 *cpl;
193         struct sk_buff *skb;
194         unsigned int len;
195         int qid_atid;
196         u64 options;
197
198         len = sizeof(*cpl6);
199         skb = alloc_skb(len, GFP_KERNEL);
200         if (unlikely(!skb))
201                 return -ENOMEM;
202         /* mark it a control pkt */
203         set_wr_txq(skb, CPL_PRIORITY_CONTROL, tx_info->port_id);
204
205         cpl6 = __skb_put_zero(skb, len);
206         cpl = (struct cpl_act_open_req6 *)cpl6;
207         INIT_TP_WR(cpl6, 0);
208         qid_atid = TID_QID_V(tx_info->rx_qid) | TID_TID_V(atid);
209         OPCODE_TID(cpl) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6, qid_atid));
210         cpl->local_port = inet->inet_sport;
211         cpl->peer_port = inet->inet_dport;
212         cpl->local_ip_hi = *(__be64 *)&sk->sk_v6_rcv_saddr.in6_u.u6_addr8[0];
213         cpl->local_ip_lo = *(__be64 *)&sk->sk_v6_rcv_saddr.in6_u.u6_addr8[8];
214         cpl->peer_ip_hi = *(__be64 *)&sk->sk_v6_daddr.in6_u.u6_addr8[0];
215         cpl->peer_ip_lo = *(__be64 *)&sk->sk_v6_daddr.in6_u.u6_addr8[8];
216
217         /* first 64 bit option field. */
218         options = TCAM_BYPASS_F | ULP_MODE_V(ULP_MODE_NONE) | NON_OFFLOAD_F |
219                   SMAC_SEL_V(tx_info->smt_idx) | TX_CHAN_V(tx_info->tx_chan);
220         cpl->opt0 = cpu_to_be64(options);
221         /* next 64 bit option field. */
222         options =
223                 TX_QUEUE_V(tx_info->adap->params.tp.tx_modq[tx_info->tx_chan]);
224         cpl->opt2 = htonl(options);
225
226         return cxgb4_l2t_send(tx_info->netdev, skb, tx_info->l2te);
227 }
228 #endif /* #if IS_ENABLED(CONFIG_IPV6) */
229
230 /*
231  * chcr_setup_connection:  create a TCB entry so that TP will form tcp packets.
232  * @sk - tcp socket.
233  * @tx_info - driver specific tls info.
234  * return: NET_TX_OK/NET_XMIT_DROP
235  */
236 static int chcr_setup_connection(struct sock *sk,
237                                  struct chcr_ktls_info *tx_info)
238 {
239         struct tid_info *t = &tx_info->adap->tids;
240         int atid, ret = 0;
241
242         atid = cxgb4_alloc_atid(t, tx_info);
243         if (atid == -1)
244                 return -EINVAL;
245
246         tx_info->atid = atid;
247
248         if (tx_info->ip_family == AF_INET) {
249                 ret = chcr_ktls_act_open_req(sk, tx_info, atid);
250 #if IS_ENABLED(CONFIG_IPV6)
251         } else {
252                 ret = cxgb4_clip_get(tx_info->netdev, (const u32 *)
253                                      &sk->sk_v6_rcv_saddr,
254                                      1);
255                 if (ret)
256                         return ret;
257                 ret = chcr_ktls_act_open_req6(sk, tx_info, atid);
258 #endif
259         }
260
261         /* if return type is NET_XMIT_CN, msg will be sent but delayed, mark ret
262          * success, if any other return type clear atid and return that failure.
263          */
264         if (ret) {
265                 if (ret == NET_XMIT_CN) {
266                         ret = 0;
267                 } else {
268 #if IS_ENABLED(CONFIG_IPV6)
269                         /* clear clip entry */
270                         if (tx_info->ip_family == AF_INET6)
271                                 cxgb4_clip_release(tx_info->netdev,
272                                                    (const u32 *)
273                                                    &sk->sk_v6_rcv_saddr,
274                                                    1);
275 #endif
276                         cxgb4_free_atid(t, atid);
277                 }
278         }
279
280         return ret;
281 }
282
283 /*
284  * chcr_set_tcb_field: update tcb fields.
285  * @tx_info - driver specific tls info.
286  * @word - TCB word.
287  * @mask - TCB word related mask.
288  * @val - TCB word related value.
289  * @no_reply - set 1 if not looking for TP response.
290  */
291 static int chcr_set_tcb_field(struct chcr_ktls_info *tx_info, u16 word,
292                               u64 mask, u64 val, int no_reply)
293 {
294         struct cpl_set_tcb_field *req;
295         struct sk_buff *skb;
296
297         skb = alloc_skb(sizeof(struct cpl_set_tcb_field), GFP_ATOMIC);
298         if (!skb)
299                 return -ENOMEM;
300
301         req = (struct cpl_set_tcb_field *)__skb_put_zero(skb, sizeof(*req));
302         INIT_TP_WR_CPL(req, CPL_SET_TCB_FIELD, tx_info->tid);
303         req->reply_ctrl = htons(QUEUENO_V(tx_info->rx_qid) |
304                                 NO_REPLY_V(no_reply));
305         req->word_cookie = htons(TCB_WORD_V(word));
306         req->mask = cpu_to_be64(mask);
307         req->val = cpu_to_be64(val);
308
309         set_wr_txq(skb, CPL_PRIORITY_CONTROL, tx_info->port_id);
310         return cxgb4_ofld_send(tx_info->netdev, skb);
311 }
312
313 /*
314  * chcr_ktls_mark_tcb_close: mark tcb state to CLOSE
315  * @tx_info - driver specific tls info.
316  * return: NET_TX_OK/NET_XMIT_DROP.
317  */
318 static int chcr_ktls_mark_tcb_close(struct chcr_ktls_info *tx_info)
319 {
320         return chcr_set_tcb_field(tx_info, TCB_T_STATE_W,
321                                   TCB_T_STATE_V(TCB_T_STATE_M),
322                                   CHCR_TCB_STATE_CLOSED, 1);
323 }
324
325 /*
326  * chcr_ktls_dev_del:  call back for tls_dev_del.
327  * Remove the tid and l2t entry and close the connection.
328  * it per connection basis.
329  * @netdev - net device.
330  * @tls_cts - tls context.
331  * @direction - TX/RX crypto direction
332  */
333 static void chcr_ktls_dev_del(struct net_device *netdev,
334                               struct tls_context *tls_ctx,
335                               enum tls_offload_ctx_dir direction)
336 {
337         struct chcr_ktls_ofld_ctx_tx *tx_ctx =
338                                 chcr_get_ktls_tx_context(tls_ctx);
339         struct chcr_ktls_info *tx_info = tx_ctx->chcr_info;
340         struct ch_ktls_port_stats_debug *port_stats;
341
342         if (!tx_info)
343                 return;
344
345         /* clear l2t entry */
346         if (tx_info->l2te)
347                 cxgb4_l2t_release(tx_info->l2te);
348
349 #if IS_ENABLED(CONFIG_IPV6)
350         /* clear clip entry */
351         if (tx_info->ip_family == AF_INET6)
352                 cxgb4_clip_release(netdev, (const u32 *)
353                                    &tx_info->sk->sk_v6_rcv_saddr,
354                                    1);
355 #endif
356
357         /* clear tid */
358         if (tx_info->tid != -1) {
359                 /* clear tcb state and then release tid */
360                 chcr_ktls_mark_tcb_close(tx_info);
361                 cxgb4_remove_tid(&tx_info->adap->tids, tx_info->tx_chan,
362                                  tx_info->tid, tx_info->ip_family);
363         }
364
365         port_stats = &tx_info->adap->ch_ktls_stats.ktls_port[tx_info->port_id];
366         atomic64_inc(&port_stats->ktls_tx_connection_close);
367         kvfree(tx_info);
368         tx_ctx->chcr_info = NULL;
369         /* release module refcount */
370         module_put(THIS_MODULE);
371 }
372
373 /*
374  * chcr_ktls_dev_add:  call back for tls_dev_add.
375  * Create a tcb entry for TP. Also add l2t entry for the connection. And
376  * generate keys & save those keys locally.
377  * @netdev - net device.
378  * @tls_cts - tls context.
379  * @direction - TX/RX crypto direction
380  * return: SUCCESS/FAILURE.
381  */
382 static int chcr_ktls_dev_add(struct net_device *netdev, struct sock *sk,
383                              enum tls_offload_ctx_dir direction,
384                              struct tls_crypto_info *crypto_info,
385                              u32 start_offload_tcp_sn)
386 {
387         struct tls_context *tls_ctx = tls_get_ctx(sk);
388         struct ch_ktls_port_stats_debug *port_stats;
389         struct chcr_ktls_ofld_ctx_tx *tx_ctx;
390         struct chcr_ktls_info *tx_info;
391         struct dst_entry *dst;
392         struct adapter *adap;
393         struct port_info *pi;
394         struct neighbour *n;
395         u8 daaddr[16];
396         int ret = -1;
397
398         tx_ctx = chcr_get_ktls_tx_context(tls_ctx);
399
400         pi = netdev_priv(netdev);
401         adap = pi->adapter;
402         port_stats = &adap->ch_ktls_stats.ktls_port[pi->port_id];
403         atomic64_inc(&port_stats->ktls_tx_connection_open);
404
405         if (direction == TLS_OFFLOAD_CTX_DIR_RX) {
406                 pr_err("not expecting for RX direction\n");
407                 goto out;
408         }
409
410         if (tx_ctx->chcr_info)
411                 goto out;
412
413         tx_info = kvzalloc(sizeof(*tx_info), GFP_KERNEL);
414         if (!tx_info)
415                 goto out;
416
417         tx_info->sk = sk;
418         spin_lock_init(&tx_info->lock);
419         /* initialize tid and atid to -1, 0 is a also a valid id. */
420         tx_info->tid = -1;
421         tx_info->atid = -1;
422
423         tx_info->adap = adap;
424         tx_info->netdev = netdev;
425         tx_info->first_qset = pi->first_qset;
426         tx_info->tx_chan = pi->tx_chan;
427         tx_info->smt_idx = pi->smt_idx;
428         tx_info->port_id = pi->port_id;
429         tx_info->prev_ack = 0;
430         tx_info->prev_win = 0;
431
432         tx_info->rx_qid = chcr_get_first_rx_qid(adap);
433         if (unlikely(tx_info->rx_qid < 0))
434                 goto free_tx_info;
435
436         tx_info->prev_seq = start_offload_tcp_sn;
437         tx_info->tcp_start_seq_number = start_offload_tcp_sn;
438
439         /* save crypto keys */
440         ret = chcr_ktls_save_keys(tx_info, crypto_info, direction);
441         if (ret < 0)
442                 goto free_tx_info;
443
444         /* get peer ip */
445         if (sk->sk_family == AF_INET) {
446                 memcpy(daaddr, &sk->sk_daddr, 4);
447                 tx_info->ip_family = AF_INET;
448 #if IS_ENABLED(CONFIG_IPV6)
449         } else {
450                 if (!sk->sk_ipv6only &&
451                     ipv6_addr_type(&sk->sk_v6_daddr) == IPV6_ADDR_MAPPED) {
452                         memcpy(daaddr, &sk->sk_daddr, 4);
453                         tx_info->ip_family = AF_INET;
454                 } else {
455                         memcpy(daaddr, sk->sk_v6_daddr.in6_u.u6_addr8, 16);
456                         tx_info->ip_family = AF_INET6;
457                 }
458 #endif
459         }
460
461         /* get the l2t index */
462         dst = sk_dst_get(sk);
463         if (!dst) {
464                 pr_err("DST entry not found\n");
465                 goto free_tx_info;
466         }
467         n = dst_neigh_lookup(dst, daaddr);
468         if (!n || !n->dev) {
469                 pr_err("neighbour not found\n");
470                 dst_release(dst);
471                 goto free_tx_info;
472         }
473         tx_info->l2te  = cxgb4_l2t_get(adap->l2t, n, n->dev, 0);
474
475         neigh_release(n);
476         dst_release(dst);
477
478         if (!tx_info->l2te) {
479                 pr_err("l2t entry not found\n");
480                 goto free_tx_info;
481         }
482
483         /* Driver shouldn't be removed until any single connection exists */
484         if (!try_module_get(THIS_MODULE))
485                 goto free_l2t;
486
487         init_completion(&tx_info->completion);
488         /* create a filter and call cxgb4_l2t_send to send the packet out, which
489          * will take care of updating l2t entry in hw if not already done.
490          */
491         tx_info->open_state = CH_KTLS_OPEN_PENDING;
492
493         if (chcr_setup_connection(sk, tx_info))
494                 goto put_module;
495
496         /* Wait for reply */
497         wait_for_completion_timeout(&tx_info->completion, 30 * HZ);
498         spin_lock_bh(&tx_info->lock);
499         if (tx_info->open_state) {
500                 /* need to wait for hw response, can't free tx_info yet. */
501                 if (tx_info->open_state == CH_KTLS_OPEN_PENDING)
502                         tx_info->pending_close = true;
503                 /* free the lock after the cleanup */
504                 goto put_module;
505         }
506         spin_unlock_bh(&tx_info->lock);
507
508         /* initialize tcb */
509         reinit_completion(&tx_info->completion);
510         /* mark it pending for hw response */
511         tx_info->open_state = CH_KTLS_OPEN_PENDING;
512
513         if (chcr_init_tcb_fields(tx_info))
514                 goto free_tid;
515
516         /* Wait for reply */
517         wait_for_completion_timeout(&tx_info->completion, 30 * HZ);
518         spin_lock_bh(&tx_info->lock);
519         if (tx_info->open_state) {
520                 /* need to wait for hw response, can't free tx_info yet. */
521                 tx_info->pending_close = true;
522                 /* free the lock after cleanup */
523                 goto free_tid;
524         }
525         spin_unlock_bh(&tx_info->lock);
526
527         if (!cxgb4_check_l2t_valid(tx_info->l2te))
528                 goto free_tid;
529
530         atomic64_inc(&port_stats->ktls_tx_ctx);
531         tx_ctx->chcr_info = tx_info;
532
533         return 0;
534
535 free_tid:
536         chcr_ktls_mark_tcb_close(tx_info);
537 #if IS_ENABLED(CONFIG_IPV6)
538         /* clear clip entry */
539         if (tx_info->ip_family == AF_INET6)
540                 cxgb4_clip_release(netdev, (const u32 *)
541                                    &sk->sk_v6_rcv_saddr,
542                                    1);
543 #endif
544         cxgb4_remove_tid(&tx_info->adap->tids, tx_info->tx_chan,
545                          tx_info->tid, tx_info->ip_family);
546
547 put_module:
548         /* release module refcount */
549         module_put(THIS_MODULE);
550 free_l2t:
551         cxgb4_l2t_release(tx_info->l2te);
552 free_tx_info:
553         if (tx_info->pending_close)
554                 spin_unlock_bh(&tx_info->lock);
555         else
556                 kvfree(tx_info);
557 out:
558         atomic64_inc(&port_stats->ktls_tx_connection_fail);
559         return -1;
560 }
561
562 /*
563  * chcr_init_tcb_fields:  Initialize tcb fields to handle TCP seq number
564  *                        handling.
565  * @tx_info - driver specific tls info.
566  * return: NET_TX_OK/NET_XMIT_DROP
567  */
568 static int chcr_init_tcb_fields(struct chcr_ktls_info *tx_info)
569 {
570         int  ret = 0;
571
572         /* set tcb in offload and bypass */
573         ret =
574         chcr_set_tcb_field(tx_info, TCB_T_FLAGS_W,
575                            TCB_T_FLAGS_V(TF_CORE_BYPASS_F | TF_NON_OFFLOAD_F),
576                            TCB_T_FLAGS_V(TF_CORE_BYPASS_F), 1);
577         if (ret)
578                 return ret;
579         /* reset snd_una and snd_next fields in tcb */
580         ret = chcr_set_tcb_field(tx_info, TCB_SND_UNA_RAW_W,
581                                  TCB_SND_NXT_RAW_V(TCB_SND_NXT_RAW_M) |
582                                  TCB_SND_UNA_RAW_V(TCB_SND_UNA_RAW_M),
583                                  0, 1);
584         if (ret)
585                 return ret;
586
587         /* reset send max */
588         ret = chcr_set_tcb_field(tx_info, TCB_SND_MAX_RAW_W,
589                                  TCB_SND_MAX_RAW_V(TCB_SND_MAX_RAW_M),
590                                  0, 1);
591         if (ret)
592                 return ret;
593
594         /* update l2t index and request for tp reply to confirm tcb is
595          * initialised to handle tx traffic.
596          */
597         ret = chcr_set_tcb_field(tx_info, TCB_L2T_IX_W,
598                                  TCB_L2T_IX_V(TCB_L2T_IX_M),
599                                  TCB_L2T_IX_V(tx_info->l2te->idx), 0);
600         return ret;
601 }
602
603 /*
604  * chcr_ktls_cpl_act_open_rpl: connection reply received from TP.
605  */
606 static int chcr_ktls_cpl_act_open_rpl(struct adapter *adap,
607                                       unsigned char *input)
608 {
609         const struct cpl_act_open_rpl *p = (void *)input;
610         struct chcr_ktls_info *tx_info = NULL;
611         unsigned int atid, tid, status;
612         struct tid_info *t;
613
614         tid = GET_TID(p);
615         status = AOPEN_STATUS_G(ntohl(p->atid_status));
616         atid = TID_TID_G(AOPEN_ATID_G(ntohl(p->atid_status)));
617
618         t = &adap->tids;
619         tx_info = lookup_atid(t, atid);
620
621         if (!tx_info || tx_info->atid != atid) {
622                 pr_err("%s: incorrect tx_info or atid\n", __func__);
623                 return -1;
624         }
625
626         cxgb4_free_atid(t, atid);
627         tx_info->atid = -1;
628
629         spin_lock(&tx_info->lock);
630         /* HW response is very close, finish pending cleanup */
631         if (tx_info->pending_close) {
632                 spin_unlock(&tx_info->lock);
633                 if (!status) {
634                         /* it's a late success, tcb status is establised,
635                          * mark it close.
636                          */
637                         chcr_ktls_mark_tcb_close(tx_info);
638                         cxgb4_remove_tid(&tx_info->adap->tids, tx_info->tx_chan,
639                                          tid, tx_info->ip_family);
640                 }
641                 kvfree(tx_info);
642                 return 0;
643         }
644
645         if (!status) {
646                 tx_info->tid = tid;
647                 cxgb4_insert_tid(t, tx_info, tx_info->tid, tx_info->ip_family);
648                 tx_info->open_state = CH_KTLS_OPEN_SUCCESS;
649         } else {
650                 tx_info->open_state = CH_KTLS_OPEN_FAILURE;
651         }
652         spin_unlock(&tx_info->lock);
653
654         complete(&tx_info->completion);
655         return 0;
656 }
657
658 /*
659  * chcr_ktls_cpl_set_tcb_rpl: TCB reply received from TP.
660  */
661 static int chcr_ktls_cpl_set_tcb_rpl(struct adapter *adap, unsigned char *input)
662 {
663         const struct cpl_set_tcb_rpl *p = (void *)input;
664         struct chcr_ktls_info *tx_info = NULL;
665         struct tid_info *t;
666         u32 tid;
667
668         tid = GET_TID(p);
669
670         t = &adap->tids;
671         tx_info = lookup_tid(t, tid);
672
673         if (!tx_info || tx_info->tid != tid) {
674                 pr_err("%s: incorrect tx_info or tid\n", __func__);
675                 return -1;
676         }
677
678         spin_lock(&tx_info->lock);
679         if (tx_info->pending_close) {
680                 spin_unlock(&tx_info->lock);
681                 kvfree(tx_info);
682                 return 0;
683         }
684         tx_info->open_state = false;
685         spin_unlock(&tx_info->lock);
686
687         complete(&tx_info->completion);
688         return 0;
689 }
690
691 static void *__chcr_write_cpl_set_tcb_ulp(struct chcr_ktls_info *tx_info,
692                                         u32 tid, void *pos, u16 word, u64 mask,
693                                         u64 val, u32 reply)
694 {
695         struct cpl_set_tcb_field_core *cpl;
696         struct ulptx_idata *idata;
697         struct ulp_txpkt *txpkt;
698
699         /* ULP_TXPKT */
700         txpkt = pos;
701         txpkt->cmd_dest = htonl(ULPTX_CMD_V(ULP_TX_PKT) | ULP_TXPKT_DEST_V(0));
702         txpkt->len = htonl(DIV_ROUND_UP(CHCR_SET_TCB_FIELD_LEN, 16));
703
704         /* ULPTX_IDATA sub-command */
705         idata = (struct ulptx_idata *)(txpkt + 1);
706         idata->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM));
707         idata->len = htonl(sizeof(*cpl));
708         pos = idata + 1;
709
710         cpl = pos;
711         /* CPL_SET_TCB_FIELD */
712         OPCODE_TID(cpl) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
713         cpl->reply_ctrl = htons(QUEUENO_V(tx_info->rx_qid) |
714                         NO_REPLY_V(!reply));
715         cpl->word_cookie = htons(TCB_WORD_V(word));
716         cpl->mask = cpu_to_be64(mask);
717         cpl->val = cpu_to_be64(val);
718
719         /* ULPTX_NOOP */
720         idata = (struct ulptx_idata *)(cpl + 1);
721         idata->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_NOOP));
722         idata->len = htonl(0);
723         pos = idata + 1;
724
725         return pos;
726 }
727
728
729 /*
730  * chcr_write_cpl_set_tcb_ulp: update tcb values.
731  * TCB is responsible to create tcp headers, so all the related values
732  * should be correctly updated.
733  * @tx_info - driver specific tls info.
734  * @q - tx queue on which packet is going out.
735  * @tid - TCB identifier.
736  * @pos - current index where should we start writing.
737  * @word - TCB word.
738  * @mask - TCB word related mask.
739  * @val - TCB word related value.
740  * @reply - set 1 if looking for TP response.
741  * return - next position to write.
742  */
743 static void *chcr_write_cpl_set_tcb_ulp(struct chcr_ktls_info *tx_info,
744                                         struct sge_eth_txq *q, u32 tid,
745                                         void *pos, u16 word, u64 mask,
746                                         u64 val, u32 reply)
747 {
748         int left = (void *)q->q.stat - pos;
749
750         if (unlikely(left < CHCR_SET_TCB_FIELD_LEN)) {
751                 if (!left) {
752                         pos = q->q.desc;
753                 } else {
754                         u8 buf[48] = {0};
755
756                         __chcr_write_cpl_set_tcb_ulp(tx_info, tid, buf, word,
757                                                      mask, val, reply);
758
759                         return chcr_copy_to_txd(buf, &q->q, pos,
760                                                 CHCR_SET_TCB_FIELD_LEN);
761                 }
762         }
763
764         pos = __chcr_write_cpl_set_tcb_ulp(tx_info, tid, pos, word,
765                                            mask, val, reply);
766
767         /* check again if we are at the end of the queue */
768         if (left == CHCR_SET_TCB_FIELD_LEN)
769                 pos = q->q.desc;
770
771         return pos;
772 }
773
774 /*
775  * chcr_ktls_xmit_tcb_cpls: update tcb entry so that TP will create the header
776  * with updated values like tcp seq, ack, window etc.
777  * @tx_info - driver specific tls info.
778  * @q - TX queue.
779  * @tcp_seq
780  * @tcp_ack
781  * @tcp_win
782  * return: NETDEV_TX_BUSY/NET_TX_OK.
783  */
784 static int chcr_ktls_xmit_tcb_cpls(struct chcr_ktls_info *tx_info,
785                                    struct sge_eth_txq *q, u64 tcp_seq,
786                                    u64 tcp_ack, u64 tcp_win)
787 {
788         bool first_wr = ((tx_info->prev_ack == 0) && (tx_info->prev_win == 0));
789         struct ch_ktls_port_stats_debug *port_stats;
790         u32 len, cpl = 0, ndesc, wr_len;
791         struct fw_ulptx_wr *wr;
792         int credits;
793         void *pos;
794
795         wr_len = sizeof(*wr);
796         /* there can be max 4 cpls, check if we have enough credits */
797         len = wr_len + 4 * roundup(CHCR_SET_TCB_FIELD_LEN, 16);
798         ndesc = DIV_ROUND_UP(len, 64);
799
800         credits = chcr_txq_avail(&q->q) - ndesc;
801         if (unlikely(credits < 0)) {
802                 chcr_eth_txq_stop(q);
803                 return NETDEV_TX_BUSY;
804         }
805
806         pos = &q->q.desc[q->q.pidx];
807         /* make space for WR, we'll fill it later when we know all the cpls
808          * being sent out and have complete length.
809          */
810         wr = pos;
811         pos += wr_len;
812         /* update tx_max if its a re-transmit or the first wr */
813         if (first_wr || tcp_seq != tx_info->prev_seq) {
814                 pos = chcr_write_cpl_set_tcb_ulp(tx_info, q, tx_info->tid, pos,
815                                                  TCB_TX_MAX_W,
816                                                  TCB_TX_MAX_V(TCB_TX_MAX_M),
817                                                  TCB_TX_MAX_V(tcp_seq), 0);
818                 cpl++;
819         }
820         /* reset snd una if it's a re-transmit pkt */
821         if (tcp_seq != tx_info->prev_seq) {
822                 /* reset snd_una */
823                 port_stats =
824                         &tx_info->adap->ch_ktls_stats.ktls_port[tx_info->port_id];
825                 pos = chcr_write_cpl_set_tcb_ulp(tx_info, q, tx_info->tid, pos,
826                                                  TCB_SND_UNA_RAW_W,
827                                                  TCB_SND_UNA_RAW_V
828                                                  (TCB_SND_UNA_RAW_M),
829                                                  TCB_SND_UNA_RAW_V(0), 0);
830                 atomic64_inc(&port_stats->ktls_tx_ooo);
831                 cpl++;
832         }
833         /* update ack */
834         if (first_wr || tx_info->prev_ack != tcp_ack) {
835                 pos = chcr_write_cpl_set_tcb_ulp(tx_info, q, tx_info->tid, pos,
836                                                  TCB_RCV_NXT_W,
837                                                  TCB_RCV_NXT_V(TCB_RCV_NXT_M),
838                                                  TCB_RCV_NXT_V(tcp_ack), 0);
839                 tx_info->prev_ack = tcp_ack;
840                 cpl++;
841         }
842         /* update receive window */
843         if (first_wr || tx_info->prev_win != tcp_win) {
844                 pos = chcr_write_cpl_set_tcb_ulp(tx_info, q, tx_info->tid, pos,
845                                                  TCB_RCV_WND_W,
846                                                  TCB_RCV_WND_V(TCB_RCV_WND_M),
847                                                  TCB_RCV_WND_V(tcp_win), 0);
848                 tx_info->prev_win = tcp_win;
849                 cpl++;
850         }
851
852         if (cpl) {
853                 /* get the actual length */
854                 len = wr_len + cpl * roundup(CHCR_SET_TCB_FIELD_LEN, 16);
855                 /* ULPTX wr */
856                 wr->op_to_compl = htonl(FW_WR_OP_V(FW_ULPTX_WR));
857                 wr->cookie = 0;
858                 /* fill len in wr field */
859                 wr->flowid_len16 = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(len, 16)));
860
861                 ndesc = DIV_ROUND_UP(len, 64);
862                 chcr_txq_advance(&q->q, ndesc);
863                 cxgb4_ring_tx_db(tx_info->adap, &q->q, ndesc);
864         }
865         return 0;
866 }
867
868 /*
869  * chcr_ktls_skb_copy
870  * @nskb - new skb where the frags to be added.
871  * @skb - old skb from which frags will be copied.
872  */
873 static void chcr_ktls_skb_copy(struct sk_buff *skb, struct sk_buff *nskb)
874 {
875         int i;
876
877         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
878                 skb_shinfo(nskb)->frags[i] = skb_shinfo(skb)->frags[i];
879                 __skb_frag_ref(&skb_shinfo(nskb)->frags[i]);
880         }
881
882         skb_shinfo(nskb)->nr_frags = skb_shinfo(skb)->nr_frags;
883         nskb->len += skb->data_len;
884         nskb->data_len = skb->data_len;
885         nskb->truesize += skb->data_len;
886 }
887
888 /*
889  * chcr_ktls_get_tx_flits
890  * returns number of flits to be sent out, it includes key context length, WR
891  * size and skb fragments.
892  */
893 static unsigned int
894 chcr_ktls_get_tx_flits(const struct sk_buff *skb, unsigned int key_ctx_len)
895 {
896         return chcr_sgl_len(skb_shinfo(skb)->nr_frags) +
897                DIV_ROUND_UP(key_ctx_len + CHCR_KTLS_WR_SIZE, 8);
898 }
899
900 /*
901  * chcr_ktls_check_tcp_options: To check if there is any TCP option availbale
902  * other than timestamp.
903  * @skb - skb contains partial record..
904  * return: 1 / 0
905  */
906 static int
907 chcr_ktls_check_tcp_options(struct tcphdr *tcp)
908 {
909         int cnt, opt, optlen;
910         u_char *cp;
911
912         cp = (u_char *)(tcp + 1);
913         cnt = (tcp->doff << 2) - sizeof(struct tcphdr);
914         for (; cnt > 0; cnt -= optlen, cp += optlen) {
915                 opt = cp[0];
916                 if (opt == TCPOPT_EOL)
917                         break;
918                 if (opt == TCPOPT_NOP) {
919                         optlen = 1;
920                 } else {
921                         if (cnt < 2)
922                                 break;
923                         optlen = cp[1];
924                         if (optlen < 2 || optlen > cnt)
925                                 break;
926                 }
927                 switch (opt) {
928                 case TCPOPT_NOP:
929                         break;
930                 default:
931                         return 1;
932                 }
933         }
934         return 0;
935 }
936
937 /*
938  * chcr_ktls_write_tcp_options : TP can't send out all the options, we need to
939  * send out separately.
940  * @tx_info - driver specific tls info.
941  * @skb - skb contains partial record..
942  * @q - TX queue.
943  * @tx_chan - channel number.
944  * return: NETDEV_TX_OK/NETDEV_TX_BUSY.
945  */
946 static int
947 chcr_ktls_write_tcp_options(struct chcr_ktls_info *tx_info, struct sk_buff *skb,
948                             struct sge_eth_txq *q, uint32_t tx_chan)
949 {
950         struct fw_eth_tx_pkt_wr *wr;
951         struct cpl_tx_pkt_core *cpl;
952         u32 ctrl, iplen, maclen;
953 #if IS_ENABLED(CONFIG_IPV6)
954         struct ipv6hdr *ip6;
955 #endif
956         unsigned int ndesc;
957         struct tcphdr *tcp;
958         int len16, pktlen;
959         struct iphdr *ip;
960         int credits;
961         u8 buf[150];
962         void *pos;
963
964         iplen = skb_network_header_len(skb);
965         maclen = skb_mac_header_len(skb);
966
967         /* packet length = eth hdr len + ip hdr len + tcp hdr len
968          * (including options).
969          */
970         pktlen = skb->len - skb->data_len;
971
972         ctrl = sizeof(*cpl) + pktlen;
973         len16 = DIV_ROUND_UP(sizeof(*wr) + ctrl, 16);
974         /* check how many descriptors needed */
975         ndesc = DIV_ROUND_UP(len16, 4);
976
977         credits = chcr_txq_avail(&q->q) - ndesc;
978         if (unlikely(credits < 0)) {
979                 chcr_eth_txq_stop(q);
980                 return NETDEV_TX_BUSY;
981         }
982
983         pos = &q->q.desc[q->q.pidx];
984         wr = pos;
985
986         /* Firmware work request header */
987         wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
988                                FW_WR_IMMDLEN_V(ctrl));
989
990         wr->equiq_to_len16 = htonl(FW_WR_LEN16_V(len16));
991         wr->r3 = 0;
992
993         cpl = (void *)(wr + 1);
994
995         /* CPL header */
996         cpl->ctrl0 = htonl(TXPKT_OPCODE_V(CPL_TX_PKT) | TXPKT_INTF_V(tx_chan) |
997                            TXPKT_PF_V(tx_info->adap->pf));
998         cpl->pack = 0;
999         cpl->len = htons(pktlen);
1000         /* checksum offload */
1001         cpl->ctrl1 = 0;
1002
1003         pos = cpl + 1;
1004
1005         memcpy(buf, skb->data, pktlen);
1006         if (tx_info->ip_family == AF_INET) {
1007                 /* we need to correct ip header len */
1008                 ip = (struct iphdr *)(buf + maclen);
1009                 ip->tot_len = htons(pktlen - maclen);
1010 #if IS_ENABLED(CONFIG_IPV6)
1011         } else {
1012                 ip6 = (struct ipv6hdr *)(buf + maclen);
1013                 ip6->payload_len = htons(pktlen - maclen - iplen);
1014 #endif
1015         }
1016         /* now take care of the tcp header, if fin is not set then clear push
1017          * bit as well, and if fin is set, it will be sent at the last so we
1018          * need to update the tcp sequence number as per the last packet.
1019          */
1020         tcp = (struct tcphdr *)(buf + maclen + iplen);
1021
1022         if (!tcp->fin)
1023                 tcp->psh = 0;
1024         else
1025                 tcp->seq = htonl(tx_info->prev_seq);
1026
1027         chcr_copy_to_txd(buf, &q->q, pos, pktlen);
1028
1029         chcr_txq_advance(&q->q, ndesc);
1030         cxgb4_ring_tx_db(tx_info->adap, &q->q, ndesc);
1031         return 0;
1032 }
1033
1034 /* chcr_ktls_skb_shift - Shifts request length paged data from skb to another.
1035  * @tgt- buffer into which tail data gets added
1036  * @skb- buffer from which the paged data comes from
1037  * @shiftlen- shift up to this many bytes
1038  */
1039 static int chcr_ktls_skb_shift(struct sk_buff *tgt, struct sk_buff *skb,
1040                                int shiftlen)
1041 {
1042         skb_frag_t *fragfrom, *fragto;
1043         int from, to, todo;
1044
1045         WARN_ON(shiftlen > skb->data_len);
1046
1047         todo = shiftlen;
1048         from = 0;
1049         to = 0;
1050         fragfrom = &skb_shinfo(skb)->frags[from];
1051
1052         while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
1053                 fragfrom = &skb_shinfo(skb)->frags[from];
1054                 fragto = &skb_shinfo(tgt)->frags[to];
1055
1056                 if (todo >= skb_frag_size(fragfrom)) {
1057                         *fragto = *fragfrom;
1058                         todo -= skb_frag_size(fragfrom);
1059                         from++;
1060                         to++;
1061
1062                 } else {
1063                         __skb_frag_ref(fragfrom);
1064                         skb_frag_page_copy(fragto, fragfrom);
1065                         skb_frag_off_copy(fragto, fragfrom);
1066                         skb_frag_size_set(fragto, todo);
1067
1068                         skb_frag_off_add(fragfrom, todo);
1069                         skb_frag_size_sub(fragfrom, todo);
1070                         todo = 0;
1071
1072                         to++;
1073                         break;
1074                 }
1075         }
1076
1077         /* Ready to "commit" this state change to tgt */
1078         skb_shinfo(tgt)->nr_frags = to;
1079
1080         /* Reposition in the original skb */
1081         to = 0;
1082         while (from < skb_shinfo(skb)->nr_frags)
1083                 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
1084
1085         skb_shinfo(skb)->nr_frags = to;
1086
1087         WARN_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
1088
1089         skb->len -= shiftlen;
1090         skb->data_len -= shiftlen;
1091         skb->truesize -= shiftlen;
1092         tgt->len += shiftlen;
1093         tgt->data_len += shiftlen;
1094         tgt->truesize += shiftlen;
1095
1096         return shiftlen;
1097 }
1098
1099 /*
1100  * chcr_ktls_xmit_wr_complete: This sends out the complete record. If an skb
1101  * received has partial end part of the record, send out the complete record, so
1102  * that crypto block will be able to generate TAG/HASH.
1103  * @skb - segment which has complete or partial end part.
1104  * @tx_info - driver specific tls info.
1105  * @q - TX queue.
1106  * @tcp_seq
1107  * @tcp_push - tcp push bit.
1108  * @mss - segment size.
1109  * return: NETDEV_TX_BUSY/NET_TX_OK.
1110  */
1111 static int chcr_ktls_xmit_wr_complete(struct sk_buff *skb,
1112                                       struct chcr_ktls_info *tx_info,
1113                                       struct sge_eth_txq *q, u32 tcp_seq,
1114                                       bool tcp_push, u32 mss)
1115 {
1116         u32 len16, wr_mid = 0, flits = 0, ndesc, cipher_start;
1117         struct adapter *adap = tx_info->adap;
1118         int credits, left, last_desc;
1119         struct tx_sw_desc *sgl_sdesc;
1120         struct cpl_tx_data *tx_data;
1121         struct cpl_tx_sec_pdu *cpl;
1122         struct ulptx_idata *idata;
1123         struct ulp_txpkt *ulptx;
1124         struct fw_ulptx_wr *wr;
1125         void *pos;
1126         u64 *end;
1127
1128         /* get the number of flits required */
1129         flits = chcr_ktls_get_tx_flits(skb, tx_info->key_ctx_len);
1130         /* number of descriptors */
1131         ndesc = chcr_flits_to_desc(flits);
1132         /* check if enough credits available */
1133         credits = chcr_txq_avail(&q->q) - ndesc;
1134         if (unlikely(credits < 0)) {
1135                 chcr_eth_txq_stop(q);
1136                 return NETDEV_TX_BUSY;
1137         }
1138
1139         if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1140                 /* Credits are below the threshold vaues, stop the queue after
1141                  * injecting the Work Request for this packet.
1142                  */
1143                 chcr_eth_txq_stop(q);
1144                 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
1145         }
1146
1147         last_desc = q->q.pidx + ndesc - 1;
1148         if (last_desc >= q->q.size)
1149                 last_desc -= q->q.size;
1150         sgl_sdesc = &q->q.sdesc[last_desc];
1151
1152         if (unlikely(cxgb4_map_skb(adap->pdev_dev, skb, sgl_sdesc->addr) < 0)) {
1153                 memset(sgl_sdesc->addr, 0, sizeof(sgl_sdesc->addr));
1154                 q->mapping_err++;
1155                 return NETDEV_TX_BUSY;
1156         }
1157
1158         pos = &q->q.desc[q->q.pidx];
1159         end = (u64 *)pos + flits;
1160         /* FW_ULPTX_WR */
1161         wr = pos;
1162         /* WR will need len16 */
1163         len16 = DIV_ROUND_UP(flits, 2);
1164         wr->op_to_compl = htonl(FW_WR_OP_V(FW_ULPTX_WR));
1165         wr->flowid_len16 = htonl(wr_mid | FW_WR_LEN16_V(len16));
1166         wr->cookie = 0;
1167         pos += sizeof(*wr);
1168         /* ULP_TXPKT */
1169         ulptx = pos;
1170         ulptx->cmd_dest = htonl(ULPTX_CMD_V(ULP_TX_PKT) |
1171                                 ULP_TXPKT_CHANNELID_V(tx_info->port_id) |
1172                                 ULP_TXPKT_FID_V(q->q.cntxt_id) |
1173                                 ULP_TXPKT_RO_F);
1174         ulptx->len = htonl(len16 - 1);
1175         /* ULPTX_IDATA sub-command */
1176         idata = (struct ulptx_idata *)(ulptx + 1);
1177         idata->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM) | ULP_TX_SC_MORE_F);
1178         /* idata length will include cpl_tx_sec_pdu + key context size +
1179          * cpl_tx_data header.
1180          */
1181         idata->len = htonl(sizeof(*cpl) + tx_info->key_ctx_len +
1182                            sizeof(*tx_data));
1183         /* SEC CPL */
1184         cpl = (struct cpl_tx_sec_pdu *)(idata + 1);
1185         cpl->op_ivinsrtofst =
1186                 htonl(CPL_TX_SEC_PDU_OPCODE_V(CPL_TX_SEC_PDU) |
1187                       CPL_TX_SEC_PDU_CPLLEN_V(CHCR_CPL_TX_SEC_PDU_LEN_64BIT) |
1188                       CPL_TX_SEC_PDU_PLACEHOLDER_V(1) |
1189                       CPL_TX_SEC_PDU_IVINSRTOFST_V(TLS_HEADER_SIZE + 1));
1190         cpl->pldlen = htonl(skb->data_len);
1191
1192         /* encryption should start after tls header size + iv size */
1193         cipher_start = TLS_HEADER_SIZE + tx_info->iv_size + 1;
1194
1195         cpl->aadstart_cipherstop_hi =
1196                 htonl(CPL_TX_SEC_PDU_AADSTART_V(1) |
1197                       CPL_TX_SEC_PDU_AADSTOP_V(TLS_HEADER_SIZE) |
1198                       CPL_TX_SEC_PDU_CIPHERSTART_V(cipher_start));
1199
1200         /* authentication will also start after tls header + iv size */
1201         cpl->cipherstop_lo_authinsert =
1202         htonl(CPL_TX_SEC_PDU_AUTHSTART_V(cipher_start) |
1203               CPL_TX_SEC_PDU_AUTHSTOP_V(TLS_CIPHER_AES_GCM_128_TAG_SIZE) |
1204               CPL_TX_SEC_PDU_AUTHINSERT_V(TLS_CIPHER_AES_GCM_128_TAG_SIZE));
1205
1206         /* These two flits are actually a CPL_TLS_TX_SCMD_FMT. */
1207         cpl->seqno_numivs = htonl(tx_info->scmd0_seqno_numivs);
1208         cpl->ivgen_hdrlen = htonl(tx_info->scmd0_ivgen_hdrlen);
1209         cpl->scmd1 = cpu_to_be64(tx_info->record_no);
1210
1211         pos = cpl + 1;
1212         /* check if space left to fill the keys */
1213         left = (void *)q->q.stat - pos;
1214         if (!left) {
1215                 left = (void *)end - (void *)q->q.stat;
1216                 pos = q->q.desc;
1217                 end = pos + left;
1218         }
1219
1220         pos = chcr_copy_to_txd(&tx_info->key_ctx, &q->q, pos,
1221                                tx_info->key_ctx_len);
1222         left = (void *)q->q.stat - pos;
1223
1224         if (!left) {
1225                 left = (void *)end - (void *)q->q.stat;
1226                 pos = q->q.desc;
1227                 end = pos + left;
1228         }
1229         /* CPL_TX_DATA */
1230         tx_data = (void *)pos;
1231         OPCODE_TID(tx_data) = htonl(MK_OPCODE_TID(CPL_TX_DATA, tx_info->tid));
1232         tx_data->len = htonl(TX_DATA_MSS_V(mss) | TX_LENGTH_V(skb->data_len));
1233
1234         tx_data->rsvd = htonl(tcp_seq);
1235
1236         tx_data->flags = htonl(TX_BYPASS_F);
1237         if (tcp_push)
1238                 tx_data->flags |= htonl(TX_PUSH_F | TX_SHOVE_F);
1239
1240         /* check left again, it might go beyond queue limit */
1241         pos = tx_data + 1;
1242         left = (void *)q->q.stat - pos;
1243
1244         /* check the position again */
1245         if (!left) {
1246                 left = (void *)end - (void *)q->q.stat;
1247                 pos = q->q.desc;
1248                 end = pos + left;
1249         }
1250
1251         /* send the complete packet except the header */
1252         cxgb4_write_sgl(skb, &q->q, pos, end, skb->len - skb->data_len,
1253                         sgl_sdesc->addr);
1254         sgl_sdesc->skb = skb;
1255
1256         chcr_txq_advance(&q->q, ndesc);
1257         cxgb4_ring_tx_db(adap, &q->q, ndesc);
1258         atomic64_inc(&adap->ch_ktls_stats.ktls_tx_send_records);
1259
1260         return 0;
1261 }
1262
1263 /*
1264  * chcr_ktls_xmit_wr_short: This is to send out partial records. If its
1265  * a middle part of a record, fetch the prior data to make it 16 byte aligned
1266  * and then only send it out.
1267  *
1268  * @skb - skb contains partial record..
1269  * @tx_info - driver specific tls info.
1270  * @q - TX queue.
1271  * @tcp_seq
1272  * @tcp_push - tcp push bit.
1273  * @mss - segment size.
1274  * @tls_rec_offset - offset from start of the tls record.
1275  * @perior_data - data before the current segment, required to make this record
1276  *                16 byte aligned.
1277  * @prior_data_len - prior_data length (less than 16)
1278  * return: NETDEV_TX_BUSY/NET_TX_OK.
1279  */
1280 static int chcr_ktls_xmit_wr_short(struct sk_buff *skb,
1281                                    struct chcr_ktls_info *tx_info,
1282                                    struct sge_eth_txq *q,
1283                                    u32 tcp_seq, bool tcp_push, u32 mss,
1284                                    u32 tls_rec_offset, u8 *prior_data,
1285                                    u32 prior_data_len)
1286 {
1287         struct adapter *adap = tx_info->adap;
1288         u32 len16, wr_mid = 0, cipher_start;
1289         unsigned int flits = 0, ndesc;
1290         int credits, left, last_desc;
1291         struct tx_sw_desc *sgl_sdesc;
1292         struct cpl_tx_data *tx_data;
1293         struct cpl_tx_sec_pdu *cpl;
1294         struct ulptx_idata *idata;
1295         struct ulp_txpkt *ulptx;
1296         struct fw_ulptx_wr *wr;
1297         __be64 iv_record;
1298         void *pos;
1299         u64 *end;
1300
1301         /* get the number of flits required, it's a partial record so 2 flits
1302          * (AES_BLOCK_SIZE) will be added.
1303          */
1304         flits = chcr_ktls_get_tx_flits(skb, tx_info->key_ctx_len) + 2;
1305         /* get the correct 8 byte IV of this record */
1306         iv_record = cpu_to_be64(tx_info->iv + tx_info->record_no);
1307         /* If it's a middle record and not 16 byte aligned to run AES CTR, need
1308          * to make it 16 byte aligned. So atleadt 2 extra flits of immediate
1309          * data will be added.
1310          */
1311         if (prior_data_len)
1312                 flits += 2;
1313         /* number of descriptors */
1314         ndesc = chcr_flits_to_desc(flits);
1315         /* check if enough credits available */
1316         credits = chcr_txq_avail(&q->q) - ndesc;
1317         if (unlikely(credits < 0)) {
1318                 chcr_eth_txq_stop(q);
1319                 return NETDEV_TX_BUSY;
1320         }
1321
1322         if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1323                 chcr_eth_txq_stop(q);
1324                 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
1325         }
1326
1327         last_desc = q->q.pidx + ndesc - 1;
1328         if (last_desc >= q->q.size)
1329                 last_desc -= q->q.size;
1330         sgl_sdesc = &q->q.sdesc[last_desc];
1331
1332         if (unlikely(cxgb4_map_skb(adap->pdev_dev, skb, sgl_sdesc->addr) < 0)) {
1333                 memset(sgl_sdesc->addr, 0, sizeof(sgl_sdesc->addr));
1334                 q->mapping_err++;
1335                 return NETDEV_TX_BUSY;
1336         }
1337
1338         pos = &q->q.desc[q->q.pidx];
1339         end = (u64 *)pos + flits;
1340         /* FW_ULPTX_WR */
1341         wr = pos;
1342         /* WR will need len16 */
1343         len16 = DIV_ROUND_UP(flits, 2);
1344         wr->op_to_compl = htonl(FW_WR_OP_V(FW_ULPTX_WR));
1345         wr->flowid_len16 = htonl(wr_mid | FW_WR_LEN16_V(len16));
1346         wr->cookie = 0;
1347         pos += sizeof(*wr);
1348         /* ULP_TXPKT */
1349         ulptx = pos;
1350         ulptx->cmd_dest = htonl(ULPTX_CMD_V(ULP_TX_PKT) |
1351                                 ULP_TXPKT_CHANNELID_V(tx_info->port_id) |
1352                                 ULP_TXPKT_FID_V(q->q.cntxt_id) |
1353                                 ULP_TXPKT_RO_F);
1354         ulptx->len = htonl(len16 - 1);
1355         /* ULPTX_IDATA sub-command */
1356         idata = (struct ulptx_idata *)(ulptx + 1);
1357         idata->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM) | ULP_TX_SC_MORE_F);
1358         /* idata length will include cpl_tx_sec_pdu + key context size +
1359          * cpl_tx_data header.
1360          */
1361         idata->len = htonl(sizeof(*cpl) + tx_info->key_ctx_len +
1362                            sizeof(*tx_data) + AES_BLOCK_LEN + prior_data_len);
1363         /* SEC CPL */
1364         cpl = (struct cpl_tx_sec_pdu *)(idata + 1);
1365         /* cipher start will have tls header + iv size extra if its a header
1366          * part of tls record. else only 16 byte IV will be added.
1367          */
1368         cipher_start =
1369                 AES_BLOCK_LEN + 1 +
1370                 (!tls_rec_offset ? TLS_HEADER_SIZE + tx_info->iv_size : 0);
1371
1372         cpl->op_ivinsrtofst =
1373                 htonl(CPL_TX_SEC_PDU_OPCODE_V(CPL_TX_SEC_PDU) |
1374                       CPL_TX_SEC_PDU_CPLLEN_V(CHCR_CPL_TX_SEC_PDU_LEN_64BIT) |
1375                       CPL_TX_SEC_PDU_IVINSRTOFST_V(1));
1376         cpl->pldlen = htonl(skb->data_len + AES_BLOCK_LEN + prior_data_len);
1377         cpl->aadstart_cipherstop_hi =
1378                 htonl(CPL_TX_SEC_PDU_CIPHERSTART_V(cipher_start));
1379         cpl->cipherstop_lo_authinsert = 0;
1380         /* These two flits are actually a CPL_TLS_TX_SCMD_FMT. */
1381         cpl->seqno_numivs = htonl(tx_info->scmd0_short_seqno_numivs);
1382         cpl->ivgen_hdrlen = htonl(tx_info->scmd0_short_ivgen_hdrlen);
1383         cpl->scmd1 = 0;
1384
1385         pos = cpl + 1;
1386         /* check if space left to fill the keys */
1387         left = (void *)q->q.stat - pos;
1388         if (!left) {
1389                 left = (void *)end - (void *)q->q.stat;
1390                 pos = q->q.desc;
1391                 end = pos + left;
1392         }
1393
1394         pos = chcr_copy_to_txd(&tx_info->key_ctx, &q->q, pos,
1395                                tx_info->key_ctx_len);
1396         left = (void *)q->q.stat - pos;
1397
1398         if (!left) {
1399                 left = (void *)end - (void *)q->q.stat;
1400                 pos = q->q.desc;
1401                 end = pos + left;
1402         }
1403         /* CPL_TX_DATA */
1404         tx_data = (void *)pos;
1405         OPCODE_TID(tx_data) = htonl(MK_OPCODE_TID(CPL_TX_DATA, tx_info->tid));
1406         tx_data->len = htonl(TX_DATA_MSS_V(mss) |
1407                         TX_LENGTH_V(skb->data_len + prior_data_len));
1408         tx_data->rsvd = htonl(tcp_seq);
1409         tx_data->flags = htonl(TX_BYPASS_F);
1410         if (tcp_push)
1411                 tx_data->flags |= htonl(TX_PUSH_F | TX_SHOVE_F);
1412
1413         /* check left again, it might go beyond queue limit */
1414         pos = tx_data + 1;
1415         left = (void *)q->q.stat - pos;
1416
1417         /* check the position again */
1418         if (!left) {
1419                 left = (void *)end - (void *)q->q.stat;
1420                 pos = q->q.desc;
1421                 end = pos + left;
1422         }
1423         /* copy the 16 byte IV for AES-CTR, which includes 4 bytes of salt, 8
1424          * bytes of actual IV and 4 bytes of 16 byte-sequence.
1425          */
1426         memcpy(pos, tx_info->key_ctx.salt, tx_info->salt_size);
1427         memcpy(pos + tx_info->salt_size, &iv_record, tx_info->iv_size);
1428         *(__be32 *)(pos + tx_info->salt_size + tx_info->iv_size) =
1429                 htonl(2 + (tls_rec_offset ? ((tls_rec_offset -
1430                 (TLS_HEADER_SIZE + tx_info->iv_size)) / AES_BLOCK_LEN) : 0));
1431
1432         pos += 16;
1433         /* Prior_data_len will always be less than 16 bytes, fill the
1434          * prio_data_len after AES_CTRL_BLOCK and clear the remaining length
1435          * to 0.
1436          */
1437         if (prior_data_len)
1438                 pos = chcr_copy_to_txd(prior_data, &q->q, pos, 16);
1439         /* send the complete packet except the header */
1440         cxgb4_write_sgl(skb, &q->q, pos, end, skb->len - skb->data_len,
1441                         sgl_sdesc->addr);
1442         sgl_sdesc->skb = skb;
1443
1444         chcr_txq_advance(&q->q, ndesc);
1445         cxgb4_ring_tx_db(adap, &q->q, ndesc);
1446
1447         return 0;
1448 }
1449
1450 /*
1451  * chcr_ktls_tx_plaintxt: This handler will take care of the records which has
1452  * only plain text (only tls header and iv)
1453  * @tx_info - driver specific tls info.
1454  * @skb - skb contains partial record..
1455  * @tcp_seq
1456  * @mss - segment size.
1457  * @tcp_push - tcp push bit.
1458  * @q - TX queue.
1459  * @port_id : port number
1460  * @perior_data - data before the current segment, required to make this record
1461  *               16 byte aligned.
1462  * @prior_data_len - prior_data length (less than 16)
1463  * return: NETDEV_TX_BUSY/NET_TX_OK.
1464  */
1465 static int chcr_ktls_tx_plaintxt(struct chcr_ktls_info *tx_info,
1466                                  struct sk_buff *skb, u32 tcp_seq, u32 mss,
1467                                  bool tcp_push, struct sge_eth_txq *q,
1468                                  u32 port_id, u8 *prior_data,
1469                                  u32 prior_data_len)
1470 {
1471         int credits, left, len16, last_desc;
1472         unsigned int flits = 0, ndesc;
1473         struct tx_sw_desc *sgl_sdesc;
1474         struct cpl_tx_data *tx_data;
1475         struct ulptx_idata *idata;
1476         struct ulp_txpkt *ulptx;
1477         struct fw_ulptx_wr *wr;
1478         u32 wr_mid = 0;
1479         void *pos;
1480         u64 *end;
1481
1482         flits = DIV_ROUND_UP(CHCR_PLAIN_TX_DATA_LEN, 8);
1483         flits += chcr_sgl_len(skb_shinfo(skb)->nr_frags);
1484         if (prior_data_len)
1485                 flits += 2;
1486         /* WR will need len16 */
1487         len16 = DIV_ROUND_UP(flits, 2);
1488         /* check how many descriptors needed */
1489         ndesc = DIV_ROUND_UP(flits, 8);
1490
1491         credits = chcr_txq_avail(&q->q) - ndesc;
1492         if (unlikely(credits < 0)) {
1493                 chcr_eth_txq_stop(q);
1494                 return NETDEV_TX_BUSY;
1495         }
1496
1497         if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1498                 chcr_eth_txq_stop(q);
1499                 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
1500         }
1501
1502         last_desc = q->q.pidx + ndesc - 1;
1503         if (last_desc >= q->q.size)
1504                 last_desc -= q->q.size;
1505         sgl_sdesc = &q->q.sdesc[last_desc];
1506
1507         if (unlikely(cxgb4_map_skb(tx_info->adap->pdev_dev, skb,
1508                                    sgl_sdesc->addr) < 0)) {
1509                 memset(sgl_sdesc->addr, 0, sizeof(sgl_sdesc->addr));
1510                 q->mapping_err++;
1511                 return NETDEV_TX_BUSY;
1512         }
1513
1514         pos = &q->q.desc[q->q.pidx];
1515         end = (u64 *)pos + flits;
1516         /* FW_ULPTX_WR */
1517         wr = pos;
1518         wr->op_to_compl = htonl(FW_WR_OP_V(FW_ULPTX_WR));
1519         wr->flowid_len16 = htonl(wr_mid | FW_WR_LEN16_V(len16));
1520         wr->cookie = 0;
1521         pos += sizeof(*wr);
1522         /* ULP_TXPKT */
1523         ulptx = (struct ulp_txpkt *)(wr + 1);
1524         ulptx->cmd_dest = htonl(ULPTX_CMD_V(ULP_TX_PKT) |
1525                         ULP_TXPKT_DATAMODIFY_V(0) |
1526                         ULP_TXPKT_CHANNELID_V(tx_info->port_id) |
1527                         ULP_TXPKT_DEST_V(0) |
1528                         ULP_TXPKT_FID_V(q->q.cntxt_id) | ULP_TXPKT_RO_V(1));
1529         ulptx->len = htonl(len16 - 1);
1530         /* ULPTX_IDATA sub-command */
1531         idata = (struct ulptx_idata *)(ulptx + 1);
1532         idata->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM) | ULP_TX_SC_MORE_F);
1533         idata->len = htonl(sizeof(*tx_data) + prior_data_len);
1534         /* CPL_TX_DATA */
1535         tx_data = (struct cpl_tx_data *)(idata + 1);
1536         OPCODE_TID(tx_data) = htonl(MK_OPCODE_TID(CPL_TX_DATA, tx_info->tid));
1537         tx_data->len = htonl(TX_DATA_MSS_V(mss) |
1538                         TX_LENGTH_V(skb->data_len + prior_data_len));
1539         /* set tcp seq number */
1540         tx_data->rsvd = htonl(tcp_seq);
1541         tx_data->flags = htonl(TX_BYPASS_F);
1542         if (tcp_push)
1543                 tx_data->flags |= htonl(TX_PUSH_F | TX_SHOVE_F);
1544
1545         pos = tx_data + 1;
1546         /* apart from prior_data_len, we should set remaining part of 16 bytes
1547          * to be zero.
1548          */
1549         if (prior_data_len)
1550                 pos = chcr_copy_to_txd(prior_data, &q->q, pos, 16);
1551
1552         /* check left again, it might go beyond queue limit */
1553         left = (void *)q->q.stat - pos;
1554
1555         /* check the position again */
1556         if (!left) {
1557                 left = (void *)end - (void *)q->q.stat;
1558                 pos = q->q.desc;
1559                 end = pos + left;
1560         }
1561         /* send the complete packet including the header */
1562         cxgb4_write_sgl(skb, &q->q, pos, end, skb->len - skb->data_len,
1563                         sgl_sdesc->addr);
1564         sgl_sdesc->skb = skb;
1565
1566         chcr_txq_advance(&q->q, ndesc);
1567         cxgb4_ring_tx_db(tx_info->adap, &q->q, ndesc);
1568         return 0;
1569 }
1570
1571 /*
1572  * chcr_ktls_copy_record_in_skb
1573  * @nskb - new skb where the frags to be added.
1574  * @record - specific record which has complete 16k record in frags.
1575  */
1576 static void chcr_ktls_copy_record_in_skb(struct sk_buff *nskb,
1577                                          struct tls_record_info *record)
1578 {
1579         int i = 0;
1580
1581         for (i = 0; i < record->num_frags; i++) {
1582                 skb_shinfo(nskb)->frags[i] = record->frags[i];
1583                 /* increase the frag ref count */
1584                 __skb_frag_ref(&skb_shinfo(nskb)->frags[i]);
1585         }
1586
1587         skb_shinfo(nskb)->nr_frags = record->num_frags;
1588         nskb->data_len = record->len;
1589         nskb->len += record->len;
1590         nskb->truesize += record->len;
1591 }
1592
1593 /*
1594  * chcr_ktls_update_snd_una:  Reset the SEND_UNA. It will be done to avoid
1595  * sending the same segment again. It will discard the segment which is before
1596  * the current tx max.
1597  * @tx_info - driver specific tls info.
1598  * @q - TX queue.
1599  * return: NET_TX_OK/NET_XMIT_DROP.
1600  */
1601 static int chcr_ktls_update_snd_una(struct chcr_ktls_info *tx_info,
1602                                     struct sge_eth_txq *q)
1603 {
1604         struct fw_ulptx_wr *wr;
1605         unsigned int ndesc;
1606         int credits;
1607         void *pos;
1608         u32 len;
1609
1610         len = sizeof(*wr) + roundup(CHCR_SET_TCB_FIELD_LEN, 16);
1611         ndesc = DIV_ROUND_UP(len, 64);
1612
1613         credits = chcr_txq_avail(&q->q) - ndesc;
1614         if (unlikely(credits < 0)) {
1615                 chcr_eth_txq_stop(q);
1616                 return NETDEV_TX_BUSY;
1617         }
1618
1619         pos = &q->q.desc[q->q.pidx];
1620
1621         wr = pos;
1622         /* ULPTX wr */
1623         wr->op_to_compl = htonl(FW_WR_OP_V(FW_ULPTX_WR));
1624         wr->cookie = 0;
1625         /* fill len in wr field */
1626         wr->flowid_len16 = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(len, 16)));
1627
1628         pos += sizeof(*wr);
1629
1630         pos = chcr_write_cpl_set_tcb_ulp(tx_info, q, tx_info->tid, pos,
1631                                          TCB_SND_UNA_RAW_W,
1632                                          TCB_SND_UNA_RAW_V(TCB_SND_UNA_RAW_M),
1633                                          TCB_SND_UNA_RAW_V(0), 0);
1634
1635         chcr_txq_advance(&q->q, ndesc);
1636         cxgb4_ring_tx_db(tx_info->adap, &q->q, ndesc);
1637
1638         return 0;
1639 }
1640
1641 /*
1642  * chcr_end_part_handler: This handler will handle the record which
1643  * is complete or if record's end part is received. T6 adapter has a issue that
1644  * it can't send out TAG with partial record so if its an end part then we have
1645  * to send TAG as well and for which we need to fetch the complete record and
1646  * send it to crypto module.
1647  * @tx_info - driver specific tls info.
1648  * @skb - skb contains partial record.
1649  * @record - complete record of 16K size.
1650  * @tcp_seq
1651  * @mss - segment size in which TP needs to chop a packet.
1652  * @tcp_push_no_fin - tcp push if fin is not set.
1653  * @q - TX queue.
1654  * @tls_end_offset - offset from end of the record.
1655  * @last wr : check if this is the last part of the skb going out.
1656  * return: NETDEV_TX_OK/NETDEV_TX_BUSY.
1657  */
1658 static int chcr_end_part_handler(struct chcr_ktls_info *tx_info,
1659                                  struct sk_buff *skb,
1660                                  struct tls_record_info *record,
1661                                  u32 tcp_seq, int mss, bool tcp_push_no_fin,
1662                                  struct sge_eth_txq *q,
1663                                  u32 tls_end_offset, bool last_wr)
1664 {
1665         struct sk_buff *nskb = NULL;
1666         /* check if it is a complete record */
1667         if (tls_end_offset == record->len) {
1668                 nskb = skb;
1669                 atomic64_inc(&tx_info->adap->ch_ktls_stats.ktls_tx_complete_pkts);
1670         } else {
1671                 dev_kfree_skb_any(skb);
1672
1673                 nskb = alloc_skb(0, GFP_KERNEL);
1674                 if (!nskb)
1675                         return NETDEV_TX_BUSY;
1676                 /* copy complete record in skb */
1677                 chcr_ktls_copy_record_in_skb(nskb, record);
1678                 /* packet is being sent from the beginning, update the tcp_seq
1679                  * accordingly.
1680                  */
1681                 tcp_seq = tls_record_start_seq(record);
1682                 /* reset snd una, so the middle record won't send the already
1683                  * sent part.
1684                  */
1685                 if (chcr_ktls_update_snd_una(tx_info, q))
1686                         goto out;
1687                 atomic64_inc(&tx_info->adap->ch_ktls_stats.ktls_tx_end_pkts);
1688         }
1689
1690         if (chcr_ktls_xmit_wr_complete(nskb, tx_info, q, tcp_seq,
1691                                        (last_wr && tcp_push_no_fin),
1692                                        mss)) {
1693                 goto out;
1694         }
1695         return 0;
1696 out:
1697         dev_kfree_skb_any(nskb);
1698         return NETDEV_TX_BUSY;
1699 }
1700
1701 /*
1702  * chcr_short_record_handler: This handler will take care of the records which
1703  * doesn't have end part (1st part or the middle part(/s) of a record). In such
1704  * cases, AES CTR will be used in place of AES GCM to send out partial packet.
1705  * This partial record might be the first part of the record, or the middle
1706  * part. In case of middle record we should fetch the prior data to make it 16
1707  * byte aligned. If it has a partial tls header or iv then get to the start of
1708  * tls header. And if it has partial TAG, then remove the complete TAG and send
1709  * only the payload.
1710  * There is one more possibility that it gets a partial header, send that
1711  * portion as a plaintext.
1712  * @tx_info - driver specific tls info.
1713  * @skb - skb contains partial record..
1714  * @record - complete record of 16K size.
1715  * @tcp_seq
1716  * @mss - segment size in which TP needs to chop a packet.
1717  * @tcp_push_no_fin - tcp push if fin is not set.
1718  * @q - TX queue.
1719  * @tls_end_offset - offset from end of the record.
1720  * return: NETDEV_TX_OK/NETDEV_TX_BUSY.
1721  */
1722 static int chcr_short_record_handler(struct chcr_ktls_info *tx_info,
1723                                      struct sk_buff *skb,
1724                                      struct tls_record_info *record,
1725                                      u32 tcp_seq, int mss, bool tcp_push_no_fin,
1726                                      struct sge_eth_txq *q, u32 tls_end_offset)
1727 {
1728         u32 tls_rec_offset = tcp_seq - tls_record_start_seq(record);
1729         u8 prior_data[16] = {0};
1730         u32 prior_data_len = 0;
1731         u32 data_len;
1732
1733         /* check if the skb is ending in middle of tag/HASH, its a big
1734          * trouble, send the packet before the HASH.
1735          */
1736         int remaining_record = tls_end_offset - skb->data_len;
1737
1738         if (remaining_record > 0 &&
1739             remaining_record < TLS_CIPHER_AES_GCM_128_TAG_SIZE) {
1740                 int trimmed_len = skb->data_len -
1741                         (TLS_CIPHER_AES_GCM_128_TAG_SIZE - remaining_record);
1742                 struct sk_buff *tmp_skb = NULL;
1743                 /* don't process the pkt if it is only a partial tag */
1744                 if (skb->data_len < TLS_CIPHER_AES_GCM_128_TAG_SIZE)
1745                         goto out;
1746
1747                 WARN_ON(trimmed_len > skb->data_len);
1748
1749                 /* shift to those many bytes */
1750                 tmp_skb = alloc_skb(0, GFP_KERNEL);
1751                 if (unlikely(!tmp_skb))
1752                         goto out;
1753
1754                 chcr_ktls_skb_shift(tmp_skb, skb, trimmed_len);
1755                 /* free the last trimmed portion */
1756                 dev_kfree_skb_any(skb);
1757                 skb = tmp_skb;
1758                 atomic64_inc(&tx_info->adap->ch_ktls_stats.ktls_tx_trimmed_pkts);
1759         }
1760         data_len = skb->data_len;
1761         /* check if the middle record's start point is 16 byte aligned. CTR
1762          * needs 16 byte aligned start point to start encryption.
1763          */
1764         if (tls_rec_offset) {
1765                 /* there is an offset from start, means its a middle record */
1766                 int remaining = 0;
1767
1768                 if (tls_rec_offset < (TLS_HEADER_SIZE + tx_info->iv_size)) {
1769                         prior_data_len = tls_rec_offset;
1770                         tls_rec_offset = 0;
1771                         remaining = 0;
1772                 } else {
1773                         prior_data_len =
1774                                 (tls_rec_offset -
1775                                 (TLS_HEADER_SIZE + tx_info->iv_size))
1776                                 % AES_BLOCK_LEN;
1777                         remaining = tls_rec_offset - prior_data_len;
1778                 }
1779
1780                 /* if prior_data_len is not zero, means we need to fetch prior
1781                  * data to make this record 16 byte aligned, or we need to reach
1782                  * to start offset.
1783                  */
1784                 if (prior_data_len) {
1785                         int i = 0;
1786                         u8 *data = NULL;
1787                         skb_frag_t *f;
1788                         u8 *vaddr;
1789                         int frag_size = 0, frag_delta = 0;
1790
1791                         while (remaining > 0) {
1792                                 frag_size = skb_frag_size(&record->frags[i]);
1793                                 if (remaining < frag_size)
1794                                         break;
1795
1796                                 remaining -= frag_size;
1797                                 i++;
1798                         }
1799                         f = &record->frags[i];
1800                         vaddr = kmap_atomic(skb_frag_page(f));
1801
1802                         data = vaddr + skb_frag_off(f)  + remaining;
1803                         frag_delta = skb_frag_size(f) - remaining;
1804
1805                         if (frag_delta >= prior_data_len) {
1806                                 memcpy(prior_data, data, prior_data_len);
1807                                 kunmap_atomic(vaddr);
1808                         } else {
1809                                 memcpy(prior_data, data, frag_delta);
1810                                 kunmap_atomic(vaddr);
1811                                 /* get the next page */
1812                                 f = &record->frags[i + 1];
1813                                 vaddr = kmap_atomic(skb_frag_page(f));
1814                                 data = vaddr + skb_frag_off(f);
1815                                 memcpy(prior_data + frag_delta,
1816                                        data, (prior_data_len - frag_delta));
1817                                 kunmap_atomic(vaddr);
1818                         }
1819                         /* reset tcp_seq as per the prior_data_required len */
1820                         tcp_seq -= prior_data_len;
1821                         /* include prio_data_len for  further calculation.
1822                          */
1823                         data_len += prior_data_len;
1824                 }
1825                 /* reset snd una, so the middle record won't send the already
1826                  * sent part.
1827                  */
1828                 if (chcr_ktls_update_snd_una(tx_info, q))
1829                         goto out;
1830                 atomic64_inc(&tx_info->adap->ch_ktls_stats.ktls_tx_middle_pkts);
1831         } else {
1832                 /* Else means, its a partial first part of the record. Check if
1833                  * its only the header, don't need to send for encryption then.
1834                  */
1835                 if (data_len <= TLS_HEADER_SIZE + tx_info->iv_size) {
1836                         if (chcr_ktls_tx_plaintxt(tx_info, skb, tcp_seq, mss,
1837                                                   tcp_push_no_fin, q,
1838                                                   tx_info->port_id,
1839                                                   prior_data,
1840                                                   prior_data_len)) {
1841                                 goto out;
1842                         }
1843                         return 0;
1844                 }
1845                 atomic64_inc(&tx_info->adap->ch_ktls_stats.ktls_tx_start_pkts);
1846         }
1847
1848         if (chcr_ktls_xmit_wr_short(skb, tx_info, q, tcp_seq, tcp_push_no_fin,
1849                                     mss, tls_rec_offset, prior_data,
1850                                     prior_data_len)) {
1851                 goto out;
1852         }
1853
1854         return 0;
1855 out:
1856         dev_kfree_skb_any(skb);
1857         return NETDEV_TX_BUSY;
1858 }
1859
1860 /* nic tls TX handler */
1861 static int chcr_ktls_xmit(struct sk_buff *skb, struct net_device *dev)
1862 {
1863         struct ch_ktls_port_stats_debug *port_stats;
1864         struct chcr_ktls_ofld_ctx_tx *tx_ctx;
1865         struct ch_ktls_stats_debug *stats;
1866         struct tcphdr *th = tcp_hdr(skb);
1867         int data_len, qidx, ret = 0, mss;
1868         struct tls_record_info *record;
1869         struct chcr_ktls_info *tx_info;
1870         u32 tls_end_offset, tcp_seq;
1871         struct tls_context *tls_ctx;
1872         struct sk_buff *local_skb;
1873         struct sge_eth_txq *q;
1874         struct adapter *adap;
1875         unsigned long flags;
1876
1877         tcp_seq = ntohl(th->seq);
1878
1879         mss = skb_is_gso(skb) ? skb_shinfo(skb)->gso_size : skb->data_len;
1880
1881         /* check if we haven't set it for ktls offload */
1882         if (!skb->sk || !tls_is_sk_tx_device_offloaded(skb->sk))
1883                 goto out;
1884
1885         tls_ctx = tls_get_ctx(skb->sk);
1886         if (unlikely(tls_ctx->netdev != dev))
1887                 goto out;
1888
1889         tx_ctx = chcr_get_ktls_tx_context(tls_ctx);
1890         tx_info = tx_ctx->chcr_info;
1891
1892         if (unlikely(!tx_info))
1893                 goto out;
1894
1895         /* don't touch the original skb, make a new skb to extract each records
1896          * and send them separately.
1897          */
1898         local_skb = alloc_skb(0, GFP_KERNEL);
1899
1900         if (unlikely(!local_skb))
1901                 return NETDEV_TX_BUSY;
1902
1903         adap = tx_info->adap;
1904         stats = &adap->ch_ktls_stats;
1905         port_stats = &stats->ktls_port[tx_info->port_id];
1906
1907         qidx = skb->queue_mapping;
1908         q = &adap->sge.ethtxq[qidx + tx_info->first_qset];
1909         cxgb4_reclaim_completed_tx(adap, &q->q, true);
1910         /* if tcp options are set but finish is not send the options first */
1911         if (!th->fin && chcr_ktls_check_tcp_options(th)) {
1912                 ret = chcr_ktls_write_tcp_options(tx_info, skb, q,
1913                                                   tx_info->tx_chan);
1914                 if (ret)
1915                         return NETDEV_TX_BUSY;
1916         }
1917         /* update tcb */
1918         ret = chcr_ktls_xmit_tcb_cpls(tx_info, q, ntohl(th->seq),
1919                                       ntohl(th->ack_seq),
1920                                       ntohs(th->window));
1921         if (ret) {
1922                 dev_kfree_skb_any(local_skb);
1923                 return NETDEV_TX_BUSY;
1924         }
1925
1926         /* copy skb contents into local skb */
1927         chcr_ktls_skb_copy(skb, local_skb);
1928
1929         /* go through the skb and send only one record at a time. */
1930         data_len = skb->data_len;
1931         /* TCP segments can be in received either complete or partial.
1932          * chcr_end_part_handler will handle cases if complete record or end
1933          * part of the record is received. Incase of partial end part of record,
1934          * we will send the complete record again.
1935          */
1936
1937         do {
1938                 int i;
1939
1940                 cxgb4_reclaim_completed_tx(adap, &q->q, true);
1941                 /* lock taken */
1942                 spin_lock_irqsave(&tx_ctx->base.lock, flags);
1943                 /* fetch the tls record */
1944                 record = tls_get_record(&tx_ctx->base, tcp_seq,
1945                                         &tx_info->record_no);
1946                 /* By the time packet reached to us, ACK is received, and record
1947                  * won't be found in that case, handle it gracefully.
1948                  */
1949                 if (unlikely(!record)) {
1950                         spin_unlock_irqrestore(&tx_ctx->base.lock, flags);
1951                         atomic64_inc(&port_stats->ktls_tx_drop_no_sync_data);
1952                         goto out;
1953                 }
1954
1955                 if (unlikely(tls_record_is_start_marker(record))) {
1956                         spin_unlock_irqrestore(&tx_ctx->base.lock, flags);
1957                         atomic64_inc(&port_stats->ktls_tx_skip_no_sync_data);
1958                         goto out;
1959                 }
1960
1961                 /* increase page reference count of the record, so that there
1962                  * won't be any chance of page free in middle if in case stack
1963                  * receives ACK and try to delete the record.
1964                  */
1965                 for (i = 0; i < record->num_frags; i++)
1966                         __skb_frag_ref(&record->frags[i]);
1967                 /* lock cleared */
1968                 spin_unlock_irqrestore(&tx_ctx->base.lock, flags);
1969
1970                 tls_end_offset = record->end_seq - tcp_seq;
1971
1972                 pr_debug("seq 0x%x, end_seq 0x%x prev_seq 0x%x, datalen 0x%x\n",
1973                          tcp_seq, record->end_seq, tx_info->prev_seq, data_len);
1974                 /* if a tls record is finishing in this SKB */
1975                 if (tls_end_offset <= data_len) {
1976                         struct sk_buff *nskb = NULL;
1977
1978                         if (tls_end_offset < data_len) {
1979                                 nskb = alloc_skb(0, GFP_KERNEL);
1980                                 if (unlikely(!nskb)) {
1981                                         ret = -ENOMEM;
1982                                         goto clear_ref;
1983                                 }
1984
1985                                 chcr_ktls_skb_shift(nskb, local_skb,
1986                                                     tls_end_offset);
1987                         } else {
1988                                 /* its the only record in this skb, directly
1989                                  * point it.
1990                                  */
1991                                 nskb = local_skb;
1992                         }
1993                         ret = chcr_end_part_handler(tx_info, nskb, record,
1994                                                     tcp_seq, mss,
1995                                                     (!th->fin && th->psh), q,
1996                                                     tls_end_offset,
1997                                                     (nskb == local_skb));
1998
1999                         if (ret && nskb != local_skb)
2000                                 dev_kfree_skb_any(local_skb);
2001
2002                         data_len -= tls_end_offset;
2003                         /* tcp_seq increment is required to handle next record.
2004                          */
2005                         tcp_seq += tls_end_offset;
2006                 } else {
2007                         ret = chcr_short_record_handler(tx_info, local_skb,
2008                                                         record, tcp_seq, mss,
2009                                                         (!th->fin && th->psh),
2010                                                         q, tls_end_offset);
2011                         data_len = 0;
2012                 }
2013 clear_ref:
2014                 /* clear the frag ref count which increased locally before */
2015                 for (i = 0; i < record->num_frags; i++) {
2016                         /* clear the frag ref count */
2017                         __skb_frag_unref(&record->frags[i]);
2018                 }
2019                 /* if any failure, come out from the loop. */
2020                 if (ret)
2021                         goto out;
2022                 /* length should never be less than 0 */
2023                 WARN_ON(data_len < 0);
2024
2025         } while (data_len > 0);
2026
2027         tx_info->prev_seq = ntohl(th->seq) + skb->data_len;
2028         atomic64_inc(&port_stats->ktls_tx_encrypted_packets);
2029         atomic64_add(skb->data_len, &port_stats->ktls_tx_encrypted_bytes);
2030
2031         /* tcp finish is set, send a separate tcp msg including all the options
2032          * as well.
2033          */
2034         if (th->fin)
2035                 chcr_ktls_write_tcp_options(tx_info, skb, q, tx_info->tx_chan);
2036
2037 out:
2038         dev_kfree_skb_any(skb);
2039         return NETDEV_TX_OK;
2040 }
2041
2042 static void *chcr_ktls_uld_add(const struct cxgb4_lld_info *lldi)
2043 {
2044         struct chcr_ktls_uld_ctx *u_ctx;
2045
2046         pr_info_once("%s - version %s\n", CHCR_KTLS_DRV_DESC,
2047                      CHCR_KTLS_DRV_VERSION);
2048         u_ctx = kzalloc(sizeof(*u_ctx), GFP_KERNEL);
2049         if (!u_ctx) {
2050                 u_ctx = ERR_PTR(-ENOMEM);
2051                 goto out;
2052         }
2053         u_ctx->lldi = *lldi;
2054 out:
2055         return u_ctx;
2056 }
2057
2058 static const struct tlsdev_ops chcr_ktls_ops = {
2059         .tls_dev_add = chcr_ktls_dev_add,
2060         .tls_dev_del = chcr_ktls_dev_del,
2061 };
2062
2063 static chcr_handler_func work_handlers[NUM_CPL_CMDS] = {
2064         [CPL_ACT_OPEN_RPL] = chcr_ktls_cpl_act_open_rpl,
2065         [CPL_SET_TCB_RPL] = chcr_ktls_cpl_set_tcb_rpl,
2066 };
2067
2068 static int chcr_ktls_uld_rx_handler(void *handle, const __be64 *rsp,
2069                                     const struct pkt_gl *pgl)
2070 {
2071         const struct cpl_act_open_rpl *rpl = (struct cpl_act_open_rpl *)rsp;
2072         struct chcr_ktls_uld_ctx *u_ctx = handle;
2073         u8 opcode = rpl->ot.opcode;
2074         struct adapter *adap;
2075
2076         adap = pci_get_drvdata(u_ctx->lldi.pdev);
2077
2078         if (!work_handlers[opcode]) {
2079                 pr_err("Unsupported opcode %d received\n", opcode);
2080                 return 0;
2081         }
2082
2083         work_handlers[opcode](adap, (unsigned char *)&rsp[1]);
2084         return 0;
2085 }
2086
2087 static int chcr_ktls_uld_state_change(void *handle, enum cxgb4_state new_state)
2088 {
2089         struct chcr_ktls_uld_ctx *u_ctx = handle;
2090
2091         switch (new_state) {
2092         case CXGB4_STATE_UP:
2093                 pr_info("%s: Up\n", pci_name(u_ctx->lldi.pdev));
2094                 mutex_lock(&dev_mutex);
2095                 list_add_tail(&u_ctx->entry, &uld_ctx_list);
2096                 mutex_unlock(&dev_mutex);
2097                 break;
2098         case CXGB4_STATE_START_RECOVERY:
2099         case CXGB4_STATE_DOWN:
2100         case CXGB4_STATE_DETACH:
2101                 pr_info("%s: Down\n", pci_name(u_ctx->lldi.pdev));
2102                 mutex_lock(&dev_mutex);
2103                 list_del(&u_ctx->entry);
2104                 mutex_unlock(&dev_mutex);
2105                 break;
2106         default:
2107                 break;
2108         }
2109
2110         return 0;
2111 }
2112
2113 static struct cxgb4_uld_info chcr_ktls_uld_info = {
2114         .name = CHCR_KTLS_DRV_MODULE_NAME,
2115         .nrxq = 1,
2116         .rxq_size = 1024,
2117         .add = chcr_ktls_uld_add,
2118         .tx_handler = chcr_ktls_xmit,
2119         .rx_handler = chcr_ktls_uld_rx_handler,
2120         .state_change = chcr_ktls_uld_state_change,
2121         .tlsdev_ops = &chcr_ktls_ops,
2122 };
2123
2124 static int __init chcr_ktls_init(void)
2125 {
2126         cxgb4_register_uld(CXGB4_ULD_KTLS, &chcr_ktls_uld_info);
2127         return 0;
2128 }
2129
2130 static void __exit chcr_ktls_exit(void)
2131 {
2132         struct chcr_ktls_uld_ctx *u_ctx, *tmp;
2133         struct adapter *adap;
2134
2135         pr_info("driver unloaded\n");
2136
2137         mutex_lock(&dev_mutex);
2138         list_for_each_entry_safe(u_ctx, tmp, &uld_ctx_list, entry) {
2139                 adap = pci_get_drvdata(u_ctx->lldi.pdev);
2140                 memset(&adap->ch_ktls_stats, 0, sizeof(adap->ch_ktls_stats));
2141                 list_del(&u_ctx->entry);
2142                 kfree(u_ctx);
2143         }
2144         mutex_unlock(&dev_mutex);
2145         cxgb4_unregister_uld(CXGB4_ULD_KTLS);
2146 }
2147
2148 module_init(chcr_ktls_init);
2149 module_exit(chcr_ktls_exit);
2150
2151 MODULE_DESCRIPTION("Chelsio NIC TLS ULD driver");
2152 MODULE_LICENSE("GPL");
2153 MODULE_AUTHOR("Chelsio Communications");
2154 MODULE_VERSION(CHCR_KTLS_DRV_VERSION);