common/cnxk: support anti-replay check in SW for cn9k
[dpdk.git] / drivers / common / cnxk / roc_se.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #ifndef __ROC_SE_H__
6 #define __ROC_SE_H__
7
8 /* SE opcodes */
9 #define ROC_SE_MAJOR_OP_FC            0x33
10 #define ROC_SE_FC_MINOR_OP_ENCRYPT    0x0
11 #define ROC_SE_FC_MINOR_OP_DECRYPT    0x1
12 #define ROC_SE_FC_MINOR_OP_HMAC_FIRST 0x10
13
14 #define ROC_SE_MAJOR_OP_HASH       0x34
15 #define ROC_SE_MAJOR_OP_HMAC       0x35
16 #define ROC_SE_MAJOR_OP_ZUC_SNOW3G 0x37
17 #define ROC_SE_MAJOR_OP_KASUMI     0x38
18 #define ROC_SE_MAJOR_OP_MISC       0x01
19
20 #define ROC_SE_MAX_AAD_SIZE 64
21 #define ROC_SE_MAX_MAC_LEN  64
22
23 #define ROC_SE_OFF_CTRL_LEN 8
24 #define ROC_SE_DMA_MODE     (1 << 7)
25
26 #define ROC_SE_MAX_SG_IN_OUT_CNT 32
27 #define ROC_SE_MAX_SG_CNT        (ROC_SE_MAX_SG_IN_OUT_CNT / 2)
28
29 #define ROC_SE_SG_LIST_HDR_SIZE (8u)
30 #define ROC_SE_SG_ENTRY_SIZE    sizeof(struct roc_se_sglist_comp)
31
32 #define ROC_SE_ZS_EA 0x1
33 #define ROC_SE_ZS_IA 0x2
34 #define ROC_SE_K_F8  0x4
35 #define ROC_SE_K_F9  0x8
36
37 #define ROC_SE_FC_GEN    0x1
38 #define ROC_SE_PDCP      0x2
39 #define ROC_SE_KASUMI    0x3
40 #define ROC_SE_HASH_HMAC 0x4
41
42 #define ROC_SE_OP_CIPHER_ENCRYPT 0x1
43 #define ROC_SE_OP_CIPHER_DECRYPT 0x2
44 #define ROC_SE_OP_CIPHER_MASK                                                  \
45         (ROC_SE_OP_CIPHER_ENCRYPT | ROC_SE_OP_CIPHER_DECRYPT)
46
47 #define ROC_SE_OP_AUTH_VERIFY   0x4
48 #define ROC_SE_OP_AUTH_GENERATE 0x8
49 #define ROC_SE_OP_AUTH_MASK                                                    \
50         (ROC_SE_OP_AUTH_VERIFY | ROC_SE_OP_AUTH_GENERATE)
51
52 #define ROC_SE_OP_ENCODE (ROC_SE_OP_CIPHER_ENCRYPT | ROC_SE_OP_AUTH_GENERATE)
53 #define ROC_SE_OP_DECODE (ROC_SE_OP_CIPHER_DECRYPT | ROC_SE_OP_AUTH_VERIFY)
54
55 #define ROC_SE_ALWAYS_USE_SEPARATE_BUF
56
57 /*
58  * Parameters for Flexi Crypto
59  * requests
60  */
61 #define ROC_SE_VALID_AAD_BUF           0x01
62 #define ROC_SE_VALID_MAC_BUF           0x02
63 #define ROC_SE_VALID_IV_BUF            0x04
64 #define ROC_SE_SINGLE_BUF_INPLACE      0x08
65 #define ROC_SE_SINGLE_BUF_HEADROOM     0x10
66
67 #define ROC_SE_ENCR_IV_OFFSET(__d_offs) (((__d_offs) >> 32) & 0xffff)
68 #define ROC_SE_ENCR_OFFSET(__d_offs)    (((__d_offs) >> 16) & 0xffff)
69 #define ROC_SE_AUTH_OFFSET(__d_offs)    ((__d_offs) & 0xffff)
70 #define ROC_SE_ENCR_DLEN(__d_lens)      ((__d_lens) >> 32)
71 #define ROC_SE_AUTH_DLEN(__d_lens)      ((__d_lens) & 0xffffffff)
72
73 typedef enum { ROC_SE_FROM_CTX = 0, ROC_SE_FROM_DPTR = 1 } roc_se_input_type;
74
75 typedef enum {
76         ROC_SE_MD5_TYPE = 1,
77         ROC_SE_SHA1_TYPE = 2,
78         ROC_SE_SHA2_SHA224 = 3,
79         ROC_SE_SHA2_SHA256 = 4,
80         ROC_SE_SHA2_SHA384 = 5,
81         ROC_SE_SHA2_SHA512 = 6,
82         ROC_SE_GMAC_TYPE = 7,
83         ROC_SE_POLY1305 = 8,
84         ROC_SE_SHA3_SHA224 = 10,
85         ROC_SE_SHA3_SHA256 = 11,
86         ROC_SE_SHA3_SHA384 = 12,
87         ROC_SE_SHA3_SHA512 = 13,
88         ROC_SE_SHA3_SHAKE256 = 14,
89         ROC_SE_SHA3_SHAKE512 = 15,
90
91         /* These are only for software use */
92         ROC_SE_ZUC_EIA3 = 0x90,
93         ROC_SE_SNOW3G_UIA2 = 0x91,
94         ROC_SE_AES_CMAC_EIA2 = 0x92,
95         ROC_SE_KASUMI_F9_CBC = 0x93,
96         ROC_SE_KASUMI_F9_ECB = 0x94,
97 } roc_se_auth_type;
98
99 typedef enum {
100         /* To support passthrough */
101         ROC_SE_PASSTHROUGH = 0x0,
102         /*
103          * These are defined by MC for Flexi crypto
104          * for field of 4 bits
105          */
106         ROC_SE_DES3_CBC = 0x1,
107         ROC_SE_DES3_ECB = 0x2,
108         ROC_SE_AES_CBC = 0x3,
109         ROC_SE_AES_ECB = 0x4,
110         ROC_SE_AES_CFB = 0x5,
111         ROC_SE_AES_CTR = 0x6,
112         ROC_SE_AES_GCM = 0x7,
113         ROC_SE_AES_XTS = 0x8,
114         ROC_SE_CHACHA20 = 0x9,
115
116         /* These are only for software use */
117         ROC_SE_ZUC_EEA3 = 0x90,
118         ROC_SE_SNOW3G_UEA2 = 0x91,
119         ROC_SE_AES_CTR_EEA2 = 0x92,
120         ROC_SE_KASUMI_F8_CBC = 0x93,
121         ROC_SE_KASUMI_F8_ECB = 0x94,
122 } roc_se_cipher_type;
123
124 typedef enum {
125         /* Microcode errors */
126         ROC_SE_NO_ERR = 0x00,
127         ROC_SE_ERR_OPCODE_UNSUPPORTED = 0x01,
128
129         /* SCATTER GATHER */
130         ROC_SE_ERR_SCATTER_GATHER_WRITE_LENGTH = 0x02,
131         ROC_SE_ERR_SCATTER_GATHER_LIST = 0x03,
132         ROC_SE_ERR_SCATTER_GATHER_NOT_SUPPORTED = 0x04,
133
134         /* SE GC */
135         ROC_SE_ERR_GC_LENGTH_INVALID = 0x41,
136         ROC_SE_ERR_GC_RANDOM_LEN_INVALID = 0x42,
137         ROC_SE_ERR_GC_DATA_LEN_INVALID = 0x43,
138         ROC_SE_ERR_GC_DRBG_TYPE_INVALID = 0x44,
139         ROC_SE_ERR_GC_CTX_LEN_INVALID = 0x45,
140         ROC_SE_ERR_GC_CIPHER_UNSUPPORTED = 0x46,
141         ROC_SE_ERR_GC_AUTH_UNSUPPORTED = 0x47,
142         ROC_SE_ERR_GC_OFFSET_INVALID = 0x48,
143         ROC_SE_ERR_GC_HASH_MODE_UNSUPPORTED = 0x49,
144         ROC_SE_ERR_GC_DRBG_ENTROPY_LEN_INVALID = 0x4a,
145         ROC_SE_ERR_GC_DRBG_ADDNL_LEN_INVALID = 0x4b,
146         ROC_SE_ERR_GC_ICV_MISCOMPARE = 0x4c,
147         ROC_SE_ERR_GC_DATA_UNALIGNED = 0x4d,
148
149         /* API Layer */
150         ROC_SE_ERR_REQ_PENDING = 0xfe,
151         ROC_SE_ERR_REQ_TIMEOUT = 0xff,
152
153 } roc_se_error_code;
154
155 typedef enum {
156         ROC_SE_AES_128_BIT = 0x1,
157         ROC_SE_AES_192_BIT = 0x2,
158         ROC_SE_AES_256_BIT = 0x3
159 } roc_se_aes_type;
160
161 typedef enum {
162         ROC_SE_PDCP_MAC_LEN_32_BIT = 0x1,
163         ROC_SE_PDCP_MAC_LEN_64_BIT = 0x2,
164         ROC_SE_PDCP_MAC_LEN_128_BIT = 0x3
165 } roc_se_pdcp_mac_len_type;
166
167 struct roc_se_sglist_comp {
168         union {
169                 uint64_t len;
170                 struct {
171                         uint16_t len[4];
172                 } s;
173         } u;
174         uint64_t ptr[4];
175 };
176
177 struct roc_se_enc_context {
178         uint64_t iv_source : 1;
179         uint64_t aes_key : 2;
180         uint64_t rsvd_60 : 1;
181         uint64_t enc_cipher : 4;
182         uint64_t auth_input_type : 1;
183         uint64_t rsvd_52_54 : 3;
184         uint64_t hash_type : 4;
185         uint64_t mac_len : 8;
186         uint64_t rsvd_39_0 : 40;
187         uint8_t encr_key[32];
188         uint8_t encr_iv[16];
189 };
190
191 struct roc_se_hmac_context {
192         uint8_t ipad[64];
193         uint8_t opad[64];
194 };
195
196 struct roc_se_context {
197         struct roc_se_enc_context enc;
198         struct roc_se_hmac_context hmac;
199 };
200
201 struct roc_se_otk_zuc_ctx {
202         union {
203                 uint64_t u64;
204                 struct {
205                         uint64_t rsvd_56 : 57;
206                         uint64_t mac_len : 2;
207                         uint64_t key_len : 2;
208                         uint64_t lfsr_state : 1;
209                         uint64_t alg_type : 2;
210                 } s;
211         } w0;
212         uint8_t ci_key[32];
213         uint8_t encr_auth_iv[24];
214         uint8_t zuc_const[32];
215 };
216
217 struct roc_se_onk_zuc_ctx {
218         uint8_t encr_auth_iv[16];
219         uint8_t ci_key[16];
220         uint8_t zuc_const[32];
221 };
222
223 struct roc_se_zuc_snow3g_ctx {
224         union {
225                 struct roc_se_onk_zuc_ctx onk_ctx;
226                 struct roc_se_otk_zuc_ctx otk_ctx;
227         } zuc;
228 };
229
230 struct roc_se_kasumi_ctx {
231         uint8_t reg_A[8];
232         uint8_t ci_key[16];
233 };
234
235 /* Buffer pointer */
236 struct roc_se_buf_ptr {
237         void *vaddr;
238         uint32_t size;
239         uint32_t resv;
240 };
241
242 /* IOV Pointer */
243 struct roc_se_iov_ptr {
244         int buf_cnt;
245         struct roc_se_buf_ptr bufs[0];
246 };
247
248 struct roc_se_fc_params {
249         /* 0th cache line */
250         union {
251                 struct roc_se_buf_ptr bufs[1];
252                 struct {
253                         struct roc_se_iov_ptr *src_iov;
254                         struct roc_se_iov_ptr *dst_iov;
255                 };
256         };
257         void *iv_buf;
258         void *auth_iv_buf;
259         struct roc_se_buf_ptr meta_buf;
260         struct roc_se_buf_ptr ctx_buf;
261         uint32_t rsvd2;
262         uint16_t rsvd3;
263         uint8_t cipher_iv_len;
264         uint8_t auth_iv_len;
265
266         /* 1st cache line */
267         struct roc_se_buf_ptr aad_buf __plt_cache_aligned;
268         struct roc_se_buf_ptr mac_buf;
269 };
270
271 PLT_STATIC_ASSERT((offsetof(struct roc_se_fc_params, aad_buf) % 128) == 0);
272
273 #define ROC_SE_PDCP_ALG_TYPE_ZUC     0
274 #define ROC_SE_PDCP_ALG_TYPE_SNOW3G  1
275 #define ROC_SE_PDCP_ALG_TYPE_AES_CTR 2
276
277 struct roc_se_ctx {
278         /* Below fields are accessed by sw */
279         uint64_t enc_cipher : 8;
280         uint64_t hash_type : 8;
281         uint64_t mac_len : 8;
282         uint64_t auth_key_len : 8;
283         uint64_t fc_type : 4;
284         uint64_t hmac : 1;
285         uint64_t zsk_flags : 3;
286         uint64_t k_ecb : 1;
287         uint64_t pdcp_alg_type : 2;
288         uint64_t rsvd : 21;
289         union cpt_inst_w4 template_w4;
290         /* Below fields are accessed by hardware */
291         union {
292                 struct roc_se_context fctx;
293                 struct roc_se_zuc_snow3g_ctx zs_ctx;
294                 struct roc_se_kasumi_ctx k_ctx;
295         } se_ctx;
296         uint8_t *auth_key;
297 };
298
299 int __roc_api roc_se_auth_key_set(struct roc_se_ctx *se_ctx,
300                                   roc_se_auth_type type, const uint8_t *key,
301                                   uint16_t key_len, uint16_t mac_len);
302
303 int __roc_api roc_se_ciph_key_set(struct roc_se_ctx *se_ctx,
304                                   roc_se_cipher_type type, const uint8_t *key,
305                                   uint16_t key_len, uint8_t *salt);
306
307 void __roc_api roc_se_ctx_swap(struct roc_se_ctx *se_ctx);
308 #endif /* __ROC_SE_H__ */