Path: blob/master/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h
26292 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* sun8i-ss.h - hardware cryptographic offloader for3* Allwinner A80/A83T 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/rng.h>11#include <crypto/skcipher.h>12#include <linux/atomic.h>13#include <linux/debugfs.h>14#include <linux/crypto.h>15#include <crypto/internal/hash.h>16#include <crypto/md5.h>17#include <crypto/sha1.h>18#include <crypto/sha2.h>1920#define SS_START 12122#define SS_ENCRYPTION 023#define SS_DECRYPTION BIT(6)2425#define SS_ALG_AES 026#define SS_ALG_DES (1 << 2)27#define SS_ALG_3DES (2 << 2)28#define SS_ALG_MD5 (3 << 2)29#define SS_ALG_PRNG (4 << 2)30#define SS_ALG_SHA1 (6 << 2)31#define SS_ALG_SHA224 (7 << 2)32#define SS_ALG_SHA256 (8 << 2)3334#define SS_CTL_REG 0x0035#define SS_INT_CTL_REG 0x0436#define SS_INT_STA_REG 0x0837#define SS_KEY_ADR_REG 0x1038#define SS_IV_ADR_REG 0x1839#define SS_SRC_ADR_REG 0x2040#define SS_DST_ADR_REG 0x2841#define SS_LEN_ADR_REG 0x304243#define SS_ID_NOTSUPP 0xFF4445#define SS_ID_CIPHER_AES 046#define SS_ID_CIPHER_DES 147#define SS_ID_CIPHER_DES3 248#define SS_ID_CIPHER_MAX 34950#define SS_ID_OP_ECB 051#define SS_ID_OP_CBC 152#define SS_ID_OP_MAX 25354#define SS_AES_128BITS 055#define SS_AES_192BITS 156#define SS_AES_256BITS 25758#define SS_OP_ECB 059#define SS_OP_CBC (1 << 13)6061#define SS_ID_HASH_MD5 062#define SS_ID_HASH_SHA1 163#define SS_ID_HASH_SHA224 264#define SS_ID_HASH_SHA256 365#define SS_ID_HASH_MAX 46667#define SS_FLOW0 BIT(30)68#define SS_FLOW1 BIT(31)6970#define SS_PRNG_CONTINUE BIT(18)7172#define MAX_SG 87374#define MAXFLOW 27576#define SS_MAX_CLOCKS 27778#define SS_DIE_ID_SHIFT 2079#define SS_DIE_ID_MASK 0x078081#define PRNG_DATA_SIZE (160 / 8)82#define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)8384#define MAX_PAD_SIZE 40968586/*87* struct ss_clock - Describe clocks used by sun8i-ss88* @name: Name of clock needed by this variant89* @freq: Frequency to set for each clock90* @max_freq: Maximum frequency for each clock91*/92struct ss_clock {93const char *name;94unsigned long freq;95unsigned long max_freq;96};9798/*99* struct ss_variant - Describe SS capability for each variant hardware100* @alg_cipher: list of supported ciphers. for each SS_ID_ this will give the101* coresponding SS_ALG_XXX value102* @alg_hash: list of supported hashes. for each SS_ID_ this will give the103* corresponding SS_ALG_XXX value104* @op_mode: list of supported block modes105* @ss_clks: list of clock needed by this variant106*/107struct ss_variant {108char alg_cipher[SS_ID_CIPHER_MAX];109char alg_hash[SS_ID_HASH_MAX];110u32 op_mode[SS_ID_OP_MAX];111struct ss_clock ss_clks[SS_MAX_CLOCKS];112};113114struct sginfo {115u32 addr;116u32 len;117};118119/*120* struct sun8i_ss_flow - Information used by each flow121* @engine: ptr to the crypto_engine for this flow122* @complete: completion for the current task on this flow123* @status: set to 1 by interrupt if task is done124* @stat_req: number of request done by this flow125* @iv: list of IV to use for each step126* @biv: buffer which contain the backuped IV127* @pad: padding buffer for hash operations128* @result: buffer for storing the result of hash operations129*/130struct sun8i_ss_flow {131struct crypto_engine *engine;132struct completion complete;133int status;134u8 *iv[MAX_SG];135u8 *biv;136void *pad;137void *result;138#ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG139unsigned long stat_req;140#endif141};142143/*144* struct sun8i_ss_dev - main container for all this driver information145* @base: base address of SS146* @ssclks: clocks used by SS147* @reset: pointer to reset controller148* @dev: the platform device149* @mlock: Control access to device registers150* @flows: array of all flow151* @flow: flow to use in next request152* @variant: pointer to variant specific data153* @dbgfs_dir: Debugfs dentry for statistic directory154* @dbgfs_stats: Debugfs dentry for statistic counters155*/156struct sun8i_ss_dev {157void __iomem *base;158struct clk *ssclks[SS_MAX_CLOCKS];159struct reset_control *reset;160struct device *dev;161struct mutex mlock;162struct sun8i_ss_flow *flows;163atomic_t flow;164const struct ss_variant *variant;165#ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG166struct dentry *dbgfs_dir;167struct dentry *dbgfs_stats;168#endif169};170171/*172* struct sun8i_cipher_req_ctx - context for a skcipher request173* @t_src: list of mapped SGs with their size174* @t_dst: list of mapped SGs with their size175* @p_key: DMA address of the key176* @p_iv: DMA address of the IVs177* @niv: Number of IVs DMA mapped178* @method: current algorithm for this request179* @op_mode: op_mode for this request180* @op_dir: direction (encrypt vs decrypt) for this request181* @flow: the flow to use for this request182* @ivlen: size of IVs183* @keylen: keylen for this request184* @fallback_req: request struct for invoking the fallback skcipher TFM185*/186struct sun8i_cipher_req_ctx {187struct sginfo t_src[MAX_SG];188struct sginfo t_dst[MAX_SG];189u32 p_key;190u32 p_iv[MAX_SG];191int niv;192u32 method;193u32 op_mode;194u32 op_dir;195int flow;196unsigned int ivlen;197unsigned int keylen;198struct skcipher_request fallback_req; // keep at the end199};200201/*202* struct sun8i_cipher_tfm_ctx - context for a skcipher TFM203* @key: pointer to key data204* @keylen: len of the key205* @ss: pointer to the private data of driver handling this TFM206* @fallback_tfm: pointer to the fallback TFM207*/208struct sun8i_cipher_tfm_ctx {209u32 *key;210u32 keylen;211struct sun8i_ss_dev *ss;212struct crypto_skcipher *fallback_tfm;213};214215/*216* struct sun8i_ss_prng_ctx - context for PRNG TFM217* @seed: The seed to use218* @slen: The size of the seed219*/220struct sun8i_ss_rng_tfm_ctx {221void *seed;222unsigned int slen;223};224225/*226* struct sun8i_ss_hash_tfm_ctx - context for an ahash TFM227* @fallback_tfm: pointer to the fallback TFM228* @ss: pointer to the private data of driver handling this TFM229*/230struct sun8i_ss_hash_tfm_ctx {231struct crypto_ahash *fallback_tfm;232struct sun8i_ss_dev *ss;233u8 *ipad;234u8 *opad;235u8 key[SHA256_BLOCK_SIZE];236int keylen;237};238239/*240* struct sun8i_ss_hash_reqctx - context for an ahash request241* @t_src: list of DMA address and size for source SGs242* @t_dst: list of DMA address and size for destination SGs243* @fallback_req: pre-allocated fallback request244* @method: the register value for the algorithm used by this request245* @flow: the flow to use for this request246*/247struct sun8i_ss_hash_reqctx {248struct sginfo t_src[MAX_SG];249struct sginfo t_dst[MAX_SG];250struct ahash_request fallback_req;251u32 method;252int flow;253};254255/*256* struct sun8i_ss_alg_template - crypto_alg template257* @type: the CRYPTO_ALG_TYPE for this template258* @ss_algo_id: the SS_ID for this template259* @ss_blockmode: the type of block operation SS_ID260* @ss: pointer to the sun8i_ss_dev structure associated with261* this template262* @alg: one of sub struct must be used263* @stat_req: number of request done on this template264* @stat_fb: number of request which has fallbacked265* @stat_bytes: total data size done by this template266*/267struct sun8i_ss_alg_template {268u32 type;269u32 ss_algo_id;270u32 ss_blockmode;271struct sun8i_ss_dev *ss;272union {273struct skcipher_engine_alg skcipher;274struct rng_alg rng;275struct ahash_engine_alg hash;276} alg;277unsigned long stat_req;278unsigned long stat_fb;279unsigned long stat_bytes;280unsigned long stat_fb_len;281unsigned long stat_fb_sglen;282unsigned long stat_fb_align;283unsigned long stat_fb_sgnum;284char fbname[CRYPTO_MAX_ALG_NAME];285};286287int sun8i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,288unsigned int keylen);289int sun8i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,290unsigned int keylen);291int sun8i_ss_cipher_init(struct crypto_tfm *tfm);292void sun8i_ss_cipher_exit(struct crypto_tfm *tfm);293int sun8i_ss_handle_cipher_request(struct crypto_engine *engine, void *areq);294int sun8i_ss_skdecrypt(struct skcipher_request *areq);295int sun8i_ss_skencrypt(struct skcipher_request *areq);296297int sun8i_ss_get_engine_number(struct sun8i_ss_dev *ss);298299int sun8i_ss_run_task(struct sun8i_ss_dev *ss, struct sun8i_cipher_req_ctx *rctx, const char *name);300int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,301unsigned int slen, u8 *dst, unsigned int dlen);302int sun8i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);303int sun8i_ss_prng_init(struct crypto_tfm *tfm);304void sun8i_ss_prng_exit(struct crypto_tfm *tfm);305306int sun8i_ss_hash_init_tfm(struct crypto_ahash *tfm);307void sun8i_ss_hash_exit_tfm(struct crypto_ahash *tfm);308int sun8i_ss_hash_init(struct ahash_request *areq);309int sun8i_ss_hash_export(struct ahash_request *areq, void *out);310int sun8i_ss_hash_import(struct ahash_request *areq, const void *in);311int sun8i_ss_hash_final(struct ahash_request *areq);312int sun8i_ss_hash_update(struct ahash_request *areq);313int sun8i_ss_hash_finup(struct ahash_request *areq);314int sun8i_ss_hash_digest(struct ahash_request *areq);315int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq);316int sun8i_ss_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,317unsigned int keylen);318319320