brintos

brintos / linux-shallow public Read only

0
0
Text · 12.0 KiB · 12c17a6 Raw
408 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/*3 * K3 SA2UL crypto accelerator driver4 *5 * Copyright (C) 2018-2020 Texas Instruments Incorporated - http://www.ti.com6 *7 * Authors:	Keerthy8 *		Vitaly Andrianov9 *		Tero Kristo10 */11 12#ifndef _K3_SA2UL_13#define _K3_SA2UL_14 15#include <crypto/aes.h>16#include <crypto/sha1.h>17#include <crypto/sha2.h>18 19#define SA_ENGINE_STATUS		0x000820#define SA_ENGINE_ENABLE_CONTROL	0x100021 22struct sa_tfm_ctx;23/*24 * SA_ENGINE_ENABLE_CONTROL register bits25 */26#define SA_EEC_ENCSS_EN			0x0000000127#define SA_EEC_AUTHSS_EN		0x0000000228#define SA_EEC_TRNG_EN			0x0000000829#define SA_EEC_PKA_EN			0x0000001030#define SA_EEC_CTXCACH_EN		0x0000008031#define SA_EEC_CPPI_PORT_IN_EN		0x0000020032#define SA_EEC_CPPI_PORT_OUT_EN		0x0000080033 34/*35 * Encoding used to identify the typo of crypto operation36 * performed on the packet when the packet is returned37 * by SA38 */39#define SA_REQ_SUBTYPE_ENC	0x000140#define SA_REQ_SUBTYPE_DEC	0x000241#define SA_REQ_SUBTYPE_SHIFT	1642#define SA_REQ_SUBTYPE_MASK	0xffff43 44/* Number of 32 bit words in EPIB  */45#define SA_DMA_NUM_EPIB_WORDS   446 47/* Number of 32 bit words in PS data  */48#define SA_DMA_NUM_PS_WORDS     1649#define NKEY_SZ			350#define MCI_SZ			2751 52/*53 * Maximum number of simultaeneous security contexts54 * supported by the driver55 */56#define SA_MAX_NUM_CTX	51257 58/*59 * Assumption: CTX size is multiple of 3260 */61#define SA_CTX_SIZE_TO_DMA_SIZE(ctx_sz) \62		((ctx_sz) ? ((ctx_sz) / 32 - 1) : 0)63 64#define SA_CTX_ENC_KEY_OFFSET   3265#define SA_CTX_ENC_AUX1_OFFSET  6466#define SA_CTX_ENC_AUX2_OFFSET  9667#define SA_CTX_ENC_AUX3_OFFSET  11268#define SA_CTX_ENC_AUX4_OFFSET  12869 70/* Next Engine Select code in CP_ACE */71#define SA_ENG_ID_EM1   2       /* Enc/Dec engine with AES/DEC core */72#define SA_ENG_ID_EM2   3       /* Encryption/Decryption enginefor pass 2 */73#define SA_ENG_ID_AM1   4       /* Auth. engine with SHA1/MD5/SHA2 core */74#define SA_ENG_ID_AM2   5       /*  Authentication engine for pass 2 */75#define SA_ENG_ID_OUTPORT2 20   /*  Egress module 2  */76 77/*78 * Command Label Definitions79 */80#define SA_CMDL_OFFSET_NESC           0      /* Next Engine Select Code */81#define SA_CMDL_OFFSET_LABEL_LEN      1      /* Engine Command Label Length */82/* 16-bit Length of Data to be processed */83#define SA_CMDL_OFFSET_DATA_LEN       284#define SA_CMDL_OFFSET_DATA_OFFSET    4      /* Stat Data Offset */85#define SA_CMDL_OFFSET_OPTION_CTRL1   5      /* Option Control Byte 1 */86#define SA_CMDL_OFFSET_OPTION_CTRL2   6      /* Option Control Byte 2 */87#define SA_CMDL_OFFSET_OPTION_CTRL3   7      /* Option Control Byte 3 */88#define SA_CMDL_OFFSET_OPTION_BYTE    889 90#define SA_CMDL_HEADER_SIZE_BYTES	891 92#define SA_CMDL_OPTION_BYTES_MAX_SIZE     7293#define SA_CMDL_MAX_SIZE_BYTES (SA_CMDL_HEADER_SIZE_BYTES + \94				SA_CMDL_OPTION_BYTES_MAX_SIZE)95 96/* SWINFO word-0 flags */97#define SA_SW_INFO_FLAG_EVICT   0x000198#define SA_SW_INFO_FLAG_TEAR    0x000299#define SA_SW_INFO_FLAG_NOPD    0x0004100 101/*102 * This type represents the various packet types to be processed103 * by the PHP engine in SA.104 * It is used to identify the corresponding PHP processing function.105 */106#define SA_CTX_PE_PKT_TYPE_3GPP_AIR    0    /* 3GPP Air Cipher */107#define SA_CTX_PE_PKT_TYPE_SRTP        1    /* SRTP */108#define SA_CTX_PE_PKT_TYPE_IPSEC_AH    2    /* IPSec Authentication Header */109/* IPSec Encapsulating Security Payload */110#define SA_CTX_PE_PKT_TYPE_IPSEC_ESP   3111/* Indicates that it is in data mode, It may not be used by PHP */112#define SA_CTX_PE_PKT_TYPE_NONE        4113#define SA_CTX_ENC_TYPE1_SZ     64      /* Encryption SC with Key only */114#define SA_CTX_ENC_TYPE2_SZ     96      /* Encryption SC with Key and Aux1 */115 116#define SA_CTX_AUTH_TYPE1_SZ    64      /* Auth SC with Key only */117#define SA_CTX_AUTH_TYPE2_SZ    96      /* Auth SC with Key and Aux1 */118/* Size of security context for PHP engine */119#define SA_CTX_PHP_PE_CTX_SZ    64120 121#define SA_CTX_MAX_SZ (64 + SA_CTX_ENC_TYPE2_SZ + SA_CTX_AUTH_TYPE2_SZ)122 123/*124 * Encoding of F/E control in SCCTL125 *  Bit 0-1: Fetch PHP Bytes126 *  Bit 2-3: Fetch Encryption/Air Ciphering Bytes127 *  Bit 4-5: Fetch Authentication Bytes or Encr pass 2128 *  Bit 6-7: Evict PHP Bytes129 *130 *  where   00 = 0 bytes131 *          01 = 64 bytes132 *          10 = 96 bytes133 *          11 = 128 bytes134 */135#define SA_CTX_DMA_SIZE_0       0136#define SA_CTX_DMA_SIZE_64      1137#define SA_CTX_DMA_SIZE_96      2138#define SA_CTX_DMA_SIZE_128     3139 140/*141 * Byte offset of the owner word in SCCTL142 * in the security context143 */144#define SA_CTX_SCCTL_OWNER_OFFSET 0145 146#define SA_CTX_ENC_KEY_OFFSET   32147#define SA_CTX_ENC_AUX1_OFFSET  64148#define SA_CTX_ENC_AUX2_OFFSET  96149#define SA_CTX_ENC_AUX3_OFFSET  112150#define SA_CTX_ENC_AUX4_OFFSET  128151 152#define SA_SCCTL_FE_AUTH_ENC	0x65153#define SA_SCCTL_FE_ENC		0x8D154 155#define SA_ALIGN_MASK		(sizeof(u32) - 1)156#define SA_ALIGNED		__aligned(32)157 158#define SA_AUTH_SW_CTRL_MD5	1159#define SA_AUTH_SW_CTRL_SHA1	2160#define SA_AUTH_SW_CTRL_SHA224	3161#define SA_AUTH_SW_CTRL_SHA256	4162#define SA_AUTH_SW_CTRL_SHA384	5163#define SA_AUTH_SW_CTRL_SHA512	6164 165/* SA2UL can only handle maximum data size of 64KB */166#define SA_MAX_DATA_SZ		U16_MAX167 168/*169 * SA2UL can provide unpredictable results with packet sizes that fall170 * the following range, so avoid using it.171 */172#define SA_UNSAFE_DATA_SZ_MIN	240173#define SA_UNSAFE_DATA_SZ_MAX	255174 175struct sa_match_data;176 177/**178 * struct sa_crypto_data - Crypto driver instance data179 * @base: Base address of the register space180 * @soc_data: Pointer to SoC specific data181 * @pdev: Platform device pointer182 * @sc_pool: security context pool183 * @dev: Device pointer184 * @scid_lock: secure context ID lock185 * @sc_id_start: starting index for SC ID186 * @sc_id_end: Ending index for SC ID187 * @sc_id: Security Context ID188 * @ctx_bm: Bitmap to keep track of Security context ID's189 * @ctx: SA tfm context pointer190 * @dma_rx1: Pointer to DMA rx channel for sizes < 256 Bytes191 * @dma_rx2: Pointer to DMA rx channel for sizes > 256 Bytes192 * @dma_tx: Pointer to DMA TX channel193 */194struct sa_crypto_data {195	void __iomem *base;196	const struct sa_match_data *match_data;197	struct platform_device	*pdev;198	struct dma_pool		*sc_pool;199	struct device *dev;200	spinlock_t	scid_lock; /* lock for SC-ID allocation */201	/* Security context data */202	u16		sc_id_start;203	u16		sc_id_end;204	u16		sc_id;205	unsigned long	ctx_bm[DIV_ROUND_UP(SA_MAX_NUM_CTX,206				BITS_PER_LONG)];207	struct sa_tfm_ctx	*ctx;208	struct dma_chan		*dma_rx1;209	struct dma_chan		*dma_rx2;210	struct dma_chan		*dma_tx;211};212 213/**214 * struct sa_cmdl_param_info: Command label parameters info215 * @index: Index of the parameter in the command label format216 * @offset: the offset of the parameter217 * @size: Size of the parameter218 */219struct sa_cmdl_param_info {220	u16	index;221	u16	offset;222	u16	size;223};224 225/* Maximum length of Auxiliary data in 32bit words */226#define SA_MAX_AUX_DATA_WORDS	8227 228/**229 * struct sa_cmdl_upd_info: Command label updation info230 * @flags: flags in command label231 * @submode: Encryption submodes232 * @enc_size: Size of first pass encryption size233 * @enc_size2: Size of second pass encryption size234 * @enc_offset: Encryption payload offset in the packet235 * @enc_iv: Encryption initialization vector for pass2236 * @enc_iv2: Encryption initialization vector for pass2237 * @aad: Associated data238 * @payload: Payload info239 * @auth_size: Authentication size for pass 1240 * @auth_size2: Authentication size for pass 2241 * @auth_offset: Authentication payload offset242 * @auth_iv: Authentication initialization vector243 * @aux_key_info: Authentication aux key information244 * @aux_key: Aux key for authentication245 */246struct sa_cmdl_upd_info {247	u16	flags;248	u16	submode;249	struct sa_cmdl_param_info	enc_size;250	struct sa_cmdl_param_info	enc_size2;251	struct sa_cmdl_param_info	enc_offset;252	struct sa_cmdl_param_info	enc_iv;253	struct sa_cmdl_param_info	enc_iv2;254	struct sa_cmdl_param_info	aad;255	struct sa_cmdl_param_info	payload;256	struct sa_cmdl_param_info	auth_size;257	struct sa_cmdl_param_info	auth_size2;258	struct sa_cmdl_param_info	auth_offset;259	struct sa_cmdl_param_info	auth_iv;260	struct sa_cmdl_param_info	aux_key_info;261	u32				aux_key[SA_MAX_AUX_DATA_WORDS];262};263 264/*265 * Number of 32bit words appended after the command label266 * in PSDATA to identify the crypto request context.267 * word-0: Request type268 * word-1: pointer to request269 */270#define SA_PSDATA_CTX_WORDS 4271 272/* Maximum size of Command label in 32 words */273#define SA_MAX_CMDL_WORDS (SA_DMA_NUM_PS_WORDS - SA_PSDATA_CTX_WORDS)274 275/**276 * struct sa_ctx_info: SA context information277 * @sc: Pointer to security context278 * @sc_phys: Security context physical address that is passed on to SA2UL279 * @sc_id: Security context ID280 * @cmdl_size: Command label size281 * @cmdl: Command label for a particular iteration282 * @cmdl_upd_info: structure holding command label updation info283 * @epib: Extended protocol information block words284 */285struct sa_ctx_info {286	u8		*sc;287	dma_addr_t	sc_phys;288	u16		sc_id;289	u16		cmdl_size;290	u32		cmdl[SA_MAX_CMDL_WORDS];291	struct sa_cmdl_upd_info cmdl_upd_info;292	/* Store Auxiliary data such as K2/K3 subkeys in AES-XCBC */293	u32		epib[SA_DMA_NUM_EPIB_WORDS];294};295 296/**297 * struct sa_tfm_ctx: TFM context structure298 * @dev_data: struct sa_crypto_data pointer299 * @enc: struct sa_ctx_info for encryption300 * @dec: struct sa_ctx_info for decryption301 * @keylen: encrption/decryption keylength302 * @iv_idx: Initialization vector index303 * @key: encryption key304 * @fallback: SW fallback algorithm305 */306struct sa_tfm_ctx {307	struct sa_crypto_data *dev_data;308	struct sa_ctx_info enc;309	struct sa_ctx_info dec;310	struct sa_ctx_info auth;311	int keylen;312	int iv_idx;313	u32 key[AES_KEYSIZE_256 / sizeof(u32)];314	u8 authkey[SHA512_BLOCK_SIZE];315	struct crypto_shash	*shash;316	/* for fallback */317	union {318		struct crypto_skcipher		*skcipher;319		struct crypto_ahash		*ahash;320		struct crypto_aead		*aead;321	} fallback;322};323 324/**325 * struct sa_sha_req_ctx: Structure used for sha request326 * @dev_data: struct sa_crypto_data pointer327 * @cmdl: Complete command label with psdata and epib included328 * @fallback_req: SW fallback request container329 */330struct sa_sha_req_ctx {331	struct sa_crypto_data	*dev_data;332	u32			cmdl[SA_MAX_CMDL_WORDS + SA_PSDATA_CTX_WORDS];333	struct ahash_request	fallback_req;334};335 336enum sa_submode {337	SA_MODE_GEN = 0,338	SA_MODE_CCM,339	SA_MODE_GCM,340	SA_MODE_GMAC341};342 343/* Encryption algorithms */344enum sa_ealg_id {345	SA_EALG_ID_NONE = 0,        /* No encryption */346	SA_EALG_ID_NULL,            /* NULL encryption */347	SA_EALG_ID_AES_CTR,         /* AES Counter mode */348	SA_EALG_ID_AES_F8,          /* AES F8 mode */349	SA_EALG_ID_AES_CBC,         /* AES CBC mode */350	SA_EALG_ID_DES_CBC,         /* DES CBC mode */351	SA_EALG_ID_3DES_CBC,        /* 3DES CBC mode */352	SA_EALG_ID_CCM,             /* Counter with CBC-MAC mode */353	SA_EALG_ID_GCM,             /* Galois Counter mode */354	SA_EALG_ID_AES_ECB,355	SA_EALG_ID_LAST356};357 358/* Authentication algorithms */359enum sa_aalg_id {360	SA_AALG_ID_NONE = 0,      /* No Authentication  */361	SA_AALG_ID_NULL = SA_EALG_ID_LAST, /* NULL Authentication  */362	SA_AALG_ID_MD5,           /* MD5 mode */363	SA_AALG_ID_SHA1,          /* SHA1 mode */364	SA_AALG_ID_SHA2_224,      /* 224-bit SHA2 mode */365	SA_AALG_ID_SHA2_256,      /* 256-bit SHA2 mode */366	SA_AALG_ID_SHA2_512,      /* 512-bit SHA2 mode */367	SA_AALG_ID_HMAC_MD5,      /* HMAC with MD5 mode */368	SA_AALG_ID_HMAC_SHA1,     /* HMAC with SHA1 mode */369	SA_AALG_ID_HMAC_SHA2_224, /* HMAC with 224-bit SHA2 mode */370	SA_AALG_ID_HMAC_SHA2_256, /* HMAC with 256-bit SHA2 mode */371	SA_AALG_ID_GMAC,          /* Galois Message Auth. Code mode */372	SA_AALG_ID_CMAC,          /* Cipher-based Mes. Auth. Code mode */373	SA_AALG_ID_CBC_MAC,       /* Cipher Block Chaining */374	SA_AALG_ID_AES_XCBC       /* AES Extended Cipher Block Chaining */375};376 377/*378 * Mode control engine algorithms used to index the379 * mode control instruction tables380 */381enum sa_eng_algo_id {382	SA_ENG_ALGO_ECB = 0,383	SA_ENG_ALGO_CBC,384	SA_ENG_ALGO_CFB,385	SA_ENG_ALGO_OFB,386	SA_ENG_ALGO_CTR,387	SA_ENG_ALGO_F8,388	SA_ENG_ALGO_F8F9,389	SA_ENG_ALGO_GCM,390	SA_ENG_ALGO_GMAC,391	SA_ENG_ALGO_CCM,392	SA_ENG_ALGO_CMAC,393	SA_ENG_ALGO_CBCMAC,394	SA_NUM_ENG_ALGOS395};396 397/**398 * struct sa_eng_info: Security accelerator engine info399 * @eng_id: Engine ID400 * @sc_size: security context size401 */402struct sa_eng_info {403	u8	eng_id;404	u16	sc_size;405};406 407#endif /* _K3_SA2UL_ */408