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