]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/dsa/dsa_priv.h
net: dsa: sja1105: deny more than one VLAN-aware bridge
[mirror_ubuntu-jammy-kernel.git] / net / dsa / dsa_priv.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
91da11f8
LB
2/*
3 * net/dsa/dsa_priv.h - Hardware switch handling
e84665c9 4 * Copyright (c) 2008-2009 Marvell Semiconductor
91da11f8
LB
5 */
6
7#ifndef __DSA_PRIV_H
8#define __DSA_PRIV_H
9
412a1526 10#include <linux/if_bridge.h>
91da11f8 11#include <linux/phy.h>
5075314e 12#include <linux/netdevice.h>
04ff53f9 13#include <linux/netpoll.h>
ea5dd34b 14#include <net/dsa.h>
e131a563 15#include <net/gro_cells.h>
5075314e 16
123abc06
VO
17#define DSA_MAX_NUM_OFFLOADING_BRIDGES BITS_PER_LONG
18
52c96f9d 19enum {
1faabf74 20 DSA_NOTIFIER_AGEING_TIME,
52c96f9d
VD
21 DSA_NOTIFIER_BRIDGE_JOIN,
22 DSA_NOTIFIER_BRIDGE_LEAVE,
685fb6a4
VD
23 DSA_NOTIFIER_FDB_ADD,
24 DSA_NOTIFIER_FDB_DEL,
3dc80afc
VO
25 DSA_NOTIFIER_HOST_FDB_ADD,
26 DSA_NOTIFIER_HOST_FDB_DEL,
18596f50
GM
27 DSA_NOTIFIER_HSR_JOIN,
28 DSA_NOTIFIER_HSR_LEAVE,
058102a6
TW
29 DSA_NOTIFIER_LAG_CHANGE,
30 DSA_NOTIFIER_LAG_JOIN,
31 DSA_NOTIFIER_LAG_LEAVE,
8ae5bcdc
VD
32 DSA_NOTIFIER_MDB_ADD,
33 DSA_NOTIFIER_MDB_DEL,
b8e997c4
VO
34 DSA_NOTIFIER_HOST_MDB_ADD,
35 DSA_NOTIFIER_HOST_MDB_DEL,
d0c627b8
VD
36 DSA_NOTIFIER_VLAN_ADD,
37 DSA_NOTIFIER_VLAN_DEL,
bfcb8132 38 DSA_NOTIFIER_MTU,
53da0eba 39 DSA_NOTIFIER_TAG_PROTO,
c595c433
HV
40 DSA_NOTIFIER_MRP_ADD,
41 DSA_NOTIFIER_MRP_DEL,
42 DSA_NOTIFIER_MRP_ADD_RING_ROLE,
43 DSA_NOTIFIER_MRP_DEL_RING_ROLE,
c64b9c05
VO
44 DSA_NOTIFIER_TAG_8021Q_VLAN_ADD,
45 DSA_NOTIFIER_TAG_8021Q_VLAN_DEL,
52c96f9d
VD
46};
47
1faabf74
VD
48/* DSA_NOTIFIER_AGEING_TIME */
49struct dsa_notifier_ageing_time_info {
1faabf74 50 unsigned int ageing_time;
1faabf74
VD
51};
52
52c96f9d
VD
53/* DSA_NOTIFIER_BRIDGE_* */
54struct dsa_notifier_bridge_info {
55 struct net_device *br;
f66a6a69 56 int tree_index;
52c96f9d
VD
57 int sw_index;
58 int port;
59};
60
685fb6a4
VD
61/* DSA_NOTIFIER_FDB_* */
62struct dsa_notifier_fdb_info {
685fb6a4
VD
63 int sw_index;
64 int port;
2acf4e6a
AS
65 const unsigned char *addr;
66 u16 vid;
685fb6a4
VD
67};
68
8ae5bcdc
VD
69/* DSA_NOTIFIER_MDB_* */
70struct dsa_notifier_mdb_info {
71 const struct switchdev_obj_port_mdb *mdb;
8ae5bcdc
VD
72 int sw_index;
73 int port;
74};
75
058102a6
TW
76/* DSA_NOTIFIER_LAG_* */
77struct dsa_notifier_lag_info {
78 struct net_device *lag;
79 int sw_index;
80 int port;
81
82 struct netdev_lag_upper_info *info;
83};
84
d0c627b8
VD
85/* DSA_NOTIFIER_VLAN_* */
86struct dsa_notifier_vlan_info {
87 const struct switchdev_obj_port_vlan *vlan;
d0c627b8
VD
88 int sw_index;
89 int port;
31046a5f 90 struct netlink_ext_ack *extack;
d0c627b8
VD
91};
92
bfcb8132
VO
93/* DSA_NOTIFIER_MTU */
94struct dsa_notifier_mtu_info {
88faba20 95 bool targeted_match;
bfcb8132
VO
96 int sw_index;
97 int port;
98 int mtu;
99};
100
53da0eba
VO
101/* DSA_NOTIFIER_TAG_PROTO_* */
102struct dsa_notifier_tag_proto_info {
103 const struct dsa_device_ops *tag_ops;
104};
105
c595c433
HV
106/* DSA_NOTIFIER_MRP_* */
107struct dsa_notifier_mrp_info {
108 const struct switchdev_obj_mrp *mrp;
109 int sw_index;
110 int port;
111};
112
113/* DSA_NOTIFIER_MRP_* */
114struct dsa_notifier_mrp_ring_role_info {
115 const struct switchdev_obj_ring_role_mrp *mrp;
116 int sw_index;
117 int port;
118};
119
c64b9c05
VO
120/* DSA_NOTIFIER_TAG_8021Q_VLAN_* */
121struct dsa_notifier_tag_8021q_vlan_info {
122 int tree_index;
123 int sw_index;
124 int port;
125 u16 vid;
126};
127
c4bb76a9
VO
128struct dsa_switchdev_event_work {
129 struct dsa_switch *ds;
130 int port;
4bed397c 131 struct net_device *dev;
c4bb76a9
VO
132 struct work_struct work;
133 unsigned long event;
134 /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
135 * SWITCHDEV_FDB_DEL_TO_DEVICE
136 */
137 unsigned char addr[ETH_ALEN];
138 u16 vid;
3dc80afc 139 bool host_addr;
c4bb76a9
VO
140};
141
18596f50
GM
142/* DSA_NOTIFIER_HSR_* */
143struct dsa_notifier_hsr_info {
144 struct net_device *hsr;
145 int sw_index;
146 int port;
147};
148
91da11f8 149struct dsa_slave_priv {
15240248 150 /* Copy of CPU port xmit for faster access in slave transmit hot path */
4ed70ce9 151 struct sk_buff * (*xmit)(struct sk_buff *skb,
5075314e 152 struct net_device *dev);
e84665c9 153
e131a563
AL
154 struct gro_cells gcells;
155
afdcf151
VD
156 /* DSA port data, such as switch, port index, etc. */
157 struct dsa_port *dp;
e84665c9 158
04ff53f9
FF
159#ifdef CONFIG_NET_POLL_CONTROLLER
160 struct netpoll *netpoll;
161#endif
f50f2127
FF
162
163 /* TC context */
164 struct list_head mall_tc_list;
91da11f8
LB
165};
166
91da11f8 167/* dsa.c */
c39e2a1d 168const struct dsa_device_ops *dsa_tag_driver_get(int tag_protocol);
4dad81ee 169void dsa_tag_driver_put(const struct dsa_device_ops *ops);
53da0eba 170const struct dsa_device_ops *dsa_find_tagger_by_name(const char *buf);
c39e2a1d 171
c9eb3e0f 172bool dsa_schedule_work(struct work_struct *work);
98cdb480 173const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
91da11f8 174
4e500251
VO
175static inline int dsa_tag_protocol_overhead(const struct dsa_device_ops *ops)
176{
177 return ops->needed_headroom + ops->needed_tailroom;
178}
179
f2f23566 180/* master.c */
17a22fcf
VD
181int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp);
182void dsa_master_teardown(struct net_device *dev);
f2f23566 183
2231c43b
VD
184static inline struct net_device *dsa_master_find_slave(struct net_device *dev,
185 int device, int port)
3775b1b7 186{
2f657a60
VD
187 struct dsa_port *cpu_dp = dev->dsa_ptr;
188 struct dsa_switch_tree *dst = cpu_dp->dst;
7b9a2f4b 189 struct dsa_port *dp;
3775b1b7 190
7b9a2f4b
VD
191 list_for_each_entry(dp, &dst->ports, list)
192 if (dp->ds->index == device && dp->index == port &&
193 dp->type == DSA_PORT_TYPE_USER)
194 return dp->slave;
3775b1b7 195
7b9a2f4b 196 return NULL;
3775b1b7
VD
197}
198
a40c175b 199/* port.c */
53da0eba
VO
200void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
201 const struct dsa_device_ops *tag_ops);
bae33f2b 202int dsa_port_set_state(struct dsa_port *dp, u8 state);
8640f8dc 203int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy);
fb8a6a2b 204int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy);
8640f8dc 205void dsa_port_disable_rt(struct dsa_port *dp);
75104db0 206void dsa_port_disable(struct dsa_port *dp);
2afc526a
VO
207int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
208 struct netlink_ext_ack *extack);
4e51bf44 209void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br);
cfbed329 210void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br);
058102a6
TW
211int dsa_port_lag_change(struct dsa_port *dp,
212 struct netdev_lag_lower_state_info *linfo);
213int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
2afc526a
VO
214 struct netdev_lag_upper_info *uinfo,
215 struct netlink_ext_ack *extack);
4e51bf44 216void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
058102a6 217void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
89153ed6
VO
218int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
219 struct netlink_ext_ack *extack);
54a0ed0d 220bool dsa_port_skip_vlan_configuration(struct dsa_port *dp);
bae33f2b 221int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock);
bfcb8132 222int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
88faba20 223 bool targeted_match);
2acf4e6a
AS
224int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
225 u16 vid);
226int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
227 u16 vid);
3dc80afc
VO
228int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
229 u16 vid);
230int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
231 u16 vid);
de40fc5d 232int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data);
bb9f6031 233int dsa_port_mdb_add(const struct dsa_port *dp,
ffb68fc5 234 const struct switchdev_obj_port_mdb *mdb);
bb9f6031 235int dsa_port_mdb_del(const struct dsa_port *dp,
3a9afea3 236 const struct switchdev_obj_port_mdb *mdb);
b8e997c4
VO
237int dsa_port_host_mdb_add(const struct dsa_port *dp,
238 const struct switchdev_obj_port_mdb *mdb);
239int dsa_port_host_mdb_del(const struct dsa_port *dp,
240 const struct switchdev_obj_port_mdb *mdb);
e18f4c18 241int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
a8b659e7
VO
242 struct switchdev_brport_flags flags,
243 struct netlink_ext_ack *extack);
e18f4c18 244int dsa_port_bridge_flags(const struct dsa_port *dp,
a8b659e7
VO
245 struct switchdev_brport_flags flags,
246 struct netlink_ext_ack *extack);
247int dsa_port_mrouter(struct dsa_port *dp, bool mrouter,
248 struct netlink_ext_ack *extack);
076e7133 249int dsa_port_vlan_add(struct dsa_port *dp,
31046a5f
VO
250 const struct switchdev_obj_port_vlan *vlan,
251 struct netlink_ext_ack *extack);
076e7133
VD
252int dsa_port_vlan_del(struct dsa_port *dp,
253 const struct switchdev_obj_port_vlan *vlan);
c595c433
HV
254int dsa_port_mrp_add(const struct dsa_port *dp,
255 const struct switchdev_obj_mrp *mrp);
256int dsa_port_mrp_del(const struct dsa_port *dp,
257 const struct switchdev_obj_mrp *mrp);
258int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
259 const struct switchdev_obj_ring_role_mrp *mrp);
260int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
261 const struct switchdev_obj_ring_role_mrp *mrp);
33615367
SR
262int dsa_port_link_register_of(struct dsa_port *dp);
263void dsa_port_link_unregister_of(struct dsa_port *dp);
18596f50
GM
264int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr);
265void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr);
c64b9c05
VO
266int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid);
267void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid);
77373d49 268extern const struct phylink_mac_ops dsa_port_phylink_mac_ops;
57ab1ca2 269
03cbb870 270static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
b94dc99c 271 const struct net_device *dev)
058102a6 272{
cc76ce9e 273 return dsa_port_to_bridge_port(dp) == dev;
058102a6
TW
274}
275
03cbb870 276static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
b94dc99c 277 const struct net_device *bridge_dev)
03cbb870
VO
278{
279 /* DSA ports connected to a bridge, and event was emitted
280 * for the bridge.
281 */
282 return dp->bridge_dev == bridge_dev;
283}
284
a324d3d4 285/* Returns true if any port of this tree offloads the given net_device */
03cbb870 286static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
b94dc99c 287 const struct net_device *dev)
a324d3d4
VO
288{
289 struct dsa_port *dp;
290
291 list_for_each_entry(dp, &dst->ports, list)
03cbb870 292 if (dsa_port_offloads_bridge_port(dp, dev))
a324d3d4
VO
293 return true;
294
295 return false;
296}
297
b94dc99c
VO
298/* Returns true if any port of this tree offloads the given bridge */
299static inline bool dsa_tree_offloads_bridge(struct dsa_switch_tree *dst,
300 const struct net_device *bridge_dev)
301{
302 struct dsa_port *dp;
303
304 list_for_each_entry(dp, &dst->ports, list)
305 if (dsa_port_offloads_bridge(dp, bridge_dev))
306 return true;
307
308 return false;
309}
310
91da11f8 311/* slave.c */
5075314e 312extern const struct dsa_device_ops notag_netdev_ops;
010e269f
VO
313extern struct notifier_block dsa_slave_switchdev_notifier;
314extern struct notifier_block dsa_slave_switchdev_blocking_notifier;
315
91da11f8 316void dsa_slave_mii_bus_init(struct dsa_switch *ds);
951259aa 317int dsa_slave_create(struct dsa_port *dp);
cda5c15b 318void dsa_slave_destroy(struct net_device *slave_dev);
24462549
FF
319int dsa_slave_suspend(struct net_device *slave_dev);
320int dsa_slave_resume(struct net_device *slave_dev);
88e4f0ca
VD
321int dsa_slave_register_notifier(void);
322void dsa_slave_unregister_notifier(void);
53da0eba
VO
323void dsa_slave_setup_tagger(struct net_device *slave);
324int dsa_slave_change_mtu(struct net_device *dev, int new_mtu);
91da11f8 325
d945097b
VD
326static inline struct dsa_port *dsa_slave_to_port(const struct net_device *dev)
327{
328 struct dsa_slave_priv *p = netdev_priv(dev);
329
330 return p->dp;
331}
332
d0006b00
VD
333static inline struct net_device *
334dsa_slave_to_master(const struct net_device *dev)
335{
336 struct dsa_port *dp = dsa_slave_to_port(dev);
337
f8b8b1cd 338 return dp->cpu_dp->master;
d0006b00
VD
339}
340
412a1526
VO
341/* If under a bridge with vlan_filtering=0, make sure to send pvid-tagged
342 * frames as untagged, since the bridge will not untag them.
343 */
344static inline struct sk_buff *dsa_untag_bridge_pvid(struct sk_buff *skb)
345{
346 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
412a1526
VO
347 struct net_device *br = dp->bridge_dev;
348 struct net_device *dev = skb->dev;
349 struct net_device *upper_dev;
412a1526
VO
350 u16 vid, pvid, proto;
351 int err;
352
353 if (!br || br_vlan_enabled(br))
354 return skb;
355
356 err = br_vlan_get_proto(br, &proto);
357 if (err)
358 return skb;
359
360 /* Move VLAN tag from data to hwaccel */
a348292b 361 if (!skb_vlan_tag_present(skb) && skb->protocol == htons(proto)) {
412a1526
VO
362 skb = skb_vlan_untag(skb);
363 if (!skb)
364 return NULL;
365 }
366
367 if (!skb_vlan_tag_present(skb))
368 return skb;
369
370 vid = skb_vlan_tag_get_id(skb);
371
372 /* We already run under an RCU read-side critical section since
373 * we are called from netif_receive_skb_list_internal().
374 */
375 err = br_vlan_get_pvid_rcu(dev, &pvid);
376 if (err)
377 return skb;
378
379 if (vid != pvid)
380 return skb;
381
382 /* The sad part about attempting to untag from DSA is that we
383 * don't know, unless we check, if the skb will end up in
384 * the bridge's data path - br_allowed_ingress() - or not.
385 * For example, there might be an 8021q upper for the
386 * default_pvid of the bridge, which will steal VLAN-tagged traffic
387 * from the bridge's data path. This is a configuration that DSA
388 * supports because vlan_filtering is 0. In that case, we should
389 * definitely keep the tag, to make sure it keeps working.
390 */
3a68844d
FF
391 upper_dev = __vlan_find_dev_deep_rcu(br, htons(proto), vid);
392 if (upper_dev)
393 return skb;
412a1526
VO
394
395 __vlan_hwaccel_clear_tag(skb);
396
397 return skb;
398}
399
f515f192
VD
400/* switch.c */
401int dsa_switch_register_notifier(struct dsa_switch *ds);
402void dsa_switch_unregister_notifier(struct dsa_switch *ds);
bff33f7e
VO
403
404/* dsa2.c */
058102a6
TW
405void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag);
406void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag);
886f8e26
VO
407int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v);
408int dsa_broadcast(unsigned long e, void *v);
53da0eba
VO
409int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
410 struct net_device *master,
411 const struct dsa_device_ops *tag_ops,
412 const struct dsa_device_ops *old_tag_ops);
058102a6 413
e19cc13c
VO
414/* tag_8021q.c */
415int dsa_tag_8021q_bridge_join(struct dsa_switch *ds,
416 struct dsa_notifier_bridge_info *info);
417int dsa_tag_8021q_bridge_leave(struct dsa_switch *ds,
418 struct dsa_notifier_bridge_info *info);
c64b9c05
VO
419int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds,
420 struct dsa_notifier_tag_8021q_vlan_info *info);
421int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds,
422 struct dsa_notifier_tag_8021q_vlan_info *info);
e19cc13c 423
bff33f7e
VO
424extern struct list_head dsa_tree_list;
425
91da11f8 426#endif