mei: do not overwrite state on hw start
[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/if_vlan.h>
12 #include <linux/phy.h>
13 #include <linux/netdevice.h>
14 #include <linux/netpoll.h>
15 #include <net/dsa.h>
16 #include <net/gro_cells.h>
17
18 #define DSA_MAX_NUM_OFFLOADING_BRIDGES          BITS_PER_LONG
19
20 enum {
21         DSA_NOTIFIER_AGEING_TIME,
22         DSA_NOTIFIER_BRIDGE_JOIN,
23         DSA_NOTIFIER_BRIDGE_LEAVE,
24         DSA_NOTIFIER_FDB_ADD,
25         DSA_NOTIFIER_FDB_DEL,
26         DSA_NOTIFIER_HOST_FDB_ADD,
27         DSA_NOTIFIER_HOST_FDB_DEL,
28         DSA_NOTIFIER_LAG_CHANGE,
29         DSA_NOTIFIER_LAG_JOIN,
30         DSA_NOTIFIER_LAG_LEAVE,
31         DSA_NOTIFIER_MDB_ADD,
32         DSA_NOTIFIER_MDB_DEL,
33         DSA_NOTIFIER_HOST_MDB_ADD,
34         DSA_NOTIFIER_HOST_MDB_DEL,
35         DSA_NOTIFIER_VLAN_ADD,
36         DSA_NOTIFIER_VLAN_DEL,
37         DSA_NOTIFIER_MTU,
38         DSA_NOTIFIER_TAG_PROTO,
39         DSA_NOTIFIER_TAG_PROTO_CONNECT,
40         DSA_NOTIFIER_TAG_PROTO_DISCONNECT,
41         DSA_NOTIFIER_TAG_8021Q_VLAN_ADD,
42         DSA_NOTIFIER_TAG_8021Q_VLAN_DEL,
43 };
44
45 /* DSA_NOTIFIER_AGEING_TIME */
46 struct dsa_notifier_ageing_time_info {
47         unsigned int ageing_time;
48 };
49
50 /* DSA_NOTIFIER_BRIDGE_* */
51 struct dsa_notifier_bridge_info {
52         struct dsa_bridge bridge;
53         int tree_index;
54         int sw_index;
55         int port;
56         bool tx_fwd_offload;
57 };
58
59 /* DSA_NOTIFIER_FDB_* */
60 struct dsa_notifier_fdb_info {
61         int sw_index;
62         int port;
63         const unsigned char *addr;
64         u16 vid;
65 };
66
67 /* DSA_NOTIFIER_MDB_* */
68 struct dsa_notifier_mdb_info {
69         const struct switchdev_obj_port_mdb *mdb;
70         int sw_index;
71         int port;
72 };
73
74 /* DSA_NOTIFIER_LAG_* */
75 struct dsa_notifier_lag_info {
76         struct net_device *lag;
77         int sw_index;
78         int port;
79
80         struct netdev_lag_upper_info *info;
81 };
82
83 /* DSA_NOTIFIER_VLAN_* */
84 struct dsa_notifier_vlan_info {
85         const struct switchdev_obj_port_vlan *vlan;
86         int sw_index;
87         int port;
88         struct netlink_ext_ack *extack;
89 };
90
91 /* DSA_NOTIFIER_MTU */
92 struct dsa_notifier_mtu_info {
93         bool targeted_match;
94         int sw_index;
95         int port;
96         int mtu;
97 };
98
99 /* DSA_NOTIFIER_TAG_PROTO_* */
100 struct dsa_notifier_tag_proto_info {
101         const struct dsa_device_ops *tag_ops;
102 };
103
104 /* DSA_NOTIFIER_TAG_8021Q_VLAN_* */
105 struct dsa_notifier_tag_8021q_vlan_info {
106         int tree_index;
107         int sw_index;
108         int port;
109         u16 vid;
110 };
111
112 struct dsa_switchdev_event_work {
113         struct dsa_switch *ds;
114         int port;
115         struct net_device *dev;
116         struct work_struct work;
117         unsigned long event;
118         /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
119          * SWITCHDEV_FDB_DEL_TO_DEVICE
120          */
121         unsigned char addr[ETH_ALEN];
122         u16 vid;
123         bool host_addr;
124 };
125
126 struct dsa_slave_priv {
127         /* Copy of CPU port xmit for faster access in slave transmit hot path */
128         struct sk_buff *        (*xmit)(struct sk_buff *skb,
129                                         struct net_device *dev);
130
131         struct gro_cells        gcells;
132
133         /* DSA port data, such as switch, port index, etc. */
134         struct dsa_port         *dp;
135
136 #ifdef CONFIG_NET_POLL_CONTROLLER
137         struct netpoll          *netpoll;
138 #endif
139
140         /* TC context */
141         struct list_head        mall_tc_list;
142 };
143
144 /* dsa.c */
145 const struct dsa_device_ops *dsa_tag_driver_get(int tag_protocol);
146 void dsa_tag_driver_put(const struct dsa_device_ops *ops);
147 const struct dsa_device_ops *dsa_find_tagger_by_name(const char *buf);
148
149 bool dsa_schedule_work(struct work_struct *work);
150 void dsa_flush_workqueue(void);
151 const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
152
153 static inline int dsa_tag_protocol_overhead(const struct dsa_device_ops *ops)
154 {
155         return ops->needed_headroom + ops->needed_tailroom;
156 }
157
158 /* master.c */
159 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp);
160 void dsa_master_teardown(struct net_device *dev);
161
162 static inline struct net_device *dsa_master_find_slave(struct net_device *dev,
163                                                        int device, int port)
164 {
165         struct dsa_port *cpu_dp = dev->dsa_ptr;
166         struct dsa_switch_tree *dst = cpu_dp->dst;
167         struct dsa_port *dp;
168
169         list_for_each_entry(dp, &dst->ports, list)
170                 if (dp->ds->index == device && dp->index == port &&
171                     dp->type == DSA_PORT_TYPE_USER)
172                         return dp->slave;
173
174         return NULL;
175 }
176
177 /* port.c */
178 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
179                                const struct dsa_device_ops *tag_ops);
180 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age);
181 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy);
182 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy);
183 void dsa_port_disable_rt(struct dsa_port *dp);
184 void dsa_port_disable(struct dsa_port *dp);
185 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
186                          struct netlink_ext_ack *extack);
187 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br);
188 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br);
189 int dsa_port_lag_change(struct dsa_port *dp,
190                         struct netdev_lag_lower_state_info *linfo);
191 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
192                       struct netdev_lag_upper_info *uinfo,
193                       struct netlink_ext_ack *extack);
194 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
195 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev);
196 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
197                             struct netlink_ext_ack *extack);
198 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp);
199 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock);
200 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
201                         bool targeted_match);
202 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
203                      u16 vid);
204 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
205                      u16 vid);
206 int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
207                           u16 vid);
208 int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
209                           u16 vid);
210 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data);
211 int dsa_port_mdb_add(const struct dsa_port *dp,
212                      const struct switchdev_obj_port_mdb *mdb);
213 int dsa_port_mdb_del(const struct dsa_port *dp,
214                      const struct switchdev_obj_port_mdb *mdb);
215 int dsa_port_host_mdb_add(const struct dsa_port *dp,
216                           const struct switchdev_obj_port_mdb *mdb);
217 int dsa_port_host_mdb_del(const struct dsa_port *dp,
218                           const struct switchdev_obj_port_mdb *mdb);
219 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
220                               struct switchdev_brport_flags flags,
221                               struct netlink_ext_ack *extack);
222 int dsa_port_bridge_flags(struct dsa_port *dp,
223                           struct switchdev_brport_flags flags,
224                           struct netlink_ext_ack *extack);
225 int dsa_port_vlan_add(struct dsa_port *dp,
226                       const struct switchdev_obj_port_vlan *vlan,
227                       struct netlink_ext_ack *extack);
228 int dsa_port_vlan_del(struct dsa_port *dp,
229                       const struct switchdev_obj_port_vlan *vlan);
230 int dsa_port_mrp_add(const struct dsa_port *dp,
231                      const struct switchdev_obj_mrp *mrp);
232 int dsa_port_mrp_del(const struct dsa_port *dp,
233                      const struct switchdev_obj_mrp *mrp);
234 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
235                                const struct switchdev_obj_ring_role_mrp *mrp);
236 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
237                                const struct switchdev_obj_ring_role_mrp *mrp);
238 int dsa_port_phylink_create(struct dsa_port *dp);
239 int dsa_port_link_register_of(struct dsa_port *dp);
240 void dsa_port_link_unregister_of(struct dsa_port *dp);
241 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr);
242 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr);
243 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast);
244 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast);
245
246 /* slave.c */
247 extern const struct dsa_device_ops notag_netdev_ops;
248 extern struct notifier_block dsa_slave_switchdev_notifier;
249 extern struct notifier_block dsa_slave_switchdev_blocking_notifier;
250
251 void dsa_slave_mii_bus_init(struct dsa_switch *ds);
252 int dsa_slave_create(struct dsa_port *dp);
253 void dsa_slave_destroy(struct net_device *slave_dev);
254 int dsa_slave_suspend(struct net_device *slave_dev);
255 int dsa_slave_resume(struct net_device *slave_dev);
256 int dsa_slave_register_notifier(void);
257 void dsa_slave_unregister_notifier(void);
258 void dsa_slave_setup_tagger(struct net_device *slave);
259 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu);
260 int dsa_slave_manage_vlan_filtering(struct net_device *dev,
261                                     bool vlan_filtering);
262
263 static inline struct dsa_port *dsa_slave_to_port(const struct net_device *dev)
264 {
265         struct dsa_slave_priv *p = netdev_priv(dev);
266
267         return p->dp;
268 }
269
270 static inline struct net_device *
271 dsa_slave_to_master(const struct net_device *dev)
272 {
273         struct dsa_port *dp = dsa_slave_to_port(dev);
274
275         return dp->cpu_dp->master;
276 }
277
278 /* If under a bridge with vlan_filtering=0, make sure to send pvid-tagged
279  * frames as untagged, since the bridge will not untag them.
280  */
281 static inline struct sk_buff *dsa_untag_bridge_pvid(struct sk_buff *skb)
282 {
283         struct dsa_port *dp = dsa_slave_to_port(skb->dev);
284         struct net_device *br = dsa_port_bridge_dev_get(dp);
285         struct net_device *dev = skb->dev;
286         struct net_device *upper_dev;
287         u16 vid, pvid, proto;
288         int err;
289
290         if (!br || br_vlan_enabled(br))
291                 return skb;
292
293         err = br_vlan_get_proto(br, &proto);
294         if (err)
295                 return skb;
296
297         /* Move VLAN tag from data to hwaccel */
298         if (!skb_vlan_tag_present(skb) && skb->protocol == htons(proto)) {
299                 skb = skb_vlan_untag(skb);
300                 if (!skb)
301                         return NULL;
302         }
303
304         if (!skb_vlan_tag_present(skb))
305                 return skb;
306
307         vid = skb_vlan_tag_get_id(skb);
308
309         /* We already run under an RCU read-side critical section since
310          * we are called from netif_receive_skb_list_internal().
311          */
312         err = br_vlan_get_pvid_rcu(dev, &pvid);
313         if (err)
314                 return skb;
315
316         if (vid != pvid)
317                 return skb;
318
319         /* The sad part about attempting to untag from DSA is that we
320          * don't know, unless we check, if the skb will end up in
321          * the bridge's data path - br_allowed_ingress() - or not.
322          * For example, there might be an 8021q upper for the
323          * default_pvid of the bridge, which will steal VLAN-tagged traffic
324          * from the bridge's data path. This is a configuration that DSA
325          * supports because vlan_filtering is 0. In that case, we should
326          * definitely keep the tag, to make sure it keeps working.
327          */
328         upper_dev = __vlan_find_dev_deep_rcu(br, htons(proto), vid);
329         if (upper_dev)
330                 return skb;
331
332         __vlan_hwaccel_clear_tag(skb);
333
334         return skb;
335 }
336
337 /* For switches without hardware support for DSA tagging to be able
338  * to support termination through the bridge.
339  */
340 static inline struct net_device *
341 dsa_find_designated_bridge_port_by_vid(struct net_device *master, u16 vid)
342 {
343         struct dsa_port *cpu_dp = master->dsa_ptr;
344         struct dsa_switch_tree *dst = cpu_dp->dst;
345         struct bridge_vlan_info vinfo;
346         struct net_device *slave;
347         struct dsa_port *dp;
348         int err;
349
350         list_for_each_entry(dp, &dst->ports, list) {
351                 if (dp->type != DSA_PORT_TYPE_USER)
352                         continue;
353
354                 if (!dp->bridge)
355                         continue;
356
357                 if (dp->stp_state != BR_STATE_LEARNING &&
358                     dp->stp_state != BR_STATE_FORWARDING)
359                         continue;
360
361                 /* Since the bridge might learn this packet, keep the CPU port
362                  * affinity with the port that will be used for the reply on
363                  * xmit.
364                  */
365                 if (dp->cpu_dp != cpu_dp)
366                         continue;
367
368                 slave = dp->slave;
369
370                 err = br_vlan_get_info_rcu(slave, vid, &vinfo);
371                 if (err)
372                         continue;
373
374                 return slave;
375         }
376
377         return NULL;
378 }
379
380 /* If the ingress port offloads the bridge, we mark the frame as autonomously
381  * forwarded by hardware, so the software bridge doesn't forward in twice, back
382  * to us, because we already did. However, if we're in fallback mode and we do
383  * software bridging, we are not offloading it, therefore the dp->bridge
384  * pointer is not populated, and flooding needs to be done by software (we are
385  * effectively operating in standalone ports mode).
386  */
387 static inline void dsa_default_offload_fwd_mark(struct sk_buff *skb)
388 {
389         struct dsa_port *dp = dsa_slave_to_port(skb->dev);
390
391         skb->offload_fwd_mark = !!(dp->bridge);
392 }
393
394 /* Helper for removing DSA header tags from packets in the RX path.
395  * Must not be called before skb_pull(len).
396  *                                                                 skb->data
397  *                                                                         |
398  *                                                                         v
399  * |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
400  * +-----------------------+-----------------------+---------------+-------+
401  * |    Destination MAC    |      Source MAC       |  DSA header   | EType |
402  * +-----------------------+-----------------------+---------------+-------+
403  *                                                 |               |
404  * <----- len ----->                               <----- len ----->
405  *                 |
406  *       >>>>>>>   v
407  *       >>>>>>>   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
408  *       >>>>>>>   +-----------------------+-----------------------+-------+
409  *       >>>>>>>   |    Destination MAC    |      Source MAC       | EType |
410  *                 +-----------------------+-----------------------+-------+
411  *                                                                         ^
412  *                                                                         |
413  *                                                                 skb->data
414  */
415 static inline void dsa_strip_etype_header(struct sk_buff *skb, int len)
416 {
417         memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - len, 2 * ETH_ALEN);
418 }
419
420 /* Helper for creating space for DSA header tags in TX path packets.
421  * Must not be called before skb_push(len).
422  *
423  * Before:
424  *
425  *       <<<<<<<   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
426  * ^     <<<<<<<   +-----------------------+-----------------------+-------+
427  * |     <<<<<<<   |    Destination MAC    |      Source MAC       | EType |
428  * |               +-----------------------+-----------------------+-------+
429  * <----- len ----->
430  * |
431  * |
432  * skb->data
433  *
434  * After:
435  *
436  * |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
437  * +-----------------------+-----------------------+---------------+-------+
438  * |    Destination MAC    |      Source MAC       |  DSA header   | EType |
439  * +-----------------------+-----------------------+---------------+-------+
440  * ^                                               |               |
441  * |                                               <----- len ----->
442  * skb->data
443  */
444 static inline void dsa_alloc_etype_header(struct sk_buff *skb, int len)
445 {
446         memmove(skb->data, skb->data + len, 2 * ETH_ALEN);
447 }
448
449 /* On RX, eth_type_trans() on the DSA master pulls ETH_HLEN bytes starting from
450  * skb_mac_header(skb), which leaves skb->data pointing at the first byte after
451  * what the DSA master perceives as the EtherType (the beginning of the L3
452  * protocol). Since DSA EtherType header taggers treat the EtherType as part of
453  * the DSA tag itself, and the EtherType is 2 bytes in length, the DSA header
454  * is located 2 bytes behind skb->data. Note that EtherType in this context
455  * means the first 2 bytes of the DSA header, not the encapsulated EtherType
456  * that will become visible after the DSA header is stripped.
457  */
458 static inline void *dsa_etype_header_pos_rx(struct sk_buff *skb)
459 {
460         return skb->data - 2;
461 }
462
463 /* On TX, skb->data points to skb_mac_header(skb), which means that EtherType
464  * header taggers start exactly where the EtherType is (the EtherType is
465  * treated as part of the DSA header).
466  */
467 static inline void *dsa_etype_header_pos_tx(struct sk_buff *skb)
468 {
469         return skb->data + 2 * ETH_ALEN;
470 }
471
472 /* switch.c */
473 int dsa_switch_register_notifier(struct dsa_switch *ds);
474 void dsa_switch_unregister_notifier(struct dsa_switch *ds);
475
476 /* dsa2.c */
477 void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag);
478 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag);
479 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v);
480 int dsa_broadcast(unsigned long e, void *v);
481 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
482                               struct net_device *master,
483                               const struct dsa_device_ops *tag_ops,
484                               const struct dsa_device_ops *old_tag_ops);
485 unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max);
486 void dsa_bridge_num_put(const struct net_device *bridge_dev,
487                         unsigned int bridge_num);
488 struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
489                                         const struct net_device *br);
490
491 /* tag_8021q.c */
492 int dsa_tag_8021q_bridge_join(struct dsa_switch *ds,
493                               struct dsa_notifier_bridge_info *info);
494 int dsa_tag_8021q_bridge_leave(struct dsa_switch *ds,
495                                struct dsa_notifier_bridge_info *info);
496 int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds,
497                                   struct dsa_notifier_tag_8021q_vlan_info *info);
498 int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds,
499                                   struct dsa_notifier_tag_8021q_vlan_info *info);
500
501 extern struct list_head dsa_tree_list;
502
503 #endif