1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Statistics for Ocelot switch family
3 *
4 * Copyright (c) 2017 Microsemi Corporation
5 * Copyright 2022 NXP
6 */
7 #include <linux/ethtool_netlink.h>
8 #include <linux/spinlock.h>
9 #include <linux/mutex.h>
10 #include <linux/workqueue.h>
11 #include "ocelot.h"
12
13 enum ocelot_stat {
14 OCELOT_STAT_RX_OCTETS,
15 OCELOT_STAT_RX_UNICAST,
16 OCELOT_STAT_RX_MULTICAST,
17 OCELOT_STAT_RX_BROADCAST,
18 OCELOT_STAT_RX_SHORTS,
19 OCELOT_STAT_RX_FRAGMENTS,
20 OCELOT_STAT_RX_JABBERS,
21 OCELOT_STAT_RX_CRC_ALIGN_ERRS,
22 OCELOT_STAT_RX_SYM_ERRS,
23 OCELOT_STAT_RX_64,
24 OCELOT_STAT_RX_65_127,
25 OCELOT_STAT_RX_128_255,
26 OCELOT_STAT_RX_256_511,
27 OCELOT_STAT_RX_512_1023,
28 OCELOT_STAT_RX_1024_1526,
29 OCELOT_STAT_RX_1527_MAX,
30 OCELOT_STAT_RX_PAUSE,
31 OCELOT_STAT_RX_CONTROL,
32 OCELOT_STAT_RX_LONGS,
33 OCELOT_STAT_RX_CLASSIFIED_DROPS,
34 OCELOT_STAT_RX_RED_PRIO_0,
35 OCELOT_STAT_RX_RED_PRIO_1,
36 OCELOT_STAT_RX_RED_PRIO_2,
37 OCELOT_STAT_RX_RED_PRIO_3,
38 OCELOT_STAT_RX_RED_PRIO_4,
39 OCELOT_STAT_RX_RED_PRIO_5,
40 OCELOT_STAT_RX_RED_PRIO_6,
41 OCELOT_STAT_RX_RED_PRIO_7,
42 OCELOT_STAT_RX_YELLOW_PRIO_0,
43 OCELOT_STAT_RX_YELLOW_PRIO_1,
44 OCELOT_STAT_RX_YELLOW_PRIO_2,
45 OCELOT_STAT_RX_YELLOW_PRIO_3,
46 OCELOT_STAT_RX_YELLOW_PRIO_4,
47 OCELOT_STAT_RX_YELLOW_PRIO_5,
48 OCELOT_STAT_RX_YELLOW_PRIO_6,
49 OCELOT_STAT_RX_YELLOW_PRIO_7,
50 OCELOT_STAT_RX_GREEN_PRIO_0,
51 OCELOT_STAT_RX_GREEN_PRIO_1,
52 OCELOT_STAT_RX_GREEN_PRIO_2,
53 OCELOT_STAT_RX_GREEN_PRIO_3,
54 OCELOT_STAT_RX_GREEN_PRIO_4,
55 OCELOT_STAT_RX_GREEN_PRIO_5,
56 OCELOT_STAT_RX_GREEN_PRIO_6,
57 OCELOT_STAT_RX_GREEN_PRIO_7,
58 OCELOT_STAT_RX_ASSEMBLY_ERRS,
59 OCELOT_STAT_RX_SMD_ERRS,
60 OCELOT_STAT_RX_ASSEMBLY_OK,
61 OCELOT_STAT_RX_MERGE_FRAGMENTS,
62 OCELOT_STAT_RX_PMAC_OCTETS,
63 OCELOT_STAT_RX_PMAC_UNICAST,
64 OCELOT_STAT_RX_PMAC_MULTICAST,
65 OCELOT_STAT_RX_PMAC_BROADCAST,
66 OCELOT_STAT_RX_PMAC_SHORTS,
67 OCELOT_STAT_RX_PMAC_FRAGMENTS,
68 OCELOT_STAT_RX_PMAC_JABBERS,
69 OCELOT_STAT_RX_PMAC_CRC_ALIGN_ERRS,
70 OCELOT_STAT_RX_PMAC_SYM_ERRS,
71 OCELOT_STAT_RX_PMAC_64,
72 OCELOT_STAT_RX_PMAC_65_127,
73 OCELOT_STAT_RX_PMAC_128_255,
74 OCELOT_STAT_RX_PMAC_256_511,
75 OCELOT_STAT_RX_PMAC_512_1023,
76 OCELOT_STAT_RX_PMAC_1024_1526,
77 OCELOT_STAT_RX_PMAC_1527_MAX,
78 OCELOT_STAT_RX_PMAC_PAUSE,
79 OCELOT_STAT_RX_PMAC_CONTROL,
80 OCELOT_STAT_RX_PMAC_LONGS,
81 OCELOT_STAT_TX_OCTETS,
82 OCELOT_STAT_TX_UNICAST,
83 OCELOT_STAT_TX_MULTICAST,
84 OCELOT_STAT_TX_BROADCAST,
85 OCELOT_STAT_TX_COLLISION,
86 OCELOT_STAT_TX_DROPS,
87 OCELOT_STAT_TX_PAUSE,
88 OCELOT_STAT_TX_64,
89 OCELOT_STAT_TX_65_127,
90 OCELOT_STAT_TX_128_255,
91 OCELOT_STAT_TX_256_511,
92 OCELOT_STAT_TX_512_1023,
93 OCELOT_STAT_TX_1024_1526,
94 OCELOT_STAT_TX_1527_MAX,
95 OCELOT_STAT_TX_YELLOW_PRIO_0,
96 OCELOT_STAT_TX_YELLOW_PRIO_1,
97 OCELOT_STAT_TX_YELLOW_PRIO_2,
98 OCELOT_STAT_TX_YELLOW_PRIO_3,
99 OCELOT_STAT_TX_YELLOW_PRIO_4,
100 OCELOT_STAT_TX_YELLOW_PRIO_5,
101 OCELOT_STAT_TX_YELLOW_PRIO_6,
102 OCELOT_STAT_TX_YELLOW_PRIO_7,
103 OCELOT_STAT_TX_GREEN_PRIO_0,
104 OCELOT_STAT_TX_GREEN_PRIO_1,
105 OCELOT_STAT_TX_GREEN_PRIO_2,
106 OCELOT_STAT_TX_GREEN_PRIO_3,
107 OCELOT_STAT_TX_GREEN_PRIO_4,
108 OCELOT_STAT_TX_GREEN_PRIO_5,
109 OCELOT_STAT_TX_GREEN_PRIO_6,
110 OCELOT_STAT_TX_GREEN_PRIO_7,
111 OCELOT_STAT_TX_AGED,
112 OCELOT_STAT_TX_MM_HOLD,
113 OCELOT_STAT_TX_MERGE_FRAGMENTS,
114 OCELOT_STAT_TX_PMAC_OCTETS,
115 OCELOT_STAT_TX_PMAC_UNICAST,
116 OCELOT_STAT_TX_PMAC_MULTICAST,
117 OCELOT_STAT_TX_PMAC_BROADCAST,
118 OCELOT_STAT_TX_PMAC_PAUSE,
119 OCELOT_STAT_TX_PMAC_64,
120 OCELOT_STAT_TX_PMAC_65_127,
121 OCELOT_STAT_TX_PMAC_128_255,
122 OCELOT_STAT_TX_PMAC_256_511,
123 OCELOT_STAT_TX_PMAC_512_1023,
124 OCELOT_STAT_TX_PMAC_1024_1526,
125 OCELOT_STAT_TX_PMAC_1527_MAX,
126 OCELOT_STAT_DROP_LOCAL,
127 OCELOT_STAT_DROP_TAIL,
128 OCELOT_STAT_DROP_YELLOW_PRIO_0,
129 OCELOT_STAT_DROP_YELLOW_PRIO_1,
130 OCELOT_STAT_DROP_YELLOW_PRIO_2,
131 OCELOT_STAT_DROP_YELLOW_PRIO_3,
132 OCELOT_STAT_DROP_YELLOW_PRIO_4,
133 OCELOT_STAT_DROP_YELLOW_PRIO_5,
134 OCELOT_STAT_DROP_YELLOW_PRIO_6,
135 OCELOT_STAT_DROP_YELLOW_PRIO_7,
136 OCELOT_STAT_DROP_GREEN_PRIO_0,
137 OCELOT_STAT_DROP_GREEN_PRIO_1,
138 OCELOT_STAT_DROP_GREEN_PRIO_2,
139 OCELOT_STAT_DROP_GREEN_PRIO_3,
140 OCELOT_STAT_DROP_GREEN_PRIO_4,
141 OCELOT_STAT_DROP_GREEN_PRIO_5,
142 OCELOT_STAT_DROP_GREEN_PRIO_6,
143 OCELOT_STAT_DROP_GREEN_PRIO_7,
144 OCELOT_NUM_STATS,
145 };
146
147 struct ocelot_stat_layout {
148 u32 reg;
149 char name[ETH_GSTRING_LEN];
150 };
151
152 /* 32-bit counter checked for wraparound by ocelot_port_update_stats()
153 * and copied to ocelot->stats.
154 */
155 #define OCELOT_STAT(kind) \
156 [OCELOT_STAT_ ## kind] = { .reg = SYS_COUNT_ ## kind }
157 /* Same as above, except also exported to ethtool -S. Standard counters should
158 * only be exposed to more specific interfaces rather than by their string name.
159 */
160 #define OCELOT_STAT_ETHTOOL(kind, ethtool_name) \
161 [OCELOT_STAT_ ## kind] = { .reg = SYS_COUNT_ ## kind, .name = ethtool_name }
162
163 #define OCELOT_COMMON_STATS \
164 OCELOT_STAT_ETHTOOL(RX_OCTETS, "rx_octets"), \
165 OCELOT_STAT_ETHTOOL(RX_UNICAST, "rx_unicast"), \
166 OCELOT_STAT_ETHTOOL(RX_MULTICAST, "rx_multicast"), \
167 OCELOT_STAT_ETHTOOL(RX_BROADCAST, "rx_broadcast"), \
168 OCELOT_STAT_ETHTOOL(RX_SHORTS, "rx_shorts"), \
169 OCELOT_STAT_ETHTOOL(RX_FRAGMENTS, "rx_fragments"), \
170 OCELOT_STAT_ETHTOOL(RX_JABBERS, "rx_jabbers"), \
171 OCELOT_STAT_ETHTOOL(RX_CRC_ALIGN_ERRS, "rx_crc_align_errs"), \
172 OCELOT_STAT_ETHTOOL(RX_SYM_ERRS, "rx_sym_errs"), \
173 OCELOT_STAT_ETHTOOL(RX_64, "rx_frames_below_65_octets"), \
174 OCELOT_STAT_ETHTOOL(RX_65_127, "rx_frames_65_to_127_octets"), \
175 OCELOT_STAT_ETHTOOL(RX_128_255, "rx_frames_128_to_255_octets"), \
176 OCELOT_STAT_ETHTOOL(RX_256_511, "rx_frames_256_to_511_octets"), \
177 OCELOT_STAT_ETHTOOL(RX_512_1023, "rx_frames_512_to_1023_octets"), \
178 OCELOT_STAT_ETHTOOL(RX_1024_1526, "rx_frames_1024_to_1526_octets"), \
179 OCELOT_STAT_ETHTOOL(RX_1527_MAX, "rx_frames_over_1526_octets"), \
180 OCELOT_STAT_ETHTOOL(RX_PAUSE, "rx_pause"), \
181 OCELOT_STAT_ETHTOOL(RX_CONTROL, "rx_control"), \
182 OCELOT_STAT_ETHTOOL(RX_LONGS, "rx_longs"), \
183 OCELOT_STAT_ETHTOOL(RX_CLASSIFIED_DROPS, "rx_classified_drops"), \
184 OCELOT_STAT_ETHTOOL(RX_RED_PRIO_0, "rx_red_prio_0"), \
185 OCELOT_STAT_ETHTOOL(RX_RED_PRIO_1, "rx_red_prio_1"), \
186 OCELOT_STAT_ETHTOOL(RX_RED_PRIO_2, "rx_red_prio_2"), \
187 OCELOT_STAT_ETHTOOL(RX_RED_PRIO_3, "rx_red_prio_3"), \
188 OCELOT_STAT_ETHTOOL(RX_RED_PRIO_4, "rx_red_prio_4"), \
189 OCELOT_STAT_ETHTOOL(RX_RED_PRIO_5, "rx_red_prio_5"), \
190 OCELOT_STAT_ETHTOOL(RX_RED_PRIO_6, "rx_red_prio_6"), \
191 OCELOT_STAT_ETHTOOL(RX_RED_PRIO_7, "rx_red_prio_7"), \
192 OCELOT_STAT_ETHTOOL(RX_YELLOW_PRIO_0, "rx_yellow_prio_0"), \
193 OCELOT_STAT_ETHTOOL(RX_YELLOW_PRIO_1, "rx_yellow_prio_1"), \
194 OCELOT_STAT_ETHTOOL(RX_YELLOW_PRIO_2, "rx_yellow_prio_2"), \
195 OCELOT_STAT_ETHTOOL(RX_YELLOW_PRIO_3, "rx_yellow_prio_3"), \
196 OCELOT_STAT_ETHTOOL(RX_YELLOW_PRIO_4, "rx_yellow_prio_4"), \
197 OCELOT_STAT_ETHTOOL(RX_YELLOW_PRIO_5, "rx_yellow_prio_5"), \
198 OCELOT_STAT_ETHTOOL(RX_YELLOW_PRIO_6, "rx_yellow_prio_6"), \
199 OCELOT_STAT_ETHTOOL(RX_YELLOW_PRIO_7, "rx_yellow_prio_7"), \
200 OCELOT_STAT_ETHTOOL(RX_GREEN_PRIO_0, "rx_green_prio_0"), \
201 OCELOT_STAT_ETHTOOL(RX_GREEN_PRIO_1, "rx_green_prio_1"), \
202 OCELOT_STAT_ETHTOOL(RX_GREEN_PRIO_2, "rx_green_prio_2"), \
203 OCELOT_STAT_ETHTOOL(RX_GREEN_PRIO_3, "rx_green_prio_3"), \
204 OCELOT_STAT_ETHTOOL(RX_GREEN_PRIO_4, "rx_green_prio_4"), \
205 OCELOT_STAT_ETHTOOL(RX_GREEN_PRIO_5, "rx_green_prio_5"), \
206 OCELOT_STAT_ETHTOOL(RX_GREEN_PRIO_6, "rx_green_prio_6"), \
207 OCELOT_STAT_ETHTOOL(RX_GREEN_PRIO_7, "rx_green_prio_7"), \
208 OCELOT_STAT_ETHTOOL(TX_OCTETS, "tx_octets"), \
209 OCELOT_STAT_ETHTOOL(TX_UNICAST, "tx_unicast"), \
210 OCELOT_STAT_ETHTOOL(TX_MULTICAST, "tx_multicast"), \
211 OCELOT_STAT_ETHTOOL(TX_BROADCAST, "tx_broadcast"), \
212 OCELOT_STAT_ETHTOOL(TX_COLLISION, "tx_collision"), \
213 OCELOT_STAT_ETHTOOL(TX_DROPS, "tx_drops"), \
214 OCELOT_STAT_ETHTOOL(TX_PAUSE, "tx_pause"), \
215 OCELOT_STAT_ETHTOOL(TX_64, "tx_frames_below_65_octets"), \
216 OCELOT_STAT_ETHTOOL(TX_65_127, "tx_frames_65_to_127_octets"), \
217 OCELOT_STAT_ETHTOOL(TX_128_255, "tx_frames_128_255_octets"), \
218 OCELOT_STAT_ETHTOOL(TX_256_511, "tx_frames_256_511_octets"), \
219 OCELOT_STAT_ETHTOOL(TX_512_1023, "tx_frames_512_1023_octets"), \
220 OCELOT_STAT_ETHTOOL(TX_1024_1526, "tx_frames_1024_1526_octets"), \
221 OCELOT_STAT_ETHTOOL(TX_1527_MAX, "tx_frames_over_1526_octets"), \
222 OCELOT_STAT_ETHTOOL(TX_YELLOW_PRIO_0, "tx_yellow_prio_0"), \
223 OCELOT_STAT_ETHTOOL(TX_YELLOW_PRIO_1, "tx_yellow_prio_1"), \
224 OCELOT_STAT_ETHTOOL(TX_YELLOW_PRIO_2, "tx_yellow_prio_2"), \
225 OCELOT_STAT_ETHTOOL(TX_YELLOW_PRIO_3, "tx_yellow_prio_3"), \
226 OCELOT_STAT_ETHTOOL(TX_YELLOW_PRIO_4, "tx_yellow_prio_4"), \
227 OCELOT_STAT_ETHTOOL(TX_YELLOW_PRIO_5, "tx_yellow_prio_5"), \
228 OCELOT_STAT_ETHTOOL(TX_YELLOW_PRIO_6, "tx_yellow_prio_6"), \
229 OCELOT_STAT_ETHTOOL(TX_YELLOW_PRIO_7, "tx_yellow_prio_7"), \
230 OCELOT_STAT_ETHTOOL(TX_GREEN_PRIO_0, "tx_green_prio_0"), \
231 OCELOT_STAT_ETHTOOL(TX_GREEN_PRIO_1, "tx_green_prio_1"), \
232 OCELOT_STAT_ETHTOOL(TX_GREEN_PRIO_2, "tx_green_prio_2"), \
233 OCELOT_STAT_ETHTOOL(TX_GREEN_PRIO_3, "tx_green_prio_3"), \
234 OCELOT_STAT_ETHTOOL(TX_GREEN_PRIO_4, "tx_green_prio_4"), \
235 OCELOT_STAT_ETHTOOL(TX_GREEN_PRIO_5, "tx_green_prio_5"), \
236 OCELOT_STAT_ETHTOOL(TX_GREEN_PRIO_6, "tx_green_prio_6"), \
237 OCELOT_STAT_ETHTOOL(TX_GREEN_PRIO_7, "tx_green_prio_7"), \
238 OCELOT_STAT_ETHTOOL(TX_AGED, "tx_aged"), \
239 OCELOT_STAT_ETHTOOL(DROP_LOCAL, "drop_local"), \
240 OCELOT_STAT_ETHTOOL(DROP_TAIL, "drop_tail"), \
241 OCELOT_STAT_ETHTOOL(DROP_YELLOW_PRIO_0, "drop_yellow_prio_0"), \
242 OCELOT_STAT_ETHTOOL(DROP_YELLOW_PRIO_1, "drop_yellow_prio_1"), \
243 OCELOT_STAT_ETHTOOL(DROP_YELLOW_PRIO_2, "drop_yellow_prio_2"), \
244 OCELOT_STAT_ETHTOOL(DROP_YELLOW_PRIO_3, "drop_yellow_prio_3"), \
245 OCELOT_STAT_ETHTOOL(DROP_YELLOW_PRIO_4, "drop_yellow_prio_4"), \
246 OCELOT_STAT_ETHTOOL(DROP_YELLOW_PRIO_5, "drop_yellow_prio_5"), \
247 OCELOT_STAT_ETHTOOL(DROP_YELLOW_PRIO_6, "drop_yellow_prio_6"), \
248 OCELOT_STAT_ETHTOOL(DROP_YELLOW_PRIO_7, "drop_yellow_prio_7"), \
249 OCELOT_STAT_ETHTOOL(DROP_GREEN_PRIO_0, "drop_green_prio_0"), \
250 OCELOT_STAT_ETHTOOL(DROP_GREEN_PRIO_1, "drop_green_prio_1"), \
251 OCELOT_STAT_ETHTOOL(DROP_GREEN_PRIO_2, "drop_green_prio_2"), \
252 OCELOT_STAT_ETHTOOL(DROP_GREEN_PRIO_3, "drop_green_prio_3"), \
253 OCELOT_STAT_ETHTOOL(DROP_GREEN_PRIO_4, "drop_green_prio_4"), \
254 OCELOT_STAT_ETHTOOL(DROP_GREEN_PRIO_5, "drop_green_prio_5"), \
255 OCELOT_STAT_ETHTOOL(DROP_GREEN_PRIO_6, "drop_green_prio_6"), \
256 OCELOT_STAT_ETHTOOL(DROP_GREEN_PRIO_7, "drop_green_prio_7")
257
258 struct ocelot_stats_region {
259 struct list_head node;
260 u32 base;
261 int count;
262 u32 *buf;
263 };
264
265 static const struct ocelot_stat_layout ocelot_stats_layout[OCELOT_NUM_STATS] = {
266 OCELOT_COMMON_STATS,
267 };
268
269 static const struct ocelot_stat_layout ocelot_mm_stats_layout[OCELOT_NUM_STATS] = {
270 OCELOT_COMMON_STATS,
271 OCELOT_STAT(RX_ASSEMBLY_ERRS),
272 OCELOT_STAT(RX_SMD_ERRS),
273 OCELOT_STAT(RX_ASSEMBLY_OK),
274 OCELOT_STAT(RX_MERGE_FRAGMENTS),
275 OCELOT_STAT(TX_MERGE_FRAGMENTS),
276 OCELOT_STAT(RX_PMAC_OCTETS),
277 OCELOT_STAT(RX_PMAC_UNICAST),
278 OCELOT_STAT(RX_PMAC_MULTICAST),
279 OCELOT_STAT(RX_PMAC_BROADCAST),
280 OCELOT_STAT(RX_PMAC_SHORTS),
281 OCELOT_STAT(RX_PMAC_FRAGMENTS),
282 OCELOT_STAT(RX_PMAC_JABBERS),
283 OCELOT_STAT(RX_PMAC_CRC_ALIGN_ERRS),
284 OCELOT_STAT(RX_PMAC_SYM_ERRS),
285 OCELOT_STAT(RX_PMAC_64),
286 OCELOT_STAT(RX_PMAC_65_127),
287 OCELOT_STAT(RX_PMAC_128_255),
288 OCELOT_STAT(RX_PMAC_256_511),
289 OCELOT_STAT(RX_PMAC_512_1023),
290 OCELOT_STAT(RX_PMAC_1024_1526),
291 OCELOT_STAT(RX_PMAC_1527_MAX),
292 OCELOT_STAT(RX_PMAC_PAUSE),
293 OCELOT_STAT(RX_PMAC_CONTROL),
294 OCELOT_STAT(RX_PMAC_LONGS),
295 OCELOT_STAT(TX_PMAC_OCTETS),
296 OCELOT_STAT(TX_PMAC_UNICAST),
297 OCELOT_STAT(TX_PMAC_MULTICAST),
298 OCELOT_STAT(TX_PMAC_BROADCAST),
299 OCELOT_STAT(TX_PMAC_PAUSE),
300 OCELOT_STAT(TX_PMAC_64),
301 OCELOT_STAT(TX_PMAC_65_127),
302 OCELOT_STAT(TX_PMAC_128_255),
303 OCELOT_STAT(TX_PMAC_256_511),
304 OCELOT_STAT(TX_PMAC_512_1023),
305 OCELOT_STAT(TX_PMAC_1024_1526),
306 OCELOT_STAT(TX_PMAC_1527_MAX),
307 };
308
309 static const struct ocelot_stat_layout *
ocelot_get_stats_layout(struct ocelot * ocelot)310 ocelot_get_stats_layout(struct ocelot *ocelot)
311 {
312 if (ocelot->mm_supported)
313 return ocelot_mm_stats_layout;
314
315 return ocelot_stats_layout;
316 }
317
318 /* Read the counters from hardware and keep them in region->buf.
319 * Caller must hold &ocelot->stat_view_lock.
320 */
ocelot_port_update_stats(struct ocelot * ocelot,int port)321 static int ocelot_port_update_stats(struct ocelot *ocelot, int port)
322 {
323 struct ocelot_stats_region *region;
324 int err;
325
326 /* Configure the port to read the stats from */
327 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port), SYS_STAT_CFG);
328
329 list_for_each_entry(region, &ocelot->stats_regions, node) {
330 err = ocelot_bulk_read(ocelot, region->base, region->buf,
331 region->count);
332 if (err)
333 return err;
334 }
335
336 return 0;
337 }
338
339 /* Transfer the counters from region->buf to ocelot->stats.
340 * Caller must hold &ocelot->stat_view_lock and &ocelot->stats_lock.
341 */
ocelot_port_transfer_stats(struct ocelot * ocelot,int port)342 static void ocelot_port_transfer_stats(struct ocelot *ocelot, int port)
343 {
344 unsigned int idx = port * OCELOT_NUM_STATS;
345 struct ocelot_stats_region *region;
346 int j;
347
348 list_for_each_entry(region, &ocelot->stats_regions, node) {
349 for (j = 0; j < region->count; j++) {
350 u64 *stat = &ocelot->stats[idx + j];
351 u64 val = region->buf[j];
352
353 if (val < (*stat & U32_MAX))
354 *stat += (u64)1 << 32;
355
356 *stat = (*stat & ~(u64)U32_MAX) + val;
357 }
358
359 idx += region->count;
360 }
361 }
362
ocelot_check_stats_work(struct work_struct * work)363 static void ocelot_check_stats_work(struct work_struct *work)
364 {
365 struct delayed_work *del_work = to_delayed_work(work);
366 struct ocelot *ocelot = container_of(del_work, struct ocelot,
367 stats_work);
368 int port, err;
369
370 mutex_lock(&ocelot->stat_view_lock);
371
372 for (port = 0; port < ocelot->num_phys_ports; port++) {
373 err = ocelot_port_update_stats(ocelot, port);
374 if (err)
375 break;
376
377 spin_lock(&ocelot->stats_lock);
378 ocelot_port_transfer_stats(ocelot, port);
379 spin_unlock(&ocelot->stats_lock);
380 }
381
382 if (!err && ocelot->ops->update_stats)
383 ocelot->ops->update_stats(ocelot);
384
385 mutex_unlock(&ocelot->stat_view_lock);
386
387 if (err)
388 dev_err(ocelot->dev, "Error %d updating ethtool stats\n", err);
389
390 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
391 OCELOT_STATS_CHECK_DELAY);
392 }
393
ocelot_get_strings(struct ocelot * ocelot,int port,u32 sset,u8 * data)394 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
395 {
396 const struct ocelot_stat_layout *layout;
397 int i;
398
399 if (sset != ETH_SS_STATS)
400 return;
401
402 layout = ocelot_get_stats_layout(ocelot);
403
404 for (i = 0; i < OCELOT_NUM_STATS; i++) {
405 if (layout[i].name[0] == '\0')
406 continue;
407
408 memcpy(data, layout[i].name, ETH_GSTRING_LEN);
409 data += ETH_GSTRING_LEN;
410 }
411 }
412 EXPORT_SYMBOL(ocelot_get_strings);
413
414 /* Update ocelot->stats for the given port and run the given callback */
ocelot_port_stats_run(struct ocelot * ocelot,int port,void * priv,void (* cb)(struct ocelot * ocelot,int port,void * priv))415 static void ocelot_port_stats_run(struct ocelot *ocelot, int port, void *priv,
416 void (*cb)(struct ocelot *ocelot, int port,
417 void *priv))
418 {
419 int err;
420
421 mutex_lock(&ocelot->stat_view_lock);
422
423 err = ocelot_port_update_stats(ocelot, port);
424 if (err) {
425 dev_err(ocelot->dev, "Failed to update port %d stats: %pe\n",
426 port, ERR_PTR(err));
427 goto out_unlock;
428 }
429
430 spin_lock(&ocelot->stats_lock);
431
432 ocelot_port_transfer_stats(ocelot, port);
433 cb(ocelot, port, priv);
434
435 spin_unlock(&ocelot->stats_lock);
436
437 out_unlock:
438 mutex_unlock(&ocelot->stat_view_lock);
439 }
440
ocelot_get_sset_count(struct ocelot * ocelot,int port,int sset)441 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
442 {
443 const struct ocelot_stat_layout *layout;
444 int i, num_stats = 0;
445
446 if (sset != ETH_SS_STATS)
447 return -EOPNOTSUPP;
448
449 layout = ocelot_get_stats_layout(ocelot);
450
451 for (i = 0; i < OCELOT_NUM_STATS; i++)
452 if (layout[i].name[0] != '\0')
453 num_stats++;
454
455 return num_stats;
456 }
457 EXPORT_SYMBOL(ocelot_get_sset_count);
458
ocelot_port_ethtool_stats_cb(struct ocelot * ocelot,int port,void * priv)459 static void ocelot_port_ethtool_stats_cb(struct ocelot *ocelot, int port,
460 void *priv)
461 {
462 const struct ocelot_stat_layout *layout;
463 u64 *data = priv;
464 int i;
465
466 layout = ocelot_get_stats_layout(ocelot);
467
468 /* Copy all supported counters */
469 for (i = 0; i < OCELOT_NUM_STATS; i++) {
470 int index = port * OCELOT_NUM_STATS + i;
471
472 if (layout[i].name[0] == '\0')
473 continue;
474
475 *data++ = ocelot->stats[index];
476 }
477 }
478
ocelot_get_ethtool_stats(struct ocelot * ocelot,int port,u64 * data)479 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
480 {
481 ocelot_port_stats_run(ocelot, port, data, ocelot_port_ethtool_stats_cb);
482 }
483 EXPORT_SYMBOL(ocelot_get_ethtool_stats);
484
ocelot_port_pause_stats_cb(struct ocelot * ocelot,int port,void * priv)485 static void ocelot_port_pause_stats_cb(struct ocelot *ocelot, int port, void *priv)
486 {
487 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
488 struct ethtool_pause_stats *pause_stats = priv;
489
490 pause_stats->tx_pause_frames = s[OCELOT_STAT_TX_PAUSE];
491 pause_stats->rx_pause_frames = s[OCELOT_STAT_RX_PAUSE];
492 }
493
ocelot_port_pmac_pause_stats_cb(struct ocelot * ocelot,int port,void * priv)494 static void ocelot_port_pmac_pause_stats_cb(struct ocelot *ocelot, int port,
495 void *priv)
496 {
497 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
498 struct ethtool_pause_stats *pause_stats = priv;
499
500 pause_stats->tx_pause_frames = s[OCELOT_STAT_TX_PMAC_PAUSE];
501 pause_stats->rx_pause_frames = s[OCELOT_STAT_RX_PMAC_PAUSE];
502 }
503
ocelot_port_mm_stats_cb(struct ocelot * ocelot,int port,void * priv)504 static void ocelot_port_mm_stats_cb(struct ocelot *ocelot, int port,
505 void *priv)
506 {
507 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
508 struct ethtool_mm_stats *stats = priv;
509
510 stats->MACMergeFrameAssErrorCount = s[OCELOT_STAT_RX_ASSEMBLY_ERRS];
511 stats->MACMergeFrameSmdErrorCount = s[OCELOT_STAT_RX_SMD_ERRS];
512 stats->MACMergeFrameAssOkCount = s[OCELOT_STAT_RX_ASSEMBLY_OK];
513 stats->MACMergeFragCountRx = s[OCELOT_STAT_RX_MERGE_FRAGMENTS];
514 stats->MACMergeFragCountTx = s[OCELOT_STAT_TX_MERGE_FRAGMENTS];
515 stats->MACMergeHoldCount = s[OCELOT_STAT_TX_MM_HOLD];
516 }
517
ocelot_port_get_pause_stats(struct ocelot * ocelot,int port,struct ethtool_pause_stats * pause_stats)518 void ocelot_port_get_pause_stats(struct ocelot *ocelot, int port,
519 struct ethtool_pause_stats *pause_stats)
520 {
521 struct net_device *dev;
522
523 switch (pause_stats->src) {
524 case ETHTOOL_MAC_STATS_SRC_EMAC:
525 ocelot_port_stats_run(ocelot, port, pause_stats,
526 ocelot_port_pause_stats_cb);
527 break;
528 case ETHTOOL_MAC_STATS_SRC_PMAC:
529 if (ocelot->mm_supported)
530 ocelot_port_stats_run(ocelot, port, pause_stats,
531 ocelot_port_pmac_pause_stats_cb);
532 break;
533 case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
534 dev = ocelot->ops->port_to_netdev(ocelot, port);
535 ethtool_aggregate_pause_stats(dev, pause_stats);
536 break;
537 }
538 }
539 EXPORT_SYMBOL_GPL(ocelot_port_get_pause_stats);
540
ocelot_port_get_mm_stats(struct ocelot * ocelot,int port,struct ethtool_mm_stats * stats)541 void ocelot_port_get_mm_stats(struct ocelot *ocelot, int port,
542 struct ethtool_mm_stats *stats)
543 {
544 if (!ocelot->mm_supported)
545 return;
546
547 ocelot_port_stats_run(ocelot, port, stats, ocelot_port_mm_stats_cb);
548 }
549 EXPORT_SYMBOL_GPL(ocelot_port_get_mm_stats);
550
551 static const struct ethtool_rmon_hist_range ocelot_rmon_ranges[] = {
552 { 64, 64 },
553 { 65, 127 },
554 { 128, 255 },
555 { 256, 511 },
556 { 512, 1023 },
557 { 1024, 1526 },
558 { 1527, 65535 },
559 {},
560 };
561
ocelot_port_rmon_stats_cb(struct ocelot * ocelot,int port,void * priv)562 static void ocelot_port_rmon_stats_cb(struct ocelot *ocelot, int port, void *priv)
563 {
564 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
565 struct ethtool_rmon_stats *rmon_stats = priv;
566
567 rmon_stats->undersize_pkts = s[OCELOT_STAT_RX_SHORTS];
568 rmon_stats->oversize_pkts = s[OCELOT_STAT_RX_LONGS];
569 rmon_stats->fragments = s[OCELOT_STAT_RX_FRAGMENTS];
570 rmon_stats->jabbers = s[OCELOT_STAT_RX_JABBERS];
571
572 rmon_stats->hist[0] = s[OCELOT_STAT_RX_64];
573 rmon_stats->hist[1] = s[OCELOT_STAT_RX_65_127];
574 rmon_stats->hist[2] = s[OCELOT_STAT_RX_128_255];
575 rmon_stats->hist[3] = s[OCELOT_STAT_RX_256_511];
576 rmon_stats->hist[4] = s[OCELOT_STAT_RX_512_1023];
577 rmon_stats->hist[5] = s[OCELOT_STAT_RX_1024_1526];
578 rmon_stats->hist[6] = s[OCELOT_STAT_RX_1527_MAX];
579
580 rmon_stats->hist_tx[0] = s[OCELOT_STAT_TX_64];
581 rmon_stats->hist_tx[1] = s[OCELOT_STAT_TX_65_127];
582 rmon_stats->hist_tx[2] = s[OCELOT_STAT_TX_128_255];
583 rmon_stats->hist_tx[3] = s[OCELOT_STAT_TX_128_255];
584 rmon_stats->hist_tx[4] = s[OCELOT_STAT_TX_256_511];
585 rmon_stats->hist_tx[5] = s[OCELOT_STAT_TX_512_1023];
586 rmon_stats->hist_tx[6] = s[OCELOT_STAT_TX_1024_1526];
587 }
588
ocelot_port_pmac_rmon_stats_cb(struct ocelot * ocelot,int port,void * priv)589 static void ocelot_port_pmac_rmon_stats_cb(struct ocelot *ocelot, int port,
590 void *priv)
591 {
592 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
593 struct ethtool_rmon_stats *rmon_stats = priv;
594
595 rmon_stats->undersize_pkts = s[OCELOT_STAT_RX_PMAC_SHORTS];
596 rmon_stats->oversize_pkts = s[OCELOT_STAT_RX_PMAC_LONGS];
597 rmon_stats->fragments = s[OCELOT_STAT_RX_PMAC_FRAGMENTS];
598 rmon_stats->jabbers = s[OCELOT_STAT_RX_PMAC_JABBERS];
599
600 rmon_stats->hist[0] = s[OCELOT_STAT_RX_PMAC_64];
601 rmon_stats->hist[1] = s[OCELOT_STAT_RX_PMAC_65_127];
602 rmon_stats->hist[2] = s[OCELOT_STAT_RX_PMAC_128_255];
603 rmon_stats->hist[3] = s[OCELOT_STAT_RX_PMAC_256_511];
604 rmon_stats->hist[4] = s[OCELOT_STAT_RX_PMAC_512_1023];
605 rmon_stats->hist[5] = s[OCELOT_STAT_RX_PMAC_1024_1526];
606 rmon_stats->hist[6] = s[OCELOT_STAT_RX_PMAC_1527_MAX];
607
608 rmon_stats->hist_tx[0] = s[OCELOT_STAT_TX_PMAC_64];
609 rmon_stats->hist_tx[1] = s[OCELOT_STAT_TX_PMAC_65_127];
610 rmon_stats->hist_tx[2] = s[OCELOT_STAT_TX_PMAC_128_255];
611 rmon_stats->hist_tx[3] = s[OCELOT_STAT_TX_PMAC_128_255];
612 rmon_stats->hist_tx[4] = s[OCELOT_STAT_TX_PMAC_256_511];
613 rmon_stats->hist_tx[5] = s[OCELOT_STAT_TX_PMAC_512_1023];
614 rmon_stats->hist_tx[6] = s[OCELOT_STAT_TX_PMAC_1024_1526];
615 }
616
ocelot_port_get_rmon_stats(struct ocelot * ocelot,int port,struct ethtool_rmon_stats * rmon_stats,const struct ethtool_rmon_hist_range ** ranges)617 void ocelot_port_get_rmon_stats(struct ocelot *ocelot, int port,
618 struct ethtool_rmon_stats *rmon_stats,
619 const struct ethtool_rmon_hist_range **ranges)
620 {
621 struct net_device *dev;
622
623 *ranges = ocelot_rmon_ranges;
624
625 switch (rmon_stats->src) {
626 case ETHTOOL_MAC_STATS_SRC_EMAC:
627 ocelot_port_stats_run(ocelot, port, rmon_stats,
628 ocelot_port_rmon_stats_cb);
629 break;
630 case ETHTOOL_MAC_STATS_SRC_PMAC:
631 if (ocelot->mm_supported)
632 ocelot_port_stats_run(ocelot, port, rmon_stats,
633 ocelot_port_pmac_rmon_stats_cb);
634 break;
635 case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
636 dev = ocelot->ops->port_to_netdev(ocelot, port);
637 ethtool_aggregate_rmon_stats(dev, rmon_stats);
638 break;
639 }
640 }
641 EXPORT_SYMBOL_GPL(ocelot_port_get_rmon_stats);
642
ocelot_port_ctrl_stats_cb(struct ocelot * ocelot,int port,void * priv)643 static void ocelot_port_ctrl_stats_cb(struct ocelot *ocelot, int port, void *priv)
644 {
645 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
646 struct ethtool_eth_ctrl_stats *ctrl_stats = priv;
647
648 ctrl_stats->MACControlFramesReceived = s[OCELOT_STAT_RX_CONTROL];
649 }
650
ocelot_port_pmac_ctrl_stats_cb(struct ocelot * ocelot,int port,void * priv)651 static void ocelot_port_pmac_ctrl_stats_cb(struct ocelot *ocelot, int port,
652 void *priv)
653 {
654 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
655 struct ethtool_eth_ctrl_stats *ctrl_stats = priv;
656
657 ctrl_stats->MACControlFramesReceived = s[OCELOT_STAT_RX_PMAC_CONTROL];
658 }
659
ocelot_port_get_eth_ctrl_stats(struct ocelot * ocelot,int port,struct ethtool_eth_ctrl_stats * ctrl_stats)660 void ocelot_port_get_eth_ctrl_stats(struct ocelot *ocelot, int port,
661 struct ethtool_eth_ctrl_stats *ctrl_stats)
662 {
663 struct net_device *dev;
664
665 switch (ctrl_stats->src) {
666 case ETHTOOL_MAC_STATS_SRC_EMAC:
667 ocelot_port_stats_run(ocelot, port, ctrl_stats,
668 ocelot_port_ctrl_stats_cb);
669 break;
670 case ETHTOOL_MAC_STATS_SRC_PMAC:
671 if (ocelot->mm_supported)
672 ocelot_port_stats_run(ocelot, port, ctrl_stats,
673 ocelot_port_pmac_ctrl_stats_cb);
674 break;
675 case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
676 dev = ocelot->ops->port_to_netdev(ocelot, port);
677 ethtool_aggregate_ctrl_stats(dev, ctrl_stats);
678 break;
679 }
680 }
681 EXPORT_SYMBOL_GPL(ocelot_port_get_eth_ctrl_stats);
682
ocelot_port_mac_stats_cb(struct ocelot * ocelot,int port,void * priv)683 static void ocelot_port_mac_stats_cb(struct ocelot *ocelot, int port, void *priv)
684 {
685 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
686 struct ethtool_eth_mac_stats *mac_stats = priv;
687
688 mac_stats->OctetsTransmittedOK = s[OCELOT_STAT_TX_OCTETS];
689 mac_stats->FramesTransmittedOK = s[OCELOT_STAT_TX_64] +
690 s[OCELOT_STAT_TX_65_127] +
691 s[OCELOT_STAT_TX_128_255] +
692 s[OCELOT_STAT_TX_256_511] +
693 s[OCELOT_STAT_TX_512_1023] +
694 s[OCELOT_STAT_TX_1024_1526] +
695 s[OCELOT_STAT_TX_1527_MAX];
696 mac_stats->OctetsReceivedOK = s[OCELOT_STAT_RX_OCTETS];
697 mac_stats->FramesReceivedOK = s[OCELOT_STAT_RX_GREEN_PRIO_0] +
698 s[OCELOT_STAT_RX_GREEN_PRIO_1] +
699 s[OCELOT_STAT_RX_GREEN_PRIO_2] +
700 s[OCELOT_STAT_RX_GREEN_PRIO_3] +
701 s[OCELOT_STAT_RX_GREEN_PRIO_4] +
702 s[OCELOT_STAT_RX_GREEN_PRIO_5] +
703 s[OCELOT_STAT_RX_GREEN_PRIO_6] +
704 s[OCELOT_STAT_RX_GREEN_PRIO_7] +
705 s[OCELOT_STAT_RX_YELLOW_PRIO_0] +
706 s[OCELOT_STAT_RX_YELLOW_PRIO_1] +
707 s[OCELOT_STAT_RX_YELLOW_PRIO_2] +
708 s[OCELOT_STAT_RX_YELLOW_PRIO_3] +
709 s[OCELOT_STAT_RX_YELLOW_PRIO_4] +
710 s[OCELOT_STAT_RX_YELLOW_PRIO_5] +
711 s[OCELOT_STAT_RX_YELLOW_PRIO_6] +
712 s[OCELOT_STAT_RX_YELLOW_PRIO_7];
713 mac_stats->MulticastFramesXmittedOK = s[OCELOT_STAT_TX_MULTICAST];
714 mac_stats->BroadcastFramesXmittedOK = s[OCELOT_STAT_TX_BROADCAST];
715 mac_stats->MulticastFramesReceivedOK = s[OCELOT_STAT_RX_MULTICAST];
716 mac_stats->BroadcastFramesReceivedOK = s[OCELOT_STAT_RX_BROADCAST];
717 mac_stats->FrameTooLongErrors = s[OCELOT_STAT_RX_LONGS];
718 /* Sadly, C_RX_CRC is the sum of FCS and alignment errors, they are not
719 * counted individually.
720 */
721 mac_stats->FrameCheckSequenceErrors = s[OCELOT_STAT_RX_CRC_ALIGN_ERRS];
722 mac_stats->AlignmentErrors = s[OCELOT_STAT_RX_CRC_ALIGN_ERRS];
723 }
724
ocelot_port_pmac_mac_stats_cb(struct ocelot * ocelot,int port,void * priv)725 static void ocelot_port_pmac_mac_stats_cb(struct ocelot *ocelot, int port,
726 void *priv)
727 {
728 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
729 struct ethtool_eth_mac_stats *mac_stats = priv;
730
731 mac_stats->OctetsTransmittedOK = s[OCELOT_STAT_TX_PMAC_OCTETS];
732 mac_stats->FramesTransmittedOK = s[OCELOT_STAT_TX_PMAC_64] +
733 s[OCELOT_STAT_TX_PMAC_65_127] +
734 s[OCELOT_STAT_TX_PMAC_128_255] +
735 s[OCELOT_STAT_TX_PMAC_256_511] +
736 s[OCELOT_STAT_TX_PMAC_512_1023] +
737 s[OCELOT_STAT_TX_PMAC_1024_1526] +
738 s[OCELOT_STAT_TX_PMAC_1527_MAX];
739 mac_stats->OctetsReceivedOK = s[OCELOT_STAT_RX_PMAC_OCTETS];
740 mac_stats->FramesReceivedOK = s[OCELOT_STAT_RX_PMAC_64] +
741 s[OCELOT_STAT_RX_PMAC_65_127] +
742 s[OCELOT_STAT_RX_PMAC_128_255] +
743 s[OCELOT_STAT_RX_PMAC_256_511] +
744 s[OCELOT_STAT_RX_PMAC_512_1023] +
745 s[OCELOT_STAT_RX_PMAC_1024_1526] +
746 s[OCELOT_STAT_RX_PMAC_1527_MAX];
747 mac_stats->MulticastFramesXmittedOK = s[OCELOT_STAT_TX_PMAC_MULTICAST];
748 mac_stats->BroadcastFramesXmittedOK = s[OCELOT_STAT_TX_PMAC_BROADCAST];
749 mac_stats->MulticastFramesReceivedOK = s[OCELOT_STAT_RX_PMAC_MULTICAST];
750 mac_stats->BroadcastFramesReceivedOK = s[OCELOT_STAT_RX_PMAC_BROADCAST];
751 mac_stats->FrameTooLongErrors = s[OCELOT_STAT_RX_PMAC_LONGS];
752 /* Sadly, C_RX_CRC is the sum of FCS and alignment errors, they are not
753 * counted individually.
754 */
755 mac_stats->FrameCheckSequenceErrors = s[OCELOT_STAT_RX_PMAC_CRC_ALIGN_ERRS];
756 mac_stats->AlignmentErrors = s[OCELOT_STAT_RX_PMAC_CRC_ALIGN_ERRS];
757 }
758
ocelot_port_get_eth_mac_stats(struct ocelot * ocelot,int port,struct ethtool_eth_mac_stats * mac_stats)759 void ocelot_port_get_eth_mac_stats(struct ocelot *ocelot, int port,
760 struct ethtool_eth_mac_stats *mac_stats)
761 {
762 struct net_device *dev;
763
764 switch (mac_stats->src) {
765 case ETHTOOL_MAC_STATS_SRC_EMAC:
766 ocelot_port_stats_run(ocelot, port, mac_stats,
767 ocelot_port_mac_stats_cb);
768 break;
769 case ETHTOOL_MAC_STATS_SRC_PMAC:
770 if (ocelot->mm_supported)
771 ocelot_port_stats_run(ocelot, port, mac_stats,
772 ocelot_port_pmac_mac_stats_cb);
773 break;
774 case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
775 dev = ocelot->ops->port_to_netdev(ocelot, port);
776 ethtool_aggregate_mac_stats(dev, mac_stats);
777 break;
778 }
779 }
780 EXPORT_SYMBOL_GPL(ocelot_port_get_eth_mac_stats);
781
ocelot_port_phy_stats_cb(struct ocelot * ocelot,int port,void * priv)782 static void ocelot_port_phy_stats_cb(struct ocelot *ocelot, int port, void *priv)
783 {
784 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
785 struct ethtool_eth_phy_stats *phy_stats = priv;
786
787 phy_stats->SymbolErrorDuringCarrier = s[OCELOT_STAT_RX_SYM_ERRS];
788 }
789
ocelot_port_pmac_phy_stats_cb(struct ocelot * ocelot,int port,void * priv)790 static void ocelot_port_pmac_phy_stats_cb(struct ocelot *ocelot, int port,
791 void *priv)
792 {
793 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
794 struct ethtool_eth_phy_stats *phy_stats = priv;
795
796 phy_stats->SymbolErrorDuringCarrier = s[OCELOT_STAT_RX_PMAC_SYM_ERRS];
797 }
798
ocelot_port_get_eth_phy_stats(struct ocelot * ocelot,int port,struct ethtool_eth_phy_stats * phy_stats)799 void ocelot_port_get_eth_phy_stats(struct ocelot *ocelot, int port,
800 struct ethtool_eth_phy_stats *phy_stats)
801 {
802 struct net_device *dev;
803
804 switch (phy_stats->src) {
805 case ETHTOOL_MAC_STATS_SRC_EMAC:
806 ocelot_port_stats_run(ocelot, port, phy_stats,
807 ocelot_port_phy_stats_cb);
808 break;
809 case ETHTOOL_MAC_STATS_SRC_PMAC:
810 if (ocelot->mm_supported)
811 ocelot_port_stats_run(ocelot, port, phy_stats,
812 ocelot_port_pmac_phy_stats_cb);
813 break;
814 case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
815 dev = ocelot->ops->port_to_netdev(ocelot, port);
816 ethtool_aggregate_phy_stats(dev, phy_stats);
817 break;
818 }
819 }
820 EXPORT_SYMBOL_GPL(ocelot_port_get_eth_phy_stats);
821
ocelot_port_get_stats64(struct ocelot * ocelot,int port,struct rtnl_link_stats64 * stats)822 void ocelot_port_get_stats64(struct ocelot *ocelot, int port,
823 struct rtnl_link_stats64 *stats)
824 {
825 u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
826
827 spin_lock(&ocelot->stats_lock);
828
829 /* Get Rx stats */
830 stats->rx_bytes = s[OCELOT_STAT_RX_OCTETS];
831 stats->rx_packets = s[OCELOT_STAT_RX_SHORTS] +
832 s[OCELOT_STAT_RX_FRAGMENTS] +
833 s[OCELOT_STAT_RX_JABBERS] +
834 s[OCELOT_STAT_RX_LONGS] +
835 s[OCELOT_STAT_RX_64] +
836 s[OCELOT_STAT_RX_65_127] +
837 s[OCELOT_STAT_RX_128_255] +
838 s[OCELOT_STAT_RX_256_511] +
839 s[OCELOT_STAT_RX_512_1023] +
840 s[OCELOT_STAT_RX_1024_1526] +
841 s[OCELOT_STAT_RX_1527_MAX];
842 stats->multicast = s[OCELOT_STAT_RX_MULTICAST];
843 stats->rx_missed_errors = s[OCELOT_STAT_DROP_TAIL];
844 stats->rx_dropped = s[OCELOT_STAT_RX_RED_PRIO_0] +
845 s[OCELOT_STAT_RX_RED_PRIO_1] +
846 s[OCELOT_STAT_RX_RED_PRIO_2] +
847 s[OCELOT_STAT_RX_RED_PRIO_3] +
848 s[OCELOT_STAT_RX_RED_PRIO_4] +
849 s[OCELOT_STAT_RX_RED_PRIO_5] +
850 s[OCELOT_STAT_RX_RED_PRIO_6] +
851 s[OCELOT_STAT_RX_RED_PRIO_7] +
852 s[OCELOT_STAT_DROP_LOCAL] +
853 s[OCELOT_STAT_DROP_YELLOW_PRIO_0] +
854 s[OCELOT_STAT_DROP_YELLOW_PRIO_1] +
855 s[OCELOT_STAT_DROP_YELLOW_PRIO_2] +
856 s[OCELOT_STAT_DROP_YELLOW_PRIO_3] +
857 s[OCELOT_STAT_DROP_YELLOW_PRIO_4] +
858 s[OCELOT_STAT_DROP_YELLOW_PRIO_5] +
859 s[OCELOT_STAT_DROP_YELLOW_PRIO_6] +
860 s[OCELOT_STAT_DROP_YELLOW_PRIO_7] +
861 s[OCELOT_STAT_DROP_GREEN_PRIO_0] +
862 s[OCELOT_STAT_DROP_GREEN_PRIO_1] +
863 s[OCELOT_STAT_DROP_GREEN_PRIO_2] +
864 s[OCELOT_STAT_DROP_GREEN_PRIO_3] +
865 s[OCELOT_STAT_DROP_GREEN_PRIO_4] +
866 s[OCELOT_STAT_DROP_GREEN_PRIO_5] +
867 s[OCELOT_STAT_DROP_GREEN_PRIO_6] +
868 s[OCELOT_STAT_DROP_GREEN_PRIO_7];
869
870 /* Get Tx stats */
871 stats->tx_bytes = s[OCELOT_STAT_TX_OCTETS];
872 stats->tx_packets = s[OCELOT_STAT_TX_64] +
873 s[OCELOT_STAT_TX_65_127] +
874 s[OCELOT_STAT_TX_128_255] +
875 s[OCELOT_STAT_TX_256_511] +
876 s[OCELOT_STAT_TX_512_1023] +
877 s[OCELOT_STAT_TX_1024_1526] +
878 s[OCELOT_STAT_TX_1527_MAX];
879 stats->tx_dropped = s[OCELOT_STAT_TX_DROPS] +
880 s[OCELOT_STAT_TX_AGED];
881 stats->collisions = s[OCELOT_STAT_TX_COLLISION];
882
883 spin_unlock(&ocelot->stats_lock);
884 }
885 EXPORT_SYMBOL(ocelot_port_get_stats64);
886
ocelot_prepare_stats_regions(struct ocelot * ocelot)887 static int ocelot_prepare_stats_regions(struct ocelot *ocelot)
888 {
889 struct ocelot_stats_region *region = NULL;
890 const struct ocelot_stat_layout *layout;
891 unsigned int last = 0;
892 int i;
893
894 INIT_LIST_HEAD(&ocelot->stats_regions);
895
896 layout = ocelot_get_stats_layout(ocelot);
897
898 for (i = 0; i < OCELOT_NUM_STATS; i++) {
899 if (!layout[i].reg)
900 continue;
901
902 if (region && layout[i].reg == last + 4) {
903 region->count++;
904 } else {
905 region = devm_kzalloc(ocelot->dev, sizeof(*region),
906 GFP_KERNEL);
907 if (!region)
908 return -ENOMEM;
909
910 /* enum ocelot_stat must be kept sorted in the same
911 * order as layout[i].reg in order to have efficient
912 * bulking
913 */
914 WARN_ON(last >= layout[i].reg);
915
916 region->base = layout[i].reg;
917 region->count = 1;
918 list_add_tail(®ion->node, &ocelot->stats_regions);
919 }
920
921 last = layout[i].reg;
922 }
923
924 list_for_each_entry(region, &ocelot->stats_regions, node) {
925 region->buf = devm_kcalloc(ocelot->dev, region->count,
926 sizeof(*region->buf), GFP_KERNEL);
927 if (!region->buf)
928 return -ENOMEM;
929 }
930
931 return 0;
932 }
933
ocelot_stats_init(struct ocelot * ocelot)934 int ocelot_stats_init(struct ocelot *ocelot)
935 {
936 char queue_name[32];
937 int ret;
938
939 ocelot->stats = devm_kcalloc(ocelot->dev,
940 ocelot->num_phys_ports * OCELOT_NUM_STATS,
941 sizeof(u64), GFP_KERNEL);
942 if (!ocelot->stats)
943 return -ENOMEM;
944
945 snprintf(queue_name, sizeof(queue_name), "%s-stats",
946 dev_name(ocelot->dev));
947 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
948 if (!ocelot->stats_queue)
949 return -ENOMEM;
950
951 spin_lock_init(&ocelot->stats_lock);
952 mutex_init(&ocelot->stat_view_lock);
953
954 ret = ocelot_prepare_stats_regions(ocelot);
955 if (ret) {
956 destroy_workqueue(ocelot->stats_queue);
957 return ret;
958 }
959
960 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
961 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
962 OCELOT_STATS_CHECK_DELAY);
963
964 return 0;
965 }
966
ocelot_stats_deinit(struct ocelot * ocelot)967 void ocelot_stats_deinit(struct ocelot *ocelot)
968 {
969 cancel_delayed_work(&ocelot->stats_work);
970 destroy_workqueue(ocelot->stats_queue);
971 }
972
973