1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2019 Mellanox Technologies. */
3 
4 #ifndef __MLX5E_KTLS_H__
5 #define __MLX5E_KTLS_H__
6 
7 #include <linux/tls.h>
8 #include <net/tls.h>
9 #include "en.h"
10 
11 #ifdef CONFIG_MLX5_EN_TLS
12 int mlx5_ktls_create_key(struct mlx5_core_dev *mdev,
13 			 struct tls_crypto_info *crypto_info,
14 			 u32 *p_key_id);
15 void mlx5_ktls_destroy_key(struct mlx5_core_dev *mdev, u32 key_id);
16 
17 static inline bool mlx5e_is_ktls_device(struct mlx5_core_dev *mdev)
18 {
19 	if (is_kdump_kernel())
20 		return false;
21 
22 	if (!MLX5_CAP_GEN(mdev, tls_tx) && !MLX5_CAP_GEN(mdev, tls_rx))
23 		return false;
24 
25 	if (!MLX5_CAP_GEN(mdev, log_max_dek))
26 		return false;
27 
28 	return MLX5_CAP_TLS(mdev, tls_1_2_aes_gcm_128);
29 }
30 
31 static inline bool mlx5e_ktls_type_check(struct mlx5_core_dev *mdev,
32 					 struct tls_crypto_info *crypto_info)
33 {
34 	switch (crypto_info->cipher_type) {
35 	case TLS_CIPHER_AES_GCM_128:
36 		if (crypto_info->version == TLS_1_2_VERSION)
37 			return MLX5_CAP_TLS(mdev,  tls_1_2_aes_gcm_128);
38 		break;
39 	}
40 
41 	return false;
42 }
43 
44 void mlx5e_ktls_build_netdev(struct mlx5e_priv *priv);
45 int mlx5e_ktls_init_rx(struct mlx5e_priv *priv);
46 void mlx5e_ktls_cleanup_rx(struct mlx5e_priv *priv);
47 int mlx5e_ktls_set_feature_rx(struct net_device *netdev, bool enable);
48 struct mlx5e_ktls_resync_resp *
49 mlx5e_ktls_rx_resync_create_resp_list(void);
50 void mlx5e_ktls_rx_resync_destroy_resp_list(struct mlx5e_ktls_resync_resp *resp_list);
51 
52 static inline bool mlx5e_is_ktls_tx(struct mlx5_core_dev *mdev)
53 {
54 	return !is_kdump_kernel() && MLX5_CAP_GEN(mdev, tls_tx);
55 }
56 
57 static inline bool mlx5e_is_ktls_rx(struct mlx5_core_dev *mdev)
58 {
59 	return !is_kdump_kernel() && MLX5_CAP_GEN(mdev, tls_rx);
60 }
61 
62 struct mlx5e_tls_sw_stats {
63 	atomic64_t tx_tls_ctx;
64 	atomic64_t tx_tls_del;
65 	atomic64_t rx_tls_ctx;
66 	atomic64_t rx_tls_del;
67 };
68 
69 struct mlx5e_tls {
70 	struct mlx5e_tls_sw_stats sw_stats;
71 	struct workqueue_struct *rx_wq;
72 };
73 
74 int mlx5e_ktls_init(struct mlx5e_priv *priv);
75 void mlx5e_ktls_cleanup(struct mlx5e_priv *priv);
76 
77 int mlx5e_ktls_get_count(struct mlx5e_priv *priv);
78 int mlx5e_ktls_get_strings(struct mlx5e_priv *priv, uint8_t *data);
79 int mlx5e_ktls_get_stats(struct mlx5e_priv *priv, u64 *data);
80 
81 #else
82 static inline void mlx5e_ktls_build_netdev(struct mlx5e_priv *priv)
83 {
84 }
85 
86 static inline int mlx5e_ktls_init_rx(struct mlx5e_priv *priv)
87 {
88 	return 0;
89 }
90 
91 static inline void mlx5e_ktls_cleanup_rx(struct mlx5e_priv *priv)
92 {
93 }
94 
95 static inline int mlx5e_ktls_set_feature_rx(struct net_device *netdev, bool enable)
96 {
97 	netdev_warn(netdev, "kTLS is not supported\n");
98 	return -EOPNOTSUPP;
99 }
100 
101 static inline struct mlx5e_ktls_resync_resp *
102 mlx5e_ktls_rx_resync_create_resp_list(void)
103 {
104 	return ERR_PTR(-EOPNOTSUPP);
105 }
106 
107 static inline void
108 mlx5e_ktls_rx_resync_destroy_resp_list(struct mlx5e_ktls_resync_resp *resp_list) {}
109 
110 static inline bool mlx5e_is_ktls_rx(struct mlx5_core_dev *mdev)
111 {
112 	return false;
113 }
114 
115 static inline int mlx5e_ktls_init(struct mlx5e_priv *priv) { return 0; }
116 static inline void mlx5e_ktls_cleanup(struct mlx5e_priv *priv) { }
117 static inline int mlx5e_ktls_get_count(struct mlx5e_priv *priv) { return 0; }
118 static inline int mlx5e_ktls_get_strings(struct mlx5e_priv *priv, uint8_t *data)
119 {
120 	return 0;
121 }
122 
123 static inline int mlx5e_ktls_get_stats(struct mlx5e_priv *priv, u64 *data)
124 {
125 	return 0;
126 }
127 #endif
128 
129 #endif /* __MLX5E_TLS_H__ */
130