1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * amlogic.h - hardware cryptographic offloader for Amlogic SoC 4 * 5 * Copyright (C) 2018-2019 Corentin LABBE <clabbe@baylibre.com> 6 */ 7 #include <crypto/aes.h> 8 #include <crypto/engine.h> 9 #include <crypto/skcipher.h> 10 #include <linux/debugfs.h> 11 #include <linux/crypto.h> 12 #include <linux/scatterlist.h> 13 14 #define MODE_KEY 1 15 #define MODE_AES_128 0x8 16 #define MODE_AES_192 0x9 17 #define MODE_AES_256 0xa 18 19 #define MESON_DECRYPT 0 20 #define MESON_ENCRYPT 1 21 22 #define MESON_OPMODE_ECB 0 23 #define MESON_OPMODE_CBC 1 24 25 #define MAXFLOW 2 26 27 #define MAXDESC 64 28 29 #define DESC_LAST BIT(18) 30 #define DESC_ENCRYPTION BIT(28) 31 #define DESC_OWN BIT(31) 32 33 /* 34 * struct meson_desc - Descriptor for DMA operations 35 * Note that without datasheet, some are unknown 36 * @t_status: Descriptor of the cipher operation (see description below) 37 * @t_src: Physical address of data to read 38 * @t_dst: Physical address of data to write 39 * t_status is segmented like this: 40 * @len: 0-16 length of data to operate 41 * @irq: 17 Ignored by hardware 42 * @eoc: 18 End means the descriptor is the last 43 * @loop: 19 Unknown 44 * @mode: 20-23 Type of algorithm (AES, SHA) 45 * @begin: 24 Unknown 46 * @end: 25 Unknown 47 * @op_mode: 26-27 Blockmode (CBC, ECB) 48 * @enc: 28 0 means decryption, 1 is for encryption 49 * @block: 29 Unknown 50 * @error: 30 Unknown 51 * @owner: 31 owner of the descriptor, 1 own by HW 52 */ 53 struct meson_desc { 54 __le32 t_status; 55 __le32 t_src; 56 __le32 t_dst; 57 }; 58 59 /* 60 * struct meson_flow - Information used by each flow 61 * @engine: ptr to the crypto_engine for this flow 62 * @keylen: keylen for this flow operation 63 * @complete: completion for the current task on this flow 64 * @status: set to 1 by interrupt if task is done 65 * @t_phy: Physical address of task 66 * @tl: pointer to the current ce_task for this flow 67 * @stat_req: number of request done by this flow 68 */ 69 struct meson_flow { 70 struct crypto_engine *engine; 71 struct completion complete; 72 int status; 73 unsigned int keylen; 74 dma_addr_t t_phy; 75 struct meson_desc *tl; 76 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG 77 unsigned long stat_req; 78 #endif 79 }; 80 81 /* 82 * struct meson_dev - main container for all this driver information 83 * @base: base address of amlogic-crypto 84 * @busclk: bus clock for amlogic-crypto 85 * @dev: the platform device 86 * @chanlist: array of all flow 87 * @flow: flow to use in next request 88 * @irqs: IRQ numbers for amlogic-crypto 89 * @dbgfs_dir: Debugfs dentry for statistic directory 90 * @dbgfs_stats: Debugfs dentry for statistic counters 91 */ 92 struct meson_dev { 93 void __iomem *base; 94 struct clk *busclk; 95 struct device *dev; 96 struct meson_flow *chanlist; 97 atomic_t flow; 98 int irqs[MAXFLOW]; 99 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG 100 struct dentry *dbgfs_dir; 101 #endif 102 }; 103 104 /* 105 * struct meson_cipher_req_ctx - context for a skcipher request 106 * @op_dir: direction (encrypt vs decrypt) for this request 107 * @flow: the flow to use for this request 108 */ 109 struct meson_cipher_req_ctx { 110 u32 op_dir; 111 int flow; 112 struct skcipher_request fallback_req; // keep at the end 113 }; 114 115 /* 116 * struct meson_cipher_tfm_ctx - context for a skcipher TFM 117 * @enginectx: crypto_engine used by this TFM 118 * @key: pointer to key data 119 * @keylen: len of the key 120 * @keymode: The keymode(type and size of key) associated with this TFM 121 * @mc: pointer to the private data of driver handling this TFM 122 * @fallback_tfm: pointer to the fallback TFM 123 */ 124 struct meson_cipher_tfm_ctx { 125 struct crypto_engine_ctx enginectx; 126 u32 *key; 127 u32 keylen; 128 u32 keymode; 129 struct meson_dev *mc; 130 struct crypto_skcipher *fallback_tfm; 131 }; 132 133 /* 134 * struct meson_alg_template - crypto_alg template 135 * @type: the CRYPTO_ALG_TYPE for this template 136 * @blockmode: the type of block operation 137 * @mc: pointer to the meson_dev structure associated with this template 138 * @alg: one of sub struct must be used 139 * @stat_req: number of request done on this template 140 * @stat_fb: total of all data len done on this template 141 */ 142 struct meson_alg_template { 143 u32 type; 144 u32 blockmode; 145 union { 146 struct skcipher_alg skcipher; 147 } alg; 148 struct meson_dev *mc; 149 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG 150 unsigned long stat_req; 151 unsigned long stat_fb; 152 #endif 153 }; 154 155 int meson_enqueue(struct crypto_async_request *areq, u32 type); 156 157 int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, 158 unsigned int keylen); 159 int meson_cipher_init(struct crypto_tfm *tfm); 160 void meson_cipher_exit(struct crypto_tfm *tfm); 161 int meson_skdecrypt(struct skcipher_request *areq); 162 int meson_skencrypt(struct skcipher_request *areq); 163