net/mlx5e: Introducing new statistics rwlock
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_stats.c
1 /*
2  * Copyright (c) 2017, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include "en.h"
34 #include "en_accel/ipsec.h"
35 #include "en_accel/tls.h"
36
37 static const struct counter_desc sw_stats_desc[] = {
38         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
39         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
40         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
41         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
42         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_tso_packets) },
43         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_tso_bytes) },
44         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_tso_inner_packets) },
45         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_tso_inner_bytes) },
46         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_added_vlan_packets) },
47
48 #ifdef CONFIG_MLX5_EN_TLS
49         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_tls_ooo) },
50         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_tls_resync_bytes) },
51 #endif
52
53         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_lro_packets) },
54         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_lro_bytes) },
55         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_removed_vlan_packets) },
56         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_csum_unnecessary) },
57         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_csum_none) },
58         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_csum_complete) },
59         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_csum_unnecessary_inner) },
60         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_xdp_drop) },
61         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_xdp_tx) },
62         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_xdp_tx_full) },
63         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_csum_none) },
64         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_csum_partial) },
65         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_csum_partial_inner) },
66         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_queue_stopped) },
67         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_queue_wake) },
68         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_queue_dropped) },
69         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_xmit_more) },
70         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_cqe_err) },
71         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_recover) },
72         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_wqe_err) },
73         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_mpwqe_filler) },
74         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_buff_alloc_err) },
75         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_cqe_compress_blks) },
76         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_cqe_compress_pkts) },
77         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_page_reuse) },
78         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_cache_reuse) },
79         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_cache_full) },
80         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_cache_empty) },
81         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_cache_busy) },
82         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_cache_waive) },
83         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, ch_eq_rearm) },
84 };
85
86 #define NUM_SW_COUNTERS                 ARRAY_SIZE(sw_stats_desc)
87
88 static int mlx5e_grp_sw_get_num_stats(struct mlx5e_priv *priv)
89 {
90         return NUM_SW_COUNTERS;
91 }
92
93 static int mlx5e_grp_sw_fill_strings(struct mlx5e_priv *priv, u8 *data, int idx)
94 {
95         int i;
96
97         for (i = 0; i < NUM_SW_COUNTERS; i++)
98                 strcpy(data + (idx++) * ETH_GSTRING_LEN, sw_stats_desc[i].format);
99         return idx;
100 }
101
102 static int mlx5e_grp_sw_fill_stats(struct mlx5e_priv *priv, u64 *data, int idx)
103 {
104         int i;
105
106         for (i = 0; i < NUM_SW_COUNTERS; i++)
107                 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.sw, sw_stats_desc, i);
108         return idx;
109 }
110
111 void mlx5e_grp_sw_update_stats(struct mlx5e_priv *priv)
112 {
113         struct mlx5e_sw_stats temp, *s = &temp;
114         struct mlx5e_rq_stats *rq_stats;
115         struct mlx5e_sq_stats *sq_stats;
116         struct mlx5e_ch_stats *ch_stats;
117         int i, j;
118
119         memset(s, 0, sizeof(*s));
120         read_lock(&priv->stats_lock);
121         if (!priv->channels_active)
122                 goto out;
123         for (i = 0; i < priv->channels.num; i++) {
124                 struct mlx5e_channel *c = priv->channels.c[i];
125
126                 rq_stats = &c->rq.stats;
127                 ch_stats = &c->stats;
128
129                 s->rx_packets   += rq_stats->packets;
130                 s->rx_bytes     += rq_stats->bytes;
131                 s->rx_lro_packets += rq_stats->lro_packets;
132                 s->rx_lro_bytes += rq_stats->lro_bytes;
133                 s->rx_removed_vlan_packets += rq_stats->removed_vlan_packets;
134                 s->rx_csum_none += rq_stats->csum_none;
135                 s->rx_csum_complete += rq_stats->csum_complete;
136                 s->rx_csum_unnecessary += rq_stats->csum_unnecessary;
137                 s->rx_csum_unnecessary_inner += rq_stats->csum_unnecessary_inner;
138                 s->rx_xdp_drop += rq_stats->xdp_drop;
139                 s->rx_xdp_tx += rq_stats->xdp_tx;
140                 s->rx_xdp_tx_full += rq_stats->xdp_tx_full;
141                 s->rx_wqe_err   += rq_stats->wqe_err;
142                 s->rx_mpwqe_filler += rq_stats->mpwqe_filler;
143                 s->rx_buff_alloc_err += rq_stats->buff_alloc_err;
144                 s->rx_cqe_compress_blks += rq_stats->cqe_compress_blks;
145                 s->rx_cqe_compress_pkts += rq_stats->cqe_compress_pkts;
146                 s->rx_page_reuse  += rq_stats->page_reuse;
147                 s->rx_cache_reuse += rq_stats->cache_reuse;
148                 s->rx_cache_full  += rq_stats->cache_full;
149                 s->rx_cache_empty += rq_stats->cache_empty;
150                 s->rx_cache_busy  += rq_stats->cache_busy;
151                 s->rx_cache_waive += rq_stats->cache_waive;
152                 s->ch_eq_rearm += ch_stats->eq_rearm;
153
154                 for (j = 0; j < priv->channels.params.num_tc; j++) {
155                         sq_stats = &c->sq[j].stats;
156
157                         s->tx_packets           += sq_stats->packets;
158                         s->tx_bytes             += sq_stats->bytes;
159                         s->tx_tso_packets       += sq_stats->tso_packets;
160                         s->tx_tso_bytes         += sq_stats->tso_bytes;
161                         s->tx_tso_inner_packets += sq_stats->tso_inner_packets;
162                         s->tx_tso_inner_bytes   += sq_stats->tso_inner_bytes;
163                         s->tx_added_vlan_packets += sq_stats->added_vlan_packets;
164                         s->tx_queue_stopped     += sq_stats->stopped;
165                         s->tx_queue_wake        += sq_stats->wake;
166                         s->tx_queue_dropped     += sq_stats->dropped;
167                         s->tx_cqe_err           += sq_stats->cqe_err;
168                         s->tx_recover           += sq_stats->recover;
169                         s->tx_xmit_more         += sq_stats->xmit_more;
170                         s->tx_csum_partial_inner += sq_stats->csum_partial_inner;
171                         s->tx_csum_none         += sq_stats->csum_none;
172                         s->tx_csum_partial      += sq_stats->csum_partial;
173 #ifdef CONFIG_MLX5_EN_TLS
174                         s->tx_tls_ooo           += sq_stats->tls_ooo;
175                         s->tx_tls_resync_bytes  += sq_stats->tls_resync_bytes;
176 #endif
177                 }
178         }
179
180         memcpy(&priv->stats.sw, s, sizeof(*s));
181 out:
182         read_unlock(&priv->stats_lock);
183 }
184
185 static const struct counter_desc q_stats_desc[] = {
186         { MLX5E_DECLARE_STAT(struct mlx5e_qcounter_stats, rx_out_of_buffer) },
187 };
188
189 static const struct counter_desc drop_rq_stats_desc[] = {
190         { MLX5E_DECLARE_STAT(struct mlx5e_qcounter_stats, rx_if_down_packets) },
191 };
192
193 #define NUM_Q_COUNTERS                  ARRAY_SIZE(q_stats_desc)
194 #define NUM_DROP_RQ_COUNTERS            ARRAY_SIZE(drop_rq_stats_desc)
195
196 static int mlx5e_grp_q_get_num_stats(struct mlx5e_priv *priv)
197 {
198         int num_stats = 0;
199
200         if (priv->q_counter)
201                 num_stats += NUM_Q_COUNTERS;
202
203         if (priv->drop_rq_q_counter)
204                 num_stats += NUM_DROP_RQ_COUNTERS;
205
206         return num_stats;
207 }
208
209 static int mlx5e_grp_q_fill_strings(struct mlx5e_priv *priv, u8 *data, int idx)
210 {
211         int i;
212
213         for (i = 0; i < NUM_Q_COUNTERS && priv->q_counter; i++)
214                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
215                        q_stats_desc[i].format);
216
217         for (i = 0; i < NUM_DROP_RQ_COUNTERS && priv->drop_rq_q_counter; i++)
218                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
219                        drop_rq_stats_desc[i].format);
220
221         return idx;
222 }
223
224 static int mlx5e_grp_q_fill_stats(struct mlx5e_priv *priv, u64 *data, int idx)
225 {
226         int i;
227
228         for (i = 0; i < NUM_Q_COUNTERS && priv->q_counter; i++)
229                 data[idx++] = MLX5E_READ_CTR32_CPU(&priv->stats.qcnt,
230                                                    q_stats_desc, i);
231         for (i = 0; i < NUM_DROP_RQ_COUNTERS && priv->drop_rq_q_counter; i++)
232                 data[idx++] = MLX5E_READ_CTR32_CPU(&priv->stats.qcnt,
233                                                    drop_rq_stats_desc, i);
234         return idx;
235 }
236
237 static void mlx5e_grp_q_update_stats(struct mlx5e_priv *priv)
238 {
239         struct mlx5e_qcounter_stats *qcnt = &priv->stats.qcnt;
240         u32 out[MLX5_ST_SZ_DW(query_q_counter_out)];
241
242         if (priv->q_counter &&
243             !mlx5_core_query_q_counter(priv->mdev, priv->q_counter, 0, out,
244                                        sizeof(out)))
245                 qcnt->rx_out_of_buffer = MLX5_GET(query_q_counter_out,
246                                                   out, out_of_buffer);
247         if (priv->drop_rq_q_counter &&
248             !mlx5_core_query_q_counter(priv->mdev, priv->drop_rq_q_counter, 0,
249                                        out, sizeof(out)))
250                 qcnt->rx_if_down_packets = MLX5_GET(query_q_counter_out, out,
251                                                     out_of_buffer);
252 }
253
254 #define VNIC_ENV_OFF(c) MLX5_BYTE_OFF(query_vnic_env_out, c)
255 static const struct counter_desc vnic_env_stats_desc[] = {
256         { "rx_steer_missed_packets",
257                 VNIC_ENV_OFF(vport_env.nic_receive_steering_discard) },
258 };
259
260 #define NUM_VNIC_ENV_COUNTERS           ARRAY_SIZE(vnic_env_stats_desc)
261
262 static int mlx5e_grp_vnic_env_get_num_stats(struct mlx5e_priv *priv)
263 {
264         return MLX5_CAP_GEN(priv->mdev, nic_receive_steering_discard) ?
265                 NUM_VNIC_ENV_COUNTERS : 0;
266 }
267
268 static int mlx5e_grp_vnic_env_fill_strings(struct mlx5e_priv *priv, u8 *data,
269                                            int idx)
270 {
271         int i;
272
273         if (!MLX5_CAP_GEN(priv->mdev, nic_receive_steering_discard))
274                 return idx;
275
276         for (i = 0; i < NUM_VNIC_ENV_COUNTERS; i++)
277                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
278                        vnic_env_stats_desc[i].format);
279         return idx;
280 }
281
282 static int mlx5e_grp_vnic_env_fill_stats(struct mlx5e_priv *priv, u64 *data,
283                                          int idx)
284 {
285         int i;
286
287         if (!MLX5_CAP_GEN(priv->mdev, nic_receive_steering_discard))
288                 return idx;
289
290         for (i = 0; i < NUM_VNIC_ENV_COUNTERS; i++)
291                 data[idx++] = MLX5E_READ_CTR64_BE(priv->stats.vnic.query_vnic_env_out,
292                                                   vnic_env_stats_desc, i);
293         return idx;
294 }
295
296 static void mlx5e_grp_vnic_env_update_stats(struct mlx5e_priv *priv)
297 {
298         u32 *out = (u32 *)priv->stats.vnic.query_vnic_env_out;
299         int outlen = MLX5_ST_SZ_BYTES(query_vnic_env_out);
300         u32 in[MLX5_ST_SZ_DW(query_vnic_env_in)] = {0};
301         struct mlx5_core_dev *mdev = priv->mdev;
302
303         if (!MLX5_CAP_GEN(priv->mdev, nic_receive_steering_discard))
304                 return;
305
306         MLX5_SET(query_vnic_env_in, in, opcode,
307                  MLX5_CMD_OP_QUERY_VNIC_ENV);
308         MLX5_SET(query_vnic_env_in, in, op_mod, 0);
309         MLX5_SET(query_vnic_env_in, in, other_vport, 0);
310         mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
311 }
312
313 #define VPORT_COUNTER_OFF(c) MLX5_BYTE_OFF(query_vport_counter_out, c)
314 static const struct counter_desc vport_stats_desc[] = {
315         { "rx_vport_unicast_packets",
316                 VPORT_COUNTER_OFF(received_eth_unicast.packets) },
317         { "rx_vport_unicast_bytes",
318                 VPORT_COUNTER_OFF(received_eth_unicast.octets) },
319         { "tx_vport_unicast_packets",
320                 VPORT_COUNTER_OFF(transmitted_eth_unicast.packets) },
321         { "tx_vport_unicast_bytes",
322                 VPORT_COUNTER_OFF(transmitted_eth_unicast.octets) },
323         { "rx_vport_multicast_packets",
324                 VPORT_COUNTER_OFF(received_eth_multicast.packets) },
325         { "rx_vport_multicast_bytes",
326                 VPORT_COUNTER_OFF(received_eth_multicast.octets) },
327         { "tx_vport_multicast_packets",
328                 VPORT_COUNTER_OFF(transmitted_eth_multicast.packets) },
329         { "tx_vport_multicast_bytes",
330                 VPORT_COUNTER_OFF(transmitted_eth_multicast.octets) },
331         { "rx_vport_broadcast_packets",
332                 VPORT_COUNTER_OFF(received_eth_broadcast.packets) },
333         { "rx_vport_broadcast_bytes",
334                 VPORT_COUNTER_OFF(received_eth_broadcast.octets) },
335         { "tx_vport_broadcast_packets",
336                 VPORT_COUNTER_OFF(transmitted_eth_broadcast.packets) },
337         { "tx_vport_broadcast_bytes",
338                 VPORT_COUNTER_OFF(transmitted_eth_broadcast.octets) },
339         { "rx_vport_rdma_unicast_packets",
340                 VPORT_COUNTER_OFF(received_ib_unicast.packets) },
341         { "rx_vport_rdma_unicast_bytes",
342                 VPORT_COUNTER_OFF(received_ib_unicast.octets) },
343         { "tx_vport_rdma_unicast_packets",
344                 VPORT_COUNTER_OFF(transmitted_ib_unicast.packets) },
345         { "tx_vport_rdma_unicast_bytes",
346                 VPORT_COUNTER_OFF(transmitted_ib_unicast.octets) },
347         { "rx_vport_rdma_multicast_packets",
348                 VPORT_COUNTER_OFF(received_ib_multicast.packets) },
349         { "rx_vport_rdma_multicast_bytes",
350                 VPORT_COUNTER_OFF(received_ib_multicast.octets) },
351         { "tx_vport_rdma_multicast_packets",
352                 VPORT_COUNTER_OFF(transmitted_ib_multicast.packets) },
353         { "tx_vport_rdma_multicast_bytes",
354                 VPORT_COUNTER_OFF(transmitted_ib_multicast.octets) },
355 };
356
357 #define NUM_VPORT_COUNTERS              ARRAY_SIZE(vport_stats_desc)
358
359 static int mlx5e_grp_vport_get_num_stats(struct mlx5e_priv *priv)
360 {
361         return NUM_VPORT_COUNTERS;
362 }
363
364 static int mlx5e_grp_vport_fill_strings(struct mlx5e_priv *priv, u8 *data,
365                                         int idx)
366 {
367         int i;
368
369         for (i = 0; i < NUM_VPORT_COUNTERS; i++)
370                 strcpy(data + (idx++) * ETH_GSTRING_LEN, vport_stats_desc[i].format);
371         return idx;
372 }
373
374 static int mlx5e_grp_vport_fill_stats(struct mlx5e_priv *priv, u64 *data,
375                                       int idx)
376 {
377         int i;
378
379         for (i = 0; i < NUM_VPORT_COUNTERS; i++)
380                 data[idx++] = MLX5E_READ_CTR64_BE(priv->stats.vport.query_vport_out,
381                                                   vport_stats_desc, i);
382         return idx;
383 }
384
385 static void mlx5e_grp_vport_update_stats(struct mlx5e_priv *priv)
386 {
387         int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
388         u32 *out = (u32 *)priv->stats.vport.query_vport_out;
389         u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {0};
390         struct mlx5_core_dev *mdev = priv->mdev;
391
392         MLX5_SET(query_vport_counter_in, in, opcode, MLX5_CMD_OP_QUERY_VPORT_COUNTER);
393         MLX5_SET(query_vport_counter_in, in, op_mod, 0);
394         MLX5_SET(query_vport_counter_in, in, other_vport, 0);
395         mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
396 }
397
398 #define PPORT_802_3_OFF(c) \
399         MLX5_BYTE_OFF(ppcnt_reg, \
400                       counter_set.eth_802_3_cntrs_grp_data_layout.c##_high)
401 static const struct counter_desc pport_802_3_stats_desc[] = {
402         { "tx_packets_phy", PPORT_802_3_OFF(a_frames_transmitted_ok) },
403         { "rx_packets_phy", PPORT_802_3_OFF(a_frames_received_ok) },
404         { "rx_crc_errors_phy", PPORT_802_3_OFF(a_frame_check_sequence_errors) },
405         { "tx_bytes_phy", PPORT_802_3_OFF(a_octets_transmitted_ok) },
406         { "rx_bytes_phy", PPORT_802_3_OFF(a_octets_received_ok) },
407         { "tx_multicast_phy", PPORT_802_3_OFF(a_multicast_frames_xmitted_ok) },
408         { "tx_broadcast_phy", PPORT_802_3_OFF(a_broadcast_frames_xmitted_ok) },
409         { "rx_multicast_phy", PPORT_802_3_OFF(a_multicast_frames_received_ok) },
410         { "rx_broadcast_phy", PPORT_802_3_OFF(a_broadcast_frames_received_ok) },
411         { "rx_in_range_len_errors_phy", PPORT_802_3_OFF(a_in_range_length_errors) },
412         { "rx_out_of_range_len_phy", PPORT_802_3_OFF(a_out_of_range_length_field) },
413         { "rx_oversize_pkts_phy", PPORT_802_3_OFF(a_frame_too_long_errors) },
414         { "rx_symbol_err_phy", PPORT_802_3_OFF(a_symbol_error_during_carrier) },
415         { "tx_mac_control_phy", PPORT_802_3_OFF(a_mac_control_frames_transmitted) },
416         { "rx_mac_control_phy", PPORT_802_3_OFF(a_mac_control_frames_received) },
417         { "rx_unsupported_op_phy", PPORT_802_3_OFF(a_unsupported_opcodes_received) },
418         { "rx_pause_ctrl_phy", PPORT_802_3_OFF(a_pause_mac_ctrl_frames_received) },
419         { "tx_pause_ctrl_phy", PPORT_802_3_OFF(a_pause_mac_ctrl_frames_transmitted) },
420 };
421
422 #define NUM_PPORT_802_3_COUNTERS        ARRAY_SIZE(pport_802_3_stats_desc)
423
424 static int mlx5e_grp_802_3_get_num_stats(struct mlx5e_priv *priv)
425 {
426         return NUM_PPORT_802_3_COUNTERS;
427 }
428
429 static int mlx5e_grp_802_3_fill_strings(struct mlx5e_priv *priv, u8 *data,
430                                         int idx)
431 {
432         int i;
433
434         for (i = 0; i < NUM_PPORT_802_3_COUNTERS; i++)
435                 strcpy(data + (idx++) * ETH_GSTRING_LEN, pport_802_3_stats_desc[i].format);
436         return idx;
437 }
438
439 static int mlx5e_grp_802_3_fill_stats(struct mlx5e_priv *priv, u64 *data,
440                                       int idx)
441 {
442         int i;
443
444         for (i = 0; i < NUM_PPORT_802_3_COUNTERS; i++)
445                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.IEEE_802_3_counters,
446                                                   pport_802_3_stats_desc, i);
447         return idx;
448 }
449
450 static void mlx5e_grp_802_3_update_stats(struct mlx5e_priv *priv)
451 {
452         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
453         struct mlx5_core_dev *mdev = priv->mdev;
454         u32 in[MLX5_ST_SZ_DW(ppcnt_reg)] = {0};
455         int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
456         void *out;
457
458         MLX5_SET(ppcnt_reg, in, local_port, 1);
459         out = pstats->IEEE_802_3_counters;
460         MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
461         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
462 }
463
464 #define PPORT_2863_OFF(c) \
465         MLX5_BYTE_OFF(ppcnt_reg, \
466                       counter_set.eth_2863_cntrs_grp_data_layout.c##_high)
467 static const struct counter_desc pport_2863_stats_desc[] = {
468         { "rx_discards_phy", PPORT_2863_OFF(if_in_discards) },
469         { "tx_discards_phy", PPORT_2863_OFF(if_out_discards) },
470         { "tx_errors_phy", PPORT_2863_OFF(if_out_errors) },
471 };
472
473 #define NUM_PPORT_2863_COUNTERS         ARRAY_SIZE(pport_2863_stats_desc)
474
475 static int mlx5e_grp_2863_get_num_stats(struct mlx5e_priv *priv)
476 {
477         return NUM_PPORT_2863_COUNTERS;
478 }
479
480 static int mlx5e_grp_2863_fill_strings(struct mlx5e_priv *priv, u8 *data,
481                                        int idx)
482 {
483         int i;
484
485         for (i = 0; i < NUM_PPORT_2863_COUNTERS; i++)
486                 strcpy(data + (idx++) * ETH_GSTRING_LEN, pport_2863_stats_desc[i].format);
487         return idx;
488 }
489
490 static int mlx5e_grp_2863_fill_stats(struct mlx5e_priv *priv, u64 *data,
491                                      int idx)
492 {
493         int i;
494
495         for (i = 0; i < NUM_PPORT_2863_COUNTERS; i++)
496                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.RFC_2863_counters,
497                                                   pport_2863_stats_desc, i);
498         return idx;
499 }
500
501 static void mlx5e_grp_2863_update_stats(struct mlx5e_priv *priv)
502 {
503         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
504         struct mlx5_core_dev *mdev = priv->mdev;
505         u32 in[MLX5_ST_SZ_DW(ppcnt_reg)] = {0};
506         int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
507         void *out;
508
509         MLX5_SET(ppcnt_reg, in, local_port, 1);
510         out = pstats->RFC_2863_counters;
511         MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
512         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
513 }
514
515 #define PPORT_2819_OFF(c) \
516         MLX5_BYTE_OFF(ppcnt_reg, \
517                       counter_set.eth_2819_cntrs_grp_data_layout.c##_high)
518 static const struct counter_desc pport_2819_stats_desc[] = {
519         { "rx_undersize_pkts_phy", PPORT_2819_OFF(ether_stats_undersize_pkts) },
520         { "rx_fragments_phy", PPORT_2819_OFF(ether_stats_fragments) },
521         { "rx_jabbers_phy", PPORT_2819_OFF(ether_stats_jabbers) },
522         { "rx_64_bytes_phy", PPORT_2819_OFF(ether_stats_pkts64octets) },
523         { "rx_65_to_127_bytes_phy", PPORT_2819_OFF(ether_stats_pkts65to127octets) },
524         { "rx_128_to_255_bytes_phy", PPORT_2819_OFF(ether_stats_pkts128to255octets) },
525         { "rx_256_to_511_bytes_phy", PPORT_2819_OFF(ether_stats_pkts256to511octets) },
526         { "rx_512_to_1023_bytes_phy", PPORT_2819_OFF(ether_stats_pkts512to1023octets) },
527         { "rx_1024_to_1518_bytes_phy", PPORT_2819_OFF(ether_stats_pkts1024to1518octets) },
528         { "rx_1519_to_2047_bytes_phy", PPORT_2819_OFF(ether_stats_pkts1519to2047octets) },
529         { "rx_2048_to_4095_bytes_phy", PPORT_2819_OFF(ether_stats_pkts2048to4095octets) },
530         { "rx_4096_to_8191_bytes_phy", PPORT_2819_OFF(ether_stats_pkts4096to8191octets) },
531         { "rx_8192_to_10239_bytes_phy", PPORT_2819_OFF(ether_stats_pkts8192to10239octets) },
532 };
533
534 #define NUM_PPORT_2819_COUNTERS         ARRAY_SIZE(pport_2819_stats_desc)
535
536 static int mlx5e_grp_2819_get_num_stats(struct mlx5e_priv *priv)
537 {
538         return NUM_PPORT_2819_COUNTERS;
539 }
540
541 static int mlx5e_grp_2819_fill_strings(struct mlx5e_priv *priv, u8 *data,
542                                        int idx)
543 {
544         int i;
545
546         for (i = 0; i < NUM_PPORT_2819_COUNTERS; i++)
547                 strcpy(data + (idx++) * ETH_GSTRING_LEN, pport_2819_stats_desc[i].format);
548         return idx;
549 }
550
551 static int mlx5e_grp_2819_fill_stats(struct mlx5e_priv *priv, u64 *data,
552                                      int idx)
553 {
554         int i;
555
556         for (i = 0; i < NUM_PPORT_2819_COUNTERS; i++)
557                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.RFC_2819_counters,
558                                                   pport_2819_stats_desc, i);
559         return idx;
560 }
561
562 static void mlx5e_grp_2819_update_stats(struct mlx5e_priv *priv)
563 {
564         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
565         struct mlx5_core_dev *mdev = priv->mdev;
566         u32 in[MLX5_ST_SZ_DW(ppcnt_reg)] = {0};
567         int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
568         void *out;
569
570         MLX5_SET(ppcnt_reg, in, local_port, 1);
571         out = pstats->RFC_2819_counters;
572         MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
573         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
574 }
575
576 #define PPORT_PHY_STATISTICAL_OFF(c) \
577         MLX5_BYTE_OFF(ppcnt_reg, \
578                       counter_set.phys_layer_statistical_cntrs.c##_high)
579 static const struct counter_desc pport_phy_statistical_stats_desc[] = {
580         { "rx_pcs_symbol_err_phy", PPORT_PHY_STATISTICAL_OFF(phy_symbol_errors) },
581         { "rx_corrected_bits_phy", PPORT_PHY_STATISTICAL_OFF(phy_corrected_bits) },
582 };
583
584 #define NUM_PPORT_PHY_STATISTICAL_COUNTERS ARRAY_SIZE(pport_phy_statistical_stats_desc)
585
586 static int mlx5e_grp_phy_get_num_stats(struct mlx5e_priv *priv)
587 {
588         /* "1" for link_down_events special counter */
589         return MLX5_CAP_PCAM_FEATURE((priv)->mdev, ppcnt_statistical_group) ?
590                 NUM_PPORT_PHY_STATISTICAL_COUNTERS + 1 : 1;
591 }
592
593 static int mlx5e_grp_phy_fill_strings(struct mlx5e_priv *priv, u8 *data,
594                                       int idx)
595 {
596         int i;
597
598         strcpy(data + (idx++) * ETH_GSTRING_LEN, "link_down_events_phy");
599
600         if (!MLX5_CAP_PCAM_FEATURE((priv)->mdev, ppcnt_statistical_group))
601                 return idx;
602
603         for (i = 0; i < NUM_PPORT_PHY_STATISTICAL_COUNTERS; i++)
604                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
605                        pport_phy_statistical_stats_desc[i].format);
606         return idx;
607 }
608
609 static int mlx5e_grp_phy_fill_stats(struct mlx5e_priv *priv, u64 *data, int idx)
610 {
611         int i;
612
613         /* link_down_events_phy has special handling since it is not stored in __be64 format */
614         data[idx++] = MLX5_GET(ppcnt_reg, priv->stats.pport.phy_counters,
615                                counter_set.phys_layer_cntrs.link_down_events);
616
617         if (!MLX5_CAP_PCAM_FEATURE((priv)->mdev, ppcnt_statistical_group))
618                 return idx;
619
620         for (i = 0; i < NUM_PPORT_PHY_STATISTICAL_COUNTERS; i++)
621                 data[idx++] =
622                         MLX5E_READ_CTR64_BE(&priv->stats.pport.phy_statistical_counters,
623                                             pport_phy_statistical_stats_desc, i);
624         return idx;
625 }
626
627 static void mlx5e_grp_phy_update_stats(struct mlx5e_priv *priv)
628 {
629         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
630         struct mlx5_core_dev *mdev = priv->mdev;
631         u32 in[MLX5_ST_SZ_DW(ppcnt_reg)] = {0};
632         int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
633         void *out;
634
635         MLX5_SET(ppcnt_reg, in, local_port, 1);
636         out = pstats->phy_counters;
637         MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
638         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
639
640         if (!MLX5_CAP_PCAM_FEATURE(mdev, ppcnt_statistical_group))
641                 return;
642
643         out = pstats->phy_statistical_counters;
644         MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_STATISTICAL_GROUP);
645         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
646 }
647
648 #define PPORT_ETH_EXT_OFF(c) \
649         MLX5_BYTE_OFF(ppcnt_reg, \
650                       counter_set.eth_extended_cntrs_grp_data_layout.c##_high)
651 static const struct counter_desc pport_eth_ext_stats_desc[] = {
652         { "rx_buffer_passed_thres_phy", PPORT_ETH_EXT_OFF(rx_buffer_almost_full) },
653 };
654
655 #define NUM_PPORT_ETH_EXT_COUNTERS      ARRAY_SIZE(pport_eth_ext_stats_desc)
656
657 static int mlx5e_grp_eth_ext_get_num_stats(struct mlx5e_priv *priv)
658 {
659         if (MLX5_CAP_PCAM_FEATURE((priv)->mdev, rx_buffer_fullness_counters))
660                 return NUM_PPORT_ETH_EXT_COUNTERS;
661
662         return 0;
663 }
664
665 static int mlx5e_grp_eth_ext_fill_strings(struct mlx5e_priv *priv, u8 *data,
666                                           int idx)
667 {
668         int i;
669
670         if (MLX5_CAP_PCAM_FEATURE((priv)->mdev, rx_buffer_fullness_counters))
671                 for (i = 0; i < NUM_PPORT_ETH_EXT_COUNTERS; i++)
672                         strcpy(data + (idx++) * ETH_GSTRING_LEN,
673                                pport_eth_ext_stats_desc[i].format);
674         return idx;
675 }
676
677 static int mlx5e_grp_eth_ext_fill_stats(struct mlx5e_priv *priv, u64 *data,
678                                         int idx)
679 {
680         int i;
681
682         if (MLX5_CAP_PCAM_FEATURE((priv)->mdev, rx_buffer_fullness_counters))
683                 for (i = 0; i < NUM_PPORT_ETH_EXT_COUNTERS; i++)
684                         data[idx++] =
685                                 MLX5E_READ_CTR64_BE(&priv->stats.pport.eth_ext_counters,
686                                                     pport_eth_ext_stats_desc, i);
687         return idx;
688 }
689
690 static void mlx5e_grp_eth_ext_update_stats(struct mlx5e_priv *priv)
691 {
692         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
693         struct mlx5_core_dev *mdev = priv->mdev;
694         u32 in[MLX5_ST_SZ_DW(ppcnt_reg)] = {0};
695         int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
696         void *out;
697
698         if (!MLX5_CAP_PCAM_FEATURE(mdev, rx_buffer_fullness_counters))
699                 return;
700
701         MLX5_SET(ppcnt_reg, in, local_port, 1);
702         out = pstats->eth_ext_counters;
703         MLX5_SET(ppcnt_reg, in, grp, MLX5_ETHERNET_EXTENDED_COUNTERS_GROUP);
704         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
705 }
706
707 #define PCIE_PERF_OFF(c) \
708         MLX5_BYTE_OFF(mpcnt_reg, counter_set.pcie_perf_cntrs_grp_data_layout.c)
709 static const struct counter_desc pcie_perf_stats_desc[] = {
710         { "rx_pci_signal_integrity", PCIE_PERF_OFF(rx_errors) },
711         { "tx_pci_signal_integrity", PCIE_PERF_OFF(tx_errors) },
712 };
713
714 #define PCIE_PERF_OFF64(c) \
715         MLX5_BYTE_OFF(mpcnt_reg, counter_set.pcie_perf_cntrs_grp_data_layout.c##_high)
716 static const struct counter_desc pcie_perf_stats_desc64[] = {
717         { "outbound_pci_buffer_overflow", PCIE_PERF_OFF64(tx_overflow_buffer_pkt) },
718 };
719
720 static const struct counter_desc pcie_perf_stall_stats_desc[] = {
721         { "outbound_pci_stalled_rd", PCIE_PERF_OFF(outbound_stalled_reads) },
722         { "outbound_pci_stalled_wr", PCIE_PERF_OFF(outbound_stalled_writes) },
723         { "outbound_pci_stalled_rd_events", PCIE_PERF_OFF(outbound_stalled_reads_events) },
724         { "outbound_pci_stalled_wr_events", PCIE_PERF_OFF(outbound_stalled_writes_events) },
725 };
726
727 #define NUM_PCIE_PERF_COUNTERS          ARRAY_SIZE(pcie_perf_stats_desc)
728 #define NUM_PCIE_PERF_COUNTERS64        ARRAY_SIZE(pcie_perf_stats_desc64)
729 #define NUM_PCIE_PERF_STALL_COUNTERS    ARRAY_SIZE(pcie_perf_stall_stats_desc)
730
731 static int mlx5e_grp_pcie_get_num_stats(struct mlx5e_priv *priv)
732 {
733         int num_stats = 0;
734
735         if (MLX5_CAP_MCAM_FEATURE((priv)->mdev, pcie_performance_group))
736                 num_stats += NUM_PCIE_PERF_COUNTERS;
737
738         if (MLX5_CAP_MCAM_FEATURE((priv)->mdev, tx_overflow_buffer_pkt))
739                 num_stats += NUM_PCIE_PERF_COUNTERS64;
740
741         if (MLX5_CAP_MCAM_FEATURE((priv)->mdev, pcie_outbound_stalled))
742                 num_stats += NUM_PCIE_PERF_STALL_COUNTERS;
743
744         return num_stats;
745 }
746
747 static int mlx5e_grp_pcie_fill_strings(struct mlx5e_priv *priv, u8 *data,
748                                        int idx)
749 {
750         int i;
751
752         if (MLX5_CAP_MCAM_FEATURE((priv)->mdev, pcie_performance_group))
753                 for (i = 0; i < NUM_PCIE_PERF_COUNTERS; i++)
754                         strcpy(data + (idx++) * ETH_GSTRING_LEN,
755                                pcie_perf_stats_desc[i].format);
756
757         if (MLX5_CAP_MCAM_FEATURE((priv)->mdev, tx_overflow_buffer_pkt))
758                 for (i = 0; i < NUM_PCIE_PERF_COUNTERS64; i++)
759                         strcpy(data + (idx++) * ETH_GSTRING_LEN,
760                                pcie_perf_stats_desc64[i].format);
761
762         if (MLX5_CAP_MCAM_FEATURE((priv)->mdev, pcie_outbound_stalled))
763                 for (i = 0; i < NUM_PCIE_PERF_STALL_COUNTERS; i++)
764                         strcpy(data + (idx++) * ETH_GSTRING_LEN,
765                                pcie_perf_stall_stats_desc[i].format);
766         return idx;
767 }
768
769 static int mlx5e_grp_pcie_fill_stats(struct mlx5e_priv *priv, u64 *data,
770                                      int idx)
771 {
772         int i;
773
774         if (MLX5_CAP_MCAM_FEATURE((priv)->mdev, pcie_performance_group))
775                 for (i = 0; i < NUM_PCIE_PERF_COUNTERS; i++)
776                         data[idx++] =
777                                 MLX5E_READ_CTR32_BE(&priv->stats.pcie.pcie_perf_counters,
778                                                     pcie_perf_stats_desc, i);
779
780         if (MLX5_CAP_MCAM_FEATURE((priv)->mdev, tx_overflow_buffer_pkt))
781                 for (i = 0; i < NUM_PCIE_PERF_COUNTERS64; i++)
782                         data[idx++] =
783                                 MLX5E_READ_CTR64_BE(&priv->stats.pcie.pcie_perf_counters,
784                                                     pcie_perf_stats_desc64, i);
785
786         if (MLX5_CAP_MCAM_FEATURE((priv)->mdev, pcie_outbound_stalled))
787                 for (i = 0; i < NUM_PCIE_PERF_STALL_COUNTERS; i++)
788                         data[idx++] =
789                                 MLX5E_READ_CTR32_BE(&priv->stats.pcie.pcie_perf_counters,
790                                                     pcie_perf_stall_stats_desc, i);
791         return idx;
792 }
793
794 static void mlx5e_grp_pcie_update_stats(struct mlx5e_priv *priv)
795 {
796         struct mlx5e_pcie_stats *pcie_stats = &priv->stats.pcie;
797         struct mlx5_core_dev *mdev = priv->mdev;
798         u32 in[MLX5_ST_SZ_DW(mpcnt_reg)] = {0};
799         int sz = MLX5_ST_SZ_BYTES(mpcnt_reg);
800         void *out;
801
802         if (!MLX5_CAP_MCAM_FEATURE(mdev, pcie_performance_group))
803                 return;
804
805         out = pcie_stats->pcie_perf_counters;
806         MLX5_SET(mpcnt_reg, in, grp, MLX5_PCIE_PERFORMANCE_COUNTERS_GROUP);
807         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_MPCNT, 0, 0);
808 }
809
810 #define PPORT_PER_PRIO_OFF(c) \
811         MLX5_BYTE_OFF(ppcnt_reg, \
812                       counter_set.eth_per_prio_grp_data_layout.c##_high)
813 static const struct counter_desc pport_per_prio_traffic_stats_desc[] = {
814         { "rx_prio%d_bytes", PPORT_PER_PRIO_OFF(rx_octets) },
815         { "rx_prio%d_packets", PPORT_PER_PRIO_OFF(rx_frames) },
816         { "tx_prio%d_bytes", PPORT_PER_PRIO_OFF(tx_octets) },
817         { "tx_prio%d_packets", PPORT_PER_PRIO_OFF(tx_frames) },
818 };
819
820 #define NUM_PPORT_PER_PRIO_TRAFFIC_COUNTERS     ARRAY_SIZE(pport_per_prio_traffic_stats_desc)
821
822 static int mlx5e_grp_per_prio_traffic_get_num_stats(struct mlx5e_priv *priv)
823 {
824         return NUM_PPORT_PER_PRIO_TRAFFIC_COUNTERS * NUM_PPORT_PRIO;
825 }
826
827 static int mlx5e_grp_per_prio_traffic_fill_strings(struct mlx5e_priv *priv,
828                                                    u8 *data,
829                                                    int idx)
830 {
831         int i, prio;
832
833         for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
834                 for (i = 0; i < NUM_PPORT_PER_PRIO_TRAFFIC_COUNTERS; i++)
835                         sprintf(data + (idx++) * ETH_GSTRING_LEN,
836                                 pport_per_prio_traffic_stats_desc[i].format, prio);
837         }
838
839         return idx;
840 }
841
842 static int mlx5e_grp_per_prio_traffic_fill_stats(struct mlx5e_priv *priv,
843                                                  u64 *data,
844                                                  int idx)
845 {
846         int i, prio;
847
848         for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
849                 for (i = 0; i < NUM_PPORT_PER_PRIO_TRAFFIC_COUNTERS; i++)
850                         data[idx++] =
851                                 MLX5E_READ_CTR64_BE(&priv->stats.pport.per_prio_counters[prio],
852                                                     pport_per_prio_traffic_stats_desc, i);
853         }
854
855         return idx;
856 }
857
858 static const struct counter_desc pport_per_prio_pfc_stats_desc[] = {
859         /* %s is "global" or "prio{i}" */
860         { "rx_%s_pause", PPORT_PER_PRIO_OFF(rx_pause) },
861         { "rx_%s_pause_duration", PPORT_PER_PRIO_OFF(rx_pause_duration) },
862         { "tx_%s_pause", PPORT_PER_PRIO_OFF(tx_pause) },
863         { "tx_%s_pause_duration", PPORT_PER_PRIO_OFF(tx_pause_duration) },
864         { "rx_%s_pause_transition", PPORT_PER_PRIO_OFF(rx_pause_transition) },
865 };
866
867 static const struct counter_desc pport_pfc_stall_stats_desc[] = {
868         { "tx_pause_storm_warning_events ", PPORT_PER_PRIO_OFF(device_stall_minor_watermark_cnt) },
869         { "tx_pause_storm_error_events", PPORT_PER_PRIO_OFF(device_stall_critical_watermark_cnt) },
870 };
871
872 #define NUM_PPORT_PER_PRIO_PFC_COUNTERS         ARRAY_SIZE(pport_per_prio_pfc_stats_desc)
873 #define NUM_PPORT_PFC_STALL_COUNTERS(priv)      (ARRAY_SIZE(pport_pfc_stall_stats_desc) * \
874                                                  MLX5_CAP_PCAM_FEATURE((priv)->mdev, pfcc_mask) * \
875                                                  MLX5_CAP_DEBUG((priv)->mdev, stall_detect))
876
877 static unsigned long mlx5e_query_pfc_combined(struct mlx5e_priv *priv)
878 {
879         struct mlx5_core_dev *mdev = priv->mdev;
880         u8 pfc_en_tx;
881         u8 pfc_en_rx;
882         int err;
883
884         if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
885                 return 0;
886
887         err = mlx5_query_port_pfc(mdev, &pfc_en_tx, &pfc_en_rx);
888
889         return err ? 0 : pfc_en_tx | pfc_en_rx;
890 }
891
892 static bool mlx5e_query_global_pause_combined(struct mlx5e_priv *priv)
893 {
894         struct mlx5_core_dev *mdev = priv->mdev;
895         u32 rx_pause;
896         u32 tx_pause;
897         int err;
898
899         if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
900                 return false;
901
902         err = mlx5_query_port_pause(mdev, &rx_pause, &tx_pause);
903
904         return err ? false : rx_pause | tx_pause;
905 }
906
907 static int mlx5e_grp_per_prio_pfc_get_num_stats(struct mlx5e_priv *priv)
908 {
909         return (mlx5e_query_global_pause_combined(priv) +
910                 hweight8(mlx5e_query_pfc_combined(priv))) *
911                 NUM_PPORT_PER_PRIO_PFC_COUNTERS +
912                 NUM_PPORT_PFC_STALL_COUNTERS(priv);
913 }
914
915 static int mlx5e_grp_per_prio_pfc_fill_strings(struct mlx5e_priv *priv,
916                                                u8 *data,
917                                                int idx)
918 {
919         unsigned long pfc_combined;
920         int i, prio;
921
922         pfc_combined = mlx5e_query_pfc_combined(priv);
923         for_each_set_bit(prio, &pfc_combined, NUM_PPORT_PRIO) {
924                 for (i = 0; i < NUM_PPORT_PER_PRIO_PFC_COUNTERS; i++) {
925                         char pfc_string[ETH_GSTRING_LEN];
926
927                         snprintf(pfc_string, sizeof(pfc_string), "prio%d", prio);
928                         sprintf(data + (idx++) * ETH_GSTRING_LEN,
929                                 pport_per_prio_pfc_stats_desc[i].format, pfc_string);
930                 }
931         }
932
933         if (mlx5e_query_global_pause_combined(priv)) {
934                 for (i = 0; i < NUM_PPORT_PER_PRIO_PFC_COUNTERS; i++) {
935                         sprintf(data + (idx++) * ETH_GSTRING_LEN,
936                                 pport_per_prio_pfc_stats_desc[i].format, "global");
937                 }
938         }
939
940         for (i = 0; i < NUM_PPORT_PFC_STALL_COUNTERS(priv); i++)
941                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
942                        pport_pfc_stall_stats_desc[i].format);
943
944         return idx;
945 }
946
947 static int mlx5e_grp_per_prio_pfc_fill_stats(struct mlx5e_priv *priv,
948                                              u64 *data,
949                                              int idx)
950 {
951         unsigned long pfc_combined;
952         int i, prio;
953
954         pfc_combined = mlx5e_query_pfc_combined(priv);
955         for_each_set_bit(prio, &pfc_combined, NUM_PPORT_PRIO) {
956                 for (i = 0; i < NUM_PPORT_PER_PRIO_PFC_COUNTERS; i++) {
957                         data[idx++] =
958                                 MLX5E_READ_CTR64_BE(&priv->stats.pport.per_prio_counters[prio],
959                                                     pport_per_prio_pfc_stats_desc, i);
960                 }
961         }
962
963         if (mlx5e_query_global_pause_combined(priv)) {
964                 for (i = 0; i < NUM_PPORT_PER_PRIO_PFC_COUNTERS; i++) {
965                         data[idx++] =
966                                 MLX5E_READ_CTR64_BE(&priv->stats.pport.per_prio_counters[0],
967                                                     pport_per_prio_pfc_stats_desc, i);
968                 }
969         }
970
971         for (i = 0; i < NUM_PPORT_PFC_STALL_COUNTERS(priv); i++)
972                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.per_prio_counters[0],
973                                                   pport_pfc_stall_stats_desc, i);
974
975         return idx;
976 }
977
978 static int mlx5e_grp_per_prio_get_num_stats(struct mlx5e_priv *priv)
979 {
980         return mlx5e_grp_per_prio_traffic_get_num_stats(priv) +
981                 mlx5e_grp_per_prio_pfc_get_num_stats(priv);
982 }
983
984 static int mlx5e_grp_per_prio_fill_strings(struct mlx5e_priv *priv, u8 *data,
985                                            int idx)
986 {
987         idx = mlx5e_grp_per_prio_traffic_fill_strings(priv, data, idx);
988         idx = mlx5e_grp_per_prio_pfc_fill_strings(priv, data, idx);
989         return idx;
990 }
991
992 static int mlx5e_grp_per_prio_fill_stats(struct mlx5e_priv *priv, u64 *data,
993                                          int idx)
994 {
995         idx = mlx5e_grp_per_prio_traffic_fill_stats(priv, data, idx);
996         idx = mlx5e_grp_per_prio_pfc_fill_stats(priv, data, idx);
997         return idx;
998 }
999
1000 static void mlx5e_grp_per_prio_update_stats(struct mlx5e_priv *priv)
1001 {
1002         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
1003         struct mlx5_core_dev *mdev = priv->mdev;
1004         u32 in[MLX5_ST_SZ_DW(ppcnt_reg)] = {0};
1005         int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
1006         int prio;
1007         void *out;
1008
1009         MLX5_SET(ppcnt_reg, in, local_port, 1);
1010         MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
1011         for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
1012                 out = pstats->per_prio_counters[prio];
1013                 MLX5_SET(ppcnt_reg, in, prio_tc, prio);
1014                 mlx5_core_access_reg(mdev, in, sz, out, sz,
1015                                      MLX5_REG_PPCNT, 0, 0);
1016         }
1017 }
1018
1019 static const struct counter_desc mlx5e_pme_status_desc[] = {
1020         { "module_unplug", 8 },
1021 };
1022
1023 static const struct counter_desc mlx5e_pme_error_desc[] = {
1024         { "module_bus_stuck", 16 },       /* bus stuck (I2C or data shorted) */
1025         { "module_high_temp", 48 },       /* high temperature */
1026         { "module_bad_shorted", 56 },    /* bad or shorted cable/module */
1027 };
1028
1029 #define NUM_PME_STATUS_STATS            ARRAY_SIZE(mlx5e_pme_status_desc)
1030 #define NUM_PME_ERR_STATS               ARRAY_SIZE(mlx5e_pme_error_desc)
1031
1032 static int mlx5e_grp_pme_get_num_stats(struct mlx5e_priv *priv)
1033 {
1034         return NUM_PME_STATUS_STATS + NUM_PME_ERR_STATS;
1035 }
1036
1037 static int mlx5e_grp_pme_fill_strings(struct mlx5e_priv *priv, u8 *data,
1038                                       int idx)
1039 {
1040         int i;
1041
1042         for (i = 0; i < NUM_PME_STATUS_STATS; i++)
1043                 strcpy(data + (idx++) * ETH_GSTRING_LEN, mlx5e_pme_status_desc[i].format);
1044
1045         for (i = 0; i < NUM_PME_ERR_STATS; i++)
1046                 strcpy(data + (idx++) * ETH_GSTRING_LEN, mlx5e_pme_error_desc[i].format);
1047
1048         return idx;
1049 }
1050
1051 static int mlx5e_grp_pme_fill_stats(struct mlx5e_priv *priv, u64 *data,
1052                                     int idx)
1053 {
1054         struct mlx5_priv *mlx5_priv = &priv->mdev->priv;
1055         int i;
1056
1057         for (i = 0; i < NUM_PME_STATUS_STATS; i++)
1058                 data[idx++] = MLX5E_READ_CTR64_CPU(mlx5_priv->pme_stats.status_counters,
1059                                                    mlx5e_pme_status_desc, i);
1060
1061         for (i = 0; i < NUM_PME_ERR_STATS; i++)
1062                 data[idx++] = MLX5E_READ_CTR64_CPU(mlx5_priv->pme_stats.error_counters,
1063                                                    mlx5e_pme_error_desc, i);
1064
1065         return idx;
1066 }
1067
1068 static int mlx5e_grp_ipsec_get_num_stats(struct mlx5e_priv *priv)
1069 {
1070         return mlx5e_ipsec_get_count(priv);
1071 }
1072
1073 static int mlx5e_grp_ipsec_fill_strings(struct mlx5e_priv *priv, u8 *data,
1074                                         int idx)
1075 {
1076         return idx + mlx5e_ipsec_get_strings(priv,
1077                                              data + idx * ETH_GSTRING_LEN);
1078 }
1079
1080 static int mlx5e_grp_ipsec_fill_stats(struct mlx5e_priv *priv, u64 *data,
1081                                       int idx)
1082 {
1083         return idx + mlx5e_ipsec_get_stats(priv, data + idx);
1084 }
1085
1086 static void mlx5e_grp_ipsec_update_stats(struct mlx5e_priv *priv)
1087 {
1088         mlx5e_ipsec_update_stats(priv);
1089 }
1090
1091 static int mlx5e_grp_tls_get_num_stats(struct mlx5e_priv *priv)
1092 {
1093         return mlx5e_tls_get_count(priv);
1094 }
1095
1096 static int mlx5e_grp_tls_fill_strings(struct mlx5e_priv *priv, u8 *data,
1097                                       int idx)
1098 {
1099         return idx + mlx5e_tls_get_strings(priv, data + idx * ETH_GSTRING_LEN);
1100 }
1101
1102 static int mlx5e_grp_tls_fill_stats(struct mlx5e_priv *priv, u64 *data, int idx)
1103 {
1104         return idx + mlx5e_tls_get_stats(priv, data + idx);
1105 }
1106
1107 static const struct counter_desc rq_stats_desc[] = {
1108         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, packets) },
1109         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, bytes) },
1110         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, csum_complete) },
1111         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, csum_unnecessary) },
1112         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, csum_unnecessary_inner) },
1113         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, csum_none) },
1114         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, xdp_drop) },
1115         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, xdp_tx) },
1116         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, xdp_tx_full) },
1117         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, lro_packets) },
1118         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, lro_bytes) },
1119         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, removed_vlan_packets) },
1120         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, wqe_err) },
1121         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, mpwqe_filler) },
1122         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, buff_alloc_err) },
1123         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, cqe_compress_blks) },
1124         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, cqe_compress_pkts) },
1125         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, page_reuse) },
1126         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, cache_reuse) },
1127         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, cache_full) },
1128         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, cache_empty) },
1129         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, cache_busy) },
1130         { MLX5E_DECLARE_RX_STAT(struct mlx5e_rq_stats, cache_waive) },
1131 };
1132
1133 static const struct counter_desc sq_stats_desc[] = {
1134         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, packets) },
1135         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, bytes) },
1136         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, tso_packets) },
1137         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, tso_bytes) },
1138         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, tso_inner_packets) },
1139         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, tso_inner_bytes) },
1140         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, csum_partial) },
1141         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, csum_partial_inner) },
1142         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, added_vlan_packets) },
1143         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, nop) },
1144         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, csum_none) },
1145         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, stopped) },
1146         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, wake) },
1147         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, dropped) },
1148         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, xmit_more) },
1149         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, cqe_err) },
1150         { MLX5E_DECLARE_TX_STAT(struct mlx5e_sq_stats, recover) },
1151 };
1152
1153 static const struct counter_desc ch_stats_desc[] = {
1154         { MLX5E_DECLARE_CH_STAT(struct mlx5e_ch_stats, eq_rearm) },
1155 };
1156
1157 #define NUM_RQ_STATS                    ARRAY_SIZE(rq_stats_desc)
1158 #define NUM_SQ_STATS                    ARRAY_SIZE(sq_stats_desc)
1159 #define NUM_CH_STATS                    ARRAY_SIZE(ch_stats_desc)
1160
1161 static int mlx5e_grp_channels_get_num_stats(struct mlx5e_priv *priv)
1162 {
1163         return (NUM_RQ_STATS * priv->channels.num) +
1164                 (NUM_CH_STATS * priv->channels.num) +
1165                 (NUM_SQ_STATS * priv->channels.num * priv->channels.params.num_tc);
1166 }
1167
1168 static int mlx5e_grp_channels_fill_strings(struct mlx5e_priv *priv, u8 *data,
1169                                            int idx)
1170 {
1171         int i, j, tc;
1172
1173         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1174                 return idx;
1175
1176         for (i = 0; i < priv->channels.num; i++)
1177                 for (j = 0; j < NUM_CH_STATS; j++)
1178                         sprintf(data + (idx++) * ETH_GSTRING_LEN,
1179                                 ch_stats_desc[j].format, i);
1180
1181         for (i = 0; i < priv->channels.num; i++)
1182                 for (j = 0; j < NUM_RQ_STATS; j++)
1183                         sprintf(data + (idx++) * ETH_GSTRING_LEN, rq_stats_desc[j].format, i);
1184
1185         for (tc = 0; tc < priv->channels.params.num_tc; tc++)
1186                 for (i = 0; i < priv->channels.num; i++)
1187                         for (j = 0; j < NUM_SQ_STATS; j++)
1188                                 sprintf(data + (idx++) * ETH_GSTRING_LEN,
1189                                         sq_stats_desc[j].format,
1190                                         priv->channel_tc2txq[i][tc]);
1191
1192         return idx;
1193 }
1194
1195 static int mlx5e_grp_channels_fill_stats(struct mlx5e_priv *priv, u64 *data,
1196                                          int idx)
1197 {
1198         struct mlx5e_channels *channels = &priv->channels;
1199         int i, j, tc;
1200
1201         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1202                 return idx;
1203
1204         for (i = 0; i < channels->num; i++)
1205                 for (j = 0; j < NUM_CH_STATS; j++)
1206                         data[idx++] =
1207                                 MLX5E_READ_CTR64_CPU(&channels->c[i]->stats,
1208                                                      ch_stats_desc, j);
1209
1210         for (i = 0; i < channels->num; i++)
1211                 for (j = 0; j < NUM_RQ_STATS; j++)
1212                         data[idx++] =
1213                                 MLX5E_READ_CTR64_CPU(&channels->c[i]->rq.stats,
1214                                                      rq_stats_desc, j);
1215
1216         for (tc = 0; tc < priv->channels.params.num_tc; tc++)
1217                 for (i = 0; i < channels->num; i++)
1218                         for (j = 0; j < NUM_SQ_STATS; j++)
1219                                 data[idx++] =
1220                                         MLX5E_READ_CTR64_CPU(&channels->c[i]->sq[tc].stats,
1221                                                              sq_stats_desc, j);
1222
1223         return idx;
1224 }
1225
1226 /* The stats groups order is opposite to the update_stats() order calls */
1227 const struct mlx5e_stats_grp mlx5e_stats_grps[] = {
1228         {
1229                 .get_num_stats = mlx5e_grp_sw_get_num_stats,
1230                 .fill_strings = mlx5e_grp_sw_fill_strings,
1231                 .fill_stats = mlx5e_grp_sw_fill_stats,
1232                 .update_stats = mlx5e_grp_sw_update_stats,
1233         },
1234         {
1235                 .get_num_stats = mlx5e_grp_q_get_num_stats,
1236                 .fill_strings = mlx5e_grp_q_fill_strings,
1237                 .fill_stats = mlx5e_grp_q_fill_stats,
1238                 .update_stats_mask = MLX5E_NDO_UPDATE_STATS,
1239                 .update_stats = mlx5e_grp_q_update_stats,
1240         },
1241         {
1242                 .get_num_stats = mlx5e_grp_vnic_env_get_num_stats,
1243                 .fill_strings = mlx5e_grp_vnic_env_fill_strings,
1244                 .fill_stats = mlx5e_grp_vnic_env_fill_stats,
1245                 .update_stats = mlx5e_grp_vnic_env_update_stats,
1246         },
1247         {
1248                 .get_num_stats = mlx5e_grp_vport_get_num_stats,
1249                 .fill_strings = mlx5e_grp_vport_fill_strings,
1250                 .fill_stats = mlx5e_grp_vport_fill_stats,
1251                 .update_stats_mask = MLX5E_NDO_UPDATE_STATS,
1252                 .update_stats = mlx5e_grp_vport_update_stats,
1253         },
1254         {
1255                 .get_num_stats = mlx5e_grp_802_3_get_num_stats,
1256                 .fill_strings = mlx5e_grp_802_3_fill_strings,
1257                 .fill_stats = mlx5e_grp_802_3_fill_stats,
1258                 .update_stats_mask = MLX5E_NDO_UPDATE_STATS,
1259                 .update_stats = mlx5e_grp_802_3_update_stats,
1260         },
1261         {
1262                 .get_num_stats = mlx5e_grp_2863_get_num_stats,
1263                 .fill_strings = mlx5e_grp_2863_fill_strings,
1264                 .fill_stats = mlx5e_grp_2863_fill_stats,
1265                 .update_stats = mlx5e_grp_2863_update_stats,
1266         },
1267         {
1268                 .get_num_stats = mlx5e_grp_2819_get_num_stats,
1269                 .fill_strings = mlx5e_grp_2819_fill_strings,
1270                 .fill_stats = mlx5e_grp_2819_fill_stats,
1271                 .update_stats = mlx5e_grp_2819_update_stats,
1272         },
1273         {
1274                 .get_num_stats = mlx5e_grp_phy_get_num_stats,
1275                 .fill_strings = mlx5e_grp_phy_fill_strings,
1276                 .fill_stats = mlx5e_grp_phy_fill_stats,
1277                 .update_stats = mlx5e_grp_phy_update_stats,
1278         },
1279         {
1280                 .get_num_stats = mlx5e_grp_eth_ext_get_num_stats,
1281                 .fill_strings = mlx5e_grp_eth_ext_fill_strings,
1282                 .fill_stats = mlx5e_grp_eth_ext_fill_stats,
1283                 .update_stats = mlx5e_grp_eth_ext_update_stats,
1284         },
1285         {
1286                 .get_num_stats = mlx5e_grp_pcie_get_num_stats,
1287                 .fill_strings = mlx5e_grp_pcie_fill_strings,
1288                 .fill_stats = mlx5e_grp_pcie_fill_stats,
1289                 .update_stats = mlx5e_grp_pcie_update_stats,
1290         },
1291         {
1292                 .get_num_stats = mlx5e_grp_per_prio_get_num_stats,
1293                 .fill_strings = mlx5e_grp_per_prio_fill_strings,
1294                 .fill_stats = mlx5e_grp_per_prio_fill_stats,
1295                 .update_stats = mlx5e_grp_per_prio_update_stats,
1296         },
1297         {
1298                 .get_num_stats = mlx5e_grp_pme_get_num_stats,
1299                 .fill_strings = mlx5e_grp_pme_fill_strings,
1300                 .fill_stats = mlx5e_grp_pme_fill_stats,
1301         },
1302         {
1303                 .get_num_stats = mlx5e_grp_ipsec_get_num_stats,
1304                 .fill_strings = mlx5e_grp_ipsec_fill_strings,
1305                 .fill_stats = mlx5e_grp_ipsec_fill_stats,
1306                 .update_stats = mlx5e_grp_ipsec_update_stats,
1307         },
1308         {
1309                 .get_num_stats = mlx5e_grp_tls_get_num_stats,
1310                 .fill_strings = mlx5e_grp_tls_fill_strings,
1311                 .fill_stats = mlx5e_grp_tls_fill_stats,
1312         },
1313         {
1314                 .get_num_stats = mlx5e_grp_channels_get_num_stats,
1315                 .fill_strings = mlx5e_grp_channels_fill_strings,
1316                 .fill_stats = mlx5e_grp_channels_fill_stats,
1317         }
1318 };
1319
1320 const int mlx5e_num_stats_grps = ARRAY_SIZE(mlx5e_stats_grps);