Lines Matching full:cryp
13 #include "jh7110-cryp.h"
52 static inline int starfive_aes_wait_busy(struct starfive_cryp_dev *cryp) in starfive_aes_wait_busy() argument
56 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status, in starfive_aes_wait_busy()
60 static inline int starfive_aes_wait_keydone(struct starfive_cryp_dev *cryp) in starfive_aes_wait_keydone() argument
64 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status, in starfive_aes_wait_keydone()
68 static inline int starfive_aes_wait_gcmdone(struct starfive_cryp_dev *cryp) in starfive_aes_wait_gcmdone() argument
72 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status, in starfive_aes_wait_gcmdone()
76 static inline int is_gcm(struct starfive_cryp_dev *cryp) in is_gcm() argument
78 return (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_GCM; in is_gcm()
81 static inline int is_encrypt(struct starfive_cryp_dev *cryp) in is_encrypt() argument
83 return cryp->flags & FLG_ENCRYPT; in is_encrypt()
88 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_aead_hw_start() local
93 value = readl(ctx->cryp->base + STARFIVE_AES_CSR); in starfive_aes_aead_hw_start()
95 writel(value, cryp->base + STARFIVE_AES_CSR); in starfive_aes_aead_hw_start()
96 starfive_aes_wait_gcmdone(cryp); in starfive_aes_aead_hw_start()
99 value = readl(ctx->cryp->base + STARFIVE_AES_CSR); in starfive_aes_aead_hw_start()
101 writel(value, cryp->base + STARFIVE_AES_CSR); in starfive_aes_aead_hw_start()
108 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_set_ivlen() local
110 if (is_gcm(cryp)) in starfive_aes_set_ivlen()
111 writel(GCM_AES_IV_SIZE, cryp->base + STARFIVE_AES_IVLEN); in starfive_aes_set_ivlen()
113 writel(AES_BLOCK_SIZE, cryp->base + STARFIVE_AES_IVLEN); in starfive_aes_set_ivlen()
118 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_set_alen() local
120 writel(upper_32_bits(cryp->assoclen), cryp->base + STARFIVE_AES_ALEN0); in starfive_aes_set_alen()
121 writel(lower_32_bits(cryp->assoclen), cryp->base + STARFIVE_AES_ALEN1); in starfive_aes_set_alen()
126 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_set_mlen() local
128 writel(upper_32_bits(cryp->total_in), cryp->base + STARFIVE_AES_MLEN0); in starfive_aes_set_mlen()
129 writel(lower_32_bits(cryp->total_in), cryp->base + STARFIVE_AES_MLEN1); in starfive_aes_set_mlen()
143 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_write_iv() local
145 writel(iv[0], cryp->base + STARFIVE_AES_IV0); in starfive_aes_write_iv()
146 writel(iv[1], cryp->base + STARFIVE_AES_IV1); in starfive_aes_write_iv()
147 writel(iv[2], cryp->base + STARFIVE_AES_IV2); in starfive_aes_write_iv()
149 if (is_gcm(cryp)) { in starfive_aes_write_iv()
150 if (starfive_aes_wait_gcmdone(cryp)) in starfive_aes_write_iv()
156 writel(iv[3], cryp->base + STARFIVE_AES_IV3); in starfive_aes_write_iv()
161 static inline void starfive_aes_get_iv(struct starfive_cryp_dev *cryp, u32 *iv) in starfive_aes_get_iv() argument
163 iv[0] = readl(cryp->base + STARFIVE_AES_IV0); in starfive_aes_get_iv()
164 iv[1] = readl(cryp->base + STARFIVE_AES_IV1); in starfive_aes_get_iv()
165 iv[2] = readl(cryp->base + STARFIVE_AES_IV2); in starfive_aes_get_iv()
166 iv[3] = readl(cryp->base + STARFIVE_AES_IV3); in starfive_aes_get_iv()
171 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_write_nonce() local
173 writel(nonce[0], cryp->base + STARFIVE_AES_NONCE0); in starfive_aes_write_nonce()
174 writel(nonce[1], cryp->base + STARFIVE_AES_NONCE1); in starfive_aes_write_nonce()
175 writel(nonce[2], cryp->base + STARFIVE_AES_NONCE2); in starfive_aes_write_nonce()
176 writel(nonce[3], cryp->base + STARFIVE_AES_NONCE3); in starfive_aes_write_nonce()
181 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_write_key() local
185 writel(key[0], cryp->base + STARFIVE_AES_KEY0); in starfive_aes_write_key()
186 writel(key[1], cryp->base + STARFIVE_AES_KEY1); in starfive_aes_write_key()
187 writel(key[2], cryp->base + STARFIVE_AES_KEY2); in starfive_aes_write_key()
188 writel(key[3], cryp->base + STARFIVE_AES_KEY3); in starfive_aes_write_key()
192 writel(key[4], cryp->base + STARFIVE_AES_KEY4); in starfive_aes_write_key()
193 writel(key[5], cryp->base + STARFIVE_AES_KEY5); in starfive_aes_write_key()
197 writel(key[6], cryp->base + STARFIVE_AES_KEY6); in starfive_aes_write_key()
198 writel(key[7], cryp->base + STARFIVE_AES_KEY7); in starfive_aes_write_key()
201 if (starfive_aes_wait_keydone(cryp)) in starfive_aes_write_key()
209 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_ccm_init() local
213 memcpy(iv, cryp->req.areq->iv, AES_BLOCK_SIZE); in starfive_aes_ccm_init()
219 b0[0] |= (8 * ((cryp->authsize - 2) / 2)); in starfive_aes_ccm_init()
221 if (cryp->assoclen) in starfive_aes_ccm_init()
224 textlen = cryp->total_in; in starfive_aes_ccm_init()
237 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_hw_init() local
243 writel(rctx->csr.aes.v, cryp->base + STARFIVE_AES_CSR); in starfive_aes_hw_init()
246 hw_mode = cryp->flags & FLG_MODE_MASK; in starfive_aes_hw_init()
263 rctx->csr.aes.cmode = !is_encrypt(cryp); in starfive_aes_hw_init()
272 if (cryp->side_chan) { in starfive_aes_hw_init()
277 writel(rctx->csr.aes.v, cryp->base + STARFIVE_AES_CSR); in starfive_aes_hw_init()
279 cryp->err = starfive_aes_write_key(ctx); in starfive_aes_hw_init()
280 if (cryp->err) in starfive_aes_hw_init()
281 return cryp->err; in starfive_aes_hw_init()
289 starfive_aes_write_iv(ctx, (void *)cryp->req.areq->iv); in starfive_aes_hw_init()
301 starfive_aes_write_iv(ctx, (void *)cryp->req.sreq->iv); in starfive_aes_hw_init()
307 return cryp->err; in starfive_aes_hw_init()
310 static int starfive_aes_read_authtag(struct starfive_cryp_dev *cryp) in starfive_aes_read_authtag() argument
314 if (starfive_aes_wait_busy(cryp)) in starfive_aes_read_authtag()
315 return dev_err_probe(cryp->dev, -ETIMEDOUT, in starfive_aes_read_authtag()
320 if (is_gcm(cryp)) in starfive_aes_read_authtag()
322 cryp->tag_out[i] = readl(cryp->base + start_addr); in starfive_aes_read_authtag()
325 cryp->tag_out[i] = readl(cryp->base + STARFIVE_AES_AESDIO0R); in starfive_aes_read_authtag()
327 if (is_encrypt(cryp)) { in starfive_aes_read_authtag()
328 scatterwalk_copychunks(cryp->tag_out, &cryp->out_walk, cryp->authsize, 1); in starfive_aes_read_authtag()
330 scatterwalk_copychunks(cryp->tag_in, &cryp->in_walk, cryp->authsize, 0); in starfive_aes_read_authtag()
332 if (crypto_memneq(cryp->tag_in, cryp->tag_out, cryp->authsize)) in starfive_aes_read_authtag()
333 return dev_err_probe(cryp->dev, -EBADMSG, "Failed tag verification\n"); in starfive_aes_read_authtag()
339 static void starfive_aes_finish_req(struct starfive_cryp_dev *cryp) in starfive_aes_finish_req() argument
342 int err = cryp->err; in starfive_aes_finish_req()
344 if (!err && cryp->authsize) in starfive_aes_finish_req()
345 err = starfive_aes_read_authtag(cryp); in starfive_aes_finish_req()
347 if (!err && ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CBC || in starfive_aes_finish_req()
348 (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CTR)) in starfive_aes_finish_req()
349 starfive_aes_get_iv(cryp, (void *)cryp->req.sreq->iv); in starfive_aes_finish_req()
354 writel(csr.v, cryp->base + STARFIVE_AES_CSR); in starfive_aes_finish_req()
356 if (cryp->authsize) in starfive_aes_finish_req()
357 crypto_finalize_aead_request(cryp->engine, cryp->req.areq, err); in starfive_aes_finish_req()
359 crypto_finalize_skcipher_request(cryp->engine, cryp->req.sreq, in starfive_aes_finish_req()
365 struct starfive_cryp_dev *cryp = (struct starfive_cryp_dev *)param; in starfive_aes_done_task() local
371 block[i] = readl(cryp->base + STARFIVE_AES_AESDIO0R); in starfive_aes_done_task()
373 scatterwalk_copychunks(block, &cryp->out_walk, min_t(size_t, AES_BLOCK_SIZE, in starfive_aes_done_task()
374 cryp->total_out), 1); in starfive_aes_done_task()
376 cryp->total_out -= min_t(size_t, AES_BLOCK_SIZE, cryp->total_out); in starfive_aes_done_task()
378 if (!cryp->total_out) { in starfive_aes_done_task()
379 starfive_aes_finish_req(cryp); in starfive_aes_done_task()
384 scatterwalk_copychunks(block, &cryp->in_walk, min_t(size_t, AES_BLOCK_SIZE, in starfive_aes_done_task()
385 cryp->total_in), 0); in starfive_aes_done_task()
386 cryp->total_in -= min_t(size_t, AES_BLOCK_SIZE, cryp->total_in); in starfive_aes_done_task()
389 writel(block[i], cryp->base + STARFIVE_AES_AESDIO0R); in starfive_aes_done_task()
391 stat = readl(cryp->base + STARFIVE_IE_MASK_OFFSET); in starfive_aes_done_task()
393 writel(stat, cryp->base + STARFIVE_IE_MASK_OFFSET); in starfive_aes_done_task()
398 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_gcm_write_adata() local
403 total_len = ALIGN(cryp->assoclen, AES_BLOCK_SIZE) / sizeof(unsigned int); in starfive_aes_gcm_write_adata()
407 writel(*buffer, cryp->base + STARFIVE_AES_NONCE0); in starfive_aes_gcm_write_adata()
409 writel(*buffer, cryp->base + STARFIVE_AES_NONCE1); in starfive_aes_gcm_write_adata()
411 writel(*buffer, cryp->base + STARFIVE_AES_NONCE2); in starfive_aes_gcm_write_adata()
413 writel(*buffer, cryp->base + STARFIVE_AES_NONCE3); in starfive_aes_gcm_write_adata()
417 if (starfive_aes_wait_gcmdone(cryp)) in starfive_aes_gcm_write_adata()
418 return dev_err_probe(cryp->dev, -ETIMEDOUT, in starfive_aes_gcm_write_adata()
426 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_ccm_write_adata() local
432 total_len = cryp->assoclen; in starfive_aes_ccm_write_adata()
435 writeb(*ci, cryp->base + STARFIVE_AES_AESDIO0R); in starfive_aes_ccm_write_adata()
437 writeb(*ci, cryp->base + STARFIVE_AES_AESDIO0R); in starfive_aes_ccm_write_adata()
443 writel(*buffer, cryp->base + STARFIVE_AES_AESDIO0R); in starfive_aes_ccm_write_adata()
449 writel(*buffer, cryp->base + STARFIVE_AES_AESDIO0R); in starfive_aes_ccm_write_adata()
454 if (starfive_aes_wait_busy(cryp)) in starfive_aes_ccm_write_adata()
455 return dev_err_probe(cryp->dev, -ETIMEDOUT, in starfive_aes_ccm_write_adata()
466 struct starfive_cryp_dev *cryp; in starfive_aes_prepare_req() local
474 cryp = ctx->cryp; in starfive_aes_prepare_req()
478 cryp->req.sreq = req; in starfive_aes_prepare_req()
479 cryp->total_in = req->cryptlen; in starfive_aes_prepare_req()
480 cryp->total_out = req->cryptlen; in starfive_aes_prepare_req()
481 cryp->assoclen = 0; in starfive_aes_prepare_req()
482 cryp->authsize = 0; in starfive_aes_prepare_req()
484 cryp->req.areq = areq; in starfive_aes_prepare_req()
485 cryp->assoclen = areq->assoclen; in starfive_aes_prepare_req()
486 cryp->authsize = crypto_aead_authsize(crypto_aead_reqtfm(areq)); in starfive_aes_prepare_req()
487 if (is_encrypt(cryp)) { in starfive_aes_prepare_req()
488 cryp->total_in = areq->cryptlen; in starfive_aes_prepare_req()
489 cryp->total_out = areq->cryptlen; in starfive_aes_prepare_req()
491 cryp->total_in = areq->cryptlen - cryp->authsize; in starfive_aes_prepare_req()
492 cryp->total_out = cryp->total_in; in starfive_aes_prepare_req()
497 scatterwalk_start(&cryp->in_walk, rctx->in_sg); in starfive_aes_prepare_req()
500 scatterwalk_start(&cryp->out_walk, rctx->out_sg); in starfive_aes_prepare_req()
502 if (cryp->assoclen) { in starfive_aes_prepare_req()
503 rctx->adata = kzalloc(ALIGN(cryp->assoclen, AES_BLOCK_SIZE), GFP_KERNEL); in starfive_aes_prepare_req()
505 return dev_err_probe(cryp->dev, -ENOMEM, in starfive_aes_prepare_req()
508 scatterwalk_copychunks(rctx->adata, &cryp->in_walk, cryp->assoclen, 0); in starfive_aes_prepare_req()
509 scatterwalk_copychunks(NULL, &cryp->out_walk, cryp->assoclen, 2); in starfive_aes_prepare_req()
523 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_do_one_req() local
537 scatterwalk_copychunks(block, &cryp->in_walk, min_t(size_t, AES_BLOCK_SIZE, in starfive_aes_do_one_req()
538 cryp->total_in), 0); in starfive_aes_do_one_req()
539 cryp->total_in -= min_t(size_t, AES_BLOCK_SIZE, cryp->total_in); in starfive_aes_do_one_req()
542 writel(block[i], cryp->base + STARFIVE_AES_AESDIO0R); in starfive_aes_do_one_req()
544 stat = readl(cryp->base + STARFIVE_IE_MASK_OFFSET); in starfive_aes_do_one_req()
546 writel(stat, cryp->base + STARFIVE_IE_MASK_OFFSET); in starfive_aes_do_one_req()
555 ctx->cryp = starfive_cryp_find_dev(ctx); in starfive_aes_init_tfm()
556 if (!ctx->cryp) in starfive_aes_init_tfm()
571 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_aead_do_one_req() local
582 if (!cryp->assoclen) in starfive_aes_aead_do_one_req()
585 if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CCM) in starfive_aes_aead_do_one_req()
586 cryp->err = starfive_aes_ccm_write_adata(ctx); in starfive_aes_aead_do_one_req()
588 cryp->err = starfive_aes_gcm_write_adata(ctx); in starfive_aes_aead_do_one_req()
592 if (cryp->err) in starfive_aes_aead_do_one_req()
593 return cryp->err; in starfive_aes_aead_do_one_req()
596 if (!cryp->total_in) in starfive_aes_aead_do_one_req()
603 scatterwalk_copychunks(block, &cryp->in_walk, min_t(size_t, AES_BLOCK_SIZE, in starfive_aes_aead_do_one_req()
604 cryp->total_in), 0); in starfive_aes_aead_do_one_req()
605 cryp->total_in -= min_t(size_t, AES_BLOCK_SIZE, cryp->total_in); in starfive_aes_aead_do_one_req()
608 writel(block[i], cryp->base + STARFIVE_AES_AESDIO0R); in starfive_aes_aead_do_one_req()
610 stat = readl(cryp->base + STARFIVE_IE_MASK_OFFSET); in starfive_aes_aead_do_one_req()
612 writel(stat, cryp->base + STARFIVE_IE_MASK_OFFSET); in starfive_aes_aead_do_one_req()
617 starfive_aes_finish_req(cryp); in starfive_aes_aead_do_one_req()
624 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_aead_init_tfm() local
628 ctx->cryp = starfive_cryp_find_dev(ctx); in starfive_aes_aead_init_tfm()
629 if (!ctx->cryp) in starfive_aes_aead_init_tfm()
636 return dev_err_probe(cryp->dev, PTR_ERR(ctx->aead_fbk), in starfive_aes_aead_init_tfm()
658 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_crypt() local
661 cryp->flags = flags; in starfive_aes_crypt()
663 if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_ECB || in starfive_aes_crypt()
664 (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CBC) in starfive_aes_crypt()
668 return crypto_transfer_skcipher_request_to_engine(cryp->engine, req); in starfive_aes_crypt()
674 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_aes_aead_crypt() local
676 cryp->flags = flags; in starfive_aes_aead_crypt()
682 if (ctx->aead_fbk && !is_encrypt(cryp)) { in starfive_aes_aead_crypt()
695 return crypto_transfer_aead_request_to_engine(cryp->engine, req); in starfive_aes_aead_crypt()