caamalg.c 91.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5
/*
 * caam - Freescale FSL CAAM support for crypto API
 *
 * Copyright 2008-2011 Freescale Semiconductor, Inc.
6
 * Copyright 2016-2018 NXP
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
 *
 * Based on talitos crypto API driver.
 *
 * relationship of job descriptors to shared descriptors (SteveC Dec 10 2008):
 *
 * ---------------                     ---------------
 * | JobDesc #1  |-------------------->|  ShareDesc  |
 * | *(packet 1) |                     |   (PDB)     |
 * ---------------      |------------->|  (hashKey)  |
 *       .              |              | (cipherKey) |
 *       .              |    |-------->| (operation) |
 * ---------------      |    |         ---------------
 * | JobDesc #2  |------|    |
 * | *(packet 2) |           |
 * ---------------           |
 *       .                   |
 *       .                   |
 * ---------------           |
 * | JobDesc #3  |------------
 * | *(packet 3) |
 * ---------------
 *
 * 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)   |
42
 * | (output length)   |
43 44
 * | SEQ_IN_PTR        |
 * | (input buffer)    |
45
 * | (input length)    |
46 47 48 49 50 51 52 53 54 55
 * ---------------------
 */

#include "compat.h"

#include "regs.h"
#include "intern.h"
#include "desc_constr.h"
#include "jr.h"
#include "error.h"
Y
Yuan Kang 已提交
56
#include "sg_sw_sec4.h"
Y
Yuan Kang 已提交
57
#include "key_gen.h"
58
#include "caamalg_desc.h"
59 60 61 62 63 64 65

/*
 * crypto alg
 */
#define CAAM_CRA_PRIORITY		3000
/* max key is sum of AES_MAX_KEY_SIZE, max split key size */
#define CAAM_MAX_KEY_SIZE		(AES_MAX_KEY_SIZE + \
66
					 CTR_RFC3686_NONCE_SIZE + \
67 68
					 SHA512_DIGEST_SIZE * 2)

69 70 71
#define AEAD_DESC_JOB_IO_LEN		(DESC_JOB_IO_LEN + CAAM_CMD_SZ * 2)
#define GCM_DESC_JOB_IO_LEN		(AEAD_DESC_JOB_IO_LEN + \
					 CAAM_CMD_SZ * 4)
72 73
#define AUTHENC_DESC_JOB_IO_LEN		(AEAD_DESC_JOB_IO_LEN + \
					 CAAM_CMD_SZ * 5)
74

75 76
#define CHACHAPOLY_DESC_JOB_IO_LEN	(AEAD_DESC_JOB_IO_LEN + CAAM_CMD_SZ * 6)

77 78
#define DESC_MAX_USED_BYTES		(CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN)
#define DESC_MAX_USED_LEN		(DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
79

80 81 82 83 84 85
#ifdef DEBUG
/* for print_hex_dumps with line references */
#define debug(format, arg...) printk(format, arg)
#else
#define debug(format, arg...)
#endif
C
Catalin Vasile 已提交
86

87 88 89 90 91 92 93 94 95 96 97 98 99
struct caam_alg_entry {
	int class1_alg_type;
	int class2_alg_type;
	bool rfc3686;
	bool geniv;
};

struct caam_aead_alg {
	struct aead_alg aead;
	struct caam_alg_entry caam;
	bool registered;
};

100 101 102 103 104 105
struct caam_skcipher_alg {
	struct skcipher_alg skcipher;
	struct caam_alg_entry caam;
	bool registered;
};

106 107 108 109
/*
 * per-session context
 */
struct caam_ctx {
110 111
	u32 sh_desc_enc[DESC_MAX_USED_LEN];
	u32 sh_desc_dec[DESC_MAX_USED_LEN];
112
	u8 key[CAAM_MAX_KEY_SIZE];
113 114
	dma_addr_t sh_desc_enc_dma;
	dma_addr_t sh_desc_dec_dma;
Y
Yuan Kang 已提交
115
	dma_addr_t key_dma;
116
	enum dma_data_direction dir;
117
	struct device *jrdev;
118 119
	struct alginfo adata;
	struct alginfo cdata;
120 121 122
	unsigned int authsize;
};

123 124 125 126
static int aead_null_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
127
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
128
	u32 *desc;
129 130
	int rem_bytes = CAAM_DESC_BYTES_MAX - AEAD_DESC_JOB_IO_LEN -
			ctx->adata.keylen_pad;
131 132 133 134 135

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
136
	if (rem_bytes >= DESC_AEAD_NULL_ENC_LEN) {
137
		ctx->adata.key_inline = true;
138
		ctx->adata.key_virt = ctx->key;
139 140
	} else {
		ctx->adata.key_inline = false;
141
		ctx->adata.key_dma = ctx->key_dma;
142
	}
143

144
	/* aead_encrypt shared descriptor */
145
	desc = ctx->sh_desc_enc;
146 147
	cnstr_shdsc_aead_null_encap(desc, &ctx->adata, ctx->authsize,
				    ctrlpriv->era);
148
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
149
				   desc_bytes(desc), ctx->dir);
150 151 152 153 154

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
155
	if (rem_bytes >= DESC_AEAD_NULL_DEC_LEN) {
156
		ctx->adata.key_inline = true;
157
		ctx->adata.key_virt = ctx->key;
158 159
	} else {
		ctx->adata.key_inline = false;
160
		ctx->adata.key_dma = ctx->key_dma;
161
	}
162

163
	/* aead_decrypt shared descriptor */
164
	desc = ctx->sh_desc_dec;
165 166
	cnstr_shdsc_aead_null_decap(desc, &ctx->adata, ctx->authsize,
				    ctrlpriv->era);
167
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
168
				   desc_bytes(desc), ctx->dir);
169 170 171 172

	return 0;
}

173 174
static int aead_set_sh_desc(struct crypto_aead *aead)
{
175 176
	struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
						 struct caam_aead_alg, aead);
177
	unsigned int ivsize = crypto_aead_ivsize(aead);
178 179
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
180
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
181
	u32 ctx1_iv_off = 0;
182
	u32 *desc, *nonce = NULL;
183 184
	u32 inl_mask;
	unsigned int data_len[2];
185
	const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
186
			       OP_ALG_AAI_CTR_MOD128);
187
	const bool is_rfc3686 = alg->caam.rfc3686;
188

189 190 191
	if (!ctx->authsize)
		return 0;

192
	/* NULL encryption / decryption */
193
	if (!ctx->cdata.keylen)
194 195
		return aead_null_set_sh_desc(aead);

196 197 198 199 200 201 202 203 204 205 206 207
	/*
	 * AES-CTR needs to load IV in CONTEXT1 reg
	 * at an offset of 128bits (16bytes)
	 * CONTEXT1[255:128] = IV
	 */
	if (ctr_mode)
		ctx1_iv_off = 16;

	/*
	 * RFC3686 specific:
	 *	CONTEXT1[255:128] = {NONCE, IV, COUNTER}
	 */
208
	if (is_rfc3686) {
209
		ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
210 211 212
		nonce = (u32 *)((void *)ctx->key + ctx->adata.keylen_pad +
				ctx->cdata.keylen - CTR_RFC3686_NONCE_SIZE);
	}
213

214 215 216
	data_len[0] = ctx->adata.keylen_pad;
	data_len[1] = ctx->cdata.keylen;

217 218 219
	if (alg->caam.geniv)
		goto skip_enc;

220 221 222 223
	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
224 225 226 227 228 229 230
	if (desc_inline_query(DESC_AEAD_ENC_LEN +
			      (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
			      AUTHENC_DESC_JOB_IO_LEN, data_len, &inl_mask,
			      ARRAY_SIZE(data_len)) < 0)
		return -EINVAL;

	if (inl_mask & 1)
231
		ctx->adata.key_virt = ctx->key;
232
	else
233
		ctx->adata.key_dma = ctx->key_dma;
234 235

	if (inl_mask & 2)
236
		ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
237
	else
238
		ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
239 240 241

	ctx->adata.key_inline = !!(inl_mask & 1);
	ctx->cdata.key_inline = !!(inl_mask & 2);
242

243
	/* aead_encrypt shared descriptor */
244
	desc = ctx->sh_desc_enc;
245 246
	cnstr_shdsc_aead_encap(desc, &ctx->cdata, &ctx->adata, ivsize,
			       ctx->authsize, is_rfc3686, nonce, ctx1_iv_off,
247
			       false, ctrlpriv->era);
248
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
249
				   desc_bytes(desc), ctx->dir);
250

251
skip_enc:
252 253 254 255
	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
256 257 258 259 260 261 262
	if (desc_inline_query(DESC_AEAD_DEC_LEN +
			      (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
			      AUTHENC_DESC_JOB_IO_LEN, data_len, &inl_mask,
			      ARRAY_SIZE(data_len)) < 0)
		return -EINVAL;

	if (inl_mask & 1)
263
		ctx->adata.key_virt = ctx->key;
264
	else
265
		ctx->adata.key_dma = ctx->key_dma;
266 267

	if (inl_mask & 2)
268
		ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
269
	else
270
		ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
271 272 273

	ctx->adata.key_inline = !!(inl_mask & 1);
	ctx->cdata.key_inline = !!(inl_mask & 2);
274

275
	/* aead_decrypt shared descriptor */
276
	desc = ctx->sh_desc_dec;
277 278
	cnstr_shdsc_aead_decap(desc, &ctx->cdata, &ctx->adata, ivsize,
			       ctx->authsize, alg->caam.geniv, is_rfc3686,
279
			       nonce, ctx1_iv_off, false, ctrlpriv->era);
280
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
281
				   desc_bytes(desc), ctx->dir);
282

283 284 285
	if (!alg->caam.geniv)
		goto skip_givenc;

286 287 288 289
	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
290 291 292 293 294 295 296
	if (desc_inline_query(DESC_AEAD_GIVENC_LEN +
			      (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
			      AUTHENC_DESC_JOB_IO_LEN, data_len, &inl_mask,
			      ARRAY_SIZE(data_len)) < 0)
		return -EINVAL;

	if (inl_mask & 1)
297
		ctx->adata.key_virt = ctx->key;
298
	else
299
		ctx->adata.key_dma = ctx->key_dma;
300 301

	if (inl_mask & 2)
302
		ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
303
	else
304
		ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
305 306 307

	ctx->adata.key_inline = !!(inl_mask & 1);
	ctx->cdata.key_inline = !!(inl_mask & 2);
308 309

	/* aead_givencrypt shared descriptor */
310
	desc = ctx->sh_desc_enc;
311 312
	cnstr_shdsc_aead_givencap(desc, &ctx->cdata, &ctx->adata, ivsize,
				  ctx->authsize, is_rfc3686, nonce,
313
				  ctx1_iv_off, false, ctrlpriv->era);
314
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
315
				   desc_bytes(desc), ctx->dir);
