blob: 71e9707d11d48b70ed249635020fe74d497267c1 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00002/*
3 * net/dsa/dsa_priv.h - Hardware switch handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00004 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00005 */
6
7#ifndef __DSA_PRIV_H
8#define __DSA_PRIV_H
9
Vladimir Oltean412a1522020-09-23 14:40:37 -070010#include <linux/if_bridge.h>
Jakub Kicinskib6459412021-12-28 16:49:13 -080011#include <linux/if_vlan.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000012#include <linux/phy.h>
Alexander Duyck50753142014-09-15 13:00:19 -040013#include <linux/netdevice.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070014#include <linux/netpoll.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040015#include <net/dsa.h>
Alexander Lobakine131a562020-04-21 16:41:08 +030016#include <net/gro_cells.h>
Alexander Duyck50753142014-09-15 13:00:19 -040017
Vladimir Oltean123abc062021-07-22 18:55:40 +030018#define DSA_MAX_NUM_OFFLOADING_BRIDGES BITS_PER_LONG
19
Vivien Didelot52c96f92017-05-19 17:00:51 -040020enum {
Vivien Didelot1faabf72017-05-19 17:00:52 -040021 DSA_NOTIFIER_AGEING_TIME,
Vivien Didelot52c96f92017-05-19 17:00:51 -040022 DSA_NOTIFIER_BRIDGE_JOIN,
23 DSA_NOTIFIER_BRIDGE_LEAVE,
Vivien Didelot685fb6a2017-05-19 17:00:53 -040024 DSA_NOTIFIER_FDB_ADD,
25 DSA_NOTIFIER_FDB_DEL,
Vladimir Oltean3dc80af2021-06-29 17:06:51 +030026 DSA_NOTIFIER_HOST_FDB_ADD,
27 DSA_NOTIFIER_HOST_FDB_DEL,
Vladimir Olteane212fa72022-02-23 16:00:53 +020028 DSA_NOTIFIER_LAG_FDB_ADD,
29 DSA_NOTIFIER_LAG_FDB_DEL,
Tobias Waldekranz058102a62021-01-13 09:42:53 +010030 DSA_NOTIFIER_LAG_CHANGE,
31 DSA_NOTIFIER_LAG_JOIN,
32 DSA_NOTIFIER_LAG_LEAVE,
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -040033 DSA_NOTIFIER_MDB_ADD,
34 DSA_NOTIFIER_MDB_DEL,
Vladimir Olteanb8e997c2021-06-29 17:06:49 +030035 DSA_NOTIFIER_HOST_MDB_ADD,
36 DSA_NOTIFIER_HOST_MDB_DEL,
Vivien Didelotd0c627b2017-05-19 17:00:55 -040037 DSA_NOTIFIER_VLAN_ADD,
38 DSA_NOTIFIER_VLAN_DEL,
Vladimir Oltean134ef232022-02-15 19:02:17 +020039 DSA_NOTIFIER_HOST_VLAN_ADD,
40 DSA_NOTIFIER_HOST_VLAN_DEL,
Vladimir Olteanbfcb8132020-03-27 21:55:42 +020041 DSA_NOTIFIER_MTU,
Vladimir Oltean53da0eb2021-01-29 03:00:06 +020042 DSA_NOTIFIER_TAG_PROTO,
Vladimir Olteandc452a42021-12-10 01:34:37 +020043 DSA_NOTIFIER_TAG_PROTO_CONNECT,
Vladimir Oltean7f297312021-12-14 03:45:36 +020044 DSA_NOTIFIER_TAG_PROTO_DISCONNECT,
Vladimir Olteanc64b9c02021-07-19 20:14:52 +030045 DSA_NOTIFIER_TAG_8021Q_VLAN_ADD,
46 DSA_NOTIFIER_TAG_8021Q_VLAN_DEL,
Vladimir Oltean295ab962022-02-02 01:03:20 +010047 DSA_NOTIFIER_MASTER_STATE_CHANGE,
Vivien Didelot52c96f92017-05-19 17:00:51 -040048};
49
Vivien Didelot1faabf72017-05-19 17:00:52 -040050/* DSA_NOTIFIER_AGEING_TIME */
51struct dsa_notifier_ageing_time_info {
Vivien Didelot1faabf72017-05-19 17:00:52 -040052 unsigned int ageing_time;
Vivien Didelot1faabf72017-05-19 17:00:52 -040053};
54
Vivien Didelot52c96f92017-05-19 17:00:51 -040055/* DSA_NOTIFIER_BRIDGE_* */
56struct dsa_notifier_bridge_info {
Vladimir Oltean726816a2022-04-15 18:46:22 +030057 const struct dsa_port *dp;
Vladimir Olteand3eed0e2021-12-06 18:57:56 +020058 struct dsa_bridge bridge;
Vladimir Olteanb0799222021-12-06 18:57:57 +020059 bool tx_fwd_offload;
Vladimir Oltean06b9cce2022-02-25 11:22:23 +020060 struct netlink_ext_ack *extack;
Vivien Didelot52c96f92017-05-19 17:00:51 -040061};
62
Vivien Didelot685fb6a2017-05-19 17:00:53 -040063/* DSA_NOTIFIER_FDB_* */
64struct dsa_notifier_fdb_info {
Vladimir Oltean726816a2022-04-15 18:46:22 +030065 const struct dsa_port *dp;
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +030066 const unsigned char *addr;
67 u16 vid;
Vladimir Olteanc2693362022-02-25 11:22:22 +020068 struct dsa_db db;
Vivien Didelot685fb6a2017-05-19 17:00:53 -040069};
70
Vladimir Olteane212fa72022-02-23 16:00:53 +020071/* DSA_NOTIFIER_LAG_FDB_* */
72struct dsa_notifier_lag_fdb_info {
73 struct dsa_lag *lag;
74 const unsigned char *addr;
75 u16 vid;
Vladimir Olteanc2693362022-02-25 11:22:22 +020076 struct dsa_db db;
Vladimir Olteane212fa72022-02-23 16:00:53 +020077};
78
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -040079/* DSA_NOTIFIER_MDB_* */
80struct dsa_notifier_mdb_info {
Vladimir Oltean726816a2022-04-15 18:46:22 +030081 const struct dsa_port *dp;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -040082 const struct switchdev_obj_port_mdb *mdb;
Vladimir Olteanc2693362022-02-25 11:22:22 +020083 struct dsa_db db;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -040084};
85
Tobias Waldekranz058102a62021-01-13 09:42:53 +010086/* DSA_NOTIFIER_LAG_* */
87struct dsa_notifier_lag_info {
Vladimir Oltean726816a2022-04-15 18:46:22 +030088 const struct dsa_port *dp;
Vladimir Olteandedd6a02022-02-23 16:00:49 +020089 struct dsa_lag lag;
Tobias Waldekranz058102a62021-01-13 09:42:53 +010090 struct netdev_lag_upper_info *info;
Vladimir Oltean2e359b02022-09-11 04:07:03 +030091 struct netlink_ext_ack *extack;
Tobias Waldekranz058102a62021-01-13 09:42:53 +010092};
93
Vivien Didelotd0c627b2017-05-19 17:00:55 -040094/* DSA_NOTIFIER_VLAN_* */
95struct dsa_notifier_vlan_info {
Vladimir Oltean726816a2022-04-15 18:46:22 +030096 const struct dsa_port *dp;
Vivien Didelotd0c627b2017-05-19 17:00:55 -040097 const struct switchdev_obj_port_vlan *vlan;
Vladimir Oltean31046a52021-02-13 22:43:18 +020098 struct netlink_ext_ack *extack;
Vivien Didelotd0c627b2017-05-19 17:00:55 -040099};
100
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200101/* DSA_NOTIFIER_MTU */
102struct dsa_notifier_mtu_info {
Vladimir Oltean726816a2022-04-15 18:46:22 +0300103 const struct dsa_port *dp;
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200104 int mtu;
105};
106
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200107/* DSA_NOTIFIER_TAG_PROTO_* */
108struct dsa_notifier_tag_proto_info {
109 const struct dsa_device_ops *tag_ops;
110};
111
Vladimir Olteanc64b9c02021-07-19 20:14:52 +0300112/* DSA_NOTIFIER_TAG_8021Q_VLAN_* */
113struct dsa_notifier_tag_8021q_vlan_info {
Vladimir Oltean726816a2022-04-15 18:46:22 +0300114 const struct dsa_port *dp;
Vladimir Olteanc64b9c02021-07-19 20:14:52 +0300115 u16 vid;
116};
117
Vladimir Oltean295ab962022-02-02 01:03:20 +0100118/* DSA_NOTIFIER_MASTER_STATE_CHANGE */
119struct dsa_notifier_master_state_info {
120 const struct net_device *master;
121 bool operational;
122};
123
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +0200124struct dsa_switchdev_event_work {
Vladimir Oltean4bed3972021-06-29 17:06:57 +0300125 struct net_device *dev;
Vladimir Oltean93c79822022-02-23 16:00:52 +0200126 struct net_device *orig_dev;
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +0200127 struct work_struct work;
128 unsigned long event;
129 /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
130 * SWITCHDEV_FDB_DEL_TO_DEVICE
131 */
132 unsigned char addr[ETH_ALEN];
133 u16 vid;
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300134 bool host_addr;
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +0200135};
136
Vladimir Oltean5e8a1e02022-03-02 21:14:10 +0200137enum dsa_standalone_event {
138 DSA_UC_ADD,
139 DSA_UC_DEL,
140 DSA_MC_ADD,
141 DSA_MC_DEL,
142};
143
144struct dsa_standalone_event_work {
145 struct work_struct work;
146 struct net_device *dev;
147 enum dsa_standalone_event event;
148 unsigned char addr[ETH_ALEN];
149 u16 vid;
150};
151
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000152struct dsa_slave_priv {
Vivien Didelot15240242017-09-29 17:19:18 -0400153 /* Copy of CPU port xmit for faster access in slave transmit hot path */
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700154 struct sk_buff * (*xmit)(struct sk_buff *skb,
Alexander Duyck50753142014-09-15 13:00:19 -0400155 struct net_device *dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000156
Alexander Lobakine131a562020-04-21 16:41:08 +0300157 struct gro_cells gcells;
158
Vivien Didelotafdcf152017-01-27 15:29:39 -0500159 /* DSA port data, such as switch, port index, etc. */
160 struct dsa_port *dp;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000161
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700162#ifdef CONFIG_NET_POLL_CONTROLLER
163 struct netpoll *netpoll;
164#endif
Florian Fainellif50f2122017-01-30 12:41:40 -0800165
166 /* TC context */
167 struct list_head mall_tc_list;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000168};
169
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000170/* dsa.c */
Andrew Lunnc39e2a12019-04-28 19:37:18 +0200171const struct dsa_device_ops *dsa_tag_driver_get(int tag_protocol);
Andrew Lunn4dad81e2019-04-28 19:37:19 +0200172void dsa_tag_driver_put(const struct dsa_device_ops *ops);
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200173const struct dsa_device_ops *dsa_find_tagger_by_name(const char *buf);
Andrew Lunnc39e2a12019-04-28 19:37:18 +0200174
Vladimir Oltean7e580492022-03-08 11:15:15 +0200175bool dsa_db_equal(const struct dsa_db *a, const struct dsa_db *b);
176
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +0300177bool dsa_schedule_work(struct work_struct *work);
Florian Fainelli98cdb482018-09-07 11:09:02 -0700178const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000179
Vladimir Oltean4e500252021-06-11 22:01:24 +0300180static inline int dsa_tag_protocol_overhead(const struct dsa_device_ops *ops)
181{
182 return ops->needed_headroom + ops->needed_tailroom;
183}
184
Vivien Didelotf2f23562017-09-19 11:57:00 -0400185/* master.c */
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500186int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp);
187void dsa_master_teardown(struct net_device *dev);
Vladimir Olteanacc43b72022-09-11 04:07:04 +0300188int dsa_master_lag_setup(struct net_device *lag_dev, struct dsa_port *cpu_dp,
189 struct netdev_lag_upper_info *uinfo,
190 struct netlink_ext_ack *extack);
191void dsa_master_lag_teardown(struct net_device *lag_dev,
192 struct dsa_port *cpu_dp);
Vivien Didelotf2f23562017-09-19 11:57:00 -0400193
Vivien Didelot2231c432017-10-16 11:12:17 -0400194static inline struct net_device *dsa_master_find_slave(struct net_device *dev,
195 int device, int port)
Vivien Didelot3775b1b2017-09-29 17:19:15 -0400196{
Vivien Didelot2f657a62017-09-29 17:19:20 -0400197 struct dsa_port *cpu_dp = dev->dsa_ptr;
198 struct dsa_switch_tree *dst = cpu_dp->dst;
Vivien Didelot7b9a2f42019-10-21 16:51:18 -0400199 struct dsa_port *dp;
Vivien Didelot3775b1b2017-09-29 17:19:15 -0400200
Vivien Didelot7b9a2f42019-10-21 16:51:18 -0400201 list_for_each_entry(dp, &dst->ports, list)
202 if (dp->ds->index == device && dp->index == port &&
203 dp->type == DSA_PORT_TYPE_USER)
204 return dp->slave;
Vivien Didelot3775b1b2017-09-29 17:19:15 -0400205
Vivien Didelot7b9a2f42019-10-21 16:51:18 -0400206 return NULL;
Vivien Didelot3775b1b2017-09-29 17:19:15 -0400207}
208
Vladimir Oltean95f510d2022-09-11 04:06:59 +0300209/* netlink.c */
210extern struct rtnl_link_ops dsa_link_ops __read_mostly;
211
Vivien Didelota40c1752017-05-19 17:00:44 -0400212/* port.c */
Vladimir Olteaned1fe1b2022-11-11 23:10:20 +0200213bool dsa_port_supports_hwtstamp(struct dsa_port *dp, struct ifreq *ifr);
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200214void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
215 const struct dsa_device_ops *tag_ops);
Vladimir Oltean39f32102021-08-08 14:16:37 +0300216int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age);
Tobias Waldekranz7414af32022-03-16 16:08:54 +0100217int dsa_port_set_mst_state(struct dsa_port *dp,
218 const struct switchdev_mst_state *state,
219 struct netlink_ext_ack *extack);
Russell King8640f8d2020-03-03 15:01:46 +0000220int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -0400221int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy);
Russell King8640f8d2020-03-03 15:01:46 +0000222void dsa_port_disable_rt(struct dsa_port *dp);
Andrew Lunn75104db2019-02-24 20:44:43 +0100223void dsa_port_disable(struct dsa_port *dp);
Vladimir Oltean2afc5262021-03-23 01:51:48 +0200224int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
225 struct netlink_ext_ack *extack);
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300226void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br);
Vivien Didelotcfbed322017-05-19 17:00:45 -0400227void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br);
Tobias Waldekranz058102a62021-01-13 09:42:53 +0100228int dsa_port_lag_change(struct dsa_port *dp,
229 struct netdev_lag_lower_state_info *linfo);
230int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
Vladimir Oltean2afc5262021-03-23 01:51:48 +0200231 struct netdev_lag_upper_info *uinfo,
232 struct netlink_ext_ack *extack);
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300233void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
Tobias Waldekranz058102a62021-01-13 09:42:53 +0100234void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200235int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
236 struct netlink_ext_ack *extack);
Russell King54a0ed02020-05-12 20:20:25 +0300237bool dsa_port_skip_vlan_configuration(struct dsa_port *dp);
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200238int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock);
Tobias Waldekranz332afc42022-03-16 16:08:52 +0100239int dsa_port_mst_enable(struct dsa_port *dp, bool on,
240 struct netlink_ext_ack *extack);
Tobias Waldekranz8e6598a72022-03-16 16:08:53 +0100241int dsa_port_vlan_msti(struct dsa_port *dp,
242 const struct switchdev_vlan_msti *msti);
Vladimir Olteanbe6ff9662022-04-15 18:46:26 +0300243int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu);
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300244int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
245 u16 vid);
246int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
247 u16 vid);
Vladimir Oltean5e8a1e02022-03-02 21:14:10 +0200248int dsa_port_standalone_host_fdb_add(struct dsa_port *dp,
249 const unsigned char *addr, u16 vid);
250int dsa_port_standalone_host_fdb_del(struct dsa_port *dp,
251 const unsigned char *addr, u16 vid);
Vladimir Oltean68d6d712022-03-02 21:14:09 +0200252int dsa_port_bridge_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
253 u16 vid);
254int dsa_port_bridge_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
255 u16 vid);
Vladimir Olteane212fa72022-02-23 16:00:53 +0200256int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
257 u16 vid);
258int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
259 u16 vid);
Vivien Didelotde40fc52017-09-20 19:32:14 -0400260int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data);
Andrew Lunnbb9f603172017-11-09 23:11:01 +0100261int dsa_port_mdb_add(const struct dsa_port *dp,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200262 const struct switchdev_obj_port_mdb *mdb);
Andrew Lunnbb9f603172017-11-09 23:11:01 +0100263int dsa_port_mdb_del(const struct dsa_port *dp,
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400264 const struct switchdev_obj_port_mdb *mdb);
Vladimir Oltean5e8a1e02022-03-02 21:14:10 +0200265int dsa_port_standalone_host_mdb_add(const struct dsa_port *dp,
266 const struct switchdev_obj_port_mdb *mdb);
267int dsa_port_standalone_host_mdb_del(const struct dsa_port *dp,
268 const struct switchdev_obj_port_mdb *mdb);
Vladimir Oltean68d6d712022-03-02 21:14:09 +0200269int dsa_port_bridge_host_mdb_add(const struct dsa_port *dp,
270 const struct switchdev_obj_port_mdb *mdb);
271int dsa_port_bridge_host_mdb_del(const struct dsa_port *dp,
272 const struct switchdev_obj_port_mdb *mdb);
Vladimir Olteane18f4c12021-02-12 17:15:55 +0200273int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200274 struct switchdev_brport_flags flags,
275 struct netlink_ext_ack *extack);
Vladimir Oltean045c45d2021-08-08 17:35:23 +0300276int dsa_port_bridge_flags(struct dsa_port *dp,
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200277 struct switchdev_brport_flags flags,
278 struct netlink_ext_ack *extack);
Vivien Didelot076e7132017-05-19 17:00:50 -0400279int dsa_port_vlan_add(struct dsa_port *dp,
Vladimir Oltean31046a52021-02-13 22:43:18 +0200280 const struct switchdev_obj_port_vlan *vlan,
281 struct netlink_ext_ack *extack);
Vivien Didelot076e7132017-05-19 17:00:50 -0400282int dsa_port_vlan_del(struct dsa_port *dp,
283 const struct switchdev_obj_port_vlan *vlan);
Vladimir Oltean134ef232022-02-15 19:02:17 +0200284int dsa_port_host_vlan_add(struct dsa_port *dp,
285 const struct switchdev_obj_port_vlan *vlan,
286 struct netlink_ext_ack *extack);
287int dsa_port_host_vlan_del(struct dsa_port *dp,
288 const struct switchdev_obj_port_vlan *vlan);
Horatiu Vulturc595c432021-02-16 22:42:04 +0100289int dsa_port_mrp_add(const struct dsa_port *dp,
290 const struct switchdev_obj_mrp *mrp);
291int dsa_port_mrp_del(const struct dsa_port *dp,
292 const struct switchdev_obj_mrp *mrp);
293int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
294 const struct switchdev_obj_ring_role_mrp *mrp);
295int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
296 const struct switchdev_obj_ring_role_mrp *mrp);
Russell King (Oracle)21bd64b2021-11-30 13:09:55 +0000297int dsa_port_phylink_create(struct dsa_port *dp);
Vladimir Olteancf5ca4d2022-09-29 09:29:00 +0200298void dsa_port_phylink_destroy(struct dsa_port *dp);
Vladimir Oltean770375f2022-08-18 14:54:59 +0300299int dsa_shared_port_link_register_of(struct dsa_port *dp);
300void dsa_shared_port_link_unregister_of(struct dsa_port *dp);
George McCollister18596f52021-02-09 19:02:12 -0600301int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr);
302void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr);
Vladimir Oltean724395f2021-08-11 16:46:06 +0300303int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast);
304void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast);
Vladimir Oltean72c3b0c2022-05-11 12:50:17 +0300305void dsa_port_set_host_flood(struct dsa_port *dp, bool uc, bool mc);
Vladimir Oltean95f510d2022-09-11 04:06:59 +0300306int dsa_port_change_master(struct dsa_port *dp, struct net_device *master,
307 struct netlink_ext_ack *extack);
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400308
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000309/* slave.c */
Alexander Duyck50753142014-09-15 13:00:19 -0400310extern const struct dsa_device_ops notag_netdev_ops;
Vladimir Oltean010e2692021-03-23 01:51:50 +0200311extern struct notifier_block dsa_slave_switchdev_notifier;
312extern struct notifier_block dsa_slave_switchdev_blocking_notifier;
313
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000314void dsa_slave_mii_bus_init(struct dsa_switch *ds);
Vivien Didelot951259aa2017-10-27 15:55:19 -0400315int dsa_slave_create(struct dsa_port *dp);
Neil Armstrongcda5c152015-12-07 13:57:35 +0100316void dsa_slave_destroy(struct net_device *slave_dev);
Florian Fainelli24462542014-09-18 17:31:22 -0700317int dsa_slave_suspend(struct net_device *slave_dev);
318int dsa_slave_resume(struct net_device *slave_dev);
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500319int dsa_slave_register_notifier(void);
320void dsa_slave_unregister_notifier(void);
Vladimir Oltean95f510d2022-09-11 04:06:59 +0300321void dsa_slave_sync_ha(struct net_device *dev);
322void dsa_slave_unsync_ha(struct net_device *dev);
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200323void dsa_slave_setup_tagger(struct net_device *slave);
324int dsa_slave_change_mtu(struct net_device *dev, int new_mtu);
Vladimir Oltean95f510d2022-09-11 04:06:59 +0300325int dsa_slave_change_master(struct net_device *dev, struct net_device *master,
326 struct netlink_ext_ack *extack);
Vladimir Oltean06cfb2d2021-08-24 00:22:57 +0300327int dsa_slave_manage_vlan_filtering(struct net_device *dev,
328 bool vlan_filtering);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000329
Vivien Didelotd9450972017-10-16 11:12:15 -0400330static inline struct dsa_port *dsa_slave_to_port(const struct net_device *dev)
331{
332 struct dsa_slave_priv *p = netdev_priv(dev);
333
334 return p->dp;
335}
336
Vivien Didelotd0006b02017-10-16 11:12:16 -0400337static inline struct net_device *
338dsa_slave_to_master(const struct net_device *dev)
339{
340 struct dsa_port *dp = dsa_slave_to_port(dev);
341
Vladimir Oltean8f6a19c2022-09-11 04:06:58 +0300342 return dsa_port_to_master(dp);
Vivien Didelotd0006b02017-10-16 11:12:16 -0400343}
344
Vladimir Oltean412a1522020-09-23 14:40:37 -0700345/* If under a bridge with vlan_filtering=0, make sure to send pvid-tagged
346 * frames as untagged, since the bridge will not untag them.
347 */
348static inline struct sk_buff *dsa_untag_bridge_pvid(struct sk_buff *skb)
349{
350 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
Vladimir Oltean36cbf392021-12-06 18:57:52 +0200351 struct net_device *br = dsa_port_bridge_dev_get(dp);
Vladimir Oltean412a1522020-09-23 14:40:37 -0700352 struct net_device *dev = skb->dev;
353 struct net_device *upper_dev;
Vladimir Oltean412a1522020-09-23 14:40:37 -0700354 u16 vid, pvid, proto;
355 int err;
356
357 if (!br || br_vlan_enabled(br))
358 return skb;
359
360 err = br_vlan_get_proto(br, &proto);
361 if (err)
362 return skb;
363
364 /* Move VLAN tag from data to hwaccel */
Florian Fainellia3482922020-10-01 19:42:14 -0700365 if (!skb_vlan_tag_present(skb) && skb->protocol == htons(proto)) {
Vladimir Oltean412a1522020-09-23 14:40:37 -0700366 skb = skb_vlan_untag(skb);
367 if (!skb)
368 return NULL;
369 }
370
371 if (!skb_vlan_tag_present(skb))
372 return skb;
373
374 vid = skb_vlan_tag_get_id(skb);
375
376 /* We already run under an RCU read-side critical section since
377 * we are called from netif_receive_skb_list_internal().
378 */
379 err = br_vlan_get_pvid_rcu(dev, &pvid);
380 if (err)
381 return skb;
382
383 if (vid != pvid)
384 return skb;
385
386 /* The sad part about attempting to untag from DSA is that we
387 * don't know, unless we check, if the skb will end up in
388 * the bridge's data path - br_allowed_ingress() - or not.
389 * For example, there might be an 8021q upper for the
390 * default_pvid of the bridge, which will steal VLAN-tagged traffic
391 * from the bridge's data path. This is a configuration that DSA
392 * supports because vlan_filtering is 0. In that case, we should
393 * definitely keep the tag, to make sure it keeps working.
394 */
Florian Fainelli3a688442020-10-01 19:42:15 -0700395 upper_dev = __vlan_find_dev_deep_rcu(br, htons(proto), vid);
396 if (upper_dev)
397 return skb;
Vladimir Oltean412a1522020-09-23 14:40:37 -0700398
399 __vlan_hwaccel_clear_tag(skb);
400
401 return skb;
402}
403
Vladimir Oltean884be122021-07-26 19:55:34 +0300404/* For switches without hardware support for DSA tagging to be able
405 * to support termination through the bridge.
406 */
407static inline struct net_device *
408dsa_find_designated_bridge_port_by_vid(struct net_device *master, u16 vid)
409{
410 struct dsa_port *cpu_dp = master->dsa_ptr;
411 struct dsa_switch_tree *dst = cpu_dp->dst;
412 struct bridge_vlan_info vinfo;
413 struct net_device *slave;
414 struct dsa_port *dp;
415 int err;
416
417 list_for_each_entry(dp, &dst->ports, list) {
418 if (dp->type != DSA_PORT_TYPE_USER)
419 continue;
420
Vladimir Olteand3eed0e2021-12-06 18:57:56 +0200421 if (!dp->bridge)
Vladimir Oltean884be122021-07-26 19:55:34 +0300422 continue;
423
424 if (dp->stp_state != BR_STATE_LEARNING &&
425 dp->stp_state != BR_STATE_FORWARDING)
426 continue;
427
428 /* Since the bridge might learn this packet, keep the CPU port
429 * affinity with the port that will be used for the reply on
430 * xmit.
431 */
432 if (dp->cpu_dp != cpu_dp)
433 continue;
434
435 slave = dp->slave;
436
437 err = br_vlan_get_info_rcu(slave, vid, &vinfo);
438 if (err)
439 continue;
440
441 return slave;
442 }
443
444 return NULL;
445}
446
Vladimir Olteanbea79072021-07-29 17:56:00 +0300447/* If the ingress port offloads the bridge, we mark the frame as autonomously
448 * forwarded by hardware, so the software bridge doesn't forward in twice, back
449 * to us, because we already did. However, if we're in fallback mode and we do
Vladimir Olteand3eed0e2021-12-06 18:57:56 +0200450 * software bridging, we are not offloading it, therefore the dp->bridge
Vladimir Olteanbea79072021-07-29 17:56:00 +0300451 * pointer is not populated, and flooding needs to be done by software (we are
452 * effectively operating in standalone ports mode).
453 */
454static inline void dsa_default_offload_fwd_mark(struct sk_buff *skb)
455{
456 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
457
Vladimir Olteand3eed0e2021-12-06 18:57:56 +0200458 skb->offload_fwd_mark = !!(dp->bridge);
Vladimir Olteanbea79072021-07-29 17:56:00 +0300459}
460
Vladimir Olteanf1dacd72021-08-10 16:13:53 +0300461/* Helper for removing DSA header tags from packets in the RX path.
462 * Must not be called before skb_pull(len).
463 * skb->data
464 * |
465 * v
466 * | | | | | | | | | | | | | | | | | | |
467 * +-----------------------+-----------------------+---------------+-------+
468 * | Destination MAC | Source MAC | DSA header | EType |
469 * +-----------------------+-----------------------+---------------+-------+
470 * | |
471 * <----- len -----> <----- len ----->
472 * |
473 * >>>>>>> v
474 * >>>>>>> | | | | | | | | | | | | | | |
475 * >>>>>>> +-----------------------+-----------------------+-------+
476 * >>>>>>> | Destination MAC | Source MAC | EType |
477 * +-----------------------+-----------------------+-------+
478 * ^
479 * |
480 * skb->data
481 */
482static inline void dsa_strip_etype_header(struct sk_buff *skb, int len)
483{
484 memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - len, 2 * ETH_ALEN);
485}
486
Vladimir Oltean6bef7942021-08-10 16:13:54 +0300487/* Helper for creating space for DSA header tags in TX path packets.
488 * Must not be called before skb_push(len).
489 *
490 * Before:
491 *
492 * <<<<<<< | | | | | | | | | | | | | | |
493 * ^ <<<<<<< +-----------------------+-----------------------+-------+
494 * | <<<<<<< | Destination MAC | Source MAC | EType |
495 * | +-----------------------+-----------------------+-------+
496 * <----- len ----->
497 * |
498 * |
499 * skb->data
500 *
501 * After:
502 *
503 * | | | | | | | | | | | | | | | | | | |
504 * +-----------------------+-----------------------+---------------+-------+
505 * | Destination MAC | Source MAC | DSA header | EType |
506 * +-----------------------+-----------------------+---------------+-------+
507 * ^ | |
508 * | <----- len ----->
509 * skb->data
510 */
511static inline void dsa_alloc_etype_header(struct sk_buff *skb, int len)
512{
513 memmove(skb->data, skb->data + len, 2 * ETH_ALEN);
514}
515
Vladimir Oltean5d928ff2021-08-10 16:13:55 +0300516/* On RX, eth_type_trans() on the DSA master pulls ETH_HLEN bytes starting from
517 * skb_mac_header(skb), which leaves skb->data pointing at the first byte after
518 * what the DSA master perceives as the EtherType (the beginning of the L3
519 * protocol). Since DSA EtherType header taggers treat the EtherType as part of
520 * the DSA tag itself, and the EtherType is 2 bytes in length, the DSA header
521 * is located 2 bytes behind skb->data. Note that EtherType in this context
522 * means the first 2 bytes of the DSA header, not the encapsulated EtherType
523 * that will become visible after the DSA header is stripped.
524 */
525static inline void *dsa_etype_header_pos_rx(struct sk_buff *skb)
526{
527 return skb->data - 2;
528}
529
Vladimir Olteana72808b2021-08-10 16:13:56 +0300530/* On TX, skb->data points to skb_mac_header(skb), which means that EtherType
531 * header taggers start exactly where the EtherType is (the EtherType is
532 * treated as part of the DSA header).
533 */
534static inline void *dsa_etype_header_pos_tx(struct sk_buff *skb)
535{
536 return skb->data + 2 * ETH_ALEN;
537}
538
Vivien Didelotf515f192017-02-03 13:20:20 -0500539/* switch.c */
540int dsa_switch_register_notifier(struct dsa_switch *ds);
541void dsa_switch_unregister_notifier(struct dsa_switch *ds);
Vladimir Olteanbff33f72020-03-27 21:55:43 +0200542
Vladimir Oltean5e8a1e02022-03-02 21:14:10 +0200543static inline bool dsa_switch_supports_uc_filtering(struct dsa_switch *ds)
544{
545 return ds->ops->port_fdb_add && ds->ops->port_fdb_del &&
546 ds->fdb_isolation && !ds->vlan_filtering_is_global &&
547 !ds->needs_standalone_vlan_filtering;
548}
549
550static inline bool dsa_switch_supports_mc_filtering(struct dsa_switch *ds)
551{
552 return ds->ops->port_mdb_add && ds->ops->port_mdb_del &&
553 ds->fdb_isolation && !ds->vlan_filtering_is_global &&
554 !ds->needs_standalone_vlan_filtering;
555}
556
Vladimir Olteanbff33f72020-03-27 21:55:43 +0200557/* dsa2.c */
Vladimir Olteandedd6a02022-02-23 16:00:49 +0200558void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag);
559void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag);
560struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
561 const struct net_device *lag_dev);
Vladimir Oltean95f510d2022-09-11 04:06:59 +0300562struct net_device *dsa_tree_find_first_master(struct dsa_switch_tree *dst);
Vladimir Oltean886f8e22021-01-29 03:00:04 +0200563int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v);
564int dsa_broadcast(unsigned long e, void *v);
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200565int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200566 const struct dsa_device_ops *tag_ops,
567 const struct dsa_device_ops *old_tag_ops);
Vladimir Oltean295ab962022-02-02 01:03:20 +0100568void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
569 struct net_device *master,
570 bool up);
571void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
572 struct net_device *master,
573 bool up);
Vladimir Oltean3f9bb032021-12-06 18:57:47 +0200574unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max);
575void dsa_bridge_num_put(const struct net_device *bridge_dev,
576 unsigned int bridge_num);
Vladimir Olteand3eed0e2021-12-06 18:57:56 +0200577struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
578 const struct net_device *br);
Tobias Waldekranz058102a62021-01-13 09:42:53 +0100579
Vladimir Olteane19cc132021-07-19 20:14:51 +0300580/* tag_8021q.c */
Vladimir Olteanc64b9c02021-07-19 20:14:52 +0300581int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds,
582 struct dsa_notifier_tag_8021q_vlan_info *info);
583int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds,
584 struct dsa_notifier_tag_8021q_vlan_info *info);
Vladimir Olteane19cc132021-07-19 20:14:51 +0300585
Vladimir Olteanbff33f72020-03-27 21:55:43 +0200586extern struct list_head dsa_tree_list;
587
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000588#endif