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 #define DSA_TAG_PROTO_RTL8_4T_VALUE 25 56 #define DSA_TAG_PROTO_RZN1_A5PSW_VALUE 26 57 58 enum dsa_tag_protocol { 59 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE, 60 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE, 61 DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE, 62 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE, 63 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE, 64 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE, 65 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE, 66 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE, 67 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE, 68 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE, 69 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE, 70 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE, 71 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE, 72 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE, 73 DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE, 74 DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE, 75 DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE, 76 DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE, 77 DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE, 78 DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE, 79 DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE, 80 DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE, 81 DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE, 82 DSA_TAG_PROTO_SJA1110 = DSA_TAG_PROTO_SJA1110_VALUE, 83 DSA_TAG_PROTO_RTL8_4 = DSA_TAG_PROTO_RTL8_4_VALUE, 84 DSA_TAG_PROTO_RTL8_4T = DSA_TAG_PROTO_RTL8_4T_VALUE, 85 DSA_TAG_PROTO_RZN1_A5PSW = DSA_TAG_PROTO_RZN1_A5PSW_VALUE, 86 }; 87 88 struct dsa_switch; 89 90 struct dsa_device_ops { 91 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev); 92 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev); 93 void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto, 94 int *offset); 95 int (*connect)(struct dsa_switch *ds); 96 void (*disconnect)(struct dsa_switch *ds); 97 unsigned int needed_headroom; 98 unsigned int needed_tailroom; 99 const char *name; 100 enum dsa_tag_protocol proto; 101 /* Some tagging protocols either mangle or shift the destination MAC 102 * address, in which case the DSA master would drop packets on ingress 103 * if what it understands out of the destination MAC address is not in 104 * its RX filter. 105 */ 106 bool promisc_on_master; 107 }; 108 109 /* This structure defines the control interfaces that are overlayed by the 110 * DSA layer on top of the DSA CPU/management net_device instance. This is 111 * used by the core net_device layer while calling various net_device_ops 112 * function pointers. 113 */ 114 struct dsa_netdevice_ops { 115 int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr, 116 int cmd); 117 }; 118 119 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-" 120 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \ 121 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE)) 122 123 struct dsa_lag { 124 struct net_device *dev; 125 unsigned int id; 126 struct mutex fdb_lock; 127 struct list_head fdbs; 128 refcount_t refcount; 129 }; 130 131 struct dsa_switch_tree { 132 struct list_head list; 133 134 /* List of switch ports */ 135 struct list_head ports; 136 137 /* Notifier chain for switch-wide events */ 138 struct raw_notifier_head nh; 139 140 /* Tree identifier */ 141 unsigned int index; 142 143 /* Number of switches attached to this tree */ 144 struct kref refcount; 145 146 /* Maps offloaded LAG netdevs to a zero-based linear ID for 147 * drivers that need it. 148 */ 149 struct dsa_lag **lags; 150 151 /* Tagging protocol operations */ 152 const struct dsa_device_ops *tag_ops; 153 154 /* Default tagging protocol preferred by the switches in this 155 * tree. 156 */ 157 enum dsa_tag_protocol default_proto; 158 159 /* Has this tree been applied to the hardware? */ 160 bool setup; 161 162 /* 163 * Configuration data for the platform device that owns 164 * this dsa switch tree instance. 165 */ 166 struct dsa_platform_data *pd; 167 168 /* List of DSA links composing the routing table */ 169 struct list_head rtable; 170 171 /* Length of "lags" array */ 172 unsigned int lags_len; 173 174 /* Track the largest switch index within a tree */ 175 unsigned int last_switch; 176 }; 177 178 /* LAG IDs are one-based, the dst->lags array is zero-based */ 179 #define dsa_lags_foreach_id(_id, _dst) \ 180 for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \ 181 if ((_dst)->lags[(_id) - 1]) 182 183 #define dsa_lag_foreach_port(_dp, _dst, _lag) \ 184 list_for_each_entry((_dp), &(_dst)->ports, list) \ 185 if (dsa_port_offloads_lag((_dp), (_lag))) 186 187 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \ 188 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 189 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr)) 190 191 static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst, 192 unsigned int id) 193 { 194 /* DSA LAG IDs are one-based, dst->lags is zero-based */ 195 return dst->lags[id - 1]; 196 } 197 198 static inline int dsa_lag_id(struct dsa_switch_tree *dst, 199 struct net_device *lag_dev) 200 { 201 unsigned int id; 202 203 dsa_lags_foreach_id(id, dst) { 204 struct dsa_lag *lag = dsa_lag_by_id(dst, id); 205 206 if (lag->dev == lag_dev) 207 return lag->id; 208 } 209 210 return -ENODEV; 211 } 212 213 /* TC matchall action types */ 214 enum dsa_port_mall_action_type { 215 DSA_PORT_MALL_MIRROR, 216 DSA_PORT_MALL_POLICER, 217 }; 218 219 /* TC mirroring entry */ 220 struct dsa_mall_mirror_tc_entry { 221 u8 to_local_port; 222 bool ingress; 223 }; 224 225 /* TC port policer entry */ 226 struct dsa_mall_policer_tc_entry { 227 u32 burst; 228 u64 rate_bytes_per_sec; 229 }; 230 231 /* TC matchall entry */ 232 struct dsa_mall_tc_entry { 233 struct list_head list; 234 unsigned long cookie; 235 enum dsa_port_mall_action_type type; 236 union { 237 struct dsa_mall_mirror_tc_entry mirror; 238 struct dsa_mall_policer_tc_entry policer; 239 }; 240 }; 241 242 struct dsa_bridge { 243 struct net_device *dev; 244 unsigned int num; 245 bool tx_fwd_offload; 246 refcount_t refcount; 247 }; 248 249 struct dsa_port { 250 /* A CPU port is physically connected to a master device. 251 * A user port exposed to userspace has a slave device. 252 */ 253 union { 254 struct net_device *master; 255 struct net_device *slave; 256 }; 257 258 /* Copy of the tagging protocol operations, for quicker access 259 * in the data path. Valid only for the CPU ports. 260 */ 261 const struct dsa_device_ops *tag_ops; 262 263 /* Copies for faster access in master receive hot path */ 264 struct dsa_switch_tree *dst; 265 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev); 266 267 struct dsa_switch *ds; 268 269 unsigned int index; 270 271 enum { 272 DSA_PORT_TYPE_UNUSED = 0, 273 DSA_PORT_TYPE_CPU, 274 DSA_PORT_TYPE_DSA, 275 DSA_PORT_TYPE_USER, 276 } type; 277 278 const char *name; 279 struct dsa_port *cpu_dp; 280 u8 mac[ETH_ALEN]; 281 282 u8 stp_state; 283 284 /* Warning: the following bit fields are not atomic, and updating them 285 * can only be done from code paths where concurrency is not possible 286 * (probe time or under rtnl_lock). 287 */ 288 u8 vlan_filtering:1; 289 290 /* Managed by DSA on user ports and by drivers on CPU and DSA ports */ 291 u8 learning:1; 292 293 u8 lag_tx_enabled:1; 294 295 u8 devlink_port_setup:1; 296 297 /* Master state bits, valid only on CPU ports */ 298 u8 master_admin_up:1; 299 u8 master_oper_up:1; 300 301 u8 setup:1; 302 303 struct device_node *dn; 304 unsigned int ageing_time; 305 306 struct dsa_bridge *bridge; 307 struct devlink_port devlink_port; 308 struct phylink *pl; 309 struct phylink_config pl_config; 310 struct dsa_lag *lag; 311 struct net_device *hsr_dev; 312 313 struct list_head list; 314 315 /* 316 * Original copy of the master netdev ethtool_ops 317 */ 318 const struct ethtool_ops *orig_ethtool_ops; 319 320 /* 321 * Original copy of the master netdev net_device_ops 322 */ 323 const struct dsa_netdevice_ops *netdev_ops; 324 325 /* List of MAC addresses that must be forwarded on this port. 326 * These are only valid on CPU ports and DSA links. 327 */ 328 struct mutex addr_lists_lock; 329 struct list_head fdbs; 330 struct list_head mdbs; 331 332 /* List of VLANs that CPU and DSA ports are members of. */ 333 struct mutex vlans_lock; 334 struct list_head vlans; 335 }; 336 337 /* TODO: ideally DSA ports would have a single dp->link_dp member, 338 * and no dst->rtable nor this struct dsa_link would be needed, 339 * but this would require some more complex tree walking, 340 * so keep it stupid at the moment and list them all. 341 */ 342 struct dsa_link { 343 struct dsa_port *dp; 344 struct dsa_port *link_dp; 345 struct list_head list; 346 }; 347 348 enum dsa_db_type { 349 DSA_DB_PORT, 350 DSA_DB_LAG, 351 DSA_DB_BRIDGE, 352 }; 353 354 struct dsa_db { 355 enum dsa_db_type type; 356 357 union { 358 const struct dsa_port *dp; 359 struct dsa_lag lag; 360 struct dsa_bridge bridge; 361 }; 362 }; 363 364 struct dsa_mac_addr { 365 unsigned char addr[ETH_ALEN]; 366 u16 vid; 367 refcount_t refcount; 368 struct list_head list; 369 struct dsa_db db; 370 }; 371 372 struct dsa_vlan { 373 u16 vid; 374 refcount_t refcount; 375 struct list_head list; 376 }; 377 378 struct dsa_switch { 379 struct device *dev; 380 381 /* 382 * Parent switch tree, and switch index. 383 */ 384 struct dsa_switch_tree *dst; 385 unsigned int index; 386 387 /* Warning: the following bit fields are not atomic, and updating them 388 * can only be done from code paths where concurrency is not possible 389 * (probe time or under rtnl_lock). 390 */ 391 u32 setup:1; 392 393 /* Disallow bridge core from requesting different VLAN awareness 394 * settings on ports if not hardware-supported 395 */ 396 u32 vlan_filtering_is_global:1; 397 398 /* Keep VLAN filtering enabled on ports not offloading any upper */ 399 u32 needs_standalone_vlan_filtering:1; 400 401 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges 402 * that have vlan_filtering=0. All drivers should ideally set this (and 403 * then the option would get removed), but it is unknown whether this 404 * would break things or not. 405 */ 406 u32 configure_vlan_while_not_filtering:1; 407 408 /* If the switch driver always programs the CPU port as egress tagged 409 * despite the VLAN configuration indicating otherwise, then setting 410 * @untag_bridge_pvid will force the DSA receive path to pop the 411 * bridge's default_pvid VLAN tagged frames to offer a consistent 412 * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge 413 * device. 414 */ 415 u32 untag_bridge_pvid:1; 416 417 /* Let DSA manage the FDB entries towards the 418 * CPU, based on the software bridge database. 419 */ 420 u32 assisted_learning_on_cpu_port:1; 421 422 /* In case vlan_filtering_is_global is set, the VLAN awareness state 423 * should be retrieved from here and not from the per-port settings. 424 */ 425 u32 vlan_filtering:1; 426 427 /* For switches that only have the MRU configurable. To ensure the 428 * configured MTU is not exceeded, normalization of MRU on all bridged 429 * interfaces is needed. 430 */ 431 u32 mtu_enforcement_ingress:1; 432 433 /* Drivers that isolate the FDBs of multiple bridges must set this 434 * to true to receive the bridge as an argument in .port_fdb_{add,del} 435 * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be 436 * passed as zero. 437 */ 438 u32 fdb_isolation:1; 439 440 /* Listener for switch fabric events */ 441 struct notifier_block nb; 442 443 /* 444 * Give the switch driver somewhere to hang its private data 445 * structure. 446 */ 447 void *priv; 448 449 void *tagger_data; 450 451 /* 452 * Configuration data for this switch. 453 */ 454 struct dsa_chip_data *cd; 455 456 /* 457 * The switch operations. 458 */ 459 const struct dsa_switch_ops *ops; 460 461 /* 462 * Slave mii_bus and devices for the individual ports. 463 */ 464 u32 phys_mii_mask; 465 struct mii_bus *slave_mii_bus; 466 467 /* Ageing Time limits in msecs */ 468 unsigned int ageing_time_min; 469 unsigned int ageing_time_max; 470 471 /* Storage for drivers using tag_8021q */ 472 struct dsa_8021q_context *tag_8021q_ctx; 473 474 /* devlink used to represent this switch device */ 475 struct devlink *devlink; 476 477 /* Number of switch port queues */ 478 unsigned int num_tx_queues; 479 480 /* Drivers that benefit from having an ID associated with each 481 * offloaded LAG should set this to the maximum number of 482 * supported IDs. DSA will then maintain a mapping of _at 483 * least_ these many IDs, accessible to drivers via 484 * dsa_lag_id(). 485 */ 486 unsigned int num_lag_ids; 487 488 /* Drivers that support bridge forwarding offload or FDB isolation 489 * should set this to the maximum number of bridges spanning the same 490 * switch tree (or all trees, in the case of cross-tree bridging 491 * support) that can be offloaded. 492 */ 493 unsigned int max_num_bridges; 494 495 unsigned int num_ports; 496 }; 497 498 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 499 { 500 struct dsa_switch_tree *dst = ds->dst; 501 struct dsa_port *dp; 502 503 list_for_each_entry(dp, &dst->ports, list) 504 if (dp->ds == ds && dp->index == p) 505 return dp; 506 507 return NULL; 508 } 509 510 static inline bool dsa_port_is_dsa(struct dsa_port *port) 511 { 512 return port->type == DSA_PORT_TYPE_DSA; 513 } 514 515 static inline bool dsa_port_is_cpu(struct dsa_port *port) 516 { 517 return port->type == DSA_PORT_TYPE_CPU; 518 } 519 520 static inline bool dsa_port_is_user(struct dsa_port *dp) 521 { 522 return dp->type == DSA_PORT_TYPE_USER; 523 } 524 525 static inline bool dsa_port_is_unused(struct dsa_port *dp) 526 { 527 return dp->type == DSA_PORT_TYPE_UNUSED; 528 } 529 530 static inline bool dsa_port_master_is_operational(struct dsa_port *dp) 531 { 532 return dsa_port_is_cpu(dp) && dp->master_admin_up && 533 dp->master_oper_up; 534 } 535 536 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 537 { 538 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 539 } 540 541 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 542 { 543 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 544 } 545 546 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 547 { 548 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 549 } 550 551 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 552 { 553 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 554 } 555 556 #define dsa_tree_for_each_user_port(_dp, _dst) \ 557 list_for_each_entry((_dp), &(_dst)->ports, list) \ 558 if (dsa_port_is_user((_dp))) 559 560 #define dsa_switch_for_each_port(_dp, _ds) \ 561 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 562 if ((_dp)->ds == (_ds)) 563 564 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \ 565 list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \ 566 if ((_dp)->ds == (_ds)) 567 568 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \ 569 list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \ 570 if ((_dp)->ds == (_ds)) 571 572 #define dsa_switch_for_each_available_port(_dp, _ds) \ 573 dsa_switch_for_each_port((_dp), (_ds)) \ 574 if (!dsa_port_is_unused((_dp))) 575 576 #define dsa_switch_for_each_user_port(_dp, _ds) \ 577 dsa_switch_for_each_port((_dp), (_ds)) \ 578 if (dsa_port_is_user((_dp))) 579 580 #define dsa_switch_for_each_cpu_port(_dp, _ds) \ 581 dsa_switch_for_each_port((_dp), (_ds)) \ 582 if (dsa_port_is_cpu((_dp))) 583 584 #define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \ 585 dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \ 586 if (dsa_port_is_cpu((_dp))) 587 588 static inline u32 dsa_user_ports(struct dsa_switch *ds) 589 { 590 struct dsa_port *dp; 591 u32 mask = 0; 592 593 dsa_switch_for_each_user_port(dp, ds) 594 mask |= BIT(dp->index); 595 596 return mask; 597 } 598 599 static inline u32 dsa_cpu_ports(struct dsa_switch *ds) 600 { 601 struct dsa_port *cpu_dp; 602 u32 mask = 0; 603 604 dsa_switch_for_each_cpu_port(cpu_dp, ds) 605 mask |= BIT(cpu_dp->index); 606 607 return mask; 608 } 609 610 /* Return the local port used to reach an arbitrary switch device */ 611 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 612 { 613 struct dsa_switch_tree *dst = ds->dst; 614 struct dsa_link *dl; 615 616 list_for_each_entry(dl, &dst->rtable, list) 617 if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 618 return dl->dp->index; 619 620 return ds->num_ports; 621 } 622 623 /* Return the local port used to reach an arbitrary switch port */ 624 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 625 int port) 626 { 627 if (device == ds->index) 628 return port; 629 else 630 return dsa_routing_port(ds, device); 631 } 632 633 /* Return the local port used to reach the dedicated CPU port */ 634 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 635 { 636 const struct dsa_port *dp = dsa_to_port(ds, port); 637 const struct dsa_port *cpu_dp = dp->cpu_dp; 638 639 if (!cpu_dp) 640 return port; 641 642 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 643 } 644 645 /* Return true if this is the local port used to reach the CPU port */ 646 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port) 647 { 648 if (dsa_is_unused_port(ds, port)) 649 return false; 650 651 return port == dsa_upstream_port(ds, port); 652 } 653 654 /* Return true if this is a DSA port leading away from the CPU */ 655 static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port) 656 { 657 return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port); 658 } 659 660 /* Return the local port used to reach the CPU port */ 661 static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds) 662 { 663 struct dsa_port *dp; 664 665 dsa_switch_for_each_available_port(dp, ds) { 666 return dsa_upstream_port(ds, dp->index); 667 } 668 669 return ds->num_ports; 670 } 671 672 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning 673 * that the routing port from @downstream_ds to @upstream_ds is also the port 674 * which @downstream_ds uses to reach its dedicated CPU. 675 */ 676 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds, 677 struct dsa_switch *downstream_ds) 678 { 679 int routing_port; 680 681 if (upstream_ds == downstream_ds) 682 return true; 683 684 routing_port = dsa_routing_port(downstream_ds, upstream_ds->index); 685 686 return dsa_is_upstream_port(downstream_ds, routing_port); 687 } 688 689 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 690 { 691 const struct dsa_switch *ds = dp->ds; 692 693 if (ds->vlan_filtering_is_global) 694 return ds->vlan_filtering; 695 else 696 return dp->vlan_filtering; 697 } 698 699 static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp) 700 { 701 return dp->lag ? dp->lag->id : 0; 702 } 703 704 static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp) 705 { 706 return dp->lag ? dp->lag->dev : NULL; 707 } 708 709 static inline bool dsa_port_offloads_lag(struct dsa_port *dp, 710 const struct dsa_lag *lag) 711 { 712 return dsa_port_lag_dev_get(dp) == lag->dev; 713 } 714 715 static inline 716 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) 717 { 718 if (!dp->bridge) 719 return NULL; 720 721 if (dp->lag) 722 return dp->lag->dev; 723 else if (dp->hsr_dev) 724 return dp->hsr_dev; 725 726 return dp->slave; 727 } 728 729 static inline struct net_device * 730 dsa_port_bridge_dev_get(const struct dsa_port *dp) 731 { 732 return dp->bridge ? dp->bridge->dev : NULL; 733 } 734 735 static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp) 736 { 737 return dp->bridge ? dp->bridge->num : 0; 738 } 739 740 static inline bool dsa_port_bridge_same(const struct dsa_port *a, 741 const struct dsa_port *b) 742 { 743 struct net_device *br_a = dsa_port_bridge_dev_get(a); 744 struct net_device *br_b = dsa_port_bridge_dev_get(b); 745 746 /* Standalone ports are not in the same bridge with one another */ 747 return (!br_a || !br_b) ? false : (br_a == br_b); 748 } 749 750 static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp, 751 const struct net_device *dev) 752 { 753 return dsa_port_to_bridge_port(dp) == dev; 754 } 755 756 static inline bool 757 dsa_port_offloads_bridge_dev(struct dsa_port *dp, 758 const struct net_device *bridge_dev) 759 { 760 /* DSA ports connected to a bridge, and event was emitted 761 * for the bridge. 762 */ 763 return dsa_port_bridge_dev_get(dp) == bridge_dev; 764 } 765 766 static inline bool dsa_port_offloads_bridge(struct dsa_port *dp, 767 const struct dsa_bridge *bridge) 768 { 769 return dsa_port_bridge_dev_get(dp) == bridge->dev; 770 } 771 772 /* Returns true if any port of this tree offloads the given net_device */ 773 static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst, 774 const struct net_device *dev) 775 { 776 struct dsa_port *dp; 777 778 list_for_each_entry(dp, &dst->ports, list) 779 if (dsa_port_offloads_bridge_port(dp, dev)) 780 return true; 781 782 return false; 783 } 784 785 /* Returns true if any port of this tree offloads the given bridge */ 786 static inline bool 787 dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst, 788 const struct net_device *bridge_dev) 789 { 790 struct dsa_port *dp; 791 792 list_for_each_entry(dp, &dst->ports, list) 793 if (dsa_port_offloads_bridge_dev(dp, bridge_dev)) 794 return true; 795 796 return false; 797 } 798 799 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 800 bool is_static, void *data); 801 struct dsa_switch_ops { 802 /* 803 * Tagging protocol helpers called for the CPU ports and DSA links. 804 * @get_tag_protocol retrieves the initial tagging protocol and is 805 * mandatory. Switches which can operate using multiple tagging 806 * protocols should implement @change_tag_protocol and report in 807 * @get_tag_protocol the tagger in current use. 808 */ 809 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 810 int port, 811 enum dsa_tag_protocol mprot); 812 int (*change_tag_protocol)(struct dsa_switch *ds, 813 enum dsa_tag_protocol proto); 814 /* 815 * Method for switch drivers to connect to the tagging protocol driver 816 * in current use. The switch driver can provide handlers for certain 817 * types of packets for switch management. 818 */ 819 int (*connect_tag_protocol)(struct dsa_switch *ds, 820 enum dsa_tag_protocol proto); 821 822 /* Optional switch-wide initialization and destruction methods */ 823 int (*setup)(struct dsa_switch *ds); 824 void (*teardown)(struct dsa_switch *ds); 825 826 /* Per-port initialization and destruction methods. Mandatory if the 827 * driver registers devlink port regions, optional otherwise. 828 */ 829 int (*port_setup)(struct dsa_switch *ds, int port); 830 void (*port_teardown)(struct dsa_switch *ds, int port); 831 832 u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 833 834 /* 835 * Access to the switch's PHY registers. 836 */ 837 int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 838 int (*phy_write)(struct dsa_switch *ds, int port, 839 int regnum, u16 val); 840 841 /* 842 * Link state adjustment (called from libphy) 843 */ 844 void (*adjust_link)(struct dsa_switch *ds, int port, 845 struct phy_device *phydev); 846 void (*fixed_link_update)(struct dsa_switch *ds, int port, 847 struct fixed_phy_status *st); 848 849 /* 850 * PHYLINK integration 851 */ 852 void (*phylink_get_caps)(struct dsa_switch *ds, int port, 853 struct phylink_config *config); 854 void (*phylink_validate)(struct dsa_switch *ds, int port, 855 unsigned long *supported, 856 struct phylink_link_state *state); 857 struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds, 858 int port, 859 phy_interface_t iface); 860 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 861 struct phylink_link_state *state); 862 void (*phylink_mac_config)(struct dsa_switch *ds, int port, 863 unsigned int mode, 864 const struct phylink_link_state *state); 865 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 866 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 867 unsigned int mode, 868 phy_interface_t interface); 869 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 870 unsigned int mode, 871 phy_interface_t interface, 872 struct phy_device *phydev, 873 int speed, int duplex, 874 bool tx_pause, bool rx_pause); 875 void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 876 struct phylink_link_state *state); 877 /* 878 * Port statistics counters. 879 */ 880 void (*get_strings)(struct dsa_switch *ds, int port, 881 u32 stringset, uint8_t *data); 882 void (*get_ethtool_stats)(struct dsa_switch *ds, 883 int port, uint64_t *data); 884 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 885 void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 886 int port, uint64_t *data); 887 void (*get_eth_phy_stats)(struct dsa_switch *ds, int port, 888 struct ethtool_eth_phy_stats *phy_stats); 889 void (*get_eth_mac_stats)(struct dsa_switch *ds, int port, 890 struct ethtool_eth_mac_stats *mac_stats); 891 void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port, 892 struct ethtool_eth_ctrl_stats *ctrl_stats); 893 void (*get_rmon_stats)(struct dsa_switch *ds, int port, 894 struct ethtool_rmon_stats *rmon_stats, 895 const struct ethtool_rmon_hist_range **ranges); 896 void (*get_stats64)(struct dsa_switch *ds, int port, 897 struct rtnl_link_stats64 *s); 898 void (*self_test)(struct dsa_switch *ds, int port, 899 struct ethtool_test *etest, u64 *data); 900 901 /* 902 * ethtool Wake-on-LAN 903 */ 904 void (*get_wol)(struct dsa_switch *ds, int port, 905 struct ethtool_wolinfo *w); 906 int (*set_wol)(struct dsa_switch *ds, int port, 907 struct ethtool_wolinfo *w); 908 909 /* 910 * ethtool timestamp info 911 */ 912 int (*get_ts_info)(struct dsa_switch *ds, int port, 913 struct ethtool_ts_info *ts); 914 915 /* 916 * DCB ops 917 */ 918 int (*port_get_default_prio)(struct dsa_switch *ds, int port); 919 int (*port_set_default_prio)(struct dsa_switch *ds, int port, 920 u8 prio); 921 int (*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp); 922 int (*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp, 923 u8 prio); 924 int (*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp, 925 u8 prio); 926 927 /* 928 * Suspend and resume 929 */ 930 int (*suspend)(struct dsa_switch *ds); 931 int (*resume)(struct dsa_switch *ds); 932 933 /* 934 * Port enable/disable 935 */ 936 int (*port_enable)(struct dsa_switch *ds, int port, 937 struct phy_device *phy); 938 void (*port_disable)(struct dsa_switch *ds, int port); 939 940 /* 941 * Port's MAC EEE settings 942 */ 943 int (*set_mac_eee)(struct dsa_switch *ds, int port, 944 struct ethtool_eee *e); 945 int (*get_mac_eee)(struct dsa_switch *ds, int port, 946 struct ethtool_eee *e); 947 948 /* EEPROM access */ 949 int (*get_eeprom_len)(struct dsa_switch *ds); 950 int (*get_eeprom)(struct dsa_switch *ds, 951 struct ethtool_eeprom *eeprom, u8 *data); 952 int (*set_eeprom)(struct dsa_switch *ds, 953 struct ethtool_eeprom *eeprom, u8 *data); 954 955 /* 956 * Register access. 957 */ 958 int (*get_regs_len)(struct dsa_switch *ds, int port); 959 void (*get_regs)(struct dsa_switch *ds, int port, 960 struct ethtool_regs *regs, void *p); 961 962 /* 963 * Upper device tracking. 964 */ 965 int (*port_prechangeupper)(struct dsa_switch *ds, int port, 966 struct netdev_notifier_changeupper_info *info); 967 968 /* 969 * Bridge integration 970 */ 971 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 972 int (*port_bridge_join)(struct dsa_switch *ds, int port, 973 struct dsa_bridge bridge, 974 bool *tx_fwd_offload, 975 struct netlink_ext_ack *extack); 976 void (*port_bridge_leave)(struct dsa_switch *ds, int port, 977 struct dsa_bridge bridge); 978 void (*port_stp_state_set)(struct dsa_switch *ds, int port, 979 u8 state); 980 int (*port_mst_state_set)(struct dsa_switch *ds, int port, 981 const struct switchdev_mst_state *state); 982 void (*port_fast_age)(struct dsa_switch *ds, int port); 983 int (*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid); 984 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port, 985 struct switchdev_brport_flags flags, 986 struct netlink_ext_ack *extack); 987 int (*port_bridge_flags)(struct dsa_switch *ds, int port, 988 struct switchdev_brport_flags flags, 989 struct netlink_ext_ack *extack); 990 void (*port_set_host_flood)(struct dsa_switch *ds, int port, 991 bool uc, bool mc); 992 993 /* 994 * VLAN support 995 */ 996 int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 997 bool vlan_filtering, 998 struct netlink_ext_ack *extack); 999 int (*port_vlan_add)(struct dsa_switch *ds, int port, 1000 const struct switchdev_obj_port_vlan *vlan, 1001 struct netlink_ext_ack *extack); 1002 int (*port_vlan_del)(struct dsa_switch *ds, int port, 1003 const struct switchdev_obj_port_vlan *vlan); 1004 int (*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge, 1005 const struct switchdev_vlan_msti *msti); 1006 1007 /* 1008 * Forwarding database 1009 */ 1010 int (*port_fdb_add)(struct dsa_switch *ds, int port, 1011 const unsigned char *addr, u16 vid, 1012 struct dsa_db db); 1013 int (*port_fdb_del)(struct dsa_switch *ds, int port, 1014 const unsigned char *addr, u16 vid, 1015 struct dsa_db db); 1016 int (*port_fdb_dump)(struct dsa_switch *ds, int port, 1017 dsa_fdb_dump_cb_t *cb, void *data); 1018 int (*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag, 1019 const unsigned char *addr, u16 vid, 1020 struct dsa_db db); 1021 int (*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag, 1022 const unsigned char *addr, u16 vid, 1023 struct dsa_db db); 1024 1025 /* 1026 * Multicast database 1027 */ 1028 int (*port_mdb_add)(struct dsa_switch *ds, int port, 1029 const struct switchdev_obj_port_mdb *mdb, 1030 struct dsa_db db); 1031 int (*port_mdb_del)(struct dsa_switch *ds, int port, 1032 const struct switchdev_obj_port_mdb *mdb, 1033 struct dsa_db db); 1034 /* 1035 * RXNFC 1036 */ 1037 int (*get_rxnfc)(struct dsa_switch *ds, int port, 1038 struct ethtool_rxnfc *nfc, u32 *rule_locs); 1039 int (*set_rxnfc)(struct dsa_switch *ds, int port, 1040 struct ethtool_rxnfc *nfc); 1041 1042 /* 1043 * TC integration 1044 */ 1045 int (*cls_flower_add)(struct dsa_switch *ds, int port, 1046 struct flow_cls_offload *cls, bool ingress); 1047 int (*cls_flower_del)(struct dsa_switch *ds, int port, 1048 struct flow_cls_offload *cls, bool ingress); 1049 int (*cls_flower_stats)(struct dsa_switch *ds, int port, 1050 struct flow_cls_offload *cls, bool ingress); 1051 int (*port_mirror_add)(struct dsa_switch *ds, int port, 1052 struct dsa_mall_mirror_tc_entry *mirror, 1053 bool ingress, struct netlink_ext_ack *extack); 1054 void (*port_mirror_del)(struct dsa_switch *ds, int port, 1055 struct dsa_mall_mirror_tc_entry *mirror); 1056 int (*port_policer_add)(struct dsa_switch *ds, int port, 1057 struct dsa_mall_policer_tc_entry *policer); 1058 void (*port_policer_del)(struct dsa_switch *ds, int port); 1059 int (*port_setup_tc)(struct dsa_switch *ds, int port, 1060 enum tc_setup_type type, void *type_data); 1061 1062 /* 1063 * Cross-chip operations 1064 */ 1065 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 1066 int sw_index, int port, 1067 struct dsa_bridge bridge, 1068 struct netlink_ext_ack *extack); 1069 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 1070 int sw_index, int port, 1071 struct dsa_bridge bridge); 1072 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 1073 int port); 1074 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 1075 int port, struct dsa_lag lag, 1076 struct netdev_lag_upper_info *info); 1077 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 1078 int port, struct dsa_lag lag); 1079 1080 /* 1081 * PTP functionality 1082 */ 1083 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 1084 struct ifreq *ifr); 1085 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 1086 struct ifreq *ifr); 1087 void (*port_txtstamp)(struct dsa_switch *ds, int port, 1088 struct sk_buff *skb); 1089 bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 1090 struct sk_buff *skb, unsigned int type); 1091 1092 /* Devlink parameters, etc */ 1093 int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 1094 struct devlink_param_gset_ctx *ctx); 1095 int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 1096 struct devlink_param_gset_ctx *ctx); 1097 int (*devlink_info_get)(struct dsa_switch *ds, 1098 struct devlink_info_req *req, 1099 struct netlink_ext_ack *extack); 1100 int (*devlink_sb_pool_get)(struct dsa_switch *ds, 1101 unsigned int sb_index, u16 pool_index, 1102 struct devlink_sb_pool_info *pool_info); 1103 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 1104 u16 pool_index, u32 size, 1105 enum devlink_sb_threshold_type threshold_type, 1106 struct netlink_ext_ack *extack); 1107 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 1108 unsigned int sb_index, u16 pool_index, 1109 u32 *p_threshold); 1110 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 1111 unsigned int sb_index, u16 pool_index, 1112 u32 threshold, 1113 struct netlink_ext_ack *extack); 1114 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 1115 unsigned int sb_index, u16 tc_index, 1116 enum devlink_sb_pool_type pool_type, 1117 u16 *p_pool_index, u32 *p_threshold); 1118 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 1119 unsigned int sb_index, u16 tc_index, 1120 enum devlink_sb_pool_type pool_type, 1121 u16 pool_index, u32 threshold, 1122 struct netlink_ext_ack *extack); 1123 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 1124 unsigned int sb_index); 1125 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 1126 unsigned int sb_index); 1127 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 1128 unsigned int sb_index, u16 pool_index, 1129 u32 *p_cur, u32 *p_max); 1130 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 1131 unsigned int sb_index, u16 tc_index, 1132 enum devlink_sb_pool_type pool_type, 1133 u32 *p_cur, u32 *p_max); 1134 1135 /* 1136 * MTU change functionality. Switches can also adjust their MRU through 1137 * this method. By MTU, one understands the SDU (L2 payload) length. 1138 * If the switch needs to account for the DSA tag on the CPU port, this 1139 * method needs to do so privately. 1140 */ 1141 int (*port_change_mtu)(struct dsa_switch *ds, int port, 1142 int new_mtu); 1143 int (*port_max_mtu)(struct dsa_switch *ds, int port); 1144 1145 /* 1146 * LAG integration 1147 */ 1148 int (*port_lag_change)(struct dsa_switch *ds, int port); 1149 int (*port_lag_join)(struct dsa_switch *ds, int port, 1150 struct dsa_lag lag, 1151 struct netdev_lag_upper_info *info); 1152 int (*port_lag_leave)(struct dsa_switch *ds, int port, 1153 struct dsa_lag lag); 1154 1155 /* 1156 * HSR integration 1157 */ 1158 int (*port_hsr_join)(struct dsa_switch *ds, int port, 1159 struct net_device *hsr); 1160 int (*port_hsr_leave)(struct dsa_switch *ds, int port, 1161 struct net_device *hsr); 1162 1163 /* 1164 * MRP integration 1165 */ 1166 int (*port_mrp_add)(struct dsa_switch *ds, int port, 1167 const struct switchdev_obj_mrp *mrp); 1168 int (*port_mrp_del)(struct dsa_switch *ds, int port, 1169 const struct switchdev_obj_mrp *mrp); 1170 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port, 1171 const struct switchdev_obj_ring_role_mrp *mrp); 1172 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port, 1173 const struct switchdev_obj_ring_role_mrp *mrp); 1174 1175 /* 1176 * tag_8021q operations 1177 */ 1178 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid, 1179 u16 flags); 1180 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid); 1181 1182 /* 1183 * DSA master tracking operations 1184 */ 1185 void (*master_state_change)(struct dsa_switch *ds, 1186 const struct net_device *master, 1187 bool operational); 1188 }; 1189 1190 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 1191 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 1192 dsa_devlink_param_get, dsa_devlink_param_set, NULL) 1193 1194 int dsa_devlink_param_get(struct devlink *dl, u32 id, 1195 struct devlink_param_gset_ctx *ctx); 1196 int dsa_devlink_param_set(struct devlink *dl, u32 id, 1197 struct devlink_param_gset_ctx *ctx); 1198 int dsa_devlink_params_register(struct dsa_switch *ds, 1199 const struct devlink_param *params, 1200 size_t params_count); 1201 void dsa_devlink_params_unregister(struct dsa_switch *ds, 1202 const struct devlink_param *params, 1203 size_t params_count); 1204 int dsa_devlink_resource_register(struct dsa_switch *ds, 1205 const char *resource_name, 1206 u64 resource_size, 1207 u64 resource_id, 1208 u64 parent_resource_id, 1209 const struct devlink_resource_size_params *size_params); 1210 1211 void dsa_devlink_resources_unregister(struct dsa_switch *ds); 1212 1213 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 1214 u64 resource_id, 1215 devlink_resource_occ_get_t *occ_get, 1216 void *occ_get_priv); 1217 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 1218 u64 resource_id); 1219 struct devlink_region * 1220 dsa_devlink_region_create(struct dsa_switch *ds, 1221 const struct devlink_region_ops *ops, 1222 u32 region_max_snapshots, u64 region_size); 1223 struct devlink_region * 1224 dsa_devlink_port_region_create(struct dsa_switch *ds, 1225 int port, 1226 const struct devlink_port_region_ops *ops, 1227 u32 region_max_snapshots, u64 region_size); 1228 void dsa_devlink_region_destroy(struct devlink_region *region); 1229 1230 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 1231 1232 struct dsa_devlink_priv { 1233 struct dsa_switch *ds; 1234 }; 1235 1236 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 1237 { 1238 struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1239 1240 return dl_priv->ds; 1241 } 1242 1243 static inline 1244 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 1245 { 1246 struct devlink *dl = port->devlink; 1247 struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1248 1249 return dl_priv->ds; 1250 } 1251 1252 static inline int dsa_devlink_port_to_port(struct devlink_port *port) 1253 { 1254 return port->index; 1255 } 1256 1257 struct dsa_switch_driver { 1258 struct list_head list; 1259 const struct dsa_switch_ops *ops; 1260 }; 1261 1262 struct net_device *dsa_dev_to_net_device(struct device *dev); 1263 1264 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port, 1265 const unsigned char *addr, u16 vid, 1266 struct dsa_db db); 1267 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port, 1268 const struct switchdev_obj_port_mdb *mdb, 1269 struct dsa_db db); 1270 1271 /* Keep inline for faster access in hot path */ 1272 static inline bool netdev_uses_dsa(const struct net_device *dev) 1273 { 1274 #if IS_ENABLED(CONFIG_NET_DSA) 1275 return dev->dsa_ptr && dev->dsa_ptr->rcv; 1276 #endif 1277 return false; 1278 } 1279 1280 /* All DSA tags that push the EtherType to the right (basically all except tail 1281 * tags, which don't break dissection) can be treated the same from the 1282 * perspective of the flow dissector. 1283 * 1284 * We need to return: 1285 * - offset: the (B - A) difference between: 1286 * A. the position of the real EtherType and 1287 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 1288 * after the normal EtherType was supposed to be) 1289 * The offset in bytes is exactly equal to the tagger overhead (and half of 1290 * that, in __be16 shorts). 1291 * 1292 * - proto: the value of the real EtherType. 1293 */ 1294 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 1295 __be16 *proto, int *offset) 1296 { 1297 #if IS_ENABLED(CONFIG_NET_DSA) 1298 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 1299 int tag_len = ops->needed_headroom; 1300 1301 *offset = tag_len; 1302 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 1303 #endif 1304 } 1305 1306 #if IS_ENABLED(CONFIG_NET_DSA) 1307 static inline int __dsa_netdevice_ops_check(struct net_device *dev) 1308 { 1309 int err = -EOPNOTSUPP; 1310 1311 if (!dev->dsa_ptr) 1312 return err; 1313 1314 if (!dev->dsa_ptr->netdev_ops) 1315 return err; 1316 1317 return 0; 1318 } 1319 1320 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 1321 int cmd) 1322 { 1323 const struct dsa_netdevice_ops *ops; 1324 int err; 1325 1326 err = __dsa_netdevice_ops_check(dev); 1327 if (err) 1328 return err; 1329 1330 ops = dev->dsa_ptr->netdev_ops; 1331 1332 return ops->ndo_eth_ioctl(dev, ifr, cmd); 1333 } 1334 #else 1335 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 1336 int cmd) 1337 { 1338 return -EOPNOTSUPP; 1339 } 1340 #endif 1341 1342 void dsa_unregister_switch(struct dsa_switch *ds); 1343 int dsa_register_switch(struct dsa_switch *ds); 1344 void dsa_switch_shutdown(struct dsa_switch *ds); 1345 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1346 void dsa_flush_workqueue(void); 1347 #ifdef CONFIG_PM_SLEEP 1348 int dsa_switch_suspend(struct dsa_switch *ds); 1349 int dsa_switch_resume(struct dsa_switch *ds); 1350 #else 1351 static inline int dsa_switch_suspend(struct dsa_switch *ds) 1352 { 1353 return 0; 1354 } 1355 static inline int dsa_switch_resume(struct dsa_switch *ds) 1356 { 1357 return 0; 1358 } 1359 #endif /* CONFIG_PM_SLEEP */ 1360 1361 #if IS_ENABLED(CONFIG_NET_DSA) 1362 bool dsa_slave_dev_check(const struct net_device *dev); 1363 #else 1364 static inline bool dsa_slave_dev_check(const struct net_device *dev) 1365 { 1366 return false; 1367 } 1368 #endif 1369 1370 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 1371 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 1372 1373 struct dsa_tag_driver { 1374 const struct dsa_device_ops *ops; 1375 struct list_head list; 1376 struct module *owner; 1377 }; 1378 1379 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 1380 unsigned int count, 1381 struct module *owner); 1382 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 1383 unsigned int count); 1384 1385 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 1386 static int __init dsa_tag_driver_module_init(void) \ 1387 { \ 1388 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 1389 THIS_MODULE); \ 1390 return 0; \ 1391 } \ 1392 module_init(dsa_tag_driver_module_init); \ 1393 \ 1394 static void __exit dsa_tag_driver_module_exit(void) \ 1395 { \ 1396 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 1397 } \ 1398 module_exit(dsa_tag_driver_module_exit) 1399 1400 /** 1401 * module_dsa_tag_drivers() - Helper macro for registering DSA tag 1402 * drivers 1403 * @__ops_array: Array of tag driver structures 1404 * 1405 * Helper macro for DSA tag drivers which do not do anything special 1406 * in module init/exit. Each module may only use this macro once, and 1407 * calling it replaces module_init() and module_exit(). 1408 */ 1409 #define module_dsa_tag_drivers(__ops_array) \ 1410 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 1411 1412 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1413 1414 /* Create a static structure we can build a linked list of dsa_tag 1415 * drivers 1416 */ 1417 #define DSA_TAG_DRIVER(__ops) \ 1418 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1419 .ops = &__ops, \ 1420 } 1421 1422 /** 1423 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1424 * driver 1425 * @__ops: Single tag driver structures 1426 * 1427 * Helper macro for DSA tag drivers which do not do anything special 1428 * in module init/exit. Each module may only use this macro once, and 1429 * calling it replaces module_init() and module_exit(). 1430 */ 1431 #define module_dsa_tag_driver(__ops) \ 1432 DSA_TAG_DRIVER(__ops); \ 1433 \ 1434 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1435 &DSA_TAG_DRIVER_NAME(__ops) \ 1436 }; \ 1437 module_dsa_tag_drivers(dsa_tag_driver_array) 1438 #endif 1439 1440