146c5338dSCorentin Labbe /* SPDX-License-Identifier: GPL-2.0 */ 246c5338dSCorentin Labbe /* 346c5338dSCorentin Labbe * sl3516-ce.h - hardware cryptographic offloader for cortina/gemini SoC 446c5338dSCorentin Labbe * 546c5338dSCorentin Labbe * Copyright (C) 2021 Corentin LABBE <clabbe@baylibre.com> 646c5338dSCorentin Labbe * 746c5338dSCorentin Labbe * General notes on this driver: 846c5338dSCorentin Labbe * Called either Crypto Acceleration Engine Module, Security Acceleration Engine 946c5338dSCorentin Labbe * or IPSEC module in the datasheet, it will be called Crypto Engine for short 1046c5338dSCorentin Labbe * in this driver. 1146c5338dSCorentin Labbe * The CE was designed to handle IPSEC and wifi(TKIP WEP) protocol. 1246c5338dSCorentin Labbe * It can handle AES, DES, 3DES, MD5, WEP, TKIP, SHA1, HMAC(MD5), HMAC(SHA1), 1346c5338dSCorentin Labbe * Michael cipher/digest suites. 1446c5338dSCorentin Labbe * It acts the same as a network hw, with both RX and TX chained descriptors. 1546c5338dSCorentin Labbe */ 1646c5338dSCorentin Labbe #include <crypto/aes.h> 1746c5338dSCorentin Labbe #include <crypto/engine.h> 1846c5338dSCorentin Labbe #include <crypto/scatterwalk.h> 1946c5338dSCorentin Labbe #include <crypto/skcipher.h> 2046c5338dSCorentin Labbe #include <linux/debugfs.h> 2146c5338dSCorentin Labbe #include <linux/hw_random.h> 2246c5338dSCorentin Labbe 2346c5338dSCorentin Labbe #define TQ0_TYPE_DATA 0 2446c5338dSCorentin Labbe #define TQ0_TYPE_CTRL BIT(0) 2546c5338dSCorentin Labbe #define TQ1_CIPHER BIT(1) 2646c5338dSCorentin Labbe #define TQ2_AUTH BIT(2) 2746c5338dSCorentin Labbe #define TQ3_IV BIT(3) 2846c5338dSCorentin Labbe #define TQ4_KEY0 BIT(4) 2946c5338dSCorentin Labbe #define TQ5_KEY4 BIT(5) 3046c5338dSCorentin Labbe #define TQ6_KEY6 BIT(6) 3146c5338dSCorentin Labbe #define TQ7_AKEY0 BIT(7) 3246c5338dSCorentin Labbe #define TQ8_AKEY2 BIT(8) 3346c5338dSCorentin Labbe #define TQ9_AKEY2 BIT(9) 3446c5338dSCorentin Labbe 3546c5338dSCorentin Labbe #define ECB_AES 0x2 3646c5338dSCorentin Labbe 3746c5338dSCorentin Labbe #define DESC_LAST 0x01 3846c5338dSCorentin Labbe #define DESC_FIRST 0x02 3946c5338dSCorentin Labbe 4046c5338dSCorentin Labbe #define IPSEC_ID 0x0000 4146c5338dSCorentin Labbe #define IPSEC_STATUS_REG 0x00a8 4246c5338dSCorentin Labbe #define IPSEC_RAND_NUM_REG 0x00ac 4346c5338dSCorentin Labbe #define IPSEC_DMA_DEVICE_ID 0xff00 4446c5338dSCorentin Labbe #define IPSEC_DMA_STATUS 0xff04 4546c5338dSCorentin Labbe #define IPSEC_TXDMA_CTRL 0xff08 4646c5338dSCorentin Labbe #define IPSEC_TXDMA_FIRST_DESC 0xff0c 4746c5338dSCorentin Labbe #define IPSEC_TXDMA_CURR_DESC 0xff10 4846c5338dSCorentin Labbe #define IPSEC_RXDMA_CTRL 0xff14 4946c5338dSCorentin Labbe #define IPSEC_RXDMA_FIRST_DESC 0xff18 5046c5338dSCorentin Labbe #define IPSEC_RXDMA_CURR_DESC 0xff1c 5146c5338dSCorentin Labbe #define IPSEC_TXDMA_BUF_ADDR 0xff28 5246c5338dSCorentin Labbe #define IPSEC_RXDMA_BUF_ADDR 0xff38 5346c5338dSCorentin Labbe #define IPSEC_RXDMA_BUF_SIZE 0xff30 5446c5338dSCorentin Labbe 5546c5338dSCorentin Labbe #define CE_ENCRYPTION 0x01 5646c5338dSCorentin Labbe #define CE_DECRYPTION 0x03 5746c5338dSCorentin Labbe 5846c5338dSCorentin Labbe #define MAXDESC 6 5946c5338dSCorentin Labbe 6046c5338dSCorentin Labbe #define DMA_STATUS_RS_EOFI BIT(22) 6146c5338dSCorentin Labbe #define DMA_STATUS_RS_PERR BIT(24) 6246c5338dSCorentin Labbe #define DMA_STATUS_RS_DERR BIT(25) 6346c5338dSCorentin Labbe #define DMA_STATUS_TS_EOFI BIT(27) 6446c5338dSCorentin Labbe #define DMA_STATUS_TS_PERR BIT(29) 6546c5338dSCorentin Labbe #define DMA_STATUS_TS_DERR BIT(30) 6646c5338dSCorentin Labbe 6746c5338dSCorentin Labbe #define TXDMA_CTRL_START BIT(31) 6846c5338dSCorentin Labbe #define TXDMA_CTRL_CONTINUE BIT(30) 6946c5338dSCorentin Labbe #define TXDMA_CTRL_CHAIN_MODE BIT(29) 7046c5338dSCorentin Labbe /* the burst value is not documented in the datasheet */ 7146c5338dSCorentin Labbe #define TXDMA_CTRL_BURST_UNK BIT(22) 7246c5338dSCorentin Labbe #define TXDMA_CTRL_INT_FAIL BIT(17) 7346c5338dSCorentin Labbe #define TXDMA_CTRL_INT_PERR BIT(16) 7446c5338dSCorentin Labbe 7546c5338dSCorentin Labbe #define RXDMA_CTRL_START BIT(31) 7646c5338dSCorentin Labbe #define RXDMA_CTRL_CONTINUE BIT(30) 7746c5338dSCorentin Labbe #define RXDMA_CTRL_CHAIN_MODE BIT(29) 7846c5338dSCorentin Labbe /* the burst value is not documented in the datasheet */ 7946c5338dSCorentin Labbe #define RXDMA_CTRL_BURST_UNK BIT(22) 8046c5338dSCorentin Labbe #define RXDMA_CTRL_INT_FINISH BIT(18) 8146c5338dSCorentin Labbe #define RXDMA_CTRL_INT_FAIL BIT(17) 8246c5338dSCorentin Labbe #define RXDMA_CTRL_INT_PERR BIT(16) 8346c5338dSCorentin Labbe #define RXDMA_CTRL_INT_EOD BIT(15) 8446c5338dSCorentin Labbe #define RXDMA_CTRL_INT_EOF BIT(14) 8546c5338dSCorentin Labbe 8646c5338dSCorentin Labbe #define CE_CPU 0 8746c5338dSCorentin Labbe #define CE_DMA 1 8846c5338dSCorentin Labbe 8946c5338dSCorentin Labbe /* 9046c5338dSCorentin Labbe * struct sl3516_ce_descriptor - descriptor for CE operations 9146c5338dSCorentin Labbe * @frame_ctrl: Information for the current descriptor 9246c5338dSCorentin Labbe * @flag_status: For send packet, describe flag of operations. 9346c5338dSCorentin Labbe * @buf_adr: pointer to a send/recv buffer for data packet 9446c5338dSCorentin Labbe * @next_desc: control linking to other descriptors 9546c5338dSCorentin Labbe */ 9646c5338dSCorentin Labbe struct descriptor { 9746c5338dSCorentin Labbe union { 9846c5338dSCorentin Labbe u32 raw; 9946c5338dSCorentin Labbe /* 10046c5338dSCorentin Labbe * struct desc_frame_ctrl - Information for the current descriptor 10146c5338dSCorentin Labbe * @buffer_size: the size of buffer at buf_adr 10246c5338dSCorentin Labbe * @desc_count: Upon completion of a DMA operation, DMA 10346c5338dSCorentin Labbe * write the number of descriptors used 10446c5338dSCorentin Labbe * for the current frame 10546c5338dSCorentin Labbe * @checksum: unknown 10646c5338dSCorentin Labbe * @authcomp: unknown 10746c5338dSCorentin Labbe * @perr: Protocol error during processing this descriptor 10846c5338dSCorentin Labbe * @derr: Data error during processing this descriptor 10946c5338dSCorentin Labbe * @own: 0 if owned by CPU, 1 for DMA 11046c5338dSCorentin Labbe */ 11146c5338dSCorentin Labbe struct desc_frame_ctrl { 11246c5338dSCorentin Labbe u32 buffer_size :16; 11346c5338dSCorentin Labbe u32 desc_count :6; 11446c5338dSCorentin Labbe u32 checksum :6; 11546c5338dSCorentin Labbe u32 authcomp :1; 11646c5338dSCorentin Labbe u32 perr :1; 11746c5338dSCorentin Labbe u32 derr :1; 11846c5338dSCorentin Labbe u32 own :1; 11946c5338dSCorentin Labbe } bits; 12046c5338dSCorentin Labbe } frame_ctrl; 12146c5338dSCorentin Labbe 12246c5338dSCorentin Labbe union { 12346c5338dSCorentin Labbe u32 raw; 12446c5338dSCorentin Labbe /* 12546c5338dSCorentin Labbe * struct desc_flag_status - flag for this descriptor 12646c5338dSCorentin Labbe * @tqflag: list of flag describing the type of operation 12746c5338dSCorentin Labbe * to be performed. 12846c5338dSCorentin Labbe */ 12946c5338dSCorentin Labbe struct desc_tx_flag_status { 13046c5338dSCorentin Labbe u32 tqflag :10; 13146c5338dSCorentin Labbe u32 unused :22; 13246c5338dSCorentin Labbe } tx_flag; 13346c5338dSCorentin Labbe } flag_status; 13446c5338dSCorentin Labbe 13546c5338dSCorentin Labbe u32 buf_adr; 13646c5338dSCorentin Labbe 13746c5338dSCorentin Labbe union { 13846c5338dSCorentin Labbe u32 next_descriptor; 13946c5338dSCorentin Labbe /* 14046c5338dSCorentin Labbe * struct desc_next - describe chaining of descriptors 14146c5338dSCorentin Labbe * @sof_eof: does the descriptor is first (0x11), 14246c5338dSCorentin Labbe * the last (0x01), middle of a chan (0x00) 14346c5338dSCorentin Labbe * or the only one (0x11) 14446c5338dSCorentin Labbe * @dec: AHB bus address increase (0), decrease (1) 14546c5338dSCorentin Labbe * @eofie: End of frame interrupt enable 14646c5338dSCorentin Labbe * @ndar: Next descriptor address 14746c5338dSCorentin Labbe */ 14846c5338dSCorentin Labbe struct desc_next { 14946c5338dSCorentin Labbe u32 sof_eof :2; 15046c5338dSCorentin Labbe u32 dec :1; 15146c5338dSCorentin Labbe u32 eofie :1; 15246c5338dSCorentin Labbe u32 ndar :28; 15346c5338dSCorentin Labbe } bits; 15446c5338dSCorentin Labbe } next_desc; 15546c5338dSCorentin Labbe }; 15646c5338dSCorentin Labbe 15746c5338dSCorentin Labbe /* 15846c5338dSCorentin Labbe * struct control - The value of this register is used to set the 15946c5338dSCorentin Labbe * operation mode of the IPSec Module. 16046c5338dSCorentin Labbe * @process_id: Used to identify the process. The number will be copied 16146c5338dSCorentin Labbe * to the descriptor status of the received packet. 16246c5338dSCorentin Labbe * @auth_check_len: Number of 32-bit words to be checked or appended by the 16346c5338dSCorentin Labbe * authentication module 16446c5338dSCorentin Labbe * @auth_algorithm: 16546c5338dSCorentin Labbe * @auth_mode: 0:append 1:Check Authentication Result 16646c5338dSCorentin Labbe * @fcs_stream_copy: 0:enable 1:disable authentication stream copy 16746c5338dSCorentin Labbe * @mix_key_sel: 0:use rCipherKey0-3 1:use Key Mixer 16846c5338dSCorentin Labbe * @aesnk: AES Key Size 16946c5338dSCorentin Labbe * @cipher_algorithm: choice of CBC/ECE and AES/DES/3DES 17046c5338dSCorentin Labbe * @op_mode: Operation Mode for the IPSec Module 17146c5338dSCorentin Labbe */ 17246c5338dSCorentin Labbe struct pkt_control_header { 17346c5338dSCorentin Labbe u32 process_id :8; 17446c5338dSCorentin Labbe u32 auth_check_len :3; 17546c5338dSCorentin Labbe u32 un1 :1; 17646c5338dSCorentin Labbe u32 auth_algorithm :3; 17746c5338dSCorentin Labbe u32 auth_mode :1; 17846c5338dSCorentin Labbe u32 fcs_stream_copy :1; 17946c5338dSCorentin Labbe u32 un2 :2; 18046c5338dSCorentin Labbe u32 mix_key_sel :1; 18146c5338dSCorentin Labbe u32 aesnk :4; 18246c5338dSCorentin Labbe u32 cipher_algorithm :3; 18346c5338dSCorentin Labbe u32 un3 :1; 18446c5338dSCorentin Labbe u32 op_mode :4; 18546c5338dSCorentin Labbe }; 18646c5338dSCorentin Labbe 18746c5338dSCorentin Labbe struct pkt_control_cipher { 18846c5338dSCorentin Labbe u32 algorithm_len :16; 18946c5338dSCorentin Labbe u32 header_len :16; 19046c5338dSCorentin Labbe }; 19146c5338dSCorentin Labbe 19246c5338dSCorentin Labbe /* 19346c5338dSCorentin Labbe * struct pkt_control_ecb - control packet for ECB 19446c5338dSCorentin Labbe */ 19546c5338dSCorentin Labbe struct pkt_control_ecb { 19646c5338dSCorentin Labbe struct pkt_control_header control; 19746c5338dSCorentin Labbe struct pkt_control_cipher cipher; 19846c5338dSCorentin Labbe unsigned char key[AES_MAX_KEY_SIZE]; 19946c5338dSCorentin Labbe }; 20046c5338dSCorentin Labbe 20146c5338dSCorentin Labbe /* 20246c5338dSCorentin Labbe * struct sl3516_ce_dev - main container for all this driver information 20346c5338dSCorentin Labbe * @base: base address 20446c5338dSCorentin Labbe * @clks: clocks used 20546c5338dSCorentin Labbe * @reset: pointer to reset controller 20646c5338dSCorentin Labbe * @dev: the platform device 20746c5338dSCorentin Labbe * @engine: ptr to the crypto/crypto_engine 20846c5338dSCorentin Labbe * @complete: completion for the current task on this flow 20946c5338dSCorentin Labbe * @status: set to 1 by interrupt if task is done 21046c5338dSCorentin Labbe * @dtx: base DMA address for TX descriptors 21146c5338dSCorentin Labbe * @tx base address of TX descriptors 21246c5338dSCorentin Labbe * @drx: base DMA address for RX descriptors 21346c5338dSCorentin Labbe * @rx base address of RX descriptors 21446c5338dSCorentin Labbe * @ctx current used TX descriptor 21546c5338dSCorentin Labbe * @crx current used RX descriptor 21646c5338dSCorentin Labbe * @trng hw_random structure for RNG 21746c5338dSCorentin Labbe * @hwrng_stat_req number of HWRNG requests 21846c5338dSCorentin Labbe * @hwrng_stat_bytes total number of bytes generated by RNG 21946c5338dSCorentin Labbe * @stat_irq number of IRQ handled by CE 22046c5338dSCorentin Labbe * @stat_irq_tx number of TX IRQ handled by CE 22146c5338dSCorentin Labbe * @stat_irq_rx number of RX IRQ handled by CE 22246c5338dSCorentin Labbe * @stat_req number of requests handled by CE 22346c5338dSCorentin Labbe * @fallbak_sg_count_tx number of fallback due to destination SG count 22446c5338dSCorentin Labbe * @fallbak_sg_count_rx number of fallback due to source SG count 22546c5338dSCorentin Labbe * @fallbak_not_same_len number of fallback due to difference in SG length 22646c5338dSCorentin Labbe * @dbgfs_dir: Debugfs dentry for statistic directory 22746c5338dSCorentin Labbe * @dbgfs_stats: Debugfs dentry for statistic counters 22846c5338dSCorentin Labbe */ 22946c5338dSCorentin Labbe struct sl3516_ce_dev { 23046c5338dSCorentin Labbe void __iomem *base; 23146c5338dSCorentin Labbe struct clk *clks; 23246c5338dSCorentin Labbe struct reset_control *reset; 23346c5338dSCorentin Labbe struct device *dev; 23446c5338dSCorentin Labbe struct crypto_engine *engine; 23546c5338dSCorentin Labbe struct completion complete; 23646c5338dSCorentin Labbe int status; 23746c5338dSCorentin Labbe dma_addr_t dtx; 23846c5338dSCorentin Labbe struct descriptor *tx; 23946c5338dSCorentin Labbe dma_addr_t drx; 24046c5338dSCorentin Labbe struct descriptor *rx; 24146c5338dSCorentin Labbe int ctx; 24246c5338dSCorentin Labbe int crx; 24346c5338dSCorentin Labbe struct hwrng trng; 24446c5338dSCorentin Labbe unsigned long hwrng_stat_req; 24546c5338dSCorentin Labbe unsigned long hwrng_stat_bytes; 24646c5338dSCorentin Labbe unsigned long stat_irq; 24746c5338dSCorentin Labbe unsigned long stat_irq_tx; 24846c5338dSCorentin Labbe unsigned long stat_irq_rx; 24946c5338dSCorentin Labbe unsigned long stat_req; 25046c5338dSCorentin Labbe unsigned long fallback_sg_count_tx; 25146c5338dSCorentin Labbe unsigned long fallback_sg_count_rx; 25246c5338dSCorentin Labbe unsigned long fallback_not_same_len; 25346c5338dSCorentin Labbe unsigned long fallback_mod16; 25446c5338dSCorentin Labbe unsigned long fallback_align16; 25546c5338dSCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SL3516_DEBUG 25646c5338dSCorentin Labbe struct dentry *dbgfs_dir; 25746c5338dSCorentin Labbe struct dentry *dbgfs_stats; 25846c5338dSCorentin Labbe #endif 25946c5338dSCorentin Labbe void *pctrl; 26046c5338dSCorentin Labbe dma_addr_t dctrl; 26146c5338dSCorentin Labbe }; 26246c5338dSCorentin Labbe 26346c5338dSCorentin Labbe struct sginfo { 26446c5338dSCorentin Labbe u32 addr; 26546c5338dSCorentin Labbe u32 len; 26646c5338dSCorentin Labbe }; 26746c5338dSCorentin Labbe 26846c5338dSCorentin Labbe /* 26946c5338dSCorentin Labbe * struct sl3516_ce_cipher_req_ctx - context for a skcipher request 27046c5338dSCorentin Labbe * @t_src: list of mapped SGs with their size 27146c5338dSCorentin Labbe * @t_dst: list of mapped SGs with their size 27246c5338dSCorentin Labbe * @op_dir: direction (encrypt vs decrypt) for this request 27346c5338dSCorentin Labbe * @pctrllen: the length of the ctrl packet 27446c5338dSCorentin Labbe * @tqflag: the TQflag to set in data packet 27546c5338dSCorentin Labbe * @h pointer to the pkt_control_cipher header 27646c5338dSCorentin Labbe * @nr_sgs: number of source SG 27746c5338dSCorentin Labbe * @nr_sgd: number of destination SG 27846c5338dSCorentin Labbe * @fallback_req: request struct for invoking the fallback skcipher TFM 27946c5338dSCorentin Labbe */ 28046c5338dSCorentin Labbe struct sl3516_ce_cipher_req_ctx { 28146c5338dSCorentin Labbe struct sginfo t_src[MAXDESC]; 28246c5338dSCorentin Labbe struct sginfo t_dst[MAXDESC]; 28346c5338dSCorentin Labbe u32 op_dir; 28446c5338dSCorentin Labbe unsigned int pctrllen; 28546c5338dSCorentin Labbe u32 tqflag; 28646c5338dSCorentin Labbe struct pkt_control_cipher *h; 28746c5338dSCorentin Labbe int nr_sgs; 28846c5338dSCorentin Labbe int nr_sgd; 28946c5338dSCorentin Labbe struct skcipher_request fallback_req; // keep at the end 29046c5338dSCorentin Labbe }; 29146c5338dSCorentin Labbe 29246c5338dSCorentin Labbe /* 29346c5338dSCorentin Labbe * struct sl3516_ce_cipher_tfm_ctx - context for a skcipher TFM 29446c5338dSCorentin Labbe * @key: pointer to key data 29546c5338dSCorentin Labbe * @keylen: len of the key 29646c5338dSCorentin Labbe * @ce: pointer to the private data of driver handling this TFM 29746c5338dSCorentin Labbe * @fallback_tfm: pointer to the fallback TFM 29846c5338dSCorentin Labbe */ 29946c5338dSCorentin Labbe struct sl3516_ce_cipher_tfm_ctx { 30046c5338dSCorentin Labbe u32 *key; 30146c5338dSCorentin Labbe u32 keylen; 30246c5338dSCorentin Labbe struct sl3516_ce_dev *ce; 30346c5338dSCorentin Labbe struct crypto_skcipher *fallback_tfm; 30446c5338dSCorentin Labbe }; 30546c5338dSCorentin Labbe 30646c5338dSCorentin Labbe /* 30746c5338dSCorentin Labbe * struct sl3516_ce_alg_template - crypto_alg template 30846c5338dSCorentin Labbe * @type: the CRYPTO_ALG_TYPE for this template 30946c5338dSCorentin Labbe * @mode: value to be used in control packet for this algorithm 31046c5338dSCorentin Labbe * @ce: pointer to the sl3516_ce_dev structure associated with 31146c5338dSCorentin Labbe * this template 31246c5338dSCorentin Labbe * @alg: one of sub struct must be used 31346c5338dSCorentin Labbe * @stat_req: number of request done on this template 31446c5338dSCorentin Labbe * @stat_fb: number of request which has fallbacked 31546c5338dSCorentin Labbe * @stat_bytes: total data size done by this template 31646c5338dSCorentin Labbe */ 31746c5338dSCorentin Labbe struct sl3516_ce_alg_template { 31846c5338dSCorentin Labbe u32 type; 31946c5338dSCorentin Labbe u32 mode; 32046c5338dSCorentin Labbe struct sl3516_ce_dev *ce; 32146c5338dSCorentin Labbe union { 322*67b7702cSHerbert Xu struct skcipher_engine_alg skcipher; 32346c5338dSCorentin Labbe } alg; 32446c5338dSCorentin Labbe unsigned long stat_req; 32546c5338dSCorentin Labbe unsigned long stat_fb; 32646c5338dSCorentin Labbe unsigned long stat_bytes; 32746c5338dSCorentin Labbe }; 32846c5338dSCorentin Labbe 32946c5338dSCorentin Labbe int sl3516_ce_enqueue(struct crypto_async_request *areq, u32 type); 33046c5338dSCorentin Labbe 33146c5338dSCorentin Labbe int sl3516_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, 33246c5338dSCorentin Labbe unsigned int keylen); 33346c5338dSCorentin Labbe int sl3516_ce_cipher_init(struct crypto_tfm *tfm); 33446c5338dSCorentin Labbe void sl3516_ce_cipher_exit(struct crypto_tfm *tfm); 33546c5338dSCorentin Labbe int sl3516_ce_skdecrypt(struct skcipher_request *areq); 33646c5338dSCorentin Labbe int sl3516_ce_skencrypt(struct skcipher_request *areq); 33746c5338dSCorentin Labbe 33846c5338dSCorentin Labbe int sl3516_ce_run_task(struct sl3516_ce_dev *ce, 33946c5338dSCorentin Labbe struct sl3516_ce_cipher_req_ctx *rctx, const char *name); 34046c5338dSCorentin Labbe 34146c5338dSCorentin Labbe int sl3516_ce_rng_register(struct sl3516_ce_dev *ce); 34246c5338dSCorentin Labbe void sl3516_ce_rng_unregister(struct sl3516_ce_dev *ce); 343*67b7702cSHerbert Xu int sl3516_ce_handle_cipher_request(struct crypto_engine *engine, void *areq); 344