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; 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 }; 113 114 /* 115 * struct meson_cipher_tfm_ctx - context for a skcipher TFM 116 * @enginectx: crypto_engine used by this TFM 117 * @key: pointer to key data 118 * @keylen: len of the key 119 * @keymode: The keymode(type and size of key) associated with this TFM 120 * @mc: pointer to the private data of driver handling this TFM 121 * @fallback_tfm: pointer to the fallback TFM 122 */ 123 struct meson_cipher_tfm_ctx { 124 struct crypto_engine_ctx enginectx; 125 u32 *key; 126 u32 keylen; 127 u32 keymode; 128 struct meson_dev *mc; 129 struct crypto_sync_skcipher *fallback_tfm; 130 }; 131 132 /* 133 * struct meson_alg_template - crypto_alg template 134 * @type: the CRYPTO_ALG_TYPE for this template 135 * @blockmode: the type of block operation 136 * @mc: pointer to the meson_dev structure associated with this template 137 * @alg: one of sub struct must be used 138 * @stat_req: number of request done on this template 139 * @stat_fb: total of all data len done on this template 140 */ 141 struct meson_alg_template { 142 u32 type; 143 u32 blockmode; 144 union { 145 struct skcipher_alg skcipher; 146 } alg; 147 struct meson_dev *mc; 148 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG 149 unsigned long stat_req; 150 unsigned long stat_fb; 151 #endif 152 }; 153 154 int meson_enqueue(struct crypto_async_request *areq, u32 type); 155 156 int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, 157 unsigned int keylen); 158 int meson_cipher_init(struct crypto_tfm *tfm); 159 void meson_cipher_exit(struct crypto_tfm *tfm); 160 int meson_skdecrypt(struct skcipher_request *areq); 161 int meson_skencrypt(struct skcipher_request *areq); 162