316

317
skip_givenc:
318 319 320
	return 0;
}

Y
Yuan Kang 已提交
321
static int aead_setauthsize(struct crypto_aead *authenc,
322 323 324 325 326
				    unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);

	ctx->authsize = authsize;
327
	aead_set_sh_desc(authenc);
328 329 330 331

	return 0;
}

332 333 334 335
static int gcm_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
336
	unsigned int ivsize = crypto_aead_ivsize(aead);
337
	u32 *desc;
338 339
	int rem_bytes = CAAM_DESC_BYTES_MAX - GCM_DESC_JOB_IO_LEN -
			ctx->cdata.keylen;
340

341
	if (!ctx->cdata.keylen || !ctx->authsize)
342 343 344 345 346 347 348
		return 0;

	/*
	 * AES GCM encrypt shared descriptor
	 * Job Descriptor and Shared Descriptor
	 * must fit into the 64-word Descriptor h/w Buffer
	 */
349
	if (rem_bytes >= DESC_GCM_ENC_LEN) {
350
		ctx->cdata.key_inline = true;
351
		ctx->cdata.key_virt = ctx->key;
352 353
	} else {
		ctx->cdata.key_inline = false;
354
		ctx->cdata.key_dma = ctx->key_dma;
355
	}
356 357

	desc = ctx->sh_desc_enc;
358
	cnstr_shdsc_gcm_encap(desc, &ctx->cdata, ivsize, ctx->authsize, false);
359
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
360
				   desc_bytes(desc), ctx->dir);
361 362 363 364 365

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
366
	if (rem_bytes >= DESC_GCM_DEC_LEN) {
367
		ctx->cdata.key_inline = true;
368
		ctx->cdata.key_virt = ctx->key;
369 370
	} else {
		ctx->cdata.key_inline = false;
371
		ctx->cdata.key_dma = ctx->key_dma;
372
	}
373 374

	desc = ctx->sh_desc_dec;
375
	cnstr_shdsc_gcm_decap(desc, &ctx->cdata, ivsize, ctx->authsize, false);
376
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
377
				   desc_bytes(desc), ctx->dir);
378 379 380 381 382 383 384 385 386 387 388 389 390 391

	return 0;
}

static int gcm_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);

	ctx->authsize = authsize;
	gcm_set_sh_desc(authenc);

	return 0;
}

392 393 394 395
static int rfc4106_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
396
	unsigned int ivsize = crypto_aead_ivsize(aead);
397
	u32 *desc;
398 399
	int rem_bytes = CAAM_DESC_BYTES_MAX - GCM_DESC_JOB_IO_LEN -
			ctx->cdata.keylen;
400

401
	if (!ctx->cdata.keylen || !ctx->authsize)
402 403 404 405 406 407 408
		return 0;

	/*
	 * RFC4106 encrypt shared descriptor
	 * Job Descriptor and Shared Descriptor
	 * must fit into the 64-word Descriptor h/w Buffer
	 */
409
	if (rem_bytes >= DESC_RFC4106_ENC_LEN) {
410
		ctx->cdata.key_inline = true;
411
		ctx->cdata.key_virt = ctx->key;
412 413
	} else {
		ctx->cdata.key_inline = false;
414
		ctx->cdata.key_dma = ctx->key_dma;
415
	}
416 417

	desc = ctx->sh_desc_enc;
418 419
	cnstr_shdsc_rfc4106_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
420
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
421
				   desc_bytes(desc), ctx->dir);
422 423 424 425 426

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
427
	if (rem_bytes >= DESC_RFC4106_DEC_LEN) {
428
		ctx->cdata.key_inline = true;
429
		ctx->cdata.key_virt = ctx->key;
430 431
	} else {
		ctx->cdata.key_inline = false;
432
		ctx->cdata.key_dma = ctx->key_dma;
433
	}
434 435

	desc = ctx->sh_desc_dec;
436 437
	cnstr_shdsc_rfc4106_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
438
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
439
				   desc_bytes(desc), ctx->dir);
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

	return 0;
}

static int rfc4106_setauthsize(struct crypto_aead *authenc,
			       unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);

	ctx->authsize = authsize;
	rfc4106_set_sh_desc(authenc);

	return 0;
}

455 456 457 458
static int rfc4543_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
459
	unsigned int ivsize = crypto_aead_ivsize(aead);
460
	u32 *desc;
461 462
	int rem_bytes = CAAM_DESC_BYTES_MAX - GCM_DESC_JOB_IO_LEN -
			ctx->cdata.keylen;
463

464
	if (!ctx->cdata.keylen || !ctx->authsize)
465 466 467 468 469 470 471
		return 0;

	/*
	 * RFC4543 encrypt shared descriptor
	 * Job Descriptor and Shared Descriptor
	 * must fit into the 64-word Descriptor h/w Buffer
	 */
472
	if (rem_bytes >= DESC_RFC4543_ENC_LEN) {
473
		ctx->cdata.key_inline = true;
474
		ctx->cdata.key_virt = ctx->key;
475 476
	} else {
		ctx->cdata.key_inline = false;
477
		ctx->cdata.key_dma = ctx->key_dma;
478
	}
479 480

	desc = ctx->sh_desc_enc;
481 482
	cnstr_shdsc_rfc4543_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
483
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
484
				   desc_bytes(desc), ctx->dir);
485 486 487 488 489

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
490
	if (rem_bytes >= DESC_RFC4543_DEC_LEN) {
491
		ctx->cdata.key_inline = true;
492
		ctx->cdata.key_virt = ctx->key;
493 494
	} else {
		ctx->cdata.key_inline = false;
495
		ctx->cdata.key_dma = ctx->key_dma;
496
	}
497 498

	desc = ctx->sh_desc_dec;
499 500
	cnstr_shdsc_rfc4543_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
501
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
502
				   desc_bytes(desc), ctx->dir);
503

504 505
	return 0;
}
506

507 508 509 510
static int rfc4543_setauthsize(struct crypto_aead *authenc,
			       unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);
511

512 513
	ctx->authsize = authsize;
	rfc4543_set_sh_desc(authenc);
514

515 516
	return 0;
}
517

518 519 520 521 522 523 524 525 526 527 528 529
static int chachapoly_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	unsigned int ivsize = crypto_aead_ivsize(aead);
	u32 *desc;

	if (!ctx->cdata.keylen || !ctx->authsize)
		return 0;

	desc = ctx->sh_desc_enc;
	cnstr_shdsc_chachapoly(desc, &ctx->cdata, &ctx->adata, ivsize,
530
			       ctx->authsize, true, false);
531 532 533 534 535
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
				   desc_bytes(desc), ctx->dir);

	desc = ctx->sh_desc_dec;
	cnstr_shdsc_chachapoly(desc, &ctx->cdata, &ctx->adata, ivsize,
536
			       ctx->authsize, false, false);
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
				   desc_bytes(desc), ctx->dir);

	return 0;
}

static int chachapoly_setauthsize(struct crypto_aead *aead,
				  unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);

	if (authsize != POLY1305_DIGEST_SIZE)
		return -EINVAL;

	ctx->authsize = authsize;
	return chachapoly_set_sh_desc(aead);
}

static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
			     unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	unsigned int ivsize = crypto_aead_ivsize(aead);
	unsigned int saltlen = CHACHAPOLY_IV_SIZE - ivsize;

	if (keylen != CHACHA20_KEY_SIZE + saltlen) {
		crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
		return -EINVAL;
	}

	ctx->cdata.key_virt = key;
	ctx->cdata.keylen = keylen - saltlen;

	return chachapoly_set_sh_desc(aead);
}

Y
Yuan Kang 已提交
573
static int aead_setkey(struct crypto_aead *aead,
574 575 576 577
			       const u8 *key, unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
578
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
579
	struct crypto_authenc_keys keys;
580 581
	int ret = 0;

582
	if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
583 584 585 586
		goto badkey;

#ifdef DEBUG
	printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n",
587 588
	       keys.authkeylen + keys.enckeylen, keys.enckeylen,
	       keys.authkeylen);
589
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
590 591 592
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	/*
	 * If DKP is supported, use it in the shared descriptor to generate
	 * the split key.
	 */
	if (ctrlpriv->era >= 6) {
		ctx->adata.keylen = keys.authkeylen;
		ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
						      OP_ALG_ALGSEL_MASK);

		if (ctx->adata.keylen_pad + keys.enckeylen > CAAM_MAX_KEY_SIZE)
			goto badkey;

		memcpy(ctx->key, keys.authkey, keys.authkeylen);
		memcpy(ctx->key + ctx->adata.keylen_pad, keys.enckey,
		       keys.enckeylen);
		dma_sync_single_for_device(jrdev, ctx->key_dma,
					   ctx->adata.keylen_pad +
					   keys.enckeylen, ctx->dir);
		goto skip_split_key;
	}

614 615 616
	ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, keys.authkey,
			    keys.authkeylen, CAAM_MAX_KEY_SIZE -
			    keys.enckeylen);
617 618 619 620 621
	if (ret) {
		goto badkey;
	}

	/* postpend encryption key to auth split key */
622
	memcpy(ctx->key + ctx->adata.keylen_pad, keys.enckey, keys.enckeylen);
623
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->adata.keylen_pad +
624
				   keys.enckeylen, ctx->dir);
625
#ifdef DEBUG
626
	print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
627
		       DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
628
		       ctx->adata.keylen_pad + keys.enckeylen, 1);
629
#endif
630 631

skip_split_key:
632
	ctx->cdata.keylen = keys.enckeylen;
633
	memzero_explicit(&keys, sizeof(keys));
634
	return aead_set_sh_desc(aead);
635 636
badkey:
	crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
637
	memzero_explicit(&keys, sizeof(keys));
638 639 640
	return -EINVAL;
}

