caamhash.c 58.4 KB
Newer Older
Y
Yuan Kang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/*
 * caam - Freescale FSL CAAM support for ahash functions of crypto API
 *
 * Copyright 2011 Freescale Semiconductor, Inc.
 *
 * Based on caamalg.c crypto API driver.
 *
 * relationship of digest job descriptor or first job descriptor after init to
 * shared descriptors:
 *
 * ---------------                     ---------------
 * | JobDesc #1  |-------------------->|  ShareDesc  |
 * | *(packet 1) |                     |  (hashKey)  |
 * ---------------                     | (operation) |
 *                                     ---------------
 *
 * relationship of subsequent job descriptors to shared descriptors:
 *
 * ---------------                     ---------------
 * | JobDesc #2  |-------------------->|  ShareDesc  |
 * | *(packet 2) |      |------------->|  (hashKey)  |
 * ---------------      |    |-------->| (operation) |
 *       .              |    |         | (load ctx2) |
 *       .              |    |         ---------------
 * ---------------      |    |
 * | JobDesc #3  |------|    |
 * | *(packet 3) |           |
 * ---------------           |
 *       .                   |
 *       .                   |
 * ---------------           |
 * | JobDesc #4  |------------
 * | *(packet 4) |
 * ---------------
 *
 * The SharedDesc never changes for a connection unless rekeyed, but
 * each packet will likely be in a different place. So all we need
 * to know to process the packet is where the input is, where the
 * output goes, and what context we want to process with. Context is
 * in the SharedDesc, packet references in the JobDesc.
 *
 * So, a job desc looks like:
 *
 * ---------------------
 * | Header            |
 * | ShareDesc Pointer |
 * | SEQ_OUT_PTR       |
 * | (output buffer)   |
 * | (output length)   |
 * | SEQ_IN_PTR        |
 * | (input buffer)    |
 * | (input length)    |
 * ---------------------
 */

#include "compat.h"

#include "regs.h"
#include "intern.h"
#include "desc_constr.h"
#include "jr.h"
#include "error.h"
#include "sg_sw_sec4.h"
#include "key_gen.h"

#define CAAM_CRA_PRIORITY		3000

/* max hash key is max split key size */
#define CAAM_MAX_HASH_KEY_SIZE		(SHA512_DIGEST_SIZE * 2)

#define CAAM_MAX_HASH_BLOCK_SIZE	SHA512_BLOCK_SIZE
#define CAAM_MAX_HASH_DIGEST_SIZE	SHA512_DIGEST_SIZE

/* length of descriptors text */
#define DESC_AHASH_BASE			(4 * CAAM_CMD_SZ)
#define DESC_AHASH_UPDATE_LEN		(6 * CAAM_CMD_SZ)
#define DESC_AHASH_UPDATE_FIRST_LEN	(DESC_AHASH_BASE + 4 * CAAM_CMD_SZ)
#define DESC_AHASH_FINAL_LEN		(DESC_AHASH_BASE + 5 * CAAM_CMD_SZ)
#define DESC_AHASH_FINUP_LEN		(DESC_AHASH_BASE + 5 * CAAM_CMD_SZ)
#define DESC_AHASH_DIGEST_LEN		(DESC_AHASH_BASE + 4 * CAAM_CMD_SZ)

#define DESC_HASH_MAX_USED_BYTES	(DESC_AHASH_FINAL_LEN + \
					 CAAM_MAX_HASH_KEY_SIZE)
#define DESC_HASH_MAX_USED_LEN		(DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)

/* caam context sizes for hashes: running digest + 8 */
#define HASH_MSG_LEN			8
#define MAX_CTX_LEN			(HASH_MSG_LEN + SHA512_DIGEST_SIZE)

#ifdef DEBUG
/* for print_hex_dumps with line references */
#define debug(format, arg...) printk(format, arg)
#else
#define debug(format, arg...)
#endif

97 98 99

static struct list_head hash_list;

Y
Yuan Kang 已提交
100 101
/* ahash per-session context */
struct caam_hash_ctx {
102 103 104 105 106 107
	u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
	u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
	u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
	u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
	u32 sh_desc_finup[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
	dma_addr_t sh_desc_update_dma ____cacheline_aligned;
Y
Yuan Kang 已提交
108 109 110 111
	dma_addr_t sh_desc_update_first_dma;
	dma_addr_t sh_desc_fin_dma;
	dma_addr_t sh_desc_digest_dma;
	dma_addr_t sh_desc_finup_dma;
112
	struct device *jrdev;
Y
Yuan Kang 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	u32 alg_type;
	u32 alg_op;
	u8 key[CAAM_MAX_HASH_KEY_SIZE];
	dma_addr_t key_dma;
	int ctx_len;
	unsigned int split_key_len;
	unsigned int split_key_pad_len;
};

/* ahash state */
struct caam_hash_state {
	dma_addr_t buf_dma;
	dma_addr_t ctx_dma;
	u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
	int buflen_0;
	u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
	int buflen_1;
130
	u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
Y
Yuan Kang 已提交
131 132 133 134 135 136
	int (*update)(struct ahash_request *req);
	int (*final)(struct ahash_request *req);
	int (*finup)(struct ahash_request *req);
	int current_buf;
};

137 138 139 140 141 142 143 144 145
struct caam_export_state {
	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
	u8 caam_ctx[MAX_CTX_LEN];
	int buflen;
	int (*update)(struct ahash_request *req);
	int (*final)(struct ahash_request *req);
	int (*finup)(struct ahash_request *req);
};

Y
Yuan Kang 已提交
146 147 148
/* Common job descriptor seq in/out ptr routines */

/* Map state->caam_ctx, and append seq_out_ptr command that points to it */
149 150 151
static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
				      struct caam_hash_state *state,
				      int ctx_len)
Y
Yuan Kang 已提交
152 153 154
{
	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
					ctx_len, DMA_FROM_DEVICE);
155 156 157 158 159
	if (dma_mapping_error(jrdev, state->ctx_dma)) {
		dev_err(jrdev, "unable to map ctx\n");
		return -ENOMEM;
	}

Y
Yuan Kang 已提交
160
	append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
161 162

	return 0;
Y
Yuan Kang 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
}

/* Map req->result, and append seq_out_ptr command that points to it */
static inline dma_addr_t map_seq_out_ptr_result(u32 *desc, struct device *jrdev,
						u8 *result, int digestsize)
{
	dma_addr_t dst_dma;

	dst_dma = dma_map_single(jrdev, result, digestsize, DMA_FROM_DEVICE);
	append_seq_out_ptr(desc, dst_dma, digestsize, 0);

	return dst_dma;
}

/* Map current buffer in state and put it in link table */
static inline dma_addr_t buf_map_to_sec4_sg(struct device *jrdev,
					    struct sec4_sg_entry *sec4_sg,
					    u8 *buf, int buflen)
{
	dma_addr_t buf_dma;

	buf_dma = dma_map_single(jrdev, buf, buflen, DMA_TO_DEVICE);
	dma_to_sec4_sg_one(sec4_sg, buf_dma, buflen, 0);

	return buf_dma;
}

/*
 * Only put buffer in link table if it contains data, which is possible,
 * since a buffer has previously been used, and needs to be unmapped,
 */
static inline dma_addr_t
try_buf_map_to_sec4_sg(struct device *jrdev, struct sec4_sg_entry *sec4_sg,
		       u8 *buf, dma_addr_t buf_dma, int buflen,
		       int last_buflen)
{
	if (buf_dma && !dma_mapping_error(jrdev, buf_dma))
		dma_unmap_single(jrdev, buf_dma, last_buflen, DMA_TO_DEVICE);
	if (buflen)
		buf_dma = buf_map_to_sec4_sg(jrdev, sec4_sg, buf, buflen);
	else
		buf_dma = 0;

	return buf_dma;
}

/* Map state->caam_ctx, and add it to link table */
210 211 212
static inline int ctx_map_to_sec4_sg(u32 *desc, struct device *jrdev,
				     struct caam_hash_state *state, int ctx_len,
				     struct sec4_sg_entry *sec4_sg, u32 flag)
Y
Yuan Kang 已提交
213 214
{
	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
215 216 217 218 219
	if (dma_mapping_error(jrdev, state->ctx_dma)) {
		dev_err(jrdev, "unable to map ctx\n");
		return -ENOMEM;
	}

Y
Yuan Kang 已提交
220
	dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
221 222

	return 0;
Y
Yuan Kang 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
}

/* Common shared descriptor commands */
static inline void append_key_ahash(u32 *desc, struct caam_hash_ctx *ctx)
{
	append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
			  ctx->split_key_len, CLASS_2 |
			  KEY_DEST_MDHA_SPLIT | KEY_ENC);
}

