Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * amlogic.h - hardware cryptographic offloader for Amlogic SoC
0004  *
0005  * Copyright (C) 2018-2019 Corentin LABBE <clabbe@baylibre.com>
0006  */
0007 #include <crypto/aes.h>
0008 #include <crypto/engine.h>
0009 #include <crypto/skcipher.h>
0010 #include <linux/debugfs.h>
0011 #include <linux/crypto.h>
0012 #include <linux/scatterlist.h>
0013 
0014 #define MODE_KEY 1
0015 #define MODE_AES_128 0x8
0016 #define MODE_AES_192 0x9
0017 #define MODE_AES_256 0xa
0018 
0019 #define MESON_DECRYPT 0
0020 #define MESON_ENCRYPT 1
0021 
0022 #define MESON_OPMODE_ECB 0
0023 #define MESON_OPMODE_CBC 1
0024 
0025 #define MAXFLOW 2
0026 
0027 #define MAXDESC 64
0028 
0029 #define DESC_LAST BIT(18)
0030 #define DESC_ENCRYPTION BIT(28)
0031 #define DESC_OWN BIT(31)
0032 
0033 /*
0034  * struct meson_desc - Descriptor for DMA operations
0035  * Note that without datasheet, some are unknown
0036  * @t_status:   Descriptor of the cipher operation (see description below)
0037  * @t_src:  Physical address of data to read
0038  * @t_dst:  Physical address of data to write
0039  * t_status is segmented like this:
0040  * @len:    0-16    length of data to operate
0041  * @irq:    17  Ignored by hardware
0042  * @eoc:    18  End means the descriptor is the last
0043  * @loop:   19  Unknown
0044  * @mode:   20-23   Type of algorithm (AES, SHA)
0045  * @begin:  24  Unknown
0046  * @end:    25  Unknown
0047  * @op_mode:    26-27   Blockmode (CBC, ECB)
0048  * @enc:    28  0 means decryption, 1 is for encryption
0049  * @block:  29  Unknown
0050  * @error:  30  Unknown
0051  * @owner:  31  owner of the descriptor, 1 own by HW
0052  */
0053 struct meson_desc {
0054     __le32 t_status;
0055     __le32 t_src;
0056     __le32 t_dst;
0057 };
0058 
0059 /*
0060  * struct meson_flow - Information used by each flow
0061  * @engine: ptr to the crypto_engine for this flow
0062  * @keylen: keylen for this flow operation
0063  * @complete:   completion for the current task on this flow
0064  * @status: set to 1 by interrupt if task is done
0065  * @t_phy:  Physical address of task
0066  * @tl:     pointer to the current ce_task for this flow
0067  * @stat_req:   number of request done by this flow
0068  */
0069 struct meson_flow {
0070     struct crypto_engine *engine;
0071     struct completion complete;
0072     int status;
0073     unsigned int keylen;
0074     dma_addr_t t_phy;
0075     struct meson_desc *tl;
0076 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
0077     unsigned long stat_req;
0078 #endif
0079 };
0080 
0081 /*
0082  * struct meson_dev - main container for all this driver information
0083  * @base:   base address of amlogic-crypto
0084  * @busclk: bus clock for amlogic-crypto
0085  * @dev:    the platform device
0086  * @chanlist:   array of all flow
0087  * @flow:   flow to use in next request
0088  * @irqs:   IRQ numbers for amlogic-crypto
0089  * @dbgfs_dir:  Debugfs dentry for statistic directory
0090  * @dbgfs_stats: Debugfs dentry for statistic counters
0091  */
0092 struct meson_dev {
0093     void __iomem *base;
0094     struct clk *busclk;
0095     struct device *dev;
0096     struct meson_flow *chanlist;
0097     atomic_t flow;
0098     int *irqs;
0099 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
0100     struct dentry *dbgfs_dir;
0101 #endif
0102 };
0103 
0104 /*
0105  * struct meson_cipher_req_ctx - context for a skcipher request
0106  * @op_dir: direction (encrypt vs decrypt) for this request
0107  * @flow:   the flow to use for this request
0108  */
0109 struct meson_cipher_req_ctx {
0110     u32 op_dir;
0111     int flow;
0112     struct skcipher_request fallback_req;   // keep at the end
0113 };
0114 
0115 /*
0116  * struct meson_cipher_tfm_ctx - context for a skcipher TFM
0117  * @enginectx:      crypto_engine used by this TFM
0118  * @key:        pointer to key data
0119  * @keylen:     len of the key
0120  * @keymode:        The keymode(type and size of key) associated with this TFM
0121  * @mc:         pointer to the private data of driver handling this TFM
0122  * @fallback_tfm:   pointer to the fallback TFM
0123  */
0124 struct meson_cipher_tfm_ctx {
0125     struct crypto_engine_ctx enginectx;
0126     u32 *key;
0127     u32 keylen;
0128     u32 keymode;
0129     struct meson_dev *mc;
0130     struct crypto_skcipher *fallback_tfm;
0131 };
0132 
0133 /*
0134  * struct meson_alg_template - crypto_alg template
0135  * @type:       the CRYPTO_ALG_TYPE for this template
0136  * @blockmode:      the type of block operation
0137  * @mc:         pointer to the meson_dev structure associated with this template
0138  * @alg:        one of sub struct must be used
0139  * @stat_req:       number of request done on this template
0140  * @stat_fb:        total of all data len done on this template
0141  */
0142 struct meson_alg_template {
0143     u32 type;
0144     u32 blockmode;
0145     union {
0146         struct skcipher_alg skcipher;
0147     } alg;
0148     struct meson_dev *mc;
0149 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
0150     unsigned long stat_req;
0151     unsigned long stat_fb;
0152 #endif
0153 };
0154 
0155 int meson_enqueue(struct crypto_async_request *areq, u32 type);
0156 
0157 int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
0158              unsigned int keylen);
0159 int meson_cipher_init(struct crypto_tfm *tfm);
0160 void meson_cipher_exit(struct crypto_tfm *tfm);
0161 int meson_skdecrypt(struct skcipher_request *areq);
0162 int meson_skencrypt(struct skcipher_request *areq);