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