/* Append key if it has been set */
static inline void init_sh_desc_key_ahash(u32 *desc, struct caam_hash_ctx *ctx)
{
	u32 *key_jump_cmd;

238
	init_sh_desc(desc, HDR_SHARE_SERIAL);
Y
Yuan Kang 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

	if (ctx->split_key_len) {
		/* Skip if already shared */
		key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
					   JUMP_COND_SHRD);

		append_key_ahash(desc, ctx);

		set_jump_tgt_here(desc, key_jump_cmd);
	}

	/* Propagate errors from shared to job descriptor */
	append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
}

/*
 * For ahash read data from seqin following state->caam_ctx,
 * and write resulting class2 context to seqout, which may be state->caam_ctx
 * or req->result
 */
static inline void ahash_append_load_str(u32 *desc, int digestsize)
{
	/* Calculate remaining bytes to read */
	append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);

	/* Read remaining bytes */
	append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 |
			     FIFOLD_TYPE_MSG | KEY_VLF);

	/* Store class2 context bytes */
	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
			 LDST_SRCDST_BYTE_CONTEXT);
}

/*
 * For ahash update, final and finup, import context, read and write to seqout
 */
static inline void ahash_ctx_data_to_out(u32 *desc, u32 op, u32 state,
					 int digestsize,
					 struct caam_hash_ctx *ctx)
{
	init_sh_desc_key_ahash(desc, ctx);

	/* Import context from software */
	append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
		   LDST_CLASS_2_CCB | ctx->ctx_len);

	/* Class 2 operation */
	append_operation(desc, op | state | OP_ALG_ENCRYPT);

	/*
	 * Load from buf and/or src and write to req->result or state->context
	 */
	ahash_append_load_str(desc, digestsize);
}

/* For ahash firsts and digest, read and write to seqout */
static inline void ahash_data_to_out(u32 *desc, u32 op, u32 state,
				     int digestsize, struct caam_hash_ctx *ctx)
{
	init_sh_desc_key_ahash(desc, ctx);

	/* Class 2 operation */
	append_operation(desc, op | state | OP_ALG_ENCRYPT);

	/*
	 * Load from buf and/or src and write to req->result or state->context
	 */
	ahash_append_load_str(desc, digestsize);
}

static int ahash_set_sh_desc(struct crypto_ahash *ahash)
{
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	int digestsize = crypto_ahash_digestsize(ahash);
	struct device *jrdev = ctx->jrdev;
	u32 have_key = 0;
	u32 *desc;

	if (ctx->split_key_len)
		have_key = OP_ALG_AAI_HMAC_PRECOMP;

	/* ahash_update shared descriptor */
	desc = ctx->sh_desc_update;

324
	init_sh_desc(desc, HDR_SHARE_SERIAL);
Y
Yuan Kang 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

	/* Import context from software */
	append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
		   LDST_CLASS_2_CCB | ctx->ctx_len);

	/* Class 2 operation */
	append_operation(desc, ctx->alg_type | OP_ALG_AS_UPDATE |
			 OP_ALG_ENCRYPT);

	/* Load data and write to result or context */
	ahash_append_load_str(desc, ctx->ctx_len);

	ctx->sh_desc_update_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
						 DMA_TO_DEVICE);
	if (dma_mapping_error(jrdev, ctx->sh_desc_update_dma)) {
		dev_err(jrdev, "unable to map shared descriptor\n");
		return -ENOMEM;
	}
#ifdef DEBUG
344 345
	print_hex_dump(KERN_ERR,
		       "ahash update shdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
#endif

	/* ahash_update_first shared descriptor */
	desc = ctx->sh_desc_update_first;

	ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INIT,
			  ctx->ctx_len, ctx);

	ctx->sh_desc_update_first_dma = dma_map_single(jrdev, desc,
						       desc_bytes(desc),
						       DMA_TO_DEVICE);
	if (dma_mapping_error(jrdev, ctx->sh_desc_update_first_dma)) {
		dev_err(jrdev, "unable to map shared descriptor\n");
		return -ENOMEM;
	}
#ifdef DEBUG
363 364
	print_hex_dump(KERN_ERR,
		       "ahash update first shdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
#endif

	/* ahash_final shared descriptor */
	desc = ctx->sh_desc_fin;

	ahash_ctx_data_to_out(desc, have_key | ctx->alg_type,
			      OP_ALG_AS_FINALIZE, digestsize, ctx);

	ctx->sh_desc_fin_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
					      DMA_TO_DEVICE);
	if (dma_mapping_error(jrdev, ctx->sh_desc_fin_dma)) {
		dev_err(jrdev, "unable to map shared descriptor\n");
		return -ENOMEM;
	}
#ifdef DEBUG
381
	print_hex_dump(KERN_ERR, "ahash final shdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
		       DUMP_PREFIX_ADDRESS, 16, 4, desc,
		       desc_bytes(desc), 1);
#endif

	/* ahash_finup shared descriptor */
	desc = ctx->sh_desc_finup;

	ahash_ctx_data_to_out(desc, have_key | ctx->alg_type,
			      OP_ALG_AS_FINALIZE, digestsize, ctx);

	ctx->sh_desc_finup_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
						DMA_TO_DEVICE);
	if (dma_mapping_error(jrdev, ctx->sh_desc_finup_dma)) {
		dev_err(jrdev, "unable to map shared descriptor\n");
		return -ENOMEM;
	}
#ifdef DEBUG
399
	print_hex_dump(KERN_ERR, "ahash finup shdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
		       DUMP_PREFIX_ADDRESS, 16, 4, desc,
		       desc_bytes(desc), 1);
#endif

	/* ahash_digest shared descriptor */
	desc = ctx->sh_desc_digest;

	ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INITFINAL,
			  digestsize, ctx);

	ctx->sh_desc_digest_dma = dma_map_single(jrdev, desc,
						 desc_bytes(desc),
						 DMA_TO_DEVICE);
	if (dma_mapping_error(jrdev, ctx->sh_desc_digest_dma)) {
		dev_err(jrdev, "unable to map shared descriptor\n");
		return -ENOMEM;
	}
#ifdef DEBUG
418 419
	print_hex_dump(KERN_ERR,
		       "ahash digest shdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
420 421 422 423 424 425 426
		       DUMP_PREFIX_ADDRESS, 16, 4, desc,
		       desc_bytes(desc), 1);
#endif

	return 0;
}

427
static int gen_split_hash_key(struct caam_hash_ctx *ctx, const u8 *key_in,
Y
Yuan Kang 已提交
428 429 430 431 432 433 434 435
			      u32 keylen)
{
	return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len,
			       ctx->split_key_pad_len, key_in, keylen,
			       ctx->alg_op);
}

/* Digest hash size if it is too large */
436
static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
Y
Yuan Kang 已提交
437 438 439 440 441 442 443 444
			   u32 *keylen, u8 *key_out, u32 digestsize)
{
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	struct split_key_result result;
	dma_addr_t src_dma, dst_dma;
	int ret = 0;

445
	desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
K
Kim Phillips 已提交
446 447 448 449
	if (!desc) {
		dev_err(jrdev, "unable to allocate key input memory\n");
		return -ENOMEM;
	}
Y
Yuan Kang 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

	init_job_desc(desc, 0);

	src_dma = dma_map_single(jrdev, (void *)key_in, *keylen,
				 DMA_TO_DEVICE);
	if (dma_mapping_error(jrdev, src_dma)) {
		dev_err(jrdev, "unable to map key input memory\n");
		kfree(desc);
		return -ENOMEM;
	}
	dst_dma = dma_map_single(jrdev, (void *)key_out, digestsize,
				 DMA_FROM_DEVICE);
	if (dma_mapping_error(jrdev, dst_dma)) {
		dev_err(jrdev, "unable to map key output memory\n");
		dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE);
		kfree(desc);
		return -ENOMEM;
	}

	/* Job descriptor to perform unkeyed hash on key_in */
	append_operation(desc, ctx->alg_type | OP_ALG_ENCRYPT |
			 OP_ALG_AS_INITFINAL);
	append_seq_in_ptr(desc, src_dma, *keylen, 0);
	append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
	append_seq_out_ptr(desc, dst_dma, digestsize, 0);
	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
			 LDST_SRCDST_BYTE_CONTEXT);

#ifdef DEBUG
480
	print_hex_dump(KERN_ERR, "key_in@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
481
		       DUMP_PREFIX_ADDRESS, 16, 4, key_in, *keylen, 1);
482
	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
483 484 485 486 487 488 489 490 491 492 493 494
		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
#endif

	result.err = 0;
	init_completion(&result.completion);

	ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
	if (!ret) {
		/* in progress */
		wait_for_completion_interruptible(&result.completion);
		ret = result.err;
#ifdef DEBUG
495 496
		print_hex_dump(KERN_ERR,
			       "digested key@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
497 498 499 500 501 502 503
			       DUMP_PREFIX_ADDRESS, 16, 4, key_in,
			       digestsize, 1);
#endif
	}
	dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE);
	dma_unmap_single(jrdev, dst_dma, digestsize, DMA_FROM_DEVICE);

