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 #define dsa_tree_for_each_user_port(_dp, _dst) \ 478 list_for_each_entry((_dp), &(_dst)->ports, list) \ 479 if (dsa_port_is_user((_dp))) 480 481 #define dsa_switch_for_each_port(_dp, _ds) \ 482 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 483 if ((_dp)->ds == (_ds)) 484 485 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \ 486 list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \ 487 if ((_dp)->ds == (_ds)) 488 489 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \ 490 list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \ 491 if ((_dp)->ds == (_ds)) 492 493 #define dsa_switch_for_each_available_port(_dp, _ds) \ 494 dsa_switch_for_each_port((_dp), (_ds)) \ 495 if (!dsa_port_is_unused((_dp))) 496 497 #define dsa_switch_for_each_user_port(_dp, _ds) \ 498 dsa_switch_for_each_port((_dp), (_ds)) \ 499 if (dsa_port_is_user((_dp))) 500 501 #define dsa_switch_for_each_cpu_port(_dp, _ds) \ 502 dsa_switch_for_each_port((_dp), (_ds)) \ 503 if (dsa_port_is_cpu((_dp))) 504 505 static inline u32 dsa_user_ports(struct dsa_switch *ds) 506 { 507 u32 mask = 0; 508 int p; 509 510 for (p = 0; p < ds->num_ports; p++) 511 if (dsa_is_user_port(ds, p)) 512 mask |= BIT(p); 513 514 return mask; 515 } 516 517 /* Return the local port used to reach an arbitrary switch device */ 518 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 519 { 520 struct dsa_switch_tree *dst = ds->dst; 521 struct dsa_link *dl; 522 523 list_for_each_entry(dl, &dst->rtable, list) 524 if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 525 return dl->dp->index; 526 527 return ds->num_ports; 528 } 529 530 /* Return the local port used to reach an arbitrary switch port */ 531 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 532 int port) 533 { 534 if (device == ds->index) 535 return port; 536 else 537 return dsa_routing_port(ds, device); 538 } 539 540 /* Return the local port used to reach the dedicated CPU port */ 541 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 542 { 543 const struct dsa_port *dp = dsa_to_port(ds, port); 544 const struct dsa_port *cpu_dp = dp->cpu_dp; 545 546 if (!cpu_dp) 547 return port; 548 549 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 550 } 551 552 /* Return true if this is the local port used to reach the CPU port */ 553 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port) 554 { 555 if (dsa_is_unused_port(ds, port)) 556 return false; 557 558 return port == dsa_upstream_port(ds, port); 559 } 560 561 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning 562 * that the routing port from @downstream_ds to @upstream_ds is also the port 563 * which @downstream_ds uses to reach its dedicated CPU. 564 */ 565 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds, 566 struct dsa_switch *downstream_ds) 567 { 568 int routing_port; 569 570 if (upstream_ds == downstream_ds) 571 return true; 572 573 routing_port = dsa_routing_port(downstream_ds, upstream_ds->index); 574 575 return dsa_is_upstream_port(downstream_ds, routing_port); 576 } 577 578 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 579 { 580 const struct dsa_switch *ds = dp->ds; 581 582 if (ds->vlan_filtering_is_global) 583 return ds->vlan_filtering; 584 else 585 return dp->vlan_filtering; 586 } 587 588 static inline 589 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) 590 { 591 if (!dp->bridge_dev) 592 return NULL; 593 594 if (dp->lag_dev) 595 return dp->lag_dev; 596 else if (dp->hsr_dev) 597 return dp->hsr_dev; 598 599 return dp->slave; 600 } 601 602 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 603 bool is_static, void *data); 604 struct dsa_switch_ops { 605 /* 606 * Tagging protocol helpers called for the CPU ports and DSA links. 607 * @get_tag_protocol retrieves the initial tagging protocol and is 608 * mandatory. Switches which can operate using multiple tagging 609 * protocols should implement @change_tag_protocol and report in 610 * @get_tag_protocol the tagger in current use. 611 */ 612 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 613 int port, 614 enum dsa_tag_protocol mprot); 615 int (*change_tag_protocol)(struct dsa_switch *ds, int port, 616 enum dsa_tag_protocol proto); 617 618 /* Optional switch-wide initialization and destruction methods */ 619 int (*setup)(struct dsa_switch *ds); 620 void (*teardown)(struct dsa_switch *ds); 621 622 /* Per-port initialization and destruction methods. Mandatory if the 623 * driver registers devlink port regions, optional otherwise. 624 */ 625 int (*port_setup)(struct dsa_switch *ds, int port); 626 void (*port_teardown)(struct dsa_switch *ds, int port); 627 628 u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 629 630 /* 631 * Access to the switch's PHY registers. 632 */ 633 int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 634 int (*phy_write)(struct dsa_switch *ds, int port, 635 int regnum, u16 val); 636 637 /* 638 * Link state adjustment (called from libphy) 639 */ 640 void (*adjust_link)(struct dsa_switch *ds, int port, 641 struct phy_device *phydev); 642 void (*fixed_link_update)(struct dsa_switch *ds, int port, 643 struct fixed_phy_status *st); 644 645 /* 646 * PHYLINK integration 647 */ 648 void (*phylink_validate)(struct dsa_switch *ds, int port, 649 unsigned long *supported, 650 struct phylink_link_state *state); 651 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 652 struct phylink_link_state *state); 653 void (*phylink_mac_config)(struct dsa_switch *ds, int port, 654 unsigned int mode, 655 const struct phylink_link_state *state); 656 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 657 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 658 unsigned int mode, 659 phy_interface_t interface); 660 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 661 unsigned int mode, 662 phy_interface_t interface, 663 struct phy_device *phydev, 664 int speed, int duplex, 665 bool tx_pause, bool rx_pause); 666 void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 667 struct phylink_link_state *state); 668 /* 669 * Port statistics counters. 670 */ 671 void (*get_strings)(struct dsa_switch *ds, int port, 672 u32 stringset, uint8_t *data); 673 void (*get_ethtool_stats)(struct dsa_switch *ds, 674 int port, uint64_t *data); 675 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 676 void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 677 int port, uint64_t *data); 678 void (*get_eth_phy_stats)(struct dsa_switch *ds, int port, 679 struct ethtool_eth_phy_stats *phy_stats); 680 void (*get_eth_mac_stats)(struct dsa_switch *ds, int port, 681 struct ethtool_eth_mac_stats *mac_stats); 682 void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port, 683 struct ethtool_eth_ctrl_stats *ctrl_stats); 684 void (*get_stats64)(struct dsa_switch *ds, int port, 685 struct rtnl_link_stats64 *s); 686 void (*self_test)(struct dsa_switch *ds, int port, 687 struct ethtool_test *etest, u64 *data); 688 689 /* 690 * ethtool Wake-on-LAN 691 */ 692 void (*get_wol)(struct dsa_switch *ds, int port, 693 struct ethtool_wolinfo *w); 694 int (*set_wol)(struct dsa_switch *ds, int port, 695 struct ethtool_wolinfo *w); 696 697 /* 698 * ethtool timestamp info 699 */ 700 int (*get_ts_info)(struct dsa_switch *ds, int port, 701 struct ethtool_ts_info *ts); 702 703 /* 704 * Suspend and resume 705 */ 706 int (*suspend)(struct dsa_switch *ds); 707 int (*resume)(struct dsa_switch *ds); 708 709 /* 710 * Port enable/disable 711 */ 712 int (*port_enable)(struct dsa_switch *ds, int port, 713 struct phy_device *phy); 714 void (*port_disable)(struct dsa_switch *ds, int port); 715 716 /* 717 * Port's MAC EEE settings 718 */ 719 int (*set_mac_eee)(struct dsa_switch *ds, int port, 720 struct ethtool_eee *e); 721 int (*get_mac_eee)(struct dsa_switch *ds, int port, 722 struct ethtool_eee *e); 723 724 /* EEPROM access */ 725 int (*get_eeprom_len)(struct dsa_switch *ds); 726 int (*get_eeprom)(struct dsa_switch *ds, 727 struct ethtool_eeprom *eeprom, u8 *data); 728 int (*set_eeprom)(struct dsa_switch *ds, 729 struct ethtool_eeprom *eeprom, u8 *data); 730 731 /* 732 * Register access. 733 */ 734 int (*get_regs_len)(struct dsa_switch *ds, int port); 735 void (*get_regs)(struct dsa_switch *ds, int port, 736 struct ethtool_regs *regs, void *p); 737 738 /* 739 * Upper device tracking. 740 */ 741 int (*port_prechangeupper)(struct dsa_switch *ds, int port, 742 struct netdev_notifier_changeupper_info *info); 743 744 /* 745 * Bridge integration 746 */ 747 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 748 int (*port_bridge_join)(struct dsa_switch *ds, int port, 749 struct net_device *bridge); 750 void (*port_bridge_leave)(struct dsa_switch *ds, int port, 751 struct net_device *bridge); 752 /* Called right after .port_bridge_join() */ 753 int (*port_bridge_tx_fwd_offload)(struct dsa_switch *ds, int port, 754 struct net_device *bridge, 755 int bridge_num); 756 /* Called right before .port_bridge_leave() */ 757 void (*port_bridge_tx_fwd_unoffload)(struct dsa_switch *ds, int port, 758 struct net_device *bridge, 759 int bridge_num); 760 void (*port_stp_state_set)(struct dsa_switch *ds, int port, 761 u8 state); 762 void (*port_fast_age)(struct dsa_switch *ds, int port); 763 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port, 764 struct switchdev_brport_flags flags, 765 struct netlink_ext_ack *extack); 766 int (*port_bridge_flags)(struct dsa_switch *ds, int port, 767 struct switchdev_brport_flags flags, 768 struct netlink_ext_ack *extack); 769 770 /* 771 * VLAN support 772 */ 773 int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 774 bool vlan_filtering, 775 struct netlink_ext_ack *extack); 776 int (*port_vlan_add)(struct dsa_switch *ds, int port, 777 const struct switchdev_obj_port_vlan *vlan, 778 struct netlink_ext_ack *extack); 779 int (*port_vlan_del)(struct dsa_switch *ds, int port, 780 const struct switchdev_obj_port_vlan *vlan); 781 /* 782 * Forwarding database 783 */ 784 int (*port_fdb_add)(struct dsa_switch *ds, int port, 785 const unsigned char *addr, u16 vid); 786 int (*port_fdb_del)(struct dsa_switch *ds, int port, 787 const unsigned char *addr, u16 vid); 788 int (*port_fdb_dump)(struct dsa_switch *ds, int port, 789 dsa_fdb_dump_cb_t *cb, void *data); 790 791 /* 792 * Multicast database 793 */ 794 int (*port_mdb_add)(struct dsa_switch *ds, int port, 795 const struct switchdev_obj_port_mdb *mdb); 796 int (*port_mdb_del)(struct dsa_switch *ds, int port, 797 const struct switchdev_obj_port_mdb *mdb); 798 /* 799 * RXNFC 800 */ 801 int (*get_rxnfc)(struct dsa_switch *ds, int port, 802 struct ethtool_rxnfc *nfc, u32 *rule_locs); 803 int (*set_rxnfc)(struct dsa_switch *ds, int port, 804 struct ethtool_rxnfc *nfc); 805 806 /* 807 * TC integration 808 */ 809 int (*cls_flower_add)(struct dsa_switch *ds, int port, 810 struct flow_cls_offload *cls, bool ingress); 811 int (*cls_flower_del)(struct dsa_switch *ds, int port, 812 struct flow_cls_offload *cls, bool ingress); 813 int (*cls_flower_stats)(struct dsa_switch *ds, int port, 814 struct flow_cls_offload *cls, bool ingress); 815 int (*port_mirror_add)(struct dsa_switch *ds, int port, 816 struct dsa_mall_mirror_tc_entry *mirror, 817 bool ingress); 818 void (*port_mirror_del)(struct dsa_switch *ds, int port, 819 struct dsa_mall_mirror_tc_entry *mirror); 820 int (*port_policer_add)(struct dsa_switch *ds, int port, 821 struct dsa_mall_policer_tc_entry *policer); 822 void (*port_policer_del)(struct dsa_switch *ds, int port); 823 int (*port_setup_tc)(struct dsa_switch *ds, int port, 824 enum tc_setup_type type, void *type_data); 825 826 /* 827 * Cross-chip operations 828 */ 829 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 830 int sw_index, int port, 831 struct net_device *br); 832 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 833 int sw_index, int port, 834 struct net_device *br); 835 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 836 int port); 837 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 838 int port, struct net_device *lag, 839 struct netdev_lag_upper_info *info); 840 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 841 int port, struct net_device *lag); 842 843 /* 844 * PTP functionality 845 */ 846 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 847 struct ifreq *ifr); 848 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 849 struct ifreq *ifr); 850 void (*port_txtstamp)(struct dsa_switch *ds, int port, 851 struct sk_buff *skb); 852 bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 853 struct sk_buff *skb, unsigned int type); 854 855 /* Devlink parameters, etc */ 856 int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 857 struct devlink_param_gset_ctx *ctx); 858 int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 859 struct devlink_param_gset_ctx *ctx); 860 int (*devlink_info_get)(struct dsa_switch *ds, 861 struct devlink_info_req *req, 862 struct netlink_ext_ack *extack); 863 int (*devlink_sb_pool_get)(struct dsa_switch *ds, 864 unsigned int sb_index, u16 pool_index, 865 struct devlink_sb_pool_info *pool_info); 866 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 867 u16 pool_index, u32 size, 868 enum devlink_sb_threshold_type threshold_type, 869 struct netlink_ext_ack *extack); 870 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 871 unsigned int sb_index, u16 pool_index, 872 u32 *p_threshold); 873 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 874 unsigned int sb_index, u16 pool_index, 875 u32 threshold, 876 struct netlink_ext_ack *extack); 877 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 878 unsigned int sb_index, u16 tc_index, 879 enum devlink_sb_pool_type pool_type, 880 u16 *p_pool_index, u32 *p_threshold); 881 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 882 unsigned int sb_index, u16 tc_index, 883 enum devlink_sb_pool_type pool_type, 884 u16 pool_index, u32 threshold, 885 struct netlink_ext_ack *extack); 886 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 887 unsigned int sb_index); 888 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 889 unsigned int sb_index); 890 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 891 unsigned int sb_index, u16 pool_index, 892 u32 *p_cur, u32 *p_max); 893 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 894 unsigned int sb_index, u16 tc_index, 895 enum devlink_sb_pool_type pool_type, 896 u32 *p_cur, u32 *p_max); 897 898 /* 899 * MTU change functionality. Switches can also adjust their MRU through 900 * this method. By MTU, one understands the SDU (L2 payload) length. 901 * If the switch needs to account for the DSA tag on the CPU port, this 902 * method needs to do so privately. 903 */ 904 int (*port_change_mtu)(struct dsa_switch *ds, int port, 905 int new_mtu); 906 int (*port_max_mtu)(struct dsa_switch *ds, int port); 907 908 /* 909 * LAG integration 910 */ 911 int (*port_lag_change)(struct dsa_switch *ds, int port); 912 int (*port_lag_join)(struct dsa_switch *ds, int port, 913 struct net_device *lag, 914 struct netdev_lag_upper_info *info); 915 int (*port_lag_leave)(struct dsa_switch *ds, int port, 916 struct net_device *lag); 917 918 /* 919 * HSR integration 920 */ 921 int (*port_hsr_join)(struct dsa_switch *ds, int port, 922 struct net_device *hsr); 923 int (*port_hsr_leave)(struct dsa_switch *ds, int port, 924 struct net_device *hsr); 925 926 /* 927 * MRP integration 928 */ 929 int (*port_mrp_add)(struct dsa_switch *ds, int port, 930 const struct switchdev_obj_mrp *mrp); 931 int (*port_mrp_del)(struct dsa_switch *ds, int port, 932 const struct switchdev_obj_mrp *mrp); 933 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port, 934 const struct switchdev_obj_ring_role_mrp *mrp); 935 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port, 936 const struct switchdev_obj_ring_role_mrp *mrp); 937 938 /* 939 * tag_8021q operations 940 */ 941 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid, 942 u16 flags); 943 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid); 944 }; 945 946 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 947 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 948 dsa_devlink_param_get, dsa_devlink_param_set, NULL) 949 950 int dsa_devlink_param_get(struct devlink *dl, u32 id, 951 struct devlink_param_gset_ctx *ctx); 952 int dsa_devlink_param_set(struct devlink *dl, u32 id, 953 struct devlink_param_gset_ctx *ctx); 954 int dsa_devlink_params_register(struct dsa_switch *ds, 955 const struct devlink_param *params, 956 size_t params_count); 957 void dsa_devlink_params_unregister(struct dsa_switch *ds, 958 const struct devlink_param *params, 959 size_t params_count); 960 int dsa_devlink_resource_register(struct dsa_switch *ds, 961 const char *resource_name, 962 u64 resource_size, 963 u64 resource_id, 964 u64 parent_resource_id, 965 const struct devlink_resource_size_params *size_params); 966 967 void dsa_devlink_resources_unregister(struct dsa_switch *ds); 968 969 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 970 u64 resource_id, 971 devlink_resource_occ_get_t *occ_get, 972 void *occ_get_priv); 973 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 974 u64 resource_id); 975 struct devlink_region * 976 dsa_devlink_region_create(struct dsa_switch *ds, 977 const struct devlink_region_ops *ops, 978 u32 region_max_snapshots, u64 region_size); 979 struct devlink_region * 980 dsa_devlink_port_region_create(struct dsa_switch *ds, 981 int port, 982 const struct devlink_port_region_ops *ops, 983 u32 region_max_snapshots, u64 region_size); 984 void dsa_devlink_region_destroy(struct devlink_region *region); 985 986 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 987 988 struct dsa_devlink_priv { 989 struct dsa_switch *ds; 990 }; 991 992 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 993 { 994 struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 995 996 return dl_priv->ds; 997 } 998 999 static inline 1000 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 1001 { 1002 struct devlink *dl = port->devlink; 1003 struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1004 1005 return dl_priv->ds; 1006 } 1007 1008 static inline int dsa_devlink_port_to_port(struct devlink_port *port) 1009 { 1010 return port->index; 1011 } 1012 1013 struct dsa_switch_driver { 1014 struct list_head list; 1015 const struct dsa_switch_ops *ops; 1016 }; 1017 1018 struct net_device *dsa_dev_to_net_device(struct device *dev); 1019 1020 /* Keep inline for faster access in hot path */ 1021 static inline bool netdev_uses_dsa(const struct net_device *dev) 1022 { 1023 #if IS_ENABLED(CONFIG_NET_DSA) 1024 return dev->dsa_ptr && dev->dsa_ptr->rcv; 1025 #endif 1026 return false; 1027 } 1028 1029 /* All DSA tags that push the EtherType to the right (basically all except tail 1030 * tags, which don't break dissection) can be treated the same from the 1031 * perspective of the flow dissector. 1032 * 1033 * We need to return: 1034 * - offset: the (B - A) difference between: 1035 * A. the position of the real EtherType and 1036 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 1037 * after the normal EtherType was supposed to be) 1038 * The offset in bytes is exactly equal to the tagger overhead (and half of 1039 * that, in __be16 shorts). 1040 * 1041 * - proto: the value of the real EtherType. 1042 */ 1043 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 1044 __be16 *proto, int *offset) 1045 { 1046 #if IS_ENABLED(CONFIG_NET_DSA) 1047 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 1048 int tag_len = ops->needed_headroom; 1049 1050 *offset = tag_len; 1051 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 1052 #endif 1053 } 1054 1055 #if IS_ENABLED(CONFIG_NET_DSA) 1056 static inline int __dsa_netdevice_ops_check(struct net_device *dev) 1057 { 1058 int err = -EOPNOTSUPP; 1059 1060 if (!dev->dsa_ptr) 1061 return err; 1062 1063 if (!dev->dsa_ptr->netdev_ops) 1064 return err; 1065 1066 return 0; 1067 } 1068 1069 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 1070 int cmd) 1071 { 1072 const struct dsa_netdevice_ops *ops; 1073 int err; 1074 1075 err = __dsa_netdevice_ops_check(dev); 1076 if (err) 1077 return err; 1078 1079 ops = dev->dsa_ptr->netdev_ops; 1080 1081 return ops->ndo_eth_ioctl(dev, ifr, cmd); 1082 } 1083 #else 1084 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 1085 int cmd) 1086 { 1087 return -EOPNOTSUPP; 1088 } 1089 #endif 1090 1091 void dsa_unregister_switch(struct dsa_switch *ds); 1092 int dsa_register_switch(struct dsa_switch *ds); 1093 void dsa_switch_shutdown(struct dsa_switch *ds); 1094 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1095 #ifdef CONFIG_PM_SLEEP 1096 int dsa_switch_suspend(struct dsa_switch *ds); 1097 int dsa_switch_resume(struct dsa_switch *ds); 1098 #else 1099 static inline int dsa_switch_suspend(struct dsa_switch *ds) 1100 { 1101 return 0; 1102 } 1103 static inline int dsa_switch_resume(struct dsa_switch *ds) 1104 { 1105 return 0; 1106 } 1107 #endif /* CONFIG_PM_SLEEP */ 1108 1109 #if IS_ENABLED(CONFIG_NET_DSA) 1110 bool dsa_slave_dev_check(const struct net_device *dev); 1111 #else 1112 static inline bool dsa_slave_dev_check(const struct net_device *dev) 1113 { 1114 return false; 1115 } 1116 #endif 1117 1118 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 1119 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 1120 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 1121 int dsa_port_get_phy_sset_count(struct dsa_port *dp); 1122 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 1123 1124 struct dsa_tag_driver { 1125 const struct dsa_device_ops *ops; 1126 struct list_head list; 1127 struct module *owner; 1128 }; 1129 1130 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 1131 unsigned int count, 1132 struct module *owner); 1133 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 1134 unsigned int count); 1135 1136 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 1137 static int __init dsa_tag_driver_module_init(void) \ 1138 { \ 1139 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 1140 THIS_MODULE); \ 1141 return 0; \ 1142 } \ 1143 module_init(dsa_tag_driver_module_init); \ 1144 \ 1145 static void __exit dsa_tag_driver_module_exit(void) \ 1146 { \ 1147 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 1148 } \ 1149 module_exit(dsa_tag_driver_module_exit) 1150 1151 /** 1152 * module_dsa_tag_drivers() - Helper macro for registering DSA tag 1153 * drivers 1154 * @__ops_array: Array of tag driver strucutres 1155 * 1156 * Helper macro for DSA tag drivers which do not do anything special 1157 * in module init/exit. Each module may only use this macro once, and 1158 * calling it replaces module_init() and module_exit(). 1159 */ 1160 #define module_dsa_tag_drivers(__ops_array) \ 1161 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 1162 1163 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1164 1165 /* Create a static structure we can build a linked list of dsa_tag 1166 * drivers 1167 */ 1168 #define DSA_TAG_DRIVER(__ops) \ 1169 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1170 .ops = &__ops, \ 1171 } 1172 1173 /** 1174 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1175 * driver 1176 * @__ops: Single tag driver structures 1177 * 1178 * Helper macro for DSA tag drivers which do not do anything special 1179 * in module init/exit. Each module may only use this macro once, and 1180 * calling it replaces module_init() and module_exit(). 1181 */ 1182 #define module_dsa_tag_driver(__ops) \ 1183 DSA_TAG_DRIVER(__ops); \ 1184 \ 1185 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1186 &DSA_TAG_DRIVER_NAME(__ops) \ 1187 }; \ 1188 module_dsa_tag_drivers(dsa_tag_driver_array) 1189 #endif 1190 1191