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 <net/devlink.h>
26 struct fixed_phy_status
;
28 enum dsa_tag_protocol
{
29 DSA_TAG_PROTO_NONE
= 0,
31 DSA_TAG_PROTO_TRAILER
,
36 DSA_TAG_LAST
, /* MUST BE LAST */
39 #define DSA_MAX_SWITCHES 4
40 #define DSA_MAX_PORTS 12
42 #define DSA_RTABLE_NONE -1
44 struct dsa_chip_data
{
46 * How to access the switch configuration registers.
48 struct device
*host_dev
;
52 * Reference to network devices
54 struct device
*netdev
[DSA_MAX_PORTS
];
56 /* set to size of eeprom if supported by the switch */
59 /* Device tree node pointer for this specific switch chip
60 * used during switch setup in case additional properties
61 * and resources needs to be used
63 struct device_node
*of_node
;
66 * The names of the switch's ports. Use "cpu" to
67 * designate the switch port that the cpu is connected to,
68 * "dsa" to indicate that this port is a DSA link to
69 * another switch, NULL to indicate the port is unused,
70 * or any other string to indicate this is a physical port.
72 char *port_names
[DSA_MAX_PORTS
];
73 struct device_node
*port_dn
[DSA_MAX_PORTS
];
76 * An array of which element [a] indicates which port on this
77 * switch should be used to send packets to that are destined
78 * for switch a. Can be NULL if there is only one switch chip.
80 s8 rtable
[DSA_MAX_SWITCHES
];
83 struct dsa_platform_data
{
85 * Reference to a Linux network interface that connects
86 * to the root switch chip of the tree.
88 struct device
*netdev
;
89 struct net_device
*of_netdev
;
92 * Info structs describing each of the switch chips
93 * connected via this network interface.
96 struct dsa_chip_data
*chip
;
101 struct dsa_switch_tree
{
102 struct list_head list
;
104 /* Notifier chain for switch-wide events */
105 struct raw_notifier_head nh
;
107 /* Tree identifier */
110 /* Number of switches attached to this tree */
111 struct kref refcount
;
113 /* Has this tree been applied to the hardware? */
117 * Configuration data for the platform device that owns
118 * this dsa switch tree instance.
120 struct dsa_platform_data
*pd
;
123 * Reference to network device to use, and which tagging
126 struct net_device
*master_netdev
;
127 struct sk_buff
* (*rcv
)(struct sk_buff
*skb
,
128 struct net_device
*dev
,
129 struct packet_type
*pt
,
130 struct net_device
*orig_dev
);
133 * Original copy of the master netdev ethtool_ops
135 struct ethtool_ops master_ethtool_ops
;
136 const struct ethtool_ops
*master_orig_ethtool_ops
;
139 * The switch and port to which the CPU is attached.
141 struct dsa_switch
*cpu_switch
;
145 * Data for the individual switch chips.
147 struct dsa_switch
*ds
[DSA_MAX_SWITCHES
];
150 * Tagging protocol operations for adding and removing an
153 const struct dsa_device_ops
*tag_ops
;
156 /* TC matchall action types, only mirroring for now */
157 enum dsa_port_mall_action_type
{
158 DSA_PORT_MALL_MIRROR
,
161 /* TC mirroring entry */
162 struct dsa_mall_mirror_tc_entry
{
167 /* TC matchall entry */
168 struct dsa_mall_tc_entry
{
169 struct list_head list
;
170 unsigned long cookie
;
171 enum dsa_port_mall_action_type type
;
173 struct dsa_mall_mirror_tc_entry mirror
;
179 struct dsa_switch
*ds
;
182 struct net_device
*netdev
;
183 struct device_node
*dn
;
184 unsigned int ageing_time
;
186 struct net_device
*bridge_dev
;
187 struct devlink_port devlink_port
;
194 * Parent switch tree, and switch index.
196 struct dsa_switch_tree
*dst
;
199 /* Listener for switch fabric events */
200 struct notifier_block nb
;
203 * Give the switch driver somewhere to hang its private data
209 * Configuration data for this switch.
211 struct dsa_chip_data
*cd
;
214 * The switch operations.
216 const struct dsa_switch_ops
*ops
;
219 * An array of which element [a] indicates which port on this
220 * switch should be used to send packets to that are destined
221 * for switch a. Can be NULL if there is only one switch chip.
223 s8 rtable
[DSA_MAX_SWITCHES
];
226 * The lower device this switch uses to talk to the host
228 struct net_device
*master_netdev
;
231 * Slave mii_bus and devices for the individual ports.
235 u32 enabled_port_mask
;
237 struct mii_bus
*slave_mii_bus
;
239 /* Ageing Time limits in msecs */
240 unsigned int ageing_time_min
;
241 unsigned int ageing_time_max
;
243 /* devlink used to represent this switch device */
244 struct devlink
*devlink
;
246 /* Dynamically allocated ports, keep last */
248 struct dsa_port ports
[];
251 static inline bool dsa_is_cpu_port(struct dsa_switch
*ds
, int p
)
253 return !!(ds
== ds
->dst
->cpu_switch
&& p
== ds
->dst
->cpu_port
);
256 static inline bool dsa_is_dsa_port(struct dsa_switch
*ds
, int p
)
258 return !!((ds
->dsa_port_mask
) & (1 << p
));
261 static inline bool dsa_is_normal_port(struct dsa_switch
*ds
, int p
)
263 return !dsa_is_cpu_port(ds
, p
) && !dsa_is_dsa_port(ds
, p
);
266 static inline bool dsa_is_port_initialized(struct dsa_switch
*ds
, int p
)
268 return ds
->enabled_port_mask
& (1 << p
) && ds
->ports
[p
].netdev
;
271 static inline u8
dsa_upstream_port(struct dsa_switch
*ds
)
273 struct dsa_switch_tree
*dst
= ds
->dst
;
276 * If this is the root switch (i.e. the switch that connects
277 * to the CPU), return the cpu port number on this switch.
278 * Else return the (DSA) port number that connects to the
279 * switch that is one hop closer to the cpu.
281 if (dst
->cpu_switch
== ds
)
282 return dst
->cpu_port
;
284 return ds
->rtable
[dst
->cpu_switch
->index
];
287 struct switchdev_trans
;
288 struct switchdev_obj
;
289 struct switchdev_obj_port_fdb
;
290 struct switchdev_obj_port_mdb
;
291 struct switchdev_obj_port_vlan
;
293 #define DSA_NOTIFIER_BRIDGE_JOIN 1
294 #define DSA_NOTIFIER_BRIDGE_LEAVE 2
296 /* DSA_NOTIFIER_BRIDGE_* */
297 struct dsa_notifier_bridge_info
{
298 struct net_device
*br
;
303 struct dsa_switch_ops
{
307 const char *(*probe
)(struct device
*dsa_dev
,
308 struct device
*host_dev
, int sw_addr
,
311 enum dsa_tag_protocol (*get_tag_protocol
)(struct dsa_switch
*ds
);
313 int (*setup
)(struct dsa_switch
*ds
);
314 int (*set_addr
)(struct dsa_switch
*ds
, u8
*addr
);
315 u32 (*get_phy_flags
)(struct dsa_switch
*ds
, int port
);
318 * Access to the switch's PHY registers.
320 int (*phy_read
)(struct dsa_switch
*ds
, int port
, int regnum
);
321 int (*phy_write
)(struct dsa_switch
*ds
, int port
,
322 int regnum
, u16 val
);
325 * Link state adjustment (called from libphy)
327 void (*adjust_link
)(struct dsa_switch
*ds
, int port
,
328 struct phy_device
*phydev
);
329 void (*fixed_link_update
)(struct dsa_switch
*ds
, int port
,
330 struct fixed_phy_status
*st
);
333 * ethtool hardware statistics.
335 void (*get_strings
)(struct dsa_switch
*ds
, int port
, uint8_t *data
);
336 void (*get_ethtool_stats
)(struct dsa_switch
*ds
,
337 int port
, uint64_t *data
);
338 int (*get_sset_count
)(struct dsa_switch
*ds
);
341 * ethtool Wake-on-LAN
343 void (*get_wol
)(struct dsa_switch
*ds
, int port
,
344 struct ethtool_wolinfo
*w
);
345 int (*set_wol
)(struct dsa_switch
*ds
, int port
,
346 struct ethtool_wolinfo
*w
);
351 int (*suspend
)(struct dsa_switch
*ds
);
352 int (*resume
)(struct dsa_switch
*ds
);
355 * Port enable/disable
357 int (*port_enable
)(struct dsa_switch
*ds
, int port
,
358 struct phy_device
*phy
);
359 void (*port_disable
)(struct dsa_switch
*ds
, int port
,
360 struct phy_device
*phy
);
365 int (*set_eee
)(struct dsa_switch
*ds
, int port
,
366 struct phy_device
*phydev
,
367 struct ethtool_eee
*e
);
368 int (*get_eee
)(struct dsa_switch
*ds
, int port
,
369 struct ethtool_eee
*e
);
372 int (*get_eeprom_len
)(struct dsa_switch
*ds
);
373 int (*get_eeprom
)(struct dsa_switch
*ds
,
374 struct ethtool_eeprom
*eeprom
, u8
*data
);
375 int (*set_eeprom
)(struct dsa_switch
*ds
,
376 struct ethtool_eeprom
*eeprom
, u8
*data
);
381 int (*get_regs_len
)(struct dsa_switch
*ds
, int port
);
382 void (*get_regs
)(struct dsa_switch
*ds
, int port
,
383 struct ethtool_regs
*regs
, void *p
);
388 int (*set_ageing_time
)(struct dsa_switch
*ds
, unsigned int msecs
);
389 int (*port_bridge_join
)(struct dsa_switch
*ds
, int port
,
390 struct net_device
*bridge
);
391 void (*port_bridge_leave
)(struct dsa_switch
*ds
, int port
,
392 struct net_device
*bridge
);
393 void (*port_stp_state_set
)(struct dsa_switch
*ds
, int port
,
395 void (*port_fast_age
)(struct dsa_switch
*ds
, int port
);
400 int (*port_vlan_filtering
)(struct dsa_switch
*ds
, int port
,
401 bool vlan_filtering
);
402 int (*port_vlan_prepare
)(struct dsa_switch
*ds
, int port
,
403 const struct switchdev_obj_port_vlan
*vlan
,
404 struct switchdev_trans
*trans
);
405 void (*port_vlan_add
)(struct dsa_switch
*ds
, int port
,
406 const struct switchdev_obj_port_vlan
*vlan
,
407 struct switchdev_trans
*trans
);
408 int (*port_vlan_del
)(struct dsa_switch
*ds
, int port
,
409 const struct switchdev_obj_port_vlan
*vlan
);
410 int (*port_vlan_dump
)(struct dsa_switch
*ds
, int port
,
411 struct switchdev_obj_port_vlan
*vlan
,
412 int (*cb
)(struct switchdev_obj
*obj
));
415 * Forwarding database
417 int (*port_fdb_prepare
)(struct dsa_switch
*ds
, int port
,
418 const struct switchdev_obj_port_fdb
*fdb
,
419 struct switchdev_trans
*trans
);
420 void (*port_fdb_add
)(struct dsa_switch
*ds
, int port
,
421 const struct switchdev_obj_port_fdb
*fdb
,
422 struct switchdev_trans
*trans
);
423 int (*port_fdb_del
)(struct dsa_switch
*ds
, int port
,
424 const struct switchdev_obj_port_fdb
*fdb
);
425 int (*port_fdb_dump
)(struct dsa_switch
*ds
, int port
,
426 struct switchdev_obj_port_fdb
*fdb
,
427 int (*cb
)(struct switchdev_obj
*obj
));
432 int (*port_mdb_prepare
)(struct dsa_switch
*ds
, int port
,
433 const struct switchdev_obj_port_mdb
*mdb
,
434 struct switchdev_trans
*trans
);
435 void (*port_mdb_add
)(struct dsa_switch
*ds
, int port
,
436 const struct switchdev_obj_port_mdb
*mdb
,
437 struct switchdev_trans
*trans
);
438 int (*port_mdb_del
)(struct dsa_switch
*ds
, int port
,
439 const struct switchdev_obj_port_mdb
*mdb
);
440 int (*port_mdb_dump
)(struct dsa_switch
*ds
, int port
,
441 struct switchdev_obj_port_mdb
*mdb
,
442 int (*cb
)(struct switchdev_obj
*obj
));
447 int (*get_rxnfc
)(struct dsa_switch
*ds
, int port
,
448 struct ethtool_rxnfc
*nfc
, u32
*rule_locs
);
449 int (*set_rxnfc
)(struct dsa_switch
*ds
, int port
,
450 struct ethtool_rxnfc
*nfc
);
455 int (*port_mirror_add
)(struct dsa_switch
*ds
, int port
,
456 struct dsa_mall_mirror_tc_entry
*mirror
,
458 void (*port_mirror_del
)(struct dsa_switch
*ds
, int port
,
459 struct dsa_mall_mirror_tc_entry
*mirror
);
462 * Cross-chip operations
464 int (*crosschip_bridge_join
)(struct dsa_switch
*ds
, int sw_index
,
465 int port
, struct net_device
*br
);
466 void (*crosschip_bridge_leave
)(struct dsa_switch
*ds
, int sw_index
,
467 int port
, struct net_device
*br
);
470 struct dsa_switch_driver
{
471 struct list_head list
;
472 const struct dsa_switch_ops
*ops
;
475 void register_switch_driver(struct dsa_switch_driver
*type
);
476 void unregister_switch_driver(struct dsa_switch_driver
*type
);
477 struct mii_bus
*dsa_host_dev_to_mii_bus(struct device
*dev
);
478 struct net_device
*dsa_dev_to_net_device(struct device
*dev
);
480 static inline bool dsa_uses_tagged_protocol(struct dsa_switch_tree
*dst
)
482 return dst
->rcv
!= NULL
;
485 static inline bool netdev_uses_dsa(struct net_device
*dev
)
487 #if IS_ENABLED(CONFIG_NET_DSA)
488 if (dev
->dsa_ptr
!= NULL
)
489 return dsa_uses_tagged_protocol(dev
->dsa_ptr
);
494 struct dsa_switch
*dsa_switch_alloc(struct device
*dev
, size_t n
);
495 void dsa_unregister_switch(struct dsa_switch
*ds
);
496 int dsa_register_switch(struct dsa_switch
*ds
, struct device
*dev
);
497 #ifdef CONFIG_PM_SLEEP
498 int dsa_switch_suspend(struct dsa_switch
*ds
);
499 int dsa_switch_resume(struct dsa_switch
*ds
);
501 static inline int dsa_switch_suspend(struct dsa_switch
*ds
)
505 static inline int dsa_switch_resume(struct dsa_switch
*ds
)
509 #endif /* CONFIG_PM_SLEEP */