net: dsa: make assisted_learning_on_cpu_port bypass offloaded LAG interfaces
[linux-2.6-microblaze.git] / net / dsa / dsa_priv.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * net/dsa/dsa_priv.h - Hardware switch handling
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  */
6
7 #ifndef __DSA_PRIV_H
8 #define __DSA_PRIV_H
9
10 #include <linux/if_bridge.h>
11 #include <linux/phy.h>
12 #include <linux/netdevice.h>
13 #include <linux/netpoll.h>
14 #include <net/dsa.h>
15 #include <net/gro_cells.h>
16
17 enum {
18         DSA_NOTIFIER_AGEING_TIME,
19         DSA_NOTIFIER_BRIDGE_JOIN,
20         DSA_NOTIFIER_BRIDGE_LEAVE,
21         DSA_NOTIFIER_FDB_ADD,
22         DSA_NOTIFIER_FDB_DEL,
23         DSA_NOTIFIER_LAG_CHANGE,
24         DSA_NOTIFIER_LAG_JOIN,
25         DSA_NOTIFIER_LAG_LEAVE,
26         DSA_NOTIFIER_MDB_ADD,
27         DSA_NOTIFIER_MDB_DEL,
28         DSA_NOTIFIER_VLAN_ADD,
29         DSA_NOTIFIER_VLAN_DEL,
30         DSA_NOTIFIER_MTU,
31         DSA_NOTIFIER_TAG_PROTO,
32 };
33
34 /* DSA_NOTIFIER_AGEING_TIME */
35 struct dsa_notifier_ageing_time_info {
36         unsigned int ageing_time;
37 };
38
39 /* DSA_NOTIFIER_BRIDGE_* */
40 struct dsa_notifier_bridge_info {
41         struct net_device *br;
42         int tree_index;
43         int sw_index;
44         int port;
45 };
46
47 /* DSA_NOTIFIER_FDB_* */
48 struct dsa_notifier_fdb_info {
49         int sw_index;
50         int port;
51         const unsigned char *addr;
52         u16 vid;
53 };
54
55 /* DSA_NOTIFIER_MDB_* */
56 struct dsa_notifier_mdb_info {
57         const struct switchdev_obj_port_mdb *mdb;
58         int sw_index;
59         int port;
60 };
61
62 /* DSA_NOTIFIER_LAG_* */
63 struct dsa_notifier_lag_info {
64         struct net_device *lag;
65         int sw_index;
66         int port;
67
68         struct netdev_lag_upper_info *info;
69 };
70
71 /* DSA_NOTIFIER_VLAN_* */
72 struct dsa_notifier_vlan_info {
73         const struct switchdev_obj_port_vlan *vlan;
74         int sw_index;
75         int port;
76 };
77
78 /* DSA_NOTIFIER_MTU */
79 struct dsa_notifier_mtu_info {
80         bool propagate_upstream;
81         int sw_index;
82         int port;
83         int mtu;
84 };
85
86 /* DSA_NOTIFIER_TAG_PROTO_* */
87 struct dsa_notifier_tag_proto_info {
88         const struct dsa_device_ops *tag_ops;
89 };
90
91 struct dsa_switchdev_event_work {
92         struct dsa_switch *ds;
93         int port;
94         struct work_struct work;
95         unsigned long event;
96         /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
97          * SWITCHDEV_FDB_DEL_TO_DEVICE
98          */
99         unsigned char addr[ETH_ALEN];
100         u16 vid;
101 };
102
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);
107
108         struct gro_cells        gcells;
109
110         /* DSA port data, such as switch, port index, etc. */
111         struct dsa_port         *dp;
112
113 #ifdef CONFIG_NET_POLL_CONTROLLER
114         struct netpoll          *netpoll;
115 #endif
116
117         /* TC context */
118         struct list_head        mall_tc_list;
119 };
120
121 /* dsa.c */
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);
125
126 bool dsa_schedule_work(struct work_struct *work);
127 const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
128
129 /* master.c */
130 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp);
131 void dsa_master_teardown(struct net_device *dev);
132
133 static inline struct net_device *dsa_master_find_slave(struct net_device *dev,
134                                                        int device, int port)
135 {
136         struct dsa_port *cpu_dp = dev->dsa_ptr;
137         struct dsa_switch_tree *dst = cpu_dp->dst;
138         struct dsa_port *dp;
139
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)
143                         return dp->slave;
144
145         return NULL;
146 }
147
148 /* port.c */
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,
169                      u16 vid);
170 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
171                      u16 vid);
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;
187
188 static inline bool dsa_port_offloads_netdev(struct dsa_port *dp,
189                                             struct net_device *dev)
190 {
191         /* Switchdev offloading can be configured on: */
192
193         if (dev == dp->slave)
194                 /* DSA ports directly connected to a bridge, and event
195                  * was emitted for the ports themselves.
196                  */
197                 return true;
198
199         if (dp->bridge_dev == dev)
200                 /* DSA ports connected to a bridge, and event was emitted
201                  * for the bridge.
202                  */
203                 return true;
204
205         if (dp->lag_dev == dev)
206                 /* DSA ports connected to a bridge via a LAG */
207                 return true;
208
209         return false;
210 }
211
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)
215 {
216         struct dsa_port *dp;
217
218         list_for_each_entry(dp, &dst->ports, list)
219                 if (dsa_port_offloads_netdev(dp, dev))
220                         return true;
221
222         return false;
223 }
224
225 /* slave.c */
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);
236
237 static inline struct dsa_port *dsa_slave_to_port(const struct net_device *dev)
238 {
239         struct dsa_slave_priv *p = netdev_priv(dev);
240
241         return p->dp;
242 }
243
244 static inline struct net_device *
245 dsa_slave_to_master(const struct net_device *dev)
246 {
247         struct dsa_port *dp = dsa_slave_to_port(dev);
248
249         return dp->cpu_dp->master;
250 }
251
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.
254  */
255 static inline struct sk_buff *dsa_untag_bridge_pvid(struct sk_buff *skb)
256 {
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;
262         int err;
263
264         if (!br || br_vlan_enabled(br))
265                 return skb;
266
267         err = br_vlan_get_proto(br, &proto);
268         if (err)
269                 return skb;
270
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);
274                 if (!skb)
275                         return NULL;
276         }
277
278         if (!skb_vlan_tag_present(skb))
279                 return skb;
280
281         vid = skb_vlan_tag_get_id(skb);
282
283         /* We already run under an RCU read-side critical section since
284          * we are called from netif_receive_skb_list_internal().
285          */
286         err = br_vlan_get_pvid_rcu(dev, &pvid);
287         if (err)
288                 return skb;
289
290         if (vid != pvid)
291                 return skb;
292
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.
301          */
302         upper_dev = __vlan_find_dev_deep_rcu(br, htons(proto), vid);
303         if (upper_dev)
304                 return skb;
305
306         __vlan_hwaccel_clear_tag(skb);
307
308         return skb;
309 }
310
311 /* switch.c */
312 int dsa_switch_register_notifier(struct dsa_switch *ds);
313 void dsa_switch_unregister_notifier(struct dsa_switch *ds);
314
315 /* dsa2.c */
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);
324
325 extern struct list_head dsa_tree_list;
326
327 #endif