641 642 643 644 645 646 647 648 649 650 651 652
static int gcm_setkey(struct crypto_aead *aead,
		      const u8 *key, unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;

#ifdef DEBUG
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif

	memcpy(ctx->key, key, keylen);
653
	dma_sync_single_for_device(jrdev, ctx->key_dma, keylen, ctx->dir);
654
	ctx->cdata.keylen = keylen;
655

656
	return gcm_set_sh_desc(aead);
657 658
}

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
static int rfc4106_setkey(struct crypto_aead *aead,
			  const u8 *key, unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;

	if (keylen < 4)
		return -EINVAL;

#ifdef DEBUG
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif

	memcpy(ctx->key, key, keylen);

	/*
	 * The last four bytes of the key material are used as the salt value
	 * in the nonce. Update the AES key length.
	 */
679
	ctx->cdata.keylen = keylen - 4;
680
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->cdata.keylen,
681
				   ctx->dir);
682
	return rfc4106_set_sh_desc(aead);
683 684
}

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
static int rfc4543_setkey(struct crypto_aead *aead,
			  const u8 *key, unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;

	if (keylen < 4)
		return -EINVAL;

#ifdef DEBUG
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif

	memcpy(ctx->key, key, keylen);

	/*
	 * The last four bytes of the key material are used as the salt value
	 * in the nonce. Update the AES key length.
	 */
705
	ctx->cdata.keylen = keylen - 4;
706
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->cdata.keylen,
707
				   ctx->dir);
708
	return rfc4543_set_sh_desc(aead);
709 710
}

711 712
static int skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
			   unsigned int keylen)
Y
Yuan Kang 已提交
713
{
714 715 716 717
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
	struct caam_skcipher_alg *alg =
		container_of(crypto_skcipher_alg(skcipher), typeof(*alg),
			     skcipher);
Y
Yuan Kang 已提交
718
	struct device *jrdev = ctx->jrdev;
719
	unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
720
	u32 *desc;
721
	u32 ctx1_iv_off = 0;
722
	const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
723
			       OP_ALG_AAI_CTR_MOD128);
724
	const bool is_rfc3686 = alg->caam.rfc3686;
Y
Yuan Kang 已提交
725 726

#ifdef DEBUG
727
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
728 729
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif
730 731 732 733 734 735 736
	/*
	 * AES-CTR needs to load IV in CONTEXT1 reg
	 * at an offset of 128bits (16bytes)
	 * CONTEXT1[255:128] = IV
	 */
	if (ctr_mode)
		ctx1_iv_off = 16;
Y
Yuan Kang 已提交
737

738 739 740 741 742 743 744 745 746 747
	/*
	 * RFC3686 specific:
	 *	| CONTEXT1[255:128] = {NONCE, IV, COUNTER}
	 *	| *key = {KEY, NONCE}
	 */
	if (is_rfc3686) {
		ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
		keylen -= CTR_RFC3686_NONCE_SIZE;
	}

748
	ctx->cdata.keylen = keylen;
749
	ctx->cdata.key_virt = key;
750
	ctx->cdata.key_inline = true;
Y
Yuan Kang 已提交
751

752
	/* skcipher_encrypt shared descriptor */
Y
Yuan Kang 已提交
753
	desc = ctx->sh_desc_enc;
754 755
	cnstr_shdsc_skcipher_encap(desc, &ctx->cdata, ivsize, is_rfc3686,
				   ctx1_iv_off);
756
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
757
				   desc_bytes(desc), ctx->dir);
758

759
	/* skcipher_decrypt shared descriptor */
Y
Yuan Kang 已提交
760
	desc = ctx->sh_desc_dec;
761 762
	cnstr_shdsc_skcipher_decap(desc, &ctx->cdata, ivsize, is_rfc3686,
				   ctx1_iv_off);
763
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
764
				   desc_bytes(desc), ctx->dir);
Y
Yuan Kang 已提交
765

766
	return 0;
Y
Yuan Kang 已提交
767 768
}

769 770
static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
			       unsigned int keylen)
771
{
772
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
773
	struct device *jrdev = ctx->jrdev;
774
	u32 *desc;
775 776

	if (keylen != 2 * AES_MIN_KEY_SIZE  && keylen != 2 * AES_MAX_KEY_SIZE) {
777
		crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
778 779 780 781
		dev_err(jrdev, "key size mismatch\n");
		return -EINVAL;
	}

782
	ctx->cdata.keylen = keylen;
783
	ctx->cdata.key_virt = key;
784
	ctx->cdata.key_inline = true;
785

786
	/* xts_skcipher_encrypt shared descriptor */
787
	desc = ctx->sh_desc_enc;
788
	cnstr_shdsc_xts_skcipher_encap(desc, &ctx->cdata);
789
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
790
				   desc_bytes(desc), ctx->dir);
791

792
	/* xts_skcipher_decrypt shared descriptor */
793
	desc = ctx->sh_desc_dec;
794
	cnstr_shdsc_xts_skcipher_decap(desc, &ctx->cdata);
795
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
796
				   desc_bytes(desc), ctx->dir);
797 798 799 800

	return 0;
}

801
/*
802
 * aead_edesc - s/w-extended aead descriptor
803 804
 * @src_nents: number of segments in input s/w scatterlist
 * @dst_nents: number of segments in output s/w scatterlist
Y
Yuan Kang 已提交
805 806
 * @sec4_sg_bytes: length of dma mapped sec4_sg space
 * @sec4_sg_dma: bus physical mapped address of h/w link table
807
 * @sec4_sg: pointer to h/w link table
808 809
 * @hw_desc: the h/w job descriptor followed by any referenced link tables
 */
Y
Yuan Kang 已提交
810
struct aead_edesc {
811 812
	int src_nents;
	int dst_nents;
Y
Yuan Kang 已提交
813 814 815
	int sec4_sg_bytes;
	dma_addr_t sec4_sg_dma;
	struct sec4_sg_entry *sec4_sg;
816
	u32 hw_desc[];
817 818
};

Y
Yuan Kang 已提交
819
/*
820
 * skcipher_edesc - s/w-extended skcipher descriptor
821 822
 * @src_nents: number of segments in input s/w scatterlist
 * @dst_nents: number of segments in output s/w scatterlist
Y
Yuan Kang 已提交
823
 * @iv_dma: dma address of iv for checking continuity and link table
Y
Yuan Kang 已提交
824 825
 * @sec4_sg_bytes: length of dma mapped sec4_sg space
 * @sec4_sg_dma: bus physical mapped address of h/w link table
826
 * @sec4_sg: pointer to h/w link table
Y
Yuan Kang 已提交
827
 * @hw_desc: the h/w job descriptor followed by any referenced link tables
828
 *	     and IV
Y
Yuan Kang 已提交
829
 */
830
struct skcipher_edesc {
Y
Yuan Kang 已提交
831 832 833
	int src_nents;
	int dst_nents;
	dma_addr_t iv_dma;
Y
Yuan Kang 已提交
834 835 836
	int sec4_sg_bytes;
	dma_addr_t sec4_sg_dma;
	struct sec4_sg_entry *sec4_sg;
Y
Yuan Kang 已提交
837 838 839
	u32 hw_desc[0];
};

840
static void caam_unmap(struct device *dev, struct scatterlist *src,
Y
Yuan Kang 已提交
841
		       struct scatterlist *dst, int src_nents,
842
		       int dst_nents,
843
		       dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
Y
Yuan Kang 已提交
844
		       int sec4_sg_bytes)
845
{
Y
Yuan Kang 已提交
846
	if (dst != src) {
847 848 849
		if (src_nents)
			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
		dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
850
	} else {
851
		dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
852 853
	}

854
	if (iv_dma)
855
		dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
856 857
	if (sec4_sg_bytes)
		dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
858 859 860
				 DMA_TO_DEVICE);
}

861 862 863
static void aead_unmap(struct device *dev,
		       struct aead_edesc *edesc,
		       struct aead_request *req)
864 865
{
	caam_unmap(dev, req->src, req->dst,
866
		   edesc->src_nents, edesc->dst_nents, 0, 0,
867 868 869
		   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
}

870 871
static void skcipher_unmap(struct device *dev, struct skcipher_edesc *edesc,
			   struct skcipher_request *req)
Y
Yuan Kang 已提交
872
{
873 874
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
875 876

	caam_unmap(dev, req->src, req->dst,
877
		   edesc->src_nents, edesc->dst_nents,
878
		   edesc->iv_dma, ivsize,
Y
Yuan Kang 已提交
879
		   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
Y
Yuan Kang 已提交
880 881
}

Y
Yuan Kang 已提交
882
static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
883 884
				   void *context)
{
Y
Yuan Kang 已提交
885 886
	struct aead_request *req = context;
	struct aead_edesc *edesc;
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

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

	edesc = container_of(desc, struct aead_edesc, hw_desc[0]);

	if (err)
		caam_jr_strstatus(jrdev, err);

	aead_unmap(jrdev, edesc, req);

	kfree(edesc);

	aead_request_complete(req, err);
}

Y
Yuan Kang 已提交
904
static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
905 906
				   void *context)
{
Y
Yuan Kang 已提交
907 908
	struct aead_request *req = context;
	struct aead_edesc *edesc;
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

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

	edesc = container_of(desc, struct aead_edesc, hw_desc[0]);

	if (err)
		caam_jr_strstatus(jrdev, err);

	aead_unmap(jrdev, edesc, req);

	/*
	 * verify hw auth check passed else return -EBADMSG
	 */
	if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK)
		err = -EBADMSG;

	kfree(edesc);

	aead_request_complete(req, err);
}

932 933
static void skcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
				  void *context)
Y
Yuan Kang 已提交
934
{
935 936 937 938
	struct skcipher_request *req = context;
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
939

940
#ifdef DEBUG
Y
Yuan Kang 已提交
941 942 943
	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
#endif

944
	edesc = container_of(desc, struct skcipher_edesc, hw_desc[0]);
Y
Yuan Kang 已提交
945

946 947
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
948 949

#ifdef DEBUG
950
	print_hex_dump(KERN_ERR, "dstiv  @"__stringify(__LINE__)": ",
951
		       DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
Y
Yuan Kang 已提交
952 953
		       edesc->src_nents > 1 ? 100 : ivsize, 1);
#endif
954 955
	caam_dump_sg(KERN_ERR, "dst    @" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
956
		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
Y
Yuan Kang 已提交
957

958
	skcipher_unmap(jrdev, edesc, req);
959 960

	/*
961
	 * The crypto API expects us to set the IV (req->iv) to the last
962 963
	 * ciphertext block. This is used e.g. by the CTS mode.
	 */
964
	scatterwalk_map_and_copy(req->iv, req->dst, req->cryptlen - ivsize,
965 966
				 ivsize, 0);

Y
Yuan Kang 已提交
967 968
	kfree(edesc);

969
	skcipher_request_complete(req, err);
Y
Yuan Kang 已提交
970 971
}

972 973
static void skcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
				  void *context)
