160 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/*3 * amlogic.h - hardware cryptographic offloader for Amlogic SoC4 *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 115#define MODE_AES_128 0x816#define MODE_AES_192 0x917#define MODE_AES_256 0xa18 19#define MESON_DECRYPT 020#define MESON_ENCRYPT 121 22#define MESON_OPMODE_ECB 023#define MESON_OPMODE_CBC 124 25#define MAXFLOW 226 27#define MAXDESC 6428 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 operations35 * Note that without datasheet, some are unknown36 * @t_status: Descriptor of the cipher operation (see description below)37 * @t_src: Physical address of data to read38 * @t_dst: Physical address of data to write39 * t_status is segmented like this:40 * @len: 0-16 length of data to operate41 * @irq: 17 Ignored by hardware42 * @eoc: 18 End means the descriptor is the last43 * @loop: 19 Unknown44 * @mode: 20-23 Type of algorithm (AES, SHA)45 * @begin: 24 Unknown46 * @end: 25 Unknown47 * @op_mode: 26-27 Blockmode (CBC, ECB)48 * @enc: 28 0 means decryption, 1 is for encryption49 * @block: 29 Unknown50 * @error: 30 Unknown51 * @owner: 31 owner of the descriptor, 1 own by HW52 */53struct 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 flow61 * @engine: ptr to the crypto_engine for this flow62 * @keylen: keylen for this flow operation63 * @complete: completion for the current task on this flow64 * @status: set to 1 by interrupt if task is done65 * @t_phy: Physical address of task66 * @tl: pointer to the current ce_task for this flow67 * @stat_req: number of request done by this flow68 */69struct 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_DEBUG77 unsigned long stat_req;78#endif79};80 81/*82 * struct meson_dev - main container for all this driver information83 * @base: base address of amlogic-crypto84 * @busclk: bus clock for amlogic-crypto85 * @dev: the platform device86 * @chanlist: array of all flow87 * @flow: flow to use in next request88 * @irqs: IRQ numbers for amlogic-crypto89 * @dbgfs_dir: Debugfs dentry for statistic directory90 * @dbgfs_stats: Debugfs dentry for statistic counters91 */92struct 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_DEBUG100 struct dentry *dbgfs_dir;101#endif102};103 104/*105 * struct meson_cipher_req_ctx - context for a skcipher request106 * @op_dir: direction (encrypt vs decrypt) for this request107 * @flow: the flow to use for this request108 */109struct meson_cipher_req_ctx {110 u32 op_dir;111 int flow;112 struct skcipher_request fallback_req; // keep at the end113};114 115/*116 * struct meson_cipher_tfm_ctx - context for a skcipher TFM117 * @key: pointer to key data118 * @keylen: len of the key119 * @keymode: The keymode(type and size of key) associated with this TFM120 * @mc: pointer to the private data of driver handling this TFM121 * @fallback_tfm: pointer to the fallback TFM122 */123struct meson_cipher_tfm_ctx {124 u32 *key;125 u32 keylen;126 u32 keymode;127 struct meson_dev *mc;128 struct crypto_skcipher *fallback_tfm;129};130 131/*132 * struct meson_alg_template - crypto_alg template133 * @type: the CRYPTO_ALG_TYPE for this template134 * @blockmode: the type of block operation135 * @mc: pointer to the meson_dev structure associated with this template136 * @alg: one of sub struct must be used137 * @stat_req: number of request done on this template138 * @stat_fb: total of all data len done on this template139 */140struct meson_alg_template {141 u32 type;142 u32 blockmode;143 union {144 struct skcipher_engine_alg skcipher;145 } alg;146 struct meson_dev *mc;147#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG148 unsigned long stat_req;149 unsigned long stat_fb;150#endif151};152 153int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,154 unsigned int keylen);155int meson_cipher_init(struct crypto_tfm *tfm);156void meson_cipher_exit(struct crypto_tfm *tfm);157int meson_skdecrypt(struct skcipher_request *areq);158int meson_skencrypt(struct skcipher_request *areq);159int meson_handle_cipher_request(struct crypto_engine *engine, void *areq);160