162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci * Marvell OcteonTX CPT driver
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (C) 2019 Marvell International Ltd.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
762306a36Sopenharmony_ci * it under the terms of the GNU General Public License version 2 as
862306a36Sopenharmony_ci * published by the Free Software Foundation.
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef __OTX_CPTPF_UCODE_H
1262306a36Sopenharmony_ci#define __OTX_CPTPF_UCODE_H
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/pci.h>
1562306a36Sopenharmony_ci#include <linux/types.h>
1662306a36Sopenharmony_ci#include <linux/module.h>
1762306a36Sopenharmony_ci#include "otx_cpt_hw_types.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/* CPT ucode name maximum length */
2062306a36Sopenharmony_ci#define OTX_CPT_UCODE_NAME_LENGTH	64
2162306a36Sopenharmony_ci/*
2262306a36Sopenharmony_ci * On OcteonTX 83xx platform, only one type of engines is allowed to be
2362306a36Sopenharmony_ci * attached to an engine group.
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ci#define OTX_CPT_MAX_ETYPES_PER_GRP	1
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/* Default tar archive file names */
2862306a36Sopenharmony_ci#define OTX_CPT_UCODE_TAR_FILE_NAME	"cpt8x-mc.tar"
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* CPT ucode alignment */
3162306a36Sopenharmony_ci#define OTX_CPT_UCODE_ALIGNMENT		128
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* CPT ucode signature size */
3462306a36Sopenharmony_ci#define OTX_CPT_UCODE_SIGN_LEN		256
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/* Microcode version string length */
3762306a36Sopenharmony_ci#define OTX_CPT_UCODE_VER_STR_SZ	44
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/* Maximum number of supported engines/cores on OcteonTX 83XX platform */
4062306a36Sopenharmony_ci#define OTX_CPT_MAX_ENGINES		64
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define OTX_CPT_ENGS_BITMASK_LEN	(OTX_CPT_MAX_ENGINES/(BITS_PER_BYTE * \
4362306a36Sopenharmony_ci					 sizeof(unsigned long)))
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/* Microcode types */
4662306a36Sopenharmony_cienum otx_cpt_ucode_type {
4762306a36Sopenharmony_ci	OTX_CPT_AE_UC_TYPE =	1,  /* AE-MAIN */
4862306a36Sopenharmony_ci	OTX_CPT_SE_UC_TYPE1 =	20, /* SE-MAIN - combination of 21 and 22 */
4962306a36Sopenharmony_ci	OTX_CPT_SE_UC_TYPE2 =	21, /* Fast Path IPSec + AirCrypto */
5062306a36Sopenharmony_ci	OTX_CPT_SE_UC_TYPE3 =	22, /*
5162306a36Sopenharmony_ci				     * Hash + HMAC + FlexiCrypto + RNG + Full
5262306a36Sopenharmony_ci				     * Feature IPSec + AirCrypto + Kasumi
5362306a36Sopenharmony_ci				     */
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistruct otx_cpt_bitmap {
5762306a36Sopenharmony_ci	unsigned long bits[OTX_CPT_ENGS_BITMASK_LEN];
5862306a36Sopenharmony_ci	int size;
5962306a36Sopenharmony_ci};
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cistruct otx_cpt_engines {
6262306a36Sopenharmony_ci	int type;
6362306a36Sopenharmony_ci	int count;
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/* Microcode version number */
6762306a36Sopenharmony_cistruct otx_cpt_ucode_ver_num {
6862306a36Sopenharmony_ci	u8 nn;
6962306a36Sopenharmony_ci	u8 xx;
7062306a36Sopenharmony_ci	u8 yy;
7162306a36Sopenharmony_ci	u8 zz;
7262306a36Sopenharmony_ci};
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistruct otx_cpt_ucode_hdr {
7562306a36Sopenharmony_ci	struct otx_cpt_ucode_ver_num ver_num;
7662306a36Sopenharmony_ci	u8 ver_str[OTX_CPT_UCODE_VER_STR_SZ];
7762306a36Sopenharmony_ci	__be32 code_length;
7862306a36Sopenharmony_ci	u32 padding[3];
7962306a36Sopenharmony_ci};
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_cistruct otx_cpt_ucode {
8262306a36Sopenharmony_ci	u8 ver_str[OTX_CPT_UCODE_VER_STR_SZ];/*
8362306a36Sopenharmony_ci					      * ucode version in readable format
8462306a36Sopenharmony_ci					      */
8562306a36Sopenharmony_ci	struct otx_cpt_ucode_ver_num ver_num;/* ucode version number */
8662306a36Sopenharmony_ci	char filename[OTX_CPT_UCODE_NAME_LENGTH];	 /* ucode filename */
8762306a36Sopenharmony_ci	dma_addr_t dma;		/* phys address of ucode image */
8862306a36Sopenharmony_ci	dma_addr_t align_dma;	/* aligned phys address of ucode image */
8962306a36Sopenharmony_ci	void *va;		/* virt address of ucode image */
9062306a36Sopenharmony_ci	void *align_va;		/* aligned virt address of ucode image */
9162306a36Sopenharmony_ci	u32 size;		/* ucode image size */
9262306a36Sopenharmony_ci	int type;		/* ucode image type SE or AE */
9362306a36Sopenharmony_ci};
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistruct tar_ucode_info_t {
9662306a36Sopenharmony_ci	struct list_head list;
9762306a36Sopenharmony_ci	struct otx_cpt_ucode ucode;/* microcode information */
9862306a36Sopenharmony_ci	const u8 *ucode_ptr;	/* pointer to microcode in tar archive */
9962306a36Sopenharmony_ci};
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/* Maximum and current number of engines available for all engine groups */
10262306a36Sopenharmony_cistruct otx_cpt_engs_available {
10362306a36Sopenharmony_ci	int max_se_cnt;
10462306a36Sopenharmony_ci	int max_ae_cnt;
10562306a36Sopenharmony_ci	int se_cnt;
10662306a36Sopenharmony_ci	int ae_cnt;
10762306a36Sopenharmony_ci};
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci/* Engines reserved to an engine group */
11062306a36Sopenharmony_cistruct otx_cpt_engs_rsvd {
11162306a36Sopenharmony_ci	int type;	/* engine type */
11262306a36Sopenharmony_ci	int count;	/* number of engines attached */
11362306a36Sopenharmony_ci	int offset;     /* constant offset of engine type in the bitmap */
11462306a36Sopenharmony_ci	unsigned long *bmap;		/* attached engines bitmap */
11562306a36Sopenharmony_ci	struct otx_cpt_ucode *ucode;	/* ucode used by these engines */
11662306a36Sopenharmony_ci};
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistruct otx_cpt_mirror_info {
11962306a36Sopenharmony_ci	int is_ena;	/*
12062306a36Sopenharmony_ci			 * is mirroring enabled, it is set only for engine
12162306a36Sopenharmony_ci			 * group which mirrors another engine group
12262306a36Sopenharmony_ci			 */
12362306a36Sopenharmony_ci	int idx;	/*
12462306a36Sopenharmony_ci			 * index of engine group which is mirrored by this
12562306a36Sopenharmony_ci			 * group, set only for engine group which mirrors
12662306a36Sopenharmony_ci			 * another group
12762306a36Sopenharmony_ci			 */
12862306a36Sopenharmony_ci	int ref_count;	/*
12962306a36Sopenharmony_ci			 * number of times this engine group is mirrored by
13062306a36Sopenharmony_ci			 * other groups, this is set only for engine group
13162306a36Sopenharmony_ci			 * which is mirrored by other group(s)
13262306a36Sopenharmony_ci			 */
13362306a36Sopenharmony_ci};
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistruct otx_cpt_eng_grp_info {
13662306a36Sopenharmony_ci	struct otx_cpt_eng_grps *g; /* pointer to engine_groups structure */
13762306a36Sopenharmony_ci	struct device_attribute info_attr; /* group info entry attr */
13862306a36Sopenharmony_ci	/* engines attached */
13962306a36Sopenharmony_ci	struct otx_cpt_engs_rsvd engs[OTX_CPT_MAX_ETYPES_PER_GRP];
14062306a36Sopenharmony_ci	/* Microcode information */
14162306a36Sopenharmony_ci	struct otx_cpt_ucode ucode[OTX_CPT_MAX_ETYPES_PER_GRP];
14262306a36Sopenharmony_ci	/* sysfs info entry name */
14362306a36Sopenharmony_ci	char sysfs_info_name[OTX_CPT_UCODE_NAME_LENGTH];
14462306a36Sopenharmony_ci	/* engine group mirroring information */
14562306a36Sopenharmony_ci	struct otx_cpt_mirror_info mirror;
14662306a36Sopenharmony_ci	int idx;	 /* engine group index */
14762306a36Sopenharmony_ci	bool is_enabled; /*
14862306a36Sopenharmony_ci			  * is engine group enabled, engine group is enabled
14962306a36Sopenharmony_ci			  * when it has engines attached and ucode loaded
15062306a36Sopenharmony_ci			  */
15162306a36Sopenharmony_ci};
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistruct otx_cpt_eng_grps {
15462306a36Sopenharmony_ci	struct otx_cpt_eng_grp_info grp[OTX_CPT_MAX_ENGINE_GROUPS];
15562306a36Sopenharmony_ci	struct device_attribute ucode_load_attr;/* ucode load attr */
15662306a36Sopenharmony_ci	struct otx_cpt_engs_available avail;
15762306a36Sopenharmony_ci	struct mutex lock;
15862306a36Sopenharmony_ci	void *obj;
15962306a36Sopenharmony_ci	int engs_num;			/* total number of engines supported */
16062306a36Sopenharmony_ci	int eng_types_supported;	/* engine types supported SE, AE */
16162306a36Sopenharmony_ci	u8 eng_ref_cnt[OTX_CPT_MAX_ENGINES];/* engines reference count */
16262306a36Sopenharmony_ci	bool is_ucode_load_created;	/* is ucode_load sysfs entry created */
16362306a36Sopenharmony_ci	bool is_first_try; /* is this first try to create kcrypto engine grp */
16462306a36Sopenharmony_ci	bool is_rdonly;	/* do engine groups configuration can be modified */
16562306a36Sopenharmony_ci};
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciint otx_cpt_init_eng_grps(struct pci_dev *pdev,
16862306a36Sopenharmony_ci			  struct otx_cpt_eng_grps *eng_grps, int pf_type);
16962306a36Sopenharmony_civoid otx_cpt_cleanup_eng_grps(struct pci_dev *pdev,
17062306a36Sopenharmony_ci			      struct otx_cpt_eng_grps *eng_grps);
17162306a36Sopenharmony_ciint otx_cpt_try_create_default_eng_grps(struct pci_dev *pdev,
17262306a36Sopenharmony_ci					struct otx_cpt_eng_grps *eng_grps,
17362306a36Sopenharmony_ci					int pf_type);
17462306a36Sopenharmony_civoid otx_cpt_set_eng_grps_is_rdonly(struct otx_cpt_eng_grps *eng_grps,
17562306a36Sopenharmony_ci				    bool is_rdonly);
17662306a36Sopenharmony_ciint otx_cpt_uc_supports_eng_type(struct otx_cpt_ucode *ucode, int eng_type);
17762306a36Sopenharmony_ciint otx_cpt_eng_grp_has_eng_type(struct otx_cpt_eng_grp_info *eng_grp,
17862306a36Sopenharmony_ci				 int eng_type);
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci#endif /* __OTX_CPTPF_UCODE_H */
181