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 (3 * 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 u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
103 u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
104 u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
105 u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
106 dma_addr_t sh_desc_update_dma ____cacheline_aligned;
107 dma_addr_t sh_desc_update_first_dma;
108 dma_addr_t sh_desc_fin_dma;
109 dma_addr_t sh_desc_digest_dma;
110 enum dma_data_direction dir;
111 struct device *jrdev;
112 u8 key[CAAM_MAX_HASH_KEY_SIZE];
114 struct alginfo adata;
118 struct caam_hash_state {
122 u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
124 u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
126 u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
127 int (*update)(struct ahash_request *req);
128 int (*final)(struct ahash_request *req);
129 int (*finup)(struct ahash_request *req);
133 struct caam_export_state {
134 u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
135 u8 caam_ctx[MAX_CTX_LEN];
137 int (*update)(struct ahash_request *req);
138 int (*final)(struct ahash_request *req);
139 int (*finup)(struct ahash_request *req);
142 static inline void switch_buf(struct caam_hash_state *state)
144 state->current_buf ^= 1;
147 static inline u8 *current_buf(struct caam_hash_state *state)
149 return state->current_buf ? state->buf_1 : state->buf_0;
152 static inline u8 *alt_buf(struct caam_hash_state *state)
154 return state->current_buf ? state->buf_0 : state->buf_1;
157 static inline int *current_buflen(struct caam_hash_state *state)
159 return state->current_buf ? &state->buflen_1 : &state->buflen_0;
162 static inline int *alt_buflen(struct caam_hash_state *state)
164 return state->current_buf ? &state->buflen_0 : &state->buflen_1;
167 /* Common job descriptor seq in/out ptr routines */
169 /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
170 static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
171 struct caam_hash_state *state,
174 state->ctx_dma_len = ctx_len;
175 state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
176 ctx_len, DMA_FROM_DEVICE);
177 if (dma_mapping_error(jrdev, state->ctx_dma)) {
178 dev_err(jrdev, "unable to map ctx\n");
183 append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
188 /* Map current buffer in state (if length > 0) and put it in link table */
189 static inline int buf_map_to_sec4_sg(struct device *jrdev,
190 struct sec4_sg_entry *sec4_sg,
191 struct caam_hash_state *state)
193 int buflen = *current_buflen(state);
198 state->buf_dma = dma_map_single(jrdev, current_buf(state), buflen,
200 if (dma_mapping_error(jrdev, state->buf_dma)) {
201 dev_err(jrdev, "unable to map buf\n");
206 dma_to_sec4_sg_one(sec4_sg, state->buf_dma, buflen, 0);
211 /* Map state->caam_ctx, and add it to link table */
212 static inline int ctx_map_to_sec4_sg(struct device *jrdev,
213 struct caam_hash_state *state, int ctx_len,
214 struct sec4_sg_entry *sec4_sg, u32 flag)
216 state->ctx_dma_len = ctx_len;
217 state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
218 if (dma_mapping_error(jrdev, state->ctx_dma)) {
219 dev_err(jrdev, "unable to map ctx\n");
224 dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
230 * For ahash update, final and finup (import_ctx = true)
231 * import context, read and write to seqout
232 * For ahash firsts and digest (import_ctx = false)
233 * read and write to seqout
235 static inline void ahash_gen_sh_desc(u32 *desc, u32 state, int digestsize,
236 struct caam_hash_ctx *ctx, bool import_ctx,
239 u32 op = ctx->adata.algtype;
242 init_sh_desc(desc, HDR_SHARE_SERIAL);
244 /* Append key if it has been set; ahash update excluded */
245 if ((state != OP_ALG_AS_UPDATE) && (ctx->adata.keylen)) {
246 /* Skip key loading if already shared */
247 skip_key_load = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
251 append_key_as_imm(desc, ctx->key, ctx->adata.keylen_pad,
252 ctx->adata.keylen, CLASS_2 |
253 KEY_DEST_MDHA_SPLIT | KEY_ENC);
255 append_proto_dkp(desc, &ctx->adata);
257 set_jump_tgt_here(desc, skip_key_load);
259 op |= OP_ALG_AAI_HMAC_PRECOMP;
262 /* If needed, import context from software */
264 append_seq_load(desc, ctx->ctx_len, LDST_CLASS_2_CCB |
265 LDST_SRCDST_BYTE_CONTEXT);
267 /* Class 2 operation */
268 append_operation(desc, op | state | OP_ALG_ENCRYPT);
271 * Load from buf and/or src and write to req->result or state->context
272 * Calculate remaining bytes to read
274 append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
275 /* Read remaining bytes */
276 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 |
277 FIFOLD_TYPE_MSG | KEY_VLF);
278 /* Store class2 context bytes */
279 append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
280 LDST_SRCDST_BYTE_CONTEXT);
283 static int ahash_set_sh_desc(struct crypto_ahash *ahash)
285 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
286 int digestsize = crypto_ahash_digestsize(ahash);
287 struct device *jrdev = ctx->jrdev;
288 struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
291 ctx->adata.key_virt = ctx->key;
293 /* ahash_update shared descriptor */
294 desc = ctx->sh_desc_update;
295 ahash_gen_sh_desc(desc, OP_ALG_AS_UPDATE, ctx->ctx_len, ctx, true,
297 dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
298 desc_bytes(desc), ctx->dir);
300 print_hex_dump(KERN_ERR,
301 "ahash update shdesc@"__stringify(__LINE__)": ",
302 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
305 /* ahash_update_first shared descriptor */
306 desc = ctx->sh_desc_update_first;
307 ahash_gen_sh_desc(desc, OP_ALG_AS_INIT, ctx->ctx_len, ctx, false,
309 dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
310 desc_bytes(desc), ctx->dir);
312 print_hex_dump(KERN_ERR,
313 "ahash update first shdesc@"__stringify(__LINE__)": ",
314 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
317 /* ahash_final shared descriptor */
318 desc = ctx->sh_desc_fin;
319 ahash_gen_sh_desc(desc, OP_ALG_AS_FINALIZE, digestsize, ctx, true,
321 dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
322 desc_bytes(desc), ctx->dir);
324 print_hex_dump(KERN_ERR, "ahash final shdesc@"__stringify(__LINE__)": ",
325 DUMP_PREFIX_ADDRESS, 16, 4, desc,
326 desc_bytes(desc), 1);
329 /* ahash_digest shared descriptor */
330 desc = ctx->sh_desc_digest;
331 ahash_gen_sh_desc(desc, OP_ALG_AS_INITFINAL, digestsize, ctx, false,
333 dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
334 desc_bytes(desc), ctx->dir);
336 print_hex_dump(KERN_ERR,
337 "ahash digest shdesc@"__stringify(__LINE__)": ",
338 DUMP_PREFIX_ADDRESS, 16, 4, desc,
339 desc_bytes(desc), 1);
345 /* Digest hash size if it is too large */
346 static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
347 u32 *keylen, u8 *key_out, u32 digestsize)
349 struct device *jrdev = ctx->jrdev;
351 struct split_key_result result;
352 dma_addr_t src_dma, dst_dma;
355 desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
357 dev_err(jrdev, "unable to allocate key input memory\n");
361 init_job_desc(desc, 0);
363 src_dma = dma_map_single(jrdev, (void *)key_in, *keylen,
365 if (dma_mapping_error(jrdev, src_dma)) {
366 dev_err(jrdev, "unable to map key input memory\n");
370 dst_dma = dma_map_single(jrdev, (void *)key_out, digestsize,
372 if (dma_mapping_error(jrdev, dst_dma)) {
373 dev_err(jrdev, "unable to map key output memory\n");
374 dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE);
379 /* Job descriptor to perform unkeyed hash on key_in */
380 append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
381 OP_ALG_AS_INITFINAL);
382 append_seq_in_ptr(desc, src_dma, *keylen, 0);
383 append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
384 FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
385 append_seq_out_ptr(desc, dst_dma, digestsize, 0);
386 append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
387 LDST_SRCDST_BYTE_CONTEXT);
390 print_hex_dump(KERN_ERR, "key_in@"__stringify(__LINE__)": ",
391 DUMP_PREFIX_ADDRESS, 16, 4, key_in, *keylen, 1);
392 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
393 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
397 init_completion(&result.completion);
399 ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
402 wait_for_completion(&result.completion);
405 print_hex_dump(KERN_ERR,
406 "digested key@"__stringify(__LINE__)": ",
407 DUMP_PREFIX_ADDRESS, 16, 4, key_in,
411 dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE);
412 dma_unmap_single(jrdev, dst_dma, digestsize, DMA_FROM_DEVICE);
414 *keylen = digestsize;
421 static int ahash_setkey(struct crypto_ahash *ahash,
422 const u8 *key, unsigned int keylen)
424 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
425 int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
426 int digestsize = crypto_ahash_digestsize(ahash);
427 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
429 u8 *hashed_key = NULL;
432 printk(KERN_ERR "keylen %d\n", keylen);
435 if (keylen > blocksize) {
436 hashed_key = kmalloc_array(digestsize,
438 GFP_KERNEL | GFP_DMA);
441 ret = hash_digest_key(ctx, key, &keylen, hashed_key,
449 * If DKP is supported, use it in the shared descriptor to generate
452 if (ctrlpriv->era >= 6) {
453 ctx->adata.key_inline = true;
454 ctx->adata.keylen = keylen;
455 ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
458 if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
461 memcpy(ctx->key, key, keylen);
463 ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key,
464 keylen, CAAM_MAX_HASH_KEY_SIZE);
470 return ahash_set_sh_desc(ahash);
473 crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
478 * ahash_edesc - s/w-extended ahash descriptor
479 * @sec4_sg_dma: physical mapped address of h/w link table
480 * @src_nents: number of segments in input scatterlist
481 * @sec4_sg_bytes: length of dma mapped sec4_sg space
482 * @hw_desc: the h/w job descriptor followed by any referenced link tables
483 * @sec4_sg: h/w link table
486 dma_addr_t sec4_sg_dma;
489 u32 hw_desc[DESC_JOB_IO_LEN / sizeof(u32)] ____cacheline_aligned;
490 struct sec4_sg_entry sec4_sg[0];
493 static inline void ahash_unmap(struct device *dev,
494 struct ahash_edesc *edesc,
495 struct ahash_request *req, int dst_len)
497 struct caam_hash_state *state = ahash_request_ctx(req);
499 if (edesc->src_nents)
500 dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
502 if (edesc->sec4_sg_bytes)
503 dma_unmap_single(dev, edesc->sec4_sg_dma,
504 edesc->sec4_sg_bytes, DMA_TO_DEVICE);
506 if (state->buf_dma) {
507 dma_unmap_single(dev, state->buf_dma, *current_buflen(state),
513 static inline void ahash_unmap_ctx(struct device *dev,
514 struct ahash_edesc *edesc,
515 struct ahash_request *req, int dst_len, u32 flag)
517 struct caam_hash_state *state = ahash_request_ctx(req);
519 if (state->ctx_dma) {
520 dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
523 ahash_unmap(dev, edesc, req, dst_len);
526 static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
529 struct ahash_request *req = context;
530 struct ahash_edesc *edesc;
531 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
532 int digestsize = crypto_ahash_digestsize(ahash);
533 struct caam_hash_state *state = ahash_request_ctx(req);
535 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
537 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
540 edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
542 caam_jr_strstatus(jrdev, err);
544 ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
545 memcpy(req->result, state->caam_ctx, digestsize);
549 print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
550 DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
554 req->base.complete(&req->base, err);
557 static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
560 struct ahash_request *req = context;
561 struct ahash_edesc *edesc;
562 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
563 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
564 struct caam_hash_state *state = ahash_request_ctx(req);
566 int digestsize = crypto_ahash_digestsize(ahash);
568 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
571 edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
573 caam_jr_strstatus(jrdev, err);
575 ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
580 print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
581 DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
584 print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
585 DUMP_PREFIX_ADDRESS, 16, 4, req->result,
589 req->base.complete(&req->base, err);
592 static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
595 struct ahash_request *req = context;
596 struct ahash_edesc *edesc;
597 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
598 int digestsize = crypto_ahash_digestsize(ahash);
599 struct caam_hash_state *state = ahash_request_ctx(req);
601 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
603 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
606 edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
608 caam_jr_strstatus(jrdev, err);
610 ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
611 memcpy(req->result, state->caam_ctx, digestsize);
615 print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
616 DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
620 req->base.complete(&req->base, err);
623 static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
626 struct ahash_request *req = context;
627 struct ahash_edesc *edesc;
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);
632 int digestsize = crypto_ahash_digestsize(ahash);
634 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
637 edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
639 caam_jr_strstatus(jrdev, err);
641 ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_FROM_DEVICE);
646 print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
647 DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
650 print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
651 DUMP_PREFIX_ADDRESS, 16, 4, req->result,
655 req->base.complete(&req->base, err);
659 * Allocate an enhanced descriptor, which contains the hardware descriptor
660 * and space for hardware scatter table containing sg_num entries.
662 static struct ahash_edesc *ahash_edesc_alloc(struct caam_hash_ctx *ctx,
663 int sg_num, u32 *sh_desc,
664 dma_addr_t sh_desc_dma,
667 struct ahash_edesc *edesc;
668 unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
670 edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
672 dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
676 init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
677 HDR_SHARE_DEFER | HDR_REVERSE);
682 static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
683 struct ahash_edesc *edesc,
684 struct ahash_request *req, int nents,
685 unsigned int first_sg,
686 unsigned int first_bytes, size_t to_hash)
691 if (nents > 1 || first_sg) {
692 struct sec4_sg_entry *sg = edesc->sec4_sg;
693 unsigned int sgsize = sizeof(*sg) * (first_sg + nents);
695 sg_to_sec4_sg_last(req->src, nents, sg + first_sg, 0);
697 src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE);
698 if (dma_mapping_error(ctx->jrdev, src_dma)) {
699 dev_err(ctx->jrdev, "unable to map S/G table\n");
703 edesc->sec4_sg_bytes = sgsize;
704 edesc->sec4_sg_dma = src_dma;
707 src_dma = sg_dma_address(req->src);
711 append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash,
717 /* submit update job descriptor */
718 static int ahash_update_ctx(struct ahash_request *req)
720 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
721 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
722 struct caam_hash_state *state = ahash_request_ctx(req);
723 struct device *jrdev = ctx->jrdev;
724 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
725 GFP_KERNEL : GFP_ATOMIC;
726 u8 *buf = current_buf(state);
727 int *buflen = current_buflen(state);
728 u8 *next_buf = alt_buf(state);
729 int *next_buflen = alt_buflen(state), last_buflen;
730 int in_len = *buflen + req->nbytes, to_hash;
732 int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
733 struct ahash_edesc *edesc;
736 last_buflen = *next_buflen;
737 *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
738 to_hash = in_len - *next_buflen;
741 src_nents = sg_nents_for_len(req->src,
742 req->nbytes - (*next_buflen));
744 dev_err(jrdev, "Invalid number of src SG.\n");
749 mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
752 dev_err(jrdev, "unable to DMA map source\n");
759 sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
760 sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
761 sizeof(struct sec4_sg_entry);
764 * allocate space for base edesc and hw desc commands,
767 edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
769 ctx->sh_desc_update_dma, flags);
771 dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
775 edesc->src_nents = src_nents;
776 edesc->sec4_sg_bytes = sec4_sg_bytes;
778 ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
779 edesc->sec4_sg, DMA_BIDIRECTIONAL);
783 ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
788 sg_to_sec4_sg_last(req->src, mapped_nents,
789 edesc->sec4_sg + sec4_sg_src_index,
792 scatterwalk_map_and_copy(next_buf, req->src,
796 sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index -
800 desc = edesc->hw_desc;
802 edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
805 if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
806 dev_err(jrdev, "unable to map S/G table\n");
811 append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
814 append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
817 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
818 DUMP_PREFIX_ADDRESS, 16, 4, desc,
819 desc_bytes(desc), 1);
822 ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req);
827 } else if (*next_buflen) {
828 scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
830 *buflen = *next_buflen;
831 *next_buflen = last_buflen;
834 print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
835 DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
836 print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
837 DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
843 ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
848 static int ahash_final_ctx(struct ahash_request *req)
850 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
851 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
852 struct caam_hash_state *state = ahash_request_ctx(req);
853 struct device *jrdev = ctx->jrdev;
854 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
855 GFP_KERNEL : GFP_ATOMIC;
856 int buflen = *current_buflen(state);
858 int sec4_sg_bytes, sec4_sg_src_index;
859 int digestsize = crypto_ahash_digestsize(ahash);
860 struct ahash_edesc *edesc;
863 sec4_sg_src_index = 1 + (buflen ? 1 : 0);
864 sec4_sg_bytes = sec4_sg_src_index * sizeof(struct sec4_sg_entry);
866 /* allocate space for base edesc and hw desc commands, link tables */
867 edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index,
868 ctx->sh_desc_fin, ctx->sh_desc_fin_dma,
873 desc = edesc->hw_desc;
875 edesc->sec4_sg_bytes = sec4_sg_bytes;
877 ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
878 edesc->sec4_sg, DMA_BIDIRECTIONAL);
882 ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
886 sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index - 1);
888 edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
889 sec4_sg_bytes, DMA_TO_DEVICE);
890 if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
891 dev_err(jrdev, "unable to map S/G table\n");
896 append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
898 append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
901 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
902 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
905 ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
911 ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
916 static int ahash_finup_ctx(struct ahash_request *req)
918 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
919 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
920 struct caam_hash_state *state = ahash_request_ctx(req);
921 struct device *jrdev = ctx->jrdev;
922 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
923 GFP_KERNEL : GFP_ATOMIC;
924 int buflen = *current_buflen(state);
926 int sec4_sg_src_index;
927 int src_nents, mapped_nents;
928 int digestsize = crypto_ahash_digestsize(ahash);
929 struct ahash_edesc *edesc;
932 src_nents = sg_nents_for_len(req->src, req->nbytes);
934 dev_err(jrdev, "Invalid number of src SG.\n");
939 mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
942 dev_err(jrdev, "unable to DMA map source\n");
949 sec4_sg_src_index = 1 + (buflen ? 1 : 0);
951 /* allocate space for base edesc and hw desc commands, link tables */
952 edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
953 ctx->sh_desc_fin, ctx->sh_desc_fin_dma,
956 dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
960 desc = edesc->hw_desc;
962 edesc->src_nents = src_nents;
964 ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
965 edesc->sec4_sg, DMA_BIDIRECTIONAL);
969 ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
973 ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents,
974 sec4_sg_src_index, ctx->ctx_len + buflen,
979 append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
982 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
983 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
986 ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
992 ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
997 static int ahash_digest(struct ahash_request *req)
999 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1000 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1001 struct caam_hash_state *state = ahash_request_ctx(req);
1002 struct device *jrdev = ctx->jrdev;
1003 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1004 GFP_KERNEL : GFP_ATOMIC;
1006 int digestsize = crypto_ahash_digestsize(ahash);
1007 int src_nents, mapped_nents;
1008 struct ahash_edesc *edesc;
1013 src_nents = sg_nents_for_len(req->src, req->nbytes);
1014 if (src_nents < 0) {
1015 dev_err(jrdev, "Invalid number of src SG.\n");
1020 mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1022 if (!mapped_nents) {
1023 dev_err(jrdev, "unable to map source for DMA\n");
1030 /* allocate space for base edesc and hw desc commands, link tables */
1031 edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ? mapped_nents : 0,
1032 ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
1035 dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1039 edesc->src_nents = src_nents;
1041 ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
1044 ahash_unmap(jrdev, edesc, req, digestsize);
1049 desc = edesc->hw_desc;
1051 ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1053 ahash_unmap(jrdev, edesc, req, digestsize);
1059 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1060 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1063 ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1067 ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1074 /* submit ahash final if it the first job descriptor */
1075 static int ahash_final_no_ctx(struct ahash_request *req)
1077 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1078 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1079 struct caam_hash_state *state = ahash_request_ctx(req);
1080 struct device *jrdev = ctx->jrdev;
1081 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1082 GFP_KERNEL : GFP_ATOMIC;
1083 u8 *buf = current_buf(state);
1084 int buflen = *current_buflen(state);
1086 int digestsize = crypto_ahash_digestsize(ahash);
1087 struct ahash_edesc *edesc;
1090 /* allocate space for base edesc and hw desc commands, link tables */
1091 edesc = ahash_edesc_alloc(ctx, 0, ctx->sh_desc_digest,
1092 ctx->sh_desc_digest_dma, flags);
1096 desc = edesc->hw_desc;
1099 state->buf_dma = dma_map_single(jrdev, buf, buflen,
1101 if (dma_mapping_error(jrdev, state->buf_dma)) {
1102 dev_err(jrdev, "unable to map src\n");
1106 append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
1109 ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1114 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1115 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1118 ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1122 ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1128 ahash_unmap(jrdev, edesc, req, digestsize);
1134 /* submit ahash update if it the first job descriptor after update */
1135 static int ahash_update_no_ctx(struct ahash_request *req)
1137 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1138 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1139 struct caam_hash_state *state = ahash_request_ctx(req);
1140 struct device *jrdev = ctx->jrdev;
1141 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1142 GFP_KERNEL : GFP_ATOMIC;
1143 u8 *buf = current_buf(state);
1144 int *buflen = current_buflen(state);
1145 u8 *next_buf = alt_buf(state);
1146 int *next_buflen = alt_buflen(state);
1147 int in_len = *buflen + req->nbytes, to_hash;
1148 int sec4_sg_bytes, src_nents, mapped_nents;
1149 struct ahash_edesc *edesc;
1153 *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
1154 to_hash = in_len - *next_buflen;
1157 src_nents = sg_nents_for_len(req->src,
1158 req->nbytes - *next_buflen);
1159 if (src_nents < 0) {
1160 dev_err(jrdev, "Invalid number of src SG.\n");
1165 mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1167 if (!mapped_nents) {
1168 dev_err(jrdev, "unable to DMA map source\n");
1175 sec4_sg_bytes = (1 + mapped_nents) *
1176 sizeof(struct sec4_sg_entry);
1179 * allocate space for base edesc and hw desc commands,
1182 edesc = ahash_edesc_alloc(ctx, 1 + mapped_nents,
1183 ctx->sh_desc_update_first,
1184 ctx->sh_desc_update_first_dma,
1187 dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1191 edesc->src_nents = src_nents;
1192 edesc->sec4_sg_bytes = sec4_sg_bytes;
1194 ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1198 sg_to_sec4_sg_last(req->src, mapped_nents,
1199 edesc->sec4_sg + 1, 0);
1202 scatterwalk_map_and_copy(next_buf, req->src,
1207 desc = edesc->hw_desc;
1209 edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
1212 if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
1213 dev_err(jrdev, "unable to map S/G table\n");
1218 append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
1220 ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1225 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1226 DUMP_PREFIX_ADDRESS, 16, 4, desc,
1227 desc_bytes(desc), 1);
1230 ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
1235 state->update = ahash_update_ctx;
1236 state->finup = ahash_finup_ctx;
1237 state->final = ahash_final_ctx;
1238 } else if (*next_buflen) {
1239 scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
1241 *buflen = *next_buflen;
1245 print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
1246 DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
1247 print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
1248 DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
1254 ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
1259 /* submit ahash finup if it the first job descriptor after update */
1260 static int ahash_finup_no_ctx(struct ahash_request *req)
1262 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1263 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1264 struct caam_hash_state *state = ahash_request_ctx(req);
1265 struct device *jrdev = ctx->jrdev;
1266 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1267 GFP_KERNEL : GFP_ATOMIC;
1268 int buflen = *current_buflen(state);
1270 int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
1271 int digestsize = crypto_ahash_digestsize(ahash);
1272 struct ahash_edesc *edesc;
1275 src_nents = sg_nents_for_len(req->src, req->nbytes);
1276 if (src_nents < 0) {
1277 dev_err(jrdev, "Invalid number of src SG.\n");
1282 mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1284 if (!mapped_nents) {
1285 dev_err(jrdev, "unable to DMA map source\n");
1292 sec4_sg_src_index = 2;
1293 sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
1294 sizeof(struct sec4_sg_entry);
1296 /* allocate space for base edesc and hw desc commands, link tables */
1297 edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
1298 ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
1301 dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1305 desc = edesc->hw_desc;
1307 edesc->src_nents = src_nents;
1308 edesc->sec4_sg_bytes = sec4_sg_bytes;
1310 ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1314 ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen,
1317 dev_err(jrdev, "unable to map S/G table\n");
1321 ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1326 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1327 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1330 ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1334 ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1340 ahash_unmap(jrdev, edesc, req, digestsize);
1346 /* submit first update job descriptor after init */
1347 static int ahash_update_first(struct ahash_request *req)
1349 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1350 struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1351 struct caam_hash_state *state = ahash_request_ctx(req);
1352 struct device *jrdev = ctx->jrdev;
1353 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1354 GFP_KERNEL : GFP_ATOMIC;
1355 u8 *next_buf = alt_buf(state);
1356 int *next_buflen = alt_buflen(state);
1359 int src_nents, mapped_nents;
1360 struct ahash_edesc *edesc;
1363 *next_buflen = req->nbytes & (crypto_tfm_alg_blocksize(&ahash->base) -
1365 to_hash = req->nbytes - *next_buflen;
1368 src_nents = sg_nents_for_len(req->src,
1369 req->nbytes - *next_buflen);
1370 if (src_nents < 0) {
1371 dev_err(jrdev, "Invalid number of src SG.\n");
1376 mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1378 if (!mapped_nents) {
1379 dev_err(jrdev, "unable to map source for DMA\n");
1387 * allocate space for base edesc and hw desc commands,
1390 edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ?
1392 ctx->sh_desc_update_first,
1393 ctx->sh_desc_update_first_dma,
1396 dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1400 edesc->src_nents = src_nents;
1402 ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
1408 scatterwalk_map_and_copy(next_buf, req->src, to_hash,
1411 desc = edesc->hw_desc;
1413 ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1418 print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1419 DUMP_PREFIX_ADDRESS, 16, 4, desc,
1420 desc_bytes(desc), 1);
1423 ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
1428 state->update = ahash_update_ctx;
1429 state->finup = ahash_finup_ctx;
1430 state->final = ahash_final_ctx;
1431 } else if (*next_buflen) {
1432 state->update = ahash_update_no_ctx;
1433 state->finup = ahash_finup_no_ctx;
1434 state->final = ahash_final_no_ctx;
1435 scatterwalk_map_and_copy(next_buf, req->src, 0,
1440 print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
1441 DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
1447 ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
1452 static int ahash_finup_first(struct ahash_request *req)
1454 return ahash_digest(req);
1457 static int ahash_init(struct ahash_request *req)
1459 struct caam_hash_state *state = ahash_request_ctx(req);
1461 state->update = ahash_update_first;
1462 state->finup = ahash_finup_first;
1463 state->final = ahash_final_no_ctx;
1466 state->ctx_dma_len = 0;
1467 state->current_buf = 0;
1469 state->buflen_0 = 0;
1470 state->buflen_1 = 0;
1475 static int ahash_update(struct ahash_request *req)
1477 struct caam_hash_state *state = ahash_request_ctx(req);
1479 return state->update(req);
1482 static int ahash_finup(struct ahash_request *req)
1484 struct caam_hash_state *state = ahash_request_ctx(req);
1486 return state->finup(req);
1489 static int ahash_final(struct ahash_request *req)
1491 struct caam_hash_state *state = ahash_request_ctx(req);
1493 return state->final(req);
1496 static int ahash_export(struct ahash_request *req, void *out)
1498 struct caam_hash_state *state = ahash_request_ctx(req);
1499 struct caam_export_state *export = out;
1503 if (state->current_buf) {
1505 len = state->buflen_1;
1508 len = state->buflen_0;
1511 memcpy(export->buf, buf, len);
1512 memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
1513 export->buflen = len;
1514 export->update = state->update;
1515 export->final = state->final;
1516 export->finup = state->finup;
1521 static int ahash_import(struct ahash_request *req, const void *in)
1523 struct caam_hash_state *state = ahash_request_ctx(req);
1524 const struct caam_export_state *export = in;
1526 memset(state, 0, sizeof(*state));
1527 memcpy(state->buf_0, export->buf, export->buflen);
1528 memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
1529 state->buflen_0 = export->buflen;
1530 state->update = export->update;
1531 state->final = export->final;
1532 state->finup = export->finup;
1537 struct caam_hash_template {
1538 char name[CRYPTO_MAX_ALG_NAME];
1539 char driver_name[CRYPTO_MAX_ALG_NAME];
1540 char hmac_name[CRYPTO_MAX_ALG_NAME];
1541 char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
1542 unsigned int blocksize;
1543 struct ahash_alg template_ahash;
1547 /* ahash descriptors */
1548 static struct caam_hash_template driver_hash[] = {
1551 .driver_name = "sha1-caam",
1552 .hmac_name = "hmac(sha1)",
1553 .hmac_driver_name = "hmac-sha1-caam",
1554 .blocksize = SHA1_BLOCK_SIZE,
1557 .update = ahash_update,
1558 .final = ahash_final,
1559 .finup = ahash_finup,
1560 .digest = ahash_digest,
1561 .export = ahash_export,
1562 .import = ahash_import,
1563 .setkey = ahash_setkey,
1565 .digestsize = SHA1_DIGEST_SIZE,
1566 .statesize = sizeof(struct caam_export_state),
1569 .alg_type = OP_ALG_ALGSEL_SHA1,
1572 .driver_name = "sha224-caam",
1573 .hmac_name = "hmac(sha224)",
1574 .hmac_driver_name = "hmac-sha224-caam",
1575 .blocksize = SHA224_BLOCK_SIZE,
1578 .update = ahash_update,
1579 .final = ahash_final,
1580 .finup = ahash_finup,
1581 .digest = ahash_digest,
1582 .export = ahash_export,
1583 .import = ahash_import,
1584 .setkey = ahash_setkey,
1586 .digestsize = SHA224_DIGEST_SIZE,
1587 .statesize = sizeof(struct caam_export_state),
1590 .alg_type = OP_ALG_ALGSEL_SHA224,
1593 .driver_name = "sha256-caam",
1594 .hmac_name = "hmac(sha256)",
1595 .hmac_driver_name = "hmac-sha256-caam",
1596 .blocksize = SHA256_BLOCK_SIZE,
1599 .update = ahash_update,
1600 .final = ahash_final,
1601 .finup = ahash_finup,
1602 .digest = ahash_digest,
1603 .export = ahash_export,
1604 .import = ahash_import,
1605 .setkey = ahash_setkey,
1607 .digestsize = SHA256_DIGEST_SIZE,
1608 .statesize = sizeof(struct caam_export_state),
1611 .alg_type = OP_ALG_ALGSEL_SHA256,
1614 .driver_name = "sha384-caam",
1615 .hmac_name = "hmac(sha384)",
1616 .hmac_driver_name = "hmac-sha384-caam",
1617 .blocksize = SHA384_BLOCK_SIZE,
1620 .update = ahash_update,
1621 .final = ahash_final,
1622 .finup = ahash_finup,
1623 .digest = ahash_digest,
1624 .export = ahash_export,
1625 .import = ahash_import,
1626 .setkey = ahash_setkey,
1628 .digestsize = SHA384_DIGEST_SIZE,
1629 .statesize = sizeof(struct caam_export_state),
1632 .alg_type = OP_ALG_ALGSEL_SHA384,
1635 .driver_name = "sha512-caam",
1636 .hmac_name = "hmac(sha512)",
1637 .hmac_driver_name = "hmac-sha512-caam",
1638 .blocksize = SHA512_BLOCK_SIZE,
1641 .update = ahash_update,
1642 .final = ahash_final,
1643 .finup = ahash_finup,
1644 .digest = ahash_digest,
1645 .export = ahash_export,
1646 .import = ahash_import,
1647 .setkey = ahash_setkey,
1649 .digestsize = SHA512_DIGEST_SIZE,
1650 .statesize = sizeof(struct caam_export_state),
1653 .alg_type = OP_ALG_ALGSEL_SHA512,
1656 .driver_name = "md5-caam",
1657 .hmac_name = "hmac(md5)",
1658 .hmac_driver_name = "hmac-md5-caam",
1659 .blocksize = MD5_BLOCK_WORDS * 4,
1662 .update = ahash_update,
1663 .final = ahash_final,
1664 .finup = ahash_finup,
1665 .digest = ahash_digest,
1666 .export = ahash_export,
1667 .import = ahash_import,
1668 .setkey = ahash_setkey,
1670 .digestsize = MD5_DIGEST_SIZE,
1671 .statesize = sizeof(struct caam_export_state),
1674 .alg_type = OP_ALG_ALGSEL_MD5,
1678 struct caam_hash_alg {
1679 struct list_head entry;
1681 struct ahash_alg ahash_alg;
1684 static int caam_hash_cra_init(struct crypto_tfm *tfm)
1686 struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
1687 struct crypto_alg *base = tfm->__crt_alg;
1688 struct hash_alg_common *halg =
1689 container_of(base, struct hash_alg_common, base);
1690 struct ahash_alg *alg =
1691 container_of(halg, struct ahash_alg, halg);
1692 struct caam_hash_alg *caam_hash =
1693 container_of(alg, struct caam_hash_alg, ahash_alg);
1694 struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1695 /* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
1696 static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
1697 HASH_MSG_LEN + SHA1_DIGEST_SIZE,
1699 HASH_MSG_LEN + SHA256_DIGEST_SIZE,
1701 HASH_MSG_LEN + SHA512_DIGEST_SIZE };
1702 dma_addr_t dma_addr;
1703 struct caam_drv_private *priv;
1706 * Get a Job ring from Job Ring driver to ensure in-order
1707 * crypto request processing per tfm
1709 ctx->jrdev = caam_jr_alloc();
1710 if (IS_ERR(ctx->jrdev)) {
1711 pr_err("Job Ring Device allocation for transform failed\n");
1712 return PTR_ERR(ctx->jrdev);
1715 priv = dev_get_drvdata(ctx->jrdev->parent);
1716 ctx->dir = priv->era >= 6 ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
1718 dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_update,
1719 offsetof(struct caam_hash_ctx,
1720 sh_desc_update_dma),
1721 ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1722 if (dma_mapping_error(ctx->jrdev, dma_addr)) {
1723 dev_err(ctx->jrdev, "unable to map shared descriptors\n");
1724 caam_jr_free(ctx->jrdev);
1728 ctx->sh_desc_update_dma = dma_addr;
1729 ctx->sh_desc_update_first_dma = dma_addr +
1730 offsetof(struct caam_hash_ctx,
1731 sh_desc_update_first);
1732 ctx->sh_desc_fin_dma = dma_addr + offsetof(struct caam_hash_ctx,
1734 ctx->sh_desc_digest_dma = dma_addr + offsetof(struct caam_hash_ctx,
1737 /* copy descriptor header template value */
1738 ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
1740 ctx->ctx_len = runninglen[(ctx->adata.algtype &
1741 OP_ALG_ALGSEL_SUBMASK) >>
1742 OP_ALG_ALGSEL_SHIFT];
1744 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1745 sizeof(struct caam_hash_state));
1746 return ahash_set_sh_desc(ahash);
1749 static void caam_hash_cra_exit(struct crypto_tfm *tfm)
1751 struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1753 dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_update_dma,
1754 offsetof(struct caam_hash_ctx,
1755 sh_desc_update_dma),
1756 ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1757 caam_jr_free(ctx->jrdev);
1760 static void __exit caam_algapi_hash_exit(void)
1762 struct caam_hash_alg *t_alg, *n;
1764 if (!hash_list.next)
1767 list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
1768 crypto_unregister_ahash(&t_alg->ahash_alg);
1769 list_del(&t_alg->entry);
1774 static struct caam_hash_alg *
1775 caam_hash_alloc(struct caam_hash_template *template,
1778 struct caam_hash_alg *t_alg;
1779 struct ahash_alg *halg;
1780 struct crypto_alg *alg;
1782 t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
1784 pr_err("failed to allocate t_alg\n");
1785 return ERR_PTR(-ENOMEM);
1788 t_alg->ahash_alg = template->template_ahash;
1789 halg = &t_alg->ahash_alg;
1790 alg = &halg->halg.base;
1793 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1794 template->hmac_name);
1795 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1796 template->hmac_driver_name);
1798 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1800 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1801 template->driver_name);
1802 t_alg->ahash_alg.setkey = NULL;
1804 alg->cra_module = THIS_MODULE;
1805 alg->cra_init = caam_hash_cra_init;
1806 alg->cra_exit = caam_hash_cra_exit;
1807 alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
1808 alg->cra_priority = CAAM_CRA_PRIORITY;
1809 alg->cra_blocksize = template->blocksize;
1810 alg->cra_alignmask = 0;
1811 alg->cra_flags = CRYPTO_ALG_ASYNC;
1813 t_alg->alg_type = template->alg_type;
1818 static int __init caam_algapi_hash_init(void)
1820 struct device_node *dev_node;
1821 struct platform_device *pdev;
1822 struct device *ctrldev;
1824 struct caam_drv_private *priv;
1825 unsigned int md_limit = SHA512_DIGEST_SIZE;
1826 u32 cha_inst, cha_vid;
1828 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
1830 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
1835 pdev = of_find_device_by_node(dev_node);
1837 of_node_put(dev_node);
1841 ctrldev = &pdev->dev;
1842 priv = dev_get_drvdata(ctrldev);
1843 of_node_put(dev_node);
1846 * If priv is NULL, it's probably because the caam driver wasn't
1847 * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
1853 * Register crypto algorithms the device supports. First, identify
1854 * presence and attributes of MD block.
1856 cha_vid = rd_reg32(&priv->ctrl->perfmon.cha_id_ls);
1857 cha_inst = rd_reg32(&priv->ctrl->perfmon.cha_num_ls);
1860 * Skip registration of any hashing algorithms if MD block
1863 if (!((cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT))
1866 /* Limit digest size based on LP256 */
1867 if ((cha_vid & CHA_ID_LS_MD_MASK) == CHA_ID_LS_MD_LP256)
1868 md_limit = SHA256_DIGEST_SIZE;
1870 INIT_LIST_HEAD(&hash_list);
1872 /* register crypto algorithms the device supports */
1873 for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
1874 struct caam_hash_alg *t_alg;
1875 struct caam_hash_template *alg = driver_hash + i;
1877 /* If MD size is not supported by device, skip registration */
1878 if (alg->template_ahash.halg.digestsize > md_limit)
1881 /* register hmac version */
1882 t_alg = caam_hash_alloc(alg, true);
1883 if (IS_ERR(t_alg)) {
1884 err = PTR_ERR(t_alg);
1885 pr_warn("%s alg allocation failed\n", alg->driver_name);
1889 err = crypto_register_ahash(&t_alg->ahash_alg);
1891 pr_warn("%s alg registration failed: %d\n",
1892 t_alg->ahash_alg.halg.base.cra_driver_name,
1896 list_add_tail(&t_alg->entry, &hash_list);
1898 /* register unkeyed version */
1899 t_alg = caam_hash_alloc(alg, false);
1900 if (IS_ERR(t_alg)) {
1901 err = PTR_ERR(t_alg);
1902 pr_warn("%s alg allocation failed\n", alg->driver_name);
1906 err = crypto_register_ahash(&t_alg->ahash_alg);
1908 pr_warn("%s alg registration failed: %d\n",
1909 t_alg->ahash_alg.halg.base.cra_driver_name,
1913 list_add_tail(&t_alg->entry, &hash_list);
1919 module_init(caam_algapi_hash_init);
1920 module_exit(caam_algapi_hash_exit);
1922 MODULE_LICENSE("GPL");
1923 MODULE_DESCRIPTION("FSL CAAM support for ahash functions of crypto API");
1924 MODULE_AUTHOR("Freescale Semiconductor - NMG");