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 (*get_pause_stats)(struct dsa_switch *ds, int port, 899 struct ethtool_pause_stats *pause_stats); 900 void (*self_test)(struct dsa_switch *ds, int port, 901 struct ethtool_test *etest, u64 *data); 902 903 /* 904 * ethtool Wake-on-LAN 905 */ 906 void (*get_wol)(struct dsa_switch *ds, int port, 907 struct ethtool_wolinfo *w); 908 int (*set_wol)(struct dsa_switch *ds, int port, 909 struct ethtool_wolinfo *w); 910 911 /* 912 * ethtool timestamp info 913 */ 914 int (*get_ts_info)(struct dsa_switch *ds, int port, 915 struct ethtool_ts_info *ts); 916 917 /* 918 * DCB ops 919 */ 920 int (*port_get_default_prio)(struct dsa_switch *ds, int port); 921 int (*port_set_default_prio)(struct dsa_switch *ds, int port, 922 u8 prio); 923 int (*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp); 924 int (*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp, 925 u8 prio); 926 int (*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp, 927 u8 prio); 928 929 /* 930 * Suspend and resume 931 */ 932 int (*suspend)(struct dsa_switch *ds); 933 int (*resume)(struct dsa_switch *ds); 934 935 /* 936 * Port enable/disable 937 */ 938 int (*port_enable)(struct dsa_switch *ds, int port, 939 struct phy_device *phy); 940 void (*port_disable)(struct dsa_switch *ds, int port); 941 942 /* 943 * Port's MAC EEE settings 944 */ 945 int (*set_mac_eee)(struct dsa_switch *ds, int port, 946 struct ethtool_eee *e); 947 int (*get_mac_eee)(struct dsa_switch *ds, int port, 948 struct ethtool_eee *e); 949 950 /* EEPROM access */ 951 int (*get_eeprom_len)(struct dsa_switch *ds); 952 int (*get_eeprom)(struct dsa_switch *ds, 953 struct ethtool_eeprom *eeprom, u8 *data); 954 int (*set_eeprom)(struct dsa_switch *ds, 955 struct ethtool_eeprom *eeprom, u8 *data); 956 957 /* 958 * Register access. 959 */ 960 int (*get_regs_len)(struct dsa_switch *ds, int port); 961 void (*get_regs)(struct dsa_switch *ds, int port, 962 struct ethtool_regs *regs, void *p); 963 964 /* 965 * Upper device tracking. 966 */ 967 int (*port_prechangeupper)(struct dsa_switch *ds, int port, 968 struct netdev_notifier_changeupper_info *info); 969 970 /* 971 * Bridge integration 972 */ 973 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 974 int (*port_bridge_join)(struct dsa_switch *ds, int port, 975 struct dsa_bridge bridge, 976 bool *tx_fwd_offload, 977 struct netlink_ext_ack *extack); 978 void (*port_bridge_leave)(struct dsa_switch *ds, int port, 979 struct dsa_bridge bridge); 980 void (*port_stp_state_set)(struct dsa_switch *ds, int port, 981 u8 state); 982 int (*port_mst_state_set)(struct dsa_switch *ds, int port, 983 const struct switchdev_mst_state *state); 984 void (*port_fast_age)(struct dsa_switch *ds, int port); 985 int (*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid); 986 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port, 987 struct switchdev_brport_flags flags, 988 struct netlink_ext_ack *extack); 989 int (*port_bridge_flags)(struct dsa_switch *ds, int port, 990 struct switchdev_brport_flags flags, 991 struct netlink_ext_ack *extack); 992 void (*port_set_host_flood)(struct dsa_switch *ds, int port, 993 bool uc, bool mc); 994 995 /* 996 * VLAN support 997 */ 998 int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 999 bool vlan_filtering, 1000 struct netlink_ext_ack *extack); 1001 int (*port_vlan_add)(struct dsa_switch *ds, int port, 1002 const struct switchdev_obj_port_vlan *vlan, 1003 struct netlink_ext_ack *extack); 1004 int (*port_vlan_del)(struct dsa_switch *ds, int port, 1005 const struct switchdev_obj_port_vlan *vlan); 1006 int (*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge, 1007 const struct switchdev_vlan_msti *msti); 1008 1009 /* 1010 * Forwarding database 1011 */ 1012 int (*port_fdb_add)(struct dsa_switch *ds, int port, 1013 const unsigned char *addr, u16 vid, 1014 struct dsa_db db); 1015 int (*port_fdb_del)(struct dsa_switch *ds, int port, 1016 const unsigned char *addr, u16 vid, 1017 struct dsa_db db); 1018 int (*port_fdb_dump)(struct dsa_switch *ds, int port, 1019 dsa_fdb_dump_cb_t *cb, void *data); 1020 int (*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag, 1021 const unsigned char *addr, u16 vid, 1022 struct dsa_db db); 1023 int (*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag, 1024 const unsigned char *addr, u16 vid, 1025 struct dsa_db db); 1026 1027 /* 1028 * Multicast database 1029 */ 1030 int (*port_mdb_add)(struct dsa_switch *ds, int port, 1031 const struct switchdev_obj_port_mdb *mdb, 1032 struct dsa_db db); 1033 int (*port_mdb_del)(struct dsa_switch *ds, int port, 1034 const struct switchdev_obj_port_mdb *mdb, 1035 struct dsa_db db); 1036 /* 1037 * RXNFC 1038 */ 1039 int (*get_rxnfc)(struct dsa_switch *ds, int port, 1040 struct ethtool_rxnfc *nfc, u32 *rule_locs); 1041 int (*set_rxnfc)(struct dsa_switch *ds, int port, 1042 struct ethtool_rxnfc *nfc); 1043 1044 /* 1045 * TC integration 1046 */ 1047 int (*cls_flower_add)(struct dsa_switch *ds, int port, 1048 struct flow_cls_offload *cls, bool ingress); 1049 int (*cls_flower_del)(struct dsa_switch *ds, int port, 1050 struct flow_cls_offload *cls, bool ingress); 1051 int (*cls_flower_stats)(struct dsa_switch *ds, int port, 1052 struct flow_cls_offload *cls, bool ingress); 1053 int (*port_mirror_add)(struct dsa_switch *ds, int port, 1054 struct dsa_mall_mirror_tc_entry *mirror, 1055 bool ingress, struct netlink_ext_ack *extack); 1056 void (*port_mirror_del)(struct dsa_switch *ds, int port, 1057 struct dsa_mall_mirror_tc_entry *mirror); 1058 int (*port_policer_add)(struct dsa_switch *ds, int port, 1059 struct dsa_mall_policer_tc_entry *policer); 1060 void (*port_policer_del)(struct dsa_switch *ds, int port); 1061 int (*port_setup_tc)(struct dsa_switch *ds, int port, 1062 enum tc_setup_type type, void *type_data); 1063 1064 /* 1065 * Cross-chip operations 1066 */ 1067 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 1068 int sw_index, int port, 1069 struct dsa_bridge bridge, 1070 struct netlink_ext_ack *extack); 1071 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 1072 int sw_index, int port, 1073 struct dsa_bridge bridge); 1074 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 1075 int port); 1076 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 1077 int port, struct dsa_lag lag, 1078 struct netdev_lag_upper_info *info); 1079 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 1080 int port, struct dsa_lag lag); 1081 1082 /* 1083 * PTP functionality 1084 */ 1085 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 1086 struct ifreq *ifr); 1087 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 1088 struct ifreq *ifr); 1089 void (*port_txtstamp)(struct dsa_switch *ds, int port, 1090 struct sk_buff *skb); 1091 bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 1092 struct sk_buff *skb, unsigned int type); 1093 1094 /* Devlink parameters, etc */ 1095 int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 1096 struct devlink_param_gset_ctx *ctx); 1097 int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 1098 struct devlink_param_gset_ctx *ctx); 1099 int (*devlink_info_get)(struct dsa_switch *ds, 1100 struct devlink_info_req *req, 1101 struct netlink_ext_ack *extack); 1102 int (*devlink_sb_pool_get)(struct dsa_switch *ds, 1103 unsigned int sb_index, u16 pool_index, 1104 struct devlink_sb_pool_info *pool_info); 1105 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 1106 u16 pool_index, u32 size, 1107 enum devlink_sb_threshold_type threshold_type, 1108 struct netlink_ext_ack *extack); 1109 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 1110 unsigned int sb_index, u16 pool_index, 1111 u32 *p_threshold); 1112 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 1113 unsigned int sb_index, u16 pool_index, 1114 u32 threshold, 1115 struct netlink_ext_ack *extack); 1116 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 1117 unsigned int sb_index, u16 tc_index, 1118 enum devlink_sb_pool_type pool_type, 1119 u16 *p_pool_index, u32 *p_threshold); 1120 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 1121 unsigned int sb_index, u16 tc_index, 1122 enum devlink_sb_pool_type pool_type, 1123 u16 pool_index, u32 threshold, 1124 struct netlink_ext_ack *extack); 1125 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 1126 unsigned int sb_index); 1127 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 1128 unsigned int sb_index); 1129 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 1130 unsigned int sb_index, u16 pool_index, 1131 u32 *p_cur, u32 *p_max); 1132 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 1133 unsigned int sb_index, u16 tc_index, 1134 enum devlink_sb_pool_type pool_type, 1135 u32 *p_cur, u32 *p_max); 1136 1137 /* 1138 * MTU change functionality. Switches can also adjust their MRU through 1139 * this method. By MTU, one understands the SDU (L2 payload) length. 1140 * If the switch needs to account for the DSA tag on the CPU port, this 1141 * method needs to do so privately. 1142 */ 1143 int (*port_change_mtu)(struct dsa_switch *ds, int port, 1144 int new_mtu); 1145 int (*port_max_mtu)(struct dsa_switch *ds, int port); 1146 1147 /* 1148 * LAG integration 1149 */ 1150 int (*port_lag_change)(struct dsa_switch *ds, int port); 1151 int (*port_lag_join)(struct dsa_switch *ds, int port, 1152 struct dsa_lag lag, 1153 struct netdev_lag_upper_info *info); 1154 int (*port_lag_leave)(struct dsa_switch *ds, int port, 1155 struct dsa_lag lag); 1156 1157 /* 1158 * HSR integration 1159 */ 1160 int (*port_hsr_join)(struct dsa_switch *ds, int port, 1161 struct net_device *hsr); 1162 int (*port_hsr_leave)(struct dsa_switch *ds, int port, 1163 struct net_device *hsr); 1164 1165 /* 1166 * MRP integration 1167 */ 1168 int (*port_mrp_add)(struct dsa_switch *ds, int port, 1169 const struct switchdev_obj_mrp *mrp); 1170 int (*port_mrp_del)(struct dsa_switch *ds, int port, 1171 const struct switchdev_obj_mrp *mrp); 1172 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port, 1173 const struct switchdev_obj_ring_role_mrp *mrp); 1174 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port, 1175 const struct switchdev_obj_ring_role_mrp *mrp); 1176 1177 /* 1178 * tag_8021q operations 1179 */ 1180 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid, 1181 u16 flags); 1182 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid); 1183 1184 /* 1185 * DSA master tracking operations 1186 */ 1187 void (*master_state_change)(struct dsa_switch *ds, 1188 const struct net_device *master, 1189 bool operational); 1190 }; 1191 1192 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 1193 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 1194 dsa_devlink_param_get, dsa_devlink_param_set, NULL) 1195 1196 int dsa_devlink_param_get(struct devlink *dl, u32 id, 1197 struct devlink_param_gset_ctx *ctx); 1198 int dsa_devlink_param_set(struct devlink *dl, u32 id, 1199 struct devlink_param_gset_ctx *ctx); 1200 int dsa_devlink_params_register(struct dsa_switch *ds, 1201 const struct devlink_param *params, 1202 size_t params_count); 1203 void dsa_devlink_params_unregister(struct dsa_switch *ds, 1204 const struct devlink_param *params, 1205 size_t params_count); 1206 int dsa_devlink_resource_register(struct dsa_switch *ds, 1207 const char *resource_name, 1208 u64 resource_size, 1209 u64 resource_id, 1210 u64 parent_resource_id, 1211 const struct devlink_resource_size_params *size_params); 1212 1213 void dsa_devlink_resources_unregister(struct dsa_switch *ds); 1214 1215 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 1216 u64 resource_id, 1217 devlink_resource_occ_get_t *occ_get, 1218 void *occ_get_priv); 1219 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 1220 u64 resource_id); 1221 struct devlink_region * 1222 dsa_devlink_region_create(struct dsa_switch *ds, 1223 const struct devlink_region_ops *ops, 1224 u32 region_max_snapshots, u64 region_size); 1225 struct devlink_region * 1226 dsa_devlink_port_region_create(struct dsa_switch *ds, 1227 int port, 1228 const struct devlink_port_region_ops *ops, 1229 u32 region_max_snapshots, u64 region_size); 1230 void dsa_devlink_region_destroy(struct devlink_region *region); 1231 1232 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 1233 1234 struct dsa_devlink_priv { 1235 struct dsa_switch *ds; 1236 }; 1237 1238 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 1239 { 1240 struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1241 1242 return dl_priv->ds; 1243 } 1244 1245 static inline 1246 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 1247 { 1248 struct devlink *dl = port->devlink; 1249 struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1250 1251 return dl_priv->ds; 1252 } 1253 1254 static inline int dsa_devlink_port_to_port(struct devlink_port *port) 1255 { 1256 return port->index; 1257 } 1258 1259 struct dsa_switch_driver { 1260 struct list_head list; 1261 const struct dsa_switch_ops *ops; 1262 }; 1263 1264 struct net_device *dsa_dev_to_net_device(struct device *dev); 1265 1266 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port, 1267 const unsigned char *addr, u16 vid, 1268 struct dsa_db db); 1269 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port, 1270 const struct switchdev_obj_port_mdb *mdb, 1271 struct dsa_db db); 1272 1273 /* Keep inline for faster access in hot path */ 1274 static inline bool netdev_uses_dsa(const struct net_device *dev) 1275 { 1276 #if IS_ENABLED(CONFIG_NET_DSA) 1277 return dev->dsa_ptr && dev->dsa_ptr->rcv; 1278 #endif 1279 return false; 1280 } 1281 1282 /* All DSA tags that push the EtherType to the right (basically all except tail 1283 * tags, which don't break dissection) can be treated the same from the 1284 * perspective of the flow dissector. 1285 * 1286 * We need to return: 1287 * - offset: the (B - A) difference between: 1288 * A. the position of the real EtherType and 1289 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 1290 * after the normal EtherType was supposed to be) 1291 * The offset in bytes is exactly equal to the tagger overhead (and half of 1292 * that, in __be16 shorts). 1293 * 1294 * - proto: the value of the real EtherType. 1295 */ 1296 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 1297 __be16 *proto, int *offset) 1298 { 1299 #if IS_ENABLED(CONFIG_NET_DSA) 1300 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 1301 int tag_len = ops->needed_headroom; 1302 1303 *offset = tag_len; 1304 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 1305 #endif 1306 } 1307 1308 #if IS_ENABLED(CONFIG_NET_DSA) 1309 static inline int __dsa_netdevice_ops_check(struct net_device *dev) 1310 { 1311 int err = -EOPNOTSUPP; 1312 1313 if (!dev->dsa_ptr) 1314 return err; 1315 1316 if (!dev->dsa_ptr->netdev_ops) 1317 return err; 1318 1319 return 0; 1320 } 1321 1322 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 1323 int cmd) 1324 { 1325 const struct dsa_netdevice_ops *ops; 1326 int err; 1327 1328 err = __dsa_netdevice_ops_check(dev); 1329 if (err) 1330 return err; 1331 1332 ops = dev->dsa_ptr->netdev_ops; 1333 1334 return ops->ndo_eth_ioctl(dev, ifr, cmd); 1335 } 1336 #else 1337 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 1338 int cmd) 1339 { 1340 return -EOPNOTSUPP; 1341 } 1342 #endif 1343 1344 void dsa_unregister_switch(struct dsa_switch *ds); 1345 int dsa_register_switch(struct dsa_switch *ds); 1346 void dsa_switch_shutdown(struct dsa_switch *ds); 1347 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1348 void dsa_flush_workqueue(void); 1349 #ifdef CONFIG_PM_SLEEP 1350 int dsa_switch_suspend(struct dsa_switch *ds); 1351 int dsa_switch_resume(struct dsa_switch *ds); 1352 #else 1353 static inline int dsa_switch_suspend(struct dsa_switch *ds) 1354 { 1355 return 0; 1356 } 1357 static inline int dsa_switch_resume(struct dsa_switch *ds) 1358 { 1359 return 0; 1360 } 1361 #endif /* CONFIG_PM_SLEEP */ 1362 1363 #if IS_ENABLED(CONFIG_NET_DSA) 1364 bool dsa_slave_dev_check(const struct net_device *dev); 1365 #else 1366 static inline bool dsa_slave_dev_check(const struct net_device *dev) 1367 { 1368 return false; 1369 } 1370 #endif 1371 1372 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 1373 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 1374 1375 struct dsa_tag_driver { 1376 const struct dsa_device_ops *ops; 1377 struct list_head list; 1378 struct module *owner; 1379 }; 1380 1381 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 1382 unsigned int count, 1383 struct module *owner); 1384 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 1385 unsigned int count); 1386 1387 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 1388 static int __init dsa_tag_driver_module_init(void) \ 1389 { \ 1390 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 1391 THIS_MODULE); \ 1392 return 0; \ 1393 } \ 1394 module_init(dsa_tag_driver_module_init); \ 1395 \ 1396 static void __exit dsa_tag_driver_module_exit(void) \ 1397 { \ 1398 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 1399 } \ 1400 module_exit(dsa_tag_driver_module_exit) 1401 1402 /** 1403 * module_dsa_tag_drivers() - Helper macro for registering DSA tag 1404 * drivers 1405 * @__ops_array: Array of tag driver structures 1406 * 1407 * Helper macro for DSA tag drivers which do not do anything special 1408 * in module init/exit. Each module may only use this macro once, and 1409 * calling it replaces module_init() and module_exit(). 1410 */ 1411 #define module_dsa_tag_drivers(__ops_array) \ 1412 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 1413 1414 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1415 1416 /* Create a static structure we can build a linked list of dsa_tag 1417 * drivers 1418 */ 1419 #define DSA_TAG_DRIVER(__ops) \ 1420 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1421 .ops = &__ops, \ 1422 } 1423 1424 /** 1425 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1426 * driver 1427 * @__ops: Single tag driver structures 1428 * 1429 * Helper macro for DSA tag drivers which do not do anything special 1430 * in module init/exit. Each module may only use this macro once, and 1431 * calling it replaces module_init() and module_exit(). 1432 */ 1433 #define module_dsa_tag_driver(__ops) \ 1434 DSA_TAG_DRIVER(__ops); \ 1435 \ 1436 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1437 &DSA_TAG_DRIVER_NAME(__ops) \ 1438 }; \ 1439 module_dsa_tag_drivers(dsa_tag_driver_array) 1440 #endif 1441 1442