kernel: bump 5.4 to 5.4.31
[openwrt/openwrt.git] / target / linux / generic / backport-5.4 / 745-v5.7-net-dsa-mt7530-add-support-for-port-mirroring.patch
1 From 37feab6076aa816ed72fe836759a485353241916 Mon Sep 17 00:00:00 2001
2 From: DENG Qingfang <dqfext@gmail.com>
3 Date: Fri, 6 Mar 2020 20:35:35 +0800
4 Subject: net: dsa: mt7530: add support for port mirroring
5
6 Add support for configuring port mirroring through the cls_matchall
7 classifier. We do a full ingress and/or egress capture towards a
8 capture port.
9 MT7530 supports one monitor port and multiple mirrored ports.
10
11 Signed-off-by: DENG Qingfang <dqfext@gmail.com>
12 Signed-off-by: David S. Miller <davem@davemloft.net>
13 ---
14 drivers/net/dsa/mt7530.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
15 drivers/net/dsa/mt7530.h | 7 ++++++
16 2 files changed, 67 insertions(+)
17
18 --- a/drivers/net/dsa/mt7530.c
19 +++ b/drivers/net/dsa/mt7530.c
20 @@ -1222,6 +1222,64 @@ mt7530_port_vlan_del(struct dsa_switch *
21 return 0;
22 }
23
24 +static int mt7530_port_mirror_add(struct dsa_switch *ds, int port,
25 + struct dsa_mall_mirror_tc_entry *mirror,
26 + bool ingress)
27 +{
28 + struct mt7530_priv *priv = ds->priv;
29 + u32 val;
30 +
31 + /* Check for existent entry */
32 + if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
33 + return -EEXIST;
34 +
35 + val = mt7530_read(priv, MT7530_MFC);
36 +
37 + /* MT7530 only supports one monitor port */
38 + if (val & MIRROR_EN && MIRROR_PORT(val) != mirror->to_local_port)
39 + return -EEXIST;
40 +
41 + val |= MIRROR_EN;
42 + val &= ~MIRROR_MASK;
43 + val |= mirror->to_local_port;
44 + mt7530_write(priv, MT7530_MFC, val);
45 +
46 + val = mt7530_read(priv, MT7530_PCR_P(port));
47 + if (ingress) {
48 + val |= PORT_RX_MIR;
49 + priv->mirror_rx |= BIT(port);
50 + } else {
51 + val |= PORT_TX_MIR;
52 + priv->mirror_tx |= BIT(port);
53 + }
54 + mt7530_write(priv, MT7530_PCR_P(port), val);
55 +
56 + return 0;
57 +}
58 +
59 +static void mt7530_port_mirror_del(struct dsa_switch *ds, int port,
60 + struct dsa_mall_mirror_tc_entry *mirror)
61 +{
62 + struct mt7530_priv *priv = ds->priv;
63 + u32 val;
64 +
65 + val = mt7530_read(priv, MT7530_PCR_P(port));
66 + if (mirror->ingress) {
67 + val &= ~PORT_RX_MIR;
68 + priv->mirror_rx &= ~BIT(port);
69 + } else {
70 + val &= ~PORT_TX_MIR;
71 + priv->mirror_tx &= ~BIT(port);
72 + }
73 + mt7530_write(priv, MT7530_PCR_P(port), val);
74 +
75 + if (!priv->mirror_rx && !priv->mirror_tx) {
76 + val = mt7530_read(priv, MT7530_MFC);
77 + val &= ~MIRROR_EN;
78 + mt7530_write(priv, MT7530_MFC, val);
79 + }
80 +}
81 +
82 static enum dsa_tag_protocol
83 mtk_get_tag_protocol(struct dsa_switch *ds, int port)
84 {
85 @@ -1606,6 +1664,8 @@ static const struct dsa_switch_ops mt753
86 .port_vlan_prepare = mt7530_port_vlan_prepare,
87 .port_vlan_add = mt7530_port_vlan_add,
88 .port_vlan_del = mt7530_port_vlan_del,
89 + .port_mirror_add = mt7530_port_mirror_add,
90 + .port_mirror_del = mt7530_port_mirror_del,
91 .phylink_validate = mt7530_phylink_validate,
92 .phylink_mac_link_state = mt7530_phylink_mac_link_state,
93 .phylink_mac_config = mt7530_phylink_mac_config,
94 --- a/drivers/net/dsa/mt7530.h
95 +++ b/drivers/net/dsa/mt7530.h
96 @@ -36,6 +36,9 @@ enum {
97 #define CPU_EN BIT(7)
98 #define CPU_PORT(x) ((x) << 4)
99 #define CPU_MASK (0xf << 4)
100 +#define MIRROR_EN BIT(3)
101 +#define MIRROR_PORT(x) ((x) & 0x7)
102 +#define MIRROR_MASK 0x7
103
104 /* Registers for address table access */
105 #define MT7530_ATA1 0x74
106 @@ -141,6 +144,8 @@ enum mt7530_stp_state {
107
108 /* Register for port control */
109 #define MT7530_PCR_P(x) (0x2004 + ((x) * 0x100))
110 +#define PORT_TX_MIR BIT(9)
111 +#define PORT_RX_MIR BIT(8)
112 #define PORT_VLAN(x) ((x) & 0x3)
113
114 enum mt7530_port_mode {
115 @@ -460,6 +465,8 @@ struct mt7530_priv {
116 phy_interface_t p6_interface;
117 phy_interface_t p5_interface;
118 unsigned int p5_intf_sel;
119 + u8 mirror_rx;
120 + u8 mirror_tx;
121
122 struct mt7530_port ports[MT7530_NUM_PORTS];
123 /* protect among processes for registers access*/