Y
Yuan Kang 已提交
974
{
975 976
	struct skcipher_request *req = context;
	struct skcipher_edesc *edesc;
977
#ifdef DEBUG
978 979
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
980 981 982 983

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

984
	edesc = container_of(desc, struct skcipher_edesc, hw_desc[0]);
985 986
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
987 988

#ifdef DEBUG
989
	print_hex_dump(KERN_ERR, "dstiv  @"__stringify(__LINE__)": ",
990
		       DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
Y
Yuan Kang 已提交
991
#endif
992 993
	caam_dump_sg(KERN_ERR, "dst    @" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
994
		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
Y
Yuan Kang 已提交
995

996
	skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
997 998
	kfree(edesc);

999
	skcipher_request_complete(req, err);
Y
Yuan Kang 已提交
1000 1001
}

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
/*
 * Fill in aead job descriptor
 */
static void init_aead_job(struct aead_request *req,
			  struct aead_edesc *edesc,
			  bool all_contig, bool encrypt)
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	int authsize = ctx->authsize;
	u32 *desc = edesc->hw_desc;
	u32 out_options, in_options;
	dma_addr_t dst_dma, src_dma;
	int len, sec4_sg_index = 0;
	dma_addr_t ptr;
	u32 *sh_desc;

	sh_desc = encrypt ? ctx->sh_desc_enc : ctx->sh_desc_dec;
	ptr = encrypt ? ctx->sh_desc_enc_dma : ctx->sh_desc_dec_dma;

	len = desc_len(sh_desc);
	init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);

	if (all_contig) {
1026
		src_dma = edesc->src_nents ? sg_dma_address(req->src) : 0;
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
		in_options = 0;
	} else {
		src_dma = edesc->sec4_sg_dma;
		sec4_sg_index += edesc->src_nents;
		in_options = LDST_SGF;
	}

	append_seq_in_ptr(desc, src_dma, req->assoclen + req->cryptlen,
			  in_options);

	dst_dma = src_dma;
	out_options = in_options;

	if (unlikely(req->src != req->dst)) {
1041
		if (edesc->dst_nents == 1) {
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
			dst_dma = sg_dma_address(req->dst);
		} else {
			dst_dma = edesc->sec4_sg_dma +
				  sec4_sg_index *
				  sizeof(struct sec4_sg_entry);
			out_options = LDST_SGF;
		}
	}

	if (encrypt)
		append_seq_out_ptr(desc, dst_dma,
				   req->assoclen + req->cryptlen + authsize,
				   out_options);
	else
		append_seq_out_ptr(desc, dst_dma,
				   req->assoclen + req->cryptlen - authsize,
				   out_options);
}

static void init_gcm_job(struct aead_request *req,
			 struct aead_edesc *edesc,
			 bool all_contig, bool encrypt)
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	unsigned int ivsize = crypto_aead_ivsize(aead);
	u32 *desc = edesc->hw_desc;
1069
	bool generic_gcm = (ivsize == GCM_AES_IV_SIZE);
1070 1071 1072
	unsigned int last;

	init_aead_job(req, edesc, all_contig, encrypt);
1073
	append_math_add_imm_u32(desc, REG3, ZERO, IMM, req->assoclen);
1074 1075 1076 1077 1078 1079 1080 1081

	/* BUG This should not be specific to generic GCM. */
	last = 0;
	if (encrypt && generic_gcm && !(req->assoclen + req->cryptlen))
		last = FIFOLD_TYPE_LAST1;

	/* Read GCM IV */
	append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | IMMEDIATE |
1082
			 FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 | GCM_AES_IV_SIZE | last);
1083 1084
	/* Append Salt */
	if (!generic_gcm)
1085
		append_data(desc, ctx->key + ctx->cdata.keylen, 4);
1086 1087 1088 1089 1090
	/* Append IV */
	append_data(desc, req->iv, ivsize);
	/* End of blank commands */
}

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
static void init_chachapoly_job(struct aead_request *req,
				struct aead_edesc *edesc, bool all_contig,
				bool encrypt)
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	unsigned int ivsize = crypto_aead_ivsize(aead);
	unsigned int assoclen = req->assoclen;
	u32 *desc = edesc->hw_desc;
	u32 ctx_iv_off = 4;

	init_aead_job(req, edesc, all_contig, encrypt);

	if (ivsize != CHACHAPOLY_IV_SIZE) {
		/* IPsec specific: CONTEXT1[223:128] = {NONCE, IV} */
		ctx_iv_off += 4;

		/*
		 * The associated data comes already with the IV but we need
		 * to skip it when we authenticate or encrypt...
		 */
		assoclen -= ivsize;
	}

	append_math_add_imm_u32(desc, REG3, ZERO, IMM, assoclen);

	/*
	 * For IPsec load the IV further in the same register.
	 * For RFC7539 simply load the 12 bytes nonce in a single operation
	 */
	append_load_as_imm(desc, req->iv, ivsize, LDST_CLASS_1_CCB |
			   LDST_SRCDST_BYTE_CONTEXT |
			   ctx_iv_off << LDST_OFFSET_SHIFT);
}

1125 1126 1127
static void init_authenc_job(struct aead_request *req,
			     struct aead_edesc *edesc,
			     bool all_contig, bool encrypt)
1128 1129
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1130 1131 1132
	struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
						 struct caam_aead_alg, aead);
	unsigned int ivsize = crypto_aead_ivsize(aead);
1133
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
1134
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
1135
	const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
1136 1137
			       OP_ALG_AAI_CTR_MOD128);
	const bool is_rfc3686 = alg->caam.rfc3686;
1138
	u32 *desc = edesc->hw_desc;
1139
	u32 ivoffset = 0;
1140

1141 1142 1143 1144 1145 1146 1147
	/*
	 * AES-CTR needs to load IV in CONTEXT1 reg
	 * at an offset of 128bits (16bytes)
	 * CONTEXT1[255:128] = IV
	 */
	if (ctr_mode)
		ivoffset = 16;
1148

1149 1150 1151 1152 1153 1154
	/*
	 * RFC3686 specific:
	 *	CONTEXT1[255:128] = {NONCE, IV, COUNTER}
	 */
	if (is_rfc3686)
		ivoffset = 16 + CTR_RFC3686_NONCE_SIZE;
1155

1156
	init_aead_job(req, edesc, all_contig, encrypt);
1157

1158 1159 1160 1161 1162 1163 1164 1165 1166
	/*
	 * {REG3, DPOVRD} = assoclen, depending on whether MATH command supports
	 * having DPOVRD as destination.
	 */
	if (ctrlpriv->era < 3)
		append_math_add_imm_u32(desc, REG3, ZERO, IMM, req->assoclen);
	else
		append_math_add_imm_u32(desc, DPOVRD, ZERO, IMM, req->assoclen);

1167
	if (ivsize && ((is_rfc3686 && encrypt) || !alg->caam.geniv))
1168 1169 1170 1171
		append_load_as_imm(desc, req->iv, ivsize,
				   LDST_CLASS_1_CCB |
				   LDST_SRCDST_BYTE_CONTEXT |
				   (ivoffset << LDST_OFFSET_SHIFT));
1172 1173
}

Y
Yuan Kang 已提交
1174
/*
1175
 * Fill in skcipher job descriptor
Y
Yuan Kang 已提交
1176
 */
1177 1178 1179
static void init_skcipher_job(struct skcipher_request *req,
			      struct skcipher_edesc *edesc,
			      const bool encrypt)
Y
Yuan Kang 已提交
1180
{
1181 1182 1183
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
1184
	u32 *desc = edesc->hw_desc;
1185
	u32 *sh_desc;
1186
	u32 out_options = 0;
1187
	dma_addr_t dst_dma, ptr;
1188
	int len;
Y
Yuan Kang 已提交
1189 1190

#ifdef DEBUG
1191
	print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
1192 1193 1194
		       DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
	pr_err("asked=%d, cryptlen%d\n",
	       (int)edesc->src_nents > 1 ? 100 : req->cryptlen, req->cryptlen);
Y
Yuan Kang 已提交
1195
#endif
1196 1197
	caam_dump_sg(KERN_ERR, "src    @" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->src,
1198 1199 1200 1201
		     edesc->src_nents > 1 ? 100 : req->cryptlen, 1);

	sh_desc = encrypt ? ctx->sh_desc_enc : ctx->sh_desc_dec;
	ptr = encrypt ? ctx->sh_desc_enc_dma : ctx->sh_desc_dec_dma;
Y
Yuan Kang 已提交
1202 1203 1204 1205

	len = desc_len(sh_desc);
	init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);

1206
	append_seq_in_ptr(desc, edesc->sec4_sg_dma, req->cryptlen + ivsize,
1207
			  LDST_SGF);
Y
Yuan Kang 已提交
1208 1209

	if (likely(req->src == req->dst)) {
1210 1211
		dst_dma = edesc->sec4_sg_dma + sizeof(struct sec4_sg_entry);
		out_options = LDST_SGF;
Y
Yuan Kang 已提交
1212
	} else {
1213
		if (edesc->dst_nents == 1) {
Y
Yuan Kang 已提交
1214 1215
			dst_dma = sg_dma_address(req->dst);
		} else {
1216 1217
			dst_dma = edesc->sec4_sg_dma + (edesc->src_nents + 1) *
				  sizeof(struct sec4_sg_entry);
Y
Yuan Kang 已提交
1218 1219 1220
			out_options = LDST_SGF;
		}
	}
1221
	append_seq_out_ptr(desc, dst_dma, req->cryptlen, out_options);
Y
Yuan Kang 已提交
1222 1223
}

1224
/*
1225
 * allocate and map the aead extended descriptor
1226
 */
1227 1228 1229
static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
					   int desc_bytes, bool *all_contig_ptr,
					   bool encrypt)
