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