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