net/iavf: fix VLAN tag extraction handling
[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         /*
558          * If HW statistics are reset by stats_reset, but a lot of residual
559          * packets exist in the hardware queue and these packets are error
560          * packets, flip overflow may occurred. So return 0 in this case.
561          */
562         rte_stats->ipackets =
563                 stats->rcb_rx_ring_pktnum_rcd > rte_stats->ierrors ?
564                 stats->rcb_rx_ring_pktnum_rcd - rte_stats->ierrors : 0;
565         rte_stats->opackets  = stats->rcb_tx_ring_pktnum_rcd -
566                 rte_stats->oerrors;
567         rte_stats->rx_nombuf = eth_dev->data->rx_mbuf_alloc_failed;
568
569         return 0;
570 }
571
572 int
573 hns3_stats_reset(struct rte_eth_dev *eth_dev)
574 {
575         struct hns3_adapter *hns = eth_dev->data->dev_private;
576         struct hns3_hw *hw = &hns->hw;
577         struct hns3_cmd_desc desc_reset;
578         struct hns3_rx_queue *rxq;
579         uint16_t i;
580         int ret;
581
582         /*
583          * Note: Reading hardware statistics of rx/tx queue packet number
584          * will clear them.
585          */
586         for (i = 0; i < hw->tqps_num; i++) {
587                 hns3_cmd_setup_basic_desc(&desc_reset, HNS3_OPC_QUERY_RX_STATUS,
588                                           true);
589                 desc_reset.data[0] = rte_cpu_to_le_32((uint32_t)i);
590                 ret = hns3_cmd_send(hw, &desc_reset, 1);
591                 if (ret) {
592                         hns3_err(hw, "Failed to reset RX No.%u queue stat: %d",
593                                  i, ret);
594                         return ret;
595                 }
596
597                 hns3_cmd_setup_basic_desc(&desc_reset, HNS3_OPC_QUERY_TX_STATUS,
598                                           true);
599                 desc_reset.data[0] = rte_cpu_to_le_32((uint32_t)i);
600                 ret = hns3_cmd_send(hw, &desc_reset, 1);
601                 if (ret) {
602                         hns3_err(hw, "Failed to reset TX No.%u queue stat: %d",
603                                  i, ret);
604                         return ret;
605                 }
606         }
607
608         /*
609          * Clear soft stats of rx error packet which will be dropped
610          * in driver.
611          */
612         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
613                 rxq = eth_dev->data->rx_queues[i];
614                 if (rxq) {
615                         rxq->err_stats.pkt_len_errors = 0;
616                         rxq->err_stats.l2_errors = 0;
617                 }
618         }
619
620         /*
621          * 'packets' in hns3_tx_basic_stats and hns3_rx_basic_stats come
622          * from hw->tqp_stats. And clearing tqp stats is like clearing
623          * their source.
624          */
625         hns3_tqp_stats_clear(hw);
626
627         return 0;
628 }
629
630 static int
631 hns3_mac_stats_reset(__rte_unused struct rte_eth_dev *dev)
632 {
633         struct hns3_adapter *hns = dev->data->dev_private;
634         struct hns3_hw *hw = &hns->hw;
635         struct hns3_mac_stats *mac_stats = &hw->mac_stats;
636         int ret;
637
638         ret = hns3_query_update_mac_stats(dev);
639         if (ret) {
640                 hns3_err(hw, "Clear Mac stats fail : %d", ret);
641                 return ret;
642         }
643
644         memset(mac_stats, 0, sizeof(struct hns3_mac_stats));
645
646         return 0;
647 }
648
649 /* This function calculates the number of xstats based on the current config */
650 static int
651 hns3_xstats_calc_num(struct rte_eth_dev *dev)
652 {
653 #define HNS3_PF_VF_RX_COMM_STATS_NUM    (HNS3_NUM_RX_BD_ERROR_XSTATS + \
654                                          HNS3_NUM_RXQ_DFX_XSTATS + \
655                                          HNS3_NUM_RX_QUEUE_STATS + \
656                                          HNS3_NUM_RXQ_BASIC_STATS)
657 #define HNS3_PF_VF_TX_COMM_STATS_NUM    (HNS3_NUM_TXQ_DFX_XSTATS + \
658                                          HNS3_NUM_TX_QUEUE_STATS + \
659                                          HNS3_NUM_TXQ_BASIC_STATS)
660
661         struct hns3_adapter *hns = dev->data->dev_private;
662         uint16_t nb_rx_q = dev->data->nb_rx_queues;
663         uint16_t nb_tx_q = dev->data->nb_tx_queues;
664         int rx_comm_stats_num = nb_rx_q * HNS3_PF_VF_RX_COMM_STATS_NUM;
665         int tx_comm_stats_num = nb_tx_q * HNS3_PF_VF_TX_COMM_STATS_NUM;
666
667         if (hns->is_vf)
668                 return rx_comm_stats_num + tx_comm_stats_num +
669                         HNS3_NUM_RESET_XSTATS;
670         else
671                 return rx_comm_stats_num + tx_comm_stats_num +
672                         HNS3_FIX_NUM_STATS;
673 }
674
675 static void
676 hns3_queue_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
677                      int *count)
678 {
679         struct hns3_adapter *hns = dev->data->dev_private;
680         struct hns3_hw *hw = &hns->hw;
681         uint32_t reg_offset;
682         uint16_t i, j;
683
684         /* Get rx queue stats */
685         for (j = 0; j < dev->data->nb_rx_queues; j++) {
686                 for (i = 0; i < HNS3_NUM_RX_QUEUE_STATS; i++) {
687                         reg_offset = hns3_get_tqp_reg_offset(j);
688                         xstats[*count].value = hns3_read_dev(hw,
689                                 reg_offset + hns3_rx_queue_strings[i].offset);
690                         xstats[*count].id = *count;
691                         (*count)++;
692                 }
693         }
694
695         /* Get tx queue stats */
696         for (j = 0; j < dev->data->nb_tx_queues; j++) {
697                 for (i = 0; i < HNS3_NUM_TX_QUEUE_STATS; i++) {
698                         reg_offset = hns3_get_tqp_reg_offset(j);
699                         xstats[*count].value = hns3_read_dev(hw,
700                                 reg_offset + hns3_tx_queue_strings[i].offset);
701                         xstats[*count].id = *count;
702                         (*count)++;
703                 }
704         }
705 }
706
707 void
708 hns3_error_int_stats_add(struct hns3_adapter *hns, const char *err)
709 {
710         struct hns3_pf *pf = &hns->pf;
711         uint16_t i;
712         char *addr;
713
714         for (i = 0; i < HNS3_NUM_ERROR_INT_XSTATS; i++) {
715                 if (strcmp(hns3_error_int_stats_strings[i].name, err) == 0) {
716                         addr = (char *)&pf->abn_int_stats +
717                                 hns3_error_int_stats_strings[i].offset;
718                         *(uint64_t *)addr += 1;
719                         break;
720                 }
721         }
722 }
723
724 static void
725 hns3_rxq_dfx_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
726                        int *count)
727 {
728         struct hns3_rx_dfx_stats *dfx_stats;
729         struct hns3_rx_queue *rxq;
730         uint16_t i, j;
731         char *val;
732
733         for (i = 0; i < dev->data->nb_rx_queues; i++) {
734                 rxq = (struct hns3_rx_queue *)dev->data->rx_queues[i];
735                 if (rxq == NULL)
736                         continue;
737
738                 dfx_stats = &rxq->dfx_stats;
739                 for (j = 0; j < HNS3_NUM_RXQ_DFX_XSTATS; j++) {
740                         val = (char *)dfx_stats +
741                                 hns3_rxq_dfx_stats_strings[j].offset;
742                         xstats[*count].value = *(uint64_t *)val;
743                         xstats[*count].id = *count;
744                         (*count)++;
745                 }
746         }
747 }
748
749 static void
750 hns3_txq_dfx_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
751                        int *count)
752 {
753         struct hns3_tx_dfx_stats *dfx_stats;
754         struct hns3_tx_queue *txq;
755         uint16_t i, j;
756         char *val;
757
758         for (i = 0; i < dev->data->nb_tx_queues; i++) {
759                 txq = (struct hns3_tx_queue *)dev->data->tx_queues[i];
760                 if (txq == NULL)
761                         continue;
762
763                 dfx_stats = &txq->dfx_stats;
764                 for (j = 0; j < HNS3_NUM_TXQ_DFX_XSTATS; j++) {
765                         val = (char *)dfx_stats +
766                                 hns3_txq_dfx_stats_strings[j].offset;
767                         xstats[*count].value = *(uint64_t *)val;
768                         xstats[*count].id = *count;
769                         (*count)++;
770                 }
771         }
772 }
773
774 static void
775 hns3_tqp_dfx_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
776                        int *count)
777 {
778         hns3_rxq_dfx_stats_get(dev, xstats, count);
779         hns3_txq_dfx_stats_get(dev, xstats, count);
780 }
781
782 static void
783 hns3_rxq_basic_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
784                          int *count)
785 {
786         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
787         struct hns3_tqp_stats *stats = &hw->tqp_stats;
788         struct hns3_rx_basic_stats *rxq_stats;
789         struct hns3_rx_queue *rxq;
790         uint16_t i, j;
791         char *val;
792
793         for (i = 0; i < dev->data->nb_rx_queues; i++) {
794                 rxq = dev->data->rx_queues[i];
795                 if (rxq == NULL)
796                         continue;
797
798                 rxq_stats = &rxq->basic_stats;
799                 rxq_stats->errors = rxq->err_stats.l2_errors +
800                                         rxq->err_stats.pkt_len_errors;
801                 /*
802                  * If HW statistics are reset by stats_reset, but a lot of
803                  * residual packets exist in the hardware queue and these
804                  * packets are error packets, flip overflow may occurred.
805                  * So return 0 in this case.
806                  */
807                 rxq_stats->packets =
808                         stats->rcb_rx_ring_pktnum[i] > rxq_stats->errors ?
809                         stats->rcb_rx_ring_pktnum[i] - rxq_stats->errors : 0;
810                 rxq_stats->bytes = 0;
811                 for (j = 0; j < HNS3_NUM_RXQ_BASIC_STATS; j++) {
812                         val = (char *)rxq_stats +
813                                 hns3_rxq_basic_stats_strings[j].offset;
814                         xstats[*count].value = *(uint64_t *)val;
815                         xstats[*count].id = *count;
816                         (*count)++;
817                 }
818         }
819 }
820
821 static void
822 hns3_txq_basic_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
823                          int *count)
824 {
825         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
826         struct hns3_tqp_stats *stats = &hw->tqp_stats;
827         struct hns3_tx_basic_stats *txq_stats;
828         struct hns3_tx_queue *txq;
829         uint16_t i, j;
830         char *val;
831
832         for (i = 0; i < dev->data->nb_tx_queues; i++) {
833                 txq = dev->data->tx_queues[i];
834                 if (txq == NULL)
835                         continue;
836
837                 txq_stats = &txq->basic_stats;
838                 txq_stats->packets = stats->rcb_tx_ring_pktnum[i];
839                 txq_stats->bytes = 0;
840                 for (j = 0; j < HNS3_NUM_TXQ_BASIC_STATS; j++) {
841                         val = (char *)txq_stats +
842                                 hns3_txq_basic_stats_strings[j].offset;
843                         xstats[*count].value = *(uint64_t *)val;
844                         xstats[*count].id = *count;
845                         (*count)++;
846                 }
847         }
848 }
849
850 static int
851 hns3_tqp_basic_stats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
852                          int *count)
853 {
854         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
855         int ret;
856
857         /* Update tqp stats by read register */
858         ret = hns3_update_tqp_stats(hw);
859         if (ret) {
860                 hns3_err(hw, "Update tqp stats fail, ret = %d.", ret);
861                 return ret;
862         }
863
864         hns3_rxq_basic_stats_get(dev, xstats, count);
865         hns3_txq_basic_stats_get(dev, xstats, count);
866
867         return 0;
868 }
869
870 /*
871  * The function is only called by hns3_dev_xstats_reset to clear
872  * basic stats of per-queue. TQP stats are all cleared in hns3_stats_reset
873  * which is called before this function.
874  *
875  * @param dev
876  *   Pointer to Ethernet device.
877  */
878 static void
879 hns3_tqp_basic_stats_clear(struct rte_eth_dev *dev)
880 {
881         struct hns3_tx_queue *txq;
882         struct hns3_rx_queue *rxq;
883         uint16_t i;
884
885         for (i = 0; i < dev->data->nb_rx_queues; i++) {
886                 rxq = dev->data->rx_queues[i];
887                 if (rxq)
888                         memset(&rxq->basic_stats, 0,
889                                sizeof(struct hns3_rx_basic_stats));
890         }
891
892         for (i = 0; i < dev->data->nb_tx_queues; i++) {
893                 txq = dev->data->tx_queues[i];
894                 if (txq)
895                         memset(&txq->basic_stats, 0,
896                                sizeof(struct hns3_tx_basic_stats));
897         }
898 }
899
900 /*
901  * Retrieve extended(tqp | Mac) statistics of an Ethernet device.
902  * @param dev
903  *   Pointer to Ethernet device.
904  * @praram xstats
905  *   A pointer to a table of structure of type *rte_eth_xstat*
906  *   to be filled with device statistics ids and values.
907  *   This parameter can be set to NULL if n is 0.
908  * @param n
909  *   The size of the xstats array (number of elements).
910  * @return
911  *   0 on fail, count(The size of the statistics elements) on success.
912  */
913 int
914 hns3_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
915                     unsigned int n)
916 {
917         struct hns3_adapter *hns = dev->data->dev_private;
918         struct hns3_pf *pf = &hns->pf;
919         struct hns3_hw *hw = &hns->hw;
920         struct hns3_mac_stats *mac_stats = &hw->mac_stats;
921         struct hns3_reset_stats *reset_stats = &hw->reset.stats;
922         struct hns3_rx_bd_errors_stats *rx_err_stats;
923         struct hns3_rx_queue *rxq;
924         uint16_t i, j;
925         char *addr;
926         int count;
927         int ret;
928
929         if (xstats == NULL)
930                 return 0;
931
932         count = hns3_xstats_calc_num(dev);
933         if ((int)n < count)
934                 return count;
935
936         count = 0;
937
938         ret = hns3_tqp_basic_stats_get(dev, xstats, &count);
939         if (ret < 0)
940                 return ret;
941
942         if (!hns->is_vf) {
943                 /* Update Mac stats */
944                 ret = hns3_query_update_mac_stats(dev);
945                 if (ret < 0) {
946                         hns3_err(hw, "Update Mac stats fail : %d", ret);
947                         return ret;
948                 }
949
950                 /* Get MAC stats from hw->hw_xstats.mac_stats struct */
951                 for (i = 0; i < HNS3_NUM_MAC_STATS; i++) {
952                         addr = (char *)mac_stats + hns3_mac_strings[i].offset;
953                         xstats[count].value = *(uint64_t *)addr;
954                         xstats[count].id = count;
955                         count++;
956                 }
957
958                 for (i = 0; i < HNS3_NUM_ERROR_INT_XSTATS; i++) {
959                         addr = (char *)&pf->abn_int_stats +
960                                hns3_error_int_stats_strings[i].offset;
961                         xstats[count].value = *(uint64_t *)addr;
962                         xstats[count].id = count;
963                         count++;
964                 }
965         }
966
967         /* Get the reset stat */
968         for (i = 0; i < HNS3_NUM_RESET_XSTATS; i++) {
969                 addr = (char *)reset_stats + hns3_reset_stats_strings[i].offset;
970                 xstats[count].value = *(uint64_t *)addr;
971                 xstats[count].id = count;
972                 count++;
973         }
974
975         /* Get the Rx BD errors stats */
976         for (j = 0; j < dev->data->nb_rx_queues; j++) {
977                 for (i = 0; i < HNS3_NUM_RX_BD_ERROR_XSTATS; i++) {
978                         rxq = dev->data->rx_queues[j];
979                         if (rxq) {
980                                 rx_err_stats = &rxq->err_stats;
981                                 addr = (char *)rx_err_stats +
982                                         hns3_rx_bd_error_strings[i].offset;
983                                 xstats[count].value = *(uint64_t *)addr;
984                                 xstats[count].id = count;
985                                 count++;
986                         }
987                 }
988         }
989
990         hns3_tqp_dfx_stats_get(dev, xstats, &count);
991         hns3_queue_stats_get(dev, xstats, &count);
992
993         return count;
994 }
995
996 static void
997 hns3_tqp_basic_stats_name_get(struct rte_eth_dev *dev,
998                               struct rte_eth_xstat_name *xstats_names,
999                               uint32_t *count)
1000 {
1001         uint16_t i, j;
1002
1003         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1004                 for (j = 0; j < HNS3_NUM_RXQ_BASIC_STATS; j++) {
1005                         snprintf(xstats_names[*count].name,
1006                                  sizeof(xstats_names[*count].name),
1007                                  "rx_q%u_%s", i,
1008                                  hns3_rxq_basic_stats_strings[j].name);
1009                         (*count)++;
1010                 }
1011         }
1012         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1013                 for (j = 0; j < HNS3_NUM_TXQ_BASIC_STATS; j++) {
1014                         snprintf(xstats_names[*count].name,
1015                                  sizeof(xstats_names[*count].name),
1016                                  "tx_q%u_%s", i,
1017                                  hns3_txq_basic_stats_strings[j].name);
1018                         (*count)++;
1019                 }
1020         }
1021 }
1022
1023 static void
1024 hns3_tqp_dfx_stats_name_get(struct rte_eth_dev *dev,
1025                             struct rte_eth_xstat_name *xstats_names,
1026                             uint32_t *count)
1027 {
1028         uint16_t i, j;
1029
1030         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1031                 for (j = 0; j < HNS3_NUM_RXQ_DFX_XSTATS; j++) {
1032                         snprintf(xstats_names[*count].name,
1033                                  sizeof(xstats_names[*count].name),
1034                                  "rx_q%u_%s", i,
1035                                  hns3_rxq_dfx_stats_strings[j].name);
1036                         (*count)++;
1037                 }
1038         }
1039
1040         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1041                 for (j = 0; j < HNS3_NUM_TXQ_DFX_XSTATS; j++) {
1042                         snprintf(xstats_names[*count].name,
1043                                  sizeof(xstats_names[*count].name),
1044                                  "tx_q%u_%s", i,
1045                                  hns3_txq_dfx_stats_strings[j].name);
1046                         (*count)++;
1047                 }
1048         }
1049 }
1050
1051 /*
1052  * Retrieve names of extended statistics of an Ethernet device.
1053  *
1054  * There is an assumption that 'xstat_names' and 'xstats' arrays are matched
1055  * by array index:
1056  *  xstats_names[i].name => xstats[i].value
1057  *
1058  * And the array index is same with id field of 'struct rte_eth_xstat':
1059  *  xstats[i].id == i
1060  *
1061  * This assumption makes key-value pair matching less flexible but simpler.
1062  *
1063  * @param dev
1064  *   Pointer to Ethernet device.
1065  * @param xstats_names
1066  *   An rte_eth_xstat_name array of at least *size* elements to
1067  *   be filled. If set to NULL, the function returns the required number
1068  *   of elements.
1069  * @param size
1070  *   The size of the xstats_names array (number of elements).
1071  * @return
1072  *   - A positive value lower or equal to size: success. The return value
1073  *     is the number of entries filled in the stats table.
1074  */
1075 int
1076 hns3_dev_xstats_get_names(struct rte_eth_dev *dev,
1077                           struct rte_eth_xstat_name *xstats_names,
1078                           __rte_unused unsigned int size)
1079 {
1080         struct hns3_adapter *hns = dev->data->dev_private;
1081         int cnt_stats = hns3_xstats_calc_num(dev);
1082         uint32_t count = 0;
1083         uint16_t i, j;
1084
1085         if (xstats_names == NULL)
1086                 return cnt_stats;
1087
1088         hns3_tqp_basic_stats_name_get(dev, xstats_names, &count);
1089
1090         /* Note: size limited checked in rte_eth_xstats_get_names() */
1091         if (!hns->is_vf) {
1092                 /* Get MAC name from hw->hw_xstats.mac_stats struct */
1093                 for (i = 0; i < HNS3_NUM_MAC_STATS; i++) {
1094                         snprintf(xstats_names[count].name,
1095                                  sizeof(xstats_names[count].name),
1096                                  "%s", hns3_mac_strings[i].name);
1097                         count++;
1098                 }
1099
1100                 for (i = 0; i < HNS3_NUM_ERROR_INT_XSTATS; i++) {
1101                         snprintf(xstats_names[count].name,
1102                                  sizeof(xstats_names[count].name),
1103                                  "%s", hns3_error_int_stats_strings[i].name);
1104                         count++;
1105                 }
1106         }
1107         for (i = 0; i < HNS3_NUM_RESET_XSTATS; i++) {
1108                 snprintf(xstats_names[count].name,
1109                          sizeof(xstats_names[count].name),
1110                          "%s", hns3_reset_stats_strings[i].name);
1111                 count++;
1112         }
1113
1114         for (j = 0; j < dev->data->nb_rx_queues; j++) {
1115                 for (i = 0; i < HNS3_NUM_RX_BD_ERROR_XSTATS; i++) {
1116                         snprintf(xstats_names[count].name,
1117                                  sizeof(xstats_names[count].name),
1118                                  "rx_q%u_%s", j,
1119                                  hns3_rx_bd_error_strings[i].name);
1120                         count++;
1121                 }
1122         }
1123
1124         hns3_tqp_dfx_stats_name_get(dev, xstats_names, &count);
1125
1126         for (j = 0; j < dev->data->nb_rx_queues; j++) {
1127                 for (i = 0; i < HNS3_NUM_RX_QUEUE_STATS; i++) {
1128                         snprintf(xstats_names[count].name,
1129                                  sizeof(xstats_names[count].name),
1130                                  "rx_q%u_%s", j, hns3_rx_queue_strings[i].name);
1131                         count++;
1132                 }
1133         }
1134
1135         for (j = 0; j < dev->data->nb_tx_queues; j++) {
1136                 for (i = 0; i < HNS3_NUM_TX_QUEUE_STATS; i++) {
1137                         snprintf(xstats_names[count].name,
1138                                  sizeof(xstats_names[count].name),
1139                                  "tx_q%u_%s", j, hns3_tx_queue_strings[i].name);
1140                         count++;
1141                 }
1142         }
1143
1144         return count;
1145 }
1146
1147 /*
1148  * Retrieve extended statistics of an Ethernet device.
1149  *
1150  * @param dev
1151  *   Pointer to Ethernet device.
1152  * @param ids
1153  *   A pointer to an ids array passed by application. This tells which
1154  *   statistics values function should retrieve. This parameter
1155  *   can be set to NULL if size is 0. In this case function will retrieve
1156  *   all avalible statistics.
1157  * @param values
1158  *   A pointer to a table to be filled with device statistics values.
1159  * @param size
1160  *   The size of the ids array (number of elements).
1161  * @return
1162  *   - A positive value lower or equal to size: success. The return value
1163  *     is the number of entries filled in the stats table.
1164  *   - A positive value higher than size: error, the given statistics table
1165  *     is too small. The return value corresponds to the size that should
1166  *     be given to succeed. The entries in the table are not valid and
1167  *     shall not be used by the caller.
1168  *   - 0 on no ids.
1169  */
1170 int
1171 hns3_dev_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
1172                           uint64_t *values, uint32_t size)
1173 {
1174         const uint32_t cnt_stats = hns3_xstats_calc_num(dev);
1175         struct hns3_adapter *hns = dev->data->dev_private;
1176         struct rte_eth_xstat *values_copy;
1177         struct hns3_hw *hw = &hns->hw;
1178         uint32_t count_value;
1179         uint64_t len;
1180         uint32_t i;
1181
1182         if (ids == NULL && values == NULL)
1183                 return cnt_stats;
1184
1185         if (ids == NULL)
1186                 if (size < cnt_stats)
1187                         return cnt_stats;
1188
1189         len = cnt_stats * sizeof(struct rte_eth_xstat);
1190         values_copy = rte_zmalloc("hns3_xstats_values", len, 0);
1191         if (values_copy == NULL) {
1192                 hns3_err(hw, "Failed to allocate %" PRIx64 " bytes needed "
1193                              "to store statistics values", len);
1194                 return -ENOMEM;
1195         }
1196
1197         count_value = hns3_dev_xstats_get(dev, values_copy, cnt_stats);
1198         if (count_value != cnt_stats) {
1199                 rte_free(values_copy);
1200                 return -EINVAL;
1201         }
1202
1203         if (ids == NULL && values != NULL) {
1204                 for (i = 0; i < cnt_stats; i++)
1205                         memcpy(&values[i], &values_copy[i].value,
1206                                sizeof(values[i]));
1207
1208                 rte_free(values_copy);
1209                 return cnt_stats;
1210         }
1211
1212         for (i = 0; i < size; i++) {
1213                 if (ids[i] >= cnt_stats) {
1214                         hns3_err(hw, "ids[%u] (%" PRIx64 ") is invalid, "
1215                                      "should < %u", i, ids[i], cnt_stats);
1216                         rte_free(values_copy);
1217                         return -EINVAL;
1218                 }
1219                 memcpy(&values[i], &values_copy[ids[i]].value,
1220                         sizeof(values[i]));
1221         }
1222
1223         rte_free(values_copy);
1224         return size;
1225 }
1226
1227 /*
1228  * Retrieve names of extended statistics of an Ethernet device.
1229  *
1230  * @param dev
1231  *   Pointer to Ethernet device.
1232  * @param xstats_names
1233  *   An rte_eth_xstat_name array of at least *size* elements to
1234  *   be filled. If set to NULL, the function returns the required number
1235  *   of elements.
1236  * @param ids
1237  *   IDs array given by app to retrieve specific statistics
1238  * @param size
1239  *   The size of the xstats_names array (number of elements).
1240  * @return
1241  *   - A positive value lower or equal to size: success. The return value
1242  *     is the number of entries filled in the stats table.
1243  *   - A positive value higher than size: error, the given statistics table
1244  *     is too small. The return value corresponds to the size that should
1245  *     be given to succeed. The entries in the table are not valid and
1246  *     shall not be used by the caller.
1247  */
1248 int
1249 hns3_dev_xstats_get_names_by_id(struct rte_eth_dev *dev,
1250                                 struct rte_eth_xstat_name *xstats_names,
1251                                 const uint64_t *ids, uint32_t size)
1252 {
1253         const uint32_t cnt_stats = hns3_xstats_calc_num(dev);
1254         struct hns3_adapter *hns = dev->data->dev_private;
1255         struct rte_eth_xstat_name *names_copy;
1256         struct hns3_hw *hw = &hns->hw;
1257         uint64_t len;
1258         uint32_t i;
1259
1260         if (xstats_names == NULL)
1261                 return cnt_stats;
1262
1263         if (ids == NULL) {
1264                 if (size < cnt_stats)
1265                         return cnt_stats;
1266
1267                 return hns3_dev_xstats_get_names(dev, xstats_names, cnt_stats);
1268         }
1269
1270         len = cnt_stats * sizeof(struct rte_eth_xstat_name);
1271         names_copy = rte_zmalloc("hns3_xstats_names", len, 0);
1272         if (names_copy == NULL) {
1273                 hns3_err(hw, "Failed to allocate %" PRIx64 " bytes needed "
1274                              "to store statistics names", len);
1275                 return -ENOMEM;
1276         }
1277
1278         (void)hns3_dev_xstats_get_names(dev, names_copy, cnt_stats);
1279
1280         for (i = 0; i < size; i++) {
1281                 if (ids[i] >= cnt_stats) {
1282                         hns3_err(hw, "ids[%u] (%" PRIx64 ") is invalid, "
1283                                      "should < %u", i, ids[i], cnt_stats);
1284                         rte_free(names_copy);
1285                         return -EINVAL;
1286                 }
1287                 snprintf(xstats_names[i].name, sizeof(xstats_names[i].name),
1288                          "%s", names_copy[ids[i]].name);
1289         }
1290
1291         rte_free(names_copy);
1292         return size;
1293 }
1294
1295 static void
1296 hns3_tqp_dfx_stats_clear(struct rte_eth_dev *dev)
1297 {
1298         struct hns3_rx_queue *rxq;
1299         struct hns3_tx_queue *txq;
1300         uint16_t i;
1301
1302         /* Clear Rx dfx stats */
1303         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1304                 rxq = dev->data->rx_queues[i];
1305                 if (rxq)
1306                         memset(&rxq->dfx_stats, 0,
1307                                sizeof(struct hns3_rx_dfx_stats));
1308         }
1309
1310         /* Clear Tx dfx stats */
1311         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1312                 txq = dev->data->tx_queues[i];
1313                 if (txq)
1314                         memset(&txq->dfx_stats, 0,
1315                                sizeof(struct hns3_tx_dfx_stats));
1316         }
1317 }
1318
1319 int
1320 hns3_dev_xstats_reset(struct rte_eth_dev *dev)
1321 {
1322         struct hns3_adapter *hns = dev->data->dev_private;
1323         struct hns3_pf *pf = &hns->pf;
1324         int ret;
1325
1326         /* Clear tqp stats */
1327         ret = hns3_stats_reset(dev);
1328         if (ret)
1329                 return ret;
1330
1331         hns3_tqp_basic_stats_clear(dev);
1332         hns3_tqp_dfx_stats_clear(dev);
1333
1334         /* Clear reset stats */
1335         memset(&hns->hw.reset.stats, 0, sizeof(struct hns3_reset_stats));
1336
1337         if (hns->is_vf)
1338                 return 0;
1339
1340         /* HW registers are cleared on read */
1341         ret = hns3_mac_stats_reset(dev);
1342         if (ret)
1343                 return ret;
1344
1345         /* Clear error stats */
1346         memset(&pf->abn_int_stats, 0, sizeof(struct hns3_err_msix_intr_stats));
1347
1348         return 0;
1349 }
1350
1351 int
1352 hns3_tqp_stats_init(struct hns3_hw *hw)
1353 {
1354         struct hns3_tqp_stats *tqp_stats = &hw->tqp_stats;
1355
1356         tqp_stats->rcb_rx_ring_pktnum = rte_zmalloc("hns3_rx_ring_pkt_num",
1357                                          sizeof(uint64_t) * hw->tqps_num, 0);
1358         if (tqp_stats->rcb_rx_ring_pktnum == NULL) {
1359                 hns3_err(hw, "failed to allocate rx_ring pkt_num.");
1360                 return -ENOMEM;
1361         }
1362
1363         tqp_stats->rcb_tx_ring_pktnum = rte_zmalloc("hns3_tx_ring_pkt_num",
1364                                          sizeof(uint64_t) * hw->tqps_num, 0);
1365         if (tqp_stats->rcb_tx_ring_pktnum == NULL) {
1366                 hns3_err(hw, "failed to allocate tx_ring pkt_num.");
1367                 rte_free(tqp_stats->rcb_rx_ring_pktnum);
1368                 tqp_stats->rcb_rx_ring_pktnum = NULL;
1369                 return -ENOMEM;
1370         }
1371
1372         return 0;
1373 }
1374
1375 void
1376 hns3_tqp_stats_uninit(struct hns3_hw *hw)
1377 {
1378         struct hns3_tqp_stats *tqp_stats = &hw->tqp_stats;
1379
1380         rte_free(tqp_stats->rcb_rx_ring_pktnum);
1381         tqp_stats->rcb_rx_ring_pktnum = NULL;
1382         rte_free(tqp_stats->rcb_tx_ring_pktnum);
1383         tqp_stats->rcb_tx_ring_pktnum = NULL;
1384 }
1385
1386 static void
1387 hns3_tqp_stats_clear(struct hns3_hw *hw)
1388 {
1389         struct hns3_tqp_stats *stats = &hw->tqp_stats;
1390
1391         stats->rcb_rx_ring_pktnum_rcd = 0;
1392         stats->rcb_tx_ring_pktnum_rcd = 0;
1393         memset(stats->rcb_rx_ring_pktnum, 0, sizeof(uint64_t) * hw->tqps_num);
1394         memset(stats->rcb_tx_ring_pktnum, 0, sizeof(uint64_t) * hw->tqps_num);
1395 }