1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 */
6
7 #ifndef __LINUX_NET_DSA_H
8 #define __LINUX_NET_DSA_H
9
10 #include <linux/if.h>
11 #include <linux/if_ether.h>
12 #include <linux/list.h>
13 #include <linux/notifier.h>
14 #include <linux/timer.h>
15 #include <linux/workqueue.h>
16 #include <linux/of.h>
17 #include <linux/ethtool.h>
18 #include <linux/net_tstamp.h>
19 #include <linux/phy.h>
20 #include <linux/platform_data/dsa.h>
21 #include <linux/phylink.h>
22 #include <net/devlink.h>
23 #include <net/switchdev.h>
24
25 struct tc_action;
26 struct phy_device;
27 struct fixed_phy_status;
28 struct phylink_link_state;
29
30 #define DSA_TAG_PROTO_NONE_VALUE 0
31 #define DSA_TAG_PROTO_BRCM_VALUE 1
32 #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE 2
33 #define DSA_TAG_PROTO_DSA_VALUE 3
34 #define DSA_TAG_PROTO_EDSA_VALUE 4
35 #define DSA_TAG_PROTO_GSWIP_VALUE 5
36 #define DSA_TAG_PROTO_KSZ9477_VALUE 6
37 #define DSA_TAG_PROTO_KSZ9893_VALUE 7
38 #define DSA_TAG_PROTO_LAN9303_VALUE 8
39 #define DSA_TAG_PROTO_MTK_VALUE 9
40 #define DSA_TAG_PROTO_QCA_VALUE 10
41 #define DSA_TAG_PROTO_TRAILER_VALUE 11
42 #define DSA_TAG_PROTO_8021Q_VALUE 12
43 #define DSA_TAG_PROTO_SJA1105_VALUE 13
44 #define DSA_TAG_PROTO_KSZ8795_VALUE 14
45 #define DSA_TAG_PROTO_OCELOT_VALUE 15
46 #define DSA_TAG_PROTO_AR9331_VALUE 16
47 #define DSA_TAG_PROTO_RTL4_A_VALUE 17
48 #define DSA_TAG_PROTO_HELLCREEK_VALUE 18
49 #define DSA_TAG_PROTO_XRS700X_VALUE 19
50 #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE 20
51 #define DSA_TAG_PROTO_SEVILLE_VALUE 21
52 #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE 22
53 #define DSA_TAG_PROTO_SJA1110_VALUE 23
54
55 enum dsa_tag_protocol {
56 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
57 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
58 DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
59 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
60 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
61 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
62 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
63 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE,
64 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE,
65 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
66 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
67 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
68 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
69 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
70 DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE,
71 DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE,
72 DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE,
73 DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE,
74 DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE,
75 DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE,
76 DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE,
77 DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
78 DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE,
79 DSA_TAG_PROTO_SJA1110 = DSA_TAG_PROTO_SJA1110_VALUE,
80 };
81
82 struct dsa_switch;
83
84 struct dsa_device_ops {
85 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
86 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
87 void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
88 int *offset);
89 unsigned int needed_headroom;
90 unsigned int needed_tailroom;
91 const char *name;
92 enum dsa_tag_protocol proto;
93 /* Some tagging protocols either mangle or shift the destination MAC
94 * address, in which case the DSA master would drop packets on ingress
95 * if what it understands out of the destination MAC address is not in
96 * its RX filter.
97 */
98 bool promisc_on_master;
99 };
100
101 /* This structure defines the control interfaces that are overlayed by the
102 * DSA layer on top of the DSA CPU/management net_device instance. This is
103 * used by the core net_device layer while calling various net_device_ops
104 * function pointers.
105 */
106 struct dsa_netdevice_ops {
107 int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
108 int cmd);
109 };
110
111 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
112 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \
113 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
114
115 struct dsa_switch_tree {
116 struct list_head list;
117
118 /* Notifier chain for switch-wide events */
119 struct raw_notifier_head nh;
120
121 /* Tree identifier */
122 unsigned int index;
123
124 /* Number of switches attached to this tree */
125 struct kref refcount;
126
127 /* Has this tree been applied to the hardware? */
128 bool setup;
129
130 /* Tagging protocol operations */
131 const struct dsa_device_ops *tag_ops;
132
133 /* Default tagging protocol preferred by the switches in this
134 * tree.
135 */
136 enum dsa_tag_protocol default_proto;
137
138 /*
139 * Configuration data for the platform device that owns
140 * this dsa switch tree instance.
141 */
142 struct dsa_platform_data *pd;
143
144 /* List of switch ports */
145 struct list_head ports;
146
147 /* List of DSA links composing the routing table */
148 struct list_head rtable;
149
150 /* Maps offloaded LAG netdevs to a zero-based linear ID for
151 * drivers that need it.
152 */
153 struct net_device **lags;
154 unsigned int lags_len;
155
156 /* Track the largest switch index within a tree */
157 unsigned int last_switch;
158 };
159
160 #define dsa_lags_foreach_id(_id, _dst) \
161 for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++) \
162 if ((_dst)->lags[(_id)])
163
164 #define dsa_lag_foreach_port(_dp, _dst, _lag) \
165 list_for_each_entry((_dp), &(_dst)->ports, list) \
166 if ((_dp)->lag_dev == (_lag))
167
168 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
169 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
170 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
171
dsa_lag_dev(struct dsa_switch_tree * dst,unsigned int id)172 static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
173 unsigned int id)
174 {
175 return dst->lags[id];
176 }
177
dsa_lag_id(struct dsa_switch_tree * dst,struct net_device * lag)178 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
179 struct net_device *lag)
180 {
181 unsigned int id;
182
183 dsa_lags_foreach_id(id, dst) {
184 if (dsa_lag_dev(dst, id) == lag)
185 return id;
186 }
187
188 return -ENODEV;
189 }
190
191 /* TC matchall action types */
192 enum dsa_port_mall_action_type {
193 DSA_PORT_MALL_MIRROR,
194 DSA_PORT_MALL_POLICER,
195 };
196
197 /* TC mirroring entry */
198 struct dsa_mall_mirror_tc_entry {
199 u8 to_local_port;
200 bool ingress;
201 };
202
203 /* TC port policer entry */
204 struct dsa_mall_policer_tc_entry {
205 u32 burst;
206 u64 rate_bytes_per_sec;
207 };
208
209 /* TC matchall entry */
210 struct dsa_mall_tc_entry {
211 struct list_head list;
212 unsigned long cookie;
213 enum dsa_port_mall_action_type type;
214 union {
215 struct dsa_mall_mirror_tc_entry mirror;
216 struct dsa_mall_policer_tc_entry policer;
217 };
218 };
219
220
221 struct dsa_port {
222 /* A CPU port is physically connected to a master device.
223 * A user port exposed to userspace has a slave device.
224 */
225 union {
226 struct net_device *master;
227 struct net_device *slave;
228 };
229
230 /* Copy of the tagging protocol operations, for quicker access
231 * in the data path. Valid only for the CPU ports.
232 */
233 const struct dsa_device_ops *tag_ops;
234
235 /* Copies for faster access in master receive hot path */
236 struct dsa_switch_tree *dst;
237 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
238
239 enum {
240 DSA_PORT_TYPE_UNUSED = 0,
241 DSA_PORT_TYPE_CPU,
242 DSA_PORT_TYPE_DSA,
243 DSA_PORT_TYPE_USER,
244 } type;
245
246 struct dsa_switch *ds;
247 unsigned int index;
248 const char *name;
249 struct dsa_port *cpu_dp;
250 u8 mac[ETH_ALEN];
251 struct device_node *dn;
252 unsigned int ageing_time;
253 bool vlan_filtering;
254 /* Managed by DSA on user ports and by drivers on CPU and DSA ports */
255 bool learning;
256 u8 stp_state;
257 struct net_device *bridge_dev;
258 int bridge_num;
259 struct devlink_port devlink_port;
260 bool devlink_port_setup;
261 struct phylink *pl;
262 struct phylink_config pl_config;
263 struct net_device *lag_dev;
264 bool lag_tx_enabled;
265 struct net_device *hsr_dev;
266
267 struct list_head list;
268
269 /*
270 * Give the switch driver somewhere to hang its per-port private data
271 * structures (accessible from the tagger).
272 */
273 void *priv;
274
275 /*
276 * Original copy of the master netdev ethtool_ops
277 */
278 const struct ethtool_ops *orig_ethtool_ops;
279
280 /*
281 * Original copy of the master netdev net_device_ops
282 */
283 const struct dsa_netdevice_ops *netdev_ops;
284
285 /* List of MAC addresses that must be forwarded on this port.
286 * These are only valid on CPU ports and DSA links.
287 */
288 struct list_head fdbs;
289 struct list_head mdbs;
290
291 bool setup;
292 };
293
294 /* TODO: ideally DSA ports would have a single dp->link_dp member,
295 * and no dst->rtable nor this struct dsa_link would be needed,
296 * but this would require some more complex tree walking,
297 * so keep it stupid at the moment and list them all.
298 */
299 struct dsa_link {
300 struct dsa_port *dp;
301 struct dsa_port *link_dp;
302 struct list_head list;
303 };
304
305 struct dsa_mac_addr {
306 unsigned char addr[ETH_ALEN];
307 u16 vid;
308 refcount_t refcount;
309 struct list_head list;
310 };
311
312 struct dsa_switch {
313 bool setup;
314
315 struct device *dev;
316
317 /*
318 * Parent switch tree, and switch index.
319 */
320 struct dsa_switch_tree *dst;
321 unsigned int index;
322
323 /* Listener for switch fabric events */
324 struct notifier_block nb;
325
326 /*
327 * Give the switch driver somewhere to hang its private data
328 * structure.
329 */
330 void *priv;
331
332 /*
333 * Configuration data for this switch.
334 */
335 struct dsa_chip_data *cd;
336
337 /*
338 * The switch operations.
339 */
340 const struct dsa_switch_ops *ops;
341
342 /*
343 * Slave mii_bus and devices for the individual ports.
344 */
345 u32 phys_mii_mask;
346 struct mii_bus *slave_mii_bus;
347
348 /* Ageing Time limits in msecs */
349 unsigned int ageing_time_min;
350 unsigned int ageing_time_max;
351
352 /* Storage for drivers using tag_8021q */
353 struct dsa_8021q_context *tag_8021q_ctx;
354
355 /* devlink used to represent this switch device */
356 struct devlink *devlink;
357
358 /* Number of switch port queues */
359 unsigned int num_tx_queues;
360
361 /* Disallow bridge core from requesting different VLAN awareness
362 * settings on ports if not hardware-supported
363 */
364 bool vlan_filtering_is_global;
365
366 /* Keep VLAN filtering enabled on ports not offloading any upper. */
367 bool needs_standalone_vlan_filtering;
368
369 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
370 * that have vlan_filtering=0. All drivers should ideally set this (and
371 * then the option would get removed), but it is unknown whether this
372 * would break things or not.
373 */
374 bool configure_vlan_while_not_filtering;
375
376 /* If the switch driver always programs the CPU port as egress tagged
377 * despite the VLAN configuration indicating otherwise, then setting
378 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
379 * default_pvid VLAN tagged frames to offer a consistent behavior
380 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
381 */
382 bool untag_bridge_pvid;
383
384 /* Let DSA manage the FDB entries towards the CPU, based on the
385 * software bridge database.
386 */
387 bool assisted_learning_on_cpu_port;
388
389 /* In case vlan_filtering_is_global is set, the VLAN awareness state
390 * should be retrieved from here and not from the per-port settings.
391 */
392 bool vlan_filtering;
393
394 /* MAC PCS does not provide link state change interrupt, and requires
395 * polling. Flag passed on to PHYLINK.
396 */
397 bool pcs_poll;
398
399 /* For switches that only have the MRU configurable. To ensure the
400 * configured MTU is not exceeded, normalization of MRU on all bridged
401 * interfaces is needed.
402 */
403 bool mtu_enforcement_ingress;
404
405 /* Drivers that benefit from having an ID associated with each
406 * offloaded LAG should set this to the maximum number of
407 * supported IDs. DSA will then maintain a mapping of _at
408 * least_ these many IDs, accessible to drivers via
409 * dsa_lag_id().
410 */
411 unsigned int num_lag_ids;
412
413 /* Drivers that support bridge forwarding offload should set this to
414 * the maximum number of bridges spanning the same switch tree (or all
415 * trees, in the case of cross-tree bridging support) that can be
416 * offloaded.
417 */
418 unsigned int num_fwd_offloading_bridges;
419
420 size_t num_ports;
421 };
422
dsa_to_port(struct dsa_switch * ds,int p)423 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
424 {
425 struct dsa_switch_tree *dst = ds->dst;
426 struct dsa_port *dp;
427
428 list_for_each_entry(dp, &dst->ports, list)
429 if (dp->ds == ds && dp->index == p)
430 return dp;
431
432 return NULL;
433 }
434
dsa_port_is_dsa(struct dsa_port * port)435 static inline bool dsa_port_is_dsa(struct dsa_port *port)
436 {
437 return port->type == DSA_PORT_TYPE_DSA;
438 }
439
dsa_port_is_cpu(struct dsa_port * port)440 static inline bool dsa_port_is_cpu(struct dsa_port *port)
441 {
442 return port->type == DSA_PORT_TYPE_CPU;
443 }
444
dsa_port_is_user(struct dsa_port * dp)445 static inline bool dsa_port_is_user(struct dsa_port *dp)
446 {
447 return dp->type == DSA_PORT_TYPE_USER;
448 }
449
dsa_port_is_unused(struct dsa_port * dp)450 static inline bool dsa_port_is_unused(struct dsa_port *dp)
451 {
452 return dp->type == DSA_PORT_TYPE_UNUSED;
453 }
454
dsa_is_unused_port(struct dsa_switch * ds,int p)455 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
456 {
457 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
458 }
459
dsa_is_cpu_port(struct dsa_switch * ds,int p)460 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
461 {
462 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
463 }
464
dsa_is_dsa_port(struct dsa_switch * ds,int p)465 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
466 {
467 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
468 }
469
dsa_is_user_port(struct dsa_switch * ds,int p)470 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
471 {
472 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
473 }
474
475 #define dsa_tree_for_each_user_port(_dp, _dst) \
476 list_for_each_entry((_dp), &(_dst)->ports, list) \
477 if (dsa_port_is_user((_dp)))
478
479 #define dsa_switch_for_each_port(_dp, _ds) \
480 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
481 if ((_dp)->ds == (_ds))
482
483 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
484 list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
485 if ((_dp)->ds == (_ds))
486
487 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
488 list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
489 if ((_dp)->ds == (_ds))
490
491 #define dsa_switch_for_each_available_port(_dp, _ds) \
492 dsa_switch_for_each_port((_dp), (_ds)) \
493 if (!dsa_port_is_unused((_dp)))
494
495 #define dsa_switch_for_each_user_port(_dp, _ds) \
496 dsa_switch_for_each_port((_dp), (_ds)) \
497 if (dsa_port_is_user((_dp)))
498
499 #define dsa_switch_for_each_cpu_port(_dp, _ds) \
500 dsa_switch_for_each_port((_dp), (_ds)) \
501 if (dsa_port_is_cpu((_dp)))
502
dsa_user_ports(struct dsa_switch * ds)503 static inline u32 dsa_user_ports(struct dsa_switch *ds)
504 {
505 u32 mask = 0;
506 int p;
507
508 for (p = 0; p < ds->num_ports; p++)
509 if (dsa_is_user_port(ds, p))
510 mask |= BIT(p);
511
512 return mask;
513 }
514
515 /* Return the local port used to reach an arbitrary switch device */
dsa_routing_port(struct dsa_switch * ds,int device)516 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
517 {
518 struct dsa_switch_tree *dst = ds->dst;
519 struct dsa_link *dl;
520
521 list_for_each_entry(dl, &dst->rtable, list)
522 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
523 return dl->dp->index;
524
525 return ds->num_ports;
526 }
527
528 /* Return the local port used to reach an arbitrary switch port */
dsa_towards_port(struct dsa_switch * ds,int device,int port)529 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
530 int port)
531 {
532 if (device == ds->index)
533 return port;
534 else
535 return dsa_routing_port(ds, device);
536 }
537
538 /* Return the local port used to reach the dedicated CPU port */
dsa_upstream_port(struct dsa_switch * ds,int port)539 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
540 {
541 const struct dsa_port *dp = dsa_to_port(ds, port);
542 const struct dsa_port *cpu_dp = dp->cpu_dp;
543
544 if (!cpu_dp)
545 return port;
546
547 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
548 }
549
550 /* Return true if this is the local port used to reach the CPU port */
dsa_is_upstream_port(struct dsa_switch * ds,int port)551 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
552 {
553 if (dsa_is_unused_port(ds, port))
554 return false;
555
556 return port == dsa_upstream_port(ds, port);
557 }
558
559 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
560 * that the routing port from @downstream_ds to @upstream_ds is also the port
561 * which @downstream_ds uses to reach its dedicated CPU.
562 */
dsa_switch_is_upstream_of(struct dsa_switch * upstream_ds,struct dsa_switch * downstream_ds)563 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
564 struct dsa_switch *downstream_ds)
565 {
566 int routing_port;
567
568 if (upstream_ds == downstream_ds)
569 return true;
570
571 routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
572
573 return dsa_is_upstream_port(downstream_ds, routing_port);
574 }
575
dsa_port_is_vlan_filtering(const struct dsa_port * dp)576 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
577 {
578 const struct dsa_switch *ds = dp->ds;
579
580 if (ds->vlan_filtering_is_global)
581 return ds->vlan_filtering;
582 else
583 return dp->vlan_filtering;
584 }
585
586 static inline
dsa_port_to_bridge_port(const struct dsa_port * dp)587 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
588 {
589 if (!dp->bridge_dev)
590 return NULL;
591
592 if (dp->lag_dev)
593 return dp->lag_dev;
594 else if (dp->hsr_dev)
595 return dp->hsr_dev;
596
597 return dp->slave;
598 }
599
600 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
601 bool is_static, void *data);
602 struct dsa_switch_ops {
603 /*
604 * Tagging protocol helpers called for the CPU ports and DSA links.
605 * @get_tag_protocol retrieves the initial tagging protocol and is
606 * mandatory. Switches which can operate using multiple tagging
607 * protocols should implement @change_tag_protocol and report in
608 * @get_tag_protocol the tagger in current use.
609 */
610 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
611 int port,
612 enum dsa_tag_protocol mprot);
613 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
614 enum dsa_tag_protocol proto);
615
616 /* Optional switch-wide initialization and destruction methods */
617 int (*setup)(struct dsa_switch *ds);
618 void (*teardown)(struct dsa_switch *ds);
619
620 /* Per-port initialization and destruction methods. Mandatory if the
621 * driver registers devlink port regions, optional otherwise.
622 */
623 int (*port_setup)(struct dsa_switch *ds, int port);
624 void (*port_teardown)(struct dsa_switch *ds, int port);
625
626 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
627
628 /*
629 * Access to the switch's PHY registers.
630 */
631 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
632 int (*phy_write)(struct dsa_switch *ds, int port,
633 int regnum, u16 val);
634
635 /*
636 * Link state adjustment (called from libphy)
637 */
638 void (*adjust_link)(struct dsa_switch *ds, int port,
639 struct phy_device *phydev);
640 void (*fixed_link_update)(struct dsa_switch *ds, int port,
641 struct fixed_phy_status *st);
642
643 /*
644 * PHYLINK integration
645 */
646 void (*phylink_validate)(struct dsa_switch *ds, int port,
647 unsigned long *supported,
648 struct phylink_link_state *state);
649 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
650 struct phylink_link_state *state);
651 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
652 unsigned int mode,
653 const struct phylink_link_state *state);
654 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
655 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
656 unsigned int mode,
657 phy_interface_t interface);
658 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
659 unsigned int mode,
660 phy_interface_t interface,
661 struct phy_device *phydev,
662 int speed, int duplex,
663 bool tx_pause, bool rx_pause);
664 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
665 struct phylink_link_state *state);
666 /*
667 * Port statistics counters.
668 */
669 void (*get_strings)(struct dsa_switch *ds, int port,
670 u32 stringset, uint8_t *data);
671 void (*get_ethtool_stats)(struct dsa_switch *ds,
672 int port, uint64_t *data);
673 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
674 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
675 int port, uint64_t *data);
676 void (*get_stats64)(struct dsa_switch *ds, int port,
677 struct rtnl_link_stats64 *s);
678 void (*self_test)(struct dsa_switch *ds, int port,
679 struct ethtool_test *etest, u64 *data);
680
681 /*
682 * ethtool Wake-on-LAN
683 */
684 void (*get_wol)(struct dsa_switch *ds, int port,
685 struct ethtool_wolinfo *w);
686 int (*set_wol)(struct dsa_switch *ds, int port,
687 struct ethtool_wolinfo *w);
688
689 /*
690 * ethtool timestamp info
691 */
692 int (*get_ts_info)(struct dsa_switch *ds, int port,
693 struct ethtool_ts_info *ts);
694
695 /*
696 * Suspend and resume
697 */
698 int (*suspend)(struct dsa_switch *ds);
699 int (*resume)(struct dsa_switch *ds);
700
701 /*
702 * Port enable/disable
703 */
704 int (*port_enable)(struct dsa_switch *ds, int port,
705 struct phy_device *phy);
706 void (*port_disable)(struct dsa_switch *ds, int port);
707
708 /*
709 * Port's MAC EEE settings
710 */
711 int (*set_mac_eee)(struct dsa_switch *ds, int port,
712 struct ethtool_eee *e);
713 int (*get_mac_eee)(struct dsa_switch *ds, int port,
714 struct ethtool_eee *e);
715
716 /* EEPROM access */
717 int (*get_eeprom_len)(struct dsa_switch *ds);
718 int (*get_eeprom)(struct dsa_switch *ds,
719 struct ethtool_eeprom *eeprom, u8 *data);
720 int (*set_eeprom)(struct dsa_switch *ds,
721 struct ethtool_eeprom *eeprom, u8 *data);
722
723 /*
724 * Register access.
725 */
726 int (*get_regs_len)(struct dsa_switch *ds, int port);
727 void (*get_regs)(struct dsa_switch *ds, int port,
728 struct ethtool_regs *regs, void *p);
729
730 /*
731 * Upper device tracking.
732 */
733 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
734 struct netdev_notifier_changeupper_info *info);
735
736 /*
737 * Bridge integration
738 */
739 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
740 int (*port_bridge_join)(struct dsa_switch *ds, int port,
741 struct net_device *bridge);
742 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
743 struct net_device *bridge);
744 /* Called right after .port_bridge_join() */
745 int (*port_bridge_tx_fwd_offload)(struct dsa_switch *ds, int port,
746 struct net_device *bridge,
747 int bridge_num);
748 /* Called right before .port_bridge_leave() */
749 void (*port_bridge_tx_fwd_unoffload)(struct dsa_switch *ds, int port,
750 struct net_device *bridge,
751 int bridge_num);
752 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
753 u8 state);
754 void (*port_fast_age)(struct dsa_switch *ds, int port);
755 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
756 struct switchdev_brport_flags flags,
757 struct netlink_ext_ack *extack);
758 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
759 struct switchdev_brport_flags flags,
760 struct netlink_ext_ack *extack);
761
762 /*
763 * VLAN support
764 */
765 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
766 bool vlan_filtering,
767 struct netlink_ext_ack *extack);
768 int (*port_vlan_add)(struct dsa_switch *ds, int port,
769 const struct switchdev_obj_port_vlan *vlan,
770 struct netlink_ext_ack *extack);
771 int (*port_vlan_del)(struct dsa_switch *ds, int port,
772 const struct switchdev_obj_port_vlan *vlan);
773 /*
774 * Forwarding database
775 */
776 int (*port_fdb_add)(struct dsa_switch *ds, int port,
777 const unsigned char *addr, u16 vid);
778 int (*port_fdb_del)(struct dsa_switch *ds, int port,
779 const unsigned char *addr, u16 vid);
780 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
781 dsa_fdb_dump_cb_t *cb, void *data);
782
783 /*
784 * Multicast database
785 */
786 int (*port_mdb_add)(struct dsa_switch *ds, int port,
787 const struct switchdev_obj_port_mdb *mdb);
788 int (*port_mdb_del)(struct dsa_switch *ds, int port,
789 const struct switchdev_obj_port_mdb *mdb);
790 /*
791 * RXNFC
792 */
793 int (*get_rxnfc)(struct dsa_switch *ds, int port,
794 struct ethtool_rxnfc *nfc, u32 *rule_locs);
795 int (*set_rxnfc)(struct dsa_switch *ds, int port,
796 struct ethtool_rxnfc *nfc);
797
798 /*
799 * TC integration
800 */
801 int (*cls_flower_add)(struct dsa_switch *ds, int port,
802 struct flow_cls_offload *cls, bool ingress);
803 int (*cls_flower_del)(struct dsa_switch *ds, int port,
804 struct flow_cls_offload *cls, bool ingress);
805 int (*cls_flower_stats)(struct dsa_switch *ds, int port,
806 struct flow_cls_offload *cls, bool ingress);
807 int (*port_mirror_add)(struct dsa_switch *ds, int port,
808 struct dsa_mall_mirror_tc_entry *mirror,
809 bool ingress);
810 void (*port_mirror_del)(struct dsa_switch *ds, int port,
811 struct dsa_mall_mirror_tc_entry *mirror);
812 int (*port_policer_add)(struct dsa_switch *ds, int port,
813 struct dsa_mall_policer_tc_entry *policer);
814 void (*port_policer_del)(struct dsa_switch *ds, int port);
815 int (*port_setup_tc)(struct dsa_switch *ds, int port,
816 enum tc_setup_type type, void *type_data);
817
818 /*
819 * Cross-chip operations
820 */
821 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
822 int sw_index, int port,
823 struct net_device *br);
824 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
825 int sw_index, int port,
826 struct net_device *br);
827 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
828 int port);
829 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
830 int port, struct net_device *lag,
831 struct netdev_lag_upper_info *info);
832 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
833 int port, struct net_device *lag);
834
835 /*
836 * PTP functionality
837 */
838 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
839 struct ifreq *ifr);
840 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
841 struct ifreq *ifr);
842 void (*port_txtstamp)(struct dsa_switch *ds, int port,
843 struct sk_buff *skb);
844 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
845 struct sk_buff *skb, unsigned int type);
846
847 /* Devlink parameters, etc */
848 int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
849 struct devlink_param_gset_ctx *ctx);
850 int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
851 struct devlink_param_gset_ctx *ctx);
852 int (*devlink_info_get)(struct dsa_switch *ds,
853 struct devlink_info_req *req,
854 struct netlink_ext_ack *extack);
855 int (*devlink_sb_pool_get)(struct dsa_switch *ds,
856 unsigned int sb_index, u16 pool_index,
857 struct devlink_sb_pool_info *pool_info);
858 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
859 u16 pool_index, u32 size,
860 enum devlink_sb_threshold_type threshold_type,
861 struct netlink_ext_ack *extack);
862 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
863 unsigned int sb_index, u16 pool_index,
864 u32 *p_threshold);
865 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
866 unsigned int sb_index, u16 pool_index,
867 u32 threshold,
868 struct netlink_ext_ack *extack);
869 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
870 unsigned int sb_index, u16 tc_index,
871 enum devlink_sb_pool_type pool_type,
872 u16 *p_pool_index, u32 *p_threshold);
873 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
874 unsigned int sb_index, u16 tc_index,
875 enum devlink_sb_pool_type pool_type,
876 u16 pool_index, u32 threshold,
877 struct netlink_ext_ack *extack);
878 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
879 unsigned int sb_index);
880 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
881 unsigned int sb_index);
882 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
883 unsigned int sb_index, u16 pool_index,
884 u32 *p_cur, u32 *p_max);
885 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
886 unsigned int sb_index, u16 tc_index,
887 enum devlink_sb_pool_type pool_type,
888 u32 *p_cur, u32 *p_max);
889
890 /*
891 * MTU change functionality. Switches can also adjust their MRU through
892 * this method. By MTU, one understands the SDU (L2 payload) length.
893 * If the switch needs to account for the DSA tag on the CPU port, this
894 * method needs to do so privately.
895 */
896 int (*port_change_mtu)(struct dsa_switch *ds, int port,
897 int new_mtu);
898 int (*port_max_mtu)(struct dsa_switch *ds, int port);
899
900 /*
901 * LAG integration
902 */
903 int (*port_lag_change)(struct dsa_switch *ds, int port);
904 int (*port_lag_join)(struct dsa_switch *ds, int port,
905 struct net_device *lag,
906 struct netdev_lag_upper_info *info);
907 int (*port_lag_leave)(struct dsa_switch *ds, int port,
908 struct net_device *lag);
909
910 /*
911 * HSR integration
912 */
913 int (*port_hsr_join)(struct dsa_switch *ds, int port,
914 struct net_device *hsr);
915 int (*port_hsr_leave)(struct dsa_switch *ds, int port,
916 struct net_device *hsr);
917
918 /*
919 * MRP integration
920 */
921 int (*port_mrp_add)(struct dsa_switch *ds, int port,
922 const struct switchdev_obj_mrp *mrp);
923 int (*port_mrp_del)(struct dsa_switch *ds, int port,
924 const struct switchdev_obj_mrp *mrp);
925 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
926 const struct switchdev_obj_ring_role_mrp *mrp);
927 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
928 const struct switchdev_obj_ring_role_mrp *mrp);
929
930 /*
931 * tag_8021q operations
932 */
933 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
934 u16 flags);
935 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
936 };
937
938 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
939 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
940 dsa_devlink_param_get, dsa_devlink_param_set, NULL)
941
942 int dsa_devlink_param_get(struct devlink *dl, u32 id,
943 struct devlink_param_gset_ctx *ctx);
944 int dsa_devlink_param_set(struct devlink *dl, u32 id,
945 struct devlink_param_gset_ctx *ctx);
946 int dsa_devlink_params_register(struct dsa_switch *ds,
947 const struct devlink_param *params,
948 size_t params_count);
949 void dsa_devlink_params_unregister(struct dsa_switch *ds,
950 const struct devlink_param *params,
951 size_t params_count);
952 int dsa_devlink_resource_register(struct dsa_switch *ds,
953 const char *resource_name,
954 u64 resource_size,
955 u64 resource_id,
956 u64 parent_resource_id,
957 const struct devlink_resource_size_params *size_params);
958
959 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
960
961 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
962 u64 resource_id,
963 devlink_resource_occ_get_t *occ_get,
964 void *occ_get_priv);
965 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
966 u64 resource_id);
967 struct devlink_region *
968 dsa_devlink_region_create(struct dsa_switch *ds,
969 const struct devlink_region_ops *ops,
970 u32 region_max_snapshots, u64 region_size);
971 struct devlink_region *
972 dsa_devlink_port_region_create(struct dsa_switch *ds,
973 int port,
974 const struct devlink_port_region_ops *ops,
975 u32 region_max_snapshots, u64 region_size);
976 void dsa_devlink_region_destroy(struct devlink_region *region);
977
978 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
979
980 struct dsa_devlink_priv {
981 struct dsa_switch *ds;
982 };
983
dsa_devlink_to_ds(struct devlink * dl)984 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
985 {
986 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
987
988 return dl_priv->ds;
989 }
990
991 static inline
dsa_devlink_port_to_ds(struct devlink_port * port)992 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
993 {
994 struct devlink *dl = port->devlink;
995 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
996
997 return dl_priv->ds;
998 }
999
dsa_devlink_port_to_port(struct devlink_port * port)1000 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
1001 {
1002 return port->index;
1003 }
1004
1005 struct dsa_switch_driver {
1006 struct list_head list;
1007 const struct dsa_switch_ops *ops;
1008 };
1009
1010 struct net_device *dsa_dev_to_net_device(struct device *dev);
1011
1012 /* Keep inline for faster access in hot path */
netdev_uses_dsa(const struct net_device * dev)1013 static inline bool netdev_uses_dsa(const struct net_device *dev)
1014 {
1015 #if IS_ENABLED(CONFIG_NET_DSA)
1016 return dev->dsa_ptr && dev->dsa_ptr->rcv;
1017 #endif
1018 return false;
1019 }
1020
1021 /* All DSA tags that push the EtherType to the right (basically all except tail
1022 * tags, which don't break dissection) can be treated the same from the
1023 * perspective of the flow dissector.
1024 *
1025 * We need to return:
1026 * - offset: the (B - A) difference between:
1027 * A. the position of the real EtherType and
1028 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
1029 * after the normal EtherType was supposed to be)
1030 * The offset in bytes is exactly equal to the tagger overhead (and half of
1031 * that, in __be16 shorts).
1032 *
1033 * - proto: the value of the real EtherType.
1034 */
dsa_tag_generic_flow_dissect(const struct sk_buff * skb,__be16 * proto,int * offset)1035 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1036 __be16 *proto, int *offset)
1037 {
1038 #if IS_ENABLED(CONFIG_NET_DSA)
1039 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1040 int tag_len = ops->needed_headroom;
1041
1042 *offset = tag_len;
1043 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1044 #endif
1045 }
1046
1047 #if IS_ENABLED(CONFIG_NET_DSA)
__dsa_netdevice_ops_check(struct net_device * dev)1048 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1049 {
1050 int err = -EOPNOTSUPP;
1051
1052 if (!dev->dsa_ptr)
1053 return err;
1054
1055 if (!dev->dsa_ptr->netdev_ops)
1056 return err;
1057
1058 return 0;
1059 }
1060
dsa_ndo_eth_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1061 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1062 int cmd)
1063 {
1064 const struct dsa_netdevice_ops *ops;
1065 int err;
1066
1067 err = __dsa_netdevice_ops_check(dev);
1068 if (err)
1069 return err;
1070
1071 ops = dev->dsa_ptr->netdev_ops;
1072
1073 return ops->ndo_eth_ioctl(dev, ifr, cmd);
1074 }
1075 #else
dsa_ndo_eth_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1076 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1077 int cmd)
1078 {
1079 return -EOPNOTSUPP;
1080 }
1081 #endif
1082
1083 void dsa_unregister_switch(struct dsa_switch *ds);
1084 int dsa_register_switch(struct dsa_switch *ds);
1085 void dsa_switch_shutdown(struct dsa_switch *ds);
1086 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1087 #ifdef CONFIG_PM_SLEEP
1088 int dsa_switch_suspend(struct dsa_switch *ds);
1089 int dsa_switch_resume(struct dsa_switch *ds);
1090 #else
dsa_switch_suspend(struct dsa_switch * ds)1091 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1092 {
1093 return 0;
1094 }
dsa_switch_resume(struct dsa_switch * ds)1095 static inline int dsa_switch_resume(struct dsa_switch *ds)
1096 {
1097 return 0;
1098 }
1099 #endif /* CONFIG_PM_SLEEP */
1100
1101 #if IS_ENABLED(CONFIG_NET_DSA)
1102 bool dsa_slave_dev_check(const struct net_device *dev);
1103 #else
dsa_slave_dev_check(const struct net_device * dev)1104 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1105 {
1106 return false;
1107 }
1108 #endif
1109
1110 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1111 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1112 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1113 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
1114 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1115
1116 struct dsa_tag_driver {
1117 const struct dsa_device_ops *ops;
1118 struct list_head list;
1119 struct module *owner;
1120 };
1121
1122 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1123 unsigned int count,
1124 struct module *owner);
1125 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1126 unsigned int count);
1127
1128 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1129 static int __init dsa_tag_driver_module_init(void) \
1130 { \
1131 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1132 THIS_MODULE); \
1133 return 0; \
1134 } \
1135 module_init(dsa_tag_driver_module_init); \
1136 \
1137 static void __exit dsa_tag_driver_module_exit(void) \
1138 { \
1139 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1140 } \
1141 module_exit(dsa_tag_driver_module_exit)
1142
1143 /**
1144 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1145 * drivers
1146 * @__ops_array: Array of tag driver strucutres
1147 *
1148 * Helper macro for DSA tag drivers which do not do anything special
1149 * in module init/exit. Each module may only use this macro once, and
1150 * calling it replaces module_init() and module_exit().
1151 */
1152 #define module_dsa_tag_drivers(__ops_array) \
1153 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1154
1155 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1156
1157 /* Create a static structure we can build a linked list of dsa_tag
1158 * drivers
1159 */
1160 #define DSA_TAG_DRIVER(__ops) \
1161 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1162 .ops = &__ops, \
1163 }
1164
1165 /**
1166 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1167 * driver
1168 * @__ops: Single tag driver structures
1169 *
1170 * Helper macro for DSA tag drivers which do not do anything special
1171 * in module init/exit. Each module may only use this macro once, and
1172 * calling it replaces module_init() and module_exit().
1173 */
1174 #define module_dsa_tag_driver(__ops) \
1175 DSA_TAG_DRIVER(__ops); \
1176 \
1177 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1178 &DSA_TAG_DRIVER_NAME(__ops) \
1179 }; \
1180 module_dsa_tag_drivers(dsa_tag_driver_array)
1181 #endif
1182
1183