From: Valentine Fatiev <valentinef@nvidia.com>
Subject: [PATCH] BACKPORT:
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls.c

Change-Id: I943f72937b7da86797c11efde5561e70e17d2ac0
---
 .../mellanox/mlx5/core/en_accel/ktls.c        | 65 ++++++++++++++++++-
 1 file changed, 62 insertions(+), 3 deletions(-)

--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
 // Copyright (c) 2019 Mellanox Technologies.
 
+#ifdef CONFIG_MLX5_TLS
 #include <linux/debugfs.h>
 #include "en.h"
 #include "lib/mlx5.h"
@@ -9,8 +10,13 @@
 #include "en_accel/ktls_utils.h"
 #include "en_accel/fs_tcp.h"
 
+#ifdef HAVE_TLS_OFFLOAD_DESTRUCT_WORK
 struct mlx5_crypto_dek *mlx5_ktls_create_key(struct mlx5_crypto_dek_pool *dek_pool,
 					     struct tls_crypto_info *crypto_info)
+#else
+int mlx5_ktls_create_key(struct mlx5_core_dev *mdev,
+			 struct tls_crypto_info *crypto_info, u32 *p_key_id)
+#endif
 {
 	const void *key;
 	u32 sz_bytes;
@@ -24,6 +30,7 @@ struct mlx5_crypto_dek *mlx5_ktls_create
 		sz_bytes = sizeof(info->key);
 		break;
 	}
+#ifdef TLS_CIPHER_AES_GCM_256
 	case TLS_CIPHER_AES_GCM_256: {
 		struct tls12_crypto_info_aes_gcm_256 *info =
 			(struct tls12_crypto_info_aes_gcm_256 *)crypto_info;
@@ -32,17 +39,33 @@ struct mlx5_crypto_dek *mlx5_ktls_create
 		sz_bytes = sizeof(info->key);
 		break;
 	}
+#endif
 	default:
+#ifdef HAVE_TLS_OFFLOAD_DESTRUCT_WORK
 		return ERR_PTR(-EINVAL);
 	}
 
 	return mlx5_crypto_dek_create(dek_pool, key, sz_bytes);
-}
+#else
+		return -EINVAL;
+	}
 
-void mlx5_ktls_destroy_key(struct mlx5_crypto_dek_pool *dek_pool,
-			   struct mlx5_crypto_dek *dek)
+	return mlx5_create_encryption_key(mdev, key, sz_bytes,
+					  MLX5_ACCEL_OBJ_TLS_KEY,
+					  p_key_id);
+#endif
+}
+void mlx5_ktls_destroy_key(
+#ifdef HAVE_TLS_OFFLOAD_DESTRUCT_WORK
+	struct mlx5_crypto_dek_pool *dek_pool,
+	struct mlx5_crypto_dek *dek)
 {
 	mlx5_crypto_dek_destroy(dek_pool, dek);
+#else
+	struct mlx5_core_dev *mdev, u32 key_id)
+{
+	mlx5_destroy_encryption_key(mdev, key_id);
+#endif
 }
 
 static int mlx5e_ktls_add(struct net_device *netdev, struct sock *sk,
@@ -60,7 +83,11 @@ static int mlx5e_ktls_add(struct net_dev
 	if (direction == TLS_OFFLOAD_CTX_DIR_TX)
 		err = mlx5e_ktls_add_tx(netdev, sk, crypto_info, start_offload_tcp_sn);
 	else
+#ifdef HAVE_KTLS_RX_SUPPORT
 		err = mlx5e_ktls_add_rx(netdev, sk, crypto_info, start_offload_tcp_sn);
+#else
+		err = -EOPNOTSUPP;
+#endif
 
 	return err;
 }
@@ -71,10 +98,13 @@ static void mlx5e_ktls_del(struct net_de
 {
 	if (direction == TLS_OFFLOAD_CTX_DIR_TX)
 		mlx5e_ktls_del_tx(netdev, tls_ctx);
+#ifdef HAVE_KTLS_RX_SUPPORT
 	else
 		mlx5e_ktls_del_rx(netdev, tls_ctx);
+#endif
 }
 
+#ifdef HAVE_TLSDEV_OPS_HAS_TLS_DEV_RESYNC
 static int mlx5e_ktls_resync(struct net_device *netdev,
 			     struct sock *sk, u32 seq, u8 *rcd_sn,
 			     enum tls_offload_ctx_dir direction)
@@ -82,14 +112,21 @@ static int mlx5e_ktls_resync(struct net_
 	if (unlikely(direction != TLS_OFFLOAD_CTX_DIR_RX))
 		return -EOPNOTSUPP;
 
+#ifdef HAVE_KTLS_RX_SUPPORT
 	mlx5e_ktls_rx_resync(netdev, sk, seq, rcd_sn);
 	return 0;
+#else
+	return -EOPNOTSUPP;
+#endif
 }
+#endif
 
 static const struct tlsdev_ops mlx5e_ktls_ops = {
 	.tls_dev_add = mlx5e_ktls_add,
 	.tls_dev_del = mlx5e_ktls_del,
+#ifdef HAVE_TLSDEV_OPS_HAS_TLS_DEV_RESYNC
 	.tls_dev_resync = mlx5e_ktls_resync,
+#endif
 };
 
 bool mlx5e_is_ktls_rx(struct mlx5_core_dev *mdev)
@@ -123,12 +160,15 @@ void mlx5e_ktls_build_netdev(struct mlx5
 		netdev->features    |= NETIF_F_HW_TLS_TX;
 	}
 
+#ifdef HAVE_KTLS_RX_SUPPORT
 	if (mlx5e_is_ktls_rx(mdev))
 		netdev->hw_features |= NETIF_F_HW_TLS_RX;
+#endif
 
 	netdev->tlsdev_ops = &mlx5e_ktls_ops;
 }
 
+#ifdef HAVE_KTLS_RX_SUPPORT
 int mlx5e_ktls_set_feature_rx(struct net_device *netdev, bool enable)
 {
 	struct mlx5e_priv *priv = netdev_priv(netdev);
@@ -176,6 +216,24 @@ void mlx5e_ktls_cleanup_rx(struct mlx5e_
 
 	destroy_workqueue(priv->tls->rx_wq);
 }
+#else
+
+int mlx5e_ktls_set_feature_rx(struct net_device *netdev, bool enable)
+{
+	netdev_warn(netdev, "kTLS is not supported\n");
+	return -EOPNOTSUPP;
+}
+
+int mlx5e_ktls_init_rx(struct mlx5e_priv *priv)
+{
+	return 0;
+}
+
+void mlx5e_ktls_cleanup_rx(struct mlx5e_priv *priv)
+{
+}
+
+#endif /* HAVE_KTLS_RX_SUPPORT */
 
 static void mlx5e_tls_debugfs_init(struct mlx5e_tls *tls,
 				   struct dentry *dfs_root)
@@ -218,3 +276,4 @@ void mlx5e_ktls_cleanup(struct mlx5e_pri
 	kfree(priv->tls);
 	priv->tls = NULL;
 }
+#endif
