87035e3709a3823ad0d439909c895d78f8872c6b
[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 /* The statistic of imissed packet */
328 static const struct hns3_xstats_name_offset hns3_imissed_stats_strings[] = {
329         {"RPU_DROP_CNT",
330                 HNS3_IMISSED_STATS_FIELD_OFFSET(rpu_rx_drop_cnt)},
331 };
332
333 #define HNS3_NUM_MAC_STATS (sizeof(hns3_mac_strings) / \
334         sizeof(hns3_mac_strings[0]))
335
336 #define HNS3_NUM_ERROR_INT_XSTATS (sizeof(hns3_error_int_stats_strings) / \
337         sizeof(hns3_error_int_stats_strings[0]))
338
339 #define HNS3_NUM_RESET_XSTATS (sizeof(hns3_reset_stats_strings) / \
340         sizeof(hns3_reset_stats_strings[0]))
341
342 #define HNS3_NUM_RX_BD_ERROR_XSTATS (sizeof(hns3_rx_bd_error_strings) / \
343         sizeof(hns3_rx_bd_error_strings[0]))
344
345 #define HNS3_NUM_RXQ_DFX_XSTATS (sizeof(hns3_rxq_dfx_stats_strings) / \
346         sizeof(hns3_rxq_dfx_stats_strings[0]))
347
348 #define HNS3_NUM_TXQ_DFX_XSTATS (sizeof(hns3_txq_dfx_stats_strings) / \
349         sizeof(hns3_txq_dfx_stats_strings[0]))
350
351 #define HNS3_NUM_RX_QUEUE_STATS (sizeof(hns3_rx_queue_strings) / \
352         sizeof(hns3_rx_queue_strings[0]))
353
354 #define HNS3_NUM_TX_QUEUE_STATS (sizeof(hns3_tx_queue_strings) / \
355         sizeof(hns3_tx_queue_strings[0]))
356
357 #define HNS3_NUM_RXQ_BASIC_STATS (sizeof(hns3_rxq_basic_stats_strings) / \
358         sizeof(hns3_rxq_basic_stats_strings[0]))
359
360 #define HNS3_NUM_TXQ_BASIC_STATS (sizeof(hns3_txq_basic_stats_strings) / \
361         sizeof(hns3_txq_basic_stats_strings[0]))
362
363 #define HNS3_NUM_IMISSED_XSTATS (sizeof(hns3_imissed_stats_strings) / \
364         sizeof(hns3_imissed_stats_strings[0]))
365
366 #define HNS3_FIX_NUM_STATS (HNS3_NUM_MAC_STATS + HNS3_NUM_ERROR_INT_XSTATS + \
367                             HNS3_NUM_RESET_XSTATS + HNS3_NUM_IMISSED_XSTATS)
368
369 static void hns3_tqp_stats_clear(struct hns3_hw *hw);
370 static void hns3_tqp_basic_stats_clear(struct rte_eth_dev *dev);
371
372 /*
373  * Query all the MAC statistics data of Network ICL command ,opcode id: 0x0034.
374  * This command is used before send 'query_mac_stat command', the descriptor
375  * number of 'query_mac_stat command' must match with reg_num in this command.
376  * @praram hw
377  *   Pointer to structure hns3_hw.
378  * @return
379  *   0 on success.
380  */
381 static int
382 hns3_update_mac_stats(struct hns3_hw *hw, const uint32_t desc_num)
383 {
384         uint64_t *data = (uint64_t *)(&hw->mac_stats);
385         struct hns3_cmd_desc *desc;
386         uint64_t *desc_data;
387         uint16_t i, k, n;
388         int ret;
389
390         desc = rte_malloc("hns3_mac_desc",
391                           desc_num * sizeof(struct hns3_cmd_desc), 0);
392         if (desc == NULL) {
393                 hns3_err(hw, "Mac_update_stats alloced desc malloc fail");
394                 return -ENOMEM;
395         }
396
397         hns3_cmd_setup_basic_desc(desc, HNS3_OPC_STATS_MAC_ALL, true);
398         ret = hns3_cmd_send(hw, desc, desc_num);
399         if (ret) {
400                 hns3_err(hw, "Update complete MAC pkt stats fail : %d", ret);
401                 rte_free(desc);
402                 return ret;
403         }
404
405         for (i = 0; i < desc_num; i++) {
406                 /* For special opcode 0034, only the first desc has the head */
407                 if (i == 0) {
408                         desc_data = (uint64_t *)(&desc[i].data[0]);
409                         n = HNS3_RD_FIRST_STATS_NUM;
410                 } else {
411                         desc_data = (uint64_t *)(&desc[i]);
412                         n = HNS3_RD_OTHER_STATS_NUM;
413                 }
414
415                 for (k = 0; k < n; k++) {
416                         *data += rte_le_to_cpu_64(*desc_data);
417                         data++;
418                         desc_data++;
419                 }
420         }
421         rte_free(desc);
422
423         return 0;
424 }
425
426 /*
427  * Query Mac stat reg num command ,opcode id: 0x0033.
428  * This command is used before send 'query_mac_stat command', the descriptor
429  * number of 'query_mac_stat command' must match with reg_num in this command.
430  * @praram rte_stats
431  *   Pointer to structure rte_eth_stats.
432  * @return
433  *   0 on success.
434  */
435 static int
436 hns3_mac_query_reg_num(struct rte_eth_dev *dev, uint32_t *desc_num)
437 {
438         struct hns3_adapter *hns = dev->data->dev_private;
439         struct hns3_hw *hw = &hns->hw;
440         struct hns3_cmd_desc desc;
441         uint32_t *desc_data;
442         uint32_t reg_num;
443         int ret;
444
445         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_MAC_REG_NUM, true);
446         ret = hns3_cmd_send(hw, &desc, 1);
447         if (ret)
448                 return ret;
449
450         /*
451          * The num of MAC statistics registers that are provided by IMP in this
452          * version.
453          */
454         desc_data = (uint32_t *)(&desc.data[0]);
455         reg_num = rte_le_to_cpu_32(*desc_data);
456
457         /*
458          * The descriptor number of 'query_additional_mac_stat command' is
459          * '1 + (reg_num-3)/4 + ((reg_num-3)%4 !=0)';
460          * This value is 83 in this version
461          */
462         *desc_num = 1 + ((reg_num - 3) >> 2) +
463                     (uint32_t)(((reg_num - 3) & 0x3) ? 1 : 0);
464
465         return 0;
466 }
467
468 static int
469 hns3_query_update_mac_stats(struct rte_eth_dev *dev)
470 {
471         struct hns3_adapter *hns = dev->data->dev_private;
472         struct hns3_hw *hw = &hns->hw;
473         uint32_t desc_num;
474         int ret;
475
476         ret = hns3_mac_query_reg_num(dev, &desc_num);
477         if (ret == 0)
478                 ret = hns3_update_mac_stats(hw, desc_num);
479         else
480                 hns3_err(hw, "Query mac reg num fail : %d", ret);
481         return ret;
482 }
483
484 /* Get tqp stats from register */
485 static int
486 hns3_update_tqp_stats(struct hns3_hw *hw)
487 {
488         struct hns3_tqp_stats *stats = &hw->tqp_stats;
489         struct hns3_cmd_desc desc;
490         uint64_t cnt;
491         uint16_t i;
492         int ret;
493
494         for (i = 0; i < hw->tqps_num; i++) {
495                 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_RX_STATUS,
496                                           true);
497
498                 desc.data[0] = rte_cpu_to_le_32((uint32_t)i);
499                 ret = hns3_cmd_send(hw, &desc, 1);
500                 if (ret) {
501                         hns3_err(hw, "Failed to query RX No.%u queue stat: %d",
502                                  i, ret);
503                         return ret;
504                 }
505                 cnt = rte_le_to_cpu_32(desc.data[1]);
506                 stats->rcb_rx_ring_pktnum_rcd += cnt;
507                 stats->rcb_rx_ring_pktnum[i] += cnt;
508
509                 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_TX_STATUS,
510                                           true);
511
512                 desc.data[0] = rte_cpu_to_le_32((uint32_t)i);
513                 ret = hns3_cmd_send(hw, &desc, 1);
514                 if (ret) {
515                         hns3_err(hw, "Failed to query TX No.%u queue stat: %d",
516                                  i, ret);
517                         return ret;
518                 }
519                 cnt = rte_le_to_cpu_32(desc.data[1]);
520                 stats->rcb_tx_ring_pktnum_rcd += cnt;
521                 stats->rcb_tx_ring_pktnum[i] += cnt;
522         }
523
524         return 0;
525 }
526
527 static int
528 hns3_update_rpu_drop_stats(struct hns3_hw *hw)
529 {
530         struct hns3_rx_missed_stats *stats = &hw->imissed_stats;
531         struct hns3_query_rpu_cmd *req;
532         struct hns3_cmd_desc desc;
533         uint64_t cnt;
534         uint32_t tc_num;
535         int ret;
536
537         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_DFX_RPU_REG_0, true);
538         req = (struct hns3_query_rpu_cmd *)desc.data;
539
540         /*
541          * tc_num is 0, means rpu stats of all TC channels will be
542          * get from firmware
543          */
544         tc_num = 0;
545         req->tc_queue_num = rte_cpu_to_le_32(tc_num);
546         ret = hns3_cmd_send(hw, &desc, 1);
547         if (ret) {
548                 hns3_err(hw, "failed to query RPU stats: %d", ret);
549                 return ret;
550         }
551
552         cnt = rte_le_to_cpu_32(req->rpu_rx_pkt_drop_cnt);
553         stats->rpu_rx_drop_cnt += cnt;
554
555         return 0;
556 }
557
558 int
559 hns3_update_imissed_stats(struct hns3_hw *hw, bool is_clear)
560 {
561         int ret;
562
563         ret = hns3_update_rpu_drop_stats(hw);
564         if (ret)
565                 return ret;
566
567         if (is_clear)
568                 memset(&hw->imissed_stats, 0, sizeof(hw->imissed_stats));
569
570         return 0;
571 }
572
573 /*
574  * Query tqp tx queue statistics ,opcode id: 0x0B03.
575  * Query tqp rx queue statistics ,opcode id: 0x0B13.
576  * Get all statistics of a port.
577  * @param eth_dev
578  *   Pointer to Ethernet device.
579  * @praram rte_stats
580  *   Pointer to structure rte_eth_stats.
581  * @return
582  *   0 on success.
583  */
584 int
585 hns3_stats_get(struct rte_eth_dev *eth_dev, struct rte_eth_stats *rte_stats)
586 {
587         struct hns3_adapter *hns = eth_dev->data->dev_private;
588         struct hns3_hw *hw = &hns->hw;
589         struct hns3_rx_missed_stats *imissed_stats = &hw->imissed_stats;
590         struct hns3_tqp_stats *stats = &hw->tqp_stats;
591         struct hns3_rx_queue *rxq;
592         uint64_t cnt;
593         uint16_t i;
594         int ret;
595
596         /* Update tqp stats by read register */
597         ret = hns3_update_tqp_stats(hw);
598         if (ret) {
599                 hns3_err(hw, "Update tqp stats fail : %d", ret);
600                 return ret;
601         }
602
603         if (!hns->is_vf) {
604                 /* Update imissed stats */
605                 ret = hns3_update_imissed_stats(hw, false);
606                 if (ret) {
607                         hns3_err(hw, "update imissed stats failed, ret = %d",
608                                  ret);
609                         return ret;
610                 }
611
612                 rte_stats->imissed = imissed_stats->rpu_rx_drop_cnt;
613         }
614
615         /* Get the error stats and bytes of received packets */
616         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
617                 rxq = eth_dev->data->rx_queues[i];
618                 if (rxq) {
619                         cnt = rxq->err_stats.l2_errors +
620                                 rxq->err_stats.pkt_len_errors;
621                         rte_stats->ierrors += cnt;
622
623                         rte_stats->ibytes += rxq->basic_stats.bytes;
624                 }
625         }
626
627         /* Get the bytes of received packets */
628         struct hns3_tx_queue *txq;
629         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
630                 txq = eth_dev->data->tx_queues[i];
631                 if (txq)
632                         rte_stats->obytes += txq->basic_stats.bytes;
633         }
634
635         rte_stats->oerrors = 0;
636         /*
637          * If HW statistics are reset by stats_reset, but a lot of residual
638          * packets exist in the hardware queue and these packets are error
639          * packets, flip overflow may occurred. So return 0 in this case.
640          */
641         rte_stats->ipackets =
642                 stats->rcb_rx_ring_pktnum_rcd > rte_stats->ierrors ?
643                 stats->rcb_rx_ring_pktnum_rcd - rte_stats->ierrors : 0;
644         rte_stats->opackets  = stats->rcb_tx_ring_pktnum_rcd -
645                 rte_stats->oerrors;
646         rte_stats->rx_nombuf = eth_dev->data->rx_mbuf_alloc_failed;
647
648         return 0;
649 }
650
651 int
652 hns3_stats_reset(struct rte_eth_dev *eth_dev)
653 {
654         struct hns3_adapter *hns = eth_dev->data->dev_private;
655         struct hns3_hw *hw = &hns->hw;
656         struct hns3_cmd_desc desc_reset;
657         struct hns3_rx_queue *rxq;
658         uint16_t i;
659         int ret;
660
661         /*
662          * Note: Reading hardware statistics of rx/tx queue packet number
663          * will clear them.
664          */
665         for (i = 0; i < hw->tqps_num; i++) {
666                 hns3_cmd_setup_basic_desc(&desc_reset, HNS3_OPC_QUERY_RX_STATUS,
667                                           true);
668                 desc_reset.data[0] = rte_cpu_to_le_32((uint32_t)i);
669                 ret = hns3_cmd_send(hw, &desc_reset, 1);
670                 if (ret) {
671                         hns3_err(hw, "Failed to reset RX No.%u queue stat: %d",
672                                  i, ret);
673                         return ret;
674                 }
675
676                 hns3_cmd_setup_basic_desc(&desc_reset, HNS3_OPC_QUERY_TX_STATUS,
677                                           true);
678                 desc_reset.data[0] = rte_cpu_to_le_32((uint32_t)i);
679                 ret = hns3_cmd_send(hw, &desc_reset, 1);
680                 if (ret) {
681                         hns3_err(hw, "Failed to reset TX No.%u queue stat: %d",
682                                  i, ret);
683                         return ret;
684                 }
685         }
686
687         if (!hns->is_vf) {
688                 /*
689                  * Note: Reading hardware statistics of imissed registers will
690                  * clear them.
691                  */
692                 ret = hns3_update_imissed_stats(hw, true);
693                 if (ret) {
694                         hns3_err(hw, "clear imissed stats failed, ret = %d",
695                                  ret);
696                         return ret;
697                 }
698         }
699
700         /*
701          * Clear soft stats of rx error packet which will be dropped
702          * in driver.
703          */
704         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
705                 rxq = eth_dev->data->rx_queues[i];
706                 if (rxq) {
707                         rxq->err_stats.pkt_len_errors = 0;
708                         rxq->err_stats.l2_errors = 0;
709                 }
710         }
711
712         /*
713          * 'packets' in hns3_tx_basic_stats and hns3_rx_basic_stats come
714          * from hw->tqp_stats. And clearing tqp stats is like clearing
715          * their source.
716          */
717         hns3_tqp_stats_clear(hw);
718         hns3_tqp_basic_stats_clear(eth_dev);
719
720         return 0;
721 }
722
723 static int
724 hns3_mac_stats_reset(__rte_unused struct rte_eth_dev *dev)
725 {
726         struct hns3_adapter *hns = dev->data->dev_private;
727         struct hns3_hw *hw = &hns->hw;
728         struct hns3_mac_stats *mac_stats = &hw->mac_stats;
729         int ret;
730
731         ret = hns3_query_update_mac_stats(dev);
732         if (ret) {
733                 hns3_err(hw, "Clear Mac stats fail : %d", ret);
734                 return ret;
735         }
736
737         memset(mac_stats, 0, sizeof(struct hns3_mac_stats));
738
739         return 0;
740 }
741
742 /* This function calculates the number of xstats based on the current config */
743 static int
744 hns3_xstats_calc_num(struct rte_eth_dev *dev)
745 {
746 #define HNS3_PF_VF_RX_COMM_STATS_NUM    (HNS3_NUM_RX_BD_ERROR_XSTATS + \
747                                          HNS3_NUM_RXQ_DFX_XSTATS + \
748                                          HNS3_NUM_RX_QUEUE_STATS + \
749                                          HNS3_NUM_RXQ_BASIC_STATS)
750 #define HNS3_PF_VF_TX_COMM_STATS_NUM    (HNS3_NUM_TXQ_DFX_XSTATS + \
751                                          HNS3_NUM_TX_QUEUE_STATS + \
752                                          HNS3_NUM_TXQ_BASIC_STATS)
753
754         struct hns3_adapter *hns = dev->data->dev_private;
755         uint16_t nb_rx_q = dev->data->nb_rx_queues;
756         uint16_t nb_tx_q = dev->data->nb_tx_queues;
757         int rx_comm_stats_num = nb_rx_q * HNS3_PF_VF_RX_COMM_STATS_NUM;
758         int tx_comm_stats_num = nb_tx_q * HNS3_PF_VF_TX_COMM_STATS_NUM;
759
760         if (hns->is_vf)
761                 return rx_comm_stats_num + tx_comm_stats_num +
762                         HNS3_NUM_RESET_XSTATS;
763         else
764                 return rx_comm_stats_num + tx_comm_stats_num +
765                         HNS3_FIX_NUM_STATS;
766 }
767
768 static void
769 hns3_queue_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
770                      int *count)
771 {
772         struct hns3_adapter *hns = dev->data->dev_private;
773         struct hns3_hw *hw = &hns->hw;
774         uint32_t reg_offset;
775         uint16_t i, j;
776
777         /* Get rx queue stats */
778         for (j = 0; j < dev->data->nb_rx_queues; j++) {
779                 for (i = 0; i < HNS3_NUM_RX_QUEUE_STATS; i++) {
780                         reg_offset = hns3_get_tqp_reg_offset(j);
781                         xstats[*count].value = hns3_read_dev(hw,
782                                 reg_offset + hns3_rx_queue_strings[i].offset);
783                         xstats[*count].id = *count;
784                         (*count)++;
785                 }
786         }
787
788         /* Get tx queue stats */
789         for (j = 0; j < dev->data->nb_tx_queues; j++) {
790                 for (i = 0; i < HNS3_NUM_TX_QUEUE_STATS; i++) {
791                         reg_offset = hns3_get_tqp_reg_offset(j);
792                         xstats[*count].value = hns3_read_dev(hw,
793                                 reg_offset + hns3_tx_queue_strings[i].offset);
794                         xstats[*count].id = *count;
795                         (*count)++;
796                 }
797         }
798 }
799
800 void
801 hns3_error_int_stats_add(struct hns3_adapter *hns, const char *err)
802 {
803         struct hns3_pf *pf = &hns->pf;
804         uint16_t i;
805         char *addr;
806
807         for (i = 0; i < HNS3_NUM_ERROR_INT_XSTATS; i++) {
808                 if (strcmp(hns3_error_int_stats_strings[i].name, err) == 0) {
809                         addr = (char *)&pf->abn_int_stats +
810                                 hns3_error_int_stats_strings[i].offset;
811                         *(uint64_t *)addr += 1;
812                         break;
813                 }
814         }
815 }
816
817 static void
818 hns3_rxq_dfx_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
819                        int *count)
820 {
821         struct hns3_rx_dfx_stats *dfx_stats;
822         struct hns3_rx_queue *rxq;
823         uint16_t i, j;
824         char *val;
825
826         for (i = 0; i < dev->data->nb_rx_queues; i++) {
827                 rxq = (struct hns3_rx_queue *)dev->data->rx_queues[i];
828                 if (rxq == NULL)
829                         continue;
830
831                 dfx_stats = &rxq->dfx_stats;
832                 for (j = 0; j < HNS3_NUM_RXQ_DFX_XSTATS; j++) {
833                         val = (char *)dfx_stats +
834                                 hns3_rxq_dfx_stats_strings[j].offset;
835                         xstats[*count].value = *(uint64_t *)val;
836                         xstats[*count].id = *count;
837                         (*count)++;
838                 }
839         }
840 }
841
842 static void
843 hns3_txq_dfx_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
844                        int *count)
845 {
846         struct hns3_tx_dfx_stats *dfx_stats;
847         struct hns3_tx_queue *txq;
848         uint16_t i, j;
849         char *val;
850
851         for (i = 0; i < dev->data->nb_tx_queues; i++) {
852                 txq = (struct hns3_tx_queue *)dev->data->tx_queues[i];
853                 if (txq == NULL)
854                         continue;
855
856                 dfx_stats = &txq->dfx_stats;
857                 for (j = 0; j < HNS3_NUM_TXQ_DFX_XSTATS; j++) {
858                         val = (char *)dfx_stats +
859                                 hns3_txq_dfx_stats_strings[j].offset;
860                         xstats[*count].value = *(uint64_t *)val;
861                         xstats[*count].id = *count;
862                         (*count)++;
863                 }
864         }
865 }
866
867 static void
868 hns3_tqp_dfx_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
869                        int *count)
870 {
871         hns3_rxq_dfx_stats_get(dev, xstats, count);
872         hns3_txq_dfx_stats_get(dev, xstats, count);
873 }
874
875 static void
876 hns3_rxq_basic_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
877                          int *count)
878 {
879         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
880         struct hns3_tqp_stats *stats = &hw->tqp_stats;
881         struct hns3_rx_basic_stats *rxq_stats;
882         struct hns3_rx_queue *rxq;
883         uint16_t i, j;
884         char *val;
885
886         for (i = 0; i < dev->data->nb_rx_queues; i++) {
887                 rxq = dev->data->rx_queues[i];
888                 if (rxq == NULL)
889                         continue;
890
891                 rxq_stats = &rxq->basic_stats;
892                 rxq_stats->errors = rxq->err_stats.l2_errors +
893                                         rxq->err_stats.pkt_len_errors;
894                 /*
895                  * If HW statistics are reset by stats_reset, but a lot of
896                  * residual packets exist in the hardware queue and these
897                  * packets are error packets, flip overflow may occurred.
898                  * So return 0 in this case.
899                  */
900                 rxq_stats->packets =
901                         stats->rcb_rx_ring_pktnum[i] > rxq_stats->errors ?
902                         stats->rcb_rx_ring_pktnum[i] - rxq_stats->errors : 0;
903                 for (j = 0; j < HNS3_NUM_RXQ_BASIC_STATS; j++) {
904                         val = (char *)rxq_stats +
905                                 hns3_rxq_basic_stats_strings[j].offset;
906                         xstats[*count].value = *(uint64_t *)val;
907                         xstats[*count].id = *count;
908                         (*count)++;
909                 }
910         }
911 }
912
913 static void
914 hns3_txq_basic_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
915                          int *count)
916 {
917         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
918         struct hns3_tqp_stats *stats = &hw->tqp_stats;
919         struct hns3_tx_basic_stats *txq_stats;
920         struct hns3_tx_queue *txq;
921         uint16_t i, j;
922         char *val;
923
924         for (i = 0; i < dev->data->nb_tx_queues; i++) {
925                 txq = dev->data->tx_queues[i];
926                 if (txq == NULL)
927                         continue;
928
929                 txq_stats = &txq->basic_stats;
930                 txq_stats->packets = stats->rcb_tx_ring_pktnum[i];
931
932                 for (j = 0; j < HNS3_NUM_TXQ_BASIC_STATS; j++) {
933                         val = (char *)txq_stats +
934                                 hns3_txq_basic_stats_strings[j].offset;
935                         xstats[*count].value = *(uint64_t *)val;
936                         xstats[*count].id = *count;
937                         (*count)++;
938                 }
939         }
940 }
941
942 static int
943 hns3_tqp_basic_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
944                          int *count)
945 {
946         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
947         int ret;
948
949         /* Update tqp stats by read register */
950         ret = hns3_update_tqp_stats(hw);
951         if (ret) {
952                 hns3_err(hw, "Update tqp stats fail, ret = %d.", ret);
953                 return ret;
954         }
955
956         hns3_rxq_basic_stats_get(dev, xstats, count);
957         hns3_txq_basic_stats_get(dev, xstats, count);
958
959         return 0;
960 }
961
962 /*
963  * The function is only called by hns3_dev_xstats_reset to clear
964  * basic stats of per-queue. TQP stats are all cleared in hns3_stats_reset
965  * which is called before this function.
966  *
967  * @param dev
968  *   Pointer to Ethernet device.
969  */
970 static void
971 hns3_tqp_basic_stats_clear(struct rte_eth_dev *dev)
972 {
973         struct hns3_tx_queue *txq;
974         struct hns3_rx_queue *rxq;
975         uint16_t i;
976
977         for (i = 0; i < dev->data->nb_rx_queues; i++) {
978                 rxq = dev->data->rx_queues[i];
979                 if (rxq)
980                         memset(&rxq->basic_stats, 0,
981                                sizeof(struct hns3_rx_basic_stats));
982         }
983
984         for (i = 0; i < dev->data->nb_tx_queues; i++) {
985                 txq = dev->data->tx_queues[i];
986                 if (txq)
987                         memset(&txq->basic_stats, 0,
988                                sizeof(struct hns3_tx_basic_stats));
989         }
990 }
991
992 /*
993  * Retrieve extended(tqp | Mac) statistics of an Ethernet device.
994  * @param dev
995  *   Pointer to Ethernet device.
996  * @praram xstats
997  *   A pointer to a table of structure of type *rte_eth_xstat*
998  *   to be filled with device statistics ids and values.
999  *   This parameter can be set to NULL if n is 0.
1000  * @param n
1001  *   The size of the xstats array (number of elements).
1002  * @return
1003  *   0 on fail, count(The size of the statistics elements) on success.
1004  */
1005 int
1006 hns3_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
1007                     unsigned int n)
1008 {
1009         struct hns3_adapter *hns = dev->data->dev_private;
1010         struct hns3_pf *pf = &hns->pf;
1011         struct hns3_hw *hw = &hns->hw;
1012         struct hns3_rx_missed_stats *imissed_stats = &hw->imissed_stats;
1013         struct hns3_mac_stats *mac_stats = &hw->mac_stats;
1014         struct hns3_reset_stats *reset_stats = &hw->reset.stats;
1015         struct hns3_rx_bd_errors_stats *rx_err_stats;
1016         struct hns3_rx_queue *rxq;
1017         uint16_t i, j;
1018         char *addr;
1019         int count;
1020         int ret;
1021
1022         if (xstats == NULL)
1023                 return 0;
1024
1025         count = hns3_xstats_calc_num(dev);
1026         if ((int)n < count)
1027                 return count;
1028
1029         count = 0;
1030
1031         ret = hns3_tqp_basic_stats_get(dev, xstats, &count);
1032         if (ret < 0)
1033                 return ret;
1034
1035         if (!hns->is_vf) {
1036                 /* Update Mac stats */
1037                 ret = hns3_query_update_mac_stats(dev);
1038                 if (ret < 0) {
1039                         hns3_err(hw, "Update Mac stats fail : %d", ret);
1040                         return ret;
1041                 }
1042
1043                 /* Get MAC stats from hw->hw_xstats.mac_stats struct */
1044                 for (i = 0; i < HNS3_NUM_MAC_STATS; i++) {
1045                         addr = (char *)mac_stats + hns3_mac_strings[i].offset;
1046                         xstats[count].value = *(uint64_t *)addr;
1047                         xstats[count].id = count;
1048                         count++;
1049                 }
1050
1051                 ret = hns3_update_imissed_stats(hw, false);
1052                 if (ret) {
1053                         hns3_err(hw, "update imissed stats failed, ret = %d",
1054                                  ret);
1055                         return ret;
1056                 }
1057
1058                 for (i = 0; i < HNS3_NUM_IMISSED_XSTATS; i++) {
1059                         addr = (char *)imissed_stats +
1060                                 hns3_imissed_stats_strings[i].offset;
1061                         xstats[count].value = *(uint64_t *)addr;
1062                         xstats[count].id = count;
1063                         count++;
1064                 }
1065
1066                 for (i = 0; i < HNS3_NUM_ERROR_INT_XSTATS; i++) {
1067                         addr = (char *)&pf->abn_int_stats +
1068                                hns3_error_int_stats_strings[i].offset;
1069                         xstats[count].value = *(uint64_t *)addr;
1070                         xstats[count].id = count;
1071                         count++;
1072                 }
1073         }
1074
1075         /* Get the reset stat */
1076         for (i = 0; i < HNS3_NUM_RESET_XSTATS; i++) {
1077                 addr = (char *)reset_stats + hns3_reset_stats_strings[i].offset;
1078                 xstats[count].value = *(uint64_t *)addr;
1079                 xstats[count].id = count;
1080                 count++;
1081         }
1082
1083         /* Get the Rx BD errors stats */
1084         for (j = 0; j < dev->data->nb_rx_queues; j++) {
1085                 for (i = 0; i < HNS3_NUM_RX_BD_ERROR_XSTATS; i++) {
1086                         rxq = dev->data->rx_queues[j];
1087                         if (rxq) {
1088                                 rx_err_stats = &rxq->err_stats;
1089                                 addr = (char *)rx_err_stats +
1090                                         hns3_rx_bd_error_strings[i].offset;
1091                                 xstats[count].value = *(uint64_t *)addr;
1092                                 xstats[count].id = count;
1093                                 count++;
1094                         }
1095                 }
1096         }
1097
1098         hns3_tqp_dfx_stats_get(dev, xstats, &count);
1099         hns3_queue_stats_get(dev, xstats, &count);
1100
1101         return count;
1102 }
1103
1104 static void
1105 hns3_tqp_basic_stats_name_get(struct rte_eth_dev *dev,
1106                               struct rte_eth_xstat_name *xstats_names,
1107                               uint32_t *count)
1108 {
1109         uint16_t i, j;
1110
1111         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1112                 for (j = 0; j < HNS3_NUM_RXQ_BASIC_STATS; j++) {
1113                         snprintf(xstats_names[*count].name,
1114                                  sizeof(xstats_names[*count].name),
1115                                  "rx_q%u_%s", i,
1116                                  hns3_rxq_basic_stats_strings[j].name);
1117                         (*count)++;
1118                 }
1119         }
1120         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1121                 for (j = 0; j < HNS3_NUM_TXQ_BASIC_STATS; j++) {
1122                         snprintf(xstats_names[*count].name,
1123                                  sizeof(xstats_names[*count].name),
1124                                  "tx_q%u_%s", i,
1125                                  hns3_txq_basic_stats_strings[j].name);
1126                         (*count)++;
1127                 }
1128         }
1129 }
1130
1131 static void
1132 hns3_tqp_dfx_stats_name_get(struct rte_eth_dev *dev,
1133                             struct rte_eth_xstat_name *xstats_names,
1134                             uint32_t *count)
1135 {
1136         uint16_t i, j;
1137
1138         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1139                 for (j = 0; j < HNS3_NUM_RXQ_DFX_XSTATS; j++) {
1140                         snprintf(xstats_names[*count].name,
1141                                  sizeof(xstats_names[*count].name),
1142                                  "rx_q%u_%s", i,
1143                                  hns3_rxq_dfx_stats_strings[j].name);
1144                         (*count)++;
1145                 }
1146         }
1147
1148         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1149                 for (j = 0; j < HNS3_NUM_TXQ_DFX_XSTATS; j++) {
1150                         snprintf(xstats_names[*count].name,
1151                                  sizeof(xstats_names[*count].name),
1152                                  "tx_q%u_%s", i,
1153                                  hns3_txq_dfx_stats_strings[j].name);
1154                         (*count)++;
1155                 }
1156         }
1157 }
1158
1159 /*
1160  * Retrieve names of extended statistics of an Ethernet device.
1161  *
1162  * There is an assumption that 'xstat_names' and 'xstats' arrays are matched
1163  * by array index:
1164  *  xstats_names[i].name => xstats[i].value
1165  *
1166  * And the array index is same with id field of 'struct rte_eth_xstat':
1167  *  xstats[i].id == i
1168  *
1169  * This assumption makes key-value pair matching less flexible but simpler.
1170  *
1171  * @param dev
1172  *   Pointer to Ethernet device.
1173  * @param xstats_names
1174  *   An rte_eth_xstat_name array of at least *size* elements to
1175  *   be filled. If set to NULL, the function returns the required number
1176  *   of elements.
1177  * @param size
1178  *   The size of the xstats_names array (number of elements).
1179  * @return
1180  *   - A positive value lower or equal to size: success. The return value
1181  *     is the number of entries filled in the stats table.
1182  */
1183 int
1184 hns3_dev_xstats_get_names(struct rte_eth_dev *dev,
1185                           struct rte_eth_xstat_name *xstats_names,
1186                           __rte_unused unsigned int size)
1187 {
1188         struct hns3_adapter *hns = dev->data->dev_private;
1189         int cnt_stats = hns3_xstats_calc_num(dev);
1190         uint32_t count = 0;
1191         uint16_t i, j;
1192
1193         if (xstats_names == NULL)
1194                 return cnt_stats;
1195
1196         hns3_tqp_basic_stats_name_get(dev, xstats_names, &count);
1197
1198         /* Note: size limited checked in rte_eth_xstats_get_names() */
1199         if (!hns->is_vf) {
1200                 /* Get MAC name from hw->hw_xstats.mac_stats struct */
1201                 for (i = 0; i < HNS3_NUM_MAC_STATS; i++) {
1202                         snprintf(xstats_names[count].name,
1203                                  sizeof(xstats_names[count].name),
1204                                  "%s", hns3_mac_strings[i].name);
1205                         count++;
1206                 }
1207
1208                 for (i = 0; i < HNS3_NUM_IMISSED_XSTATS; i++) {
1209                         snprintf(xstats_names[count].name,
1210                                  sizeof(xstats_names[count].name),
1211                                  "%s", hns3_imissed_stats_strings[i].name);
1212                         count++;
1213                 }
1214
1215                 for (i = 0; i < HNS3_NUM_ERROR_INT_XSTATS; i++) {
1216                         snprintf(xstats_names[count].name,
1217                                  sizeof(xstats_names[count].name),
1218                                  "%s", hns3_error_int_stats_strings[i].name);
1219                         count++;
1220                 }
1221         }
1222         for (i = 0; i < HNS3_NUM_RESET_XSTATS; i++) {
1223                 snprintf(xstats_names[count].name,
1224                          sizeof(xstats_names[count].name),
1225                          "%s", hns3_reset_stats_strings[i].name);
1226                 count++;
1227         }
1228
1229         for (j = 0; j < dev->data->nb_rx_queues; j++) {
1230                 for (i = 0; i < HNS3_NUM_RX_BD_ERROR_XSTATS; i++) {
1231                         snprintf(xstats_names[count].name,
1232                                  sizeof(xstats_names[count].name),
1233                                  "rx_q%u_%s", j,
1234                                  hns3_rx_bd_error_strings[i].name);
1235                         count++;
1236                 }
1237         }
1238
1239         hns3_tqp_dfx_stats_name_get(dev, xstats_names, &count);
1240
1241         for (j = 0; j < dev->data->nb_rx_queues; j++) {
1242                 for (i = 0; i < HNS3_NUM_RX_QUEUE_STATS; i++) {
1243                         snprintf(xstats_names[count].name,
1244                                  sizeof(xstats_names[count].name),
1245                                  "rx_q%u_%s", j, hns3_rx_queue_strings[i].name);
1246                         count++;
1247                 }
1248         }
1249
1250         for (j = 0; j < dev->data->nb_tx_queues; j++) {
1251                 for (i = 0; i < HNS3_NUM_TX_QUEUE_STATS; i++) {
1252                         snprintf(xstats_names[count].name,
1253                                  sizeof(xstats_names[count].name),
1254                                  "tx_q%u_%s", j, hns3_tx_queue_strings[i].name);
1255                         count++;
1256                 }
1257         }
1258
1259         return count;
1260 }
1261
1262 /*
1263  * Retrieve extended statistics of an Ethernet device.
1264  *
1265  * @param dev
1266  *   Pointer to Ethernet device.
1267  * @param ids
1268  *   A pointer to an ids array passed by application. This tells which
1269  *   statistics values function should retrieve. This parameter
1270  *   can be set to NULL if size is 0. In this case function will retrieve
1271  *   all avalible statistics.
1272  * @param values
1273  *   A pointer to a table to be filled with device statistics values.
1274  * @param size
1275  *   The size of the ids array (number of elements).
1276  * @return
1277  *   - A positive value lower or equal to size: success. The return value
1278  *     is the number of entries filled in the stats table.
1279  *   - A positive value higher than size: error, the given statistics table
1280  *     is too small. The return value corresponds to the size that should
1281  *     be given to succeed. The entries in the table are not valid and
1282  *     shall not be used by the caller.
1283  *   - 0 on no ids.
1284  */
1285 int
1286 hns3_dev_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
1287                           uint64_t *values, uint32_t size)
1288 {
1289         const uint32_t cnt_stats = hns3_xstats_calc_num(dev);
1290         struct hns3_adapter *hns = dev->data->dev_private;
1291         struct rte_eth_xstat *values_copy;
1292         struct hns3_hw *hw = &hns->hw;
1293         uint32_t count_value;
1294         uint64_t len;
1295         uint32_t i;
1296
1297         if (ids == NULL && values == NULL)
1298                 return cnt_stats;
1299
1300         if (ids == NULL)
1301                 if (size < cnt_stats)
1302                         return cnt_stats;
1303
1304         len = cnt_stats * sizeof(struct rte_eth_xstat);
1305         values_copy = rte_zmalloc("hns3_xstats_values", len, 0);
1306         if (values_copy == NULL) {
1307                 hns3_err(hw, "Failed to allocate %" PRIx64 " bytes needed "
1308                              "to store statistics values", len);
1309                 return -ENOMEM;
1310         }
1311
1312         count_value = hns3_dev_xstats_get(dev, values_copy, cnt_stats);
1313         if (count_value != cnt_stats) {
1314                 rte_free(values_copy);
1315                 return -EINVAL;
1316         }
1317
1318         if (ids == NULL && values != NULL) {
1319                 for (i = 0; i < cnt_stats; i++)
1320                         memcpy(&values[i], &values_copy[i].value,
1321                                sizeof(values[i]));
1322
1323                 rte_free(values_copy);
1324                 return cnt_stats;
1325         }
1326
1327         for (i = 0; i < size; i++) {
1328                 if (ids[i] >= cnt_stats) {
1329                         hns3_err(hw, "ids[%u] (%" PRIx64 ") is invalid, "
1330                                      "should < %u", i, ids[i], cnt_stats);
1331                         rte_free(values_copy);
1332                         return -EINVAL;
1333                 }
1334                 memcpy(&values[i], &values_copy[ids[i]].value,
1335                         sizeof(values[i]));
1336         }
1337
1338         rte_free(values_copy);
1339         return size;
1340 }
1341
1342 /*
1343  * Retrieve names of extended statistics of an Ethernet device.
1344  *
1345  * @param dev
1346  *   Pointer to Ethernet device.
1347  * @param xstats_names
1348  *   An rte_eth_xstat_name array of at least *size* elements to
1349  *   be filled. If set to NULL, the function returns the required number
1350  *   of elements.
1351  * @param ids
1352  *   IDs array given by app to retrieve specific statistics
1353  * @param size
1354  *   The size of the xstats_names array (number of elements).
1355  * @return
1356  *   - A positive value lower or equal to size: success. The return value
1357  *     is the number of entries filled in the stats table.
1358  *   - A positive value higher than size: error, the given statistics table
1359  *     is too small. The return value corresponds to the size that should
1360  *     be given to succeed. The entries in the table are not valid and
1361  *     shall not be used by the caller.
1362  */
1363 int
1364 hns3_dev_xstats_get_names_by_id(struct rte_eth_dev *dev,
1365                                 struct rte_eth_xstat_name *xstats_names,
1366                                 const uint64_t *ids, uint32_t size)
1367 {
1368         const uint32_t cnt_stats = hns3_xstats_calc_num(dev);
1369         struct hns3_adapter *hns = dev->data->dev_private;
1370         struct rte_eth_xstat_name *names_copy;
1371         struct hns3_hw *hw = &hns->hw;
1372         uint64_t len;
1373         uint32_t i;
1374
1375         if (xstats_names == NULL)
1376                 return cnt_stats;
1377
1378         if (ids == NULL) {
1379                 if (size < cnt_stats)
1380                         return cnt_stats;
1381
1382                 return hns3_dev_xstats_get_names(dev, xstats_names, cnt_stats);
1383         }
1384
1385         len = cnt_stats * sizeof(struct rte_eth_xstat_name);
1386         names_copy = rte_zmalloc("hns3_xstats_names", len, 0);
1387         if (names_copy == NULL) {
1388                 hns3_err(hw, "Failed to allocate %" PRIx64 " bytes needed "
1389                              "to store statistics names", len);
1390                 return -ENOMEM;
1391         }
1392
1393         (void)hns3_dev_xstats_get_names(dev, names_copy, cnt_stats);
1394
1395         for (i = 0; i < size; i++) {
1396                 if (ids[i] >= cnt_stats) {
1397                         hns3_err(hw, "ids[%u] (%" PRIx64 ") is invalid, "
1398                                      "should < %u", i, ids[i], cnt_stats);
1399                         rte_free(names_copy);
1400                         return -EINVAL;
1401                 }
1402                 snprintf(xstats_names[i].name, sizeof(xstats_names[i].name),
1403                          "%s", names_copy[ids[i]].name);
1404         }
1405
1406         rte_free(names_copy);
1407         return size;
1408 }
1409
1410 static void
1411 hns3_tqp_dfx_stats_clear(struct rte_eth_dev *dev)
1412 {
1413         struct hns3_rx_queue *rxq;
1414         struct hns3_tx_queue *txq;
1415         uint16_t i;
1416
1417         /* Clear Rx dfx stats */
1418         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1419                 rxq = dev->data->rx_queues[i];
1420                 if (rxq)
1421                         memset(&rxq->dfx_stats, 0,
1422                                sizeof(struct hns3_rx_dfx_stats));
1423         }
1424
1425         /* Clear Tx dfx stats */
1426         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1427                 txq = dev->data->tx_queues[i];
1428                 if (txq)
1429                         memset(&txq->dfx_stats, 0,
1430                                sizeof(struct hns3_tx_dfx_stats));
1431         }
1432 }
1433
1434 int
1435 hns3_dev_xstats_reset(struct rte_eth_dev *dev)
1436 {
1437         struct hns3_adapter *hns = dev->data->dev_private;
1438         struct hns3_pf *pf = &hns->pf;
1439         int ret;
1440
1441         /* Clear tqp stats */
1442         ret = hns3_stats_reset(dev);
1443         if (ret)
1444                 return ret;
1445
1446         hns3_tqp_dfx_stats_clear(dev);
1447
1448         /* Clear reset stats */
1449         memset(&hns->hw.reset.stats, 0, sizeof(struct hns3_reset_stats));
1450
1451         if (hns->is_vf)
1452                 return 0;
1453
1454         /* HW registers are cleared on read */
1455         ret = hns3_mac_stats_reset(dev);
1456         if (ret)
1457                 return ret;
1458
1459         /* Clear error stats */
1460         memset(&pf->abn_int_stats, 0, sizeof(struct hns3_err_msix_intr_stats));
1461
1462         return 0;
1463 }
1464
1465 int
1466 hns3_tqp_stats_init(struct hns3_hw *hw)
1467 {
1468         struct hns3_tqp_stats *tqp_stats = &hw->tqp_stats;
1469
1470         tqp_stats->rcb_rx_ring_pktnum = rte_zmalloc("hns3_rx_ring_pkt_num",
1471                                          sizeof(uint64_t) * hw->tqps_num, 0);
1472         if (tqp_stats->rcb_rx_ring_pktnum == NULL) {
1473                 hns3_err(hw, "failed to allocate rx_ring pkt_num.");
1474                 return -ENOMEM;
1475         }
1476
1477         tqp_stats->rcb_tx_ring_pktnum = rte_zmalloc("hns3_tx_ring_pkt_num",
1478                                          sizeof(uint64_t) * hw->tqps_num, 0);
1479         if (tqp_stats->rcb_tx_ring_pktnum == NULL) {
1480                 hns3_err(hw, "failed to allocate tx_ring pkt_num.");
1481                 rte_free(tqp_stats->rcb_rx_ring_pktnum);
1482                 tqp_stats->rcb_rx_ring_pktnum = NULL;
1483                 return -ENOMEM;
1484         }
1485
1486         return 0;
1487 }
1488
1489 void
1490 hns3_tqp_stats_uninit(struct hns3_hw *hw)
1491 {
1492         struct hns3_tqp_stats *tqp_stats = &hw->tqp_stats;
1493
1494         rte_free(tqp_stats->rcb_rx_ring_pktnum);
1495         tqp_stats->rcb_rx_ring_pktnum = NULL;
1496         rte_free(tqp_stats->rcb_tx_ring_pktnum);
1497         tqp_stats->rcb_tx_ring_pktnum = NULL;
1498 }
1499
1500 static void
1501 hns3_tqp_stats_clear(struct hns3_hw *hw)
1502 {
1503         struct hns3_tqp_stats *stats = &hw->tqp_stats;
1504
1505         stats->rcb_rx_ring_pktnum_rcd = 0;
1506         stats->rcb_tx_ring_pktnum_rcd = 0;
1507         memset(stats->rcb_rx_ring_pktnum, 0, sizeof(uint64_t) * hw->tqps_num);
1508         memset(stats->rcb_tx_ring_pktnum, 0, sizeof(uint64_t) * hw->tqps_num);
1509 }