7c6a3a3f8d770d1fe513a356ffd5cd835ef2e239
[openwrt/staging/dedeckeh.git] / target / linux / generic / backport-5.15 / 766-v5.18-01-net-dsa-provide-switch-operations-for-tracking-the-m.patch
1 From 295ab96f478d0fa56393e85406f19a867e26ce22 Mon Sep 17 00:00:00 2001
2 From: Vladimir Oltean <vladimir.oltean@nxp.com>
3 Date: Wed, 2 Feb 2022 01:03:20 +0100
4 Subject: [PATCH 01/16] net: dsa: provide switch operations for tracking the
5 master state
6
7 Certain drivers may need to send management traffic to the switch for
8 things like register access, FDB dump, etc, to accelerate what their
9 slow bus (SPI, I2C, MDIO) can already do.
10
11 Ethernet is faster (especially in bulk transactions) but is also more
12 unreliable, since the user may decide to bring the DSA master down (or
13 not bring it up), therefore severing the link between the host and the
14 attached switch.
15
16 Drivers needing Ethernet-based register access already should have
17 fallback logic to the slow bus if the Ethernet method fails, but that
18 fallback may be based on a timeout, and the I/O to the switch may slow
19 down to a halt if the master is down, because every Ethernet packet will
20 have to time out. The driver also doesn't have the option to turn off
21 Ethernet-based I/O momentarily, because it wouldn't know when to turn it
22 back on.
23
24 Which is where this change comes in. By tracking NETDEV_CHANGE,
25 NETDEV_UP and NETDEV_GOING_DOWN events on the DSA master, we should know
26 the exact interval of time during which this interface is reliably
27 available for traffic. Provide this information to switches so they can
28 use it as they wish.
29
30 An helper is added dsa_port_master_is_operational() to check if a master
31 port is operational.
32
33 Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
34 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
35 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
36 Signed-off-by: David S. Miller <davem@davemloft.net>
37 ---
38 include/net/dsa.h | 17 +++++++++++++++++
39 net/dsa/dsa2.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
40 net/dsa/dsa_priv.h | 13 +++++++++++++
41 net/dsa/slave.c | 32 ++++++++++++++++++++++++++++++++
42 net/dsa/switch.c | 15 +++++++++++++++
43 5 files changed, 123 insertions(+)
44
45 --- a/include/net/dsa.h
46 +++ b/include/net/dsa.h
47 @@ -291,6 +291,10 @@ struct dsa_port {
48 struct list_head mdbs;
49
50 bool setup;
51 + /* Master state bits, valid only on CPU ports */
52 + u8 master_admin_up:1;
53 + u8 master_oper_up:1;
54 +
55 };
56
57 /* TODO: ideally DSA ports would have a single dp->link_dp member,
58 @@ -456,6 +460,12 @@ static inline bool dsa_port_is_unused(st
59 return dp->type == DSA_PORT_TYPE_UNUSED;
60 }
61
62 +static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
63 +{
64 + return dsa_port_is_cpu(dp) && dp->master_admin_up &&
65 + dp->master_oper_up;
66 +}
67 +
68 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
69 {
70 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
71 @@ -949,6 +959,13 @@ struct dsa_switch_ops {
72 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
73 u16 flags);
74 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
75 +
76 + /*
77 + * DSA master tracking operations
78 + */
79 + void (*master_state_change)(struct dsa_switch *ds,
80 + const struct net_device *master,
81 + bool operational);
82 };
83
84 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
85 --- a/net/dsa/dsa2.c
86 +++ b/net/dsa/dsa2.c
87 @@ -1275,6 +1275,52 @@ out_unlock:
88 return err;
89 }
90
91 +static void dsa_tree_master_state_change(struct dsa_switch_tree *dst,
92 + struct net_device *master)
93 +{
94 + struct dsa_notifier_master_state_info info;
95 + struct dsa_port *cpu_dp = master->dsa_ptr;
96 +
97 + info.master = master;
98 + info.operational = dsa_port_master_is_operational(cpu_dp);
99 +
100 + dsa_tree_notify(dst, DSA_NOTIFIER_MASTER_STATE_CHANGE, &info);
101 +}
102 +
103 +void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
104 + struct net_device *master,
105 + bool up)
106 +{
107 + struct dsa_port *cpu_dp = master->dsa_ptr;
108 + bool notify = false;
109 +
110 + if ((dsa_port_master_is_operational(cpu_dp)) !=
111 + (up && cpu_dp->master_oper_up))
112 + notify = true;
113 +
114 + cpu_dp->master_admin_up = up;
115 +
116 + if (notify)
117 + dsa_tree_master_state_change(dst, master);
118 +}
119 +
120 +void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
121 + struct net_device *master,
122 + bool up)
123 +{
124 + struct dsa_port *cpu_dp = master->dsa_ptr;
125 + bool notify = false;
126 +
127 + if ((dsa_port_master_is_operational(cpu_dp)) !=
128 + (cpu_dp->master_admin_up && up))
129 + notify = true;
130 +
131 + cpu_dp->master_oper_up = up;
132 +
133 + if (notify)
134 + dsa_tree_master_state_change(dst, master);
135 +}
136 +
137 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
138 {
139 struct dsa_switch_tree *dst = ds->dst;
140 --- a/net/dsa/dsa_priv.h
141 +++ b/net/dsa/dsa_priv.h
142 @@ -45,6 +45,7 @@ enum {
143 DSA_NOTIFIER_MRP_DEL_RING_ROLE,
144 DSA_NOTIFIER_TAG_8021Q_VLAN_ADD,
145 DSA_NOTIFIER_TAG_8021Q_VLAN_DEL,
146 + DSA_NOTIFIER_MASTER_STATE_CHANGE,
147 };
148
149 /* DSA_NOTIFIER_AGEING_TIME */
150 @@ -127,6 +128,12 @@ struct dsa_notifier_tag_8021q_vlan_info
151 u16 vid;
152 };
153
154 +/* DSA_NOTIFIER_MASTER_STATE_CHANGE */
155 +struct dsa_notifier_master_state_info {
156 + const struct net_device *master;
157 + bool operational;
158 +};
159 +
160 struct dsa_switchdev_event_work {
161 struct dsa_switch *ds;
162 int port;
163 @@ -548,6 +555,12 @@ int dsa_tree_change_tag_proto(struct dsa
164 struct net_device *master,
165 const struct dsa_device_ops *tag_ops,
166 const struct dsa_device_ops *old_tag_ops);
167 +void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
168 + struct net_device *master,
169 + bool up);
170 +void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
171 + struct net_device *master,
172 + bool up);
173 int dsa_bridge_num_get(const struct net_device *bridge_dev, int max);
174 void dsa_bridge_num_put(const struct net_device *bridge_dev, int bridge_num);
175
176 --- a/net/dsa/slave.c
177 +++ b/net/dsa/slave.c
178 @@ -2311,6 +2311,36 @@ static int dsa_slave_netdevice_event(str
179 err = dsa_port_lag_change(dp, info->lower_state_info);
180 return notifier_from_errno(err);
181 }
182 + case NETDEV_CHANGE:
183 + case NETDEV_UP: {
184 + /* Track state of master port.
185 + * DSA driver may require the master port (and indirectly
186 + * the tagger) to be available for some special operation.
187 + */
188 + if (netdev_uses_dsa(dev)) {
189 + struct dsa_port *cpu_dp = dev->dsa_ptr;
190 + struct dsa_switch_tree *dst = cpu_dp->ds->dst;
191 +
192 + /* Track when the master port is UP */
193 + dsa_tree_master_oper_state_change(dst, dev,
194 + netif_oper_up(dev));
195 +
196 + /* Track when the master port is ready and can accept
197 + * packet.
198 + * NETDEV_UP event is not enough to flag a port as ready.
199 + * We also have to wait for linkwatch_do_dev to dev_activate
200 + * and emit a NETDEV_CHANGE event.
201 + * We check if a master port is ready by checking if the dev
202 + * have a qdisc assigned and is not noop.
203 + */
204 + dsa_tree_master_admin_state_change(dst, dev,
205 + !qdisc_tx_is_noop(dev));
206 +
207 + return NOTIFY_OK;
208 + }
209 +
210 + return NOTIFY_DONE;
211 + }
212 case NETDEV_GOING_DOWN: {
213 struct dsa_port *dp, *cpu_dp;
214 struct dsa_switch_tree *dst;
215 @@ -2322,6 +2352,8 @@ static int dsa_slave_netdevice_event(str
216 cpu_dp = dev->dsa_ptr;
217 dst = cpu_dp->ds->dst;
218
219 + dsa_tree_master_admin_state_change(dst, dev, false);
220 +
221 list_for_each_entry(dp, &dst->ports, list) {
222 if (!dsa_is_user_port(dp->ds, dp->index))
223 continue;
224 --- a/net/dsa/switch.c
225 +++ b/net/dsa/switch.c
226 @@ -722,6 +722,18 @@ dsa_switch_mrp_del_ring_role(struct dsa_
227 return 0;
228 }
229
230 +static int
231 +dsa_switch_master_state_change(struct dsa_switch *ds,
232 + struct dsa_notifier_master_state_info *info)
233 +{
234 + if (!ds->ops->master_state_change)
235 + return 0;
236 +
237 + ds->ops->master_state_change(ds, info->master, info->operational);
238 +
239 + return 0;
240 +}
241 +
242 static int dsa_switch_event(struct notifier_block *nb,
243 unsigned long event, void *info)
244 {
245 @@ -813,6 +825,9 @@ static int dsa_switch_event(struct notif
246 case DSA_NOTIFIER_TAG_8021Q_VLAN_DEL:
247 err = dsa_switch_tag_8021q_vlan_del(ds, info);
248 break;
249 + case DSA_NOTIFIER_MASTER_STATE_CHANGE:
250 + err = dsa_switch_master_state_change(ds, info);
251 + break;
252 default:
253 err = -EOPNOTSUPP;
254 break;