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
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.
12 Signed-off-by: Felix Fietkau <nbd@nbd.name>
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
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);
29 dev_err(ppe->dev, "MIB table busy");
30 @@ -89,17 +89,31 @@ static int mtk_ppe_mib_wait_busy(struct
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)
38 + if (!ppe->acct_table)
41 + return ppe->acct_table + index * sizeof(struct mtk_foe_accounting);
44 +struct mtk_foe_accounting *mtk_ppe_mib_entry_read(struct mtk_ppe *ppe, u16 index)
46 u32 val, cnt_r0, cnt_r1, cnt_r2;
47 + struct mtk_foe_accounting *acct;
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);
53 + acct = mtk_ppe_acct_data(ppe, index);
57 ret = mtk_ppe_mib_wait_busy(ppe);
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;
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;
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);
92 mtk_ppe_cache_clear(ppe);
93 - if (ppe->accounting) {
94 - struct mtk_foe_accounting *acct;
96 - acct = ppe->acct_table + entry->hash * sizeof(*acct);
101 entry->hash = 0xffff;
103 @@ -556,11 +563,14 @@ static int __mtk_foe_entry_idle_time(str
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)
111 + struct mtk_foe_accounting *acct;
112 struct mtk_foe_entry foe = {};
113 struct mtk_foe_entry *hwe;
114 u16 hash = entry->hash;
119 @@ -571,18 +581,35 @@ mtk_flow_entry_update(struct mtk_ppe *pp
120 memcpy(&foe, hwe, len);
122 if (!mtk_flow_entry_match(ppe->eth, entry, &foe, len) ||
123 - FIELD_GET(MTK_FOE_IB1_STATE, foe.ib1) != MTK_FOE_STATE_BIND)
125 + FIELD_GET(MTK_FOE_IB1_STATE, foe.ib1) != MTK_FOE_STATE_BIND) {
126 + acct = mtk_ppe_acct_data(ppe, hash);
128 + entry->prev_packets += acct->packets;
129 + entry->prev_bytes += acct->bytes;
135 entry->data.ib1 = foe.ib1;
136 + acct = mtk_ppe_mib_entry_read(ppe, hash);
141 + *packets += acct->packets;
142 + *bytes += acct->bytes;
150 mtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
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;
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) {
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);
169 @@ -606,10 +635,29 @@ mtk_flow_entry_update_l2(struct mtk_ppe
173 +void mtk_foe_entry_get_stats(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
176 + entry->packets = entry->prev_packets;
177 + entry->bytes = entry->prev_bytes;
179 + spin_lock_bh(&ppe_lock);
181 + if (entry->type == MTK_FLOW_TYPE_L2)
182 + mtk_flow_entry_update_l2(ppe, entry);
184 + mtk_flow_entry_update(ppe, entry, &entry->packets, &entry->bytes);
186 + *idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
188 + spin_unlock_bh(&ppe_lock);
192 __mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
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
203 + acct = mtk_ppe_mib_entry_read(ppe, hash);
209 mtk_ppe_cache_clear(ppe);
212 @@ -802,21 +856,6 @@ out:
213 spin_unlock_bh(&ppe_lock);
216 -int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
220 - spin_lock_bh(&ppe_lock);
221 - if (entry->type == MTK_FLOW_TYPE_L2)
222 - mtk_flow_entry_update_l2(ppe, entry);
224 - mtk_flow_entry_update(ppe, entry);
225 - idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
226 - spin_unlock_bh(&ppe_lock);
231 int mtk_ppe_prepare_reset(struct mtk_ppe *ppe)
234 @@ -844,32 +883,6 @@ int mtk_ppe_prepare_reset(struct mtk_ppe
235 return mtk_ppe_wait_busy(ppe);
238 -struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
239 - struct mtk_foe_accounting *diff)
241 - struct mtk_foe_accounting *acct;
242 - int size = sizeof(struct mtk_foe_accounting);
243 - u64 bytes, packets;
245 - if (!ppe->accounting)
248 - if (mtk_mib_entry_read(ppe, index, &bytes, &packets))
251 - acct = ppe->acct_table + index * size;
253 - acct->bytes += bytes;
254 - acct->packets += packets;
257 - diff->bytes = bytes;
258 - diff->packets = packets;
264 struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int index)
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;
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);
284 void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash);
286 @@ -395,9 +398,8 @@ int mtk_foe_entry_set_queue(struct mtk_e
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,
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)
304 - acct = mtk_foe_entry_get_mib(ppe, i, NULL);
305 + acct = mtk_ppe_mib_entry_read(ppe, i);
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)
314 struct mtk_flow_entry *entry;
315 - struct mtk_foe_accounting diff;
317 + u64 packets, bytes;
320 entry = rhashtable_lookup(ð->flow_table, &f->cookie,
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;
333 - if (entry->hash != 0xFFFF &&
334 - mtk_foe_entry_get_mib(eth->ppe[entry->ppe_index], entry->hash,
336 - f->stats.pkts += diff.packets;
337 - f->stats.bytes += diff.bytes;