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