2 * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
3 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #ifndef __LINUX_NET_DSA_H
12 #define __LINUX_NET_DSA_H
15 #include <linux/if_ether.h>
16 #include <linux/list.h>
17 #include <linux/notifier.h>
18 #include <linux/timer.h>
19 #include <linux/workqueue.h>
21 #include <linux/ethtool.h>
22 #include <linux/net_tstamp.h>
23 #include <linux/phy.h>
24 #include <linux/platform_data/dsa.h>
25 #include <net/devlink.h>
26 #include <net/switchdev.h>
30 struct fixed_phy_status;
31 struct phylink_link_state;
33 #define DSA_TAG_PROTO_NONE_VALUE 0
34 #define DSA_TAG_PROTO_BRCM_VALUE 1
35 #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE 2
36 #define DSA_TAG_PROTO_DSA_VALUE 3
37 #define DSA_TAG_PROTO_EDSA_VALUE 4
38 #define DSA_TAG_PROTO_GSWIP_VALUE 5
39 #define DSA_TAG_PROTO_KSZ9477_VALUE 6
40 #define DSA_TAG_PROTO_KSZ9893_VALUE 7
41 #define DSA_TAG_PROTO_LAN9303_VALUE 8
42 #define DSA_TAG_PROTO_MTK_VALUE 9
43 #define DSA_TAG_PROTO_QCA_VALUE 10
44 #define DSA_TAG_PROTO_TRAILER_VALUE 11
45 #define DSA_TAG_PROTO_8021Q_VALUE 12
47 enum dsa_tag_protocol {
48 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
49 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
50 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
51 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
52 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
53 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
54 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE,
55 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE,
56 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
57 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
58 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
59 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
60 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
66 struct dsa_device_ops {
67 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
68 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
69 struct packet_type *pt);
70 int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
72 /* Used to determine which traffic should match the DSA filter in
73 * eth_type_trans, and which, if any, should bypass it and be processed
74 * as regular on the master net device.
76 bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
77 unsigned int overhead;
79 enum dsa_tag_protocol proto;
82 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
83 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \
84 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
87 struct sk_buff *clone;
92 u8 priv[48 - sizeof(struct dsa_skb_cb)];
95 #define __DSA_SKB_CB(skb) ((struct __dsa_skb_cb *)((skb)->cb))
97 #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
99 #define DSA_SKB_CB_COPY(nskb, skb) \
100 { *__DSA_SKB_CB(nskb) = *__DSA_SKB_CB(skb); }
102 #define DSA_SKB_CB_ZERO(skb) \
103 { *__DSA_SKB_CB(skb) = (struct __dsa_skb_cb) {0}; }
105 #define DSA_SKB_CB_PRIV(skb) \
106 ((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
108 #define DSA_SKB_CB_CLONE(_clone, _skb) \
110 struct sk_buff *clone = _clone; \
111 struct sk_buff *skb = _skb; \
113 DSA_SKB_CB_COPY(clone, skb); \
114 DSA_SKB_CB(skb)->clone = clone; \
117 struct dsa_switch_tree {
118 struct list_head list;
120 /* Notifier chain for switch-wide events */
121 struct raw_notifier_head nh;
123 /* Tree identifier */
126 /* Number of switches attached to this tree */
127 struct kref refcount;
129 /* Has this tree been applied to the hardware? */
133 * Configuration data for the platform device that owns
134 * this dsa switch tree instance.
136 struct dsa_platform_data *pd;
139 * The switch port to which the CPU is attached.
141 struct dsa_port *cpu_dp;
144 * Data for the individual switch chips.
146 struct dsa_switch *ds[DSA_MAX_SWITCHES];
149 /* TC matchall action types, only mirroring for now */
150 enum dsa_port_mall_action_type {
151 DSA_PORT_MALL_MIRROR,
154 /* TC mirroring entry */
155 struct dsa_mall_mirror_tc_entry {
160 /* TC matchall entry */
161 struct dsa_mall_tc_entry {
162 struct list_head list;
163 unsigned long cookie;
164 enum dsa_port_mall_action_type type;
166 struct dsa_mall_mirror_tc_entry mirror;
172 /* A CPU port is physically connected to a master device.
173 * A user port exposed to userspace has a slave device.
176 struct net_device *master;
177 struct net_device *slave;
180 /* CPU port tagging operations used by master or slave devices */
181 const struct dsa_device_ops *tag_ops;
183 /* Copies for faster access in master receive hot path */
184 struct dsa_switch_tree *dst;
185 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
186 struct packet_type *pt);
187 bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
190 DSA_PORT_TYPE_UNUSED = 0,
196 struct dsa_switch *ds;
199 const struct dsa_port *cpu_dp;
201 struct device_node *dn;
202 unsigned int ageing_time;
205 struct net_device *bridge_dev;
206 struct devlink_port devlink_port;
209 * Original copy of the master netdev ethtool_ops
211 const struct ethtool_ops *orig_ethtool_ops;
214 * Original copy of the master netdev net_device_ops
216 const struct net_device_ops *orig_ndo_ops;
223 * Parent switch tree, and switch index.
225 struct dsa_switch_tree *dst;
228 /* Listener for switch fabric events */
229 struct notifier_block nb;
232 * Give the switch driver somewhere to hang its private data
238 * Configuration data for this switch.
240 struct dsa_chip_data *cd;
243 * The switch operations.
245 const struct dsa_switch_ops *ops;
248 * An array of which element [a] indicates which port on this
249 * switch should be used to send packets to that are destined
250 * for switch a. Can be NULL if there is only one switch chip.
252 s8 rtable[DSA_MAX_SWITCHES];
255 * Slave mii_bus and devices for the individual ports.
258 struct mii_bus *slave_mii_bus;
260 /* Ageing Time limits in msecs */
261 unsigned int ageing_time_min;
262 unsigned int ageing_time_max;
264 /* devlink used to represent this switch device */
265 struct devlink *devlink;
267 /* Number of switch port queues */
268 unsigned int num_tx_queues;
270 /* Disallow bridge core from requesting different VLAN awareness
271 * settings on ports if not hardware-supported
273 bool vlan_filtering_is_global;
275 /* In case vlan_filtering_is_global is set, the VLAN awareness state
276 * should be retrieved from here and not from the per-port settings.
280 unsigned long *bitmap;
281 unsigned long _bitmap;
283 /* Dynamically allocated ports, keep last */
285 struct dsa_port ports[];
288 static inline const struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
290 return &ds->ports[p];
293 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
295 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
298 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
300 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
303 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
305 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
308 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
310 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
313 static inline u32 dsa_user_ports(struct dsa_switch *ds)
318 for (p = 0; p < ds->num_ports; p++)
319 if (dsa_is_user_port(ds, p))
325 /* Return the local port used to reach an arbitrary switch port */
326 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
329 if (device == ds->index)
332 return ds->rtable[device];
335 /* Return the local port used to reach the dedicated CPU port */
336 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
338 const struct dsa_port *dp = dsa_to_port(ds, port);
339 const struct dsa_port *cpu_dp = dp->cpu_dp;
344 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
347 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
349 const struct dsa_switch *ds = dp->ds;
351 if (ds->vlan_filtering_is_global)
352 return ds->vlan_filtering;
354 return dp->vlan_filtering;
357 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
358 bool is_static, void *data);
359 struct dsa_switch_ops {
360 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
363 int (*setup)(struct dsa_switch *ds);
364 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
367 * Access to the switch's PHY registers.
369 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
370 int (*phy_write)(struct dsa_switch *ds, int port,
371 int regnum, u16 val);
374 * Link state adjustment (called from libphy)
376 void (*adjust_link)(struct dsa_switch *ds, int port,
377 struct phy_device *phydev);
378 void (*fixed_link_update)(struct dsa_switch *ds, int port,
379 struct fixed_phy_status *st);
382 * PHYLINK integration
384 void (*phylink_validate)(struct dsa_switch *ds, int port,
385 unsigned long *supported,
386 struct phylink_link_state *state);
387 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
388 struct phylink_link_state *state);
389 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
391 const struct phylink_link_state *state);
392 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
393 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
395 phy_interface_t interface);
396 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
398 phy_interface_t interface,
399 struct phy_device *phydev);
400 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
401 struct phylink_link_state *state);
403 * ethtool hardware statistics.
405 void (*get_strings)(struct dsa_switch *ds, int port,
406 u32 stringset, uint8_t *data);
407 void (*get_ethtool_stats)(struct dsa_switch *ds,
408 int port, uint64_t *data);
409 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
410 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
411 int port, uint64_t *data);
414 * ethtool Wake-on-LAN
416 void (*get_wol)(struct dsa_switch *ds, int port,
417 struct ethtool_wolinfo *w);
418 int (*set_wol)(struct dsa_switch *ds, int port,
419 struct ethtool_wolinfo *w);
422 * ethtool timestamp info
424 int (*get_ts_info)(struct dsa_switch *ds, int port,
425 struct ethtool_ts_info *ts);
430 int (*suspend)(struct dsa_switch *ds);
431 int (*resume)(struct dsa_switch *ds);
434 * Port enable/disable
436 int (*port_enable)(struct dsa_switch *ds, int port,
437 struct phy_device *phy);
438 void (*port_disable)(struct dsa_switch *ds, int port);
441 * Port's MAC EEE settings
443 int (*set_mac_eee)(struct dsa_switch *ds, int port,
444 struct ethtool_eee *e);
445 int (*get_mac_eee)(struct dsa_switch *ds, int port,
446 struct ethtool_eee *e);
449 int (*get_eeprom_len)(struct dsa_switch *ds);
450 int (*get_eeprom)(struct dsa_switch *ds,
451 struct ethtool_eeprom *eeprom, u8 *data);
452 int (*set_eeprom)(struct dsa_switch *ds,
453 struct ethtool_eeprom *eeprom, u8 *data);
458 int (*get_regs_len)(struct dsa_switch *ds, int port);
459 void (*get_regs)(struct dsa_switch *ds, int port,
460 struct ethtool_regs *regs, void *p);
465 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
466 int (*port_bridge_join)(struct dsa_switch *ds, int port,
467 struct net_device *bridge);
468 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
469 struct net_device *bridge);
470 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
472 void (*port_fast_age)(struct dsa_switch *ds, int port);
473 int (*port_egress_floods)(struct dsa_switch *ds, int port,
474 bool unicast, bool multicast);
479 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
480 bool vlan_filtering);
481 int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
482 const struct switchdev_obj_port_vlan *vlan);
483 void (*port_vlan_add)(struct dsa_switch *ds, int port,
484 const struct switchdev_obj_port_vlan *vlan);
485 int (*port_vlan_del)(struct dsa_switch *ds, int port,
486 const struct switchdev_obj_port_vlan *vlan);
488 * Forwarding database
490 int (*port_fdb_add)(struct dsa_switch *ds, int port,
491 const unsigned char *addr, u16 vid);
492 int (*port_fdb_del)(struct dsa_switch *ds, int port,
493 const unsigned char *addr, u16 vid);
494 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
495 dsa_fdb_dump_cb_t *cb, void *data);
500 int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
501 const struct switchdev_obj_port_mdb *mdb);
502 void (*port_mdb_add)(struct dsa_switch *ds, int port,
503 const struct switchdev_obj_port_mdb *mdb);
504 int (*port_mdb_del)(struct dsa_switch *ds, int port,
505 const struct switchdev_obj_port_mdb *mdb);
509 int (*get_rxnfc)(struct dsa_switch *ds, int port,
510 struct ethtool_rxnfc *nfc, u32 *rule_locs);
511 int (*set_rxnfc)(struct dsa_switch *ds, int port,
512 struct ethtool_rxnfc *nfc);
517 int (*port_mirror_add)(struct dsa_switch *ds, int port,
518 struct dsa_mall_mirror_tc_entry *mirror,
520 void (*port_mirror_del)(struct dsa_switch *ds, int port,
521 struct dsa_mall_mirror_tc_entry *mirror);
524 * Cross-chip operations
526 int (*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
527 int port, struct net_device *br);
528 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
529 int port, struct net_device *br);
534 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
536 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
538 bool (*port_txtstamp)(struct dsa_switch *ds, int port,
539 struct sk_buff *clone, unsigned int type);
540 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
541 struct sk_buff *skb, unsigned int type);
544 struct dsa_switch_driver {
545 struct list_head list;
546 const struct dsa_switch_ops *ops;
549 struct net_device *dsa_dev_to_net_device(struct device *dev);
551 /* Keep inline for faster access in hot path */
552 static inline bool netdev_uses_dsa(struct net_device *dev)
554 #if IS_ENABLED(CONFIG_NET_DSA)
555 return dev->dsa_ptr && dev->dsa_ptr->rcv;
560 static inline bool dsa_can_decode(const struct sk_buff *skb,
561 struct net_device *dev)
563 #if IS_ENABLED(CONFIG_NET_DSA)
564 return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
569 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
570 void dsa_unregister_switch(struct dsa_switch *ds);
571 int dsa_register_switch(struct dsa_switch *ds);
572 #ifdef CONFIG_PM_SLEEP
573 int dsa_switch_suspend(struct dsa_switch *ds);
574 int dsa_switch_resume(struct dsa_switch *ds);
576 static inline int dsa_switch_suspend(struct dsa_switch *ds)
580 static inline int dsa_switch_resume(struct dsa_switch *ds)
584 #endif /* CONFIG_PM_SLEEP */
586 enum dsa_notifier_type {
591 struct dsa_notifier_info {
592 struct net_device *dev;
595 struct dsa_notifier_register_info {
596 struct dsa_notifier_info info; /* must be first */
597 struct net_device *master;
598 unsigned int port_number;
599 unsigned int switch_number;
602 static inline struct net_device *
603 dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
608 #if IS_ENABLED(CONFIG_NET_DSA)
609 int register_dsa_notifier(struct notifier_block *nb);
610 int unregister_dsa_notifier(struct notifier_block *nb);
611 int call_dsa_notifiers(unsigned long val, struct net_device *dev,
612 struct dsa_notifier_info *info);
614 static inline int register_dsa_notifier(struct notifier_block *nb)
619 static inline int unregister_dsa_notifier(struct notifier_block *nb)
624 static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
625 struct dsa_notifier_info *info)
631 /* Broadcom tag specific helpers to insert and extract queue/port number */
632 #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q)
633 #define BRCM_TAG_GET_PORT(v) ((v) >> 8)
634 #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff)
637 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
638 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
639 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
640 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
642 struct dsa_tag_driver {
643 const struct dsa_device_ops *ops;
644 struct list_head list;
645 struct module *owner;
648 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
650 struct module *owner);
651 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
654 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
655 static int __init dsa_tag_driver_module_init(void) \
657 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
661 module_init(dsa_tag_driver_module_init); \
663 static void __exit dsa_tag_driver_module_exit(void) \
665 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
667 module_exit(dsa_tag_driver_module_exit)
670 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
672 * @__ops_array: Array of tag driver strucutres
674 * Helper macro for DSA tag drivers which do not do anything special
675 * in module init/exit. Each module may only use this macro once, and
676 * calling it replaces module_init() and module_exit().
678 #define module_dsa_tag_drivers(__ops_array) \
679 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
681 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
683 /* Create a static structure we can build a linked list of dsa_tag
686 #define DSA_TAG_DRIVER(__ops) \
687 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
692 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
694 * @__ops: Single tag driver structures
696 * Helper macro for DSA tag drivers which do not do anything special
697 * in module init/exit. Each module may only use this macro once, and
698 * calling it replaces module_init() and module_exit().
700 #define module_dsa_tag_driver(__ops) \
701 DSA_TAG_DRIVER(__ops); \
703 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
704 &DSA_TAG_DRIVER_NAME(__ops) \
706 module_dsa_tag_drivers(dsa_tag_driver_array)