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