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