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