xref: /openbmc/linux/include/net/dsa.h (revision f50f212749e8a28803af3628acbeb85ee0458ed5)
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_ether.h>
15 #include <linux/list.h>
16 #include <linux/timer.h>
17 #include <linux/workqueue.h>
18 #include <linux/of.h>
19 #include <linux/phy.h>
20 #include <linux/phy_fixed.h>
21 #include <linux/ethtool.h>
22 
23 struct tc_action;
24 
25 enum dsa_tag_protocol {
26 	DSA_TAG_PROTO_NONE = 0,
27 	DSA_TAG_PROTO_DSA,
28 	DSA_TAG_PROTO_TRAILER,
29 	DSA_TAG_PROTO_EDSA,
30 	DSA_TAG_PROTO_BRCM,
31 	DSA_TAG_PROTO_QCA,
32 	DSA_TAG_LAST,		/* MUST BE LAST */
33 };
34 
35 #define DSA_MAX_SWITCHES	4
36 #define DSA_MAX_PORTS		12
37 
38 #define DSA_RTABLE_NONE		-1
39 
40 struct dsa_chip_data {
41 	/*
42 	 * How to access the switch configuration registers.
43 	 */
44 	struct device	*host_dev;
45 	int		sw_addr;
46 
47 	/* set to size of eeprom if supported by the switch */
48 	int		eeprom_len;
49 
50 	/* Device tree node pointer for this specific switch chip
51 	 * used during switch setup in case additional properties
52 	 * and resources needs to be used
53 	 */
54 	struct device_node *of_node;
55 
56 	/*
57 	 * The names of the switch's ports.  Use "cpu" to
58 	 * designate the switch port that the cpu is connected to,
59 	 * "dsa" to indicate that this port is a DSA link to
60 	 * another switch, NULL to indicate the port is unused,
61 	 * or any other string to indicate this is a physical port.
62 	 */
63 	char		*port_names[DSA_MAX_PORTS];
64 	struct device_node *port_dn[DSA_MAX_PORTS];
65 
66 	/*
67 	 * An array of which element [a] indicates which port on this
68 	 * switch should be used to send packets to that are destined
69 	 * for switch a. Can be NULL if there is only one switch chip.
70 	 */
71 	s8		rtable[DSA_MAX_SWITCHES];
72 };
73 
74 struct dsa_platform_data {
75 	/*
76 	 * Reference to a Linux network interface that connects
77 	 * to the root switch chip of the tree.
78 	 */
79 	struct device	*netdev;
80 	struct net_device *of_netdev;
81 
82 	/*
83 	 * Info structs describing each of the switch chips
84 	 * connected via this network interface.
85 	 */
86 	int		nr_chips;
87 	struct dsa_chip_data	*chip;
88 };
89 
90 struct packet_type;
91 
92 struct dsa_switch_tree {
93 	struct list_head	list;
94 
95 	/* Tree identifier */
96 	u32 tree;
97 
98 	/* Number of switches attached to this tree */
99 	struct kref refcount;
100 
101 	/* Has this tree been applied to the hardware? */
102 	bool applied;
103 
104 	/*
105 	 * Configuration data for the platform device that owns
106 	 * this dsa switch tree instance.
107 	 */
108 	struct dsa_platform_data	*pd;
109 
110 	/*
111 	 * Reference to network device to use, and which tagging
112 	 * protocol to use.
113 	 */
114 	struct net_device	*master_netdev;
115 	int			(*rcv)(struct sk_buff *skb,
116 				       struct net_device *dev,
117 				       struct packet_type *pt,
118 				       struct net_device *orig_dev);
119 
120 	/*
121 	 * Original copy of the master netdev ethtool_ops
122 	 */
123 	struct ethtool_ops	master_ethtool_ops;
124 	const struct ethtool_ops *master_orig_ethtool_ops;
125 
126 	/*
127 	 * The switch and port to which the CPU is attached.
128 	 */
129 	struct dsa_switch	*cpu_switch;
130 	s8			cpu_port;
131 
132 	/*
133 	 * Data for the individual switch chips.
134 	 */
135 	struct dsa_switch	*ds[DSA_MAX_SWITCHES];
136 
137 	/*
138 	 * Tagging protocol operations for adding and removing an
139 	 * encapsulation tag.
140 	 */
141 	const struct dsa_device_ops *tag_ops;
142 };
143 
144 /* TC matchall action types, only mirroring for now */
145 enum dsa_port_mall_action_type {
146 	DSA_PORT_MALL_MIRROR,
147 };
148 
149 /* TC mirroring entry */
150 struct dsa_mall_mirror_tc_entry {
151 	u8 to_local_port;
152 	bool ingress;
153 };
154 
155 /* TC matchall entry */
156 struct dsa_mall_tc_entry {
157 	struct list_head list;
158 	unsigned long cookie;
159 	enum dsa_port_mall_action_type type;
160 	union {
161 		struct dsa_mall_mirror_tc_entry mirror;
162 	};
163 };
164 
165 
166 struct dsa_port {
167 	struct dsa_switch	*ds;
168 	unsigned int		index;
169 	struct net_device	*netdev;
170 	struct device_node	*dn;
171 	unsigned int		ageing_time;
172 	u8			stp_state;
173 	struct net_device	*bridge_dev;
174 };
175 
176 struct dsa_switch {
177 	struct device *dev;
178 
179 	/*
180 	 * Parent switch tree, and switch index.
181 	 */
182 	struct dsa_switch_tree	*dst;
183 	int			index;
184 
185 	/*
186 	 * Give the switch driver somewhere to hang its private data
187 	 * structure.
188 	 */
189 	void *priv;
190 
191 	/*
192 	 * Configuration data for this switch.
193 	 */
194 	struct dsa_chip_data	*cd;
195 
196 	/*
197 	 * The switch operations.
198 	 */
199 	const struct dsa_switch_ops	*ops;
200 
201 	/*
202 	 * An array of which element [a] indicates which port on this
203 	 * switch should be used to send packets to that are destined
204 	 * for switch a. Can be NULL if there is only one switch chip.
205 	 */
206 	s8		rtable[DSA_MAX_SWITCHES];
207 
208 	/*
209 	 * The lower device this switch uses to talk to the host
210 	 */
211 	struct net_device *master_netdev;
212 
213 	/*
214 	 * Slave mii_bus and devices for the individual ports.
215 	 */
216 	u32			dsa_port_mask;
217 	u32			cpu_port_mask;
218 	u32			enabled_port_mask;
219 	u32			phys_mii_mask;
220 	struct mii_bus		*slave_mii_bus;
221 
222 	/* Dynamically allocated ports, keep last */
223 	size_t num_ports;
224 	struct dsa_port ports[];
225 };
226 
227 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
228 {
229 	return !!(ds == ds->dst->cpu_switch && p == ds->dst->cpu_port);
230 }
231 
232 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
233 {
234 	return !!((ds->dsa_port_mask) & (1 << p));
235 }
236 
237 static inline bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
238 {
239 	return ds->enabled_port_mask & (1 << p) && ds->ports[p].netdev;
240 }
241 
242 static inline u8 dsa_upstream_port(struct dsa_switch *ds)
243 {
244 	struct dsa_switch_tree *dst = ds->dst;
245 
246 	/*
247 	 * If this is the root switch (i.e. the switch that connects
248 	 * to the CPU), return the cpu port number on this switch.
249 	 * Else return the (DSA) port number that connects to the
250 	 * switch that is one hop closer to the cpu.
251 	 */
252 	if (dst->cpu_switch == ds)
253 		return dst->cpu_port;
254 	else
255 		return ds->rtable[dst->cpu_switch->index];
256 }
257 
258 struct switchdev_trans;
259 struct switchdev_obj;
260 struct switchdev_obj_port_fdb;
261 struct switchdev_obj_port_mdb;
262 struct switchdev_obj_port_vlan;
263 
264 struct dsa_switch_ops {
265 	/*
266 	 * Probing and setup.
267 	 */
268 	const char	*(*probe)(struct device *dsa_dev,
269 				  struct device *host_dev, int sw_addr,
270 				  void **priv);
271 
272 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds);
273 
274 	int	(*setup)(struct dsa_switch *ds);
275 	int	(*set_addr)(struct dsa_switch *ds, u8 *addr);
276 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
277 
278 	/*
279 	 * Access to the switch's PHY registers.
280 	 */
281 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
282 	int	(*phy_write)(struct dsa_switch *ds, int port,
283 			     int regnum, u16 val);
284 
285 	/*
286 	 * Link state adjustment (called from libphy)
287 	 */
288 	void	(*adjust_link)(struct dsa_switch *ds, int port,
289 				struct phy_device *phydev);
290 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
291 				struct fixed_phy_status *st);
292 
293 	/*
294 	 * ethtool hardware statistics.
295 	 */
296 	void	(*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
297 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
298 				     int port, uint64_t *data);
299 	int	(*get_sset_count)(struct dsa_switch *ds);
300 
301 	/*
302 	 * ethtool Wake-on-LAN
303 	 */
304 	void	(*get_wol)(struct dsa_switch *ds, int port,
305 			   struct ethtool_wolinfo *w);
306 	int	(*set_wol)(struct dsa_switch *ds, int port,
307 			   struct ethtool_wolinfo *w);
308 
309 	/*
310 	 * Suspend and resume
311 	 */
312 	int	(*suspend)(struct dsa_switch *ds);
313 	int	(*resume)(struct dsa_switch *ds);
314 
315 	/*
316 	 * Port enable/disable
317 	 */
318 	int	(*port_enable)(struct dsa_switch *ds, int port,
319 			       struct phy_device *phy);
320 	void	(*port_disable)(struct dsa_switch *ds, int port,
321 				struct phy_device *phy);
322 
323 	/*
324 	 * EEE setttings
325 	 */
326 	int	(*set_eee)(struct dsa_switch *ds, int port,
327 			   struct phy_device *phydev,
328 			   struct ethtool_eee *e);
329 	int	(*get_eee)(struct dsa_switch *ds, int port,
330 			   struct ethtool_eee *e);
331 
332 	/* EEPROM access */
333 	int	(*get_eeprom_len)(struct dsa_switch *ds);
334 	int	(*get_eeprom)(struct dsa_switch *ds,
335 			      struct ethtool_eeprom *eeprom, u8 *data);
336 	int	(*set_eeprom)(struct dsa_switch *ds,
337 			      struct ethtool_eeprom *eeprom, u8 *data);
338 
339 	/*
340 	 * Register access.
341 	 */
342 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
343 	void	(*get_regs)(struct dsa_switch *ds, int port,
344 			    struct ethtool_regs *regs, void *p);
345 
346 	/*
347 	 * Bridge integration
348 	 */
349 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
350 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
351 				    struct net_device *bridge);
352 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
353 				     struct net_device *bridge);
354 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
355 				      u8 state);
356 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
357 
358 	/*
359 	 * VLAN support
360 	 */
361 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
362 				       bool vlan_filtering);
363 	int	(*port_vlan_prepare)(struct dsa_switch *ds, int port,
364 				     const struct switchdev_obj_port_vlan *vlan,
365 				     struct switchdev_trans *trans);
366 	void	(*port_vlan_add)(struct dsa_switch *ds, int port,
367 				 const struct switchdev_obj_port_vlan *vlan,
368 				 struct switchdev_trans *trans);
369 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
370 				 const struct switchdev_obj_port_vlan *vlan);
371 	int	(*port_vlan_dump)(struct dsa_switch *ds, int port,
372 				  struct switchdev_obj_port_vlan *vlan,
373 				  int (*cb)(struct switchdev_obj *obj));
374 
375 	/*
376 	 * Forwarding database
377 	 */
378 	int	(*port_fdb_prepare)(struct dsa_switch *ds, int port,
379 				    const struct switchdev_obj_port_fdb *fdb,
380 				    struct switchdev_trans *trans);
381 	void	(*port_fdb_add)(struct dsa_switch *ds, int port,
382 				const struct switchdev_obj_port_fdb *fdb,
383 				struct switchdev_trans *trans);
384 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
385 				const struct switchdev_obj_port_fdb *fdb);
386 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
387 				 struct switchdev_obj_port_fdb *fdb,
388 				 int (*cb)(struct switchdev_obj *obj));
389 
390 	/*
391 	 * Multicast database
392 	 */
393 	int	(*port_mdb_prepare)(struct dsa_switch *ds, int port,
394 				    const struct switchdev_obj_port_mdb *mdb,
395 				    struct switchdev_trans *trans);
396 	void	(*port_mdb_add)(struct dsa_switch *ds, int port,
397 				const struct switchdev_obj_port_mdb *mdb,
398 				struct switchdev_trans *trans);
399 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
400 				const struct switchdev_obj_port_mdb *mdb);
401 	int	(*port_mdb_dump)(struct dsa_switch *ds, int port,
402 				 struct switchdev_obj_port_mdb *mdb,
403 				 int (*cb)(struct switchdev_obj *obj));
404 
405 	/*
406 	 * RXNFC
407 	 */
408 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
409 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
410 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
411 			     struct ethtool_rxnfc *nfc);
412 
413 	/*
414 	 * TC integration
415 	 */
416 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
417 				   struct dsa_mall_mirror_tc_entry *mirror,
418 				   bool ingress);
419 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
420 				   struct dsa_mall_mirror_tc_entry *mirror);
421 };
422 
423 struct dsa_switch_driver {
424 	struct list_head	list;
425 	const struct dsa_switch_ops *ops;
426 };
427 
428 void register_switch_driver(struct dsa_switch_driver *type);
429 void unregister_switch_driver(struct dsa_switch_driver *type);
430 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
431 
432 static inline bool dsa_uses_tagged_protocol(struct dsa_switch_tree *dst)
433 {
434 	return dst->rcv != NULL;
435 }
436 
437 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
438 void dsa_unregister_switch(struct dsa_switch *ds);
439 int dsa_register_switch(struct dsa_switch *ds, struct device *dev);
440 #ifdef CONFIG_PM_SLEEP
441 int dsa_switch_suspend(struct dsa_switch *ds);
442 int dsa_switch_resume(struct dsa_switch *ds);
443 #else
444 static inline int dsa_switch_suspend(struct dsa_switch *ds)
445 {
446 	return 0;
447 }
448 static inline int dsa_switch_resume(struct dsa_switch *ds)
449 {
450 	return 0;
451 }
452 #endif /* CONFIG_PM_SLEEP */
453 
454 #endif
455