1230
{
Y
Yuan Kang 已提交
1231
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1232 1233
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1234 1235
	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
		       GFP_KERNEL : GFP_ATOMIC;
1236
	int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
Y
Yuan Kang 已提交
1237
	struct aead_edesc *edesc;
1238
	int sec4_sg_index, sec4_sg_len, sec4_sg_bytes;
1239
	unsigned int authsize = ctx->authsize;
1240

1241
	if (unlikely(req->dst != req->src)) {
1242 1243
		src_nents = sg_nents_for_len(req->src, req->assoclen +
					     req->cryptlen);
1244 1245 1246 1247 1248 1249
		if (unlikely(src_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
				req->assoclen + req->cryptlen);
			return ERR_PTR(src_nents);
		}

1250 1251 1252 1253
		dst_nents = sg_nents_for_len(req->dst, req->assoclen +
					     req->cryptlen +
						(encrypt ? authsize :
							   (-authsize)));
1254 1255 1256 1257 1258 1259
		if (unlikely(dst_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in dst S/G\n",
				req->assoclen + req->cryptlen +
				(encrypt ? authsize : (-authsize)));
			return ERR_PTR(dst_nents);
		}
1260
	} else {
1261 1262 1263
		src_nents = sg_nents_for_len(req->src, req->assoclen +
					     req->cryptlen +
					     (encrypt ? authsize : 0));
1264 1265 1266 1267 1268 1269
		if (unlikely(src_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
				req->assoclen + req->cryptlen +
				(encrypt ? authsize : 0));
			return ERR_PTR(src_nents);
		}
1270
	}
1271

1272
	if (likely(req->src == req->dst)) {
1273 1274 1275
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_BIDIRECTIONAL);
		if (unlikely(!mapped_src_nents)) {
1276 1277 1278 1279
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
	} else {
1280 1281
		/* Cover also the case of null (zero length) input data */
		if (src_nents) {
1282 1283 1284
			mapped_src_nents = dma_map_sg(jrdev, req->src,
						      src_nents, DMA_TO_DEVICE);
			if (unlikely(!mapped_src_nents)) {
1285 1286 1287
				dev_err(jrdev, "unable to map source\n");
				return ERR_PTR(-ENOMEM);
			}
1288 1289
		} else {
			mapped_src_nents = 0;
1290 1291
		}

1292 1293 1294
		mapped_dst_nents = dma_map_sg(jrdev, req->dst, dst_nents,
					      DMA_FROM_DEVICE);
		if (unlikely(!mapped_dst_nents)) {
1295
			dev_err(jrdev, "unable to map destination\n");
1296
			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1297 1298 1299 1300
			return ERR_PTR(-ENOMEM);
		}
	}

1301 1302 1303 1304 1305 1306 1307 1308 1309
	sec4_sg_len = mapped_src_nents > 1 ? mapped_src_nents : 0;
	sec4_sg_len += mapped_dst_nents > 1 ? mapped_dst_nents : 0;
	sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);

	/* allocate space for base edesc and hw desc commands, link tables */
	edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes,
			GFP_DMA | flags);
	if (!edesc) {
		caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
1310
			   0, 0, 0);
1311 1312 1313
		return ERR_PTR(-ENOMEM);
	}

1314 1315
	edesc->src_nents = src_nents;
	edesc->dst_nents = dst_nents;
Y
Yuan Kang 已提交
1316 1317
	edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
			 desc_bytes;
1318
	*all_contig_ptr = !(mapped_src_nents > 1);
1319

Y
Yuan Kang 已提交
1320
	sec4_sg_index = 0;
1321 1322 1323 1324
	if (mapped_src_nents > 1) {
		sg_to_sec4_sg_last(req->src, mapped_src_nents,
				   edesc->sec4_sg + sec4_sg_index, 0);
		sec4_sg_index += mapped_src_nents;
1325
	}
1326 1327
	if (mapped_dst_nents > 1) {
		sg_to_sec4_sg_last(req->dst, mapped_dst_nents,
Y
Yuan Kang 已提交
1328
				   edesc->sec4_sg + sec4_sg_index, 0);
1329
	}
1330 1331 1332 1333

	if (!sec4_sg_bytes)
		return edesc;

1334 1335
	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
					    sec4_sg_bytes, DMA_TO_DEVICE);
1336 1337
	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
		dev_err(jrdev, "unable to map S/G table\n");
1338 1339
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
1340 1341
		return ERR_PTR(-ENOMEM);
	}
1342

1343 1344
	edesc->sec4_sg_bytes = sec4_sg_bytes;

1345 1346 1347
	return edesc;
}

1348
static int gcm_encrypt(struct aead_request *req)
1349
{
Y
Yuan Kang 已提交
1350 1351
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1352 1353
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1354
	bool all_contig;
1355
	u32 *desc;
1356 1357
	int ret = 0;

1358
	/* allocate extended descriptor */
1359
	edesc = aead_edesc_alloc(req, GCM_DESC_JOB_IO_LEN, &all_contig, true);
1360 1361 1362
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1363
	/* Create and submit job descriptor */
1364
	init_gcm_job(req, edesc, all_contig, true);
1365
#ifdef DEBUG
1366
	print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
1367 1368 1369
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif
1370

1371 1372 1373 1374 1375 1376 1377 1378
	desc = edesc->hw_desc;
	ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
	}
1379

1380
	return ret;
1381 1382
}

1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
static int chachapoly_encrypt(struct aead_request *req)
{
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	bool all_contig;
	u32 *desc;
	int ret;

	edesc = aead_edesc_alloc(req, CHACHAPOLY_DESC_JOB_IO_LEN, &all_contig,
				 true);
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	desc = edesc->hw_desc;

	init_chachapoly_job(req, edesc, all_contig, true);
	print_hex_dump_debug("chachapoly jobdesc@" __stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
			     1);

	ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
	}

	return ret;
}

static int chachapoly_decrypt(struct aead_request *req)
{
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	bool all_contig;
	u32 *desc;
	int ret;

	edesc = aead_edesc_alloc(req, CHACHAPOLY_DESC_JOB_IO_LEN, &all_contig,
				 false);
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	desc = edesc->hw_desc;

	init_chachapoly_job(req, edesc, all_contig, false);
	print_hex_dump_debug("chachapoly jobdesc@" __stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
			     1);

	ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
	}

	return ret;
}

1449 1450 1451 1452 1453 1454 1455 1456
static int ipsec_gcm_encrypt(struct aead_request *req)
{
	if (req->assoclen < 8)
		return -EINVAL;

	return gcm_encrypt(req);
}

1457
static int aead_encrypt(struct aead_request *req)
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
{
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	bool all_contig;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1468 1469
	edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
				 &all_contig, true);
1470 1471 1472 1473
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor */
1474
	init_authenc_job(req, edesc, all_contig, true);
1475 1476 1477 1478 1479 1480 1481
#ifdef DEBUG
	print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif

	desc = edesc->hw_desc;
1482
	ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
1483 1484 1485
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1486
		aead_unmap(jrdev, edesc, req);
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
		kfree(edesc);
	}

	return ret;
}

static int gcm_decrypt(struct aead_request *req)
{
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	bool all_contig;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
	edesc = aead_edesc_alloc(req, GCM_DESC_JOB_IO_LEN, &all_contig, false);
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor*/
	init_gcm_job(req, edesc, all_contig, false);
#ifdef DEBUG
	print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif

	desc = edesc->hw_desc;
	ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
	}

	return ret;
}

1528 1529 1530 1531 1532 1533 1534 1535
static int ipsec_gcm_decrypt(struct aead_request *req)
{
	if (req->assoclen < 8)
		return -EINVAL;

	return gcm_decrypt(req);
}

1536
static int aead_decrypt(struct aead_request *req)
1537
{
1538
	struct aead_edesc *edesc;
1539 1540 1541
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1542
	bool all_contig;
1543
	u32 *desc;
1544
	int ret = 0;
1545

1546 1547 1548
	caam_dump_sg(KERN_ERR, "dec src@" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->src,
		     req->assoclen + req->cryptlen, 1);
C
Catalin Vasile 已提交
1549

1550
	/* allocate extended descriptor */
1551 1552
	edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
				 &all_contig, false);
1553 1554 1555
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1556
	/* Create and submit job descriptor*/
1557
	init_authenc_job(req, edesc, all_contig, false);
1558
#ifdef DEBUG
1559
	print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
1560 1561 1562 1563
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif

1564
	desc = edesc->hw_desc;
1565
	ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
1566 1567 1568
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1569
		aead_unmap(jrdev, edesc, req);
1570 1571
		kfree(edesc);
	}
1572

1573 1574
	return ret;
}
1575

Y
Yuan Kang 已提交
1576
/*
1577
 * allocate and map the skcipher extended descriptor for skcipher
Y
Yuan Kang 已提交
1578
 */
1579 1580
static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req,
						   int desc_bytes)
Y
Yuan Kang 已提交
1581
{
1582 1583
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1584
	struct device *jrdev = ctx->jrdev;
1585
	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
Y
Yuan Kang 已提交
1586
		       GFP_KERNEL : GFP_ATOMIC;
1587
	int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
1588
	struct skcipher_edesc *edesc;
1589 1590
	dma_addr_t iv_dma;
	u8 *iv;
1591
	int ivsize = crypto_skcipher_ivsize(skcipher);
1592
	int dst_sg_idx, sec4_sg_ents, sec4_sg_bytes;
Y
Yuan Kang 已提交
1593

1594
	src_nents = sg_nents_for_len(req->src, req->cryptlen);
1595 1596
	if (unlikely(src_nents < 0)) {
		dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
1597
			req->cryptlen);
1598 1599
		return ERR_PTR(src_nents);
	}
Y
Yuan Kang 已提交
1600

1601
	if (req->dst != req->src) {
1602
		dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
1603 1604
		if (unlikely(dst_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in dst S/G\n",
1605
				req->cryptlen);
1606 1607 1608
			return ERR_PTR(dst_nents);
		}
	}
Y
Yuan Kang 已提交
1609 1610

	if (likely(req->src == req->dst)) {
1611 1612 1613
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_BIDIRECTIONAL);
		if (unlikely(!mapped_src_nents)) {
1614 1615 1616
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
Y
Yuan Kang 已提交
1617
	} else {
1618 1619 1620
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_TO_DEVICE);
		if (unlikely(!mapped_src_nents)) {
1621 1622 1623 1624
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}

1625 1626 1627
		mapped_dst_nents = dma_map_sg(jrdev, req->dst, dst_nents,
					      DMA_FROM_DEVICE);
		if (unlikely(!mapped_dst_nents)) {
1628
			dev_err(jrdev, "unable to map destination\n");
1629
			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1630 1631
			return ERR_PTR(-ENOMEM);
		}
Y
Yuan Kang 已提交
1632 1633
	}

1634
	sec4_sg_ents = 1 + mapped_src_nents;
1635
	dst_sg_idx = sec4_sg_ents;
1636
	sec4_sg_ents += mapped_dst_nents > 1 ? mapped_dst_nents : 0;
