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