net/bnxt: fix xstats for VF
[dpdk.git] / drivers / net / bnxt / bnxt_stats.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7
8 #include <rte_byteorder.h>
9
10 #include "bnxt.h"
11 #include "bnxt_cpr.h"
12 #include "bnxt_hwrm.h"
13 #include "bnxt_rxq.h"
14 #include "bnxt_stats.h"
15 #include "bnxt_txq.h"
16 #include "hsi_struct_def_dpdk.h"
17
18 static const struct bnxt_xstats_name_off bnxt_rx_stats_strings[] = {
19         {"rx_64b_frames", offsetof(struct rx_port_stats,
20                                 rx_64b_frames)},
21         {"rx_65b_127b_frames", offsetof(struct rx_port_stats,
22                                 rx_65b_127b_frames)},
23         {"rx_128b_255b_frames", offsetof(struct rx_port_stats,
24                                 rx_128b_255b_frames)},
25         {"rx_256b_511b_frames", offsetof(struct rx_port_stats,
26                                 rx_256b_511b_frames)},
27         {"rx_512b_1023b_frames", offsetof(struct rx_port_stats,
28                                 rx_512b_1023b_frames)},
29         {"rx_1024b_1518_frames", offsetof(struct rx_port_stats,
30                                 rx_1024b_1518_frames)},
31         {"rx_good_vlan_frames", offsetof(struct rx_port_stats,
32                                 rx_good_vlan_frames)},
33         {"rx_1519b_2047b_frames", offsetof(struct rx_port_stats,
34                                 rx_1519b_2047b_frames)},
35         {"rx_2048b_4095b_frames", offsetof(struct rx_port_stats,
36                                 rx_2048b_4095b_frames)},
37         {"rx_4096b_9216b_frames", offsetof(struct rx_port_stats,
38                                 rx_4096b_9216b_frames)},
39         {"rx_9217b_16383b_frames", offsetof(struct rx_port_stats,
40                                 rx_9217b_16383b_frames)},
41         {"rx_total_frames", offsetof(struct rx_port_stats,
42                                 rx_total_frames)},
43         {"rx_ucast_frames", offsetof(struct rx_port_stats,
44                                 rx_ucast_frames)},
45         {"rx_mcast_frames", offsetof(struct rx_port_stats,
46                                 rx_mcast_frames)},
47         {"rx_bcast_frames", offsetof(struct rx_port_stats,
48                                 rx_bcast_frames)},
49         {"rx_fcs_err_frames", offsetof(struct rx_port_stats,
50                                 rx_fcs_err_frames)},
51         {"rx_ctrl_frames", offsetof(struct rx_port_stats,
52                                 rx_ctrl_frames)},
53         {"rx_pause_frames", offsetof(struct rx_port_stats,
54                                 rx_pause_frames)},
55         {"rx_pfc_frames", offsetof(struct rx_port_stats,
56                                 rx_pfc_frames)},
57         {"rx_align_err_frames", offsetof(struct rx_port_stats,
58                                 rx_align_err_frames)},
59         {"rx_ovrsz_frames", offsetof(struct rx_port_stats,
60                                 rx_ovrsz_frames)},
61         {"rx_jbr_frames", offsetof(struct rx_port_stats,
62                                 rx_jbr_frames)},
63         {"rx_mtu_err_frames", offsetof(struct rx_port_stats,
64                                 rx_mtu_err_frames)},
65         {"rx_tagged_frames", offsetof(struct rx_port_stats,
66                                 rx_tagged_frames)},
67         {"rx_double_tagged_frames", offsetof(struct rx_port_stats,
68                                 rx_double_tagged_frames)},
69         {"rx_good_frames", offsetof(struct rx_port_stats,
70                                 rx_good_frames)},
71         {"rx_undrsz_frames", offsetof(struct rx_port_stats,
72                                 rx_undrsz_frames)},
73         {"rx_eee_lpi_events", offsetof(struct rx_port_stats,
74                                 rx_eee_lpi_events)},
75         {"rx_eee_lpi_duration", offsetof(struct rx_port_stats,
76                                 rx_eee_lpi_duration)},
77         {"rx_bytes", offsetof(struct rx_port_stats,
78                                 rx_bytes)},
79         {"rx_runt_bytes", offsetof(struct rx_port_stats,
80                                 rx_runt_bytes)},
81         {"rx_runt_frames", offsetof(struct rx_port_stats,
82                                 rx_runt_frames)},
83 };
84
85 static const struct bnxt_xstats_name_off bnxt_tx_stats_strings[] = {
86         {"tx_64b_frames", offsetof(struct tx_port_stats,
87                                 tx_64b_frames)},
88         {"tx_65b_127b_frames", offsetof(struct tx_port_stats,
89                                 tx_65b_127b_frames)},
90         {"tx_128b_255b_frames", offsetof(struct tx_port_stats,
91                                 tx_128b_255b_frames)},
92         {"tx_256b_511b_frames", offsetof(struct tx_port_stats,
93                                 tx_256b_511b_frames)},
94         {"tx_512b_1023b_frames", offsetof(struct tx_port_stats,
95                                 tx_512b_1023b_frames)},
96         {"tx_1024b_1518_frames", offsetof(struct tx_port_stats,
97                                 tx_1024b_1518_frames)},
98         {"tx_good_vlan_frames", offsetof(struct tx_port_stats,
99                                 tx_good_vlan_frames)},
100         {"tx_1519b_2047_frames", offsetof(struct tx_port_stats,
101                                 tx_1519b_2047_frames)},
102         {"tx_2048b_4095b_frames", offsetof(struct tx_port_stats,
103                                 tx_2048b_4095b_frames)},
104         {"tx_4096b_9216b_frames", offsetof(struct tx_port_stats,
105                                 tx_4096b_9216b_frames)},
106         {"tx_9217b_16383b_frames", offsetof(struct tx_port_stats,
107                                 tx_9217b_16383b_frames)},
108         {"tx_good_frames", offsetof(struct tx_port_stats,
109                                 tx_good_frames)},
110         {"tx_total_frames", offsetof(struct tx_port_stats,
111                                 tx_total_frames)},
112         {"tx_ucast_frames", offsetof(struct tx_port_stats,
113                                 tx_ucast_frames)},
114         {"tx_mcast_frames", offsetof(struct tx_port_stats,
115                                 tx_mcast_frames)},
116         {"tx_bcast_frames", offsetof(struct tx_port_stats,
117                                 tx_bcast_frames)},
118         {"tx_pause_frames", offsetof(struct tx_port_stats,
119                                 tx_pause_frames)},
120         {"tx_pfc_frames", offsetof(struct tx_port_stats,
121                                 tx_pfc_frames)},
122         {"tx_jabber_frames", offsetof(struct tx_port_stats,
123                                 tx_jabber_frames)},
124         {"tx_fcs_err_frames", offsetof(struct tx_port_stats,
125                                 tx_fcs_err_frames)},
126         {"tx_err", offsetof(struct tx_port_stats,
127                                 tx_err)},
128         {"tx_fifo_underruns", offsetof(struct tx_port_stats,
129                                 tx_fifo_underruns)},
130         {"tx_eee_lpi_events", offsetof(struct tx_port_stats,
131                                 tx_eee_lpi_events)},
132         {"tx_eee_lpi_duration", offsetof(struct tx_port_stats,
133                                 tx_eee_lpi_duration)},
134         {"tx_total_collisions", offsetof(struct tx_port_stats,
135                                 tx_total_collisions)},
136         {"tx_bytes", offsetof(struct tx_port_stats,
137                                 tx_bytes)},
138 };
139
140 static const struct bnxt_xstats_name_off bnxt_func_stats_strings[] = {
141         {"tx_ucast_pkts", offsetof(struct hwrm_func_qstats_output,
142                                 tx_ucast_pkts)},
143         {"tx_mcast_pkts", offsetof(struct hwrm_func_qstats_output,
144                                 tx_mcast_pkts)},
145         {"tx_bcast_pkts", offsetof(struct hwrm_func_qstats_output,
146                                 tx_bcast_pkts)},
147         {"tx_discard_pkts", offsetof(struct hwrm_func_qstats_output,
148                                 tx_discard_pkts)},
149         {"tx_drop_pkts", offsetof(struct hwrm_func_qstats_output,
150                                 tx_drop_pkts)},
151         {"tx_ucast_bytes", offsetof(struct hwrm_func_qstats_output,
152                                 tx_ucast_bytes)},
153         {"tx_mcast_bytes", offsetof(struct hwrm_func_qstats_output,
154                                 tx_mcast_bytes)},
155         {"tx_bcast_bytes", offsetof(struct hwrm_func_qstats_output,
156                                 tx_bcast_bytes)},
157         {"rx_ucast_pkts", offsetof(struct hwrm_func_qstats_output,
158                                 rx_ucast_pkts)},
159         {"rx_mcast_pkts", offsetof(struct hwrm_func_qstats_output,
160                                 rx_mcast_pkts)},
161         {"rx_bcast_pkts", offsetof(struct hwrm_func_qstats_output,
162                                 rx_bcast_pkts)},
163         {"rx_discard_pkts", offsetof(struct hwrm_func_qstats_output,
164                                 rx_discard_pkts)},
165         {"rx_drop_pkts", offsetof(struct hwrm_func_qstats_output,
166                                 rx_drop_pkts)},
167         {"rx_ucast_bytes", offsetof(struct hwrm_func_qstats_output,
168                                 rx_ucast_bytes)},
169         {"rx_mcast_bytes", offsetof(struct hwrm_func_qstats_output,
170                                 rx_mcast_bytes)},
171         {"rx_bcast_bytes", offsetof(struct hwrm_func_qstats_output,
172                                 rx_bcast_bytes)},
173         {"rx_agg_pkts", offsetof(struct hwrm_func_qstats_output,
174                                 rx_agg_pkts)},
175         {"rx_agg_bytes", offsetof(struct hwrm_func_qstats_output,
176                                 rx_agg_bytes)},
177         {"rx_agg_events", offsetof(struct hwrm_func_qstats_output,
178                                 rx_agg_events)},
179         {"rx_agg_aborts", offsetof(struct hwrm_func_qstats_output,
180                                 rx_agg_aborts)},
181 };
182
183 /*
184  * Statistics functions
185  */
186
187 void bnxt_free_stats(struct bnxt *bp)
188 {
189         int i;
190
191         for (i = 0; i < (int)bp->tx_cp_nr_rings; i++) {
192                 struct bnxt_tx_queue *txq = bp->tx_queues[i];
193
194                 bnxt_free_txq_stats(txq);
195         }
196         for (i = 0; i < (int)bp->rx_cp_nr_rings; i++) {
197                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
198
199                 bnxt_free_rxq_stats(rxq);
200         }
201 }
202
203 int bnxt_stats_get_op(struct rte_eth_dev *eth_dev,
204                            struct rte_eth_stats *bnxt_stats)
205 {
206         int rc = 0;
207         unsigned int i;
208         struct bnxt *bp = eth_dev->data->dev_private;
209
210         memset(bnxt_stats, 0, sizeof(*bnxt_stats));
211         if (!(bp->flags & BNXT_FLAG_INIT_DONE)) {
212                 PMD_DRV_LOG(ERR, "Device Initialization not complete!\n");
213                 return -1;
214         }
215
216         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
217                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
218                 struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
219
220                 rc = bnxt_hwrm_ctx_qstats(bp, cpr->hw_stats_ctx_id, i,
221                                      bnxt_stats, 1);
222                 if (unlikely(rc))
223                         return rc;
224                 bnxt_stats->rx_nombuf +=
225                                 rte_atomic64_read(&rxq->rx_mbuf_alloc_fail);
226         }
227
228         for (i = 0; i < bp->tx_cp_nr_rings; i++) {
229                 struct bnxt_tx_queue *txq = bp->tx_queues[i];
230                 struct bnxt_cp_ring_info *cpr = txq->cp_ring;
231
232                 rc = bnxt_hwrm_ctx_qstats(bp, cpr->hw_stats_ctx_id, i,
233                                      bnxt_stats, 0);
234                 if (unlikely(rc))
235                         return rc;
236         }
237         rc = bnxt_hwrm_func_qstats(bp, 0xffff, bnxt_stats);
238         if (unlikely(rc))
239                 return rc;
240         return rc;
241 }
242
243 void bnxt_stats_reset_op(struct rte_eth_dev *eth_dev)
244 {
245         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
246         unsigned int i;
247
248         if (!(bp->flags & BNXT_FLAG_INIT_DONE)) {
249                 PMD_DRV_LOG(ERR, "Device Initialization not complete!\n");
250                 return;
251         }
252
253         bnxt_clear_all_hwrm_stat_ctxs(bp);
254         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
255                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
256
257                 rte_atomic64_clear(&rxq->rx_mbuf_alloc_fail);
258         }
259 }
260
261 int bnxt_dev_xstats_get_op(struct rte_eth_dev *eth_dev,
262                            struct rte_eth_xstat *xstats, unsigned int n)
263 {
264         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
265
266         unsigned int count, i;
267         uint64_t tx_drop_pkts;
268
269         bnxt_hwrm_port_qstats(bp);
270         bnxt_hwrm_func_qstats_tx_drop(bp, 0xffff, &tx_drop_pkts);
271
272         count = RTE_DIM(bnxt_rx_stats_strings) +
273                 RTE_DIM(bnxt_tx_stats_strings) + 1; /* For tx_drop_pkts */
274
275         if (n < count)
276                 return count;
277
278         count = 0;
279         for (i = 0; i < RTE_DIM(bnxt_rx_stats_strings); i++) {
280                 uint64_t *rx_stats = (uint64_t *)bp->hw_rx_port_stats;
281                 xstats[count].value = rte_le_to_cpu_64(
282                                 *(uint64_t *)((char *)rx_stats +
283                                 bnxt_rx_stats_strings[i].offset));
284                 count++;
285         }
286
287         for (i = 0; i < RTE_DIM(bnxt_tx_stats_strings); i++) {
288                 uint64_t *tx_stats = (uint64_t *)bp->hw_tx_port_stats;
289                 xstats[count].value = rte_le_to_cpu_64(
290                                  *(uint64_t *)((char *)tx_stats +
291                                 bnxt_tx_stats_strings[i].offset));
292                 count++;
293         }
294
295         /* The Tx drop pkts aka the Anti spoof coounter */
296         xstats[count].value = rte_le_to_cpu_64(tx_drop_pkts);
297         count++;
298
299         return count;
300 }
301
302 int bnxt_dev_xstats_get_names_op(__rte_unused struct rte_eth_dev *eth_dev,
303         struct rte_eth_xstat_name *xstats_names,
304         __rte_unused unsigned int limit)
305 {
306         /* Account for the Tx drop pkts aka the Anti spoof counter */
307         const unsigned int stat_cnt = RTE_DIM(bnxt_rx_stats_strings) +
308                                 RTE_DIM(bnxt_tx_stats_strings) + 1;
309         unsigned int i, count;
310
311         if (xstats_names != NULL) {
312                 count = 0;
313
314                 for (i = 0; i < RTE_DIM(bnxt_rx_stats_strings); i++) {
315                         snprintf(xstats_names[count].name,
316                                 sizeof(xstats_names[count].name),
317                                 "%s",
318                                 bnxt_rx_stats_strings[i].name);
319                         count++;
320                 }
321
322                 for (i = 0; i < RTE_DIM(bnxt_tx_stats_strings); i++) {
323                         snprintf(xstats_names[count].name,
324                                 sizeof(xstats_names[count].name),
325                                 "%s",
326                                 bnxt_tx_stats_strings[i].name);
327                         count++;
328                 }
329
330                 snprintf(xstats_names[count].name,
331                                 sizeof(xstats_names[count].name),
332                                 "%s",
333                                 bnxt_func_stats_strings[4].name);
334                 count++;
335         }
336         return stat_cnt;
337 }
338
339 void bnxt_dev_xstats_reset_op(struct rte_eth_dev *eth_dev)
340 {
341         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
342
343         if (bp->flags & BNXT_FLAG_PORT_STATS && BNXT_SINGLE_PF(bp))
344                 bnxt_hwrm_port_clr_stats(bp);
345
346         if (BNXT_VF(bp))
347                 PMD_DRV_LOG(ERR, "Operation not supported on a VF device\n");
348         if (!BNXT_SINGLE_PF(bp))
349                 PMD_DRV_LOG(ERR, "Operation not supported on a MF device\n");
350         if (!(bp->flags & BNXT_FLAG_PORT_STATS))
351                 PMD_DRV_LOG(ERR, "Operation not supported\n");
352 }
353
354 int bnxt_dev_xstats_get_by_id_op(struct rte_eth_dev *dev, const uint64_t *ids,
355                 uint64_t *values, unsigned int limit)
356 {
357         /* Account for the Tx drop pkts aka the Anti spoof counter */
358         const unsigned int stat_cnt = RTE_DIM(bnxt_rx_stats_strings) +
359                                 RTE_DIM(bnxt_tx_stats_strings) + 1;
360         struct rte_eth_xstat xstats[stat_cnt];
361         uint64_t values_copy[stat_cnt];
362         uint16_t i;
363
364         if (!ids)
365                 return bnxt_dev_xstats_get_op(dev, xstats, stat_cnt);
366
367         bnxt_dev_xstats_get_by_id_op(dev, NULL, values_copy, stat_cnt);
368         for (i = 0; i < limit; i++) {
369                 if (ids[i] >= stat_cnt) {
370                         PMD_DRV_LOG(ERR, "id value isn't valid");
371                         return -1;
372                 }
373                 values[i] = values_copy[ids[i]];
374         }
375         return stat_cnt;
376 }
377
378 int bnxt_dev_xstats_get_names_by_id_op(struct rte_eth_dev *dev,
379                                 struct rte_eth_xstat_name *xstats_names,
380                                 const uint64_t *ids, unsigned int limit)
381 {
382         /* Account for the Tx drop pkts aka the Anti spoof counter */
383         const unsigned int stat_cnt = RTE_DIM(bnxt_rx_stats_strings) +
384                                 RTE_DIM(bnxt_tx_stats_strings) + 1;
385         struct rte_eth_xstat_name xstats_names_copy[stat_cnt];
386         uint16_t i;
387
388         if (!ids)
389                 return bnxt_dev_xstats_get_names_op(dev, xstats_names,
390                                                     stat_cnt);
391         bnxt_dev_xstats_get_names_by_id_op(dev, xstats_names_copy, NULL,
392                         stat_cnt);
393
394         for (i = 0; i < limit; i++) {
395                 if (ids[i] >= stat_cnt) {
396                         PMD_DRV_LOG(ERR, "id value isn't valid");
397                         return -1;
398                 }
399                 strcpy(xstats_names[i].name,
400                                 xstats_names_copy[ids[i]].name);
401         }
402         return stat_cnt;
403 }