xref: /openbmc/linux/include/net/dsa.h (revision a6a71f19fe5e05a90e0bd2487b87aba60a7bfbe0)
1 /*
2  * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
3  * Copyright (c) 2008-2009 Marvell Semiconductor
4  *
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.
9  */
10 
11 #ifndef __LINUX_NET_DSA_H
12 #define __LINUX_NET_DSA_H
13 
14 #include <linux/if.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>
20 #include <linux/of.h>
21 #include <linux/ethtool.h>
22 #include <net/devlink.h>
23 
24 struct tc_action;
25 struct phy_device;
26 struct fixed_phy_status;
27 
28 enum dsa_tag_protocol {
29 	DSA_TAG_PROTO_NONE = 0,
30 	DSA_TAG_PROTO_DSA,
31 	DSA_TAG_PROTO_TRAILER,
32 	DSA_TAG_PROTO_EDSA,
33 	DSA_TAG_PROTO_BRCM,
34 	DSA_TAG_PROTO_QCA,
35 	DSA_TAG_PROTO_MTK,
36 	DSA_TAG_LAST,		/* MUST BE LAST */
37 };
38 
39 #define DSA_MAX_SWITCHES	4
40 #define DSA_MAX_PORTS		12
41 
42 #define DSA_RTABLE_NONE		-1
43 
44 struct dsa_chip_data {
45 	/*
46 	 * How to access the switch configuration registers.
47 	 */
48 	struct device	*host_dev;
49 	int		sw_addr;
50 
51 	/*
52 	 * Reference to network devices
53 	 */
54 	struct device	*netdev[DSA_MAX_PORTS];
55 
56 	/* set to size of eeprom if supported by the switch */
57 	int		eeprom_len;
58 
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
62 	 */
63 	struct device_node *of_node;
64 
65 	/*
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.
71 	 */
72 	char		*port_names[DSA_MAX_PORTS];
73 	struct device_node *port_dn[DSA_MAX_PORTS];
74 
75 	/*
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.
79 	 */
80 	s8		rtable[DSA_MAX_SWITCHES];
81 };
82 
83 struct dsa_platform_data {
84 	/*
85 	 * Reference to a Linux network interface that connects
86 	 * to the root switch chip of the tree.
87 	 */
88 	struct device	*netdev;
89 	struct net_device *of_netdev;
90 
91 	/*
92 	 * Info structs describing each of the switch chips
93 	 * connected via this network interface.
94 	 */
95 	int		nr_chips;
96 	struct dsa_chip_data	*chip;
97 };
98 
99 struct packet_type;
100 
101 struct dsa_switch_tree {
102 	struct list_head	list;
103 
104 	/* Notifier chain for switch-wide events */
105 	struct raw_notifier_head	nh;
106 
107 	/* Tree identifier */
108 	u32 tree;
109 
110 	/* Number of switches attached to this tree */
111 	struct kref refcount;
112 
113 	/* Has this tree been applied to the hardware? */
114 	bool applied;
115 
116 	/*
117 	 * Configuration data for the platform device that owns
118 	 * this dsa switch tree instance.
119 	 */
120 	struct dsa_platform_data	*pd;
121 
122 	/*
123 	 * Reference to network device to use, and which tagging
124 	 * protocol to use.
125 	 */
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);
131 
132 	/*
133 	 * Original copy of the master netdev ethtool_ops
134 	 */
135 	struct ethtool_ops	master_ethtool_ops;
136 	const struct ethtool_ops *master_orig_ethtool_ops;
137 
138 	/*
139 	 * The switch and port to which the CPU is attached.
140 	 */
141 	struct dsa_switch	*cpu_switch;
142 	s8			cpu_port;
143 
144 	/*
145 	 * Data for the individual switch chips.
146 	 */
147 	struct dsa_switch	*ds[DSA_MAX_SWITCHES];
148 
149 	/*
150 	 * Tagging protocol operations for adding and removing an
151 	 * encapsulation tag.
152 	 */
153 	const struct dsa_device_ops *tag_ops;
154 };
155 
156 /* TC matchall action types, only mirroring for now */
157 enum dsa_port_mall_action_type {
158 	DSA_PORT_MALL_MIRROR,
159 };
160 
161 /* TC mirroring entry */
162 struct dsa_mall_mirror_tc_entry {
163 	u8 to_local_port;
164 	bool ingress;
165 };
166 
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;
172 	union {
173 		struct dsa_mall_mirror_tc_entry mirror;
174 	};
175 };
176 
177 
178 struct dsa_port {
179 	struct dsa_switch	*ds;
180 	unsigned int		index;
181 	const char		*name;
182 	struct net_device	*netdev;
183 	struct device_node	*dn;
184 	unsigned int		ageing_time;
185 	u8			stp_state;
186 	struct net_device	*bridge_dev;
187 	struct devlink_port	devlink_port;
188 };
189 
190 struct dsa_switch {
191 	struct device *dev;
192 
193 	/*
194 	 * Parent switch tree, and switch index.
195 	 */
196 	struct dsa_switch_tree	*dst;
197 	int			index;
198 
199 	/* Listener for switch fabric events */
200 	struct notifier_block	nb;
201 
202 	/*
203 	 * Give the switch driver somewhere to hang its private data
204 	 * structure.
205 	 */
206 	void *priv;
207 
208 	/*
209 	 * Configuration data for this switch.
210 	 */
211 	struct dsa_chip_data	*cd;
212 
213 	/*
214 	 * The switch operations.
215 	 */
216 	const struct dsa_switch_ops	*ops;
217 
218 	/*
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.
222 	 */
223 	s8		rtable[DSA_MAX_SWITCHES];
224 
225 	/*
226 	 * The lower device this switch uses to talk to the host
227 	 */
228 	struct net_device *master_netdev;
229 
230 	/*
231 	 * Slave mii_bus and devices for the individual ports.
232 	 */
233 	u32			dsa_port_mask;
234 	u32			cpu_port_mask;
235 	u32			enabled_port_mask;
236 	u32			phys_mii_mask;
237 	struct mii_bus		*slave_mii_bus;
238 
239 	/* Ageing Time limits in msecs */
240 	unsigned int ageing_time_min;
241 	unsigned int ageing_time_max;
242 
243 	/* devlink used to represent this switch device */
244 	struct devlink		*devlink;
245 
246 	/* Dynamically allocated ports, keep last */
247 	size_t num_ports;
248 	struct dsa_port ports[];
249 };
250 
251 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
252 {
253 	return !!(ds == ds->dst->cpu_switch && p == ds->dst->cpu_port);
254 }
255 
256 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
257 {
258 	return !!((ds->dsa_port_mask) & (1 << p));
259 }
260 
261 static inline bool dsa_is_normal_port(struct dsa_switch *ds, int p)
262 {
263 	return !dsa_is_cpu_port(ds, p) && !dsa_is_dsa_port(ds, p);
264 }
265 
266 static inline bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
267 {
268 	return ds->enabled_port_mask & (1 << p) && ds->ports[p].netdev;
269 }
270 
271 static inline u8 dsa_upstream_port(struct dsa_switch *ds)
272 {
273 	struct dsa_switch_tree *dst = ds->dst;
274 
275 	/*
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.
280 	 */
281 	if (dst->cpu_switch == ds)
282 		return dst->cpu_port;
283 	else
284 		return ds->rtable[dst->cpu_switch->index];
285 }
286 
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;
292 
293 #define DSA_NOTIFIER_BRIDGE_JOIN		1
294 #define DSA_NOTIFIER_BRIDGE_LEAVE		2
295 
296 /* DSA_NOTIFIER_BRIDGE_* */
297 struct dsa_notifier_bridge_info {
298 	struct net_device *br;
299 	int sw_index;
300 	int port;
301 };
302 
303 struct dsa_switch_ops {
304 	/*
305 	 * Legacy probing.
306 	 */
307 	const char	*(*probe)(struct device *dsa_dev,
308 				  struct device *host_dev, int sw_addr,
309 				  void **priv);
310 
311 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds);
312 
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);
316 
317 	/*
318 	 * Access to the switch's PHY registers.
319 	 */
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);
323 
324 	/*
325 	 * Link state adjustment (called from libphy)
326 	 */
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);
331 
332 	/*
333 	 * ethtool hardware statistics.
334 	 */
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);
339 
340 	/*
341 	 * ethtool Wake-on-LAN
342 	 */
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);
347 
348 	/*
349 	 * Suspend and resume
350 	 */
351 	int	(*suspend)(struct dsa_switch *ds);
352 	int	(*resume)(struct dsa_switch *ds);
353 
354 	/*
355 	 * Port enable/disable
356 	 */
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);
361 
362 	/*
363 	 * EEE setttings
364 	 */
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);
370 
371 	/* EEPROM access */
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);
377 
378 	/*
379 	 * Register access.
380 	 */
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);
384 
385 	/*
386 	 * Bridge integration
387 	 */
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,
394 				      u8 state);
395 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
396 
397 	/*
398 	 * VLAN support
399 	 */
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));
413 
414 	/*
415 	 * Forwarding database
416 	 */
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));
428 
429 	/*
430 	 * Multicast database
431 	 */
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));
443 
444 	/*
445 	 * RXNFC
446 	 */
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);
451 
452 	/*
453 	 * TC integration
454 	 */
455 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
456 				   struct dsa_mall_mirror_tc_entry *mirror,
457 				   bool ingress);
458 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
459 				   struct dsa_mall_mirror_tc_entry *mirror);
460 
461 	/*
462 	 * Cross-chip operations
463 	 */
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);
468 };
469 
470 struct dsa_switch_driver {
471 	struct list_head	list;
472 	const struct dsa_switch_ops *ops;
473 };
474 
475 /* Legacy driver registration */
476 void register_switch_driver(struct dsa_switch_driver *type);
477 void unregister_switch_driver(struct dsa_switch_driver *type);
478 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
479 
480 struct net_device *dsa_dev_to_net_device(struct device *dev);
481 
482 static inline bool dsa_uses_tagged_protocol(struct dsa_switch_tree *dst)
483 {
484 	return dst->rcv != NULL;
485 }
486 
487 static inline bool netdev_uses_dsa(struct net_device *dev)
488 {
489 #if IS_ENABLED(CONFIG_NET_DSA)
490 	if (dev->dsa_ptr != NULL)
491 		return dsa_uses_tagged_protocol(dev->dsa_ptr);
492 #endif
493 	return false;
494 }
495 
496 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
497 void dsa_unregister_switch(struct dsa_switch *ds);
498 int dsa_register_switch(struct dsa_switch *ds, struct device *dev);
499 #ifdef CONFIG_PM_SLEEP
500 int dsa_switch_suspend(struct dsa_switch *ds);
501 int dsa_switch_resume(struct dsa_switch *ds);
502 #else
503 static inline int dsa_switch_suspend(struct dsa_switch *ds)
504 {
505 	return 0;
506 }
507 static inline int dsa_switch_resume(struct dsa_switch *ds)
508 {
509 	return 0;
510 }
511 #endif /* CONFIG_PM_SLEEP */
512 
513 #endif
514