blob: f68057c7d96aaf9a68345e4c4563291075eea5e3 [file] [log] [blame]
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001#ifndef __MARVELL_CESA_H__
2#define __MARVELL_CESA_H__
3
4#include <crypto/algapi.h>
5#include <crypto/hash.h>
6#include <crypto/internal/hash.h>
7
8#include <linux/crypto.h>
9
10#define CESA_ENGINE_OFF(i) (((i) * 0x2000))
11
12#define CESA_TDMA_BYTE_CNT 0x800
13#define CESA_TDMA_SRC_ADDR 0x810
14#define CESA_TDMA_DST_ADDR 0x820
15#define CESA_TDMA_NEXT_ADDR 0x830
16
17#define CESA_TDMA_CONTROL 0x840
18#define CESA_TDMA_DST_BURST GENMASK(2, 0)
19#define CESA_TDMA_DST_BURST_32B 3
20#define CESA_TDMA_DST_BURST_128B 4
21#define CESA_TDMA_OUT_RD_EN BIT(4)
22#define CESA_TDMA_SRC_BURST GENMASK(8, 6)
23#define CESA_TDMA_SRC_BURST_32B (3 << 6)
24#define CESA_TDMA_SRC_BURST_128B (4 << 6)
25#define CESA_TDMA_CHAIN BIT(9)
26#define CESA_TDMA_BYTE_SWAP BIT(11)
27#define CESA_TDMA_NO_BYTE_SWAP BIT(11)
28#define CESA_TDMA_EN BIT(12)
29#define CESA_TDMA_FETCH_ND BIT(13)
30#define CESA_TDMA_ACT BIT(14)
31
32#define CESA_TDMA_CUR 0x870
33#define CESA_TDMA_ERROR_CAUSE 0x8c8
34#define CESA_TDMA_ERROR_MSK 0x8cc
35
36#define CESA_TDMA_WINDOW_BASE(x) (((x) * 0x8) + 0xa00)
37#define CESA_TDMA_WINDOW_CTRL(x) (((x) * 0x8) + 0xa04)
38
39#define CESA_IVDIG(x) (0xdd00 + ((x) * 4) + \
40 (((x) < 5) ? 0 : 0x14))
41
42#define CESA_SA_CMD 0xde00
43#define CESA_SA_CMD_EN_CESA_SA_ACCL0 BIT(0)
44#define CESA_SA_CMD_EN_CESA_SA_ACCL1 BIT(1)
45#define CESA_SA_CMD_DISABLE_SEC BIT(2)
46
47#define CESA_SA_DESC_P0 0xde04
48
49#define CESA_SA_DESC_P1 0xde14
50
51#define CESA_SA_CFG 0xde08
52#define CESA_SA_CFG_STOP_DIG_ERR GENMASK(1, 0)
53#define CESA_SA_CFG_DIG_ERR_CONT 0
54#define CESA_SA_CFG_DIG_ERR_SKIP 1
55#define CESA_SA_CFG_DIG_ERR_STOP 3
56#define CESA_SA_CFG_CH0_W_IDMA BIT(7)
57#define CESA_SA_CFG_CH1_W_IDMA BIT(8)
58#define CESA_SA_CFG_ACT_CH0_IDMA BIT(9)
59#define CESA_SA_CFG_ACT_CH1_IDMA BIT(10)
60#define CESA_SA_CFG_MULTI_PKT BIT(11)
61#define CESA_SA_CFG_PARA_DIS BIT(13)
62
63#define CESA_SA_ACCEL_STATUS 0xde0c
64#define CESA_SA_ST_ACT_0 BIT(0)
65#define CESA_SA_ST_ACT_1 BIT(1)
66
67/*
68 * CESA_SA_FPGA_INT_STATUS looks like a FPGA leftover and is documented only
69 * in Errata 4.12. It looks like that it was part of an IRQ-controller in FPGA
70 * and someone forgot to remove it while switching to the core and moving to
71 * CESA_SA_INT_STATUS.
72 */
73#define CESA_SA_FPGA_INT_STATUS 0xdd68
74#define CESA_SA_INT_STATUS 0xde20
75#define CESA_SA_INT_AUTH_DONE BIT(0)
76#define CESA_SA_INT_DES_E_DONE BIT(1)
77#define CESA_SA_INT_AES_E_DONE BIT(2)
78#define CESA_SA_INT_AES_D_DONE BIT(3)
79#define CESA_SA_INT_ENC_DONE BIT(4)
80#define CESA_SA_INT_ACCEL0_DONE BIT(5)
81#define CESA_SA_INT_ACCEL1_DONE BIT(6)
82#define CESA_SA_INT_ACC0_IDMA_DONE BIT(7)
83#define CESA_SA_INT_ACC1_IDMA_DONE BIT(8)
84#define CESA_SA_INT_IDMA_DONE BIT(9)
85#define CESA_SA_INT_IDMA_OWN_ERR BIT(10)
86
87#define CESA_SA_INT_MSK 0xde24
88
89#define CESA_SA_DESC_CFG_OP_MAC_ONLY 0
90#define CESA_SA_DESC_CFG_OP_CRYPT_ONLY 1
91#define CESA_SA_DESC_CFG_OP_MAC_CRYPT 2
92#define CESA_SA_DESC_CFG_OP_CRYPT_MAC 3
93#define CESA_SA_DESC_CFG_OP_MSK GENMASK(1, 0)
94#define CESA_SA_DESC_CFG_MACM_SHA256 (1 << 4)
95#define CESA_SA_DESC_CFG_MACM_HMAC_SHA256 (3 << 4)
96#define CESA_SA_DESC_CFG_MACM_MD5 (4 << 4)
97#define CESA_SA_DESC_CFG_MACM_SHA1 (5 << 4)
98#define CESA_SA_DESC_CFG_MACM_HMAC_MD5 (6 << 4)
99#define CESA_SA_DESC_CFG_MACM_HMAC_SHA1 (7 << 4)
100#define CESA_SA_DESC_CFG_MACM_MSK GENMASK(6, 4)
101#define CESA_SA_DESC_CFG_CRYPTM_DES (1 << 8)
102#define CESA_SA_DESC_CFG_CRYPTM_3DES (2 << 8)
103#define CESA_SA_DESC_CFG_CRYPTM_AES (3 << 8)
104#define CESA_SA_DESC_CFG_CRYPTM_MSK GENMASK(9, 8)
105#define CESA_SA_DESC_CFG_DIR_ENC (0 << 12)
106#define CESA_SA_DESC_CFG_DIR_DEC (1 << 12)
107#define CESA_SA_DESC_CFG_CRYPTCM_ECB (0 << 16)
108#define CESA_SA_DESC_CFG_CRYPTCM_CBC (1 << 16)
109#define CESA_SA_DESC_CFG_CRYPTCM_MSK BIT(16)
110#define CESA_SA_DESC_CFG_3DES_EEE (0 << 20)
111#define CESA_SA_DESC_CFG_3DES_EDE (1 << 20)
112#define CESA_SA_DESC_CFG_AES_LEN_128 (0 << 24)
113#define CESA_SA_DESC_CFG_AES_LEN_192 (1 << 24)
114#define CESA_SA_DESC_CFG_AES_LEN_256 (2 << 24)
115#define CESA_SA_DESC_CFG_AES_LEN_MSK GENMASK(25, 24)
116#define CESA_SA_DESC_CFG_NOT_FRAG (0 << 30)
117#define CESA_SA_DESC_CFG_FIRST_FRAG (1 << 30)
118#define CESA_SA_DESC_CFG_LAST_FRAG (2 << 30)
119#define CESA_SA_DESC_CFG_MID_FRAG (3 << 30)
120#define CESA_SA_DESC_CFG_FRAG_MSK GENMASK(31, 30)
121
122/*
123 * /-----------\ 0
124 * | ACCEL CFG | 4 * 8
125 * |-----------| 0x20
126 * | CRYPT KEY | 8 * 4
127 * |-----------| 0x40
128 * | IV IN | 4 * 4
129 * |-----------| 0x40 (inplace)
130 * | IV BUF | 4 * 4
131 * |-----------| 0x80
132 * | DATA IN | 16 * x (max ->max_req_size)
133 * |-----------| 0x80 (inplace operation)
134 * | DATA OUT | 16 * x (max ->max_req_size)
135 * \-----------/ SRAM size
136 */
137
138/*
139 * Hashing memory map:
140 * /-----------\ 0
141 * | ACCEL CFG | 4 * 8
142 * |-----------| 0x20
143 * | Inner IV | 8 * 4
144 * |-----------| 0x40
145 * | Outer IV | 8 * 4
146 * |-----------| 0x60
147 * | Output BUF| 8 * 4
148 * |-----------| 0x80
149 * | DATA IN | 64 * x (max ->max_req_size)
150 * \-----------/ SRAM size
151 */
152
153#define CESA_SA_CFG_SRAM_OFFSET 0x00
154#define CESA_SA_DATA_SRAM_OFFSET 0x80
155
156#define CESA_SA_CRYPT_KEY_SRAM_OFFSET 0x20
157#define CESA_SA_CRYPT_IV_SRAM_OFFSET 0x40
158
159#define CESA_SA_MAC_IIV_SRAM_OFFSET 0x20
160#define CESA_SA_MAC_OIV_SRAM_OFFSET 0x40
161#define CESA_SA_MAC_DIG_SRAM_OFFSET 0x60
162
163#define CESA_SA_DESC_CRYPT_DATA(offset) \
164 cpu_to_le32((CESA_SA_DATA_SRAM_OFFSET + (offset)) | \
165 ((CESA_SA_DATA_SRAM_OFFSET + (offset)) << 16))
166
167#define CESA_SA_DESC_CRYPT_IV(offset) \
168 cpu_to_le32((CESA_SA_CRYPT_IV_SRAM_OFFSET + (offset)) | \
169 ((CESA_SA_CRYPT_IV_SRAM_OFFSET + (offset)) << 16))
170
171#define CESA_SA_DESC_CRYPT_KEY(offset) \
172 cpu_to_le32(CESA_SA_CRYPT_KEY_SRAM_OFFSET + (offset))
173
174#define CESA_SA_DESC_MAC_DATA(offset) \
175 cpu_to_le32(CESA_SA_DATA_SRAM_OFFSET + (offset))
176#define CESA_SA_DESC_MAC_DATA_MSK GENMASK(15, 0)
177
178#define CESA_SA_DESC_MAC_TOTAL_LEN(total_len) cpu_to_le32((total_len) << 16)
179#define CESA_SA_DESC_MAC_TOTAL_LEN_MSK GENMASK(31, 16)
180
181#define CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX 0xffff
182
183#define CESA_SA_DESC_MAC_DIGEST(offset) \
184 cpu_to_le32(CESA_SA_MAC_DIG_SRAM_OFFSET + (offset))
185#define CESA_SA_DESC_MAC_DIGEST_MSK GENMASK(15, 0)
186
187#define CESA_SA_DESC_MAC_FRAG_LEN(frag_len) cpu_to_le32((frag_len) << 16)
188#define CESA_SA_DESC_MAC_FRAG_LEN_MSK GENMASK(31, 16)
189
190#define CESA_SA_DESC_MAC_IV(offset) \
191 cpu_to_le32((CESA_SA_MAC_IIV_SRAM_OFFSET + (offset)) | \
192 ((CESA_SA_MAC_OIV_SRAM_OFFSET + (offset)) << 16))
193
194#define CESA_SA_SRAM_SIZE 2048
195#define CESA_SA_SRAM_PAYLOAD_SIZE (cesa_dev->sram_size - \
196 CESA_SA_DATA_SRAM_OFFSET)
197
198#define CESA_SA_DEFAULT_SRAM_SIZE 2048
199#define CESA_SA_MIN_SRAM_SIZE 1024
200
201#define CESA_SA_SRAM_MSK (2048 - 1)
202
203#define CESA_MAX_HASH_BLOCK_SIZE 64
204#define CESA_HASH_BLOCK_SIZE_MSK (CESA_MAX_HASH_BLOCK_SIZE - 1)
205
206/**
207 * struct mv_cesa_sec_accel_desc - security accelerator descriptor
208 * @config: engine config
209 * @enc_p: input and output data pointers for a cipher operation
210 * @enc_len: cipher operation length
211 * @enc_key_p: cipher key pointer
212 * @enc_iv: cipher IV pointers
213 * @mac_src_p: input pointer and total hash length
214 * @mac_digest: digest pointer and hash operation length
215 * @mac_iv: hmac IV pointers
216 *
217 * Structure passed to the CESA engine to describe the crypto operation
218 * to be executed.
219 */
220struct mv_cesa_sec_accel_desc {
221 u32 config;
222 u32 enc_p;
223 u32 enc_len;
224 u32 enc_key_p;
225 u32 enc_iv;
226 u32 mac_src_p;
227 u32 mac_digest;
228 u32 mac_iv;
229};
230
231/**
232 * struct mv_cesa_blkcipher_op_ctx - cipher operation context
233 * @key: cipher key
234 * @iv: cipher IV
235 *
236 * Context associated to a cipher operation.
237 */
238struct mv_cesa_blkcipher_op_ctx {
239 u32 key[8];
240 u32 iv[4];
241};
242
243/**
244 * struct mv_cesa_hash_op_ctx - hash or hmac operation context
245 * @key: cipher key
246 * @iv: cipher IV
247 *
248 * Context associated to an hash or hmac operation.
249 */
250struct mv_cesa_hash_op_ctx {
251 u32 iv[16];
252 u32 hash[8];
253};
254
255/**
256 * struct mv_cesa_op_ctx - crypto operation context
257 * @desc: CESA descriptor
258 * @ctx: context associated to the crypto operation
259 *
260 * Context associated to a crypto operation.
261 */
262struct mv_cesa_op_ctx {
263 struct mv_cesa_sec_accel_desc desc;
264 union {
265 struct mv_cesa_blkcipher_op_ctx blkcipher;
266 struct mv_cesa_hash_op_ctx hash;
267 } ctx;
268};
269
270struct mv_cesa_engine;
271
272/**
273 * struct mv_cesa_caps - CESA device capabilities
274 * @engines: number of engines
275 * @cipher_algs: supported cipher algorithms
276 * @ncipher_algs: number of supported cipher algorithms
277 * @ahash_algs: supported hash algorithms
278 * @nahash_algs: number of supported hash algorithms
279 *
280 * Structure used to describe CESA device capabilities.
281 */
282struct mv_cesa_caps {
283 int nengines;
284 struct crypto_alg **cipher_algs;
285 int ncipher_algs;
286 struct ahash_alg **ahash_algs;
287 int nahash_algs;
288};
289
290/**
291 * struct mv_cesa_dev - CESA device
292 * @caps: device capabilities
293 * @regs: device registers
294 * @sram_size: usable SRAM size
295 * @lock: device lock
296 * @queue: crypto request queue
297 * @engines: array of engines
298 *
299 * Structure storing CESA device information.
300 */
301struct mv_cesa_dev {
302 const struct mv_cesa_caps *caps;
303 void __iomem *regs;
304 struct device *dev;
305 unsigned int sram_size;
306 spinlock_t lock;
307 struct crypto_queue queue;
308 struct mv_cesa_engine *engines;
309};
310
311/**
312 * struct mv_cesa_engine - CESA engine
313 * @id: engine id
314 * @regs: engine registers
315 * @sram: SRAM memory region
316 * @sram_dma: DMA address of the SRAM memory region
317 * @lock: engine lock
318 * @req: current crypto request
319 * @clk: engine clk
320 * @zclk: engine zclk
321 * @max_req_len: maximum chunk length (useful to create the TDMA chain)
322 * @int_mask: interrupt mask cache
323 * @pool: memory pool pointing to the memory region reserved in
324 * SRAM
325 *
326 * Structure storing CESA engine information.
327 */
328struct mv_cesa_engine {
329 int id;
330 void __iomem *regs;
331 void __iomem *sram;
332 dma_addr_t sram_dma;
333 spinlock_t lock;
334 struct crypto_async_request *req;
335 struct clk *clk;
336 struct clk *zclk;
337 size_t max_req_len;
338 u32 int_mask;
339 struct gen_pool *pool;
340};
341
342/**
343 * struct mv_cesa_req_ops - CESA request operations
344 * @prepare: prepare a request to be executed on the specified engine
345 * @process: process a request chunk result (should return 0 if the
346 * operation, -EINPROGRESS if it needs more steps or an error
347 * code)
348 * @step: launch the crypto operation on the next chunk
349 * @cleanup: cleanup the crypto request (release associated data)
350 */
351struct mv_cesa_req_ops {
352 void (*prepare)(struct crypto_async_request *req,
353 struct mv_cesa_engine *engine);
354 int (*process)(struct crypto_async_request *req, u32 status);
355 void (*step)(struct crypto_async_request *req);
356 void (*cleanup)(struct crypto_async_request *req);
357};
358
359/**
360 * struct mv_cesa_ctx - CESA operation context
361 * @ops: crypto operations
362 *
363 * Base context structure inherited by operation specific ones.
364 */
365struct mv_cesa_ctx {
366 const struct mv_cesa_req_ops *ops;
367};
368
369/**
370 * struct mv_cesa_hash_ctx - CESA hash operation context
371 * @base: base context structure
372 *
373 * Hash context structure.
374 */
375struct mv_cesa_hash_ctx {
376 struct mv_cesa_ctx base;
377};
378
379/**
380 * struct mv_cesa_hash_ctx - CESA hmac operation context
381 * @base: base context structure
382 * @iv: initialization vectors
383 *
384 * HMAC context structure.
385 */
386struct mv_cesa_hmac_ctx {
387 struct mv_cesa_ctx base;
388 u32 iv[16];
389};
390
391/**
392 * enum mv_cesa_req_type - request type definitions
393 * @CESA_STD_REQ: standard request
394 */
395enum mv_cesa_req_type {
396 CESA_STD_REQ,
397};
398
399/**
400 * struct mv_cesa_req - CESA request
401 * @type: request type
402 * @engine: engine associated with this request
403 */
404struct mv_cesa_req {
405 enum mv_cesa_req_type type;
406 struct mv_cesa_engine *engine;
407};
408
409/**
410 * struct mv_cesa_ablkcipher_std_req - cipher standard request
411 * @base: base information
412 * @op: operation context
413 * @offset: current operation offset
414 * @size: size of the crypto operation
415 */
416struct mv_cesa_ablkcipher_std_req {
417 struct mv_cesa_req base;
418 struct mv_cesa_op_ctx op;
419 unsigned int offset;
420 unsigned int size;
421 bool skip_ctx;
422};
423
424/**
425 * struct mv_cesa_ablkcipher_req - cipher request
426 * @req: type specific request information
427 * @src_nents: number of entries in the src sg list
428 * @dst_nents: number of entries in the dest sg list
429 */
430struct mv_cesa_ablkcipher_req {
431 union {
432 struct mv_cesa_req base;
433 struct mv_cesa_ablkcipher_std_req std;
434 } req;
435 int src_nents;
436 int dst_nents;
437};
438
439/**
440 * struct mv_cesa_ahash_std_req - standard hash request
441 * @base: base information
442 * @offset: current operation offset
443 */
444struct mv_cesa_ahash_std_req {
445 struct mv_cesa_req base;
446 unsigned int offset;
447};
448
449/**
450 * struct mv_cesa_ahash_req - hash request
451 * @req: type specific request information
452 * @cache: cache buffer
453 * @cache_ptr: write pointer in the cache buffer
454 * @len: hash total length
455 * @src_nents: number of entries in the scatterlist
456 * @last_req: define whether the current operation is the last one
457 * or not
458 * @state: hash state
459 */
460struct mv_cesa_ahash_req {
461 union {
462 struct mv_cesa_req base;
463 struct mv_cesa_ahash_std_req std;
464 } req;
465 struct mv_cesa_op_ctx op_tmpl;
466 u8 *cache;
467 unsigned int cache_ptr;
468 u64 len;
469 int src_nents;
470 bool last_req;
471 __be32 state[8];
472};
473
474/* CESA functions */
475
476extern struct mv_cesa_dev *cesa_dev;
477
478static inline void mv_cesa_update_op_cfg(struct mv_cesa_op_ctx *op,
479 u32 cfg, u32 mask)
480{
481 op->desc.config &= cpu_to_le32(~mask);
482 op->desc.config |= cpu_to_le32(cfg);
483}
484
485static inline u32 mv_cesa_get_op_cfg(struct mv_cesa_op_ctx *op)
486{
487 return le32_to_cpu(op->desc.config);
488}
489
490static inline void mv_cesa_set_op_cfg(struct mv_cesa_op_ctx *op, u32 cfg)
491{
492 op->desc.config = cpu_to_le32(cfg);
493}
494
495static inline void mv_cesa_adjust_op(struct mv_cesa_engine *engine,
496 struct mv_cesa_op_ctx *op)
497{
498 u32 offset = engine->sram_dma & CESA_SA_SRAM_MSK;
499
500 op->desc.enc_p = CESA_SA_DESC_CRYPT_DATA(offset);
501 op->desc.enc_key_p = CESA_SA_DESC_CRYPT_KEY(offset);
502 op->desc.enc_iv = CESA_SA_DESC_CRYPT_IV(offset);
503 op->desc.mac_src_p &= ~CESA_SA_DESC_MAC_DATA_MSK;
504 op->desc.mac_src_p |= CESA_SA_DESC_MAC_DATA(offset);
505 op->desc.mac_digest &= ~CESA_SA_DESC_MAC_DIGEST_MSK;
506 op->desc.mac_digest |= CESA_SA_DESC_MAC_DIGEST(offset);
507 op->desc.mac_iv = CESA_SA_DESC_MAC_IV(offset);
508}
509
510static inline void mv_cesa_set_crypt_op_len(struct mv_cesa_op_ctx *op, int len)
511{
512 op->desc.enc_len = cpu_to_le32(len);
513}
514
515static inline void mv_cesa_set_mac_op_total_len(struct mv_cesa_op_ctx *op,
516 int len)
517{
518 op->desc.mac_src_p &= ~CESA_SA_DESC_MAC_TOTAL_LEN_MSK;
519 op->desc.mac_src_p |= CESA_SA_DESC_MAC_TOTAL_LEN(len);
520}
521
522static inline void mv_cesa_set_mac_op_frag_len(struct mv_cesa_op_ctx *op,
523 int len)
524{
525 op->desc.mac_digest &= ~CESA_SA_DESC_MAC_FRAG_LEN_MSK;
526 op->desc.mac_digest |= CESA_SA_DESC_MAC_FRAG_LEN(len);
527}
528
529static inline void mv_cesa_set_int_mask(struct mv_cesa_engine *engine,
530 u32 int_mask)
531{
532 if (int_mask == engine->int_mask)
533 return;
534
535 writel(int_mask, engine->regs + CESA_SA_INT_MSK);
536 engine->int_mask = int_mask;
537}
538
539static inline u32 mv_cesa_get_int_mask(struct mv_cesa_engine *engine)
540{
541 return engine->int_mask;
542}
543
544int mv_cesa_queue_req(struct crypto_async_request *req);
545
546/* Algorithm definitions */
547
548extern struct ahash_alg mv_sha1_alg;
549extern struct ahash_alg mv_ahmac_sha1_alg;
550
551extern struct crypto_alg mv_cesa_ecb_aes_alg;
552extern struct crypto_alg mv_cesa_cbc_aes_alg;
553
554#endif /* __MARVELL_CESA_H__ */