1
2
3
4
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
25struct tc_action;
26struct phy_device;
27struct fixed_phy_status;
28struct 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
55enum 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
82struct dsa_switch;
83
84struct 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
94
95
96
97
98 bool promisc_on_master;
99};
100
101
102
103
104
105
106struct 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
115struct dsa_switch_tree {
116 struct list_head list;
117
118
119 struct raw_notifier_head nh;
120
121
122 unsigned int index;
123
124
125 struct kref refcount;
126
127
128 bool setup;
129
130
131 const struct dsa_device_ops *tag_ops;
132
133
134
135
136 enum dsa_tag_protocol default_proto;
137
138
139
140
141
142 struct dsa_platform_data *pd;
143
144
145 struct list_head ports;
146
147
148 struct list_head rtable;
149
150
151
152
153 struct net_device **lags;
154 unsigned int lags_len;
155
156
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
172static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
173 unsigned int id)
174{
175 return dst->lags[id];
176}
177
178static 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
192enum dsa_port_mall_action_type {
193 DSA_PORT_MALL_MIRROR,
194 DSA_PORT_MALL_POLICER,
195};
196
197
198struct dsa_mall_mirror_tc_entry {
199 u8 to_local_port;
200 bool ingress;
201};
202
203
204struct dsa_mall_policer_tc_entry {
205 u32 burst;
206 u64 rate_bytes_per_sec;
207};
208
209
210struct 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
221struct dsa_port {
222
223
224
225 union {
226 struct net_device *master;
227 struct net_device *slave;
228 };
229
230
231
232
233 const struct dsa_device_ops *tag_ops;
234
235
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
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
271
272
273 void *priv;
274
275
276
277
278 const struct ethtool_ops *orig_ethtool_ops;
279
280
281
282
283 const struct dsa_netdevice_ops *netdev_ops;
284
285
286
287
288 struct list_head fdbs;
289 struct list_head mdbs;
290
291 bool setup;
292};
293
294
295
296
297
298
299struct dsa_link {
300 struct dsa_port *dp;
301 struct dsa_port *link_dp;
302 struct list_head list;
303};
304
305struct dsa_mac_addr {
306 unsigned char addr[ETH_ALEN];
307 u16 vid;
308 refcount_t refcount;
309 struct list_head list;
310};
311
312struct dsa_switch {
313 bool setup;
314
315 struct device *dev;
316
317
318
319
320 struct dsa_switch_tree *dst;
321 unsigned int index;
322
323
324 struct notifier_block nb;
325
326
327
328
329
330 void *priv;
331
332
333
334
335 struct dsa_chip_data *cd;
336
337
338
339
340 const struct dsa_switch_ops *ops;
341
342
343
344
345 u32 phys_mii_mask;
346 struct mii_bus *slave_mii_bus;
347
348
349 unsigned int ageing_time_min;
350 unsigned int ageing_time_max;
351
352
353 struct dsa_8021q_context *tag_8021q_ctx;
354
355
356 struct devlink *devlink;
357
358
359 unsigned int num_tx_queues;
360
361
362
363
364 bool vlan_filtering_is_global;
365
366
367 bool needs_standalone_vlan_filtering;
368
369
370
371
372
373
374 bool configure_vlan_while_not_filtering;
375
376
377
378
379
380
381
382 bool untag_bridge_pvid;
383
384
385
386
387 bool assisted_learning_on_cpu_port;
388
389
390
391
392 bool vlan_filtering;
393
394
395
396
397 bool pcs_poll;
398
399
400
401
402
403 bool mtu_enforcement_ingress;
404
405
406
407
408
409
410
411 unsigned int num_lag_ids;
412
413
414
415
416
417
418 unsigned int num_fwd_offloading_bridges;
419
420 size_t num_ports;
421};
422
423static 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
435static inline bool dsa_port_is_dsa(struct dsa_port *port)
436{
437 return port->type == DSA_PORT_TYPE_DSA;
438}
439
440static inline bool dsa_port_is_cpu(struct dsa_port *port)
441{
442 return port->type == DSA_PORT_TYPE_CPU;
443}
444
445static inline bool dsa_port_is_user(struct dsa_port *dp)
446{
447 return dp->type == DSA_PORT_TYPE_USER;
448}
449
450static inline bool dsa_port_is_unused(struct dsa_port *dp)
451{
452 return dp->type == DSA_PORT_TYPE_UNUSED;
453}
454
455static 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
460static 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
465static 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
470static 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
475static inline u32 dsa_user_ports(struct dsa_switch *ds)
476{
477 u32 mask = 0;
478 int p;
479
480 for (p = 0; p < ds->num_ports; p++)
481 if (dsa_is_user_port(ds, p))
482 mask |= BIT(p);
483
484 return mask;
485}
486
487
488static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
489{
490 struct dsa_switch_tree *dst = ds->dst;
491 struct dsa_link *dl;
492
493 list_for_each_entry(dl, &dst->rtable, list)
494 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
495 return dl->dp->index;
496
497 return ds->num_ports;
498}
499
500
501static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
502 int port)
503{
504 if (device == ds->index)
505 return port;
506 else
507 return dsa_routing_port(ds, device);
508}
509
510
511static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
512{
513 const struct dsa_port *dp = dsa_to_port(ds, port);
514 const struct dsa_port *cpu_dp = dp->cpu_dp;
515
516 if (!cpu_dp)
517 return port;
518
519 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
520}
521
522
523static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
524{
525 if (dsa_is_unused_port(ds, port))
526 return false;
527
528 return port == dsa_upstream_port(ds, port);
529}
530
531
532
533
534
535static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
536 struct dsa_switch *downstream_ds)
537{
538 int routing_port;
539
540 if (upstream_ds == downstream_ds)
541 return true;
542
543 routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
544
545 return dsa_is_upstream_port(downstream_ds, routing_port);
546}
547
548static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
549{
550 const struct dsa_switch *ds = dp->ds;
551
552 if (ds->vlan_filtering_is_global)
553 return ds->vlan_filtering;
554 else
555 return dp->vlan_filtering;
556}
557
558static inline
559struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
560{
561 if (!dp->bridge_dev)
562 return NULL;
563
564 if (dp->lag_dev)
565 return dp->lag_dev;
566 else if (dp->hsr_dev)
567 return dp->hsr_dev;
568
569 return dp->slave;
570}
571
572typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
573 bool is_static, void *data);
574struct dsa_switch_ops {
575
576
577
578
579
580
581
582 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
583 int port,
584 enum dsa_tag_protocol mprot);
585 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
586 enum dsa_tag_protocol proto);
587
588
589 int (*setup)(struct dsa_switch *ds);
590 void (*teardown)(struct dsa_switch *ds);
591
592
593
594
595 int (*port_setup)(struct dsa_switch *ds, int port);
596 void (*port_teardown)(struct dsa_switch *ds, int port);
597
598 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
599
600
601
602
603 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
604 int (*phy_write)(struct dsa_switch *ds, int port,
605 int regnum, u16 val);
606
607
608
609
610 void (*adjust_link)(struct dsa_switch *ds, int port,
611 struct phy_device *phydev);
612 void (*fixed_link_update)(struct dsa_switch *ds, int port,
613 struct fixed_phy_status *st);
614
615
616
617
618 void (*phylink_validate)(struct dsa_switch *ds, int port,
619 unsigned long *supported,
620 struct phylink_link_state *state);
621 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
622 struct phylink_link_state *state);
623 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
624 unsigned int mode,
625 const struct phylink_link_state *state);
626 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
627 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
628 unsigned int mode,
629 phy_interface_t interface);
630 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
631 unsigned int mode,
632 phy_interface_t interface,
633 struct phy_device *phydev,
634 int speed, int duplex,
635 bool tx_pause, bool rx_pause);
636 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
637 struct phylink_link_state *state);
638
639
640
641 void (*get_strings)(struct dsa_switch *ds, int port,
642 u32 stringset, uint8_t *data);
643 void (*get_ethtool_stats)(struct dsa_switch *ds,
644 int port, uint64_t *data);
645 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
646 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
647 int port, uint64_t *data);
648 void (*get_stats64)(struct dsa_switch *ds, int port,
649 struct rtnl_link_stats64 *s);
650 void (*self_test)(struct dsa_switch *ds, int port,
651 struct ethtool_test *etest, u64 *data);
652
653
654
655
656 void (*get_wol)(struct dsa_switch *ds, int port,
657 struct ethtool_wolinfo *w);
658 int (*set_wol)(struct dsa_switch *ds, int port,
659 struct ethtool_wolinfo *w);
660
661
662
663
664 int (*get_ts_info)(struct dsa_switch *ds, int port,
665 struct ethtool_ts_info *ts);
666
667
668
669
670 int (*suspend)(struct dsa_switch *ds);
671 int (*resume)(struct dsa_switch *ds);
672
673
674
675
676 int (*port_enable)(struct dsa_switch *ds, int port,
677 struct phy_device *phy);
678 void (*port_disable)(struct dsa_switch *ds, int port);
679
680
681
682
683 int (*set_mac_eee)(struct dsa_switch *ds, int port,
684 struct ethtool_eee *e);
685 int (*get_mac_eee)(struct dsa_switch *ds, int port,
686 struct ethtool_eee *e);
687
688
689 int (*get_eeprom_len)(struct dsa_switch *ds);
690 int (*get_eeprom)(struct dsa_switch *ds,
691 struct ethtool_eeprom *eeprom, u8 *data);
692 int (*set_eeprom)(struct dsa_switch *ds,
693 struct ethtool_eeprom *eeprom, u8 *data);
694
695
696
697
698 int (*get_regs_len)(struct dsa_switch *ds, int port);
699 void (*get_regs)(struct dsa_switch *ds, int port,
700 struct ethtool_regs *regs, void *p);
701
702
703
704
705 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
706 struct netdev_notifier_changeupper_info *info);
707
708
709
710
711 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
712 int (*port_bridge_join)(struct dsa_switch *ds, int port,
713 struct net_device *bridge);
714 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
715 struct net_device *bridge);
716
717 int (*port_bridge_tx_fwd_offload)(struct dsa_switch *ds, int port,
718 struct net_device *bridge,
719 int bridge_num);
720
721 void (*port_bridge_tx_fwd_unoffload)(struct dsa_switch *ds, int port,
722 struct net_device *bridge,
723 int bridge_num);
724 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
725 u8 state);
726 void (*port_fast_age)(struct dsa_switch *ds, int port);
727 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
728 struct switchdev_brport_flags flags,
729 struct netlink_ext_ack *extack);
730 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
731 struct switchdev_brport_flags flags,
732 struct netlink_ext_ack *extack);
733
734
735
736
737 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
738 bool vlan_filtering,
739 struct netlink_ext_ack *extack);
740 int (*port_vlan_add)(struct dsa_switch *ds, int port,
741 const struct switchdev_obj_port_vlan *vlan,
742 struct netlink_ext_ack *extack);
743 int (*port_vlan_del)(struct dsa_switch *ds, int port,
744 const struct switchdev_obj_port_vlan *vlan);
745
746
747
748 int (*port_fdb_add)(struct dsa_switch *ds, int port,
749 const unsigned char *addr, u16 vid);
750 int (*port_fdb_del)(struct dsa_switch *ds, int port,
751 const unsigned char *addr, u16 vid);
752 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
753 dsa_fdb_dump_cb_t *cb, void *data);
754
755
756
757
758 int (*port_mdb_add)(struct dsa_switch *ds, int port,
759 const struct switchdev_obj_port_mdb *mdb);
760 int (*port_mdb_del)(struct dsa_switch *ds, int port,
761 const struct switchdev_obj_port_mdb *mdb);
762
763
764
765 int (*get_rxnfc)(struct dsa_switch *ds, int port,
766 struct ethtool_rxnfc *nfc, u32 *rule_locs);
767 int (*set_rxnfc)(struct dsa_switch *ds, int port,
768 struct ethtool_rxnfc *nfc);
769
770
771
772
773 int (*cls_flower_add)(struct dsa_switch *ds, int port,
774 struct flow_cls_offload *cls, bool ingress);
775 int (*cls_flower_del)(struct dsa_switch *ds, int port,
776 struct flow_cls_offload *cls, bool ingress);
777 int (*cls_flower_stats)(struct dsa_switch *ds, int port,
778 struct flow_cls_offload *cls, bool ingress);
779 int (*port_mirror_add)(struct dsa_switch *ds, int port,
780 struct dsa_mall_mirror_tc_entry *mirror,
781 bool ingress);
782 void (*port_mirror_del)(struct dsa_switch *ds, int port,
783 struct dsa_mall_mirror_tc_entry *mirror);
784 int (*port_policer_add)(struct dsa_switch *ds, int port,
785 struct dsa_mall_policer_tc_entry *policer);
786 void (*port_policer_del)(struct dsa_switch *ds, int port);
787 int (*port_setup_tc)(struct dsa_switch *ds, int port,
788 enum tc_setup_type type, void *type_data);
789
790
791
792
793 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
794 int sw_index, int port,
795 struct net_device *br);
796 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
797 int sw_index, int port,
798 struct net_device *br);
799 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
800 int port);
801 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
802 int port, struct net_device *lag,
803 struct netdev_lag_upper_info *info);
804 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
805 int port, struct net_device *lag);
806
807
808
809
810 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
811 struct ifreq *ifr);
812 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
813 struct ifreq *ifr);
814 void (*port_txtstamp)(struct dsa_switch *ds, int port,
815 struct sk_buff *skb);
816 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
817 struct sk_buff *skb, unsigned int type);
818
819
820 int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
821 struct devlink_param_gset_ctx *ctx);
822 int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
823 struct devlink_param_gset_ctx *ctx);
824 int (*devlink_info_get)(struct dsa_switch *ds,
825 struct devlink_info_req *req,
826 struct netlink_ext_ack *extack);
827 int (*devlink_sb_pool_get)(struct dsa_switch *ds,
828 unsigned int sb_index, u16 pool_index,
829 struct devlink_sb_pool_info *pool_info);
830 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
831 u16 pool_index, u32 size,
832 enum devlink_sb_threshold_type threshold_type,
833 struct netlink_ext_ack *extack);
834 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
835 unsigned int sb_index, u16 pool_index,
836 u32 *p_threshold);
837 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
838 unsigned int sb_index, u16 pool_index,
839 u32 threshold,
840 struct netlink_ext_ack *extack);
841 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
842 unsigned int sb_index, u16 tc_index,
843 enum devlink_sb_pool_type pool_type,
844 u16 *p_pool_index, u32 *p_threshold);
845 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
846 unsigned int sb_index, u16 tc_index,
847 enum devlink_sb_pool_type pool_type,
848 u16 pool_index, u32 threshold,
849 struct netlink_ext_ack *extack);
850 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
851 unsigned int sb_index);
852 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
853 unsigned int sb_index);
854 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
855 unsigned int sb_index, u16 pool_index,
856 u32 *p_cur, u32 *p_max);
857 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
858 unsigned int sb_index, u16 tc_index,
859 enum devlink_sb_pool_type pool_type,
860 u32 *p_cur, u32 *p_max);
861
862
863
864
865
866
867
868 int (*port_change_mtu)(struct dsa_switch *ds, int port,
869 int new_mtu);
870 int (*port_max_mtu)(struct dsa_switch *ds, int port);
871
872
873
874
875 int (*port_lag_change)(struct dsa_switch *ds, int port);
876 int (*port_lag_join)(struct dsa_switch *ds, int port,
877 struct net_device *lag,
878 struct netdev_lag_upper_info *info);
879 int (*port_lag_leave)(struct dsa_switch *ds, int port,
880 struct net_device *lag);
881
882
883
884
885 int (*port_hsr_join)(struct dsa_switch *ds, int port,
886 struct net_device *hsr);
887 int (*port_hsr_leave)(struct dsa_switch *ds, int port,
888 struct net_device *hsr);
889
890
891
892
893 int (*port_mrp_add)(struct dsa_switch *ds, int port,
894 const struct switchdev_obj_mrp *mrp);
895 int (*port_mrp_del)(struct dsa_switch *ds, int port,
896 const struct switchdev_obj_mrp *mrp);
897 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
898 const struct switchdev_obj_ring_role_mrp *mrp);
899 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
900 const struct switchdev_obj_ring_role_mrp *mrp);
901
902
903
904
905 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
906 u16 flags);
907 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
908};
909
910#define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
911 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
912 dsa_devlink_param_get, dsa_devlink_param_set, NULL)
913
914int dsa_devlink_param_get(struct devlink *dl, u32 id,
915 struct devlink_param_gset_ctx *ctx);
916int dsa_devlink_param_set(struct devlink *dl, u32 id,
917 struct devlink_param_gset_ctx *ctx);
918int dsa_devlink_params_register(struct dsa_switch *ds,
919 const struct devlink_param *params,
920 size_t params_count);
921void dsa_devlink_params_unregister(struct dsa_switch *ds,
922 const struct devlink_param *params,
923 size_t params_count);
924int dsa_devlink_resource_register(struct dsa_switch *ds,
925 const char *resource_name,
926 u64 resource_size,
927 u64 resource_id,
928 u64 parent_resource_id,
929 const struct devlink_resource_size_params *size_params);
930
931void dsa_devlink_resources_unregister(struct dsa_switch *ds);
932
933void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
934 u64 resource_id,
935 devlink_resource_occ_get_t *occ_get,
936 void *occ_get_priv);
937void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
938 u64 resource_id);
939struct devlink_region *
940dsa_devlink_region_create(struct dsa_switch *ds,
941 const struct devlink_region_ops *ops,
942 u32 region_max_snapshots, u64 region_size);
943struct devlink_region *
944dsa_devlink_port_region_create(struct dsa_switch *ds,
945 int port,
946 const struct devlink_port_region_ops *ops,
947 u32 region_max_snapshots, u64 region_size);
948void dsa_devlink_region_destroy(struct devlink_region *region);
949
950struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
951
952struct dsa_devlink_priv {
953 struct dsa_switch *ds;
954};
955
956static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
957{
958 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
959
960 return dl_priv->ds;
961}
962
963static inline
964struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
965{
966 struct devlink *dl = port->devlink;
967 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
968
969 return dl_priv->ds;
970}
971
972static inline int dsa_devlink_port_to_port(struct devlink_port *port)
973{
974 return port->index;
975}
976
977struct dsa_switch_driver {
978 struct list_head list;
979 const struct dsa_switch_ops *ops;
980};
981
982struct net_device *dsa_dev_to_net_device(struct device *dev);
983
984
985static inline bool netdev_uses_dsa(const struct net_device *dev)
986{
987#if IS_ENABLED(CONFIG_NET_DSA)
988 return dev->dsa_ptr && dev->dsa_ptr->rcv;
989#endif
990 return false;
991}
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1008 __be16 *proto, int *offset)
1009{
1010#if IS_ENABLED(CONFIG_NET_DSA)
1011 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1012 int tag_len = ops->needed_headroom;
1013
1014 *offset = tag_len;
1015 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1016#endif
1017}
1018
1019#if IS_ENABLED(CONFIG_NET_DSA)
1020static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1021{
1022 int err = -EOPNOTSUPP;
1023
1024 if (!dev->dsa_ptr)
1025 return err;
1026
1027 if (!dev->dsa_ptr->netdev_ops)
1028 return err;
1029
1030 return 0;
1031}
1032
1033static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1034 int cmd)
1035{
1036 const struct dsa_netdevice_ops *ops;
1037 int err;
1038
1039 err = __dsa_netdevice_ops_check(dev);
1040 if (err)
1041 return err;
1042
1043 ops = dev->dsa_ptr->netdev_ops;
1044
1045 return ops->ndo_eth_ioctl(dev, ifr, cmd);
1046}
1047#else
1048static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1049 int cmd)
1050{
1051 return -EOPNOTSUPP;
1052}
1053#endif
1054
1055void dsa_unregister_switch(struct dsa_switch *ds);
1056int dsa_register_switch(struct dsa_switch *ds);
1057void dsa_switch_shutdown(struct dsa_switch *ds);
1058struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1059#ifdef CONFIG_PM_SLEEP
1060int dsa_switch_suspend(struct dsa_switch *ds);
1061int dsa_switch_resume(struct dsa_switch *ds);
1062#else
1063static inline int dsa_switch_suspend(struct dsa_switch *ds)
1064{
1065 return 0;
1066}
1067static inline int dsa_switch_resume(struct dsa_switch *ds)
1068{
1069 return 0;
1070}
1071#endif
1072
1073#if IS_ENABLED(CONFIG_NET_DSA)
1074bool dsa_slave_dev_check(const struct net_device *dev);
1075#else
1076static inline bool dsa_slave_dev_check(const struct net_device *dev)
1077{
1078 return false;
1079}
1080#endif
1081
1082netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1083int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1084int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1085int dsa_port_get_phy_sset_count(struct dsa_port *dp);
1086void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1087
1088struct dsa_tag_driver {
1089 const struct dsa_device_ops *ops;
1090 struct list_head list;
1091 struct module *owner;
1092};
1093
1094void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1095 unsigned int count,
1096 struct module *owner);
1097void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1098 unsigned int count);
1099
1100#define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1101static int __init dsa_tag_driver_module_init(void) \
1102{ \
1103 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1104 THIS_MODULE); \
1105 return 0; \
1106} \
1107module_init(dsa_tag_driver_module_init); \
1108 \
1109static void __exit dsa_tag_driver_module_exit(void) \
1110{ \
1111 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1112} \
1113module_exit(dsa_tag_driver_module_exit)
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124#define module_dsa_tag_drivers(__ops_array) \
1125dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1126
1127#define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1128
1129
1130
1131
1132#define DSA_TAG_DRIVER(__ops) \
1133static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1134 .ops = &__ops, \
1135}
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146#define module_dsa_tag_driver(__ops) \
1147DSA_TAG_DRIVER(__ops); \
1148 \
1149static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1150 &DSA_TAG_DRIVER_NAME(__ops) \
1151}; \
1152module_dsa_tag_drivers(dsa_tag_driver_array)
1153#endif
1154
1155