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 	enum ocelot_reg 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 	enum ocelot_reg base;
261 	enum ocelot_stat first_stat;
262 	int count;
263 	u32 *buf;
264 };
265 
266 static const struct ocelot_stat_layout ocelot_stats_layout[OCELOT_NUM_STATS] = {
267 	OCELOT_COMMON_STATS,
268 };
269 
270 static const struct ocelot_stat_layout ocelot_mm_stats_layout[OCELOT_NUM_STATS] = {
271 	OCELOT_COMMON_STATS,
272 	OCELOT_STAT(RX_ASSEMBLY_ERRS),
273 	OCELOT_STAT(RX_SMD_ERRS),
274 	OCELOT_STAT(RX_ASSEMBLY_OK),
275 	OCELOT_STAT(RX_MERGE_FRAGMENTS),
276 	OCELOT_STAT(TX_MERGE_FRAGMENTS),
277 	OCELOT_STAT(TX_MM_HOLD),
278 	OCELOT_STAT(RX_PMAC_OCTETS),
279 	OCELOT_STAT(RX_PMAC_UNICAST),
280 	OCELOT_STAT(RX_PMAC_MULTICAST),
281 	OCELOT_STAT(RX_PMAC_BROADCAST),
282 	OCELOT_STAT(RX_PMAC_SHORTS),
283 	OCELOT_STAT(RX_PMAC_FRAGMENTS),
284 	OCELOT_STAT(RX_PMAC_JABBERS),
285 	OCELOT_STAT(RX_PMAC_CRC_ALIGN_ERRS),
286 	OCELOT_STAT(RX_PMAC_SYM_ERRS),
287 	OCELOT_STAT(RX_PMAC_64),
288 	OCELOT_STAT(RX_PMAC_65_127),
289 	OCELOT_STAT(RX_PMAC_128_255),
290 	OCELOT_STAT(RX_PMAC_256_511),
291 	OCELOT_STAT(RX_PMAC_512_1023),
292 	OCELOT_STAT(RX_PMAC_1024_1526),
293 	OCELOT_STAT(RX_PMAC_1527_MAX),
294 	OCELOT_STAT(RX_PMAC_PAUSE),
295 	OCELOT_STAT(RX_PMAC_CONTROL),
296 	OCELOT_STAT(RX_PMAC_LONGS),
297 	OCELOT_STAT(TX_PMAC_OCTETS),
298 	OCELOT_STAT(TX_PMAC_UNICAST),
299 	OCELOT_STAT(TX_PMAC_MULTICAST),
300 	OCELOT_STAT(TX_PMAC_BROADCAST),
301 	OCELOT_STAT(TX_PMAC_PAUSE),
302 	OCELOT_STAT(TX_PMAC_64),
303 	OCELOT_STAT(TX_PMAC_65_127),
304 	OCELOT_STAT(TX_PMAC_128_255),
305 	OCELOT_STAT(TX_PMAC_256_511),
306 	OCELOT_STAT(TX_PMAC_512_1023),
307 	OCELOT_STAT(TX_PMAC_1024_1526),
308 	OCELOT_STAT(TX_PMAC_1527_MAX),
309 };
310 
311 static const struct ocelot_stat_layout *
312 ocelot_get_stats_layout(struct ocelot *ocelot)
313 {
314 	if (ocelot->mm_supported)
315 		return ocelot_mm_stats_layout;
316 
317 	return ocelot_stats_layout;
318 }
319 
320 /* Read the counters from hardware and keep them in region->buf.
321  * Caller must hold &ocelot->stat_view_lock.
322  */
323 static int ocelot_port_update_stats(struct ocelot *ocelot, int port)
324 {
325 	struct ocelot_stats_region *region;
326 	int err;
327 
328 	/* Configure the port to read the stats from */
329 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port), SYS_STAT_CFG);
330 
331 	list_for_each_entry(region, &ocelot->stats_regions, node) {
332 		err = ocelot_bulk_read(ocelot, region->base, region->buf,
333 				       region->count);
334 		if (err)
335 			return err;
336 	}
337 
338 	return 0;
339 }
340 
341 /* Transfer the counters from region->buf to ocelot->stats.
342  * Caller must hold &ocelot->stat_view_lock and &ocelot->stats_lock.
343  */
344 static void ocelot_port_transfer_stats(struct ocelot *ocelot, int port)
345 {
346 	struct ocelot_stats_region *region;
347 	int j;
348 
349 	list_for_each_entry(region, &ocelot->stats_regions, node) {
350 		unsigned int idx = port * OCELOT_NUM_STATS + region->first_stat;
351 
352 		for (j = 0; j < region->count; j++) {
353 			u64 *stat = &ocelot->stats[idx + j];
354 			u64 val = region->buf[j];
355 
356 			if (val < (*stat & U32_MAX))
357 				*stat += (u64)1 << 32;
358 
359 			*stat = (*stat & ~(u64)U32_MAX) + val;
360 		}
361 	}
362 }
363 
364 static void ocelot_check_stats_work(struct work_struct *work)
365 {
366 	struct delayed_work *del_work = to_delayed_work(work);
367 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
368 					     stats_work);
369 	int port, err;
370 
371 	mutex_lock(&ocelot->stat_view_lock);
372 
373 	for (port = 0; port < ocelot->num_phys_ports; port++) {
374 		err = ocelot_port_update_stats(ocelot, port);
375 		if (err)
376 			break;
377 
378 		spin_lock(&ocelot->stats_lock);
379 		ocelot_port_transfer_stats(ocelot, port);
380 		spin_unlock(&ocelot->stats_lock);
381 	}
382 
383 	if (!err && ocelot->ops->update_stats)
384 		ocelot->ops->update_stats(ocelot);
385 
386 	mutex_unlock(&ocelot->stat_view_lock);
387 
388 	if (err)
389 		dev_err(ocelot->dev, "Error %d updating ethtool stats\n",  err);
390 
391 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
392 			   OCELOT_STATS_CHECK_DELAY);
393 }
394 
395 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
396 {
397 	const struct ocelot_stat_layout *layout;
398 	int i;
399 
400 	if (sset != ETH_SS_STATS)
401 		return;
402 
403 	layout = ocelot_get_stats_layout(ocelot);
404 
405 	for (i = 0; i < OCELOT_NUM_STATS; i++) {
406 		if (layout[i].name[0] == '\0')
407 			continue;
408 
409 		memcpy(data, layout[i].name, ETH_GSTRING_LEN);
410 		data += ETH_GSTRING_LEN;
411 	}
412 }
413 EXPORT_SYMBOL(ocelot_get_strings);
414 
415 /* Update ocelot->stats for the given port and run the given callback */
416 static void ocelot_port_stats_run(struct ocelot *ocelot, int port, void *priv,
417 				  void (*cb)(struct ocelot *ocelot, int port,
418 					     void *priv))
419 {
420 	int err;
421 
422 	mutex_lock(&ocelot->stat_view_lock);
423 
424 	err = ocelot_port_update_stats(ocelot, port);
425 	if (err) {
426 		dev_err(ocelot->dev, "Failed to update port %d stats: %pe\n",
427 			port, ERR_PTR(err));
428 		goto out_unlock;
429 	}
430 
431 	spin_lock(&ocelot->stats_lock);
432 
433 	ocelot_port_transfer_stats(ocelot, port);
434 	cb(ocelot, port, priv);
435 
436 	spin_unlock(&ocelot->stats_lock);
437 
438 out_unlock:
439 	mutex_unlock(&ocelot->stat_view_lock);
440 }
441 
442 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
443 {
444 	const struct ocelot_stat_layout *layout;
445 	int i, num_stats = 0;
446 
447 	if (sset != ETH_SS_STATS)
448 		return -EOPNOTSUPP;
449 
450 	layout = ocelot_get_stats_layout(ocelot);
451 
452 	for (i = 0; i < OCELOT_NUM_STATS; i++)
453 		if (layout[i].name[0] != '\0')
454 			num_stats++;
455 
456 	return num_stats;
457 }
458 EXPORT_SYMBOL(ocelot_get_sset_count);
459 
460 static void ocelot_port_ethtool_stats_cb(struct ocelot *ocelot, int port,
461 					 void *priv)
462 {
463 	const struct ocelot_stat_layout *layout;
464 	u64 *data = priv;
465 	int i;
466 
467 	layout = ocelot_get_stats_layout(ocelot);
468 
469 	/* Copy all supported counters */
470 	for (i = 0; i < OCELOT_NUM_STATS; i++) {
471 		int index = port * OCELOT_NUM_STATS + i;
472 
473 		if (layout[i].name[0] == '\0')
474 			continue;
475 
476 		*data++ = ocelot->stats[index];
477 	}
478 }
479 
480 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
481 {
482 	ocelot_port_stats_run(ocelot, port, data, ocelot_port_ethtool_stats_cb);
483 }
484 EXPORT_SYMBOL(ocelot_get_ethtool_stats);
485 
486 static void ocelot_port_pause_stats_cb(struct ocelot *ocelot, int port, void *priv)
487 {
488 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
489 	struct ethtool_pause_stats *pause_stats = priv;
490 
491 	pause_stats->tx_pause_frames = s[OCELOT_STAT_TX_PAUSE];
492 	pause_stats->rx_pause_frames = s[OCELOT_STAT_RX_PAUSE];
493 }
494 
495 static void ocelot_port_pmac_pause_stats_cb(struct ocelot *ocelot, int port,
496 					    void *priv)
497 {
498 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
499 	struct ethtool_pause_stats *pause_stats = priv;
500 
501 	pause_stats->tx_pause_frames = s[OCELOT_STAT_TX_PMAC_PAUSE];
502 	pause_stats->rx_pause_frames = s[OCELOT_STAT_RX_PMAC_PAUSE];
503 }
504 
505 static void ocelot_port_mm_stats_cb(struct ocelot *ocelot, int port,
506 				    void *priv)
507 {
508 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
509 	struct ethtool_mm_stats *stats = priv;
510 
511 	stats->MACMergeFrameAssErrorCount = s[OCELOT_STAT_RX_ASSEMBLY_ERRS];
512 	stats->MACMergeFrameSmdErrorCount = s[OCELOT_STAT_RX_SMD_ERRS];
513 	stats->MACMergeFrameAssOkCount = s[OCELOT_STAT_RX_ASSEMBLY_OK];
514 	stats->MACMergeFragCountRx = s[OCELOT_STAT_RX_MERGE_FRAGMENTS];
515 	stats->MACMergeFragCountTx = s[OCELOT_STAT_TX_MERGE_FRAGMENTS];
516 	stats->MACMergeHoldCount = s[OCELOT_STAT_TX_MM_HOLD];
517 }
518 
519 void ocelot_port_get_pause_stats(struct ocelot *ocelot, int port,
520 				 struct ethtool_pause_stats *pause_stats)
521 {
522 	struct net_device *dev;
523 
524 	switch (pause_stats->src) {
525 	case ETHTOOL_MAC_STATS_SRC_EMAC:
526 		ocelot_port_stats_run(ocelot, port, pause_stats,
527 				      ocelot_port_pause_stats_cb);
528 		break;
529 	case ETHTOOL_MAC_STATS_SRC_PMAC:
530 		if (ocelot->mm_supported)
531 			ocelot_port_stats_run(ocelot, port, pause_stats,
532 					      ocelot_port_pmac_pause_stats_cb);
533 		break;
534 	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
535 		dev = ocelot->ops->port_to_netdev(ocelot, port);
536 		ethtool_aggregate_pause_stats(dev, pause_stats);
537 		break;
538 	}
539 }
540 EXPORT_SYMBOL_GPL(ocelot_port_get_pause_stats);
541 
542 void ocelot_port_get_mm_stats(struct ocelot *ocelot, int port,
543 			      struct ethtool_mm_stats *stats)
544 {
545 	if (!ocelot->mm_supported)
546 		return;
547 
548 	ocelot_port_stats_run(ocelot, port, stats, ocelot_port_mm_stats_cb);
549 }
550 EXPORT_SYMBOL_GPL(ocelot_port_get_mm_stats);
551 
552 static const struct ethtool_rmon_hist_range ocelot_rmon_ranges[] = {
553 	{   64,    64 },
554 	{   65,   127 },
555 	{  128,   255 },
556 	{  256,   511 },
557 	{  512,  1023 },
558 	{ 1024,  1526 },
559 	{ 1527, 65535 },
560 	{},
561 };
562 
563 static void ocelot_port_rmon_stats_cb(struct ocelot *ocelot, int port, void *priv)
564 {
565 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
566 	struct ethtool_rmon_stats *rmon_stats = priv;
567 
568 	rmon_stats->undersize_pkts = s[OCELOT_STAT_RX_SHORTS];
569 	rmon_stats->oversize_pkts = s[OCELOT_STAT_RX_LONGS];
570 	rmon_stats->fragments = s[OCELOT_STAT_RX_FRAGMENTS];
571 	rmon_stats->jabbers = s[OCELOT_STAT_RX_JABBERS];
572 
573 	rmon_stats->hist[0] = s[OCELOT_STAT_RX_64];
574 	rmon_stats->hist[1] = s[OCELOT_STAT_RX_65_127];
575 	rmon_stats->hist[2] = s[OCELOT_STAT_RX_128_255];
576 	rmon_stats->hist[3] = s[OCELOT_STAT_RX_256_511];
577 	rmon_stats->hist[4] = s[OCELOT_STAT_RX_512_1023];
578 	rmon_stats->hist[5] = s[OCELOT_STAT_RX_1024_1526];
579 	rmon_stats->hist[6] = s[OCELOT_STAT_RX_1527_MAX];
580 
581 	rmon_stats->hist_tx[0] = s[OCELOT_STAT_TX_64];
582 	rmon_stats->hist_tx[1] = s[OCELOT_STAT_TX_65_127];
583 	rmon_stats->hist_tx[2] = s[OCELOT_STAT_TX_128_255];
584 	rmon_stats->hist_tx[3] = s[OCELOT_STAT_TX_128_255];
585 	rmon_stats->hist_tx[4] = s[OCELOT_STAT_TX_256_511];
586 	rmon_stats->hist_tx[5] = s[OCELOT_STAT_TX_512_1023];
587 	rmon_stats->hist_tx[6] = s[OCELOT_STAT_TX_1024_1526];
588 }
589 
590 static void ocelot_port_pmac_rmon_stats_cb(struct ocelot *ocelot, int port,
591 					   void *priv)
592 {
593 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
594 	struct ethtool_rmon_stats *rmon_stats = priv;
595 
596 	rmon_stats->undersize_pkts = s[OCELOT_STAT_RX_PMAC_SHORTS];
597 	rmon_stats->oversize_pkts = s[OCELOT_STAT_RX_PMAC_LONGS];
598 	rmon_stats->fragments = s[OCELOT_STAT_RX_PMAC_FRAGMENTS];
599 	rmon_stats->jabbers = s[OCELOT_STAT_RX_PMAC_JABBERS];
600 
601 	rmon_stats->hist[0] = s[OCELOT_STAT_RX_PMAC_64];
602 	rmon_stats->hist[1] = s[OCELOT_STAT_RX_PMAC_65_127];
603 	rmon_stats->hist[2] = s[OCELOT_STAT_RX_PMAC_128_255];
604 	rmon_stats->hist[3] = s[OCELOT_STAT_RX_PMAC_256_511];
605 	rmon_stats->hist[4] = s[OCELOT_STAT_RX_PMAC_512_1023];
606 	rmon_stats->hist[5] = s[OCELOT_STAT_RX_PMAC_1024_1526];
607 	rmon_stats->hist[6] = s[OCELOT_STAT_RX_PMAC_1527_MAX];
608 
609 	rmon_stats->hist_tx[0] = s[OCELOT_STAT_TX_PMAC_64];
610 	rmon_stats->hist_tx[1] = s[OCELOT_STAT_TX_PMAC_65_127];
611 	rmon_stats->hist_tx[2] = s[OCELOT_STAT_TX_PMAC_128_255];
612 	rmon_stats->hist_tx[3] = s[OCELOT_STAT_TX_PMAC_128_255];
613 	rmon_stats->hist_tx[4] = s[OCELOT_STAT_TX_PMAC_256_511];
614 	rmon_stats->hist_tx[5] = s[OCELOT_STAT_TX_PMAC_512_1023];
615 	rmon_stats->hist_tx[6] = s[OCELOT_STAT_TX_PMAC_1024_1526];
616 }
617 
618 void ocelot_port_get_rmon_stats(struct ocelot *ocelot, int port,
619 				struct ethtool_rmon_stats *rmon_stats,
620 				const struct ethtool_rmon_hist_range **ranges)
621 {
622 	struct net_device *dev;
623 
624 	*ranges = ocelot_rmon_ranges;
625 
626 	switch (rmon_stats->src) {
627 	case ETHTOOL_MAC_STATS_SRC_EMAC:
628 		ocelot_port_stats_run(ocelot, port, rmon_stats,
629 				      ocelot_port_rmon_stats_cb);
630 		break;
631 	case ETHTOOL_MAC_STATS_SRC_PMAC:
632 		if (ocelot->mm_supported)
633 			ocelot_port_stats_run(ocelot, port, rmon_stats,
634 					      ocelot_port_pmac_rmon_stats_cb);
635 		break;
636 	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
637 		dev = ocelot->ops->port_to_netdev(ocelot, port);
638 		ethtool_aggregate_rmon_stats(dev, rmon_stats);
639 		break;
640 	}
641 }
642 EXPORT_SYMBOL_GPL(ocelot_port_get_rmon_stats);
643 
644 static void ocelot_port_ctrl_stats_cb(struct ocelot *ocelot, int port, void *priv)
645 {
646 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
647 	struct ethtool_eth_ctrl_stats *ctrl_stats = priv;
648 
649 	ctrl_stats->MACControlFramesReceived = s[OCELOT_STAT_RX_CONTROL];
650 }
651 
652 static void ocelot_port_pmac_ctrl_stats_cb(struct ocelot *ocelot, int port,
653 					   void *priv)
654 {
655 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
656 	struct ethtool_eth_ctrl_stats *ctrl_stats = priv;
657 
658 	ctrl_stats->MACControlFramesReceived = s[OCELOT_STAT_RX_PMAC_CONTROL];
659 }
660 
661 void ocelot_port_get_eth_ctrl_stats(struct ocelot *ocelot, int port,
662 				    struct ethtool_eth_ctrl_stats *ctrl_stats)
663 {
664 	struct net_device *dev;
665 
666 	switch (ctrl_stats->src) {
667 	case ETHTOOL_MAC_STATS_SRC_EMAC:
668 		ocelot_port_stats_run(ocelot, port, ctrl_stats,
669 				      ocelot_port_ctrl_stats_cb);
670 		break;
671 	case ETHTOOL_MAC_STATS_SRC_PMAC:
672 		if (ocelot->mm_supported)
673 			ocelot_port_stats_run(ocelot, port, ctrl_stats,
674 					      ocelot_port_pmac_ctrl_stats_cb);
675 		break;
676 	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
677 		dev = ocelot->ops->port_to_netdev(ocelot, port);
678 		ethtool_aggregate_ctrl_stats(dev, ctrl_stats);
679 		break;
680 	}
681 }
682 EXPORT_SYMBOL_GPL(ocelot_port_get_eth_ctrl_stats);
683 
684 static void ocelot_port_mac_stats_cb(struct ocelot *ocelot, int port, void *priv)
685 {
686 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
687 	struct ethtool_eth_mac_stats *mac_stats = priv;
688 
689 	mac_stats->OctetsTransmittedOK = s[OCELOT_STAT_TX_OCTETS];
690 	mac_stats->FramesTransmittedOK = s[OCELOT_STAT_TX_64] +
691 					 s[OCELOT_STAT_TX_65_127] +
692 					 s[OCELOT_STAT_TX_128_255] +
693 					 s[OCELOT_STAT_TX_256_511] +
694 					 s[OCELOT_STAT_TX_512_1023] +
695 					 s[OCELOT_STAT_TX_1024_1526] +
696 					 s[OCELOT_STAT_TX_1527_MAX];
697 	mac_stats->OctetsReceivedOK = s[OCELOT_STAT_RX_OCTETS];
698 	mac_stats->FramesReceivedOK = s[OCELOT_STAT_RX_GREEN_PRIO_0] +
699 				      s[OCELOT_STAT_RX_GREEN_PRIO_1] +
700 				      s[OCELOT_STAT_RX_GREEN_PRIO_2] +
701 				      s[OCELOT_STAT_RX_GREEN_PRIO_3] +
702 				      s[OCELOT_STAT_RX_GREEN_PRIO_4] +
703 				      s[OCELOT_STAT_RX_GREEN_PRIO_5] +
704 				      s[OCELOT_STAT_RX_GREEN_PRIO_6] +
705 				      s[OCELOT_STAT_RX_GREEN_PRIO_7] +
706 				      s[OCELOT_STAT_RX_YELLOW_PRIO_0] +
707 				      s[OCELOT_STAT_RX_YELLOW_PRIO_1] +
708 				      s[OCELOT_STAT_RX_YELLOW_PRIO_2] +
709 				      s[OCELOT_STAT_RX_YELLOW_PRIO_3] +
710 				      s[OCELOT_STAT_RX_YELLOW_PRIO_4] +
711 				      s[OCELOT_STAT_RX_YELLOW_PRIO_5] +
712 				      s[OCELOT_STAT_RX_YELLOW_PRIO_6] +
713 				      s[OCELOT_STAT_RX_YELLOW_PRIO_7];
714 	mac_stats->MulticastFramesXmittedOK = s[OCELOT_STAT_TX_MULTICAST];
715 	mac_stats->BroadcastFramesXmittedOK = s[OCELOT_STAT_TX_BROADCAST];
716 	mac_stats->MulticastFramesReceivedOK = s[OCELOT_STAT_RX_MULTICAST];
717 	mac_stats->BroadcastFramesReceivedOK = s[OCELOT_STAT_RX_BROADCAST];
718 	mac_stats->FrameTooLongErrors = s[OCELOT_STAT_RX_LONGS];
719 	/* Sadly, C_RX_CRC is the sum of FCS and alignment errors, they are not
720 	 * counted individually.
721 	 */
722 	mac_stats->FrameCheckSequenceErrors = s[OCELOT_STAT_RX_CRC_ALIGN_ERRS];
723 	mac_stats->AlignmentErrors = s[OCELOT_STAT_RX_CRC_ALIGN_ERRS];
724 }
725 
726 static void ocelot_port_pmac_mac_stats_cb(struct ocelot *ocelot, int port,
727 					  void *priv)
728 {
729 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
730 	struct ethtool_eth_mac_stats *mac_stats = priv;
731 
732 	mac_stats->OctetsTransmittedOK = s[OCELOT_STAT_TX_PMAC_OCTETS];
733 	mac_stats->FramesTransmittedOK = s[OCELOT_STAT_TX_PMAC_64] +
734 					 s[OCELOT_STAT_TX_PMAC_65_127] +
735 					 s[OCELOT_STAT_TX_PMAC_128_255] +
736 					 s[OCELOT_STAT_TX_PMAC_256_511] +
737 					 s[OCELOT_STAT_TX_PMAC_512_1023] +
738 					 s[OCELOT_STAT_TX_PMAC_1024_1526] +
739 					 s[OCELOT_STAT_TX_PMAC_1527_MAX];
740 	mac_stats->OctetsReceivedOK = s[OCELOT_STAT_RX_PMAC_OCTETS];
741 	mac_stats->FramesReceivedOK = s[OCELOT_STAT_RX_PMAC_64] +
742 				      s[OCELOT_STAT_RX_PMAC_65_127] +
743 				      s[OCELOT_STAT_RX_PMAC_128_255] +
744 				      s[OCELOT_STAT_RX_PMAC_256_511] +
745 				      s[OCELOT_STAT_RX_PMAC_512_1023] +
746 				      s[OCELOT_STAT_RX_PMAC_1024_1526] +
747 				      s[OCELOT_STAT_RX_PMAC_1527_MAX];
748 	mac_stats->MulticastFramesXmittedOK = s[OCELOT_STAT_TX_PMAC_MULTICAST];
749 	mac_stats->BroadcastFramesXmittedOK = s[OCELOT_STAT_TX_PMAC_BROADCAST];
750 	mac_stats->MulticastFramesReceivedOK = s[OCELOT_STAT_RX_PMAC_MULTICAST];
751 	mac_stats->BroadcastFramesReceivedOK = s[OCELOT_STAT_RX_PMAC_BROADCAST];
752 	mac_stats->FrameTooLongErrors = s[OCELOT_STAT_RX_PMAC_LONGS];
753 	/* Sadly, C_RX_CRC is the sum of FCS and alignment errors, they are not
754 	 * counted individually.
755 	 */
756 	mac_stats->FrameCheckSequenceErrors = s[OCELOT_STAT_RX_PMAC_CRC_ALIGN_ERRS];
757 	mac_stats->AlignmentErrors = s[OCELOT_STAT_RX_PMAC_CRC_ALIGN_ERRS];
758 }
759 
760 void ocelot_port_get_eth_mac_stats(struct ocelot *ocelot, int port,
761 				   struct ethtool_eth_mac_stats *mac_stats)
762 {
763 	struct net_device *dev;
764 
765 	switch (mac_stats->src) {
766 	case ETHTOOL_MAC_STATS_SRC_EMAC:
767 		ocelot_port_stats_run(ocelot, port, mac_stats,
768 				      ocelot_port_mac_stats_cb);
769 		break;
770 	case ETHTOOL_MAC_STATS_SRC_PMAC:
771 		if (ocelot->mm_supported)
772 			ocelot_port_stats_run(ocelot, port, mac_stats,
773 					      ocelot_port_pmac_mac_stats_cb);
774 		break;
775 	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
776 		dev = ocelot->ops->port_to_netdev(ocelot, port);
777 		ethtool_aggregate_mac_stats(dev, mac_stats);
778 		break;
779 	}
780 }
781 EXPORT_SYMBOL_GPL(ocelot_port_get_eth_mac_stats);
782 
783 static void ocelot_port_phy_stats_cb(struct ocelot *ocelot, int port, void *priv)
784 {
785 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
786 	struct ethtool_eth_phy_stats *phy_stats = priv;
787 
788 	phy_stats->SymbolErrorDuringCarrier = s[OCELOT_STAT_RX_SYM_ERRS];
789 }
790 
791 static void ocelot_port_pmac_phy_stats_cb(struct ocelot *ocelot, int port,
792 					  void *priv)
793 {
794 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
795 	struct ethtool_eth_phy_stats *phy_stats = priv;
796 
797 	phy_stats->SymbolErrorDuringCarrier = s[OCELOT_STAT_RX_PMAC_SYM_ERRS];
798 }
799 
800 void ocelot_port_get_eth_phy_stats(struct ocelot *ocelot, int port,
801 				   struct ethtool_eth_phy_stats *phy_stats)
802 {
803 	struct net_device *dev;
804 
805 	switch (phy_stats->src) {
806 	case ETHTOOL_MAC_STATS_SRC_EMAC:
807 		ocelot_port_stats_run(ocelot, port, phy_stats,
808 				      ocelot_port_phy_stats_cb);
809 		break;
810 	case ETHTOOL_MAC_STATS_SRC_PMAC:
811 		if (ocelot->mm_supported)
812 			ocelot_port_stats_run(ocelot, port, phy_stats,
813 					      ocelot_port_pmac_phy_stats_cb);
814 		break;
815 	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
816 		dev = ocelot->ops->port_to_netdev(ocelot, port);
817 		ethtool_aggregate_phy_stats(dev, phy_stats);
818 		break;
819 	}
820 }
821 EXPORT_SYMBOL_GPL(ocelot_port_get_eth_phy_stats);
822 
823 void ocelot_port_get_stats64(struct ocelot *ocelot, int port,
824 			     struct rtnl_link_stats64 *stats)
825 {
826 	u64 *s = &ocelot->stats[port * OCELOT_NUM_STATS];
827 
828 	spin_lock(&ocelot->stats_lock);
829 
830 	/* Get Rx stats */
831 	stats->rx_bytes = s[OCELOT_STAT_RX_OCTETS];
832 	stats->rx_packets = s[OCELOT_STAT_RX_SHORTS] +
833 			    s[OCELOT_STAT_RX_FRAGMENTS] +
834 			    s[OCELOT_STAT_RX_JABBERS] +
835 			    s[OCELOT_STAT_RX_LONGS] +
836 			    s[OCELOT_STAT_RX_64] +
837 			    s[OCELOT_STAT_RX_65_127] +
838 			    s[OCELOT_STAT_RX_128_255] +
839 			    s[OCELOT_STAT_RX_256_511] +
840 			    s[OCELOT_STAT_RX_512_1023] +
841 			    s[OCELOT_STAT_RX_1024_1526] +
842 			    s[OCELOT_STAT_RX_1527_MAX];
843 	stats->multicast = s[OCELOT_STAT_RX_MULTICAST];
844 	stats->rx_missed_errors = s[OCELOT_STAT_DROP_TAIL];
845 	stats->rx_dropped = s[OCELOT_STAT_RX_RED_PRIO_0] +
846 			    s[OCELOT_STAT_RX_RED_PRIO_1] +
847 			    s[OCELOT_STAT_RX_RED_PRIO_2] +
848 			    s[OCELOT_STAT_RX_RED_PRIO_3] +
849 			    s[OCELOT_STAT_RX_RED_PRIO_4] +
850 			    s[OCELOT_STAT_RX_RED_PRIO_5] +
851 			    s[OCELOT_STAT_RX_RED_PRIO_6] +
852 			    s[OCELOT_STAT_RX_RED_PRIO_7] +
853 			    s[OCELOT_STAT_DROP_LOCAL] +
854 			    s[OCELOT_STAT_DROP_YELLOW_PRIO_0] +
855 			    s[OCELOT_STAT_DROP_YELLOW_PRIO_1] +
856 			    s[OCELOT_STAT_DROP_YELLOW_PRIO_2] +
857 			    s[OCELOT_STAT_DROP_YELLOW_PRIO_3] +
858 			    s[OCELOT_STAT_DROP_YELLOW_PRIO_4] +
859 			    s[OCELOT_STAT_DROP_YELLOW_PRIO_5] +
860 			    s[OCELOT_STAT_DROP_YELLOW_PRIO_6] +
861 			    s[OCELOT_STAT_DROP_YELLOW_PRIO_7] +
862 			    s[OCELOT_STAT_DROP_GREEN_PRIO_0] +
863 			    s[OCELOT_STAT_DROP_GREEN_PRIO_1] +
864 			    s[OCELOT_STAT_DROP_GREEN_PRIO_2] +
865 			    s[OCELOT_STAT_DROP_GREEN_PRIO_3] +
866 			    s[OCELOT_STAT_DROP_GREEN_PRIO_4] +
867 			    s[OCELOT_STAT_DROP_GREEN_PRIO_5] +
868 			    s[OCELOT_STAT_DROP_GREEN_PRIO_6] +
869 			    s[OCELOT_STAT_DROP_GREEN_PRIO_7];
870 
871 	/* Get Tx stats */
872 	stats->tx_bytes = s[OCELOT_STAT_TX_OCTETS];
873 	stats->tx_packets = s[OCELOT_STAT_TX_64] +
874 			    s[OCELOT_STAT_TX_65_127] +
875 			    s[OCELOT_STAT_TX_128_255] +
876 			    s[OCELOT_STAT_TX_256_511] +
877 			    s[OCELOT_STAT_TX_512_1023] +
878 			    s[OCELOT_STAT_TX_1024_1526] +
879 			    s[OCELOT_STAT_TX_1527_MAX];
880 	stats->tx_dropped = s[OCELOT_STAT_TX_DROPS] +
881 			    s[OCELOT_STAT_TX_AGED];
882 	stats->collisions = s[OCELOT_STAT_TX_COLLISION];
883 
884 	spin_unlock(&ocelot->stats_lock);
885 }
886 EXPORT_SYMBOL(ocelot_port_get_stats64);
887 
888 static int ocelot_prepare_stats_regions(struct ocelot *ocelot)
889 {
890 	struct ocelot_stats_region *region = NULL;
891 	const struct ocelot_stat_layout *layout;
892 	enum ocelot_reg last = 0;
893 	int i;
894 
895 	INIT_LIST_HEAD(&ocelot->stats_regions);
896 
897 	layout = ocelot_get_stats_layout(ocelot);
898 
899 	for (i = 0; i < OCELOT_NUM_STATS; i++) {
900 		if (!layout[i].reg)
901 			continue;
902 
903 		if (region && ocelot->map[SYS][layout[i].reg & REG_MASK] ==
904 		    ocelot->map[SYS][last & REG_MASK] + 4) {
905 			region->count++;
906 		} else {
907 			region = devm_kzalloc(ocelot->dev, sizeof(*region),
908 					      GFP_KERNEL);
909 			if (!region)
910 				return -ENOMEM;
911 
912 			/* enum ocelot_stat must be kept sorted in the same
913 			 * order as layout[i].reg in order to have efficient
914 			 * bulking
915 			 */
916 			WARN_ON(last >= layout[i].reg);
917 
918 			region->base = layout[i].reg;
919 			region->first_stat = i;
920 			region->count = 1;
921 			list_add_tail(&region->node, &ocelot->stats_regions);
922 		}
923 
924 		last = layout[i].reg;
925 	}
926 
927 	list_for_each_entry(region, &ocelot->stats_regions, node) {
928 		enum ocelot_target target;
929 		u32 addr;
930 
931 		ocelot_reg_to_target_addr(ocelot, region->base, &target,
932 					  &addr);
933 
934 		dev_dbg(ocelot->dev,
935 			"region of %d contiguous counters starting with SYS:STAT:CNT[0x%03x]\n",
936 			region->count, addr / 4);
937 		region->buf = devm_kcalloc(ocelot->dev, region->count,
938 					   sizeof(*region->buf), GFP_KERNEL);
939 		if (!region->buf)
940 			return -ENOMEM;
941 	}
942 
943 	return 0;
944 }
945 
946 int ocelot_stats_init(struct ocelot *ocelot)
947 {
948 	char queue_name[32];
949 	int ret;
950 
951 	ocelot->stats = devm_kcalloc(ocelot->dev,
952 				     ocelot->num_phys_ports * OCELOT_NUM_STATS,
953 				     sizeof(u64), GFP_KERNEL);
954 	if (!ocelot->stats)
955 		return -ENOMEM;
956 
957 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
958 		 dev_name(ocelot->dev));
959 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
960 	if (!ocelot->stats_queue)
961 		return -ENOMEM;
962 
963 	spin_lock_init(&ocelot->stats_lock);
964 	mutex_init(&ocelot->stat_view_lock);
965 
966 	ret = ocelot_prepare_stats_regions(ocelot);
967 	if (ret) {
968 		destroy_workqueue(ocelot->stats_queue);
969 		return ret;
970 	}
971 
972 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
973 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
974 			   OCELOT_STATS_CHECK_DELAY);
975 
976 	return 0;
977 }
978 
979 void ocelot_stats_deinit(struct ocelot *ocelot)
980 {
981 	cancel_delayed_work(&ocelot->stats_work);
982 	destroy_workqueue(ocelot->stats_queue);
983 }
984