Path: blob/master/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h
29537 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* sun8i-ce.h - hardware cryptographic offloader for3* Allwinner H3/A64/H5/H2+/H6 SoC4*5* Copyright (C) 2016-2019 Corentin LABBE <[email protected]>6*/7#include <crypto/aes.h>8#include <crypto/des.h>9#include <crypto/engine.h>10#include <crypto/skcipher.h>11#include <linux/atomic.h>12#include <linux/debugfs.h>13#include <linux/crypto.h>14#include <linux/hw_random.h>15#include <crypto/internal/hash.h>16#include <crypto/md5.h>17#include <crypto/rng.h>18#include <crypto/sha1.h>19#include <crypto/sha2.h>2021/* CE Registers */22#define CE_TDQ 0x0023#define CE_CTR 0x0424#define CE_ICR 0x0825#define CE_ISR 0x0C26#define CE_TLR 0x1027#define CE_TSR 0x1428#define CE_ESR 0x1829#define CE_CSSGR 0x1C30#define CE_CDSGR 0x2031#define CE_CSAR 0x2432#define CE_CDAR 0x2833#define CE_TPR 0x2C3435/* Used in struct ce_task */36/* ce_task common */37#define CE_ENCRYPTION 038#define CE_DECRYPTION BIT(8)3940#define CE_COMM_INT BIT(31)4142/* ce_task symmetric */43#define CE_AES_128BITS 044#define CE_AES_192BITS 145#define CE_AES_256BITS 24647#define CE_OP_ECB 048#define CE_OP_CBC (1 << 8)4950#define CE_ALG_AES 051#define CE_ALG_DES 152#define CE_ALG_3DES 253#define CE_ALG_MD5 1654#define CE_ALG_SHA1 1755#define CE_ALG_SHA224 1856#define CE_ALG_SHA256 1957#define CE_ALG_SHA384 2058#define CE_ALG_SHA512 2159#define CE_ALG_TRNG 4860#define CE_ALG_PRNG 4961#define CE_ALG_TRNG_V2 0x1c62#define CE_ALG_PRNG_V2 0x1d6364/* Used in ce_variant */65#define CE_ID_NOTSUPP 0xFF6667#define CE_ID_CIPHER_AES 068#define CE_ID_CIPHER_DES 169#define CE_ID_CIPHER_DES3 270#define CE_ID_CIPHER_MAX 37172#define CE_ID_HASH_MD5 073#define CE_ID_HASH_SHA1 174#define CE_ID_HASH_SHA224 275#define CE_ID_HASH_SHA256 376#define CE_ID_HASH_SHA384 477#define CE_ID_HASH_SHA512 578#define CE_ID_HASH_MAX 67980#define CE_ID_OP_ECB 081#define CE_ID_OP_CBC 182#define CE_ID_OP_MAX 28384/* Used in CE registers */85#define CE_ERR_ALGO_NOTSUP BIT(0)86#define CE_ERR_DATALEN BIT(1)87#define CE_ERR_KEYSRAM BIT(2)88#define CE_ERR_ADDR_INVALID BIT(5)89#define CE_ERR_KEYLADDER BIT(6)9091#define ESR_H3 092#define ESR_A64 193#define ESR_R40 294#define ESR_H5 395#define ESR_H6 496#define ESR_D1 59798#define PRNG_DATA_SIZE (160 / 8)99#define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)100#define PRNG_LD BIT(17)101102#define CE_DIE_ID_SHIFT 16103#define CE_DIE_ID_MASK 0x07104105#define MAX_SG 8106107#define CE_MAX_CLOCKS 4108#define CE_DMA_TIMEOUT_MS 3000109110#define MAXFLOW 4111112#define CE_MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE113#define CE_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE114115/*116* struct ce_clock - Describe clocks used by sun8i-ce117* @name: Name of clock needed by this variant118* @freq: Frequency to set for each clock119* @max_freq: Maximum frequency for each clock (generally given by datasheet)120*/121struct ce_clock {122const char *name;123unsigned long freq;124unsigned long max_freq;125};126127/*128* struct ce_variant - Describe CE capability for each variant hardware129* @alg_cipher: list of supported ciphers. for each CE_ID_ this will give the130* coresponding CE_ALG_XXX value131* @alg_hash: list of supported hashes. for each CE_ID_ this will give the132* corresponding CE_ALG_XXX value133* @op_mode: list of supported block modes134* @cipher_t_dlen_in_bytes: Does the request size for cipher is in135* bytes or words136* @hash_t_dlen_in_bytes: Does the request size for hash is in137* bits or words138* @prng_t_dlen_in_bytes: Does the request size for PRNG is in139* bytes or words140* @trng_t_dlen_in_bytes: Does the request size for TRNG is in141* bytes or words142* @ce_clks: list of clocks needed by this variant143* @esr: The type of error register144* @prng: The CE_ALG_XXX value for the PRNG145* @trng: The CE_ALG_XXX value for the TRNG146*/147struct ce_variant {148char alg_cipher[CE_ID_CIPHER_MAX];149char alg_hash[CE_ID_HASH_MAX];150u32 op_mode[CE_ID_OP_MAX];151bool cipher_t_dlen_in_bytes;152bool hash_t_dlen_in_bits;153bool prng_t_dlen_in_bytes;154bool trng_t_dlen_in_bytes;155bool needs_word_addresses;156struct ce_clock ce_clks[CE_MAX_CLOCKS];157int esr;158unsigned char prng;159unsigned char trng;160};161162struct sginfo {163__le32 addr;164__le32 len;165} __packed;166167/*168* struct ce_task - CE Task descriptor169* The structure of this descriptor could be found in the datasheet170*/171struct ce_task {172__le32 t_id;173__le32 t_common_ctl;174__le32 t_sym_ctl;175__le32 t_asym_ctl;176__le32 t_key;177__le32 t_iv;178__le32 t_ctr;179__le32 t_dlen;180struct sginfo t_src[MAX_SG];181struct sginfo t_dst[MAX_SG];182__le32 next;183__le32 reserved[3];184} __packed __aligned(8);185186/*187* struct sun8i_ce_flow - Information used by each flow188* @engine: ptr to the crypto_engine for this flow189* @complete: completion for the current task on this flow190* @status: set to 1 by interrupt if task is done191* @t_phy: Physical address of task192* @tl: pointer to the current ce_task for this flow193* @stat_req: number of request done by this flow194*/195struct sun8i_ce_flow {196struct crypto_engine *engine;197struct completion complete;198int status;199dma_addr_t t_phy;200struct ce_task *tl;201#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG202unsigned long stat_req;203#endif204};205206/*207* struct sun8i_ce_dev - main container for all this driver information208* @base: base address of CE209* @ceclks: clocks used by CE210* @reset: pointer to reset controller211* @dev: the platform device212* @mlock: Control access to device registers213* @rnglock: Control access to the RNG (dedicated channel 3)214* @chanlist: array of all flow215* @flow: flow to use in next request216* @variant: pointer to variant specific data217* @dbgfs_dir: Debugfs dentry for statistic directory218* @dbgfs_stats: Debugfs dentry for statistic counters219*/220struct sun8i_ce_dev {221void __iomem *base;222struct clk *ceclks[CE_MAX_CLOCKS];223struct reset_control *reset;224struct device *dev;225struct mutex mlock;226struct mutex rnglock;227struct sun8i_ce_flow *chanlist;228atomic_t flow;229const struct ce_variant *variant;230#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG231struct dentry *dbgfs_dir;232struct dentry *dbgfs_stats;233#endif234#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG235struct hwrng trng;236#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG237unsigned long hwrng_stat_req;238unsigned long hwrng_stat_bytes;239#endif240#endif241};242243static inline u32 desc_addr_val(struct sun8i_ce_dev *dev, dma_addr_t addr)244{245if (dev->variant->needs_word_addresses)246return addr / 4;247248return addr;249}250251static inline __le32 desc_addr_val_le32(struct sun8i_ce_dev *dev,252dma_addr_t addr)253{254return cpu_to_le32(desc_addr_val(dev, addr));255}256257/*258* struct sun8i_cipher_req_ctx - context for a skcipher request259* @op_dir: direction (encrypt vs decrypt) for this request260* @flow: the flow to use for this request261* @nr_sgs: The number of source SG (as given by dma_map_sg())262* @nr_sgd: The number of destination SG (as given by dma_map_sg())263* @addr_iv: The IV addr returned by dma_map_single, need to unmap later264* @addr_key: The key addr returned by dma_map_single, need to unmap later265* @bounce_iv: Current IV buffer266* @backup_iv: Next IV buffer267* @fallback_req: request struct for invoking the fallback skcipher TFM268*/269struct sun8i_cipher_req_ctx {270u32 op_dir;271int flow;272int nr_sgs;273int nr_sgd;274dma_addr_t addr_iv;275dma_addr_t addr_key;276u8 bounce_iv[AES_BLOCK_SIZE] __aligned(sizeof(u32));277u8 backup_iv[AES_BLOCK_SIZE];278struct skcipher_request fallback_req; // keep at the end279};280281/*282* struct sun8i_cipher_tfm_ctx - context for a skcipher TFM283* @key: pointer to key data284* @keylen: len of the key285* @ce: pointer to the private data of driver handling this TFM286* @fallback_tfm: pointer to the fallback TFM287*/288struct sun8i_cipher_tfm_ctx {289u32 *key;290u32 keylen;291struct sun8i_ce_dev *ce;292struct crypto_skcipher *fallback_tfm;293};294295/*296* struct sun8i_ce_hash_tfm_ctx - context for an ahash TFM297* @ce: pointer to the private data of driver handling this TFM298* @fallback_tfm: pointer to the fallback TFM299*/300struct sun8i_ce_hash_tfm_ctx {301struct sun8i_ce_dev *ce;302struct crypto_ahash *fallback_tfm;303};304305/*306* struct sun8i_ce_hash_reqctx - context for an ahash request307* @fallback_req: pre-allocated fallback request308* @flow: the flow to use for this request309* @nr_sgs: number of entries in the source scatterlist310* @result_len: result length in bytes311* @pad_len: padding length in bytes312* @addr_res: DMA address of the result buffer, returned by dma_map_single()313* @addr_pad: DMA address of the padding buffer, returned by dma_map_single()314* @result: per-request result buffer315* @pad: per-request padding buffer (up to 2 blocks)316*/317struct sun8i_ce_hash_reqctx {318int flow;319int nr_sgs;320size_t result_len;321size_t pad_len;322dma_addr_t addr_res;323dma_addr_t addr_pad;324u8 result[CE_MAX_HASH_DIGEST_SIZE] __aligned(CRYPTO_DMA_ALIGN);325u8 pad[2 * CE_MAX_HASH_BLOCK_SIZE];326struct ahash_request fallback_req; // keep at the end327};328329/*330* struct sun8i_ce_prng_ctx - context for PRNG TFM331* @seed: The seed to use332* @slen: The size of the seed333*/334struct sun8i_ce_rng_tfm_ctx {335void *seed;336unsigned int slen;337};338339/*340* struct sun8i_ce_alg_template - crypto_alg template341* @type: the CRYPTO_ALG_TYPE for this template342* @ce_algo_id: the CE_ID for this template343* @ce_blockmode: the type of block operation CE_ID344* @ce: pointer to the sun8i_ce_dev structure associated with345* this template346* @alg: one of sub struct must be used347* @stat_req: number of request done on this template348* @stat_fb: number of request which has fallbacked349* @stat_bytes: total data size done by this template350*/351struct sun8i_ce_alg_template {352u32 type;353u32 ce_algo_id;354u32 ce_blockmode;355struct sun8i_ce_dev *ce;356union {357struct skcipher_engine_alg skcipher;358struct ahash_engine_alg hash;359struct rng_alg rng;360} alg;361unsigned long stat_req;362unsigned long stat_fb;363unsigned long stat_bytes;364unsigned long stat_fb_maxsg;365unsigned long stat_fb_leniv;366unsigned long stat_fb_len0;367unsigned long stat_fb_mod16;368unsigned long stat_fb_srcali;369unsigned long stat_fb_srclen;370unsigned long stat_fb_dstali;371unsigned long stat_fb_dstlen;372char fbname[CRYPTO_MAX_ALG_NAME];373};374375int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,376unsigned int keylen);377int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,378unsigned int keylen);379int sun8i_ce_cipher_init(struct crypto_tfm *tfm);380void sun8i_ce_cipher_exit(struct crypto_tfm *tfm);381int sun8i_ce_cipher_do_one(struct crypto_engine *engine, void *areq);382int sun8i_ce_skdecrypt(struct skcipher_request *areq);383int sun8i_ce_skencrypt(struct skcipher_request *areq);384385int sun8i_ce_get_engine_number(struct sun8i_ce_dev *ce);386387int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name);388389int sun8i_ce_hash_init_tfm(struct crypto_ahash *tfm);390void sun8i_ce_hash_exit_tfm(struct crypto_ahash *tfm);391int sun8i_ce_hash_init(struct ahash_request *areq);392int sun8i_ce_hash_export(struct ahash_request *areq, void *out);393int sun8i_ce_hash_import(struct ahash_request *areq, const void *in);394int sun8i_ce_hash_final(struct ahash_request *areq);395int sun8i_ce_hash_update(struct ahash_request *areq);396int sun8i_ce_hash_finup(struct ahash_request *areq);397int sun8i_ce_hash_digest(struct ahash_request *areq);398int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq);399400int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src,401unsigned int slen, u8 *dst, unsigned int dlen);402int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);403void sun8i_ce_prng_exit(struct crypto_tfm *tfm);404int sun8i_ce_prng_init(struct crypto_tfm *tfm);405406int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce);407void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce);408409410