504 505
	*keylen = digestsize;

Y
Yuan Kang 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	kfree(desc);

	return ret;
}

static int ahash_setkey(struct crypto_ahash *ahash,
			const u8 *key, unsigned int keylen)
{
	/* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
	static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct device *jrdev = ctx->jrdev;
	int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
	int digestsize = crypto_ahash_digestsize(ahash);
	int ret = 0;
	u8 *hashed_key = NULL;

#ifdef DEBUG
	printk(KERN_ERR "keylen %d\n", keylen);
#endif

	if (keylen > blocksize) {
		hashed_key = kmalloc(sizeof(u8) * digestsize, GFP_KERNEL |
				     GFP_DMA);
		if (!hashed_key)
			return -ENOMEM;
		ret = hash_digest_key(ctx, key, &keylen, hashed_key,
				      digestsize);
		if (ret)
			goto badkey;
		key = hashed_key;
	}

	/* Pick class 2 key length from algorithm submask */
	ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
				      OP_ALG_ALGSEL_SHIFT] * 2;
	ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16);

#ifdef DEBUG
	printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
	       ctx->split_key_len, ctx->split_key_pad_len);
547
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
548 549 550 551 552 553 554 555 556 557 558
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif

	ret = gen_split_hash_key(ctx, key, keylen);
	if (ret)
		goto badkey;

	ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len,
				      DMA_TO_DEVICE);
	if (dma_mapping_error(jrdev, ctx->key_dma)) {
		dev_err(jrdev, "unable to map key i/o memory\n");
559 560
		ret = -ENOMEM;
		goto map_err;
Y
Yuan Kang 已提交
561 562
	}
#ifdef DEBUG
563
	print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
564 565 566 567 568 569 570 571 572 573
		       DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
		       ctx->split_key_pad_len, 1);
#endif

	ret = ahash_set_sh_desc(ahash);
	if (ret) {
		dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len,
				 DMA_TO_DEVICE);
	}

574
map_err:
Y
Yuan Kang 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	kfree(hashed_key);
	return ret;
badkey:
	kfree(hashed_key);
	crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
	return -EINVAL;
}

/*
 * ahash_edesc - s/w-extended ahash descriptor
 * @dst_dma: physical mapped address of req->result
 * @sec4_sg_dma: physical mapped address of h/w link table
 * @src_nents: number of segments in input scatterlist
 * @sec4_sg_bytes: length of dma mapped sec4_sg space
 * @hw_desc: the h/w job descriptor followed by any referenced link tables
590
 * @sec4_sg: h/w link table
Y
Yuan Kang 已提交
591 592 593 594 595 596
 */
struct ahash_edesc {
	dma_addr_t dst_dma;
	dma_addr_t sec4_sg_dma;
	int src_nents;
	int sec4_sg_bytes;
597
	u32 hw_desc[DESC_JOB_IO_LEN / sizeof(u32)] ____cacheline_aligned;
598
	struct sec4_sg_entry sec4_sg[0];
Y
Yuan Kang 已提交
599 600 601 602 603 604 605
};

static inline void ahash_unmap(struct device *dev,
			struct ahash_edesc *edesc,
			struct ahash_request *req, int dst_len)
{
	if (edesc->src_nents)
606
		dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	if (edesc->dst_dma)
		dma_unmap_single(dev, edesc->dst_dma, dst_len, DMA_FROM_DEVICE);

	if (edesc->sec4_sg_bytes)
		dma_unmap_single(dev, edesc->sec4_sg_dma,
				 edesc->sec4_sg_bytes, DMA_TO_DEVICE);
}

static inline void ahash_unmap_ctx(struct device *dev,
			struct ahash_edesc *edesc,
			struct ahash_request *req, int dst_len, u32 flag)
{
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);

	if (state->ctx_dma)
		dma_unmap_single(dev, state->ctx_dma, ctx->ctx_len, flag);
	ahash_unmap(dev, edesc, req, dst_len);
}

static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
		       void *context)
{
	struct ahash_request *req = context;
	struct ahash_edesc *edesc;
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	int digestsize = crypto_ahash_digestsize(ahash);
#ifdef DEBUG
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);

	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
#endif

	edesc = (struct ahash_edesc *)((char *)desc -
		 offsetof(struct ahash_edesc, hw_desc));
644 645
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
646 647 648 649 650

	ahash_unmap(jrdev, edesc, req, digestsize);
	kfree(edesc);

#ifdef DEBUG
651
	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
652 653 654
		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
		       ctx->ctx_len, 1);
	if (req->result)
655
		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
			       digestsize, 1);
#endif

	req->base.complete(&req->base, err);
}

static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
			    void *context)
{
	struct ahash_request *req = context;
	struct ahash_edesc *edesc;
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
#ifdef DEBUG
	struct caam_hash_state *state = ahash_request_ctx(req);
	int digestsize = crypto_ahash_digestsize(ahash);

	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
#endif

	edesc = (struct ahash_edesc *)((char *)desc -
		 offsetof(struct ahash_edesc, hw_desc));
679 680
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
681 682 683 684 685

	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
	kfree(edesc);

#ifdef DEBUG
686
	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
687 688 689
		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
		       ctx->ctx_len, 1);
	if (req->result)
690
		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
			       digestsize, 1);
#endif

	req->base.complete(&req->base, err);
}

static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
			       void *context)
{
	struct ahash_request *req = context;
	struct ahash_edesc *edesc;
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	int digestsize = crypto_ahash_digestsize(ahash);
#ifdef DEBUG
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);

	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
#endif

	edesc = (struct ahash_edesc *)((char *)desc -
		 offsetof(struct ahash_edesc, hw_desc));
714 715
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
716

717
	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
718 719 720
	kfree(edesc);

#ifdef DEBUG
721
	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
722 723 724
		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
		       ctx->ctx_len, 1);
	if (req->result)
725
		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
			       digestsize, 1);
#endif

	req->base.complete(&req->base, err);
}

static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
			       void *context)
{
	struct ahash_request *req = context;
	struct ahash_edesc *edesc;
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
#ifdef DEBUG
	struct caam_hash_state *state = ahash_request_ctx(req);
	int digestsize = crypto_ahash_digestsize(ahash);

	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
#endif

	edesc = (struct ahash_edesc *)((char *)desc -
		 offsetof(struct ahash_edesc, hw_desc));
749 750
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
751

752
	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_FROM_DEVICE);
Y
Yuan Kang 已提交
753 754 755
	kfree(edesc);

#ifdef DEBUG
756
	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
757 758 759
		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
		       ctx->ctx_len, 1);
	if (req->result)
760
		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
761 762 763 764 765 766 767
			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
			       digestsize, 1);
#endif

	req->base.complete(&req->base, err);
}

768 769 770 771 772
/*
 * Allocate an enhanced descriptor, which contains the hardware descriptor
 * and space for hardware scatter table containing sg_num entries.
 */
static struct ahash_edesc *ahash_edesc_alloc(struct caam_hash_ctx *ctx,
773 774 775
					     int sg_num, u32 *sh_desc,
					     dma_addr_t sh_desc_dma,
					     gfp_t flags)
776 777 778 779 780 781 782 783 784 785
{
	struct ahash_edesc *edesc;
	unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);

	edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
	if (!edesc) {
		dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
		return NULL;
	}

786 787 788
	init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
			     HDR_SHARE_DEFER | HDR_REVERSE);

789 790 791
	return edesc;
}

Y
Yuan Kang 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
/* submit update job descriptor */
static int ahash_update_ctx(struct ahash_request *req)
{
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);
	struct device *jrdev = ctx->jrdev;
	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
	int *buflen = state->current_buf ? &state->buflen_1 : &state->buflen_0;
	u8 *next_buf = state->current_buf ? state->buf_0 : state->buf_1;
	int *next_buflen = state->current_buf ? &state->buflen_0 :
			   &state->buflen_1, last_buflen;
	int in_len = *buflen + req->nbytes, to_hash;
807
	u32 *desc;
808
	int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
Y
Yuan Kang 已提交
809 810 811 812 813 814 815 816
	struct ahash_edesc *edesc;
	int ret = 0;

	last_buflen = *next_buflen;
	*next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
	to_hash = in_len - *next_buflen;

	if (to_hash) {
817 818
		src_nents = sg_nents_for_len(req->src,
					     req->nbytes - (*next_buflen));
819 820 821 822
		if (src_nents < 0) {
			dev_err(jrdev, "Invalid number of src SG.\n");
			return src_nents;
		}
823 824 825 826 827 828 829 830 831 832 833 834

		if (src_nents) {
			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
						  DMA_TO_DEVICE);
			if (!mapped_nents) {
				dev_err(jrdev, "unable to DMA map source\n");
				return -ENOMEM;
			}
		} else {
			mapped_nents = 0;
		}