1637
	sec4_sg_bytes = sec4_sg_ents * sizeof(struct sec4_sg_entry);
Y
Yuan Kang 已提交
1638

1639 1640 1641 1642
	/*
	 * allocate space for base edesc and hw desc commands, link tables, IV
	 */
	edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes + ivsize,
1643
			GFP_DMA | flags);
Y
Yuan Kang 已提交
1644 1645
	if (!edesc) {
		dev_err(jrdev, "could not allocate extended descriptor\n");
1646
		caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
1647
			   0, 0, 0);
Y
Yuan Kang 已提交
1648 1649 1650 1651 1652
		return ERR_PTR(-ENOMEM);
	}

	edesc->src_nents = src_nents;
	edesc->dst_nents = dst_nents;
Y
Yuan Kang 已提交
1653
	edesc->sec4_sg_bytes = sec4_sg_bytes;
1654 1655
	edesc->sec4_sg = (struct sec4_sg_entry *)((u8 *)edesc->hw_desc +
						  desc_bytes);
Y
Yuan Kang 已提交
1656

1657 1658
	/* Make sure IV is located in a DMAable area */
	iv = (u8 *)edesc->hw_desc + desc_bytes + sec4_sg_bytes;
1659
	memcpy(iv, req->iv, ivsize);
1660 1661 1662 1663 1664

	iv_dma = dma_map_single(jrdev, iv, ivsize, DMA_TO_DEVICE);
	if (dma_mapping_error(jrdev, iv_dma)) {
		dev_err(jrdev, "unable to map IV\n");
		caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
1665
			   0, 0, 0);
1666 1667
		kfree(edesc);
		return ERR_PTR(-ENOMEM);
Y
Yuan Kang 已提交
1668 1669
	}

1670 1671 1672
	dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
	sg_to_sec4_sg_last(req->src, mapped_src_nents, edesc->sec4_sg + 1, 0);

1673 1674 1675
	if (mapped_dst_nents > 1) {
		sg_to_sec4_sg_last(req->dst, mapped_dst_nents,
				   edesc->sec4_sg + dst_sg_idx, 0);
Y
Yuan Kang 已提交
1676 1677
	}

Y
Yuan Kang 已提交
1678 1679
	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
					    sec4_sg_bytes, DMA_TO_DEVICE);
1680 1681
	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
		dev_err(jrdev, "unable to map S/G table\n");
1682
		caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
1683
			   iv_dma, ivsize, 0, 0);
1684
		kfree(edesc);
1685 1686 1687
		return ERR_PTR(-ENOMEM);
	}

Y
Yuan Kang 已提交
1688 1689 1690
	edesc->iv_dma = iv_dma;

#ifdef DEBUG
1691
	print_hex_dump(KERN_ERR, "skcipher sec4_sg@" __stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1692 1693
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
		       sec4_sg_bytes, 1);
Y
Yuan Kang 已提交
1694 1695 1696 1697 1698
#endif

	return edesc;
}

1699
static int skcipher_encrypt(struct skcipher_request *req)
Y
Yuan Kang 已提交
1700
{
1701 1702 1703
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1704 1705 1706 1707 1708
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1709
	edesc = skcipher_edesc_alloc(req, DESC_JOB_IO_LEN * CAAM_CMD_SZ);
Y
Yuan Kang 已提交
1710 1711 1712 1713
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor*/
1714
	init_skcipher_job(req, edesc, true);
Y
Yuan Kang 已提交
1715
#ifdef DEBUG
1716
	print_hex_dump(KERN_ERR, "skcipher jobdesc@" __stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1717 1718 1719 1720
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif
	desc = edesc->hw_desc;
1721
	ret = caam_jr_enqueue(jrdev, desc, skcipher_encrypt_done, req);
Y
Yuan Kang 已提交
1722 1723 1724 1725

	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1726
		skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
1727 1728 1729 1730 1731 1732
		kfree(edesc);
	}

	return ret;
}

1733
static int skcipher_decrypt(struct skcipher_request *req)
Y
Yuan Kang 已提交
1734
{
1735 1736 1737 1738
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
1739 1740 1741 1742 1743
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1744
	edesc = skcipher_edesc_alloc(req, DESC_JOB_IO_LEN * CAAM_CMD_SZ);
Y
Yuan Kang 已提交
1745 1746 1747
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1748
	/*
1749
	 * The crypto API expects us to set the IV (req->iv) to the last
1750 1751
	 * ciphertext block.
	 */
1752
	scatterwalk_map_and_copy(req->iv, req->src, req->cryptlen - ivsize,
1753 1754
				 ivsize, 0);

Y
Yuan Kang 已提交
1755
	/* Create and submit job descriptor*/
1756
	init_skcipher_job(req, edesc, false);
Y
Yuan Kang 已提交
1757 1758
	desc = edesc->hw_desc;
#ifdef DEBUG
1759
	print_hex_dump(KERN_ERR, "skcipher jobdesc@" __stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1760 1761 1762 1763
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif

1764
	ret = caam_jr_enqueue(jrdev, desc, skcipher_decrypt_done, req);
Y
Yuan Kang 已提交
1765 1766 1767
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1768
		skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
1769 1770 1771 1772 1773 1774
		kfree(edesc);
	}

	return ret;
}

1775
static struct caam_skcipher_alg driver_algs[] = {
1776
	{
1777 1778 1779 1780 1781 1782 1783 1784 1785
		.skcipher = {
			.base = {
				.cra_name = "cbc(aes)",
				.cra_driver_name = "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
			.setkey = skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1786 1787 1788
			.min_keysize = AES_MIN_KEY_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
1789 1790
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
1791 1792
	},
	{
1793 1794 1795 1796 1797 1798 1799 1800 1801
		.skcipher = {
			.base = {
				.cra_name = "cbc(des3_ede)",
				.cra_driver_name = "cbc-3des-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
			.setkey = skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1802 1803 1804
			.min_keysize = DES3_EDE_KEY_SIZE,
			.max_keysize = DES3_EDE_KEY_SIZE,
			.ivsize = DES3_EDE_BLOCK_SIZE,
1805 1806
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
1807 1808
	},
	{
1809 1810 1811 1812 1813 1814 1815 1816 1817
		.skcipher = {
			.base = {
				.cra_name = "cbc(des)",
				.cra_driver_name = "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
			.setkey = skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1818 1819 1820
			.min_keysize = DES_KEY_SIZE,
			.max_keysize = DES_KEY_SIZE,
			.ivsize = DES_BLOCK_SIZE,
1821 1822
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
1823 1824
	},
	{
1825 1826 1827 1828 1829 1830 1831 1832 1833
		.skcipher = {
			.base = {
				.cra_name = "ctr(aes)",
				.cra_driver_name = "ctr-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1834 1835 1836
			.min_keysize = AES_MIN_KEY_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
1837 1838 1839 1840
			.chunksize = AES_BLOCK_SIZE,
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES |
					OP_ALG_AAI_CTR_MOD128,
1841 1842
	},
	{
1843 1844 1845 1846 1847 1848 1849 1850 1851
		.skcipher = {
			.base = {
				.cra_name = "rfc3686(ctr(aes))",
				.cra_driver_name = "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1852 1853 1854 1855 1856
			.min_keysize = AES_MIN_KEY_SIZE +
				       CTR_RFC3686_NONCE_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE +
				       CTR_RFC3686_NONCE_SIZE,
			.ivsize = CTR_RFC3686_IV_SIZE,
1857 1858 1859 1860 1861 1862 1863
			.chunksize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.rfc3686 = true,
		},
1864 1865
	},
	{
1866 1867 1868 1869 1870 1871 1872 1873 1874
		.skcipher = {
			.base = {
				.cra_name = "xts(aes)",
				.cra_driver_name = "xts-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
			.setkey = xts_skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1875 1876 1877
			.min_keysize = 2 * AES_MIN_KEY_SIZE,
			.max_keysize = 2 * AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
1878 1879
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS,
1880
	},
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
};

static struct caam_aead_alg driver_aeads[] = {
	{
		.aead = {
			.base = {
				.cra_name = "rfc4106(gcm(aes))",
				.cra_driver_name = "rfc4106-gcm-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = rfc4106_setkey,
			.setauthsize = rfc4106_setauthsize,
			.encrypt = ipsec_gcm_encrypt,
			.decrypt = ipsec_gcm_decrypt,
1895
			.ivsize = GCM_RFC4106_IV_SIZE,
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
			.maxauthsize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "rfc4543(gcm(aes))",
				.cra_driver_name = "rfc4543-gcm-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = rfc4543_setkey,
			.setauthsize = rfc4543_setauthsize,
			.encrypt = ipsec_gcm_encrypt,
			.decrypt = ipsec_gcm_decrypt,
1913
			.ivsize = GCM_RFC4543_IV_SIZE,
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
			.maxauthsize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
		},
	},
	/* Galois Counter Mode */
	{
		.aead = {
			.base = {
				.cra_name = "gcm(aes)",
				.cra_driver_name = "gcm-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = gcm_setkey,
			.setauthsize = gcm_setauthsize,
			.encrypt = gcm_encrypt,
			.decrypt = gcm_decrypt,
1932
			.ivsize = GCM_AES_IV_SIZE,
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
			.maxauthsize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
		},
	},
	/* single-pass ipsec_esp descriptor */
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
1953
			.ivsize = NULL_IV_SIZE,
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
1969
			},
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
1981 1982
	},
	{
1983 1984 1985 1986 1987 1988 1989 1990
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
1991 1992
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
1993 1994
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
1995 1996
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
1997 1998 1999 2000 2001
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2002 2003
	},
	{
2004 2005 2006 2007 2008 2009 2010 2011
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
2012 2013
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2014 2015
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2016 2017
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
2018 2019 2020 2021 2022
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2023 2024
	},
	{
2025 2026 2027 2028 2029 2030 2031 2032
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
2033 2034
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2035 2036
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2037 2038
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
2039 2040 2041 2042 2043
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2044 2045
	},
	{
2046 2047 2048 2049 2050 2051 2052 2053
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha512),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
2054 2055
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2056 2057
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2058 2059
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),cbc(aes))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
2073
			},
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2086
	},
2087
	{
2088 2089 2090 2091 2092 2093 2094 2095
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(md5),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-hmac-md5-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
2096 2097
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2098
			.encrypt = aead_encrypt,
2099
			.decrypt = aead_decrypt,
2100 2101
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
2117
			},
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2130
	},
2131
	{
2132 2133 2134 2135 2136 2137 2138 2139
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha1),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha1-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2140 2141
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2142
			.encrypt = aead_encrypt,
2143
			.decrypt = aead_decrypt,
2144 2145
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
2161
			},
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2174
	},
2175
	{
2176 2177 2178 2179 2180 2181 2182 2183
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha224),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha224-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
2184 2185
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2186
			.encrypt = aead_encrypt,
2187
			.decrypt = aead_decrypt,
2188 2189
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
2205
			},
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2218
	},
2219
	{
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha256),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha256-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
2231
			.decrypt = aead_decrypt,
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha384),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha384-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2272 2273
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2274
			.encrypt = aead_encrypt,
2275
			.decrypt = aead_decrypt,
2276
			.ivsize = AES_BLOCK_SIZE,
2277 2278 2279 2280 2281 2282 2283 2284
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
2285
	},
2286
	{
2287 2288 2289 2290 2291 2292 2293
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha512),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
2294 2295
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2296 2297
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2298
			.ivsize = AES_BLOCK_SIZE,
2299 2300 2301 2302 2303 2304 2305
			.maxauthsize = SHA512_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2306
	},
