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