Y
Yuan Kang 已提交
835
		sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
836
		sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
Y
Yuan Kang 已提交
837 838 839 840 841 842
				 sizeof(struct sec4_sg_entry);

		/*
		 * allocate space for base edesc and hw desc commands,
		 * link tables
		 */
843
		edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
844 845
					  ctx->sh_desc_update,
					  ctx->sh_desc_update_dma, flags);
Y
Yuan Kang 已提交
846
		if (!edesc) {
847
			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
848 849 850 851 852 853
			return -ENOMEM;
		}

		edesc->src_nents = src_nents;
		edesc->sec4_sg_bytes = sec4_sg_bytes;

854 855 856
		ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len,
					 edesc->sec4_sg, DMA_BIDIRECTIONAL);
		if (ret)
857
			goto err;
Y
Yuan Kang 已提交
858 859 860 861

		state->buf_dma = try_buf_map_to_sec4_sg(jrdev,
							edesc->sec4_sg + 1,
							buf, state->buf_dma,
862
							*buflen, last_buflen);
Y
Yuan Kang 已提交
863

864 865 866 867
		if (mapped_nents) {
			sg_to_sec4_sg_last(req->src, mapped_nents,
					   edesc->sec4_sg + sec4_sg_src_index,
					   0);
868
			if (*next_buflen)
869 870 871
				scatterwalk_map_and_copy(next_buf, req->src,
							 to_hash - *buflen,
							 *next_buflen, 0);
Y
Yuan Kang 已提交
872 873
		} else {
			(edesc->sec4_sg + sec4_sg_src_index - 1)->len |=
874
				cpu_to_caam32(SEC4_SG_LEN_FIN);
Y
Yuan Kang 已提交
875 876
		}

877 878
		state->current_buf = !state->current_buf;

Y
Yuan Kang 已提交
879 880
		desc = edesc->hw_desc;

881 882 883
		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
						     sec4_sg_bytes,
						     DMA_TO_DEVICE);
884 885
		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
			dev_err(jrdev, "unable to map S/G table\n");
886 887
			ret = -ENOMEM;
			goto err;
888
		}
889

Y
Yuan Kang 已提交
890 891 892 893 894 895
		append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
				       to_hash, LDST_SGF);

		append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);

#ifdef DEBUG
896
		print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
897 898 899 900 901
			       DUMP_PREFIX_ADDRESS, 16, 4, desc,
			       desc_bytes(desc), 1);
#endif

		ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req);
902 903 904 905
		if (ret)
			goto err;

		ret = -EINPROGRESS;
Y
Yuan Kang 已提交
906
	} else if (*next_buflen) {
907 908
		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
					 req->nbytes, 0);
Y
Yuan Kang 已提交
909 910 911 912
		*buflen = *next_buflen;
		*next_buflen = last_buflen;
	}
#ifdef DEBUG
913
	print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
914
		       DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
915
	print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
916 917 918 919 920
		       DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
		       *next_buflen, 1);
#endif

	return ret;
921 922 923 924 925

 err:
	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
	kfree(edesc);
	return ret;
Y
Yuan Kang 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939
}

static int ahash_final_ctx(struct ahash_request *req)
{
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);
	struct device *jrdev = ctx->jrdev;
	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
	int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
	int last_buflen = state->current_buf ? state->buflen_0 :
			  state->buflen_1;
940
	u32 *desc;
941
	int sec4_sg_bytes, sec4_sg_src_index;
Y
Yuan Kang 已提交
942 943 944 945
	int digestsize = crypto_ahash_digestsize(ahash);
	struct ahash_edesc *edesc;
	int ret = 0;

946 947
	sec4_sg_src_index = 1 + (buflen ? 1 : 0);
	sec4_sg_bytes = sec4_sg_src_index * sizeof(struct sec4_sg_entry);
Y
Yuan Kang 已提交
948 949

	/* allocate space for base edesc and hw desc commands, link tables */
950 951 952
	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index,
				  ctx->sh_desc_fin, ctx->sh_desc_fin_dma,
				  flags);
953
	if (!edesc)
Y
Yuan Kang 已提交
954 955 956 957 958 959 960
		return -ENOMEM;

	desc = edesc->hw_desc;

	edesc->sec4_sg_bytes = sec4_sg_bytes;
	edesc->src_nents = 0;

961 962 963
	ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len,
				 edesc->sec4_sg, DMA_TO_DEVICE);
	if (ret)
964
		goto err;
Y
Yuan Kang 已提交
965 966 967 968

	state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1,
						buf, state->buf_dma, buflen,
						last_buflen);
969 970
	(edesc->sec4_sg + sec4_sg_src_index - 1)->len |=
		cpu_to_caam32(SEC4_SG_LEN_FIN);
Y
Yuan Kang 已提交
971

972 973
	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
					    sec4_sg_bytes, DMA_TO_DEVICE);
974 975
	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
		dev_err(jrdev, "unable to map S/G table\n");
976 977
		ret = -ENOMEM;
		goto err;
978
	}
979

Y
Yuan Kang 已提交
980 981 982 983 984
	append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
			  LDST_SGF);

	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
						digestsize);
985 986
	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
		dev_err(jrdev, "unable to map dst\n");
987 988
		ret = -ENOMEM;
		goto err;
989
	}
Y
Yuan Kang 已提交
990 991

#ifdef DEBUG
992
	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
993 994 995 996
		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
#endif

	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
997 998
	if (ret)
		goto err;
Y
Yuan Kang 已提交
999

1000 1001 1002 1003 1004
	return -EINPROGRESS;

err:
	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
	kfree(edesc);
Y
Yuan Kang 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	return ret;
}

static int ahash_finup_ctx(struct ahash_request *req)
{
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);
	struct device *jrdev = ctx->jrdev;
	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
	int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
	int last_buflen = state->current_buf ? state->buflen_0 :
			  state->buflen_1;
1020
	u32 *desc;
Y
Yuan Kang 已提交
1021
	int sec4_sg_bytes, sec4_sg_src_index;
1022
	int src_nents, mapped_nents;
Y
Yuan Kang 已提交
1023 1024 1025 1026
	int digestsize = crypto_ahash_digestsize(ahash);
	struct ahash_edesc *edesc;
	int ret = 0;

1027
	src_nents = sg_nents_for_len(req->src, req->nbytes);
1028 1029 1030 1031
	if (src_nents < 0) {
		dev_err(jrdev, "Invalid number of src SG.\n");
		return src_nents;
	}
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043

	if (src_nents) {
		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
					  DMA_TO_DEVICE);
		if (!mapped_nents) {
			dev_err(jrdev, "unable to DMA map source\n");
			return -ENOMEM;
		}
	} else {
		mapped_nents = 0;
	}

Y
Yuan Kang 已提交
1044
	sec4_sg_src_index = 1 + (buflen ? 1 : 0);
1045
	sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
Y
Yuan Kang 已提交
1046 1047 1048
			 sizeof(struct sec4_sg_entry);

	/* allocate space for base edesc and hw desc commands, link tables */
1049
	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
1050
				  ctx->sh_desc_finup, ctx->sh_desc_finup_dma,
1051
				  flags);
Y
Yuan Kang 已提交
1052
	if (!edesc) {
1053
		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
1054 1055 1056 1057 1058 1059 1060 1061
		return -ENOMEM;
	}

	desc = edesc->hw_desc;

	edesc->src_nents = src_nents;
	edesc->sec4_sg_bytes = sec4_sg_bytes;

1062 1063 1064
	ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len,
				 edesc->sec4_sg, DMA_TO_DEVICE);
	if (ret)
1065
		goto err;
Y
Yuan Kang 已提交
1066 1067 1068 1069 1070

	state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1,
						buf, state->buf_dma, buflen,
						last_buflen);

1071 1072
	sg_to_sec4_sg_last(req->src, mapped_nents,
			   edesc->sec4_sg + sec4_sg_src_index, 0);
Y
Yuan Kang 已提交
1073

1074 1075
	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
					    sec4_sg_bytes, DMA_TO_DEVICE);
1076 1077
	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
		dev_err(jrdev, "unable to map S/G table\n");
1078 1079
		ret = -ENOMEM;
		goto err;
1080
	}
1081

Y
Yuan Kang 已提交
1082 1083 1084 1085 1086
	append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
			       buflen + req->nbytes, LDST_SGF);

	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
						digestsize);
1087 1088
	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
		dev_err(jrdev, "unable to map dst\n");
1089 1090
		ret = -ENOMEM;
		goto err;
1091
	}
Y
Yuan Kang 已提交
1092 1093

#ifdef DEBUG
1094
	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1095 1096 1097 1098
		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
#endif

	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
1099 1100
	if (ret)
		goto err;
