xref: /dpdk/drivers/common/cpt/cpt_mcode_defines.h (revision 88d664aa1ccf4a1c1b9ab48575683ca915ab83d7)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium, Inc
3  */
4 
5 #ifndef _CPT_MCODE_DEFINES_H_
6 #define _CPT_MCODE_DEFINES_H_
7 
8 #include <rte_byteorder.h>
9 #include <rte_crypto_asym.h>
10 #include <rte_memory.h>
11 
12 /*
13  * This file defines macros and structures according to microcode spec
14  *
15  */
16 /* SE opcodes */
17 #define CPT_MAJOR_OP_FC		0x33
18 #define CPT_MAJOR_OP_HASH	0x34
19 #define CPT_MAJOR_OP_HMAC	0x35
20 #define CPT_MAJOR_OP_ZUC_SNOW3G	0x37
21 #define CPT_MAJOR_OP_KASUMI	0x38
22 #define CPT_MAJOR_OP_MISC	0x01
23 
24 /* AE opcodes */
25 #define CPT_MAJOR_OP_MODEX	0x03
26 #define CPT_MINOR_OP_MODEX	0x01
27 #define CPT_MINOR_OP_PKCS_ENC	0x02
28 #define CPT_MINOR_OP_PKCS_ENC_CRT	0x03
29 #define CPT_MINOR_OP_PKCS_DEC	0x04
30 #define CPT_MINOR_OP_PKCS_DEC_CRT	0x05
31 #define CPT_MINOR_OP_MODEX_CRT	0x06
32 
33 #define CPT_BLOCK_TYPE1 0
34 #define CPT_BLOCK_TYPE2 1
35 
36 #define CPT_BYTE_16		16
37 #define CPT_BYTE_24		24
38 #define CPT_BYTE_32		32
39 #define CPT_MAX_SG_IN_OUT_CNT	32
40 #define CPT_MAX_SG_CNT		(CPT_MAX_SG_IN_OUT_CNT/2)
41 
42 #define COMPLETION_CODE_SIZE	8
43 #define COMPLETION_CODE_INIT	0
44 
45 #define SG_LIST_HDR_SIZE	(8u)
46 #define SG_ENTRY_SIZE		sizeof(sg_comp_t)
47 
48 #define CPT_DMA_MODE		(1 << 7)
49 
50 #define CPT_FROM_CTX		0
51 #define CPT_FROM_DPTR		1
52 
53 #define FC_GEN			0x1
54 #define ZUC_SNOW3G		0x2
55 #define KASUMI			0x3
56 #define HASH_HMAC		0x4
57 
58 #define ZS_EA			0x1
59 #define ZS_IA			0x2
60 #define K_F8			0x4
61 #define K_F9			0x8
62 
63 #define CPT_OP_CIPHER_ENCRYPT	0x1
64 #define CPT_OP_CIPHER_DECRYPT	0x2
65 #define CPT_OP_CIPHER_MASK	0x3
66 
67 #define CPT_OP_AUTH_VERIFY	0x4
68 #define CPT_OP_AUTH_GENERATE	0x8
69 #define CPT_OP_AUTH_MASK	0xC
70 
71 #define CPT_OP_ENCODE	(CPT_OP_CIPHER_ENCRYPT | CPT_OP_AUTH_GENERATE)
72 #define CPT_OP_DECODE	(CPT_OP_CIPHER_DECRYPT | CPT_OP_AUTH_VERIFY)
73 
74 /* #define CPT_ALWAYS_USE_SG_MODE */
75 #define CPT_ALWAYS_USE_SEPARATE_BUF
76 
77 /*
78  * Parameters for Flexi Crypto
79  * requests
80  */
81 #define VALID_AAD_BUF 0x01
82 #define VALID_MAC_BUF 0x02
83 #define VALID_IV_BUF 0x04
84 #define SINGLE_BUF_INPLACE 0x08
85 #define SINGLE_BUF_HEADTAILROOM 0x10
86 
87 #define ENCR_IV_OFFSET(__d_offs) ((__d_offs >> 32) & 0xffff)
88 #define ENCR_OFFSET(__d_offs) ((__d_offs >> 16) & 0xffff)
89 #define AUTH_OFFSET(__d_offs) (__d_offs & 0xffff)
90 #define ENCR_DLEN(__d_lens) (__d_lens >> 32)
91 #define AUTH_DLEN(__d_lens) (__d_lens & 0xffffffff)
92 
93 /* FC offset_control at start of DPTR in bytes */
94 #define OFF_CTRL_LEN  8 /**< bytes */
95 
96 typedef enum {
97 	MD5_TYPE        = 1,
98 	SHA1_TYPE       = 2,
99 	SHA2_SHA224     = 3,
100 	SHA2_SHA256     = 4,
101 	SHA2_SHA384     = 5,
102 	SHA2_SHA512     = 6,
103 	GMAC_TYPE       = 7,
104 	XCBC_TYPE       = 8,
105 	SHA3_SHA224     = 10,
106 	SHA3_SHA256     = 11,
107 	SHA3_SHA384     = 12,
108 	SHA3_SHA512     = 13,
109 	SHA3_SHAKE256   = 14,
110 	SHA3_SHAKE512   = 15,
111 
112 	/* These are only for software use */
113 	ZUC_EIA3        = 0x90,
114 	SNOW3G_UIA2     = 0x91,
115 	KASUMI_F9_CBC   = 0x92,
116 	KASUMI_F9_ECB   = 0x93,
117 } mc_hash_type_t;
118 
119 typedef enum {
120 	/* To support passthrough */
121 	PASSTHROUGH  = 0x0,
122 	/*
123 	 * These are defined by MC for Flexi crypto
124 	 * for field of 4 bits
125 	 */
126 	DES3_CBC    = 0x1,
127 	DES3_ECB    = 0x2,
128 	AES_CBC     = 0x3,
129 	AES_ECB     = 0x4,
130 	AES_CFB     = 0x5,
131 	AES_CTR     = 0x6,
132 	AES_GCM     = 0x7,
133 	AES_XTS     = 0x8,
134 
135 	/* These are only for software use */
136 	ZUC_EEA3        = 0x90,
137 	SNOW3G_UEA2     = 0x91,
138 	KASUMI_F8_CBC   = 0x92,
139 	KASUMI_F8_ECB   = 0x93,
140 } mc_cipher_type_t;
141 
142 typedef enum {
143 	AES_128_BIT = 0x1,
144 	AES_192_BIT = 0x2,
145 	AES_256_BIT = 0x3
146 } mc_aes_type_t;
147 
148 typedef enum {
149 	/* Microcode errors */
150 	NO_ERR = 0x00,
151 	ERR_OPCODE_UNSUPPORTED = 0x01,
152 
153 	/* SCATTER GATHER */
154 	ERR_SCATTER_GATHER_WRITE_LENGTH = 0x02,
155 	ERR_SCATTER_GATHER_LIST = 0x03,
156 	ERR_SCATTER_GATHER_NOT_SUPPORTED = 0x04,
157 
158 	/* SE GC */
159 	ERR_GC_LENGTH_INVALID = 0x41,
160 	ERR_GC_RANDOM_LEN_INVALID = 0x42,
161 	ERR_GC_DATA_LEN_INVALID = 0x43,
162 	ERR_GC_DRBG_TYPE_INVALID = 0x44,
163 	ERR_GC_CTX_LEN_INVALID = 0x45,
164 	ERR_GC_CIPHER_UNSUPPORTED = 0x46,
165 	ERR_GC_AUTH_UNSUPPORTED = 0x47,
166 	ERR_GC_OFFSET_INVALID = 0x48,
167 	ERR_GC_HASH_MODE_UNSUPPORTED = 0x49,
168 	ERR_GC_DRBG_ENTROPY_LEN_INVALID = 0x4a,
169 	ERR_GC_DRBG_ADDNL_LEN_INVALID = 0x4b,
170 	ERR_GC_ICV_MISCOMPARE = 0x4c,
171 	ERR_GC_DATA_UNALIGNED = 0x4d,
172 
173 	/* API Layer */
174 	ERR_BAD_ALT_CCODE = 0xfd,
175 	ERR_REQ_PENDING = 0xfe,
176 	ERR_REQ_TIMEOUT = 0xff,
177 
178 	ERR_BAD_INPUT_LENGTH = (0x40000000 | 384),    /* 0x40000180 */
179 	ERR_BAD_KEY_LENGTH,
180 	ERR_BAD_KEY_HANDLE,
181 	ERR_BAD_CONTEXT_HANDLE,
182 	ERR_BAD_SCALAR_LENGTH,
183 	ERR_BAD_DIGEST_LENGTH,
184 	ERR_BAD_INPUT_ARG,
185 	ERR_BAD_RECORD_PADDING,
186 	ERR_NB_REQUEST_PENDING,
187 	ERR_EIO,
188 	ERR_ENODEV,
189 } mc_error_code_t;
190 
191 /**
192  * Enumeration cpt_comp_e
193  *
194  * CPT Completion Enumeration
195  * Enumerates the values of CPT_RES_S[COMPCODE].
196  */
197 typedef enum {
198 	CPT_8X_COMP_E_NOTDONE    = (0x00),
199 	CPT_8X_COMP_E_GOOD       = (0x01),
200 	CPT_8X_COMP_E_FAULT      = (0x02),
201 	CPT_8X_COMP_E_SWERR      = (0x03),
202 	CPT_8X_COMP_E_HWERR      = (0x04),
203 	CPT_8X_COMP_E_LAST_ENTRY = (0xFF)
204 } cpt_comp_e_t;
205 
206 typedef struct sglist_comp {
207 	union {
208 		uint64_t len;
209 		struct {
210 			uint16_t len[4];
211 		} s;
212 	} u;
213 	uint64_t ptr[4];
214 } sg_comp_t;
215 
216 struct cpt_sess_misc {
217 	/** CPT opcode */
218 	uint16_t cpt_op:4;
219 	/** ZUC, SNOW3G &  KASUMI flags */
220 	uint16_t zsk_flag:4;
221 	/** Flag for AES GCM */
222 	uint16_t aes_gcm:1;
223 	/** Flag for AES CTR */
224 	uint16_t aes_ctr:1;
225 	/** Flag for NULL cipher/auth */
226 	uint16_t is_null:1;
227 	/** Flag for GMAC */
228 	uint16_t is_gmac:1;
229 	/** AAD length */
230 	uint16_t aad_length;
231 	/** MAC len in bytes */
232 	uint8_t mac_len;
233 	/** IV length in bytes */
234 	uint8_t iv_length;
235 	/** Auth IV length in bytes */
236 	uint8_t auth_iv_length;
237 	/** Reserved field */
238 	uint8_t rsvd1;
239 	/** IV offset in bytes */
240 	uint16_t iv_offset;
241 	/** Auth IV offset in bytes */
242 	uint16_t auth_iv_offset;
243 	/** Salt */
244 	uint32_t salt;
245 	/** Context DMA address */
246 	phys_addr_t ctx_dma_addr;
247 };
248 
249 typedef union {
250 	uint64_t flags;
251 	struct {
252 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
253 		uint64_t enc_cipher   : 4;
254 		uint64_t reserved1    : 1;
255 		uint64_t aes_key      : 2;
256 		uint64_t iv_source    : 1;
257 		uint64_t hash_type    : 4;
258 		uint64_t reserved2    : 3;
259 		uint64_t auth_input_type : 1;
260 		uint64_t mac_len      : 8;
261 		uint64_t reserved3    : 8;
262 		uint64_t encr_offset  : 16;
263 		uint64_t iv_offset    : 8;
264 		uint64_t auth_offset  : 8;
265 #else
266 		uint64_t auth_offset  : 8;
267 		uint64_t iv_offset    : 8;
268 		uint64_t encr_offset  : 16;
269 		uint64_t reserved3    : 8;
270 		uint64_t mac_len      : 8;
271 		uint64_t auth_input_type : 1;
272 		uint64_t reserved2    : 3;
273 		uint64_t hash_type    : 4;
274 		uint64_t iv_source    : 1;
275 		uint64_t aes_key      : 2;
276 		uint64_t reserved1    : 1;
277 		uint64_t enc_cipher   : 4;
278 #endif
279 	} e;
280 } encr_ctrl_t;
281 
282 typedef struct {
283 	encr_ctrl_t enc_ctrl;
284 	uint8_t  encr_key[32];
285 	uint8_t  encr_iv[16];
286 } mc_enc_context_t;
287 
288 typedef struct {
289 	uint8_t  ipad[64];
290 	uint8_t  opad[64];
291 } mc_fc_hmac_context_t;
292 
293 typedef struct {
294 	mc_enc_context_t     enc;
295 	mc_fc_hmac_context_t hmac;
296 } mc_fc_context_t;
297 
298 typedef struct {
299 	uint8_t encr_auth_iv[16];
300 	uint8_t ci_key[16];
301 	uint8_t zuc_const[32];
302 } mc_zuc_snow3g_ctx_t;
303 
304 typedef struct {
305 	uint8_t reg_A[8];
306 	uint8_t ci_key[16];
307 } mc_kasumi_ctx_t;
308 
309 struct cpt_ctx {
310 	/* Below fields are accessed by sw */
311 	uint64_t enc_cipher	:8;
312 	uint64_t hash_type	:8;
313 	uint64_t mac_len	:8;
314 	uint64_t auth_key_len	:8;
315 	uint64_t fc_type	:4;
316 	uint64_t hmac		:1;
317 	uint64_t zsk_flags	:3;
318 	uint64_t k_ecb		:1;
319 	uint64_t snow3g		:2;
320 	uint64_t rsvd		:21;
321 	/* Below fields are accessed by hardware */
322 	union {
323 		mc_fc_context_t fctx;
324 		mc_zuc_snow3g_ctx_t zs_ctx;
325 		mc_kasumi_ctx_t k_ctx;
326 	};
327 	uint8_t  auth_key[64];
328 };
329 
330 struct cpt_asym_sess_misc {
331 	enum rte_crypto_asym_xform_type xfrm_type;
332 	union {
333 		struct rte_crypto_rsa_xform rsa_ctx;
334 		struct rte_crypto_modex_xform mod_ctx;
335 	};
336 };
337 
338 /* Buffer pointer */
339 typedef struct buf_ptr {
340 	void *vaddr;
341 	phys_addr_t dma_addr;
342 	uint32_t size;
343 	uint32_t resv;
344 } buf_ptr_t;
345 
346 /* IOV Pointer */
347 typedef struct{
348 	int buf_cnt;
349 	buf_ptr_t bufs[0];
350 } iov_ptr_t;
351 
352 typedef union opcode_info {
353 	uint16_t flags;
354 	struct {
355 		uint8_t major;
356 		uint8_t minor;
357 	} s;
358 } opcode_info_t;
359 
360 typedef struct fc_params {
361 	/* 0th cache line */
362 	union {
363 		buf_ptr_t bufs[1];
364 		struct {
365 			iov_ptr_t *src_iov;
366 			iov_ptr_t *dst_iov;
367 		};
368 	};
369 	void *iv_buf;
370 	void *auth_iv_buf;
371 	buf_ptr_t meta_buf;
372 	buf_ptr_t ctx_buf;
373 	uint64_t rsvd2;
374 
375 	/* 1st cache line */
376 	buf_ptr_t aad_buf;
377 	buf_ptr_t mac_buf;
378 
379 } fc_params_t;
380 
381 /*
382  * Parameters for asymmetric operations
383  */
384 struct asym_op_params {
385 	struct cpt_request_info *req;
386 	phys_addr_t meta_buf;
387 };
388 
389 /*
390  * Parameters for digest
391  * generate requests
392  * Only src_iov, op, ctx_buf, mac_buf, prep_req
393  * meta_buf, auth_data_len are used for digest gen.
394  */
395 typedef struct fc_params digest_params_t;
396 
397 /* Cipher Algorithms */
398 typedef mc_cipher_type_t cipher_type_t;
399 
400 /* Auth Algorithms */
401 typedef mc_hash_type_t auth_type_t;
402 
403 /* Helper macros */
404 
405 #define CPT_P_ENC_CTRL(fctx)  fctx->enc.enc_ctrl.e
406 
407 #define SRC_IOV_SIZE \
408 	(sizeof(iov_ptr_t) + (sizeof(buf_ptr_t) * CPT_MAX_SG_CNT))
409 #define DST_IOV_SIZE \
410 	(sizeof(iov_ptr_t) + (sizeof(buf_ptr_t) * CPT_MAX_SG_CNT))
411 
412 #define SESS_PRIV(__sess) \
413 	(void *)((uint8_t *)__sess + sizeof(struct cpt_sess_misc))
414 
415 /*
416  * Get the session size
417  *
418  * @return
419  *   - session size
420  */
421 static __rte_always_inline unsigned int
422 cpt_get_session_size(void)
423 {
424 	unsigned int ctx_len = sizeof(struct cpt_ctx);
425 	return (sizeof(struct cpt_sess_misc) + RTE_ALIGN_CEIL(ctx_len, 8));
426 }
427 #endif /* _CPT_MCODE_DEFINES_H_ */
428