Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * sun8i-ce-cipher.c - hardware cryptographic offloader for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Allwinner H3/A64/H5/H2+/H6/R40 SoC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2016-2019 Corentin LABBE <clabbe.montjoie@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * This file add support for AES cipher with 128,192,256 bits keysize in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * CBC and ECB mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * You could find a link for the datasheet in Documentation/arm/sunxi.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/bottom_half.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <crypto/internal/des.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "sun8i-ce.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int sun8i_ce_cipher_need_fallback(struct skcipher_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	if (sg_nents(areq->src) > MAX_SG || sg_nents(areq->dst) > MAX_SG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (areq->cryptlen < crypto_skcipher_ivsize(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (areq->cryptlen == 0 || areq->cryptlen % 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	sg = areq->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	while (sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		if (sg->length % 4 || !IS_ALIGNED(sg->offset, sizeof(u32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	sg = areq->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	while (sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		if (sg->length % 4 || !IS_ALIGNED(sg->offset, sizeof(u32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static int sun8i_ce_cipher_fallback(struct skcipher_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct sun8i_ce_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	algt->stat_fb++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 				      areq->base.complete, areq->base.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				   areq->cryptlen, areq->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (rctx->op_dir & CE_DECRYPTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		err = crypto_skcipher_decrypt(&rctx->fallback_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		err = crypto_skcipher_encrypt(&rctx->fallback_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static int sun8i_ce_cipher_prepare(struct crypto_engine *engine, void *async_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct skcipher_request *areq = container_of(async_req, struct skcipher_request, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	struct sun8i_ce_dev *ce = op->ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct sun8i_ce_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct sun8i_ce_flow *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct ce_task *cet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	unsigned int todo, len, offset, ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	u32 common, sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int flow, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	int nr_sgs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int nr_sgd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	dev_dbg(ce->dev, "%s %s %u %x IV(%p %u) key=%u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		crypto_tfm_alg_name(areq->base.tfm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		areq->cryptlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		rctx->op_dir, areq->iv, crypto_skcipher_ivsize(tfm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		op->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	algt->stat_req++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	flow = rctx->flow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	chan = &ce->chanlist[flow];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	cet = chan->tl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	memset(cet, 0, sizeof(struct ce_task));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	cet->t_id = cpu_to_le32(flow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	common = ce->variant->alg_cipher[algt->ce_algo_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	common |= rctx->op_dir | CE_COMM_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	cet->t_common_ctl = cpu_to_le32(common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* CTS and recent CE (H6) need length in bytes, in word otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (ce->variant->cipher_t_dlen_in_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		cet->t_dlen = cpu_to_le32(areq->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		cet->t_dlen = cpu_to_le32(areq->cryptlen / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	sym = ce->variant->op_mode[algt->ce_blockmode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	len = op->keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	switch (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	case 128 / 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		sym |= CE_AES_128BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	case 192 / 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		sym |= CE_AES_192BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	case 256 / 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		sym |= CE_AES_256BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	cet->t_sym_ctl = cpu_to_le32(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	cet->t_asym_ctl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	rctx->addr_key = dma_map_single(ce->dev, op->key, op->keylen, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (dma_mapping_error(ce->dev, rctx->addr_key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		dev_err(ce->dev, "Cannot DMA MAP KEY\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		goto theend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	cet->t_key = cpu_to_le32(rctx->addr_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	ivsize = crypto_skcipher_ivsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		rctx->ivlen = ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		rctx->bounce_iv = kzalloc(ivsize, GFP_KERNEL | GFP_DMA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (!rctx->bounce_iv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			goto theend_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		if (rctx->op_dir & CE_DECRYPTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			rctx->backup_iv = kzalloc(ivsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			if (!rctx->backup_iv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				goto theend_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			offset = areq->cryptlen - ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			scatterwalk_map_and_copy(rctx->backup_iv, areq->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 						 offset, ivsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		memcpy(rctx->bounce_iv, areq->iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		rctx->addr_iv = dma_map_single(ce->dev, rctx->bounce_iv, rctx->ivlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 					       DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		if (dma_mapping_error(ce->dev, rctx->addr_iv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			dev_err(ce->dev, "Cannot DMA MAP IV\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			goto theend_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		cet->t_iv = cpu_to_le32(rctx->addr_iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (areq->src == areq->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				    DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			goto theend_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		nr_sgd = nr_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				    DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			goto theend_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		nr_sgd = dma_map_sg(ce->dev, areq->dst, sg_nents(areq->dst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				    DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (nr_sgd <= 0 || nr_sgd > MAX_SG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			dev_err(ce->dev, "Invalid sg number %d\n", nr_sgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			goto theend_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	len = areq->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	for_each_sg(areq->src, sg, nr_sgs, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		cet->t_src[i].addr = cpu_to_le32(sg_dma_address(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		todo = min(len, sg_dma_len(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		cet->t_src[i].len = cpu_to_le32(todo / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			areq->cryptlen, i, cet->t_src[i].len, sg->offset, todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		len -= todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		dev_err(ce->dev, "remaining len %d\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		goto theend_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	len = areq->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	for_each_sg(areq->dst, sg, nr_sgd, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		cet->t_dst[i].addr = cpu_to_le32(sg_dma_address(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		todo = min(len, sg_dma_len(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		cet->t_dst[i].len = cpu_to_le32(todo / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			areq->cryptlen, i, cet->t_dst[i].len, sg->offset, todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		len -= todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		dev_err(ce->dev, "remaining len %d\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		goto theend_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	chan->timeout = areq->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	rctx->nr_sgs = nr_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	rctx->nr_sgd = nr_sgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) theend_sgs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (areq->src == areq->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		if (nr_sgs > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		dma_unmap_sg(ce->dev, areq->dst, nr_sgd, DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) theend_iv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (areq->iv && ivsize > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		if (rctx->addr_iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			dma_unmap_single(ce->dev, rctx->addr_iv, rctx->ivlen, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		offset = areq->cryptlen - ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (rctx->op_dir & CE_DECRYPTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			memcpy(areq->iv, rctx->backup_iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			kfree_sensitive(rctx->backup_iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 						 ivsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		kfree(rctx->bounce_iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) theend_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	dma_unmap_single(ce->dev, rctx->addr_key, op->keylen, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) theend:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int sun8i_ce_cipher_run(struct crypto_engine *engine, void *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	struct skcipher_request *breq = container_of(areq, struct skcipher_request, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(breq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct sun8i_ce_dev *ce = op->ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(breq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	int flow, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	flow = rctx->flow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(breq->base.tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	crypto_finalize_skcipher_request(engine, breq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int sun8i_ce_cipher_unprepare(struct crypto_engine *engine, void *async_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	struct skcipher_request *areq = container_of(async_req, struct skcipher_request, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	struct sun8i_ce_dev *ce = op->ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	struct sun8i_ce_flow *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct ce_task *cet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	unsigned int ivsize, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	int nr_sgs = rctx->nr_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	int nr_sgd = rctx->nr_sgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	int flow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	flow = rctx->flow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	chan = &ce->chanlist[flow];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	cet = chan->tl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	ivsize = crypto_skcipher_ivsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (areq->src == areq->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		if (nr_sgs > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		dma_unmap_sg(ce->dev, areq->dst, nr_sgd, DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (areq->iv && ivsize > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		if (cet->t_iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			dma_unmap_single(ce->dev, rctx->addr_iv, rctx->ivlen, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		offset = areq->cryptlen - ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		if (rctx->op_dir & CE_DECRYPTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			memcpy(areq->iv, rctx->backup_iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			kfree_sensitive(rctx->backup_iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 						 ivsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		kfree(rctx->bounce_iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	dma_unmap_single(ce->dev, rctx->addr_key, op->keylen, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int sun8i_ce_skdecrypt(struct skcipher_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	struct crypto_engine *engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	int e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	rctx->op_dir = CE_DECRYPTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (sun8i_ce_cipher_need_fallback(areq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		return sun8i_ce_cipher_fallback(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	e = sun8i_ce_get_engine_number(op->ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	rctx->flow = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	engine = op->ce->chanlist[e].engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	return crypto_transfer_skcipher_request_to_engine(engine, areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) int sun8i_ce_skencrypt(struct skcipher_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct crypto_engine *engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	int e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	rctx->op_dir = CE_ENCRYPTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (sun8i_ce_cipher_need_fallback(areq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		return sun8i_ce_cipher_fallback(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	e = sun8i_ce_get_engine_number(op->ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	rctx->flow = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	engine = op->ce->chanlist[e].engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	return crypto_transfer_skcipher_request_to_engine(engine, areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) int sun8i_ce_cipher_init(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	struct sun8i_ce_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	const char *name = crypto_tfm_alg_name(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	struct crypto_skcipher *sktfm = __crypto_skcipher_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	struct skcipher_alg *alg = crypto_skcipher_alg(sktfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	memset(op, 0, sizeof(struct sun8i_cipher_tfm_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	op->ce = algt->ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (IS_ERR(op->fallback_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		dev_err(op->ce->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			name, PTR_ERR(op->fallback_tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		return PTR_ERR(op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	sktfm->reqsize = sizeof(struct sun8i_cipher_req_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			 crypto_skcipher_reqsize(op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	dev_info(op->ce->dev, "Fallback for %s is %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		 crypto_tfm_alg_driver_name(&sktfm->base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(op->fallback_tfm)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	op->enginectx.op.do_one_request = sun8i_ce_cipher_run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	op->enginectx.op.prepare_request = sun8i_ce_cipher_prepare;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	op->enginectx.op.unprepare_request = sun8i_ce_cipher_unprepare;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	err = pm_runtime_get_sync(op->ce->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		goto error_pm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) error_pm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	pm_runtime_put_noidle(op->ce->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	crypto_free_skcipher(op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) void sun8i_ce_cipher_exit(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	kfree_sensitive(op->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	crypto_free_skcipher(op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	pm_runtime_put_sync_suspend(op->ce->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	struct sun8i_ce_dev *ce = op->ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	switch (keylen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	case 128 / 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	case 192 / 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	case 256 / 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		dev_dbg(ce->dev, "ERROR: Invalid keylen %u\n", keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	kfree_sensitive(op->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	op->keylen = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	if (!op->key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			 unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	err = verify_skcipher_des3_key(tfm, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	kfree_sensitive(op->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	op->keylen = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (!op->key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }