2ea6d341b0647331a5ea5492d2d1ca1c44344e4d
[openwrt/staging/stintel.git] /
1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Thu, 23 Mar 2023 11:05:22 +0100
3 Subject: [PATCH] net: ethernet: mediatek: fix ppe flow accounting for L2
4 flows
5
6 For L2 flows, the packet/byte counters should report the sum of the
7 counters of their subflows, both current and expired.
8 In order to make this work, change the way that accounting data is tracked.
9 Reset counters when a flow enters bind. Once it expires (or enters unbind),
10 store the last counter value in struct mtk_flow_entry.
11
12 Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 ---
14
15 --- a/drivers/net/ethernet/mediatek/mtk_ppe.c
16 +++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
17 @@ -79,9 +79,9 @@ static int mtk_ppe_mib_wait_busy(struct
18 int ret;
19 u32 val;
20
21 - ret = readl_poll_timeout(ppe->base + MTK_PPE_MIB_SER_CR, val,
22 - !(val & MTK_PPE_MIB_SER_CR_ST),
23 - 20, MTK_PPE_WAIT_TIMEOUT_US);
24 + ret = readl_poll_timeout_atomic(ppe->base + MTK_PPE_MIB_SER_CR, val,
25 + !(val & MTK_PPE_MIB_SER_CR_ST),
26 + 20, MTK_PPE_WAIT_TIMEOUT_US);
27
28 if (ret)
29 dev_err(ppe->dev, "MIB table busy");
30 @@ -89,17 +89,31 @@ static int mtk_ppe_mib_wait_busy(struct
31 return ret;
32 }
33
34 -static int mtk_mib_entry_read(struct mtk_ppe *ppe, u16 index, u64 *bytes, u64 *packets)
35 +static inline struct mtk_foe_accounting *
36 +mtk_ppe_acct_data(struct mtk_ppe *ppe, u16 index)
37 +{
38 + if (!ppe->acct_table)
39 + return NULL;
40 +
41 + return ppe->acct_table + index * sizeof(struct mtk_foe_accounting);
42 +}
43 +
44 +struct mtk_foe_accounting *mtk_ppe_mib_entry_read(struct mtk_ppe *ppe, u16 index)
45 {
46 u32 val, cnt_r0, cnt_r1, cnt_r2;
47 + struct mtk_foe_accounting *acct;
48 int ret;
49
50 val = FIELD_PREP(MTK_PPE_MIB_SER_CR_ADDR, index) | MTK_PPE_MIB_SER_CR_ST;
51 ppe_w32(ppe, MTK_PPE_MIB_SER_CR, val);
52
53 + acct = mtk_ppe_acct_data(ppe, index);
54 + if (!acct)
55 + return NULL;
56 +
57 ret = mtk_ppe_mib_wait_busy(ppe);
58 if (ret)
59 - return ret;
60 + return acct;
61
62 cnt_r0 = readl(ppe->base + MTK_PPE_MIB_SER_R0);
63 cnt_r1 = readl(ppe->base + MTK_PPE_MIB_SER_R1);
64 @@ -108,19 +122,19 @@ static int mtk_mib_entry_read(struct mtk
65 if (mtk_is_netsys_v3_or_greater(ppe->eth)) {
66 /* 64 bit for each counter */
67 u32 cnt_r3 = readl(ppe->base + MTK_PPE_MIB_SER_R3);
68 - *bytes = ((u64)cnt_r1 << 32) | cnt_r0;
69 - *packets = ((u64)cnt_r3 << 32) | cnt_r2;
70 + acct->bytes += ((u64)cnt_r1 << 32) | cnt_r0;
71 + acct->packets += ((u64)cnt_r3 << 32) | cnt_r2;
72 } else {
73 /* 48 bit byte counter, 40 bit packet counter */
74 u32 byte_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R0_BYTE_CNT_LOW, cnt_r0);
75 u32 byte_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R1_BYTE_CNT_HIGH, cnt_r1);
76 u32 pkt_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R1_PKT_CNT_LOW, cnt_r1);
77 u32 pkt_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R2_PKT_CNT_HIGH, cnt_r2);
78 - *bytes = ((u64)byte_cnt_high << 32) | byte_cnt_low;
79 - *packets = (pkt_cnt_high << 16) | pkt_cnt_low;
80 + acct->bytes += ((u64)byte_cnt_high << 32) | byte_cnt_low;
81 + acct->packets += (pkt_cnt_high << 16) | pkt_cnt_low;
82 }
83
84 - return 0;
85 + return acct;
86 }
87
88 static void mtk_ppe_cache_clear(struct mtk_ppe *ppe)
89 @@ -525,13 +539,6 @@ __mtk_foe_entry_clear(struct mtk_ppe *pp
90 hwe->ib1 |= FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_INVALID);
91 dma_wmb();
92 mtk_ppe_cache_clear(ppe);
93 - if (ppe->accounting) {
94 - struct mtk_foe_accounting *acct;
95 -
96 - acct = ppe->acct_table + entry->hash * sizeof(*acct);
97 - acct->packets = 0;
98 - acct->bytes = 0;
99 - }
100 }
101 entry->hash = 0xffff;
102
103 @@ -556,11 +563,14 @@ static int __mtk_foe_entry_idle_time(str
104 }
105
106 static bool
107 -mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
108 +mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
109 + u64 *packets, u64 *bytes)
110 {
111 + struct mtk_foe_accounting *acct;
112 struct mtk_foe_entry foe = {};
113 struct mtk_foe_entry *hwe;
114 u16 hash = entry->hash;
115 + bool ret = false;
116 int len;
117
118 if (hash == 0xffff)
119 @@ -571,18 +581,35 @@ mtk_flow_entry_update(struct mtk_ppe *pp
120 memcpy(&foe, hwe, len);
121
122 if (!mtk_flow_entry_match(ppe->eth, entry, &foe, len) ||
123 - FIELD_GET(MTK_FOE_IB1_STATE, foe.ib1) != MTK_FOE_STATE_BIND)
124 - return false;
125 + FIELD_GET(MTK_FOE_IB1_STATE, foe.ib1) != MTK_FOE_STATE_BIND) {
126 + acct = mtk_ppe_acct_data(ppe, hash);
127 + if (acct) {
128 + entry->prev_packets += acct->packets;
129 + entry->prev_bytes += acct->bytes;
130 + }
131 +
132 + goto out;
133 + }
134
135 entry->data.ib1 = foe.ib1;
136 + acct = mtk_ppe_mib_entry_read(ppe, hash);
137 + ret = true;
138 +
139 +out:
140 + if (acct) {
141 + *packets += acct->packets;
142 + *bytes += acct->bytes;
143 + }
144
145 - return true;
146 + return ret;
147 }
148
149 static void
150 mtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
151 {
152 u32 ib1_ts_mask = mtk_get_ib1_ts_mask(ppe->eth);
153 + u64 *packets = &entry->packets;
154 + u64 *bytes = &entry->bytes;
155 struct mtk_flow_entry *cur;
156 struct hlist_node *tmp;
157 int idle;
158 @@ -591,7 +618,9 @@ mtk_flow_entry_update_l2(struct mtk_ppe
159 hlist_for_each_entry_safe(cur, tmp, &entry->l2_flows, l2_list) {
160 int cur_idle;
161
162 - if (!mtk_flow_entry_update(ppe, cur)) {
163 + if (!mtk_flow_entry_update(ppe, cur, packets, bytes)) {
164 + entry->prev_packets += cur->prev_packets;
165 + entry->prev_bytes += cur->prev_bytes;
166 __mtk_foe_entry_clear(ppe, entry, false);
167 continue;
168 }
169 @@ -606,10 +635,29 @@ mtk_flow_entry_update_l2(struct mtk_ppe
170 }
171 }
172
173 +void mtk_foe_entry_get_stats(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
174 + int *idle)
175 +{
176 + entry->packets = entry->prev_packets;
177 + entry->bytes = entry->prev_bytes;
178 +
179 + spin_lock_bh(&ppe_lock);
180 +
181 + if (entry->type == MTK_FLOW_TYPE_L2)
182 + mtk_flow_entry_update_l2(ppe, entry);
183 + else
184 + mtk_flow_entry_update(ppe, entry, &entry->packets, &entry->bytes);
185 +
186 + *idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
187 +
188 + spin_unlock_bh(&ppe_lock);
189 +}
190 +
191 static void
192 __mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
193 u16 hash)
194 {
195 + struct mtk_foe_accounting *acct;
196 struct mtk_eth *eth = ppe->eth;
197 u16 timestamp = mtk_eth_timestamp(eth);
198 struct mtk_foe_entry *hwe;
199 @@ -640,6 +688,12 @@ __mtk_foe_entry_commit(struct mtk_ppe *p
200
201 dma_wmb();
202
203 + acct = mtk_ppe_mib_entry_read(ppe, hash);
204 + if (acct) {
205 + acct->packets = 0;
206 + acct->bytes = 0;
207 + }
208 +
209 mtk_ppe_cache_clear(ppe);
210 }
211
212 @@ -802,21 +856,6 @@ out:
213 spin_unlock_bh(&ppe_lock);
214 }
215
216 -int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
217 -{
218 - int idle;
219 -
220 - spin_lock_bh(&ppe_lock);
221 - if (entry->type == MTK_FLOW_TYPE_L2)
222 - mtk_flow_entry_update_l2(ppe, entry);
223 - else
224 - mtk_flow_entry_update(ppe, entry);
225 - idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
226 - spin_unlock_bh(&ppe_lock);
227 -
228 - return idle;
229 -}
230 -
231 int mtk_ppe_prepare_reset(struct mtk_ppe *ppe)
232 {
233 if (!ppe)
234 @@ -844,32 +883,6 @@ int mtk_ppe_prepare_reset(struct mtk_ppe
235 return mtk_ppe_wait_busy(ppe);
236 }
237
238 -struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
239 - struct mtk_foe_accounting *diff)
240 -{
241 - struct mtk_foe_accounting *acct;
242 - int size = sizeof(struct mtk_foe_accounting);
243 - u64 bytes, packets;
244 -
245 - if (!ppe->accounting)
246 - return NULL;
247 -
248 - if (mtk_mib_entry_read(ppe, index, &bytes, &packets))
249 - return NULL;
250 -
251 - acct = ppe->acct_table + index * size;
252 -
253 - acct->bytes += bytes;
254 - acct->packets += packets;
255 -
256 - if (diff) {
257 - diff->bytes = bytes;
258 - diff->packets = packets;
259 - }
260 -
261 - return acct;
262 -}
263 -
264 struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int index)
265 {
266 bool accounting = eth->soc->has_accounting;
267 --- a/drivers/net/ethernet/mediatek/mtk_ppe.h
268 +++ b/drivers/net/ethernet/mediatek/mtk_ppe.h
269 @@ -304,6 +304,8 @@ struct mtk_flow_entry {
270 struct mtk_foe_entry data;
271 struct rhash_head node;
272 unsigned long cookie;
273 + u64 prev_packets, prev_bytes;
274 + u64 packets, bytes;
275 };
276
277 struct mtk_mib_entry {
278 @@ -347,6 +349,7 @@ void mtk_ppe_deinit(struct mtk_eth *eth)
279 void mtk_ppe_start(struct mtk_ppe *ppe);
280 int mtk_ppe_stop(struct mtk_ppe *ppe);
281 int mtk_ppe_prepare_reset(struct mtk_ppe *ppe);
282 +struct mtk_foe_accounting *mtk_ppe_mib_entry_read(struct mtk_ppe *ppe, u16 index);
283
284 void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash);
285
286 @@ -395,9 +398,8 @@ int mtk_foe_entry_set_queue(struct mtk_e
287 unsigned int queue);
288 int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
289 void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
290 -int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
291 int mtk_ppe_debugfs_init(struct mtk_ppe *ppe, int index);
292 -struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
293 - struct mtk_foe_accounting *diff);
294 +void mtk_foe_entry_get_stats(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
295 + int *idle);
296
297 #endif
298 --- a/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
299 +++ b/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
300 @@ -96,7 +96,7 @@ mtk_ppe_debugfs_foe_show(struct seq_file
301 if (bind && state != MTK_FOE_STATE_BIND)
302 continue;
303
304 - acct = mtk_foe_entry_get_mib(ppe, i, NULL);
305 + acct = mtk_ppe_mib_entry_read(ppe, i);
306
307 type = mtk_get_ib1_pkt_type(ppe->eth, entry->ib1);
308 seq_printf(m, "%05x %s %7s", i,
309 --- a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
310 +++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
311 @@ -501,24 +501,21 @@ static int
312 mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
313 {
314 struct mtk_flow_entry *entry;
315 - struct mtk_foe_accounting diff;
316 - u32 idle;
317 + u64 packets, bytes;
318 + int idle;
319
320 entry = rhashtable_lookup(&eth->flow_table, &f->cookie,
321 mtk_flow_ht_params);
322 if (!entry)
323 return -ENOENT;
324
325 - idle = mtk_foe_entry_idle_time(eth->ppe[entry->ppe_index], entry);
326 + packets = entry->packets;
327 + bytes = entry->bytes;
328 + mtk_foe_entry_get_stats(eth->ppe[entry->ppe_index], entry, &idle);
329 + f->stats.pkts += entry->packets - packets;
330 + f->stats.bytes += entry->bytes - bytes;
331 f->stats.lastused = jiffies - idle * HZ;
332
333 - if (entry->hash != 0xFFFF &&
334 - mtk_foe_entry_get_mib(eth->ppe[entry->ppe_index], entry->hash,
335 - &diff)) {
336 - f->stats.pkts += diff.packets;
337 - f->stats.bytes += diff.bytes;
338 - }
339 -
340 return 0;
341 }
342