Path: blob/master/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h
26296 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 4108109#define MAXFLOW 4110111/*112* struct ce_clock - Describe clocks used by sun8i-ce113* @name: Name of clock needed by this variant114* @freq: Frequency to set for each clock115* @max_freq: Maximum frequency for each clock (generally given by datasheet)116*/117struct ce_clock {118const char *name;119unsigned long freq;120unsigned long max_freq;121};122123/*124* struct ce_variant - Describe CE capability for each variant hardware125* @alg_cipher: list of supported ciphers. for each CE_ID_ this will give the126* coresponding CE_ALG_XXX value127* @alg_hash: list of supported hashes. for each CE_ID_ this will give the128* corresponding CE_ALG_XXX value129* @op_mode: list of supported block modes130* @cipher_t_dlen_in_bytes: Does the request size for cipher is in131* bytes or words132* @hash_t_dlen_in_bytes: Does the request size for hash is in133* bits or words134* @prng_t_dlen_in_bytes: Does the request size for PRNG is in135* bytes or words136* @trng_t_dlen_in_bytes: Does the request size for TRNG is in137* bytes or words138* @ce_clks: list of clocks needed by this variant139* @esr: The type of error register140* @prng: The CE_ALG_XXX value for the PRNG141* @trng: The CE_ALG_XXX value for the TRNG142*/143struct ce_variant {144char alg_cipher[CE_ID_CIPHER_MAX];145char alg_hash[CE_ID_HASH_MAX];146u32 op_mode[CE_ID_OP_MAX];147bool cipher_t_dlen_in_bytes;148bool hash_t_dlen_in_bits;149bool prng_t_dlen_in_bytes;150bool trng_t_dlen_in_bytes;151bool needs_word_addresses;152struct ce_clock ce_clks[CE_MAX_CLOCKS];153int esr;154unsigned char prng;155unsigned char trng;156};157158struct sginfo {159__le32 addr;160__le32 len;161} __packed;162163/*164* struct ce_task - CE Task descriptor165* The structure of this descriptor could be found in the datasheet166*/167struct ce_task {168__le32 t_id;169__le32 t_common_ctl;170__le32 t_sym_ctl;171__le32 t_asym_ctl;172__le32 t_key;173__le32 t_iv;174__le32 t_ctr;175__le32 t_dlen;176struct sginfo t_src[MAX_SG];177struct sginfo t_dst[MAX_SG];178__le32 next;179__le32 reserved[3];180} __packed __aligned(8);181182/*183* struct sun8i_ce_flow - Information used by each flow184* @engine: ptr to the crypto_engine for this flow185* @complete: completion for the current task on this flow186* @status: set to 1 by interrupt if task is done187* @t_phy: Physical address of task188* @tl: pointer to the current ce_task for this flow189* @backup_iv: buffer which contain the next IV to store190* @bounce_iv: buffer which contain the IV191* @stat_req: number of request done by this flow192*/193struct sun8i_ce_flow {194struct crypto_engine *engine;195struct completion complete;196int status;197dma_addr_t t_phy;198int timeout;199struct ce_task *tl;200void *backup_iv;201void *bounce_iv;202#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG203unsigned long stat_req;204#endif205};206207/*208* struct sun8i_ce_dev - main container for all this driver information209* @base: base address of CE210* @ceclks: clocks used by CE211* @reset: pointer to reset controller212* @dev: the platform device213* @mlock: Control access to device registers214* @rnglock: Control access to the RNG (dedicated channel 3)215* @chanlist: array of all flow216* @flow: flow to use in next request217* @variant: pointer to variant specific data218* @dbgfs_dir: Debugfs dentry for statistic directory219* @dbgfs_stats: Debugfs dentry for statistic counters220*/221struct sun8i_ce_dev {222void __iomem *base;223struct clk *ceclks[CE_MAX_CLOCKS];224struct reset_control *reset;225struct device *dev;226struct mutex mlock;227struct mutex rnglock;228struct sun8i_ce_flow *chanlist;229atomic_t flow;230const struct ce_variant *variant;231#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG232struct dentry *dbgfs_dir;233struct dentry *dbgfs_stats;234#endif235#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG236struct hwrng trng;237#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG238unsigned long hwrng_stat_req;239unsigned long hwrng_stat_bytes;240#endif241#endif242};243244static inline u32 desc_addr_val(struct sun8i_ce_dev *dev, dma_addr_t addr)245{246if (dev->variant->needs_word_addresses)247return addr / 4;248249return addr;250}251252static inline __le32 desc_addr_val_le32(struct sun8i_ce_dev *dev,253dma_addr_t addr)254{255return cpu_to_le32(desc_addr_val(dev, addr));256}257258/*259* struct sun8i_cipher_req_ctx - context for a skcipher request260* @op_dir: direction (encrypt vs decrypt) for this request261* @flow: the flow to use for this request262* @nr_sgs: The number of source SG (as given by dma_map_sg())263* @nr_sgd: The number of destination SG (as given by dma_map_sg())264* @addr_iv: The IV addr returned by dma_map_single, need to unmap later265* @addr_key: The key addr returned by dma_map_single, need to unmap later266* @fallback_req: request struct for invoking the fallback skcipher TFM267*/268struct sun8i_cipher_req_ctx {269u32 op_dir;270int flow;271int nr_sgs;272int nr_sgd;273dma_addr_t addr_iv;274dma_addr_t addr_key;275struct skcipher_request fallback_req; // keep at the end276};277278/*279* struct sun8i_cipher_tfm_ctx - context for a skcipher TFM280* @key: pointer to key data281* @keylen: len of the key282* @ce: pointer to the private data of driver handling this TFM283* @fallback_tfm: pointer to the fallback TFM284*/285struct sun8i_cipher_tfm_ctx {286u32 *key;287u32 keylen;288struct sun8i_ce_dev *ce;289struct crypto_skcipher *fallback_tfm;290};291292/*293* struct sun8i_ce_hash_tfm_ctx - context for an ahash TFM294* @ce: pointer to the private data of driver handling this TFM295* @fallback_tfm: pointer to the fallback TFM296*/297struct sun8i_ce_hash_tfm_ctx {298struct sun8i_ce_dev *ce;299struct crypto_ahash *fallback_tfm;300};301302/*303* struct sun8i_ce_hash_reqctx - context for an ahash request304* @fallback_req: pre-allocated fallback request305* @flow: the flow to use for this request306*/307struct sun8i_ce_hash_reqctx {308int flow;309struct ahash_request fallback_req; // keep at the end310};311312/*313* struct sun8i_ce_prng_ctx - context for PRNG TFM314* @seed: The seed to use315* @slen: The size of the seed316*/317struct sun8i_ce_rng_tfm_ctx {318void *seed;319unsigned int slen;320};321322/*323* struct sun8i_ce_alg_template - crypto_alg template324* @type: the CRYPTO_ALG_TYPE for this template325* @ce_algo_id: the CE_ID for this template326* @ce_blockmode: the type of block operation CE_ID327* @ce: pointer to the sun8i_ce_dev structure associated with328* this template329* @alg: one of sub struct must be used330* @stat_req: number of request done on this template331* @stat_fb: number of request which has fallbacked332* @stat_bytes: total data size done by this template333*/334struct sun8i_ce_alg_template {335u32 type;336u32 ce_algo_id;337u32 ce_blockmode;338struct sun8i_ce_dev *ce;339union {340struct skcipher_engine_alg skcipher;341struct ahash_engine_alg hash;342struct rng_alg rng;343} alg;344unsigned long stat_req;345unsigned long stat_fb;346unsigned long stat_bytes;347unsigned long stat_fb_maxsg;348unsigned long stat_fb_leniv;349unsigned long stat_fb_len0;350unsigned long stat_fb_mod16;351unsigned long stat_fb_srcali;352unsigned long stat_fb_srclen;353unsigned long stat_fb_dstali;354unsigned long stat_fb_dstlen;355char fbname[CRYPTO_MAX_ALG_NAME];356};357358int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,359unsigned int keylen);360int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,361unsigned int keylen);362int sun8i_ce_cipher_init(struct crypto_tfm *tfm);363void sun8i_ce_cipher_exit(struct crypto_tfm *tfm);364int sun8i_ce_cipher_do_one(struct crypto_engine *engine, void *areq);365int sun8i_ce_skdecrypt(struct skcipher_request *areq);366int sun8i_ce_skencrypt(struct skcipher_request *areq);367368int sun8i_ce_get_engine_number(struct sun8i_ce_dev *ce);369370int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name);371372int sun8i_ce_hash_init_tfm(struct crypto_ahash *tfm);373void sun8i_ce_hash_exit_tfm(struct crypto_ahash *tfm);374int sun8i_ce_hash_init(struct ahash_request *areq);375int sun8i_ce_hash_export(struct ahash_request *areq, void *out);376int sun8i_ce_hash_import(struct ahash_request *areq, const void *in);377int sun8i_ce_hash_final(struct ahash_request *areq);378int sun8i_ce_hash_update(struct ahash_request *areq);379int sun8i_ce_hash_finup(struct ahash_request *areq);380int sun8i_ce_hash_digest(struct ahash_request *areq);381int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq);382383int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src,384unsigned int slen, u8 *dst, unsigned int dlen);385int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);386void sun8i_ce_prng_exit(struct crypto_tfm *tfm);387int sun8i_ce_prng_init(struct crypto_tfm *tfm);388389int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce);390void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce);391392393