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 mutex addr_lists_lock; 291 struct list_head fdbs; 292 struct list_head mdbs; 293 294 bool setup; 295 }; 296 297 /* TODO: ideally DSA ports would have a single dp->link_dp member, 298 * and no dst->rtable nor this struct dsa_link would be needed, 299 * but this would require some more complex tree walking, 300 * so keep it stupid at the moment and list them all. 301 */ 302 struct dsa_link { 303 struct dsa_port *dp; 304 struct dsa_port *link_dp; 305 struct list_head list; 306 }; 307 308 struct dsa_mac_addr { 309 unsigned char addr[ETH_ALEN]; 310 u16 vid; 311 refcount_t refcount; 312 struct list_head list; 313 }; 314 315 struct dsa_switch { 316 bool setup; 317 318 struct device *dev; 319 320 /* 321 * Parent switch tree, and switch index. 322 */ 323 struct dsa_switch_tree *dst; 324 unsigned int index; 325 326 /* Listener for switch fabric events */ 327 struct notifier_block nb; 328 329 /* 330 * Give the switch driver somewhere to hang its private data 331 * structure. 332 */ 333 void *priv; 334 335 /* 336 * Configuration data for this switch. 337 */ 338 struct dsa_chip_data *cd; 339 340 /* 341 * The switch operations. 342 */ 343 const struct dsa_switch_ops *ops; 344 345 /* 346 * Slave mii_bus and devices for the individual ports. 347 */ 348 u32 phys_mii_mask; 349 struct mii_bus *slave_mii_bus; 350 351 /* Ageing Time limits in msecs */ 352 unsigned int ageing_time_min; 353 unsigned int ageing_time_max; 354 355 /* Storage for drivers using tag_8021q */ 356 struct dsa_8021q_context *tag_8021q_ctx; 357 358 /* devlink used to represent this switch device */ 359 struct devlink *devlink; 360 361 /* Number of switch port queues */ 362 unsigned int num_tx_queues; 363 364 /* Disallow bridge core from requesting different VLAN awareness 365 * settings on ports if not hardware-supported 366 */ 367 bool vlan_filtering_is_global; 368 369 /* Keep VLAN filtering enabled on ports not offloading any upper. */ 370 bool needs_standalone_vlan_filtering; 371 372 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges 373 * that have vlan_filtering=0. All drivers should ideally set this (and 374 * then the option would get removed), but it is unknown whether this 375 * would break things or not. 376 */ 377 bool configure_vlan_while_not_filtering; 378 379 /* If the switch driver always programs the CPU port as egress tagged 380 * despite the VLAN configuration indicating otherwise, then setting 381 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's 382 * default_pvid VLAN tagged frames to offer a consistent behavior 383 * between a vlan_filtering=0 and vlan_filtering=1 bridge device. 384 */ 385 bool untag_bridge_pvid; 386 387 /* Let DSA manage the FDB entries towards the CPU, based on the 388 * software bridge database. 389 */ 390 bool assisted_learning_on_cpu_port; 391 392 /* In case vlan_filtering_is_global is set, the VLAN awareness state 393 * should be retrieved from here and not from the per-port settings. 394 */ 395 bool vlan_filtering; 396 397 /* MAC PCS does not provide link state change interrupt, and requires 398 * polling. Flag passed on to PHYLINK. 399 */ 400 bool pcs_poll; 401 402 /* For switches that only have the MRU configurable. To ensure the 403 * configured MTU is not exceeded, normalization of MRU on all bridged 404 * interfaces is needed. 405 */ 406 bool mtu_enforcement_ingress; 407 408 /* Drivers that benefit from having an ID associated with each 409 * offloaded LAG should set this to the maximum number of 410 * supported IDs. DSA will then maintain a mapping of _at 411 * least_ these many IDs, accessible to drivers via 412 * dsa_lag_id(). 413 */ 414 unsigned int num_lag_ids; 415 416 /* Drivers that support bridge forwarding offload should set this to 417 * the maximum number of bridges spanning the same switch tree (or all 418 * trees, in the case of cross-tree bridging support) that can be 419 * offloaded. 420 */ 421 unsigned int num_fwd_offloading_bridges; 422 423 size_t num_ports; 424 }; 425 426 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 427 { 428 struct dsa_switch_tree *dst = ds->dst; 429 struct dsa_port *dp; 430 431 list_for_each_entry(dp, &dst->ports, list) 432 if (dp->ds == ds && dp->index == p) 433 return dp; 434 435 return NULL; 436 } 437 438 static inline bool dsa_port_is_dsa(struct dsa_port *port) 439 { 440 return port->type == DSA_PORT_TYPE_DSA; 441 } 442 443 static inline bool dsa_port_is_cpu(struct dsa_port *port) 444 { 445 return port->type == DSA_PORT_TYPE_CPU; 446 } 447 448 static inline bool dsa_port_is_user(struct dsa_port *dp) 449 { 450 return dp->type == DSA_PORT_TYPE_USER; 451 } 452 453 static inline bool dsa_port_is_unused(struct dsa_port *dp) 454 { 455 return dp->type == DSA_PORT_TYPE_UNUSED; 456 } 457 458 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 459 { 460 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 461 } 462 463 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 464 { 465 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 466 } 467 468 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 469 { 470 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 471 } 472 473 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 474 { 475 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 476 } 477 478 #define dsa_tree_for_each_user_port(_dp, _dst) \ 479 list_for_each_entry((_dp), &(_dst)->ports, list) \ 480 if (dsa_port_is_user((_dp))) 481 482 #define dsa_switch_for_each_port(_dp, _ds) \ 483 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 484 if ((_dp)->ds == (_ds)) 485 486 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \ 487 list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \ 488 if ((_dp)->ds == (_ds)) 489 490 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \ 491 list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \ 492 if ((_dp)->ds == (_ds)) 493 494 #define dsa_switch_for_each_available_port(_dp, _ds) \ 495 dsa_switch_for_each_port((_dp), (_ds)) \ 496 if (!dsa_port_is_unused((_dp))) 497 498 #define dsa_switch_for_each_user_port(_dp, _ds) \ 499 dsa_switch_for_each_port((_dp), (_ds)) \ 500 if (dsa_port_is_user((_dp))) 501 502 #define dsa_switch_for_each_cpu_port(_dp, _ds) \ 503 dsa_switch_for_each_port((_dp), (_ds)) \ 504 if (dsa_port_is_cpu((_dp))) 505 506 static inline u32 dsa_user_ports(struct dsa_switch *ds) 507 { 508 struct dsa_port *dp; 509 u32 mask = 0; 510 511 dsa_switch_for_each_user_port(dp, ds) 512 mask |= BIT(dp->index); 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_get_interfaces)(struct dsa_switch *ds, int port, 649 unsigned long *supported_interfaces); 650 void (*phylink_validate)(struct dsa_switch *ds, int port, 651 unsigned long *supported, 652 struct phylink_link_state *state); 653 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 654 struct phylink_link_state *state); 655 void (*phylink_mac_config)(struct dsa_switch *ds, int port, 656 unsigned int mode, 657 const struct phylink_link_state *state); 658 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 659 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 660 unsigned int mode, 661 phy_interface_t interface); 662 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 663 unsigned int mode, 664 phy_interface_t interface, 665 struct phy_device *phydev, 666 int speed, int duplex, 667 bool tx_pause, bool rx_pause); 668 void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 669 struct phylink_link_state *state); 670 /* 671 * Port statistics counters. 672 */ 673 void (*get_strings)(struct dsa_switch *ds, int port, 674 u32 stringset, uint8_t *data); 675 void (*get_ethtool_stats)(struct dsa_switch *ds, 676 int port, uint64_t *data); 677 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 678 void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 679 int port, uint64_t *data); 680 void (*get_eth_phy_stats)(struct dsa_switch *ds, int port, 681 struct ethtool_eth_phy_stats *phy_stats); 682 void (*get_eth_mac_stats)(struct dsa_switch *ds, int port, 683 struct ethtool_eth_mac_stats *mac_stats); 684 void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port, 685 struct ethtool_eth_ctrl_stats *ctrl_stats); 686 void (*get_stats64)(struct dsa_switch *ds, int port, 687 struct rtnl_link_stats64 *s); 688 void (*self_test)(struct dsa_switch *ds, int port, 689 struct ethtool_test *etest, u64 *data); 690 691 /* 692 * ethtool Wake-on-LAN 693 */ 694 void (*get_wol)(struct dsa_switch *ds, int port, 695 struct ethtool_wolinfo *w); 696 int (*set_wol)(struct dsa_switch *ds, int port, 697 struct ethtool_wolinfo *w); 698 699 /* 700 * ethtool timestamp info 701 */ 702 int (*get_ts_info)(struct dsa_switch *ds, int port, 703 struct ethtool_ts_info *ts); 704 705 /* 706 * Suspend and resume 707 */ 708 int (*suspend)(struct dsa_switch *ds); 709 int (*resume)(struct dsa_switch *ds); 710 711 /* 712 * Port enable/disable 713 */ 714 int (*port_enable)(struct dsa_switch *ds, int port, 715 struct phy_device *phy); 716 void (*port_disable)(struct dsa_switch *ds, int port); 717 718 /* 719 * Port's MAC EEE settings 720 */ 721 int (*set_mac_eee)(struct dsa_switch *ds, int port, 722 struct ethtool_eee *e); 723 int (*get_mac_eee)(struct dsa_switch *ds, int port, 724 struct ethtool_eee *e); 725 726 /* EEPROM access */ 727 int (*get_eeprom_len)(struct dsa_switch *ds); 728 int (*get_eeprom)(struct dsa_switch *ds, 729 struct ethtool_eeprom *eeprom, u8 *data); 730 int (*set_eeprom)(struct dsa_switch *ds, 731 struct ethtool_eeprom *eeprom, u8 *data); 732 733 /* 734 * Register access. 735 */ 736 int (*get_regs_len)(struct dsa_switch *ds, int port); 737 void (*get_regs)(struct dsa_switch *ds, int port, 738 struct ethtool_regs *regs, void *p); 739 740 /* 741 * Upper device tracking. 742 */ 743 int (*port_prechangeupper)(struct dsa_switch *ds, int port, 744 struct netdev_notifier_changeupper_info *info); 745 746 /* 747 * Bridge integration 748 */ 749 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 750 int (*port_bridge_join)(struct dsa_switch *ds, int port, 751 struct net_device *bridge); 752 void (*port_bridge_leave)(struct dsa_switch *ds, int port, 753 struct net_device *bridge); 754 /* Called right after .port_bridge_join() */ 755 int (*port_bridge_tx_fwd_offload)(struct dsa_switch *ds, int port, 756 struct net_device *bridge, 757 int bridge_num); 758 /* Called right before .port_bridge_leave() */ 759 void (*port_bridge_tx_fwd_unoffload)(struct dsa_switch *ds, int port, 760 struct net_device *bridge, 761 int bridge_num); 762 void (*port_stp_state_set)(struct dsa_switch *ds, int port, 763 u8 state); 764 void (*port_fast_age)(struct dsa_switch *ds, int port); 765 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port, 766 struct switchdev_brport_flags flags, 767 struct netlink_ext_ack *extack); 768 int (*port_bridge_flags)(struct dsa_switch *ds, int port, 769 struct switchdev_brport_flags flags, 770 struct netlink_ext_ack *extack); 771 772 /* 773 * VLAN support 774 */ 775 int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 776 bool vlan_filtering, 777 struct netlink_ext_ack *extack); 778 int (*port_vlan_add)(struct dsa_switch *ds, int port, 779 const struct switchdev_obj_port_vlan *vlan, 780 struct netlink_ext_ack *extack); 781 int (*port_vlan_del)(struct dsa_switch *ds, int port, 782 const struct switchdev_obj_port_vlan *vlan); 783 /* 784 * Forwarding database 785 */ 786 int (*port_fdb_add)(struct dsa_switch *ds, int port, 787 const unsigned char *addr, u16 vid); 788 int (*port_fdb_del)(struct dsa_switch *ds, int port, 789 const unsigned char *addr, u16 vid); 790 int (*port_fdb_dump)(struct dsa_switch *ds, int port, 791 dsa_fdb_dump_cb_t *cb, void *data); 792 793 /* 794 * Multicast database 795 */ 796 int (*port_mdb_add)(struct dsa_switch *ds, int port, 797 const struct switchdev_obj_port_mdb *mdb); 798 int (*port_mdb_del)(struct dsa_switch *ds, int port, 799 const struct switchdev_obj_port_mdb *mdb); 800 /* 801 * RXNFC 802 */ 803 int (*get_rxnfc)(struct dsa_switch *ds, int port, 804 struct ethtool_rxnfc *nfc, u32 *rule_locs); 805 int (*set_rxnfc)(struct dsa_switch *ds, int port, 806 struct ethtool_rxnfc *nfc); 807 808 /* 809 * TC integration 810 */ 811 int (*cls_flower_add)(struct dsa_switch *ds, int port, 812 struct flow_cls_offload *cls, bool ingress); 813 int (*cls_flower_del)(struct dsa_switch *ds, int port, 814 struct flow_cls_offload *cls, bool ingress); 815 int (*cls_flower_stats)(struct dsa_switch *ds, int port, 816 struct flow_cls_offload *cls, bool ingress); 817 int (*port_mirror_add)(struct dsa_switch *ds, int port, 818 struct dsa_mall_mirror_tc_entry *mirror, 819 bool ingress); 820 void (*port_mirror_del)(struct dsa_switch *ds, int port, 821 struct dsa_mall_mirror_tc_entry *mirror); 822 int (*port_policer_add)(struct dsa_switch *ds, int port, 823 struct dsa_mall_policer_tc_entry *policer); 824 void (*port_policer_del)(struct dsa_switch *ds, int port); 825 int (*port_setup_tc)(struct dsa_switch *ds, int port, 826 enum tc_setup_type type, void *type_data); 827 828 /* 829 * Cross-chip operations 830 */ 831 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 832 int sw_index, int port, 833 struct net_device *br); 834 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 835 int sw_index, int port, 836 struct net_device *br); 837 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 838 int port); 839 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 840 int port, struct net_device *lag, 841 struct netdev_lag_upper_info *info); 842 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 843 int port, struct net_device *lag); 844 845 /* 846 * PTP functionality 847 */ 848 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 849 struct ifreq *ifr); 850 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 851 struct ifreq *ifr); 852 void (*port_txtstamp)(struct dsa_switch *ds, int port, 853 struct sk_buff *skb); 854 bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 855 struct sk_buff *skb, unsigned int type); 856 857 /* Devlink parameters, etc */ 858 int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 859 struct devlink_param_gset_ctx *ctx); 860 int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 861 struct devlink_param_gset_ctx *ctx); 862 int (*devlink_info_get)(struct dsa_switch *ds, 863 struct devlink_info_req *req, 864 struct netlink_ext_ack *extack); 865 int (*devlink_sb_pool_get)(struct dsa_switch *ds, 866 unsigned int sb_index, u16 pool_index, 867 struct devlink_sb_pool_info *pool_info); 868 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 869 u16 pool_index, u32 size, 870 enum devlink_sb_threshold_type threshold_type, 871 struct netlink_ext_ack *extack); 872 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 873 unsigned int sb_index, u16 pool_index, 874 u32 *p_threshold); 875 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 876 unsigned int sb_index, u16 pool_index, 877 u32 threshold, 878 struct netlink_ext_ack *extack); 879 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 880 unsigned int sb_index, u16 tc_index, 881 enum devlink_sb_pool_type pool_type, 882 u16 *p_pool_index, u32 *p_threshold); 883 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 884 unsigned int sb_index, u16 tc_index, 885 enum devlink_sb_pool_type pool_type, 886 u16 pool_index, u32 threshold, 887 struct netlink_ext_ack *extack); 888 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 889 unsigned int sb_index); 890 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 891 unsigned int sb_index); 892 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 893 unsigned int sb_index, u16 pool_index, 894 u32 *p_cur, u32 *p_max); 895 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 896 unsigned int sb_index, u16 tc_index, 897 enum devlink_sb_pool_type pool_type, 898 u32 *p_cur, u32 *p_max); 899 900 /* 901 * MTU change functionality. Switches can also adjust their MRU through 902 * this method. By MTU, one understands the SDU (L2 payload) length. 903 * If the switch needs to account for the DSA tag on the CPU port, this 904 * method needs to do so privately. 905 */ 906 int (*port_change_mtu)(struct dsa_switch *ds, int port, 907 int new_mtu); 908 int (*port_max_mtu)(struct dsa_switch *ds, int port); 909 910 /* 911 * LAG integration 912 */ 913 int (*port_lag_change)(struct dsa_switch *ds, int port); 914 int (*port_lag_join)(struct dsa_switch *ds, int port, 915 struct net_device *lag, 916 struct netdev_lag_upper_info *info); 917 int (*port_lag_leave)(struct dsa_switch *ds, int port, 918 struct net_device *lag); 919 920 /* 921 * HSR integration 922 */ 923 int (*port_hsr_join)(struct dsa_switch *ds, int port, 924 struct net_device *hsr); 925 int (*port_hsr_leave)(struct dsa_switch *ds, int port, 926 struct net_device *hsr); 927 928 /* 929 * MRP integration 930 */ 931 int (*port_mrp_add)(struct dsa_switch *ds, int port, 932 const struct switchdev_obj_mrp *mrp); 933 int (*port_mrp_del)(struct dsa_switch *ds, int port, 934 const struct switchdev_obj_mrp *mrp); 935 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port, 936 const struct switchdev_obj_ring_role_mrp *mrp); 937 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port, 938 const struct switchdev_obj_ring_role_mrp *mrp); 939 940 /* 941 * tag_8021q operations 942 */ 943 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid, 944 u16 flags); 945 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid); 946 }; 947 948 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 949 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 950 dsa_devlink_param_get, dsa_devlink_param_set, NULL) 951 952 int dsa_devlink_param_get(struct devlink *dl, u32 id, 953 struct devlink_param_gset_ctx *ctx); 954 int dsa_devlink_param_set(struct devlink *dl, u32 id, 955 struct devlink_param_gset_ctx *ctx); 956 int dsa_devlink_params_register(struct dsa_switch *ds, 957 const struct devlink_param *params, 958 size_t params_count); 959 void dsa_devlink_params_unregister(struct dsa_switch *ds, 960 const struct devlink_param *params, 961 size_t params_count); 962 int dsa_devlink_resource_register(struct dsa_switch *ds, 963 const char *resource_name, 964 u64 resource_size, 965 u64 resource_id, 966 u64 parent_resource_id, 967 const struct devlink_resource_size_params *size_params); 968 969 void dsa_devlink_resources_unregister(struct dsa_switch *ds); 970 971 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 972 u64 resource_id, 973 devlink_resource_occ_get_t *occ_get, 974 void *occ_get_priv); 975 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 976 u64 resource_id); 977 struct devlink_region * 978 dsa_devlink_region_create(struct dsa_switch *ds, 979 const struct devlink_region_ops *ops, 980 u32 region_max_snapshots, u64 region_size); 981 struct devlink_region * 982 dsa_devlink_port_region_create(struct dsa_switch *ds, 983 int port, 984 const struct devlink_port_region_ops *ops, 985 u32 region_max_snapshots, u64 region_size); 986 void dsa_devlink_region_destroy(struct devlink_region *region); 987 988 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 989 990 struct dsa_devlink_priv { 991 struct dsa_switch *ds; 992 }; 993 994 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 995 { 996 struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 997 998 return dl_priv->ds; 999 } 1000 1001 static inline 1002 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 1003 { 1004 struct devlink *dl = port->devlink; 1005 struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1006 1007 return dl_priv->ds; 1008 } 1009 1010 static inline int dsa_devlink_port_to_port(struct devlink_port *port) 1011 { 1012 return port->index; 1013 } 1014 1015 struct dsa_switch_driver { 1016 struct list_head list; 1017 const struct dsa_switch_ops *ops; 1018 }; 1019 1020 struct net_device *dsa_dev_to_net_device(struct device *dev); 1021 1022 /* Keep inline for faster access in hot path */ 1023 static inline bool netdev_uses_dsa(const struct net_device *dev) 1024 { 1025 #if IS_ENABLED(CONFIG_NET_DSA) 1026 return dev->dsa_ptr && dev->dsa_ptr->rcv; 1027 #endif 1028 return false; 1029 } 1030 1031 /* All DSA tags that push the EtherType to the right (basically all except tail 1032 * tags, which don't break dissection) can be treated the same from the 1033 * perspective of the flow dissector. 1034 * 1035 * We need to return: 1036 * - offset: the (B - A) difference between: 1037 * A. the position of the real EtherType and 1038 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 1039 * after the normal EtherType was supposed to be) 1040 * The offset in bytes is exactly equal to the tagger overhead (and half of 1041 * that, in __be16 shorts). 1042 * 1043 * - proto: the value of the real EtherType. 1044 */ 1045 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 1046 __be16 *proto, int *offset) 1047 { 1048 #if IS_ENABLED(CONFIG_NET_DSA) 1049 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 1050 int tag_len = ops->needed_headroom; 1051 1052 *offset = tag_len; 1053 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 1054 #endif 1055 } 1056 1057 #if IS_ENABLED(CONFIG_NET_DSA) 1058 static inline int __dsa_netdevice_ops_check(struct net_device *dev) 1059 { 1060 int err = -EOPNOTSUPP; 1061 1062 if (!dev->dsa_ptr) 1063 return err; 1064 1065 if (!dev->dsa_ptr->netdev_ops) 1066 return err; 1067 1068 return 0; 1069 } 1070 1071 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 1072 int cmd) 1073 { 1074 const struct dsa_netdevice_ops *ops; 1075 int err; 1076 1077 err = __dsa_netdevice_ops_check(dev); 1078 if (err) 1079 return err; 1080 1081 ops = dev->dsa_ptr->netdev_ops; 1082 1083 return ops->ndo_eth_ioctl(dev, ifr, cmd); 1084 } 1085 #else 1086 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 1087 int cmd) 1088 { 1089 return -EOPNOTSUPP; 1090 } 1091 #endif 1092 1093 void dsa_unregister_switch(struct dsa_switch *ds); 1094 int dsa_register_switch(struct dsa_switch *ds); 1095 void dsa_switch_shutdown(struct dsa_switch *ds); 1096 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1097 #ifdef CONFIG_PM_SLEEP 1098 int dsa_switch_suspend(struct dsa_switch *ds); 1099 int dsa_switch_resume(struct dsa_switch *ds); 1100 #else 1101 static inline int dsa_switch_suspend(struct dsa_switch *ds) 1102 { 1103 return 0; 1104 } 1105 static inline int dsa_switch_resume(struct dsa_switch *ds) 1106 { 1107 return 0; 1108 } 1109 #endif /* CONFIG_PM_SLEEP */ 1110 1111 #if IS_ENABLED(CONFIG_NET_DSA) 1112 bool dsa_slave_dev_check(const struct net_device *dev); 1113 #else 1114 static inline bool dsa_slave_dev_check(const struct net_device *dev) 1115 { 1116 return false; 1117 } 1118 #endif 1119 1120 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 1121 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 1122 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 1123 int dsa_port_get_phy_sset_count(struct dsa_port *dp); 1124 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 1125 1126 struct dsa_tag_driver { 1127 const struct dsa_device_ops *ops; 1128 struct list_head list; 1129 struct module *owner; 1130 }; 1131 1132 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 1133 unsigned int count, 1134 struct module *owner); 1135 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 1136 unsigned int count); 1137 1138 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 1139 static int __init dsa_tag_driver_module_init(void) \ 1140 { \ 1141 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 1142 THIS_MODULE); \ 1143 return 0; \ 1144 } \ 1145 module_init(dsa_tag_driver_module_init); \ 1146 \ 1147 static void __exit dsa_tag_driver_module_exit(void) \ 1148 { \ 1149 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 1150 } \ 1151 module_exit(dsa_tag_driver_module_exit) 1152 1153 /** 1154 * module_dsa_tag_drivers() - Helper macro for registering DSA tag 1155 * drivers 1156 * @__ops_array: Array of tag driver strucutres 1157 * 1158 * Helper macro for DSA tag drivers which do not do anything special 1159 * in module init/exit. Each module may only use this macro once, and 1160 * calling it replaces module_init() and module_exit(). 1161 */ 1162 #define module_dsa_tag_drivers(__ops_array) \ 1163 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 1164 1165 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1166 1167 /* Create a static structure we can build a linked list of dsa_tag 1168 * drivers 1169 */ 1170 #define DSA_TAG_DRIVER(__ops) \ 1171 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1172 .ops = &__ops, \ 1173 } 1174 1175 /** 1176 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1177 * driver 1178 * @__ops: Single tag driver structures 1179 * 1180 * Helper macro for DSA tag drivers which do not do anything special 1181 * in module init/exit. Each module may only use this macro once, and 1182 * calling it replaces module_init() and module_exit(). 1183 */ 1184 #define module_dsa_tag_driver(__ops) \ 1185 DSA_TAG_DRIVER(__ops); \ 1186 \ 1187 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1188 &DSA_TAG_DRIVER_NAME(__ops) \ 1189 }; \ 1190 module_dsa_tag_drivers(dsa_tag_driver_array) 1191 #endif 1192 1193