4ebdfda6513e61c07691d710bbcfc04582d7e4a9
[openwrt/openwrt.git] / target / linux / generic / backport-5.15 / 762-net-next-net-dsa-qca8k-add-support-for-mirror-mode.patch
1 From 2c1bdbc7e7560d7de754cad277d968d56bb1899e Mon Sep 17 00:00:00 2001
2 From: Ansuel Smith <ansuelsmth@gmail.com>
3 Date: Tue, 23 Nov 2021 03:59:10 +0100
4 Subject: net: dsa: qca8k: add support for mirror mode
5
6 The switch supports mirror mode. Only one port can set as mirror port and
7 every other port can set to both ingress and egress mode. The mirror
8 port is disabled and reverted to normal operation once every port is
9 removed from sending packet to it.
10
11 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
12 Signed-off-by: David S. Miller <davem@davemloft.net>
13 ---
14 drivers/net/dsa/qca8k.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++
15 drivers/net/dsa/qca8k.h | 4 +++
16 2 files changed, 99 insertions(+)
17
18 diff --git a/drivers/net/dsa/qca8k.c b/drivers/net/dsa/qca8k.c
19 index 67742fbd80409..bd9d756f40011 100644
20 --- a/drivers/net/dsa/qca8k.c
21 +++ b/drivers/net/dsa/qca8k.c
22 @@ -2022,6 +2022,99 @@ qca8k_port_mdb_del(struct dsa_switch *ds, int port,
23 return qca8k_fdb_search_and_del(priv, BIT(port), addr, vid);
24 }
25
26 +static int
27 +qca8k_port_mirror_add(struct dsa_switch *ds, int port,
28 + struct dsa_mall_mirror_tc_entry *mirror,
29 + bool ingress)
30 +{
31 + struct qca8k_priv *priv = ds->priv;
32 + int monitor_port, ret;
33 + u32 reg, val;
34 +
35 + /* Check for existent entry */
36 + if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
37 + return -EEXIST;
38 +
39 + ret = regmap_read(priv->regmap, QCA8K_REG_GLOBAL_FW_CTRL0, &val);
40 + if (ret)
41 + return ret;
42 +
43 + /* QCA83xx can have only one port set to mirror mode.
44 + * Check that the correct port is requested and return error otherwise.
45 + * When no mirror port is set, the values is set to 0xF
46 + */
47 + monitor_port = FIELD_GET(QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM, val);
48 + if (monitor_port != 0xF && monitor_port != mirror->to_local_port)
49 + return -EEXIST;
50 +
51 + /* Set the monitor port */
52 + val = FIELD_PREP(QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM,
53 + mirror->to_local_port);
54 + ret = regmap_update_bits(priv->regmap, QCA8K_REG_GLOBAL_FW_CTRL0,
55 + QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM, val);
56 + if (ret)
57 + return ret;
58 +
59 + if (ingress) {
60 + reg = QCA8K_PORT_LOOKUP_CTRL(port);
61 + val = QCA8K_PORT_LOOKUP_ING_MIRROR_EN;
62 + } else {
63 + reg = QCA8K_REG_PORT_HOL_CTRL1(port);
64 + val = QCA8K_PORT_HOL_CTRL1_EG_MIRROR_EN;
65 + }
66 +
67 + ret = regmap_update_bits(priv->regmap, reg, val, val);
68 + if (ret)
69 + return ret;
70 +
71 + /* Track mirror port for tx and rx to decide when the
72 + * mirror port has to be disabled.
73 + */
74 + if (ingress)
75 + priv->mirror_rx |= BIT(port);
76 + else
77 + priv->mirror_tx |= BIT(port);
78 +
79 + return 0;
80 +}
81 +
82 +static void
83 +qca8k_port_mirror_del(struct dsa_switch *ds, int port,
84 + struct dsa_mall_mirror_tc_entry *mirror)
85 +{
86 + struct qca8k_priv *priv = ds->priv;
87 + u32 reg, val;
88 + int ret;
89 +
90 + if (mirror->ingress) {
91 + reg = QCA8K_PORT_LOOKUP_CTRL(port);
92 + val = QCA8K_PORT_LOOKUP_ING_MIRROR_EN;
93 + } else {
94 + reg = QCA8K_REG_PORT_HOL_CTRL1(port);
95 + val = QCA8K_PORT_HOL_CTRL1_EG_MIRROR_EN;
96 + }
97 +
98 + ret = regmap_clear_bits(priv->regmap, reg, val);
99 + if (ret)
100 + goto err;
101 +
102 + if (mirror->ingress)
103 + priv->mirror_rx &= ~BIT(port);
104 + else
105 + priv->mirror_tx &= ~BIT(port);
106 +
107 + /* No port set to send packet to mirror port. Disable mirror port */
108 + if (!priv->mirror_rx && !priv->mirror_tx) {
109 + val = FIELD_PREP(QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM, 0xF);
110 + ret = regmap_update_bits(priv->regmap, QCA8K_REG_GLOBAL_FW_CTRL0,
111 + QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM, val);
112 + if (ret)
113 + goto err;
114 + }
115 +err:
116 + dev_err(priv->dev, "Failed to del mirror port from %d", port);
117 +}
118 +
119 static int
120 qca8k_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
121 struct netlink_ext_ack *extack)
122 @@ -2132,6 +2225,8 @@ static const struct dsa_switch_ops qca8k_switch_ops = {
123 .port_fdb_dump = qca8k_port_fdb_dump,
124 .port_mdb_add = qca8k_port_mdb_add,
125 .port_mdb_del = qca8k_port_mdb_del,
126 + .port_mirror_add = qca8k_port_mirror_add,
127 + .port_mirror_del = qca8k_port_mirror_del,
128 .port_vlan_filtering = qca8k_port_vlan_filtering,
129 .port_vlan_add = qca8k_port_vlan_add,
130 .port_vlan_del = qca8k_port_vlan_del,
131 diff --git a/drivers/net/dsa/qca8k.h b/drivers/net/dsa/qca8k.h
132 index 40ec8012622f7..7c87a968c0104 100644
133 --- a/drivers/net/dsa/qca8k.h
134 +++ b/drivers/net/dsa/qca8k.h
135 @@ -180,6 +180,7 @@
136 #define QCA8K_ATU_AGE_TIME(x) FIELD_PREP(QCA8K_ATU_AGE_TIME_MASK, (x))
137 #define QCA8K_REG_GLOBAL_FW_CTRL0 0x620
138 #define QCA8K_GLOBAL_FW_CTRL0_CPU_PORT_EN BIT(10)
139 +#define QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM GENMASK(7, 4)
140 #define QCA8K_REG_GLOBAL_FW_CTRL1 0x624
141 #define QCA8K_GLOBAL_FW_CTRL1_IGMP_DP_MASK GENMASK(30, 24)
142 #define QCA8K_GLOBAL_FW_CTRL1_BC_DP_MASK GENMASK(22, 16)
143 @@ -201,6 +202,7 @@
144 #define QCA8K_PORT_LOOKUP_STATE_LEARNING QCA8K_PORT_LOOKUP_STATE(0x3)
145 #define QCA8K_PORT_LOOKUP_STATE_FORWARD QCA8K_PORT_LOOKUP_STATE(0x4)
146 #define QCA8K_PORT_LOOKUP_LEARN BIT(20)
147 +#define QCA8K_PORT_LOOKUP_ING_MIRROR_EN BIT(25)
148
149 #define QCA8K_REG_GLOBAL_FC_THRESH 0x800
150 #define QCA8K_GLOBAL_FC_GOL_XON_THRES_MASK GENMASK(24, 16)
151 @@ -305,6 +307,8 @@ struct qca8k_ports_config {
152 struct qca8k_priv {
153 u8 switch_id;
154 u8 switch_revision;
155 + u8 mirror_rx;
156 + u8 mirror_tx;
157 bool legacy_phy_port_mapping;
158 struct qca8k_ports_config ports_config;
159 struct regmap *regmap;
160 --
161 cgit 1.2.3-1.el7
162