1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Fri, 28 Oct 2022 11:01:12 +0200
3 Subject: [PATCH] net: ethernet: mtk_eth_soc: fix VLAN rx hardware
6 - enable VLAN untagging for PDMA rx
7 - make it possible to disable the feature via ethtool
8 - pass VLAN tag to the DSA driver
9 - untag special tag on PDMA only if no non-DSA devices are in use
10 - disable special tag untagging on 7986 for now, since it's not working yet
12 Signed-off-by: Felix Fietkau <nbd@nbd.name>
15 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
16 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
18 #include <linux/jhash.h>
19 #include <linux/bitfield.h>
21 +#include <net/dst_metadata.h>
23 #include "mtk_eth_soc.h"
25 @@ -2022,16 +2023,22 @@ static int mtk_poll_rx(struct napi_struc
26 htons(RX_DMA_VPID(trxd.rxd4)),
27 RX_DMA_VID(trxd.rxd4));
28 } else if (trxd.rxd2 & RX_DMA_VTAG) {
29 - __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
30 + __vlan_hwaccel_put_tag(skb, htons(RX_DMA_VPID(trxd.rxd3)),
31 RX_DMA_VID(trxd.rxd3));
35 + /* When using VLAN untagging in combination with DSA, the
36 + * hardware treats the MTK special tag as a VLAN and untags it.
38 + if (skb_vlan_tag_present(skb) && netdev_uses_dsa(netdev)) {
39 + unsigned int port = ntohs(skb->vlan_proto) & GENMASK(2, 0);
41 - /* If the device is attached to a dsa switch, the special
42 - * tag inserted in VLAN field by hw switch can * be offloaded
43 - * by RX HW VLAN offload. Clear vlan info.
45 - if (netdev_uses_dsa(netdev))
46 - __vlan_hwaccel_clear_tag(skb);
47 + if (port < ARRAY_SIZE(eth->dsa_meta) &&
48 + eth->dsa_meta[port])
49 + skb_dst_set_noref(skb, ð->dsa_meta[port]->dst);
51 + __vlan_hwaccel_clear_tag(skb);
54 skb_record_rx_queue(skb, 0);
55 @@ -2856,15 +2863,30 @@ static netdev_features_t mtk_fix_feature
57 static int mtk_set_features(struct net_device *dev, netdev_features_t features)
60 + struct mtk_mac *mac = netdev_priv(dev);
61 + struct mtk_eth *eth = mac->hw;
62 + netdev_features_t diff = dev->features ^ features;
65 + if ((diff & NETIF_F_LRO) && !(features & NETIF_F_LRO))
66 + mtk_hwlro_netdev_disable(dev);
68 - if (!((dev->features ^ features) & NETIF_F_LRO))
69 + /* Set RX VLAN offloading */
70 + if (!(diff & NETIF_F_HW_VLAN_CTAG_RX))
73 - if (!(features & NETIF_F_LRO))
74 - mtk_hwlro_netdev_disable(dev);
75 + mtk_w32(eth, !!(features & NETIF_F_HW_VLAN_CTAG_RX),
79 + /* sync features with other MAC */
80 + for (i = 0; i < MTK_MAC_COUNT; i++) {
81 + if (!eth->netdev[i] || eth->netdev[i] == dev)
83 + eth->netdev[i]->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
84 + eth->netdev[i]->features |= features & NETIF_F_HW_VLAN_CTAG_RX;
90 /* wait for DMA to finish whatever it is doing before we start using it again */
91 @@ -3161,11 +3183,45 @@ found:
95 +static bool mtk_uses_dsa(struct net_device *dev)
97 +#if IS_ENABLED(CONFIG_NET_DSA)
98 + return netdev_uses_dsa(dev) &&
99 + dev->dsa_ptr->tag_ops->proto == DSA_TAG_PROTO_MTK;
105 static int mtk_open(struct net_device *dev)
107 struct mtk_mac *mac = netdev_priv(dev);
108 struct mtk_eth *eth = mac->hw;
112 + if (mtk_uses_dsa(dev) && !eth->prog) {
113 + for (i = 0; i < ARRAY_SIZE(eth->dsa_meta); i++) {
114 + struct metadata_dst *md_dst = eth->dsa_meta[i];
119 + md_dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
124 + md_dst->u.port_info.port_id = i;
125 + eth->dsa_meta[i] = md_dst;
128 + /* Hardware special tag parsing needs to be disabled if at least
129 + * one MAC does not use DSA.
131 + u32 val = mtk_r32(eth, MTK_CDMP_IG_CTRL);
132 + val &= ~MTK_CDMP_STAG_EN;
133 + mtk_w32(eth, val, MTK_CDMP_IG_CTRL);
136 err = phylink_of_phy_connect(mac->phylink, mac->of_node, 0);
138 @@ -3686,6 +3742,10 @@ static int mtk_hw_init(struct mtk_eth *e
140 val = mtk_r32(eth, MTK_CDMQ_IG_CTRL);
141 mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL);
142 + if (!MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
143 + val = mtk_r32(eth, MTK_CDMP_IG_CTRL);
144 + mtk_w32(eth, val | MTK_CDMP_STAG_EN, MTK_CDMP_IG_CTRL);
147 /* Enable RX VLan Offloading */
148 mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
149 @@ -3905,6 +3965,12 @@ static int mtk_free_dev(struct mtk_eth *
150 free_netdev(eth->netdev[i]);
153 + for (i = 0; i < ARRAY_SIZE(eth->dsa_meta); i++) {
154 + if (!eth->dsa_meta[i])
156 + metadata_dst_free(eth->dsa_meta[i]);
162 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
163 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
165 #include <linux/bpf_trace.h>
168 +#define MTK_MAX_DSA_PORTS 7
169 +#define MTK_DSA_PORT_MASK GENMASK(2, 0)
171 #define MTK_QDMA_NUM_QUEUES 16
172 #define MTK_QDMA_PAGE_SIZE 2048
173 #define MTK_MAX_RX_LENGTH 1536
175 #define MTK_CDMQ_IG_CTRL 0x1400
176 #define MTK_CDMQ_STAG_EN BIT(0)
178 +/* CDMQ Exgress Control Register */
179 +#define MTK_CDMQ_EG_CTRL 0x1404
181 /* CDMP Ingress Control Register */
182 #define MTK_CDMP_IG_CTRL 0x400
183 #define MTK_CDMP_STAG_EN BIT(0)
184 @@ -1164,6 +1170,8 @@ struct mtk_eth {
188 + struct metadata_dst *dsa_meta[MTK_MAX_DSA_PORTS];
190 struct mtk_ppe *ppe[2];
191 struct rhashtable flow_table;