blob: f6243d0b28cd0eea69e8c4a1cf996d59df7dbf08 [file] [log] [blame]
developerbc6a6a32022-07-19 11:03:07 +08001diff --git a/drivers/net/ethernet/mediatek/mtk_eth_soc.c b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
2index 4f53794..dc5d050 100644
3--- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
4+++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
5@@ -3944,7 +3944,8 @@ static int mtk_probe(struct platform_device *pdev)
6 for (i = 0; i < eth->ppe_num; i++) {
7 eth->ppe[i] = mtk_ppe_init(eth,
8 eth->base + MTK_ETH_PPE_BASE + i * 0x400,
9- 2, eth->soc->hash_way, i);
10+ 2, eth->soc->hash_way, i,
11+ eth->soc->has_accounting);
12 if (!eth->ppe[i]) {
13 err = -ENOMEM;
14 goto err_free_dev;
15@@ -4057,6 +4058,7 @@ static const struct mtk_soc_data mt2701_data = {
16 .required_clks = MT7623_CLKS_BITMAP,
17 .required_pctl = true,
18 .has_sram = false,
19+ .has_accounting = false,
20 .hash_way = 2,
21 .offload_version = 2,
22 .txrx = {
23@@ -4073,6 +4075,7 @@ static const struct mtk_soc_data mt7621_data = {
24 .required_clks = MT7621_CLKS_BITMAP,
25 .required_pctl = false,
26 .has_sram = false,
27+ .has_accounting = false,
28 .hash_way = 2,
29 .offload_version = 2,
30 .txrx = {
31@@ -4090,6 +4093,7 @@ static const struct mtk_soc_data mt7622_data = {
32 .required_clks = MT7622_CLKS_BITMAP,
33 .required_pctl = false,
34 .has_sram = false,
35+ .has_accounting = true,
36 .hash_way = 2,
37 .offload_version = 2,
38 .txrx = {
39@@ -4106,6 +4110,7 @@ static const struct mtk_soc_data mt7623_data = {
40 .required_clks = MT7623_CLKS_BITMAP,
41 .required_pctl = true,
42 .has_sram = false,
43+ .has_accounting = false,
44 .hash_way = 2,
45 .offload_version = 2,
46 .txrx = {
47@@ -4123,6 +4128,7 @@ static const struct mtk_soc_data mt7629_data = {
48 .required_clks = MT7629_CLKS_BITMAP,
49 .required_pctl = false,
50 .has_sram = false,
51+ .has_accounting = true,
52 .txrx = {
53 .txd_size = sizeof(struct mtk_tx_dma),
54 .rxd_size = sizeof(struct mtk_rx_dma),
55@@ -4138,6 +4144,7 @@ static const struct mtk_soc_data mt7986_data = {
56 .required_clks = MT7986_CLKS_BITMAP,
57 .required_pctl = false,
58 .has_sram = true,
59+ .has_accounting = true,
60 .hash_way = 4,
61 .offload_version = 2,
62 .txrx = {
63@@ -4155,6 +4162,7 @@ static const struct mtk_soc_data mt7981_data = {
64 .required_clks = MT7981_CLKS_BITMAP,
65 .required_pctl = false,
66 .has_sram = true,
67+ .has_accounting = true,
68 .hash_way = 4,
69 .offload_version = 2,
70 .txrx = {
71@@ -4171,6 +4179,7 @@ static const struct mtk_soc_data rt5350_data = {
72 .required_clks = MT7628_CLKS_BITMAP,
73 .required_pctl = false,
74 .has_sram = false,
75+ .has_accounting = false,
76 .txrx = {
77 .txd_size = sizeof(struct mtk_tx_dma),
78 .rxd_size = sizeof(struct mtk_rx_dma),
79diff --git a/drivers/net/ethernet/mediatek/mtk_eth_soc.h b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
80index f659633..5e16fa8 100644
81--- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
82+++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
83@@ -1213,6 +1213,7 @@ struct mtk_soc_data {
84 u8 offload_version;
85 netdev_features_t hw_features;
86 bool has_sram;
87+ bool has_accounting;
88 struct {
89 u32 txd_size;
90 u32 rxd_size;
91diff --git a/drivers/net/ethernet/mediatek/mtk_ppe.c b/drivers/net/ethernet/mediatek/mtk_ppe.c
92index 918aa22..8c036cd 100755
93--- a/drivers/net/ethernet/mediatek/mtk_ppe.c
94+++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
95@@ -74,6 +74,46 @@ static int mtk_ppe_wait_busy(struct mtk_ppe *ppe)
96 return ret;
97 }
98
99+static int mtk_ppe_mib_wait_busy(struct mtk_ppe *ppe)
100+{
101+ int ret;
102+ u32 val;
103+
104+ ret = readl_poll_timeout(ppe->base + MTK_PPE_MIB_SER_CR, val,
105+ !(val & MTK_PPE_MIB_SER_CR_ST),
106+ 20, MTK_PPE_WAIT_TIMEOUT_US);
107+
108+ if (ret)
109+ dev_err(ppe->dev, "MIB table busy");
110+
111+ return ret;
112+}
113+
114+int mtk_mib_entry_read(struct mtk_ppe *ppe, u16 index, u64 *bytes, u64 *packets)
115+{
116+ u32 val, cnt_r0, cnt_r1, cnt_r2;
117+ u32 byte_cnt_low, byte_cnt_high, pkt_cnt_low, pkt_cnt_high;
118+
119+ val = FIELD_PREP(MTK_PPE_MIB_SER_CR_ADDR, index) | MTK_PPE_MIB_SER_CR_ST;
120+ ppe_w32(ppe, MTK_PPE_MIB_SER_CR, val);
121+
122+ if (mtk_ppe_mib_wait_busy(ppe))
123+ return -ETIMEDOUT;
124+
125+ cnt_r0 = readl(ppe->base + MTK_PPE_MIB_SER_R0);
126+ cnt_r1 = readl(ppe->base + MTK_PPE_MIB_SER_R1);
127+ cnt_r2 = readl(ppe->base + MTK_PPE_MIB_SER_R2);
128+
129+ byte_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R0_BYTE_CNT_LOW, cnt_r0);
130+ byte_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R1_BYTE_CNT_HIGH, cnt_r1);
131+ pkt_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R1_PKT_CNT_LOW, cnt_r1);
132+ pkt_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R2_PKT_CNT_HIGH, cnt_r2);
133+ *bytes = ((u64)byte_cnt_high << 32) | byte_cnt_low;
134+ *packets = (pkt_cnt_high << 16) | pkt_cnt_low;
135+
136+ return 0;
137+}
138+
139 static void mtk_ppe_cache_clear(struct mtk_ppe *ppe)
140 {
141 ppe_set(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_CLEAR);
developerae582382023-02-03 09:08:52 +0800142@@ -412,6 +452,18 @@ __mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
developerbc6a6a32022-07-19 11:03:07 +0800143 MTK_FOE_STATE_INVALID);
144 dma_wmb();
145+
146+ if (ppe->accounting) {
developerae582382023-02-03 09:08:52 +0800147+ struct mtk_foe_accounting *acct, *acct_updated;
developerbc6a6a32022-07-19 11:03:07 +0800148+
149+ acct = ppe->acct_table + entry->hash * sizeof(*acct);
150+ acct->packets = 0;
151+ acct->bytes = 0;
developerae582382023-02-03 09:08:52 +0800152+
153+ acct_updated = ppe->acct_updated_table + entry->hash * sizeof(*acct_updated);
154+ acct_updated->packets = 0;
155+ acct_updated->bytes = 0;
developerbc6a6a32022-07-19 11:03:07 +0800156+ }
157 }
158 entry->hash = 0xffff;
159
160 if (entry->type != MTK_FLOW_TYPE_L2_SUBFLOW)
161@@ -513,6 +560,16 @@ __mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
162 wmb();
163 hwe->ib1 = entry->ib1;
164
165+ if (ppe->accounting) {
166+ int type;
167+
168+ type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
169+ if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
170+ hwe->ipv6.ib2 |= MTK_FOE_IB2_MIB_CNT;
171+ else
172+ hwe->ipv4.ib2 |= MTK_FOE_IB2_MIB_CNT;
173+ }
174+
175 dma_wmb();
176
177 mtk_ppe_cache_clear(ppe);
178@@ -618,8 +675,6 @@ void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
179 }
180
181 if (found || !mtk_flow_entry_match(entry, hwe)) {
182- if (entry->hash != 0xffff)
183- entry->hash = 0xffff;
184 continue;
185 }
186
developerae582382023-02-03 09:08:52 +0800187@@ -676,12 +731,44 @@ int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
developerbc6a6a32022-07-19 11:03:07 +0800188 return __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
189 }
190
191-struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int version, int way, int id)
192+struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index, struct mtk_foe_accounting *diff)
193+{
developerae582382023-02-03 09:08:52 +0800194+ struct mtk_foe_accounting *acct, *acct_updated;
developerbc6a6a32022-07-19 11:03:07 +0800195+ int size = sizeof(struct mtk_foe_accounting);
196+ u64 bytes, packets;
197+
198+ if (!ppe->accounting)
199+ return NULL;
200+
201+ if (mtk_mib_entry_read(ppe, index, &bytes, &packets))
202+ return NULL;
203+
204+ acct = ppe->acct_table + index * size;
205+
206+ acct->bytes += bytes;
207+ acct->packets += packets;
208+
209+ if (diff) {
developerae582382023-02-03 09:08:52 +0800210+ acct_updated = ppe->acct_updated_table + index * size;
211+
212+ diff->bytes = acct->bytes - acct_updated->bytes;
213+ diff->packets = acct->packets - acct_updated->packets;
214+ acct_updated->bytes += diff->bytes;
215+ acct_updated->packets += diff->packets;
developerbc6a6a32022-07-19 11:03:07 +0800216+ }
217+
218+ return acct;
219+}
220+
221+struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int version, int way, int id,
222+ int accounting)
223 {
224 struct device *dev = eth->dev;
225 struct mtk_foe_entry *foe;
226+ struct mtk_mib_entry *mib;
227 struct mtk_ppe *ppe;
228 struct hlist_head *flow;
developerae582382023-02-03 09:08:52 +0800229+ struct mtk_foe_accounting *acct, *acct_updated;
developerbc6a6a32022-07-19 11:03:07 +0800230
231 ppe = devm_kzalloc(dev, sizeof(*ppe), GFP_KERNEL);
232 if (!ppe)
233@@ -698,6 +781,7 @@ struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int versio
234 ppe->version = version;
235 ppe->way = way;
236 ppe->id = id;
237+ ppe->accounting = accounting;
238
239 foe = dmam_alloc_coherent(ppe->dev, MTK_PPE_ENTRIES * sizeof(*foe),
240 &ppe->foe_phys, GFP_KERNEL);
developerae582382023-02-03 09:08:52 +0800241@@ -713,6 +797,31 @@ struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int versio
developerbc6a6a32022-07-19 11:03:07 +0800242
243 ppe->foe_flow = flow;
244
245+ if (accounting) {
246+ mib = dmam_alloc_coherent(ppe->dev, MTK_PPE_ENTRIES * sizeof(*mib),
247+ &ppe->mib_phys, GFP_KERNEL);
248+ if (!foe)
249+ return NULL;
250+
251+ memset(mib, 0, MTK_PPE_ENTRIES * sizeof(*mib));
252+
253+ ppe->mib_table = mib;
254+
255+ acct = devm_kzalloc(dev, MTK_PPE_ENTRIES * sizeof(*acct),
256+ GFP_KERNEL);
257+ if (!acct)
258+ return NULL;
259+
260+ ppe->acct_table = acct;
developerae582382023-02-03 09:08:52 +0800261+
262+ acct_updated = devm_kzalloc(dev, MTK_PPE_ENTRIES * sizeof(*acct_updated),
263+ GFP_KERNEL);
264+ if (!acct_updated)
265+ return NULL;
266+
267+ ppe->acct_updated_table = acct_updated;
developerbc6a6a32022-07-19 11:03:07 +0800268+ }
269+
270 return ppe;
271 }
272
273@@ -811,6 +949,13 @@ int mtk_ppe_start(struct mtk_ppe *ppe)
274 ppe_w32(ppe, MTK_PPE_DEFAULT_CPU_PORT1, 0xcb777);
275 ppe_w32(ppe, MTK_PPE_SBW_CTRL, 0x7f);
276
277+ if (ppe->accounting && ppe->mib_phys) {
278+ ppe_w32(ppe, MTK_PPE_MIB_TB_BASE, ppe->mib_phys);
279+ ppe_m32(ppe, MTK_PPE_MIB_CFG, MTK_PPE_MIB_CFG_EN, MTK_PPE_MIB_CFG_EN);
280+ ppe_m32(ppe, MTK_PPE_MIB_CFG, MTK_PPE_MIB_CFG_RD_CLR, MTK_PPE_MIB_CFG_RD_CLR);
281+ ppe_m32(ppe, MTK_PPE_MIB_CACHE_CTL, MTK_PPE_MIB_CACHE_CTL_EN, MTK_PPE_MIB_CFG_RD_CLR);
282+ }
283+
284 return 0;
285 }
286
287diff --git a/drivers/net/ethernet/mediatek/mtk_ppe.h b/drivers/net/ethernet/mediatek/mtk_ppe.h
288index 3d6928c..8076e5d 100644
289--- a/drivers/net/ethernet/mediatek/mtk_ppe.h
290+++ b/drivers/net/ethernet/mediatek/mtk_ppe.h
291@@ -270,6 +270,20 @@ struct mtk_flow_entry {
292 unsigned long cookie;
293 };
294
295+struct mtk_mib_entry {
296+ u32 byt_cnt_l;
297+ u16 byt_cnt_h;
298+ u32 pkt_cnt_l;
299+ u8 pkt_cnt_h;
300+ u8 _rsv0;
301+ u32 _rsv1;
302+} __packed;
303+
304+struct mtk_foe_accounting {
305+ u64 bytes;
306+ u64 packets;
307+};
308+
309 struct mtk_ppe {
310 struct mtk_eth *eth;
311 struct device *dev;
312@@ -277,10 +291,14 @@ struct mtk_ppe {
313 int version;
314 int id;
315 int way;
316+ int accounting;
317
318 struct mtk_foe_entry *foe_table;
319 dma_addr_t foe_phys;
320
321+ struct mtk_mib_entry *mib_table;
322+ dma_addr_t mib_phys;
323+
324 u16 foe_check_time[MTK_PPE_ENTRIES];
325 struct hlist_head *foe_flow;
326
developerae582382023-02-03 09:08:52 +0800327@@ -289,7 +307,9 @@ struct mtk_ppe {
developerbc6a6a32022-07-19 11:03:07 +0800328 void *acct_table;
developerae582382023-02-03 09:08:52 +0800329+ void *acct_updated_table;
developerbc6a6a32022-07-19 11:03:07 +0800330 };
331
332-struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int version, int way, int id);
333+struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int version, int way, int id,
334+ int accounting);
335 int mtk_ppe_start(struct mtk_ppe *ppe);
336 int mtk_ppe_stop(struct mtk_ppe *ppe);
337
338@@ -340,5 +359,6 @@ int mtk_foe_entry_set_wdma(struct mtk_foe_entry *entry, int wdma_idx, int txq,
339 int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
340 void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
341 int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
342+struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index, struct mtk_foe_accounting *diff);
343
344 #endif
345diff --git a/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c b/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
346index f4ebe59..d713e2e 100644
347--- a/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
348+++ b/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
349@@ -81,6 +81,7 @@ mtk_ppe_debugfs_foe_show(struct seq_file *m, struct mtk_ppe *ppe, bool bind)
350 struct mtk_foe_entry *entry = &ppe->foe_table[i];
351 struct mtk_foe_mac_info *l2;
352 struct mtk_flow_addr_info ai = {};
353+ struct mtk_foe_accounting *acct;
354 unsigned char h_source[ETH_ALEN];
355 unsigned char h_dest[ETH_ALEN];
356 int type, state;
357@@ -94,6 +95,8 @@ mtk_ppe_debugfs_foe_show(struct seq_file *m, struct mtk_ppe *ppe, bool bind)
358 if (bind && state != MTK_FOE_STATE_BIND)
359 continue;
360
361+ acct = mtk_foe_entry_get_mib(ppe, i, NULL);
362+
363 type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
364 seq_printf(m, "%05x %s %7s", i,
365 mtk_foe_entry_state_str(state),
366@@ -154,9 +157,12 @@ mtk_ppe_debugfs_foe_show(struct seq_file *m, struct mtk_ppe *ppe, bool bind)
367 *((__be16 *)&h_dest[4]) = htons(l2->dest_mac_lo);
368
369 seq_printf(m, " eth=%pM->%pM etype=%04x"
370- " vlan=%d,%d ib1=%08x ib2=%08x\n",
371+ " vlan=%d,%d ib1=%08x ib2=%08x"
372+ " packets=%lld bytes=%lld\n",
373 h_source, h_dest, ntohs(l2->etype),
374- l2->vlan1, l2->vlan2, entry->ib1, ib2);
375+ l2->vlan1, l2->vlan2, entry->ib1, ib2,
376+ acct->packets, acct->bytes
377+ );
378 }
379
380 return 0;
381diff --git a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
382index 2f7d76d..f258539 100755
383--- a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
384+++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
385@@ -504,6 +504,7 @@ static int
386 mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
387 {
388 struct mtk_flow_entry *entry;
389+ struct mtk_foe_accounting diff;
390 u32 idle;
391 int i;
392
393@@ -516,6 +517,12 @@ mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
394 idle = mtk_foe_entry_idle_time(eth->ppe[i], entry);
395 f->stats.lastused = jiffies - idle * HZ;
396
397+ if (entry->hash != 0xFFFF) {
398+ mtk_foe_entry_get_mib(eth->ppe[i], entry->hash, &diff);
399+ f->stats.pkts += diff.packets;
400+ f->stats.bytes += diff.bytes;
401+ }
402+
403 return 0;
404 }
405
406diff --git a/drivers/net/ethernet/mediatek/mtk_ppe_regs.h b/drivers/net/ethernet/mediatek/mtk_ppe_regs.h
407index d319f18..9eb7a0d 100644
408--- a/drivers/net/ethernet/mediatek/mtk_ppe_regs.h
409+++ b/drivers/net/ethernet/mediatek/mtk_ppe_regs.h
410@@ -145,6 +146,20 @@ enum {
411
412 #define MTK_PPE_MIB_TB_BASE 0x338
413
414+#define MTK_PPE_MIB_SER_CR 0x33C
415+#define MTK_PPE_MIB_SER_CR_ST BIT(16)
416+#define MTK_PPE_MIB_SER_CR_ADDR GENMASK(13, 0)
417+
418+#define MTK_PPE_MIB_SER_R0 0x340
419+#define MTK_PPE_MIB_SER_R0_BYTE_CNT_LOW GENMASK(31, 0)
420+
421+#define MTK_PPE_MIB_SER_R1 0x344
422+#define MTK_PPE_MIB_SER_R1_PKT_CNT_LOW GENMASK(31, 16)
423+#define MTK_PPE_MIB_SER_R1_BYTE_CNT_HIGH GENMASK(15, 0)
424+
425+#define MTK_PPE_MIB_SER_R2 0x348
426+#define MTK_PPE_MIB_SER_R2_PKT_CNT_HIGH GENMASK(23, 0)
427+
428 #define MTK_PPE_MIB_CACHE_CTL 0x350
429 #define MTK_PPE_MIB_CACHE_CTL_EN BIT(0)
430 #define MTK_PPE_MIB_CACHE_CTL_FLUSH BIT(2)
431diff --git a/net/netfilter/xt_FLOWOFFLOAD.c b/net/netfilter/xt_FLOWOFFLOAD.c
432index 8547f4a..c175e4d 100644
433--- a/net/netfilter/xt_FLOWOFFLOAD.c
434+++ b/net/netfilter/xt_FLOWOFFLOAD.c
435@@ -700,12 +781,12 @@ static int __init xt_flowoffload_tg_init(void)
436 if (ret)
437 goto cleanup;
438
439- flowtable[1].ft.flags = NF_FLOWTABLE_HW_OFFLOAD;
440+ flowtable[1].ft.flags = NF_FLOWTABLE_HW_OFFLOAD | NF_FLOWTABLE_COUNTER;
441
442 ret = xt_register_target(&offload_tg_reg);
443 if (ret)
444 goto cleanup2;
445
446 return 0;
447
448 cleanup2: