1618b5dc4SHoria Geantă // SPDX-License-Identifier: GPL-2.0+ 2045e3678SYuan Kang /* 3045e3678SYuan Kang * caam - Freescale FSL CAAM support for ahash functions of crypto API 4045e3678SYuan Kang * 5045e3678SYuan Kang * Copyright 2011 Freescale Semiconductor, Inc. 687870cfbSIuliana Prodan * Copyright 2018-2019 NXP 7045e3678SYuan Kang * 8045e3678SYuan Kang * Based on caamalg.c crypto API driver. 9045e3678SYuan Kang * 10045e3678SYuan Kang * relationship of digest job descriptor or first job descriptor after init to 11045e3678SYuan Kang * shared descriptors: 12045e3678SYuan Kang * 13045e3678SYuan Kang * --------------- --------------- 14045e3678SYuan Kang * | JobDesc #1 |-------------------->| ShareDesc | 15045e3678SYuan Kang * | *(packet 1) | | (hashKey) | 16045e3678SYuan Kang * --------------- | (operation) | 17045e3678SYuan Kang * --------------- 18045e3678SYuan Kang * 19045e3678SYuan Kang * relationship of subsequent job descriptors to shared descriptors: 20045e3678SYuan Kang * 21045e3678SYuan Kang * --------------- --------------- 22045e3678SYuan Kang * | JobDesc #2 |-------------------->| ShareDesc | 23045e3678SYuan Kang * | *(packet 2) | |------------->| (hashKey) | 24045e3678SYuan Kang * --------------- | |-------->| (operation) | 25045e3678SYuan Kang * . | | | (load ctx2) | 26045e3678SYuan Kang * . | | --------------- 27045e3678SYuan Kang * --------------- | | 28045e3678SYuan Kang * | JobDesc #3 |------| | 29045e3678SYuan Kang * | *(packet 3) | | 30045e3678SYuan Kang * --------------- | 31045e3678SYuan Kang * . | 32045e3678SYuan Kang * . | 33045e3678SYuan Kang * --------------- | 34045e3678SYuan Kang * | JobDesc #4 |------------ 35045e3678SYuan Kang * | *(packet 4) | 36045e3678SYuan Kang * --------------- 37045e3678SYuan Kang * 38045e3678SYuan Kang * The SharedDesc never changes for a connection unless rekeyed, but 39045e3678SYuan Kang * each packet will likely be in a different place. So all we need 40045e3678SYuan Kang * to know to process the packet is where the input is, where the 41045e3678SYuan Kang * output goes, and what context we want to process with. Context is 42045e3678SYuan Kang * in the SharedDesc, packet references in the JobDesc. 43045e3678SYuan Kang * 44045e3678SYuan Kang * So, a job desc looks like: 45045e3678SYuan Kang * 46045e3678SYuan Kang * --------------------- 47045e3678SYuan Kang * | Header | 48045e3678SYuan Kang * | ShareDesc Pointer | 49045e3678SYuan Kang * | SEQ_OUT_PTR | 50045e3678SYuan Kang * | (output buffer) | 51045e3678SYuan Kang * | (output length) | 52045e3678SYuan Kang * | SEQ_IN_PTR | 53045e3678SYuan Kang * | (input buffer) | 54045e3678SYuan Kang * | (input length) | 55045e3678SYuan Kang * --------------------- 56045e3678SYuan Kang */ 57045e3678SYuan Kang 58045e3678SYuan Kang #include "compat.h" 59045e3678SYuan Kang 60045e3678SYuan Kang #include "regs.h" 61045e3678SYuan Kang #include "intern.h" 62045e3678SYuan Kang #include "desc_constr.h" 63045e3678SYuan Kang #include "jr.h" 64045e3678SYuan Kang #include "error.h" 65045e3678SYuan Kang #include "sg_sw_sec4.h" 66045e3678SYuan Kang #include "key_gen.h" 670efa7579SHoria Geantă #include "caamhash_desc.h" 6821b014f0SIuliana Prodan #include <crypto/engine.h> 69045e3678SYuan Kang 70045e3678SYuan Kang #define CAAM_CRA_PRIORITY 3000 71045e3678SYuan Kang 72045e3678SYuan Kang /* max hash key is max split key size */ 73045e3678SYuan Kang #define CAAM_MAX_HASH_KEY_SIZE (SHA512_DIGEST_SIZE * 2) 74045e3678SYuan Kang 75045e3678SYuan Kang #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE 76045e3678SYuan Kang #define CAAM_MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE 77045e3678SYuan Kang 78045e3678SYuan Kang #define DESC_HASH_MAX_USED_BYTES (DESC_AHASH_FINAL_LEN + \ 79045e3678SYuan Kang CAAM_MAX_HASH_KEY_SIZE) 80045e3678SYuan Kang #define DESC_HASH_MAX_USED_LEN (DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ) 81045e3678SYuan Kang 82045e3678SYuan Kang /* caam context sizes for hashes: running digest + 8 */ 83045e3678SYuan Kang #define HASH_MSG_LEN 8 84045e3678SYuan Kang #define MAX_CTX_LEN (HASH_MSG_LEN + SHA512_DIGEST_SIZE) 85045e3678SYuan Kang 86cfc6f11bSRuchika Gupta static struct list_head hash_list; 87cfc6f11bSRuchika Gupta 88045e3678SYuan Kang /* ahash per-session context */ 89045e3678SYuan Kang struct caam_hash_ctx { 9021b014f0SIuliana Prodan struct crypto_engine_ctx enginectx; 91e11793f5SRussell King u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned; 92e11793f5SRussell King u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned; 93e11793f5SRussell King u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned; 94e11793f5SRussell King u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned; 9512b8567fSIuliana Prodan u8 key[CAAM_MAX_HASH_KEY_SIZE] ____cacheline_aligned; 96e11793f5SRussell King dma_addr_t sh_desc_update_dma ____cacheline_aligned; 97045e3678SYuan Kang dma_addr_t sh_desc_update_first_dma; 98045e3678SYuan Kang dma_addr_t sh_desc_fin_dma; 99045e3678SYuan Kang dma_addr_t sh_desc_digest_dma; 1007e0880b9SHoria Geantă enum dma_data_direction dir; 101e9b4913aSHoria Geantă enum dma_data_direction key_dir; 102e11793f5SRussell King struct device *jrdev; 103045e3678SYuan Kang int ctx_len; 104db57656bSHoria Geantă struct alginfo adata; 105045e3678SYuan Kang }; 106045e3678SYuan Kang 107045e3678SYuan Kang /* ahash state */ 108045e3678SYuan Kang struct caam_hash_state { 109045e3678SYuan Kang dma_addr_t buf_dma; 110045e3678SYuan Kang dma_addr_t ctx_dma; 11165055e21SFranck LENORMAND int ctx_dma_len; 11246b49abcSAndrei Botila u8 buf[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned; 11346b49abcSAndrei Botila int buflen; 11446b49abcSAndrei Botila int next_buflen; 115e7472422SVictoria Milhoan u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned; 11621b014f0SIuliana Prodan int (*update)(struct ahash_request *req) ____cacheline_aligned; 117045e3678SYuan Kang int (*final)(struct ahash_request *req); 118045e3678SYuan Kang int (*finup)(struct ahash_request *req); 11921b014f0SIuliana Prodan struct ahash_edesc *edesc; 12021b014f0SIuliana Prodan void (*ahash_op_done)(struct device *jrdev, u32 *desc, u32 err, 12121b014f0SIuliana Prodan void *context); 122045e3678SYuan Kang }; 123045e3678SYuan Kang 1245ec90831SRussell King struct caam_export_state { 1255ec90831SRussell King u8 buf[CAAM_MAX_HASH_BLOCK_SIZE]; 1265ec90831SRussell King u8 caam_ctx[MAX_CTX_LEN]; 1275ec90831SRussell King int buflen; 1285ec90831SRussell King int (*update)(struct ahash_request *req); 1295ec90831SRussell King int (*final)(struct ahash_request *req); 1305ec90831SRussell King int (*finup)(struct ahash_request *req); 1315ec90831SRussell King }; 1325ec90831SRussell King 13387870cfbSIuliana Prodan static inline bool is_cmac_aes(u32 algtype) 13412b8567fSIuliana Prodan { 13512b8567fSIuliana Prodan return (algtype & (OP_ALG_ALGSEL_MASK | OP_ALG_AAI_MASK)) == 13687870cfbSIuliana Prodan (OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC); 13712b8567fSIuliana Prodan } 138045e3678SYuan Kang /* Common job descriptor seq in/out ptr routines */ 139045e3678SYuan Kang 140045e3678SYuan Kang /* Map state->caam_ctx, and append seq_out_ptr command that points to it */ 141ce572085SHoria Geanta static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev, 142045e3678SYuan Kang struct caam_hash_state *state, 143045e3678SYuan Kang int ctx_len) 144045e3678SYuan Kang { 14565055e21SFranck LENORMAND state->ctx_dma_len = ctx_len; 146045e3678SYuan Kang state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, 147045e3678SYuan Kang ctx_len, DMA_FROM_DEVICE); 148ce572085SHoria Geanta if (dma_mapping_error(jrdev, state->ctx_dma)) { 149ce572085SHoria Geanta dev_err(jrdev, "unable to map ctx\n"); 15087ec02e7SHoria Geantă state->ctx_dma = 0; 151ce572085SHoria Geanta return -ENOMEM; 152ce572085SHoria Geanta } 153ce572085SHoria Geanta 154045e3678SYuan Kang append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0); 155ce572085SHoria Geanta 156ce572085SHoria Geanta return 0; 157045e3678SYuan Kang } 158045e3678SYuan Kang 159944c3d4dSHoria Geantă /* Map current buffer in state (if length > 0) and put it in link table */ 160944c3d4dSHoria Geantă static inline int buf_map_to_sec4_sg(struct device *jrdev, 161045e3678SYuan Kang struct sec4_sg_entry *sec4_sg, 162944c3d4dSHoria Geantă struct caam_hash_state *state) 163045e3678SYuan Kang { 16446b49abcSAndrei Botila int buflen = state->buflen; 165045e3678SYuan Kang 166944c3d4dSHoria Geantă if (!buflen) 167944c3d4dSHoria Geantă return 0; 168045e3678SYuan Kang 16946b49abcSAndrei Botila state->buf_dma = dma_map_single(jrdev, state->buf, buflen, 170944c3d4dSHoria Geantă DMA_TO_DEVICE); 171944c3d4dSHoria Geantă if (dma_mapping_error(jrdev, state->buf_dma)) { 172944c3d4dSHoria Geantă dev_err(jrdev, "unable to map buf\n"); 173944c3d4dSHoria Geantă state->buf_dma = 0; 174944c3d4dSHoria Geantă return -ENOMEM; 175045e3678SYuan Kang } 176045e3678SYuan Kang 177944c3d4dSHoria Geantă dma_to_sec4_sg_one(sec4_sg, state->buf_dma, buflen, 0); 178045e3678SYuan Kang 179944c3d4dSHoria Geantă return 0; 180045e3678SYuan Kang } 181045e3678SYuan Kang 182045e3678SYuan Kang /* Map state->caam_ctx, and add it to link table */ 183dfcd8393SHoria Geantă static inline int ctx_map_to_sec4_sg(struct device *jrdev, 184ce572085SHoria Geanta struct caam_hash_state *state, int ctx_len, 185ce572085SHoria Geanta struct sec4_sg_entry *sec4_sg, u32 flag) 186045e3678SYuan Kang { 18765055e21SFranck LENORMAND state->ctx_dma_len = ctx_len; 188045e3678SYuan Kang state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag); 189ce572085SHoria Geanta if (dma_mapping_error(jrdev, state->ctx_dma)) { 190ce572085SHoria Geanta dev_err(jrdev, "unable to map ctx\n"); 19187ec02e7SHoria Geantă state->ctx_dma = 0; 192ce572085SHoria Geanta return -ENOMEM; 193ce572085SHoria Geanta } 194ce572085SHoria Geanta 195045e3678SYuan Kang dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0); 196ce572085SHoria Geanta 197ce572085SHoria Geanta return 0; 198045e3678SYuan Kang } 199045e3678SYuan Kang 200045e3678SYuan Kang static int ahash_set_sh_desc(struct crypto_ahash *ahash) 201045e3678SYuan Kang { 202045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 203045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 204045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 2057e0880b9SHoria Geantă struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent); 206045e3678SYuan Kang u32 *desc; 207045e3678SYuan Kang 2087e0880b9SHoria Geantă ctx->adata.key_virt = ctx->key; 2097e0880b9SHoria Geantă 210045e3678SYuan Kang /* ahash_update shared descriptor */ 211045e3678SYuan Kang desc = ctx->sh_desc_update; 2120efa7579SHoria Geantă cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len, 2130efa7579SHoria Geantă ctx->ctx_len, true, ctrlpriv->era); 214bbf22344SHoria Geantă dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma, 2157e0880b9SHoria Geantă desc_bytes(desc), ctx->dir); 2166e005503SSascha Hauer 2176e005503SSascha Hauer print_hex_dump_debug("ahash update shdesc@"__stringify(__LINE__)": ", 2186e005503SSascha Hauer DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 2196e005503SSascha Hauer 1); 220045e3678SYuan Kang 221045e3678SYuan Kang /* ahash_update_first shared descriptor */ 222045e3678SYuan Kang desc = ctx->sh_desc_update_first; 2230efa7579SHoria Geantă cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len, 2240efa7579SHoria Geantă ctx->ctx_len, false, ctrlpriv->era); 225bbf22344SHoria Geantă dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma, 2267e0880b9SHoria Geantă desc_bytes(desc), ctx->dir); 2276e005503SSascha Hauer print_hex_dump_debug("ahash update first shdesc@"__stringify(__LINE__) 2286e005503SSascha Hauer ": ", DUMP_PREFIX_ADDRESS, 16, 4, desc, 2296e005503SSascha Hauer desc_bytes(desc), 1); 230045e3678SYuan Kang 231045e3678SYuan Kang /* ahash_final shared descriptor */ 232045e3678SYuan Kang desc = ctx->sh_desc_fin; 2330efa7579SHoria Geantă cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize, 2340efa7579SHoria Geantă ctx->ctx_len, true, ctrlpriv->era); 235bbf22344SHoria Geantă dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma, 2367e0880b9SHoria Geantă desc_bytes(desc), ctx->dir); 2376e005503SSascha Hauer 2386e005503SSascha Hauer print_hex_dump_debug("ahash final shdesc@"__stringify(__LINE__)": ", 239045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 240045e3678SYuan Kang desc_bytes(desc), 1); 241045e3678SYuan Kang 242045e3678SYuan Kang /* ahash_digest shared descriptor */ 243045e3678SYuan Kang desc = ctx->sh_desc_digest; 2440efa7579SHoria Geantă cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize, 2450efa7579SHoria Geantă ctx->ctx_len, false, ctrlpriv->era); 246bbf22344SHoria Geantă dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma, 2477e0880b9SHoria Geantă desc_bytes(desc), ctx->dir); 2486e005503SSascha Hauer 2496e005503SSascha Hauer print_hex_dump_debug("ahash digest shdesc@"__stringify(__LINE__)": ", 250045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 251045e3678SYuan Kang desc_bytes(desc), 1); 252045e3678SYuan Kang 253045e3678SYuan Kang return 0; 254045e3678SYuan Kang } 255045e3678SYuan Kang 25612b8567fSIuliana Prodan static int axcbc_set_sh_desc(struct crypto_ahash *ahash) 25712b8567fSIuliana Prodan { 25812b8567fSIuliana Prodan struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 25912b8567fSIuliana Prodan int digestsize = crypto_ahash_digestsize(ahash); 26012b8567fSIuliana Prodan struct device *jrdev = ctx->jrdev; 26112b8567fSIuliana Prodan u32 *desc; 26212b8567fSIuliana Prodan 26312b8567fSIuliana Prodan /* shared descriptor for ahash_update */ 26412b8567fSIuliana Prodan desc = ctx->sh_desc_update; 26587870cfbSIuliana Prodan cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE, 266a2fb864cSHoria Geantă ctx->ctx_len, ctx->ctx_len); 26712b8567fSIuliana Prodan dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma, 26812b8567fSIuliana Prodan desc_bytes(desc), ctx->dir); 26912b8567fSIuliana Prodan print_hex_dump_debug("axcbc update shdesc@" __stringify(__LINE__)" : ", 27012b8567fSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 27112b8567fSIuliana Prodan 1); 27212b8567fSIuliana Prodan 27312b8567fSIuliana Prodan /* shared descriptor for ahash_{final,finup} */ 27412b8567fSIuliana Prodan desc = ctx->sh_desc_fin; 27587870cfbSIuliana Prodan cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, 276a2fb864cSHoria Geantă digestsize, ctx->ctx_len); 27712b8567fSIuliana Prodan dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma, 27812b8567fSIuliana Prodan desc_bytes(desc), ctx->dir); 27912b8567fSIuliana Prodan print_hex_dump_debug("axcbc finup shdesc@" __stringify(__LINE__)" : ", 28012b8567fSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 28112b8567fSIuliana Prodan 1); 28212b8567fSIuliana Prodan 28312b8567fSIuliana Prodan /* key is immediate data for INIT and INITFINAL states */ 28412b8567fSIuliana Prodan ctx->adata.key_virt = ctx->key; 28512b8567fSIuliana Prodan 28612b8567fSIuliana Prodan /* shared descriptor for first invocation of ahash_update */ 28712b8567fSIuliana Prodan desc = ctx->sh_desc_update_first; 28887870cfbSIuliana Prodan cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len, 289a2fb864cSHoria Geantă ctx->ctx_len); 29012b8567fSIuliana Prodan dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma, 29112b8567fSIuliana Prodan desc_bytes(desc), ctx->dir); 2926e005503SSascha Hauer print_hex_dump_debug("axcbc update first shdesc@" __stringify(__LINE__) 2936e005503SSascha Hauer " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc, 2946e005503SSascha Hauer desc_bytes(desc), 1); 29512b8567fSIuliana Prodan 29612b8567fSIuliana Prodan /* shared descriptor for ahash_digest */ 29712b8567fSIuliana Prodan desc = ctx->sh_desc_digest; 29887870cfbSIuliana Prodan cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, 299a2fb864cSHoria Geantă digestsize, ctx->ctx_len); 30012b8567fSIuliana Prodan dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma, 30112b8567fSIuliana Prodan desc_bytes(desc), ctx->dir); 30212b8567fSIuliana Prodan print_hex_dump_debug("axcbc digest shdesc@" __stringify(__LINE__)" : ", 30312b8567fSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 30412b8567fSIuliana Prodan 1); 30587870cfbSIuliana Prodan return 0; 30687870cfbSIuliana Prodan } 30787870cfbSIuliana Prodan 30887870cfbSIuliana Prodan static int acmac_set_sh_desc(struct crypto_ahash *ahash) 30987870cfbSIuliana Prodan { 31087870cfbSIuliana Prodan struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 31187870cfbSIuliana Prodan int digestsize = crypto_ahash_digestsize(ahash); 31287870cfbSIuliana Prodan struct device *jrdev = ctx->jrdev; 31387870cfbSIuliana Prodan u32 *desc; 31487870cfbSIuliana Prodan 31587870cfbSIuliana Prodan /* shared descriptor for ahash_update */ 31687870cfbSIuliana Prodan desc = ctx->sh_desc_update; 31787870cfbSIuliana Prodan cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE, 318a2fb864cSHoria Geantă ctx->ctx_len, ctx->ctx_len); 31987870cfbSIuliana Prodan dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma, 32087870cfbSIuliana Prodan desc_bytes(desc), ctx->dir); 32187870cfbSIuliana Prodan print_hex_dump_debug("acmac update shdesc@" __stringify(__LINE__)" : ", 32287870cfbSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, desc, 32387870cfbSIuliana Prodan desc_bytes(desc), 1); 32487870cfbSIuliana Prodan 32587870cfbSIuliana Prodan /* shared descriptor for ahash_{final,finup} */ 32687870cfbSIuliana Prodan desc = ctx->sh_desc_fin; 32787870cfbSIuliana Prodan cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, 328a2fb864cSHoria Geantă digestsize, ctx->ctx_len); 32987870cfbSIuliana Prodan dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma, 33087870cfbSIuliana Prodan desc_bytes(desc), ctx->dir); 33187870cfbSIuliana Prodan print_hex_dump_debug("acmac finup shdesc@" __stringify(__LINE__)" : ", 33287870cfbSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, desc, 33387870cfbSIuliana Prodan desc_bytes(desc), 1); 33487870cfbSIuliana Prodan 33587870cfbSIuliana Prodan /* shared descriptor for first invocation of ahash_update */ 33687870cfbSIuliana Prodan desc = ctx->sh_desc_update_first; 33787870cfbSIuliana Prodan cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len, 338a2fb864cSHoria Geantă ctx->ctx_len); 33987870cfbSIuliana Prodan dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma, 34087870cfbSIuliana Prodan desc_bytes(desc), ctx->dir); 3416e005503SSascha Hauer print_hex_dump_debug("acmac update first shdesc@" __stringify(__LINE__) 3426e005503SSascha Hauer " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc, 34387870cfbSIuliana Prodan desc_bytes(desc), 1); 34487870cfbSIuliana Prodan 34587870cfbSIuliana Prodan /* shared descriptor for ahash_digest */ 34687870cfbSIuliana Prodan desc = ctx->sh_desc_digest; 34787870cfbSIuliana Prodan cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, 348a2fb864cSHoria Geantă digestsize, ctx->ctx_len); 34987870cfbSIuliana Prodan dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma, 35087870cfbSIuliana Prodan desc_bytes(desc), ctx->dir); 35187870cfbSIuliana Prodan print_hex_dump_debug("acmac digest shdesc@" __stringify(__LINE__)" : ", 35287870cfbSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, desc, 35387870cfbSIuliana Prodan desc_bytes(desc), 1); 35412b8567fSIuliana Prodan 35512b8567fSIuliana Prodan return 0; 35612b8567fSIuliana Prodan } 35712b8567fSIuliana Prodan 358045e3678SYuan Kang /* Digest hash size if it is too large */ 35930724445SHoria Geantă static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key, 36030724445SHoria Geantă u32 digestsize) 361045e3678SYuan Kang { 362045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 363045e3678SYuan Kang u32 *desc; 364045e3678SYuan Kang struct split_key_result result; 36530724445SHoria Geantă dma_addr_t key_dma; 3669e6df0fdSMarkus Elfring int ret; 367045e3678SYuan Kang 3689c23b7d3SVakul Garg desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA); 3692af8f4a2SKim Phillips if (!desc) { 3702af8f4a2SKim Phillips dev_err(jrdev, "unable to allocate key input memory\n"); 3712af8f4a2SKim Phillips return -ENOMEM; 3722af8f4a2SKim Phillips } 373045e3678SYuan Kang 374045e3678SYuan Kang init_job_desc(desc, 0); 375045e3678SYuan Kang 37630724445SHoria Geantă key_dma = dma_map_single(jrdev, key, *keylen, DMA_BIDIRECTIONAL); 37730724445SHoria Geantă if (dma_mapping_error(jrdev, key_dma)) { 37830724445SHoria Geantă dev_err(jrdev, "unable to map key memory\n"); 379045e3678SYuan Kang kfree(desc); 380045e3678SYuan Kang return -ENOMEM; 381045e3678SYuan Kang } 382045e3678SYuan Kang 383045e3678SYuan Kang /* Job descriptor to perform unkeyed hash on key_in */ 384db57656bSHoria Geantă append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT | 385045e3678SYuan Kang OP_ALG_AS_INITFINAL); 38630724445SHoria Geantă append_seq_in_ptr(desc, key_dma, *keylen, 0); 387045e3678SYuan Kang append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 | 388045e3678SYuan Kang FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG); 38930724445SHoria Geantă append_seq_out_ptr(desc, key_dma, digestsize, 0); 390045e3678SYuan Kang append_seq_store(desc, digestsize, LDST_CLASS_2_CCB | 391045e3678SYuan Kang LDST_SRCDST_BYTE_CONTEXT); 392045e3678SYuan Kang 3936e005503SSascha Hauer print_hex_dump_debug("key_in@"__stringify(__LINE__)": ", 39430724445SHoria Geantă DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1); 3956e005503SSascha Hauer print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 3966e005503SSascha Hauer DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 3976e005503SSascha Hauer 1); 398045e3678SYuan Kang 399045e3678SYuan Kang result.err = 0; 400045e3678SYuan Kang init_completion(&result.completion); 401045e3678SYuan Kang 402045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result); 4034d370a10SIuliana Prodan if (ret == -EINPROGRESS) { 404045e3678SYuan Kang /* in progress */ 4057459e1d2SHoria Geantă wait_for_completion(&result.completion); 406045e3678SYuan Kang ret = result.err; 4076e005503SSascha Hauer 4086e005503SSascha Hauer print_hex_dump_debug("digested key@"__stringify(__LINE__)": ", 4096e005503SSascha Hauer DUMP_PREFIX_ADDRESS, 16, 4, key, 4106e005503SSascha Hauer digestsize, 1); 411045e3678SYuan Kang } 41230724445SHoria Geantă dma_unmap_single(jrdev, key_dma, *keylen, DMA_BIDIRECTIONAL); 413045e3678SYuan Kang 414e11aa9f1SHoria Geanta *keylen = digestsize; 415e11aa9f1SHoria Geanta 416045e3678SYuan Kang kfree(desc); 417045e3678SYuan Kang 418045e3678SYuan Kang return ret; 419045e3678SYuan Kang } 420045e3678SYuan Kang 421045e3678SYuan Kang static int ahash_setkey(struct crypto_ahash *ahash, 422045e3678SYuan Kang const u8 *key, unsigned int keylen) 423045e3678SYuan Kang { 424045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 4256e005503SSascha Hauer struct device *jrdev = ctx->jrdev; 426045e3678SYuan Kang int blocksize = crypto_tfm_alg_blocksize(&ahash->base); 427045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 4287e0880b9SHoria Geantă struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent); 4299e6df0fdSMarkus Elfring int ret; 430045e3678SYuan Kang u8 *hashed_key = NULL; 431045e3678SYuan Kang 4326e005503SSascha Hauer dev_dbg(jrdev, "keylen %d\n", keylen); 433045e3678SYuan Kang 434045e3678SYuan Kang if (keylen > blocksize) { 43530724445SHoria Geantă hashed_key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA); 436045e3678SYuan Kang if (!hashed_key) 437045e3678SYuan Kang return -ENOMEM; 43830724445SHoria Geantă ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize); 439045e3678SYuan Kang if (ret) 440d6e7a7d0SMarkus Elfring goto bad_free_key; 441045e3678SYuan Kang key = hashed_key; 442045e3678SYuan Kang } 443045e3678SYuan Kang 4447e0880b9SHoria Geantă /* 4457e0880b9SHoria Geantă * If DKP is supported, use it in the shared descriptor to generate 4467e0880b9SHoria Geantă * the split key. 4477e0880b9SHoria Geantă */ 4487e0880b9SHoria Geantă if (ctrlpriv->era >= 6) { 4497e0880b9SHoria Geantă ctx->adata.key_inline = true; 4507e0880b9SHoria Geantă ctx->adata.keylen = keylen; 4517e0880b9SHoria Geantă ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype & 4527e0880b9SHoria Geantă OP_ALG_ALGSEL_MASK); 4537e0880b9SHoria Geantă 4547e0880b9SHoria Geantă if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE) 455d6e7a7d0SMarkus Elfring goto bad_free_key; 456045e3678SYuan Kang 4577e0880b9SHoria Geantă memcpy(ctx->key, key, keylen); 458e9b4913aSHoria Geantă 459e9b4913aSHoria Geantă /* 460e9b4913aSHoria Geantă * In case |user key| > |derived key|, using DKP<imm,imm> 461e9b4913aSHoria Geantă * would result in invalid opcodes (last bytes of user key) in 462e9b4913aSHoria Geantă * the resulting descriptor. Use DKP<ptr,imm> instead => both 463e9b4913aSHoria Geantă * virtual and dma key addresses are needed. 464e9b4913aSHoria Geantă */ 465e9b4913aSHoria Geantă if (keylen > ctx->adata.keylen_pad) 466e9b4913aSHoria Geantă dma_sync_single_for_device(ctx->jrdev, 467e9b4913aSHoria Geantă ctx->adata.key_dma, 468e9b4913aSHoria Geantă ctx->adata.keylen_pad, 469e9b4913aSHoria Geantă DMA_TO_DEVICE); 4707e0880b9SHoria Geantă } else { 4717e0880b9SHoria Geantă ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key, 4727e0880b9SHoria Geantă keylen, CAAM_MAX_HASH_KEY_SIZE); 4737e0880b9SHoria Geantă if (ret) 4747e0880b9SHoria Geantă goto bad_free_key; 4757e0880b9SHoria Geantă } 476045e3678SYuan Kang 477045e3678SYuan Kang kfree(hashed_key); 478cfb725f6SHoria Geantă return ahash_set_sh_desc(ahash); 479d6e7a7d0SMarkus Elfring bad_free_key: 480045e3678SYuan Kang kfree(hashed_key); 481045e3678SYuan Kang return -EINVAL; 482045e3678SYuan Kang } 483045e3678SYuan Kang 48412b8567fSIuliana Prodan static int axcbc_setkey(struct crypto_ahash *ahash, const u8 *key, 48512b8567fSIuliana Prodan unsigned int keylen) 48612b8567fSIuliana Prodan { 48712b8567fSIuliana Prodan struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 48812b8567fSIuliana Prodan struct device *jrdev = ctx->jrdev; 48912b8567fSIuliana Prodan 490674f368aSEric Biggers if (keylen != AES_KEYSIZE_128) 491836d8f43SIuliana Prodan return -EINVAL; 492836d8f43SIuliana Prodan 49312b8567fSIuliana Prodan memcpy(ctx->key, key, keylen); 494a2fb864cSHoria Geantă dma_sync_single_for_device(jrdev, ctx->adata.key_dma, keylen, 495a2fb864cSHoria Geantă DMA_TO_DEVICE); 49612b8567fSIuliana Prodan ctx->adata.keylen = keylen; 49712b8567fSIuliana Prodan 49812b8567fSIuliana Prodan print_hex_dump_debug("axcbc ctx.key@" __stringify(__LINE__)" : ", 49912b8567fSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, keylen, 1); 50012b8567fSIuliana Prodan 50112b8567fSIuliana Prodan return axcbc_set_sh_desc(ahash); 50212b8567fSIuliana Prodan } 50387870cfbSIuliana Prodan 50487870cfbSIuliana Prodan static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key, 50587870cfbSIuliana Prodan unsigned int keylen) 50687870cfbSIuliana Prodan { 50787870cfbSIuliana Prodan struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 508836d8f43SIuliana Prodan int err; 509836d8f43SIuliana Prodan 510836d8f43SIuliana Prodan err = aes_check_keylen(keylen); 511674f368aSEric Biggers if (err) 512836d8f43SIuliana Prodan return err; 51387870cfbSIuliana Prodan 51487870cfbSIuliana Prodan /* key is immediate data for all cmac shared descriptors */ 51587870cfbSIuliana Prodan ctx->adata.key_virt = key; 51687870cfbSIuliana Prodan ctx->adata.keylen = keylen; 51787870cfbSIuliana Prodan 51887870cfbSIuliana Prodan print_hex_dump_debug("acmac ctx.key@" __stringify(__LINE__)" : ", 51987870cfbSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1); 52087870cfbSIuliana Prodan 52187870cfbSIuliana Prodan return acmac_set_sh_desc(ahash); 52287870cfbSIuliana Prodan } 52387870cfbSIuliana Prodan 524045e3678SYuan Kang /* 525045e3678SYuan Kang * ahash_edesc - s/w-extended ahash descriptor 526045e3678SYuan Kang * @sec4_sg_dma: physical mapped address of h/w link table 527045e3678SYuan Kang * @src_nents: number of segments in input scatterlist 528045e3678SYuan Kang * @sec4_sg_bytes: length of dma mapped sec4_sg space 52921b014f0SIuliana Prodan * @bklog: stored to determine if the request needs backlog 530045e3678SYuan Kang * @hw_desc: the h/w job descriptor followed by any referenced link tables 531343e44b1SRussell King * @sec4_sg: h/w link table 532045e3678SYuan Kang */ 533045e3678SYuan Kang struct ahash_edesc { 534045e3678SYuan Kang dma_addr_t sec4_sg_dma; 535045e3678SYuan Kang int src_nents; 536045e3678SYuan Kang int sec4_sg_bytes; 53721b014f0SIuliana Prodan bool bklog; 5381a3daadcSAndrey Smirnov u32 hw_desc[DESC_JOB_IO_LEN_MAX / sizeof(u32)] ____cacheline_aligned; 5395a8a0765SGustavo A. R. Silva struct sec4_sg_entry sec4_sg[]; 540045e3678SYuan Kang }; 541045e3678SYuan Kang 542045e3678SYuan Kang static inline void ahash_unmap(struct device *dev, 543045e3678SYuan Kang struct ahash_edesc *edesc, 544045e3678SYuan Kang struct ahash_request *req, int dst_len) 545045e3678SYuan Kang { 546944c3d4dSHoria Geantă struct caam_hash_state *state = ahash_request_ctx(req); 547944c3d4dSHoria Geantă 548045e3678SYuan Kang if (edesc->src_nents) 54913fb8fd7SLABBE Corentin dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE); 550045e3678SYuan Kang 551045e3678SYuan Kang if (edesc->sec4_sg_bytes) 552045e3678SYuan Kang dma_unmap_single(dev, edesc->sec4_sg_dma, 553045e3678SYuan Kang edesc->sec4_sg_bytes, DMA_TO_DEVICE); 554944c3d4dSHoria Geantă 555944c3d4dSHoria Geantă if (state->buf_dma) { 55646b49abcSAndrei Botila dma_unmap_single(dev, state->buf_dma, state->buflen, 557944c3d4dSHoria Geantă DMA_TO_DEVICE); 558944c3d4dSHoria Geantă state->buf_dma = 0; 559944c3d4dSHoria Geantă } 560045e3678SYuan Kang } 561045e3678SYuan Kang 562045e3678SYuan Kang static inline void ahash_unmap_ctx(struct device *dev, 563045e3678SYuan Kang struct ahash_edesc *edesc, 564045e3678SYuan Kang struct ahash_request *req, int dst_len, u32 flag) 565045e3678SYuan Kang { 566045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 567045e3678SYuan Kang 56887ec02e7SHoria Geantă if (state->ctx_dma) { 56965055e21SFranck LENORMAND dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag); 57087ec02e7SHoria Geantă state->ctx_dma = 0; 57187ec02e7SHoria Geantă } 572045e3678SYuan Kang ahash_unmap(dev, edesc, req, dst_len); 573045e3678SYuan Kang } 574045e3678SYuan Kang 575c3f7394eSIuliana Prodan static inline void ahash_done_cpy(struct device *jrdev, u32 *desc, u32 err, 576c3f7394eSIuliana Prodan void *context, enum dma_data_direction dir) 577045e3678SYuan Kang { 578045e3678SYuan Kang struct ahash_request *req = context; 57921b014f0SIuliana Prodan struct caam_drv_private_jr *jrp = dev_get_drvdata(jrdev); 580045e3678SYuan Kang struct ahash_edesc *edesc; 581045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 582045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 583c19650d6SHoria Geantă struct caam_hash_state *state = ahash_request_ctx(req); 584045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 5851984aaeeSHoria Geantă int ecode = 0; 58663db32e6SIuliana Prodan bool has_bklog; 587045e3678SYuan Kang 5886e005503SSascha Hauer dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); 589045e3678SYuan Kang 59021b014f0SIuliana Prodan edesc = state->edesc; 59163db32e6SIuliana Prodan has_bklog = edesc->bklog; 59221b014f0SIuliana Prodan 593fa9659cdSMarek Vasut if (err) 5941984aaeeSHoria Geantă ecode = caam_jr_strstatus(jrdev, err); 595045e3678SYuan Kang 596c3f7394eSIuliana Prodan ahash_unmap_ctx(jrdev, edesc, req, digestsize, dir); 597c19650d6SHoria Geantă memcpy(req->result, state->caam_ctx, digestsize); 598045e3678SYuan Kang kfree(edesc); 599045e3678SYuan Kang 6006e005503SSascha Hauer print_hex_dump_debug("ctx@"__stringify(__LINE__)": ", 601045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx, 602045e3678SYuan Kang ctx->ctx_len, 1); 603045e3678SYuan Kang 60421b014f0SIuliana Prodan /* 60521b014f0SIuliana Prodan * If no backlog flag, the completion of the request is done 60621b014f0SIuliana Prodan * by CAAM, not crypto engine. 60721b014f0SIuliana Prodan */ 60863db32e6SIuliana Prodan if (!has_bklog) 6091984aaeeSHoria Geantă req->base.complete(&req->base, ecode); 61021b014f0SIuliana Prodan else 61121b014f0SIuliana Prodan crypto_finalize_hash_request(jrp->engine, req, ecode); 612045e3678SYuan Kang } 613045e3678SYuan Kang 614c3f7394eSIuliana Prodan static void ahash_done(struct device *jrdev, u32 *desc, u32 err, 615c3f7394eSIuliana Prodan void *context) 616c3f7394eSIuliana Prodan { 617c3f7394eSIuliana Prodan ahash_done_cpy(jrdev, desc, err, context, DMA_FROM_DEVICE); 618c3f7394eSIuliana Prodan } 619c3f7394eSIuliana Prodan 620c3f7394eSIuliana Prodan static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err, 621c3f7394eSIuliana Prodan void *context) 622c3f7394eSIuliana Prodan { 623c3f7394eSIuliana Prodan ahash_done_cpy(jrdev, desc, err, context, DMA_BIDIRECTIONAL); 624c3f7394eSIuliana Prodan } 625c3f7394eSIuliana Prodan 626c3f7394eSIuliana Prodan static inline void ahash_done_switch(struct device *jrdev, u32 *desc, u32 err, 627c3f7394eSIuliana Prodan void *context, enum dma_data_direction dir) 628c3f7394eSIuliana Prodan { 629c3f7394eSIuliana Prodan struct ahash_request *req = context; 63021b014f0SIuliana Prodan struct caam_drv_private_jr *jrp = dev_get_drvdata(jrdev); 631c3f7394eSIuliana Prodan struct ahash_edesc *edesc; 632c3f7394eSIuliana Prodan struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 633c3f7394eSIuliana Prodan struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 634c3f7394eSIuliana Prodan struct caam_hash_state *state = ahash_request_ctx(req); 635c3f7394eSIuliana Prodan int digestsize = crypto_ahash_digestsize(ahash); 636c3f7394eSIuliana Prodan int ecode = 0; 63763db32e6SIuliana Prodan bool has_bklog; 638c3f7394eSIuliana Prodan 639c3f7394eSIuliana Prodan dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); 640c3f7394eSIuliana Prodan 64121b014f0SIuliana Prodan edesc = state->edesc; 64263db32e6SIuliana Prodan has_bklog = edesc->bklog; 643c3f7394eSIuliana Prodan if (err) 644c3f7394eSIuliana Prodan ecode = caam_jr_strstatus(jrdev, err); 645c3f7394eSIuliana Prodan 646c3f7394eSIuliana Prodan ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, dir); 647c3f7394eSIuliana Prodan kfree(edesc); 648c3f7394eSIuliana Prodan 649c3f7394eSIuliana Prodan scatterwalk_map_and_copy(state->buf, req->src, 650c3f7394eSIuliana Prodan req->nbytes - state->next_buflen, 651c3f7394eSIuliana Prodan state->next_buflen, 0); 652c3f7394eSIuliana Prodan state->buflen = state->next_buflen; 653c3f7394eSIuliana Prodan 654c3f7394eSIuliana Prodan print_hex_dump_debug("buf@" __stringify(__LINE__)": ", 655c3f7394eSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, state->buf, 656c3f7394eSIuliana Prodan state->buflen, 1); 657c3f7394eSIuliana Prodan 658c3f7394eSIuliana Prodan print_hex_dump_debug("ctx@"__stringify(__LINE__)": ", 659c3f7394eSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx, 660c3f7394eSIuliana Prodan ctx->ctx_len, 1); 661c3f7394eSIuliana Prodan if (req->result) 662c3f7394eSIuliana Prodan print_hex_dump_debug("result@"__stringify(__LINE__)": ", 663c3f7394eSIuliana Prodan DUMP_PREFIX_ADDRESS, 16, 4, req->result, 664c3f7394eSIuliana Prodan digestsize, 1); 665c3f7394eSIuliana Prodan 66621b014f0SIuliana Prodan /* 66721b014f0SIuliana Prodan * If no backlog flag, the completion of the request is done 66821b014f0SIuliana Prodan * by CAAM, not crypto engine. 66921b014f0SIuliana Prodan */ 67063db32e6SIuliana Prodan if (!has_bklog) 671c3f7394eSIuliana Prodan req->base.complete(&req->base, ecode); 67221b014f0SIuliana Prodan else 67321b014f0SIuliana Prodan crypto_finalize_hash_request(jrp->engine, req, ecode); 67421b014f0SIuliana Prodan 675c3f7394eSIuliana Prodan } 676c3f7394eSIuliana Prodan 677045e3678SYuan Kang static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err, 678045e3678SYuan Kang void *context) 679045e3678SYuan Kang { 680c3f7394eSIuliana Prodan ahash_done_switch(jrdev, desc, err, context, DMA_BIDIRECTIONAL); 681045e3678SYuan Kang } 682045e3678SYuan Kang 683045e3678SYuan Kang static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err, 684045e3678SYuan Kang void *context) 685045e3678SYuan Kang { 686c3f7394eSIuliana Prodan ahash_done_switch(jrdev, desc, err, context, DMA_FROM_DEVICE); 687045e3678SYuan Kang } 688045e3678SYuan Kang 6895588d039SRussell King /* 6905588d039SRussell King * Allocate an enhanced descriptor, which contains the hardware descriptor 6915588d039SRussell King * and space for hardware scatter table containing sg_num entries. 6925588d039SRussell King */ 6932ba1e798SIuliana Prodan static struct ahash_edesc *ahash_edesc_alloc(struct ahash_request *req, 69430a43b44SRussell King int sg_num, u32 *sh_desc, 6952ba1e798SIuliana Prodan dma_addr_t sh_desc_dma) 6965588d039SRussell King { 6972ba1e798SIuliana Prodan struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 6982ba1e798SIuliana Prodan struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 69921b014f0SIuliana Prodan struct caam_hash_state *state = ahash_request_ctx(req); 7002ba1e798SIuliana Prodan gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? 7012ba1e798SIuliana Prodan GFP_KERNEL : GFP_ATOMIC; 7025588d039SRussell King struct ahash_edesc *edesc; 7035588d039SRussell King unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry); 7045588d039SRussell King 7055588d039SRussell King edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags); 7065588d039SRussell King if (!edesc) { 7075588d039SRussell King dev_err(ctx->jrdev, "could not allocate extended descriptor\n"); 7085588d039SRussell King return NULL; 7095588d039SRussell King } 7105588d039SRussell King 71121b014f0SIuliana Prodan state->edesc = edesc; 71221b014f0SIuliana Prodan 71330a43b44SRussell King init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc), 71430a43b44SRussell King HDR_SHARE_DEFER | HDR_REVERSE); 71530a43b44SRussell King 7165588d039SRussell King return edesc; 7175588d039SRussell King } 7185588d039SRussell King 71965cf164aSRussell King static int ahash_edesc_add_src(struct caam_hash_ctx *ctx, 72065cf164aSRussell King struct ahash_edesc *edesc, 72165cf164aSRussell King struct ahash_request *req, int nents, 72265cf164aSRussell King unsigned int first_sg, 72365cf164aSRussell King unsigned int first_bytes, size_t to_hash) 72465cf164aSRussell King { 72565cf164aSRussell King dma_addr_t src_dma; 72665cf164aSRussell King u32 options; 72765cf164aSRussell King 72865cf164aSRussell King if (nents > 1 || first_sg) { 72965cf164aSRussell King struct sec4_sg_entry *sg = edesc->sec4_sg; 730a5e5c133SHoria Geantă unsigned int sgsize = sizeof(*sg) * 731a5e5c133SHoria Geantă pad_sg_nents(first_sg + nents); 73265cf164aSRussell King 733059d73eeSHoria Geantă sg_to_sec4_sg_last(req->src, to_hash, sg + first_sg, 0); 73465cf164aSRussell King 73565cf164aSRussell King src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE); 73665cf164aSRussell King if (dma_mapping_error(ctx->jrdev, src_dma)) { 73765cf164aSRussell King dev_err(ctx->jrdev, "unable to map S/G table\n"); 73865cf164aSRussell King return -ENOMEM; 73965cf164aSRussell King } 74065cf164aSRussell King 74165cf164aSRussell King edesc->sec4_sg_bytes = sgsize; 74265cf164aSRussell King edesc->sec4_sg_dma = src_dma; 74365cf164aSRussell King options = LDST_SGF; 74465cf164aSRussell King } else { 74565cf164aSRussell King src_dma = sg_dma_address(req->src); 74665cf164aSRussell King options = 0; 74765cf164aSRussell King } 74865cf164aSRussell King 74965cf164aSRussell King append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash, 75065cf164aSRussell King options); 75165cf164aSRussell King 75265cf164aSRussell King return 0; 75365cf164aSRussell King } 75465cf164aSRussell King 75521b014f0SIuliana Prodan static int ahash_do_one_req(struct crypto_engine *engine, void *areq) 75621b014f0SIuliana Prodan { 75721b014f0SIuliana Prodan struct ahash_request *req = ahash_request_cast(areq); 75821b014f0SIuliana Prodan struct caam_hash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 75921b014f0SIuliana Prodan struct caam_hash_state *state = ahash_request_ctx(req); 76021b014f0SIuliana Prodan struct device *jrdev = ctx->jrdev; 76121b014f0SIuliana Prodan u32 *desc = state->edesc->hw_desc; 76221b014f0SIuliana Prodan int ret; 76321b014f0SIuliana Prodan 76421b014f0SIuliana Prodan state->edesc->bklog = true; 76521b014f0SIuliana Prodan 76621b014f0SIuliana Prodan ret = caam_jr_enqueue(jrdev, desc, state->ahash_op_done, req); 76721b014f0SIuliana Prodan 768*087e1d71SGaurav Jain if (ret == -ENOSPC && engine->retry_support) 769*087e1d71SGaurav Jain return ret; 770*087e1d71SGaurav Jain 77121b014f0SIuliana Prodan if (ret != -EINPROGRESS) { 77221b014f0SIuliana Prodan ahash_unmap(jrdev, state->edesc, req, 0); 77321b014f0SIuliana Prodan kfree(state->edesc); 77421b014f0SIuliana Prodan } else { 77521b014f0SIuliana Prodan ret = 0; 77621b014f0SIuliana Prodan } 77721b014f0SIuliana Prodan 77821b014f0SIuliana Prodan return ret; 77921b014f0SIuliana Prodan } 78021b014f0SIuliana Prodan 78121b014f0SIuliana Prodan static int ahash_enqueue_req(struct device *jrdev, 78221b014f0SIuliana Prodan void (*cbk)(struct device *jrdev, u32 *desc, 78321b014f0SIuliana Prodan u32 err, void *context), 78421b014f0SIuliana Prodan struct ahash_request *req, 78521b014f0SIuliana Prodan int dst_len, enum dma_data_direction dir) 78621b014f0SIuliana Prodan { 78721b014f0SIuliana Prodan struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev); 78821b014f0SIuliana Prodan struct caam_hash_state *state = ahash_request_ctx(req); 78921b014f0SIuliana Prodan struct ahash_edesc *edesc = state->edesc; 79021b014f0SIuliana Prodan u32 *desc = edesc->hw_desc; 79121b014f0SIuliana Prodan int ret; 79221b014f0SIuliana Prodan 79321b014f0SIuliana Prodan state->ahash_op_done = cbk; 79421b014f0SIuliana Prodan 79521b014f0SIuliana Prodan /* 79621b014f0SIuliana Prodan * Only the backlog request are sent to crypto-engine since the others 79721b014f0SIuliana Prodan * can be handled by CAAM, if free, especially since JR has up to 1024 79821b014f0SIuliana Prodan * entries (more than the 10 entries from crypto-engine). 79921b014f0SIuliana Prodan */ 80021b014f0SIuliana Prodan if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) 80121b014f0SIuliana Prodan ret = crypto_transfer_hash_request_to_engine(jrpriv->engine, 80221b014f0SIuliana Prodan req); 80321b014f0SIuliana Prodan else 80421b014f0SIuliana Prodan ret = caam_jr_enqueue(jrdev, desc, cbk, req); 80521b014f0SIuliana Prodan 80621b014f0SIuliana Prodan if ((ret != -EINPROGRESS) && (ret != -EBUSY)) { 80721b014f0SIuliana Prodan ahash_unmap_ctx(jrdev, edesc, req, dst_len, dir); 80821b014f0SIuliana Prodan kfree(edesc); 80921b014f0SIuliana Prodan } 81021b014f0SIuliana Prodan 81121b014f0SIuliana Prodan return ret; 81221b014f0SIuliana Prodan } 81321b014f0SIuliana Prodan 814045e3678SYuan Kang /* submit update job descriptor */ 815045e3678SYuan Kang static int ahash_update_ctx(struct ahash_request *req) 816045e3678SYuan Kang { 817045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 818045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 819045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 820045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 82146b49abcSAndrei Botila u8 *buf = state->buf; 82246b49abcSAndrei Botila int *buflen = &state->buflen; 82346b49abcSAndrei Botila int *next_buflen = &state->next_buflen; 82412b8567fSIuliana Prodan int blocksize = crypto_ahash_blocksize(ahash); 825045e3678SYuan Kang int in_len = *buflen + req->nbytes, to_hash; 82630a43b44SRussell King u32 *desc; 827bc13c69eSRussell King int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index; 828045e3678SYuan Kang struct ahash_edesc *edesc; 829045e3678SYuan Kang int ret = 0; 830045e3678SYuan Kang 83112b8567fSIuliana Prodan *next_buflen = in_len & (blocksize - 1); 832045e3678SYuan Kang to_hash = in_len - *next_buflen; 833045e3678SYuan Kang 83412b8567fSIuliana Prodan /* 83587870cfbSIuliana Prodan * For XCBC and CMAC, if to_hash is multiple of block size, 83612b8567fSIuliana Prodan * keep last block in internal buffer 83712b8567fSIuliana Prodan */ 83887870cfbSIuliana Prodan if ((is_xcbc_aes(ctx->adata.algtype) || 83987870cfbSIuliana Prodan is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize && 84012b8567fSIuliana Prodan (*next_buflen == 0)) { 84112b8567fSIuliana Prodan *next_buflen = blocksize; 84212b8567fSIuliana Prodan to_hash -= blocksize; 84312b8567fSIuliana Prodan } 84412b8567fSIuliana Prodan 845045e3678SYuan Kang if (to_hash) { 846a5e5c133SHoria Geantă int pad_nents; 847059d73eeSHoria Geantă int src_len = req->nbytes - *next_buflen; 848a5e5c133SHoria Geantă 849059d73eeSHoria Geantă src_nents = sg_nents_for_len(req->src, src_len); 850f9970c28SLABBE Corentin if (src_nents < 0) { 851f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 852f9970c28SLABBE Corentin return src_nents; 853f9970c28SLABBE Corentin } 854bc13c69eSRussell King 855bc13c69eSRussell King if (src_nents) { 856bc13c69eSRussell King mapped_nents = dma_map_sg(jrdev, req->src, src_nents, 857bc13c69eSRussell King DMA_TO_DEVICE); 858bc13c69eSRussell King if (!mapped_nents) { 859bc13c69eSRussell King dev_err(jrdev, "unable to DMA map source\n"); 860bc13c69eSRussell King return -ENOMEM; 861bc13c69eSRussell King } 862bc13c69eSRussell King } else { 863bc13c69eSRussell King mapped_nents = 0; 864bc13c69eSRussell King } 865bc13c69eSRussell King 866045e3678SYuan Kang sec4_sg_src_index = 1 + (*buflen ? 1 : 0); 867a5e5c133SHoria Geantă pad_nents = pad_sg_nents(sec4_sg_src_index + mapped_nents); 868a5e5c133SHoria Geantă sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry); 869045e3678SYuan Kang 870045e3678SYuan Kang /* 871045e3678SYuan Kang * allocate space for base edesc and hw desc commands, 872045e3678SYuan Kang * link tables 873045e3678SYuan Kang */ 8742ba1e798SIuliana Prodan edesc = ahash_edesc_alloc(req, pad_nents, ctx->sh_desc_update, 8752ba1e798SIuliana Prodan ctx->sh_desc_update_dma); 876045e3678SYuan Kang if (!edesc) { 877bc13c69eSRussell King dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE); 878045e3678SYuan Kang return -ENOMEM; 879045e3678SYuan Kang } 880045e3678SYuan Kang 881045e3678SYuan Kang edesc->src_nents = src_nents; 882045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 883045e3678SYuan Kang 884dfcd8393SHoria Geantă ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len, 885045e3678SYuan Kang edesc->sec4_sg, DMA_BIDIRECTIONAL); 886ce572085SHoria Geanta if (ret) 88758b0e5d0SMarkus Elfring goto unmap_ctx; 888045e3678SYuan Kang 889944c3d4dSHoria Geantă ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state); 890944c3d4dSHoria Geantă if (ret) 891944c3d4dSHoria Geantă goto unmap_ctx; 892045e3678SYuan Kang 893b4e9e931SIuliana Prodan if (mapped_nents) 894059d73eeSHoria Geantă sg_to_sec4_sg_last(req->src, src_len, 895bc13c69eSRussell King edesc->sec4_sg + sec4_sg_src_index, 896bc13c69eSRussell King 0); 897b4e9e931SIuliana Prodan else 898b4e9e931SIuliana Prodan sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index - 899b4e9e931SIuliana Prodan 1); 900b4e9e931SIuliana Prodan 901045e3678SYuan Kang desc = edesc->hw_desc; 902045e3678SYuan Kang 9031da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, 9041da2be33SRuchika Gupta sec4_sg_bytes, 9051da2be33SRuchika Gupta DMA_TO_DEVICE); 906ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 907ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 90832686d34SRussell King ret = -ENOMEM; 90958b0e5d0SMarkus Elfring goto unmap_ctx; 910ce572085SHoria Geanta } 9111da2be33SRuchika Gupta 912045e3678SYuan Kang append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + 913045e3678SYuan Kang to_hash, LDST_SGF); 914045e3678SYuan Kang 915045e3678SYuan Kang append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0); 916045e3678SYuan Kang 9176e005503SSascha Hauer print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 918045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 919045e3678SYuan Kang desc_bytes(desc), 1); 920045e3678SYuan Kang 92121b014f0SIuliana Prodan ret = ahash_enqueue_req(jrdev, ahash_done_bi, req, 92221b014f0SIuliana Prodan ctx->ctx_len, DMA_BIDIRECTIONAL); 923045e3678SYuan Kang } else if (*next_buflen) { 924307fd543SCristian Stoica scatterwalk_map_and_copy(buf + *buflen, req->src, 0, 925307fd543SCristian Stoica req->nbytes, 0); 926045e3678SYuan Kang *buflen = *next_buflen; 9276e005503SSascha Hauer 9286e005503SSascha Hauer print_hex_dump_debug("buf@" __stringify(__LINE__)": ", 92946b49abcSAndrei Botila DUMP_PREFIX_ADDRESS, 16, 4, buf, 93046b49abcSAndrei Botila *buflen, 1); 93146b49abcSAndrei Botila } 932045e3678SYuan Kang 933045e3678SYuan Kang return ret; 93458b0e5d0SMarkus Elfring unmap_ctx: 93532686d34SRussell King ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL); 93632686d34SRussell King kfree(edesc); 93732686d34SRussell King return ret; 938045e3678SYuan Kang } 939045e3678SYuan Kang 940045e3678SYuan Kang static int ahash_final_ctx(struct ahash_request *req) 941045e3678SYuan Kang { 942045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 943045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 944045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 945045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 94646b49abcSAndrei Botila int buflen = state->buflen; 94730a43b44SRussell King u32 *desc; 948a5e5c133SHoria Geantă int sec4_sg_bytes; 949045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 950045e3678SYuan Kang struct ahash_edesc *edesc; 9519e6df0fdSMarkus Elfring int ret; 952045e3678SYuan Kang 953a5e5c133SHoria Geantă sec4_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) * 954a5e5c133SHoria Geantă sizeof(struct sec4_sg_entry); 955045e3678SYuan Kang 956045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 9572ba1e798SIuliana Prodan edesc = ahash_edesc_alloc(req, 4, ctx->sh_desc_fin, 9582ba1e798SIuliana Prodan ctx->sh_desc_fin_dma); 9595588d039SRussell King if (!edesc) 960045e3678SYuan Kang return -ENOMEM; 961045e3678SYuan Kang 962045e3678SYuan Kang desc = edesc->hw_desc; 963045e3678SYuan Kang 964045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 965045e3678SYuan Kang 966dfcd8393SHoria Geantă ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len, 967c19650d6SHoria Geantă edesc->sec4_sg, DMA_BIDIRECTIONAL); 968ce572085SHoria Geanta if (ret) 96958b0e5d0SMarkus Elfring goto unmap_ctx; 970045e3678SYuan Kang 971944c3d4dSHoria Geantă ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state); 972944c3d4dSHoria Geantă if (ret) 973944c3d4dSHoria Geantă goto unmap_ctx; 974944c3d4dSHoria Geantă 975a5e5c133SHoria Geantă sg_to_sec4_set_last(edesc->sec4_sg + (buflen ? 1 : 0)); 976045e3678SYuan Kang 9771da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, 9781da2be33SRuchika Gupta sec4_sg_bytes, DMA_TO_DEVICE); 979ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 980ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 98132686d34SRussell King ret = -ENOMEM; 98258b0e5d0SMarkus Elfring goto unmap_ctx; 983ce572085SHoria Geanta } 9841da2be33SRuchika Gupta 985045e3678SYuan Kang append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen, 986045e3678SYuan Kang LDST_SGF); 987c19650d6SHoria Geantă append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0); 988045e3678SYuan Kang 9896e005503SSascha Hauer print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 9906e005503SSascha Hauer DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 9916e005503SSascha Hauer 1); 992045e3678SYuan Kang 99321b014f0SIuliana Prodan return ahash_enqueue_req(jrdev, ahash_done_ctx_src, req, 99421b014f0SIuliana Prodan digestsize, DMA_BIDIRECTIONAL); 99558b0e5d0SMarkus Elfring unmap_ctx: 996c19650d6SHoria Geantă ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL); 997045e3678SYuan Kang kfree(edesc); 998045e3678SYuan Kang return ret; 999045e3678SYuan Kang } 1000045e3678SYuan Kang 1001045e3678SYuan Kang static int ahash_finup_ctx(struct ahash_request *req) 1002045e3678SYuan Kang { 1003045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1004045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1005045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1006045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 100746b49abcSAndrei Botila int buflen = state->buflen; 100830a43b44SRussell King u32 *desc; 100965cf164aSRussell King int sec4_sg_src_index; 1010bc13c69eSRussell King int src_nents, mapped_nents; 1011045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 1012045e3678SYuan Kang struct ahash_edesc *edesc; 10139e6df0fdSMarkus Elfring int ret; 1014045e3678SYuan Kang 101513fb8fd7SLABBE Corentin src_nents = sg_nents_for_len(req->src, req->nbytes); 1016f9970c28SLABBE Corentin if (src_nents < 0) { 1017f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1018f9970c28SLABBE Corentin return src_nents; 1019f9970c28SLABBE Corentin } 1020bc13c69eSRussell King 1021bc13c69eSRussell King if (src_nents) { 1022bc13c69eSRussell King mapped_nents = dma_map_sg(jrdev, req->src, src_nents, 1023bc13c69eSRussell King DMA_TO_DEVICE); 1024bc13c69eSRussell King if (!mapped_nents) { 1025bc13c69eSRussell King dev_err(jrdev, "unable to DMA map source\n"); 1026bc13c69eSRussell King return -ENOMEM; 1027bc13c69eSRussell King } 1028bc13c69eSRussell King } else { 1029bc13c69eSRussell King mapped_nents = 0; 1030bc13c69eSRussell King } 1031bc13c69eSRussell King 1032045e3678SYuan Kang sec4_sg_src_index = 1 + (buflen ? 1 : 0); 1033045e3678SYuan Kang 1034045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 10352ba1e798SIuliana Prodan edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents, 10362ba1e798SIuliana Prodan ctx->sh_desc_fin, ctx->sh_desc_fin_dma); 1037045e3678SYuan Kang if (!edesc) { 1038bc13c69eSRussell King dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE); 1039045e3678SYuan Kang return -ENOMEM; 1040045e3678SYuan Kang } 1041045e3678SYuan Kang 1042045e3678SYuan Kang desc = edesc->hw_desc; 1043045e3678SYuan Kang 1044045e3678SYuan Kang edesc->src_nents = src_nents; 1045045e3678SYuan Kang 1046dfcd8393SHoria Geantă ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len, 1047c19650d6SHoria Geantă edesc->sec4_sg, DMA_BIDIRECTIONAL); 1048ce572085SHoria Geanta if (ret) 104958b0e5d0SMarkus Elfring goto unmap_ctx; 1050045e3678SYuan Kang 1051944c3d4dSHoria Geantă ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state); 1052944c3d4dSHoria Geantă if (ret) 1053944c3d4dSHoria Geantă goto unmap_ctx; 1054045e3678SYuan Kang 105565cf164aSRussell King ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 105665cf164aSRussell King sec4_sg_src_index, ctx->ctx_len + buflen, 105765cf164aSRussell King req->nbytes); 105865cf164aSRussell King if (ret) 105958b0e5d0SMarkus Elfring goto unmap_ctx; 1060045e3678SYuan Kang 1061c19650d6SHoria Geantă append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0); 1062045e3678SYuan Kang 10636e005503SSascha Hauer print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 10646e005503SSascha Hauer DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 10656e005503SSascha Hauer 1); 1066045e3678SYuan Kang 106721b014f0SIuliana Prodan return ahash_enqueue_req(jrdev, ahash_done_ctx_src, req, 106821b014f0SIuliana Prodan digestsize, DMA_BIDIRECTIONAL); 106958b0e5d0SMarkus Elfring unmap_ctx: 1070c19650d6SHoria Geantă ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL); 1071045e3678SYuan Kang kfree(edesc); 1072045e3678SYuan Kang return ret; 1073045e3678SYuan Kang } 1074045e3678SYuan Kang 1075045e3678SYuan Kang static int ahash_digest(struct ahash_request *req) 1076045e3678SYuan Kang { 1077045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1078045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1079944c3d4dSHoria Geantă struct caam_hash_state *state = ahash_request_ctx(req); 1080045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 108130a43b44SRussell King u32 *desc; 1082045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 108365cf164aSRussell King int src_nents, mapped_nents; 1084045e3678SYuan Kang struct ahash_edesc *edesc; 10859e6df0fdSMarkus Elfring int ret; 1086045e3678SYuan Kang 1087944c3d4dSHoria Geantă state->buf_dma = 0; 1088944c3d4dSHoria Geantă 10893d5a2db6SRussell King src_nents = sg_nents_for_len(req->src, req->nbytes); 1090f9970c28SLABBE Corentin if (src_nents < 0) { 1091f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1092f9970c28SLABBE Corentin return src_nents; 1093f9970c28SLABBE Corentin } 1094bc13c69eSRussell King 1095bc13c69eSRussell King if (src_nents) { 1096bc13c69eSRussell King mapped_nents = dma_map_sg(jrdev, req->src, src_nents, 1097bc13c69eSRussell King DMA_TO_DEVICE); 1098bc13c69eSRussell King if (!mapped_nents) { 1099bc13c69eSRussell King dev_err(jrdev, "unable to map source for DMA\n"); 1100bc13c69eSRussell King return -ENOMEM; 1101bc13c69eSRussell King } 1102bc13c69eSRussell King } else { 1103bc13c69eSRussell King mapped_nents = 0; 1104bc13c69eSRussell King } 1105bc13c69eSRussell King 1106045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 11072ba1e798SIuliana Prodan edesc = ahash_edesc_alloc(req, mapped_nents > 1 ? mapped_nents : 0, 11082ba1e798SIuliana Prodan ctx->sh_desc_digest, ctx->sh_desc_digest_dma); 1109045e3678SYuan Kang if (!edesc) { 1110bc13c69eSRussell King dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE); 1111045e3678SYuan Kang return -ENOMEM; 1112045e3678SYuan Kang } 1113343e44b1SRussell King 1114045e3678SYuan Kang edesc->src_nents = src_nents; 1115045e3678SYuan Kang 111665cf164aSRussell King ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0, 111765cf164aSRussell King req->nbytes); 111865cf164aSRussell King if (ret) { 111932686d34SRussell King ahash_unmap(jrdev, edesc, req, digestsize); 112032686d34SRussell King kfree(edesc); 112165cf164aSRussell King return ret; 1122ce572085SHoria Geanta } 112365cf164aSRussell King 112465cf164aSRussell King desc = edesc->hw_desc; 1125045e3678SYuan Kang 1126c19650d6SHoria Geantă ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize); 1127c19650d6SHoria Geantă if (ret) { 112832686d34SRussell King ahash_unmap(jrdev, edesc, req, digestsize); 112932686d34SRussell King kfree(edesc); 1130ce572085SHoria Geanta return -ENOMEM; 1131ce572085SHoria Geanta } 1132045e3678SYuan Kang 11336e005503SSascha Hauer print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 11346e005503SSascha Hauer DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 11356e005503SSascha Hauer 1); 1136045e3678SYuan Kang 113721b014f0SIuliana Prodan return ahash_enqueue_req(jrdev, ahash_done, req, digestsize, 113821b014f0SIuliana Prodan DMA_FROM_DEVICE); 1139045e3678SYuan Kang } 1140045e3678SYuan Kang 1141045e3678SYuan Kang /* submit ahash final if it the first job descriptor */ 1142045e3678SYuan Kang static int ahash_final_no_ctx(struct ahash_request *req) 1143045e3678SYuan Kang { 1144045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1145045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1146045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1147045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 114846b49abcSAndrei Botila u8 *buf = state->buf; 114946b49abcSAndrei Botila int buflen = state->buflen; 115030a43b44SRussell King u32 *desc; 1151045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 1152045e3678SYuan Kang struct ahash_edesc *edesc; 11539e6df0fdSMarkus Elfring int ret; 1154045e3678SYuan Kang 1155045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 11562ba1e798SIuliana Prodan edesc = ahash_edesc_alloc(req, 0, ctx->sh_desc_digest, 11572ba1e798SIuliana Prodan ctx->sh_desc_digest_dma); 11585588d039SRussell King if (!edesc) 1159045e3678SYuan Kang return -ENOMEM; 1160045e3678SYuan Kang 1161045e3678SYuan Kang desc = edesc->hw_desc; 1162045e3678SYuan Kang 116304e6d25cSAymen Sghaier if (buflen) { 116404e6d25cSAymen Sghaier state->buf_dma = dma_map_single(jrdev, buf, buflen, 116504e6d25cSAymen Sghaier DMA_TO_DEVICE); 1166ce572085SHoria Geanta if (dma_mapping_error(jrdev, state->buf_dma)) { 1167ce572085SHoria Geanta dev_err(jrdev, "unable to map src\n"); 116806435f34SMarkus Elfring goto unmap; 1169ce572085SHoria Geanta } 1170045e3678SYuan Kang 1171045e3678SYuan Kang append_seq_in_ptr(desc, state->buf_dma, buflen, 0); 117204e6d25cSAymen Sghaier } 1173045e3678SYuan Kang 1174c19650d6SHoria Geantă ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize); 1175c19650d6SHoria Geantă if (ret) 117606435f34SMarkus Elfring goto unmap; 1177045e3678SYuan Kang 11786e005503SSascha Hauer print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 11796e005503SSascha Hauer DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 11806e005503SSascha Hauer 1); 1181045e3678SYuan Kang 118221b014f0SIuliana Prodan return ahash_enqueue_req(jrdev, ahash_done, req, 118321b014f0SIuliana Prodan digestsize, DMA_FROM_DEVICE); 118406435f34SMarkus Elfring unmap: 118506435f34SMarkus Elfring ahash_unmap(jrdev, edesc, req, digestsize); 118606435f34SMarkus Elfring kfree(edesc); 118706435f34SMarkus Elfring return -ENOMEM; 1188045e3678SYuan Kang } 1189045e3678SYuan Kang 1190045e3678SYuan Kang /* submit ahash update if it the first job descriptor after update */ 1191045e3678SYuan Kang static int ahash_update_no_ctx(struct ahash_request *req) 1192045e3678SYuan Kang { 1193045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1194045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1195045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1196045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 119746b49abcSAndrei Botila u8 *buf = state->buf; 119846b49abcSAndrei Botila int *buflen = &state->buflen; 119946b49abcSAndrei Botila int *next_buflen = &state->next_buflen; 120012b8567fSIuliana Prodan int blocksize = crypto_ahash_blocksize(ahash); 1201045e3678SYuan Kang int in_len = *buflen + req->nbytes, to_hash; 1202bc13c69eSRussell King int sec4_sg_bytes, src_nents, mapped_nents; 1203045e3678SYuan Kang struct ahash_edesc *edesc; 120430a43b44SRussell King u32 *desc; 1205045e3678SYuan Kang int ret = 0; 1206045e3678SYuan Kang 120712b8567fSIuliana Prodan *next_buflen = in_len & (blocksize - 1); 1208045e3678SYuan Kang to_hash = in_len - *next_buflen; 1209045e3678SYuan Kang 121012b8567fSIuliana Prodan /* 121187870cfbSIuliana Prodan * For XCBC and CMAC, if to_hash is multiple of block size, 121212b8567fSIuliana Prodan * keep last block in internal buffer 121312b8567fSIuliana Prodan */ 121487870cfbSIuliana Prodan if ((is_xcbc_aes(ctx->adata.algtype) || 121587870cfbSIuliana Prodan is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize && 121612b8567fSIuliana Prodan (*next_buflen == 0)) { 121712b8567fSIuliana Prodan *next_buflen = blocksize; 121812b8567fSIuliana Prodan to_hash -= blocksize; 121912b8567fSIuliana Prodan } 122012b8567fSIuliana Prodan 1221045e3678SYuan Kang if (to_hash) { 1222a5e5c133SHoria Geantă int pad_nents; 1223059d73eeSHoria Geantă int src_len = req->nbytes - *next_buflen; 1224a5e5c133SHoria Geantă 1225059d73eeSHoria Geantă src_nents = sg_nents_for_len(req->src, src_len); 1226f9970c28SLABBE Corentin if (src_nents < 0) { 1227f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1228f9970c28SLABBE Corentin return src_nents; 1229f9970c28SLABBE Corentin } 1230bc13c69eSRussell King 1231bc13c69eSRussell King if (src_nents) { 1232bc13c69eSRussell King mapped_nents = dma_map_sg(jrdev, req->src, src_nents, 1233bc13c69eSRussell King DMA_TO_DEVICE); 1234bc13c69eSRussell King if (!mapped_nents) { 1235bc13c69eSRussell King dev_err(jrdev, "unable to DMA map source\n"); 1236bc13c69eSRussell King return -ENOMEM; 1237bc13c69eSRussell King } 1238bc13c69eSRussell King } else { 1239bc13c69eSRussell King mapped_nents = 0; 1240bc13c69eSRussell King } 1241bc13c69eSRussell King 1242a5e5c133SHoria Geantă pad_nents = pad_sg_nents(1 + mapped_nents); 1243a5e5c133SHoria Geantă sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry); 1244045e3678SYuan Kang 1245045e3678SYuan Kang /* 1246045e3678SYuan Kang * allocate space for base edesc and hw desc commands, 1247045e3678SYuan Kang * link tables 1248045e3678SYuan Kang */ 12492ba1e798SIuliana Prodan edesc = ahash_edesc_alloc(req, pad_nents, 125030a43b44SRussell King ctx->sh_desc_update_first, 12512ba1e798SIuliana Prodan ctx->sh_desc_update_first_dma); 1252045e3678SYuan Kang if (!edesc) { 1253bc13c69eSRussell King dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE); 1254045e3678SYuan Kang return -ENOMEM; 1255045e3678SYuan Kang } 1256045e3678SYuan Kang 1257045e3678SYuan Kang edesc->src_nents = src_nents; 1258045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 1259045e3678SYuan Kang 1260944c3d4dSHoria Geantă ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state); 1261944c3d4dSHoria Geantă if (ret) 1262944c3d4dSHoria Geantă goto unmap_ctx; 1263944c3d4dSHoria Geantă 1264059d73eeSHoria Geantă sg_to_sec4_sg_last(req->src, src_len, edesc->sec4_sg + 1, 0); 1265bc13c69eSRussell King 1266045e3678SYuan Kang desc = edesc->hw_desc; 1267045e3678SYuan Kang 12681da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, 12691da2be33SRuchika Gupta sec4_sg_bytes, 12701da2be33SRuchika Gupta DMA_TO_DEVICE); 1271ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 1272ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 127332686d34SRussell King ret = -ENOMEM; 127458b0e5d0SMarkus Elfring goto unmap_ctx; 1275ce572085SHoria Geanta } 12761da2be33SRuchika Gupta 1277045e3678SYuan Kang append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF); 1278045e3678SYuan Kang 1279ce572085SHoria Geanta ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len); 1280ce572085SHoria Geanta if (ret) 128158b0e5d0SMarkus Elfring goto unmap_ctx; 1282045e3678SYuan Kang 12836e005503SSascha Hauer print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 1284045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 1285045e3678SYuan Kang desc_bytes(desc), 1); 1286045e3678SYuan Kang 128721b014f0SIuliana Prodan ret = ahash_enqueue_req(jrdev, ahash_done_ctx_dst, req, 128821b014f0SIuliana Prodan ctx->ctx_len, DMA_TO_DEVICE); 128921b014f0SIuliana Prodan if ((ret != -EINPROGRESS) && (ret != -EBUSY)) 129021b014f0SIuliana Prodan return ret; 1291045e3678SYuan Kang state->update = ahash_update_ctx; 1292045e3678SYuan Kang state->finup = ahash_finup_ctx; 1293045e3678SYuan Kang state->final = ahash_final_ctx; 1294045e3678SYuan Kang } else if (*next_buflen) { 1295307fd543SCristian Stoica scatterwalk_map_and_copy(buf + *buflen, req->src, 0, 1296307fd543SCristian Stoica req->nbytes, 0); 1297045e3678SYuan Kang *buflen = *next_buflen; 12986e005503SSascha Hauer 12996e005503SSascha Hauer print_hex_dump_debug("buf@" __stringify(__LINE__)": ", 130046b49abcSAndrei Botila DUMP_PREFIX_ADDRESS, 16, 4, buf, 130146b49abcSAndrei Botila *buflen, 1); 130246b49abcSAndrei Botila } 1303045e3678SYuan Kang 1304045e3678SYuan Kang return ret; 130558b0e5d0SMarkus Elfring unmap_ctx: 130632686d34SRussell King ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE); 130732686d34SRussell King kfree(edesc); 130832686d34SRussell King return ret; 1309045e3678SYuan Kang } 1310045e3678SYuan Kang 1311045e3678SYuan Kang /* submit ahash finup if it the first job descriptor after update */ 1312045e3678SYuan Kang static int ahash_finup_no_ctx(struct ahash_request *req) 1313045e3678SYuan Kang { 1314045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1315045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1316045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1317045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 131846b49abcSAndrei Botila int buflen = state->buflen; 131930a43b44SRussell King u32 *desc; 1320bc13c69eSRussell King int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents; 1321045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 1322045e3678SYuan Kang struct ahash_edesc *edesc; 13239e6df0fdSMarkus Elfring int ret; 1324045e3678SYuan Kang 132513fb8fd7SLABBE Corentin src_nents = sg_nents_for_len(req->src, req->nbytes); 1326f9970c28SLABBE Corentin if (src_nents < 0) { 1327f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1328f9970c28SLABBE Corentin return src_nents; 1329f9970c28SLABBE Corentin } 1330bc13c69eSRussell King 1331bc13c69eSRussell King if (src_nents) { 1332bc13c69eSRussell King mapped_nents = dma_map_sg(jrdev, req->src, src_nents, 1333bc13c69eSRussell King DMA_TO_DEVICE); 1334bc13c69eSRussell King if (!mapped_nents) { 1335bc13c69eSRussell King dev_err(jrdev, "unable to DMA map source\n"); 1336bc13c69eSRussell King return -ENOMEM; 1337bc13c69eSRussell King } 1338bc13c69eSRussell King } else { 1339bc13c69eSRussell King mapped_nents = 0; 1340bc13c69eSRussell King } 1341bc13c69eSRussell King 1342045e3678SYuan Kang sec4_sg_src_index = 2; 1343bc13c69eSRussell King sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) * 1344045e3678SYuan Kang sizeof(struct sec4_sg_entry); 1345045e3678SYuan Kang 1346045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 13472ba1e798SIuliana Prodan edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents, 13482ba1e798SIuliana Prodan ctx->sh_desc_digest, ctx->sh_desc_digest_dma); 1349045e3678SYuan Kang if (!edesc) { 1350bc13c69eSRussell King dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE); 1351045e3678SYuan Kang return -ENOMEM; 1352045e3678SYuan Kang } 1353045e3678SYuan Kang 1354045e3678SYuan Kang desc = edesc->hw_desc; 1355045e3678SYuan Kang 1356045e3678SYuan Kang edesc->src_nents = src_nents; 1357045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 1358045e3678SYuan Kang 1359944c3d4dSHoria Geantă ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state); 1360944c3d4dSHoria Geantă if (ret) 1361944c3d4dSHoria Geantă goto unmap; 1362045e3678SYuan Kang 136365cf164aSRussell King ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen, 136465cf164aSRussell King req->nbytes); 136565cf164aSRussell King if (ret) { 1366ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 136706435f34SMarkus Elfring goto unmap; 1368ce572085SHoria Geanta } 13691da2be33SRuchika Gupta 1370c19650d6SHoria Geantă ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize); 1371c19650d6SHoria Geantă if (ret) 137206435f34SMarkus Elfring goto unmap; 1373045e3678SYuan Kang 13746e005503SSascha Hauer print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 13756e005503SSascha Hauer DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 13766e005503SSascha Hauer 1); 1377045e3678SYuan Kang 137821b014f0SIuliana Prodan return ahash_enqueue_req(jrdev, ahash_done, req, 137921b014f0SIuliana Prodan digestsize, DMA_FROM_DEVICE); 138006435f34SMarkus Elfring unmap: 138106435f34SMarkus Elfring ahash_unmap(jrdev, edesc, req, digestsize); 138206435f34SMarkus Elfring kfree(edesc); 138306435f34SMarkus Elfring return -ENOMEM; 138406435f34SMarkus Elfring 1385045e3678SYuan Kang } 1386045e3678SYuan Kang 1387045e3678SYuan Kang /* submit first update job descriptor after init */ 1388045e3678SYuan Kang static int ahash_update_first(struct ahash_request *req) 1389045e3678SYuan Kang { 1390045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1391045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1392045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1393045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 139446b49abcSAndrei Botila u8 *buf = state->buf; 139546b49abcSAndrei Botila int *buflen = &state->buflen; 139646b49abcSAndrei Botila int *next_buflen = &state->next_buflen; 1397045e3678SYuan Kang int to_hash; 139812b8567fSIuliana Prodan int blocksize = crypto_ahash_blocksize(ahash); 139930a43b44SRussell King u32 *desc; 140065cf164aSRussell King int src_nents, mapped_nents; 1401045e3678SYuan Kang struct ahash_edesc *edesc; 1402045e3678SYuan Kang int ret = 0; 1403045e3678SYuan Kang 140412b8567fSIuliana Prodan *next_buflen = req->nbytes & (blocksize - 1); 1405045e3678SYuan Kang to_hash = req->nbytes - *next_buflen; 1406045e3678SYuan Kang 140712b8567fSIuliana Prodan /* 140887870cfbSIuliana Prodan * For XCBC and CMAC, if to_hash is multiple of block size, 140912b8567fSIuliana Prodan * keep last block in internal buffer 141012b8567fSIuliana Prodan */ 141187870cfbSIuliana Prodan if ((is_xcbc_aes(ctx->adata.algtype) || 141287870cfbSIuliana Prodan is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize && 141312b8567fSIuliana Prodan (*next_buflen == 0)) { 141412b8567fSIuliana Prodan *next_buflen = blocksize; 141512b8567fSIuliana Prodan to_hash -= blocksize; 141612b8567fSIuliana Prodan } 141712b8567fSIuliana Prodan 1418045e3678SYuan Kang if (to_hash) { 14193d5a2db6SRussell King src_nents = sg_nents_for_len(req->src, 14203d5a2db6SRussell King req->nbytes - *next_buflen); 1421f9970c28SLABBE Corentin if (src_nents < 0) { 1422f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1423f9970c28SLABBE Corentin return src_nents; 1424f9970c28SLABBE Corentin } 1425bc13c69eSRussell King 1426bc13c69eSRussell King if (src_nents) { 1427bc13c69eSRussell King mapped_nents = dma_map_sg(jrdev, req->src, src_nents, 1428bc13c69eSRussell King DMA_TO_DEVICE); 1429bc13c69eSRussell King if (!mapped_nents) { 1430bc13c69eSRussell King dev_err(jrdev, "unable to map source for DMA\n"); 1431bc13c69eSRussell King return -ENOMEM; 1432bc13c69eSRussell King } 1433bc13c69eSRussell King } else { 1434bc13c69eSRussell King mapped_nents = 0; 1435bc13c69eSRussell King } 1436045e3678SYuan Kang 1437045e3678SYuan Kang /* 1438045e3678SYuan Kang * allocate space for base edesc and hw desc commands, 1439045e3678SYuan Kang * link tables 1440045e3678SYuan Kang */ 14412ba1e798SIuliana Prodan edesc = ahash_edesc_alloc(req, mapped_nents > 1 ? 144230a43b44SRussell King mapped_nents : 0, 144330a43b44SRussell King ctx->sh_desc_update_first, 14442ba1e798SIuliana Prodan ctx->sh_desc_update_first_dma); 1445045e3678SYuan Kang if (!edesc) { 1446bc13c69eSRussell King dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE); 1447045e3678SYuan Kang return -ENOMEM; 1448045e3678SYuan Kang } 1449045e3678SYuan Kang 1450045e3678SYuan Kang edesc->src_nents = src_nents; 1451045e3678SYuan Kang 145265cf164aSRussell King ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0, 145365cf164aSRussell King to_hash); 145465cf164aSRussell King if (ret) 145558b0e5d0SMarkus Elfring goto unmap_ctx; 1456045e3678SYuan Kang 1457045e3678SYuan Kang desc = edesc->hw_desc; 1458045e3678SYuan Kang 1459ce572085SHoria Geanta ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len); 1460ce572085SHoria Geanta if (ret) 146158b0e5d0SMarkus Elfring goto unmap_ctx; 1462045e3678SYuan Kang 14636e005503SSascha Hauer print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ", 1464045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 1465045e3678SYuan Kang desc_bytes(desc), 1); 1466045e3678SYuan Kang 146721b014f0SIuliana Prodan ret = ahash_enqueue_req(jrdev, ahash_done_ctx_dst, req, 146821b014f0SIuliana Prodan ctx->ctx_len, DMA_TO_DEVICE); 146921b014f0SIuliana Prodan if ((ret != -EINPROGRESS) && (ret != -EBUSY)) 147021b014f0SIuliana Prodan return ret; 1471045e3678SYuan Kang state->update = ahash_update_ctx; 1472045e3678SYuan Kang state->finup = ahash_finup_ctx; 1473045e3678SYuan Kang state->final = ahash_final_ctx; 1474045e3678SYuan Kang } else if (*next_buflen) { 1475045e3678SYuan Kang state->update = ahash_update_no_ctx; 1476045e3678SYuan Kang state->finup = ahash_finup_no_ctx; 1477045e3678SYuan Kang state->final = ahash_final_no_ctx; 147846b49abcSAndrei Botila scatterwalk_map_and_copy(buf, req->src, 0, 1479307fd543SCristian Stoica req->nbytes, 0); 148046b49abcSAndrei Botila *buflen = *next_buflen; 14816e005503SSascha Hauer 148246b49abcSAndrei Botila print_hex_dump_debug("buf@" __stringify(__LINE__)": ", 148346b49abcSAndrei Botila DUMP_PREFIX_ADDRESS, 16, 4, buf, 148446b49abcSAndrei Botila *buflen, 1); 148546b49abcSAndrei Botila } 1486045e3678SYuan Kang 1487045e3678SYuan Kang return ret; 148858b0e5d0SMarkus Elfring unmap_ctx: 148932686d34SRussell King ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE); 149032686d34SRussell King kfree(edesc); 149132686d34SRussell King return ret; 1492045e3678SYuan Kang } 1493045e3678SYuan Kang 1494045e3678SYuan Kang static int ahash_finup_first(struct ahash_request *req) 1495045e3678SYuan Kang { 1496045e3678SYuan Kang return ahash_digest(req); 1497045e3678SYuan Kang } 1498045e3678SYuan Kang 1499045e3678SYuan Kang static int ahash_init(struct ahash_request *req) 1500045e3678SYuan Kang { 1501045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1502045e3678SYuan Kang 1503045e3678SYuan Kang state->update = ahash_update_first; 1504045e3678SYuan Kang state->finup = ahash_finup_first; 1505045e3678SYuan Kang state->final = ahash_final_no_ctx; 1506045e3678SYuan Kang 150787ec02e7SHoria Geantă state->ctx_dma = 0; 150865055e21SFranck LENORMAND state->ctx_dma_len = 0; 1509de0e35ecSHoria Geanta state->buf_dma = 0; 151046b49abcSAndrei Botila state->buflen = 0; 151146b49abcSAndrei Botila state->next_buflen = 0; 1512045e3678SYuan Kang 1513045e3678SYuan Kang return 0; 1514045e3678SYuan Kang } 1515045e3678SYuan Kang 1516045e3678SYuan Kang static int ahash_update(struct ahash_request *req) 1517045e3678SYuan Kang { 1518045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1519045e3678SYuan Kang 1520045e3678SYuan Kang return state->update(req); 1521045e3678SYuan Kang } 1522045e3678SYuan Kang 1523045e3678SYuan Kang static int ahash_finup(struct ahash_request *req) 1524045e3678SYuan Kang { 1525045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1526045e3678SYuan Kang 1527045e3678SYuan Kang return state->finup(req); 1528045e3678SYuan Kang } 1529045e3678SYuan Kang 1530045e3678SYuan Kang static int ahash_final(struct ahash_request *req) 1531045e3678SYuan Kang { 1532045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1533045e3678SYuan Kang 1534045e3678SYuan Kang return state->final(req); 1535045e3678SYuan Kang } 1536045e3678SYuan Kang 1537045e3678SYuan Kang static int ahash_export(struct ahash_request *req, void *out) 1538045e3678SYuan Kang { 1539045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 15405ec90831SRussell King struct caam_export_state *export = out; 154146b49abcSAndrei Botila u8 *buf = state->buf; 154246b49abcSAndrei Botila int len = state->buflen; 15435ec90831SRussell King 15445ec90831SRussell King memcpy(export->buf, buf, len); 15455ec90831SRussell King memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx)); 15465ec90831SRussell King export->buflen = len; 15475ec90831SRussell King export->update = state->update; 15485ec90831SRussell King export->final = state->final; 15495ec90831SRussell King export->finup = state->finup; 1550434b4212SRussell King 1551045e3678SYuan Kang return 0; 1552045e3678SYuan Kang } 1553045e3678SYuan Kang 1554045e3678SYuan Kang static int ahash_import(struct ahash_request *req, const void *in) 1555045e3678SYuan Kang { 1556045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 15575ec90831SRussell King const struct caam_export_state *export = in; 1558045e3678SYuan Kang 15595ec90831SRussell King memset(state, 0, sizeof(*state)); 156046b49abcSAndrei Botila memcpy(state->buf, export->buf, export->buflen); 15615ec90831SRussell King memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx)); 156246b49abcSAndrei Botila state->buflen = export->buflen; 15635ec90831SRussell King state->update = export->update; 15645ec90831SRussell King state->final = export->final; 15655ec90831SRussell King state->finup = export->finup; 1566434b4212SRussell King 1567045e3678SYuan Kang return 0; 1568045e3678SYuan Kang } 1569045e3678SYuan Kang 1570045e3678SYuan Kang struct caam_hash_template { 1571045e3678SYuan Kang char name[CRYPTO_MAX_ALG_NAME]; 1572045e3678SYuan Kang char driver_name[CRYPTO_MAX_ALG_NAME]; 1573b0e09baeSYuan Kang char hmac_name[CRYPTO_MAX_ALG_NAME]; 1574b0e09baeSYuan Kang char hmac_driver_name[CRYPTO_MAX_ALG_NAME]; 1575045e3678SYuan Kang unsigned int blocksize; 1576045e3678SYuan Kang struct ahash_alg template_ahash; 1577045e3678SYuan Kang u32 alg_type; 1578045e3678SYuan Kang }; 1579045e3678SYuan Kang 1580045e3678SYuan Kang /* ahash descriptors */ 1581045e3678SYuan Kang static struct caam_hash_template driver_hash[] = { 1582045e3678SYuan Kang { 1583b0e09baeSYuan Kang .name = "sha1", 1584b0e09baeSYuan Kang .driver_name = "sha1-caam", 1585b0e09baeSYuan Kang .hmac_name = "hmac(sha1)", 1586b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha1-caam", 1587045e3678SYuan Kang .blocksize = SHA1_BLOCK_SIZE, 1588045e3678SYuan Kang .template_ahash = { 1589045e3678SYuan Kang .init = ahash_init, 1590045e3678SYuan Kang .update = ahash_update, 1591045e3678SYuan Kang .final = ahash_final, 1592045e3678SYuan Kang .finup = ahash_finup, 1593045e3678SYuan Kang .digest = ahash_digest, 1594045e3678SYuan Kang .export = ahash_export, 1595045e3678SYuan Kang .import = ahash_import, 1596045e3678SYuan Kang .setkey = ahash_setkey, 1597045e3678SYuan Kang .halg = { 1598045e3678SYuan Kang .digestsize = SHA1_DIGEST_SIZE, 15995ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1600045e3678SYuan Kang }, 1601045e3678SYuan Kang }, 1602045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA1, 1603045e3678SYuan Kang }, { 1604b0e09baeSYuan Kang .name = "sha224", 1605b0e09baeSYuan Kang .driver_name = "sha224-caam", 1606b0e09baeSYuan Kang .hmac_name = "hmac(sha224)", 1607b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha224-caam", 1608045e3678SYuan Kang .blocksize = SHA224_BLOCK_SIZE, 1609045e3678SYuan Kang .template_ahash = { 1610045e3678SYuan Kang .init = ahash_init, 1611045e3678SYuan Kang .update = ahash_update, 1612045e3678SYuan Kang .final = ahash_final, 1613045e3678SYuan Kang .finup = ahash_finup, 1614045e3678SYuan Kang .digest = ahash_digest, 1615045e3678SYuan Kang .export = ahash_export, 1616045e3678SYuan Kang .import = ahash_import, 1617045e3678SYuan Kang .setkey = ahash_setkey, 1618045e3678SYuan Kang .halg = { 1619045e3678SYuan Kang .digestsize = SHA224_DIGEST_SIZE, 16205ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1621045e3678SYuan Kang }, 1622045e3678SYuan Kang }, 1623045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA224, 1624045e3678SYuan Kang }, { 1625b0e09baeSYuan Kang .name = "sha256", 1626b0e09baeSYuan Kang .driver_name = "sha256-caam", 1627b0e09baeSYuan Kang .hmac_name = "hmac(sha256)", 1628b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha256-caam", 1629045e3678SYuan Kang .blocksize = SHA256_BLOCK_SIZE, 1630045e3678SYuan Kang .template_ahash = { 1631045e3678SYuan Kang .init = ahash_init, 1632045e3678SYuan Kang .update = ahash_update, 1633045e3678SYuan Kang .final = ahash_final, 1634045e3678SYuan Kang .finup = ahash_finup, 1635045e3678SYuan Kang .digest = ahash_digest, 1636045e3678SYuan Kang .export = ahash_export, 1637045e3678SYuan Kang .import = ahash_import, 1638045e3678SYuan Kang .setkey = ahash_setkey, 1639045e3678SYuan Kang .halg = { 1640045e3678SYuan Kang .digestsize = SHA256_DIGEST_SIZE, 16415ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1642045e3678SYuan Kang }, 1643045e3678SYuan Kang }, 1644045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA256, 1645045e3678SYuan Kang }, { 1646b0e09baeSYuan Kang .name = "sha384", 1647b0e09baeSYuan Kang .driver_name = "sha384-caam", 1648b0e09baeSYuan Kang .hmac_name = "hmac(sha384)", 1649b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha384-caam", 1650045e3678SYuan Kang .blocksize = SHA384_BLOCK_SIZE, 1651045e3678SYuan Kang .template_ahash = { 1652045e3678SYuan Kang .init = ahash_init, 1653045e3678SYuan Kang .update = ahash_update, 1654045e3678SYuan Kang .final = ahash_final, 1655045e3678SYuan Kang .finup = ahash_finup, 1656045e3678SYuan Kang .digest = ahash_digest, 1657045e3678SYuan Kang .export = ahash_export, 1658045e3678SYuan Kang .import = ahash_import, 1659045e3678SYuan Kang .setkey = ahash_setkey, 1660045e3678SYuan Kang .halg = { 1661045e3678SYuan Kang .digestsize = SHA384_DIGEST_SIZE, 16625ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1663045e3678SYuan Kang }, 1664045e3678SYuan Kang }, 1665045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA384, 1666045e3678SYuan Kang }, { 1667b0e09baeSYuan Kang .name = "sha512", 1668b0e09baeSYuan Kang .driver_name = "sha512-caam", 1669b0e09baeSYuan Kang .hmac_name = "hmac(sha512)", 1670b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha512-caam", 1671045e3678SYuan Kang .blocksize = SHA512_BLOCK_SIZE, 1672045e3678SYuan Kang .template_ahash = { 1673045e3678SYuan Kang .init = ahash_init, 1674045e3678SYuan Kang .update = ahash_update, 1675045e3678SYuan Kang .final = ahash_final, 1676045e3678SYuan Kang .finup = ahash_finup, 1677045e3678SYuan Kang .digest = ahash_digest, 1678045e3678SYuan Kang .export = ahash_export, 1679045e3678SYuan Kang .import = ahash_import, 1680045e3678SYuan Kang .setkey = ahash_setkey, 1681045e3678SYuan Kang .halg = { 1682045e3678SYuan Kang .digestsize = SHA512_DIGEST_SIZE, 16835ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1684045e3678SYuan Kang }, 1685045e3678SYuan Kang }, 1686045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA512, 1687045e3678SYuan Kang }, { 1688b0e09baeSYuan Kang .name = "md5", 1689b0e09baeSYuan Kang .driver_name = "md5-caam", 1690b0e09baeSYuan Kang .hmac_name = "hmac(md5)", 1691b0e09baeSYuan Kang .hmac_driver_name = "hmac-md5-caam", 1692045e3678SYuan Kang .blocksize = MD5_BLOCK_WORDS * 4, 1693045e3678SYuan Kang .template_ahash = { 1694045e3678SYuan Kang .init = ahash_init, 1695045e3678SYuan Kang .update = ahash_update, 1696045e3678SYuan Kang .final = ahash_final, 1697045e3678SYuan Kang .finup = ahash_finup, 1698045e3678SYuan Kang .digest = ahash_digest, 1699045e3678SYuan Kang .export = ahash_export, 1700045e3678SYuan Kang .import = ahash_import, 1701045e3678SYuan Kang .setkey = ahash_setkey, 1702045e3678SYuan Kang .halg = { 1703045e3678SYuan Kang .digestsize = MD5_DIGEST_SIZE, 17045ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1705045e3678SYuan Kang }, 1706045e3678SYuan Kang }, 1707045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_MD5, 170812b8567fSIuliana Prodan }, { 170912b8567fSIuliana Prodan .hmac_name = "xcbc(aes)", 171012b8567fSIuliana Prodan .hmac_driver_name = "xcbc-aes-caam", 171112b8567fSIuliana Prodan .blocksize = AES_BLOCK_SIZE, 171212b8567fSIuliana Prodan .template_ahash = { 171312b8567fSIuliana Prodan .init = ahash_init, 171412b8567fSIuliana Prodan .update = ahash_update, 171512b8567fSIuliana Prodan .final = ahash_final, 171612b8567fSIuliana Prodan .finup = ahash_finup, 171712b8567fSIuliana Prodan .digest = ahash_digest, 171812b8567fSIuliana Prodan .export = ahash_export, 171912b8567fSIuliana Prodan .import = ahash_import, 172012b8567fSIuliana Prodan .setkey = axcbc_setkey, 172112b8567fSIuliana Prodan .halg = { 172212b8567fSIuliana Prodan .digestsize = AES_BLOCK_SIZE, 172312b8567fSIuliana Prodan .statesize = sizeof(struct caam_export_state), 172412b8567fSIuliana Prodan }, 172512b8567fSIuliana Prodan }, 172612b8567fSIuliana Prodan .alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XCBC_MAC, 172787870cfbSIuliana Prodan }, { 172887870cfbSIuliana Prodan .hmac_name = "cmac(aes)", 172987870cfbSIuliana Prodan .hmac_driver_name = "cmac-aes-caam", 173087870cfbSIuliana Prodan .blocksize = AES_BLOCK_SIZE, 173187870cfbSIuliana Prodan .template_ahash = { 173287870cfbSIuliana Prodan .init = ahash_init, 173387870cfbSIuliana Prodan .update = ahash_update, 173487870cfbSIuliana Prodan .final = ahash_final, 173587870cfbSIuliana Prodan .finup = ahash_finup, 173687870cfbSIuliana Prodan .digest = ahash_digest, 173787870cfbSIuliana Prodan .export = ahash_export, 173887870cfbSIuliana Prodan .import = ahash_import, 173987870cfbSIuliana Prodan .setkey = acmac_setkey, 174087870cfbSIuliana Prodan .halg = { 174187870cfbSIuliana Prodan .digestsize = AES_BLOCK_SIZE, 174287870cfbSIuliana Prodan .statesize = sizeof(struct caam_export_state), 174387870cfbSIuliana Prodan }, 174487870cfbSIuliana Prodan }, 174587870cfbSIuliana Prodan .alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC, 1746045e3678SYuan Kang }, 1747045e3678SYuan Kang }; 1748045e3678SYuan Kang 1749045e3678SYuan Kang struct caam_hash_alg { 1750045e3678SYuan Kang struct list_head entry; 1751045e3678SYuan Kang int alg_type; 1752045e3678SYuan Kang struct ahash_alg ahash_alg; 1753045e3678SYuan Kang }; 1754045e3678SYuan Kang 1755045e3678SYuan Kang static int caam_hash_cra_init(struct crypto_tfm *tfm) 1756045e3678SYuan Kang { 1757045e3678SYuan Kang struct crypto_ahash *ahash = __crypto_ahash_cast(tfm); 1758045e3678SYuan Kang struct crypto_alg *base = tfm->__crt_alg; 1759045e3678SYuan Kang struct hash_alg_common *halg = 1760045e3678SYuan Kang container_of(base, struct hash_alg_common, base); 1761045e3678SYuan Kang struct ahash_alg *alg = 1762045e3678SYuan Kang container_of(halg, struct ahash_alg, halg); 1763045e3678SYuan Kang struct caam_hash_alg *caam_hash = 1764045e3678SYuan Kang container_of(alg, struct caam_hash_alg, ahash_alg); 1765045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm); 1766045e3678SYuan Kang /* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */ 1767045e3678SYuan Kang static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE, 1768045e3678SYuan Kang HASH_MSG_LEN + SHA1_DIGEST_SIZE, 1769045e3678SYuan Kang HASH_MSG_LEN + 32, 1770045e3678SYuan Kang HASH_MSG_LEN + SHA256_DIGEST_SIZE, 1771045e3678SYuan Kang HASH_MSG_LEN + 64, 1772045e3678SYuan Kang HASH_MSG_LEN + SHA512_DIGEST_SIZE }; 177321b014f0SIuliana Prodan const size_t sh_desc_update_offset = offsetof(struct caam_hash_ctx, 177421b014f0SIuliana Prodan sh_desc_update); 1775bbf22344SHoria Geantă dma_addr_t dma_addr; 17767e0880b9SHoria Geantă struct caam_drv_private *priv; 1777045e3678SYuan Kang 1778045e3678SYuan Kang /* 1779cfc6f11bSRuchika Gupta * Get a Job ring from Job Ring driver to ensure in-order 1780045e3678SYuan Kang * crypto request processing per tfm 1781045e3678SYuan Kang */ 1782cfc6f11bSRuchika Gupta ctx->jrdev = caam_jr_alloc(); 1783cfc6f11bSRuchika Gupta if (IS_ERR(ctx->jrdev)) { 1784cfc6f11bSRuchika Gupta pr_err("Job Ring Device allocation for transform failed\n"); 1785cfc6f11bSRuchika Gupta return PTR_ERR(ctx->jrdev); 1786cfc6f11bSRuchika Gupta } 1787bbf22344SHoria Geantă 17887e0880b9SHoria Geantă priv = dev_get_drvdata(ctx->jrdev->parent); 178912b8567fSIuliana Prodan 179012b8567fSIuliana Prodan if (is_xcbc_aes(caam_hash->alg_type)) { 179112b8567fSIuliana Prodan ctx->dir = DMA_TO_DEVICE; 1792e9b4913aSHoria Geantă ctx->key_dir = DMA_BIDIRECTIONAL; 179312b8567fSIuliana Prodan ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type; 179412b8567fSIuliana Prodan ctx->ctx_len = 48; 1795e9b4913aSHoria Geantă } else if (is_cmac_aes(caam_hash->alg_type)) { 1796e9b4913aSHoria Geantă ctx->dir = DMA_TO_DEVICE; 1797e9b4913aSHoria Geantă ctx->key_dir = DMA_NONE; 1798e9b4913aSHoria Geantă ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type; 1799e9b4913aSHoria Geantă ctx->ctx_len = 32; 1800e9b4913aSHoria Geantă } else { 1801e9b4913aSHoria Geantă if (priv->era >= 6) { 1802e9b4913aSHoria Geantă ctx->dir = DMA_BIDIRECTIONAL; 1803e9b4913aSHoria Geantă ctx->key_dir = alg->setkey ? DMA_TO_DEVICE : DMA_NONE; 1804e9b4913aSHoria Geantă } else { 1805e9b4913aSHoria Geantă ctx->dir = DMA_TO_DEVICE; 1806e9b4913aSHoria Geantă ctx->key_dir = DMA_NONE; 1807e9b4913aSHoria Geantă } 1808e9b4913aSHoria Geantă ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type; 1809e9b4913aSHoria Geantă ctx->ctx_len = runninglen[(ctx->adata.algtype & 1810e9b4913aSHoria Geantă OP_ALG_ALGSEL_SUBMASK) >> 1811e9b4913aSHoria Geantă OP_ALG_ALGSEL_SHIFT]; 1812e9b4913aSHoria Geantă } 181312b8567fSIuliana Prodan 1814e9b4913aSHoria Geantă if (ctx->key_dir != DMA_NONE) { 1815a2fb864cSHoria Geantă ctx->adata.key_dma = dma_map_single_attrs(ctx->jrdev, ctx->key, 181612b8567fSIuliana Prodan ARRAY_SIZE(ctx->key), 1817e9b4913aSHoria Geantă ctx->key_dir, 181812b8567fSIuliana Prodan DMA_ATTR_SKIP_CPU_SYNC); 1819a2fb864cSHoria Geantă if (dma_mapping_error(ctx->jrdev, ctx->adata.key_dma)) { 182012b8567fSIuliana Prodan dev_err(ctx->jrdev, "unable to map key\n"); 182112b8567fSIuliana Prodan caam_jr_free(ctx->jrdev); 182212b8567fSIuliana Prodan return -ENOMEM; 182312b8567fSIuliana Prodan } 182412b8567fSIuliana Prodan } 18257e0880b9SHoria Geantă 1826bbf22344SHoria Geantă dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_update, 182721b014f0SIuliana Prodan offsetof(struct caam_hash_ctx, key) - 182821b014f0SIuliana Prodan sh_desc_update_offset, 18297e0880b9SHoria Geantă ctx->dir, DMA_ATTR_SKIP_CPU_SYNC); 1830bbf22344SHoria Geantă if (dma_mapping_error(ctx->jrdev, dma_addr)) { 1831bbf22344SHoria Geantă dev_err(ctx->jrdev, "unable to map shared descriptors\n"); 183212b8567fSIuliana Prodan 1833e9b4913aSHoria Geantă if (ctx->key_dir != DMA_NONE) 1834a2fb864cSHoria Geantă dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma, 183512b8567fSIuliana Prodan ARRAY_SIZE(ctx->key), 1836e9b4913aSHoria Geantă ctx->key_dir, 183712b8567fSIuliana Prodan DMA_ATTR_SKIP_CPU_SYNC); 183812b8567fSIuliana Prodan 1839bbf22344SHoria Geantă caam_jr_free(ctx->jrdev); 1840bbf22344SHoria Geantă return -ENOMEM; 1841bbf22344SHoria Geantă } 1842bbf22344SHoria Geantă 1843bbf22344SHoria Geantă ctx->sh_desc_update_dma = dma_addr; 1844bbf22344SHoria Geantă ctx->sh_desc_update_first_dma = dma_addr + 1845bbf22344SHoria Geantă offsetof(struct caam_hash_ctx, 184621b014f0SIuliana Prodan sh_desc_update_first) - 184721b014f0SIuliana Prodan sh_desc_update_offset; 1848bbf22344SHoria Geantă ctx->sh_desc_fin_dma = dma_addr + offsetof(struct caam_hash_ctx, 184921b014f0SIuliana Prodan sh_desc_fin) - 185021b014f0SIuliana Prodan sh_desc_update_offset; 1851bbf22344SHoria Geantă ctx->sh_desc_digest_dma = dma_addr + offsetof(struct caam_hash_ctx, 185221b014f0SIuliana Prodan sh_desc_digest) - 185321b014f0SIuliana Prodan sh_desc_update_offset; 185421b014f0SIuliana Prodan 185521b014f0SIuliana Prodan ctx->enginectx.op.do_one_request = ahash_do_one_req; 1856bbf22344SHoria Geantă 1857045e3678SYuan Kang crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 1858045e3678SYuan Kang sizeof(struct caam_hash_state)); 18599a2537d0SIuliana Prodan 18609a2537d0SIuliana Prodan /* 18619a2537d0SIuliana Prodan * For keyed hash algorithms shared descriptors 18629a2537d0SIuliana Prodan * will be created later in setkey() callback 18639a2537d0SIuliana Prodan */ 18649a2537d0SIuliana Prodan return alg->setkey ? 0 : ahash_set_sh_desc(ahash); 1865045e3678SYuan Kang } 1866045e3678SYuan Kang 1867045e3678SYuan Kang static void caam_hash_cra_exit(struct crypto_tfm *tfm) 1868045e3678SYuan Kang { 1869045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm); 1870045e3678SYuan Kang 1871bbf22344SHoria Geantă dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_update_dma, 187221b014f0SIuliana Prodan offsetof(struct caam_hash_ctx, key) - 187321b014f0SIuliana Prodan offsetof(struct caam_hash_ctx, sh_desc_update), 18747e0880b9SHoria Geantă ctx->dir, DMA_ATTR_SKIP_CPU_SYNC); 1875e9b4913aSHoria Geantă if (ctx->key_dir != DMA_NONE) 1876a2fb864cSHoria Geantă dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma, 1877e9b4913aSHoria Geantă ARRAY_SIZE(ctx->key), ctx->key_dir, 187812b8567fSIuliana Prodan DMA_ATTR_SKIP_CPU_SYNC); 1879cfc6f11bSRuchika Gupta caam_jr_free(ctx->jrdev); 1880045e3678SYuan Kang } 1881045e3678SYuan Kang 18821b46c90cSHoria Geantă void caam_algapi_hash_exit(void) 1883045e3678SYuan Kang { 1884045e3678SYuan Kang struct caam_hash_alg *t_alg, *n; 1885045e3678SYuan Kang 1886cfc6f11bSRuchika Gupta if (!hash_list.next) 1887045e3678SYuan Kang return; 1888045e3678SYuan Kang 1889cfc6f11bSRuchika Gupta list_for_each_entry_safe(t_alg, n, &hash_list, entry) { 1890045e3678SYuan Kang crypto_unregister_ahash(&t_alg->ahash_alg); 1891045e3678SYuan Kang list_del(&t_alg->entry); 1892045e3678SYuan Kang kfree(t_alg); 1893045e3678SYuan Kang } 1894045e3678SYuan Kang } 1895045e3678SYuan Kang 1896045e3678SYuan Kang static struct caam_hash_alg * 1897cfc6f11bSRuchika Gupta caam_hash_alloc(struct caam_hash_template *template, 1898b0e09baeSYuan Kang bool keyed) 1899045e3678SYuan Kang { 1900045e3678SYuan Kang struct caam_hash_alg *t_alg; 1901045e3678SYuan Kang struct ahash_alg *halg; 1902045e3678SYuan Kang struct crypto_alg *alg; 1903045e3678SYuan Kang 19049c4f9733SFabio Estevam t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL); 1905045e3678SYuan Kang if (!t_alg) { 1906cfc6f11bSRuchika Gupta pr_err("failed to allocate t_alg\n"); 1907045e3678SYuan Kang return ERR_PTR(-ENOMEM); 1908045e3678SYuan Kang } 1909045e3678SYuan Kang 1910045e3678SYuan Kang t_alg->ahash_alg = template->template_ahash; 1911045e3678SYuan Kang halg = &t_alg->ahash_alg; 1912045e3678SYuan Kang alg = &halg->halg.base; 1913045e3678SYuan Kang 1914b0e09baeSYuan Kang if (keyed) { 1915b0e09baeSYuan Kang snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", 1916b0e09baeSYuan Kang template->hmac_name); 1917b0e09baeSYuan Kang snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", 1918b0e09baeSYuan Kang template->hmac_driver_name); 1919b0e09baeSYuan Kang } else { 1920b0e09baeSYuan Kang snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", 1921b0e09baeSYuan Kang template->name); 1922045e3678SYuan Kang snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", 1923045e3678SYuan Kang template->driver_name); 1924a0118c8bSRussell King t_alg->ahash_alg.setkey = NULL; 1925b0e09baeSYuan Kang } 1926045e3678SYuan Kang alg->cra_module = THIS_MODULE; 1927045e3678SYuan Kang alg->cra_init = caam_hash_cra_init; 1928045e3678SYuan Kang alg->cra_exit = caam_hash_cra_exit; 1929045e3678SYuan Kang alg->cra_ctxsize = sizeof(struct caam_hash_ctx); 1930045e3678SYuan Kang alg->cra_priority = CAAM_CRA_PRIORITY; 1931045e3678SYuan Kang alg->cra_blocksize = template->blocksize; 1932045e3678SYuan Kang alg->cra_alignmask = 0; 1933b8aa7dc5SMikulas Patocka alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY; 1934045e3678SYuan Kang 1935045e3678SYuan Kang t_alg->alg_type = template->alg_type; 1936045e3678SYuan Kang 1937045e3678SYuan Kang return t_alg; 1938045e3678SYuan Kang } 1939045e3678SYuan Kang 19401b46c90cSHoria Geantă int caam_algapi_hash_init(struct device *ctrldev) 1941045e3678SYuan Kang { 1942045e3678SYuan Kang int i = 0, err = 0; 19431b46c90cSHoria Geantă struct caam_drv_private *priv = dev_get_drvdata(ctrldev); 1944bf83490eSVictoria Milhoan unsigned int md_limit = SHA512_DIGEST_SIZE; 1945d239b10dSHoria Geantă u32 md_inst, md_vid; 1946045e3678SYuan Kang 1947bf83490eSVictoria Milhoan /* 1948bf83490eSVictoria Milhoan * Register crypto algorithms the device supports. First, identify 1949bf83490eSVictoria Milhoan * presence and attributes of MD block. 1950bf83490eSVictoria Milhoan */ 1951d239b10dSHoria Geantă if (priv->era < 10) { 1952d239b10dSHoria Geantă md_vid = (rd_reg32(&priv->ctrl->perfmon.cha_id_ls) & 1953d239b10dSHoria Geantă CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT; 1954d239b10dSHoria Geantă md_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) & 1955d239b10dSHoria Geantă CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT; 1956d239b10dSHoria Geantă } else { 1957d239b10dSHoria Geantă u32 mdha = rd_reg32(&priv->ctrl->vreg.mdha); 1958d239b10dSHoria Geantă 1959d239b10dSHoria Geantă md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT; 1960d239b10dSHoria Geantă md_inst = mdha & CHA_VER_NUM_MASK; 1961d239b10dSHoria Geantă } 1962bf83490eSVictoria Milhoan 1963bf83490eSVictoria Milhoan /* 1964bf83490eSVictoria Milhoan * Skip registration of any hashing algorithms if MD block 1965bf83490eSVictoria Milhoan * is not present. 1966bf83490eSVictoria Milhoan */ 19671b46c90cSHoria Geantă if (!md_inst) 19680435d47eSIuliana Prodan return 0; 1969bf83490eSVictoria Milhoan 1970bf83490eSVictoria Milhoan /* Limit digest size based on LP256 */ 1971d239b10dSHoria Geantă if (md_vid == CHA_VER_VID_MD_LP256) 1972bf83490eSVictoria Milhoan md_limit = SHA256_DIGEST_SIZE; 1973bf83490eSVictoria Milhoan 1974cfc6f11bSRuchika Gupta INIT_LIST_HEAD(&hash_list); 1975045e3678SYuan Kang 1976045e3678SYuan Kang /* register crypto algorithms the device supports */ 1977045e3678SYuan Kang for (i = 0; i < ARRAY_SIZE(driver_hash); i++) { 1978045e3678SYuan Kang struct caam_hash_alg *t_alg; 1979bf83490eSVictoria Milhoan struct caam_hash_template *alg = driver_hash + i; 1980bf83490eSVictoria Milhoan 1981bf83490eSVictoria Milhoan /* If MD size is not supported by device, skip registration */ 198212b8567fSIuliana Prodan if (is_mdha(alg->alg_type) && 198312b8567fSIuliana Prodan alg->template_ahash.halg.digestsize > md_limit) 1984bf83490eSVictoria Milhoan continue; 1985045e3678SYuan Kang 1986b0e09baeSYuan Kang /* register hmac version */ 1987bf83490eSVictoria Milhoan t_alg = caam_hash_alloc(alg, true); 1988b0e09baeSYuan Kang if (IS_ERR(t_alg)) { 1989b0e09baeSYuan Kang err = PTR_ERR(t_alg); 19900f103b37SIuliana Prodan pr_warn("%s alg allocation failed\n", 19910f103b37SIuliana Prodan alg->hmac_driver_name); 1992b0e09baeSYuan Kang continue; 1993b0e09baeSYuan Kang } 1994b0e09baeSYuan Kang 1995b0e09baeSYuan Kang err = crypto_register_ahash(&t_alg->ahash_alg); 1996b0e09baeSYuan Kang if (err) { 19976ea30f0aSRussell King pr_warn("%s alg registration failed: %d\n", 19986ea30f0aSRussell King t_alg->ahash_alg.halg.base.cra_driver_name, 19996ea30f0aSRussell King err); 2000b0e09baeSYuan Kang kfree(t_alg); 2001b0e09baeSYuan Kang } else 2002cfc6f11bSRuchika Gupta list_add_tail(&t_alg->entry, &hash_list); 2003b0e09baeSYuan Kang 200412b8567fSIuliana Prodan if ((alg->alg_type & OP_ALG_ALGSEL_MASK) == OP_ALG_ALGSEL_AES) 200512b8567fSIuliana Prodan continue; 200612b8567fSIuliana Prodan 2007b0e09baeSYuan Kang /* register unkeyed version */ 2008bf83490eSVictoria Milhoan t_alg = caam_hash_alloc(alg, false); 2009045e3678SYuan Kang if (IS_ERR(t_alg)) { 2010045e3678SYuan Kang err = PTR_ERR(t_alg); 2011bf83490eSVictoria Milhoan pr_warn("%s alg allocation failed\n", alg->driver_name); 2012045e3678SYuan Kang continue; 2013045e3678SYuan Kang } 2014045e3678SYuan Kang 2015045e3678SYuan Kang err = crypto_register_ahash(&t_alg->ahash_alg); 2016045e3678SYuan Kang if (err) { 20176ea30f0aSRussell King pr_warn("%s alg registration failed: %d\n", 20186ea30f0aSRussell King t_alg->ahash_alg.halg.base.cra_driver_name, 20196ea30f0aSRussell King err); 2020045e3678SYuan Kang kfree(t_alg); 2021045e3678SYuan Kang } else 2022cfc6f11bSRuchika Gupta list_add_tail(&t_alg->entry, &hash_list); 2023045e3678SYuan Kang } 2024045e3678SYuan Kang 2025045e3678SYuan Kang return err; 2026045e3678SYuan Kang } 2027