You've already forked linux-rockchip
mirror of
https://github.com/armbian/linux-rockchip.git
synced 2026-01-06 11:08:10 -08:00
crypto: allwinner - Add sun8i-ce Crypto Engine
The Crypto Engine is an hardware cryptographic offloader present on all recent Allwinner SoCs H2+, H3, R40, A64, H5, H6 This driver supports AES cipher in CBC/ECB mode. Acked-by: Maxime Ripard <mripard@kernel.org> Signed-off-by: Corentin Labbe <clabbe.montjoie@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
committed by
Herbert Xu
parent
3914b93115
commit
06f751b613
@@ -4,3 +4,30 @@ config CRYPTO_DEV_ALLWINNER
|
||||
default y if ARCH_SUNXI
|
||||
help
|
||||
Say Y here to get to see options for Allwinner hardware crypto devices
|
||||
|
||||
config CRYPTO_DEV_SUN8I_CE
|
||||
tristate "Support for Allwinner Crypto Engine cryptographic offloader"
|
||||
select CRYPTO_BLKCIPHER
|
||||
select CRYPTO_ENGINE
|
||||
select CRYPTO_ECB
|
||||
select CRYPTO_CBC
|
||||
select CRYPTO_AES
|
||||
select CRYPTO_DES
|
||||
depends on CRYPTO_DEV_ALLWINNER
|
||||
depends on PM
|
||||
help
|
||||
Select y here to have support for the crypto Engine availlable on
|
||||
Allwinner SoC H2+, H3, H5, H6, R40 and A64.
|
||||
The Crypto Engine handle AES/3DES ciphers in ECB/CBC mode.
|
||||
|
||||
To compile this driver as a module, choose M here: the module
|
||||
will be called sun8i-ce.
|
||||
|
||||
config CRYPTO_DEV_SUN8I_CE_DEBUG
|
||||
bool "Enable sun8i-ce stats"
|
||||
depends on CRYPTO_DEV_SUN8I_CE
|
||||
depends on DEBUG_FS
|
||||
help
|
||||
Say y to enable sun8i-ce debug stats.
|
||||
This will create /sys/kernel/debug/sun8i-ce/stats for displaying
|
||||
the number of requests per flow and per algorithm.
|
||||
|
||||
1
drivers/crypto/allwinner/Makefile
Normal file
1
drivers/crypto/allwinner/Makefile
Normal file
@@ -0,0 +1 @@
|
||||
obj-$(CONFIG_CRYPTO_DEV_SUN8I_CE) += sun8i-ce/
|
||||
2
drivers/crypto/allwinner/sun8i-ce/Makefile
Normal file
2
drivers/crypto/allwinner/sun8i-ce/Makefile
Normal file
@@ -0,0 +1,2 @@
|
||||
obj-$(CONFIG_CRYPTO_DEV_SUN8I_CE) += sun8i-ce.o
|
||||
sun8i-ce-y += sun8i-ce-core.o sun8i-ce-cipher.o
|
||||
434
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c
Normal file
434
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c
Normal file
@@ -0,0 +1,434 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* sun8i-ce-cipher.c - hardware cryptographic offloader for
|
||||
* Allwinner H3/A64/H5/H2+/H6/R40 SoC
|
||||
*
|
||||
* Copyright (C) 2016-2019 Corentin LABBE <clabbe.montjoie@gmail.com>
|
||||
*
|
||||
* This file add support for AES cipher with 128,192,256 bits keysize in
|
||||
* CBC and ECB mode.
|
||||
*
|
||||
* You could find a link for the datasheet in Documentation/arm/sunxi/README
|
||||
*/
|
||||
|
||||
#include <linux/crypto.h>
|
||||
#include <linux/dma-mapping.h>
|
||||
#include <linux/io.h>
|
||||
#include <linux/pm_runtime.h>
|
||||
#include <crypto/scatterwalk.h>
|
||||
#include <crypto/internal/des.h>
|
||||
#include <crypto/internal/skcipher.h>
|
||||
#include "sun8i-ce.h"
|
||||
|
||||
static int sun8i_ce_cipher_need_fallback(struct skcipher_request *areq)
|
||||
{
|
||||
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
||||
struct scatterlist *sg;
|
||||
|
||||
if (sg_nents(areq->src) > MAX_SG || sg_nents(areq->dst) > MAX_SG)
|
||||
return true;
|
||||
|
||||
if (areq->cryptlen < crypto_skcipher_ivsize(tfm))
|
||||
return true;
|
||||
|
||||
if (areq->cryptlen == 0 || areq->cryptlen % 16)
|
||||
return true;
|
||||
|
||||
sg = areq->src;
|
||||
while (sg) {
|
||||
if (sg->length % 4 || !IS_ALIGNED(sg->offset, sizeof(u32)))
|
||||
return true;
|
||||
sg = sg_next(sg);
|
||||
}
|
||||
sg = areq->dst;
|
||||
while (sg) {
|
||||
if (sg->length % 4 || !IS_ALIGNED(sg->offset, sizeof(u32)))
|
||||
return true;
|
||||
sg = sg_next(sg);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int sun8i_ce_cipher_fallback(struct skcipher_request *areq)
|
||||
{
|
||||
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
||||
struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
||||
struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
||||
int err;
|
||||
#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
|
||||
struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
|
||||
struct sun8i_ce_alg_template *algt;
|
||||
#endif
|
||||
SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, op->fallback_tfm);
|
||||
|
||||
#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
|
||||
algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher);
|
||||
algt->stat_fb++;
|
||||
#endif
|
||||
|
||||
skcipher_request_set_sync_tfm(subreq, op->fallback_tfm);
|
||||
skcipher_request_set_callback(subreq, areq->base.flags, NULL, NULL);
|
||||
skcipher_request_set_crypt(subreq, areq->src, areq->dst,
|
||||
areq->cryptlen, areq->iv);
|
||||
if (rctx->op_dir & CE_DECRYPTION)
|
||||
err = crypto_skcipher_decrypt(subreq);
|
||||
else
|
||||
err = crypto_skcipher_encrypt(subreq);
|
||||
skcipher_request_zero(subreq);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int sun8i_ce_cipher(struct skcipher_request *areq)
|
||||
{
|
||||
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
||||
struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
||||
struct sun8i_ce_dev *ce = op->ce;
|
||||
struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
||||
struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
|
||||
struct sun8i_ce_alg_template *algt;
|
||||
struct sun8i_ce_flow *chan;
|
||||
struct ce_task *cet;
|
||||
struct scatterlist *sg;
|
||||
unsigned int todo, len, offset, ivsize;
|
||||
void *backup_iv = NULL;
|
||||
int flow, i;
|
||||
int nr_sgs = 0;
|
||||
int nr_sgd = 0;
|
||||
int err = 0;
|
||||
|
||||
algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher);
|
||||
|
||||
dev_dbg(ce->dev, "%s %s %u %x IV(%p %u) key=%u\n", __func__,
|
||||
crypto_tfm_alg_name(areq->base.tfm),
|
||||
areq->cryptlen,
|
||||
rctx->op_dir, areq->iv, crypto_skcipher_ivsize(tfm),
|
||||
op->keylen);
|
||||
|
||||
#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
|
||||
algt->stat_req++;
|
||||
#endif
|
||||
|
||||
flow = rctx->flow;
|
||||
|
||||
chan = &ce->chanlist[flow];
|
||||
|
||||
cet = chan->tl;
|
||||
memset(cet, 0, sizeof(struct ce_task));
|
||||
|
||||
cet->t_id = flow;
|
||||
cet->t_common_ctl = ce->variant->alg_cipher[algt->ce_algo_id];
|
||||
cet->t_common_ctl |= rctx->op_dir | CE_COMM_INT;
|
||||
cet->t_dlen = areq->cryptlen / 4;
|
||||
/* CTS and recent CE (H6) need length in bytes, in word otherwise */
|
||||
if (ce->variant->has_t_dlen_in_bytes)
|
||||
cet->t_dlen = areq->cryptlen;
|
||||
|
||||
cet->t_sym_ctl = ce->variant->op_mode[algt->ce_blockmode];
|
||||
len = op->keylen;
|
||||
switch (len) {
|
||||
case 128 / 8:
|
||||
cet->t_sym_ctl |= CE_AES_128BITS;
|
||||
break;
|
||||
case 192 / 8:
|
||||
cet->t_sym_ctl |= CE_AES_192BITS;
|
||||
break;
|
||||
case 256 / 8:
|
||||
cet->t_sym_ctl |= CE_AES_256BITS;
|
||||
break;
|
||||
}
|
||||
|
||||
cet->t_asym_ctl = 0;
|
||||
|
||||
chan->op_mode = ce->variant->op_mode[algt->ce_blockmode];
|
||||
chan->op_dir = rctx->op_dir;
|
||||
chan->method = ce->variant->alg_cipher[algt->ce_algo_id];
|
||||
chan->keylen = op->keylen;
|
||||
|
||||
cet->t_key = dma_map_single(ce->dev, op->key, op->keylen,
|
||||
DMA_TO_DEVICE);
|
||||
if (dma_mapping_error(ce->dev, cet->t_key)) {
|
||||
dev_err(ce->dev, "Cannot DMA MAP KEY\n");
|
||||
err = -EFAULT;
|
||||
goto theend;
|
||||
}
|
||||
|
||||
ivsize = crypto_skcipher_ivsize(tfm);
|
||||
if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) {
|
||||
chan->ivlen = ivsize;
|
||||
chan->bounce_iv = kzalloc(ivsize, GFP_KERNEL | GFP_DMA);
|
||||
if (!chan->bounce_iv) {
|
||||
err = -ENOMEM;
|
||||
goto theend_key;
|
||||
}
|
||||
if (rctx->op_dir & CE_DECRYPTION) {
|
||||
backup_iv = kzalloc(ivsize, GFP_KERNEL);
|
||||
if (!backup_iv) {
|
||||
err = -ENOMEM;
|
||||
goto theend_key;
|
||||
}
|
||||
offset = areq->cryptlen - ivsize;
|
||||
scatterwalk_map_and_copy(backup_iv, areq->src, offset,
|
||||
ivsize, 0);
|
||||
}
|
||||
memcpy(chan->bounce_iv, areq->iv, ivsize);
|
||||
cet->t_iv = dma_map_single(ce->dev, chan->bounce_iv,
|
||||
chan->ivlen, DMA_TO_DEVICE);
|
||||
if (dma_mapping_error(ce->dev, cet->t_iv)) {
|
||||
dev_err(ce->dev, "Cannot DMA MAP IV\n");
|
||||
err = -ENOMEM;
|
||||
goto theend_iv;
|
||||
}
|
||||
}
|
||||
|
||||
if (areq->src == areq->dst) {
|
||||
nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src),
|
||||
DMA_BIDIRECTIONAL);
|
||||
if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
|
||||
dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
|
||||
err = -EINVAL;
|
||||
goto theend_iv;
|
||||
}
|
||||
nr_sgd = nr_sgs;
|
||||
} else {
|
||||
nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src),
|
||||
DMA_TO_DEVICE);
|
||||
if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
|
||||
dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
|
||||
err = -EINVAL;
|
||||
goto theend_iv;
|
||||
}
|
||||
nr_sgd = dma_map_sg(ce->dev, areq->dst, sg_nents(areq->dst),
|
||||
DMA_FROM_DEVICE);
|
||||
if (nr_sgd <= 0 || nr_sgd > MAX_SG) {
|
||||
dev_err(ce->dev, "Invalid sg number %d\n", nr_sgd);
|
||||
err = -EINVAL;
|
||||
goto theend_sgs;
|
||||
}
|
||||
}
|
||||
|
||||
len = areq->cryptlen;
|
||||
for_each_sg(areq->src, sg, nr_sgs, i) {
|
||||
cet->t_src[i].addr = sg_dma_address(sg);
|
||||
todo = min(len, sg_dma_len(sg));
|
||||
cet->t_src[i].len = todo / 4;
|
||||
dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__,
|
||||
areq->cryptlen, i, cet->t_src[i].len, sg->offset, todo);
|
||||
len -= todo;
|
||||
}
|
||||
if (len > 0) {
|
||||
dev_err(ce->dev, "remaining len %d\n", len);
|
||||
err = -EINVAL;
|
||||
goto theend_sgs;
|
||||
}
|
||||
|
||||
len = areq->cryptlen;
|
||||
for_each_sg(areq->dst, sg, nr_sgd, i) {
|
||||
cet->t_dst[i].addr = sg_dma_address(sg);
|
||||
todo = min(len, sg_dma_len(sg));
|
||||
cet->t_dst[i].len = todo / 4;
|
||||
dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__,
|
||||
areq->cryptlen, i, cet->t_dst[i].len, sg->offset, todo);
|
||||
len -= todo;
|
||||
}
|
||||
if (len > 0) {
|
||||
dev_err(ce->dev, "remaining len %d\n", len);
|
||||
err = -EINVAL;
|
||||
goto theend_sgs;
|
||||
}
|
||||
|
||||
chan->timeout = areq->cryptlen;
|
||||
err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm));
|
||||
|
||||
theend_sgs:
|
||||
if (areq->src == areq->dst) {
|
||||
dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_BIDIRECTIONAL);
|
||||
} else {
|
||||
if (nr_sgs > 0)
|
||||
dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_TO_DEVICE);
|
||||
dma_unmap_sg(ce->dev, areq->dst, nr_sgd, DMA_FROM_DEVICE);
|
||||
}
|
||||
|
||||
theend_iv:
|
||||
if (areq->iv && ivsize > 0) {
|
||||
if (cet->t_iv)
|
||||
dma_unmap_single(ce->dev, cet->t_iv, chan->ivlen,
|
||||
DMA_TO_DEVICE);
|
||||
offset = areq->cryptlen - ivsize;
|
||||
if (rctx->op_dir & CE_DECRYPTION) {
|
||||
memcpy(areq->iv, backup_iv, ivsize);
|
||||
kzfree(backup_iv);
|
||||
} else {
|
||||
scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
|
||||
ivsize, 0);
|
||||
}
|
||||
kfree(chan->bounce_iv);
|
||||
}
|
||||
|
||||
theend_key:
|
||||
dma_unmap_single(ce->dev, cet->t_key, op->keylen, DMA_TO_DEVICE);
|
||||
|
||||
theend:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int sun8i_ce_handle_cipher_request(struct crypto_engine *engine, void *areq)
|
||||
{
|
||||
int err;
|
||||
struct skcipher_request *breq = container_of(areq, struct skcipher_request, base);
|
||||
|
||||
err = sun8i_ce_cipher(breq);
|
||||
crypto_finalize_skcipher_request(engine, breq, err);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sun8i_ce_skdecrypt(struct skcipher_request *areq)
|
||||
{
|
||||
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
||||
struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
||||
struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
||||
struct crypto_engine *engine;
|
||||
int e;
|
||||
|
||||
rctx->op_dir = CE_DECRYPTION;
|
||||
if (sun8i_ce_cipher_need_fallback(areq))
|
||||
return sun8i_ce_cipher_fallback(areq);
|
||||
|
||||
e = sun8i_ce_get_engine_number(op->ce);
|
||||
rctx->flow = e;
|
||||
engine = op->ce->chanlist[e].engine;
|
||||
|
||||
return crypto_transfer_skcipher_request_to_engine(engine, areq);
|
||||
}
|
||||
|
||||
int sun8i_ce_skencrypt(struct skcipher_request *areq)
|
||||
{
|
||||
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
||||
struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
||||
struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
||||
struct crypto_engine *engine;
|
||||
int e;
|
||||
|
||||
rctx->op_dir = CE_ENCRYPTION;
|
||||
if (sun8i_ce_cipher_need_fallback(areq))
|
||||
return sun8i_ce_cipher_fallback(areq);
|
||||
|
||||
e = sun8i_ce_get_engine_number(op->ce);
|
||||
rctx->flow = e;
|
||||
engine = op->ce->chanlist[e].engine;
|
||||
|
||||
return crypto_transfer_skcipher_request_to_engine(engine, areq);
|
||||
}
|
||||
|
||||
int sun8i_ce_cipher_init(struct crypto_tfm *tfm)
|
||||
{
|
||||
struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
|
||||
struct sun8i_ce_alg_template *algt;
|
||||
const char *name = crypto_tfm_alg_name(tfm);
|
||||
struct crypto_skcipher *sktfm = __crypto_skcipher_cast(tfm);
|
||||
struct skcipher_alg *alg = crypto_skcipher_alg(sktfm);
|
||||
int err;
|
||||
|
||||
memset(op, 0, sizeof(struct sun8i_cipher_tfm_ctx));
|
||||
|
||||
algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher);
|
||||
op->ce = algt->ce;
|
||||
|
||||
sktfm->reqsize = sizeof(struct sun8i_cipher_req_ctx);
|
||||
|
||||
op->fallback_tfm = crypto_alloc_sync_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
|
||||
if (IS_ERR(op->fallback_tfm)) {
|
||||
dev_err(op->ce->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
|
||||
name, PTR_ERR(op->fallback_tfm));
|
||||
return PTR_ERR(op->fallback_tfm);
|
||||
}
|
||||
|
||||
dev_info(op->ce->dev, "Fallback for %s is %s\n",
|
||||
crypto_tfm_alg_driver_name(&sktfm->base),
|
||||
crypto_tfm_alg_driver_name(crypto_skcipher_tfm(&op->fallback_tfm->base)));
|
||||
|
||||
op->enginectx.op.do_one_request = sun8i_ce_handle_cipher_request;
|
||||
op->enginectx.op.prepare_request = NULL;
|
||||
op->enginectx.op.unprepare_request = NULL;
|
||||
|
||||
err = pm_runtime_get_sync(op->ce->dev);
|
||||
if (err < 0)
|
||||
goto error_pm;
|
||||
|
||||
return 0;
|
||||
error_pm:
|
||||
crypto_free_sync_skcipher(op->fallback_tfm);
|
||||
return err;
|
||||
}
|
||||
|
||||
void sun8i_ce_cipher_exit(struct crypto_tfm *tfm)
|
||||
{
|
||||
struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
|
||||
|
||||
if (op->key) {
|
||||
memzero_explicit(op->key, op->keylen);
|
||||
kfree(op->key);
|
||||
}
|
||||
crypto_free_sync_skcipher(op->fallback_tfm);
|
||||
pm_runtime_put_sync_suspend(op->ce->dev);
|
||||
}
|
||||
|
||||
int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keylen)
|
||||
{
|
||||
struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
||||
struct sun8i_ce_dev *ce = op->ce;
|
||||
|
||||
switch (keylen) {
|
||||
case 128 / 8:
|
||||
break;
|
||||
case 192 / 8:
|
||||
break;
|
||||
case 256 / 8:
|
||||
break;
|
||||
default:
|
||||
dev_dbg(ce->dev, "ERROR: Invalid keylen %u\n", keylen);
|
||||
crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (op->key) {
|
||||
memzero_explicit(op->key, op->keylen);
|
||||
kfree(op->key);
|
||||
}
|
||||
op->keylen = keylen;
|
||||
op->key = kmalloc(keylen, GFP_KERNEL | GFP_DMA);
|
||||
if (!op->key)
|
||||
return -ENOMEM;
|
||||
memcpy(op->key, key, keylen);
|
||||
|
||||
crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
|
||||
crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
|
||||
|
||||
return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen);
|
||||
}
|
||||
|
||||
int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keylen)
|
||||
{
|
||||
struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
||||
int err;
|
||||
|
||||
err = verify_skcipher_des3_key(tfm, key);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
if (op->key) {
|
||||
memzero_explicit(op->key, op->keylen);
|
||||
kfree(op->key);
|
||||
}
|
||||
op->keylen = keylen;
|
||||
op->key = kmalloc(keylen, GFP_KERNEL | GFP_DMA);
|
||||
if (!op->key)
|
||||
return -ENOMEM;
|
||||
memcpy(op->key, key, keylen);
|
||||
|
||||
crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
|
||||
crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
|
||||
|
||||
return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen);
|
||||
}
|
||||
676
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c
Normal file
676
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c
Normal file
File diff suppressed because it is too large
Load Diff
254
drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h
Normal file
254
drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h
Normal file
@@ -0,0 +1,254 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
/*
|
||||
* sun8i-ce.h - hardware cryptographic offloader for
|
||||
* Allwinner H3/A64/H5/H2+/H6 SoC
|
||||
*
|
||||
* Copyright (C) 2016-2019 Corentin LABBE <clabbe.montjoie@gmail.com>
|
||||
*/
|
||||
#include <crypto/aes.h>
|
||||
#include <crypto/des.h>
|
||||
#include <crypto/engine.h>
|
||||
#include <crypto/skcipher.h>
|
||||
#include <linux/atomic.h>
|
||||
#include <linux/debugfs.h>
|
||||
#include <linux/crypto.h>
|
||||
|
||||
/* CE Registers */
|
||||
#define CE_TDQ 0x00
|
||||
#define CE_CTR 0x04
|
||||
#define CE_ICR 0x08
|
||||
#define CE_ISR 0x0C
|
||||
#define CE_TLR 0x10
|
||||
#define CE_TSR 0x14
|
||||
#define CE_ESR 0x18
|
||||
#define CE_CSSGR 0x1C
|
||||
#define CE_CDSGR 0x20
|
||||
#define CE_CSAR 0x24
|
||||
#define CE_CDAR 0x28
|
||||
#define CE_TPR 0x2C
|
||||
|
||||
/* Used in struct ce_task */
|
||||
/* ce_task common */
|
||||
#define CE_ENCRYPTION 0
|
||||
#define CE_DECRYPTION BIT(8)
|
||||
|
||||
#define CE_COMM_INT BIT(31)
|
||||
|
||||
/* ce_task symmetric */
|
||||
#define CE_AES_128BITS 0
|
||||
#define CE_AES_192BITS 1
|
||||
#define CE_AES_256BITS 2
|
||||
|
||||
#define CE_OP_ECB 0
|
||||
#define CE_OP_CBC (1 << 8)
|
||||
|
||||
#define CE_ALG_AES 0
|
||||
#define CE_ALG_DES 1
|
||||
#define CE_ALG_3DES 2
|
||||
|
||||
/* Used in ce_variant */
|
||||
#define CE_ID_NOTSUPP 0xFF
|
||||
|
||||
#define CE_ID_CIPHER_AES 0
|
||||
#define CE_ID_CIPHER_DES 1
|
||||
#define CE_ID_CIPHER_DES3 2
|
||||
#define CE_ID_CIPHER_MAX 3
|
||||
|
||||
#define CE_ID_OP_ECB 0
|
||||
#define CE_ID_OP_CBC 1
|
||||
#define CE_ID_OP_MAX 2
|
||||
|
||||
/* Used in CE registers */
|
||||
#define CE_ERR_ALGO_NOTSUP BIT(0)
|
||||
#define CE_ERR_DATALEN BIT(1)
|
||||
#define CE_ERR_KEYSRAM BIT(2)
|
||||
#define CE_ERR_ADDR_INVALID BIT(5)
|
||||
#define CE_ERR_KEYLADDER BIT(6)
|
||||
|
||||
#define CE_DIE_ID_SHIFT 16
|
||||
#define CE_DIE_ID_MASK 0x07
|
||||
|
||||
#define MAX_SG 8
|
||||
|
||||
#define CE_MAX_CLOCKS 3
|
||||
|
||||
#define MAXFLOW 4
|
||||
|
||||
/*
|
||||
* struct ce_clock - Describe clocks used by sun8i-ce
|
||||
* @name: Name of clock needed by this variant
|
||||
* @freq: Frequency to set for each clock
|
||||
* @max_freq: Maximum frequency for each clock (generally given by datasheet)
|
||||
*/
|
||||
struct ce_clock {
|
||||
const char *name;
|
||||
unsigned long freq;
|
||||
unsigned long max_freq;
|
||||
};
|
||||
|
||||
/*
|
||||
* struct ce_variant - Describe CE capability for each variant hardware
|
||||
* @alg_cipher: list of supported ciphers. for each CE_ID_ this will give the
|
||||
* coresponding CE_ALG_XXX value
|
||||
* @op_mode: list of supported block modes
|
||||
* @has_t_dlen_in_bytes: Does the request size for cipher is in
|
||||
* bytes or words
|
||||
* @ce_clks: list of clocks needed by this variant
|
||||
*/
|
||||
struct ce_variant {
|
||||
char alg_cipher[CE_ID_CIPHER_MAX];
|
||||
u32 op_mode[CE_ID_OP_MAX];
|
||||
bool has_t_dlen_in_bytes;
|
||||
struct ce_clock ce_clks[CE_MAX_CLOCKS];
|
||||
};
|
||||
|
||||
struct sginfo {
|
||||
u32 addr;
|
||||
u32 len;
|
||||
} __packed;
|
||||
|
||||
/*
|
||||
* struct ce_task - CE Task descriptor
|
||||
* The structure of this descriptor could be found in the datasheet
|
||||
*/
|
||||
struct ce_task {
|
||||
u32 t_id;
|
||||
u32 t_common_ctl;
|
||||
u32 t_sym_ctl;
|
||||
u32 t_asym_ctl;
|
||||
u32 t_key;
|
||||
u32 t_iv;
|
||||
u32 t_ctr;
|
||||
u32 t_dlen;
|
||||
struct sginfo t_src[MAX_SG];
|
||||
struct sginfo t_dst[MAX_SG];
|
||||
u32 next;
|
||||
u32 reserved[3];
|
||||
} __packed __aligned(8);
|
||||
|
||||
/*
|
||||
* struct sun8i_ce_flow - Information used by each flow
|
||||
* @engine: ptr to the crypto_engine for this flow
|
||||
* @bounce_iv: buffer which contain the IV
|
||||
* @ivlen: size of bounce_iv
|
||||
* @keylen: keylen for this flow operation
|
||||
* @complete: completion for the current task on this flow
|
||||
* @status: set to 1 by interrupt if task is done
|
||||
* @method: current method for flow
|
||||
* @op_dir: direction (encrypt vs decrypt) of this flow
|
||||
* @op_mode: op_mode for this flow
|
||||
* @t_phy: Physical address of task
|
||||
* @tl: pointer to the current ce_task for this flow
|
||||
* @stat_req: number of request done by this flow
|
||||
*/
|
||||
struct sun8i_ce_flow {
|
||||
struct crypto_engine *engine;
|
||||
void *bounce_iv;
|
||||
unsigned int ivlen;
|
||||
unsigned int keylen;
|
||||
struct completion complete;
|
||||
int status;
|
||||
u32 method;
|
||||
u32 op_dir;
|
||||
u32 op_mode;
|
||||
dma_addr_t t_phy;
|
||||
int timeout;
|
||||
struct ce_task *tl;
|
||||
#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
|
||||
unsigned long stat_req;
|
||||
#endif
|
||||
};
|
||||
|
||||
/*
|
||||
* struct sun8i_ce_dev - main container for all this driver information
|
||||
* @base: base address of CE
|
||||
* @ceclks: clocks used by CE
|
||||
* @reset: pointer to reset controller
|
||||
* @dev: the platform device
|
||||
* @mlock: Control access to device registers
|
||||
* @chanlist: array of all flow
|
||||
* @flow: flow to use in next request
|
||||
* @variant: pointer to variant specific data
|
||||
* @dbgfs_dir: Debugfs dentry for statistic directory
|
||||
* @dbgfs_stats: Debugfs dentry for statistic counters
|
||||
*/
|
||||
struct sun8i_ce_dev {
|
||||
void __iomem *base;
|
||||
struct clk *ceclks[CE_MAX_CLOCKS];
|
||||
struct reset_control *reset;
|
||||
struct device *dev;
|
||||
struct mutex mlock;
|
||||
struct sun8i_ce_flow *chanlist;
|
||||
atomic_t flow;
|
||||
const struct ce_variant *variant;
|
||||
#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
|
||||
struct dentry *dbgfs_dir;
|
||||
struct dentry *dbgfs_stats;
|
||||
#endif
|
||||
};
|
||||
|
||||
/*
|
||||
* struct sun8i_cipher_req_ctx - context for a skcipher request
|
||||
* @op_dir: direction (encrypt vs decrypt) for this request
|
||||
* @flow: the flow to use for this request
|
||||
*/
|
||||
struct sun8i_cipher_req_ctx {
|
||||
u32 op_dir;
|
||||
int flow;
|
||||
};
|
||||
|
||||
/*
|
||||
* struct sun8i_cipher_tfm_ctx - context for a skcipher TFM
|
||||
* @enginectx: crypto_engine used by this TFM
|
||||
* @key: pointer to key data
|
||||
* @keylen: len of the key
|
||||
* @ce: pointer to the private data of driver handling this TFM
|
||||
* @fallback_tfm: pointer to the fallback TFM
|
||||
*/
|
||||
struct sun8i_cipher_tfm_ctx {
|
||||
struct crypto_engine_ctx enginectx;
|
||||
u32 *key;
|
||||
u32 keylen;
|
||||
struct sun8i_ce_dev *ce;
|
||||
struct crypto_sync_skcipher *fallback_tfm;
|
||||
};
|
||||
|
||||
/*
|
||||
* struct sun8i_ce_alg_template - crypto_alg template
|
||||
* @type: the CRYPTO_ALG_TYPE for this template
|
||||
* @ce_algo_id: the CE_ID for this template
|
||||
* @ce_blockmode: the type of block operation CE_ID
|
||||
* @ce: pointer to the sun8i_ce_dev structure associated with
|
||||
* this template
|
||||
* @alg: one of sub struct must be used
|
||||
* @stat_req: number of request done on this template
|
||||
* @stat_fb: total of all data len done on this template
|
||||
*/
|
||||
struct sun8i_ce_alg_template {
|
||||
u32 type;
|
||||
u32 ce_algo_id;
|
||||
u32 ce_blockmode;
|
||||
struct sun8i_ce_dev *ce;
|
||||
union {
|
||||
struct skcipher_alg skcipher;
|
||||
} alg;
|
||||
#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
|
||||
unsigned long stat_req;
|
||||
unsigned long stat_fb;
|
||||
#endif
|
||||
};
|
||||
|
||||
int sun8i_ce_enqueue(struct crypto_async_request *areq, u32 type);
|
||||
|
||||
int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keylen);
|
||||
int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keylen);
|
||||
int sun8i_ce_cipher_init(struct crypto_tfm *tfm);
|
||||
void sun8i_ce_cipher_exit(struct crypto_tfm *tfm);
|
||||
int sun8i_ce_skdecrypt(struct skcipher_request *areq);
|
||||
int sun8i_ce_skencrypt(struct skcipher_request *areq);
|
||||
|
||||
int sun8i_ce_get_engine_number(struct sun8i_ce_dev *ce);
|
||||
|
||||
int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name);
|
||||
Reference in New Issue
Block a user