162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * sl3516-ce.h - hardware cryptographic offloader for cortina/gemini SoC 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2021 Corentin LABBE <clabbe@baylibre.com> 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * General notes on this driver: 862306a36Sopenharmony_ci * Called either Crypto Acceleration Engine Module, Security Acceleration Engine 962306a36Sopenharmony_ci * or IPSEC module in the datasheet, it will be called Crypto Engine for short 1062306a36Sopenharmony_ci * in this driver. 1162306a36Sopenharmony_ci * The CE was designed to handle IPSEC and wifi(TKIP WEP) protocol. 1262306a36Sopenharmony_ci * It can handle AES, DES, 3DES, MD5, WEP, TKIP, SHA1, HMAC(MD5), HMAC(SHA1), 1362306a36Sopenharmony_ci * Michael cipher/digest suites. 1462306a36Sopenharmony_ci * It acts the same as a network hw, with both RX and TX chained descriptors. 1562306a36Sopenharmony_ci */ 1662306a36Sopenharmony_ci#include <crypto/aes.h> 1762306a36Sopenharmony_ci#include <crypto/engine.h> 1862306a36Sopenharmony_ci#include <crypto/scatterwalk.h> 1962306a36Sopenharmony_ci#include <crypto/skcipher.h> 2062306a36Sopenharmony_ci#include <linux/debugfs.h> 2162306a36Sopenharmony_ci#include <linux/hw_random.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#define TQ0_TYPE_DATA 0 2462306a36Sopenharmony_ci#define TQ0_TYPE_CTRL BIT(0) 2562306a36Sopenharmony_ci#define TQ1_CIPHER BIT(1) 2662306a36Sopenharmony_ci#define TQ2_AUTH BIT(2) 2762306a36Sopenharmony_ci#define TQ3_IV BIT(3) 2862306a36Sopenharmony_ci#define TQ4_KEY0 BIT(4) 2962306a36Sopenharmony_ci#define TQ5_KEY4 BIT(5) 3062306a36Sopenharmony_ci#define TQ6_KEY6 BIT(6) 3162306a36Sopenharmony_ci#define TQ7_AKEY0 BIT(7) 3262306a36Sopenharmony_ci#define TQ8_AKEY2 BIT(8) 3362306a36Sopenharmony_ci#define TQ9_AKEY2 BIT(9) 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci#define ECB_AES 0x2 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci#define DESC_LAST 0x01 3862306a36Sopenharmony_ci#define DESC_FIRST 0x02 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci#define IPSEC_ID 0x0000 4162306a36Sopenharmony_ci#define IPSEC_STATUS_REG 0x00a8 4262306a36Sopenharmony_ci#define IPSEC_RAND_NUM_REG 0x00ac 4362306a36Sopenharmony_ci#define IPSEC_DMA_DEVICE_ID 0xff00 4462306a36Sopenharmony_ci#define IPSEC_DMA_STATUS 0xff04 4562306a36Sopenharmony_ci#define IPSEC_TXDMA_CTRL 0xff08 4662306a36Sopenharmony_ci#define IPSEC_TXDMA_FIRST_DESC 0xff0c 4762306a36Sopenharmony_ci#define IPSEC_TXDMA_CURR_DESC 0xff10 4862306a36Sopenharmony_ci#define IPSEC_RXDMA_CTRL 0xff14 4962306a36Sopenharmony_ci#define IPSEC_RXDMA_FIRST_DESC 0xff18 5062306a36Sopenharmony_ci#define IPSEC_RXDMA_CURR_DESC 0xff1c 5162306a36Sopenharmony_ci#define IPSEC_TXDMA_BUF_ADDR 0xff28 5262306a36Sopenharmony_ci#define IPSEC_RXDMA_BUF_ADDR 0xff38 5362306a36Sopenharmony_ci#define IPSEC_RXDMA_BUF_SIZE 0xff30 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci#define CE_ENCRYPTION 0x01 5662306a36Sopenharmony_ci#define CE_DECRYPTION 0x03 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci#define MAXDESC 6 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci#define DMA_STATUS_RS_EOFI BIT(22) 6162306a36Sopenharmony_ci#define DMA_STATUS_RS_PERR BIT(24) 6262306a36Sopenharmony_ci#define DMA_STATUS_RS_DERR BIT(25) 6362306a36Sopenharmony_ci#define DMA_STATUS_TS_EOFI BIT(27) 6462306a36Sopenharmony_ci#define DMA_STATUS_TS_PERR BIT(29) 6562306a36Sopenharmony_ci#define DMA_STATUS_TS_DERR BIT(30) 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci#define TXDMA_CTRL_START BIT(31) 6862306a36Sopenharmony_ci#define TXDMA_CTRL_CONTINUE BIT(30) 6962306a36Sopenharmony_ci#define TXDMA_CTRL_CHAIN_MODE BIT(29) 7062306a36Sopenharmony_ci/* the burst value is not documented in the datasheet */ 7162306a36Sopenharmony_ci#define TXDMA_CTRL_BURST_UNK BIT(22) 7262306a36Sopenharmony_ci#define TXDMA_CTRL_INT_FAIL BIT(17) 7362306a36Sopenharmony_ci#define TXDMA_CTRL_INT_PERR BIT(16) 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci#define RXDMA_CTRL_START BIT(31) 7662306a36Sopenharmony_ci#define RXDMA_CTRL_CONTINUE BIT(30) 7762306a36Sopenharmony_ci#define RXDMA_CTRL_CHAIN_MODE BIT(29) 7862306a36Sopenharmony_ci/* the burst value is not documented in the datasheet */ 7962306a36Sopenharmony_ci#define RXDMA_CTRL_BURST_UNK BIT(22) 8062306a36Sopenharmony_ci#define RXDMA_CTRL_INT_FINISH BIT(18) 8162306a36Sopenharmony_ci#define RXDMA_CTRL_INT_FAIL BIT(17) 8262306a36Sopenharmony_ci#define RXDMA_CTRL_INT_PERR BIT(16) 8362306a36Sopenharmony_ci#define RXDMA_CTRL_INT_EOD BIT(15) 8462306a36Sopenharmony_ci#define RXDMA_CTRL_INT_EOF BIT(14) 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci#define CE_CPU 0 8762306a36Sopenharmony_ci#define CE_DMA 1 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci/* 9062306a36Sopenharmony_ci * struct sl3516_ce_descriptor - descriptor for CE operations 9162306a36Sopenharmony_ci * @frame_ctrl: Information for the current descriptor 9262306a36Sopenharmony_ci * @flag_status: For send packet, describe flag of operations. 9362306a36Sopenharmony_ci * @buf_adr: pointer to a send/recv buffer for data packet 9462306a36Sopenharmony_ci * @next_desc: control linking to other descriptors 9562306a36Sopenharmony_ci */ 9662306a36Sopenharmony_cistruct descriptor { 9762306a36Sopenharmony_ci union { 9862306a36Sopenharmony_ci u32 raw; 9962306a36Sopenharmony_ci /* 10062306a36Sopenharmony_ci * struct desc_frame_ctrl - Information for the current descriptor 10162306a36Sopenharmony_ci * @buffer_size: the size of buffer at buf_adr 10262306a36Sopenharmony_ci * @desc_count: Upon completion of a DMA operation, DMA 10362306a36Sopenharmony_ci * write the number of descriptors used 10462306a36Sopenharmony_ci * for the current frame 10562306a36Sopenharmony_ci * @checksum: unknown 10662306a36Sopenharmony_ci * @authcomp: unknown 10762306a36Sopenharmony_ci * @perr: Protocol error during processing this descriptor 10862306a36Sopenharmony_ci * @derr: Data error during processing this descriptor 10962306a36Sopenharmony_ci * @own: 0 if owned by CPU, 1 for DMA 11062306a36Sopenharmony_ci */ 11162306a36Sopenharmony_ci struct desc_frame_ctrl { 11262306a36Sopenharmony_ci u32 buffer_size :16; 11362306a36Sopenharmony_ci u32 desc_count :6; 11462306a36Sopenharmony_ci u32 checksum :6; 11562306a36Sopenharmony_ci u32 authcomp :1; 11662306a36Sopenharmony_ci u32 perr :1; 11762306a36Sopenharmony_ci u32 derr :1; 11862306a36Sopenharmony_ci u32 own :1; 11962306a36Sopenharmony_ci } bits; 12062306a36Sopenharmony_ci } frame_ctrl; 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci union { 12362306a36Sopenharmony_ci u32 raw; 12462306a36Sopenharmony_ci /* 12562306a36Sopenharmony_ci * struct desc_flag_status - flag for this descriptor 12662306a36Sopenharmony_ci * @tqflag: list of flag describing the type of operation 12762306a36Sopenharmony_ci * to be performed. 12862306a36Sopenharmony_ci */ 12962306a36Sopenharmony_ci struct desc_tx_flag_status { 13062306a36Sopenharmony_ci u32 tqflag :10; 13162306a36Sopenharmony_ci u32 unused :22; 13262306a36Sopenharmony_ci } tx_flag; 13362306a36Sopenharmony_ci } flag_status; 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci u32 buf_adr; 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci union { 13862306a36Sopenharmony_ci u32 next_descriptor; 13962306a36Sopenharmony_ci /* 14062306a36Sopenharmony_ci * struct desc_next - describe chaining of descriptors 14162306a36Sopenharmony_ci * @sof_eof: does the descriptor is first (0x11), 14262306a36Sopenharmony_ci * the last (0x01), middle of a chan (0x00) 14362306a36Sopenharmony_ci * or the only one (0x11) 14462306a36Sopenharmony_ci * @dec: AHB bus address increase (0), decrease (1) 14562306a36Sopenharmony_ci * @eofie: End of frame interrupt enable 14662306a36Sopenharmony_ci * @ndar: Next descriptor address 14762306a36Sopenharmony_ci */ 14862306a36Sopenharmony_ci struct desc_next { 14962306a36Sopenharmony_ci u32 sof_eof :2; 15062306a36Sopenharmony_ci u32 dec :1; 15162306a36Sopenharmony_ci u32 eofie :1; 15262306a36Sopenharmony_ci u32 ndar :28; 15362306a36Sopenharmony_ci } bits; 15462306a36Sopenharmony_ci } next_desc; 15562306a36Sopenharmony_ci}; 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci/* 15862306a36Sopenharmony_ci * struct control - The value of this register is used to set the 15962306a36Sopenharmony_ci * operation mode of the IPSec Module. 16062306a36Sopenharmony_ci * @process_id: Used to identify the process. The number will be copied 16162306a36Sopenharmony_ci * to the descriptor status of the received packet. 16262306a36Sopenharmony_ci * @auth_check_len: Number of 32-bit words to be checked or appended by the 16362306a36Sopenharmony_ci * authentication module 16462306a36Sopenharmony_ci * @auth_algorithm: 16562306a36Sopenharmony_ci * @auth_mode: 0:append 1:Check Authentication Result 16662306a36Sopenharmony_ci * @fcs_stream_copy: 0:enable 1:disable authentication stream copy 16762306a36Sopenharmony_ci * @mix_key_sel: 0:use rCipherKey0-3 1:use Key Mixer 16862306a36Sopenharmony_ci * @aesnk: AES Key Size 16962306a36Sopenharmony_ci * @cipher_algorithm: choice of CBC/ECE and AES/DES/3DES 17062306a36Sopenharmony_ci * @op_mode: Operation Mode for the IPSec Module 17162306a36Sopenharmony_ci */ 17262306a36Sopenharmony_cistruct pkt_control_header { 17362306a36Sopenharmony_ci u32 process_id :8; 17462306a36Sopenharmony_ci u32 auth_check_len :3; 17562306a36Sopenharmony_ci u32 un1 :1; 17662306a36Sopenharmony_ci u32 auth_algorithm :3; 17762306a36Sopenharmony_ci u32 auth_mode :1; 17862306a36Sopenharmony_ci u32 fcs_stream_copy :1; 17962306a36Sopenharmony_ci u32 un2 :2; 18062306a36Sopenharmony_ci u32 mix_key_sel :1; 18162306a36Sopenharmony_ci u32 aesnk :4; 18262306a36Sopenharmony_ci u32 cipher_algorithm :3; 18362306a36Sopenharmony_ci u32 un3 :1; 18462306a36Sopenharmony_ci u32 op_mode :4; 18562306a36Sopenharmony_ci}; 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_cistruct pkt_control_cipher { 18862306a36Sopenharmony_ci u32 algorithm_len :16; 18962306a36Sopenharmony_ci u32 header_len :16; 19062306a36Sopenharmony_ci}; 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci/* 19362306a36Sopenharmony_ci * struct pkt_control_ecb - control packet for ECB 19462306a36Sopenharmony_ci */ 19562306a36Sopenharmony_cistruct pkt_control_ecb { 19662306a36Sopenharmony_ci struct pkt_control_header control; 19762306a36Sopenharmony_ci struct pkt_control_cipher cipher; 19862306a36Sopenharmony_ci unsigned char key[AES_MAX_KEY_SIZE]; 19962306a36Sopenharmony_ci}; 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci/* 20262306a36Sopenharmony_ci * struct sl3516_ce_dev - main container for all this driver information 20362306a36Sopenharmony_ci * @base: base address 20462306a36Sopenharmony_ci * @clks: clocks used 20562306a36Sopenharmony_ci * @reset: pointer to reset controller 20662306a36Sopenharmony_ci * @dev: the platform device 20762306a36Sopenharmony_ci * @engine: ptr to the crypto/crypto_engine 20862306a36Sopenharmony_ci * @complete: completion for the current task on this flow 20962306a36Sopenharmony_ci * @status: set to 1 by interrupt if task is done 21062306a36Sopenharmony_ci * @dtx: base DMA address for TX descriptors 21162306a36Sopenharmony_ci * @tx base address of TX descriptors 21262306a36Sopenharmony_ci * @drx: base DMA address for RX descriptors 21362306a36Sopenharmony_ci * @rx base address of RX descriptors 21462306a36Sopenharmony_ci * @ctx current used TX descriptor 21562306a36Sopenharmony_ci * @crx current used RX descriptor 21662306a36Sopenharmony_ci * @trng hw_random structure for RNG 21762306a36Sopenharmony_ci * @hwrng_stat_req number of HWRNG requests 21862306a36Sopenharmony_ci * @hwrng_stat_bytes total number of bytes generated by RNG 21962306a36Sopenharmony_ci * @stat_irq number of IRQ handled by CE 22062306a36Sopenharmony_ci * @stat_irq_tx number of TX IRQ handled by CE 22162306a36Sopenharmony_ci * @stat_irq_rx number of RX IRQ handled by CE 22262306a36Sopenharmony_ci * @stat_req number of requests handled by CE 22362306a36Sopenharmony_ci * @fallbak_sg_count_tx number of fallback due to destination SG count 22462306a36Sopenharmony_ci * @fallbak_sg_count_rx number of fallback due to source SG count 22562306a36Sopenharmony_ci * @fallbak_not_same_len number of fallback due to difference in SG length 22662306a36Sopenharmony_ci * @dbgfs_dir: Debugfs dentry for statistic directory 22762306a36Sopenharmony_ci * @dbgfs_stats: Debugfs dentry for statistic counters 22862306a36Sopenharmony_ci */ 22962306a36Sopenharmony_cistruct sl3516_ce_dev { 23062306a36Sopenharmony_ci void __iomem *base; 23162306a36Sopenharmony_ci struct clk *clks; 23262306a36Sopenharmony_ci struct reset_control *reset; 23362306a36Sopenharmony_ci struct device *dev; 23462306a36Sopenharmony_ci struct crypto_engine *engine; 23562306a36Sopenharmony_ci struct completion complete; 23662306a36Sopenharmony_ci int status; 23762306a36Sopenharmony_ci dma_addr_t dtx; 23862306a36Sopenharmony_ci struct descriptor *tx; 23962306a36Sopenharmony_ci dma_addr_t drx; 24062306a36Sopenharmony_ci struct descriptor *rx; 24162306a36Sopenharmony_ci int ctx; 24262306a36Sopenharmony_ci int crx; 24362306a36Sopenharmony_ci struct hwrng trng; 24462306a36Sopenharmony_ci unsigned long hwrng_stat_req; 24562306a36Sopenharmony_ci unsigned long hwrng_stat_bytes; 24662306a36Sopenharmony_ci unsigned long stat_irq; 24762306a36Sopenharmony_ci unsigned long stat_irq_tx; 24862306a36Sopenharmony_ci unsigned long stat_irq_rx; 24962306a36Sopenharmony_ci unsigned long stat_req; 25062306a36Sopenharmony_ci unsigned long fallback_sg_count_tx; 25162306a36Sopenharmony_ci unsigned long fallback_sg_count_rx; 25262306a36Sopenharmony_ci unsigned long fallback_not_same_len; 25362306a36Sopenharmony_ci unsigned long fallback_mod16; 25462306a36Sopenharmony_ci unsigned long fallback_align16; 25562306a36Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_SL3516_DEBUG 25662306a36Sopenharmony_ci struct dentry *dbgfs_dir; 25762306a36Sopenharmony_ci struct dentry *dbgfs_stats; 25862306a36Sopenharmony_ci#endif 25962306a36Sopenharmony_ci void *pctrl; 26062306a36Sopenharmony_ci dma_addr_t dctrl; 26162306a36Sopenharmony_ci}; 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_cistruct sginfo { 26462306a36Sopenharmony_ci u32 addr; 26562306a36Sopenharmony_ci u32 len; 26662306a36Sopenharmony_ci}; 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci/* 26962306a36Sopenharmony_ci * struct sl3516_ce_cipher_req_ctx - context for a skcipher request 27062306a36Sopenharmony_ci * @t_src: list of mapped SGs with their size 27162306a36Sopenharmony_ci * @t_dst: list of mapped SGs with their size 27262306a36Sopenharmony_ci * @op_dir: direction (encrypt vs decrypt) for this request 27362306a36Sopenharmony_ci * @pctrllen: the length of the ctrl packet 27462306a36Sopenharmony_ci * @tqflag: the TQflag to set in data packet 27562306a36Sopenharmony_ci * @h pointer to the pkt_control_cipher header 27662306a36Sopenharmony_ci * @nr_sgs: number of source SG 27762306a36Sopenharmony_ci * @nr_sgd: number of destination SG 27862306a36Sopenharmony_ci * @fallback_req: request struct for invoking the fallback skcipher TFM 27962306a36Sopenharmony_ci */ 28062306a36Sopenharmony_cistruct sl3516_ce_cipher_req_ctx { 28162306a36Sopenharmony_ci struct sginfo t_src[MAXDESC]; 28262306a36Sopenharmony_ci struct sginfo t_dst[MAXDESC]; 28362306a36Sopenharmony_ci u32 op_dir; 28462306a36Sopenharmony_ci unsigned int pctrllen; 28562306a36Sopenharmony_ci u32 tqflag; 28662306a36Sopenharmony_ci struct pkt_control_cipher *h; 28762306a36Sopenharmony_ci int nr_sgs; 28862306a36Sopenharmony_ci int nr_sgd; 28962306a36Sopenharmony_ci struct skcipher_request fallback_req; // keep at the end 29062306a36Sopenharmony_ci}; 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci/* 29362306a36Sopenharmony_ci * struct sl3516_ce_cipher_tfm_ctx - context for a skcipher TFM 29462306a36Sopenharmony_ci * @key: pointer to key data 29562306a36Sopenharmony_ci * @keylen: len of the key 29662306a36Sopenharmony_ci * @ce: pointer to the private data of driver handling this TFM 29762306a36Sopenharmony_ci * @fallback_tfm: pointer to the fallback TFM 29862306a36Sopenharmony_ci */ 29962306a36Sopenharmony_cistruct sl3516_ce_cipher_tfm_ctx { 30062306a36Sopenharmony_ci u32 *key; 30162306a36Sopenharmony_ci u32 keylen; 30262306a36Sopenharmony_ci struct sl3516_ce_dev *ce; 30362306a36Sopenharmony_ci struct crypto_skcipher *fallback_tfm; 30462306a36Sopenharmony_ci}; 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_ci/* 30762306a36Sopenharmony_ci * struct sl3516_ce_alg_template - crypto_alg template 30862306a36Sopenharmony_ci * @type: the CRYPTO_ALG_TYPE for this template 30962306a36Sopenharmony_ci * @mode: value to be used in control packet for this algorithm 31062306a36Sopenharmony_ci * @ce: pointer to the sl3516_ce_dev structure associated with 31162306a36Sopenharmony_ci * this template 31262306a36Sopenharmony_ci * @alg: one of sub struct must be used 31362306a36Sopenharmony_ci * @stat_req: number of request done on this template 31462306a36Sopenharmony_ci * @stat_fb: number of request which has fallbacked 31562306a36Sopenharmony_ci * @stat_bytes: total data size done by this template 31662306a36Sopenharmony_ci */ 31762306a36Sopenharmony_cistruct sl3516_ce_alg_template { 31862306a36Sopenharmony_ci u32 type; 31962306a36Sopenharmony_ci u32 mode; 32062306a36Sopenharmony_ci struct sl3516_ce_dev *ce; 32162306a36Sopenharmony_ci union { 32262306a36Sopenharmony_ci struct skcipher_engine_alg skcipher; 32362306a36Sopenharmony_ci } alg; 32462306a36Sopenharmony_ci unsigned long stat_req; 32562306a36Sopenharmony_ci unsigned long stat_fb; 32662306a36Sopenharmony_ci unsigned long stat_bytes; 32762306a36Sopenharmony_ci}; 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ciint sl3516_ce_enqueue(struct crypto_async_request *areq, u32 type); 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ciint sl3516_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, 33262306a36Sopenharmony_ci unsigned int keylen); 33362306a36Sopenharmony_ciint sl3516_ce_cipher_init(struct crypto_tfm *tfm); 33462306a36Sopenharmony_civoid sl3516_ce_cipher_exit(struct crypto_tfm *tfm); 33562306a36Sopenharmony_ciint sl3516_ce_skdecrypt(struct skcipher_request *areq); 33662306a36Sopenharmony_ciint sl3516_ce_skencrypt(struct skcipher_request *areq); 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ciint sl3516_ce_run_task(struct sl3516_ce_dev *ce, 33962306a36Sopenharmony_ci struct sl3516_ce_cipher_req_ctx *rctx, const char *name); 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ciint sl3516_ce_rng_register(struct sl3516_ce_dev *ce); 34262306a36Sopenharmony_civoid sl3516_ce_rng_unregister(struct sl3516_ce_dev *ce); 34362306a36Sopenharmony_ciint sl3516_ce_handle_cipher_request(struct crypto_engine *engine, void *areq); 344