Merge tag 'wireless-drivers-next-2021-02-12' of git://git.kernel.org/pub/scm/linux...
[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_HSR_JOIN,
24         DSA_NOTIFIER_HSR_LEAVE,
25         DSA_NOTIFIER_LAG_CHANGE,
26         DSA_NOTIFIER_LAG_JOIN,
27         DSA_NOTIFIER_LAG_LEAVE,
28         DSA_NOTIFIER_MDB_ADD,
29         DSA_NOTIFIER_MDB_DEL,
30         DSA_NOTIFIER_VLAN_ADD,
31         DSA_NOTIFIER_VLAN_DEL,
32         DSA_NOTIFIER_MTU,
33         DSA_NOTIFIER_TAG_PROTO,
34 };
35
36 /* DSA_NOTIFIER_AGEING_TIME */
37 struct dsa_notifier_ageing_time_info {
38         unsigned int ageing_time;
39 };
40
41 /* DSA_NOTIFIER_BRIDGE_* */
42 struct dsa_notifier_bridge_info {
43         struct net_device *br;
44         int tree_index;
45         int sw_index;
46         int port;
47 };
48
49 /* DSA_NOTIFIER_FDB_* */
50 struct dsa_notifier_fdb_info {
51         int sw_index;
52         int port;
53         const unsigned char *addr;
54         u16 vid;
55 };
56
57 /* DSA_NOTIFIER_MDB_* */
58 struct dsa_notifier_mdb_info {
59         const struct switchdev_obj_port_mdb *mdb;
60         int sw_index;
61         int port;
62 };
63
64 /* DSA_NOTIFIER_LAG_* */
65 struct dsa_notifier_lag_info {
66         struct net_device *lag;
67         int sw_index;
68         int port;
69
70         struct netdev_lag_upper_info *info;
71 };
72
73 /* DSA_NOTIFIER_VLAN_* */
74 struct dsa_notifier_vlan_info {
75         const struct switchdev_obj_port_vlan *vlan;
76         int sw_index;
77         int port;
78 };
79
80 /* DSA_NOTIFIER_MTU */
81 struct dsa_notifier_mtu_info {
82         bool propagate_upstream;
83         int sw_index;
84         int port;
85         int mtu;
86 };
87
88 /* DSA_NOTIFIER_TAG_PROTO_* */
89 struct dsa_notifier_tag_proto_info {
90         const struct dsa_device_ops *tag_ops;
91 };
92
93 struct dsa_switchdev_event_work {
94         struct dsa_switch *ds;
95         int port;
96         struct work_struct work;
97         unsigned long event;
98         /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
99          * SWITCHDEV_FDB_DEL_TO_DEVICE
100          */
101         unsigned char addr[ETH_ALEN];
102         u16 vid;
103 };
104
105 /* DSA_NOTIFIER_HSR_* */
106 struct dsa_notifier_hsr_info {
107         struct net_device *hsr;
108         int sw_index;
109         int port;
110 };
111
112 struct dsa_slave_priv {
113         /* Copy of CPU port xmit for faster access in slave transmit hot path */
114         struct sk_buff *        (*xmit)(struct sk_buff *skb,
115                                         struct net_device *dev);
116
117         struct gro_cells        gcells;
118
119         /* DSA port data, such as switch, port index, etc. */
120         struct dsa_port         *dp;
121
122 #ifdef CONFIG_NET_POLL_CONTROLLER
123         struct netpoll          *netpoll;
124 #endif
125
126         /* TC context */
127         struct list_head        mall_tc_list;
128 };
129
130 /* dsa.c */
131 const struct dsa_device_ops *dsa_tag_driver_get(int tag_protocol);
132 void dsa_tag_driver_put(const struct dsa_device_ops *ops);
133 const struct dsa_device_ops *dsa_find_tagger_by_name(const char *buf);
134
135 bool dsa_schedule_work(struct work_struct *work);
136 const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
137
138 /* master.c */
139 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp);
140 void dsa_master_teardown(struct net_device *dev);
141
142 static inline struct net_device *dsa_master_find_slave(struct net_device *dev,
143                                                        int device, int port)
144 {
145         struct dsa_port *cpu_dp = dev->dsa_ptr;
146         struct dsa_switch_tree *dst = cpu_dp->dst;
147         struct dsa_port *dp;
148
149         list_for_each_entry(dp, &dst->ports, list)
150                 if (dp->ds->index == device && dp->index == port &&
151                     dp->type == DSA_PORT_TYPE_USER)
152                         return dp->slave;
153
154         return NULL;
155 }
156
157 /* port.c */
158 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
159                                const struct dsa_device_ops *tag_ops);
160 int dsa_port_set_state(struct dsa_port *dp, u8 state);
161 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy);
162 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy);
163 void dsa_port_disable_rt(struct dsa_port *dp);
164 void dsa_port_disable(struct dsa_port *dp);
165 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br);
166 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br);
167 int dsa_port_lag_change(struct dsa_port *dp,
168                         struct netdev_lag_lower_state_info *linfo);
169 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
170                       struct netdev_lag_upper_info *uinfo);
171 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
172 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering);
173 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp);
174 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock);
175 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
176                         bool propagate_upstream);
177 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
178                      u16 vid);
179 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
180                      u16 vid);
181 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data);
182 int dsa_port_mdb_add(const struct dsa_port *dp,
183                      const struct switchdev_obj_port_mdb *mdb);
184 int dsa_port_mdb_del(const struct dsa_port *dp,
185                      const struct switchdev_obj_port_mdb *mdb);
186 int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags);
187 int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags);
188 int dsa_port_mrouter(struct dsa_port *dp, bool mrouter);
189 int dsa_port_vlan_add(struct dsa_port *dp,
190                       const struct switchdev_obj_port_vlan *vlan);
191 int dsa_port_vlan_del(struct dsa_port *dp,
192                       const struct switchdev_obj_port_vlan *vlan);
193 int dsa_port_link_register_of(struct dsa_port *dp);
194 void dsa_port_link_unregister_of(struct dsa_port *dp);
195 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr);
196 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr);
197 extern const struct phylink_mac_ops dsa_port_phylink_mac_ops;
198
199 static inline bool dsa_port_offloads_netdev(struct dsa_port *dp,
200                                             struct net_device *dev)
201 {
202         /* Switchdev offloading can be configured on: */
203
204         if (dev == dp->slave)
205                 /* DSA ports directly connected to a bridge, and event
206                  * was emitted for the ports themselves.
207                  */
208                 return true;
209
210         if (dp->bridge_dev == dev)
211                 /* DSA ports connected to a bridge, and event was emitted
212                  * for the bridge.
213                  */
214                 return true;
215
216         if (dp->lag_dev == dev)
217                 /* DSA ports connected to a bridge via a LAG */
218                 return true;
219
220         return false;
221 }
222
223 /* Returns true if any port of this tree offloads the given net_device */
224 static inline bool dsa_tree_offloads_netdev(struct dsa_switch_tree *dst,
225                                             struct net_device *dev)
226 {
227         struct dsa_port *dp;
228
229         list_for_each_entry(dp, &dst->ports, list)
230                 if (dsa_port_offloads_netdev(dp, dev))
231                         return true;
232
233         return false;
234 }
235
236 /* slave.c */
237 extern const struct dsa_device_ops notag_netdev_ops;
238 void dsa_slave_mii_bus_init(struct dsa_switch *ds);
239 int dsa_slave_create(struct dsa_port *dp);
240 void dsa_slave_destroy(struct net_device *slave_dev);
241 int dsa_slave_suspend(struct net_device *slave_dev);
242 int dsa_slave_resume(struct net_device *slave_dev);
243 int dsa_slave_register_notifier(void);
244 void dsa_slave_unregister_notifier(void);
245 void dsa_slave_setup_tagger(struct net_device *slave);
246 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu);
247
248 static inline struct dsa_port *dsa_slave_to_port(const struct net_device *dev)
249 {
250         struct dsa_slave_priv *p = netdev_priv(dev);
251
252         return p->dp;
253 }
254
255 static inline struct net_device *
256 dsa_slave_to_master(const struct net_device *dev)
257 {
258         struct dsa_port *dp = dsa_slave_to_port(dev);
259
260         return dp->cpu_dp->master;
261 }
262
263 /* If under a bridge with vlan_filtering=0, make sure to send pvid-tagged
264  * frames as untagged, since the bridge will not untag them.
265  */
266 static inline struct sk_buff *dsa_untag_bridge_pvid(struct sk_buff *skb)
267 {
268         struct dsa_port *dp = dsa_slave_to_port(skb->dev);
269         struct net_device *br = dp->bridge_dev;
270         struct net_device *dev = skb->dev;
271         struct net_device *upper_dev;
272         u16 vid, pvid, proto;
273         int err;
274
275         if (!br || br_vlan_enabled(br))
276                 return skb;
277
278         err = br_vlan_get_proto(br, &proto);
279         if (err)
280                 return skb;
281
282         /* Move VLAN tag from data to hwaccel */
283         if (!skb_vlan_tag_present(skb) && skb->protocol == htons(proto)) {
284                 skb = skb_vlan_untag(skb);
285                 if (!skb)
286                         return NULL;
287         }
288
289         if (!skb_vlan_tag_present(skb))
290                 return skb;
291
292         vid = skb_vlan_tag_get_id(skb);
293
294         /* We already run under an RCU read-side critical section since
295          * we are called from netif_receive_skb_list_internal().
296          */
297         err = br_vlan_get_pvid_rcu(dev, &pvid);
298         if (err)
299                 return skb;
300
301         if (vid != pvid)
302                 return skb;
303
304         /* The sad part about attempting to untag from DSA is that we
305          * don't know, unless we check, if the skb will end up in
306          * the bridge's data path - br_allowed_ingress() - or not.
307          * For example, there might be an 8021q upper for the
308          * default_pvid of the bridge, which will steal VLAN-tagged traffic
309          * from the bridge's data path. This is a configuration that DSA
310          * supports because vlan_filtering is 0. In that case, we should
311          * definitely keep the tag, to make sure it keeps working.
312          */
313         upper_dev = __vlan_find_dev_deep_rcu(br, htons(proto), vid);
314         if (upper_dev)
315                 return skb;
316
317         __vlan_hwaccel_clear_tag(skb);
318
319         return skb;
320 }
321
322 /* switch.c */
323 int dsa_switch_register_notifier(struct dsa_switch *ds);
324 void dsa_switch_unregister_notifier(struct dsa_switch *ds);
325
326 /* dsa2.c */
327 void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag);
328 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag);
329 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v);
330 int dsa_broadcast(unsigned long e, void *v);
331 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
332                               struct net_device *master,
333                               const struct dsa_device_ops *tag_ops,
334                               const struct dsa_device_ops *old_tag_ops);
335
336 extern struct list_head dsa_tree_list;
337
338 #endif