xref: /openbmc/linux/drivers/crypto/hisilicon/zip/zip_main.c (revision cd1b7ae3435cc428579615241adeee36d217320c)
162c455caSZhou Wang // SPDX-License-Identifier: GPL-2.0
262c455caSZhou Wang /* Copyright (c) 2019 HiSilicon Limited. */
362c455caSZhou Wang #include <linux/acpi.h>
462c455caSZhou Wang #include <linux/aer.h>
562c455caSZhou Wang #include <linux/bitops.h>
672c7a68dSZhou Wang #include <linux/debugfs.h>
762c455caSZhou Wang #include <linux/init.h>
862c455caSZhou Wang #include <linux/io.h>
962c455caSZhou Wang #include <linux/kernel.h>
1062c455caSZhou Wang #include <linux/module.h>
1162c455caSZhou Wang #include <linux/pci.h>
1272c7a68dSZhou Wang #include <linux/seq_file.h>
1362c455caSZhou Wang #include <linux/topology.h>
149e00df71SZhangfei Gao #include <linux/uacce.h>
1562c455caSZhou Wang #include "zip.h"
1662c455caSZhou Wang 
1762c455caSZhou Wang #define PCI_DEVICE_ID_ZIP_PF		0xa250
1879e09f30SZhou Wang #define PCI_DEVICE_ID_ZIP_VF		0xa251
1962c455caSZhou Wang 
2062c455caSZhou Wang #define HZIP_VF_NUM			63
2162c455caSZhou Wang #define HZIP_QUEUE_NUM_V1		4096
2262c455caSZhou Wang #define HZIP_QUEUE_NUM_V2		1024
2362c455caSZhou Wang 
2462c455caSZhou Wang #define HZIP_CLOCK_GATE_CTRL		0x301004
2562c455caSZhou Wang #define COMP0_ENABLE			BIT(0)
2662c455caSZhou Wang #define COMP1_ENABLE			BIT(1)
2762c455caSZhou Wang #define DECOMP0_ENABLE			BIT(2)
2862c455caSZhou Wang #define DECOMP1_ENABLE			BIT(3)
2962c455caSZhou Wang #define DECOMP2_ENABLE			BIT(4)
3062c455caSZhou Wang #define DECOMP3_ENABLE			BIT(5)
3162c455caSZhou Wang #define DECOMP4_ENABLE			BIT(6)
3262c455caSZhou Wang #define DECOMP5_ENABLE			BIT(7)
3362c455caSZhou Wang #define ALL_COMP_DECOMP_EN		(COMP0_ENABLE | COMP1_ENABLE |	\
3462c455caSZhou Wang 					 DECOMP0_ENABLE | DECOMP1_ENABLE | \
3562c455caSZhou Wang 					 DECOMP2_ENABLE | DECOMP3_ENABLE | \
3662c455caSZhou Wang 					 DECOMP4_ENABLE | DECOMP5_ENABLE)
3762c455caSZhou Wang #define DECOMP_CHECK_ENABLE		BIT(16)
3872c7a68dSZhou Wang #define HZIP_FSM_MAX_CNT		0x301008
3962c455caSZhou Wang 
4062c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_0		0x301040
4162c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_1		0x301044
4262c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_0		0x301060
4362c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_1		0x301064
4462c455caSZhou Wang #define CACHE_ALL_EN			0xffffffff
4562c455caSZhou Wang 
4662c455caSZhou Wang #define HZIP_BD_RUSER_32_63		0x301110
4762c455caSZhou Wang #define HZIP_SGL_RUSER_32_63		0x30111c
4862c455caSZhou Wang #define HZIP_DATA_RUSER_32_63		0x301128
4962c455caSZhou Wang #define HZIP_DATA_WUSER_32_63		0x301134
5062c455caSZhou Wang #define HZIP_BD_WUSER_32_63		0x301140
5162c455caSZhou Wang 
5272c7a68dSZhou Wang #define HZIP_QM_IDEL_STATUS		0x3040e4
5362c455caSZhou Wang 
5472c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_0		0x302000
5572c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_1		0x303000
5672c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_0	0x304000
5772c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_1	0x305000
5872c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_2	0x306000
5972c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_3	0x307000
6072c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_4	0x308000
6172c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_5	0x309000
6262c455caSZhou Wang 
6362c455caSZhou Wang #define HZIP_CORE_INT_SOURCE		0x3010A0
64eaebf4c3SShukun Tan #define HZIP_CORE_INT_MASK_REG		0x3010A4
6562c455caSZhou Wang #define HZIP_CORE_INT_STATUS		0x3010AC
6662c455caSZhou Wang #define HZIP_CORE_INT_STATUS_M_ECC	BIT(1)
6762c455caSZhou Wang #define HZIP_CORE_SRAM_ECC_ERR_INFO	0x301148
68de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_CE_ENB	0x301160
69de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_NFE_ENB	0x301164
70de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_FE_ENB        0x301168
71de3daf4bSShukun Tan #define HZIP_CORE_INT_RAS_NFE_ENABLE	0x7FE
72f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_NUM_SHIFT	16
73f826e6efSShukun Tan #define HZIP_SRAM_ECC_ERR_ADDR_SHIFT	24
74eaebf4c3SShukun Tan #define HZIP_CORE_INT_MASK_ALL		GENMASK(10, 0)
7572c7a68dSZhou Wang #define HZIP_COMP_CORE_NUM		2
7672c7a68dSZhou Wang #define HZIP_DECOMP_CORE_NUM		6
7772c7a68dSZhou Wang #define HZIP_CORE_NUM			(HZIP_COMP_CORE_NUM + \
7872c7a68dSZhou Wang 					 HZIP_DECOMP_CORE_NUM)
7962c455caSZhou Wang #define HZIP_SQE_SIZE			128
8072c7a68dSZhou Wang #define HZIP_SQ_SIZE			(HZIP_SQE_SIZE * QM_Q_DEPTH)
8162c455caSZhou Wang #define HZIP_PF_DEF_Q_NUM		64
8262c455caSZhou Wang #define HZIP_PF_DEF_Q_BASE		0
8362c455caSZhou Wang 
8472c7a68dSZhou Wang #define HZIP_SOFT_CTRL_CNT_CLR_CE	0x301000
8572c7a68dSZhou Wang #define SOFT_CTRL_CNT_CLR_CE_BIT	BIT(0)
8662c455caSZhou Wang 
8772c7a68dSZhou Wang #define HZIP_BUF_SIZE			22
8862c455caSZhou Wang 
8962c455caSZhou Wang static const char hisi_zip_name[] = "hisi_zip";
9072c7a68dSZhou Wang static struct dentry *hzip_debugfs_root;
9118f1ab3fSShukun Tan static struct hisi_qm_list zip_devices;
9262c455caSZhou Wang 
9362c455caSZhou Wang struct hisi_zip_hw_error {
9462c455caSZhou Wang 	u32 int_msk;
9562c455caSZhou Wang 	const char *msg;
9662c455caSZhou Wang };
9762c455caSZhou Wang 
9862c455caSZhou Wang static const struct hisi_zip_hw_error zip_hw_error[] = {
9962c455caSZhou Wang 	{ .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" },
10062c455caSZhou Wang 	{ .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" },
10162c455caSZhou Wang 	{ .int_msk = BIT(2), .msg = "zip_axi_rresp_err" },
10262c455caSZhou Wang 	{ .int_msk = BIT(3), .msg = "zip_axi_bresp_err" },
10362c455caSZhou Wang 	{ .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" },
10462c455caSZhou Wang 	{ .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" },
10562c455caSZhou Wang 	{ .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" },
10662c455caSZhou Wang 	{ .int_msk = BIT(7), .msg = "zip_pre_in_data_err" },
10762c455caSZhou Wang 	{ .int_msk = BIT(8), .msg = "zip_com_inf_err" },
10862c455caSZhou Wang 	{ .int_msk = BIT(9), .msg = "zip_enc_inf_err" },
10962c455caSZhou Wang 	{ .int_msk = BIT(10), .msg = "zip_pre_out_err" },
11062c455caSZhou Wang 	{ /* sentinel */ }
11162c455caSZhou Wang };
11262c455caSZhou Wang 
11372c7a68dSZhou Wang enum ctrl_debug_file_index {
11472c7a68dSZhou Wang 	HZIP_CURRENT_QM,
11572c7a68dSZhou Wang 	HZIP_CLEAR_ENABLE,
11672c7a68dSZhou Wang 	HZIP_DEBUG_FILE_NUM,
11772c7a68dSZhou Wang };
11872c7a68dSZhou Wang 
11972c7a68dSZhou Wang static const char * const ctrl_debug_file_name[] = {
12072c7a68dSZhou Wang 	[HZIP_CURRENT_QM]   = "current_qm",
12172c7a68dSZhou Wang 	[HZIP_CLEAR_ENABLE] = "clear_enable",
12272c7a68dSZhou Wang };
12372c7a68dSZhou Wang 
12472c7a68dSZhou Wang struct ctrl_debug_file {
12572c7a68dSZhou Wang 	enum ctrl_debug_file_index index;
12672c7a68dSZhou Wang 	spinlock_t lock;
12772c7a68dSZhou Wang 	struct hisi_zip_ctrl *ctrl;
12872c7a68dSZhou Wang };
12972c7a68dSZhou Wang 
13062c455caSZhou Wang /*
13162c455caSZhou Wang  * One ZIP controller has one PF and multiple VFs, some global configurations
13262c455caSZhou Wang  * which PF has need this structure.
13362c455caSZhou Wang  *
13462c455caSZhou Wang  * Just relevant for PF.
13562c455caSZhou Wang  */
13662c455caSZhou Wang struct hisi_zip_ctrl {
13762c455caSZhou Wang 	struct hisi_zip *hisi_zip;
13872c7a68dSZhou Wang 	struct dentry *debug_root;
13972c7a68dSZhou Wang 	struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM];
14072c7a68dSZhou Wang };
14172c7a68dSZhou Wang 
14272c7a68dSZhou Wang enum {
14372c7a68dSZhou Wang 	HZIP_COMP_CORE0,
14472c7a68dSZhou Wang 	HZIP_COMP_CORE1,
14572c7a68dSZhou Wang 	HZIP_DECOMP_CORE0,
14672c7a68dSZhou Wang 	HZIP_DECOMP_CORE1,
14772c7a68dSZhou Wang 	HZIP_DECOMP_CORE2,
14872c7a68dSZhou Wang 	HZIP_DECOMP_CORE3,
14972c7a68dSZhou Wang 	HZIP_DECOMP_CORE4,
15072c7a68dSZhou Wang 	HZIP_DECOMP_CORE5,
15172c7a68dSZhou Wang };
15272c7a68dSZhou Wang 
15372c7a68dSZhou Wang static const u64 core_offsets[] = {
15472c7a68dSZhou Wang 	[HZIP_COMP_CORE0]   = 0x302000,
15572c7a68dSZhou Wang 	[HZIP_COMP_CORE1]   = 0x303000,
15672c7a68dSZhou Wang 	[HZIP_DECOMP_CORE0] = 0x304000,
15772c7a68dSZhou Wang 	[HZIP_DECOMP_CORE1] = 0x305000,
15872c7a68dSZhou Wang 	[HZIP_DECOMP_CORE2] = 0x306000,
15972c7a68dSZhou Wang 	[HZIP_DECOMP_CORE3] = 0x307000,
16072c7a68dSZhou Wang 	[HZIP_DECOMP_CORE4] = 0x308000,
16172c7a68dSZhou Wang 	[HZIP_DECOMP_CORE5] = 0x309000,
16272c7a68dSZhou Wang };
16372c7a68dSZhou Wang 
16472c7a68dSZhou Wang static struct debugfs_reg32 hzip_dfx_regs[] = {
16572c7a68dSZhou Wang 	{"HZIP_GET_BD_NUM                ",  0x00ull},
16672c7a68dSZhou Wang 	{"HZIP_GET_RIGHT_BD              ",  0x04ull},
16772c7a68dSZhou Wang 	{"HZIP_GET_ERROR_BD              ",  0x08ull},
16872c7a68dSZhou Wang 	{"HZIP_DONE_BD_NUM               ",  0x0cull},
16972c7a68dSZhou Wang 	{"HZIP_WORK_CYCLE                ",  0x10ull},
17072c7a68dSZhou Wang 	{"HZIP_IDLE_CYCLE                ",  0x18ull},
17172c7a68dSZhou Wang 	{"HZIP_MAX_DELAY                 ",  0x20ull},
17272c7a68dSZhou Wang 	{"HZIP_MIN_DELAY                 ",  0x24ull},
17372c7a68dSZhou Wang 	{"HZIP_AVG_DELAY                 ",  0x28ull},
17472c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_DATA          ",  0x30ull},
17572c7a68dSZhou Wang 	{"HZIP_MEM_VISIBLE_ADDR          ",  0x34ull},
17672c7a68dSZhou Wang 	{"HZIP_COMSUMED_BYTE             ",  0x38ull},
17772c7a68dSZhou Wang 	{"HZIP_PRODUCED_BYTE             ",  0x40ull},
17872c7a68dSZhou Wang 	{"HZIP_COMP_INF                  ",  0x70ull},
17972c7a68dSZhou Wang 	{"HZIP_PRE_OUT                   ",  0x78ull},
18072c7a68dSZhou Wang 	{"HZIP_BD_RD                     ",  0x7cull},
18172c7a68dSZhou Wang 	{"HZIP_BD_WR                     ",  0x80ull},
18272c7a68dSZhou Wang 	{"HZIP_GET_BD_AXI_ERR_NUM        ",  0x84ull},
18372c7a68dSZhou Wang 	{"HZIP_GET_BD_PARSE_ERR_NUM      ",  0x88ull},
18472c7a68dSZhou Wang 	{"HZIP_ADD_BD_AXI_ERR_NUM        ",  0x8cull},
18572c7a68dSZhou Wang 	{"HZIP_DECOMP_STF_RELOAD_CURR_ST ",  0x94ull},
18672c7a68dSZhou Wang 	{"HZIP_DECOMP_LZ77_CURR_ST       ",  0x9cull},
18762c455caSZhou Wang };
18862c455caSZhou Wang 
18962c455caSZhou Wang static int pf_q_num_set(const char *val, const struct kernel_param *kp)
19062c455caSZhou Wang {
19162c455caSZhou Wang 	struct pci_dev *pdev = pci_get_device(PCI_VENDOR_ID_HUAWEI,
19262c455caSZhou Wang 					      PCI_DEVICE_ID_ZIP_PF, NULL);
19362c455caSZhou Wang 	u32 n, q_num;
19462c455caSZhou Wang 	u8 rev_id;
19562c455caSZhou Wang 	int ret;
19662c455caSZhou Wang 
19762c455caSZhou Wang 	if (!val)
19862c455caSZhou Wang 		return -EINVAL;
19962c455caSZhou Wang 
20062c455caSZhou Wang 	if (!pdev) {
20162c455caSZhou Wang 		q_num = min_t(u32, HZIP_QUEUE_NUM_V1, HZIP_QUEUE_NUM_V2);
20262c455caSZhou Wang 		pr_info("No device found currently, suppose queue number is %d\n",
20362c455caSZhou Wang 			q_num);
20462c455caSZhou Wang 	} else {
20562c455caSZhou Wang 		rev_id = pdev->revision;
20662c455caSZhou Wang 		switch (rev_id) {
20762c455caSZhou Wang 		case QM_HW_V1:
20862c455caSZhou Wang 			q_num = HZIP_QUEUE_NUM_V1;
20962c455caSZhou Wang 			break;
21062c455caSZhou Wang 		case QM_HW_V2:
21162c455caSZhou Wang 			q_num = HZIP_QUEUE_NUM_V2;
21262c455caSZhou Wang 			break;
21362c455caSZhou Wang 		default:
21462c455caSZhou Wang 			return -EINVAL;
21562c455caSZhou Wang 		}
21662c455caSZhou Wang 	}
21762c455caSZhou Wang 
21862c455caSZhou Wang 	ret = kstrtou32(val, 10, &n);
21962c455caSZhou Wang 	if (ret != 0 || n > q_num || n == 0)
22062c455caSZhou Wang 		return -EINVAL;
22162c455caSZhou Wang 
22262c455caSZhou Wang 	return param_set_int(val, kp);
22362c455caSZhou Wang }
22462c455caSZhou Wang 
22562c455caSZhou Wang static const struct kernel_param_ops pf_q_num_ops = {
22662c455caSZhou Wang 	.set = pf_q_num_set,
22762c455caSZhou Wang 	.get = param_get_int,
22862c455caSZhou Wang };
22962c455caSZhou Wang 
23062c455caSZhou Wang static u32 pf_q_num = HZIP_PF_DEF_Q_NUM;
23162c455caSZhou Wang module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444);
23262c455caSZhou Wang MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 1-4096, v2 1-1024)");
23362c455caSZhou Wang 
23439977f4bSHao Fang static u32 vfs_num;
23539977f4bSHao Fang module_param(vfs_num, uint, 0444);
23639977f4bSHao Fang MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63)");
23739977f4bSHao Fang 
23862c455caSZhou Wang static const struct pci_device_id hisi_zip_dev_ids[] = {
23962c455caSZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_PF) },
24079e09f30SZhou Wang 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_VF) },
24162c455caSZhou Wang 	{ 0, }
24262c455caSZhou Wang };
24362c455caSZhou Wang MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids);
24462c455caSZhou Wang 
24518f1ab3fSShukun Tan int zip_create_qps(struct hisi_qp **qps, int qp_num)
24662c455caSZhou Wang {
24718f1ab3fSShukun Tan 	int node = cpu_to_node(smp_processor_id());
24862c455caSZhou Wang 
24918f1ab3fSShukun Tan 	return hisi_qm_alloc_qps_node(&zip_devices, qp_num, 0, node, qps);
25062c455caSZhou Wang }
25162c455caSZhou Wang 
25262c455caSZhou Wang static void hisi_zip_set_user_domain_and_cache(struct hisi_zip *hisi_zip)
25362c455caSZhou Wang {
25462c455caSZhou Wang 	void __iomem *base = hisi_zip->qm.io_base;
25562c455caSZhou Wang 
25662c455caSZhou Wang 	/* qm user domain */
25762c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1);
25862c455caSZhou Wang 	writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE);
25962c455caSZhou Wang 	writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1);
26062c455caSZhou Wang 	writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE);
26162c455caSZhou Wang 	writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE);
26262c455caSZhou Wang 
26362c455caSZhou Wang 	/* qm cache */
26462c455caSZhou Wang 	writel(AXI_M_CFG, base + QM_AXI_M_CFG);
26562c455caSZhou Wang 	writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE);
26662c455caSZhou Wang 	/* disable FLR triggered by BME(bus master enable) */
26762c455caSZhou Wang 	writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG);
26862c455caSZhou Wang 	writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE);
26962c455caSZhou Wang 
27062c455caSZhou Wang 	/* cache */
27162c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0);
27262c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1);
27362c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0);
27462c455caSZhou Wang 	writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1);
27562c455caSZhou Wang 
27662c455caSZhou Wang 	/* user domain configurations */
27762c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63);
27862c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63);
27962c455caSZhou Wang 	writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63);
2809e00df71SZhangfei Gao 
2819e00df71SZhangfei Gao 	if (hisi_zip->qm.use_sva) {
2829e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_RUSER_32_63);
2839e00df71SZhangfei Gao 		writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_WUSER_32_63);
2849e00df71SZhangfei Gao 	} else {
28562c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63);
28662c455caSZhou Wang 		writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63);
2879e00df71SZhangfei Gao 	}
28862c455caSZhou Wang 
28962c455caSZhou Wang 	/* let's open all compression/decompression cores */
29062c455caSZhou Wang 	writel(DECOMP_CHECK_ENABLE | ALL_COMP_DECOMP_EN,
29162c455caSZhou Wang 	       base + HZIP_CLOCK_GATE_CTRL);
29262c455caSZhou Wang 
29362c455caSZhou Wang 	/* enable sqc writeback */
29462c455caSZhou Wang 	writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE |
29562c455caSZhou Wang 	       CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) |
29662c455caSZhou Wang 	       FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL);
29762c455caSZhou Wang }
29862c455caSZhou Wang 
299eaebf4c3SShukun Tan static void hisi_zip_hw_error_enable(struct hisi_qm *qm)
30062c455caSZhou Wang {
30162c455caSZhou Wang 	if (qm->ver == QM_HW_V1) {
302eaebf4c3SShukun Tan 		writel(HZIP_CORE_INT_MASK_ALL,
303eaebf4c3SShukun Tan 		       qm->io_base + HZIP_CORE_INT_MASK_REG);
304ee1788c6SZhou Wang 		dev_info(&qm->pdev->dev, "Does not support hw error handle\n");
30562c455caSZhou Wang 		return;
30662c455caSZhou Wang 	}
30762c455caSZhou Wang 
30862c455caSZhou Wang 	/* clear ZIP hw error source if having */
309eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_SOURCE);
310eaebf4c3SShukun Tan 
311de3daf4bSShukun Tan 	/* configure error type */
312de3daf4bSShukun Tan 	writel(0x1, qm->io_base + HZIP_CORE_INT_RAS_CE_ENB);
313de3daf4bSShukun Tan 	writel(0x0, qm->io_base + HZIP_CORE_INT_RAS_FE_ENB);
314de3daf4bSShukun Tan 	writel(HZIP_CORE_INT_RAS_NFE_ENABLE,
315de3daf4bSShukun Tan 		qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB);
316de3daf4bSShukun Tan 
31762c455caSZhou Wang 	/* enable ZIP hw error interrupts */
318eaebf4c3SShukun Tan 	writel(0, qm->io_base + HZIP_CORE_INT_MASK_REG);
31962c455caSZhou Wang }
320eaebf4c3SShukun Tan 
321eaebf4c3SShukun Tan static void hisi_zip_hw_error_disable(struct hisi_qm *qm)
322eaebf4c3SShukun Tan {
323eaebf4c3SShukun Tan 	/* disable ZIP hw error interrupts */
324eaebf4c3SShukun Tan 	writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_MASK_REG);
32562c455caSZhou Wang }
32662c455caSZhou Wang 
32772c7a68dSZhou Wang static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file)
32872c7a68dSZhou Wang {
32972c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = file->ctrl->hisi_zip;
33072c7a68dSZhou Wang 
33172c7a68dSZhou Wang 	return &hisi_zip->qm;
33272c7a68dSZhou Wang }
33372c7a68dSZhou Wang 
33472c7a68dSZhou Wang static u32 current_qm_read(struct ctrl_debug_file *file)
33572c7a68dSZhou Wang {
33672c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
33772c7a68dSZhou Wang 
33872c7a68dSZhou Wang 	return readl(qm->io_base + QM_DFX_MB_CNT_VF);
33972c7a68dSZhou Wang }
34072c7a68dSZhou Wang 
34172c7a68dSZhou Wang static int current_qm_write(struct ctrl_debug_file *file, u32 val)
34272c7a68dSZhou Wang {
34372c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
34472c7a68dSZhou Wang 	u32 vfq_num;
34572c7a68dSZhou Wang 	u32 tmp;
34672c7a68dSZhou Wang 
347619e464aSShukun Tan 	if (val > qm->vfs_num)
34872c7a68dSZhou Wang 		return -EINVAL;
34972c7a68dSZhou Wang 
35072c7a68dSZhou Wang 	/* Calculate curr_qm_qp_num and store */
35172c7a68dSZhou Wang 	if (val == 0) {
35272c7a68dSZhou Wang 		qm->debug.curr_qm_qp_num = qm->qp_num;
35372c7a68dSZhou Wang 	} else {
354619e464aSShukun Tan 		vfq_num = (qm->ctrl_qp_num - qm->qp_num) / qm->vfs_num;
355619e464aSShukun Tan 		if (val == qm->vfs_num)
35672c7a68dSZhou Wang 			qm->debug.curr_qm_qp_num = qm->ctrl_qp_num -
357619e464aSShukun Tan 				qm->qp_num - (qm->vfs_num - 1) * vfq_num;
35872c7a68dSZhou Wang 		else
35972c7a68dSZhou Wang 			qm->debug.curr_qm_qp_num = vfq_num;
36072c7a68dSZhou Wang 	}
36172c7a68dSZhou Wang 
36272c7a68dSZhou Wang 	writel(val, qm->io_base + QM_DFX_MB_CNT_VF);
36372c7a68dSZhou Wang 	writel(val, qm->io_base + QM_DFX_DB_CNT_VF);
36472c7a68dSZhou Wang 
36572c7a68dSZhou Wang 	tmp = val |
36672c7a68dSZhou Wang 	      (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK);
36772c7a68dSZhou Wang 	writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
36872c7a68dSZhou Wang 
36972c7a68dSZhou Wang 	tmp = val |
37072c7a68dSZhou Wang 	      (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK);
37172c7a68dSZhou Wang 	writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
37272c7a68dSZhou Wang 
37372c7a68dSZhou Wang 	return  0;
37472c7a68dSZhou Wang }
37572c7a68dSZhou Wang 
37672c7a68dSZhou Wang static u32 clear_enable_read(struct ctrl_debug_file *file)
37772c7a68dSZhou Wang {
37872c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
37972c7a68dSZhou Wang 
38072c7a68dSZhou Wang 	return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
38172c7a68dSZhou Wang 	       SOFT_CTRL_CNT_CLR_CE_BIT;
38272c7a68dSZhou Wang }
38372c7a68dSZhou Wang 
38472c7a68dSZhou Wang static int clear_enable_write(struct ctrl_debug_file *file, u32 val)
38572c7a68dSZhou Wang {
38672c7a68dSZhou Wang 	struct hisi_qm *qm = file_to_qm(file);
38772c7a68dSZhou Wang 	u32 tmp;
38872c7a68dSZhou Wang 
38972c7a68dSZhou Wang 	if (val != 1 && val != 0)
39072c7a68dSZhou Wang 		return -EINVAL;
39172c7a68dSZhou Wang 
39272c7a68dSZhou Wang 	tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) &
39372c7a68dSZhou Wang 	       ~SOFT_CTRL_CNT_CLR_CE_BIT) | val;
39472c7a68dSZhou Wang 	writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
39572c7a68dSZhou Wang 
39672c7a68dSZhou Wang 	return  0;
39772c7a68dSZhou Wang }
39872c7a68dSZhou Wang 
39972c7a68dSZhou Wang static ssize_t ctrl_debug_read(struct file *filp, char __user *buf,
40072c7a68dSZhou Wang 			       size_t count, loff_t *pos)
40172c7a68dSZhou Wang {
40272c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
40372c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
40472c7a68dSZhou Wang 	u32 val;
40572c7a68dSZhou Wang 	int ret;
40672c7a68dSZhou Wang 
40772c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
40872c7a68dSZhou Wang 	switch (file->index) {
40972c7a68dSZhou Wang 	case HZIP_CURRENT_QM:
41072c7a68dSZhou Wang 		val = current_qm_read(file);
41172c7a68dSZhou Wang 		break;
41272c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
41372c7a68dSZhou Wang 		val = clear_enable_read(file);
41472c7a68dSZhou Wang 		break;
41572c7a68dSZhou Wang 	default:
41672c7a68dSZhou Wang 		spin_unlock_irq(&file->lock);
41772c7a68dSZhou Wang 		return -EINVAL;
41872c7a68dSZhou Wang 	}
41972c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
42072c7a68dSZhou Wang 	ret = sprintf(tbuf, "%u\n", val);
42172c7a68dSZhou Wang 	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
42272c7a68dSZhou Wang }
42372c7a68dSZhou Wang 
42472c7a68dSZhou Wang static ssize_t ctrl_debug_write(struct file *filp, const char __user *buf,
42572c7a68dSZhou Wang 				size_t count, loff_t *pos)
42672c7a68dSZhou Wang {
42772c7a68dSZhou Wang 	struct ctrl_debug_file *file = filp->private_data;
42872c7a68dSZhou Wang 	char tbuf[HZIP_BUF_SIZE];
42972c7a68dSZhou Wang 	unsigned long val;
43072c7a68dSZhou Wang 	int len, ret;
43172c7a68dSZhou Wang 
43272c7a68dSZhou Wang 	if (*pos != 0)
43372c7a68dSZhou Wang 		return 0;
43472c7a68dSZhou Wang 
43572c7a68dSZhou Wang 	if (count >= HZIP_BUF_SIZE)
43672c7a68dSZhou Wang 		return -ENOSPC;
43772c7a68dSZhou Wang 
43872c7a68dSZhou Wang 	len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count);
43972c7a68dSZhou Wang 	if (len < 0)
44072c7a68dSZhou Wang 		return len;
44172c7a68dSZhou Wang 
44272c7a68dSZhou Wang 	tbuf[len] = '\0';
44372c7a68dSZhou Wang 	if (kstrtoul(tbuf, 0, &val))
44472c7a68dSZhou Wang 		return -EFAULT;
44572c7a68dSZhou Wang 
44672c7a68dSZhou Wang 	spin_lock_irq(&file->lock);
44772c7a68dSZhou Wang 	switch (file->index) {
44872c7a68dSZhou Wang 	case HZIP_CURRENT_QM:
44972c7a68dSZhou Wang 		ret = current_qm_write(file, val);
45072c7a68dSZhou Wang 		if (ret)
45172c7a68dSZhou Wang 			goto err_input;
45272c7a68dSZhou Wang 		break;
45372c7a68dSZhou Wang 	case HZIP_CLEAR_ENABLE:
45472c7a68dSZhou Wang 		ret = clear_enable_write(file, val);
45572c7a68dSZhou Wang 		if (ret)
45672c7a68dSZhou Wang 			goto err_input;
45772c7a68dSZhou Wang 		break;
45872c7a68dSZhou Wang 	default:
45972c7a68dSZhou Wang 		ret = -EINVAL;
46072c7a68dSZhou Wang 		goto err_input;
46172c7a68dSZhou Wang 	}
46272c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
46372c7a68dSZhou Wang 
46472c7a68dSZhou Wang 	return count;
46572c7a68dSZhou Wang 
46672c7a68dSZhou Wang err_input:
46772c7a68dSZhou Wang 	spin_unlock_irq(&file->lock);
46872c7a68dSZhou Wang 	return ret;
46972c7a68dSZhou Wang }
47072c7a68dSZhou Wang 
47172c7a68dSZhou Wang static const struct file_operations ctrl_debug_fops = {
47272c7a68dSZhou Wang 	.owner = THIS_MODULE,
47372c7a68dSZhou Wang 	.open = simple_open,
47472c7a68dSZhou Wang 	.read = ctrl_debug_read,
47572c7a68dSZhou Wang 	.write = ctrl_debug_write,
47672c7a68dSZhou Wang };
47772c7a68dSZhou Wang 
47872c7a68dSZhou Wang static int hisi_zip_core_debug_init(struct hisi_zip_ctrl *ctrl)
47972c7a68dSZhou Wang {
48072c7a68dSZhou Wang 	struct hisi_zip *hisi_zip = ctrl->hisi_zip;
48172c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
48272c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
48372c7a68dSZhou Wang 	struct debugfs_regset32 *regset;
4844a97bfc7SGreg Kroah-Hartman 	struct dentry *tmp_d;
48572c7a68dSZhou Wang 	char buf[HZIP_BUF_SIZE];
48672c7a68dSZhou Wang 	int i;
48772c7a68dSZhou Wang 
48872c7a68dSZhou Wang 	for (i = 0; i < HZIP_CORE_NUM; i++) {
48972c7a68dSZhou Wang 		if (i < HZIP_COMP_CORE_NUM)
49072c7a68dSZhou Wang 			sprintf(buf, "comp_core%d", i);
49172c7a68dSZhou Wang 		else
49272c7a68dSZhou Wang 			sprintf(buf, "decomp_core%d", i - HZIP_COMP_CORE_NUM);
49372c7a68dSZhou Wang 
49472c7a68dSZhou Wang 		regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
49572c7a68dSZhou Wang 		if (!regset)
49672c7a68dSZhou Wang 			return -ENOENT;
49772c7a68dSZhou Wang 
49872c7a68dSZhou Wang 		regset->regs = hzip_dfx_regs;
49972c7a68dSZhou Wang 		regset->nregs = ARRAY_SIZE(hzip_dfx_regs);
50072c7a68dSZhou Wang 		regset->base = qm->io_base + core_offsets[i];
50172c7a68dSZhou Wang 
5024a97bfc7SGreg Kroah-Hartman 		tmp_d = debugfs_create_dir(buf, ctrl->debug_root);
5034a97bfc7SGreg Kroah-Hartman 		debugfs_create_regset32("regs", 0444, tmp_d, regset);
50472c7a68dSZhou Wang 	}
50572c7a68dSZhou Wang 
50672c7a68dSZhou Wang 	return 0;
50772c7a68dSZhou Wang }
50872c7a68dSZhou Wang 
50972c7a68dSZhou Wang static int hisi_zip_ctrl_debug_init(struct hisi_zip_ctrl *ctrl)
51072c7a68dSZhou Wang {
51172c7a68dSZhou Wang 	int i;
51272c7a68dSZhou Wang 
51372c7a68dSZhou Wang 	for (i = HZIP_CURRENT_QM; i < HZIP_DEBUG_FILE_NUM; i++) {
51472c7a68dSZhou Wang 		spin_lock_init(&ctrl->files[i].lock);
51572c7a68dSZhou Wang 		ctrl->files[i].ctrl = ctrl;
51672c7a68dSZhou Wang 		ctrl->files[i].index = i;
51772c7a68dSZhou Wang 
5184a97bfc7SGreg Kroah-Hartman 		debugfs_create_file(ctrl_debug_file_name[i], 0600,
51972c7a68dSZhou Wang 				    ctrl->debug_root, ctrl->files + i,
52072c7a68dSZhou Wang 				    &ctrl_debug_fops);
52172c7a68dSZhou Wang 	}
52272c7a68dSZhou Wang 
52372c7a68dSZhou Wang 	return hisi_zip_core_debug_init(ctrl);
52472c7a68dSZhou Wang }
52572c7a68dSZhou Wang 
52672c7a68dSZhou Wang static int hisi_zip_debugfs_init(struct hisi_zip *hisi_zip)
52772c7a68dSZhou Wang {
52872c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
52972c7a68dSZhou Wang 	struct device *dev = &qm->pdev->dev;
53072c7a68dSZhou Wang 	struct dentry *dev_d;
53172c7a68dSZhou Wang 	int ret;
53272c7a68dSZhou Wang 
53372c7a68dSZhou Wang 	dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root);
53472c7a68dSZhou Wang 
53572c7a68dSZhou Wang 	qm->debug.debug_root = dev_d;
53672c7a68dSZhou Wang 	ret = hisi_qm_debug_init(qm);
53772c7a68dSZhou Wang 	if (ret)
53872c7a68dSZhou Wang 		goto failed_to_create;
53972c7a68dSZhou Wang 
54072c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF) {
54172c7a68dSZhou Wang 		hisi_zip->ctrl->debug_root = dev_d;
54272c7a68dSZhou Wang 		ret = hisi_zip_ctrl_debug_init(hisi_zip->ctrl);
54372c7a68dSZhou Wang 		if (ret)
54472c7a68dSZhou Wang 			goto failed_to_create;
54572c7a68dSZhou Wang 	}
54672c7a68dSZhou Wang 
54772c7a68dSZhou Wang 	return 0;
54872c7a68dSZhou Wang 
54972c7a68dSZhou Wang failed_to_create:
55072c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
55172c7a68dSZhou Wang 	return ret;
55272c7a68dSZhou Wang }
55372c7a68dSZhou Wang 
55472c7a68dSZhou Wang static void hisi_zip_debug_regs_clear(struct hisi_zip *hisi_zip)
55572c7a68dSZhou Wang {
55672c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
55772c7a68dSZhou Wang 
55872c7a68dSZhou Wang 	writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF);
55972c7a68dSZhou Wang 	writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF);
56072c7a68dSZhou Wang 	writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE);
56172c7a68dSZhou Wang 
56272c7a68dSZhou Wang 	hisi_qm_debug_regs_clear(qm);
56372c7a68dSZhou Wang }
56472c7a68dSZhou Wang 
56572c7a68dSZhou Wang static void hisi_zip_debugfs_exit(struct hisi_zip *hisi_zip)
56672c7a68dSZhou Wang {
56772c7a68dSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
56872c7a68dSZhou Wang 
56972c7a68dSZhou Wang 	debugfs_remove_recursive(qm->debug.debug_root);
57072c7a68dSZhou Wang 
57172c7a68dSZhou Wang 	if (qm->fun_type == QM_HW_PF)
57272c7a68dSZhou Wang 		hisi_zip_debug_regs_clear(hisi_zip);
57372c7a68dSZhou Wang }
57472c7a68dSZhou Wang 
575f826e6efSShukun Tan static void hisi_zip_log_hw_error(struct hisi_qm *qm, u32 err_sts)
576f826e6efSShukun Tan {
577f826e6efSShukun Tan 	const struct hisi_zip_hw_error *err = zip_hw_error;
578f826e6efSShukun Tan 	struct device *dev = &qm->pdev->dev;
579f826e6efSShukun Tan 	u32 err_val;
580f826e6efSShukun Tan 
581f826e6efSShukun Tan 	while (err->msg) {
582f826e6efSShukun Tan 		if (err->int_msk & err_sts) {
583f826e6efSShukun Tan 			dev_err(dev, "%s [error status=0x%x] found\n",
584f826e6efSShukun Tan 				 err->msg, err->int_msk);
585f826e6efSShukun Tan 
586f826e6efSShukun Tan 			if (err->int_msk & HZIP_CORE_INT_STATUS_M_ECC) {
587f826e6efSShukun Tan 				err_val = readl(qm->io_base +
588f826e6efSShukun Tan 						HZIP_CORE_SRAM_ECC_ERR_INFO);
589f826e6efSShukun Tan 				dev_err(dev, "hisi-zip multi ecc sram num=0x%x\n",
590f826e6efSShukun Tan 					((err_val >>
591f826e6efSShukun Tan 					HZIP_SRAM_ECC_ERR_NUM_SHIFT) & 0xFF));
592f826e6efSShukun Tan 				dev_err(dev, "hisi-zip multi ecc sram addr=0x%x\n",
593f826e6efSShukun Tan 					(err_val >>
594f826e6efSShukun Tan 					HZIP_SRAM_ECC_ERR_ADDR_SHIFT));
595f826e6efSShukun Tan 			}
596f826e6efSShukun Tan 		}
597f826e6efSShukun Tan 		err++;
598f826e6efSShukun Tan 	}
599f826e6efSShukun Tan 
600f826e6efSShukun Tan 	writel(err_sts, qm->io_base + HZIP_CORE_INT_SOURCE);
601f826e6efSShukun Tan }
602f826e6efSShukun Tan 
603f826e6efSShukun Tan static u32 hisi_zip_get_hw_err_status(struct hisi_qm *qm)
604f826e6efSShukun Tan {
605f826e6efSShukun Tan 	return readl(qm->io_base + HZIP_CORE_INT_STATUS);
606f826e6efSShukun Tan }
607f826e6efSShukun Tan 
608eaebf4c3SShukun Tan static const struct hisi_qm_err_ini hisi_zip_err_ini = {
609eaebf4c3SShukun Tan 	.hw_err_enable		= hisi_zip_hw_error_enable,
610eaebf4c3SShukun Tan 	.hw_err_disable		= hisi_zip_hw_error_disable,
611f826e6efSShukun Tan 	.get_dev_hw_err_status	= hisi_zip_get_hw_err_status,
612f826e6efSShukun Tan 	.log_dev_hw_err		= hisi_zip_log_hw_error,
613eaebf4c3SShukun Tan 	.err_info		= {
614eaebf4c3SShukun Tan 		.ce			= QM_BASE_CE,
615f826e6efSShukun Tan 		.nfe			= QM_BASE_NFE |
616f826e6efSShukun Tan 					  QM_ACC_WB_NOT_READY_TIMEOUT,
617eaebf4c3SShukun Tan 		.fe			= 0,
618eaebf4c3SShukun Tan 		.msi			= QM_DB_RANDOM_INVALID,
61962c455caSZhou Wang 	}
620eaebf4c3SShukun Tan };
62162c455caSZhou Wang 
62262c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip)
62362c455caSZhou Wang {
62462c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
62562c455caSZhou Wang 	struct hisi_zip_ctrl *ctrl;
62662c455caSZhou Wang 
62762c455caSZhou Wang 	ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL);
62862c455caSZhou Wang 	if (!ctrl)
62962c455caSZhou Wang 		return -ENOMEM;
63062c455caSZhou Wang 
63162c455caSZhou Wang 	hisi_zip->ctrl = ctrl;
63262c455caSZhou Wang 	ctrl->hisi_zip = hisi_zip;
63362c455caSZhou Wang 
63462c455caSZhou Wang 	switch (qm->ver) {
63562c455caSZhou Wang 	case QM_HW_V1:
63662c455caSZhou Wang 		qm->ctrl_qp_num = HZIP_QUEUE_NUM_V1;
63762c455caSZhou Wang 		break;
63862c455caSZhou Wang 
63962c455caSZhou Wang 	case QM_HW_V2:
64062c455caSZhou Wang 		qm->ctrl_qp_num = HZIP_QUEUE_NUM_V2;
64162c455caSZhou Wang 		break;
64262c455caSZhou Wang 
64362c455caSZhou Wang 	default:
64462c455caSZhou Wang 		return -EINVAL;
64562c455caSZhou Wang 	}
64662c455caSZhou Wang 
647eaebf4c3SShukun Tan 	qm->err_ini = &hisi_zip_err_ini;
648eaebf4c3SShukun Tan 
64962c455caSZhou Wang 	hisi_zip_set_user_domain_and_cache(hisi_zip);
650eaebf4c3SShukun Tan 	hisi_qm_dev_err_init(qm);
65172c7a68dSZhou Wang 	hisi_zip_debug_regs_clear(hisi_zip);
65262c455caSZhou Wang 
65362c455caSZhou Wang 	return 0;
65462c455caSZhou Wang }
65562c455caSZhou Wang 
65639977f4bSHao Fang static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id)
65739977f4bSHao Fang {
65839977f4bSHao Fang 	struct hisi_zip *hisi_zip;
65939977f4bSHao Fang 	enum qm_hw_ver rev_id;
66039977f4bSHao Fang 	struct hisi_qm *qm;
66139977f4bSHao Fang 	int ret;
66239977f4bSHao Fang 
66339977f4bSHao Fang 	rev_id = hisi_qm_get_hw_version(pdev);
66439977f4bSHao Fang 	if (rev_id == QM_HW_UNKNOWN)
66539977f4bSHao Fang 		return -EINVAL;
66639977f4bSHao Fang 
66739977f4bSHao Fang 	hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL);
66839977f4bSHao Fang 	if (!hisi_zip)
66939977f4bSHao Fang 		return -ENOMEM;
67039977f4bSHao Fang 	pci_set_drvdata(pdev, hisi_zip);
67139977f4bSHao Fang 
67239977f4bSHao Fang 	qm = &hisi_zip->qm;
67318bead70SZhangfei Gao 	qm->use_dma_api = true;
67439977f4bSHao Fang 	qm->pdev = pdev;
67539977f4bSHao Fang 	qm->ver = rev_id;
67639977f4bSHao Fang 
6779e00df71SZhangfei Gao 	qm->algs = "zlib\ngzip";
67839977f4bSHao Fang 	qm->sqe_size = HZIP_SQE_SIZE;
67939977f4bSHao Fang 	qm->dev_name = hisi_zip_name;
68039977f4bSHao Fang 	qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ? QM_HW_PF :
68139977f4bSHao Fang 								QM_HW_VF;
68239977f4bSHao Fang 	ret = hisi_qm_init(qm);
68339977f4bSHao Fang 	if (ret) {
68439977f4bSHao Fang 		dev_err(&pdev->dev, "Failed to init qm!\n");
68539977f4bSHao Fang 		return ret;
68639977f4bSHao Fang 	}
68739977f4bSHao Fang 
68839977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF) {
68939977f4bSHao Fang 		ret = hisi_zip_pf_probe_init(hisi_zip);
69039977f4bSHao Fang 		if (ret)
69139977f4bSHao Fang 			return ret;
69239977f4bSHao Fang 
69339977f4bSHao Fang 		qm->qp_base = HZIP_PF_DEF_Q_BASE;
69439977f4bSHao Fang 		qm->qp_num = pf_q_num;
69539977f4bSHao Fang 	} else if (qm->fun_type == QM_HW_VF) {
69639977f4bSHao Fang 		/*
69739977f4bSHao Fang 		 * have no way to get qm configure in VM in v1 hardware,
69839977f4bSHao Fang 		 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force
69939977f4bSHao Fang 		 * to trigger only one VF in v1 hardware.
70039977f4bSHao Fang 		 *
70139977f4bSHao Fang 		 * v2 hardware has no such problem.
70239977f4bSHao Fang 		 */
70339977f4bSHao Fang 		if (qm->ver == QM_HW_V1) {
70439977f4bSHao Fang 			qm->qp_base = HZIP_PF_DEF_Q_NUM;
70539977f4bSHao Fang 			qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM;
70639977f4bSHao Fang 		} else if (qm->ver == QM_HW_V2)
70739977f4bSHao Fang 			/* v2 starts to support get vft by mailbox */
70839977f4bSHao Fang 			hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num);
70939977f4bSHao Fang 	}
71039977f4bSHao Fang 
71139977f4bSHao Fang 	ret = hisi_qm_start(qm);
71239977f4bSHao Fang 	if (ret)
71339977f4bSHao Fang 		goto err_qm_uninit;
71439977f4bSHao Fang 
71539977f4bSHao Fang 	ret = hisi_zip_debugfs_init(hisi_zip);
71639977f4bSHao Fang 	if (ret)
71739977f4bSHao Fang 		dev_err(&pdev->dev, "Failed to init debugfs (%d)!\n", ret);
71839977f4bSHao Fang 
71918f1ab3fSShukun Tan 	hisi_qm_add_to_list(qm, &zip_devices);
72039977f4bSHao Fang 
7219e00df71SZhangfei Gao 	if (qm->uacce) {
7229e00df71SZhangfei Gao 		ret = uacce_register(qm->uacce);
7239e00df71SZhangfei Gao 		if (ret)
7249e00df71SZhangfei Gao 			goto err_qm_uninit;
7259e00df71SZhangfei Gao 	}
7269e00df71SZhangfei Gao 
72739977f4bSHao Fang 	if (qm->fun_type == QM_HW_PF && vfs_num > 0) {
728*cd1b7ae3SShukun Tan 		ret = hisi_qm_sriov_enable(pdev, vfs_num);
72939977f4bSHao Fang 		if (ret < 0)
73039977f4bSHao Fang 			goto err_remove_from_list;
73139977f4bSHao Fang 	}
73239977f4bSHao Fang 
73339977f4bSHao Fang 	return 0;
73439977f4bSHao Fang 
73539977f4bSHao Fang err_remove_from_list:
73618f1ab3fSShukun Tan 	hisi_qm_del_from_list(qm, &zip_devices);
73739977f4bSHao Fang 	hisi_zip_debugfs_exit(hisi_zip);
73839977f4bSHao Fang 	hisi_qm_stop(qm);
73939977f4bSHao Fang err_qm_uninit:
74039977f4bSHao Fang 	hisi_qm_uninit(qm);
74139977f4bSHao Fang 	return ret;
74239977f4bSHao Fang }
74339977f4bSHao Fang 
74462c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev)
74562c455caSZhou Wang {
74662c455caSZhou Wang 	struct hisi_zip *hisi_zip = pci_get_drvdata(pdev);
74762c455caSZhou Wang 	struct hisi_qm *qm = &hisi_zip->qm;
74862c455caSZhou Wang 
749619e464aSShukun Tan 	if (qm->fun_type == QM_HW_PF && qm->vfs_num)
750*cd1b7ae3SShukun Tan 		hisi_qm_sriov_disable(pdev);
75179e09f30SZhou Wang 
75272c7a68dSZhou Wang 	hisi_zip_debugfs_exit(hisi_zip);
75362c455caSZhou Wang 	hisi_qm_stop(qm);
75479e09f30SZhou Wang 
755eaebf4c3SShukun Tan 	hisi_qm_dev_err_uninit(qm);
75662c455caSZhou Wang 	hisi_qm_uninit(qm);
75718f1ab3fSShukun Tan 	hisi_qm_del_from_list(qm, &zip_devices);
75862c455caSZhou Wang }
75962c455caSZhou Wang 
76062c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = {
761f826e6efSShukun Tan 	.error_detected	= hisi_qm_dev_err_detected,
76262c455caSZhou Wang };
76362c455caSZhou Wang 
76462c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = {
76562c455caSZhou Wang 	.name			= "hisi_zip",
76662c455caSZhou Wang 	.id_table		= hisi_zip_dev_ids,
76762c455caSZhou Wang 	.probe			= hisi_zip_probe,
76862c455caSZhou Wang 	.remove			= hisi_zip_remove,
769bf6a7a5aSArnd Bergmann 	.sriov_configure	= IS_ENABLED(CONFIG_PCI_IOV) ?
770*cd1b7ae3SShukun Tan 					hisi_qm_sriov_configure : NULL,
77162c455caSZhou Wang 	.err_handler		= &hisi_zip_err_handler,
77262c455caSZhou Wang };
77362c455caSZhou Wang 
77472c7a68dSZhou Wang static void hisi_zip_register_debugfs(void)
77572c7a68dSZhou Wang {
77672c7a68dSZhou Wang 	if (!debugfs_initialized())
77772c7a68dSZhou Wang 		return;
77872c7a68dSZhou Wang 
77972c7a68dSZhou Wang 	hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL);
78072c7a68dSZhou Wang }
78172c7a68dSZhou Wang 
78272c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void)
78372c7a68dSZhou Wang {
78472c7a68dSZhou Wang 	debugfs_remove_recursive(hzip_debugfs_root);
78572c7a68dSZhou Wang }
78672c7a68dSZhou Wang 
78762c455caSZhou Wang static int __init hisi_zip_init(void)
78862c455caSZhou Wang {
78962c455caSZhou Wang 	int ret;
79062c455caSZhou Wang 
79118f1ab3fSShukun Tan 	hisi_qm_init_list(&zip_devices);
79272c7a68dSZhou Wang 	hisi_zip_register_debugfs();
79372c7a68dSZhou Wang 
79462c455caSZhou Wang 	ret = pci_register_driver(&hisi_zip_pci_driver);
79562c455caSZhou Wang 	if (ret < 0) {
79662c455caSZhou Wang 		pr_err("Failed to register pci driver.\n");
79772c7a68dSZhou Wang 		goto err_pci;
79862c455caSZhou Wang 	}
79962c455caSZhou Wang 
80062c455caSZhou Wang 	ret = hisi_zip_register_to_crypto();
80162c455caSZhou Wang 	if (ret < 0) {
80262c455caSZhou Wang 		pr_err("Failed to register driver to crypto.\n");
80362c455caSZhou Wang 		goto err_crypto;
80462c455caSZhou Wang 	}
80562c455caSZhou Wang 
80662c455caSZhou Wang 	return 0;
80762c455caSZhou Wang 
80862c455caSZhou Wang err_crypto:
80962c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
81072c7a68dSZhou Wang err_pci:
81172c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
81272c7a68dSZhou Wang 
81362c455caSZhou Wang 	return ret;
81462c455caSZhou Wang }
81562c455caSZhou Wang 
81662c455caSZhou Wang static void __exit hisi_zip_exit(void)
81762c455caSZhou Wang {
81862c455caSZhou Wang 	hisi_zip_unregister_from_crypto();
81962c455caSZhou Wang 	pci_unregister_driver(&hisi_zip_pci_driver);
82072c7a68dSZhou Wang 	hisi_zip_unregister_debugfs();
82162c455caSZhou Wang }
82262c455caSZhou Wang 
82362c455caSZhou Wang module_init(hisi_zip_init);
82462c455caSZhou Wang module_exit(hisi_zip_exit);
82562c455caSZhou Wang 
82662c455caSZhou Wang MODULE_LICENSE("GPL v2");
82762c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
82862c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator");
829