1045e3678SYuan Kang /* 2045e3678SYuan Kang * caam - Freescale FSL CAAM support for ahash functions of crypto API 3045e3678SYuan Kang * 4045e3678SYuan Kang * Copyright 2011 Freescale Semiconductor, Inc. 5045e3678SYuan Kang * 6045e3678SYuan Kang * Based on caamalg.c crypto API driver. 7045e3678SYuan Kang * 8045e3678SYuan Kang * relationship of digest job descriptor or first job descriptor after init to 9045e3678SYuan Kang * shared descriptors: 10045e3678SYuan Kang * 11045e3678SYuan Kang * --------------- --------------- 12045e3678SYuan Kang * | JobDesc #1 |-------------------->| ShareDesc | 13045e3678SYuan Kang * | *(packet 1) | | (hashKey) | 14045e3678SYuan Kang * --------------- | (operation) | 15045e3678SYuan Kang * --------------- 16045e3678SYuan Kang * 17045e3678SYuan Kang * relationship of subsequent job descriptors to shared descriptors: 18045e3678SYuan Kang * 19045e3678SYuan Kang * --------------- --------------- 20045e3678SYuan Kang * | JobDesc #2 |-------------------->| ShareDesc | 21045e3678SYuan Kang * | *(packet 2) | |------------->| (hashKey) | 22045e3678SYuan Kang * --------------- | |-------->| (operation) | 23045e3678SYuan Kang * . | | | (load ctx2) | 24045e3678SYuan Kang * . | | --------------- 25045e3678SYuan Kang * --------------- | | 26045e3678SYuan Kang * | JobDesc #3 |------| | 27045e3678SYuan Kang * | *(packet 3) | | 28045e3678SYuan Kang * --------------- | 29045e3678SYuan Kang * . | 30045e3678SYuan Kang * . | 31045e3678SYuan Kang * --------------- | 32045e3678SYuan Kang * | JobDesc #4 |------------ 33045e3678SYuan Kang * | *(packet 4) | 34045e3678SYuan Kang * --------------- 35045e3678SYuan Kang * 36045e3678SYuan Kang * The SharedDesc never changes for a connection unless rekeyed, but 37045e3678SYuan Kang * each packet will likely be in a different place. So all we need 38045e3678SYuan Kang * to know to process the packet is where the input is, where the 39045e3678SYuan Kang * output goes, and what context we want to process with. Context is 40045e3678SYuan Kang * in the SharedDesc, packet references in the JobDesc. 41045e3678SYuan Kang * 42045e3678SYuan Kang * So, a job desc looks like: 43045e3678SYuan Kang * 44045e3678SYuan Kang * --------------------- 45045e3678SYuan Kang * | Header | 46045e3678SYuan Kang * | ShareDesc Pointer | 47045e3678SYuan Kang * | SEQ_OUT_PTR | 48045e3678SYuan Kang * | (output buffer) | 49045e3678SYuan Kang * | (output length) | 50045e3678SYuan Kang * | SEQ_IN_PTR | 51045e3678SYuan Kang * | (input buffer) | 52045e3678SYuan Kang * | (input length) | 53045e3678SYuan Kang * --------------------- 54045e3678SYuan Kang */ 55045e3678SYuan Kang 56045e3678SYuan Kang #include "compat.h" 57045e3678SYuan Kang 58045e3678SYuan Kang #include "regs.h" 59045e3678SYuan Kang #include "intern.h" 60045e3678SYuan Kang #include "desc_constr.h" 61045e3678SYuan Kang #include "jr.h" 62045e3678SYuan Kang #include "error.h" 63045e3678SYuan Kang #include "sg_sw_sec4.h" 64045e3678SYuan Kang #include "key_gen.h" 65045e3678SYuan Kang 66045e3678SYuan Kang #define CAAM_CRA_PRIORITY 3000 67045e3678SYuan Kang 68045e3678SYuan Kang /* max hash key is max split key size */ 69045e3678SYuan Kang #define CAAM_MAX_HASH_KEY_SIZE (SHA512_DIGEST_SIZE * 2) 70045e3678SYuan Kang 71045e3678SYuan Kang #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE 72045e3678SYuan Kang #define CAAM_MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE 73045e3678SYuan Kang 74045e3678SYuan Kang /* length of descriptors text */ 75045e3678SYuan Kang #define DESC_AHASH_BASE (4 * CAAM_CMD_SZ) 76045e3678SYuan Kang #define DESC_AHASH_UPDATE_LEN (6 * CAAM_CMD_SZ) 77045e3678SYuan Kang #define DESC_AHASH_UPDATE_FIRST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ) 78045e3678SYuan Kang #define DESC_AHASH_FINAL_LEN (DESC_AHASH_BASE + 5 * CAAM_CMD_SZ) 79045e3678SYuan Kang #define DESC_AHASH_FINUP_LEN (DESC_AHASH_BASE + 5 * CAAM_CMD_SZ) 80045e3678SYuan Kang #define DESC_AHASH_DIGEST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ) 81045e3678SYuan Kang 82045e3678SYuan Kang #define DESC_HASH_MAX_USED_BYTES (DESC_AHASH_FINAL_LEN + \ 83045e3678SYuan Kang CAAM_MAX_HASH_KEY_SIZE) 84045e3678SYuan Kang #define DESC_HASH_MAX_USED_LEN (DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ) 85045e3678SYuan Kang 86045e3678SYuan Kang /* caam context sizes for hashes: running digest + 8 */ 87045e3678SYuan Kang #define HASH_MSG_LEN 8 88045e3678SYuan Kang #define MAX_CTX_LEN (HASH_MSG_LEN + SHA512_DIGEST_SIZE) 89045e3678SYuan Kang 90045e3678SYuan Kang #ifdef DEBUG 91045e3678SYuan Kang /* for print_hex_dumps with line references */ 92045e3678SYuan Kang #define debug(format, arg...) printk(format, arg) 93045e3678SYuan Kang #else 94045e3678SYuan Kang #define debug(format, arg...) 95045e3678SYuan Kang #endif 96045e3678SYuan Kang 97cfc6f11bSRuchika Gupta 98cfc6f11bSRuchika Gupta static struct list_head hash_list; 99cfc6f11bSRuchika Gupta 100045e3678SYuan Kang /* ahash per-session context */ 101045e3678SYuan Kang struct caam_hash_ctx { 102045e3678SYuan Kang struct device *jrdev; 103045e3678SYuan Kang u32 sh_desc_update[DESC_HASH_MAX_USED_LEN]; 104045e3678SYuan Kang u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN]; 105045e3678SYuan Kang u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN]; 106045e3678SYuan Kang u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN]; 107045e3678SYuan Kang u32 sh_desc_finup[DESC_HASH_MAX_USED_LEN]; 108045e3678SYuan Kang dma_addr_t sh_desc_update_dma; 109045e3678SYuan Kang dma_addr_t sh_desc_update_first_dma; 110045e3678SYuan Kang dma_addr_t sh_desc_fin_dma; 111045e3678SYuan Kang dma_addr_t sh_desc_digest_dma; 112045e3678SYuan Kang dma_addr_t sh_desc_finup_dma; 113045e3678SYuan Kang u32 alg_type; 114045e3678SYuan Kang u32 alg_op; 115045e3678SYuan Kang u8 key[CAAM_MAX_HASH_KEY_SIZE]; 116045e3678SYuan Kang dma_addr_t key_dma; 117045e3678SYuan Kang int ctx_len; 118045e3678SYuan Kang unsigned int split_key_len; 119045e3678SYuan Kang unsigned int split_key_pad_len; 120045e3678SYuan Kang }; 121045e3678SYuan Kang 122045e3678SYuan Kang /* ahash state */ 123045e3678SYuan Kang struct caam_hash_state { 124045e3678SYuan Kang dma_addr_t buf_dma; 125045e3678SYuan Kang dma_addr_t ctx_dma; 126045e3678SYuan Kang u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned; 127045e3678SYuan Kang int buflen_0; 128045e3678SYuan Kang u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned; 129045e3678SYuan Kang int buflen_1; 130e7472422SVictoria Milhoan u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned; 131045e3678SYuan Kang int (*update)(struct ahash_request *req); 132045e3678SYuan Kang int (*final)(struct ahash_request *req); 133045e3678SYuan Kang int (*finup)(struct ahash_request *req); 134045e3678SYuan Kang int current_buf; 135045e3678SYuan Kang }; 136045e3678SYuan Kang 1375ec90831SRussell King struct caam_export_state { 1385ec90831SRussell King u8 buf[CAAM_MAX_HASH_BLOCK_SIZE]; 1395ec90831SRussell King u8 caam_ctx[MAX_CTX_LEN]; 1405ec90831SRussell King int buflen; 1415ec90831SRussell King int (*update)(struct ahash_request *req); 1425ec90831SRussell King int (*final)(struct ahash_request *req); 1435ec90831SRussell King int (*finup)(struct ahash_request *req); 1445ec90831SRussell King }; 1455ec90831SRussell King 146045e3678SYuan Kang /* Common job descriptor seq in/out ptr routines */ 147045e3678SYuan Kang 148045e3678SYuan Kang /* Map state->caam_ctx, and append seq_out_ptr command that points to it */ 149ce572085SHoria Geanta static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev, 150045e3678SYuan Kang struct caam_hash_state *state, 151045e3678SYuan Kang int ctx_len) 152045e3678SYuan Kang { 153045e3678SYuan Kang state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, 154045e3678SYuan Kang ctx_len, DMA_FROM_DEVICE); 155ce572085SHoria Geanta if (dma_mapping_error(jrdev, state->ctx_dma)) { 156ce572085SHoria Geanta dev_err(jrdev, "unable to map ctx\n"); 157ce572085SHoria Geanta return -ENOMEM; 158ce572085SHoria Geanta } 159ce572085SHoria Geanta 160045e3678SYuan Kang append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0); 161ce572085SHoria Geanta 162ce572085SHoria Geanta return 0; 163045e3678SYuan Kang } 164045e3678SYuan Kang 165045e3678SYuan Kang /* Map req->result, and append seq_out_ptr command that points to it */ 166045e3678SYuan Kang static inline dma_addr_t map_seq_out_ptr_result(u32 *desc, struct device *jrdev, 167045e3678SYuan Kang u8 *result, int digestsize) 168045e3678SYuan Kang { 169045e3678SYuan Kang dma_addr_t dst_dma; 170045e3678SYuan Kang 171045e3678SYuan Kang dst_dma = dma_map_single(jrdev, result, digestsize, DMA_FROM_DEVICE); 172045e3678SYuan Kang append_seq_out_ptr(desc, dst_dma, digestsize, 0); 173045e3678SYuan Kang 174045e3678SYuan Kang return dst_dma; 175045e3678SYuan Kang } 176045e3678SYuan Kang 177045e3678SYuan Kang /* Map current buffer in state and put it in link table */ 178045e3678SYuan Kang static inline dma_addr_t buf_map_to_sec4_sg(struct device *jrdev, 179045e3678SYuan Kang struct sec4_sg_entry *sec4_sg, 180045e3678SYuan Kang u8 *buf, int buflen) 181045e3678SYuan Kang { 182045e3678SYuan Kang dma_addr_t buf_dma; 183045e3678SYuan Kang 184045e3678SYuan Kang buf_dma = dma_map_single(jrdev, buf, buflen, DMA_TO_DEVICE); 185045e3678SYuan Kang dma_to_sec4_sg_one(sec4_sg, buf_dma, buflen, 0); 186045e3678SYuan Kang 187045e3678SYuan Kang return buf_dma; 188045e3678SYuan Kang } 189045e3678SYuan Kang 190045e3678SYuan Kang /* Map req->src and put it in link table */ 191045e3678SYuan Kang static inline void src_map_to_sec4_sg(struct device *jrdev, 192045e3678SYuan Kang struct scatterlist *src, int src_nents, 19313fb8fd7SLABBE Corentin struct sec4_sg_entry *sec4_sg) 194045e3678SYuan Kang { 19513fb8fd7SLABBE Corentin dma_map_sg(jrdev, src, src_nents, DMA_TO_DEVICE); 196045e3678SYuan Kang sg_to_sec4_sg_last(src, src_nents, sec4_sg, 0); 197045e3678SYuan Kang } 198045e3678SYuan Kang 199045e3678SYuan Kang /* 200045e3678SYuan Kang * Only put buffer in link table if it contains data, which is possible, 201045e3678SYuan Kang * since a buffer has previously been used, and needs to be unmapped, 202045e3678SYuan Kang */ 203045e3678SYuan Kang static inline dma_addr_t 204045e3678SYuan Kang try_buf_map_to_sec4_sg(struct device *jrdev, struct sec4_sg_entry *sec4_sg, 205045e3678SYuan Kang u8 *buf, dma_addr_t buf_dma, int buflen, 206045e3678SYuan Kang int last_buflen) 207045e3678SYuan Kang { 208045e3678SYuan Kang if (buf_dma && !dma_mapping_error(jrdev, buf_dma)) 209045e3678SYuan Kang dma_unmap_single(jrdev, buf_dma, last_buflen, DMA_TO_DEVICE); 210045e3678SYuan Kang if (buflen) 211045e3678SYuan Kang buf_dma = buf_map_to_sec4_sg(jrdev, sec4_sg, buf, buflen); 212045e3678SYuan Kang else 213045e3678SYuan Kang buf_dma = 0; 214045e3678SYuan Kang 215045e3678SYuan Kang return buf_dma; 216045e3678SYuan Kang } 217045e3678SYuan Kang 218045e3678SYuan Kang /* Map state->caam_ctx, and add it to link table */ 219ce572085SHoria Geanta static inline int ctx_map_to_sec4_sg(u32 *desc, struct device *jrdev, 220ce572085SHoria Geanta struct caam_hash_state *state, int ctx_len, 221ce572085SHoria Geanta struct sec4_sg_entry *sec4_sg, u32 flag) 222045e3678SYuan Kang { 223045e3678SYuan Kang state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag); 224ce572085SHoria Geanta if (dma_mapping_error(jrdev, state->ctx_dma)) { 225ce572085SHoria Geanta dev_err(jrdev, "unable to map ctx\n"); 226ce572085SHoria Geanta return -ENOMEM; 227ce572085SHoria Geanta } 228ce572085SHoria Geanta 229045e3678SYuan Kang dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0); 230ce572085SHoria Geanta 231ce572085SHoria Geanta return 0; 232045e3678SYuan Kang } 233045e3678SYuan Kang 234045e3678SYuan Kang /* Common shared descriptor commands */ 235045e3678SYuan Kang static inline void append_key_ahash(u32 *desc, struct caam_hash_ctx *ctx) 236045e3678SYuan Kang { 237045e3678SYuan Kang append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len, 238045e3678SYuan Kang ctx->split_key_len, CLASS_2 | 239045e3678SYuan Kang KEY_DEST_MDHA_SPLIT | KEY_ENC); 240045e3678SYuan Kang } 241045e3678SYuan Kang 242045e3678SYuan Kang /* Append key if it has been set */ 243045e3678SYuan Kang static inline void init_sh_desc_key_ahash(u32 *desc, struct caam_hash_ctx *ctx) 244045e3678SYuan Kang { 245045e3678SYuan Kang u32 *key_jump_cmd; 246045e3678SYuan Kang 24761bb86bbSKim Phillips init_sh_desc(desc, HDR_SHARE_SERIAL); 248045e3678SYuan Kang 249045e3678SYuan Kang if (ctx->split_key_len) { 250045e3678SYuan Kang /* Skip if already shared */ 251045e3678SYuan Kang key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | 252045e3678SYuan Kang JUMP_COND_SHRD); 253045e3678SYuan Kang 254045e3678SYuan Kang append_key_ahash(desc, ctx); 255045e3678SYuan Kang 256045e3678SYuan Kang set_jump_tgt_here(desc, key_jump_cmd); 257045e3678SYuan Kang } 258045e3678SYuan Kang 259045e3678SYuan Kang /* Propagate errors from shared to job descriptor */ 260045e3678SYuan Kang append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD); 261045e3678SYuan Kang } 262045e3678SYuan Kang 263045e3678SYuan Kang /* 264045e3678SYuan Kang * For ahash read data from seqin following state->caam_ctx, 265045e3678SYuan Kang * and write resulting class2 context to seqout, which may be state->caam_ctx 266045e3678SYuan Kang * or req->result 267045e3678SYuan Kang */ 268045e3678SYuan Kang static inline void ahash_append_load_str(u32 *desc, int digestsize) 269045e3678SYuan Kang { 270045e3678SYuan Kang /* Calculate remaining bytes to read */ 271045e3678SYuan Kang append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ); 272045e3678SYuan Kang 273045e3678SYuan Kang /* Read remaining bytes */ 274045e3678SYuan Kang append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 | 275045e3678SYuan Kang FIFOLD_TYPE_MSG | KEY_VLF); 276045e3678SYuan Kang 277045e3678SYuan Kang /* Store class2 context bytes */ 278045e3678SYuan Kang append_seq_store(desc, digestsize, LDST_CLASS_2_CCB | 279045e3678SYuan Kang LDST_SRCDST_BYTE_CONTEXT); 280045e3678SYuan Kang } 281045e3678SYuan Kang 282045e3678SYuan Kang /* 283045e3678SYuan Kang * For ahash update, final and finup, import context, read and write to seqout 284045e3678SYuan Kang */ 285045e3678SYuan Kang static inline void ahash_ctx_data_to_out(u32 *desc, u32 op, u32 state, 286045e3678SYuan Kang int digestsize, 287045e3678SYuan Kang struct caam_hash_ctx *ctx) 288045e3678SYuan Kang { 289045e3678SYuan Kang init_sh_desc_key_ahash(desc, ctx); 290045e3678SYuan Kang 291045e3678SYuan Kang /* Import context from software */ 292045e3678SYuan Kang append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT | 293045e3678SYuan Kang LDST_CLASS_2_CCB | ctx->ctx_len); 294045e3678SYuan Kang 295045e3678SYuan Kang /* Class 2 operation */ 296045e3678SYuan Kang append_operation(desc, op | state | OP_ALG_ENCRYPT); 297045e3678SYuan Kang 298045e3678SYuan Kang /* 299045e3678SYuan Kang * Load from buf and/or src and write to req->result or state->context 300045e3678SYuan Kang */ 301045e3678SYuan Kang ahash_append_load_str(desc, digestsize); 302045e3678SYuan Kang } 303045e3678SYuan Kang 304045e3678SYuan Kang /* For ahash firsts and digest, read and write to seqout */ 305045e3678SYuan Kang static inline void ahash_data_to_out(u32 *desc, u32 op, u32 state, 306045e3678SYuan Kang int digestsize, struct caam_hash_ctx *ctx) 307045e3678SYuan Kang { 308045e3678SYuan Kang init_sh_desc_key_ahash(desc, ctx); 309045e3678SYuan Kang 310045e3678SYuan Kang /* Class 2 operation */ 311045e3678SYuan Kang append_operation(desc, op | state | OP_ALG_ENCRYPT); 312045e3678SYuan Kang 313045e3678SYuan Kang /* 314045e3678SYuan Kang * Load from buf and/or src and write to req->result or state->context 315045e3678SYuan Kang */ 316045e3678SYuan Kang ahash_append_load_str(desc, digestsize); 317045e3678SYuan Kang } 318045e3678SYuan Kang 319045e3678SYuan Kang static int ahash_set_sh_desc(struct crypto_ahash *ahash) 320045e3678SYuan Kang { 321045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 322045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 323045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 324045e3678SYuan Kang u32 have_key = 0; 325045e3678SYuan Kang u32 *desc; 326045e3678SYuan Kang 327045e3678SYuan Kang if (ctx->split_key_len) 328045e3678SYuan Kang have_key = OP_ALG_AAI_HMAC_PRECOMP; 329045e3678SYuan Kang 330045e3678SYuan Kang /* ahash_update shared descriptor */ 331045e3678SYuan Kang desc = ctx->sh_desc_update; 332045e3678SYuan Kang 33361bb86bbSKim Phillips init_sh_desc(desc, HDR_SHARE_SERIAL); 334045e3678SYuan Kang 335045e3678SYuan Kang /* Import context from software */ 336045e3678SYuan Kang append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT | 337045e3678SYuan Kang LDST_CLASS_2_CCB | ctx->ctx_len); 338045e3678SYuan Kang 339045e3678SYuan Kang /* Class 2 operation */ 340045e3678SYuan Kang append_operation(desc, ctx->alg_type | OP_ALG_AS_UPDATE | 341045e3678SYuan Kang OP_ALG_ENCRYPT); 342045e3678SYuan Kang 343045e3678SYuan Kang /* Load data and write to result or context */ 344045e3678SYuan Kang ahash_append_load_str(desc, ctx->ctx_len); 345045e3678SYuan Kang 346045e3678SYuan Kang ctx->sh_desc_update_dma = dma_map_single(jrdev, desc, desc_bytes(desc), 347045e3678SYuan Kang DMA_TO_DEVICE); 348045e3678SYuan Kang if (dma_mapping_error(jrdev, ctx->sh_desc_update_dma)) { 349045e3678SYuan Kang dev_err(jrdev, "unable to map shared descriptor\n"); 350045e3678SYuan Kang return -ENOMEM; 351045e3678SYuan Kang } 352045e3678SYuan Kang #ifdef DEBUG 353514df281SAlex Porosanu print_hex_dump(KERN_ERR, 354514df281SAlex Porosanu "ahash update shdesc@"__stringify(__LINE__)": ", 355045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); 356045e3678SYuan Kang #endif 357045e3678SYuan Kang 358045e3678SYuan Kang /* ahash_update_first shared descriptor */ 359045e3678SYuan Kang desc = ctx->sh_desc_update_first; 360045e3678SYuan Kang 361045e3678SYuan Kang ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INIT, 362045e3678SYuan Kang ctx->ctx_len, ctx); 363045e3678SYuan Kang 364045e3678SYuan Kang ctx->sh_desc_update_first_dma = dma_map_single(jrdev, desc, 365045e3678SYuan Kang desc_bytes(desc), 366045e3678SYuan Kang DMA_TO_DEVICE); 367045e3678SYuan Kang if (dma_mapping_error(jrdev, ctx->sh_desc_update_first_dma)) { 368045e3678SYuan Kang dev_err(jrdev, "unable to map shared descriptor\n"); 369045e3678SYuan Kang return -ENOMEM; 370045e3678SYuan Kang } 371045e3678SYuan Kang #ifdef DEBUG 372514df281SAlex Porosanu print_hex_dump(KERN_ERR, 373514df281SAlex Porosanu "ahash update first shdesc@"__stringify(__LINE__)": ", 374045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); 375045e3678SYuan Kang #endif 376045e3678SYuan Kang 377045e3678SYuan Kang /* ahash_final shared descriptor */ 378045e3678SYuan Kang desc = ctx->sh_desc_fin; 379045e3678SYuan Kang 380045e3678SYuan Kang ahash_ctx_data_to_out(desc, have_key | ctx->alg_type, 381045e3678SYuan Kang OP_ALG_AS_FINALIZE, digestsize, ctx); 382045e3678SYuan Kang 383045e3678SYuan Kang ctx->sh_desc_fin_dma = dma_map_single(jrdev, desc, desc_bytes(desc), 384045e3678SYuan Kang DMA_TO_DEVICE); 385045e3678SYuan Kang if (dma_mapping_error(jrdev, ctx->sh_desc_fin_dma)) { 386045e3678SYuan Kang dev_err(jrdev, "unable to map shared descriptor\n"); 387045e3678SYuan Kang return -ENOMEM; 388045e3678SYuan Kang } 389045e3678SYuan Kang #ifdef DEBUG 390514df281SAlex Porosanu print_hex_dump(KERN_ERR, "ahash final shdesc@"__stringify(__LINE__)": ", 391045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 392045e3678SYuan Kang desc_bytes(desc), 1); 393045e3678SYuan Kang #endif 394045e3678SYuan Kang 395045e3678SYuan Kang /* ahash_finup shared descriptor */ 396045e3678SYuan Kang desc = ctx->sh_desc_finup; 397045e3678SYuan Kang 398045e3678SYuan Kang ahash_ctx_data_to_out(desc, have_key | ctx->alg_type, 399045e3678SYuan Kang OP_ALG_AS_FINALIZE, digestsize, ctx); 400045e3678SYuan Kang 401045e3678SYuan Kang ctx->sh_desc_finup_dma = dma_map_single(jrdev, desc, desc_bytes(desc), 402045e3678SYuan Kang DMA_TO_DEVICE); 403045e3678SYuan Kang if (dma_mapping_error(jrdev, ctx->sh_desc_finup_dma)) { 404045e3678SYuan Kang dev_err(jrdev, "unable to map shared descriptor\n"); 405045e3678SYuan Kang return -ENOMEM; 406045e3678SYuan Kang } 407045e3678SYuan Kang #ifdef DEBUG 408514df281SAlex Porosanu print_hex_dump(KERN_ERR, "ahash finup shdesc@"__stringify(__LINE__)": ", 409045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 410045e3678SYuan Kang desc_bytes(desc), 1); 411045e3678SYuan Kang #endif 412045e3678SYuan Kang 413045e3678SYuan Kang /* ahash_digest shared descriptor */ 414045e3678SYuan Kang desc = ctx->sh_desc_digest; 415045e3678SYuan Kang 416045e3678SYuan Kang ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INITFINAL, 417045e3678SYuan Kang digestsize, ctx); 418045e3678SYuan Kang 419045e3678SYuan Kang ctx->sh_desc_digest_dma = dma_map_single(jrdev, desc, 420045e3678SYuan Kang desc_bytes(desc), 421045e3678SYuan Kang DMA_TO_DEVICE); 422045e3678SYuan Kang if (dma_mapping_error(jrdev, ctx->sh_desc_digest_dma)) { 423045e3678SYuan Kang dev_err(jrdev, "unable to map shared descriptor\n"); 424045e3678SYuan Kang return -ENOMEM; 425045e3678SYuan Kang } 426045e3678SYuan Kang #ifdef DEBUG 427514df281SAlex Porosanu print_hex_dump(KERN_ERR, 428514df281SAlex Porosanu "ahash digest shdesc@"__stringify(__LINE__)": ", 429045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 430045e3678SYuan Kang desc_bytes(desc), 1); 431045e3678SYuan Kang #endif 432045e3678SYuan Kang 433045e3678SYuan Kang return 0; 434045e3678SYuan Kang } 435045e3678SYuan Kang 43666b3e887SKim Phillips static int gen_split_hash_key(struct caam_hash_ctx *ctx, const u8 *key_in, 437045e3678SYuan Kang u32 keylen) 438045e3678SYuan Kang { 439045e3678SYuan Kang return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len, 440045e3678SYuan Kang ctx->split_key_pad_len, key_in, keylen, 441045e3678SYuan Kang ctx->alg_op); 442045e3678SYuan Kang } 443045e3678SYuan Kang 444045e3678SYuan Kang /* Digest hash size if it is too large */ 44566b3e887SKim Phillips static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in, 446045e3678SYuan Kang u32 *keylen, u8 *key_out, u32 digestsize) 447045e3678SYuan Kang { 448045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 449045e3678SYuan Kang u32 *desc; 450045e3678SYuan Kang struct split_key_result result; 451045e3678SYuan Kang dma_addr_t src_dma, dst_dma; 452045e3678SYuan Kang int ret = 0; 453045e3678SYuan Kang 4549c23b7d3SVakul Garg desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA); 4552af8f4a2SKim Phillips if (!desc) { 4562af8f4a2SKim Phillips dev_err(jrdev, "unable to allocate key input memory\n"); 4572af8f4a2SKim Phillips return -ENOMEM; 4582af8f4a2SKim Phillips } 459045e3678SYuan Kang 460045e3678SYuan Kang init_job_desc(desc, 0); 461045e3678SYuan Kang 462045e3678SYuan Kang src_dma = dma_map_single(jrdev, (void *)key_in, *keylen, 463045e3678SYuan Kang DMA_TO_DEVICE); 464045e3678SYuan Kang if (dma_mapping_error(jrdev, src_dma)) { 465045e3678SYuan Kang dev_err(jrdev, "unable to map key input memory\n"); 466045e3678SYuan Kang kfree(desc); 467045e3678SYuan Kang return -ENOMEM; 468045e3678SYuan Kang } 469045e3678SYuan Kang dst_dma = dma_map_single(jrdev, (void *)key_out, digestsize, 470045e3678SYuan Kang DMA_FROM_DEVICE); 471045e3678SYuan Kang if (dma_mapping_error(jrdev, dst_dma)) { 472045e3678SYuan Kang dev_err(jrdev, "unable to map key output memory\n"); 473045e3678SYuan Kang dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE); 474045e3678SYuan Kang kfree(desc); 475045e3678SYuan Kang return -ENOMEM; 476045e3678SYuan Kang } 477045e3678SYuan Kang 478045e3678SYuan Kang /* Job descriptor to perform unkeyed hash on key_in */ 479045e3678SYuan Kang append_operation(desc, ctx->alg_type | OP_ALG_ENCRYPT | 480045e3678SYuan Kang OP_ALG_AS_INITFINAL); 481045e3678SYuan Kang append_seq_in_ptr(desc, src_dma, *keylen, 0); 482045e3678SYuan Kang append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 | 483045e3678SYuan Kang FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG); 484045e3678SYuan Kang append_seq_out_ptr(desc, dst_dma, digestsize, 0); 485045e3678SYuan Kang append_seq_store(desc, digestsize, LDST_CLASS_2_CCB | 486045e3678SYuan Kang LDST_SRCDST_BYTE_CONTEXT); 487045e3678SYuan Kang 488045e3678SYuan Kang #ifdef DEBUG 489514df281SAlex Porosanu print_hex_dump(KERN_ERR, "key_in@"__stringify(__LINE__)": ", 490045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, key_in, *keylen, 1); 491514df281SAlex Porosanu print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ", 492045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); 493045e3678SYuan Kang #endif 494045e3678SYuan Kang 495045e3678SYuan Kang result.err = 0; 496045e3678SYuan Kang init_completion(&result.completion); 497045e3678SYuan Kang 498045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result); 499045e3678SYuan Kang if (!ret) { 500045e3678SYuan Kang /* in progress */ 501045e3678SYuan Kang wait_for_completion_interruptible(&result.completion); 502045e3678SYuan Kang ret = result.err; 503045e3678SYuan Kang #ifdef DEBUG 504514df281SAlex Porosanu print_hex_dump(KERN_ERR, 505514df281SAlex Porosanu "digested key@"__stringify(__LINE__)": ", 506045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, key_in, 507045e3678SYuan Kang digestsize, 1); 508045e3678SYuan Kang #endif 509045e3678SYuan Kang } 510045e3678SYuan Kang dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE); 511045e3678SYuan Kang dma_unmap_single(jrdev, dst_dma, digestsize, DMA_FROM_DEVICE); 512045e3678SYuan Kang 513e11aa9f1SHoria Geanta *keylen = digestsize; 514e11aa9f1SHoria Geanta 515045e3678SYuan Kang kfree(desc); 516045e3678SYuan Kang 517045e3678SYuan Kang return ret; 518045e3678SYuan Kang } 519045e3678SYuan Kang 520045e3678SYuan Kang static int ahash_setkey(struct crypto_ahash *ahash, 521045e3678SYuan Kang const u8 *key, unsigned int keylen) 522045e3678SYuan Kang { 523045e3678SYuan Kang /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */ 524045e3678SYuan Kang static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 }; 525045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 526045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 527045e3678SYuan Kang int blocksize = crypto_tfm_alg_blocksize(&ahash->base); 528045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 529045e3678SYuan Kang int ret = 0; 530045e3678SYuan Kang u8 *hashed_key = NULL; 531045e3678SYuan Kang 532045e3678SYuan Kang #ifdef DEBUG 533045e3678SYuan Kang printk(KERN_ERR "keylen %d\n", keylen); 534045e3678SYuan Kang #endif 535045e3678SYuan Kang 536045e3678SYuan Kang if (keylen > blocksize) { 537045e3678SYuan Kang hashed_key = kmalloc(sizeof(u8) * digestsize, GFP_KERNEL | 538045e3678SYuan Kang GFP_DMA); 539045e3678SYuan Kang if (!hashed_key) 540045e3678SYuan Kang return -ENOMEM; 541045e3678SYuan Kang ret = hash_digest_key(ctx, key, &keylen, hashed_key, 542045e3678SYuan Kang digestsize); 543045e3678SYuan Kang if (ret) 544045e3678SYuan Kang goto badkey; 545045e3678SYuan Kang key = hashed_key; 546045e3678SYuan Kang } 547045e3678SYuan Kang 548045e3678SYuan Kang /* Pick class 2 key length from algorithm submask */ 549045e3678SYuan Kang ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >> 550045e3678SYuan Kang OP_ALG_ALGSEL_SHIFT] * 2; 551045e3678SYuan Kang ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16); 552045e3678SYuan Kang 553045e3678SYuan Kang #ifdef DEBUG 554045e3678SYuan Kang printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n", 555045e3678SYuan Kang ctx->split_key_len, ctx->split_key_pad_len); 556514df281SAlex Porosanu print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ", 557045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1); 558045e3678SYuan Kang #endif 559045e3678SYuan Kang 560045e3678SYuan Kang ret = gen_split_hash_key(ctx, key, keylen); 561045e3678SYuan Kang if (ret) 562045e3678SYuan Kang goto badkey; 563045e3678SYuan Kang 564045e3678SYuan Kang ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len, 565045e3678SYuan Kang DMA_TO_DEVICE); 566045e3678SYuan Kang if (dma_mapping_error(jrdev, ctx->key_dma)) { 567045e3678SYuan Kang dev_err(jrdev, "unable to map key i/o memory\n"); 5683d67be27SHoria Geanta ret = -ENOMEM; 5693d67be27SHoria Geanta goto map_err; 570045e3678SYuan Kang } 571045e3678SYuan Kang #ifdef DEBUG 572514df281SAlex Porosanu print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ", 573045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, 574045e3678SYuan Kang ctx->split_key_pad_len, 1); 575045e3678SYuan Kang #endif 576045e3678SYuan Kang 577045e3678SYuan Kang ret = ahash_set_sh_desc(ahash); 578045e3678SYuan Kang if (ret) { 579045e3678SYuan Kang dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len, 580045e3678SYuan Kang DMA_TO_DEVICE); 581045e3678SYuan Kang } 582045e3678SYuan Kang 5833d67be27SHoria Geanta map_err: 584045e3678SYuan Kang kfree(hashed_key); 585045e3678SYuan Kang return ret; 586045e3678SYuan Kang badkey: 587045e3678SYuan Kang kfree(hashed_key); 588045e3678SYuan Kang crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN); 589045e3678SYuan Kang return -EINVAL; 590045e3678SYuan Kang } 591045e3678SYuan Kang 592045e3678SYuan Kang /* 593045e3678SYuan Kang * ahash_edesc - s/w-extended ahash descriptor 594045e3678SYuan Kang * @dst_dma: physical mapped address of req->result 595045e3678SYuan Kang * @sec4_sg_dma: physical mapped address of h/w link table 596045e3678SYuan Kang * @src_nents: number of segments in input scatterlist 597045e3678SYuan Kang * @sec4_sg_bytes: length of dma mapped sec4_sg space 598045e3678SYuan Kang * @sec4_sg: pointer to h/w link table 599045e3678SYuan Kang * @hw_desc: the h/w job descriptor followed by any referenced link tables 600045e3678SYuan Kang */ 601045e3678SYuan Kang struct ahash_edesc { 602045e3678SYuan Kang dma_addr_t dst_dma; 603045e3678SYuan Kang dma_addr_t sec4_sg_dma; 604045e3678SYuan Kang int src_nents; 605045e3678SYuan Kang int sec4_sg_bytes; 606045e3678SYuan Kang struct sec4_sg_entry *sec4_sg; 607045e3678SYuan Kang u32 hw_desc[0]; 608045e3678SYuan Kang }; 609045e3678SYuan Kang 610045e3678SYuan Kang static inline void ahash_unmap(struct device *dev, 611045e3678SYuan Kang struct ahash_edesc *edesc, 612045e3678SYuan Kang struct ahash_request *req, int dst_len) 613045e3678SYuan Kang { 614045e3678SYuan Kang if (edesc->src_nents) 61513fb8fd7SLABBE Corentin dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE); 616045e3678SYuan Kang if (edesc->dst_dma) 617045e3678SYuan Kang dma_unmap_single(dev, edesc->dst_dma, dst_len, DMA_FROM_DEVICE); 618045e3678SYuan Kang 619045e3678SYuan Kang if (edesc->sec4_sg_bytes) 620045e3678SYuan Kang dma_unmap_single(dev, edesc->sec4_sg_dma, 621045e3678SYuan Kang edesc->sec4_sg_bytes, DMA_TO_DEVICE); 622045e3678SYuan Kang } 623045e3678SYuan Kang 624045e3678SYuan Kang static inline void ahash_unmap_ctx(struct device *dev, 625045e3678SYuan Kang struct ahash_edesc *edesc, 626045e3678SYuan Kang struct ahash_request *req, int dst_len, u32 flag) 627045e3678SYuan Kang { 628045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 629045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 630045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 631045e3678SYuan Kang 632045e3678SYuan Kang if (state->ctx_dma) 633045e3678SYuan Kang dma_unmap_single(dev, state->ctx_dma, ctx->ctx_len, flag); 634045e3678SYuan Kang ahash_unmap(dev, edesc, req, dst_len); 635045e3678SYuan Kang } 636045e3678SYuan Kang 637045e3678SYuan Kang static void ahash_done(struct device *jrdev, u32 *desc, u32 err, 638045e3678SYuan Kang void *context) 639045e3678SYuan Kang { 640045e3678SYuan Kang struct ahash_request *req = context; 641045e3678SYuan Kang struct ahash_edesc *edesc; 642045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 643045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 644045e3678SYuan Kang #ifdef DEBUG 645045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 646045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 647045e3678SYuan Kang 648045e3678SYuan Kang dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); 649045e3678SYuan Kang #endif 650045e3678SYuan Kang 651045e3678SYuan Kang edesc = (struct ahash_edesc *)((char *)desc - 652045e3678SYuan Kang offsetof(struct ahash_edesc, hw_desc)); 653fa9659cdSMarek Vasut if (err) 654fa9659cdSMarek Vasut caam_jr_strstatus(jrdev, err); 655045e3678SYuan Kang 656045e3678SYuan Kang ahash_unmap(jrdev, edesc, req, digestsize); 657045e3678SYuan Kang kfree(edesc); 658045e3678SYuan Kang 659045e3678SYuan Kang #ifdef DEBUG 660514df281SAlex Porosanu print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ", 661045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx, 662045e3678SYuan Kang ctx->ctx_len, 1); 663045e3678SYuan Kang if (req->result) 664514df281SAlex Porosanu print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ", 665045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, req->result, 666045e3678SYuan Kang digestsize, 1); 667045e3678SYuan Kang #endif 668045e3678SYuan Kang 669045e3678SYuan Kang req->base.complete(&req->base, err); 670045e3678SYuan Kang } 671045e3678SYuan Kang 672045e3678SYuan Kang static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err, 673045e3678SYuan Kang void *context) 674045e3678SYuan Kang { 675045e3678SYuan Kang struct ahash_request *req = context; 676045e3678SYuan Kang struct ahash_edesc *edesc; 677045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 678045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 679045e3678SYuan Kang #ifdef DEBUG 680045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 681045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 682045e3678SYuan Kang 683045e3678SYuan Kang dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); 684045e3678SYuan Kang #endif 685045e3678SYuan Kang 686045e3678SYuan Kang edesc = (struct ahash_edesc *)((char *)desc - 687045e3678SYuan Kang offsetof(struct ahash_edesc, hw_desc)); 688fa9659cdSMarek Vasut if (err) 689fa9659cdSMarek Vasut caam_jr_strstatus(jrdev, err); 690045e3678SYuan Kang 691045e3678SYuan Kang ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL); 692045e3678SYuan Kang kfree(edesc); 693045e3678SYuan Kang 694045e3678SYuan Kang #ifdef DEBUG 695514df281SAlex Porosanu print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ", 696045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx, 697045e3678SYuan Kang ctx->ctx_len, 1); 698045e3678SYuan Kang if (req->result) 699514df281SAlex Porosanu print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ", 700045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, req->result, 701045e3678SYuan Kang digestsize, 1); 702045e3678SYuan Kang #endif 703045e3678SYuan Kang 704045e3678SYuan Kang req->base.complete(&req->base, err); 705045e3678SYuan Kang } 706045e3678SYuan Kang 707045e3678SYuan Kang static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err, 708045e3678SYuan Kang void *context) 709045e3678SYuan Kang { 710045e3678SYuan Kang struct ahash_request *req = context; 711045e3678SYuan Kang struct ahash_edesc *edesc; 712045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 713045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 714045e3678SYuan Kang #ifdef DEBUG 715045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 716045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 717045e3678SYuan Kang 718045e3678SYuan Kang dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); 719045e3678SYuan Kang #endif 720045e3678SYuan Kang 721045e3678SYuan Kang edesc = (struct ahash_edesc *)((char *)desc - 722045e3678SYuan Kang offsetof(struct ahash_edesc, hw_desc)); 723fa9659cdSMarek Vasut if (err) 724fa9659cdSMarek Vasut caam_jr_strstatus(jrdev, err); 725045e3678SYuan Kang 726bc9e05f9SHoria Geanta ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_TO_DEVICE); 727045e3678SYuan Kang kfree(edesc); 728045e3678SYuan Kang 729045e3678SYuan Kang #ifdef DEBUG 730514df281SAlex Porosanu print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ", 731045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx, 732045e3678SYuan Kang ctx->ctx_len, 1); 733045e3678SYuan Kang if (req->result) 734514df281SAlex Porosanu print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ", 735045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, req->result, 736045e3678SYuan Kang digestsize, 1); 737045e3678SYuan Kang #endif 738045e3678SYuan Kang 739045e3678SYuan Kang req->base.complete(&req->base, err); 740045e3678SYuan Kang } 741045e3678SYuan Kang 742045e3678SYuan Kang static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err, 743045e3678SYuan Kang void *context) 744045e3678SYuan Kang { 745045e3678SYuan Kang struct ahash_request *req = context; 746045e3678SYuan Kang struct ahash_edesc *edesc; 747045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 748045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 749045e3678SYuan Kang #ifdef DEBUG 750045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 751045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 752045e3678SYuan Kang 753045e3678SYuan Kang dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); 754045e3678SYuan Kang #endif 755045e3678SYuan Kang 756045e3678SYuan Kang edesc = (struct ahash_edesc *)((char *)desc - 757045e3678SYuan Kang offsetof(struct ahash_edesc, hw_desc)); 758fa9659cdSMarek Vasut if (err) 759fa9659cdSMarek Vasut caam_jr_strstatus(jrdev, err); 760045e3678SYuan Kang 761ef62b231SHoria Geanta ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_FROM_DEVICE); 762045e3678SYuan Kang kfree(edesc); 763045e3678SYuan Kang 764045e3678SYuan Kang #ifdef DEBUG 765514df281SAlex Porosanu print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ", 766045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx, 767045e3678SYuan Kang ctx->ctx_len, 1); 768045e3678SYuan Kang if (req->result) 769514df281SAlex Porosanu print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ", 770045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, req->result, 771045e3678SYuan Kang digestsize, 1); 772045e3678SYuan Kang #endif 773045e3678SYuan Kang 774045e3678SYuan Kang req->base.complete(&req->base, err); 775045e3678SYuan Kang } 776045e3678SYuan Kang 777045e3678SYuan Kang /* submit update job descriptor */ 778045e3678SYuan Kang static int ahash_update_ctx(struct ahash_request *req) 779045e3678SYuan Kang { 780045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 781045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 782045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 783045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 784045e3678SYuan Kang gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | 785045e3678SYuan Kang CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; 786045e3678SYuan Kang u8 *buf = state->current_buf ? state->buf_1 : state->buf_0; 787045e3678SYuan Kang int *buflen = state->current_buf ? &state->buflen_1 : &state->buflen_0; 788045e3678SYuan Kang u8 *next_buf = state->current_buf ? state->buf_0 : state->buf_1; 789045e3678SYuan Kang int *next_buflen = state->current_buf ? &state->buflen_0 : 790045e3678SYuan Kang &state->buflen_1, last_buflen; 791045e3678SYuan Kang int in_len = *buflen + req->nbytes, to_hash; 792045e3678SYuan Kang u32 *sh_desc = ctx->sh_desc_update, *desc; 793045e3678SYuan Kang dma_addr_t ptr = ctx->sh_desc_update_dma; 794045e3678SYuan Kang int src_nents, sec4_sg_bytes, sec4_sg_src_index; 795045e3678SYuan Kang struct ahash_edesc *edesc; 796045e3678SYuan Kang int ret = 0; 797045e3678SYuan Kang int sh_len; 798045e3678SYuan Kang 799045e3678SYuan Kang last_buflen = *next_buflen; 800045e3678SYuan Kang *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1); 801045e3678SYuan Kang to_hash = in_len - *next_buflen; 802045e3678SYuan Kang 803045e3678SYuan Kang if (to_hash) { 80413fb8fd7SLABBE Corentin src_nents = sg_nents_for_len(req->src, 80513fb8fd7SLABBE Corentin req->nbytes - (*next_buflen)); 806f9970c28SLABBE Corentin if (src_nents < 0) { 807f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 808f9970c28SLABBE Corentin return src_nents; 809f9970c28SLABBE Corentin } 810045e3678SYuan Kang sec4_sg_src_index = 1 + (*buflen ? 1 : 0); 811045e3678SYuan Kang sec4_sg_bytes = (sec4_sg_src_index + src_nents) * 812045e3678SYuan Kang sizeof(struct sec4_sg_entry); 813045e3678SYuan Kang 814045e3678SYuan Kang /* 815045e3678SYuan Kang * allocate space for base edesc and hw desc commands, 816045e3678SYuan Kang * link tables 817045e3678SYuan Kang */ 818dde20ae9SVictoria Milhoan edesc = kzalloc(sizeof(*edesc) + DESC_JOB_IO_LEN + 819045e3678SYuan Kang sec4_sg_bytes, GFP_DMA | flags); 820045e3678SYuan Kang if (!edesc) { 821045e3678SYuan Kang dev_err(jrdev, 822045e3678SYuan Kang "could not allocate extended descriptor\n"); 823045e3678SYuan Kang return -ENOMEM; 824045e3678SYuan Kang } 825045e3678SYuan Kang 826045e3678SYuan Kang edesc->src_nents = src_nents; 827045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 828045e3678SYuan Kang edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) + 829045e3678SYuan Kang DESC_JOB_IO_LEN; 830045e3678SYuan Kang 831ce572085SHoria Geanta ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len, 832045e3678SYuan Kang edesc->sec4_sg, DMA_BIDIRECTIONAL); 833ce572085SHoria Geanta if (ret) 834ce572085SHoria Geanta return ret; 835045e3678SYuan Kang 836045e3678SYuan Kang state->buf_dma = try_buf_map_to_sec4_sg(jrdev, 837045e3678SYuan Kang edesc->sec4_sg + 1, 838045e3678SYuan Kang buf, state->buf_dma, 839c7556ff7SRussell King *buflen, last_buflen); 840045e3678SYuan Kang 841045e3678SYuan Kang if (src_nents) { 842045e3678SYuan Kang src_map_to_sec4_sg(jrdev, req->src, src_nents, 84313fb8fd7SLABBE Corentin edesc->sec4_sg + sec4_sg_src_index); 8448af7b0f8SVictoria Milhoan if (*next_buflen) 845307fd543SCristian Stoica scatterwalk_map_and_copy(next_buf, req->src, 846307fd543SCristian Stoica to_hash - *buflen, 847307fd543SCristian Stoica *next_buflen, 0); 848045e3678SYuan Kang } else { 849045e3678SYuan Kang (edesc->sec4_sg + sec4_sg_src_index - 1)->len |= 850261ea058SHoria Geantă cpu_to_caam32(SEC4_SG_LEN_FIN); 851045e3678SYuan Kang } 852045e3678SYuan Kang 8538af7b0f8SVictoria Milhoan state->current_buf = !state->current_buf; 8548af7b0f8SVictoria Milhoan 855045e3678SYuan Kang sh_len = desc_len(sh_desc); 856045e3678SYuan Kang desc = edesc->hw_desc; 857045e3678SYuan Kang init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | 858045e3678SYuan Kang HDR_REVERSE); 859045e3678SYuan Kang 8601da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, 8611da2be33SRuchika Gupta sec4_sg_bytes, 8621da2be33SRuchika Gupta DMA_TO_DEVICE); 863ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 864ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 865ce572085SHoria Geanta return -ENOMEM; 866ce572085SHoria Geanta } 8671da2be33SRuchika Gupta 868045e3678SYuan Kang append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + 869045e3678SYuan Kang to_hash, LDST_SGF); 870045e3678SYuan Kang 871045e3678SYuan Kang append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0); 872045e3678SYuan Kang 873045e3678SYuan Kang #ifdef DEBUG 874514df281SAlex Porosanu print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ", 875045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 876045e3678SYuan Kang desc_bytes(desc), 1); 877045e3678SYuan Kang #endif 878045e3678SYuan Kang 879045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req); 880045e3678SYuan Kang if (!ret) { 881045e3678SYuan Kang ret = -EINPROGRESS; 882045e3678SYuan Kang } else { 883045e3678SYuan Kang ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, 884045e3678SYuan Kang DMA_BIDIRECTIONAL); 885045e3678SYuan Kang kfree(edesc); 886045e3678SYuan Kang } 887045e3678SYuan Kang } else if (*next_buflen) { 888307fd543SCristian Stoica scatterwalk_map_and_copy(buf + *buflen, req->src, 0, 889307fd543SCristian Stoica req->nbytes, 0); 890045e3678SYuan Kang *buflen = *next_buflen; 891045e3678SYuan Kang *next_buflen = last_buflen; 892045e3678SYuan Kang } 893045e3678SYuan Kang #ifdef DEBUG 894514df281SAlex Porosanu print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ", 895045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1); 896514df281SAlex Porosanu print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ", 897045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, next_buf, 898045e3678SYuan Kang *next_buflen, 1); 899045e3678SYuan Kang #endif 900045e3678SYuan Kang 901045e3678SYuan Kang return ret; 902045e3678SYuan Kang } 903045e3678SYuan Kang 904045e3678SYuan Kang static int ahash_final_ctx(struct ahash_request *req) 905045e3678SYuan Kang { 906045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 907045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 908045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 909045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 910045e3678SYuan Kang gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | 911045e3678SYuan Kang CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; 912045e3678SYuan Kang u8 *buf = state->current_buf ? state->buf_1 : state->buf_0; 913045e3678SYuan Kang int buflen = state->current_buf ? state->buflen_1 : state->buflen_0; 914045e3678SYuan Kang int last_buflen = state->current_buf ? state->buflen_0 : 915045e3678SYuan Kang state->buflen_1; 916045e3678SYuan Kang u32 *sh_desc = ctx->sh_desc_fin, *desc; 917045e3678SYuan Kang dma_addr_t ptr = ctx->sh_desc_fin_dma; 918b310c178SHoria Geant? int sec4_sg_bytes, sec4_sg_src_index; 919045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 920045e3678SYuan Kang struct ahash_edesc *edesc; 921045e3678SYuan Kang int ret = 0; 922045e3678SYuan Kang int sh_len; 923045e3678SYuan Kang 924b310c178SHoria Geant? sec4_sg_src_index = 1 + (buflen ? 1 : 0); 925b310c178SHoria Geant? sec4_sg_bytes = sec4_sg_src_index * sizeof(struct sec4_sg_entry); 926045e3678SYuan Kang 927045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 928dde20ae9SVictoria Milhoan edesc = kzalloc(sizeof(*edesc) + DESC_JOB_IO_LEN + sec4_sg_bytes, 929dde20ae9SVictoria Milhoan GFP_DMA | flags); 930045e3678SYuan Kang if (!edesc) { 931045e3678SYuan Kang dev_err(jrdev, "could not allocate extended descriptor\n"); 932045e3678SYuan Kang return -ENOMEM; 933045e3678SYuan Kang } 934045e3678SYuan Kang 935045e3678SYuan Kang sh_len = desc_len(sh_desc); 936045e3678SYuan Kang desc = edesc->hw_desc; 937045e3678SYuan Kang init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE); 938045e3678SYuan Kang 939045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 940045e3678SYuan Kang edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) + 941045e3678SYuan Kang DESC_JOB_IO_LEN; 942045e3678SYuan Kang edesc->src_nents = 0; 943045e3678SYuan Kang 944ce572085SHoria Geanta ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len, 945ce572085SHoria Geanta edesc->sec4_sg, DMA_TO_DEVICE); 946ce572085SHoria Geanta if (ret) 947ce572085SHoria Geanta return ret; 948045e3678SYuan Kang 949045e3678SYuan Kang state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, 950045e3678SYuan Kang buf, state->buf_dma, buflen, 951045e3678SYuan Kang last_buflen); 952261ea058SHoria Geantă (edesc->sec4_sg + sec4_sg_src_index - 1)->len |= 953261ea058SHoria Geantă cpu_to_caam32(SEC4_SG_LEN_FIN); 954045e3678SYuan Kang 9551da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, 9561da2be33SRuchika Gupta sec4_sg_bytes, DMA_TO_DEVICE); 957ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 958ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 959ce572085SHoria Geanta return -ENOMEM; 960ce572085SHoria Geanta } 9611da2be33SRuchika Gupta 962045e3678SYuan Kang append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen, 963045e3678SYuan Kang LDST_SGF); 964045e3678SYuan Kang 965045e3678SYuan Kang edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result, 966045e3678SYuan Kang digestsize); 967ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->dst_dma)) { 968ce572085SHoria Geanta dev_err(jrdev, "unable to map dst\n"); 969ce572085SHoria Geanta return -ENOMEM; 970ce572085SHoria Geanta } 971045e3678SYuan Kang 972045e3678SYuan Kang #ifdef DEBUG 973514df281SAlex Porosanu print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ", 974045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); 975045e3678SYuan Kang #endif 976045e3678SYuan Kang 977045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req); 978045e3678SYuan Kang if (!ret) { 979045e3678SYuan Kang ret = -EINPROGRESS; 980045e3678SYuan Kang } else { 981045e3678SYuan Kang ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE); 982045e3678SYuan Kang kfree(edesc); 983045e3678SYuan Kang } 984045e3678SYuan Kang 985045e3678SYuan Kang return ret; 986045e3678SYuan Kang } 987045e3678SYuan Kang 988045e3678SYuan Kang static int ahash_finup_ctx(struct ahash_request *req) 989045e3678SYuan Kang { 990045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 991045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 992045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 993045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 994045e3678SYuan Kang gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | 995045e3678SYuan Kang CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; 996045e3678SYuan Kang u8 *buf = state->current_buf ? state->buf_1 : state->buf_0; 997045e3678SYuan Kang int buflen = state->current_buf ? state->buflen_1 : state->buflen_0; 998045e3678SYuan Kang int last_buflen = state->current_buf ? state->buflen_0 : 999045e3678SYuan Kang state->buflen_1; 1000045e3678SYuan Kang u32 *sh_desc = ctx->sh_desc_finup, *desc; 1001045e3678SYuan Kang dma_addr_t ptr = ctx->sh_desc_finup_dma; 1002045e3678SYuan Kang int sec4_sg_bytes, sec4_sg_src_index; 1003045e3678SYuan Kang int src_nents; 1004045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 1005045e3678SYuan Kang struct ahash_edesc *edesc; 1006045e3678SYuan Kang int ret = 0; 1007045e3678SYuan Kang int sh_len; 1008045e3678SYuan Kang 100913fb8fd7SLABBE Corentin src_nents = sg_nents_for_len(req->src, req->nbytes); 1010f9970c28SLABBE Corentin if (src_nents < 0) { 1011f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1012f9970c28SLABBE Corentin return src_nents; 1013f9970c28SLABBE Corentin } 1014045e3678SYuan Kang sec4_sg_src_index = 1 + (buflen ? 1 : 0); 1015045e3678SYuan Kang sec4_sg_bytes = (sec4_sg_src_index + src_nents) * 1016045e3678SYuan Kang sizeof(struct sec4_sg_entry); 1017045e3678SYuan Kang 1018045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 1019dde20ae9SVictoria Milhoan edesc = kzalloc(sizeof(*edesc) + DESC_JOB_IO_LEN + sec4_sg_bytes, 1020dde20ae9SVictoria Milhoan GFP_DMA | flags); 1021045e3678SYuan Kang if (!edesc) { 1022045e3678SYuan Kang dev_err(jrdev, "could not allocate extended descriptor\n"); 1023045e3678SYuan Kang return -ENOMEM; 1024045e3678SYuan Kang } 1025045e3678SYuan Kang 1026045e3678SYuan Kang sh_len = desc_len(sh_desc); 1027045e3678SYuan Kang desc = edesc->hw_desc; 1028045e3678SYuan Kang init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE); 1029045e3678SYuan Kang 1030045e3678SYuan Kang edesc->src_nents = src_nents; 1031045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 1032045e3678SYuan Kang edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) + 1033045e3678SYuan Kang DESC_JOB_IO_LEN; 1034045e3678SYuan Kang 1035ce572085SHoria Geanta ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len, 1036ce572085SHoria Geanta edesc->sec4_sg, DMA_TO_DEVICE); 1037ce572085SHoria Geanta if (ret) 1038ce572085SHoria Geanta return ret; 1039045e3678SYuan Kang 1040045e3678SYuan Kang state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, 1041045e3678SYuan Kang buf, state->buf_dma, buflen, 1042045e3678SYuan Kang last_buflen); 1043045e3678SYuan Kang 1044045e3678SYuan Kang src_map_to_sec4_sg(jrdev, req->src, src_nents, edesc->sec4_sg + 104513fb8fd7SLABBE Corentin sec4_sg_src_index); 1046045e3678SYuan Kang 10471da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, 10481da2be33SRuchika Gupta sec4_sg_bytes, DMA_TO_DEVICE); 1049ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 1050ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 1051ce572085SHoria Geanta return -ENOMEM; 1052ce572085SHoria Geanta } 10531da2be33SRuchika Gupta 1054045e3678SYuan Kang append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + 1055045e3678SYuan Kang buflen + req->nbytes, LDST_SGF); 1056045e3678SYuan Kang 1057045e3678SYuan Kang edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result, 1058045e3678SYuan Kang digestsize); 1059ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->dst_dma)) { 1060ce572085SHoria Geanta dev_err(jrdev, "unable to map dst\n"); 1061ce572085SHoria Geanta return -ENOMEM; 1062ce572085SHoria Geanta } 1063045e3678SYuan Kang 1064045e3678SYuan Kang #ifdef DEBUG 1065514df281SAlex Porosanu print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ", 1066045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); 1067045e3678SYuan Kang #endif 1068045e3678SYuan Kang 1069045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req); 1070045e3678SYuan Kang if (!ret) { 1071045e3678SYuan Kang ret = -EINPROGRESS; 1072045e3678SYuan Kang } else { 1073045e3678SYuan Kang ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE); 1074045e3678SYuan Kang kfree(edesc); 1075045e3678SYuan Kang } 1076045e3678SYuan Kang 1077045e3678SYuan Kang return ret; 1078045e3678SYuan Kang } 1079045e3678SYuan Kang 1080045e3678SYuan Kang static int ahash_digest(struct ahash_request *req) 1081045e3678SYuan Kang { 1082045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1083045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1084045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 1085045e3678SYuan Kang gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | 1086045e3678SYuan Kang CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; 1087045e3678SYuan Kang u32 *sh_desc = ctx->sh_desc_digest, *desc; 1088045e3678SYuan Kang dma_addr_t ptr = ctx->sh_desc_digest_dma; 1089045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 1090045e3678SYuan Kang int src_nents, sec4_sg_bytes; 1091045e3678SYuan Kang dma_addr_t src_dma; 1092045e3678SYuan Kang struct ahash_edesc *edesc; 1093045e3678SYuan Kang int ret = 0; 1094045e3678SYuan Kang u32 options; 1095045e3678SYuan Kang int sh_len; 1096045e3678SYuan Kang 1097*3d5a2db6SRussell King src_nents = sg_nents_for_len(req->src, req->nbytes); 1098f9970c28SLABBE Corentin if (src_nents < 0) { 1099f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1100f9970c28SLABBE Corentin return src_nents; 1101f9970c28SLABBE Corentin } 1102*3d5a2db6SRussell King dma_map_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE); 1103*3d5a2db6SRussell King if (src_nents > 1) 1104045e3678SYuan Kang sec4_sg_bytes = src_nents * sizeof(struct sec4_sg_entry); 1105*3d5a2db6SRussell King else 1106*3d5a2db6SRussell King sec4_sg_bytes = 0; 1107045e3678SYuan Kang 1108045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 1109dde20ae9SVictoria Milhoan edesc = kzalloc(sizeof(*edesc) + sec4_sg_bytes + DESC_JOB_IO_LEN, 1110dde20ae9SVictoria Milhoan GFP_DMA | flags); 1111045e3678SYuan Kang if (!edesc) { 1112045e3678SYuan Kang dev_err(jrdev, "could not allocate extended descriptor\n"); 1113045e3678SYuan Kang return -ENOMEM; 1114045e3678SYuan Kang } 1115045e3678SYuan Kang edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) + 1116045e3678SYuan Kang DESC_JOB_IO_LEN; 111745e9af78SHoria Geanta edesc->sec4_sg_bytes = sec4_sg_bytes; 1118045e3678SYuan Kang edesc->src_nents = src_nents; 1119045e3678SYuan Kang 1120045e3678SYuan Kang sh_len = desc_len(sh_desc); 1121045e3678SYuan Kang desc = edesc->hw_desc; 1122045e3678SYuan Kang init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE); 1123045e3678SYuan Kang 1124*3d5a2db6SRussell King if (src_nents > 1) { 1125045e3678SYuan Kang sg_to_sec4_sg_last(req->src, src_nents, edesc->sec4_sg, 0); 11261da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, 11271da2be33SRuchika Gupta sec4_sg_bytes, DMA_TO_DEVICE); 1128ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 1129ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 1130ce572085SHoria Geanta return -ENOMEM; 1131ce572085SHoria Geanta } 1132045e3678SYuan Kang src_dma = edesc->sec4_sg_dma; 1133045e3678SYuan Kang options = LDST_SGF; 1134045e3678SYuan Kang } else { 1135045e3678SYuan Kang src_dma = sg_dma_address(req->src); 1136045e3678SYuan Kang options = 0; 1137045e3678SYuan Kang } 1138045e3678SYuan Kang append_seq_in_ptr(desc, src_dma, req->nbytes, options); 1139045e3678SYuan Kang 1140045e3678SYuan Kang edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result, 1141045e3678SYuan Kang digestsize); 1142ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->dst_dma)) { 1143ce572085SHoria Geanta dev_err(jrdev, "unable to map dst\n"); 1144ce572085SHoria Geanta return -ENOMEM; 1145ce572085SHoria Geanta } 1146045e3678SYuan Kang 1147045e3678SYuan Kang #ifdef DEBUG 1148514df281SAlex Porosanu print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ", 1149045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); 1150045e3678SYuan Kang #endif 1151045e3678SYuan Kang 1152045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, ahash_done, req); 1153045e3678SYuan Kang if (!ret) { 1154045e3678SYuan Kang ret = -EINPROGRESS; 1155045e3678SYuan Kang } else { 1156045e3678SYuan Kang ahash_unmap(jrdev, edesc, req, digestsize); 1157045e3678SYuan Kang kfree(edesc); 1158045e3678SYuan Kang } 1159045e3678SYuan Kang 1160045e3678SYuan Kang return ret; 1161045e3678SYuan Kang } 1162045e3678SYuan Kang 1163045e3678SYuan Kang /* submit ahash final if it the first job descriptor */ 1164045e3678SYuan Kang static int ahash_final_no_ctx(struct ahash_request *req) 1165045e3678SYuan Kang { 1166045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1167045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1168045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1169045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 1170045e3678SYuan Kang gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | 1171045e3678SYuan Kang CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; 1172045e3678SYuan Kang u8 *buf = state->current_buf ? state->buf_1 : state->buf_0; 1173045e3678SYuan Kang int buflen = state->current_buf ? state->buflen_1 : state->buflen_0; 1174045e3678SYuan Kang u32 *sh_desc = ctx->sh_desc_digest, *desc; 1175045e3678SYuan Kang dma_addr_t ptr = ctx->sh_desc_digest_dma; 1176045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 1177045e3678SYuan Kang struct ahash_edesc *edesc; 1178045e3678SYuan Kang int ret = 0; 1179045e3678SYuan Kang int sh_len; 1180045e3678SYuan Kang 1181045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 1182dde20ae9SVictoria Milhoan edesc = kzalloc(sizeof(*edesc) + DESC_JOB_IO_LEN, GFP_DMA | flags); 1183045e3678SYuan Kang if (!edesc) { 1184045e3678SYuan Kang dev_err(jrdev, "could not allocate extended descriptor\n"); 1185045e3678SYuan Kang return -ENOMEM; 1186045e3678SYuan Kang } 1187045e3678SYuan Kang 1188060e234eSYanjiang Jin edesc->sec4_sg_bytes = 0; 1189045e3678SYuan Kang sh_len = desc_len(sh_desc); 1190045e3678SYuan Kang desc = edesc->hw_desc; 1191045e3678SYuan Kang init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE); 1192045e3678SYuan Kang 1193045e3678SYuan Kang state->buf_dma = dma_map_single(jrdev, buf, buflen, DMA_TO_DEVICE); 1194ce572085SHoria Geanta if (dma_mapping_error(jrdev, state->buf_dma)) { 1195ce572085SHoria Geanta dev_err(jrdev, "unable to map src\n"); 1196ce572085SHoria Geanta return -ENOMEM; 1197ce572085SHoria Geanta } 1198045e3678SYuan Kang 1199045e3678SYuan Kang append_seq_in_ptr(desc, state->buf_dma, buflen, 0); 1200045e3678SYuan Kang 1201045e3678SYuan Kang edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result, 1202045e3678SYuan Kang digestsize); 1203ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->dst_dma)) { 1204ce572085SHoria Geanta dev_err(jrdev, "unable to map dst\n"); 1205ce572085SHoria Geanta return -ENOMEM; 1206ce572085SHoria Geanta } 1207045e3678SYuan Kang edesc->src_nents = 0; 1208045e3678SYuan Kang 1209045e3678SYuan Kang #ifdef DEBUG 1210514df281SAlex Porosanu print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ", 1211045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); 1212045e3678SYuan Kang #endif 1213045e3678SYuan Kang 1214045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, ahash_done, req); 1215045e3678SYuan Kang if (!ret) { 1216045e3678SYuan Kang ret = -EINPROGRESS; 1217045e3678SYuan Kang } else { 1218045e3678SYuan Kang ahash_unmap(jrdev, edesc, req, digestsize); 1219045e3678SYuan Kang kfree(edesc); 1220045e3678SYuan Kang } 1221045e3678SYuan Kang 1222045e3678SYuan Kang return ret; 1223045e3678SYuan Kang } 1224045e3678SYuan Kang 1225045e3678SYuan Kang /* submit ahash update if it the first job descriptor after update */ 1226045e3678SYuan Kang static int ahash_update_no_ctx(struct ahash_request *req) 1227045e3678SYuan Kang { 1228045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1229045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1230045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1231045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 1232045e3678SYuan Kang gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | 1233045e3678SYuan Kang CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; 1234045e3678SYuan Kang u8 *buf = state->current_buf ? state->buf_1 : state->buf_0; 1235045e3678SYuan Kang int *buflen = state->current_buf ? &state->buflen_1 : &state->buflen_0; 1236045e3678SYuan Kang u8 *next_buf = state->current_buf ? state->buf_0 : state->buf_1; 1237045e3678SYuan Kang int *next_buflen = state->current_buf ? &state->buflen_0 : 1238045e3678SYuan Kang &state->buflen_1; 1239045e3678SYuan Kang int in_len = *buflen + req->nbytes, to_hash; 1240045e3678SYuan Kang int sec4_sg_bytes, src_nents; 1241045e3678SYuan Kang struct ahash_edesc *edesc; 1242045e3678SYuan Kang u32 *desc, *sh_desc = ctx->sh_desc_update_first; 1243045e3678SYuan Kang dma_addr_t ptr = ctx->sh_desc_update_first_dma; 1244045e3678SYuan Kang int ret = 0; 1245045e3678SYuan Kang int sh_len; 1246045e3678SYuan Kang 1247045e3678SYuan Kang *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1); 1248045e3678SYuan Kang to_hash = in_len - *next_buflen; 1249045e3678SYuan Kang 1250045e3678SYuan Kang if (to_hash) { 125113fb8fd7SLABBE Corentin src_nents = sg_nents_for_len(req->src, 1252*3d5a2db6SRussell King req->nbytes - *next_buflen); 1253f9970c28SLABBE Corentin if (src_nents < 0) { 1254f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1255f9970c28SLABBE Corentin return src_nents; 1256f9970c28SLABBE Corentin } 1257045e3678SYuan Kang sec4_sg_bytes = (1 + src_nents) * 1258045e3678SYuan Kang sizeof(struct sec4_sg_entry); 1259045e3678SYuan Kang 1260045e3678SYuan Kang /* 1261045e3678SYuan Kang * allocate space for base edesc and hw desc commands, 1262045e3678SYuan Kang * link tables 1263045e3678SYuan Kang */ 1264dde20ae9SVictoria Milhoan edesc = kzalloc(sizeof(*edesc) + DESC_JOB_IO_LEN + 1265045e3678SYuan Kang sec4_sg_bytes, GFP_DMA | flags); 1266045e3678SYuan Kang if (!edesc) { 1267045e3678SYuan Kang dev_err(jrdev, 1268045e3678SYuan Kang "could not allocate extended descriptor\n"); 1269045e3678SYuan Kang return -ENOMEM; 1270045e3678SYuan Kang } 1271045e3678SYuan Kang 1272045e3678SYuan Kang edesc->src_nents = src_nents; 1273045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 1274045e3678SYuan Kang edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) + 1275045e3678SYuan Kang DESC_JOB_IO_LEN; 127676b99080SHoria Geanta edesc->dst_dma = 0; 1277045e3678SYuan Kang 1278045e3678SYuan Kang state->buf_dma = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, 1279045e3678SYuan Kang buf, *buflen); 1280045e3678SYuan Kang src_map_to_sec4_sg(jrdev, req->src, src_nents, 128113fb8fd7SLABBE Corentin edesc->sec4_sg + 1); 1282045e3678SYuan Kang if (*next_buflen) { 1283307fd543SCristian Stoica scatterwalk_map_and_copy(next_buf, req->src, 1284307fd543SCristian Stoica to_hash - *buflen, 1285307fd543SCristian Stoica *next_buflen, 0); 1286045e3678SYuan Kang } 1287045e3678SYuan Kang 12888af7b0f8SVictoria Milhoan state->current_buf = !state->current_buf; 12898af7b0f8SVictoria Milhoan 1290045e3678SYuan Kang sh_len = desc_len(sh_desc); 1291045e3678SYuan Kang desc = edesc->hw_desc; 1292045e3678SYuan Kang init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | 1293045e3678SYuan Kang HDR_REVERSE); 1294045e3678SYuan Kang 12951da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, 12961da2be33SRuchika Gupta sec4_sg_bytes, 12971da2be33SRuchika Gupta DMA_TO_DEVICE); 1298ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 1299ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 1300ce572085SHoria Geanta return -ENOMEM; 1301ce572085SHoria Geanta } 13021da2be33SRuchika Gupta 1303045e3678SYuan Kang append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF); 1304045e3678SYuan Kang 1305ce572085SHoria Geanta ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len); 1306ce572085SHoria Geanta if (ret) 1307ce572085SHoria Geanta return ret; 1308045e3678SYuan Kang 1309045e3678SYuan Kang #ifdef DEBUG 1310514df281SAlex Porosanu print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ", 1311045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 1312045e3678SYuan Kang desc_bytes(desc), 1); 1313045e3678SYuan Kang #endif 1314045e3678SYuan Kang 1315045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req); 1316045e3678SYuan Kang if (!ret) { 1317045e3678SYuan Kang ret = -EINPROGRESS; 1318045e3678SYuan Kang state->update = ahash_update_ctx; 1319045e3678SYuan Kang state->finup = ahash_finup_ctx; 1320045e3678SYuan Kang state->final = ahash_final_ctx; 1321045e3678SYuan Kang } else { 1322045e3678SYuan Kang ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, 1323045e3678SYuan Kang DMA_TO_DEVICE); 1324045e3678SYuan Kang kfree(edesc); 1325045e3678SYuan Kang } 1326045e3678SYuan Kang } else if (*next_buflen) { 1327307fd543SCristian Stoica scatterwalk_map_and_copy(buf + *buflen, req->src, 0, 1328307fd543SCristian Stoica req->nbytes, 0); 1329045e3678SYuan Kang *buflen = *next_buflen; 1330045e3678SYuan Kang *next_buflen = 0; 1331045e3678SYuan Kang } 1332045e3678SYuan Kang #ifdef DEBUG 1333514df281SAlex Porosanu print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ", 1334045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1); 1335514df281SAlex Porosanu print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ", 1336045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, next_buf, 1337045e3678SYuan Kang *next_buflen, 1); 1338045e3678SYuan Kang #endif 1339045e3678SYuan Kang 1340045e3678SYuan Kang return ret; 1341045e3678SYuan Kang } 1342045e3678SYuan Kang 1343045e3678SYuan Kang /* submit ahash finup if it the first job descriptor after update */ 1344045e3678SYuan Kang static int ahash_finup_no_ctx(struct ahash_request *req) 1345045e3678SYuan Kang { 1346045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1347045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1348045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1349045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 1350045e3678SYuan Kang gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | 1351045e3678SYuan Kang CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; 1352045e3678SYuan Kang u8 *buf = state->current_buf ? state->buf_1 : state->buf_0; 1353045e3678SYuan Kang int buflen = state->current_buf ? state->buflen_1 : state->buflen_0; 1354045e3678SYuan Kang int last_buflen = state->current_buf ? state->buflen_0 : 1355045e3678SYuan Kang state->buflen_1; 1356045e3678SYuan Kang u32 *sh_desc = ctx->sh_desc_digest, *desc; 1357045e3678SYuan Kang dma_addr_t ptr = ctx->sh_desc_digest_dma; 1358045e3678SYuan Kang int sec4_sg_bytes, sec4_sg_src_index, src_nents; 1359045e3678SYuan Kang int digestsize = crypto_ahash_digestsize(ahash); 1360045e3678SYuan Kang struct ahash_edesc *edesc; 1361045e3678SYuan Kang int sh_len; 1362045e3678SYuan Kang int ret = 0; 1363045e3678SYuan Kang 136413fb8fd7SLABBE Corentin src_nents = sg_nents_for_len(req->src, req->nbytes); 1365f9970c28SLABBE Corentin if (src_nents < 0) { 1366f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1367f9970c28SLABBE Corentin return src_nents; 1368f9970c28SLABBE Corentin } 1369045e3678SYuan Kang sec4_sg_src_index = 2; 1370045e3678SYuan Kang sec4_sg_bytes = (sec4_sg_src_index + src_nents) * 1371045e3678SYuan Kang sizeof(struct sec4_sg_entry); 1372045e3678SYuan Kang 1373045e3678SYuan Kang /* allocate space for base edesc and hw desc commands, link tables */ 1374dde20ae9SVictoria Milhoan edesc = kzalloc(sizeof(*edesc) + DESC_JOB_IO_LEN + sec4_sg_bytes, 1375dde20ae9SVictoria Milhoan GFP_DMA | flags); 1376045e3678SYuan Kang if (!edesc) { 1377045e3678SYuan Kang dev_err(jrdev, "could not allocate extended descriptor\n"); 1378045e3678SYuan Kang return -ENOMEM; 1379045e3678SYuan Kang } 1380045e3678SYuan Kang 1381045e3678SYuan Kang sh_len = desc_len(sh_desc); 1382045e3678SYuan Kang desc = edesc->hw_desc; 1383045e3678SYuan Kang init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE); 1384045e3678SYuan Kang 1385045e3678SYuan Kang edesc->src_nents = src_nents; 1386045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 1387045e3678SYuan Kang edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) + 1388045e3678SYuan Kang DESC_JOB_IO_LEN; 1389045e3678SYuan Kang 1390045e3678SYuan Kang state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, buf, 1391045e3678SYuan Kang state->buf_dma, buflen, 1392045e3678SYuan Kang last_buflen); 1393045e3678SYuan Kang 139413fb8fd7SLABBE Corentin src_map_to_sec4_sg(jrdev, req->src, src_nents, edesc->sec4_sg + 1); 1395045e3678SYuan Kang 13961da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, 13971da2be33SRuchika Gupta sec4_sg_bytes, DMA_TO_DEVICE); 1398ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 1399ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 1400ce572085SHoria Geanta return -ENOMEM; 1401ce572085SHoria Geanta } 14021da2be33SRuchika Gupta 1403045e3678SYuan Kang append_seq_in_ptr(desc, edesc->sec4_sg_dma, buflen + 1404045e3678SYuan Kang req->nbytes, LDST_SGF); 1405045e3678SYuan Kang 1406045e3678SYuan Kang edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result, 1407045e3678SYuan Kang digestsize); 1408ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->dst_dma)) { 1409ce572085SHoria Geanta dev_err(jrdev, "unable to map dst\n"); 1410ce572085SHoria Geanta return -ENOMEM; 1411ce572085SHoria Geanta } 1412045e3678SYuan Kang 1413045e3678SYuan Kang #ifdef DEBUG 1414514df281SAlex Porosanu print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ", 1415045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); 1416045e3678SYuan Kang #endif 1417045e3678SYuan Kang 1418045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, ahash_done, req); 1419045e3678SYuan Kang if (!ret) { 1420045e3678SYuan Kang ret = -EINPROGRESS; 1421045e3678SYuan Kang } else { 1422045e3678SYuan Kang ahash_unmap(jrdev, edesc, req, digestsize); 1423045e3678SYuan Kang kfree(edesc); 1424045e3678SYuan Kang } 1425045e3678SYuan Kang 1426045e3678SYuan Kang return ret; 1427045e3678SYuan Kang } 1428045e3678SYuan Kang 1429045e3678SYuan Kang /* submit first update job descriptor after init */ 1430045e3678SYuan Kang static int ahash_update_first(struct ahash_request *req) 1431045e3678SYuan Kang { 1432045e3678SYuan Kang struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); 1433045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); 1434045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1435045e3678SYuan Kang struct device *jrdev = ctx->jrdev; 1436045e3678SYuan Kang gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | 1437045e3678SYuan Kang CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; 14384451d494SCristian Stoica u8 *next_buf = state->current_buf ? state->buf_1 : state->buf_0; 14394451d494SCristian Stoica int *next_buflen = state->current_buf ? 14404451d494SCristian Stoica &state->buflen_1 : &state->buflen_0; 1441045e3678SYuan Kang int to_hash; 1442045e3678SYuan Kang u32 *sh_desc = ctx->sh_desc_update_first, *desc; 1443045e3678SYuan Kang dma_addr_t ptr = ctx->sh_desc_update_first_dma; 1444045e3678SYuan Kang int sec4_sg_bytes, src_nents; 1445045e3678SYuan Kang dma_addr_t src_dma; 1446045e3678SYuan Kang u32 options; 1447045e3678SYuan Kang struct ahash_edesc *edesc; 1448045e3678SYuan Kang int ret = 0; 1449045e3678SYuan Kang int sh_len; 1450045e3678SYuan Kang 1451045e3678SYuan Kang *next_buflen = req->nbytes & (crypto_tfm_alg_blocksize(&ahash->base) - 1452045e3678SYuan Kang 1); 1453045e3678SYuan Kang to_hash = req->nbytes - *next_buflen; 1454045e3678SYuan Kang 1455045e3678SYuan Kang if (to_hash) { 1456*3d5a2db6SRussell King src_nents = sg_nents_for_len(req->src, 1457*3d5a2db6SRussell King req->nbytes - *next_buflen); 1458f9970c28SLABBE Corentin if (src_nents < 0) { 1459f9970c28SLABBE Corentin dev_err(jrdev, "Invalid number of src SG.\n"); 1460f9970c28SLABBE Corentin return src_nents; 1461f9970c28SLABBE Corentin } 1462*3d5a2db6SRussell King dma_map_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE); 1463*3d5a2db6SRussell King if (src_nents > 1) 1464*3d5a2db6SRussell King sec4_sg_bytes = src_nents * 1465*3d5a2db6SRussell King sizeof(struct sec4_sg_entry); 1466*3d5a2db6SRussell King else 1467*3d5a2db6SRussell King sec4_sg_bytes = 0; 1468045e3678SYuan Kang 1469045e3678SYuan Kang /* 1470045e3678SYuan Kang * allocate space for base edesc and hw desc commands, 1471045e3678SYuan Kang * link tables 1472045e3678SYuan Kang */ 1473dde20ae9SVictoria Milhoan edesc = kzalloc(sizeof(*edesc) + DESC_JOB_IO_LEN + 1474045e3678SYuan Kang sec4_sg_bytes, GFP_DMA | flags); 1475045e3678SYuan Kang if (!edesc) { 1476045e3678SYuan Kang dev_err(jrdev, 1477045e3678SYuan Kang "could not allocate extended descriptor\n"); 1478045e3678SYuan Kang return -ENOMEM; 1479045e3678SYuan Kang } 1480045e3678SYuan Kang 1481045e3678SYuan Kang edesc->src_nents = src_nents; 1482045e3678SYuan Kang edesc->sec4_sg_bytes = sec4_sg_bytes; 1483045e3678SYuan Kang edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) + 1484045e3678SYuan Kang DESC_JOB_IO_LEN; 148576b99080SHoria Geanta edesc->dst_dma = 0; 1486045e3678SYuan Kang 1487*3d5a2db6SRussell King if (src_nents > 1) { 1488045e3678SYuan Kang sg_to_sec4_sg_last(req->src, src_nents, 1489045e3678SYuan Kang edesc->sec4_sg, 0); 14901da2be33SRuchika Gupta edesc->sec4_sg_dma = dma_map_single(jrdev, 14911da2be33SRuchika Gupta edesc->sec4_sg, 14921da2be33SRuchika Gupta sec4_sg_bytes, 14931da2be33SRuchika Gupta DMA_TO_DEVICE); 1494ce572085SHoria Geanta if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { 1495ce572085SHoria Geanta dev_err(jrdev, "unable to map S/G table\n"); 1496ce572085SHoria Geanta return -ENOMEM; 1497ce572085SHoria Geanta } 1498045e3678SYuan Kang src_dma = edesc->sec4_sg_dma; 1499045e3678SYuan Kang options = LDST_SGF; 1500045e3678SYuan Kang } else { 1501045e3678SYuan Kang src_dma = sg_dma_address(req->src); 1502045e3678SYuan Kang options = 0; 1503045e3678SYuan Kang } 1504045e3678SYuan Kang 1505045e3678SYuan Kang if (*next_buflen) 1506307fd543SCristian Stoica scatterwalk_map_and_copy(next_buf, req->src, to_hash, 1507307fd543SCristian Stoica *next_buflen, 0); 1508045e3678SYuan Kang 1509045e3678SYuan Kang sh_len = desc_len(sh_desc); 1510045e3678SYuan Kang desc = edesc->hw_desc; 1511045e3678SYuan Kang init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | 1512045e3678SYuan Kang HDR_REVERSE); 1513045e3678SYuan Kang 1514045e3678SYuan Kang append_seq_in_ptr(desc, src_dma, to_hash, options); 1515045e3678SYuan Kang 1516ce572085SHoria Geanta ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len); 1517ce572085SHoria Geanta if (ret) 1518ce572085SHoria Geanta return ret; 1519045e3678SYuan Kang 1520045e3678SYuan Kang #ifdef DEBUG 1521514df281SAlex Porosanu print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ", 1522045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, desc, 1523045e3678SYuan Kang desc_bytes(desc), 1); 1524045e3678SYuan Kang #endif 1525045e3678SYuan Kang 1526045e3678SYuan Kang ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, 1527045e3678SYuan Kang req); 1528045e3678SYuan Kang if (!ret) { 1529045e3678SYuan Kang ret = -EINPROGRESS; 1530045e3678SYuan Kang state->update = ahash_update_ctx; 1531045e3678SYuan Kang state->finup = ahash_finup_ctx; 1532045e3678SYuan Kang state->final = ahash_final_ctx; 1533045e3678SYuan Kang } else { 1534045e3678SYuan Kang ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, 1535045e3678SYuan Kang DMA_TO_DEVICE); 1536045e3678SYuan Kang kfree(edesc); 1537045e3678SYuan Kang } 1538045e3678SYuan Kang } else if (*next_buflen) { 1539045e3678SYuan Kang state->update = ahash_update_no_ctx; 1540045e3678SYuan Kang state->finup = ahash_finup_no_ctx; 1541045e3678SYuan Kang state->final = ahash_final_no_ctx; 1542307fd543SCristian Stoica scatterwalk_map_and_copy(next_buf, req->src, 0, 1543307fd543SCristian Stoica req->nbytes, 0); 1544045e3678SYuan Kang } 1545045e3678SYuan Kang #ifdef DEBUG 1546514df281SAlex Porosanu print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ", 1547045e3678SYuan Kang DUMP_PREFIX_ADDRESS, 16, 4, next_buf, 1548045e3678SYuan Kang *next_buflen, 1); 1549045e3678SYuan Kang #endif 1550045e3678SYuan Kang 1551045e3678SYuan Kang return ret; 1552045e3678SYuan Kang } 1553045e3678SYuan Kang 1554045e3678SYuan Kang static int ahash_finup_first(struct ahash_request *req) 1555045e3678SYuan Kang { 1556045e3678SYuan Kang return ahash_digest(req); 1557045e3678SYuan Kang } 1558045e3678SYuan Kang 1559045e3678SYuan Kang static int ahash_init(struct ahash_request *req) 1560045e3678SYuan Kang { 1561045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1562045e3678SYuan Kang 1563045e3678SYuan Kang state->update = ahash_update_first; 1564045e3678SYuan Kang state->finup = ahash_finup_first; 1565045e3678SYuan Kang state->final = ahash_final_no_ctx; 1566045e3678SYuan Kang 1567045e3678SYuan Kang state->current_buf = 0; 1568de0e35ecSHoria Geanta state->buf_dma = 0; 15696fd4b156SSteve Cornelius state->buflen_0 = 0; 15706fd4b156SSteve Cornelius state->buflen_1 = 0; 1571045e3678SYuan Kang 1572045e3678SYuan Kang return 0; 1573045e3678SYuan Kang } 1574045e3678SYuan Kang 1575045e3678SYuan Kang static int ahash_update(struct ahash_request *req) 1576045e3678SYuan Kang { 1577045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1578045e3678SYuan Kang 1579045e3678SYuan Kang return state->update(req); 1580045e3678SYuan Kang } 1581045e3678SYuan Kang 1582045e3678SYuan Kang static int ahash_finup(struct ahash_request *req) 1583045e3678SYuan Kang { 1584045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1585045e3678SYuan Kang 1586045e3678SYuan Kang return state->finup(req); 1587045e3678SYuan Kang } 1588045e3678SYuan Kang 1589045e3678SYuan Kang static int ahash_final(struct ahash_request *req) 1590045e3678SYuan Kang { 1591045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 1592045e3678SYuan Kang 1593045e3678SYuan Kang return state->final(req); 1594045e3678SYuan Kang } 1595045e3678SYuan Kang 1596045e3678SYuan Kang static int ahash_export(struct ahash_request *req, void *out) 1597045e3678SYuan Kang { 1598045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 15995ec90831SRussell King struct caam_export_state *export = out; 16005ec90831SRussell King int len; 16015ec90831SRussell King u8 *buf; 1602045e3678SYuan Kang 16035ec90831SRussell King if (state->current_buf) { 16045ec90831SRussell King buf = state->buf_1; 16055ec90831SRussell King len = state->buflen_1; 16065ec90831SRussell King } else { 16075ec90831SRussell King buf = state->buf_0; 1608f456cd2dSFabio Estevam len = state->buflen_0; 16095ec90831SRussell King } 16105ec90831SRussell King 16115ec90831SRussell King memcpy(export->buf, buf, len); 16125ec90831SRussell King memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx)); 16135ec90831SRussell King export->buflen = len; 16145ec90831SRussell King export->update = state->update; 16155ec90831SRussell King export->final = state->final; 16165ec90831SRussell King export->finup = state->finup; 1617434b4212SRussell King 1618045e3678SYuan Kang return 0; 1619045e3678SYuan Kang } 1620045e3678SYuan Kang 1621045e3678SYuan Kang static int ahash_import(struct ahash_request *req, const void *in) 1622045e3678SYuan Kang { 1623045e3678SYuan Kang struct caam_hash_state *state = ahash_request_ctx(req); 16245ec90831SRussell King const struct caam_export_state *export = in; 1625045e3678SYuan Kang 16265ec90831SRussell King memset(state, 0, sizeof(*state)); 16275ec90831SRussell King memcpy(state->buf_0, export->buf, export->buflen); 16285ec90831SRussell King memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx)); 16295ec90831SRussell King state->buflen_0 = export->buflen; 16305ec90831SRussell King state->update = export->update; 16315ec90831SRussell King state->final = export->final; 16325ec90831SRussell King state->finup = export->finup; 1633434b4212SRussell King 1634045e3678SYuan Kang return 0; 1635045e3678SYuan Kang } 1636045e3678SYuan Kang 1637045e3678SYuan Kang struct caam_hash_template { 1638045e3678SYuan Kang char name[CRYPTO_MAX_ALG_NAME]; 1639045e3678SYuan Kang char driver_name[CRYPTO_MAX_ALG_NAME]; 1640b0e09baeSYuan Kang char hmac_name[CRYPTO_MAX_ALG_NAME]; 1641b0e09baeSYuan Kang char hmac_driver_name[CRYPTO_MAX_ALG_NAME]; 1642045e3678SYuan Kang unsigned int blocksize; 1643045e3678SYuan Kang struct ahash_alg template_ahash; 1644045e3678SYuan Kang u32 alg_type; 1645045e3678SYuan Kang u32 alg_op; 1646045e3678SYuan Kang }; 1647045e3678SYuan Kang 1648045e3678SYuan Kang /* ahash descriptors */ 1649045e3678SYuan Kang static struct caam_hash_template driver_hash[] = { 1650045e3678SYuan Kang { 1651b0e09baeSYuan Kang .name = "sha1", 1652b0e09baeSYuan Kang .driver_name = "sha1-caam", 1653b0e09baeSYuan Kang .hmac_name = "hmac(sha1)", 1654b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha1-caam", 1655045e3678SYuan Kang .blocksize = SHA1_BLOCK_SIZE, 1656045e3678SYuan Kang .template_ahash = { 1657045e3678SYuan Kang .init = ahash_init, 1658045e3678SYuan Kang .update = ahash_update, 1659045e3678SYuan Kang .final = ahash_final, 1660045e3678SYuan Kang .finup = ahash_finup, 1661045e3678SYuan Kang .digest = ahash_digest, 1662045e3678SYuan Kang .export = ahash_export, 1663045e3678SYuan Kang .import = ahash_import, 1664045e3678SYuan Kang .setkey = ahash_setkey, 1665045e3678SYuan Kang .halg = { 1666045e3678SYuan Kang .digestsize = SHA1_DIGEST_SIZE, 16675ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1668045e3678SYuan Kang }, 1669045e3678SYuan Kang }, 1670045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA1, 1671045e3678SYuan Kang .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC, 1672045e3678SYuan Kang }, { 1673b0e09baeSYuan Kang .name = "sha224", 1674b0e09baeSYuan Kang .driver_name = "sha224-caam", 1675b0e09baeSYuan Kang .hmac_name = "hmac(sha224)", 1676b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha224-caam", 1677045e3678SYuan Kang .blocksize = SHA224_BLOCK_SIZE, 1678045e3678SYuan Kang .template_ahash = { 1679045e3678SYuan Kang .init = ahash_init, 1680045e3678SYuan Kang .update = ahash_update, 1681045e3678SYuan Kang .final = ahash_final, 1682045e3678SYuan Kang .finup = ahash_finup, 1683045e3678SYuan Kang .digest = ahash_digest, 1684045e3678SYuan Kang .export = ahash_export, 1685045e3678SYuan Kang .import = ahash_import, 1686045e3678SYuan Kang .setkey = ahash_setkey, 1687045e3678SYuan Kang .halg = { 1688045e3678SYuan Kang .digestsize = SHA224_DIGEST_SIZE, 16895ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1690045e3678SYuan Kang }, 1691045e3678SYuan Kang }, 1692045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA224, 1693045e3678SYuan Kang .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC, 1694045e3678SYuan Kang }, { 1695b0e09baeSYuan Kang .name = "sha256", 1696b0e09baeSYuan Kang .driver_name = "sha256-caam", 1697b0e09baeSYuan Kang .hmac_name = "hmac(sha256)", 1698b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha256-caam", 1699045e3678SYuan Kang .blocksize = SHA256_BLOCK_SIZE, 1700045e3678SYuan Kang .template_ahash = { 1701045e3678SYuan Kang .init = ahash_init, 1702045e3678SYuan Kang .update = ahash_update, 1703045e3678SYuan Kang .final = ahash_final, 1704045e3678SYuan Kang .finup = ahash_finup, 1705045e3678SYuan Kang .digest = ahash_digest, 1706045e3678SYuan Kang .export = ahash_export, 1707045e3678SYuan Kang .import = ahash_import, 1708045e3678SYuan Kang .setkey = ahash_setkey, 1709045e3678SYuan Kang .halg = { 1710045e3678SYuan Kang .digestsize = SHA256_DIGEST_SIZE, 17115ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1712045e3678SYuan Kang }, 1713045e3678SYuan Kang }, 1714045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA256, 1715045e3678SYuan Kang .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC, 1716045e3678SYuan Kang }, { 1717b0e09baeSYuan Kang .name = "sha384", 1718b0e09baeSYuan Kang .driver_name = "sha384-caam", 1719b0e09baeSYuan Kang .hmac_name = "hmac(sha384)", 1720b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha384-caam", 1721045e3678SYuan Kang .blocksize = SHA384_BLOCK_SIZE, 1722045e3678SYuan Kang .template_ahash = { 1723045e3678SYuan Kang .init = ahash_init, 1724045e3678SYuan Kang .update = ahash_update, 1725045e3678SYuan Kang .final = ahash_final, 1726045e3678SYuan Kang .finup = ahash_finup, 1727045e3678SYuan Kang .digest = ahash_digest, 1728045e3678SYuan Kang .export = ahash_export, 1729045e3678SYuan Kang .import = ahash_import, 1730045e3678SYuan Kang .setkey = ahash_setkey, 1731045e3678SYuan Kang .halg = { 1732045e3678SYuan Kang .digestsize = SHA384_DIGEST_SIZE, 17335ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1734045e3678SYuan Kang }, 1735045e3678SYuan Kang }, 1736045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA384, 1737045e3678SYuan Kang .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC, 1738045e3678SYuan Kang }, { 1739b0e09baeSYuan Kang .name = "sha512", 1740b0e09baeSYuan Kang .driver_name = "sha512-caam", 1741b0e09baeSYuan Kang .hmac_name = "hmac(sha512)", 1742b0e09baeSYuan Kang .hmac_driver_name = "hmac-sha512-caam", 1743045e3678SYuan Kang .blocksize = SHA512_BLOCK_SIZE, 1744045e3678SYuan Kang .template_ahash = { 1745045e3678SYuan Kang .init = ahash_init, 1746045e3678SYuan Kang .update = ahash_update, 1747045e3678SYuan Kang .final = ahash_final, 1748045e3678SYuan Kang .finup = ahash_finup, 1749045e3678SYuan Kang .digest = ahash_digest, 1750045e3678SYuan Kang .export = ahash_export, 1751045e3678SYuan Kang .import = ahash_import, 1752045e3678SYuan Kang .setkey = ahash_setkey, 1753045e3678SYuan Kang .halg = { 1754045e3678SYuan Kang .digestsize = SHA512_DIGEST_SIZE, 17555ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1756045e3678SYuan Kang }, 1757045e3678SYuan Kang }, 1758045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_SHA512, 1759045e3678SYuan Kang .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC, 1760045e3678SYuan Kang }, { 1761b0e09baeSYuan Kang .name = "md5", 1762b0e09baeSYuan Kang .driver_name = "md5-caam", 1763b0e09baeSYuan Kang .hmac_name = "hmac(md5)", 1764b0e09baeSYuan Kang .hmac_driver_name = "hmac-md5-caam", 1765045e3678SYuan Kang .blocksize = MD5_BLOCK_WORDS * 4, 1766045e3678SYuan Kang .template_ahash = { 1767045e3678SYuan Kang .init = ahash_init, 1768045e3678SYuan Kang .update = ahash_update, 1769045e3678SYuan Kang .final = ahash_final, 1770045e3678SYuan Kang .finup = ahash_finup, 1771045e3678SYuan Kang .digest = ahash_digest, 1772045e3678SYuan Kang .export = ahash_export, 1773045e3678SYuan Kang .import = ahash_import, 1774045e3678SYuan Kang .setkey = ahash_setkey, 1775045e3678SYuan Kang .halg = { 1776045e3678SYuan Kang .digestsize = MD5_DIGEST_SIZE, 17775ec90831SRussell King .statesize = sizeof(struct caam_export_state), 1778045e3678SYuan Kang }, 1779045e3678SYuan Kang }, 1780045e3678SYuan Kang .alg_type = OP_ALG_ALGSEL_MD5, 1781045e3678SYuan Kang .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC, 1782045e3678SYuan Kang }, 1783045e3678SYuan Kang }; 1784045e3678SYuan Kang 1785045e3678SYuan Kang struct caam_hash_alg { 1786045e3678SYuan Kang struct list_head entry; 1787045e3678SYuan Kang int alg_type; 1788045e3678SYuan Kang int alg_op; 1789045e3678SYuan Kang struct ahash_alg ahash_alg; 1790045e3678SYuan Kang }; 1791045e3678SYuan Kang 1792045e3678SYuan Kang static int caam_hash_cra_init(struct crypto_tfm *tfm) 1793045e3678SYuan Kang { 1794045e3678SYuan Kang struct crypto_ahash *ahash = __crypto_ahash_cast(tfm); 1795045e3678SYuan Kang struct crypto_alg *base = tfm->__crt_alg; 1796045e3678SYuan Kang struct hash_alg_common *halg = 1797045e3678SYuan Kang container_of(base, struct hash_alg_common, base); 1798045e3678SYuan Kang struct ahash_alg *alg = 1799045e3678SYuan Kang container_of(halg, struct ahash_alg, halg); 1800045e3678SYuan Kang struct caam_hash_alg *caam_hash = 1801045e3678SYuan Kang container_of(alg, struct caam_hash_alg, ahash_alg); 1802045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm); 1803045e3678SYuan Kang /* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */ 1804045e3678SYuan Kang static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE, 1805045e3678SYuan Kang HASH_MSG_LEN + SHA1_DIGEST_SIZE, 1806045e3678SYuan Kang HASH_MSG_LEN + 32, 1807045e3678SYuan Kang HASH_MSG_LEN + SHA256_DIGEST_SIZE, 1808045e3678SYuan Kang HASH_MSG_LEN + 64, 1809045e3678SYuan Kang HASH_MSG_LEN + SHA512_DIGEST_SIZE }; 1810045e3678SYuan Kang int ret = 0; 1811045e3678SYuan Kang 1812045e3678SYuan Kang /* 1813cfc6f11bSRuchika Gupta * Get a Job ring from Job Ring driver to ensure in-order 1814045e3678SYuan Kang * crypto request processing per tfm 1815045e3678SYuan Kang */ 1816cfc6f11bSRuchika Gupta ctx->jrdev = caam_jr_alloc(); 1817cfc6f11bSRuchika Gupta if (IS_ERR(ctx->jrdev)) { 1818cfc6f11bSRuchika Gupta pr_err("Job Ring Device allocation for transform failed\n"); 1819cfc6f11bSRuchika Gupta return PTR_ERR(ctx->jrdev); 1820cfc6f11bSRuchika Gupta } 1821045e3678SYuan Kang /* copy descriptor header template value */ 1822045e3678SYuan Kang ctx->alg_type = OP_TYPE_CLASS2_ALG | caam_hash->alg_type; 1823045e3678SYuan Kang ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_hash->alg_op; 1824045e3678SYuan Kang 1825045e3678SYuan Kang ctx->ctx_len = runninglen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >> 1826045e3678SYuan Kang OP_ALG_ALGSEL_SHIFT]; 1827045e3678SYuan Kang 1828045e3678SYuan Kang crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 1829045e3678SYuan Kang sizeof(struct caam_hash_state)); 1830045e3678SYuan Kang 1831045e3678SYuan Kang ret = ahash_set_sh_desc(ahash); 1832045e3678SYuan Kang 1833045e3678SYuan Kang return ret; 1834045e3678SYuan Kang } 1835045e3678SYuan Kang 1836045e3678SYuan Kang static void caam_hash_cra_exit(struct crypto_tfm *tfm) 1837045e3678SYuan Kang { 1838045e3678SYuan Kang struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm); 1839045e3678SYuan Kang 1840045e3678SYuan Kang if (ctx->sh_desc_update_dma && 1841045e3678SYuan Kang !dma_mapping_error(ctx->jrdev, ctx->sh_desc_update_dma)) 1842045e3678SYuan Kang dma_unmap_single(ctx->jrdev, ctx->sh_desc_update_dma, 1843045e3678SYuan Kang desc_bytes(ctx->sh_desc_update), 1844045e3678SYuan Kang DMA_TO_DEVICE); 1845045e3678SYuan Kang if (ctx->sh_desc_update_first_dma && 1846045e3678SYuan Kang !dma_mapping_error(ctx->jrdev, ctx->sh_desc_update_first_dma)) 1847045e3678SYuan Kang dma_unmap_single(ctx->jrdev, ctx->sh_desc_update_first_dma, 1848045e3678SYuan Kang desc_bytes(ctx->sh_desc_update_first), 1849045e3678SYuan Kang DMA_TO_DEVICE); 1850045e3678SYuan Kang if (ctx->sh_desc_fin_dma && 1851045e3678SYuan Kang !dma_mapping_error(ctx->jrdev, ctx->sh_desc_fin_dma)) 1852045e3678SYuan Kang dma_unmap_single(ctx->jrdev, ctx->sh_desc_fin_dma, 1853045e3678SYuan Kang desc_bytes(ctx->sh_desc_fin), DMA_TO_DEVICE); 1854045e3678SYuan Kang if (ctx->sh_desc_digest_dma && 1855045e3678SYuan Kang !dma_mapping_error(ctx->jrdev, ctx->sh_desc_digest_dma)) 1856045e3678SYuan Kang dma_unmap_single(ctx->jrdev, ctx->sh_desc_digest_dma, 1857045e3678SYuan Kang desc_bytes(ctx->sh_desc_digest), 1858045e3678SYuan Kang DMA_TO_DEVICE); 1859045e3678SYuan Kang if (ctx->sh_desc_finup_dma && 1860045e3678SYuan Kang !dma_mapping_error(ctx->jrdev, ctx->sh_desc_finup_dma)) 1861045e3678SYuan Kang dma_unmap_single(ctx->jrdev, ctx->sh_desc_finup_dma, 1862045e3678SYuan Kang desc_bytes(ctx->sh_desc_finup), DMA_TO_DEVICE); 1863cfc6f11bSRuchika Gupta 1864cfc6f11bSRuchika Gupta caam_jr_free(ctx->jrdev); 1865045e3678SYuan Kang } 1866045e3678SYuan Kang 1867045e3678SYuan Kang static void __exit caam_algapi_hash_exit(void) 1868045e3678SYuan Kang { 1869045e3678SYuan Kang struct caam_hash_alg *t_alg, *n; 1870045e3678SYuan Kang 1871cfc6f11bSRuchika Gupta if (!hash_list.next) 1872045e3678SYuan Kang return; 1873045e3678SYuan Kang 1874cfc6f11bSRuchika Gupta list_for_each_entry_safe(t_alg, n, &hash_list, entry) { 1875045e3678SYuan Kang crypto_unregister_ahash(&t_alg->ahash_alg); 1876045e3678SYuan Kang list_del(&t_alg->entry); 1877045e3678SYuan Kang kfree(t_alg); 1878045e3678SYuan Kang } 1879045e3678SYuan Kang } 1880045e3678SYuan Kang 1881045e3678SYuan Kang static struct caam_hash_alg * 1882cfc6f11bSRuchika Gupta caam_hash_alloc(struct caam_hash_template *template, 1883b0e09baeSYuan Kang bool keyed) 1884045e3678SYuan Kang { 1885045e3678SYuan Kang struct caam_hash_alg *t_alg; 1886045e3678SYuan Kang struct ahash_alg *halg; 1887045e3678SYuan Kang struct crypto_alg *alg; 1888045e3678SYuan Kang 18899c4f9733SFabio Estevam t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL); 1890045e3678SYuan Kang if (!t_alg) { 1891cfc6f11bSRuchika Gupta pr_err("failed to allocate t_alg\n"); 1892045e3678SYuan Kang return ERR_PTR(-ENOMEM); 1893045e3678SYuan Kang } 1894045e3678SYuan Kang 1895045e3678SYuan Kang t_alg->ahash_alg = template->template_ahash; 1896045e3678SYuan Kang halg = &t_alg->ahash_alg; 1897045e3678SYuan Kang alg = &halg->halg.base; 1898045e3678SYuan Kang 1899b0e09baeSYuan Kang if (keyed) { 1900b0e09baeSYuan Kang snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", 1901b0e09baeSYuan Kang template->hmac_name); 1902b0e09baeSYuan Kang snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", 1903b0e09baeSYuan Kang template->hmac_driver_name); 1904b0e09baeSYuan Kang } else { 1905b0e09baeSYuan Kang snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", 1906b0e09baeSYuan Kang template->name); 1907045e3678SYuan Kang snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", 1908045e3678SYuan Kang template->driver_name); 1909a0118c8bSRussell King t_alg->ahash_alg.setkey = NULL; 1910b0e09baeSYuan Kang } 1911045e3678SYuan Kang alg->cra_module = THIS_MODULE; 1912045e3678SYuan Kang alg->cra_init = caam_hash_cra_init; 1913045e3678SYuan Kang alg->cra_exit = caam_hash_cra_exit; 1914045e3678SYuan Kang alg->cra_ctxsize = sizeof(struct caam_hash_ctx); 1915045e3678SYuan Kang alg->cra_priority = CAAM_CRA_PRIORITY; 1916045e3678SYuan Kang alg->cra_blocksize = template->blocksize; 1917045e3678SYuan Kang alg->cra_alignmask = 0; 1918045e3678SYuan Kang alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_TYPE_AHASH; 1919045e3678SYuan Kang alg->cra_type = &crypto_ahash_type; 1920045e3678SYuan Kang 1921045e3678SYuan Kang t_alg->alg_type = template->alg_type; 1922045e3678SYuan Kang t_alg->alg_op = template->alg_op; 1923045e3678SYuan Kang 1924045e3678SYuan Kang return t_alg; 1925045e3678SYuan Kang } 1926045e3678SYuan Kang 1927045e3678SYuan Kang static int __init caam_algapi_hash_init(void) 1928045e3678SYuan Kang { 192935af6403SRuchika Gupta struct device_node *dev_node; 193035af6403SRuchika Gupta struct platform_device *pdev; 193135af6403SRuchika Gupta struct device *ctrldev; 1932045e3678SYuan Kang int i = 0, err = 0; 1933bf83490eSVictoria Milhoan struct caam_drv_private *priv; 1934bf83490eSVictoria Milhoan unsigned int md_limit = SHA512_DIGEST_SIZE; 1935bf83490eSVictoria Milhoan u32 cha_inst, cha_vid; 1936045e3678SYuan Kang 193735af6403SRuchika Gupta dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0"); 193835af6403SRuchika Gupta if (!dev_node) { 193935af6403SRuchika Gupta dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0"); 194035af6403SRuchika Gupta if (!dev_node) 194135af6403SRuchika Gupta return -ENODEV; 194235af6403SRuchika Gupta } 194335af6403SRuchika Gupta 194435af6403SRuchika Gupta pdev = of_find_device_by_node(dev_node); 194535af6403SRuchika Gupta if (!pdev) { 194635af6403SRuchika Gupta of_node_put(dev_node); 194735af6403SRuchika Gupta return -ENODEV; 194835af6403SRuchika Gupta } 194935af6403SRuchika Gupta 195035af6403SRuchika Gupta ctrldev = &pdev->dev; 195135af6403SRuchika Gupta priv = dev_get_drvdata(ctrldev); 195235af6403SRuchika Gupta of_node_put(dev_node); 195335af6403SRuchika Gupta 195435af6403SRuchika Gupta /* 195535af6403SRuchika Gupta * If priv is NULL, it's probably because the caam driver wasn't 195635af6403SRuchika Gupta * properly initialized (e.g. RNG4 init failed). Thus, bail out here. 195735af6403SRuchika Gupta */ 195835af6403SRuchika Gupta if (!priv) 195935af6403SRuchika Gupta return -ENODEV; 196035af6403SRuchika Gupta 1961bf83490eSVictoria Milhoan /* 1962bf83490eSVictoria Milhoan * Register crypto algorithms the device supports. First, identify 1963bf83490eSVictoria Milhoan * presence and attributes of MD block. 1964bf83490eSVictoria Milhoan */ 1965bf83490eSVictoria Milhoan cha_vid = rd_reg32(&priv->ctrl->perfmon.cha_id_ls); 1966bf83490eSVictoria Milhoan cha_inst = rd_reg32(&priv->ctrl->perfmon.cha_num_ls); 1967bf83490eSVictoria Milhoan 1968bf83490eSVictoria Milhoan /* 1969bf83490eSVictoria Milhoan * Skip registration of any hashing algorithms if MD block 1970bf83490eSVictoria Milhoan * is not present. 1971bf83490eSVictoria Milhoan */ 1972bf83490eSVictoria Milhoan if (!((cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT)) 1973bf83490eSVictoria Milhoan return -ENODEV; 1974bf83490eSVictoria Milhoan 1975bf83490eSVictoria Milhoan /* Limit digest size based on LP256 */ 1976bf83490eSVictoria Milhoan if ((cha_vid & CHA_ID_LS_MD_MASK) == CHA_ID_LS_MD_LP256) 1977bf83490eSVictoria Milhoan md_limit = SHA256_DIGEST_SIZE; 1978bf83490eSVictoria Milhoan 1979cfc6f11bSRuchika Gupta INIT_LIST_HEAD(&hash_list); 1980045e3678SYuan Kang 1981045e3678SYuan Kang /* register crypto algorithms the device supports */ 1982045e3678SYuan Kang for (i = 0; i < ARRAY_SIZE(driver_hash); i++) { 1983045e3678SYuan Kang struct caam_hash_alg *t_alg; 1984bf83490eSVictoria Milhoan struct caam_hash_template *alg = driver_hash + i; 1985bf83490eSVictoria Milhoan 1986bf83490eSVictoria Milhoan /* If MD size is not supported by device, skip registration */ 1987bf83490eSVictoria Milhoan if (alg->template_ahash.halg.digestsize > md_limit) 1988bf83490eSVictoria Milhoan continue; 1989045e3678SYuan Kang 1990b0e09baeSYuan Kang /* register hmac version */ 1991bf83490eSVictoria Milhoan t_alg = caam_hash_alloc(alg, true); 1992b0e09baeSYuan Kang if (IS_ERR(t_alg)) { 1993b0e09baeSYuan Kang err = PTR_ERR(t_alg); 1994bf83490eSVictoria Milhoan pr_warn("%s alg allocation failed\n", alg->driver_name); 1995b0e09baeSYuan Kang continue; 1996b0e09baeSYuan Kang } 1997b0e09baeSYuan Kang 1998b0e09baeSYuan Kang err = crypto_register_ahash(&t_alg->ahash_alg); 1999b0e09baeSYuan Kang if (err) { 20006ea30f0aSRussell King pr_warn("%s alg registration failed: %d\n", 20016ea30f0aSRussell King t_alg->ahash_alg.halg.base.cra_driver_name, 20026ea30f0aSRussell King err); 2003b0e09baeSYuan Kang kfree(t_alg); 2004b0e09baeSYuan Kang } else 2005cfc6f11bSRuchika Gupta list_add_tail(&t_alg->entry, &hash_list); 2006b0e09baeSYuan Kang 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 } 2027045e3678SYuan Kang 2028045e3678SYuan Kang module_init(caam_algapi_hash_init); 2029045e3678SYuan Kang module_exit(caam_algapi_hash_exit); 2030045e3678SYuan Kang 2031045e3678SYuan Kang MODULE_LICENSE("GPL"); 2032045e3678SYuan Kang MODULE_DESCRIPTION("FSL CAAM support for ahash functions of crypto API"); 2033045e3678SYuan Kang MODULE_AUTHOR("Freescale Semiconductor - NMG"); 2034