net/hns3: move queue stats to xstats
[dpdk.git] / drivers / net / hns3 / hns3_stats.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2019 Hisilicon Limited.
3  */
4
5 #include <rte_ethdev.h>
6 #include <rte_io.h>
7 #include <rte_malloc.h>
8
9 #include "hns3_ethdev.h"
10 #include "hns3_rxtx.h"
11 #include "hns3_logs.h"
12 #include "hns3_regs.h"
13
14 /* The statistics of the per-rxq basic stats */
15 static const struct hns3_xstats_name_offset hns3_rxq_basic_stats_strings[] = {
16         {"packets",
17                 HNS3_RXQ_BASIC_STATS_FIELD_OFFSET(packets)},
18         {"bytes",
19                 HNS3_RXQ_BASIC_STATS_FIELD_OFFSET(bytes)},
20         {"errors",
21                 HNS3_RXQ_BASIC_STATS_FIELD_OFFSET(errors)}
22 };
23
24 /* The statistics of the per-txq basic stats */
25 static const struct hns3_xstats_name_offset hns3_txq_basic_stats_strings[] = {
26         {"packets",
27                 HNS3_TXQ_BASIC_STATS_FIELD_OFFSET(packets)},
28         {"bytes",
29                 HNS3_TXQ_BASIC_STATS_FIELD_OFFSET(bytes)}
30 };
31
32 /* MAC statistics */
33 static const struct hns3_xstats_name_offset hns3_mac_strings[] = {
34         {"mac_tx_mac_pause_num",
35                 HNS3_MAC_STATS_OFFSET(mac_tx_mac_pause_num)},
36         {"mac_rx_mac_pause_num",
37                 HNS3_MAC_STATS_OFFSET(mac_rx_mac_pause_num)},
38         {"mac_tx_control_pkt_num",
39                 HNS3_MAC_STATS_OFFSET(mac_tx_ctrl_pkt_num)},
40         {"mac_rx_control_pkt_num",
41                 HNS3_MAC_STATS_OFFSET(mac_rx_ctrl_pkt_num)},
42         {"mac_tx_pfc_pkt_num",
43                 HNS3_MAC_STATS_OFFSET(mac_tx_pfc_pause_pkt_num)},
44         {"mac_tx_pfc_pri0_pkt_num",
45                 HNS3_MAC_STATS_OFFSET(mac_tx_pfc_pri0_pkt_num)},
46         {"mac_tx_pfc_pri1_pkt_num",
47                 HNS3_MAC_STATS_OFFSET(mac_tx_pfc_pri1_pkt_num)},
48         {"mac_tx_pfc_pri2_pkt_num",
49                 HNS3_MAC_STATS_OFFSET(mac_tx_pfc_pri2_pkt_num)},
50         {"mac_tx_pfc_pri3_pkt_num",
51                 HNS3_MAC_STATS_OFFSET(mac_tx_pfc_pri3_pkt_num)},
52         {"mac_tx_pfc_pri4_pkt_num",
53                 HNS3_MAC_STATS_OFFSET(mac_tx_pfc_pri4_pkt_num)},
54         {"mac_tx_pfc_pri5_pkt_num",
55                 HNS3_MAC_STATS_OFFSET(mac_tx_pfc_pri5_pkt_num)},
56         {"mac_tx_pfc_pri6_pkt_num",
57                 HNS3_MAC_STATS_OFFSET(mac_tx_pfc_pri6_pkt_num)},
58         {"mac_tx_pfc_pri7_pkt_num",
59                 HNS3_MAC_STATS_OFFSET(mac_tx_pfc_pri7_pkt_num)},
60         {"mac_rx_pfc_pkt_num",
61                 HNS3_MAC_STATS_OFFSET(mac_rx_pfc_pause_pkt_num)},
62         {"mac_rx_pfc_pri0_pkt_num",
63                 HNS3_MAC_STATS_OFFSET(mac_rx_pfc_pri0_pkt_num)},
64         {"mac_rx_pfc_pri1_pkt_num",
65                 HNS3_MAC_STATS_OFFSET(mac_rx_pfc_pri1_pkt_num)},
66         {"mac_rx_pfc_pri2_pkt_num",
67                 HNS3_MAC_STATS_OFFSET(mac_rx_pfc_pri2_pkt_num)},
68         {"mac_rx_pfc_pri3_pkt_num",
69                 HNS3_MAC_STATS_OFFSET(mac_rx_pfc_pri3_pkt_num)},
70         {"mac_rx_pfc_pri4_pkt_num",
71                 HNS3_MAC_STATS_OFFSET(mac_rx_pfc_pri4_pkt_num)},
72         {"mac_rx_pfc_pri5_pkt_num",
73                 HNS3_MAC_STATS_OFFSET(mac_rx_pfc_pri5_pkt_num)},
74         {"mac_rx_pfc_pri6_pkt_num",
75                 HNS3_MAC_STATS_OFFSET(mac_rx_pfc_pri6_pkt_num)},
76         {"mac_rx_pfc_pri7_pkt_num",
77                 HNS3_MAC_STATS_OFFSET(mac_rx_pfc_pri7_pkt_num)},
78         {"mac_tx_total_pkt_num",
79                 HNS3_MAC_STATS_OFFSET(mac_tx_total_pkt_num)},
80         {"mac_tx_total_oct_num",
81                 HNS3_MAC_STATS_OFFSET(mac_tx_total_oct_num)},
82         {"mac_tx_good_pkt_num",
83                 HNS3_MAC_STATS_OFFSET(mac_tx_good_pkt_num)},
84         {"mac_tx_bad_pkt_num",
85                 HNS3_MAC_STATS_OFFSET(mac_tx_bad_pkt_num)},
86         {"mac_tx_good_oct_num",
87                 HNS3_MAC_STATS_OFFSET(mac_tx_good_oct_num)},
88         {"mac_tx_bad_oct_num",
89                 HNS3_MAC_STATS_OFFSET(mac_tx_bad_oct_num)},
90         {"mac_tx_uni_pkt_num",
91                 HNS3_MAC_STATS_OFFSET(mac_tx_uni_pkt_num)},
92         {"mac_tx_multi_pkt_num",
93                 HNS3_MAC_STATS_OFFSET(mac_tx_multi_pkt_num)},
94         {"mac_tx_broad_pkt_num",
95                 HNS3_MAC_STATS_OFFSET(mac_tx_broad_pkt_num)},
96         {"mac_tx_undersize_pkt_num",
97                 HNS3_MAC_STATS_OFFSET(mac_tx_undersize_pkt_num)},
98         {"mac_tx_oversize_pkt_num",
99                 HNS3_MAC_STATS_OFFSET(mac_tx_oversize_pkt_num)},
100         {"mac_tx_64_oct_pkt_num",
101                 HNS3_MAC_STATS_OFFSET(mac_tx_64_oct_pkt_num)},
102         {"mac_tx_65_127_oct_pkt_num",
103                 HNS3_MAC_STATS_OFFSET(mac_tx_65_127_oct_pkt_num)},
104         {"mac_tx_128_255_oct_pkt_num",
105                 HNS3_MAC_STATS_OFFSET(mac_tx_128_255_oct_pkt_num)},
106         {"mac_tx_256_511_oct_pkt_num",
107                 HNS3_MAC_STATS_OFFSET(mac_tx_256_511_oct_pkt_num)},
108         {"mac_tx_512_1023_oct_pkt_num",
109                 HNS3_MAC_STATS_OFFSET(mac_tx_512_1023_oct_pkt_num)},
110         {"mac_tx_1024_1518_oct_pkt_num",
111                 HNS3_MAC_STATS_OFFSET(mac_tx_1024_1518_oct_pkt_num)},
112         {"mac_tx_1519_2047_oct_pkt_num",
113                 HNS3_MAC_STATS_OFFSET(mac_tx_1519_2047_oct_pkt_num)},
114         {"mac_tx_2048_4095_oct_pkt_num",
115                 HNS3_MAC_STATS_OFFSET(mac_tx_2048_4095_oct_pkt_num)},
116         {"mac_tx_4096_8191_oct_pkt_num",
117                 HNS3_MAC_STATS_OFFSET(mac_tx_4096_8191_oct_pkt_num)},
118         {"mac_tx_8192_9216_oct_pkt_num",
119                 HNS3_MAC_STATS_OFFSET(mac_tx_8192_9216_oct_pkt_num)},
120         {"mac_tx_9217_12287_oct_pkt_num",
121                 HNS3_MAC_STATS_OFFSET(mac_tx_9217_12287_oct_pkt_num)},
122         {"mac_tx_12288_16383_oct_pkt_num",
123                 HNS3_MAC_STATS_OFFSET(mac_tx_12288_16383_oct_pkt_num)},
124         {"mac_tx_1519_max_good_pkt_num",
125                 HNS3_MAC_STATS_OFFSET(mac_tx_1519_max_good_oct_pkt_num)},
126         {"mac_tx_1519_max_bad_pkt_num",
127                 HNS3_MAC_STATS_OFFSET(mac_tx_1519_max_bad_oct_pkt_num)},
128         {"mac_rx_total_pkt_num",
129                 HNS3_MAC_STATS_OFFSET(mac_rx_total_pkt_num)},
130         {"mac_rx_total_oct_num",
131                 HNS3_MAC_STATS_OFFSET(mac_rx_total_oct_num)},
132         {"mac_rx_good_pkt_num",
133                 HNS3_MAC_STATS_OFFSET(mac_rx_good_pkt_num)},
134         {"mac_rx_bad_pkt_num",
135                 HNS3_MAC_STATS_OFFSET(mac_rx_bad_pkt_num)},
136         {"mac_rx_good_oct_num",
137                 HNS3_MAC_STATS_OFFSET(mac_rx_good_oct_num)},
138         {"mac_rx_bad_oct_num",
139                 HNS3_MAC_STATS_OFFSET(mac_rx_bad_oct_num)},
140         {"mac_rx_uni_pkt_num",
141                 HNS3_MAC_STATS_OFFSET(mac_rx_uni_pkt_num)},
142         {"mac_rx_multi_pkt_num",
143                 HNS3_MAC_STATS_OFFSET(mac_rx_multi_pkt_num)},
144         {"mac_rx_broad_pkt_num",
145                 HNS3_MAC_STATS_OFFSET(mac_rx_broad_pkt_num)},
146         {"mac_rx_undersize_pkt_num",
147                 HNS3_MAC_STATS_OFFSET(mac_rx_undersize_pkt_num)},
148         {"mac_rx_oversize_pkt_num",
149                 HNS3_MAC_STATS_OFFSET(mac_rx_oversize_pkt_num)},
150         {"mac_rx_64_oct_pkt_num",
151                 HNS3_MAC_STATS_OFFSET(mac_rx_64_oct_pkt_num)},
152         {"mac_rx_65_127_oct_pkt_num",
153                 HNS3_MAC_STATS_OFFSET(mac_rx_65_127_oct_pkt_num)},
154         {"mac_rx_128_255_oct_pkt_num",
155                 HNS3_MAC_STATS_OFFSET(mac_rx_128_255_oct_pkt_num)},
156         {"mac_rx_256_511_oct_pkt_num",
157                 HNS3_MAC_STATS_OFFSET(mac_rx_256_511_oct_pkt_num)},
158         {"mac_rx_512_1023_oct_pkt_num",
159                 HNS3_MAC_STATS_OFFSET(mac_rx_512_1023_oct_pkt_num)},
160         {"mac_rx_1024_1518_oct_pkt_num",
161                 HNS3_MAC_STATS_OFFSET(mac_rx_1024_1518_oct_pkt_num)},
162         {"mac_rx_1519_2047_oct_pkt_num",
163                 HNS3_MAC_STATS_OFFSET(mac_rx_1519_2047_oct_pkt_num)},
164         {"mac_rx_2048_4095_oct_pkt_num",
165                 HNS3_MAC_STATS_OFFSET(mac_rx_2048_4095_oct_pkt_num)},
166         {"mac_rx_4096_8191_oct_pkt_num",
167                 HNS3_MAC_STATS_OFFSET(mac_rx_4096_8191_oct_pkt_num)},
168         {"mac_rx_8192_9216_oct_pkt_num",
169                 HNS3_MAC_STATS_OFFSET(mac_rx_8192_9216_oct_pkt_num)},
170         {"mac_rx_9217_12287_oct_pkt_num",
171                 HNS3_MAC_STATS_OFFSET(mac_rx_9217_12287_oct_pkt_num)},
172         {"mac_rx_12288_16383_oct_pkt_num",
173                 HNS3_MAC_STATS_OFFSET(mac_rx_12288_16383_oct_pkt_num)},
174         {"mac_rx_1519_max_good_pkt_num",
175                 HNS3_MAC_STATS_OFFSET(mac_rx_1519_max_good_oct_pkt_num)},
176         {"mac_rx_1519_max_bad_pkt_num",
177                 HNS3_MAC_STATS_OFFSET(mac_rx_1519_max_bad_oct_pkt_num)},
178         {"mac_tx_fragment_pkt_num",
179                 HNS3_MAC_STATS_OFFSET(mac_tx_fragment_pkt_num)},
180         {"mac_tx_undermin_pkt_num",
181                 HNS3_MAC_STATS_OFFSET(mac_tx_undermin_pkt_num)},
182         {"mac_tx_jabber_pkt_num",
183                 HNS3_MAC_STATS_OFFSET(mac_tx_jabber_pkt_num)},
184         {"mac_tx_err_all_pkt_num",
185                 HNS3_MAC_STATS_OFFSET(mac_tx_err_all_pkt_num)},
186         {"mac_tx_from_app_good_pkt_num",
187                 HNS3_MAC_STATS_OFFSET(mac_tx_from_app_good_pkt_num)},
188         {"mac_tx_from_app_bad_pkt_num",
189                 HNS3_MAC_STATS_OFFSET(mac_tx_from_app_bad_pkt_num)},
190         {"mac_rx_fragment_pkt_num",
191                 HNS3_MAC_STATS_OFFSET(mac_rx_fragment_pkt_num)},
192         {"mac_rx_undermin_pkt_num",
193                 HNS3_MAC_STATS_OFFSET(mac_rx_undermin_pkt_num)},
194         {"mac_rx_jabber_pkt_num",
195                 HNS3_MAC_STATS_OFFSET(mac_rx_jabber_pkt_num)},
196         {"mac_rx_fcs_err_pkt_num",
197                 HNS3_MAC_STATS_OFFSET(mac_rx_fcs_err_pkt_num)},
198         {"mac_rx_send_app_good_pkt_num",
199                 HNS3_MAC_STATS_OFFSET(mac_rx_send_app_good_pkt_num)},
200         {"mac_rx_send_app_bad_pkt_num",
201                 HNS3_MAC_STATS_OFFSET(mac_rx_send_app_bad_pkt_num)}
202 };
203
204 static const struct hns3_xstats_name_offset hns3_error_int_stats_strings[] = {
205         {"MAC_AFIFO_TNL_INT_R",
206                 HNS3_ERR_INT_STATS_FIELD_OFFSET(mac_afifo_tnl_int_cnt)},
207         {"PPU_MPF_ABNORMAL_INT_ST2_MSIX",
208                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ppu_mpf_abn_int_st2_msix_cnt)},
209         {"SSU_PORT_BASED_ERR_INT_MSIX",
210                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ssu_port_based_pf_int_cnt)},
211         {"PPP_PF_ABNORMAL_INT_ST0",
212                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ppp_pf_abnormal_int_cnt)},
213         {"PPU_PF_ABNORMAL_INT_ST_MSIX",
214                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ppu_pf_abnormal_int_msix_cnt)},
215         {"IMP_TCM_ECC_INT_STS",
216                 HNS3_ERR_INT_STATS_FIELD_OFFSET(imp_tcm_ecc_int_cnt)},
217         {"CMDQ_MEM_ECC_INT_STS",
218                 HNS3_ERR_INT_STATS_FIELD_OFFSET(cmdq_mem_ecc_int_cnt)},
219         {"IMP_RD_POISON_INT_STS",
220                 HNS3_ERR_INT_STATS_FIELD_OFFSET(imp_rd_poison_int_cnt)},
221         {"TQP_INT_ECC_INT_STS",
222                 HNS3_ERR_INT_STATS_FIELD_OFFSET(tqp_int_ecc_int_cnt)},
223         {"MSIX_ECC_INT_STS",
224                 HNS3_ERR_INT_STATS_FIELD_OFFSET(msix_ecc_int_cnt)},
225         {"SSU_ECC_MULTI_BIT_INT_0",
226                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ssu_ecc_multi_bit_int_0_cnt)},
227         {"SSU_ECC_MULTI_BIT_INT_1",
228                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ssu_ecc_multi_bit_int_1_cnt)},
229         {"SSU_COMMON_ERR_INT",
230                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ssu_common_ecc_int_cnt)},
231         {"IGU_INT_STS",
232                 HNS3_ERR_INT_STATS_FIELD_OFFSET(igu_int_cnt)},
233         {"PPP_MPF_ABNORMAL_INT_ST1",
234                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ppp_mpf_abnormal_int_st1_cnt)},
235         {"PPP_MPF_ABNORMAL_INT_ST3",
236                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ppp_mpf_abnormal_int_st3_cnt)},
237         {"PPU_MPF_ABNORMAL_INT_ST1",
238                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ppu_mpf_abnormal_int_st1_cnt)},
239         {"PPU_MPF_ABNORMAL_INT_ST2_RAS",
240                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ppu_mpf_abn_int_st2_ras_cnt)},
241         {"PPU_MPF_ABNORMAL_INT_ST3",
242                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ppu_mpf_abnormal_int_st3_cnt)},
243         {"TM_SCH_RINT",
244                 HNS3_ERR_INT_STATS_FIELD_OFFSET(tm_sch_int_cnt)},
245         {"QCN_FIFO_RINT",
246                 HNS3_ERR_INT_STATS_FIELD_OFFSET(qcn_fifo_int_cnt)},
247         {"QCN_ECC_RINT",
248                 HNS3_ERR_INT_STATS_FIELD_OFFSET(qcn_ecc_int_cnt)},
249         {"NCSI_ECC_INT_RPT",
250                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ncsi_ecc_int_cnt)},
251         {"SSU_PORT_BASED_ERR_INT_RAS",
252                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ssu_port_based_err_int_cnt)},
253         {"SSU_FIFO_OVERFLOW_INT",
254                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ssu_fifo_overflow_int_cnt)},
255         {"SSU_ETS_TCG_INT",
256                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ssu_ets_tcg_int_cnt)},
257         {"IGU_EGU_TNL_INT_STS",
258                 HNS3_ERR_INT_STATS_FIELD_OFFSET(igu_egu_tnl_int_cnt)},
259         {"PPU_PF_ABNORMAL_INT_ST_RAS",
260                 HNS3_ERR_INT_STATS_FIELD_OFFSET(ppu_pf_abnormal_int_ras_cnt)},
261 };
262
263 /* The statistic of reset */
264 static const struct hns3_xstats_name_offset hns3_reset_stats_strings[] = {
265         {"REQ_RESET_CNT",
266                 HNS3_RESET_STATS_FIELD_OFFSET(request_cnt)},
267         {"GLOBAL_RESET_CNT",
268                 HNS3_RESET_STATS_FIELD_OFFSET(global_cnt)},
269         {"IMP_RESET_CNT",
270                 HNS3_RESET_STATS_FIELD_OFFSET(imp_cnt)},
271         {"RESET_EXEC_CNT",
272                 HNS3_RESET_STATS_FIELD_OFFSET(exec_cnt)},
273         {"RESET_SUCCESS_CNT",
274                 HNS3_RESET_STATS_FIELD_OFFSET(success_cnt)},
275         {"RESET_FAIL_CNT",
276                 HNS3_RESET_STATS_FIELD_OFFSET(fail_cnt)},
277         {"RESET_MERGE_CNT",
278                 HNS3_RESET_STATS_FIELD_OFFSET(merge_cnt)}
279 };
280
281 /* The statistic of errors in Rx BD */
282 static const struct hns3_xstats_name_offset hns3_rx_bd_error_strings[] = {
283         {"PKT_LEN_ERRORS",
284                 HNS3_RX_BD_ERROR_STATS_FIELD_OFFSET(pkt_len_errors)},
285         {"L2_ERRORS",
286                 HNS3_RX_BD_ERROR_STATS_FIELD_OFFSET(l2_errors)}
287 };
288
289 /* The dfx statistic in Rx datapath */
290 static const struct hns3_xstats_name_offset hns3_rxq_dfx_stats_strings[] = {
291         {"L3_CHECKSUM_ERRORS",
292                 HNS3_RXQ_DFX_STATS_FIELD_OFFSET(l3_csum_errors)},
293         {"L4_CHECKSUM_ERRORS",
294                 HNS3_RXQ_DFX_STATS_FIELD_OFFSET(l4_csum_errors)},
295         {"OL3_CHECKSUM_ERRORS",
296                 HNS3_RXQ_DFX_STATS_FIELD_OFFSET(ol3_csum_errors)},
297         {"OL4_CHECKSUM_ERRORS",
298                 HNS3_RXQ_DFX_STATS_FIELD_OFFSET(ol4_csum_errors)}
299 };
300
301 /* The dfx statistic in Tx datapath */
302 static const struct hns3_xstats_name_offset hns3_txq_dfx_stats_strings[] = {
303         {"OVER_LENGTH_PKT_CNT",
304                 HNS3_TXQ_DFX_STATS_FIELD_OFFSET(over_length_pkt_cnt)},
305         {"EXCEED_LIMITED_BD_PKT_CNT",
306                 HNS3_TXQ_DFX_STATS_FIELD_OFFSET(exceed_limit_bd_pkt_cnt)},
307         {"EXCEED_LIMITED_BD_PKT_REASSEMBLE_FAIL_CNT",
308                 HNS3_TXQ_DFX_STATS_FIELD_OFFSET(exceed_limit_bd_reassem_fail)},
309         {"UNSUPPORTED_TUNNEL_PKT_CNT",
310                 HNS3_TXQ_DFX_STATS_FIELD_OFFSET(unsupported_tunnel_pkt_cnt)},
311         {"QUEUE_FULL_CNT",
312                 HNS3_TXQ_DFX_STATS_FIELD_OFFSET(queue_full_cnt)},
313         {"SHORT_PKT_PAD_FAIL_CNT",
314                 HNS3_TXQ_DFX_STATS_FIELD_OFFSET(pkt_padding_fail_cnt)}
315 };
316
317 /* The statistic of rx queue */
318 static const struct hns3_xstats_name_offset hns3_rx_queue_strings[] = {
319         {"RX_QUEUE_FBD", HNS3_RING_RX_FBDNUM_REG}
320 };
321
322 /* The statistic of tx queue */
323 static const struct hns3_xstats_name_offset hns3_tx_queue_strings[] = {
324         {"TX_QUEUE_FBD", HNS3_RING_TX_FBDNUM_REG}
325 };
326
327 #define HNS3_NUM_MAC_STATS (sizeof(hns3_mac_strings) / \
328         sizeof(hns3_mac_strings[0]))
329
330 #define HNS3_NUM_ERROR_INT_XSTATS (sizeof(hns3_error_int_stats_strings) / \
331         sizeof(hns3_error_int_stats_strings[0]))
332
333 #define HNS3_NUM_RESET_XSTATS (sizeof(hns3_reset_stats_strings) / \
334         sizeof(hns3_reset_stats_strings[0]))
335
336 #define HNS3_NUM_RX_BD_ERROR_XSTATS (sizeof(hns3_rx_bd_error_strings) / \
337         sizeof(hns3_rx_bd_error_strings[0]))
338
339 #define HNS3_NUM_RXQ_DFX_XSTATS (sizeof(hns3_rxq_dfx_stats_strings) / \
340         sizeof(hns3_rxq_dfx_stats_strings[0]))
341
342 #define HNS3_NUM_TXQ_DFX_XSTATS (sizeof(hns3_txq_dfx_stats_strings) / \
343         sizeof(hns3_txq_dfx_stats_strings[0]))
344
345 #define HNS3_NUM_RX_QUEUE_STATS (sizeof(hns3_rx_queue_strings) / \
346         sizeof(hns3_rx_queue_strings[0]))
347
348 #define HNS3_NUM_TX_QUEUE_STATS (sizeof(hns3_tx_queue_strings) / \
349         sizeof(hns3_tx_queue_strings[0]))
350
351 #define HNS3_NUM_RXQ_BASIC_STATS (sizeof(hns3_rxq_basic_stats_strings) / \
352         sizeof(hns3_rxq_basic_stats_strings[0]))
353
354 #define HNS3_NUM_TXQ_BASIC_STATS (sizeof(hns3_txq_basic_stats_strings) / \
355         sizeof(hns3_txq_basic_stats_strings[0]))
356
357 #define HNS3_FIX_NUM_STATS (HNS3_NUM_MAC_STATS + HNS3_NUM_ERROR_INT_XSTATS + \
358                             HNS3_NUM_RESET_XSTATS)
359
360 static void hns3_tqp_stats_clear(struct hns3_hw *hw);
361
362 /*
363  * Query all the MAC statistics data of Network ICL command ,opcode id: 0x0034.
364  * This command is used before send 'query_mac_stat command', the descriptor
365  * number of 'query_mac_stat command' must match with reg_num in this command.
366  * @praram hw
367  *   Pointer to structure hns3_hw.
368  * @return
369  *   0 on success.
370  */
371 static int
372 hns3_update_mac_stats(struct hns3_hw *hw, const uint32_t desc_num)
373 {
374         uint64_t *data = (uint64_t *)(&hw->mac_stats);
375         struct hns3_cmd_desc *desc;
376         uint64_t *desc_data;
377         uint16_t i, k, n;
378         int ret;
379
380         desc = rte_malloc("hns3_mac_desc",
381                           desc_num * sizeof(struct hns3_cmd_desc), 0);
382         if (desc == NULL) {
383                 hns3_err(hw, "Mac_update_stats alloced desc malloc fail");
384                 return -ENOMEM;
385         }
386
387         hns3_cmd_setup_basic_desc(desc, HNS3_OPC_STATS_MAC_ALL, true);
388         ret = hns3_cmd_send(hw, desc, desc_num);
389         if (ret) {
390                 hns3_err(hw, "Update complete MAC pkt stats fail : %d", ret);
391                 rte_free(desc);
392                 return ret;
393         }
394
395         for (i = 0; i < desc_num; i++) {
396                 /* For special opcode 0034, only the first desc has the head */
397                 if (i == 0) {
398                         desc_data = (uint64_t *)(&desc[i].data[0]);
399                         n = HNS3_RD_FIRST_STATS_NUM;
400                 } else {
401                         desc_data = (uint64_t *)(&desc[i]);
402                         n = HNS3_RD_OTHER_STATS_NUM;
403                 }
404
405                 for (k = 0; k < n; k++) {
406                         *data += rte_le_to_cpu_64(*desc_data);
407                         data++;
408                         desc_data++;
409                 }
410         }
411         rte_free(desc);
412
413         return 0;
414 }
415
416 /*
417  * Query Mac stat reg num command ,opcode id: 0x0033.
418  * This command is used before send 'query_mac_stat command', the descriptor
419  * number of 'query_mac_stat command' must match with reg_num in this command.
420  * @praram rte_stats
421  *   Pointer to structure rte_eth_stats.
422  * @return
423  *   0 on success.
424  */
425 static int
426 hns3_mac_query_reg_num(struct rte_eth_dev *dev, uint32_t *desc_num)
427 {
428         struct hns3_adapter *hns = dev->data->dev_private;
429         struct hns3_hw *hw = &hns->hw;
430         struct hns3_cmd_desc desc;
431         uint32_t *desc_data;
432         uint32_t reg_num;
433         int ret;
434
435         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_MAC_REG_NUM, true);
436         ret = hns3_cmd_send(hw, &desc, 1);
437         if (ret)
438                 return ret;
439
440         /*
441          * The num of MAC statistics registers that are provided by IMP in this
442          * version.
443          */
444         desc_data = (uint32_t *)(&desc.data[0]);
445         reg_num = rte_le_to_cpu_32(*desc_data);
446
447         /*
448          * The descriptor number of 'query_additional_mac_stat command' is
449          * '1 + (reg_num-3)/4 + ((reg_num-3)%4 !=0)';
450          * This value is 83 in this version
451          */
452         *desc_num = 1 + ((reg_num - 3) >> 2) +
453                     (uint32_t)(((reg_num - 3) & 0x3) ? 1 : 0);
454
455         return 0;
456 }
457
458 static int
459 hns3_query_update_mac_stats(struct rte_eth_dev *dev)
460 {
461         struct hns3_adapter *hns = dev->data->dev_private;
462         struct hns3_hw *hw = &hns->hw;
463         uint32_t desc_num;
464         int ret;
465
466         ret = hns3_mac_query_reg_num(dev, &desc_num);
467         if (ret == 0)
468                 ret = hns3_update_mac_stats(hw, desc_num);
469         else
470                 hns3_err(hw, "Query mac reg num fail : %d", ret);
471         return ret;
472 }
473
474 /* Get tqp stats from register */
475 static int
476 hns3_update_tqp_stats(struct hns3_hw *hw)
477 {
478         struct hns3_tqp_stats *stats = &hw->tqp_stats;
479         struct hns3_cmd_desc desc;
480         uint64_t cnt;
481         uint16_t i;
482         int ret;
483
484         for (i = 0; i < hw->tqps_num; i++) {
485                 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_RX_STATUS,
486                                           true);
487
488                 desc.data[0] = rte_cpu_to_le_32((uint32_t)i);
489                 ret = hns3_cmd_send(hw, &desc, 1);
490                 if (ret) {
491                         hns3_err(hw, "Failed to query RX No.%u queue stat: %d",
492                                  i, ret);
493                         return ret;
494                 }
495                 cnt = rte_le_to_cpu_32(desc.data[1]);
496                 stats->rcb_rx_ring_pktnum_rcd += cnt;
497                 stats->rcb_rx_ring_pktnum[i] += cnt;
498
499                 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_TX_STATUS,
500                                           true);
501
502                 desc.data[0] = rte_cpu_to_le_32((uint32_t)i);
503                 ret = hns3_cmd_send(hw, &desc, 1);
504                 if (ret) {
505                         hns3_err(hw, "Failed to query TX No.%u queue stat: %d",
506                                  i, ret);
507                         return ret;
508                 }
509                 cnt = rte_le_to_cpu_32(desc.data[1]);
510                 stats->rcb_tx_ring_pktnum_rcd += cnt;
511                 stats->rcb_tx_ring_pktnum[i] += cnt;
512         }
513
514         return 0;
515 }
516
517 /*
518  * Query tqp tx queue statistics ,opcode id: 0x0B03.
519  * Query tqp rx queue statistics ,opcode id: 0x0B13.
520  * Get all statistics of a port.
521  * @param eth_dev
522  *   Pointer to Ethernet device.
523  * @praram rte_stats
524  *   Pointer to structure rte_eth_stats.
525  * @return
526  *   0 on success.
527  */
528 int
529 hns3_stats_get(struct rte_eth_dev *eth_dev, struct rte_eth_stats *rte_stats)
530 {
531         struct hns3_adapter *hns = eth_dev->data->dev_private;
532         struct hns3_hw *hw = &hns->hw;
533         struct hns3_tqp_stats *stats = &hw->tqp_stats;
534         struct hns3_rx_queue *rxq;
535         uint64_t cnt;
536         uint16_t i;
537         int ret;
538
539         /* Update tqp stats by read register */
540         ret = hns3_update_tqp_stats(hw);
541         if (ret) {
542                 hns3_err(hw, "Update tqp stats fail : %d", ret);
543                 return ret;
544         }
545
546         /* Get the error stats of received packets */
547         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
548                 rxq = eth_dev->data->rx_queues[i];
549                 if (rxq) {
550                         cnt = rxq->err_stats.l2_errors +
551                                 rxq->err_stats.pkt_len_errors;
552                         rte_stats->ierrors += cnt;
553                 }
554         }
555
556         rte_stats->oerrors = 0;
557         rte_stats->ipackets  = stats->rcb_rx_ring_pktnum_rcd -
558                 rte_stats->ierrors;
559         rte_stats->opackets  = stats->rcb_tx_ring_pktnum_rcd -
560                 rte_stats->oerrors;
561         rte_stats->rx_nombuf = eth_dev->data->rx_mbuf_alloc_failed;
562
563         return 0;
564 }
565
566 int
567 hns3_stats_reset(struct rte_eth_dev *eth_dev)
568 {
569         struct hns3_adapter *hns = eth_dev->data->dev_private;
570         struct hns3_hw *hw = &hns->hw;
571         struct hns3_cmd_desc desc_reset;
572         struct hns3_rx_queue *rxq;
573         uint16_t i;
574         int ret;
575
576         /*
577          * Note: Reading hardware statistics of rx/tx queue packet number
578          * will clear them.
579          */
580         for (i = 0; i < hw->tqps_num; i++) {
581                 hns3_cmd_setup_basic_desc(&desc_reset, HNS3_OPC_QUERY_RX_STATUS,
582                                           true);
583                 desc_reset.data[0] = rte_cpu_to_le_32((uint32_t)i);
584                 ret = hns3_cmd_send(hw, &desc_reset, 1);
585                 if (ret) {
586                         hns3_err(hw, "Failed to reset RX No.%u queue stat: %d",
587                                  i, ret);
588                         return ret;
589                 }
590
591                 hns3_cmd_setup_basic_desc(&desc_reset, HNS3_OPC_QUERY_TX_STATUS,
592                                           true);
593                 desc_reset.data[0] = rte_cpu_to_le_32((uint32_t)i);
594                 ret = hns3_cmd_send(hw, &desc_reset, 1);
595                 if (ret) {
596                         hns3_err(hw, "Failed to reset TX No.%u queue stat: %d",
597                                  i, ret);
598                         return ret;
599                 }
600         }
601
602         /*
603          * Clear soft stats of rx error packet which will be dropped
604          * in driver.
605          */
606         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
607                 rxq = eth_dev->data->rx_queues[i];
608                 if (rxq) {
609                         rxq->err_stats.pkt_len_errors = 0;
610                         rxq->err_stats.l2_errors = 0;
611                 }
612         }
613
614         /*
615          * 'packets' in hns3_tx_basic_stats and hns3_rx_basic_stats come
616          * from hw->tqp_stats. And clearing tqp stats is like clearing
617          * their source.
618          */
619         hns3_tqp_stats_clear(hw);
620
621         return 0;
622 }
623
624 static int
625 hns3_mac_stats_reset(__rte_unused struct rte_eth_dev *dev)
626 {
627         struct hns3_adapter *hns = dev->data->dev_private;
628         struct hns3_hw *hw = &hns->hw;
629         struct hns3_mac_stats *mac_stats = &hw->mac_stats;
630         int ret;
631
632         ret = hns3_query_update_mac_stats(dev);
633         if (ret) {
634                 hns3_err(hw, "Clear Mac stats fail : %d", ret);
635                 return ret;
636         }
637
638         memset(mac_stats, 0, sizeof(struct hns3_mac_stats));
639
640         return 0;
641 }
642
643 /* This function calculates the number of xstats based on the current config */
644 static int
645 hns3_xstats_calc_num(struct rte_eth_dev *dev)
646 {
647 #define HNS3_PF_VF_RX_COMM_STATS_NUM    (HNS3_NUM_RX_BD_ERROR_XSTATS + \
648                                          HNS3_NUM_RXQ_DFX_XSTATS + \
649                                          HNS3_NUM_RX_QUEUE_STATS + \
650                                          HNS3_NUM_RXQ_BASIC_STATS)
651 #define HNS3_PF_VF_TX_COMM_STATS_NUM    (HNS3_NUM_TXQ_DFX_XSTATS + \
652                                          HNS3_NUM_TX_QUEUE_STATS + \
653                                          HNS3_NUM_TXQ_BASIC_STATS)
654
655         struct hns3_adapter *hns = dev->data->dev_private;
656         uint16_t nb_rx_q = dev->data->nb_rx_queues;
657         uint16_t nb_tx_q = dev->data->nb_tx_queues;
658         int rx_comm_stats_num = nb_rx_q * HNS3_PF_VF_RX_COMM_STATS_NUM;
659         int tx_comm_stats_num = nb_tx_q * HNS3_PF_VF_TX_COMM_STATS_NUM;
660
661         if (hns->is_vf)
662                 return rx_comm_stats_num + tx_comm_stats_num +
663                         HNS3_NUM_RESET_XSTATS;
664         else
665                 return rx_comm_stats_num + tx_comm_stats_num +
666                         HNS3_FIX_NUM_STATS;
667 }
668
669 static void
670 hns3_queue_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
671                      int *count)
672 {
673         struct hns3_adapter *hns = dev->data->dev_private;
674         struct hns3_hw *hw = &hns->hw;
675         uint32_t reg_offset;
676         uint16_t i, j;
677
678         /* Get rx queue stats */
679         for (j = 0; j < dev->data->nb_rx_queues; j++) {
680                 for (i = 0; i < HNS3_NUM_RX_QUEUE_STATS; i++) {
681                         reg_offset = hns3_get_tqp_reg_offset(j);
682                         xstats[*count].value = hns3_read_dev(hw,
683                                 reg_offset + hns3_rx_queue_strings[i].offset);
684                         xstats[*count].id = *count;
685                         (*count)++;
686                 }
687         }
688
689         /* Get tx queue stats */
690         for (j = 0; j < dev->data->nb_tx_queues; j++) {
691                 for (i = 0; i < HNS3_NUM_TX_QUEUE_STATS; i++) {
692                         reg_offset = hns3_get_tqp_reg_offset(j);
693                         xstats[*count].value = hns3_read_dev(hw,
694                                 reg_offset + hns3_tx_queue_strings[i].offset);
695                         xstats[*count].id = *count;
696                         (*count)++;
697                 }
698         }
699 }
700
701 void
702 hns3_error_int_stats_add(struct hns3_adapter *hns, const char *err)
703 {
704         struct hns3_pf *pf = &hns->pf;
705         uint16_t i;
706         char *addr;
707
708         for (i = 0; i < HNS3_NUM_ERROR_INT_XSTATS; i++) {
709                 if (strcmp(hns3_error_int_stats_strings[i].name, err) == 0) {
710                         addr = (char *)&pf->abn_int_stats +
711                                 hns3_error_int_stats_strings[i].offset;
712                         *(uint64_t *)addr += 1;
713                         break;
714                 }
715         }
716 }
717
718 static void
719 hns3_rxq_dfx_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
720                        int *count)
721 {
722         struct hns3_rx_dfx_stats *dfx_stats;
723         struct hns3_rx_queue *rxq;
724         uint16_t i, j;
725         char *val;
726
727         for (i = 0; i < dev->data->nb_rx_queues; i++) {
728                 rxq = (struct hns3_rx_queue *)dev->data->rx_queues[i];
729                 if (rxq == NULL)
730                         continue;
731
732                 dfx_stats = &rxq->dfx_stats;
733                 for (j = 0; j < HNS3_NUM_RXQ_DFX_XSTATS; j++) {
734                         val = (char *)dfx_stats +
735                                 hns3_rxq_dfx_stats_strings[j].offset;
736                         xstats[*count].value = *(uint64_t *)val;
737                         xstats[*count].id = *count;
738                         (*count)++;
739                 }
740         }
741 }
742
743 static void
744 hns3_txq_dfx_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
745                        int *count)
746 {
747         struct hns3_tx_dfx_stats *dfx_stats;
748         struct hns3_tx_queue *txq;
749         uint16_t i, j;
750         char *val;
751
752         for (i = 0; i < dev->data->nb_tx_queues; i++) {
753                 txq = (struct hns3_tx_queue *)dev->data->tx_queues[i];
754                 if (txq == NULL)
755                         continue;
756
757                 dfx_stats = &txq->dfx_stats;
758                 for (j = 0; j < HNS3_NUM_TXQ_DFX_XSTATS; j++) {
759                         val = (char *)dfx_stats +
760                                 hns3_txq_dfx_stats_strings[j].offset;
761                         xstats[*count].value = *(uint64_t *)val;
762                         xstats[*count].id = *count;
763                         (*count)++;
764                 }
765         }
766 }
767
768 static void
769 hns3_tqp_dfx_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
770                        int *count)
771 {
772         hns3_rxq_dfx_stats_get(dev, xstats, count);
773         hns3_txq_dfx_stats_get(dev, xstats, count);
774 }
775
776 static void
777 hns3_rxq_basic_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
778                          int *count)
779 {
780         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
781         struct hns3_tqp_stats *stats = &hw->tqp_stats;
782         struct hns3_rx_basic_stats *rxq_stats;
783         struct hns3_rx_queue *rxq;
784         uint16_t i, j;
785         char *val;
786
787         for (i = 0; i < dev->data->nb_rx_queues; i++) {
788                 rxq = dev->data->rx_queues[i];
789                 if (rxq == NULL)
790                         continue;
791
792                 rxq_stats = &rxq->basic_stats;
793                 rxq_stats->errors = rxq->err_stats.l2_errors +
794                                         rxq->err_stats.pkt_len_errors;
795                 rxq_stats->packets = stats->rcb_rx_ring_pktnum[i] -
796                         rxq_stats->errors;
797                 rxq_stats->bytes = 0;
798                 for (j = 0; j < HNS3_NUM_RXQ_BASIC_STATS; j++) {
799                         val = (char *)rxq_stats +
800                                 hns3_rxq_basic_stats_strings[j].offset;
801                         xstats[*count].value = *(uint64_t *)val;
802                         xstats[*count].id = *count;
803                         (*count)++;
804                 }
805         }
806 }
807
808 static void
809 hns3_txq_basic_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
810                          int *count)
811 {
812         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
813         struct hns3_tqp_stats *stats = &hw->tqp_stats;
814         struct hns3_tx_basic_stats *txq_stats;
815         struct hns3_tx_queue *txq;
816         uint16_t i, j;
817         char *val;
818
819         for (i = 0; i < dev->data->nb_tx_queues; i++) {
820                 txq = dev->data->tx_queues[i];
821                 if (txq == NULL)
822                         continue;
823
824                 txq_stats = &txq->basic_stats;
825                 txq_stats->packets = stats->rcb_tx_ring_pktnum[i];
826                 txq_stats->bytes = 0;
827                 for (j = 0; j < HNS3_NUM_TXQ_BASIC_STATS; j++) {
828                         val = (char *)txq_stats +
829                                 hns3_txq_basic_stats_strings[j].offset;
830                         xstats[*count].value = *(uint64_t *)val;
831                         xstats[*count].id = *count;
832                         (*count)++;
833                 }
834         }
835 }
836
837 static int
838 hns3_tqp_basic_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
839                          int *count)
840 {
841         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
842         int ret;
843
844         /* Update tqp stats by read register */
845         ret = hns3_update_tqp_stats(hw);
846         if (ret) {
847                 hns3_err(hw, "Update tqp stats fail, ret = %d.", ret);
848                 return ret;
849         }
850
851         hns3_rxq_basic_stats_get(dev, xstats, count);
852         hns3_txq_basic_stats_get(dev, xstats, count);
853
854         return 0;
855 }
856
857 /*
858  * The function is only called by hns3_dev_xstats_reset to clear
859  * basic stats of per-queue. TQP stats are all cleared in hns3_stats_reset
860  * which is called before this function.
861  *
862  * @param dev
863  *   Pointer to Ethernet device.
864  */
865 static void
866 hns3_tqp_basic_stats_clear(struct rte_eth_dev *dev)
867 {
868         struct hns3_tx_queue *txq;
869         struct hns3_rx_queue *rxq;
870         uint16_t i;
871
872         for (i = 0; i < dev->data->nb_rx_queues; i++) {
873                 rxq = dev->data->rx_queues[i];
874                 if (rxq)
875                         memset(&rxq->basic_stats, 0,
876                                sizeof(struct hns3_rx_basic_stats));
877         }
878
879         for (i = 0; i < dev->data->nb_tx_queues; i++) {
880                 txq = dev->data->tx_queues[i];
881                 if (txq)
882                         memset(&txq->basic_stats, 0,
883                                sizeof(struct hns3_tx_basic_stats));
884         }
885 }
886
887 /*
888  * Retrieve extended(tqp | Mac) statistics of an Ethernet device.
889  * @param dev
890  *   Pointer to Ethernet device.
891  * @praram xstats
892  *   A pointer to a table of structure of type *rte_eth_xstat*
893  *   to be filled with device statistics ids and values.
894  *   This parameter can be set to NULL if n is 0.
895  * @param n
896  *   The size of the xstats array (number of elements).
897  * @return
898  *   0 on fail, count(The size of the statistics elements) on success.
899  */
900 int
901 hns3_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
902                     unsigned int n)
903 {
904         struct hns3_adapter *hns = dev->data->dev_private;
905         struct hns3_pf *pf = &hns->pf;
906         struct hns3_hw *hw = &hns->hw;
907         struct hns3_mac_stats *mac_stats = &hw->mac_stats;
908         struct hns3_reset_stats *reset_stats = &hw->reset.stats;
909         struct hns3_rx_bd_errors_stats *rx_err_stats;
910         struct hns3_rx_queue *rxq;
911         uint16_t i, j;
912         char *addr;
913         int count;
914         int ret;
915
916         if (xstats == NULL)
917                 return 0;
918
919         count = hns3_xstats_calc_num(dev);
920         if ((int)n < count)
921                 return count;
922
923         count = 0;
924
925         ret = hns3_tqp_basic_stats_get(dev, xstats, &count);
926         if (ret < 0)
927                 return ret;
928
929         if (!hns->is_vf) {
930                 /* Update Mac stats */
931                 ret = hns3_query_update_mac_stats(dev);
932                 if (ret < 0) {
933                         hns3_err(hw, "Update Mac stats fail : %d", ret);
934                         return ret;
935                 }
936
937                 /* Get MAC stats from hw->hw_xstats.mac_stats struct */
938                 for (i = 0; i < HNS3_NUM_MAC_STATS; i++) {
939                         addr = (char *)mac_stats + hns3_mac_strings[i].offset;
940                         xstats[count].value = *(uint64_t *)addr;
941                         xstats[count].id = count;
942                         count++;
943                 }
944
945                 for (i = 0; i < HNS3_NUM_ERROR_INT_XSTATS; i++) {
946                         addr = (char *)&pf->abn_int_stats +
947                                hns3_error_int_stats_strings[i].offset;
948                         xstats[count].value = *(uint64_t *)addr;
949                         xstats[count].id = count;
950                         count++;
951                 }
952         }
953
954         /* Get the reset stat */
955         for (i = 0; i < HNS3_NUM_RESET_XSTATS; i++) {
956                 addr = (char *)reset_stats + hns3_reset_stats_strings[i].offset;
957                 xstats[count].value = *(uint64_t *)addr;
958                 xstats[count].id = count;
959                 count++;
960         }
961
962         /* Get the Rx BD errors stats */
963         for (j = 0; j < dev->data->nb_rx_queues; j++) {
964                 for (i = 0; i < HNS3_NUM_RX_BD_ERROR_XSTATS; i++) {
965                         rxq = dev->data->rx_queues[j];
966                         if (rxq) {
967                                 rx_err_stats = &rxq->err_stats;
968                                 addr = (char *)rx_err_stats +
969                                         hns3_rx_bd_error_strings[i].offset;
970                                 xstats[count].value = *(uint64_t *)addr;
971                                 xstats[count].id = count;
972                                 count++;
973                         }
974                 }
975         }
976
977         hns3_tqp_dfx_stats_get(dev, xstats, &count);
978         hns3_queue_stats_get(dev, xstats, &count);
979
980         return count;
981 }
982
983 static void
984 hns3_tqp_basic_stats_name_get(struct rte_eth_dev *dev,
985                               struct rte_eth_xstat_name *xstats_names,
986                               uint32_t *count)
987 {
988         uint16_t i, j;
989
990         for (i = 0; i < dev->data->nb_rx_queues; i++) {
991                 for (j = 0; j < HNS3_NUM_RXQ_BASIC_STATS; j++) {
992                         snprintf(xstats_names[*count].name,
993                                  sizeof(xstats_names[*count].name),
994                                  "rx_q%u_%s", i,
995                                  hns3_rxq_basic_stats_strings[j].name);
996                         (*count)++;
997                 }
998         }
999         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1000                 for (j = 0; j < HNS3_NUM_TXQ_BASIC_STATS; j++) {
1001                         snprintf(xstats_names[*count].name,
1002                                  sizeof(xstats_names[*count].name),
1003                                  "tx_q%u_%s", i,
1004                                  hns3_txq_basic_stats_strings[j].name);
1005                         (*count)++;
1006                 }
1007         }
1008 }
1009
1010 static void
1011 hns3_tqp_dfx_stats_name_get(struct rte_eth_dev *dev,
1012                             struct rte_eth_xstat_name *xstats_names,
1013                             uint32_t *count)
1014 {
1015         uint16_t i, j;
1016
1017         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1018                 for (j = 0; j < HNS3_NUM_RXQ_DFX_XSTATS; j++) {
1019                         snprintf(xstats_names[*count].name,
1020                                  sizeof(xstats_names[*count].name),
1021                                  "rx_q%u_%s", i,
1022                                  hns3_rxq_dfx_stats_strings[j].name);
1023                         (*count)++;
1024                 }
1025         }
1026
1027         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1028                 for (j = 0; j < HNS3_NUM_TXQ_DFX_XSTATS; j++) {
1029                         snprintf(xstats_names[*count].name,
1030                                  sizeof(xstats_names[*count].name),
1031                                  "tx_q%u_%s", i,
1032                                  hns3_txq_dfx_stats_strings[j].name);
1033                         (*count)++;
1034                 }
1035         }
1036 }
1037
1038 /*
1039  * Retrieve names of extended statistics of an Ethernet device.
1040  *
1041  * There is an assumption that 'xstat_names' and 'xstats' arrays are matched
1042  * by array index:
1043  *  xstats_names[i].name => xstats[i].value
1044  *
1045  * And the array index is same with id field of 'struct rte_eth_xstat':
1046  *  xstats[i].id == i
1047  *
1048  * This assumption makes key-value pair matching less flexible but simpler.
1049  *
1050  * @param dev
1051  *   Pointer to Ethernet device.
1052  * @param xstats_names
1053  *   An rte_eth_xstat_name array of at least *size* elements to
1054  *   be filled. If set to NULL, the function returns the required number
1055  *   of elements.
1056  * @param size
1057  *   The size of the xstats_names array (number of elements).
1058  * @return
1059  *   - A positive value lower or equal to size: success. The return value
1060  *     is the number of entries filled in the stats table.
1061  */
1062 int
1063 hns3_dev_xstats_get_names(struct rte_eth_dev *dev,
1064                           struct rte_eth_xstat_name *xstats_names,
1065                           __rte_unused unsigned int size)
1066 {
1067         struct hns3_adapter *hns = dev->data->dev_private;
1068         int cnt_stats = hns3_xstats_calc_num(dev);
1069         uint32_t count = 0;
1070         uint16_t i, j;
1071
1072         if (xstats_names == NULL)
1073                 return cnt_stats;
1074
1075         hns3_tqp_basic_stats_name_get(dev, xstats_names, &count);
1076
1077         /* Note: size limited checked in rte_eth_xstats_get_names() */
1078         if (!hns->is_vf) {
1079                 /* Get MAC name from hw->hw_xstats.mac_stats struct */
1080                 for (i = 0; i < HNS3_NUM_MAC_STATS; i++) {
1081                         snprintf(xstats_names[count].name,
1082                                  sizeof(xstats_names[count].name),
1083                                  "%s", hns3_mac_strings[i].name);
1084                         count++;
1085                 }
1086
1087                 for (i = 0; i < HNS3_NUM_ERROR_INT_XSTATS; i++) {
1088                         snprintf(xstats_names[count].name,
1089                                  sizeof(xstats_names[count].name),
1090                                  "%s", hns3_error_int_stats_strings[i].name);
1091                         count++;
1092                 }
1093         }
1094         for (i = 0; i < HNS3_NUM_RESET_XSTATS; i++) {
1095                 snprintf(xstats_names[count].name,
1096                          sizeof(xstats_names[count].name),
1097                          "%s", hns3_reset_stats_strings[i].name);
1098                 count++;
1099         }
1100
1101         for (j = 0; j < dev->data->nb_rx_queues; j++) {
1102                 for (i = 0; i < HNS3_NUM_RX_BD_ERROR_XSTATS; i++) {
1103                         snprintf(xstats_names[count].name,
1104                                  sizeof(xstats_names[count].name),
1105                                  "rx_q%u_%s", j,
1106                                  hns3_rx_bd_error_strings[i].name);
1107                         count++;
1108                 }
1109         }
1110
1111         hns3_tqp_dfx_stats_name_get(dev, xstats_names, &count);
1112
1113         for (j = 0; j < dev->data->nb_rx_queues; j++) {
1114                 for (i = 0; i < HNS3_NUM_RX_QUEUE_STATS; i++) {
1115                         snprintf(xstats_names[count].name,
1116                                  sizeof(xstats_names[count].name),
1117                                  "rx_q%u_%s", j, hns3_rx_queue_strings[i].name);
1118                         count++;
1119                 }
1120         }
1121
1122         for (j = 0; j < dev->data->nb_tx_queues; j++) {
1123                 for (i = 0; i < HNS3_NUM_TX_QUEUE_STATS; i++) {
1124                         snprintf(xstats_names[count].name,
1125                                  sizeof(xstats_names[count].name),
1126                                  "tx_q%u_%s", j, hns3_tx_queue_strings[i].name);
1127                         count++;
1128                 }
1129         }
1130
1131         return count;
1132 }
1133
1134 /*
1135  * Retrieve extended statistics of an Ethernet device.
1136  *
1137  * @param dev
1138  *   Pointer to Ethernet device.
1139  * @param ids
1140  *   A pointer to an ids array passed by application. This tells which
1141  *   statistics values function should retrieve. This parameter
1142  *   can be set to NULL if size is 0. In this case function will retrieve
1143  *   all avalible statistics.
1144  * @param values
1145  *   A pointer to a table to be filled with device statistics values.
1146  * @param size
1147  *   The size of the ids array (number of elements).
1148  * @return
1149  *   - A positive value lower or equal to size: success. The return value
1150  *     is the number of entries filled in the stats table.
1151  *   - A positive value higher than size: error, the given statistics table
1152  *     is too small. The return value corresponds to the size that should
1153  *     be given to succeed. The entries in the table are not valid and
1154  *     shall not be used by the caller.
1155  *   - 0 on no ids.
1156  */
1157 int
1158 hns3_dev_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
1159                           uint64_t *values, uint32_t size)
1160 {
1161         const uint32_t cnt_stats = hns3_xstats_calc_num(dev);
1162         struct hns3_adapter *hns = dev->data->dev_private;
1163         struct rte_eth_xstat *values_copy;
1164         struct hns3_hw *hw = &hns->hw;
1165         uint32_t count_value;
1166         uint64_t len;
1167         uint32_t i;
1168
1169         if (ids == NULL && values == NULL)
1170                 return cnt_stats;
1171
1172         if (ids == NULL)
1173                 if (size < cnt_stats)
1174                         return cnt_stats;
1175
1176         len = cnt_stats * sizeof(struct rte_eth_xstat);
1177         values_copy = rte_zmalloc("hns3_xstats_values", len, 0);
1178         if (values_copy == NULL) {
1179                 hns3_err(hw, "Failed to allocate %" PRIx64 " bytes needed "
1180                              "to store statistics values", len);
1181                 return -ENOMEM;
1182         }
1183
1184         count_value = hns3_dev_xstats_get(dev, values_copy, cnt_stats);
1185         if (count_value != cnt_stats) {
1186                 rte_free(values_copy);
1187                 return -EINVAL;
1188         }
1189
1190         if (ids == NULL && values != NULL) {
1191                 for (i = 0; i < cnt_stats; i++)
1192                         memcpy(&values[i], &values_copy[i].value,
1193                                sizeof(values[i]));
1194
1195                 rte_free(values_copy);
1196                 return cnt_stats;
1197         }
1198
1199         for (i = 0; i < size; i++) {
1200                 if (ids[i] >= cnt_stats) {
1201                         hns3_err(hw, "ids[%u] (%" PRIx64 ") is invalid, "
1202                                      "should < %u", i, ids[i], cnt_stats);
1203                         rte_free(values_copy);
1204                         return -EINVAL;
1205                 }
1206                 memcpy(&values[i], &values_copy[ids[i]].value,
1207                         sizeof(values[i]));
1208         }
1209
1210         rte_free(values_copy);
1211         return size;
1212 }
1213
1214 /*
1215  * Retrieve names of extended statistics of an Ethernet device.
1216  *
1217  * @param dev
1218  *   Pointer to Ethernet device.
1219  * @param xstats_names
1220  *   An rte_eth_xstat_name array of at least *size* elements to
1221  *   be filled. If set to NULL, the function returns the required number
1222  *   of elements.
1223  * @param ids
1224  *   IDs array given by app to retrieve specific statistics
1225  * @param size
1226  *   The size of the xstats_names array (number of elements).
1227  * @return
1228  *   - A positive value lower or equal to size: success. The return value
1229  *     is the number of entries filled in the stats table.
1230  *   - A positive value higher than size: error, the given statistics table
1231  *     is too small. The return value corresponds to the size that should
1232  *     be given to succeed. The entries in the table are not valid and
1233  *     shall not be used by the caller.
1234  */
1235 int
1236 hns3_dev_xstats_get_names_by_id(struct rte_eth_dev *dev,
1237                                 struct rte_eth_xstat_name *xstats_names,
1238                                 const uint64_t *ids, uint32_t size)
1239 {
1240         const uint32_t cnt_stats = hns3_xstats_calc_num(dev);
1241         struct hns3_adapter *hns = dev->data->dev_private;
1242         struct rte_eth_xstat_name *names_copy;
1243         struct hns3_hw *hw = &hns->hw;
1244         uint64_t len;
1245         uint32_t i;
1246
1247         if (xstats_names == NULL)
1248                 return cnt_stats;
1249
1250         if (ids == NULL) {
1251                 if (size < cnt_stats)
1252                         return cnt_stats;
1253
1254                 return hns3_dev_xstats_get_names(dev, xstats_names, cnt_stats);
1255         }
1256
1257         len = cnt_stats * sizeof(struct rte_eth_xstat_name);
1258         names_copy = rte_zmalloc("hns3_xstats_names", len, 0);
1259         if (names_copy == NULL) {
1260                 hns3_err(hw, "Failed to allocate %" PRIx64 " bytes needed "
1261                              "to store statistics names", len);
1262                 return -ENOMEM;
1263         }
1264
1265         (void)hns3_dev_xstats_get_names(dev, names_copy, cnt_stats);
1266
1267         for (i = 0; i < size; i++) {
1268                 if (ids[i] >= cnt_stats) {
1269                         hns3_err(hw, "ids[%u] (%" PRIx64 ") is invalid, "
1270                                      "should < %u", i, ids[i], cnt_stats);
1271                         rte_free(names_copy);
1272                         return -EINVAL;
1273                 }
1274                 snprintf(xstats_names[i].name, sizeof(xstats_names[i].name),
1275                          "%s", names_copy[ids[i]].name);
1276         }
1277
1278         rte_free(names_copy);
1279         return size;
1280 }
1281
1282 static void
1283 hns3_tqp_dfx_stats_clear(struct rte_eth_dev *dev)
1284 {
1285         struct hns3_rx_queue *rxq;
1286         struct hns3_tx_queue *txq;
1287         uint16_t i;
1288
1289         /* Clear Rx dfx stats */
1290         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1291                 rxq = dev->data->rx_queues[i];
1292                 if (rxq)
1293                         memset(&rxq->dfx_stats, 0,
1294                                sizeof(struct hns3_rx_dfx_stats));
1295         }
1296
1297         /* Clear Tx dfx stats */
1298         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1299                 txq = dev->data->tx_queues[i];
1300                 if (txq)
1301                         memset(&txq->dfx_stats, 0,
1302                                sizeof(struct hns3_tx_dfx_stats));
1303         }
1304 }
1305
1306 int
1307 hns3_dev_xstats_reset(struct rte_eth_dev *dev)
1308 {
1309         struct hns3_adapter *hns = dev->data->dev_private;
1310         struct hns3_pf *pf = &hns->pf;
1311         int ret;
1312
1313         /* Clear tqp stats */
1314         ret = hns3_stats_reset(dev);
1315         if (ret)
1316                 return ret;
1317
1318         hns3_tqp_basic_stats_clear(dev);
1319         hns3_tqp_dfx_stats_clear(dev);
1320
1321         /* Clear reset stats */
1322         memset(&hns->hw.reset.stats, 0, sizeof(struct hns3_reset_stats));
1323
1324         if (hns->is_vf)
1325                 return 0;
1326
1327         /* HW registers are cleared on read */
1328         ret = hns3_mac_stats_reset(dev);
1329         if (ret)
1330                 return ret;
1331
1332         /* Clear error stats */
1333         memset(&pf->abn_int_stats, 0, sizeof(struct hns3_err_msix_intr_stats));
1334
1335         return 0;
1336 }
1337
1338 int
1339 hns3_tqp_stats_init(struct hns3_hw *hw)
1340 {
1341         struct hns3_tqp_stats *tqp_stats = &hw->tqp_stats;
1342
1343         tqp_stats->rcb_rx_ring_pktnum = rte_zmalloc("hns3_rx_ring_pkt_num",
1344                                          sizeof(uint64_t) * hw->tqps_num, 0);
1345         if (tqp_stats->rcb_rx_ring_pktnum == NULL) {
1346                 hns3_err(hw, "failed to allocate rx_ring pkt_num.");
1347                 return -ENOMEM;
1348         }
1349
1350         tqp_stats->rcb_tx_ring_pktnum = rte_zmalloc("hns3_tx_ring_pkt_num",
1351                                          sizeof(uint64_t) * hw->tqps_num, 0);
1352         if (tqp_stats->rcb_tx_ring_pktnum == NULL) {
1353                 hns3_err(hw, "failed to allocate tx_ring pkt_num.");
1354                 rte_free(tqp_stats->rcb_rx_ring_pktnum);
1355                 tqp_stats->rcb_rx_ring_pktnum = NULL;
1356                 return -ENOMEM;
1357         }
1358
1359         return 0;
1360 }
1361
1362 void
1363 hns3_tqp_stats_uninit(struct hns3_hw *hw)
1364 {
1365         struct hns3_tqp_stats *tqp_stats = &hw->tqp_stats;
1366
1367         rte_free(tqp_stats->rcb_rx_ring_pktnum);
1368         tqp_stats->rcb_rx_ring_pktnum = NULL;
1369         rte_free(tqp_stats->rcb_tx_ring_pktnum);
1370         tqp_stats->rcb_tx_ring_pktnum = NULL;
1371 }
1372
1373 static void
1374 hns3_tqp_stats_clear(struct hns3_hw *hw)
1375 {
1376         struct hns3_tqp_stats *stats = &hw->tqp_stats;
1377
1378         stats->rcb_rx_ring_pktnum_rcd = 0;
1379         stats->rcb_tx_ring_pktnum_rcd = 0;
1380         memset(stats->rcb_rx_ring_pktnum, 0, sizeof(uint64_t) * hw->tqps_num);
1381         memset(stats->rcb_tx_ring_pktnum, 0, sizeof(uint64_t) * hw->tqps_num);
1382 }