1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * net/dsa/dsa_priv.h - Hardware switch handling
4 * Copyright (c) 2008-2009 Marvell Semiconductor
10 #include <linux/if_bridge.h>
11 #include <linux/phy.h>
12 #include <linux/netdevice.h>
13 #include <linux/netpoll.h>
15 #include <net/gro_cells.h>
18 DSA_NOTIFIER_AGEING_TIME,
19 DSA_NOTIFIER_BRIDGE_JOIN,
20 DSA_NOTIFIER_BRIDGE_LEAVE,
23 DSA_NOTIFIER_LAG_CHANGE,
24 DSA_NOTIFIER_LAG_JOIN,
25 DSA_NOTIFIER_LAG_LEAVE,
28 DSA_NOTIFIER_VLAN_ADD,
29 DSA_NOTIFIER_VLAN_DEL,
31 DSA_NOTIFIER_TAG_PROTO,
34 /* DSA_NOTIFIER_AGEING_TIME */
35 struct dsa_notifier_ageing_time_info {
36 unsigned int ageing_time;
39 /* DSA_NOTIFIER_BRIDGE_* */
40 struct dsa_notifier_bridge_info {
41 struct net_device *br;
47 /* DSA_NOTIFIER_FDB_* */
48 struct dsa_notifier_fdb_info {
51 const unsigned char *addr;
55 /* DSA_NOTIFIER_MDB_* */
56 struct dsa_notifier_mdb_info {
57 const struct switchdev_obj_port_mdb *mdb;
62 /* DSA_NOTIFIER_LAG_* */
63 struct dsa_notifier_lag_info {
64 struct net_device *lag;
68 struct netdev_lag_upper_info *info;
71 /* DSA_NOTIFIER_VLAN_* */
72 struct dsa_notifier_vlan_info {
73 const struct switchdev_obj_port_vlan *vlan;
78 /* DSA_NOTIFIER_MTU */
79 struct dsa_notifier_mtu_info {
80 bool propagate_upstream;
86 /* DSA_NOTIFIER_TAG_PROTO_* */
87 struct dsa_notifier_tag_proto_info {
88 const struct dsa_device_ops *tag_ops;
91 struct dsa_switchdev_event_work {
92 struct dsa_switch *ds;
94 struct work_struct work;
96 /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
97 * SWITCHDEV_FDB_DEL_TO_DEVICE
99 unsigned char addr[ETH_ALEN];
103 struct dsa_slave_priv {
104 /* Copy of CPU port xmit for faster access in slave transmit hot path */
105 struct sk_buff * (*xmit)(struct sk_buff *skb,
106 struct net_device *dev);
108 struct gro_cells gcells;
110 /* DSA port data, such as switch, port index, etc. */
113 #ifdef CONFIG_NET_POLL_CONTROLLER
114 struct netpoll *netpoll;
118 struct list_head mall_tc_list;
122 const struct dsa_device_ops *dsa_tag_driver_get(int tag_protocol);
123 void dsa_tag_driver_put(const struct dsa_device_ops *ops);
124 const struct dsa_device_ops *dsa_find_tagger_by_name(const char *buf);
126 bool dsa_schedule_work(struct work_struct *work);
127 const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
130 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp);
131 void dsa_master_teardown(struct net_device *dev);
133 static inline struct net_device *dsa_master_find_slave(struct net_device *dev,
134 int device, int port)
136 struct dsa_port *cpu_dp = dev->dsa_ptr;
137 struct dsa_switch_tree *dst = cpu_dp->dst;
140 list_for_each_entry(dp, &dst->ports, list)
141 if (dp->ds->index == device && dp->index == port &&
142 dp->type == DSA_PORT_TYPE_USER)
149 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
150 const struct dsa_device_ops *tag_ops);
151 int dsa_port_set_state(struct dsa_port *dp, u8 state);
152 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy);
153 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy);
154 void dsa_port_disable_rt(struct dsa_port *dp);
155 void dsa_port_disable(struct dsa_port *dp);
156 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br);
157 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br);
158 int dsa_port_lag_change(struct dsa_port *dp,
159 struct netdev_lag_lower_state_info *linfo);
160 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
161 struct netdev_lag_upper_info *uinfo);
162 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
163 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering);
164 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp);
165 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock);
166 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
167 bool propagate_upstream);
168 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
170 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
172 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data);
173 int dsa_port_mdb_add(const struct dsa_port *dp,
174 const struct switchdev_obj_port_mdb *mdb);
175 int dsa_port_mdb_del(const struct dsa_port *dp,
176 const struct switchdev_obj_port_mdb *mdb);
177 int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags);
178 int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags);
179 int dsa_port_mrouter(struct dsa_port *dp, bool mrouter);
180 int dsa_port_vlan_add(struct dsa_port *dp,
181 const struct switchdev_obj_port_vlan *vlan);
182 int dsa_port_vlan_del(struct dsa_port *dp,
183 const struct switchdev_obj_port_vlan *vlan);
184 int dsa_port_link_register_of(struct dsa_port *dp);
185 void dsa_port_link_unregister_of(struct dsa_port *dp);
186 extern const struct phylink_mac_ops dsa_port_phylink_mac_ops;
188 static inline bool dsa_port_offloads_netdev(struct dsa_port *dp,
189 struct net_device *dev)
191 /* Switchdev offloading can be configured on: */
193 if (dev == dp->slave)
194 /* DSA ports directly connected to a bridge, and event
195 * was emitted for the ports themselves.
199 if (dp->bridge_dev == dev)
200 /* DSA ports connected to a bridge, and event was emitted
205 if (dp->lag_dev == dev)
206 /* DSA ports connected to a bridge via a LAG */
212 /* Returns true if any port of this tree offloads the given net_device */
213 static inline bool dsa_tree_offloads_netdev(struct dsa_switch_tree *dst,
214 struct net_device *dev)
218 list_for_each_entry(dp, &dst->ports, list)
219 if (dsa_port_offloads_netdev(dp, dev))
226 extern const struct dsa_device_ops notag_netdev_ops;
227 void dsa_slave_mii_bus_init(struct dsa_switch *ds);
228 int dsa_slave_create(struct dsa_port *dp);
229 void dsa_slave_destroy(struct net_device *slave_dev);
230 int dsa_slave_suspend(struct net_device *slave_dev);
231 int dsa_slave_resume(struct net_device *slave_dev);
232 int dsa_slave_register_notifier(void);
233 void dsa_slave_unregister_notifier(void);
234 void dsa_slave_setup_tagger(struct net_device *slave);
235 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu);
237 static inline struct dsa_port *dsa_slave_to_port(const struct net_device *dev)
239 struct dsa_slave_priv *p = netdev_priv(dev);
244 static inline struct net_device *
245 dsa_slave_to_master(const struct net_device *dev)
247 struct dsa_port *dp = dsa_slave_to_port(dev);
249 return dp->cpu_dp->master;
252 /* If under a bridge with vlan_filtering=0, make sure to send pvid-tagged
253 * frames as untagged, since the bridge will not untag them.
255 static inline struct sk_buff *dsa_untag_bridge_pvid(struct sk_buff *skb)
257 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
258 struct net_device *br = dp->bridge_dev;
259 struct net_device *dev = skb->dev;
260 struct net_device *upper_dev;
261 u16 vid, pvid, proto;
264 if (!br || br_vlan_enabled(br))
267 err = br_vlan_get_proto(br, &proto);
271 /* Move VLAN tag from data to hwaccel */
272 if (!skb_vlan_tag_present(skb) && skb->protocol == htons(proto)) {
273 skb = skb_vlan_untag(skb);
278 if (!skb_vlan_tag_present(skb))
281 vid = skb_vlan_tag_get_id(skb);
283 /* We already run under an RCU read-side critical section since
284 * we are called from netif_receive_skb_list_internal().
286 err = br_vlan_get_pvid_rcu(dev, &pvid);
293 /* The sad part about attempting to untag from DSA is that we
294 * don't know, unless we check, if the skb will end up in
295 * the bridge's data path - br_allowed_ingress() - or not.
296 * For example, there might be an 8021q upper for the
297 * default_pvid of the bridge, which will steal VLAN-tagged traffic
298 * from the bridge's data path. This is a configuration that DSA
299 * supports because vlan_filtering is 0. In that case, we should
300 * definitely keep the tag, to make sure it keeps working.
302 upper_dev = __vlan_find_dev_deep_rcu(br, htons(proto), vid);
306 __vlan_hwaccel_clear_tag(skb);
312 int dsa_switch_register_notifier(struct dsa_switch *ds);
313 void dsa_switch_unregister_notifier(struct dsa_switch *ds);
316 void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag);
317 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag);
318 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v);
319 int dsa_broadcast(unsigned long e, void *v);
320 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
321 struct net_device *master,
322 const struct dsa_device_ops *tag_ops,
323 const struct dsa_device_ops *old_tag_ops);
325 extern struct list_head dsa_tree_list;