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
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.
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
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
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
30 An helper is added dsa_port_master_is_operational() to check if a master
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>
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(+)
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;
51 + /* Master state bits, valid only on CPU ports */
52 + u8 master_admin_up:1;
53 + u8 master_oper_up:1;
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;
62 +static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
64 + return dsa_port_is_cpu(dp) && dp->master_admin_up &&
68 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
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,
74 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
77 + * DSA master tracking operations
79 + void (*master_state_change)(struct dsa_switch *ds,
80 + const struct net_device *master,
84 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
87 @@ -1275,6 +1275,52 @@ out_unlock:
91 +static void dsa_tree_master_state_change(struct dsa_switch_tree *dst,
92 + struct net_device *master)
94 + struct dsa_notifier_master_state_info info;
95 + struct dsa_port *cpu_dp = master->dsa_ptr;
97 + info.master = master;
98 + info.operational = dsa_port_master_is_operational(cpu_dp);
100 + dsa_tree_notify(dst, DSA_NOTIFIER_MASTER_STATE_CHANGE, &info);
103 +void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
104 + struct net_device *master,
107 + struct dsa_port *cpu_dp = master->dsa_ptr;
108 + bool notify = false;
110 + if ((dsa_port_master_is_operational(cpu_dp)) !=
111 + (up && cpu_dp->master_oper_up))
114 + cpu_dp->master_admin_up = up;
117 + dsa_tree_master_state_change(dst, master);
120 +void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
121 + struct net_device *master,
124 + struct dsa_port *cpu_dp = master->dsa_ptr;
125 + bool notify = false;
127 + if ((dsa_port_master_is_operational(cpu_dp)) !=
128 + (cpu_dp->master_admin_up && up))
131 + cpu_dp->master_oper_up = up;
134 + dsa_tree_master_state_change(dst, master);
137 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
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,
149 /* DSA_NOTIFIER_AGEING_TIME */
150 @@ -127,6 +128,12 @@ struct dsa_notifier_tag_8021q_vlan_info
154 +/* DSA_NOTIFIER_MASTER_STATE_CHANGE */
155 +struct dsa_notifier_master_state_info {
156 + const struct net_device *master;
160 struct dsa_switchdev_event_work {
161 struct dsa_switch *ds;
163 @@ -549,6 +556,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,
170 +void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
171 + struct net_device *master,
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);
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);
182 + case NETDEV_CHANGE:
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.
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;
192 + /* Track when the master port is UP */
193 + dsa_tree_master_oper_state_change(dst, dev,
194 + netif_oper_up(dev));
196 + /* Track when the master port is ready and can accept
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.
204 + dsa_tree_master_admin_state_change(dst, dev,
205 + !qdisc_tx_is_noop(dev));
210 + return NOTIFY_DONE;
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;
219 + dsa_tree_master_admin_state_change(dst, dev, false);
221 list_for_each_entry(dp, &dst->ports, list) {
222 if (!dsa_is_user_port(dp->ds, dp->index))
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_
231 +dsa_switch_master_state_change(struct dsa_switch *ds,
232 + struct dsa_notifier_master_state_info *info)
234 + if (!ds->ops->master_state_change)
237 + ds->ops->master_state_change(ds, info->master, info->operational);
242 static int dsa_switch_event(struct notifier_block *nb,
243 unsigned long event, void *info)
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);
249 + case DSA_NOTIFIER_MASTER_STATE_CHANGE:
250 + err = dsa_switch_master_state_change(ds, info);