1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2019 Mellanox Technologies.
3 
4 #include "en.h"
5 #include "en_accel/ktls.h"
6 #include "en_accel/ktls_utils.h"
7 #include "en_accel/fs_tcp.h"
8 
9 static int mlx5e_ktls_add(struct net_device *netdev, struct sock *sk,
10 			  enum tls_offload_ctx_dir direction,
11 			  struct tls_crypto_info *crypto_info,
12 			  u32 start_offload_tcp_sn)
13 {
14 	struct mlx5e_priv *priv = netdev_priv(netdev);
15 	struct mlx5_core_dev *mdev = priv->mdev;
16 	int err;
17 
18 	if (WARN_ON(!mlx5e_ktls_type_check(mdev, crypto_info)))
19 		return -EOPNOTSUPP;
20 
21 	if (direction == TLS_OFFLOAD_CTX_DIR_TX)
22 		err = mlx5e_ktls_add_tx(netdev, sk, crypto_info, start_offload_tcp_sn);
23 	else
24 		err = mlx5e_ktls_add_rx(netdev, sk, crypto_info, start_offload_tcp_sn);
25 
26 	return err;
27 }
28 
29 static void mlx5e_ktls_del(struct net_device *netdev,
30 			   struct tls_context *tls_ctx,
31 			   enum tls_offload_ctx_dir direction)
32 {
33 	if (direction == TLS_OFFLOAD_CTX_DIR_TX)
34 		mlx5e_ktls_del_tx(netdev, tls_ctx);
35 	else
36 		mlx5e_ktls_del_rx(netdev, tls_ctx);
37 }
38 
39 static int mlx5e_ktls_resync(struct net_device *netdev,
40 			     struct sock *sk, u32 seq, u8 *rcd_sn,
41 			     enum tls_offload_ctx_dir direction)
42 {
43 	if (unlikely(direction != TLS_OFFLOAD_CTX_DIR_RX))
44 		return -EOPNOTSUPP;
45 
46 	mlx5e_ktls_rx_resync(netdev, sk, seq, rcd_sn);
47 	return 0;
48 }
49 
50 static const struct tlsdev_ops mlx5e_ktls_ops = {
51 	.tls_dev_add = mlx5e_ktls_add,
52 	.tls_dev_del = mlx5e_ktls_del,
53 	.tls_dev_resync = mlx5e_ktls_resync,
54 };
55 
56 void mlx5e_ktls_build_netdev(struct mlx5e_priv *priv)
57 {
58 	struct net_device *netdev = priv->netdev;
59 	struct mlx5_core_dev *mdev = priv->mdev;
60 
61 	if (mlx5_accel_is_ktls_tx(mdev)) {
62 		netdev->hw_features |= NETIF_F_HW_TLS_TX;
63 		netdev->features    |= NETIF_F_HW_TLS_TX;
64 	}
65 
66 	if (mlx5_accel_is_ktls_rx(mdev))
67 		netdev->hw_features |= NETIF_F_HW_TLS_RX;
68 
69 	netdev->tlsdev_ops = &mlx5e_ktls_ops;
70 }
71 
72 int mlx5e_ktls_set_feature_rx(struct net_device *netdev, bool enable)
73 {
74 	struct mlx5e_priv *priv = netdev_priv(netdev);
75 	int err = 0;
76 
77 	mutex_lock(&priv->state_lock);
78 	if (enable)
79 		err = mlx5e_accel_fs_tcp_create(priv);
80 	else
81 		mlx5e_accel_fs_tcp_destroy(priv);
82 	mutex_unlock(&priv->state_lock);
83 
84 	return err;
85 }
86 
87 int mlx5e_ktls_init_rx(struct mlx5e_priv *priv)
88 {
89 	int err = 0;
90 
91 	if (priv->netdev->features & NETIF_F_HW_TLS_RX)
92 		err = mlx5e_accel_fs_tcp_create(priv);
93 
94 	return err;
95 }
96 
97 void mlx5e_ktls_cleanup_rx(struct mlx5e_priv *priv)
98 {
99 	if (priv->netdev->features & NETIF_F_HW_TLS_RX)
100 		mlx5e_accel_fs_tcp_destroy(priv);
101 }
102