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 */
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 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 */
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_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 */
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_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 */
109struct 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 * @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 */
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 template
133 * @type:		the CRYPTO_ALG_TYPE for this template
134 * @blockmode:		the type of block operation
135 * @mc:			pointer to the meson_dev structure associated with this template
136 * @alg:		one of sub struct must be used
137 * @stat_req:		number of request done on this template
138 * @stat_fb:		total of all data len done on this template
139 */
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_DEBUG
148	unsigned long stat_req;
149	unsigned long stat_fb;
150#endif
151};
152
153int meson_enqueue(struct crypto_async_request *areq, u32 type);
154
155int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
156		     unsigned int keylen);
157int meson_cipher_init(struct crypto_tfm *tfm);
158void meson_cipher_exit(struct crypto_tfm *tfm);
159int meson_skdecrypt(struct skcipher_request *areq);
160int meson_skencrypt(struct skcipher_request *areq);
161int meson_handle_cipher_request(struct crypto_engine *engine, void *areq);
162