2307
	{
2308 2309 2310 2311 2312 2313 2314 2315
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha512),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha512-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2316 2317
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2318
			.encrypt = aead_encrypt,
2319
			.decrypt = aead_decrypt,
2320 2321
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2337
			},
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		}
2350
	},
2351
	{
2352 2353 2354 2355 2356 2357 2358 2359
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(md5),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-hmac-md5-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
2360 2361
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2362
			.encrypt = aead_encrypt,
2363
			.decrypt = aead_decrypt,
2364 2365
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		}
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2382
			},
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2395
	},
2396
	{
2397 2398 2399 2400 2401 2402 2403 2404 2405
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha1),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha1-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2406 2407
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2408
			.encrypt = aead_encrypt,
2409
			.decrypt = aead_decrypt,
2410 2411
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2428
			},
2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2441
	},
2442
	{
2443 2444 2445 2446 2447 2448 2449 2450 2451
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha224),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha224-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
2452 2453
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2454
			.encrypt = aead_encrypt,
2455
			.decrypt = aead_decrypt,
2456 2457
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2474
			},
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2487
	},
2488
	{
2489 2490 2491 2492 2493 2494 2495 2496 2497
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha256),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha256-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2498 2499
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2500
			.encrypt = aead_encrypt,
2501
			.decrypt = aead_decrypt,
2502 2503
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2520
			},
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2533
	},
2534
	{
2535 2536 2537 2538 2539 2540 2541 2542 2543
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha384),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha384-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
2544 2545
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2546
			.encrypt = aead_encrypt,
2547
			.decrypt = aead_decrypt,
2548 2549
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha512),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2566
			},
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2579
	},
2580
	{
2581 2582 2583 2584 2585 2586 2587 2588 2589
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha512),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha512-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2590 2591
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2592
			.encrypt = aead_encrypt,
2593
			.decrypt = aead_decrypt,
2594 2595
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),cbc(des))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2611
			},
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2624
	},
2625
	{
2626 2627 2628 2629 2630 2631 2632 2633
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(md5),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-hmac-md5-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
2634 2635
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2636
			.encrypt = aead_encrypt,
2637
			.decrypt = aead_decrypt,
2638 2639
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2655
			},
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2668
	},
2669
	{
2670 2671 2672 2673 2674 2675 2676 2677
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha1),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha1-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2678 2679
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2680
			.encrypt = aead_encrypt,
2681
			.decrypt = aead_decrypt,
2682 2683
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2699
			},
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2712
	},
2713
	{
2714 2715 2716 2717 2718 2719 2720 2721
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha224),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha224-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
2722 2723
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2724
			.encrypt = aead_encrypt,
2725
			.decrypt = aead_decrypt,
2726 2727
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2743
			},
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2756
	},
2757
	{
2758 2759 2760 2761 2762 2763 2764 2765
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha256),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha256-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2766 2767
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2768
			.encrypt = aead_encrypt,
2769
			.decrypt = aead_decrypt,
2770 2771
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2787
			},
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2800
	},
2801
	{
2802 2803 2804 2805 2806 2807 2808 2809
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha384),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha384-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
2810 2811
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2812
			.encrypt = aead_encrypt,
2813
			.decrypt = aead_decrypt,
2814 2815
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha512),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2831
			},
2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2844
	},
2845
	{
2846 2847 2848 2849 2850 2851 2852 2853
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha512),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha512-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2854 2855
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2856
			.encrypt = aead_encrypt,
2857
			.decrypt = aead_decrypt,
2858 2859
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
2860 2861 2862 2863 2864 2865 2866
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
2867
	},
2868
	{
2869 2870 2871 2872 2873 2874 2875 2876
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
2877 2878
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2879 2880
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2881 2882
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
2883 2884 2885 2886 2887 2888 2889 2890
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
2891 2892
	},
	{
2893 2894 2895 2896 2897 2898 2899 2900
		.aead = {
			.base = {
				.cra_name = "seqiv(authenc("
					    "hmac(md5),rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-md5-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
2901 2902
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2903
			.encrypt = aead_encrypt,
2904
			.decrypt = aead_decrypt,
2905
			.ivsize = CTR_RFC3686_IV_SIZE,
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
		},
2916 2917
	},
	{
2918 2919 2920 2921 2922 2923 2924 2925
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
2926 2927
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2928 2929
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2930
			.ivsize = CTR_RFC3686_IV_SIZE,
2931 2932 2933 2934 2935 2936 2937 2938 2939
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
2940 2941
	},
	{
2942 2943 2944 2945 2946 2947 2948 2949
		.aead = {
			.base = {
				.cra_name = "seqiv(authenc("
					    "hmac(sha1),rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha1-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
2950 2951
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2952
			.encrypt = aead_encrypt,
2953
			.decrypt = aead_decrypt,
2954
			.ivsize = CTR_RFC3686_IV_SIZE,
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
		},
2965 2966
	},
	{
2967 2968 2969 2970 2971 2972 2973 2974
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
2975 2976
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2977 2978
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2979
			.ivsize = CTR_RFC3686_IV_SIZE,
2980 2981 2982 2983 2984 2985 2986 2987 2988
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
2989 2990
	},
	{
2991 2992 2993 2994 2995 2996 2997 2998
		.aead = {
			.base = {
				.cra_name = "seqiv(authenc("
					    "hmac(sha224),rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha224-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
2999 3000
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
3001
			.encrypt = aead_encrypt,
3002
			.decrypt = aead_decrypt,
3003
			.ivsize = CTR_RFC3686_IV_SIZE,
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
		},
Y
Yuan Kang 已提交
3014 3015
	},
	{
3016 3017 3018 3019 3020 3021 3022
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
Y
Yuan Kang 已提交
3023
			},
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
Y
Yuan Kang 已提交
3038 3039
	},
	{
3040 3041 3042 3043 3044 3045 3046
		.aead = {
			.base = {
				.cra_name = "seqiv(authenc(hmac(sha256),"
					    "rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha256-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
Y
Yuan Kang 已提交
3047
			},
3048 3049 3050
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
3051
			.decrypt = aead_decrypt,
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
		},
3063 3064
	},
	{
3065 3066 3067 3068 3069 3070 3071
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
3072
			},
3073 3074 3075 3076
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
3077
			.ivsize = CTR_RFC3686_IV_SIZE,
3078 3079 3080 3081 3082 3083 3084 3085 3086 3087
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
	},
3088 3089 3090
	{
		.aead = {
			.base = {
3091 3092 3093 3094
				.cra_name = "seqiv(authenc(hmac(sha384),"
					    "rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha384-"
						   "rfc3686-ctr-aes-caam",
3095 3096
				.cra_blocksize = 1,
			},
3097 3098 3099
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
3100
			.decrypt = aead_decrypt,
3101 3102
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
3103 3104
		},
		.caam = {
3105 3106 3107 3108 3109 3110
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
3111 3112 3113 3114 3115
		},
	},
	{
		.aead = {
			.base = {
3116 3117 3118 3119
				.cra_name = "authenc(hmac(sha512),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "rfc3686-ctr-aes-caam",
3120 3121
				.cra_blocksize = 1,
			},
3122 3123 3124 3125 3126 3127
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
3128 3129
		},
		.caam = {
3130 3131 3132 3133 3134
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
3135 3136 3137 3138 3139
		},
	},
	{
		.aead = {
			.base = {
3140 3141 3142 3143
				.cra_name = "seqiv(authenc(hmac(sha512),"
					    "rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha512-"
						   "rfc3686-ctr-aes-caam",
3144 3145
				.cra_blocksize = 1,
			},
3146 3147 3148
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
3149
			.decrypt = aead_decrypt,
3150 3151
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
3152 3153
		},
		.caam = {
3154 3155 3156 3157 3158 3159
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
3160 3161
		},
	},
3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205
	{
		.aead = {
			.base = {
				.cra_name = "rfc7539(chacha20,poly1305)",
				.cra_driver_name = "rfc7539-chacha20-poly1305-"
						   "caam",
				.cra_blocksize = 1,
			},
			.setkey = chachapoly_setkey,
			.setauthsize = chachapoly_setauthsize,
			.encrypt = chachapoly_encrypt,
			.decrypt = chachapoly_decrypt,
			.ivsize = CHACHAPOLY_IV_SIZE,
			.maxauthsize = POLY1305_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_CHACHA20 |
					   OP_ALG_AAI_AEAD,
			.class2_alg_type = OP_ALG_ALGSEL_POLY1305 |
					   OP_ALG_AAI_AEAD,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "rfc7539esp(chacha20,poly1305)",
				.cra_driver_name = "rfc7539esp-chacha20-"
						   "poly1305-caam",
				.cra_blocksize = 1,
			},
			.setkey = chachapoly_setkey,
			.setauthsize = chachapoly_setauthsize,
			.encrypt = chachapoly_encrypt,
			.decrypt = chachapoly_decrypt,
			.ivsize = 8,
			.maxauthsize = POLY1305_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_CHACHA20 |
					   OP_ALG_AAI_AEAD,
			.class2_alg_type = OP_ALG_ALGSEL_POLY1305 |
					   OP_ALG_AAI_AEAD,
		},
	},
3206 3207
};

3208 3209
static int caam_init_common(struct caam_ctx *ctx, struct caam_alg_entry *caam,
			    bool uses_dkp)
