brintos

brintos / linux-shallow public Read only

0
0
Text · 8.8 KiB · ae66eb4 Raw
319 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/*3 * sun8i-ss.h - hardware cryptographic offloader for4 * Allwinner A80/A83T SoC5 *6 * Copyright (C) 2016-2019 Corentin LABBE <clabbe.montjoie@gmail.com>7 */8#include <crypto/aes.h>9#include <crypto/des.h>10#include <crypto/engine.h>11#include <crypto/rng.h>12#include <crypto/skcipher.h>13#include <linux/atomic.h>14#include <linux/debugfs.h>15#include <linux/crypto.h>16#include <crypto/internal/hash.h>17#include <crypto/md5.h>18#include <crypto/sha1.h>19#include <crypto/sha2.h>20 21#define SS_START	122 23#define SS_ENCRYPTION		024#define SS_DECRYPTION		BIT(6)25 26#define SS_ALG_AES		027#define SS_ALG_DES		(1 << 2)28#define SS_ALG_3DES		(2 << 2)29#define SS_ALG_MD5		(3 << 2)30#define SS_ALG_PRNG		(4 << 2)31#define SS_ALG_SHA1		(6 << 2)32#define SS_ALG_SHA224		(7 << 2)33#define SS_ALG_SHA256		(8 << 2)34 35#define SS_CTL_REG		0x0036#define SS_INT_CTL_REG		0x0437#define SS_INT_STA_REG		0x0838#define SS_KEY_ADR_REG		0x1039#define SS_IV_ADR_REG		0x1840#define SS_SRC_ADR_REG		0x2041#define SS_DST_ADR_REG		0x2842#define SS_LEN_ADR_REG		0x3043 44#define SS_ID_NOTSUPP		0xFF45 46#define SS_ID_CIPHER_AES	047#define SS_ID_CIPHER_DES	148#define SS_ID_CIPHER_DES3	249#define SS_ID_CIPHER_MAX	350 51#define SS_ID_OP_ECB	052#define SS_ID_OP_CBC	153#define SS_ID_OP_MAX	254 55#define SS_AES_128BITS 056#define SS_AES_192BITS 157#define SS_AES_256BITS 258 59#define SS_OP_ECB	060#define SS_OP_CBC	(1 << 13)61 62#define SS_ID_HASH_MD5	063#define SS_ID_HASH_SHA1	164#define SS_ID_HASH_SHA224	265#define SS_ID_HASH_SHA256	366#define SS_ID_HASH_MAX	467 68#define SS_FLOW0	BIT(30)69#define SS_FLOW1	BIT(31)70 71#define SS_PRNG_CONTINUE	BIT(18)72 73#define MAX_SG 874 75#define MAXFLOW 276 77#define SS_MAX_CLOCKS 278 79#define SS_DIE_ID_SHIFT	2080#define SS_DIE_ID_MASK	0x0781 82#define PRNG_DATA_SIZE (160 / 8)83#define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)84 85#define MAX_PAD_SIZE 409686 87/*88 * struct ss_clock - Describe clocks used by sun8i-ss89 * @name:       Name of clock needed by this variant90 * @freq:       Frequency to set for each clock91 * @max_freq:   Maximum frequency for each clock92 */93struct ss_clock {94	const char *name;95	unsigned long freq;96	unsigned long max_freq;97};98 99/*100 * struct ss_variant - Describe SS capability for each variant hardware101 * @alg_cipher:	list of supported ciphers. for each SS_ID_ this will give the102 *              coresponding SS_ALG_XXX value103 * @alg_hash:	list of supported hashes. for each SS_ID_ this will give the104 *              corresponding SS_ALG_XXX value105 * @op_mode:	list of supported block modes106 * @ss_clks:	list of clock needed by this variant107 */108struct ss_variant {109	char alg_cipher[SS_ID_CIPHER_MAX];110	char alg_hash[SS_ID_HASH_MAX];111	u32 op_mode[SS_ID_OP_MAX];112	struct ss_clock ss_clks[SS_MAX_CLOCKS];113};114 115struct sginfo {116	u32 addr;117	u32 len;118};119 120/*121 * struct sun8i_ss_flow - Information used by each flow122 * @engine:	ptr to the crypto_engine for this flow123 * @complete:	completion for the current task on this flow124 * @status:	set to 1 by interrupt if task is done125 * @stat_req:	number of request done by this flow126 * @iv:		list of IV to use for each step127 * @biv:	buffer which contain the backuped IV128 * @pad:	padding buffer for hash operations129 * @result:	buffer for storing the result of hash operations130 */131struct sun8i_ss_flow {132	struct crypto_engine *engine;133	struct completion complete;134	int status;135	u8 *iv[MAX_SG];136	u8 *biv;137	void *pad;138	void *result;139#ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG140	unsigned long stat_req;141#endif142};143 144/*145 * struct sun8i_ss_dev - main container for all this driver information146 * @base:	base address of SS147 * @ssclks:	clocks used by SS148 * @reset:	pointer to reset controller149 * @dev:	the platform device150 * @mlock:	Control access to device registers151 * @flows:	array of all flow152 * @flow:	flow to use in next request153 * @variant:	pointer to variant specific data154 * @dbgfs_dir:	Debugfs dentry for statistic directory155 * @dbgfs_stats: Debugfs dentry for statistic counters156 */157struct sun8i_ss_dev {158	void __iomem *base;159	struct clk *ssclks[SS_MAX_CLOCKS];160	struct reset_control *reset;161	struct device *dev;162	struct mutex mlock;163	struct sun8i_ss_flow *flows;164	atomic_t flow;165	const struct ss_variant *variant;166#ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG167	struct dentry *dbgfs_dir;168	struct dentry *dbgfs_stats;169#endif170};171 172/*173 * struct sun8i_cipher_req_ctx - context for a skcipher request174 * @t_src:		list of mapped SGs with their size175 * @t_dst:		list of mapped SGs with their size176 * @p_key:		DMA address of the key177 * @p_iv:		DMA address of the IVs178 * @niv:		Number of IVs DMA mapped179 * @method:		current algorithm for this request180 * @op_mode:		op_mode for this request181 * @op_dir:		direction (encrypt vs decrypt) for this request182 * @flow:		the flow to use for this request183 * @ivlen:		size of IVs184 * @keylen:		keylen for this request185 * @fallback_req:	request struct for invoking the fallback skcipher TFM186 */187struct sun8i_cipher_req_ctx {188	struct sginfo t_src[MAX_SG];189	struct sginfo t_dst[MAX_SG];190	u32 p_key;191	u32 p_iv[MAX_SG];192	int niv;193	u32 method;194	u32 op_mode;195	u32 op_dir;196	int flow;197	unsigned int ivlen;198	unsigned int keylen;199	struct skcipher_request fallback_req;   // keep at the end200};201 202/*203 * struct sun8i_cipher_tfm_ctx - context for a skcipher TFM204 * @key:		pointer to key data205 * @keylen:		len of the key206 * @ss:			pointer to the private data of driver handling this TFM207 * @fallback_tfm:	pointer to the fallback TFM208 */209struct sun8i_cipher_tfm_ctx {210	u32 *key;211	u32 keylen;212	struct sun8i_ss_dev *ss;213	struct crypto_skcipher *fallback_tfm;214};215 216/*217 * struct sun8i_ss_prng_ctx - context for PRNG TFM218 * @seed:	The seed to use219 * @slen:	The size of the seed220 */221struct sun8i_ss_rng_tfm_ctx {222	void *seed;223	unsigned int slen;224};225 226/*227 * struct sun8i_ss_hash_tfm_ctx - context for an ahash TFM228 * @fallback_tfm:	pointer to the fallback TFM229 * @ss:			pointer to the private data of driver handling this TFM230 */231struct sun8i_ss_hash_tfm_ctx {232	struct crypto_ahash *fallback_tfm;233	struct sun8i_ss_dev *ss;234	u8 *ipad;235	u8 *opad;236	u8 key[SHA256_BLOCK_SIZE];237	int keylen;238};239 240/*241 * struct sun8i_ss_hash_reqctx - context for an ahash request242 * @t_src:	list of DMA address and size for source SGs243 * @t_dst:	list of DMA address and size for destination SGs244 * @fallback_req:	pre-allocated fallback request245 * @method:	the register value for the algorithm used by this request246 * @flow:	the flow to use for this request247 */248struct sun8i_ss_hash_reqctx {249	struct sginfo t_src[MAX_SG];250	struct sginfo t_dst[MAX_SG];251	struct ahash_request fallback_req;252	u32 method;253	int flow;254};255 256/*257 * struct sun8i_ss_alg_template - crypto_alg template258 * @type:		the CRYPTO_ALG_TYPE for this template259 * @ss_algo_id:		the SS_ID for this template260 * @ss_blockmode:	the type of block operation SS_ID261 * @ss:			pointer to the sun8i_ss_dev structure associated with262 *			this template263 * @alg:		one of sub struct must be used264 * @stat_req:		number of request done on this template265 * @stat_fb:		number of request which has fallbacked266 * @stat_bytes:		total data size done by this template267 */268struct sun8i_ss_alg_template {269	u32 type;270	u32 ss_algo_id;271	u32 ss_blockmode;272	struct sun8i_ss_dev *ss;273	union {274		struct skcipher_engine_alg skcipher;275		struct rng_alg rng;276		struct ahash_engine_alg hash;277	} alg;278	unsigned long stat_req;279	unsigned long stat_fb;280	unsigned long stat_bytes;281	unsigned long stat_fb_len;282	unsigned long stat_fb_sglen;283	unsigned long stat_fb_align;284	unsigned long stat_fb_sgnum;285	char fbname[CRYPTO_MAX_ALG_NAME];286};287 288int sun8i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,289			unsigned int keylen);290int sun8i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,291			 unsigned int keylen);292int sun8i_ss_cipher_init(struct crypto_tfm *tfm);293void sun8i_ss_cipher_exit(struct crypto_tfm *tfm);294int sun8i_ss_handle_cipher_request(struct crypto_engine *engine, void *areq);295int sun8i_ss_skdecrypt(struct skcipher_request *areq);296int sun8i_ss_skencrypt(struct skcipher_request *areq);297 298int sun8i_ss_get_engine_number(struct sun8i_ss_dev *ss);299 300int sun8i_ss_run_task(struct sun8i_ss_dev *ss, struct sun8i_cipher_req_ctx *rctx, const char *name);301int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,302			   unsigned int slen, u8 *dst, unsigned int dlen);303int sun8i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);304int sun8i_ss_prng_init(struct crypto_tfm *tfm);305void sun8i_ss_prng_exit(struct crypto_tfm *tfm);306 307int sun8i_ss_hash_init_tfm(struct crypto_ahash *tfm);308void sun8i_ss_hash_exit_tfm(struct crypto_ahash *tfm);309int sun8i_ss_hash_init(struct ahash_request *areq);310int sun8i_ss_hash_export(struct ahash_request *areq, void *out);311int sun8i_ss_hash_import(struct ahash_request *areq, const void *in);312int sun8i_ss_hash_final(struct ahash_request *areq);313int sun8i_ss_hash_update(struct ahash_request *areq);314int sun8i_ss_hash_finup(struct ahash_request *areq);315int sun8i_ss_hash_digest(struct ahash_request *areq);316int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq);317int sun8i_ss_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,318			 unsigned int keylen);319