blob: d4955abf873b0d0ae3efc88c7e7fe1f65c7d8a38 [file] [log] [blame]
Antoine Ténart1b44c5a2017-05-24 16:10:34 +02001/*
2 * Copyright (C) 2017 Marvell
3 *
4 * Antoine Tenart <antoine.tenart@free-electrons.com>
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
10
11#ifndef __SAFEXCEL_H__
12#define __SAFEXCEL_H__
13
14#include <crypto/algapi.h>
15#include <crypto/internal/hash.h>
16#include <crypto/skcipher.h>
17
18#define EIP197_HIA_VERSION_LE 0xca35
19#define EIP197_HIA_VERSION_BE 0x35ca
20
21/* Static configuration */
Antoine Ténartfc8c72b2017-12-14 15:26:54 +010022#define EIP197_DEFAULT_RING_SIZE 400
Antoine Ténart1b44c5a2017-05-24 16:10:34 +020023#define EIP197_MAX_TOKENS 5
24#define EIP197_MAX_RINGS 4
25#define EIP197_FETCH_COUNT 1
Antoine Ténartfc8c72b2017-12-14 15:26:54 +010026#define EIP197_MAX_BATCH_SZ 64
Antoine Ténart1b44c5a2017-05-24 16:10:34 +020027
28#define EIP197_GFP_FLAGS(base) ((base).flags & CRYPTO_TFM_REQ_MAY_SLEEP ? \
29 GFP_KERNEL : GFP_ATOMIC)
30
31/* CDR/RDR register offsets */
32#define EIP197_HIA_xDR_OFF(r) (0x80000 + (r) * 0x1000)
33#define EIP197_HIA_CDR(r) (EIP197_HIA_xDR_OFF(r))
34#define EIP197_HIA_RDR(r) (EIP197_HIA_xDR_OFF(r) + 0x800)
35#define EIP197_HIA_xDR_RING_BASE_ADDR_LO 0x0
36#define EIP197_HIA_xDR_RING_BASE_ADDR_HI 0x4
37#define EIP197_HIA_xDR_RING_SIZE 0x18
38#define EIP197_HIA_xDR_DESC_SIZE 0x1c
39#define EIP197_HIA_xDR_CFG 0x20
40#define EIP197_HIA_xDR_DMA_CFG 0x24
41#define EIP197_HIA_xDR_THRESH 0x28
42#define EIP197_HIA_xDR_PREP_COUNT 0x2c
43#define EIP197_HIA_xDR_PROC_COUNT 0x30
44#define EIP197_HIA_xDR_PREP_PNTR 0x34
45#define EIP197_HIA_xDR_PROC_PNTR 0x38
46#define EIP197_HIA_xDR_STAT 0x3c
47
48/* register offsets */
49#define EIP197_HIA_DFE_CFG 0x8c000
50#define EIP197_HIA_DFE_THR_CTRL 0x8c040
51#define EIP197_HIA_DFE_THR_STAT 0x8c044
52#define EIP197_HIA_DSE_CFG 0x8d000
53#define EIP197_HIA_DSE_THR_CTRL 0x8d040
54#define EIP197_HIA_DSE_THR_STAT 0x8d044
55#define EIP197_HIA_RA_PE_CTRL 0x90010
56#define EIP197_HIA_RA_PE_STAT 0x90014
57#define EIP197_HIA_AIC_R_OFF(r) ((r) * 0x1000)
58#define EIP197_HIA_AIC_R_ENABLE_CTRL(r) (0x9e808 - EIP197_HIA_AIC_R_OFF(r))
59#define EIP197_HIA_AIC_R_ENABLED_STAT(r) (0x9e810 - EIP197_HIA_AIC_R_OFF(r))
60#define EIP197_HIA_AIC_R_ACK(r) (0x9e810 - EIP197_HIA_AIC_R_OFF(r))
61#define EIP197_HIA_AIC_R_ENABLE_CLR(r) (0x9e814 - EIP197_HIA_AIC_R_OFF(r))
62#define EIP197_HIA_AIC_G_ENABLE_CTRL 0x9f808
63#define EIP197_HIA_AIC_G_ENABLED_STAT 0x9f810
64#define EIP197_HIA_AIC_G_ACK 0x9f810
65#define EIP197_HIA_MST_CTRL 0x9fff4
66#define EIP197_HIA_OPTIONS 0x9fff8
67#define EIP197_HIA_VERSION 0x9fffc
68#define EIP197_PE_IN_DBUF_THRES 0xa0000
69#define EIP197_PE_IN_TBUF_THRES 0xa0100
70#define EIP197_PE_ICE_SCRATCH_RAM 0xa0800
71#define EIP197_PE_ICE_PUE_CTRL 0xa0c80
72#define EIP197_PE_ICE_SCRATCH_CTRL 0xa0d04
73#define EIP197_PE_ICE_FPP_CTRL 0xa0d80
74#define EIP197_PE_ICE_RAM_CTRL 0xa0ff0
75#define EIP197_PE_EIP96_FUNCTION_EN 0xa1004
76#define EIP197_PE_EIP96_CONTEXT_CTRL 0xa1008
77#define EIP197_PE_EIP96_CONTEXT_STAT 0xa100c
78#define EIP197_PE_OUT_DBUF_THRES 0xa1c00
79#define EIP197_PE_OUT_TBUF_THRES 0xa1d00
80#define EIP197_CLASSIFICATION_RAMS 0xe0000
81#define EIP197_TRC_CTRL 0xf0800
82#define EIP197_TRC_LASTRES 0xf0804
83#define EIP197_TRC_REGINDEX 0xf0808
84#define EIP197_TRC_PARAMS 0xf0820
85#define EIP197_TRC_FREECHAIN 0xf0824
86#define EIP197_TRC_PARAMS2 0xf0828
87#define EIP197_TRC_ECCCTRL 0xf0830
88#define EIP197_TRC_ECCSTAT 0xf0834
89#define EIP197_TRC_ECCADMINSTAT 0xf0838
90#define EIP197_TRC_ECCDATASTAT 0xf083c
91#define EIP197_TRC_ECCDATA 0xf0840
92#define EIP197_CS_RAM_CTRL 0xf7ff0
93#define EIP197_MST_CTRL 0xffff4
94
95/* EIP197_HIA_xDR_DESC_SIZE */
96#define EIP197_xDR_DESC_MODE_64BIT BIT(31)
97
98/* EIP197_HIA_xDR_DMA_CFG */
99#define EIP197_HIA_xDR_WR_RES_BUF BIT(22)
100#define EIP197_HIA_xDR_WR_CTRL_BUG BIT(23)
101#define EIP197_HIA_xDR_WR_OWN_BUF BIT(24)
Antoine Ténartaefa7942017-06-15 09:56:18 +0200102#define EIP197_HIA_xDR_CFG_WR_CACHE(n) (((n) & 0x7) << 25)
Antoine Ténart1b44c5a2017-05-24 16:10:34 +0200103#define EIP197_HIA_xDR_CFG_RD_CACHE(n) (((n) & 0x7) << 29)
104
105/* EIP197_HIA_CDR_THRESH */
106#define EIP197_HIA_CDR_THRESH_PROC_PKT(n) (n)
107#define EIP197_HIA_CDR_THRESH_PROC_MODE BIT(22)
108#define EIP197_HIA_CDR_THRESH_PKT_MODE BIT(23)
109#define EIP197_HIA_CDR_THRESH_TIMEOUT(n) ((n) << 24) /* x256 clk cycles */
110
111/* EIP197_HIA_RDR_THRESH */
112#define EIP197_HIA_RDR_THRESH_PROC_PKT(n) (n)
113#define EIP197_HIA_RDR_THRESH_PKT_MODE BIT(23)
114#define EIP197_HIA_RDR_THRESH_TIMEOUT(n) ((n) << 24) /* x256 clk cycles */
115
116/* EIP197_HIA_xDR_PREP_COUNT */
117#define EIP197_xDR_PREP_CLR_COUNT BIT(31)
118
119/* EIP197_HIA_xDR_PROC_COUNT */
Antoine Ténart7f77f5a2017-12-14 15:26:56 +0100120#define EIP197_xDR_PROC_xD_PKT_OFFSET 24
121#define EIP197_xDR_PROC_xD_PKT_MASK GENMASK(6, 0)
Antoine Ténart1b44c5a2017-05-24 16:10:34 +0200122#define EIP197_xDR_PROC_xD_COUNT(n) ((n) << 2)
123#define EIP197_xDR_PROC_xD_PKT(n) ((n) << 24)
124#define EIP197_xDR_PROC_CLR_COUNT BIT(31)
125
126/* EIP197_HIA_xDR_STAT */
127#define EIP197_xDR_DMA_ERR BIT(0)
128#define EIP197_xDR_PREP_CMD_THRES BIT(1)
129#define EIP197_xDR_ERR BIT(2)
130#define EIP197_xDR_THRESH BIT(4)
131#define EIP197_xDR_TIMEOUT BIT(5)
132
133#define EIP197_HIA_RA_PE_CTRL_RESET BIT(31)
134#define EIP197_HIA_RA_PE_CTRL_EN BIT(30)
135
136/* EIP197_HIA_AIC_R_ENABLE_CTRL */
137#define EIP197_CDR_IRQ(n) BIT((n) * 2)
138#define EIP197_RDR_IRQ(n) BIT((n) * 2 + 1)
139
140/* EIP197_HIA_DFE/DSE_CFG */
141#define EIP197_HIA_DxE_CFG_MIN_DATA_SIZE(n) ((n) << 0)
142#define EIP197_HIA_DxE_CFG_DATA_CACHE_CTRL(n) (((n) & 0x7) << 4)
143#define EIP197_HIA_DxE_CFG_MAX_DATA_SIZE(n) ((n) << 8)
Igal Libermanee1fd872017-06-15 09:56:21 +0200144#define EIP197_HIA_DSE_CFG_ALLWAYS_BUFFERABLE GENMASK(15, 14)
Antoine Ténart1b44c5a2017-05-24 16:10:34 +0200145#define EIP197_HIA_DxE_CFG_MIN_CTRL_SIZE(n) ((n) << 16)
146#define EIP197_HIA_DxE_CFG_CTRL_CACHE_CTRL(n) (((n) & 0x7) << 20)
147#define EIP197_HIA_DxE_CFG_MAX_CTRL_SIZE(n) ((n) << 24)
148#define EIP197_HIA_DFE_CFG_DIS_DEBUG (BIT(31) | BIT(29))
Igal Libermanc87925b2017-06-15 09:56:20 +0200149#define EIP197_HIA_DSE_CFG_EN_SINGLE_WR BIT(29)
Antoine Ténart1b44c5a2017-05-24 16:10:34 +0200150#define EIP197_HIA_DSE_CFG_DIS_DEBUG BIT(31)
151
152/* EIP197_HIA_DFE/DSE_THR_CTRL */
153#define EIP197_DxE_THR_CTRL_EN BIT(30)
154#define EIP197_DxE_THR_CTRL_RESET_PE BIT(31)
155
156/* EIP197_HIA_AIC_G_ENABLED_STAT */
157#define EIP197_G_IRQ_DFE(n) BIT((n) << 1)
158#define EIP197_G_IRQ_DSE(n) BIT(((n) << 1) + 1)
159#define EIP197_G_IRQ_RING BIT(16)
160#define EIP197_G_IRQ_PE(n) BIT((n) + 20)
161
162/* EIP197_HIA_MST_CTRL */
163#define RD_CACHE_3BITS 0x5
164#define WR_CACHE_3BITS 0x3
165#define RD_CACHE_4BITS (RD_CACHE_3BITS << 1 | BIT(0))
166#define WR_CACHE_4BITS (WR_CACHE_3BITS << 1 | BIT(0))
167#define EIP197_MST_CTRL_RD_CACHE(n) (((n) & 0xf) << 0)
168#define EIP197_MST_CTRL_WD_CACHE(n) (((n) & 0xf) << 4)
169#define EIP197_MST_CTRL_BYTE_SWAP BIT(24)
170#define EIP197_MST_CTRL_NO_BYTE_SWAP BIT(25)
171
172/* EIP197_PE_IN_DBUF/TBUF_THRES */
173#define EIP197_PE_IN_xBUF_THRES_MIN(n) ((n) << 8)
174#define EIP197_PE_IN_xBUF_THRES_MAX(n) ((n) << 12)
175
176/* EIP197_PE_OUT_DBUF_THRES */
177#define EIP197_PE_OUT_DBUF_THRES_MIN(n) ((n) << 0)
178#define EIP197_PE_OUT_DBUF_THRES_MAX(n) ((n) << 4)
179
180/* EIP197_PE_ICE_SCRATCH_CTRL */
181#define EIP197_PE_ICE_SCRATCH_CTRL_CHANGE_TIMER BIT(2)
182#define EIP197_PE_ICE_SCRATCH_CTRL_TIMER_EN BIT(3)
183#define EIP197_PE_ICE_SCRATCH_CTRL_CHANGE_ACCESS BIT(24)
184#define EIP197_PE_ICE_SCRATCH_CTRL_SCRATCH_ACCESS BIT(25)
185
186/* EIP197_PE_ICE_SCRATCH_RAM */
187#define EIP197_NUM_OF_SCRATCH_BLOCKS 32
188
189/* EIP197_PE_ICE_PUE/FPP_CTRL */
190#define EIP197_PE_ICE_x_CTRL_SW_RESET BIT(0)
191#define EIP197_PE_ICE_x_CTRL_CLR_ECC_NON_CORR BIT(14)
192#define EIP197_PE_ICE_x_CTRL_CLR_ECC_CORR BIT(15)
193
194/* EIP197_PE_ICE_RAM_CTRL */
195#define EIP197_PE_ICE_RAM_CTRL_PUE_PROG_EN BIT(0)
196#define EIP197_PE_ICE_RAM_CTRL_FPP_PROG_EN BIT(1)
197
198/* EIP197_PE_EIP96_FUNCTION_EN */
199#define EIP197_FUNCTION_RSVD (BIT(6) | BIT(15) | BIT(20) | BIT(23))
200#define EIP197_PROTOCOL_HASH_ONLY BIT(0)
201#define EIP197_PROTOCOL_ENCRYPT_ONLY BIT(1)
202#define EIP197_PROTOCOL_HASH_ENCRYPT BIT(2)
203#define EIP197_PROTOCOL_HASH_DECRYPT BIT(3)
204#define EIP197_PROTOCOL_ENCRYPT_HASH BIT(4)
205#define EIP197_PROTOCOL_DECRYPT_HASH BIT(5)
206#define EIP197_ALG_ARC4 BIT(7)
207#define EIP197_ALG_AES_ECB BIT(8)
208#define EIP197_ALG_AES_CBC BIT(9)
209#define EIP197_ALG_AES_CTR_ICM BIT(10)
210#define EIP197_ALG_AES_OFB BIT(11)
211#define EIP197_ALG_AES_CFB BIT(12)
212#define EIP197_ALG_DES_ECB BIT(13)
213#define EIP197_ALG_DES_CBC BIT(14)
214#define EIP197_ALG_DES_OFB BIT(16)
215#define EIP197_ALG_DES_CFB BIT(17)
216#define EIP197_ALG_3DES_ECB BIT(18)
217#define EIP197_ALG_3DES_CBC BIT(19)
218#define EIP197_ALG_3DES_OFB BIT(21)
219#define EIP197_ALG_3DES_CFB BIT(22)
220#define EIP197_ALG_MD5 BIT(24)
221#define EIP197_ALG_HMAC_MD5 BIT(25)
222#define EIP197_ALG_SHA1 BIT(26)
223#define EIP197_ALG_HMAC_SHA1 BIT(27)
224#define EIP197_ALG_SHA2 BIT(28)
225#define EIP197_ALG_HMAC_SHA2 BIT(29)
226#define EIP197_ALG_AES_XCBC_MAC BIT(30)
227#define EIP197_ALG_GCM_HASH BIT(31)
228
229/* EIP197_PE_EIP96_CONTEXT_CTRL */
230#define EIP197_CONTEXT_SIZE(n) (n)
231#define EIP197_ADDRESS_MODE BIT(8)
232#define EIP197_CONTROL_MODE BIT(9)
233
234/* Context Control */
235struct safexcel_context_record {
236 u32 control0;
237 u32 control1;
238
239 __le32 data[12];
240} __packed;
241
242/* control0 */
243#define CONTEXT_CONTROL_TYPE_NULL_OUT 0x0
244#define CONTEXT_CONTROL_TYPE_NULL_IN 0x1
245#define CONTEXT_CONTROL_TYPE_HASH_OUT 0x2
246#define CONTEXT_CONTROL_TYPE_HASH_IN 0x3
247#define CONTEXT_CONTROL_TYPE_CRYPTO_OUT 0x4
248#define CONTEXT_CONTROL_TYPE_CRYPTO_IN 0x5
249#define CONTEXT_CONTROL_TYPE_ENCRYPT_HASH_OUT 0x6
250#define CONTEXT_CONTROL_TYPE_DECRYPT_HASH_IN 0x7
251#define CONTEXT_CONTROL_TYPE_HASH_ENCRYPT_OUT 0x14
252#define CONTEXT_CONTROL_TYPE_HASH_DECRYPT_OUT 0x15
253#define CONTEXT_CONTROL_RESTART_HASH BIT(4)
254#define CONTEXT_CONTROL_NO_FINISH_HASH BIT(5)
255#define CONTEXT_CONTROL_SIZE(n) ((n) << 8)
256#define CONTEXT_CONTROL_KEY_EN BIT(16)
257#define CONTEXT_CONTROL_CRYPTO_ALG_AES128 (0x5 << 17)
258#define CONTEXT_CONTROL_CRYPTO_ALG_AES192 (0x6 << 17)
259#define CONTEXT_CONTROL_CRYPTO_ALG_AES256 (0x7 << 17)
260#define CONTEXT_CONTROL_DIGEST_PRECOMPUTED (0x1 << 21)
261#define CONTEXT_CONTROL_DIGEST_HMAC (0x3 << 21)
262#define CONTEXT_CONTROL_CRYPTO_ALG_SHA1 (0x2 << 23)
263#define CONTEXT_CONTROL_CRYPTO_ALG_SHA224 (0x4 << 23)
264#define CONTEXT_CONTROL_CRYPTO_ALG_SHA256 (0x3 << 23)
265#define CONTEXT_CONTROL_INV_FR (0x5 << 24)
266#define CONTEXT_CONTROL_INV_TR (0x6 << 24)
267
268/* control1 */
269#define CONTEXT_CONTROL_CRYPTO_MODE_ECB (0 << 0)
270#define CONTEXT_CONTROL_CRYPTO_MODE_CBC (1 << 0)
271#define CONTEXT_CONTROL_IV0 BIT(5)
272#define CONTEXT_CONTROL_IV1 BIT(6)
273#define CONTEXT_CONTROL_IV2 BIT(7)
274#define CONTEXT_CONTROL_IV3 BIT(8)
275#define CONTEXT_CONTROL_DIGEST_CNT BIT(9)
276#define CONTEXT_CONTROL_COUNTER_MODE BIT(10)
277#define CONTEXT_CONTROL_HASH_STORE BIT(19)
278
279/* EIP197_CS_RAM_CTRL */
280#define EIP197_TRC_ENABLE_0 BIT(4)
281#define EIP197_TRC_ENABLE_1 BIT(5)
282#define EIP197_TRC_ENABLE_2 BIT(6)
283#define EIP197_TRC_ENABLE_MASK GENMASK(6, 4)
284
285/* EIP197_TRC_PARAMS */
286#define EIP197_TRC_PARAMS_SW_RESET BIT(0)
287#define EIP197_TRC_PARAMS_DATA_ACCESS BIT(2)
288#define EIP197_TRC_PARAMS_HTABLE_SZ(x) ((x) << 4)
289#define EIP197_TRC_PARAMS_BLK_TIMER_SPEED(x) ((x) << 10)
290#define EIP197_TRC_PARAMS_RC_SZ_LARGE(n) ((n) << 18)
291
292/* EIP197_TRC_FREECHAIN */
293#define EIP197_TRC_FREECHAIN_HEAD_PTR(p) (p)
294#define EIP197_TRC_FREECHAIN_TAIL_PTR(p) ((p) << 16)
295
296/* EIP197_TRC_PARAMS2 */
297#define EIP197_TRC_PARAMS2_HTABLE_PTR(p) (p)
298#define EIP197_TRC_PARAMS2_RC_SZ_SMALL(n) ((n) << 18)
299
300/* Cache helpers */
301#define EIP197_CS_RC_MAX 52
302#define EIP197_CS_RC_SIZE (4 * sizeof(u32))
303#define EIP197_CS_RC_NEXT(x) (x)
304#define EIP197_CS_RC_PREV(x) ((x) << 10)
305#define EIP197_RC_NULL 0x3ff
306#define EIP197_CS_TRC_REC_WC 59
307#define EIP197_CS_TRC_LG_REC_WC 73
308
309/* Result data */
310struct result_data_desc {
311 u32 packet_length:17;
312 u32 error_code:15;
313
314 u8 bypass_length:4;
315 u8 e15:1;
316 u16 rsvd0;
317 u8 hash_bytes:1;
318 u8 hash_length:6;
319 u8 generic_bytes:1;
320 u8 checksum:1;
321 u8 next_header:1;
322 u8 length:1;
323
324 u16 application_id;
325 u16 rsvd1;
326
327 u32 rsvd2;
328} __packed;
329
330
331/* Basic Result Descriptor format */
332struct safexcel_result_desc {
333 u32 particle_size:17;
334 u8 rsvd0:3;
335 u8 descriptor_overflow:1;
336 u8 buffer_overflow:1;
337 u8 last_seg:1;
338 u8 first_seg:1;
339 u16 result_size:8;
340
341 u32 rsvd1;
342
343 u32 data_lo;
344 u32 data_hi;
345
346 struct result_data_desc result_data;
347} __packed;
348
349struct safexcel_token {
350 u32 packet_length:17;
351 u8 stat:2;
352 u16 instructions:9;
353 u8 opcode:4;
354} __packed;
355
356#define EIP197_TOKEN_STAT_LAST_HASH BIT(0)
357#define EIP197_TOKEN_STAT_LAST_PACKET BIT(1)
358#define EIP197_TOKEN_OPCODE_DIRECTION 0x0
359#define EIP197_TOKEN_OPCODE_INSERT 0x2
360#define EIP197_TOKEN_OPCODE_NOOP EIP197_TOKEN_OPCODE_INSERT
361#define EIP197_TOKEN_OPCODE_BYPASS GENMASK(3, 0)
362
363static inline void eip197_noop_token(struct safexcel_token *token)
364{
365 token->opcode = EIP197_TOKEN_OPCODE_NOOP;
366 token->packet_length = BIT(2);
367}
368
369/* Instructions */
370#define EIP197_TOKEN_INS_INSERT_HASH_DIGEST 0x1c
371#define EIP197_TOKEN_INS_TYPE_OUTPUT BIT(5)
372#define EIP197_TOKEN_INS_TYPE_HASH BIT(6)
373#define EIP197_TOKEN_INS_TYPE_CRYTO BIT(7)
374#define EIP197_TOKEN_INS_LAST BIT(8)
375
376/* Processing Engine Control Data */
377struct safexcel_control_data_desc {
378 u32 packet_length:17;
379 u16 options:13;
380 u8 type:2;
381
382 u16 application_id;
383 u16 rsvd;
384
385 u8 refresh:2;
386 u32 context_lo:30;
387 u32 context_hi;
388
389 u32 control0;
390 u32 control1;
391
392 u32 token[EIP197_MAX_TOKENS];
393} __packed;
394
395#define EIP197_OPTION_MAGIC_VALUE BIT(0)
396#define EIP197_OPTION_64BIT_CTX BIT(1)
397#define EIP197_OPTION_CTX_CTRL_IN_CMD BIT(8)
398#define EIP197_OPTION_4_TOKEN_IV_CMD GENMASK(11, 9)
399
400#define EIP197_TYPE_EXTENDED 0x3
401
402/* Basic Command Descriptor format */
403struct safexcel_command_desc {
404 u32 particle_size:17;
405 u8 rsvd0:5;
406 u8 last_seg:1;
407 u8 first_seg:1;
408 u16 additional_cdata_size:8;
409
410 u32 rsvd1;
411
412 u32 data_lo;
413 u32 data_hi;
414
415 struct safexcel_control_data_desc control_data;
416} __packed;
417
418/*
419 * Internal structures & functions
420 */
421
422enum eip197_fw {
423 FW_IFPP = 0,
424 FW_IPUE,
425 FW_NB
426};
427
428struct safexcel_ring {
429 void *base;
430 void *base_end;
431 dma_addr_t base_dma;
432
433 /* write and read pointers */
434 void *write;
435 void *read;
436
437 /* number of elements used in the ring */
438 unsigned nr;
439 unsigned offset;
440};
441
442enum safexcel_alg_type {
443 SAFEXCEL_ALG_TYPE_SKCIPHER,
444 SAFEXCEL_ALG_TYPE_AHASH,
445};
446
447struct safexcel_request {
448 struct list_head list;
449 struct crypto_async_request *req;
450};
451
452struct safexcel_config {
453 u32 rings;
454
455 u32 cd_size;
456 u32 cd_offset;
457
458 u32 rd_size;
459 u32 rd_offset;
460};
461
462struct safexcel_work_data {
463 struct work_struct work;
464 struct safexcel_crypto_priv *priv;
465 int ring;
466};
467
468struct safexcel_crypto_priv {
469 void __iomem *base;
470 struct device *dev;
471 struct clk *clk;
472 struct safexcel_config config;
473
Antoine Ténart1b44c5a2017-05-24 16:10:34 +0200474 /* context DMA pool */
475 struct dma_pool *context_pool;
476
477 atomic_t ring_used;
478
479 struct {
480 spinlock_t lock;
481 spinlock_t egress_lock;
482
483 struct list_head list;
484 struct workqueue_struct *workqueue;
485 struct safexcel_work_data work_data;
486
487 /* command/result rings */
488 struct safexcel_ring cdr;
489 struct safexcel_ring rdr;
Antoine Ténart86671ab2017-06-15 09:56:24 +0200490
491 /* queue */
492 struct crypto_queue queue;
493 spinlock_t queue_lock;
Antoine Ténartdc7e28a2017-12-14 15:26:53 +0100494
495 /* Number of requests in the engine that needs the threshold
496 * interrupt to be set up.
497 */
498 int requests_left;
499
500 /* The ring is currently handling at least one request */
501 bool busy;
Antoine Ténart8732b292017-12-14 15:26:57 +0100502
503 /* Store for current requests when bailing out of the dequeueing
504 * function when no enough resources are available.
505 */
506 struct crypto_async_request *req;
507 struct crypto_async_request *backlog;
Antoine Ténart1b44c5a2017-05-24 16:10:34 +0200508 } ring[EIP197_MAX_RINGS];
509};
510
511struct safexcel_context {
512 int (*send)(struct crypto_async_request *req, int ring,
513 struct safexcel_request *request, int *commands,
514 int *results);
515 int (*handle_result)(struct safexcel_crypto_priv *priv, int ring,
516 struct crypto_async_request *req, bool *complete,
517 int *ret);
518 struct safexcel_context_record *ctxr;
519 dma_addr_t ctxr_dma;
520
521 int ring;
522 bool needs_inv;
523 bool exit_inv;
524
525 /* Used for ahash requests */
526 dma_addr_t result_dma;
527 void *cache;
528 dma_addr_t cache_dma;
529 unsigned int cache_sz;
530};
531
532/*
533 * Template structure to describe the algorithms in order to register them.
534 * It also has the purpose to contain our private structure and is actually
535 * the only way I know in this framework to avoid having global pointers...
536 */
537struct safexcel_alg_template {
538 struct safexcel_crypto_priv *priv;
539 enum safexcel_alg_type type;
540 union {
541 struct skcipher_alg skcipher;
542 struct ahash_alg ahash;
543 } alg;
544};
545
546struct safexcel_inv_result {
547 struct completion completion;
548 int error;
549};
550
Antoine Ténart86671ab2017-06-15 09:56:24 +0200551void safexcel_dequeue(struct safexcel_crypto_priv *priv, int ring);
Antoine Ténart1b44c5a2017-05-24 16:10:34 +0200552void safexcel_complete(struct safexcel_crypto_priv *priv, int ring);
553void safexcel_free_context(struct safexcel_crypto_priv *priv,
554 struct crypto_async_request *req,
555 int result_sz);
556int safexcel_invalidate_cache(struct crypto_async_request *async,
Antoine Ténart1b44c5a2017-05-24 16:10:34 +0200557 struct safexcel_crypto_priv *priv,
558 dma_addr_t ctxr_dma, int ring,
559 struct safexcel_request *request);
560int safexcel_init_ring_descriptors(struct safexcel_crypto_priv *priv,
561 struct safexcel_ring *cdr,
562 struct safexcel_ring *rdr);
563int safexcel_select_ring(struct safexcel_crypto_priv *priv);
564void *safexcel_ring_next_rptr(struct safexcel_crypto_priv *priv,
565 struct safexcel_ring *ring);
566void safexcel_ring_rollback_wptr(struct safexcel_crypto_priv *priv,
567 struct safexcel_ring *ring);
568struct safexcel_command_desc *safexcel_add_cdesc(struct safexcel_crypto_priv *priv,
569 int ring_id,
570 bool first, bool last,
571 dma_addr_t data, u32 len,
572 u32 full_data_len,
573 dma_addr_t context);
574struct safexcel_result_desc *safexcel_add_rdesc(struct safexcel_crypto_priv *priv,
575 int ring_id,
576 bool first, bool last,
577 dma_addr_t data, u32 len);
578void safexcel_inv_complete(struct crypto_async_request *req, int error);
579
580/* available algorithms */
581extern struct safexcel_alg_template safexcel_alg_ecb_aes;
582extern struct safexcel_alg_template safexcel_alg_cbc_aes;
583extern struct safexcel_alg_template safexcel_alg_sha1;
584extern struct safexcel_alg_template safexcel_alg_sha224;
585extern struct safexcel_alg_template safexcel_alg_sha256;
586extern struct safexcel_alg_template safexcel_alg_hmac_sha1;
587
588#endif