/* SPDX-License-Identifier: GPL-2.0 */1/*2* amlogic.h - hardware cryptographic offloader for Amlogic SoC3*4* Copyright (C) 2018-2019 Corentin LABBE <[email protected]>5*/6#include <crypto/aes.h>7#include <crypto/engine.h>8#include <crypto/skcipher.h>9#include <linux/debugfs.h>10#include <linux/crypto.h>11#include <linux/scatterlist.h>1213#define MODE_KEY 114#define MODE_AES_128 0x815#define MODE_AES_192 0x916#define MODE_AES_256 0xa1718#define MESON_DECRYPT 019#define MESON_ENCRYPT 12021#define MESON_OPMODE_ECB 022#define MESON_OPMODE_CBC 12324#define MAXFLOW 22526#define MAXDESC 642728#define DESC_LAST BIT(18)29#define DESC_ENCRYPTION BIT(28)30#define DESC_OWN BIT(31)3132/*33* struct meson_desc - Descriptor for DMA operations34* Note that without datasheet, some are unknown35* @t_status: Descriptor of the cipher operation (see description below)36* @t_src: Physical address of data to read37* @t_dst: Physical address of data to write38* t_status is segmented like this:39* @len: 0-16 length of data to operate40* @irq: 17 Ignored by hardware41* @eoc: 18 End means the descriptor is the last42* @loop: 19 Unknown43* @mode: 20-23 Type of algorithm (AES, SHA)44* @begin: 24 Unknown45* @end: 25 Unknown46* @op_mode: 26-27 Blockmode (CBC, ECB)47* @enc: 28 0 means decryption, 1 is for encryption48* @block: 29 Unknown49* @error: 30 Unknown50* @owner: 31 owner of the descriptor, 1 own by HW51*/52struct meson_desc {53__le32 t_status;54__le32 t_src;55__le32 t_dst;56};5758/*59* struct meson_flow - Information used by each flow60* @engine: ptr to the crypto_engine for this flow61* @keylen: keylen for this flow operation62* @complete: completion for the current task on this flow63* @status: set to 1 by interrupt if task is done64* @t_phy: Physical address of task65* @tl: pointer to the current ce_task for this flow66* @stat_req: number of request done by this flow67*/68struct meson_flow {69struct crypto_engine *engine;70struct completion complete;71int status;72unsigned int keylen;73dma_addr_t t_phy;74struct meson_desc *tl;75#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG76unsigned long stat_req;77#endif78};7980/*81* struct meson_dev - main container for all this driver information82* @base: base address of amlogic-crypto83* @busclk: bus clock for amlogic-crypto84* @dev: the platform device85* @chanlist: array of all flow86* @flow: flow to use in next request87* @irqs: IRQ numbers for amlogic-crypto88* @dbgfs_dir: Debugfs dentry for statistic directory89* @dbgfs_stats: Debugfs dentry for statistic counters90*/91struct meson_dev {92void __iomem *base;93struct clk *busclk;94struct device *dev;95struct meson_flow *chanlist;96atomic_t flow;97int irqs[MAXFLOW];98#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG99struct dentry *dbgfs_dir;100#endif101};102103/*104* struct meson_cipher_req_ctx - context for a skcipher request105* @op_dir: direction (encrypt vs decrypt) for this request106* @flow: the flow to use for this request107*/108struct meson_cipher_req_ctx {109u32 op_dir;110int flow;111struct skcipher_request fallback_req; // keep at the end112};113114/*115* struct meson_cipher_tfm_ctx - context for a skcipher TFM116* @key: pointer to key data117* @keylen: len of the key118* @keymode: The keymode(type and size of key) associated with this TFM119* @mc: pointer to the private data of driver handling this TFM120* @fallback_tfm: pointer to the fallback TFM121*/122struct meson_cipher_tfm_ctx {123u32 *key;124u32 keylen;125u32 keymode;126struct meson_dev *mc;127struct crypto_skcipher *fallback_tfm;128};129130/*131* struct meson_alg_template - crypto_alg template132* @type: the CRYPTO_ALG_TYPE for this template133* @blockmode: the type of block operation134* @mc: pointer to the meson_dev structure associated with this template135* @alg: one of sub struct must be used136* @stat_req: number of request done on this template137* @stat_fb: total of all data len done on this template138*/139struct meson_alg_template {140u32 type;141u32 blockmode;142union {143struct skcipher_engine_alg skcipher;144} alg;145struct meson_dev *mc;146#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG147unsigned long stat_req;148unsigned long stat_fb;149#endif150};151152int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,153unsigned int keylen);154int meson_cipher_init(struct crypto_tfm *tfm);155void meson_cipher_exit(struct crypto_tfm *tfm);156int meson_skdecrypt(struct skcipher_request *areq);157int meson_skencrypt(struct skcipher_request *areq);158int meson_handle_cipher_request(struct crypto_engine *engine, void *areq);159160161