3210
{
3211
	dma_addr_t dma_addr;
3212
	struct caam_drv_private *priv;
3213

3214 3215 3216 3217 3218
	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);
	}
3219

3220 3221 3222 3223 3224 3225
	priv = dev_get_drvdata(ctx->jrdev->parent);
	if (priv->era >= 6 && uses_dkp)
		ctx->dir = DMA_BIDIRECTIONAL;
	else
		ctx->dir = DMA_TO_DEVICE;

3226 3227 3228
	dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_enc,
					offsetof(struct caam_ctx,
						 sh_desc_enc_dma),
3229
					ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240
	if (dma_mapping_error(ctx->jrdev, dma_addr)) {
		dev_err(ctx->jrdev, "unable to map key, shared descriptors\n");
		caam_jr_free(ctx->jrdev);
		return -ENOMEM;
	}

	ctx->sh_desc_enc_dma = dma_addr;
	ctx->sh_desc_dec_dma = dma_addr + offsetof(struct caam_ctx,
						   sh_desc_dec);
	ctx->key_dma = dma_addr + offsetof(struct caam_ctx, key);

3241
	/* copy descriptor header template value */
3242 3243
	ctx->cdata.algtype = OP_TYPE_CLASS1_ALG | caam->class1_alg_type;
	ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam->class2_alg_type;
3244 3245 3246 3247

	return 0;
}

3248
static int caam_cra_init(struct crypto_skcipher *tfm)
3249
{
3250 3251 3252
	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
	struct caam_skcipher_alg *caam_alg =
		container_of(alg, typeof(*caam_alg), skcipher);
3253

3254 3255
	return caam_init_common(crypto_skcipher_ctx(tfm), &caam_alg->caam,
				false);
3256 3257 3258 3259 3260 3261 3262 3263 3264
}

static int caam_aead_init(struct crypto_aead *tfm)
{
	struct aead_alg *alg = crypto_aead_alg(tfm);
	struct caam_aead_alg *caam_alg =
		 container_of(alg, struct caam_aead_alg, aead);
	struct caam_ctx *ctx = crypto_aead_ctx(tfm);

3265 3266
	return caam_init_common(ctx, &caam_alg->caam,
				alg->setkey == aead_setkey);
3267 3268 3269 3270
}

static void caam_exit_common(struct caam_ctx *ctx)
{
3271 3272
	dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_enc_dma,
			       offsetof(struct caam_ctx, sh_desc_enc_dma),
3273
			       ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
3274
	caam_jr_free(ctx->jrdev);
3275 3276
}

3277
static void caam_cra_exit(struct crypto_skcipher *tfm)
3278
{
3279
	caam_exit_common(crypto_skcipher_ctx(tfm));
3280 3281 3282 3283 3284 3285 3286
}

static void caam_aead_exit(struct crypto_aead *tfm)
{
	caam_exit_common(crypto_aead_ctx(tfm));
}

3287 3288
static void __exit caam_algapi_exit(void)
{
3289 3290 3291 3292 3293 3294 3295 3296
	int i;

	for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
		struct caam_aead_alg *t_alg = driver_aeads + i;

		if (t_alg->registered)
			crypto_unregister_aead(&t_alg->aead);
	}
3297

3298 3299
	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
		struct caam_skcipher_alg *t_alg = driver_algs + i;
3300

3301 3302
		if (t_alg->registered)
			crypto_unregister_skcipher(&t_alg->skcipher);
3303 3304 3305
	}
}

3306
static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg)
3307
{
3308
	struct skcipher_alg *alg = &t_alg->skcipher;
3309

3310 3311 3312 3313
	alg->base.cra_module = THIS_MODULE;
	alg->base.cra_priority = CAAM_CRA_PRIORITY;
	alg->base.cra_ctxsize = sizeof(struct caam_ctx);
	alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
3314

3315 3316
	alg->init = caam_cra_init;
	alg->exit = caam_cra_exit;
3317 3318
}

3319 3320 3321 3322 3323 3324 3325
static void caam_aead_alg_init(struct caam_aead_alg *t_alg)
{
	struct aead_alg *alg = &t_alg->aead;

	alg->base.cra_module = THIS_MODULE;
	alg->base.cra_priority = CAAM_CRA_PRIORITY;
	alg->base.cra_ctxsize = sizeof(struct caam_ctx);
3326
	alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
3327 3328 3329 3330 3331

	alg->init = caam_aead_init;
	alg->exit = caam_aead_exit;
}

3332 3333
static int __init caam_algapi_init(void)
{
3334 3335 3336
	struct device_node *dev_node;
	struct platform_device *pdev;
	struct device *ctrldev;
3337
	struct caam_drv_private *priv;
3338
	int i = 0, err = 0;
3339
	u32 aes_vid, aes_inst, des_inst, md_vid, md_inst, ccha_inst, ptha_inst;
3340
	unsigned int md_limit = SHA512_DIGEST_SIZE;
3341
	bool registered = false;
3342

3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
	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;


3368 3369 3370 3371
	/*
	 * Register crypto algorithms the device supports.
	 * First, detect presence and attributes of DES, AES, and MD blocks.
	 */
3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383
	if (priv->era < 10) {
		u32 cha_vid, cha_inst;

		cha_vid = rd_reg32(&priv->ctrl->perfmon.cha_id_ls);
		aes_vid = cha_vid & CHA_ID_LS_AES_MASK;
		md_vid = (cha_vid & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;

		cha_inst = rd_reg32(&priv->ctrl->perfmon.cha_num_ls);
		des_inst = (cha_inst & CHA_ID_LS_DES_MASK) >>
			   CHA_ID_LS_DES_SHIFT;
		aes_inst = cha_inst & CHA_ID_LS_AES_MASK;
		md_inst = (cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
3384 3385
		ccha_inst = 0;
		ptha_inst = 0;
3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397
	} else {
		u32 aesa, mdha;

		aesa = rd_reg32(&priv->ctrl->vreg.aesa);
		mdha = rd_reg32(&priv->ctrl->vreg.mdha);

		aes_vid = (aesa & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
		md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;

		des_inst = rd_reg32(&priv->ctrl->vreg.desa) & CHA_VER_NUM_MASK;
		aes_inst = aesa & CHA_VER_NUM_MASK;
		md_inst = mdha & CHA_VER_NUM_MASK;
3398 3399
		ccha_inst = rd_reg32(&priv->ctrl->vreg.ccha) & CHA_VER_NUM_MASK;
		ptha_inst = rd_reg32(&priv->ctrl->vreg.ptha) & CHA_VER_NUM_MASK;
3400
	}
3401 3402

	/* If MD is present, limit digest size based on LP256 */
3403
	if (md_inst && md_vid  == CHA_VER_VID_MD_LP256)
3404 3405
		md_limit = SHA256_DIGEST_SIZE;

3406
	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
3407 3408
		struct caam_skcipher_alg *t_alg = driver_algs + i;
		u32 alg_sel = t_alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK;
3409 3410 3411 3412 3413 3414 3415 3416 3417 3418

		/* Skip DES algorithms if not supported by device */
		if (!des_inst &&
		    ((alg_sel == OP_ALG_ALGSEL_3DES) ||
		     (alg_sel == OP_ALG_ALGSEL_DES)))
				continue;

		/* Skip AES algorithms if not supported by device */
		if (!aes_inst && (alg_sel == OP_ALG_ALGSEL_AES))
				continue;
3419

3420 3421 3422 3423
		/*
		 * Check support for AES modes not available
		 * on LP devices.
		 */
3424 3425 3426 3427
		if (aes_vid == CHA_VER_VID_AES_LP &&
		    (t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK) ==
		    OP_ALG_AAI_XTS)
			continue;
3428

3429
		caam_skcipher_alg_init(t_alg);
3430

3431
		err = crypto_register_skcipher(&t_alg->skcipher);
3432
		if (err) {
3433
			pr_warn("%s alg registration failed\n",
3434
				t_alg->skcipher.base.cra_driver_name);
3435 3436 3437
			continue;
		}

3438
		t_alg->registered = true;
3439 3440 3441 3442 3443
		registered = true;
	}

	for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
		struct caam_aead_alg *t_alg = driver_aeads + i;
3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459
		u32 c1_alg_sel = t_alg->caam.class1_alg_type &
				 OP_ALG_ALGSEL_MASK;
		u32 c2_alg_sel = t_alg->caam.class2_alg_type &
				 OP_ALG_ALGSEL_MASK;
		u32 alg_aai = t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK;

		/* Skip DES algorithms if not supported by device */
		if (!des_inst &&
		    ((c1_alg_sel == OP_ALG_ALGSEL_3DES) ||
		     (c1_alg_sel == OP_ALG_ALGSEL_DES)))
				continue;

		/* Skip AES algorithms if not supported by device */
		if (!aes_inst && (c1_alg_sel == OP_ALG_ALGSEL_AES))
				continue;

3460 3461 3462 3463 3464 3465 3466 3467
		/* Skip CHACHA20 algorithms if not supported by device */
		if (c1_alg_sel == OP_ALG_ALGSEL_CHACHA20 && !ccha_inst)
			continue;

		/* Skip POLY1305 algorithms if not supported by device */
		if (c2_alg_sel == OP_ALG_ALGSEL_POLY1305 && !ptha_inst)
			continue;

3468 3469 3470 3471
		/*
		 * Check support for AES algorithms not available
		 * on LP devices.
		 */
3472 3473
		if (aes_vid  == CHA_VER_VID_AES_LP && alg_aai == OP_ALG_AAI_GCM)
			continue;
3474 3475 3476 3477 3478

		/*
		 * Skip algorithms requiring message digests
		 * if MD or MD size is not supported by device.
		 */
3479 3480 3481
		if ((c2_alg_sel & ~OP_ALG_ALGSEL_SUBMASK) == 0x40 &&
		    (!md_inst || t_alg->aead.maxauthsize > md_limit))
			continue;
3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493

		caam_aead_alg_init(t_alg);

		err = crypto_register_aead(&t_alg->aead);
		if (err) {
			pr_warn("%s alg registration failed\n",
				t_alg->aead.base.cra_driver_name);
			continue;
		}

		t_alg->registered = true;
		registered = true;
3494
	}
3495 3496

	if (registered)
3497
		pr_info("caam algorithms registered in /proc/crypto\n");
3498 3499 3500 3501 3502 3503 3504 3505 3506 3507

	return err;
}

module_init(caam_algapi_init);
module_exit(caam_algapi_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("FSL CAAM support for crypto API");
MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");