1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
4 #include <linux/kernel.h>
6 #include <linux/iopoll.h>
7 #include <linux/etherdevice.h>
8 #include <linux/platform_device.h>
9 #include <linux/if_ether.h>
10 #include <linux/if_vlan.h>
11 #include <net/dst_metadata.h>
13 #include "mtk_eth_soc.h"
15 #include "mtk_ppe_regs.h"
17 static DEFINE_SPINLOCK(ppe_lock);
19 static const struct rhashtable_params mtk_flow_l2_ht_params = {
20 .head_offset = offsetof(struct mtk_flow_entry, l2_node),
21 .key_offset = offsetof(struct mtk_flow_entry, data.bridge),
22 .key_len = offsetof(struct mtk_foe_bridge, key_end),
23 .automatic_shrinking = true,
26 static void ppe_w32(struct mtk_ppe *ppe, u32 reg, u32 val)
28 writel(val, ppe->base + reg);
31 static u32 ppe_r32(struct mtk_ppe *ppe, u32 reg)
33 return readl(ppe->base + reg);
36 static u32 ppe_m32(struct mtk_ppe *ppe, u32 reg, u32 mask, u32 set)
40 val = ppe_r32(ppe, reg);
43 ppe_w32(ppe, reg, val);
48 static u32 ppe_set(struct mtk_ppe *ppe, u32 reg, u32 val)
50 return ppe_m32(ppe, reg, 0, val);
53 static u32 ppe_clear(struct mtk_ppe *ppe, u32 reg, u32 val)
55 return ppe_m32(ppe, reg, val, 0);
58 static u32 mtk_eth_timestamp(struct mtk_eth *eth)
60 return mtk_r32(eth, 0x0010) & mtk_get_ib1_ts_mask(eth);
63 static int mtk_ppe_wait_busy(struct mtk_ppe *ppe)
68 ret = readl_poll_timeout(ppe->base + MTK_PPE_GLO_CFG, val,
69 !(val & MTK_PPE_GLO_CFG_BUSY),
70 20, MTK_PPE_WAIT_TIMEOUT_US);
73 dev_err(ppe->dev, "PPE table busy");
78 static int mtk_ppe_mib_wait_busy(struct mtk_ppe *ppe)
83 ret = readl_poll_timeout(ppe->base + MTK_PPE_MIB_SER_CR, val,
84 !(val & MTK_PPE_MIB_SER_CR_ST),
85 20, MTK_PPE_WAIT_TIMEOUT_US);
88 dev_err(ppe->dev, "MIB table busy");
93 static int mtk_mib_entry_read(struct mtk_ppe *ppe, u16 index, u64 *bytes, u64 *packets)
95 u32 byte_cnt_low, byte_cnt_high, pkt_cnt_low, pkt_cnt_high;
96 u32 val, cnt_r0, cnt_r1, cnt_r2;
99 val = FIELD_PREP(MTK_PPE_MIB_SER_CR_ADDR, index) | MTK_PPE_MIB_SER_CR_ST;
100 ppe_w32(ppe, MTK_PPE_MIB_SER_CR, val);
102 ret = mtk_ppe_mib_wait_busy(ppe);
106 cnt_r0 = readl(ppe->base + MTK_PPE_MIB_SER_R0);
107 cnt_r1 = readl(ppe->base + MTK_PPE_MIB_SER_R1);
108 cnt_r2 = readl(ppe->base + MTK_PPE_MIB_SER_R2);
110 byte_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R0_BYTE_CNT_LOW, cnt_r0);
111 byte_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R1_BYTE_CNT_HIGH, cnt_r1);
112 pkt_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R1_PKT_CNT_LOW, cnt_r1);
113 pkt_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R2_PKT_CNT_HIGH, cnt_r2);
114 *bytes = ((u64)byte_cnt_high << 32) | byte_cnt_low;
115 *packets = (pkt_cnt_high << 16) | pkt_cnt_low;
120 static void mtk_ppe_cache_clear(struct mtk_ppe *ppe)
122 ppe_set(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_CLEAR);
123 ppe_clear(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_CLEAR);
126 static void mtk_ppe_cache_enable(struct mtk_ppe *ppe, bool enable)
128 mtk_ppe_cache_clear(ppe);
130 ppe_m32(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_EN,
131 enable * MTK_PPE_CACHE_CTL_EN);
134 static u32 mtk_ppe_hash_entry(struct mtk_eth *eth, struct mtk_foe_entry *e)
139 switch (mtk_get_ib1_pkt_type(eth, e->ib1)) {
140 case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
141 case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
142 hv1 = e->ipv4.orig.ports;
143 hv2 = e->ipv4.orig.dest_ip;
144 hv3 = e->ipv4.orig.src_ip;
146 case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
147 case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
148 hv1 = e->ipv6.src_ip[3] ^ e->ipv6.dest_ip[3];
149 hv1 ^= e->ipv6.ports;
151 hv2 = e->ipv6.src_ip[2] ^ e->ipv6.dest_ip[2];
152 hv2 ^= e->ipv6.dest_ip[0];
154 hv3 = e->ipv6.src_ip[1] ^ e->ipv6.dest_ip[1];
155 hv3 ^= e->ipv6.src_ip[0];
157 case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
158 case MTK_PPE_PKT_TYPE_IPV6_6RD:
161 return MTK_PPE_HASH_MASK;
164 hash = (hv1 & hv2) | ((~hv1) & hv3);
165 hash = (hash >> 24) | ((hash & 0xffffff) << 8);
166 hash ^= hv1 ^ hv2 ^ hv3;
168 hash <<= (ffs(eth->soc->hash_offset) - 1);
169 hash &= MTK_PPE_ENTRIES - 1;
174 static inline struct mtk_foe_mac_info *
175 mtk_foe_entry_l2(struct mtk_eth *eth, struct mtk_foe_entry *entry)
177 int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
179 if (type == MTK_PPE_PKT_TYPE_BRIDGE)
180 return &entry->bridge.l2;
182 if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
183 return &entry->ipv6.l2;
185 return &entry->ipv4.l2;
189 mtk_foe_entry_ib2(struct mtk_eth *eth, struct mtk_foe_entry *entry)
191 int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
193 if (type == MTK_PPE_PKT_TYPE_BRIDGE)
194 return &entry->bridge.ib2;
196 if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
197 return &entry->ipv6.ib2;
199 return &entry->ipv4.ib2;
202 int mtk_foe_entry_prepare(struct mtk_eth *eth, struct mtk_foe_entry *entry,
203 int type, int l4proto, u8 pse_port, u8 *src_mac,
206 struct mtk_foe_mac_info *l2;
209 memset(entry, 0, sizeof(*entry));
211 if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
212 val = FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_BIND) |
213 FIELD_PREP(MTK_FOE_IB1_PACKET_TYPE_V2, type) |
214 FIELD_PREP(MTK_FOE_IB1_UDP, l4proto == IPPROTO_UDP) |
215 MTK_FOE_IB1_BIND_CACHE_V2 | MTK_FOE_IB1_BIND_TTL_V2;
218 val = FIELD_PREP(MTK_FOE_IB2_DEST_PORT_V2, pse_port) |
219 FIELD_PREP(MTK_FOE_IB2_PORT_AG_V2, 0xf);
221 int port_mg = eth->soc->offload_version > 1 ? 0 : 0x3f;
223 val = FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_BIND) |
224 FIELD_PREP(MTK_FOE_IB1_PACKET_TYPE, type) |
225 FIELD_PREP(MTK_FOE_IB1_UDP, l4proto == IPPROTO_UDP) |
226 MTK_FOE_IB1_BIND_CACHE | MTK_FOE_IB1_BIND_TTL;
229 val = FIELD_PREP(MTK_FOE_IB2_DEST_PORT, pse_port) |
230 FIELD_PREP(MTK_FOE_IB2_PORT_MG, port_mg) |
231 FIELD_PREP(MTK_FOE_IB2_PORT_AG, 0x1f);
234 if (is_multicast_ether_addr(dest_mac))
235 val |= mtk_get_ib2_multicast_mask(eth);
237 ports_pad = 0xa5a5a500 | (l4proto & 0xff);
238 if (type == MTK_PPE_PKT_TYPE_IPV4_ROUTE)
239 entry->ipv4.orig.ports = ports_pad;
240 if (type == MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T)
241 entry->ipv6.ports = ports_pad;
243 if (type == MTK_PPE_PKT_TYPE_BRIDGE) {
244 ether_addr_copy(entry->bridge.src_mac, src_mac);
245 ether_addr_copy(entry->bridge.dest_mac, dest_mac);
246 entry->bridge.ib2 = val;
247 l2 = &entry->bridge.l2;
248 } else if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE) {
249 entry->ipv6.ib2 = val;
250 l2 = &entry->ipv6.l2;
252 entry->ipv4.ib2 = val;
253 l2 = &entry->ipv4.l2;
256 l2->dest_mac_hi = get_unaligned_be32(dest_mac);
257 l2->dest_mac_lo = get_unaligned_be16(dest_mac + 4);
258 l2->src_mac_hi = get_unaligned_be32(src_mac);
259 l2->src_mac_lo = get_unaligned_be16(src_mac + 4);
261 if (type >= MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T)
262 l2->etype = ETH_P_IPV6;
264 l2->etype = ETH_P_IP;
269 int mtk_foe_entry_set_pse_port(struct mtk_eth *eth,
270 struct mtk_foe_entry *entry, u8 port)
272 u32 *ib2 = mtk_foe_entry_ib2(eth, entry);
275 if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
276 val &= ~MTK_FOE_IB2_DEST_PORT_V2;
277 val |= FIELD_PREP(MTK_FOE_IB2_DEST_PORT_V2, port);
279 val &= ~MTK_FOE_IB2_DEST_PORT;
280 val |= FIELD_PREP(MTK_FOE_IB2_DEST_PORT, port);
287 int mtk_foe_entry_set_ipv4_tuple(struct mtk_eth *eth,
288 struct mtk_foe_entry *entry, bool egress,
289 __be32 src_addr, __be16 src_port,
290 __be32 dest_addr, __be16 dest_port)
292 int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
293 struct mtk_ipv4_tuple *t;
296 case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
298 t = &entry->ipv4.new;
302 case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
303 case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
304 t = &entry->ipv4.orig;
306 case MTK_PPE_PKT_TYPE_IPV6_6RD:
307 entry->ipv6_6rd.tunnel_src_ip = be32_to_cpu(src_addr);
308 entry->ipv6_6rd.tunnel_dest_ip = be32_to_cpu(dest_addr);
315 t->src_ip = be32_to_cpu(src_addr);
316 t->dest_ip = be32_to_cpu(dest_addr);
318 if (type == MTK_PPE_PKT_TYPE_IPV4_ROUTE)
321 t->src_port = be16_to_cpu(src_port);
322 t->dest_port = be16_to_cpu(dest_port);
327 int mtk_foe_entry_set_ipv6_tuple(struct mtk_eth *eth,
328 struct mtk_foe_entry *entry,
329 __be32 *src_addr, __be16 src_port,
330 __be32 *dest_addr, __be16 dest_port)
332 int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
337 case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
338 src = entry->dslite.tunnel_src_ip;
339 dest = entry->dslite.tunnel_dest_ip;
341 case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
342 case MTK_PPE_PKT_TYPE_IPV6_6RD:
343 entry->ipv6.src_port = be16_to_cpu(src_port);
344 entry->ipv6.dest_port = be16_to_cpu(dest_port);
346 case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
347 src = entry->ipv6.src_ip;
348 dest = entry->ipv6.dest_ip;
355 for (i = 0; i < 4; i++)
356 src[i] = be32_to_cpu(src_addr[i]);
357 for (i = 0; i < 4; i++)
358 dest[i] = be32_to_cpu(dest_addr[i]);
363 int mtk_foe_entry_set_dsa(struct mtk_eth *eth, struct mtk_foe_entry *entry,
366 struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
368 l2->etype = BIT(port);
370 if (!(entry->ib1 & mtk_get_ib1_vlan_layer_mask(eth)))
371 entry->ib1 |= mtk_prep_ib1_vlan_layer(eth, 1);
375 entry->ib1 &= ~mtk_get_ib1_vlan_tag_mask(eth);
380 int mtk_foe_entry_set_vlan(struct mtk_eth *eth, struct mtk_foe_entry *entry,
383 struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
385 switch (mtk_get_ib1_vlan_layer(eth, entry->ib1)) {
387 entry->ib1 |= mtk_get_ib1_vlan_tag_mask(eth) |
388 mtk_prep_ib1_vlan_layer(eth, 1);
392 if (!(entry->ib1 & mtk_get_ib1_vlan_tag_mask(eth))) {
397 entry->ib1 += mtk_prep_ib1_vlan_layer(eth, 1);
405 int mtk_foe_entry_set_pppoe(struct mtk_eth *eth, struct mtk_foe_entry *entry,
408 struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
410 if (!(entry->ib1 & mtk_get_ib1_vlan_layer_mask(eth)) ||
411 (entry->ib1 & mtk_get_ib1_vlan_tag_mask(eth)))
412 l2->etype = ETH_P_PPP_SES;
414 entry->ib1 |= mtk_get_ib1_ppoe_mask(eth);
420 int mtk_foe_entry_set_wdma(struct mtk_eth *eth, struct mtk_foe_entry *entry,
421 int wdma_idx, int txq, int bss, int wcid)
423 struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
424 u32 *ib2 = mtk_foe_entry_ib2(eth, entry);
426 if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
427 *ib2 &= ~MTK_FOE_IB2_PORT_MG_V2;
428 *ib2 |= FIELD_PREP(MTK_FOE_IB2_RX_IDX, txq) |
429 MTK_FOE_IB2_WDMA_WINFO_V2;
430 l2->winfo = FIELD_PREP(MTK_FOE_WINFO_WCID, wcid) |
431 FIELD_PREP(MTK_FOE_WINFO_BSS, bss);
433 *ib2 &= ~MTK_FOE_IB2_PORT_MG;
434 *ib2 |= MTK_FOE_IB2_WDMA_WINFO;
436 *ib2 |= MTK_FOE_IB2_WDMA_DEVIDX;
437 l2->vlan2 = FIELD_PREP(MTK_FOE_VLAN2_WINFO_BSS, bss) |
438 FIELD_PREP(MTK_FOE_VLAN2_WINFO_WCID, wcid) |
439 FIELD_PREP(MTK_FOE_VLAN2_WINFO_RING, txq);
445 int mtk_foe_entry_set_queue(struct mtk_eth *eth, struct mtk_foe_entry *entry,
448 u32 *ib2 = mtk_foe_entry_ib2(eth, entry);
450 if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
451 *ib2 &= ~MTK_FOE_IB2_QID_V2;
452 *ib2 |= FIELD_PREP(MTK_FOE_IB2_QID_V2, queue);
453 *ib2 |= MTK_FOE_IB2_PSE_QOS_V2;
455 *ib2 &= ~MTK_FOE_IB2_QID;
456 *ib2 |= FIELD_PREP(MTK_FOE_IB2_QID, queue);
457 *ib2 |= MTK_FOE_IB2_PSE_QOS;
464 mtk_flow_entry_match(struct mtk_eth *eth, struct mtk_flow_entry *entry,
465 struct mtk_foe_entry *data)
469 if ((data->ib1 ^ entry->data.ib1) & MTK_FOE_IB1_UDP)
472 type = mtk_get_ib1_pkt_type(eth, entry->data.ib1);
473 if (type > MTK_PPE_PKT_TYPE_IPV4_DSLITE)
474 len = offsetof(struct mtk_foe_entry, ipv6._rsv);
476 len = offsetof(struct mtk_foe_entry, ipv4.ib2);
478 return !memcmp(&entry->data.data, &data->data, len - 4);
482 __mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
484 struct hlist_head *head;
485 struct hlist_node *tmp;
487 if (entry->type == MTK_FLOW_TYPE_L2) {
488 rhashtable_remove_fast(&ppe->l2_flows, &entry->l2_node,
489 mtk_flow_l2_ht_params);
491 head = &entry->l2_flows;
492 hlist_for_each_entry_safe(entry, tmp, head, l2_data.list)
493 __mtk_foe_entry_clear(ppe, entry);
497 hlist_del_init(&entry->list);
498 if (entry->hash != 0xffff) {
499 struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, entry->hash);
501 hwe->ib1 &= ~MTK_FOE_IB1_STATE;
502 hwe->ib1 |= FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_INVALID);
504 mtk_ppe_cache_clear(ppe);
506 if (ppe->accounting) {
507 struct mtk_foe_accounting *acct;
509 acct = ppe->acct_table + entry->hash * sizeof(*acct);
514 entry->hash = 0xffff;
516 if (entry->type != MTK_FLOW_TYPE_L2_SUBFLOW)
519 hlist_del_init(&entry->l2_data.list);
523 static int __mtk_foe_entry_idle_time(struct mtk_ppe *ppe, u32 ib1)
525 u32 ib1_ts_mask = mtk_get_ib1_ts_mask(ppe->eth);
526 u16 now = mtk_eth_timestamp(ppe->eth);
527 u16 timestamp = ib1 & ib1_ts_mask;
530 return ib1_ts_mask + 1 - timestamp + now;
532 return now - timestamp;
536 mtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
538 u32 ib1_ts_mask = mtk_get_ib1_ts_mask(ppe->eth);
539 struct mtk_flow_entry *cur;
540 struct mtk_foe_entry *hwe;
541 struct hlist_node *tmp;
544 idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
545 hlist_for_each_entry_safe(cur, tmp, &entry->l2_flows, l2_data.list) {
549 hwe = mtk_foe_get_entry(ppe, cur->hash);
550 ib1 = READ_ONCE(hwe->ib1);
552 if (FIELD_GET(MTK_FOE_IB1_STATE, ib1) != MTK_FOE_STATE_BIND) {
554 __mtk_foe_entry_clear(ppe, cur);
558 cur_idle = __mtk_foe_entry_idle_time(ppe, ib1);
559 if (cur_idle >= idle)
563 entry->data.ib1 &= ~ib1_ts_mask;
564 entry->data.ib1 |= hwe->ib1 & ib1_ts_mask;
569 mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
571 struct mtk_foe_entry foe = {};
572 struct mtk_foe_entry *hwe;
574 spin_lock_bh(&ppe_lock);
576 if (entry->type == MTK_FLOW_TYPE_L2) {
577 mtk_flow_entry_update_l2(ppe, entry);
581 if (entry->hash == 0xffff)
584 hwe = mtk_foe_get_entry(ppe, entry->hash);
585 memcpy(&foe, hwe, ppe->eth->soc->foe_entry_size);
586 if (!mtk_flow_entry_match(ppe->eth, entry, &foe)) {
587 entry->hash = 0xffff;
591 entry->data.ib1 = foe.ib1;
594 spin_unlock_bh(&ppe_lock);
598 __mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
601 struct mtk_eth *eth = ppe->eth;
602 u16 timestamp = mtk_eth_timestamp(eth);
603 struct mtk_foe_entry *hwe;
606 if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
607 entry->ib1 &= ~MTK_FOE_IB1_BIND_TIMESTAMP_V2;
608 entry->ib1 |= FIELD_PREP(MTK_FOE_IB1_BIND_TIMESTAMP_V2,
611 entry->ib1 &= ~MTK_FOE_IB1_BIND_TIMESTAMP;
612 entry->ib1 |= FIELD_PREP(MTK_FOE_IB1_BIND_TIMESTAMP,
616 hwe = mtk_foe_get_entry(ppe, hash);
617 memcpy(&hwe->data, &entry->data, eth->soc->foe_entry_size - sizeof(hwe->ib1));
619 hwe->ib1 = entry->ib1;
621 if (ppe->accounting) {
622 if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
623 val = MTK_FOE_IB2_MIB_CNT_V2;
625 val = MTK_FOE_IB2_MIB_CNT;
626 *mtk_foe_entry_ib2(eth, hwe) |= val;
631 mtk_ppe_cache_clear(ppe);
634 void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
636 spin_lock_bh(&ppe_lock);
637 __mtk_foe_entry_clear(ppe, entry);
638 spin_unlock_bh(&ppe_lock);
642 mtk_foe_entry_commit_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
644 struct mtk_flow_entry *prev;
646 entry->type = MTK_FLOW_TYPE_L2;
648 prev = rhashtable_lookup_get_insert_fast(&ppe->l2_flows, &entry->l2_node,
649 mtk_flow_l2_ht_params);
654 return PTR_ERR(prev);
656 return rhashtable_replace_fast(&ppe->l2_flows, &prev->l2_node,
657 &entry->l2_node, mtk_flow_l2_ht_params);
660 int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
662 const struct mtk_soc_data *soc = ppe->eth->soc;
663 int type = mtk_get_ib1_pkt_type(ppe->eth, entry->data.ib1);
666 if (type == MTK_PPE_PKT_TYPE_BRIDGE)
667 return mtk_foe_entry_commit_l2(ppe, entry);
669 hash = mtk_ppe_hash_entry(ppe->eth, &entry->data);
670 entry->hash = 0xffff;
671 spin_lock_bh(&ppe_lock);
672 hlist_add_head(&entry->list, &ppe->foe_flow[hash / soc->hash_offset]);
673 spin_unlock_bh(&ppe_lock);
679 mtk_foe_entry_commit_subflow(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
682 const struct mtk_soc_data *soc = ppe->eth->soc;
683 struct mtk_flow_entry *flow_info;
684 struct mtk_foe_entry foe = {}, *hwe;
685 struct mtk_foe_mac_info *l2;
686 u32 ib1_mask = mtk_get_ib1_pkt_type_mask(ppe->eth) | MTK_FOE_IB1_UDP;
689 flow_info = kzalloc(sizeof(*flow_info), GFP_ATOMIC);
693 flow_info->l2_data.base_flow = entry;
694 flow_info->type = MTK_FLOW_TYPE_L2_SUBFLOW;
695 flow_info->hash = hash;
696 hlist_add_head(&flow_info->list,
697 &ppe->foe_flow[hash / soc->hash_offset]);
698 hlist_add_head(&flow_info->l2_data.list, &entry->l2_flows);
700 hwe = mtk_foe_get_entry(ppe, hash);
701 memcpy(&foe, hwe, soc->foe_entry_size);
703 foe.ib1 |= entry->data.ib1 & ~ib1_mask;
705 l2 = mtk_foe_entry_l2(ppe->eth, &foe);
706 memcpy(l2, &entry->data.bridge.l2, sizeof(*l2));
708 type = mtk_get_ib1_pkt_type(ppe->eth, foe.ib1);
709 if (type == MTK_PPE_PKT_TYPE_IPV4_HNAPT)
710 memcpy(&foe.ipv4.new, &foe.ipv4.orig, sizeof(foe.ipv4.new));
711 else if (type >= MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T && l2->etype == ETH_P_IP)
712 l2->etype = ETH_P_IPV6;
714 *mtk_foe_entry_ib2(ppe->eth, &foe) = entry->data.bridge.ib2;
716 __mtk_foe_entry_commit(ppe, &foe, hash);
719 void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
721 const struct mtk_soc_data *soc = ppe->eth->soc;
722 struct hlist_head *head = &ppe->foe_flow[hash / soc->hash_offset];
723 struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, hash);
724 struct mtk_flow_entry *entry;
725 struct mtk_foe_bridge key = {};
726 struct hlist_node *n;
731 spin_lock_bh(&ppe_lock);
733 if (FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) == MTK_FOE_STATE_BIND)
736 hlist_for_each_entry_safe(entry, n, head, list) {
737 if (entry->type == MTK_FLOW_TYPE_L2_SUBFLOW) {
738 if (unlikely(FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) ==
742 entry->hash = 0xffff;
743 __mtk_foe_entry_clear(ppe, entry);
747 if (found || !mtk_flow_entry_match(ppe->eth, entry, hwe)) {
748 if (entry->hash != 0xffff)
749 entry->hash = 0xffff;
754 __mtk_foe_entry_commit(ppe, &entry->data, hash);
762 ether_addr_copy(key.dest_mac, eh->h_dest);
763 ether_addr_copy(key.src_mac, eh->h_source);
766 switch (skb->protocol) {
767 #if IS_ENABLED(CONFIG_NET_DSA)
768 case htons(ETH_P_XDSA):
769 if (!netdev_uses_dsa(skb->dev) ||
770 skb->dev->dsa_ptr->tag_ops->proto != DSA_TAG_PROTO_MTK)
773 if (!skb_metadata_dst(skb))
776 if (get_unaligned_be16(tag) != ETH_P_8021Q)
781 case htons(ETH_P_8021Q):
782 key.vlan = get_unaligned_be16(tag + 2) & VLAN_VID_MASK;
788 entry = rhashtable_lookup_fast(&ppe->l2_flows, &key, mtk_flow_l2_ht_params);
792 mtk_foe_entry_commit_subflow(ppe, entry, hash);
795 spin_unlock_bh(&ppe_lock);
798 int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
800 mtk_flow_entry_update(ppe, entry);
802 return __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
805 int mtk_ppe_prepare_reset(struct mtk_ppe *ppe)
811 ppe_clear(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_CFG_KEEPALIVE);
812 ppe_clear(ppe, MTK_PPE_BIND_LMT1, MTK_PPE_NTU_KEEPALIVE);
813 ppe_w32(ppe, MTK_PPE_KEEPALIVE, 0);
814 usleep_range(10000, 11000);
816 /* set KA timer to maximum */
817 ppe_set(ppe, MTK_PPE_BIND_LMT1, MTK_PPE_NTU_KEEPALIVE);
818 ppe_w32(ppe, MTK_PPE_KEEPALIVE, 0xffffffff);
820 /* set KA tick select */
821 ppe_set(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_TICK_SEL);
822 ppe_set(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_CFG_KEEPALIVE);
823 usleep_range(10000, 11000);
825 /* disable scan mode */
826 ppe_clear(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_CFG_SCAN_MODE);
827 usleep_range(10000, 11000);
829 return mtk_ppe_wait_busy(ppe);
832 struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
833 struct mtk_foe_accounting *diff)
835 struct mtk_foe_accounting *acct;
836 int size = sizeof(struct mtk_foe_accounting);
839 if (!ppe->accounting)
842 if (mtk_mib_entry_read(ppe, index, &bytes, &packets))
845 acct = ppe->acct_table + index * size;
847 acct->bytes += bytes;
848 acct->packets += packets;
852 diff->packets = packets;
858 struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int index)
860 bool accounting = eth->soc->has_accounting;
861 const struct mtk_soc_data *soc = eth->soc;
862 struct mtk_foe_accounting *acct;
863 struct device *dev = eth->dev;
864 struct mtk_mib_entry *mib;
869 ppe = devm_kzalloc(dev, sizeof(*ppe), GFP_KERNEL);
873 rhashtable_init(&ppe->l2_flows, &mtk_flow_l2_ht_params);
875 /* need to allocate a separate device, since it PPE DMA access is
881 ppe->version = eth->soc->offload_version;
882 ppe->accounting = accounting;
884 foe = dmam_alloc_coherent(ppe->dev,
885 MTK_PPE_ENTRIES * soc->foe_entry_size,
886 &ppe->foe_phys, GFP_KERNEL);
888 goto err_free_l2_flows;
890 ppe->foe_table = foe;
892 foe_flow_size = (MTK_PPE_ENTRIES / soc->hash_offset) *
893 sizeof(*ppe->foe_flow);
894 ppe->foe_flow = devm_kzalloc(dev, foe_flow_size, GFP_KERNEL);
896 goto err_free_l2_flows;
899 mib = dmam_alloc_coherent(ppe->dev, MTK_PPE_ENTRIES * sizeof(*mib),
900 &ppe->mib_phys, GFP_KERNEL);
904 ppe->mib_table = mib;
906 acct = devm_kzalloc(dev, MTK_PPE_ENTRIES * sizeof(*acct),
912 ppe->acct_table = acct;
915 mtk_ppe_debugfs_init(ppe, index);
920 rhashtable_destroy(&ppe->l2_flows);
924 void mtk_ppe_deinit(struct mtk_eth *eth)
928 for (i = 0; i < ARRAY_SIZE(eth->ppe); i++) {
931 rhashtable_destroy(ð->ppe[i]->l2_flows);
935 static void mtk_ppe_init_foe_table(struct mtk_ppe *ppe)
937 static const u8 skip[] = { 12, 25, 38, 51, 76, 89, 102 };
940 memset(ppe->foe_table, 0,
941 MTK_PPE_ENTRIES * ppe->eth->soc->foe_entry_size);
943 if (!IS_ENABLED(CONFIG_SOC_MT7621))
946 /* skip all entries that cross the 1024 byte boundary */
947 for (i = 0; i < MTK_PPE_ENTRIES; i += 128) {
948 for (k = 0; k < ARRAY_SIZE(skip); k++) {
949 struct mtk_foe_entry *hwe;
951 hwe = mtk_foe_get_entry(ppe, i + skip[k]);
952 hwe->ib1 |= MTK_FOE_IB1_STATIC;
957 void mtk_ppe_start(struct mtk_ppe *ppe)
964 mtk_ppe_init_foe_table(ppe);
965 ppe_w32(ppe, MTK_PPE_TB_BASE, ppe->foe_phys);
967 val = MTK_PPE_TB_CFG_ENTRY_80B |
968 MTK_PPE_TB_CFG_AGE_NON_L4 |
969 MTK_PPE_TB_CFG_AGE_UNBIND |
970 MTK_PPE_TB_CFG_AGE_TCP |
971 MTK_PPE_TB_CFG_AGE_UDP |
972 MTK_PPE_TB_CFG_AGE_TCP_FIN |
973 FIELD_PREP(MTK_PPE_TB_CFG_SEARCH_MISS,
974 MTK_PPE_SEARCH_MISS_ACTION_FORWARD_BUILD) |
975 FIELD_PREP(MTK_PPE_TB_CFG_KEEPALIVE,
976 MTK_PPE_KEEPALIVE_DISABLE) |
977 FIELD_PREP(MTK_PPE_TB_CFG_HASH_MODE, 1) |
978 FIELD_PREP(MTK_PPE_TB_CFG_SCAN_MODE,
979 MTK_PPE_SCAN_MODE_KEEPALIVE_AGE) |
980 FIELD_PREP(MTK_PPE_TB_CFG_ENTRY_NUM,
981 MTK_PPE_ENTRIES_SHIFT);
982 if (MTK_HAS_CAPS(ppe->eth->soc->caps, MTK_NETSYS_V2))
983 val |= MTK_PPE_TB_CFG_INFO_SEL;
984 ppe_w32(ppe, MTK_PPE_TB_CFG, val);
986 ppe_w32(ppe, MTK_PPE_IP_PROTO_CHK,
987 MTK_PPE_IP_PROTO_CHK_IPV4 | MTK_PPE_IP_PROTO_CHK_IPV6);
989 mtk_ppe_cache_enable(ppe, true);
991 val = MTK_PPE_FLOW_CFG_IP6_3T_ROUTE |
992 MTK_PPE_FLOW_CFG_IP6_5T_ROUTE |
993 MTK_PPE_FLOW_CFG_IP6_6RD |
994 MTK_PPE_FLOW_CFG_IP4_NAT |
995 MTK_PPE_FLOW_CFG_IP4_NAPT |
996 MTK_PPE_FLOW_CFG_IP4_DSLITE |
997 MTK_PPE_FLOW_CFG_IP4_NAT_FRAG;
998 if (MTK_HAS_CAPS(ppe->eth->soc->caps, MTK_NETSYS_V2))
999 val |= MTK_PPE_MD_TOAP_BYP_CRSN0 |
1000 MTK_PPE_MD_TOAP_BYP_CRSN1 |
1001 MTK_PPE_MD_TOAP_BYP_CRSN2 |
1002 MTK_PPE_FLOW_CFG_IP4_HASH_GRE_KEY;
1004 val |= MTK_PPE_FLOW_CFG_IP4_TCP_FRAG |
1005 MTK_PPE_FLOW_CFG_IP4_UDP_FRAG;
1006 ppe_w32(ppe, MTK_PPE_FLOW_CFG, val);
1008 val = FIELD_PREP(MTK_PPE_UNBIND_AGE_MIN_PACKETS, 1000) |
1009 FIELD_PREP(MTK_PPE_UNBIND_AGE_DELTA, 3);
1010 ppe_w32(ppe, MTK_PPE_UNBIND_AGE, val);
1012 val = FIELD_PREP(MTK_PPE_BIND_AGE0_DELTA_UDP, 12) |
1013 FIELD_PREP(MTK_PPE_BIND_AGE0_DELTA_NON_L4, 1);
1014 ppe_w32(ppe, MTK_PPE_BIND_AGE0, val);
1016 val = FIELD_PREP(MTK_PPE_BIND_AGE1_DELTA_TCP_FIN, 1) |
1017 FIELD_PREP(MTK_PPE_BIND_AGE1_DELTA_TCP, 7);
1018 ppe_w32(ppe, MTK_PPE_BIND_AGE1, val);
1020 val = MTK_PPE_BIND_LIMIT0_QUARTER | MTK_PPE_BIND_LIMIT0_HALF;
1021 ppe_w32(ppe, MTK_PPE_BIND_LIMIT0, val);
1023 val = MTK_PPE_BIND_LIMIT1_FULL |
1024 FIELD_PREP(MTK_PPE_BIND_LIMIT1_NON_L4, 1);
1025 ppe_w32(ppe, MTK_PPE_BIND_LIMIT1, val);
1027 val = FIELD_PREP(MTK_PPE_BIND_RATE_BIND, 30) |
1028 FIELD_PREP(MTK_PPE_BIND_RATE_PREBIND, 1);
1029 ppe_w32(ppe, MTK_PPE_BIND_RATE, val);
1032 val = MTK_PPE_GLO_CFG_EN |
1033 MTK_PPE_GLO_CFG_IP4_L4_CS_DROP |
1034 MTK_PPE_GLO_CFG_IP4_CS_DROP |
1035 MTK_PPE_GLO_CFG_FLOW_DROP_UPDATE;
1036 ppe_w32(ppe, MTK_PPE_GLO_CFG, val);
1038 ppe_w32(ppe, MTK_PPE_DEFAULT_CPU_PORT, 0);
1040 if (MTK_HAS_CAPS(ppe->eth->soc->caps, MTK_NETSYS_V2)) {
1041 ppe_w32(ppe, MTK_PPE_DEFAULT_CPU_PORT1, 0xcb777);
1042 ppe_w32(ppe, MTK_PPE_SBW_CTRL, 0x7f);
1045 if (ppe->accounting && ppe->mib_phys) {
1046 ppe_w32(ppe, MTK_PPE_MIB_TB_BASE, ppe->mib_phys);
1047 ppe_m32(ppe, MTK_PPE_MIB_CFG, MTK_PPE_MIB_CFG_EN,
1048 MTK_PPE_MIB_CFG_EN);
1049 ppe_m32(ppe, MTK_PPE_MIB_CFG, MTK_PPE_MIB_CFG_RD_CLR,
1050 MTK_PPE_MIB_CFG_RD_CLR);
1051 ppe_m32(ppe, MTK_PPE_MIB_CACHE_CTL, MTK_PPE_MIB_CACHE_CTL_EN,
1052 MTK_PPE_MIB_CFG_RD_CLR);
1056 int mtk_ppe_stop(struct mtk_ppe *ppe)
1064 for (i = 0; i < MTK_PPE_ENTRIES; i++) {
1065 struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, i);
1067 hwe->ib1 = FIELD_PREP(MTK_FOE_IB1_STATE,
1068 MTK_FOE_STATE_INVALID);
1071 mtk_ppe_cache_enable(ppe, false);
1073 /* disable offload engine */
1074 ppe_clear(ppe, MTK_PPE_GLO_CFG, MTK_PPE_GLO_CFG_EN);
1075 ppe_w32(ppe, MTK_PPE_FLOW_CFG, 0);
1078 val = MTK_PPE_TB_CFG_AGE_NON_L4 |
1079 MTK_PPE_TB_CFG_AGE_UNBIND |
1080 MTK_PPE_TB_CFG_AGE_TCP |
1081 MTK_PPE_TB_CFG_AGE_UDP |
1082 MTK_PPE_TB_CFG_AGE_TCP_FIN;
1083 ppe_clear(ppe, MTK_PPE_TB_CFG, val);
1085 return mtk_ppe_wait_busy(ppe);