Y
Yuan Kang 已提交
1101

1102 1103 1104 1105 1106
	return -EINPROGRESS;

err:
	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
	kfree(edesc);
Y
Yuan Kang 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	return ret;
}

static int ahash_digest(struct ahash_request *req)
{
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct device *jrdev = ctx->jrdev;
	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1117
	u32 *desc;
Y
Yuan Kang 已提交
1118
	int digestsize = crypto_ahash_digestsize(ahash);
1119
	int src_nents, mapped_nents, sec4_sg_bytes;
Y
Yuan Kang 已提交
1120 1121 1122 1123 1124
	dma_addr_t src_dma;
	struct ahash_edesc *edesc;
	int ret = 0;
	u32 options;

1125
	src_nents = sg_nents_for_len(req->src, req->nbytes);
1126 1127 1128 1129
	if (src_nents < 0) {
		dev_err(jrdev, "Invalid number of src SG.\n");
		return src_nents;
	}
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

	if (src_nents) {
		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
					  DMA_TO_DEVICE);
		if (!mapped_nents) {
			dev_err(jrdev, "unable to map source for DMA\n");
			return -ENOMEM;
		}
	} else {
		mapped_nents = 0;
	}

	if (mapped_nents > 1)
		sec4_sg_bytes = mapped_nents * sizeof(struct sec4_sg_entry);
1144 1145
	else
		sec4_sg_bytes = 0;
Y
Yuan Kang 已提交
1146 1147

	/* allocate space for base edesc and hw desc commands, link tables */
1148
	edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ? mapped_nents : 0,
1149
				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
1150
				  flags);
Y
Yuan Kang 已提交
1151
	if (!edesc) {
1152
		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
1153 1154
		return -ENOMEM;
	}
1155

1156
	edesc->sec4_sg_bytes = sec4_sg_bytes;
Y
Yuan Kang 已提交
1157 1158 1159 1160
	edesc->src_nents = src_nents;

	desc = edesc->hw_desc;

1161
	if (src_nents > 1) {
1162
		sg_to_sec4_sg_last(req->src, mapped_nents, edesc->sec4_sg, 0);
1163 1164
		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
					    sec4_sg_bytes, DMA_TO_DEVICE);
1165 1166
		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
			dev_err(jrdev, "unable to map S/G table\n");
1167 1168
			ahash_unmap(jrdev, edesc, req, digestsize);
			kfree(edesc);
1169 1170
			return -ENOMEM;
		}
Y
Yuan Kang 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
		src_dma = edesc->sec4_sg_dma;
		options = LDST_SGF;
	} else {
		src_dma = sg_dma_address(req->src);
		options = 0;
	}
	append_seq_in_ptr(desc, src_dma, req->nbytes, options);

	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
						digestsize);
1181 1182
	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
		dev_err(jrdev, "unable to map dst\n");
1183 1184
		ahash_unmap(jrdev, edesc, req, digestsize);
		kfree(edesc);
1185 1186
		return -ENOMEM;
	}
Y
Yuan Kang 已提交
1187 1188

#ifdef DEBUG
1189
	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
#endif

	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		ahash_unmap(jrdev, edesc, req, digestsize);
		kfree(edesc);
	}

	return ret;
}

/* submit ahash final if it the first job descriptor */
static int ahash_final_no_ctx(struct ahash_request *req)
{
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);
	struct device *jrdev = ctx->jrdev;
	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
	int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
1215
	u32 *desc;
Y
Yuan Kang 已提交
1216 1217 1218 1219 1220
	int digestsize = crypto_ahash_digestsize(ahash);
	struct ahash_edesc *edesc;
	int ret = 0;

	/* allocate space for base edesc and hw desc commands, link tables */
1221 1222
	edesc = ahash_edesc_alloc(ctx, 0, ctx->sh_desc_digest,
				  ctx->sh_desc_digest_dma, flags);
1223
	if (!edesc)
Y
Yuan Kang 已提交
1224 1225 1226 1227 1228
		return -ENOMEM;

	desc = edesc->hw_desc;

	state->buf_dma = dma_map_single(jrdev, buf, buflen, DMA_TO_DEVICE);
1229 1230
	if (dma_mapping_error(jrdev, state->buf_dma)) {
		dev_err(jrdev, "unable to map src\n");
1231 1232
		ahash_unmap(jrdev, edesc, req, digestsize);
		kfree(edesc);
1233 1234
		return -ENOMEM;
	}
Y
Yuan Kang 已提交
1235 1236 1237 1238 1239

	append_seq_in_ptr(desc, state->buf_dma, buflen, 0);

	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
						digestsize);
1240 1241
	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
		dev_err(jrdev, "unable to map dst\n");
1242 1243
		ahash_unmap(jrdev, edesc, req, digestsize);
		kfree(edesc);
1244 1245
		return -ENOMEM;
	}
Y
Yuan Kang 已提交
1246 1247 1248
	edesc->src_nents = 0;

#ifdef DEBUG
1249
	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
#endif

	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		ahash_unmap(jrdev, edesc, req, digestsize);
		kfree(edesc);
	}

	return ret;
}

/* submit ahash update if it the first job descriptor after update */
static int ahash_update_no_ctx(struct ahash_request *req)
{
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);
	struct device *jrdev = ctx->jrdev;
	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
	int *buflen = state->current_buf ? &state->buflen_1 : &state->buflen_0;
	u8 *next_buf = state->current_buf ? state->buf_0 : state->buf_1;
	int *next_buflen = state->current_buf ? &state->buflen_0 :
			   &state->buflen_1;
	int in_len = *buflen + req->nbytes, to_hash;
1279
	int sec4_sg_bytes, src_nents, mapped_nents;
Y
Yuan Kang 已提交
1280
	struct ahash_edesc *edesc;
1281
	u32 *desc;
Y
Yuan Kang 已提交
1282 1283 1284 1285 1286 1287
	int ret = 0;

	*next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
	to_hash = in_len - *next_buflen;

	if (to_hash) {
1288
		src_nents = sg_nents_for_len(req->src,
1289
					     req->nbytes - *next_buflen);
1290 1291 1292 1293
		if (src_nents < 0) {
			dev_err(jrdev, "Invalid number of src SG.\n");
			return src_nents;
		}
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306

		if (src_nents) {
			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
						  DMA_TO_DEVICE);
			if (!mapped_nents) {
				dev_err(jrdev, "unable to DMA map source\n");
				return -ENOMEM;
			}
		} else {
			mapped_nents = 0;
		}

		sec4_sg_bytes = (1 + mapped_nents) *
Y
Yuan Kang 已提交
1307 1308 1309 1310 1311 1312
				sizeof(struct sec4_sg_entry);

		/*
		 * allocate space for base edesc and hw desc commands,
		 * link tables
		 */
1313 1314 1315 1316
		edesc = ahash_edesc_alloc(ctx, 1 + mapped_nents,
					  ctx->sh_desc_update_first,
					  ctx->sh_desc_update_first_dma,
					  flags);
Y
Yuan Kang 已提交
1317
		if (!edesc) {
1318
			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
1319 1320 1321 1322 1323
			return -ENOMEM;
		}

		edesc->src_nents = src_nents;
		edesc->sec4_sg_bytes = sec4_sg_bytes;
1324
		edesc->dst_dma = 0;
Y
Yuan Kang 已提交
1325 1326 1327

		state->buf_dma = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg,
						    buf, *buflen);
1328 1329 1330
		sg_to_sec4_sg_last(req->src, mapped_nents,
				   edesc->sec4_sg + 1, 0);

Y
Yuan Kang 已提交
1331
		if (*next_buflen) {
1332 1333 1334
			scatterwalk_map_and_copy(next_buf, req->src,
						 to_hash - *buflen,
						 *next_buflen, 0);
Y
Yuan Kang 已提交
1335 1336
		}

1337 1338
		state->current_buf = !state->current_buf;

Y
Yuan Kang 已提交
1339 1340
		desc = edesc->hw_desc;

1341 1342 1343
		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
						    sec4_sg_bytes,
						    DMA_TO_DEVICE);
1344 1345
		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
			dev_err(jrdev, "unable to map S/G table\n");
1346 1347
			ret = -ENOMEM;
			goto err;
1348
		}
1349

Y
Yuan Kang 已提交
1350 1351
		append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);

1352 1353
		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
		if (ret)
1354
			goto err;
Y
Yuan Kang 已提交
1355 1356

#ifdef DEBUG
1357
		print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1358 1359 1360 1361 1362
			       DUMP_PREFIX_ADDRESS, 16, 4, desc,
			       desc_bytes(desc), 1);
#endif

		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
1363 1364 1365 1366 1367 1368 1369
		if (ret)
			goto err;

		ret = -EINPROGRESS;
		state->update = ahash_update_ctx;
		state->finup = ahash_finup_ctx;
		state->final = ahash_final_ctx;
