/* SPDX-License-Identifier: GPL-2.0 */1/*2* sl3516-ce.h - hardware cryptographic offloader for cortina/gemini SoC3*4* Copyright (C) 2021 Corentin LABBE <[email protected]>5*6* General notes on this driver:7* Called either Crypto Acceleration Engine Module, Security Acceleration Engine8* or IPSEC module in the datasheet, it will be called Crypto Engine for short9* in this driver.10* The CE was designed to handle IPSEC and wifi(TKIP WEP) protocol.11* It can handle AES, DES, 3DES, MD5, WEP, TKIP, SHA1, HMAC(MD5), HMAC(SHA1),12* Michael cipher/digest suites.13* It acts the same as a network hw, with both RX and TX chained descriptors.14*/15#include <crypto/aes.h>16#include <crypto/engine.h>17#include <crypto/scatterwalk.h>18#include <crypto/skcipher.h>19#include <linux/debugfs.h>20#include <linux/hw_random.h>2122#define TQ0_TYPE_DATA 023#define TQ0_TYPE_CTRL BIT(0)24#define TQ1_CIPHER BIT(1)25#define TQ2_AUTH BIT(2)26#define TQ3_IV BIT(3)27#define TQ4_KEY0 BIT(4)28#define TQ5_KEY4 BIT(5)29#define TQ6_KEY6 BIT(6)30#define TQ7_AKEY0 BIT(7)31#define TQ8_AKEY2 BIT(8)32#define TQ9_AKEY2 BIT(9)3334#define ECB_AES 0x23536#define DESC_LAST 0x0137#define DESC_FIRST 0x023839#define IPSEC_ID 0x000040#define IPSEC_STATUS_REG 0x00a841#define IPSEC_RAND_NUM_REG 0x00ac42#define IPSEC_DMA_DEVICE_ID 0xff0043#define IPSEC_DMA_STATUS 0xff0444#define IPSEC_TXDMA_CTRL 0xff0845#define IPSEC_TXDMA_FIRST_DESC 0xff0c46#define IPSEC_TXDMA_CURR_DESC 0xff1047#define IPSEC_RXDMA_CTRL 0xff1448#define IPSEC_RXDMA_FIRST_DESC 0xff1849#define IPSEC_RXDMA_CURR_DESC 0xff1c50#define IPSEC_TXDMA_BUF_ADDR 0xff2851#define IPSEC_RXDMA_BUF_ADDR 0xff3852#define IPSEC_RXDMA_BUF_SIZE 0xff305354#define CE_ENCRYPTION 0x0155#define CE_DECRYPTION 0x035657#define MAXDESC 65859#define DMA_STATUS_RS_EOFI BIT(22)60#define DMA_STATUS_RS_PERR BIT(24)61#define DMA_STATUS_RS_DERR BIT(25)62#define DMA_STATUS_TS_EOFI BIT(27)63#define DMA_STATUS_TS_PERR BIT(29)64#define DMA_STATUS_TS_DERR BIT(30)6566#define TXDMA_CTRL_START BIT(31)67#define TXDMA_CTRL_CONTINUE BIT(30)68#define TXDMA_CTRL_CHAIN_MODE BIT(29)69/* the burst value is not documented in the datasheet */70#define TXDMA_CTRL_BURST_UNK BIT(22)71#define TXDMA_CTRL_INT_FAIL BIT(17)72#define TXDMA_CTRL_INT_PERR BIT(16)7374#define RXDMA_CTRL_START BIT(31)75#define RXDMA_CTRL_CONTINUE BIT(30)76#define RXDMA_CTRL_CHAIN_MODE BIT(29)77/* the burst value is not documented in the datasheet */78#define RXDMA_CTRL_BURST_UNK BIT(22)79#define RXDMA_CTRL_INT_FINISH BIT(18)80#define RXDMA_CTRL_INT_FAIL BIT(17)81#define RXDMA_CTRL_INT_PERR BIT(16)82#define RXDMA_CTRL_INT_EOD BIT(15)83#define RXDMA_CTRL_INT_EOF BIT(14)8485#define CE_CPU 086#define CE_DMA 18788/*89* struct sl3516_ce_descriptor - descriptor for CE operations90* @frame_ctrl: Information for the current descriptor91* @flag_status: For send packet, describe flag of operations.92* @buf_adr: pointer to a send/recv buffer for data packet93* @next_desc: control linking to other descriptors94*/95struct descriptor {96union {97u32 raw;98/*99* struct desc_frame_ctrl - Information for the current descriptor100* @buffer_size: the size of buffer at buf_adr101* @desc_count: Upon completion of a DMA operation, DMA102* write the number of descriptors used103* for the current frame104* @checksum: unknown105* @authcomp: unknown106* @perr: Protocol error during processing this descriptor107* @derr: Data error during processing this descriptor108* @own: 0 if owned by CPU, 1 for DMA109*/110struct desc_frame_ctrl {111u32 buffer_size :16;112u32 desc_count :6;113u32 checksum :6;114u32 authcomp :1;115u32 perr :1;116u32 derr :1;117u32 own :1;118} bits;119} frame_ctrl;120121union {122u32 raw;123/*124* struct desc_flag_status - flag for this descriptor125* @tqflag: list of flag describing the type of operation126* to be performed.127*/128struct desc_tx_flag_status {129u32 tqflag :10;130u32 unused :22;131} tx_flag;132} flag_status;133134u32 buf_adr;135136union {137u32 next_descriptor;138/*139* struct desc_next - describe chaining of descriptors140* @sof_eof: does the descriptor is first (0x11),141* the last (0x01), middle of a chan (0x00)142* or the only one (0x11)143* @dec: AHB bus address increase (0), decrease (1)144* @eofie: End of frame interrupt enable145* @ndar: Next descriptor address146*/147struct desc_next {148u32 sof_eof :2;149u32 dec :1;150u32 eofie :1;151u32 ndar :28;152} bits;153} next_desc;154};155156/*157* struct control - The value of this register is used to set the158* operation mode of the IPSec Module.159* @process_id: Used to identify the process. The number will be copied160* to the descriptor status of the received packet.161* @auth_check_len: Number of 32-bit words to be checked or appended by the162* authentication module163* @auth_algorithm:164* @auth_mode: 0:append 1:Check Authentication Result165* @fcs_stream_copy: 0:enable 1:disable authentication stream copy166* @mix_key_sel: 0:use rCipherKey0-3 1:use Key Mixer167* @aesnk: AES Key Size168* @cipher_algorithm: choice of CBC/ECE and AES/DES/3DES169* @op_mode: Operation Mode for the IPSec Module170*/171struct pkt_control_header {172u32 process_id :8;173u32 auth_check_len :3;174u32 un1 :1;175u32 auth_algorithm :3;176u32 auth_mode :1;177u32 fcs_stream_copy :1;178u32 un2 :2;179u32 mix_key_sel :1;180u32 aesnk :4;181u32 cipher_algorithm :3;182u32 un3 :1;183u32 op_mode :4;184};185186struct pkt_control_cipher {187u32 algorithm_len :16;188u32 header_len :16;189};190191/*192* struct pkt_control_ecb - control packet for ECB193*/194struct pkt_control_ecb {195struct pkt_control_header control;196struct pkt_control_cipher cipher;197unsigned char key[AES_MAX_KEY_SIZE];198};199200/*201* struct sl3516_ce_dev - main container for all this driver information202* @base: base address203* @clks: clocks used204* @reset: pointer to reset controller205* @dev: the platform device206* @engine: ptr to the crypto/crypto_engine207* @complete: completion for the current task on this flow208* @status: set to 1 by interrupt if task is done209* @dtx: base DMA address for TX descriptors210* @tx base address of TX descriptors211* @drx: base DMA address for RX descriptors212* @rx base address of RX descriptors213* @ctx current used TX descriptor214* @crx current used RX descriptor215* @trng hw_random structure for RNG216* @hwrng_stat_req number of HWRNG requests217* @hwrng_stat_bytes total number of bytes generated by RNG218* @stat_irq number of IRQ handled by CE219* @stat_irq_tx number of TX IRQ handled by CE220* @stat_irq_rx number of RX IRQ handled by CE221* @stat_req number of requests handled by CE222* @fallbak_sg_count_tx number of fallback due to destination SG count223* @fallbak_sg_count_rx number of fallback due to source SG count224* @fallbak_not_same_len number of fallback due to difference in SG length225* @dbgfs_dir: Debugfs dentry for statistic directory226* @dbgfs_stats: Debugfs dentry for statistic counters227*/228struct sl3516_ce_dev {229void __iomem *base;230struct clk *clks;231struct reset_control *reset;232struct device *dev;233struct crypto_engine *engine;234struct completion complete;235int status;236dma_addr_t dtx;237struct descriptor *tx;238dma_addr_t drx;239struct descriptor *rx;240int ctx;241int crx;242struct hwrng trng;243unsigned long hwrng_stat_req;244unsigned long hwrng_stat_bytes;245unsigned long stat_irq;246unsigned long stat_irq_tx;247unsigned long stat_irq_rx;248unsigned long stat_req;249unsigned long fallback_sg_count_tx;250unsigned long fallback_sg_count_rx;251unsigned long fallback_not_same_len;252unsigned long fallback_mod16;253unsigned long fallback_align16;254#ifdef CONFIG_CRYPTO_DEV_SL3516_DEBUG255struct dentry *dbgfs_dir;256struct dentry *dbgfs_stats;257#endif258void *pctrl;259dma_addr_t dctrl;260};261262struct sginfo {263u32 addr;264u32 len;265};266267/*268* struct sl3516_ce_cipher_req_ctx - context for a skcipher request269* @t_src: list of mapped SGs with their size270* @t_dst: list of mapped SGs with their size271* @op_dir: direction (encrypt vs decrypt) for this request272* @pctrllen: the length of the ctrl packet273* @tqflag: the TQflag to set in data packet274* @h pointer to the pkt_control_cipher header275* @nr_sgs: number of source SG276* @nr_sgd: number of destination SG277* @fallback_req: request struct for invoking the fallback skcipher TFM278*/279struct sl3516_ce_cipher_req_ctx {280struct sginfo t_src[MAXDESC];281struct sginfo t_dst[MAXDESC];282u32 op_dir;283unsigned int pctrllen;284u32 tqflag;285struct pkt_control_cipher *h;286int nr_sgs;287int nr_sgd;288struct skcipher_request fallback_req; // keep at the end289};290291/*292* struct sl3516_ce_cipher_tfm_ctx - context for a skcipher TFM293* @key: pointer to key data294* @keylen: len of the key295* @ce: pointer to the private data of driver handling this TFM296* @fallback_tfm: pointer to the fallback TFM297*/298struct sl3516_ce_cipher_tfm_ctx {299u32 *key;300u32 keylen;301struct sl3516_ce_dev *ce;302struct crypto_skcipher *fallback_tfm;303};304305/*306* struct sl3516_ce_alg_template - crypto_alg template307* @type: the CRYPTO_ALG_TYPE for this template308* @mode: value to be used in control packet for this algorithm309* @ce: pointer to the sl3516_ce_dev structure associated with310* this template311* @alg: one of sub struct must be used312* @stat_req: number of request done on this template313* @stat_fb: number of request which has fallbacked314* @stat_bytes: total data size done by this template315*/316struct sl3516_ce_alg_template {317u32 type;318u32 mode;319struct sl3516_ce_dev *ce;320union {321struct skcipher_engine_alg skcipher;322} alg;323unsigned long stat_req;324unsigned long stat_fb;325unsigned long stat_bytes;326};327328int sl3516_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,329unsigned int keylen);330int sl3516_ce_cipher_init(struct crypto_tfm *tfm);331void sl3516_ce_cipher_exit(struct crypto_tfm *tfm);332int sl3516_ce_skdecrypt(struct skcipher_request *areq);333int sl3516_ce_skencrypt(struct skcipher_request *areq);334335int sl3516_ce_run_task(struct sl3516_ce_dev *ce,336struct sl3516_ce_cipher_req_ctx *rctx, const char *name);337338int sl3516_ce_rng_register(struct sl3516_ce_dev *ce);339void sl3516_ce_rng_unregister(struct sl3516_ce_dev *ce);340int sl3516_ce_handle_cipher_request(struct crypto_engine *engine, void *areq);341342343