18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci * Marvell OcteonTX CPT driver
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2019 Marvell International Ltd.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
78c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License version 2 as
88c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifndef __OTX_CPTPF_UCODE_H
128c2ecf20Sopenharmony_ci#define __OTX_CPTPF_UCODE_H
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci#include <linux/types.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include "otx_cpt_hw_types.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* CPT ucode name maximum length */
208c2ecf20Sopenharmony_ci#define OTX_CPT_UCODE_NAME_LENGTH	64
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * On OcteonTX 83xx platform, only one type of engines is allowed to be
238c2ecf20Sopenharmony_ci * attached to an engine group.
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci#define OTX_CPT_MAX_ETYPES_PER_GRP	1
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* Default tar archive file names */
288c2ecf20Sopenharmony_ci#define OTX_CPT_UCODE_TAR_FILE_NAME	"cpt8x-mc.tar"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* CPT ucode alignment */
318c2ecf20Sopenharmony_ci#define OTX_CPT_UCODE_ALIGNMENT		128
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* CPT ucode signature size */
348c2ecf20Sopenharmony_ci#define OTX_CPT_UCODE_SIGN_LEN		256
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Microcode version string length */
378c2ecf20Sopenharmony_ci#define OTX_CPT_UCODE_VER_STR_SZ	44
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* Maximum number of supported engines/cores on OcteonTX 83XX platform */
408c2ecf20Sopenharmony_ci#define OTX_CPT_MAX_ENGINES		64
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define OTX_CPT_ENGS_BITMASK_LEN	(OTX_CPT_MAX_ENGINES/(BITS_PER_BYTE * \
438c2ecf20Sopenharmony_ci					 sizeof(unsigned long)))
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* Microcode types */
468c2ecf20Sopenharmony_cienum otx_cpt_ucode_type {
478c2ecf20Sopenharmony_ci	OTX_CPT_AE_UC_TYPE =	1,  /* AE-MAIN */
488c2ecf20Sopenharmony_ci	OTX_CPT_SE_UC_TYPE1 =	20, /* SE-MAIN - combination of 21 and 22 */
498c2ecf20Sopenharmony_ci	OTX_CPT_SE_UC_TYPE2 =	21, /* Fast Path IPSec + AirCrypto */
508c2ecf20Sopenharmony_ci	OTX_CPT_SE_UC_TYPE3 =	22, /*
518c2ecf20Sopenharmony_ci				     * Hash + HMAC + FlexiCrypto + RNG + Full
528c2ecf20Sopenharmony_ci				     * Feature IPSec + AirCrypto + Kasumi
538c2ecf20Sopenharmony_ci				     */
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistruct otx_cpt_bitmap {
578c2ecf20Sopenharmony_ci	unsigned long bits[OTX_CPT_ENGS_BITMASK_LEN];
588c2ecf20Sopenharmony_ci	int size;
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistruct otx_cpt_engines {
628c2ecf20Sopenharmony_ci	int type;
638c2ecf20Sopenharmony_ci	int count;
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* Microcode version number */
678c2ecf20Sopenharmony_cistruct otx_cpt_ucode_ver_num {
688c2ecf20Sopenharmony_ci	u8 nn;
698c2ecf20Sopenharmony_ci	u8 xx;
708c2ecf20Sopenharmony_ci	u8 yy;
718c2ecf20Sopenharmony_ci	u8 zz;
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistruct otx_cpt_ucode_hdr {
758c2ecf20Sopenharmony_ci	struct otx_cpt_ucode_ver_num ver_num;
768c2ecf20Sopenharmony_ci	u8 ver_str[OTX_CPT_UCODE_VER_STR_SZ];
778c2ecf20Sopenharmony_ci	__be32 code_length;
788c2ecf20Sopenharmony_ci	u32 padding[3];
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistruct otx_cpt_ucode {
828c2ecf20Sopenharmony_ci	u8 ver_str[OTX_CPT_UCODE_VER_STR_SZ];/*
838c2ecf20Sopenharmony_ci					      * ucode version in readable format
848c2ecf20Sopenharmony_ci					      */
858c2ecf20Sopenharmony_ci	struct otx_cpt_ucode_ver_num ver_num;/* ucode version number */
868c2ecf20Sopenharmony_ci	char filename[OTX_CPT_UCODE_NAME_LENGTH];	 /* ucode filename */
878c2ecf20Sopenharmony_ci	dma_addr_t dma;		/* phys address of ucode image */
888c2ecf20Sopenharmony_ci	dma_addr_t align_dma;	/* aligned phys address of ucode image */
898c2ecf20Sopenharmony_ci	void *va;		/* virt address of ucode image */
908c2ecf20Sopenharmony_ci	void *align_va;		/* aligned virt address of ucode image */
918c2ecf20Sopenharmony_ci	u32 size;		/* ucode image size */
928c2ecf20Sopenharmony_ci	int type;		/* ucode image type SE or AE */
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistruct tar_ucode_info_t {
968c2ecf20Sopenharmony_ci	struct list_head list;
978c2ecf20Sopenharmony_ci	struct otx_cpt_ucode ucode;/* microcode information */
988c2ecf20Sopenharmony_ci	const u8 *ucode_ptr;	/* pointer to microcode in tar archive */
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* Maximum and current number of engines available for all engine groups */
1028c2ecf20Sopenharmony_cistruct otx_cpt_engs_available {
1038c2ecf20Sopenharmony_ci	int max_se_cnt;
1048c2ecf20Sopenharmony_ci	int max_ae_cnt;
1058c2ecf20Sopenharmony_ci	int se_cnt;
1068c2ecf20Sopenharmony_ci	int ae_cnt;
1078c2ecf20Sopenharmony_ci};
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* Engines reserved to an engine group */
1108c2ecf20Sopenharmony_cistruct otx_cpt_engs_rsvd {
1118c2ecf20Sopenharmony_ci	int type;	/* engine type */
1128c2ecf20Sopenharmony_ci	int count;	/* number of engines attached */
1138c2ecf20Sopenharmony_ci	int offset;     /* constant offset of engine type in the bitmap */
1148c2ecf20Sopenharmony_ci	unsigned long *bmap;		/* attached engines bitmap */
1158c2ecf20Sopenharmony_ci	struct otx_cpt_ucode *ucode;	/* ucode used by these engines */
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistruct otx_cpt_mirror_info {
1198c2ecf20Sopenharmony_ci	int is_ena;	/*
1208c2ecf20Sopenharmony_ci			 * is mirroring enabled, it is set only for engine
1218c2ecf20Sopenharmony_ci			 * group which mirrors another engine group
1228c2ecf20Sopenharmony_ci			 */
1238c2ecf20Sopenharmony_ci	int idx;	/*
1248c2ecf20Sopenharmony_ci			 * index of engine group which is mirrored by this
1258c2ecf20Sopenharmony_ci			 * group, set only for engine group which mirrors
1268c2ecf20Sopenharmony_ci			 * another group
1278c2ecf20Sopenharmony_ci			 */
1288c2ecf20Sopenharmony_ci	int ref_count;	/*
1298c2ecf20Sopenharmony_ci			 * number of times this engine group is mirrored by
1308c2ecf20Sopenharmony_ci			 * other groups, this is set only for engine group
1318c2ecf20Sopenharmony_ci			 * which is mirrored by other group(s)
1328c2ecf20Sopenharmony_ci			 */
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistruct otx_cpt_eng_grp_info {
1368c2ecf20Sopenharmony_ci	struct otx_cpt_eng_grps *g; /* pointer to engine_groups structure */
1378c2ecf20Sopenharmony_ci	struct device_attribute info_attr; /* group info entry attr */
1388c2ecf20Sopenharmony_ci	/* engines attached */
1398c2ecf20Sopenharmony_ci	struct otx_cpt_engs_rsvd engs[OTX_CPT_MAX_ETYPES_PER_GRP];
1408c2ecf20Sopenharmony_ci	/* Microcode information */
1418c2ecf20Sopenharmony_ci	struct otx_cpt_ucode ucode[OTX_CPT_MAX_ETYPES_PER_GRP];
1428c2ecf20Sopenharmony_ci	/* sysfs info entry name */
1438c2ecf20Sopenharmony_ci	char sysfs_info_name[OTX_CPT_UCODE_NAME_LENGTH];
1448c2ecf20Sopenharmony_ci	/* engine group mirroring information */
1458c2ecf20Sopenharmony_ci	struct otx_cpt_mirror_info mirror;
1468c2ecf20Sopenharmony_ci	int idx;	 /* engine group index */
1478c2ecf20Sopenharmony_ci	bool is_enabled; /*
1488c2ecf20Sopenharmony_ci			  * is engine group enabled, engine group is enabled
1498c2ecf20Sopenharmony_ci			  * when it has engines attached and ucode loaded
1508c2ecf20Sopenharmony_ci			  */
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistruct otx_cpt_eng_grps {
1548c2ecf20Sopenharmony_ci	struct otx_cpt_eng_grp_info grp[OTX_CPT_MAX_ENGINE_GROUPS];
1558c2ecf20Sopenharmony_ci	struct device_attribute ucode_load_attr;/* ucode load attr */
1568c2ecf20Sopenharmony_ci	struct otx_cpt_engs_available avail;
1578c2ecf20Sopenharmony_ci	struct mutex lock;
1588c2ecf20Sopenharmony_ci	void *obj;
1598c2ecf20Sopenharmony_ci	int engs_num;			/* total number of engines supported */
1608c2ecf20Sopenharmony_ci	int eng_types_supported;	/* engine types supported SE, AE */
1618c2ecf20Sopenharmony_ci	u8 eng_ref_cnt[OTX_CPT_MAX_ENGINES];/* engines reference count */
1628c2ecf20Sopenharmony_ci	bool is_ucode_load_created;	/* is ucode_load sysfs entry created */
1638c2ecf20Sopenharmony_ci	bool is_first_try; /* is this first try to create kcrypto engine grp */
1648c2ecf20Sopenharmony_ci	bool is_rdonly;	/* do engine groups configuration can be modified */
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciint otx_cpt_init_eng_grps(struct pci_dev *pdev,
1688c2ecf20Sopenharmony_ci			  struct otx_cpt_eng_grps *eng_grps, int pf_type);
1698c2ecf20Sopenharmony_civoid otx_cpt_cleanup_eng_grps(struct pci_dev *pdev,
1708c2ecf20Sopenharmony_ci			      struct otx_cpt_eng_grps *eng_grps);
1718c2ecf20Sopenharmony_ciint otx_cpt_try_create_default_eng_grps(struct pci_dev *pdev,
1728c2ecf20Sopenharmony_ci					struct otx_cpt_eng_grps *eng_grps,
1738c2ecf20Sopenharmony_ci					int pf_type);
1748c2ecf20Sopenharmony_civoid otx_cpt_set_eng_grps_is_rdonly(struct otx_cpt_eng_grps *eng_grps,
1758c2ecf20Sopenharmony_ci				    bool is_rdonly);
1768c2ecf20Sopenharmony_ciint otx_cpt_uc_supports_eng_type(struct otx_cpt_ucode *ucode, int eng_type);
1778c2ecf20Sopenharmony_ciint otx_cpt_eng_grp_has_eng_type(struct otx_cpt_eng_grp_info *eng_grp,
1788c2ecf20Sopenharmony_ci				 int eng_type);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci#endif /* __OTX_CPTPF_UCODE_H */
181