Y
Yuan Kang 已提交
1370
	} else if (*next_buflen) {
1371 1372
		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
					 req->nbytes, 0);
Y
Yuan Kang 已提交
1373 1374 1375 1376
		*buflen = *next_buflen;
		*next_buflen = 0;
	}
#ifdef DEBUG
1377
	print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1378
		       DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
1379
	print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1380 1381 1382 1383 1384
		       DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
		       *next_buflen, 1);
#endif

	return ret;
1385 1386 1387 1388 1389

err:
	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
	kfree(edesc);
	return ret;
Y
Yuan Kang 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
}

/* submit ahash finup if it the first job descriptor after update */
static int ahash_finup_no_ctx(struct ahash_request *req)
{
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);
	struct device *jrdev = ctx->jrdev;
	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
	int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
	int last_buflen = state->current_buf ? state->buflen_0 :
			  state->buflen_1;
1405
	u32 *desc;
1406
	int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
Y
Yuan Kang 已提交
1407 1408 1409 1410
	int digestsize = crypto_ahash_digestsize(ahash);
	struct ahash_edesc *edesc;
	int ret = 0;

1411
	src_nents = sg_nents_for_len(req->src, req->nbytes);
1412 1413 1414 1415
	if (src_nents < 0) {
		dev_err(jrdev, "Invalid number of src SG.\n");
		return src_nents;
	}
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427

	if (src_nents) {
		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
					  DMA_TO_DEVICE);
		if (!mapped_nents) {
			dev_err(jrdev, "unable to DMA map source\n");
			return -ENOMEM;
		}
	} else {
		mapped_nents = 0;
	}

Y
Yuan Kang 已提交
1428
	sec4_sg_src_index = 2;
1429
	sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
Y
Yuan Kang 已提交
1430 1431 1432
			 sizeof(struct sec4_sg_entry);

	/* allocate space for base edesc and hw desc commands, link tables */
1433 1434 1435
	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
				  flags);
Y
Yuan Kang 已提交
1436
	if (!edesc) {
1437
		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
		return -ENOMEM;
	}

	desc = edesc->hw_desc;

	edesc->src_nents = src_nents;
	edesc->sec4_sg_bytes = sec4_sg_bytes;

	state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, buf,
						state->buf_dma, buflen,
						last_buflen);

1450
	sg_to_sec4_sg_last(req->src, mapped_nents, edesc->sec4_sg + 1, 0);
Y
Yuan Kang 已提交
1451

1452 1453
	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
					    sec4_sg_bytes, DMA_TO_DEVICE);
1454 1455
	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
		dev_err(jrdev, "unable to map S/G table\n");
1456 1457
		ahash_unmap(jrdev, edesc, req, digestsize);
		kfree(edesc);
1458 1459
		return -ENOMEM;
	}
1460

Y
Yuan Kang 已提交
1461 1462 1463 1464 1465
	append_seq_in_ptr(desc, edesc->sec4_sg_dma, buflen +
			       req->nbytes, LDST_SGF);

	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
						digestsize);
1466 1467
	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
		dev_err(jrdev, "unable to map dst\n");
1468 1469
		ahash_unmap(jrdev, edesc, req, digestsize);
		kfree(edesc);
1470 1471
		return -ENOMEM;
	}
Y
Yuan Kang 已提交
1472 1473

#ifdef DEBUG
1474
	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
#endif

	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		ahash_unmap(jrdev, edesc, req, digestsize);
		kfree(edesc);
	}

	return ret;
}

/* submit first update job descriptor after init */
static int ahash_update_first(struct ahash_request *req)
{
	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
	struct caam_hash_state *state = ahash_request_ctx(req);
	struct device *jrdev = ctx->jrdev;
	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1498 1499 1500
	u8 *next_buf = state->current_buf ? state->buf_1 : state->buf_0;
	int *next_buflen = state->current_buf ?
		&state->buflen_1 : &state->buflen_0;
Y
Yuan Kang 已提交
1501
	int to_hash;
1502
	u32 *desc;
1503
	int sec4_sg_bytes, src_nents, mapped_nents;
Y
Yuan Kang 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
	dma_addr_t src_dma;
	u32 options;
	struct ahash_edesc *edesc;
	int ret = 0;

	*next_buflen = req->nbytes & (crypto_tfm_alg_blocksize(&ahash->base) -
				      1);
	to_hash = req->nbytes - *next_buflen;

	if (to_hash) {
1514 1515
		src_nents = sg_nents_for_len(req->src,
					     req->nbytes - *next_buflen);
1516 1517 1518 1519
		if (src_nents < 0) {
			dev_err(jrdev, "Invalid number of src SG.\n");
			return src_nents;
		}
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532

		if (src_nents) {
			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
						  DMA_TO_DEVICE);
			if (!mapped_nents) {
				dev_err(jrdev, "unable to map source for DMA\n");
				return -ENOMEM;
			}
		} else {
			mapped_nents = 0;
		}
		if (mapped_nents > 1)
			sec4_sg_bytes = mapped_nents *
1533 1534 1535
					sizeof(struct sec4_sg_entry);
		else
			sec4_sg_bytes = 0;
Y
Yuan Kang 已提交
1536 1537 1538 1539 1540

		/*
		 * allocate space for base edesc and hw desc commands,
		 * link tables
		 */
1541
		edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ?
1542 1543 1544 1545
					  mapped_nents : 0,
					  ctx->sh_desc_update_first,
					  ctx->sh_desc_update_first_dma,
					  flags);
Y
Yuan Kang 已提交
1546
		if (!edesc) {
1547
			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
1548 1549 1550 1551 1552
			return -ENOMEM;
		}

		edesc->src_nents = src_nents;
		edesc->sec4_sg_bytes = sec4_sg_bytes;
1553
		edesc->dst_dma = 0;
Y
Yuan Kang 已提交
1554

1555
		if (src_nents > 1) {
1556
			sg_to_sec4_sg_last(req->src, mapped_nents,
Y
Yuan Kang 已提交
1557
					   edesc->sec4_sg, 0);
1558 1559 1560 1561
			edesc->sec4_sg_dma = dma_map_single(jrdev,
							    edesc->sec4_sg,
							    sec4_sg_bytes,
							    DMA_TO_DEVICE);
1562 1563
			if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
				dev_err(jrdev, "unable to map S/G table\n");
1564 1565
				ret = -ENOMEM;
				goto err;
1566
			}
Y
Yuan Kang 已提交
1567 1568 1569 1570 1571 1572 1573 1574
			src_dma = edesc->sec4_sg_dma;
			options = LDST_SGF;
		} else {
			src_dma = sg_dma_address(req->src);
			options = 0;
		}

		if (*next_buflen)
1575 1576
			scatterwalk_map_and_copy(next_buf, req->src, to_hash,
						 *next_buflen, 0);
Y
Yuan Kang 已提交
1577 1578 1579 1580 1581

		desc = edesc->hw_desc;

		append_seq_in_ptr(desc, src_dma, to_hash, options);

1582 1583
		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
		if (ret)
1584
			goto err;
Y
Yuan Kang 已提交
1585 1586

#ifdef DEBUG
1587
		print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1588 1589 1590 1591
			       DUMP_PREFIX_ADDRESS, 16, 4, desc,
			       desc_bytes(desc), 1);
#endif

1592 1593 1594 1595 1596 1597 1598 1599
		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
		if (ret)
			goto err;

		ret = -EINPROGRESS;
		state->update = ahash_update_ctx;
		state->finup = ahash_finup_ctx;
		state->final = ahash_final_ctx;
Y
Yuan Kang 已提交
1600 1601 1602 1603
	} else if (*next_buflen) {
		state->update = ahash_update_no_ctx;
		state->finup = ahash_finup_no_ctx;
		state->final = ahash_final_no_ctx;
1604 1605
		scatterwalk_map_and_copy(next_buf, req->src, 0,
					 req->nbytes, 0);
Y
Yuan Kang 已提交
1606 1607
	}
#ifdef DEBUG
1608
	print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1609 1610 1611 1612 1613
		       DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
		       *next_buflen, 1);
#endif

	return ret;
1614 1615 1616 1617 1618

err:
	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
	kfree(edesc);
	return ret;
Y
Yuan Kang 已提交
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
}

static int ahash_finup_first(struct ahash_request *req)
{
	return ahash_digest(req);
}

static int ahash_init(struct ahash_request *req)
{
	struct caam_hash_state *state = ahash_request_ctx(req);

	state->update = ahash_update_first;
	state->finup = ahash_finup_first;
	state->final = ahash_final_no_ctx;

	state->current_buf = 0;
1635
	state->buf_dma = 0;
1636 1637
	state->buflen_0 = 0;
	state->buflen_1 = 0;
Y
Yuan Kang 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665

	return 0;
}

