1939ccd10SJijie Shao // SPDX-License-Identifier: GPL-2.0+
2939ccd10SJijie Shao // Copyright (c) 2023 Hisilicon Limited.
3939ccd10SJijie Shao
4939ccd10SJijie Shao #include "hclgevf_main.h"
5939ccd10SJijie Shao #include "hclgevf_regs.h"
6939ccd10SJijie Shao #include "hnae3.h"
7939ccd10SJijie Shao
8939ccd10SJijie Shao static const u32 cmdq_reg_addr_list[] = {HCLGE_COMM_NIC_CSQ_BASEADDR_L_REG,
9939ccd10SJijie Shao HCLGE_COMM_NIC_CSQ_BASEADDR_H_REG,
10939ccd10SJijie Shao HCLGE_COMM_NIC_CSQ_DEPTH_REG,
11939ccd10SJijie Shao HCLGE_COMM_NIC_CSQ_TAIL_REG,
12939ccd10SJijie Shao HCLGE_COMM_NIC_CSQ_HEAD_REG,
13939ccd10SJijie Shao HCLGE_COMM_NIC_CRQ_BASEADDR_L_REG,
14939ccd10SJijie Shao HCLGE_COMM_NIC_CRQ_BASEADDR_H_REG,
15939ccd10SJijie Shao HCLGE_COMM_NIC_CRQ_DEPTH_REG,
16939ccd10SJijie Shao HCLGE_COMM_NIC_CRQ_TAIL_REG,
17939ccd10SJijie Shao HCLGE_COMM_NIC_CRQ_HEAD_REG,
18939ccd10SJijie Shao HCLGE_COMM_VECTOR0_CMDQ_SRC_REG,
19939ccd10SJijie Shao HCLGE_COMM_VECTOR0_CMDQ_STATE_REG,
20939ccd10SJijie Shao HCLGE_COMM_CMDQ_INTR_EN_REG,
21939ccd10SJijie Shao HCLGE_COMM_CMDQ_INTR_GEN_REG};
22939ccd10SJijie Shao
23939ccd10SJijie Shao static const u32 common_reg_addr_list[] = {HCLGEVF_MISC_VECTOR_REG_BASE,
24939ccd10SJijie Shao HCLGEVF_RST_ING,
25939ccd10SJijie Shao HCLGEVF_GRO_EN_REG};
26939ccd10SJijie Shao
27939ccd10SJijie Shao static const u32 ring_reg_addr_list[] = {HCLGEVF_RING_RX_ADDR_L_REG,
28939ccd10SJijie Shao HCLGEVF_RING_RX_ADDR_H_REG,
29939ccd10SJijie Shao HCLGEVF_RING_RX_BD_NUM_REG,
30939ccd10SJijie Shao HCLGEVF_RING_RX_BD_LENGTH_REG,
31939ccd10SJijie Shao HCLGEVF_RING_RX_MERGE_EN_REG,
32939ccd10SJijie Shao HCLGEVF_RING_RX_TAIL_REG,
33939ccd10SJijie Shao HCLGEVF_RING_RX_HEAD_REG,
34939ccd10SJijie Shao HCLGEVF_RING_RX_FBD_NUM_REG,
35939ccd10SJijie Shao HCLGEVF_RING_RX_OFFSET_REG,
36939ccd10SJijie Shao HCLGEVF_RING_RX_FBD_OFFSET_REG,
37939ccd10SJijie Shao HCLGEVF_RING_RX_STASH_REG,
38939ccd10SJijie Shao HCLGEVF_RING_RX_BD_ERR_REG,
39939ccd10SJijie Shao HCLGEVF_RING_TX_ADDR_L_REG,
40939ccd10SJijie Shao HCLGEVF_RING_TX_ADDR_H_REG,
41939ccd10SJijie Shao HCLGEVF_RING_TX_BD_NUM_REG,
42939ccd10SJijie Shao HCLGEVF_RING_TX_PRIORITY_REG,
43939ccd10SJijie Shao HCLGEVF_RING_TX_TC_REG,
44939ccd10SJijie Shao HCLGEVF_RING_TX_MERGE_EN_REG,
45939ccd10SJijie Shao HCLGEVF_RING_TX_TAIL_REG,
46939ccd10SJijie Shao HCLGEVF_RING_TX_HEAD_REG,
47939ccd10SJijie Shao HCLGEVF_RING_TX_FBD_NUM_REG,
48939ccd10SJijie Shao HCLGEVF_RING_TX_OFFSET_REG,
49939ccd10SJijie Shao HCLGEVF_RING_TX_EBD_NUM_REG,
50939ccd10SJijie Shao HCLGEVF_RING_TX_EBD_OFFSET_REG,
51939ccd10SJijie Shao HCLGEVF_RING_TX_BD_ERR_REG,
52939ccd10SJijie Shao HCLGEVF_RING_EN_REG};
53939ccd10SJijie Shao
54939ccd10SJijie Shao static const u32 tqp_intr_reg_addr_list[] = {HCLGEVF_TQP_INTR_CTRL_REG,
55939ccd10SJijie Shao HCLGEVF_TQP_INTR_GL0_REG,
56939ccd10SJijie Shao HCLGEVF_TQP_INTR_GL1_REG,
57939ccd10SJijie Shao HCLGEVF_TQP_INTR_GL2_REG,
58939ccd10SJijie Shao HCLGEVF_TQP_INTR_RL_REG};
59939ccd10SJijie Shao
603ef5d70bSJijie Shao enum hclgevf_reg_tag {
613ef5d70bSJijie Shao HCLGEVF_REG_TAG_CMDQ = 0,
623ef5d70bSJijie Shao HCLGEVF_REG_TAG_COMMON,
633ef5d70bSJijie Shao HCLGEVF_REG_TAG_RING,
643ef5d70bSJijie Shao HCLGEVF_REG_TAG_TQP_INTR,
653ef5d70bSJijie Shao };
663ef5d70bSJijie Shao
673ef5d70bSJijie Shao #pragma pack(4)
683ef5d70bSJijie Shao struct hclgevf_reg_tlv {
693ef5d70bSJijie Shao u16 tag;
703ef5d70bSJijie Shao u16 len;
713ef5d70bSJijie Shao };
723ef5d70bSJijie Shao
733ef5d70bSJijie Shao struct hclgevf_reg_header {
743ef5d70bSJijie Shao u64 magic_number;
753ef5d70bSJijie Shao u8 is_vf;
763ef5d70bSJijie Shao u8 rsv[7];
773ef5d70bSJijie Shao };
783ef5d70bSJijie Shao
793ef5d70bSJijie Shao #pragma pack()
803ef5d70bSJijie Shao
813ef5d70bSJijie Shao #define HCLGEVF_REG_TLV_SIZE sizeof(struct hclgevf_reg_tlv)
823ef5d70bSJijie Shao #define HCLGEVF_REG_HEADER_SIZE sizeof(struct hclgevf_reg_header)
833ef5d70bSJijie Shao #define HCLGEVF_REG_TLV_SPACE (sizeof(struct hclgevf_reg_tlv) / sizeof(u32))
843ef5d70bSJijie Shao #define HCLGEVF_REG_HEADER_SPACE (sizeof(struct hclgevf_reg_header) / sizeof(u32))
853ef5d70bSJijie Shao #define HCLGEVF_REG_MAGIC_NUMBER 0x686e733372656773 /* meaning is hns3regs */
863ef5d70bSJijie Shao
hclgevf_reg_get_header(void * data)873ef5d70bSJijie Shao static u32 hclgevf_reg_get_header(void *data)
883ef5d70bSJijie Shao {
893ef5d70bSJijie Shao struct hclgevf_reg_header *header = data;
903ef5d70bSJijie Shao
913ef5d70bSJijie Shao header->magic_number = HCLGEVF_REG_MAGIC_NUMBER;
923ef5d70bSJijie Shao header->is_vf = 0x1;
933ef5d70bSJijie Shao
943ef5d70bSJijie Shao return HCLGEVF_REG_HEADER_SPACE;
953ef5d70bSJijie Shao }
963ef5d70bSJijie Shao
hclgevf_reg_get_tlv(u32 tag,u32 regs_num,void * data)973ef5d70bSJijie Shao static u32 hclgevf_reg_get_tlv(u32 tag, u32 regs_num, void *data)
983ef5d70bSJijie Shao {
993ef5d70bSJijie Shao struct hclgevf_reg_tlv *tlv = data;
1003ef5d70bSJijie Shao
1013ef5d70bSJijie Shao tlv->tag = tag;
1023ef5d70bSJijie Shao tlv->len = regs_num * sizeof(u32) + HCLGEVF_REG_TLV_SIZE;
1033ef5d70bSJijie Shao
1043ef5d70bSJijie Shao return HCLGEVF_REG_TLV_SPACE;
1053ef5d70bSJijie Shao }
106939ccd10SJijie Shao
hclgevf_get_regs_len(struct hnae3_handle * handle)107939ccd10SJijie Shao int hclgevf_get_regs_len(struct hnae3_handle *handle)
108939ccd10SJijie Shao {
109939ccd10SJijie Shao struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1103ef5d70bSJijie Shao int cmdq_len, common_len, ring_len, tqp_intr_len;
111939ccd10SJijie Shao
1123ef5d70bSJijie Shao cmdq_len = HCLGEVF_REG_TLV_SIZE + sizeof(cmdq_reg_addr_list);
1133ef5d70bSJijie Shao common_len = HCLGEVF_REG_TLV_SIZE + sizeof(common_reg_addr_list);
1143ef5d70bSJijie Shao ring_len = HCLGEVF_REG_TLV_SIZE + sizeof(ring_reg_addr_list);
1153ef5d70bSJijie Shao tqp_intr_len = HCLGEVF_REG_TLV_SIZE + sizeof(tqp_intr_reg_addr_list);
116939ccd10SJijie Shao
1173ef5d70bSJijie Shao /* return the total length of all register values */
1183ef5d70bSJijie Shao return HCLGEVF_REG_HEADER_SIZE + cmdq_len + common_len +
1193ef5d70bSJijie Shao tqp_intr_len * (hdev->num_msi_used - 1) +
1203ef5d70bSJijie Shao ring_len * hdev->num_tqps;
121939ccd10SJijie Shao }
122939ccd10SJijie Shao
hclgevf_get_regs(struct hnae3_handle * handle,u32 * version,void * data)123939ccd10SJijie Shao void hclgevf_get_regs(struct hnae3_handle *handle, u32 *version,
124939ccd10SJijie Shao void *data)
125939ccd10SJijie Shao {
126939ccd10SJijie Shao #define HCLGEVF_RING_INT_REG_OFFSET 0x4
127939ccd10SJijie Shao
128939ccd10SJijie Shao struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
129*3e22b7deSHao Lan struct hnae3_queue *tqp;
1303ef5d70bSJijie Shao int i, j, reg_um;
131939ccd10SJijie Shao u32 *reg = data;
132939ccd10SJijie Shao
133939ccd10SJijie Shao *version = hdev->fw_version;
1343ef5d70bSJijie Shao reg += hclgevf_reg_get_header(reg);
135939ccd10SJijie Shao
136939ccd10SJijie Shao /* fetching per-VF registers values from VF PCIe register space */
1372cbece60SZhang Zekun reg_um = ARRAY_SIZE(cmdq_reg_addr_list);
1383ef5d70bSJijie Shao reg += hclgevf_reg_get_tlv(HCLGEVF_REG_TAG_CMDQ, reg_um, reg);
139939ccd10SJijie Shao for (i = 0; i < reg_um; i++)
140939ccd10SJijie Shao *reg++ = hclgevf_read_dev(&hdev->hw, cmdq_reg_addr_list[i]);
141939ccd10SJijie Shao
1422cbece60SZhang Zekun reg_um = ARRAY_SIZE(common_reg_addr_list);
1433ef5d70bSJijie Shao reg += hclgevf_reg_get_tlv(HCLGEVF_REG_TAG_COMMON, reg_um, reg);
144939ccd10SJijie Shao for (i = 0; i < reg_um; i++)
145939ccd10SJijie Shao *reg++ = hclgevf_read_dev(&hdev->hw, common_reg_addr_list[i]);
146939ccd10SJijie Shao
1472cbece60SZhang Zekun reg_um = ARRAY_SIZE(ring_reg_addr_list);
148939ccd10SJijie Shao for (j = 0; j < hdev->num_tqps; j++) {
1493ef5d70bSJijie Shao reg += hclgevf_reg_get_tlv(HCLGEVF_REG_TAG_RING, reg_um, reg);
150*3e22b7deSHao Lan tqp = &hdev->htqp[j].q;
151939ccd10SJijie Shao for (i = 0; i < reg_um; i++)
152*3e22b7deSHao Lan *reg++ = readl_relaxed(tqp->io_base -
153*3e22b7deSHao Lan HCLGEVF_TQP_REG_OFFSET +
154*3e22b7deSHao Lan ring_reg_addr_list[i]);
155939ccd10SJijie Shao }
156939ccd10SJijie Shao
1572cbece60SZhang Zekun reg_um = ARRAY_SIZE(tqp_intr_reg_addr_list);
158939ccd10SJijie Shao for (j = 0; j < hdev->num_msi_used - 1; j++) {
1593ef5d70bSJijie Shao reg += hclgevf_reg_get_tlv(HCLGEVF_REG_TAG_TQP_INTR, reg_um, reg);
160939ccd10SJijie Shao for (i = 0; i < reg_um; i++)
161939ccd10SJijie Shao *reg++ = hclgevf_read_dev(&hdev->hw,
162939ccd10SJijie Shao tqp_intr_reg_addr_list[i] +
163939ccd10SJijie Shao HCLGEVF_RING_INT_REG_OFFSET * j);
164939ccd10SJijie Shao }
165939ccd10SJijie Shao }
166