162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci * Copyright (C) 2020 Marvell.
362306a36Sopenharmony_ci */
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#ifndef __OTX2_CPTPF_UCODE_H
662306a36Sopenharmony_ci#define __OTX2_CPTPF_UCODE_H
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/pci.h>
962306a36Sopenharmony_ci#include <linux/types.h>
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include "otx2_cpt_hw_types.h"
1262306a36Sopenharmony_ci#include "otx2_cpt_common.h"
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/*
1562306a36Sopenharmony_ci * On OcteonTX2 platform IPSec ucode can use both IE and SE engines therefore
1662306a36Sopenharmony_ci * IE and SE engines can be attached to the same engine group.
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_ci#define OTX2_CPT_MAX_ETYPES_PER_GRP 2
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* CPT ucode signature size */
2162306a36Sopenharmony_ci#define OTX2_CPT_UCODE_SIGN_LEN     256
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/* Microcode version string length */
2462306a36Sopenharmony_ci#define OTX2_CPT_UCODE_VER_STR_SZ   44
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/* Maximum number of supported engines/cores on OcteonTX2/CN10K platform */
2762306a36Sopenharmony_ci#define OTX2_CPT_MAX_ENGINES        144
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define OTX2_CPT_ENGS_BITMASK_LEN   BITS_TO_LONGS(OTX2_CPT_MAX_ENGINES)
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#define OTX2_CPT_UCODE_SZ           (64 * 1024)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* Microcode types */
3462306a36Sopenharmony_cienum otx2_cpt_ucode_type {
3562306a36Sopenharmony_ci	OTX2_CPT_AE_UC_TYPE = 1,  /* AE-MAIN */
3662306a36Sopenharmony_ci	OTX2_CPT_SE_UC_TYPE1 = 20,/* SE-MAIN - combination of 21 and 22 */
3762306a36Sopenharmony_ci	OTX2_CPT_SE_UC_TYPE2 = 21,/* Fast Path IPSec + AirCrypto */
3862306a36Sopenharmony_ci	OTX2_CPT_SE_UC_TYPE3 = 22,/*
3962306a36Sopenharmony_ci				   * Hash + HMAC + FlexiCrypto + RNG +
4062306a36Sopenharmony_ci				   * Full Feature IPSec + AirCrypto + Kasumi
4162306a36Sopenharmony_ci				   */
4262306a36Sopenharmony_ci	OTX2_CPT_IE_UC_TYPE1 = 30, /* IE-MAIN - combination of 31 and 32 */
4362306a36Sopenharmony_ci	OTX2_CPT_IE_UC_TYPE2 = 31, /* Fast Path IPSec */
4462306a36Sopenharmony_ci	OTX2_CPT_IE_UC_TYPE3 = 32, /*
4562306a36Sopenharmony_ci				    * Hash + HMAC + FlexiCrypto + RNG +
4662306a36Sopenharmony_ci				    * Full Future IPSec
4762306a36Sopenharmony_ci				    */
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistruct otx2_cpt_bitmap {
5162306a36Sopenharmony_ci	unsigned long bits[OTX2_CPT_ENGS_BITMASK_LEN];
5262306a36Sopenharmony_ci	int size;
5362306a36Sopenharmony_ci};
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistruct otx2_cpt_engines {
5662306a36Sopenharmony_ci	int type;
5762306a36Sopenharmony_ci	int count;
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci/* Microcode version number */
6162306a36Sopenharmony_cistruct otx2_cpt_ucode_ver_num {
6262306a36Sopenharmony_ci	u8 nn;
6362306a36Sopenharmony_ci	u8 xx;
6462306a36Sopenharmony_ci	u8 yy;
6562306a36Sopenharmony_ci	u8 zz;
6662306a36Sopenharmony_ci};
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistruct otx2_cpt_ucode_hdr {
6962306a36Sopenharmony_ci	struct otx2_cpt_ucode_ver_num ver_num;
7062306a36Sopenharmony_ci	u8 ver_str[OTX2_CPT_UCODE_VER_STR_SZ];
7162306a36Sopenharmony_ci	__be32 code_length;
7262306a36Sopenharmony_ci	u32 padding[3];
7362306a36Sopenharmony_ci};
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistruct otx2_cpt_ucode {
7662306a36Sopenharmony_ci	u8 ver_str[OTX2_CPT_UCODE_VER_STR_SZ];/*
7762306a36Sopenharmony_ci					       * ucode version in readable
7862306a36Sopenharmony_ci					       * format
7962306a36Sopenharmony_ci					       */
8062306a36Sopenharmony_ci	struct otx2_cpt_ucode_ver_num ver_num;/* ucode version number */
8162306a36Sopenharmony_ci	char filename[OTX2_CPT_NAME_LENGTH];/* ucode filename */
8262306a36Sopenharmony_ci	dma_addr_t dma;		/* phys address of ucode image */
8362306a36Sopenharmony_ci	void *va;		/* virt address of ucode image */
8462306a36Sopenharmony_ci	u32 size;		/* ucode image size */
8562306a36Sopenharmony_ci	int type;		/* ucode image type SE, IE, AE or SE+IE */
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistruct otx2_cpt_uc_info_t {
8962306a36Sopenharmony_ci	struct list_head list;
9062306a36Sopenharmony_ci	struct otx2_cpt_ucode ucode;/* microcode information */
9162306a36Sopenharmony_ci	const struct firmware *fw;
9262306a36Sopenharmony_ci};
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/* Maximum and current number of engines available for all engine groups */
9562306a36Sopenharmony_cistruct otx2_cpt_engs_available {
9662306a36Sopenharmony_ci	int max_se_cnt;
9762306a36Sopenharmony_ci	int max_ie_cnt;
9862306a36Sopenharmony_ci	int max_ae_cnt;
9962306a36Sopenharmony_ci	int se_cnt;
10062306a36Sopenharmony_ci	int ie_cnt;
10162306a36Sopenharmony_ci	int ae_cnt;
10262306a36Sopenharmony_ci};
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci/* Engines reserved to an engine group */
10562306a36Sopenharmony_cistruct otx2_cpt_engs_rsvd {
10662306a36Sopenharmony_ci	int type;	/* engine type */
10762306a36Sopenharmony_ci	int count;	/* number of engines attached */
10862306a36Sopenharmony_ci	int offset;     /* constant offset of engine type in the bitmap */
10962306a36Sopenharmony_ci	unsigned long *bmap;		/* attached engines bitmap */
11062306a36Sopenharmony_ci	struct otx2_cpt_ucode *ucode;	/* ucode used by these engines */
11162306a36Sopenharmony_ci};
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistruct otx2_cpt_mirror_info {
11462306a36Sopenharmony_ci	int is_ena;	/*
11562306a36Sopenharmony_ci			 * is mirroring enabled, it is set only for engine
11662306a36Sopenharmony_ci			 * group which mirrors another engine group
11762306a36Sopenharmony_ci			 */
11862306a36Sopenharmony_ci	int idx;	/*
11962306a36Sopenharmony_ci			 * index of engine group which is mirrored by this
12062306a36Sopenharmony_ci			 * group, set only for engine group which mirrors
12162306a36Sopenharmony_ci			 * another group
12262306a36Sopenharmony_ci			 */
12362306a36Sopenharmony_ci	int ref_count;	/*
12462306a36Sopenharmony_ci			 * number of times this engine group is mirrored by
12562306a36Sopenharmony_ci			 * other groups, this is set only for engine group
12662306a36Sopenharmony_ci			 * which is mirrored by other group(s)
12762306a36Sopenharmony_ci			 */
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistruct otx2_cpt_eng_grp_info {
13162306a36Sopenharmony_ci	struct otx2_cpt_eng_grps *g; /* pointer to engine_groups structure */
13262306a36Sopenharmony_ci	/* engines attached */
13362306a36Sopenharmony_ci	struct otx2_cpt_engs_rsvd engs[OTX2_CPT_MAX_ETYPES_PER_GRP];
13462306a36Sopenharmony_ci	/* ucodes information */
13562306a36Sopenharmony_ci	struct otx2_cpt_ucode ucode[OTX2_CPT_MAX_ETYPES_PER_GRP];
13662306a36Sopenharmony_ci	/* engine group mirroring information */
13762306a36Sopenharmony_ci	struct otx2_cpt_mirror_info mirror;
13862306a36Sopenharmony_ci	int idx;	 /* engine group index */
13962306a36Sopenharmony_ci	bool is_enabled; /*
14062306a36Sopenharmony_ci			  * is engine group enabled, engine group is enabled
14162306a36Sopenharmony_ci			  * when it has engines attached and ucode loaded
14262306a36Sopenharmony_ci			  */
14362306a36Sopenharmony_ci};
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_cistruct otx2_cpt_eng_grps {
14662306a36Sopenharmony_ci	struct mutex lock;
14762306a36Sopenharmony_ci	struct otx2_cpt_eng_grp_info grp[OTX2_CPT_MAX_ENGINE_GROUPS];
14862306a36Sopenharmony_ci	struct otx2_cpt_engs_available avail;
14962306a36Sopenharmony_ci	void *obj;			/* device specific data */
15062306a36Sopenharmony_ci	int engs_num;			/* total number of engines supported */
15162306a36Sopenharmony_ci	u8 eng_ref_cnt[OTX2_CPT_MAX_ENGINES];/* engines reference count */
15262306a36Sopenharmony_ci	bool is_grps_created; /* Is the engine groups are already created */
15362306a36Sopenharmony_ci};
15462306a36Sopenharmony_cistruct otx2_cptpf_dev;
15562306a36Sopenharmony_ciint otx2_cpt_init_eng_grps(struct pci_dev *pdev,
15662306a36Sopenharmony_ci			   struct otx2_cpt_eng_grps *eng_grps);
15762306a36Sopenharmony_civoid otx2_cpt_cleanup_eng_grps(struct pci_dev *pdev,
15862306a36Sopenharmony_ci			       struct otx2_cpt_eng_grps *eng_grps);
15962306a36Sopenharmony_ciint otx2_cpt_create_eng_grps(struct otx2_cptpf_dev *cptpf,
16062306a36Sopenharmony_ci			     struct otx2_cpt_eng_grps *eng_grps);
16162306a36Sopenharmony_ciint otx2_cpt_disable_all_cores(struct otx2_cptpf_dev *cptpf);
16262306a36Sopenharmony_ciint otx2_cpt_get_eng_grp(struct otx2_cpt_eng_grps *eng_grps, int eng_type);
16362306a36Sopenharmony_ciint otx2_cpt_discover_eng_capabilities(struct otx2_cptpf_dev *cptpf);
16462306a36Sopenharmony_ciint otx2_cpt_dl_custom_egrp_create(struct otx2_cptpf_dev *cptpf,
16562306a36Sopenharmony_ci				   struct devlink_param_gset_ctx *ctx);
16662306a36Sopenharmony_ciint otx2_cpt_dl_custom_egrp_delete(struct otx2_cptpf_dev *cptpf,
16762306a36Sopenharmony_ci				   struct devlink_param_gset_ctx *ctx);
16862306a36Sopenharmony_civoid otx2_cpt_print_uc_dbg_info(struct otx2_cptpf_dev *cptpf);
16962306a36Sopenharmony_cistruct otx2_cpt_engs_rsvd *find_engines_by_type(
17062306a36Sopenharmony_ci					struct otx2_cpt_eng_grp_info *eng_grp,
17162306a36Sopenharmony_ci					int eng_type);
17262306a36Sopenharmony_ci#endif /* __OTX2_CPTPF_UCODE_H */
173