static int ahash_update(struct ahash_request *req)
{
	struct caam_hash_state *state = ahash_request_ctx(req);

	return state->update(req);
}

static int ahash_finup(struct ahash_request *req)
{
	struct caam_hash_state *state = ahash_request_ctx(req);

	return state->finup(req);
}

static int ahash_final(struct ahash_request *req)
{
	struct caam_hash_state *state = ahash_request_ctx(req);

	return state->final(req);
}

static int ahash_export(struct ahash_request *req, void *out)
{
	struct caam_hash_state *state = ahash_request_ctx(req);
1666 1667 1668
	struct caam_export_state *export = out;
	int len;
	u8 *buf;
Y
Yuan Kang 已提交
1669

1670 1671 1672 1673 1674
	if (state->current_buf) {
		buf = state->buf_1;
		len = state->buflen_1;
	} else {
		buf = state->buf_0;
1675
		len = state->buflen_0;
1676 1677 1678 1679 1680 1681 1682 1683
	}

	memcpy(export->buf, buf, len);
	memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
	export->buflen = len;
	export->update = state->update;
	export->final = state->final;
	export->finup = state->finup;
1684

Y
Yuan Kang 已提交
1685 1686 1687 1688 1689 1690
	return 0;
}

static int ahash_import(struct ahash_request *req, const void *in)
{
	struct caam_hash_state *state = ahash_request_ctx(req);
1691
	const struct caam_export_state *export = in;
Y
Yuan Kang 已提交
1692

1693 1694 1695 1696 1697 1698 1699
	memset(state, 0, sizeof(*state));
	memcpy(state->buf_0, export->buf, export->buflen);
	memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
	state->buflen_0 = export->buflen;
	state->update = export->update;
	state->final = export->final;
	state->finup = export->finup;
1700

Y
Yuan Kang 已提交
1701 1702 1703 1704 1705 1706
	return 0;
}

struct caam_hash_template {
	char name[CRYPTO_MAX_ALG_NAME];
	char driver_name[CRYPTO_MAX_ALG_NAME];
Y
Yuan Kang 已提交
1707 1708
	char hmac_name[CRYPTO_MAX_ALG_NAME];
	char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
Y
Yuan Kang 已提交
1709 1710 1711 1712 1713 1714 1715 1716 1717
	unsigned int blocksize;
	struct ahash_alg template_ahash;
	u32 alg_type;
	u32 alg_op;
};

/* ahash descriptors */
static struct caam_hash_template driver_hash[] = {
	{
Y
Yuan Kang 已提交
1718 1719 1720 1721
		.name = "sha1",
		.driver_name = "sha1-caam",
		.hmac_name = "hmac(sha1)",
		.hmac_driver_name = "hmac-sha1-caam",
Y
Yuan Kang 已提交
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
		.blocksize = SHA1_BLOCK_SIZE,
		.template_ahash = {
			.init = ahash_init,
			.update = ahash_update,
			.final = ahash_final,
			.finup = ahash_finup,
			.digest = ahash_digest,
			.export = ahash_export,
			.import = ahash_import,
			.setkey = ahash_setkey,
			.halg = {
				.digestsize = SHA1_DIGEST_SIZE,
1734
				.statesize = sizeof(struct caam_export_state),
Y
Yuan Kang 已提交
1735
			},
1736
		},
Y
Yuan Kang 已提交
1737 1738 1739
		.alg_type = OP_ALG_ALGSEL_SHA1,
		.alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
	}, {
Y
Yuan Kang 已提交
1740 1741 1742 1743
		.name = "sha224",
		.driver_name = "sha224-caam",
		.hmac_name = "hmac(sha224)",
		.hmac_driver_name = "hmac-sha224-caam",
Y
Yuan Kang 已提交
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
		.blocksize = SHA224_BLOCK_SIZE,
		.template_ahash = {
			.init = ahash_init,
			.update = ahash_update,
			.final = ahash_final,
			.finup = ahash_finup,
			.digest = ahash_digest,
			.export = ahash_export,
			.import = ahash_import,
			.setkey = ahash_setkey,
			.halg = {
				.digestsize = SHA224_DIGEST_SIZE,
1756
				.statesize = sizeof(struct caam_export_state),
Y
Yuan Kang 已提交
1757
			},
1758
		},
Y
Yuan Kang 已提交
1759 1760 1761
		.alg_type = OP_ALG_ALGSEL_SHA224,
		.alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
	}, {
Y
Yuan Kang 已提交
1762 1763 1764 1765
		.name = "sha256",
		.driver_name = "sha256-caam",
		.hmac_name = "hmac(sha256)",
		.hmac_driver_name = "hmac-sha256-caam",
Y
Yuan Kang 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
		.blocksize = SHA256_BLOCK_SIZE,
		.template_ahash = {
			.init = ahash_init,
			.update = ahash_update,
			.final = ahash_final,
			.finup = ahash_finup,
			.digest = ahash_digest,
			.export = ahash_export,
			.import = ahash_import,
			.setkey = ahash_setkey,
			.halg = {
				.digestsize = SHA256_DIGEST_SIZE,
1778
				.statesize = sizeof(struct caam_export_state),
Y
Yuan Kang 已提交
1779
			},
1780
		},
Y
Yuan Kang 已提交
1781 1782 1783
		.alg_type = OP_ALG_ALGSEL_SHA256,
		.alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
	}, {
Y
Yuan Kang 已提交
1784 1785 1786 1787
		.name = "sha384",
		.driver_name = "sha384-caam",
		.hmac_name = "hmac(sha384)",
		.hmac_driver_name = "hmac-sha384-caam",
Y
Yuan Kang 已提交
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
		.blocksize = SHA384_BLOCK_SIZE,
		.template_ahash = {
			.init = ahash_init,
			.update = ahash_update,
			.final = ahash_final,
			.finup = ahash_finup,
			.digest = ahash_digest,
			.export = ahash_export,
			.import = ahash_import,
			.setkey = ahash_setkey,
			.halg = {
				.digestsize = SHA384_DIGEST_SIZE,
1800
				.statesize = sizeof(struct caam_export_state),
Y
Yuan Kang 已提交
1801
			},
1802
		},
Y
Yuan Kang 已提交
1803 1804 1805
		.alg_type = OP_ALG_ALGSEL_SHA384,
		.alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
	}, {
Y
Yuan Kang 已提交
1806 1807 1808 1809
		.name = "sha512",
		.driver_name = "sha512-caam",
		.hmac_name = "hmac(sha512)",
		.hmac_driver_name = "hmac-sha512-caam",
Y
Yuan Kang 已提交
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
		.blocksize = SHA512_BLOCK_SIZE,
		.template_ahash = {
			.init = ahash_init,
			.update = ahash_update,
			.final = ahash_final,
			.finup = ahash_finup,
			.digest = ahash_digest,
			.export = ahash_export,
			.import = ahash_import,
			.setkey = ahash_setkey,
			.halg = {
				.digestsize = SHA512_DIGEST_SIZE,
1822
				.statesize = sizeof(struct caam_export_state),
Y
Yuan Kang 已提交
1823
			},
1824
		},
Y
Yuan Kang 已提交
1825 1826 1827
		.alg_type = OP_ALG_ALGSEL_SHA512,
		.alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
	}, {
Y
Yuan Kang 已提交
1828 1829 1830 1831
		.name = "md5",
		.driver_name = "md5-caam",
		.hmac_name = "hmac(md5)",
		.hmac_driver_name = "hmac-md5-caam",
Y
Yuan Kang 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
		.blocksize = MD5_BLOCK_WORDS * 4,
		.template_ahash = {
			.init = ahash_init,
			.update = ahash_update,
			.final = ahash_final,
			.finup = ahash_finup,
			.digest = ahash_digest,
			.export = ahash_export,
			.import = ahash_import,
			.setkey = ahash_setkey,
			.halg = {
				.digestsize = MD5_DIGEST_SIZE,
1844
				.statesize = sizeof(struct caam_export_state),
Y
Yuan Kang 已提交
1845
			},
1846
		},
Y
Yuan Kang 已提交
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
		.alg_type = OP_ALG_ALGSEL_MD5,
		.alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
	},
};

struct caam_hash_alg {
	struct list_head entry;
	int alg_type;
	int alg_op;
	struct ahash_alg ahash_alg;
};

