/* 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 <[email protected]> */ #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> #include <linux/hw_random.h> #include <crypto/internal/hash.h> #include <crypto/md5.h> #include <crypto/rng.h> #include <crypto/sha1.h> #include <crypto/sha2.h> /* CE Registers */ #define CE_TDQ … #define CE_CTR … #define CE_ICR … #define CE_ISR … #define CE_TLR … #define CE_TSR … #define CE_ESR … #define CE_CSSGR … #define CE_CDSGR … #define CE_CSAR … #define CE_CDAR … #define CE_TPR … /* Used in struct ce_task */ /* ce_task common */ #define CE_ENCRYPTION … #define CE_DECRYPTION … #define CE_COMM_INT … /* ce_task symmetric */ #define CE_AES_128BITS … #define CE_AES_192BITS … #define CE_AES_256BITS … #define CE_OP_ECB … #define CE_OP_CBC … #define CE_ALG_AES … #define CE_ALG_DES … #define CE_ALG_3DES … #define CE_ALG_MD5 … #define CE_ALG_SHA1 … #define CE_ALG_SHA224 … #define CE_ALG_SHA256 … #define CE_ALG_SHA384 … #define CE_ALG_SHA512 … #define CE_ALG_TRNG … #define CE_ALG_PRNG … #define CE_ALG_TRNG_V2 … #define CE_ALG_PRNG_V2 … /* Used in ce_variant */ #define CE_ID_NOTSUPP … #define CE_ID_CIPHER_AES … #define CE_ID_CIPHER_DES … #define CE_ID_CIPHER_DES3 … #define CE_ID_CIPHER_MAX … #define CE_ID_HASH_MD5 … #define CE_ID_HASH_SHA1 … #define CE_ID_HASH_SHA224 … #define CE_ID_HASH_SHA256 … #define CE_ID_HASH_SHA384 … #define CE_ID_HASH_SHA512 … #define CE_ID_HASH_MAX … #define CE_ID_OP_ECB … #define CE_ID_OP_CBC … #define CE_ID_OP_MAX … /* Used in CE registers */ #define CE_ERR_ALGO_NOTSUP … #define CE_ERR_DATALEN … #define CE_ERR_KEYSRAM … #define CE_ERR_ADDR_INVALID … #define CE_ERR_KEYLADDER … #define ESR_H3 … #define ESR_A64 … #define ESR_R40 … #define ESR_H5 … #define ESR_H6 … #define ESR_D1 … #define PRNG_DATA_SIZE … #define PRNG_SEED_SIZE … #define PRNG_LD … #define CE_DIE_ID_SHIFT … #define CE_DIE_ID_MASK … #define MAX_SG … #define CE_MAX_CLOCKS … #define MAXFLOW … /* * 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 { … }; /* * 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 * @alg_hash: list of supported hashes. for each CE_ID_ this will give the * corresponding CE_ALG_XXX value * @op_mode: list of supported block modes * @cipher_t_dlen_in_bytes: Does the request size for cipher is in * bytes or words * @hash_t_dlen_in_bytes: Does the request size for hash is in * bits or words * @prng_t_dlen_in_bytes: Does the request size for PRNG is in * bytes or words * @trng_t_dlen_in_bytes: Does the request size for TRNG is in * bytes or words * @ce_clks: list of clocks needed by this variant * @esr: The type of error register * @prng: The CE_ALG_XXX value for the PRNG * @trng: The CE_ALG_XXX value for the TRNG */ struct ce_variant { … }; struct sginfo { … } __packed; /* * struct ce_task - CE Task descriptor * The structure of this descriptor could be found in the datasheet */ struct ce_task { … } __packed __aligned(…); /* * struct sun8i_ce_flow - Information used by each flow * @engine: ptr to the crypto_engine for this flow * @complete: completion for the current task on this flow * @status: set to 1 by interrupt if task is done * @t_phy: Physical address of task * @tl: pointer to the current ce_task for this flow * @backup_iv: buffer which contain the next IV to store * @bounce_iv: buffer which contain the IV * @stat_req: number of request done by this flow */ struct sun8i_ce_flow { … }; /* * 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 * @rnglock: Control access to the RNG (dedicated channel 3) * @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 { … }; static inline u32 desc_addr_val(struct sun8i_ce_dev *dev, dma_addr_t addr) { … } static inline __le32 desc_addr_val_le32(struct sun8i_ce_dev *dev, dma_addr_t addr) { … } /* * 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 * @ivlen: size of bounce_iv * @nr_sgs: The number of source SG (as given by dma_map_sg()) * @nr_sgd: The number of destination SG (as given by dma_map_sg()) * @addr_iv: The IV addr returned by dma_map_single, need to unmap later * @addr_key: The key addr returned by dma_map_single, need to unmap later * @fallback_req: request struct for invoking the fallback skcipher TFM */ struct sun8i_cipher_req_ctx { … }; /* * struct sun8i_cipher_tfm_ctx - context for a skcipher 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 sun8i_ce_hash_tfm_ctx - context for an ahash TFM * @ce: pointer to the private data of driver handling this TFM * @fallback_tfm: pointer to the fallback TFM */ struct sun8i_ce_hash_tfm_ctx { … }; /* * struct sun8i_ce_hash_reqctx - context for an ahash request * @fallback_req: pre-allocated fallback request * @flow: the flow to use for this request */ struct sun8i_ce_hash_reqctx { … }; /* * struct sun8i_ce_prng_ctx - context for PRNG TFM * @seed: The seed to use * @slen: The size of the seed */ struct sun8i_ce_rng_tfm_ctx { … }; /* * 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: number of request which has fallbacked * @stat_bytes: total data size done by this template */ struct sun8i_ce_alg_template { … }; 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_cipher_do_one(struct crypto_engine *engine, void *areq); 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); int sun8i_ce_hash_init_tfm(struct crypto_ahash *tfm); void sun8i_ce_hash_exit_tfm(struct crypto_ahash *tfm); int sun8i_ce_hash_init(struct ahash_request *areq); int sun8i_ce_hash_export(struct ahash_request *areq, void *out); int sun8i_ce_hash_import(struct ahash_request *areq, const void *in); int sun8i_ce_hash_final(struct ahash_request *areq); int sun8i_ce_hash_update(struct ahash_request *areq); int sun8i_ce_hash_finup(struct ahash_request *areq); int sun8i_ce_hash_digest(struct ahash_request *areq); int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq); int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int dlen); int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen); void sun8i_ce_prng_exit(struct crypto_tfm *tfm); int sun8i_ce_prng_init(struct crypto_tfm *tfm); int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce); void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce);