2 * caam - Freescale FSL CAAM support for ahash functions of crypto API
4 * Copyright 2011 Freescale Semiconductor, Inc.
6 * Based on caamalg.c crypto API driver.
8 * relationship of digest job descriptor or first job descriptor after init to
11 * --------------- ---------------
12 * | JobDesc #1 |-------------------->| ShareDesc |
13 * | *(packet 1) | | (hashKey) |
14 * --------------- | (operation) |
17 * relationship of subsequent job descriptors to shared descriptors:
19 * --------------- ---------------
20 * | JobDesc #2 |-------------------->| ShareDesc |
21 * | *(packet 2) | |------------->| (hashKey) |
22 * --------------- | |-------->| (operation) |
23 * . | | | (load ctx2) |
24 * . | | ---------------
26 * | JobDesc #3 |------| |
32 * | JobDesc #4 |------------
36 * The SharedDesc never changes for a connection unless rekeyed, but
37 * each packet will likely be in a different place. So all we need
38 * to know to process the packet is where the input is, where the
39 * output goes, and what context we want to process with. Context is
40 * in the SharedDesc, packet references in the JobDesc.
42 * So, a job desc looks like:
44 * ---------------------
46 * | ShareDesc Pointer |
53 * ---------------------
60 #include "desc_constr.h"
63 #include "sg_sw_sec4.h"
66 #define CAAM_CRA_PRIORITY 3000
68 /* max hash key is max split key size */
69 #define CAAM_MAX_HASH_KEY_SIZE (SHA512_DIGEST_SIZE * 2)
71 #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE
72 #define CAAM_MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE
74 /* length of descriptors text */
75 #define DESC_AHASH_BASE (4 * CAAM_CMD_SZ)
76 #define DESC_AHASH_UPDATE_LEN (6 * CAAM_CMD_SZ)
77 #define DESC_AHASH_UPDATE_FIRST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ)
78 #define DESC_AHASH_FINAL_LEN (DESC_AHASH_BASE + 5 * CAAM_CMD_SZ)
79 #define DESC_AHASH_FINUP_LEN (DESC_AHASH_BASE + 5 * CAAM_CMD_SZ)
80 #define DESC_AHASH_DIGEST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ)
82 #define DESC_HASH_MAX_USED_BYTES (DESC_AHASH_FINAL_LEN + \
83 CAAM_MAX_HASH_KEY_SIZE)
84 #define DESC_HASH_MAX_USED_LEN (DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)
86 /* caam context sizes for hashes: running digest + 8 */
87 #define HASH_MSG_LEN 8
88 #define MAX_CTX_LEN (HASH_MSG_LEN + SHA512_DIGEST_SIZE)
91 /* for print_hex_dumps with line references */
92 #define debug(format, arg...) printk(format, arg)
94 #define debug(format, arg...)
98 static struct list_head hash_list
;
100 /* ahash per-session context */
101 struct caam_hash_ctx
{
102 struct device
*jrdev
;
103 u32 sh_desc_update
[DESC_HASH_MAX_USED_LEN
];
104 u32 sh_desc_update_first
[DESC_HASH_MAX_USED_LEN
];
105 u32 sh_desc_fin
[DESC_HASH_MAX_USED_LEN
];
106 u32 sh_desc_digest
[DESC_HASH_MAX_USED_LEN
];
107 u32 sh_desc_finup
[DESC_HASH_MAX_USED_LEN
];
108 dma_addr_t sh_desc_update_dma
;
109 dma_addr_t sh_desc_update_first_dma
;
110 dma_addr_t sh_desc_fin_dma
;
111 dma_addr_t sh_desc_digest_dma
;
112 dma_addr_t sh_desc_finup_dma
;
115 u8 key
[CAAM_MAX_HASH_KEY_SIZE
];
118 unsigned int split_key_len
;
119 unsigned int split_key_pad_len
;
123 struct caam_hash_state
{
126 u8 buf_0
[CAAM_MAX_HASH_BLOCK_SIZE
] ____cacheline_aligned
;
128 u8 buf_1
[CAAM_MAX_HASH_BLOCK_SIZE
] ____cacheline_aligned
;
130 u8 caam_ctx
[MAX_CTX_LEN
] ____cacheline_aligned
;
131 int (*update
)(struct ahash_request
*req
);
132 int (*final
)(struct ahash_request
*req
);
133 int (*finup
)(struct ahash_request
*req
);
137 struct caam_export_state
{
138 u8 buf
[CAAM_MAX_HASH_BLOCK_SIZE
];
139 u8 caam_ctx
[MAX_CTX_LEN
];
141 int (*update
)(struct ahash_request
*req
);
142 int (*final
)(struct ahash_request
*req
);
143 int (*finup
)(struct ahash_request
*req
);
146 /* Common job descriptor seq in/out ptr routines */
148 /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
149 static inline int map_seq_out_ptr_ctx(u32
*desc
, struct device
*jrdev
,
150 struct caam_hash_state
*state
,
153 state
->ctx_dma
= dma_map_single(jrdev
, state
->caam_ctx
,
154 ctx_len
, DMA_FROM_DEVICE
);
155 if (dma_mapping_error(jrdev
, state
->ctx_dma
)) {
156 dev_err(jrdev
, "unable to map ctx\n");
160 append_seq_out_ptr(desc
, state
->ctx_dma
, ctx_len
, 0);
165 /* Map req->result, and append seq_out_ptr command that points to it */
166 static inline dma_addr_t
map_seq_out_ptr_result(u32
*desc
, struct device
*jrdev
,
167 u8
*result
, int digestsize
)
171 dst_dma
= dma_map_single(jrdev
, result
, digestsize
, DMA_FROM_DEVICE
);
172 append_seq_out_ptr(desc
, dst_dma
, digestsize
, 0);
177 /* Map current buffer in state and put it in link table */
178 static inline dma_addr_t
buf_map_to_sec4_sg(struct device
*jrdev
,
179 struct sec4_sg_entry
*sec4_sg
,
184 buf_dma
= dma_map_single(jrdev
, buf
, buflen
, DMA_TO_DEVICE
);
185 dma_to_sec4_sg_one(sec4_sg
, buf_dma
, buflen
, 0);
190 /* Map req->src and put it in link table */
191 static inline void src_map_to_sec4_sg(struct device
*jrdev
,
192 struct scatterlist
*src
, int src_nents
,
193 struct sec4_sg_entry
*sec4_sg
)
195 dma_map_sg(jrdev
, src
, src_nents
, DMA_TO_DEVICE
);
196 sg_to_sec4_sg_last(src
, src_nents
, sec4_sg
, 0);
200 * Only put buffer in link table if it contains data, which is possible,
201 * since a buffer has previously been used, and needs to be unmapped,
203 static inline dma_addr_t
204 try_buf_map_to_sec4_sg(struct device
*jrdev
, struct sec4_sg_entry
*sec4_sg
,
205 u8
*buf
, dma_addr_t buf_dma
, int buflen
,
208 if (buf_dma
&& !dma_mapping_error(jrdev
, buf_dma
))
209 dma_unmap_single(jrdev
, buf_dma
, last_buflen
, DMA_TO_DEVICE
);
211 buf_dma
= buf_map_to_sec4_sg(jrdev
, sec4_sg
, buf
, buflen
);
218 /* Map state->caam_ctx, and add it to link table */
219 static inline int ctx_map_to_sec4_sg(u32
*desc
, struct device
*jrdev
,
220 struct caam_hash_state
*state
, int ctx_len
,
221 struct sec4_sg_entry
*sec4_sg
, u32 flag
)
223 state
->ctx_dma
= dma_map_single(jrdev
, state
->caam_ctx
, ctx_len
, flag
);
224 if (dma_mapping_error(jrdev
, state
->ctx_dma
)) {
225 dev_err(jrdev
, "unable to map ctx\n");
229 dma_to_sec4_sg_one(sec4_sg
, state
->ctx_dma
, ctx_len
, 0);
234 /* Common shared descriptor commands */
235 static inline void append_key_ahash(u32
*desc
, struct caam_hash_ctx
*ctx
)
237 append_key_as_imm(desc
, ctx
->key
, ctx
->split_key_pad_len
,
238 ctx
->split_key_len
, CLASS_2
|
239 KEY_DEST_MDHA_SPLIT
| KEY_ENC
);
242 /* Append key if it has been set */
243 static inline void init_sh_desc_key_ahash(u32
*desc
, struct caam_hash_ctx
*ctx
)
247 init_sh_desc(desc
, HDR_SHARE_SERIAL
);
249 if (ctx
->split_key_len
) {
250 /* Skip if already shared */
251 key_jump_cmd
= append_jump(desc
, JUMP_JSL
| JUMP_TEST_ALL
|
254 append_key_ahash(desc
, ctx
);
256 set_jump_tgt_here(desc
, key_jump_cmd
);
259 /* Propagate errors from shared to job descriptor */
260 append_cmd(desc
, SET_OK_NO_PROP_ERRORS
| CMD_LOAD
);
264 * For ahash read data from seqin following state->caam_ctx,
265 * and write resulting class2 context to seqout, which may be state->caam_ctx
268 static inline void ahash_append_load_str(u32
*desc
, int digestsize
)
270 /* Calculate remaining bytes to read */
271 append_math_add(desc
, VARSEQINLEN
, SEQINLEN
, REG0
, CAAM_CMD_SZ
);
273 /* Read remaining bytes */
274 append_seq_fifo_load(desc
, 0, FIFOLD_CLASS_CLASS2
| FIFOLD_TYPE_LAST2
|
275 FIFOLD_TYPE_MSG
| KEY_VLF
);
277 /* Store class2 context bytes */
278 append_seq_store(desc
, digestsize
, LDST_CLASS_2_CCB
|
279 LDST_SRCDST_BYTE_CONTEXT
);
283 * For ahash update, final and finup, import context, read and write to seqout
285 static inline void ahash_ctx_data_to_out(u32
*desc
, u32 op
, u32 state
,
287 struct caam_hash_ctx
*ctx
)
289 init_sh_desc_key_ahash(desc
, ctx
);
291 /* Import context from software */
292 append_cmd(desc
, CMD_SEQ_LOAD
| LDST_SRCDST_BYTE_CONTEXT
|
293 LDST_CLASS_2_CCB
| ctx
->ctx_len
);
295 /* Class 2 operation */
296 append_operation(desc
, op
| state
| OP_ALG_ENCRYPT
);
299 * Load from buf and/or src and write to req->result or state->context
301 ahash_append_load_str(desc
, digestsize
);
304 /* For ahash firsts and digest, read and write to seqout */
305 static inline void ahash_data_to_out(u32
*desc
, u32 op
, u32 state
,
306 int digestsize
, struct caam_hash_ctx
*ctx
)
308 init_sh_desc_key_ahash(desc
, ctx
);
310 /* Class 2 operation */
311 append_operation(desc
, op
| state
| OP_ALG_ENCRYPT
);
314 * Load from buf and/or src and write to req->result or state->context
316 ahash_append_load_str(desc
, digestsize
);
319 static int ahash_set_sh_desc(struct crypto_ahash
*ahash
)
321 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
322 int digestsize
= crypto_ahash_digestsize(ahash
);
323 struct device
*jrdev
= ctx
->jrdev
;
327 if (ctx
->split_key_len
)
328 have_key
= OP_ALG_AAI_HMAC_PRECOMP
;
330 /* ahash_update shared descriptor */
331 desc
= ctx
->sh_desc_update
;
333 init_sh_desc(desc
, HDR_SHARE_SERIAL
);
335 /* Import context from software */
336 append_cmd(desc
, CMD_SEQ_LOAD
| LDST_SRCDST_BYTE_CONTEXT
|
337 LDST_CLASS_2_CCB
| ctx
->ctx_len
);
339 /* Class 2 operation */
340 append_operation(desc
, ctx
->alg_type
| OP_ALG_AS_UPDATE
|
343 /* Load data and write to result or context */
344 ahash_append_load_str(desc
, ctx
->ctx_len
);
346 ctx
->sh_desc_update_dma
= dma_map_single(jrdev
, desc
, desc_bytes(desc
),
348 if (dma_mapping_error(jrdev
, ctx
->sh_desc_update_dma
)) {
349 dev_err(jrdev
, "unable to map shared descriptor\n");
353 print_hex_dump(KERN_ERR
,
354 "ahash update shdesc@"__stringify(__LINE__
)": ",
355 DUMP_PREFIX_ADDRESS
, 16, 4, desc
, desc_bytes(desc
), 1);
358 /* ahash_update_first shared descriptor */
359 desc
= ctx
->sh_desc_update_first
;
361 ahash_data_to_out(desc
, have_key
| ctx
->alg_type
, OP_ALG_AS_INIT
,
364 ctx
->sh_desc_update_first_dma
= dma_map_single(jrdev
, desc
,
367 if (dma_mapping_error(jrdev
, ctx
->sh_desc_update_first_dma
)) {
368 dev_err(jrdev
, "unable to map shared descriptor\n");
372 print_hex_dump(KERN_ERR
,
373 "ahash update first shdesc@"__stringify(__LINE__
)": ",
374 DUMP_PREFIX_ADDRESS
, 16, 4, desc
, desc_bytes(desc
), 1);
377 /* ahash_final shared descriptor */
378 desc
= ctx
->sh_desc_fin
;
380 ahash_ctx_data_to_out(desc
, have_key
| ctx
->alg_type
,
381 OP_ALG_AS_FINALIZE
, digestsize
, ctx
);
383 ctx
->sh_desc_fin_dma
= dma_map_single(jrdev
, desc
, desc_bytes(desc
),
385 if (dma_mapping_error(jrdev
, ctx
->sh_desc_fin_dma
)) {
386 dev_err(jrdev
, "unable to map shared descriptor\n");
390 print_hex_dump(KERN_ERR
, "ahash final shdesc@"__stringify(__LINE__
)": ",
391 DUMP_PREFIX_ADDRESS
, 16, 4, desc
,
392 desc_bytes(desc
), 1);
395 /* ahash_finup shared descriptor */
396 desc
= ctx
->sh_desc_finup
;
398 ahash_ctx_data_to_out(desc
, have_key
| ctx
->alg_type
,
399 OP_ALG_AS_FINALIZE
, digestsize
, ctx
);
401 ctx
->sh_desc_finup_dma
= dma_map_single(jrdev
, desc
, desc_bytes(desc
),
403 if (dma_mapping_error(jrdev
, ctx
->sh_desc_finup_dma
)) {
404 dev_err(jrdev
, "unable to map shared descriptor\n");
408 print_hex_dump(KERN_ERR
, "ahash finup shdesc@"__stringify(__LINE__
)": ",
409 DUMP_PREFIX_ADDRESS
, 16, 4, desc
,
410 desc_bytes(desc
), 1);
413 /* ahash_digest shared descriptor */
414 desc
= ctx
->sh_desc_digest
;
416 ahash_data_to_out(desc
, have_key
| ctx
->alg_type
, OP_ALG_AS_INITFINAL
,
419 ctx
->sh_desc_digest_dma
= dma_map_single(jrdev
, desc
,
422 if (dma_mapping_error(jrdev
, ctx
->sh_desc_digest_dma
)) {
423 dev_err(jrdev
, "unable to map shared descriptor\n");
427 print_hex_dump(KERN_ERR
,
428 "ahash digest shdesc@"__stringify(__LINE__
)": ",
429 DUMP_PREFIX_ADDRESS
, 16, 4, desc
,
430 desc_bytes(desc
), 1);
436 static int gen_split_hash_key(struct caam_hash_ctx
*ctx
, const u8
*key_in
,
439 return gen_split_key(ctx
->jrdev
, ctx
->key
, ctx
->split_key_len
,
440 ctx
->split_key_pad_len
, key_in
, keylen
,
444 /* Digest hash size if it is too large */
445 static int hash_digest_key(struct caam_hash_ctx
*ctx
, const u8
*key_in
,
446 u32
*keylen
, u8
*key_out
, u32 digestsize
)
448 struct device
*jrdev
= ctx
->jrdev
;
450 struct split_key_result result
;
451 dma_addr_t src_dma
, dst_dma
;
454 desc
= kmalloc(CAAM_CMD_SZ
* 8 + CAAM_PTR_SZ
* 2, GFP_KERNEL
| GFP_DMA
);
456 dev_err(jrdev
, "unable to allocate key input memory\n");
460 init_job_desc(desc
, 0);
462 src_dma
= dma_map_single(jrdev
, (void *)key_in
, *keylen
,
464 if (dma_mapping_error(jrdev
, src_dma
)) {
465 dev_err(jrdev
, "unable to map key input memory\n");
469 dst_dma
= dma_map_single(jrdev
, (void *)key_out
, digestsize
,
471 if (dma_mapping_error(jrdev
, dst_dma
)) {
472 dev_err(jrdev
, "unable to map key output memory\n");
473 dma_unmap_single(jrdev
, src_dma
, *keylen
, DMA_TO_DEVICE
);
478 /* Job descriptor to perform unkeyed hash on key_in */
479 append_operation(desc
, ctx
->alg_type
| OP_ALG_ENCRYPT
|
480 OP_ALG_AS_INITFINAL
);
481 append_seq_in_ptr(desc
, src_dma
, *keylen
, 0);
482 append_seq_fifo_load(desc
, *keylen
, FIFOLD_CLASS_CLASS2
|
483 FIFOLD_TYPE_LAST2
| FIFOLD_TYPE_MSG
);
484 append_seq_out_ptr(desc
, dst_dma
, digestsize
, 0);
485 append_seq_store(desc
, digestsize
, LDST_CLASS_2_CCB
|
486 LDST_SRCDST_BYTE_CONTEXT
);
489 print_hex_dump(KERN_ERR
, "key_in@"__stringify(__LINE__
)": ",
490 DUMP_PREFIX_ADDRESS
, 16, 4, key_in
, *keylen
, 1);
491 print_hex_dump(KERN_ERR
, "jobdesc@"__stringify(__LINE__
)": ",
492 DUMP_PREFIX_ADDRESS
, 16, 4, desc
, desc_bytes(desc
), 1);
496 init_completion(&result
.completion
);
498 ret
= caam_jr_enqueue(jrdev
, desc
, split_key_done
, &result
);
501 wait_for_completion_interruptible(&result
.completion
);
504 print_hex_dump(KERN_ERR
,
505 "digested key@"__stringify(__LINE__
)": ",
506 DUMP_PREFIX_ADDRESS
, 16, 4, key_in
,
510 dma_unmap_single(jrdev
, src_dma
, *keylen
, DMA_TO_DEVICE
);
511 dma_unmap_single(jrdev
, dst_dma
, digestsize
, DMA_FROM_DEVICE
);
513 *keylen
= digestsize
;
520 static int ahash_setkey(struct crypto_ahash
*ahash
,
521 const u8
*key
, unsigned int keylen
)
523 /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
524 static const u8 mdpadlen
[] = { 16, 20, 32, 32, 64, 64 };
525 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
526 struct device
*jrdev
= ctx
->jrdev
;
527 int blocksize
= crypto_tfm_alg_blocksize(&ahash
->base
);
528 int digestsize
= crypto_ahash_digestsize(ahash
);
530 u8
*hashed_key
= NULL
;
533 printk(KERN_ERR
"keylen %d\n", keylen
);
536 if (keylen
> blocksize
) {
537 hashed_key
= kmalloc(sizeof(u8
) * digestsize
, GFP_KERNEL
|
541 ret
= hash_digest_key(ctx
, key
, &keylen
, hashed_key
,
548 /* Pick class 2 key length from algorithm submask */
549 ctx
->split_key_len
= mdpadlen
[(ctx
->alg_op
& OP_ALG_ALGSEL_SUBMASK
) >>
550 OP_ALG_ALGSEL_SHIFT
] * 2;
551 ctx
->split_key_pad_len
= ALIGN(ctx
->split_key_len
, 16);
554 printk(KERN_ERR
"split_key_len %d split_key_pad_len %d\n",
555 ctx
->split_key_len
, ctx
->split_key_pad_len
);
556 print_hex_dump(KERN_ERR
, "key in @"__stringify(__LINE__
)": ",
557 DUMP_PREFIX_ADDRESS
, 16, 4, key
, keylen
, 1);
560 ret
= gen_split_hash_key(ctx
, key
, keylen
);
564 ctx
->key_dma
= dma_map_single(jrdev
, ctx
->key
, ctx
->split_key_pad_len
,
566 if (dma_mapping_error(jrdev
, ctx
->key_dma
)) {
567 dev_err(jrdev
, "unable to map key i/o memory\n");
572 print_hex_dump(KERN_ERR
, "ctx.key@"__stringify(__LINE__
)": ",
573 DUMP_PREFIX_ADDRESS
, 16, 4, ctx
->key
,
574 ctx
->split_key_pad_len
, 1);
577 ret
= ahash_set_sh_desc(ahash
);
579 dma_unmap_single(jrdev
, ctx
->key_dma
, ctx
->split_key_pad_len
,
588 crypto_ahash_set_flags(ahash
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
593 * ahash_edesc - s/w-extended ahash descriptor
594 * @dst_dma: physical mapped address of req->result
595 * @sec4_sg_dma: physical mapped address of h/w link table
596 * @src_nents: number of segments in input scatterlist
597 * @sec4_sg_bytes: length of dma mapped sec4_sg space
598 * @sec4_sg: pointer to h/w link table
599 * @hw_desc: the h/w job descriptor followed by any referenced link tables
603 dma_addr_t sec4_sg_dma
;
606 struct sec4_sg_entry
*sec4_sg
;
610 static inline void ahash_unmap(struct device
*dev
,
611 struct ahash_edesc
*edesc
,
612 struct ahash_request
*req
, int dst_len
)
614 if (edesc
->src_nents
)
615 dma_unmap_sg(dev
, req
->src
, edesc
->src_nents
, DMA_TO_DEVICE
);
617 dma_unmap_single(dev
, edesc
->dst_dma
, dst_len
, DMA_FROM_DEVICE
);
619 if (edesc
->sec4_sg_bytes
)
620 dma_unmap_single(dev
, edesc
->sec4_sg_dma
,
621 edesc
->sec4_sg_bytes
, DMA_TO_DEVICE
);
624 static inline void ahash_unmap_ctx(struct device
*dev
,
625 struct ahash_edesc
*edesc
,
626 struct ahash_request
*req
, int dst_len
, u32 flag
)
628 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
629 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
630 struct caam_hash_state
*state
= ahash_request_ctx(req
);
633 dma_unmap_single(dev
, state
->ctx_dma
, ctx
->ctx_len
, flag
);
634 ahash_unmap(dev
, edesc
, req
, dst_len
);
637 static void ahash_done(struct device
*jrdev
, u32
*desc
, u32 err
,
640 struct ahash_request
*req
= context
;
641 struct ahash_edesc
*edesc
;
642 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
643 int digestsize
= crypto_ahash_digestsize(ahash
);
645 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
646 struct caam_hash_state
*state
= ahash_request_ctx(req
);
648 dev_err(jrdev
, "%s %d: err 0x%x\n", __func__
, __LINE__
, err
);
651 edesc
= (struct ahash_edesc
*)((char *)desc
-
652 offsetof(struct ahash_edesc
, hw_desc
));
654 caam_jr_strstatus(jrdev
, err
);
656 ahash_unmap(jrdev
, edesc
, req
, digestsize
);
660 print_hex_dump(KERN_ERR
, "ctx@"__stringify(__LINE__
)": ",
661 DUMP_PREFIX_ADDRESS
, 16, 4, state
->caam_ctx
,
664 print_hex_dump(KERN_ERR
, "result@"__stringify(__LINE__
)": ",
665 DUMP_PREFIX_ADDRESS
, 16, 4, req
->result
,
669 req
->base
.complete(&req
->base
, err
);
672 static void ahash_done_bi(struct device
*jrdev
, u32
*desc
, u32 err
,
675 struct ahash_request
*req
= context
;
676 struct ahash_edesc
*edesc
;
677 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
678 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
680 struct caam_hash_state
*state
= ahash_request_ctx(req
);
681 int digestsize
= crypto_ahash_digestsize(ahash
);
683 dev_err(jrdev
, "%s %d: err 0x%x\n", __func__
, __LINE__
, err
);
686 edesc
= (struct ahash_edesc
*)((char *)desc
-
687 offsetof(struct ahash_edesc
, hw_desc
));
689 caam_jr_strstatus(jrdev
, err
);
691 ahash_unmap_ctx(jrdev
, edesc
, req
, ctx
->ctx_len
, DMA_BIDIRECTIONAL
);
695 print_hex_dump(KERN_ERR
, "ctx@"__stringify(__LINE__
)": ",
696 DUMP_PREFIX_ADDRESS
, 16, 4, state
->caam_ctx
,
699 print_hex_dump(KERN_ERR
, "result@"__stringify(__LINE__
)": ",
700 DUMP_PREFIX_ADDRESS
, 16, 4, req
->result
,
704 req
->base
.complete(&req
->base
, err
);
707 static void ahash_done_ctx_src(struct device
*jrdev
, u32
*desc
, u32 err
,
710 struct ahash_request
*req
= context
;
711 struct ahash_edesc
*edesc
;
712 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
713 int digestsize
= crypto_ahash_digestsize(ahash
);
715 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
716 struct caam_hash_state
*state
= ahash_request_ctx(req
);
718 dev_err(jrdev
, "%s %d: err 0x%x\n", __func__
, __LINE__
, err
);
721 edesc
= (struct ahash_edesc
*)((char *)desc
-
722 offsetof(struct ahash_edesc
, hw_desc
));
724 caam_jr_strstatus(jrdev
, err
);
726 ahash_unmap_ctx(jrdev
, edesc
, req
, digestsize
, DMA_TO_DEVICE
);
730 print_hex_dump(KERN_ERR
, "ctx@"__stringify(__LINE__
)": ",
731 DUMP_PREFIX_ADDRESS
, 16, 4, state
->caam_ctx
,
734 print_hex_dump(KERN_ERR
, "result@"__stringify(__LINE__
)": ",
735 DUMP_PREFIX_ADDRESS
, 16, 4, req
->result
,
739 req
->base
.complete(&req
->base
, err
);
742 static void ahash_done_ctx_dst(struct device
*jrdev
, u32
*desc
, u32 err
,
745 struct ahash_request
*req
= context
;
746 struct ahash_edesc
*edesc
;
747 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
748 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
750 struct caam_hash_state
*state
= ahash_request_ctx(req
);
751 int digestsize
= crypto_ahash_digestsize(ahash
);
753 dev_err(jrdev
, "%s %d: err 0x%x\n", __func__
, __LINE__
, err
);
756 edesc
= (struct ahash_edesc
*)((char *)desc
-
757 offsetof(struct ahash_edesc
, hw_desc
));
759 caam_jr_strstatus(jrdev
, err
);
761 ahash_unmap_ctx(jrdev
, edesc
, req
, ctx
->ctx_len
, DMA_FROM_DEVICE
);
765 print_hex_dump(KERN_ERR
, "ctx@"__stringify(__LINE__
)": ",
766 DUMP_PREFIX_ADDRESS
, 16, 4, state
->caam_ctx
,
769 print_hex_dump(KERN_ERR
, "result@"__stringify(__LINE__
)": ",
770 DUMP_PREFIX_ADDRESS
, 16, 4, req
->result
,
774 req
->base
.complete(&req
->base
, err
);
777 /* submit update job descriptor */
778 static int ahash_update_ctx(struct ahash_request
*req
)
780 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
781 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
782 struct caam_hash_state
*state
= ahash_request_ctx(req
);
783 struct device
*jrdev
= ctx
->jrdev
;
784 gfp_t flags
= (req
->base
.flags
& (CRYPTO_TFM_REQ_MAY_BACKLOG
|
785 CRYPTO_TFM_REQ_MAY_SLEEP
)) ? GFP_KERNEL
: GFP_ATOMIC
;
786 u8
*buf
= state
->current_buf
? state
->buf_1
: state
->buf_0
;
787 int *buflen
= state
->current_buf
? &state
->buflen_1
: &state
->buflen_0
;
788 u8
*next_buf
= state
->current_buf
? state
->buf_0
: state
->buf_1
;
789 int *next_buflen
= state
->current_buf
? &state
->buflen_0
:
790 &state
->buflen_1
, last_buflen
;
791 int in_len
= *buflen
+ req
->nbytes
, to_hash
;
792 u32
*sh_desc
= ctx
->sh_desc_update
, *desc
;
793 dma_addr_t ptr
= ctx
->sh_desc_update_dma
;
794 int src_nents
, sec4_sg_bytes
, sec4_sg_src_index
;
795 struct ahash_edesc
*edesc
;
799 last_buflen
= *next_buflen
;
800 *next_buflen
= in_len
& (crypto_tfm_alg_blocksize(&ahash
->base
) - 1);
801 to_hash
= in_len
- *next_buflen
;
804 src_nents
= sg_nents_for_len(req
->src
,
805 req
->nbytes
- (*next_buflen
));
807 dev_err(jrdev
, "Invalid number of src SG.\n");
810 sec4_sg_src_index
= 1 + (*buflen
? 1 : 0);
811 sec4_sg_bytes
= (sec4_sg_src_index
+ src_nents
) *
812 sizeof(struct sec4_sg_entry
);
815 * allocate space for base edesc and hw desc commands,
818 edesc
= kzalloc(sizeof(*edesc
) + DESC_JOB_IO_LEN
+
819 sec4_sg_bytes
, GFP_DMA
| flags
);
822 "could not allocate extended descriptor\n");
826 edesc
->src_nents
= src_nents
;
827 edesc
->sec4_sg_bytes
= sec4_sg_bytes
;
828 edesc
->sec4_sg
= (void *)edesc
+ sizeof(struct ahash_edesc
) +
831 ret
= ctx_map_to_sec4_sg(desc
, jrdev
, state
, ctx
->ctx_len
,
832 edesc
->sec4_sg
, DMA_BIDIRECTIONAL
);
836 state
->buf_dma
= try_buf_map_to_sec4_sg(jrdev
,
839 *buflen
, last_buflen
);
842 src_map_to_sec4_sg(jrdev
, req
->src
, src_nents
,
843 edesc
->sec4_sg
+ sec4_sg_src_index
);
845 scatterwalk_map_and_copy(next_buf
, req
->src
,
849 (edesc
->sec4_sg
+ sec4_sg_src_index
- 1)->len
|=
853 state
->current_buf
= !state
->current_buf
;
855 sh_len
= desc_len(sh_desc
);
856 desc
= edesc
->hw_desc
;
857 init_job_desc_shared(desc
, ptr
, sh_len
, HDR_SHARE_DEFER
|
860 edesc
->sec4_sg_dma
= dma_map_single(jrdev
, edesc
->sec4_sg
,
863 if (dma_mapping_error(jrdev
, edesc
->sec4_sg_dma
)) {
864 dev_err(jrdev
, "unable to map S/G table\n");
868 append_seq_in_ptr(desc
, edesc
->sec4_sg_dma
, ctx
->ctx_len
+
871 append_seq_out_ptr(desc
, state
->ctx_dma
, ctx
->ctx_len
, 0);
874 print_hex_dump(KERN_ERR
, "jobdesc@"__stringify(__LINE__
)": ",
875 DUMP_PREFIX_ADDRESS
, 16, 4, desc
,
876 desc_bytes(desc
), 1);
879 ret
= caam_jr_enqueue(jrdev
, desc
, ahash_done_bi
, req
);
883 ahash_unmap_ctx(jrdev
, edesc
, req
, ctx
->ctx_len
,
887 } else if (*next_buflen
) {
888 scatterwalk_map_and_copy(buf
+ *buflen
, req
->src
, 0,
890 *buflen
= *next_buflen
;
891 *next_buflen
= last_buflen
;
894 print_hex_dump(KERN_ERR
, "buf@"__stringify(__LINE__
)": ",
895 DUMP_PREFIX_ADDRESS
, 16, 4, buf
, *buflen
, 1);
896 print_hex_dump(KERN_ERR
, "next buf@"__stringify(__LINE__
)": ",
897 DUMP_PREFIX_ADDRESS
, 16, 4, next_buf
,
904 static int ahash_final_ctx(struct ahash_request
*req
)
906 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
907 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
908 struct caam_hash_state
*state
= ahash_request_ctx(req
);
909 struct device
*jrdev
= ctx
->jrdev
;
910 gfp_t flags
= (req
->base
.flags
& (CRYPTO_TFM_REQ_MAY_BACKLOG
|
911 CRYPTO_TFM_REQ_MAY_SLEEP
)) ? GFP_KERNEL
: GFP_ATOMIC
;
912 u8
*buf
= state
->current_buf
? state
->buf_1
: state
->buf_0
;
913 int buflen
= state
->current_buf
? state
->buflen_1
: state
->buflen_0
;
914 int last_buflen
= state
->current_buf
? state
->buflen_0
:
916 u32
*sh_desc
= ctx
->sh_desc_fin
, *desc
;
917 dma_addr_t ptr
= ctx
->sh_desc_fin_dma
;
918 int sec4_sg_bytes
, sec4_sg_src_index
;
919 int digestsize
= crypto_ahash_digestsize(ahash
);
920 struct ahash_edesc
*edesc
;
924 sec4_sg_src_index
= 1 + (buflen
? 1 : 0);
925 sec4_sg_bytes
= sec4_sg_src_index
* sizeof(struct sec4_sg_entry
);
927 /* allocate space for base edesc and hw desc commands, link tables */
928 edesc
= kzalloc(sizeof(*edesc
) + DESC_JOB_IO_LEN
+ sec4_sg_bytes
,
931 dev_err(jrdev
, "could not allocate extended descriptor\n");
935 sh_len
= desc_len(sh_desc
);
936 desc
= edesc
->hw_desc
;
937 init_job_desc_shared(desc
, ptr
, sh_len
, HDR_SHARE_DEFER
| HDR_REVERSE
);
939 edesc
->sec4_sg_bytes
= sec4_sg_bytes
;
940 edesc
->sec4_sg
= (void *)edesc
+ sizeof(struct ahash_edesc
) +
942 edesc
->src_nents
= 0;
944 ret
= ctx_map_to_sec4_sg(desc
, jrdev
, state
, ctx
->ctx_len
,
945 edesc
->sec4_sg
, DMA_TO_DEVICE
);
949 state
->buf_dma
= try_buf_map_to_sec4_sg(jrdev
, edesc
->sec4_sg
+ 1,
950 buf
, state
->buf_dma
, buflen
,
952 (edesc
->sec4_sg
+ sec4_sg_src_index
- 1)->len
|= SEC4_SG_LEN_FIN
;
954 edesc
->sec4_sg_dma
= dma_map_single(jrdev
, edesc
->sec4_sg
,
955 sec4_sg_bytes
, DMA_TO_DEVICE
);
956 if (dma_mapping_error(jrdev
, edesc
->sec4_sg_dma
)) {
957 dev_err(jrdev
, "unable to map S/G table\n");
961 append_seq_in_ptr(desc
, edesc
->sec4_sg_dma
, ctx
->ctx_len
+ buflen
,
964 edesc
->dst_dma
= map_seq_out_ptr_result(desc
, jrdev
, req
->result
,
966 if (dma_mapping_error(jrdev
, edesc
->dst_dma
)) {
967 dev_err(jrdev
, "unable to map dst\n");
972 print_hex_dump(KERN_ERR
, "jobdesc@"__stringify(__LINE__
)": ",
973 DUMP_PREFIX_ADDRESS
, 16, 4, desc
, desc_bytes(desc
), 1);
976 ret
= caam_jr_enqueue(jrdev
, desc
, ahash_done_ctx_src
, req
);
980 ahash_unmap_ctx(jrdev
, edesc
, req
, digestsize
, DMA_FROM_DEVICE
);
987 static int ahash_finup_ctx(struct ahash_request
*req
)
989 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
990 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
991 struct caam_hash_state
*state
= ahash_request_ctx(req
);
992 struct device
*jrdev
= ctx
->jrdev
;
993 gfp_t flags
= (req
->base
.flags
& (CRYPTO_TFM_REQ_MAY_BACKLOG
|
994 CRYPTO_TFM_REQ_MAY_SLEEP
)) ? GFP_KERNEL
: GFP_ATOMIC
;
995 u8
*buf
= state
->current_buf
? state
->buf_1
: state
->buf_0
;
996 int buflen
= state
->current_buf
? state
->buflen_1
: state
->buflen_0
;
997 int last_buflen
= state
->current_buf
? state
->buflen_0
:
999 u32
*sh_desc
= ctx
->sh_desc_finup
, *desc
;
1000 dma_addr_t ptr
= ctx
->sh_desc_finup_dma
;
1001 int sec4_sg_bytes
, sec4_sg_src_index
;
1003 int digestsize
= crypto_ahash_digestsize(ahash
);
1004 struct ahash_edesc
*edesc
;
1008 src_nents
= sg_nents_for_len(req
->src
, req
->nbytes
);
1009 if (src_nents
< 0) {
1010 dev_err(jrdev
, "Invalid number of src SG.\n");
1013 sec4_sg_src_index
= 1 + (buflen
? 1 : 0);
1014 sec4_sg_bytes
= (sec4_sg_src_index
+ src_nents
) *
1015 sizeof(struct sec4_sg_entry
);
1017 /* allocate space for base edesc and hw desc commands, link tables */
1018 edesc
= kzalloc(sizeof(*edesc
) + DESC_JOB_IO_LEN
+ sec4_sg_bytes
,
1021 dev_err(jrdev
, "could not allocate extended descriptor\n");
1025 sh_len
= desc_len(sh_desc
);
1026 desc
= edesc
->hw_desc
;
1027 init_job_desc_shared(desc
, ptr
, sh_len
, HDR_SHARE_DEFER
| HDR_REVERSE
);
1029 edesc
->src_nents
= src_nents
;
1030 edesc
->sec4_sg_bytes
= sec4_sg_bytes
;
1031 edesc
->sec4_sg
= (void *)edesc
+ sizeof(struct ahash_edesc
) +
1034 ret
= ctx_map_to_sec4_sg(desc
, jrdev
, state
, ctx
->ctx_len
,
1035 edesc
->sec4_sg
, DMA_TO_DEVICE
);
1039 state
->buf_dma
= try_buf_map_to_sec4_sg(jrdev
, edesc
->sec4_sg
+ 1,
1040 buf
, state
->buf_dma
, buflen
,
1043 src_map_to_sec4_sg(jrdev
, req
->src
, src_nents
, edesc
->sec4_sg
+
1046 edesc
->sec4_sg_dma
= dma_map_single(jrdev
, edesc
->sec4_sg
,
1047 sec4_sg_bytes
, DMA_TO_DEVICE
);
1048 if (dma_mapping_error(jrdev
, edesc
->sec4_sg_dma
)) {
1049 dev_err(jrdev
, "unable to map S/G table\n");
1053 append_seq_in_ptr(desc
, edesc
->sec4_sg_dma
, ctx
->ctx_len
+
1054 buflen
+ req
->nbytes
, LDST_SGF
);
1056 edesc
->dst_dma
= map_seq_out_ptr_result(desc
, jrdev
, req
->result
,
1058 if (dma_mapping_error(jrdev
, edesc
->dst_dma
)) {
1059 dev_err(jrdev
, "unable to map dst\n");
1064 print_hex_dump(KERN_ERR
, "jobdesc@"__stringify(__LINE__
)": ",
1065 DUMP_PREFIX_ADDRESS
, 16, 4, desc
, desc_bytes(desc
), 1);
1068 ret
= caam_jr_enqueue(jrdev
, desc
, ahash_done_ctx_src
, req
);
1072 ahash_unmap_ctx(jrdev
, edesc
, req
, digestsize
, DMA_FROM_DEVICE
);
1079 static int ahash_digest(struct ahash_request
*req
)
1081 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
1082 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
1083 struct device
*jrdev
= ctx
->jrdev
;
1084 gfp_t flags
= (req
->base
.flags
& (CRYPTO_TFM_REQ_MAY_BACKLOG
|
1085 CRYPTO_TFM_REQ_MAY_SLEEP
)) ? GFP_KERNEL
: GFP_ATOMIC
;
1086 u32
*sh_desc
= ctx
->sh_desc_digest
, *desc
;
1087 dma_addr_t ptr
= ctx
->sh_desc_digest_dma
;
1088 int digestsize
= crypto_ahash_digestsize(ahash
);
1089 int src_nents
, sec4_sg_bytes
;
1091 struct ahash_edesc
*edesc
;
1096 src_nents
= sg_count(req
->src
, req
->nbytes
);
1097 if (src_nents
< 0) {
1098 dev_err(jrdev
, "Invalid number of src SG.\n");
1101 dma_map_sg(jrdev
, req
->src
, src_nents
? : 1, DMA_TO_DEVICE
);
1102 sec4_sg_bytes
= src_nents
* sizeof(struct sec4_sg_entry
);
1104 /* allocate space for base edesc and hw desc commands, link tables */
1105 edesc
= kzalloc(sizeof(*edesc
) + sec4_sg_bytes
+ DESC_JOB_IO_LEN
,
1108 dev_err(jrdev
, "could not allocate extended descriptor\n");
1111 edesc
->sec4_sg
= (void *)edesc
+ sizeof(struct ahash_edesc
) +
1113 edesc
->sec4_sg_bytes
= sec4_sg_bytes
;
1114 edesc
->src_nents
= src_nents
;
1116 sh_len
= desc_len(sh_desc
);
1117 desc
= edesc
->hw_desc
;
1118 init_job_desc_shared(desc
, ptr
, sh_len
, HDR_SHARE_DEFER
| HDR_REVERSE
);
1121 sg_to_sec4_sg_last(req
->src
, src_nents
, edesc
->sec4_sg
, 0);
1122 edesc
->sec4_sg_dma
= dma_map_single(jrdev
, edesc
->sec4_sg
,
1123 sec4_sg_bytes
, DMA_TO_DEVICE
);
1124 if (dma_mapping_error(jrdev
, edesc
->sec4_sg_dma
)) {
1125 dev_err(jrdev
, "unable to map S/G table\n");
1128 src_dma
= edesc
->sec4_sg_dma
;
1131 src_dma
= sg_dma_address(req
->src
);
1134 append_seq_in_ptr(desc
, src_dma
, req
->nbytes
, options
);
1136 edesc
->dst_dma
= map_seq_out_ptr_result(desc
, jrdev
, req
->result
,
1138 if (dma_mapping_error(jrdev
, edesc
->dst_dma
)) {
1139 dev_err(jrdev
, "unable to map dst\n");
1144 print_hex_dump(KERN_ERR
, "jobdesc@"__stringify(__LINE__
)": ",
1145 DUMP_PREFIX_ADDRESS
, 16, 4, desc
, desc_bytes(desc
), 1);
1148 ret
= caam_jr_enqueue(jrdev
, desc
, ahash_done
, req
);
1152 ahash_unmap(jrdev
, edesc
, req
, digestsize
);
1159 /* submit ahash final if it the first job descriptor */
1160 static int ahash_final_no_ctx(struct ahash_request
*req
)
1162 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
1163 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
1164 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1165 struct device
*jrdev
= ctx
->jrdev
;
1166 gfp_t flags
= (req
->base
.flags
& (CRYPTO_TFM_REQ_MAY_BACKLOG
|
1167 CRYPTO_TFM_REQ_MAY_SLEEP
)) ? GFP_KERNEL
: GFP_ATOMIC
;
1168 u8
*buf
= state
->current_buf
? state
->buf_1
: state
->buf_0
;
1169 int buflen
= state
->current_buf
? state
->buflen_1
: state
->buflen_0
;
1170 u32
*sh_desc
= ctx
->sh_desc_digest
, *desc
;
1171 dma_addr_t ptr
= ctx
->sh_desc_digest_dma
;
1172 int digestsize
= crypto_ahash_digestsize(ahash
);
1173 struct ahash_edesc
*edesc
;
1177 /* allocate space for base edesc and hw desc commands, link tables */
1178 edesc
= kzalloc(sizeof(*edesc
) + DESC_JOB_IO_LEN
, GFP_DMA
| flags
);
1180 dev_err(jrdev
, "could not allocate extended descriptor\n");
1184 edesc
->sec4_sg_bytes
= 0;
1185 sh_len
= desc_len(sh_desc
);
1186 desc
= edesc
->hw_desc
;
1187 init_job_desc_shared(desc
, ptr
, sh_len
, HDR_SHARE_DEFER
| HDR_REVERSE
);
1189 state
->buf_dma
= dma_map_single(jrdev
, buf
, buflen
, DMA_TO_DEVICE
);
1190 if (dma_mapping_error(jrdev
, state
->buf_dma
)) {
1191 dev_err(jrdev
, "unable to map src\n");
1195 append_seq_in_ptr(desc
, state
->buf_dma
, buflen
, 0);
1197 edesc
->dst_dma
= map_seq_out_ptr_result(desc
, jrdev
, req
->result
,
1199 if (dma_mapping_error(jrdev
, edesc
->dst_dma
)) {
1200 dev_err(jrdev
, "unable to map dst\n");
1203 edesc
->src_nents
= 0;
1206 print_hex_dump(KERN_ERR
, "jobdesc@"__stringify(__LINE__
)": ",
1207 DUMP_PREFIX_ADDRESS
, 16, 4, desc
, desc_bytes(desc
), 1);
1210 ret
= caam_jr_enqueue(jrdev
, desc
, ahash_done
, req
);
1214 ahash_unmap(jrdev
, edesc
, req
, digestsize
);
1221 /* submit ahash update if it the first job descriptor after update */
1222 static int ahash_update_no_ctx(struct ahash_request
*req
)
1224 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
1225 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
1226 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1227 struct device
*jrdev
= ctx
->jrdev
;
1228 gfp_t flags
= (req
->base
.flags
& (CRYPTO_TFM_REQ_MAY_BACKLOG
|
1229 CRYPTO_TFM_REQ_MAY_SLEEP
)) ? GFP_KERNEL
: GFP_ATOMIC
;
1230 u8
*buf
= state
->current_buf
? state
->buf_1
: state
->buf_0
;
1231 int *buflen
= state
->current_buf
? &state
->buflen_1
: &state
->buflen_0
;
1232 u8
*next_buf
= state
->current_buf
? state
->buf_0
: state
->buf_1
;
1233 int *next_buflen
= state
->current_buf
? &state
->buflen_0
:
1235 int in_len
= *buflen
+ req
->nbytes
, to_hash
;
1236 int sec4_sg_bytes
, src_nents
;
1237 struct ahash_edesc
*edesc
;
1238 u32
*desc
, *sh_desc
= ctx
->sh_desc_update_first
;
1239 dma_addr_t ptr
= ctx
->sh_desc_update_first_dma
;
1243 *next_buflen
= in_len
& (crypto_tfm_alg_blocksize(&ahash
->base
) - 1);
1244 to_hash
= in_len
- *next_buflen
;
1247 src_nents
= sg_nents_for_len(req
->src
,
1248 req
->nbytes
- (*next_buflen
));
1249 if (src_nents
< 0) {
1250 dev_err(jrdev
, "Invalid number of src SG.\n");
1253 sec4_sg_bytes
= (1 + src_nents
) *
1254 sizeof(struct sec4_sg_entry
);
1257 * allocate space for base edesc and hw desc commands,
1260 edesc
= kzalloc(sizeof(*edesc
) + DESC_JOB_IO_LEN
+
1261 sec4_sg_bytes
, GFP_DMA
| flags
);
1264 "could not allocate extended descriptor\n");
1268 edesc
->src_nents
= src_nents
;
1269 edesc
->sec4_sg_bytes
= sec4_sg_bytes
;
1270 edesc
->sec4_sg
= (void *)edesc
+ sizeof(struct ahash_edesc
) +
1274 state
->buf_dma
= buf_map_to_sec4_sg(jrdev
, edesc
->sec4_sg
,
1276 src_map_to_sec4_sg(jrdev
, req
->src
, src_nents
,
1277 edesc
->sec4_sg
+ 1);
1279 scatterwalk_map_and_copy(next_buf
, req
->src
,
1284 state
->current_buf
= !state
->current_buf
;
1286 sh_len
= desc_len(sh_desc
);
1287 desc
= edesc
->hw_desc
;
1288 init_job_desc_shared(desc
, ptr
, sh_len
, HDR_SHARE_DEFER
|
1291 edesc
->sec4_sg_dma
= dma_map_single(jrdev
, edesc
->sec4_sg
,
1294 if (dma_mapping_error(jrdev
, edesc
->sec4_sg_dma
)) {
1295 dev_err(jrdev
, "unable to map S/G table\n");
1299 append_seq_in_ptr(desc
, edesc
->sec4_sg_dma
, to_hash
, LDST_SGF
);
1301 ret
= map_seq_out_ptr_ctx(desc
, jrdev
, state
, ctx
->ctx_len
);
1306 print_hex_dump(KERN_ERR
, "jobdesc@"__stringify(__LINE__
)": ",
1307 DUMP_PREFIX_ADDRESS
, 16, 4, desc
,
1308 desc_bytes(desc
), 1);
1311 ret
= caam_jr_enqueue(jrdev
, desc
, ahash_done_ctx_dst
, req
);
1314 state
->update
= ahash_update_ctx
;
1315 state
->finup
= ahash_finup_ctx
;
1316 state
->final
= ahash_final_ctx
;
1318 ahash_unmap_ctx(jrdev
, edesc
, req
, ctx
->ctx_len
,
1322 } else if (*next_buflen
) {
1323 scatterwalk_map_and_copy(buf
+ *buflen
, req
->src
, 0,
1325 *buflen
= *next_buflen
;
1329 print_hex_dump(KERN_ERR
, "buf@"__stringify(__LINE__
)": ",
1330 DUMP_PREFIX_ADDRESS
, 16, 4, buf
, *buflen
, 1);
1331 print_hex_dump(KERN_ERR
, "next buf@"__stringify(__LINE__
)": ",
1332 DUMP_PREFIX_ADDRESS
, 16, 4, next_buf
,
1339 /* submit ahash finup if it the first job descriptor after update */
1340 static int ahash_finup_no_ctx(struct ahash_request
*req
)
1342 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
1343 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
1344 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1345 struct device
*jrdev
= ctx
->jrdev
;
1346 gfp_t flags
= (req
->base
.flags
& (CRYPTO_TFM_REQ_MAY_BACKLOG
|
1347 CRYPTO_TFM_REQ_MAY_SLEEP
)) ? GFP_KERNEL
: GFP_ATOMIC
;
1348 u8
*buf
= state
->current_buf
? state
->buf_1
: state
->buf_0
;
1349 int buflen
= state
->current_buf
? state
->buflen_1
: state
->buflen_0
;
1350 int last_buflen
= state
->current_buf
? state
->buflen_0
:
1352 u32
*sh_desc
= ctx
->sh_desc_digest
, *desc
;
1353 dma_addr_t ptr
= ctx
->sh_desc_digest_dma
;
1354 int sec4_sg_bytes
, sec4_sg_src_index
, src_nents
;
1355 int digestsize
= crypto_ahash_digestsize(ahash
);
1356 struct ahash_edesc
*edesc
;
1360 src_nents
= sg_nents_for_len(req
->src
, req
->nbytes
);
1361 if (src_nents
< 0) {
1362 dev_err(jrdev
, "Invalid number of src SG.\n");
1365 sec4_sg_src_index
= 2;
1366 sec4_sg_bytes
= (sec4_sg_src_index
+ src_nents
) *
1367 sizeof(struct sec4_sg_entry
);
1369 /* allocate space for base edesc and hw desc commands, link tables */
1370 edesc
= kzalloc(sizeof(*edesc
) + DESC_JOB_IO_LEN
+ sec4_sg_bytes
,
1373 dev_err(jrdev
, "could not allocate extended descriptor\n");
1377 sh_len
= desc_len(sh_desc
);
1378 desc
= edesc
->hw_desc
;
1379 init_job_desc_shared(desc
, ptr
, sh_len
, HDR_SHARE_DEFER
| HDR_REVERSE
);
1381 edesc
->src_nents
= src_nents
;
1382 edesc
->sec4_sg_bytes
= sec4_sg_bytes
;
1383 edesc
->sec4_sg
= (void *)edesc
+ sizeof(struct ahash_edesc
) +
1386 state
->buf_dma
= try_buf_map_to_sec4_sg(jrdev
, edesc
->sec4_sg
, buf
,
1387 state
->buf_dma
, buflen
,
1390 src_map_to_sec4_sg(jrdev
, req
->src
, src_nents
, edesc
->sec4_sg
+ 1);
1392 edesc
->sec4_sg_dma
= dma_map_single(jrdev
, edesc
->sec4_sg
,
1393 sec4_sg_bytes
, DMA_TO_DEVICE
);
1394 if (dma_mapping_error(jrdev
, edesc
->sec4_sg_dma
)) {
1395 dev_err(jrdev
, "unable to map S/G table\n");
1399 append_seq_in_ptr(desc
, edesc
->sec4_sg_dma
, buflen
+
1400 req
->nbytes
, LDST_SGF
);
1402 edesc
->dst_dma
= map_seq_out_ptr_result(desc
, jrdev
, req
->result
,
1404 if (dma_mapping_error(jrdev
, edesc
->dst_dma
)) {
1405 dev_err(jrdev
, "unable to map dst\n");
1410 print_hex_dump(KERN_ERR
, "jobdesc@"__stringify(__LINE__
)": ",
1411 DUMP_PREFIX_ADDRESS
, 16, 4, desc
, desc_bytes(desc
), 1);
1414 ret
= caam_jr_enqueue(jrdev
, desc
, ahash_done
, req
);
1418 ahash_unmap(jrdev
, edesc
, req
, digestsize
);
1425 /* submit first update job descriptor after init */
1426 static int ahash_update_first(struct ahash_request
*req
)
1428 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
1429 struct caam_hash_ctx
*ctx
= crypto_ahash_ctx(ahash
);
1430 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1431 struct device
*jrdev
= ctx
->jrdev
;
1432 gfp_t flags
= (req
->base
.flags
& (CRYPTO_TFM_REQ_MAY_BACKLOG
|
1433 CRYPTO_TFM_REQ_MAY_SLEEP
)) ? GFP_KERNEL
: GFP_ATOMIC
;
1434 u8
*next_buf
= state
->current_buf
? state
->buf_1
: state
->buf_0
;
1435 int *next_buflen
= state
->current_buf
?
1436 &state
->buflen_1
: &state
->buflen_0
;
1438 u32
*sh_desc
= ctx
->sh_desc_update_first
, *desc
;
1439 dma_addr_t ptr
= ctx
->sh_desc_update_first_dma
;
1440 int sec4_sg_bytes
, src_nents
;
1443 struct ahash_edesc
*edesc
;
1447 *next_buflen
= req
->nbytes
& (crypto_tfm_alg_blocksize(&ahash
->base
) -
1449 to_hash
= req
->nbytes
- *next_buflen
;
1452 src_nents
= sg_count(req
->src
, req
->nbytes
- (*next_buflen
));
1453 if (src_nents
< 0) {
1454 dev_err(jrdev
, "Invalid number of src SG.\n");
1457 dma_map_sg(jrdev
, req
->src
, src_nents
? : 1, DMA_TO_DEVICE
);
1458 sec4_sg_bytes
= src_nents
* sizeof(struct sec4_sg_entry
);
1461 * allocate space for base edesc and hw desc commands,
1464 edesc
= kzalloc(sizeof(*edesc
) + DESC_JOB_IO_LEN
+
1465 sec4_sg_bytes
, GFP_DMA
| flags
);
1468 "could not allocate extended descriptor\n");
1472 edesc
->src_nents
= src_nents
;
1473 edesc
->sec4_sg_bytes
= sec4_sg_bytes
;
1474 edesc
->sec4_sg
= (void *)edesc
+ sizeof(struct ahash_edesc
) +
1479 sg_to_sec4_sg_last(req
->src
, src_nents
,
1481 edesc
->sec4_sg_dma
= dma_map_single(jrdev
,
1485 if (dma_mapping_error(jrdev
, edesc
->sec4_sg_dma
)) {
1486 dev_err(jrdev
, "unable to map S/G table\n");
1489 src_dma
= edesc
->sec4_sg_dma
;
1492 src_dma
= sg_dma_address(req
->src
);
1497 scatterwalk_map_and_copy(next_buf
, req
->src
, to_hash
,
1500 sh_len
= desc_len(sh_desc
);
1501 desc
= edesc
->hw_desc
;
1502 init_job_desc_shared(desc
, ptr
, sh_len
, HDR_SHARE_DEFER
|
1505 append_seq_in_ptr(desc
, src_dma
, to_hash
, options
);
1507 ret
= map_seq_out_ptr_ctx(desc
, jrdev
, state
, ctx
->ctx_len
);
1512 print_hex_dump(KERN_ERR
, "jobdesc@"__stringify(__LINE__
)": ",
1513 DUMP_PREFIX_ADDRESS
, 16, 4, desc
,
1514 desc_bytes(desc
), 1);
1517 ret
= caam_jr_enqueue(jrdev
, desc
, ahash_done_ctx_dst
,
1521 state
->update
= ahash_update_ctx
;
1522 state
->finup
= ahash_finup_ctx
;
1523 state
->final
= ahash_final_ctx
;
1525 ahash_unmap_ctx(jrdev
, edesc
, req
, ctx
->ctx_len
,
1529 } else if (*next_buflen
) {
1530 state
->update
= ahash_update_no_ctx
;
1531 state
->finup
= ahash_finup_no_ctx
;
1532 state
->final
= ahash_final_no_ctx
;
1533 scatterwalk_map_and_copy(next_buf
, req
->src
, 0,
1537 print_hex_dump(KERN_ERR
, "next buf@"__stringify(__LINE__
)": ",
1538 DUMP_PREFIX_ADDRESS
, 16, 4, next_buf
,
1545 static int ahash_finup_first(struct ahash_request
*req
)
1547 return ahash_digest(req
);
1550 static int ahash_init(struct ahash_request
*req
)
1552 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1554 state
->update
= ahash_update_first
;
1555 state
->finup
= ahash_finup_first
;
1556 state
->final
= ahash_final_no_ctx
;
1558 state
->current_buf
= 0;
1560 state
->buflen_0
= 0;
1561 state
->buflen_1
= 0;
1566 static int ahash_update(struct ahash_request
*req
)
1568 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1570 return state
->update(req
);
1573 static int ahash_finup(struct ahash_request
*req
)
1575 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1577 return state
->finup(req
);
1580 static int ahash_final(struct ahash_request
*req
)
1582 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1584 return state
->final(req
);
1587 static int ahash_export(struct ahash_request
*req
, void *out
)
1589 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1590 struct caam_export_state
*export
= out
;
1594 if (state
->current_buf
) {
1596 len
= state
->buflen_1
;
1599 len
= state
->buflen_0
;
1602 memcpy(export
->buf
, buf
, len
);
1603 memcpy(export
->caam_ctx
, state
->caam_ctx
, sizeof(export
->caam_ctx
));
1604 export
->buflen
= len
;
1605 export
->update
= state
->update
;
1606 export
->final
= state
->final
;
1607 export
->finup
= state
->finup
;
1612 static int ahash_import(struct ahash_request
*req
, const void *in
)
1614 struct caam_hash_state
*state
= ahash_request_ctx(req
);
1615 const struct caam_export_state
*export
= in
;
1617 memset(state
, 0, sizeof(*state
));
1618 memcpy(state
->buf_0
, export
->buf
, export
->buflen
);
1619 memcpy(state
->caam_ctx
, export
->caam_ctx
, sizeof(state
->caam_ctx
));
1620 state
->buflen_0
= export
->buflen
;
1621 state
->update
= export
->update
;
1622 state
->final
= export
->final
;
1623 state
->finup
= export
->finup
;
1628 struct caam_hash_template
{
1629 char name
[CRYPTO_MAX_ALG_NAME
];
1630 char driver_name
[CRYPTO_MAX_ALG_NAME
];
1631 char hmac_name
[CRYPTO_MAX_ALG_NAME
];
1632 char hmac_driver_name
[CRYPTO_MAX_ALG_NAME
];
1633 unsigned int blocksize
;
1634 struct ahash_alg template_ahash
;
1639 /* ahash descriptors */
1640 static struct caam_hash_template driver_hash
[] = {
1643 .driver_name
= "sha1-caam",
1644 .hmac_name
= "hmac(sha1)",
1645 .hmac_driver_name
= "hmac-sha1-caam",
1646 .blocksize
= SHA1_BLOCK_SIZE
,
1649 .update
= ahash_update
,
1650 .final
= ahash_final
,
1651 .finup
= ahash_finup
,
1652 .digest
= ahash_digest
,
1653 .export
= ahash_export
,
1654 .import
= ahash_import
,
1655 .setkey
= ahash_setkey
,
1657 .digestsize
= SHA1_DIGEST_SIZE
,
1658 .statesize
= sizeof(struct caam_export_state
),
1661 .alg_type
= OP_ALG_ALGSEL_SHA1
,
1662 .alg_op
= OP_ALG_ALGSEL_SHA1
| OP_ALG_AAI_HMAC
,
1665 .driver_name
= "sha224-caam",
1666 .hmac_name
= "hmac(sha224)",
1667 .hmac_driver_name
= "hmac-sha224-caam",
1668 .blocksize
= SHA224_BLOCK_SIZE
,
1671 .update
= ahash_update
,
1672 .final
= ahash_final
,
1673 .finup
= ahash_finup
,
1674 .digest
= ahash_digest
,
1675 .export
= ahash_export
,
1676 .import
= ahash_import
,
1677 .setkey
= ahash_setkey
,
1679 .digestsize
= SHA224_DIGEST_SIZE
,
1680 .statesize
= sizeof(struct caam_export_state
),
1683 .alg_type
= OP_ALG_ALGSEL_SHA224
,
1684 .alg_op
= OP_ALG_ALGSEL_SHA224
| OP_ALG_AAI_HMAC
,
1687 .driver_name
= "sha256-caam",
1688 .hmac_name
= "hmac(sha256)",
1689 .hmac_driver_name
= "hmac-sha256-caam",
1690 .blocksize
= SHA256_BLOCK_SIZE
,
1693 .update
= ahash_update
,
1694 .final
= ahash_final
,
1695 .finup
= ahash_finup
,
1696 .digest
= ahash_digest
,
1697 .export
= ahash_export
,
1698 .import
= ahash_import
,
1699 .setkey
= ahash_setkey
,
1701 .digestsize
= SHA256_DIGEST_SIZE
,
1702 .statesize
= sizeof(struct caam_export_state
),
1705 .alg_type
= OP_ALG_ALGSEL_SHA256
,
1706 .alg_op
= OP_ALG_ALGSEL_SHA256
| OP_ALG_AAI_HMAC
,
1709 .driver_name
= "sha384-caam",
1710 .hmac_name
= "hmac(sha384)",
1711 .hmac_driver_name
= "hmac-sha384-caam",
1712 .blocksize
= SHA384_BLOCK_SIZE
,
1715 .update
= ahash_update
,
1716 .final
= ahash_final
,
1717 .finup
= ahash_finup
,
1718 .digest
= ahash_digest
,
1719 .export
= ahash_export
,
1720 .import
= ahash_import
,
1721 .setkey
= ahash_setkey
,
1723 .digestsize
= SHA384_DIGEST_SIZE
,
1724 .statesize
= sizeof(struct caam_export_state
),
1727 .alg_type
= OP_ALG_ALGSEL_SHA384
,
1728 .alg_op
= OP_ALG_ALGSEL_SHA384
| OP_ALG_AAI_HMAC
,
1731 .driver_name
= "sha512-caam",
1732 .hmac_name
= "hmac(sha512)",
1733 .hmac_driver_name
= "hmac-sha512-caam",
1734 .blocksize
= SHA512_BLOCK_SIZE
,
1737 .update
= ahash_update
,
1738 .final
= ahash_final
,
1739 .finup
= ahash_finup
,
1740 .digest
= ahash_digest
,
1741 .export
= ahash_export
,
1742 .import
= ahash_import
,
1743 .setkey
= ahash_setkey
,
1745 .digestsize
= SHA512_DIGEST_SIZE
,
1746 .statesize
= sizeof(struct caam_export_state
),
1749 .alg_type
= OP_ALG_ALGSEL_SHA512
,
1750 .alg_op
= OP_ALG_ALGSEL_SHA512
| OP_ALG_AAI_HMAC
,
1753 .driver_name
= "md5-caam",
1754 .hmac_name
= "hmac(md5)",
1755 .hmac_driver_name
= "hmac-md5-caam",
1756 .blocksize
= MD5_BLOCK_WORDS
* 4,
1759 .update
= ahash_update
,
1760 .final
= ahash_final
,
1761 .finup
= ahash_finup
,
1762 .digest
= ahash_digest
,
1763 .export
= ahash_export
,
1764 .import
= ahash_import
,
1765 .setkey
= ahash_setkey
,
1767 .digestsize
= MD5_DIGEST_SIZE
,
1768 .statesize
= sizeof(struct caam_export_state
),
1771 .alg_type
= OP_ALG_ALGSEL_MD5
,
1772 .alg_op
= OP_ALG_ALGSEL_MD5
| OP_ALG_AAI_HMAC
,
1776 struct caam_hash_alg
{
1777 struct list_head entry
;
1780 struct ahash_alg ahash_alg
;
1783 static int caam_hash_cra_init(struct crypto_tfm
*tfm
)
1785 struct crypto_ahash
*ahash
= __crypto_ahash_cast(tfm
);
1786 struct crypto_alg
*base
= tfm
->__crt_alg
;
1787 struct hash_alg_common
*halg
=
1788 container_of(base
, struct hash_alg_common
, base
);
1789 struct ahash_alg
*alg
=
1790 container_of(halg
, struct ahash_alg
, halg
);
1791 struct caam_hash_alg
*caam_hash
=
1792 container_of(alg
, struct caam_hash_alg
, ahash_alg
);
1793 struct caam_hash_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1794 /* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
1795 static const u8 runninglen
[] = { HASH_MSG_LEN
+ MD5_DIGEST_SIZE
,
1796 HASH_MSG_LEN
+ SHA1_DIGEST_SIZE
,
1798 HASH_MSG_LEN
+ SHA256_DIGEST_SIZE
,
1800 HASH_MSG_LEN
+ SHA512_DIGEST_SIZE
};
1804 * Get a Job ring from Job Ring driver to ensure in-order
1805 * crypto request processing per tfm
1807 ctx
->jrdev
= caam_jr_alloc();
1808 if (IS_ERR(ctx
->jrdev
)) {
1809 pr_err("Job Ring Device allocation for transform failed\n");
1810 return PTR_ERR(ctx
->jrdev
);
1812 /* copy descriptor header template value */
1813 ctx
->alg_type
= OP_TYPE_CLASS2_ALG
| caam_hash
->alg_type
;
1814 ctx
->alg_op
= OP_TYPE_CLASS2_ALG
| caam_hash
->alg_op
;
1816 ctx
->ctx_len
= runninglen
[(ctx
->alg_op
& OP_ALG_ALGSEL_SUBMASK
) >>
1817 OP_ALG_ALGSEL_SHIFT
];
1819 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
1820 sizeof(struct caam_hash_state
));
1822 ret
= ahash_set_sh_desc(ahash
);
1827 static void caam_hash_cra_exit(struct crypto_tfm
*tfm
)
1829 struct caam_hash_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1831 if (ctx
->sh_desc_update_dma
&&
1832 !dma_mapping_error(ctx
->jrdev
, ctx
->sh_desc_update_dma
))
1833 dma_unmap_single(ctx
->jrdev
, ctx
->sh_desc_update_dma
,
1834 desc_bytes(ctx
->sh_desc_update
),
1836 if (ctx
->sh_desc_update_first_dma
&&
1837 !dma_mapping_error(ctx
->jrdev
, ctx
->sh_desc_update_first_dma
))
1838 dma_unmap_single(ctx
->jrdev
, ctx
->sh_desc_update_first_dma
,
1839 desc_bytes(ctx
->sh_desc_update_first
),
1841 if (ctx
->sh_desc_fin_dma
&&
1842 !dma_mapping_error(ctx
->jrdev
, ctx
->sh_desc_fin_dma
))
1843 dma_unmap_single(ctx
->jrdev
, ctx
->sh_desc_fin_dma
,
1844 desc_bytes(ctx
->sh_desc_fin
), DMA_TO_DEVICE
);
1845 if (ctx
->sh_desc_digest_dma
&&
1846 !dma_mapping_error(ctx
->jrdev
, ctx
->sh_desc_digest_dma
))
1847 dma_unmap_single(ctx
->jrdev
, ctx
->sh_desc_digest_dma
,
1848 desc_bytes(ctx
->sh_desc_digest
),
1850 if (ctx
->sh_desc_finup_dma
&&
1851 !dma_mapping_error(ctx
->jrdev
, ctx
->sh_desc_finup_dma
))
1852 dma_unmap_single(ctx
->jrdev
, ctx
->sh_desc_finup_dma
,
1853 desc_bytes(ctx
->sh_desc_finup
), DMA_TO_DEVICE
);
1855 caam_jr_free(ctx
->jrdev
);
1858 static void __exit
caam_algapi_hash_exit(void)
1860 struct caam_hash_alg
*t_alg
, *n
;
1862 if (!hash_list
.next
)
1865 list_for_each_entry_safe(t_alg
, n
, &hash_list
, entry
) {
1866 crypto_unregister_ahash(&t_alg
->ahash_alg
);
1867 list_del(&t_alg
->entry
);
1872 static struct caam_hash_alg
*
1873 caam_hash_alloc(struct caam_hash_template
*template,
1876 struct caam_hash_alg
*t_alg
;
1877 struct ahash_alg
*halg
;
1878 struct crypto_alg
*alg
;
1880 t_alg
= kzalloc(sizeof(*t_alg
), GFP_KERNEL
);
1882 pr_err("failed to allocate t_alg\n");
1883 return ERR_PTR(-ENOMEM
);
1886 t_alg
->ahash_alg
= template->template_ahash
;
1887 halg
= &t_alg
->ahash_alg
;
1888 alg
= &halg
->halg
.base
;
1891 snprintf(alg
->cra_name
, CRYPTO_MAX_ALG_NAME
, "%s",
1892 template->hmac_name
);
1893 snprintf(alg
->cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s",
1894 template->hmac_driver_name
);
1896 snprintf(alg
->cra_name
, CRYPTO_MAX_ALG_NAME
, "%s",
1898 snprintf(alg
->cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s",
1899 template->driver_name
);
1901 alg
->cra_module
= THIS_MODULE
;
1902 alg
->cra_init
= caam_hash_cra_init
;
1903 alg
->cra_exit
= caam_hash_cra_exit
;
1904 alg
->cra_ctxsize
= sizeof(struct caam_hash_ctx
);
1905 alg
->cra_priority
= CAAM_CRA_PRIORITY
;
1906 alg
->cra_blocksize
= template->blocksize
;
1907 alg
->cra_alignmask
= 0;
1908 alg
->cra_flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_TYPE_AHASH
;
1909 alg
->cra_type
= &crypto_ahash_type
;
1911 t_alg
->alg_type
= template->alg_type
;
1912 t_alg
->alg_op
= template->alg_op
;
1917 static int __init
caam_algapi_hash_init(void)
1919 struct device_node
*dev_node
;
1920 struct platform_device
*pdev
;
1921 struct device
*ctrldev
;
1923 struct caam_drv_private
*priv
;
1924 unsigned int md_limit
= SHA512_DIGEST_SIZE
;
1925 u32 cha_inst
, cha_vid
;
1927 dev_node
= of_find_compatible_node(NULL
, NULL
, "fsl,sec-v4.0");
1929 dev_node
= of_find_compatible_node(NULL
, NULL
, "fsl,sec4.0");
1934 pdev
= of_find_device_by_node(dev_node
);
1936 of_node_put(dev_node
);
1940 ctrldev
= &pdev
->dev
;
1941 priv
= dev_get_drvdata(ctrldev
);
1942 of_node_put(dev_node
);
1945 * If priv is NULL, it's probably because the caam driver wasn't
1946 * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
1952 * Register crypto algorithms the device supports. First, identify
1953 * presence and attributes of MD block.
1955 cha_vid
= rd_reg32(&priv
->ctrl
->perfmon
.cha_id_ls
);
1956 cha_inst
= rd_reg32(&priv
->ctrl
->perfmon
.cha_num_ls
);
1959 * Skip registration of any hashing algorithms if MD block
1962 if (!((cha_inst
& CHA_ID_LS_MD_MASK
) >> CHA_ID_LS_MD_SHIFT
))
1965 /* Limit digest size based on LP256 */
1966 if ((cha_vid
& CHA_ID_LS_MD_MASK
) == CHA_ID_LS_MD_LP256
)
1967 md_limit
= SHA256_DIGEST_SIZE
;
1969 INIT_LIST_HEAD(&hash_list
);
1971 /* register crypto algorithms the device supports */
1972 for (i
= 0; i
< ARRAY_SIZE(driver_hash
); i
++) {
1973 struct caam_hash_alg
*t_alg
;
1974 struct caam_hash_template
*alg
= driver_hash
+ i
;
1976 /* If MD size is not supported by device, skip registration */
1977 if (alg
->template_ahash
.halg
.digestsize
> md_limit
)
1980 /* register hmac version */
1981 t_alg
= caam_hash_alloc(alg
, true);
1982 if (IS_ERR(t_alg
)) {
1983 err
= PTR_ERR(t_alg
);
1984 pr_warn("%s alg allocation failed\n", alg
->driver_name
);
1988 err
= crypto_register_ahash(&t_alg
->ahash_alg
);
1990 pr_warn("%s alg registration failed: %d\n",
1991 t_alg
->ahash_alg
.halg
.base
.cra_driver_name
,
1995 list_add_tail(&t_alg
->entry
, &hash_list
);
1997 /* register unkeyed version */
1998 t_alg
= caam_hash_alloc(alg
, false);
1999 if (IS_ERR(t_alg
)) {
2000 err
= PTR_ERR(t_alg
);
2001 pr_warn("%s alg allocation failed\n", alg
->driver_name
);
2005 err
= crypto_register_ahash(&t_alg
->ahash_alg
);
2007 pr_warn("%s alg registration failed: %d\n",
2008 t_alg
->ahash_alg
.halg
.base
.cra_driver_name
,
2012 list_add_tail(&t_alg
->entry
, &hash_list
);
2018 module_init(caam_algapi_hash_init
);
2019 module_exit(caam_algapi_hash_exit
);
2021 MODULE_LICENSE("GPL");
2022 MODULE_DESCRIPTION("FSL CAAM support for ahash functions of crypto API");
2023 MODULE_AUTHOR("Freescale Semiconductor - NMG");