static int caam_hash_cra_init(struct crypto_tfm *tfm)
{
	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
	struct crypto_alg *base = tfm->__crt_alg;
	struct hash_alg_common *halg =
		 container_of(base, struct hash_alg_common, base);
	struct ahash_alg *alg =
		 container_of(halg, struct ahash_alg, halg);
	struct caam_hash_alg *caam_hash =
		 container_of(alg, struct caam_hash_alg, ahash_alg);
	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
	/* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
	static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
					 HASH_MSG_LEN + SHA1_DIGEST_SIZE,
					 HASH_MSG_LEN + 32,
					 HASH_MSG_LEN + SHA256_DIGEST_SIZE,
					 HASH_MSG_LEN + 64,
					 HASH_MSG_LEN + SHA512_DIGEST_SIZE };
	int ret = 0;

	/*
1880
	 * Get a Job ring from Job Ring driver to ensure in-order
Y
Yuan Kang 已提交
1881 1882
	 * crypto request processing per tfm
	 */
1883 1884 1885 1886 1887
	ctx->jrdev = caam_jr_alloc();
	if (IS_ERR(ctx->jrdev)) {
		pr_err("Job Ring Device allocation for transform failed\n");
		return PTR_ERR(ctx->jrdev);
	}
Y
Yuan Kang 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
	/* copy descriptor header template value */
	ctx->alg_type = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
	ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_hash->alg_op;

	ctx->ctx_len = runninglen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
				  OP_ALG_ALGSEL_SHIFT];

	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
				 sizeof(struct caam_hash_state));

	ret = ahash_set_sh_desc(ahash);

	return ret;
}

static void caam_hash_cra_exit(struct crypto_tfm *tfm)
{
	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);

	if (ctx->sh_desc_update_dma &&
	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_update_dma))
		dma_unmap_single(ctx->jrdev, ctx->sh_desc_update_dma,
				 desc_bytes(ctx->sh_desc_update),
				 DMA_TO_DEVICE);
	if (ctx->sh_desc_update_first_dma &&
	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_update_first_dma))
		dma_unmap_single(ctx->jrdev, ctx->sh_desc_update_first_dma,
				 desc_bytes(ctx->sh_desc_update_first),
				 DMA_TO_DEVICE);
	if (ctx->sh_desc_fin_dma &&
	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_fin_dma))
		dma_unmap_single(ctx->jrdev, ctx->sh_desc_fin_dma,
				 desc_bytes(ctx->sh_desc_fin), DMA_TO_DEVICE);
	if (ctx->sh_desc_digest_dma &&
	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_digest_dma))
		dma_unmap_single(ctx->jrdev, ctx->sh_desc_digest_dma,
				 desc_bytes(ctx->sh_desc_digest),
				 DMA_TO_DEVICE);
	if (ctx->sh_desc_finup_dma &&
	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_finup_dma))
		dma_unmap_single(ctx->jrdev, ctx->sh_desc_finup_dma,
				 desc_bytes(ctx->sh_desc_finup), DMA_TO_DEVICE);
1930 1931

	caam_jr_free(ctx->jrdev);
Y
Yuan Kang 已提交
1932 1933 1934 1935 1936 1937
}

static void __exit caam_algapi_hash_exit(void)
{
	struct caam_hash_alg *t_alg, *n;

1938
	if (!hash_list.next)
Y
Yuan Kang 已提交
1939 1940
		return;

1941
	list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
Y
Yuan Kang 已提交
1942 1943 1944 1945 1946 1947 1948
		crypto_unregister_ahash(&t_alg->ahash_alg);
		list_del(&t_alg->entry);
		kfree(t_alg);
	}
}

static struct caam_hash_alg *
1949
caam_hash_alloc(struct caam_hash_template *template,
Y
Yuan Kang 已提交
1950
		bool keyed)
Y
Yuan Kang 已提交
1951 1952 1953 1954 1955
{
	struct caam_hash_alg *t_alg;
	struct ahash_alg *halg;
	struct crypto_alg *alg;

1956
	t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
Y
Yuan Kang 已提交
1957
	if (!t_alg) {
1958
		pr_err("failed to allocate t_alg\n");
Y
Yuan Kang 已提交
1959 1960 1961 1962 1963 1964 1965
		return ERR_PTR(-ENOMEM);
	}

	t_alg->ahash_alg = template->template_ahash;
	halg = &t_alg->ahash_alg;
	alg = &halg->halg.base;

Y
Yuan Kang 已提交
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
	if (keyed) {
		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
			 template->hmac_name);
		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
			 template->hmac_driver_name);
	} else {
		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
			 template->name);
		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
			 template->driver_name);
1976
		t_alg->ahash_alg.setkey = NULL;
Y
Yuan Kang 已提交
1977
	}
Y
Yuan Kang 已提交
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
	alg->cra_module = THIS_MODULE;
	alg->cra_init = caam_hash_cra_init;
	alg->cra_exit = caam_hash_cra_exit;
	alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
	alg->cra_priority = CAAM_CRA_PRIORITY;
	alg->cra_blocksize = template->blocksize;
	alg->cra_alignmask = 0;
	alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_TYPE_AHASH;
	alg->cra_type = &crypto_ahash_type;

	t_alg->alg_type = template->alg_type;
	t_alg->alg_op = template->alg_op;

	return t_alg;
}

static int __init caam_algapi_hash_init(void)
{
1996 1997 1998
	struct device_node *dev_node;
	struct platform_device *pdev;
	struct device *ctrldev;
Y
Yuan Kang 已提交
1999
	int i = 0, err = 0;
2000 2001 2002
	struct caam_drv_private *priv;
	unsigned int md_limit = SHA512_DIGEST_SIZE;
	u32 cha_inst, cha_vid;
Y
Yuan Kang 已提交
2003

2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
	dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
	if (!dev_node) {
		dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
		if (!dev_node)
			return -ENODEV;
	}

	pdev = of_find_device_by_node(dev_node);
	if (!pdev) {
		of_node_put(dev_node);
		return -ENODEV;
	}

	ctrldev = &pdev->dev;
	priv = dev_get_drvdata(ctrldev);
	of_node_put(dev_node);

	/*
	 * If priv is NULL, it's probably because the caam driver wasn't
	 * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
	 */
	if (!priv)
		return -ENODEV;

2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
	/*
	 * Register crypto algorithms the device supports.  First, identify
	 * presence and attributes of MD block.
	 */
	cha_vid = rd_reg32(&priv->ctrl->perfmon.cha_id_ls);
	cha_inst = rd_reg32(&priv->ctrl->perfmon.cha_num_ls);

	/*
	 * Skip registration of any hashing algorithms if MD block
	 * is not present.
	 */
	if (!((cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT))
		return -ENODEV;

	/* Limit digest size based on LP256 */
	if ((cha_vid & CHA_ID_LS_MD_MASK) == CHA_ID_LS_MD_LP256)
		md_limit = SHA256_DIGEST_SIZE;

2046
	INIT_LIST_HEAD(&hash_list);
Y
Yuan Kang 已提交
2047 2048 2049 2050

	/* register crypto algorithms the device supports */
	for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
		struct caam_hash_alg *t_alg;
2051 2052 2053 2054 2055
		struct caam_hash_template *alg = driver_hash + i;

		/* If MD size is not supported by device, skip registration */
		if (alg->template_ahash.halg.digestsize > md_limit)
			continue;
Y
Yuan Kang 已提交
2056

Y
Yuan Kang 已提交
2057
		/* register hmac version */
2058
		t_alg = caam_hash_alloc(alg, true);
Y
Yuan Kang 已提交
2059 2060
		if (IS_ERR(t_alg)) {
			err = PTR_ERR(t_alg);
2061
			pr_warn("%s alg allocation failed\n", alg->driver_name);
Y
Yuan Kang 已提交
2062 2063 2064 2065 2066
			continue;
		}

		err = crypto_register_ahash(&t_alg->ahash_alg);
		if (err) {
2067 2068 2069
			pr_warn("%s alg registration failed: %d\n",
				t_alg->ahash_alg.halg.base.cra_driver_name,
				err);
Y
Yuan Kang 已提交
2070 2071
			kfree(t_alg);
		} else
2072
			list_add_tail(&t_alg->entry, &hash_list);
Y
Yuan Kang 已提交
2073 2074

		/* register unkeyed version */
2075
		t_alg = caam_hash_alloc(alg, false);
Y
Yuan Kang 已提交
2076 2077
		if (IS_ERR(t_alg)) {
			err = PTR_ERR(t_alg);
2078
			pr_warn("%s alg allocation failed\n", alg->driver_name);
Y
Yuan Kang 已提交
2079 2080 2081 2082 2083
			continue;
		}

		err = crypto_register_ahash(&t_alg->ahash_alg);
		if (err) {
2084 2085 2086
			pr_warn("%s alg registration failed: %d\n",
				t_alg->ahash_alg.halg.base.cra_driver_name,
				err);
Y
Yuan Kang 已提交
2087 2088
			kfree(t_alg);
		} else
2089
			list_add_tail(&t_alg->entry, &hash_list);
Y
Yuan Kang 已提交
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
	}

	return err;
}

module_init(caam_algapi_hash_init);
module_exit(caam_algapi_hash_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("FSL CAAM support for ahash functions of crypto API");
MODULE_AUTHOR("Freescale Semiconductor - NMG");