1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2019 Mellanox Technologies.
4 #include <linux/debugfs.h>
5 #include "en_accel/ktls.h"
6 #include "en_accel/ktls_txrx.h"
7 #include "en_accel/ktls_utils.h"
9 struct mlx5e_dump_wqe {
10 struct mlx5_wqe_ctrl_seg ctrl;
11 struct mlx5_wqe_data_seg data;
14 #define MLX5E_KTLS_DUMP_WQEBBS \
15 (DIV_ROUND_UP(sizeof(struct mlx5e_dump_wqe), MLX5_SEND_WQE_BB))
18 mlx5e_ktls_dumps_num_wqes(struct mlx5e_params *params, unsigned int nfrags,
19 unsigned int sync_len)
21 /* Given the MTU and sync_len, calculates an upper bound for the
22 * number of DUMP WQEs needed for the TX resync of a record.
24 return nfrags + DIV_ROUND_UP(sync_len, MLX5E_SW2HW_MTU(params, params->sw_mtu));
27 u16 mlx5e_ktls_get_stop_room(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
29 u16 num_dumps, stop_room = 0;
31 if (!mlx5e_is_ktls_tx(mdev))
34 num_dumps = mlx5e_ktls_dumps_num_wqes(params, MAX_SKB_FRAGS, TLS_MAX_PAYLOAD_SIZE);
36 stop_room += mlx5e_stop_room_for_wqe(mdev, MLX5E_TLS_SET_STATIC_PARAMS_WQEBBS);
37 stop_room += mlx5e_stop_room_for_wqe(mdev, MLX5E_TLS_SET_PROGRESS_PARAMS_WQEBBS);
38 stop_room += num_dumps * mlx5e_stop_room_for_wqe(mdev, MLX5E_KTLS_DUMP_WQEBBS);
39 stop_room += 1; /* fence nop */
44 static void mlx5e_ktls_set_tisc(struct mlx5_core_dev *mdev, void *tisc)
46 MLX5_SET(tisc, tisc, tls_en, 1);
47 MLX5_SET(tisc, tisc, pd, mdev->mlx5e_res.hw_objs.pdn);
48 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.hw_objs.td.tdn);
51 static int mlx5e_ktls_create_tis(struct mlx5_core_dev *mdev, u32 *tisn)
53 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
55 mlx5e_ktls_set_tisc(mdev, MLX5_ADDR_OF(create_tis_in, in, ctx));
57 return mlx5_core_create_tis(mdev, in, tisn);
60 static int mlx5e_ktls_create_tis_cb(struct mlx5_core_dev *mdev,
61 struct mlx5_async_ctx *async_ctx,
63 mlx5_async_cbk_t callback,
64 struct mlx5_async_work *context)
66 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
68 mlx5e_ktls_set_tisc(mdev, MLX5_ADDR_OF(create_tis_in, in, ctx));
69 MLX5_SET(create_tis_in, in, opcode, MLX5_CMD_OP_CREATE_TIS);
71 return mlx5_cmd_exec_cb(async_ctx, in, sizeof(in),
72 out, outlen, callback, context);
75 static int mlx5e_ktls_destroy_tis_cb(struct mlx5_core_dev *mdev, u32 tisn,
76 struct mlx5_async_ctx *async_ctx,
78 mlx5_async_cbk_t callback,
79 struct mlx5_async_work *context)
81 u32 in[MLX5_ST_SZ_DW(destroy_tis_in)] = {};
83 MLX5_SET(destroy_tis_in, in, opcode, MLX5_CMD_OP_DESTROY_TIS);
84 MLX5_SET(destroy_tis_in, in, tisn, tisn);
86 return mlx5_cmd_exec_cb(async_ctx, in, sizeof(in),
87 out, outlen, callback, context);
90 struct mlx5e_ktls_offload_context_tx {
94 bool ctx_post_pending;
95 /* control / resync */
96 struct list_head list_node; /* member of the pool */
97 union mlx5e_crypto_info crypto_info;
98 struct tls_offload_context_tx *tx_ctx;
99 struct mlx5_core_dev *mdev;
100 struct mlx5e_tls_sw_stats *sw_stats;
101 struct mlx5_crypto_dek *dek;
106 mlx5e_set_ktls_tx_priv_ctx(struct tls_context *tls_ctx,
107 struct mlx5e_ktls_offload_context_tx *priv_tx)
109 struct mlx5e_ktls_offload_context_tx **ctx =
110 __tls_driver_ctx(tls_ctx, TLS_OFFLOAD_CTX_DIR_TX);
112 BUILD_BUG_ON(sizeof(priv_tx) > TLS_DRIVER_STATE_SIZE_TX);
117 static struct mlx5e_ktls_offload_context_tx *
118 mlx5e_get_ktls_tx_priv_ctx(struct tls_context *tls_ctx)
120 struct mlx5e_ktls_offload_context_tx **ctx =
121 __tls_driver_ctx(tls_ctx, TLS_OFFLOAD_CTX_DIR_TX);
126 /* struct for callback API management */
127 struct mlx5e_async_ctx {
128 struct mlx5_async_work context;
129 struct mlx5_async_ctx *async_ctx;
130 struct mlx5e_ktls_offload_context_tx *priv_tx;
133 u32 out_create[MLX5_ST_SZ_DW(create_tis_out)];
134 u32 out_destroy[MLX5_ST_SZ_DW(destroy_tis_out)];
138 struct mlx5e_bulk_async_ctx {
139 struct mlx5_async_ctx async_ctx;
140 DECLARE_FLEX_ARRAY(struct mlx5e_async_ctx, arr);
143 static struct mlx5e_bulk_async_ctx *mlx5e_bulk_async_init(struct mlx5_core_dev *mdev, int n)
145 struct mlx5e_bulk_async_ctx *bulk_async;
149 sz = struct_size(bulk_async, arr, n);
150 bulk_async = kvzalloc(sz, GFP_KERNEL);
154 mlx5_cmd_init_async_ctx(mdev, &bulk_async->async_ctx);
156 for (i = 0; i < n; i++)
157 bulk_async->arr[i].async_ctx = &bulk_async->async_ctx;
162 static void mlx5e_bulk_async_cleanup(struct mlx5e_bulk_async_ctx *bulk_async)
164 mlx5_cmd_cleanup_async_ctx(&bulk_async->async_ctx);
168 static void create_tis_callback(int status, struct mlx5_async_work *context)
170 struct mlx5e_async_ctx *async =
171 container_of(context, struct mlx5e_async_ctx, context);
172 struct mlx5e_ktls_offload_context_tx *priv_tx = async->priv_tx;
176 priv_tx->create_err = 1;
180 priv_tx->tisn = MLX5_GET(create_tis_out, async->out_create, tisn);
183 static void destroy_tis_callback(int status, struct mlx5_async_work *context)
185 struct mlx5e_async_ctx *async =
186 container_of(context, struct mlx5e_async_ctx, context);
187 struct mlx5e_ktls_offload_context_tx *priv_tx = async->priv_tx;
192 static struct mlx5e_ktls_offload_context_tx *
193 mlx5e_tls_priv_tx_init(struct mlx5_core_dev *mdev, struct mlx5e_tls_sw_stats *sw_stats,
194 struct mlx5e_async_ctx *async)
196 struct mlx5e_ktls_offload_context_tx *priv_tx;
199 priv_tx = kzalloc(sizeof(*priv_tx), GFP_KERNEL);
201 return ERR_PTR(-ENOMEM);
203 priv_tx->mdev = mdev;
204 priv_tx->sw_stats = sw_stats;
207 err = mlx5e_ktls_create_tis(mdev, &priv_tx->tisn);
211 async->priv_tx = priv_tx;
212 err = mlx5e_ktls_create_tis_cb(mdev, async->async_ctx,
213 async->out_create, sizeof(async->out_create),
214 create_tis_callback, &async->context);
226 static void mlx5e_tls_priv_tx_cleanup(struct mlx5e_ktls_offload_context_tx *priv_tx,
227 struct mlx5e_async_ctx *async)
229 if (priv_tx->create_err) {
233 async->priv_tx = priv_tx;
234 mlx5e_ktls_destroy_tis_cb(priv_tx->mdev, priv_tx->tisn,
236 async->out_destroy, sizeof(async->out_destroy),
237 destroy_tis_callback, &async->context);
240 static void mlx5e_tls_priv_tx_list_cleanup(struct mlx5_core_dev *mdev,
241 struct list_head *list, int size)
243 struct mlx5e_ktls_offload_context_tx *obj, *n;
244 struct mlx5e_bulk_async_ctx *bulk_async;
247 bulk_async = mlx5e_bulk_async_init(mdev, size);
252 list_for_each_entry_safe(obj, n, list, list_node) {
253 mlx5e_tls_priv_tx_cleanup(obj, &bulk_async->arr[i]);
257 mlx5e_bulk_async_cleanup(bulk_async);
260 /* Recycling pool API */
262 #define MLX5E_TLS_TX_POOL_BULK (16)
263 #define MLX5E_TLS_TX_POOL_HIGH (4 * 1024)
264 #define MLX5E_TLS_TX_POOL_LOW (MLX5E_TLS_TX_POOL_HIGH / 4)
266 struct mlx5e_tls_tx_pool {
267 struct mlx5_core_dev *mdev;
268 struct mlx5e_tls_sw_stats *sw_stats;
269 struct mutex lock; /* Protects access to the pool */
270 struct list_head list;
273 struct workqueue_struct *wq;
274 struct work_struct create_work;
275 struct work_struct destroy_work;
278 static void create_work(struct work_struct *work)
280 struct mlx5e_tls_tx_pool *pool =
281 container_of(work, struct mlx5e_tls_tx_pool, create_work);
282 struct mlx5e_ktls_offload_context_tx *obj;
283 struct mlx5e_bulk_async_ctx *bulk_async;
284 LIST_HEAD(local_list);
287 bulk_async = mlx5e_bulk_async_init(pool->mdev, MLX5E_TLS_TX_POOL_BULK);
291 for (i = 0; i < MLX5E_TLS_TX_POOL_BULK; i++) {
292 obj = mlx5e_tls_priv_tx_init(pool->mdev, pool->sw_stats, &bulk_async->arr[i]);
297 list_add(&obj->list_node, &local_list);
300 for (j = 0; j < i; j++) {
301 struct mlx5e_async_ctx *async = &bulk_async->arr[j];
303 if (!err && async->err)
306 atomic64_add(i, &pool->sw_stats->tx_tls_pool_alloc);
307 mlx5e_bulk_async_cleanup(bulk_async);
311 mutex_lock(&pool->lock);
312 if (pool->size + MLX5E_TLS_TX_POOL_BULK >= MLX5E_TLS_TX_POOL_HIGH) {
313 mutex_unlock(&pool->lock);
316 list_splice(&local_list, &pool->list);
317 pool->size += MLX5E_TLS_TX_POOL_BULK;
318 if (pool->size <= MLX5E_TLS_TX_POOL_LOW)
319 queue_work(pool->wq, work);
320 mutex_unlock(&pool->lock);
324 mlx5e_tls_priv_tx_list_cleanup(pool->mdev, &local_list, i);
325 atomic64_add(i, &pool->sw_stats->tx_tls_pool_free);
328 static void destroy_work(struct work_struct *work)
330 struct mlx5e_tls_tx_pool *pool =
331 container_of(work, struct mlx5e_tls_tx_pool, destroy_work);
332 struct mlx5e_ktls_offload_context_tx *obj;
333 LIST_HEAD(local_list);
336 mutex_lock(&pool->lock);
337 if (pool->size < MLX5E_TLS_TX_POOL_HIGH) {
338 mutex_unlock(&pool->lock);
342 list_for_each_entry(obj, &pool->list, list_node)
343 if (++i == MLX5E_TLS_TX_POOL_BULK)
346 list_cut_position(&local_list, &pool->list, &obj->list_node);
347 pool->size -= MLX5E_TLS_TX_POOL_BULK;
348 if (pool->size >= MLX5E_TLS_TX_POOL_HIGH)
349 queue_work(pool->wq, work);
350 mutex_unlock(&pool->lock);
352 mlx5e_tls_priv_tx_list_cleanup(pool->mdev, &local_list, MLX5E_TLS_TX_POOL_BULK);
353 atomic64_add(MLX5E_TLS_TX_POOL_BULK, &pool->sw_stats->tx_tls_pool_free);
356 static struct mlx5e_tls_tx_pool *mlx5e_tls_tx_pool_init(struct mlx5_core_dev *mdev,
357 struct mlx5e_tls_sw_stats *sw_stats)
359 struct mlx5e_tls_tx_pool *pool;
361 BUILD_BUG_ON(MLX5E_TLS_TX_POOL_LOW + MLX5E_TLS_TX_POOL_BULK >= MLX5E_TLS_TX_POOL_HIGH);
363 pool = kvzalloc(sizeof(*pool), GFP_KERNEL);
367 pool->wq = create_singlethread_workqueue("mlx5e_tls_tx_pool");
371 INIT_LIST_HEAD(&pool->list);
372 mutex_init(&pool->lock);
374 INIT_WORK(&pool->create_work, create_work);
375 INIT_WORK(&pool->destroy_work, destroy_work);
378 pool->sw_stats = sw_stats;
387 static void mlx5e_tls_tx_pool_list_cleanup(struct mlx5e_tls_tx_pool *pool)
389 while (pool->size > MLX5E_TLS_TX_POOL_BULK) {
390 struct mlx5e_ktls_offload_context_tx *obj;
391 LIST_HEAD(local_list);
394 list_for_each_entry(obj, &pool->list, list_node)
395 if (++i == MLX5E_TLS_TX_POOL_BULK)
398 list_cut_position(&local_list, &pool->list, &obj->list_node);
399 mlx5e_tls_priv_tx_list_cleanup(pool->mdev, &local_list, MLX5E_TLS_TX_POOL_BULK);
400 atomic64_add(MLX5E_TLS_TX_POOL_BULK, &pool->sw_stats->tx_tls_pool_free);
401 pool->size -= MLX5E_TLS_TX_POOL_BULK;
404 mlx5e_tls_priv_tx_list_cleanup(pool->mdev, &pool->list, pool->size);
405 atomic64_add(pool->size, &pool->sw_stats->tx_tls_pool_free);
409 static void mlx5e_tls_tx_pool_cleanup(struct mlx5e_tls_tx_pool *pool)
411 mlx5e_tls_tx_pool_list_cleanup(pool);
412 destroy_workqueue(pool->wq);
416 static void pool_push(struct mlx5e_tls_tx_pool *pool, struct mlx5e_ktls_offload_context_tx *obj)
418 mutex_lock(&pool->lock);
419 list_add(&obj->list_node, &pool->list);
420 if (++pool->size == MLX5E_TLS_TX_POOL_HIGH)
421 queue_work(pool->wq, &pool->destroy_work);
422 mutex_unlock(&pool->lock);
425 static struct mlx5e_ktls_offload_context_tx *pool_pop(struct mlx5e_tls_tx_pool *pool)
427 struct mlx5e_ktls_offload_context_tx *obj;
429 mutex_lock(&pool->lock);
430 if (unlikely(pool->size == 0)) {
432 * - trigger the populating work, and
433 * - serve the current context via the regular blocking api.
435 queue_work(pool->wq, &pool->create_work);
436 mutex_unlock(&pool->lock);
437 obj = mlx5e_tls_priv_tx_init(pool->mdev, pool->sw_stats, NULL);
439 atomic64_inc(&pool->sw_stats->tx_tls_pool_alloc);
443 obj = list_first_entry(&pool->list, struct mlx5e_ktls_offload_context_tx,
445 list_del(&obj->list_node);
446 if (--pool->size == MLX5E_TLS_TX_POOL_LOW)
447 queue_work(pool->wq, &pool->create_work);
448 mutex_unlock(&pool->lock);
452 /* End of pool API */
454 int mlx5e_ktls_add_tx(struct net_device *netdev, struct sock *sk,
455 struct tls_crypto_info *crypto_info, u32 start_offload_tcp_sn)
457 struct mlx5e_ktls_offload_context_tx *priv_tx;
458 struct mlx5e_tls_tx_pool *pool;
459 struct tls_context *tls_ctx;
460 struct mlx5_crypto_dek *dek;
461 struct mlx5e_priv *priv;
464 tls_ctx = tls_get_ctx(sk);
465 priv = netdev_priv(netdev);
466 pool = priv->tls->tx_pool;
468 priv_tx = pool_pop(pool);
470 return PTR_ERR(priv_tx);
472 switch (crypto_info->cipher_type) {
473 case TLS_CIPHER_AES_GCM_128:
474 priv_tx->crypto_info.crypto_info_128 =
475 *(struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
477 case TLS_CIPHER_AES_GCM_256:
478 priv_tx->crypto_info.crypto_info_256 =
479 *(struct tls12_crypto_info_aes_gcm_256 *)crypto_info;
482 WARN_ONCE(1, "Unsupported cipher type %u\n",
483 crypto_info->cipher_type);
488 dek = mlx5_ktls_create_key(priv->tls->dek_pool, crypto_info);
495 priv_tx->expected_seq = start_offload_tcp_sn;
496 priv_tx->tx_ctx = tls_offload_ctx_tx(tls_ctx);
498 mlx5e_set_ktls_tx_priv_ctx(tls_ctx, priv_tx);
500 priv_tx->ctx_post_pending = true;
501 atomic64_inc(&priv_tx->sw_stats->tx_tls_ctx);
506 pool_push(pool, priv_tx);
510 void mlx5e_ktls_del_tx(struct net_device *netdev, struct tls_context *tls_ctx)
512 struct mlx5e_ktls_offload_context_tx *priv_tx;
513 struct mlx5e_tls_tx_pool *pool;
514 struct mlx5e_priv *priv;
516 priv_tx = mlx5e_get_ktls_tx_priv_ctx(tls_ctx);
517 priv = netdev_priv(netdev);
518 pool = priv->tls->tx_pool;
520 atomic64_inc(&priv_tx->sw_stats->tx_tls_del);
521 mlx5_ktls_destroy_key(priv->tls->dek_pool, priv_tx->dek);
522 pool_push(pool, priv_tx);
525 static void tx_fill_wi(struct mlx5e_txqsq *sq,
526 u16 pi, u8 num_wqebbs, u32 num_bytes,
529 struct mlx5e_tx_wqe_info *wi = &sq->db.wqe_info[pi];
531 *wi = (struct mlx5e_tx_wqe_info) {
532 .num_wqebbs = num_wqebbs,
533 .num_bytes = num_bytes,
534 .resync_dump_frag_page = page,
539 mlx5e_ktls_tx_offload_test_and_clear_pending(struct mlx5e_ktls_offload_context_tx *priv_tx)
541 bool ret = priv_tx->ctx_post_pending;
543 priv_tx->ctx_post_pending = false;
549 post_static_params(struct mlx5e_txqsq *sq,
550 struct mlx5e_ktls_offload_context_tx *priv_tx,
553 struct mlx5e_set_tls_static_params_wqe *wqe;
556 num_wqebbs = MLX5E_TLS_SET_STATIC_PARAMS_WQEBBS;
557 pi = mlx5e_txqsq_get_next_pi(sq, num_wqebbs);
558 wqe = MLX5E_TLS_FETCH_SET_STATIC_PARAMS_WQE(sq, pi);
559 mlx5e_ktls_build_static_params(wqe, sq->pc, sq->sqn, &priv_tx->crypto_info,
561 mlx5_crypto_dek_get_id(priv_tx->dek),
562 0, fence, TLS_OFFLOAD_CTX_DIR_TX);
563 tx_fill_wi(sq, pi, num_wqebbs, 0, NULL);
564 sq->pc += num_wqebbs;
568 post_progress_params(struct mlx5e_txqsq *sq,
569 struct mlx5e_ktls_offload_context_tx *priv_tx,
572 struct mlx5e_set_tls_progress_params_wqe *wqe;
575 num_wqebbs = MLX5E_TLS_SET_PROGRESS_PARAMS_WQEBBS;
576 pi = mlx5e_txqsq_get_next_pi(sq, num_wqebbs);
577 wqe = MLX5E_TLS_FETCH_SET_PROGRESS_PARAMS_WQE(sq, pi);
578 mlx5e_ktls_build_progress_params(wqe, sq->pc, sq->sqn, priv_tx->tisn, fence, 0,
579 TLS_OFFLOAD_CTX_DIR_TX);
580 tx_fill_wi(sq, pi, num_wqebbs, 0, NULL);
581 sq->pc += num_wqebbs;
584 static void tx_post_fence_nop(struct mlx5e_txqsq *sq)
586 struct mlx5_wq_cyc *wq = &sq->wq;
587 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
589 tx_fill_wi(sq, pi, 1, 0, NULL);
591 mlx5e_post_nop_fence(wq, sq->sqn, &sq->pc);
595 mlx5e_ktls_tx_post_param_wqes(struct mlx5e_txqsq *sq,
596 struct mlx5e_ktls_offload_context_tx *priv_tx,
597 bool skip_static_post, bool fence_first_post)
599 bool progress_fence = skip_static_post || !fence_first_post;
601 if (!skip_static_post)
602 post_static_params(sq, priv_tx, fence_first_post);
604 post_progress_params(sq, priv_tx, progress_fence);
605 tx_post_fence_nop(sq);
608 struct tx_sync_info {
612 skb_frag_t frags[MAX_SKB_FRAGS];
615 enum mlx5e_ktls_sync_retval {
616 MLX5E_KTLS_SYNC_DONE,
617 MLX5E_KTLS_SYNC_FAIL,
618 MLX5E_KTLS_SYNC_SKIP_NO_DATA,
621 static enum mlx5e_ktls_sync_retval
622 tx_sync_info_get(struct mlx5e_ktls_offload_context_tx *priv_tx,
623 u32 tcp_seq, int datalen, struct tx_sync_info *info)
625 struct tls_offload_context_tx *tx_ctx = priv_tx->tx_ctx;
626 enum mlx5e_ktls_sync_retval ret = MLX5E_KTLS_SYNC_DONE;
627 struct tls_record_info *record;
628 int remaining, i = 0;
632 spin_lock_irqsave(&tx_ctx->lock, flags);
633 record = tls_get_record(tx_ctx, tcp_seq, &info->rcd_sn);
635 if (unlikely(!record)) {
636 ret = MLX5E_KTLS_SYNC_FAIL;
640 /* There are the following cases:
641 * 1. packet ends before start marker: bypass offload.
642 * 2. packet starts before start marker and ends after it: drop,
643 * not supported, breaks contract with kernel.
644 * 3. packet ends before tls record info starts: drop,
645 * this packet was already acknowledged and its record info
648 ends_before = before(tcp_seq + datalen - 1, tls_record_start_seq(record));
650 if (unlikely(tls_record_is_start_marker(record))) {
651 ret = ends_before ? MLX5E_KTLS_SYNC_SKIP_NO_DATA : MLX5E_KTLS_SYNC_FAIL;
653 } else if (ends_before) {
654 ret = MLX5E_KTLS_SYNC_FAIL;
658 info->sync_len = tcp_seq - tls_record_start_seq(record);
659 remaining = info->sync_len;
660 while (remaining > 0) {
661 skb_frag_t *frag = &record->frags[i];
663 get_page(skb_frag_page(frag));
664 remaining -= skb_frag_size(frag);
665 info->frags[i++] = *frag;
667 /* reduce the part which will be sent with the original SKB */
669 skb_frag_size_add(&info->frags[i - 1], remaining);
672 spin_unlock_irqrestore(&tx_ctx->lock, flags);
677 tx_post_resync_params(struct mlx5e_txqsq *sq,
678 struct mlx5e_ktls_offload_context_tx *priv_tx,
681 __be64 rn_be = cpu_to_be64(rcd_sn);
682 bool skip_static_post;
686 switch (priv_tx->crypto_info.crypto_info.cipher_type) {
687 case TLS_CIPHER_AES_GCM_128: {
688 struct tls12_crypto_info_aes_gcm_128 *info = &priv_tx->crypto_info.crypto_info_128;
690 rec_seq = info->rec_seq;
691 rec_seq_sz = sizeof(info->rec_seq);
694 case TLS_CIPHER_AES_GCM_256: {
695 struct tls12_crypto_info_aes_gcm_256 *info = &priv_tx->crypto_info.crypto_info_256;
697 rec_seq = info->rec_seq;
698 rec_seq_sz = sizeof(info->rec_seq);
702 WARN_ONCE(1, "Unsupported cipher type %u\n",
703 priv_tx->crypto_info.crypto_info.cipher_type);
707 skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
708 if (!skip_static_post)
709 memcpy(rec_seq, &rn_be, rec_seq_sz);
711 mlx5e_ktls_tx_post_param_wqes(sq, priv_tx, skip_static_post, true);
715 tx_post_resync_dump(struct mlx5e_txqsq *sq, skb_frag_t *frag, u32 tisn)
717 struct mlx5_wqe_ctrl_seg *cseg;
718 struct mlx5_wqe_data_seg *dseg;
719 struct mlx5e_dump_wqe *wqe;
720 dma_addr_t dma_addr = 0;
725 BUILD_BUG_ON(MLX5E_KTLS_DUMP_WQEBBS != 1);
726 pi = mlx5_wq_cyc_ctr2ix(&sq->wq, sq->pc);
727 wqe = MLX5E_TLS_FETCH_DUMP_WQE(sq, pi);
729 ds_cnt = sizeof(*wqe) / MLX5_SEND_WQE_DS;
734 cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_DUMP);
735 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
736 cseg->tis_tir_num = cpu_to_be32(tisn << 8);
738 fsz = skb_frag_size(frag);
739 dma_addr = skb_frag_dma_map(sq->pdev, frag, 0, fsz,
741 if (unlikely(dma_mapping_error(sq->pdev, dma_addr)))
744 dseg->addr = cpu_to_be64(dma_addr);
745 dseg->lkey = sq->mkey_be;
746 dseg->byte_count = cpu_to_be32(fsz);
747 mlx5e_dma_push(sq, dma_addr, fsz, MLX5E_DMA_MAP_PAGE);
749 tx_fill_wi(sq, pi, MLX5E_KTLS_DUMP_WQEBBS, fsz, skb_frag_page(frag));
750 sq->pc += MLX5E_KTLS_DUMP_WQEBBS;
755 void mlx5e_ktls_tx_handle_resync_dump_comp(struct mlx5e_txqsq *sq,
756 struct mlx5e_tx_wqe_info *wi,
759 struct mlx5e_sq_stats *stats;
760 struct mlx5e_sq_dma *dma;
762 dma = mlx5e_dma_get(sq, (*dma_fifo_cc)++);
765 mlx5e_tx_dma_unmap(sq->pdev, dma);
766 put_page(wi->resync_dump_frag_page);
767 stats->tls_dump_packets++;
768 stats->tls_dump_bytes += wi->num_bytes;
771 static enum mlx5e_ktls_sync_retval
772 mlx5e_ktls_tx_handle_ooo(struct mlx5e_ktls_offload_context_tx *priv_tx,
773 struct mlx5e_txqsq *sq,
777 enum mlx5e_ktls_sync_retval ret;
778 struct tx_sync_info info = {};
781 ret = tx_sync_info_get(priv_tx, seq, datalen, &info);
782 if (unlikely(ret != MLX5E_KTLS_SYNC_DONE))
783 /* We might get here with ret == FAIL if a retransmission
784 * reaches the driver after the relevant record is acked.
785 * It should be safe to drop the packet in this case
789 tx_post_resync_params(sq, priv_tx, info.rcd_sn);
791 for (i = 0; i < info.nr_frags; i++) {
792 unsigned int orig_fsz, frag_offset = 0, n = 0;
793 skb_frag_t *f = &info.frags[i];
795 orig_fsz = skb_frag_size(f);
801 fsz = min_t(unsigned int, sq->hw_mtu, orig_fsz - frag_offset);
802 skb_frag_size_set(f, fsz);
803 if (tx_post_resync_dump(sq, f, priv_tx->tisn)) {
804 page_ref_add(skb_frag_page(f), n - 1);
808 skb_frag_off_add(f, fsz);
810 } while (frag_offset < orig_fsz);
812 page_ref_add(skb_frag_page(f), n - 1);
815 return MLX5E_KTLS_SYNC_DONE;
818 for (; i < info.nr_frags; i++)
819 /* The put_page() here undoes the page ref obtained in tx_sync_info_get().
820 * Page refs obtained for the DUMP WQEs above (by page_ref_add) will be
821 * released only upon their completions (or in mlx5e_free_txqsq_descs,
822 * if channel closes).
824 put_page(skb_frag_page(&info.frags[i]));
826 return MLX5E_KTLS_SYNC_FAIL;
829 bool mlx5e_ktls_handle_tx_skb(struct net_device *netdev, struct mlx5e_txqsq *sq,
831 struct mlx5e_accel_tx_tls_state *state)
833 struct mlx5e_ktls_offload_context_tx *priv_tx;
834 struct mlx5e_sq_stats *stats = sq->stats;
835 struct net_device *tls_netdev;
836 struct tls_context *tls_ctx;
840 datalen = skb->len - skb_tcp_all_headers(skb);
844 mlx5e_tx_mpwqe_ensure_complete(sq);
846 tls_ctx = tls_get_ctx(skb->sk);
847 tls_netdev = rcu_dereference_bh(tls_ctx->netdev);
848 /* Don't WARN on NULL: if tls_device_down is running in parallel,
849 * netdev might become NULL, even if tls_is_skb_tx_device_offloaded was
850 * true. Rather continue processing this packet.
852 if (WARN_ON_ONCE(tls_netdev && tls_netdev != netdev))
855 priv_tx = mlx5e_get_ktls_tx_priv_ctx(tls_ctx);
857 if (unlikely(mlx5e_ktls_tx_offload_test_and_clear_pending(priv_tx)))
858 mlx5e_ktls_tx_post_param_wqes(sq, priv_tx, false, false);
860 seq = ntohl(tcp_hdr(skb)->seq);
861 if (unlikely(priv_tx->expected_seq != seq)) {
862 enum mlx5e_ktls_sync_retval ret =
863 mlx5e_ktls_tx_handle_ooo(priv_tx, sq, datalen, seq);
868 case MLX5E_KTLS_SYNC_DONE:
870 case MLX5E_KTLS_SYNC_SKIP_NO_DATA:
871 stats->tls_skip_no_sync_data++;
872 if (likely(!skb->decrypted))
876 case MLX5E_KTLS_SYNC_FAIL:
877 stats->tls_drop_no_sync_data++;
882 priv_tx->expected_seq = seq + datalen;
884 state->tls_tisn = priv_tx->tisn;
886 stats->tls_encrypted_packets += skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1;
887 stats->tls_encrypted_bytes += datalen;
893 dev_kfree_skb_any(skb);
897 static void mlx5e_tls_tx_debugfs_init(struct mlx5e_tls *tls,
898 struct dentry *dfs_root)
900 if (IS_ERR_OR_NULL(dfs_root))
903 tls->debugfs.dfs_tx = debugfs_create_dir("tx", dfs_root);
905 debugfs_create_size_t("pool_size", 0400, tls->debugfs.dfs_tx,
906 &tls->tx_pool->size);
909 int mlx5e_ktls_init_tx(struct mlx5e_priv *priv)
911 struct mlx5e_tls *tls = priv->tls;
913 if (!mlx5e_is_ktls_tx(priv->mdev))
916 priv->tls->tx_pool = mlx5e_tls_tx_pool_init(priv->mdev, &priv->tls->sw_stats);
917 if (!priv->tls->tx_pool)
920 mlx5e_tls_tx_debugfs_init(tls, tls->debugfs.dfs);
925 void mlx5e_ktls_cleanup_tx(struct mlx5e_priv *priv)
927 if (!mlx5e_is_ktls_tx(priv->mdev))
930 debugfs_remove_recursive(priv->tls->debugfs.dfs_tx);
931 priv->tls->debugfs.dfs_tx = NULL;
933 mlx5e_tls_tx_pool_cleanup(priv->tls->tx_pool);
934 priv->tls->tx_pool = NULL;