1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * sun8i-ss.h - hardware cryptographic offloader for 4 * Allwinner A80/A83T SoC 5 * 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 1 22 23 #define SS_ENCRYPTION 0 24 #define SS_DECRYPTION BIT(6) 25 26 #define SS_ALG_AES 0 27 #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 0x00 36 #define SS_INT_CTL_REG 0x04 37 #define SS_INT_STA_REG 0x08 38 #define SS_KEY_ADR_REG 0x10 39 #define SS_IV_ADR_REG 0x18 40 #define SS_SRC_ADR_REG 0x20 41 #define SS_DST_ADR_REG 0x28 42 #define SS_LEN_ADR_REG 0x30 43 44 #define SS_ID_NOTSUPP 0xFF 45 46 #define SS_ID_CIPHER_AES 0 47 #define SS_ID_CIPHER_DES 1 48 #define SS_ID_CIPHER_DES3 2 49 #define SS_ID_CIPHER_MAX 3 50 51 #define SS_ID_OP_ECB 0 52 #define SS_ID_OP_CBC 1 53 #define SS_ID_OP_MAX 2 54 55 #define SS_AES_128BITS 0 56 #define SS_AES_192BITS 1 57 #define SS_AES_256BITS 2 58 59 #define SS_OP_ECB 0 60 #define SS_OP_CBC (1 << 13) 61 62 #define SS_ID_HASH_MD5 0 63 #define SS_ID_HASH_SHA1 1 64 #define SS_ID_HASH_SHA224 2 65 #define SS_ID_HASH_SHA256 3 66 #define SS_ID_HASH_MAX 4 67 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 8 74 75 #define MAXFLOW 2 76 77 #define SS_MAX_CLOCKS 2 78 79 #define SS_DIE_ID_SHIFT 20 80 #define SS_DIE_ID_MASK 0x07 81 82 #define PRNG_DATA_SIZE (160 / 8) 83 #define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8) 84 85 /* 86 * struct ss_clock - Describe clocks used by sun8i-ss 87 * @name: Name of clock needed by this variant 88 * @freq: Frequency to set for each clock 89 * @max_freq: Maximum frequency for each clock 90 */ 91 struct ss_clock { 92 const char *name; 93 unsigned long freq; 94 unsigned long max_freq; 95 }; 96 97 /* 98 * struct ss_variant - Describe SS capability for each variant hardware 99 * @alg_cipher: list of supported ciphers. for each SS_ID_ this will give the 100 * coresponding SS_ALG_XXX value 101 * @alg_hash: list of supported hashes. for each SS_ID_ this will give the 102 * corresponding SS_ALG_XXX value 103 * @op_mode: list of supported block modes 104 * @ss_clks: list of clock needed by this variant 105 */ 106 struct ss_variant { 107 char alg_cipher[SS_ID_CIPHER_MAX]; 108 char alg_hash[SS_ID_HASH_MAX]; 109 u32 op_mode[SS_ID_OP_MAX]; 110 struct ss_clock ss_clks[SS_MAX_CLOCKS]; 111 }; 112 113 struct sginfo { 114 u32 addr; 115 u32 len; 116 }; 117 118 /* 119 * struct sun8i_ss_flow - Information used by each flow 120 * @engine: ptr to the crypto_engine for this flow 121 * @complete: completion for the current task on this flow 122 * @status: set to 1 by interrupt if task is done 123 * @stat_req: number of request done by this flow 124 * @iv: list of IV to use for each step 125 * @biv: buffer which contain the backuped IV 126 * @pad: padding buffer for hash operations 127 * @result: buffer for storing the result of hash operations 128 */ 129 struct sun8i_ss_flow { 130 struct crypto_engine *engine; 131 struct completion complete; 132 int status; 133 u8 *iv[MAX_SG]; 134 u8 *biv; 135 void *pad; 136 void *result; 137 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG 138 unsigned long stat_req; 139 #endif 140 }; 141 142 /* 143 * struct sun8i_ss_dev - main container for all this driver information 144 * @base: base address of SS 145 * @ssclks: clocks used by SS 146 * @reset: pointer to reset controller 147 * @dev: the platform device 148 * @mlock: Control access to device registers 149 * @flows: array of all flow 150 * @flow: flow to use in next request 151 * @variant: pointer to variant specific data 152 * @dbgfs_dir: Debugfs dentry for statistic directory 153 * @dbgfs_stats: Debugfs dentry for statistic counters 154 */ 155 struct sun8i_ss_dev { 156 void __iomem *base; 157 struct clk *ssclks[SS_MAX_CLOCKS]; 158 struct reset_control *reset; 159 struct device *dev; 160 struct mutex mlock; 161 struct sun8i_ss_flow *flows; 162 atomic_t flow; 163 const struct ss_variant *variant; 164 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG 165 struct dentry *dbgfs_dir; 166 struct dentry *dbgfs_stats; 167 #endif 168 }; 169 170 /* 171 * struct sun8i_cipher_req_ctx - context for a skcipher request 172 * @t_src: list of mapped SGs with their size 173 * @t_dst: list of mapped SGs with their size 174 * @p_key: DMA address of the key 175 * @p_iv: DMA address of the IVs 176 * @niv: Number of IVs DMA mapped 177 * @method: current algorithm for this request 178 * @op_mode: op_mode for this request 179 * @op_dir: direction (encrypt vs decrypt) for this request 180 * @flow: the flow to use for this request 181 * @ivlen: size of IVs 182 * @keylen: keylen for this request 183 * @fallback_req: request struct for invoking the fallback skcipher TFM 184 */ 185 struct sun8i_cipher_req_ctx { 186 struct sginfo t_src[MAX_SG]; 187 struct sginfo t_dst[MAX_SG]; 188 u32 p_key; 189 u32 p_iv[MAX_SG]; 190 int niv; 191 u32 method; 192 u32 op_mode; 193 u32 op_dir; 194 int flow; 195 unsigned int ivlen; 196 unsigned int keylen; 197 struct skcipher_request fallback_req; // keep at the end 198 }; 199 200 /* 201 * struct sun8i_cipher_tfm_ctx - context for a skcipher TFM 202 * @enginectx: crypto_engine used by this TFM 203 * @key: pointer to key data 204 * @keylen: len of the key 205 * @ss: pointer to the private data of driver handling this TFM 206 * @fallback_tfm: pointer to the fallback TFM 207 * 208 * enginectx must be the first element 209 */ 210 struct sun8i_cipher_tfm_ctx { 211 struct crypto_engine_ctx enginectx; 212 u32 *key; 213 u32 keylen; 214 struct sun8i_ss_dev *ss; 215 struct crypto_skcipher *fallback_tfm; 216 }; 217 218 /* 219 * struct sun8i_ss_prng_ctx - context for PRNG TFM 220 * @seed: The seed to use 221 * @slen: The size of the seed 222 */ 223 struct sun8i_ss_rng_tfm_ctx { 224 void *seed; 225 unsigned int slen; 226 }; 227 228 /* 229 * struct sun8i_ss_hash_tfm_ctx - context for an ahash TFM 230 * @enginectx: crypto_engine used by this TFM 231 * @fallback_tfm: pointer to the fallback TFM 232 * @ss: pointer to the private data of driver handling this TFM 233 * 234 * enginectx must be the first element 235 */ 236 struct sun8i_ss_hash_tfm_ctx { 237 struct crypto_engine_ctx enginectx; 238 struct crypto_ahash *fallback_tfm; 239 struct sun8i_ss_dev *ss; 240 }; 241 242 /* 243 * struct sun8i_ss_hash_reqctx - context for an ahash request 244 * @t_src: list of DMA address and size for source SGs 245 * @t_dst: list of DMA address and size for destination SGs 246 * @fallback_req: pre-allocated fallback request 247 * @method: the register value for the algorithm used by this request 248 * @flow: the flow to use for this request 249 */ 250 struct sun8i_ss_hash_reqctx { 251 struct sginfo t_src[MAX_SG]; 252 struct sginfo t_dst[MAX_SG]; 253 struct ahash_request fallback_req; 254 u32 method; 255 int flow; 256 }; 257 258 /* 259 * struct sun8i_ss_alg_template - crypto_alg template 260 * @type: the CRYPTO_ALG_TYPE for this template 261 * @ss_algo_id: the SS_ID for this template 262 * @ss_blockmode: the type of block operation SS_ID 263 * @ss: pointer to the sun8i_ss_dev structure associated with 264 * this template 265 * @alg: one of sub struct must be used 266 * @stat_req: number of request done on this template 267 * @stat_fb: number of request which has fallbacked 268 * @stat_bytes: total data size done by this template 269 */ 270 struct sun8i_ss_alg_template { 271 u32 type; 272 u32 ss_algo_id; 273 u32 ss_blockmode; 274 struct sun8i_ss_dev *ss; 275 union { 276 struct skcipher_alg skcipher; 277 struct rng_alg rng; 278 struct ahash_alg hash; 279 } alg; 280 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG 281 unsigned long stat_req; 282 unsigned long stat_fb; 283 unsigned long stat_bytes; 284 #endif 285 }; 286 287 int sun8i_ss_enqueue(struct crypto_async_request *areq, u32 type); 288 289 int sun8i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, 290 unsigned int keylen); 291 int sun8i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key, 292 unsigned int keylen); 293 int sun8i_ss_cipher_init(struct crypto_tfm *tfm); 294 void sun8i_ss_cipher_exit(struct crypto_tfm *tfm); 295 int sun8i_ss_skdecrypt(struct skcipher_request *areq); 296 int sun8i_ss_skencrypt(struct skcipher_request *areq); 297 298 int sun8i_ss_get_engine_number(struct sun8i_ss_dev *ss); 299 300 int sun8i_ss_run_task(struct sun8i_ss_dev *ss, struct sun8i_cipher_req_ctx *rctx, const char *name); 301 int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src, 302 unsigned int slen, u8 *dst, unsigned int dlen); 303 int sun8i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen); 304 int sun8i_ss_prng_init(struct crypto_tfm *tfm); 305 void sun8i_ss_prng_exit(struct crypto_tfm *tfm); 306 307 int sun8i_ss_hash_crainit(struct crypto_tfm *tfm); 308 void sun8i_ss_hash_craexit(struct crypto_tfm *tfm); 309 int sun8i_ss_hash_init(struct ahash_request *areq); 310 int sun8i_ss_hash_export(struct ahash_request *areq, void *out); 311 int sun8i_ss_hash_import(struct ahash_request *areq, const void *in); 312 int sun8i_ss_hash_final(struct ahash_request *areq); 313 int sun8i_ss_hash_update(struct ahash_request *areq); 314 int sun8i_ss_hash_finup(struct ahash_request *areq); 315 int sun8i_ss_hash_digest(struct ahash_request *areq); 316 int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq); 317