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 enum dsa_tag_protocol {
34 DSA_TAG_PROTO_NONE = 0,
36 DSA_TAG_PROTO_BRCM_PREPEND,
40 DSA_TAG_PROTO_KSZ9477,
41 DSA_TAG_PROTO_KSZ9893,
42 DSA_TAG_PROTO_LAN9303,
45 DSA_TAG_PROTO_TRAILER,
46 DSA_TAG_LAST, /* MUST BE LAST */
52 struct dsa_device_ops {
53 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
54 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
55 struct packet_type *pt);
56 int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
58 unsigned int overhead;
62 struct dsa_switch_tree {
63 struct list_head list;
65 /* Notifier chain for switch-wide events */
66 struct raw_notifier_head nh;
71 /* Number of switches attached to this tree */
74 /* Has this tree been applied to the hardware? */
78 * Configuration data for the platform device that owns
79 * this dsa switch tree instance.
81 struct dsa_platform_data *pd;
84 * The switch port to which the CPU is attached.
86 struct dsa_port *cpu_dp;
89 * Data for the individual switch chips.
91 struct dsa_switch *ds[DSA_MAX_SWITCHES];
94 /* TC matchall action types, only mirroring for now */
95 enum dsa_port_mall_action_type {
99 /* TC mirroring entry */
100 struct dsa_mall_mirror_tc_entry {
105 /* TC matchall entry */
106 struct dsa_mall_tc_entry {
107 struct list_head list;
108 unsigned long cookie;
109 enum dsa_port_mall_action_type type;
111 struct dsa_mall_mirror_tc_entry mirror;
117 /* A CPU port is physically connected to a master device.
118 * A user port exposed to userspace has a slave device.
121 struct net_device *master;
122 struct net_device *slave;
125 /* CPU port tagging operations used by master or slave devices */
126 const struct dsa_device_ops *tag_ops;
128 /* Copies for faster access in master receive hot path */
129 struct dsa_switch_tree *dst;
130 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
131 struct packet_type *pt);
134 DSA_PORT_TYPE_UNUSED = 0,
140 struct dsa_switch *ds;
143 const struct dsa_port *cpu_dp;
145 struct device_node *dn;
146 unsigned int ageing_time;
148 struct net_device *bridge_dev;
149 struct devlink_port devlink_port;
152 * Original copy of the master netdev ethtool_ops
154 const struct ethtool_ops *orig_ethtool_ops;
157 * Original copy of the master netdev net_device_ops
159 const struct net_device_ops *orig_ndo_ops;
166 * Parent switch tree, and switch index.
168 struct dsa_switch_tree *dst;
171 /* Listener for switch fabric events */
172 struct notifier_block nb;
175 * Give the switch driver somewhere to hang its private data
181 * Configuration data for this switch.
183 struct dsa_chip_data *cd;
186 * The switch operations.
188 const struct dsa_switch_ops *ops;
191 * An array of which element [a] indicates which port on this
192 * switch should be used to send packets to that are destined
193 * for switch a. Can be NULL if there is only one switch chip.
195 s8 rtable[DSA_MAX_SWITCHES];
198 * Slave mii_bus and devices for the individual ports.
201 struct mii_bus *slave_mii_bus;
203 /* Ageing Time limits in msecs */
204 unsigned int ageing_time_min;
205 unsigned int ageing_time_max;
207 /* devlink used to represent this switch device */
208 struct devlink *devlink;
210 /* Number of switch port queues */
211 unsigned int num_tx_queues;
213 unsigned long *bitmap;
214 unsigned long _bitmap;
216 /* Dynamically allocated ports, keep last */
218 struct dsa_port ports[];
221 static inline const struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
223 return &ds->ports[p];
226 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
228 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
231 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
233 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
236 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
238 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
241 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
243 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
246 static inline u32 dsa_user_ports(struct dsa_switch *ds)
251 for (p = 0; p < ds->num_ports; p++)
252 if (dsa_is_user_port(ds, p))
258 /* Return the local port used to reach an arbitrary switch port */
259 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
262 if (device == ds->index)
265 return ds->rtable[device];
268 /* Return the local port used to reach the dedicated CPU port */
269 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
271 const struct dsa_port *dp = dsa_to_port(ds, port);
272 const struct dsa_port *cpu_dp = dp->cpu_dp;
277 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
280 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
281 bool is_static, void *data);
282 struct dsa_switch_ops {
283 #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
287 const char *(*probe)(struct device *dsa_dev,
288 struct device *host_dev, int sw_addr,
292 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
295 int (*setup)(struct dsa_switch *ds);
296 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
299 * Access to the switch's PHY registers.
301 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
302 int (*phy_write)(struct dsa_switch *ds, int port,
303 int regnum, u16 val);
306 * Link state adjustment (called from libphy)
308 void (*adjust_link)(struct dsa_switch *ds, int port,
309 struct phy_device *phydev);
310 void (*fixed_link_update)(struct dsa_switch *ds, int port,
311 struct fixed_phy_status *st);
314 * PHYLINK integration
316 void (*phylink_validate)(struct dsa_switch *ds, int port,
317 unsigned long *supported,
318 struct phylink_link_state *state);
319 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
320 struct phylink_link_state *state);
321 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
323 const struct phylink_link_state *state);
324 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
325 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
327 phy_interface_t interface);
328 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
330 phy_interface_t interface,
331 struct phy_device *phydev);
332 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
333 struct phylink_link_state *state);
335 * ethtool hardware statistics.
337 void (*get_strings)(struct dsa_switch *ds, int port,
338 u32 stringset, uint8_t *data);
339 void (*get_ethtool_stats)(struct dsa_switch *ds,
340 int port, uint64_t *data);
341 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
342 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
343 int port, uint64_t *data);
346 * ethtool Wake-on-LAN
348 void (*get_wol)(struct dsa_switch *ds, int port,
349 struct ethtool_wolinfo *w);
350 int (*set_wol)(struct dsa_switch *ds, int port,
351 struct ethtool_wolinfo *w);
354 * ethtool timestamp info
356 int (*get_ts_info)(struct dsa_switch *ds, int port,
357 struct ethtool_ts_info *ts);
362 int (*suspend)(struct dsa_switch *ds);
363 int (*resume)(struct dsa_switch *ds);
366 * Port enable/disable
368 int (*port_enable)(struct dsa_switch *ds, int port,
369 struct phy_device *phy);
370 void (*port_disable)(struct dsa_switch *ds, int port);
373 * Port's MAC EEE settings
375 int (*set_mac_eee)(struct dsa_switch *ds, int port,
376 struct ethtool_eee *e);
377 int (*get_mac_eee)(struct dsa_switch *ds, int port,
378 struct ethtool_eee *e);
381 int (*get_eeprom_len)(struct dsa_switch *ds);
382 int (*get_eeprom)(struct dsa_switch *ds,
383 struct ethtool_eeprom *eeprom, u8 *data);
384 int (*set_eeprom)(struct dsa_switch *ds,
385 struct ethtool_eeprom *eeprom, u8 *data);
390 int (*get_regs_len)(struct dsa_switch *ds, int port);
391 void (*get_regs)(struct dsa_switch *ds, int port,
392 struct ethtool_regs *regs, void *p);
397 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
398 int (*port_bridge_join)(struct dsa_switch *ds, int port,
399 struct net_device *bridge);
400 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
401 struct net_device *bridge);
402 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
404 void (*port_fast_age)(struct dsa_switch *ds, int port);
405 int (*port_egress_floods)(struct dsa_switch *ds, int port,
406 bool unicast, bool multicast);
411 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
412 bool vlan_filtering);
413 int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
414 const struct switchdev_obj_port_vlan *vlan);
415 void (*port_vlan_add)(struct dsa_switch *ds, int port,
416 const struct switchdev_obj_port_vlan *vlan);
417 int (*port_vlan_del)(struct dsa_switch *ds, int port,
418 const struct switchdev_obj_port_vlan *vlan);
420 * Forwarding database
422 int (*port_fdb_add)(struct dsa_switch *ds, int port,
423 const unsigned char *addr, u16 vid);
424 int (*port_fdb_del)(struct dsa_switch *ds, int port,
425 const unsigned char *addr, u16 vid);
426 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
427 dsa_fdb_dump_cb_t *cb, void *data);
432 int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
433 const struct switchdev_obj_port_mdb *mdb);
434 void (*port_mdb_add)(struct dsa_switch *ds, int port,
435 const struct switchdev_obj_port_mdb *mdb);
436 int (*port_mdb_del)(struct dsa_switch *ds, int port,
437 const struct switchdev_obj_port_mdb *mdb);
441 int (*get_rxnfc)(struct dsa_switch *ds, int port,
442 struct ethtool_rxnfc *nfc, u32 *rule_locs);
443 int (*set_rxnfc)(struct dsa_switch *ds, int port,
444 struct ethtool_rxnfc *nfc);
449 int (*port_mirror_add)(struct dsa_switch *ds, int port,
450 struct dsa_mall_mirror_tc_entry *mirror,
452 void (*port_mirror_del)(struct dsa_switch *ds, int port,
453 struct dsa_mall_mirror_tc_entry *mirror);
456 * Cross-chip operations
458 int (*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
459 int port, struct net_device *br);
460 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
461 int port, struct net_device *br);
466 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
468 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
470 bool (*port_txtstamp)(struct dsa_switch *ds, int port,
471 struct sk_buff *clone, unsigned int type);
472 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
473 struct sk_buff *skb, unsigned int type);
476 struct dsa_switch_driver {
477 struct list_head list;
478 const struct dsa_switch_ops *ops;
481 #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
482 /* Legacy driver registration */
483 void register_switch_driver(struct dsa_switch_driver *type);
484 void unregister_switch_driver(struct dsa_switch_driver *type);
485 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
488 static inline void register_switch_driver(struct dsa_switch_driver *type) { }
489 static inline void unregister_switch_driver(struct dsa_switch_driver *type) { }
490 static inline struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
495 struct net_device *dsa_dev_to_net_device(struct device *dev);
497 /* Keep inline for faster access in hot path */
498 static inline bool netdev_uses_dsa(struct net_device *dev)
500 #if IS_ENABLED(CONFIG_NET_DSA)
501 return dev->dsa_ptr && dev->dsa_ptr->rcv;
506 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
507 void dsa_unregister_switch(struct dsa_switch *ds);
508 int dsa_register_switch(struct dsa_switch *ds);
509 #ifdef CONFIG_PM_SLEEP
510 int dsa_switch_suspend(struct dsa_switch *ds);
511 int dsa_switch_resume(struct dsa_switch *ds);
513 static inline int dsa_switch_suspend(struct dsa_switch *ds)
517 static inline int dsa_switch_resume(struct dsa_switch *ds)
521 #endif /* CONFIG_PM_SLEEP */
523 enum dsa_notifier_type {
528 struct dsa_notifier_info {
529 struct net_device *dev;
532 struct dsa_notifier_register_info {
533 struct dsa_notifier_info info; /* must be first */
534 struct net_device *master;
535 unsigned int port_number;
536 unsigned int switch_number;
539 static inline struct net_device *
540 dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
545 #if IS_ENABLED(CONFIG_NET_DSA)
546 int register_dsa_notifier(struct notifier_block *nb);
547 int unregister_dsa_notifier(struct notifier_block *nb);
548 int call_dsa_notifiers(unsigned long val, struct net_device *dev,
549 struct dsa_notifier_info *info);
551 static inline int register_dsa_notifier(struct notifier_block *nb)
556 static inline int unregister_dsa_notifier(struct notifier_block *nb)
561 static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
562 struct dsa_notifier_info *info)
568 /* Broadcom tag specific helpers to insert and extract queue/port number */
569 #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q)
570 #define BRCM_TAG_GET_PORT(v) ((v) >> 8)
571 #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff)
574 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
575 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
576 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
577 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);