1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020, Mellanox Technologies inc. All rights reserved. */
3
4 #include "en_accel/ktls_txrx.h"
5 #include "en_accel/ktls_utils.h"
6
7 enum {
8 MLX5E_STATIC_PARAMS_CONTEXT_TLS_1_2 = 0x2,
9 };
10
11 enum {
12 MLX5E_ENCRYPTION_STANDARD_TLS = 0x1,
13 };
14
15 #define EXTRACT_INFO_FIELDS do { \
16 salt = info->salt; \
17 rec_seq = info->rec_seq; \
18 salt_sz = sizeof(info->salt); \
19 rec_seq_sz = sizeof(info->rec_seq); \
20 } while (0)
21
22 static void
fill_static_params(struct mlx5_wqe_tls_static_params_seg * params,struct tls12_crypto_info_aes_gcm_128 * info,u32 key_id,u32 resync_tcp_sn)23 fill_static_params(struct mlx5_wqe_tls_static_params_seg *params,
24 struct tls12_crypto_info_aes_gcm_128 *info,
25 u32 key_id, u32 resync_tcp_sn)
26 {
27 char *initial_rn, *gcm_iv;
28 u16 salt_sz, rec_seq_sz;
29 char *salt, *rec_seq;
30 u8 tls_version;
31 u8 *ctx;
32
33 ctx = params->ctx;
34
35 EXTRACT_INFO_FIELDS;
36
37 gcm_iv = MLX5_ADDR_OF(tls_static_params, ctx, gcm_iv);
38 initial_rn = MLX5_ADDR_OF(tls_static_params, ctx, initial_record_number);
39
40 memcpy(gcm_iv, salt, salt_sz);
41 memcpy(initial_rn, rec_seq, rec_seq_sz);
42
43 tls_version = MLX5E_STATIC_PARAMS_CONTEXT_TLS_1_2;
44
45 MLX5_SET(tls_static_params, ctx, tls_version, tls_version);
46 MLX5_SET(tls_static_params, ctx, const_1, 1);
47 MLX5_SET(tls_static_params, ctx, const_2, 2);
48 MLX5_SET(tls_static_params, ctx, encryption_standard,
49 MLX5E_ENCRYPTION_STANDARD_TLS);
50 MLX5_SET(tls_static_params, ctx, resync_tcp_sn, resync_tcp_sn);
51 MLX5_SET(tls_static_params, ctx, dek_index, key_id);
52 }
53
54 void
mlx5e_ktls_build_static_params(struct mlx5e_set_tls_static_params_wqe * wqe,u16 pc,u32 sqn,struct tls12_crypto_info_aes_gcm_128 * info,u32 tis_tir_num,u32 key_id,u32 resync_tcp_sn,bool fence,enum tls_offload_ctx_dir direction)55 mlx5e_ktls_build_static_params(struct mlx5e_set_tls_static_params_wqe *wqe,
56 u16 pc, u32 sqn,
57 struct tls12_crypto_info_aes_gcm_128 *info,
58 u32 tis_tir_num, u32 key_id, u32 resync_tcp_sn,
59 bool fence, enum tls_offload_ctx_dir direction)
60 {
61 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
62 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
63 u8 opmod = direction == TLS_OFFLOAD_CTX_DIR_TX ?
64 MLX5_OPC_MOD_TLS_TIS_STATIC_PARAMS :
65 MLX5_OPC_MOD_TLS_TIR_STATIC_PARAMS;
66
67 #define STATIC_PARAMS_DS_CNT DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_DS)
68
69 cseg->opmod_idx_opcode = cpu_to_be32((pc << 8) | MLX5_OPCODE_UMR | (opmod << 24));
70 cseg->qpn_ds = cpu_to_be32((sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
71 STATIC_PARAMS_DS_CNT);
72 cseg->fm_ce_se = fence ? MLX5_FENCE_MODE_INITIATOR_SMALL : 0;
73 cseg->tis_tir_num = cpu_to_be32(tis_tir_num << 8);
74
75 ucseg->flags = MLX5_UMR_INLINE;
76 ucseg->bsf_octowords = cpu_to_be16(MLX5_ST_SZ_BYTES(tls_static_params) / 16);
77
78 fill_static_params(&wqe->params, info, key_id, resync_tcp_sn);
79 }
80
81 static void
fill_progress_params(struct mlx5_wqe_tls_progress_params_seg * params,u32 tis_tir_num,u32 next_record_tcp_sn)82 fill_progress_params(struct mlx5_wqe_tls_progress_params_seg *params, u32 tis_tir_num,
83 u32 next_record_tcp_sn)
84 {
85 u8 *ctx = params->ctx;
86
87 params->tis_tir_num = cpu_to_be32(tis_tir_num);
88
89 MLX5_SET(tls_progress_params, ctx, next_record_tcp_sn,
90 next_record_tcp_sn);
91 MLX5_SET(tls_progress_params, ctx, record_tracker_state,
92 MLX5E_TLS_PROGRESS_PARAMS_RECORD_TRACKER_STATE_START);
93 MLX5_SET(tls_progress_params, ctx, auth_state,
94 MLX5E_TLS_PROGRESS_PARAMS_AUTH_STATE_NO_OFFLOAD);
95 }
96
97 void
mlx5e_ktls_build_progress_params(struct mlx5e_set_tls_progress_params_wqe * wqe,u16 pc,u32 sqn,u32 tis_tir_num,bool fence,u32 next_record_tcp_sn,enum tls_offload_ctx_dir direction)98 mlx5e_ktls_build_progress_params(struct mlx5e_set_tls_progress_params_wqe *wqe,
99 u16 pc, u32 sqn,
100 u32 tis_tir_num, bool fence,
101 u32 next_record_tcp_sn,
102 enum tls_offload_ctx_dir direction)
103 {
104 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
105 u8 opmod = direction == TLS_OFFLOAD_CTX_DIR_TX ?
106 MLX5_OPC_MOD_TLS_TIS_PROGRESS_PARAMS :
107 MLX5_OPC_MOD_TLS_TIR_PROGRESS_PARAMS;
108
109 #define PROGRESS_PARAMS_DS_CNT DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_DS)
110
111 cseg->opmod_idx_opcode =
112 cpu_to_be32((pc << 8) | MLX5_OPCODE_SET_PSV | (opmod << 24));
113 cseg->qpn_ds = cpu_to_be32((sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
114 PROGRESS_PARAMS_DS_CNT);
115 cseg->fm_ce_se = fence ? MLX5_FENCE_MODE_INITIATOR_SMALL : 0;
116
117 fill_progress_params(&wqe->params, tis_tir_num, next_record_tcp_sn);
118 }
119
120