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