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 48 enum dsa_tag_protocol { 49 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE, 50 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE, 51 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE, 52 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE, 53 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE, 54 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE, 55 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE, 56 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE, 57 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE, 58 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE, 59 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE, 60 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE, 61 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE, 62 DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE, 63 DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE, 64 DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE, 65 DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE, 66 }; 67 68 struct packet_type; 69 struct dsa_switch; 70 71 struct dsa_device_ops { 72 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev); 73 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev, 74 struct packet_type *pt); 75 int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto, 76 int *offset); 77 /* Used to determine which traffic should match the DSA filter in 78 * eth_type_trans, and which, if any, should bypass it and be processed 79 * as regular on the master net device. 80 */ 81 bool (*filter)(const struct sk_buff *skb, struct net_device *dev); 82 unsigned int overhead; 83 const char *name; 84 enum dsa_tag_protocol proto; 85 }; 86 87 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-" 88 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \ 89 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE)) 90 91 struct dsa_skb_cb { 92 struct sk_buff *clone; 93 }; 94 95 struct __dsa_skb_cb { 96 struct dsa_skb_cb cb; 97 u8 priv[48 - sizeof(struct dsa_skb_cb)]; 98 }; 99 100 #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb)) 101 102 #define DSA_SKB_CB_PRIV(skb) \ 103 ((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv)) 104 105 struct dsa_switch_tree { 106 struct list_head list; 107 108 /* Notifier chain for switch-wide events */ 109 struct raw_notifier_head nh; 110 111 /* Tree identifier */ 112 unsigned int index; 113 114 /* Number of switches attached to this tree */ 115 struct kref refcount; 116 117 /* Has this tree been applied to the hardware? */ 118 bool setup; 119 120 /* 121 * Configuration data for the platform device that owns 122 * this dsa switch tree instance. 123 */ 124 struct dsa_platform_data *pd; 125 126 /* List of switch ports */ 127 struct list_head ports; 128 129 /* List of DSA links composing the routing table */ 130 struct list_head rtable; 131 }; 132 133 /* TC matchall action types, only mirroring for now */ 134 enum dsa_port_mall_action_type { 135 DSA_PORT_MALL_MIRROR, 136 }; 137 138 /* TC mirroring entry */ 139 struct dsa_mall_mirror_tc_entry { 140 u8 to_local_port; 141 bool ingress; 142 }; 143 144 /* TC matchall entry */ 145 struct dsa_mall_tc_entry { 146 struct list_head list; 147 unsigned long cookie; 148 enum dsa_port_mall_action_type type; 149 union { 150 struct dsa_mall_mirror_tc_entry mirror; 151 }; 152 }; 153 154 155 struct dsa_port { 156 /* A CPU port is physically connected to a master device. 157 * A user port exposed to userspace has a slave device. 158 */ 159 union { 160 struct net_device *master; 161 struct net_device *slave; 162 }; 163 164 /* CPU port tagging operations used by master or slave devices */ 165 const struct dsa_device_ops *tag_ops; 166 167 /* Copies for faster access in master receive hot path */ 168 struct dsa_switch_tree *dst; 169 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev, 170 struct packet_type *pt); 171 bool (*filter)(const struct sk_buff *skb, struct net_device *dev); 172 173 enum { 174 DSA_PORT_TYPE_UNUSED = 0, 175 DSA_PORT_TYPE_CPU, 176 DSA_PORT_TYPE_DSA, 177 DSA_PORT_TYPE_USER, 178 } type; 179 180 struct dsa_switch *ds; 181 unsigned int index; 182 const char *name; 183 struct dsa_port *cpu_dp; 184 const char *mac; 185 struct device_node *dn; 186 unsigned int ageing_time; 187 bool vlan_filtering; 188 u8 stp_state; 189 struct net_device *bridge_dev; 190 struct devlink_port devlink_port; 191 struct phylink *pl; 192 struct phylink_config pl_config; 193 194 struct list_head list; 195 196 /* 197 * Give the switch driver somewhere to hang its per-port private data 198 * structures (accessible from the tagger). 199 */ 200 void *priv; 201 202 /* 203 * Original copy of the master netdev ethtool_ops 204 */ 205 const struct ethtool_ops *orig_ethtool_ops; 206 207 /* 208 * Original copy of the master netdev net_device_ops 209 */ 210 const struct net_device_ops *orig_ndo_ops; 211 212 bool setup; 213 }; 214 215 /* TODO: ideally DSA ports would have a single dp->link_dp member, 216 * and no dst->rtable nor this struct dsa_link would be needed, 217 * but this would require some more complex tree walking, 218 * so keep it stupid at the moment and list them all. 219 */ 220 struct dsa_link { 221 struct dsa_port *dp; 222 struct dsa_port *link_dp; 223 struct list_head list; 224 }; 225 226 struct dsa_switch { 227 bool setup; 228 229 struct device *dev; 230 231 /* 232 * Parent switch tree, and switch index. 233 */ 234 struct dsa_switch_tree *dst; 235 unsigned int index; 236 237 /* Listener for switch fabric events */ 238 struct notifier_block nb; 239 240 /* 241 * Give the switch driver somewhere to hang its private data 242 * structure. 243 */ 244 void *priv; 245 246 /* 247 * Configuration data for this switch. 248 */ 249 struct dsa_chip_data *cd; 250 251 /* 252 * The switch operations. 253 */ 254 const struct dsa_switch_ops *ops; 255 256 /* 257 * Slave mii_bus and devices for the individual ports. 258 */ 259 u32 phys_mii_mask; 260 struct mii_bus *slave_mii_bus; 261 262 /* Ageing Time limits in msecs */ 263 unsigned int ageing_time_min; 264 unsigned int ageing_time_max; 265 266 /* devlink used to represent this switch device */ 267 struct devlink *devlink; 268 269 /* Number of switch port queues */ 270 unsigned int num_tx_queues; 271 272 /* Disallow bridge core from requesting different VLAN awareness 273 * settings on ports if not hardware-supported 274 */ 275 bool vlan_filtering_is_global; 276 277 /* In case vlan_filtering_is_global is set, the VLAN awareness state 278 * should be retrieved from here and not from the per-port settings. 279 */ 280 bool vlan_filtering; 281 282 /* MAC PCS does not provide link state change interrupt, and requires 283 * polling. Flag passed on to PHYLINK. 284 */ 285 bool pcs_poll; 286 287 /* For switches that only have the MRU configurable. To ensure the 288 * configured MTU is not exceeded, normalization of MRU on all bridged 289 * interfaces is needed. 290 */ 291 bool mtu_enforcement_ingress; 292 293 size_t num_ports; 294 }; 295 296 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 297 { 298 struct dsa_switch_tree *dst = ds->dst; 299 struct dsa_port *dp; 300 301 list_for_each_entry(dp, &dst->ports, list) 302 if (dp->ds == ds && dp->index == p) 303 return dp; 304 305 return NULL; 306 } 307 308 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 309 { 310 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 311 } 312 313 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 314 { 315 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 316 } 317 318 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 319 { 320 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 321 } 322 323 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 324 { 325 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 326 } 327 328 static inline u32 dsa_user_ports(struct dsa_switch *ds) 329 { 330 u32 mask = 0; 331 int p; 332 333 for (p = 0; p < ds->num_ports; p++) 334 if (dsa_is_user_port(ds, p)) 335 mask |= BIT(p); 336 337 return mask; 338 } 339 340 /* Return the local port used to reach an arbitrary switch device */ 341 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 342 { 343 struct dsa_switch_tree *dst = ds->dst; 344 struct dsa_link *dl; 345 346 list_for_each_entry(dl, &dst->rtable, list) 347 if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 348 return dl->dp->index; 349 350 return ds->num_ports; 351 } 352 353 /* Return the local port used to reach an arbitrary switch port */ 354 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 355 int port) 356 { 357 if (device == ds->index) 358 return port; 359 else 360 return dsa_routing_port(ds, device); 361 } 362 363 /* Return the local port used to reach the dedicated CPU port */ 364 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 365 { 366 const struct dsa_port *dp = dsa_to_port(ds, port); 367 const struct dsa_port *cpu_dp = dp->cpu_dp; 368 369 if (!cpu_dp) 370 return port; 371 372 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 373 } 374 375 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 376 { 377 const struct dsa_switch *ds = dp->ds; 378 379 if (ds->vlan_filtering_is_global) 380 return ds->vlan_filtering; 381 else 382 return dp->vlan_filtering; 383 } 384 385 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 386 bool is_static, void *data); 387 struct dsa_switch_ops { 388 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 389 int port, 390 enum dsa_tag_protocol mprot); 391 392 int (*setup)(struct dsa_switch *ds); 393 void (*teardown)(struct dsa_switch *ds); 394 u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 395 396 /* 397 * Access to the switch's PHY registers. 398 */ 399 int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 400 int (*phy_write)(struct dsa_switch *ds, int port, 401 int regnum, u16 val); 402 403 /* 404 * Link state adjustment (called from libphy) 405 */ 406 void (*adjust_link)(struct dsa_switch *ds, int port, 407 struct phy_device *phydev); 408 void (*fixed_link_update)(struct dsa_switch *ds, int port, 409 struct fixed_phy_status *st); 410 411 /* 412 * PHYLINK integration 413 */ 414 void (*phylink_validate)(struct dsa_switch *ds, int port, 415 unsigned long *supported, 416 struct phylink_link_state *state); 417 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 418 struct phylink_link_state *state); 419 void (*phylink_mac_config)(struct dsa_switch *ds, int port, 420 unsigned int mode, 421 const struct phylink_link_state *state); 422 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 423 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 424 unsigned int mode, 425 phy_interface_t interface); 426 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 427 unsigned int mode, 428 phy_interface_t interface, 429 struct phy_device *phydev, 430 int speed, int duplex, 431 bool tx_pause, bool rx_pause); 432 void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 433 struct phylink_link_state *state); 434 /* 435 * ethtool hardware statistics. 436 */ 437 void (*get_strings)(struct dsa_switch *ds, int port, 438 u32 stringset, uint8_t *data); 439 void (*get_ethtool_stats)(struct dsa_switch *ds, 440 int port, uint64_t *data); 441 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 442 void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 443 int port, uint64_t *data); 444 445 /* 446 * ethtool Wake-on-LAN 447 */ 448 void (*get_wol)(struct dsa_switch *ds, int port, 449 struct ethtool_wolinfo *w); 450 int (*set_wol)(struct dsa_switch *ds, int port, 451 struct ethtool_wolinfo *w); 452 453 /* 454 * ethtool timestamp info 455 */ 456 int (*get_ts_info)(struct dsa_switch *ds, int port, 457 struct ethtool_ts_info *ts); 458 459 /* 460 * Suspend and resume 461 */ 462 int (*suspend)(struct dsa_switch *ds); 463 int (*resume)(struct dsa_switch *ds); 464 465 /* 466 * Port enable/disable 467 */ 468 int (*port_enable)(struct dsa_switch *ds, int port, 469 struct phy_device *phy); 470 void (*port_disable)(struct dsa_switch *ds, int port); 471 472 /* 473 * Port's MAC EEE settings 474 */ 475 int (*set_mac_eee)(struct dsa_switch *ds, int port, 476 struct ethtool_eee *e); 477 int (*get_mac_eee)(struct dsa_switch *ds, int port, 478 struct ethtool_eee *e); 479 480 /* EEPROM access */ 481 int (*get_eeprom_len)(struct dsa_switch *ds); 482 int (*get_eeprom)(struct dsa_switch *ds, 483 struct ethtool_eeprom *eeprom, u8 *data); 484 int (*set_eeprom)(struct dsa_switch *ds, 485 struct ethtool_eeprom *eeprom, u8 *data); 486 487 /* 488 * Register access. 489 */ 490 int (*get_regs_len)(struct dsa_switch *ds, int port); 491 void (*get_regs)(struct dsa_switch *ds, int port, 492 struct ethtool_regs *regs, void *p); 493 494 /* 495 * Bridge integration 496 */ 497 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 498 int (*port_bridge_join)(struct dsa_switch *ds, int port, 499 struct net_device *bridge); 500 void (*port_bridge_leave)(struct dsa_switch *ds, int port, 501 struct net_device *bridge); 502 void (*port_stp_state_set)(struct dsa_switch *ds, int port, 503 u8 state); 504 void (*port_fast_age)(struct dsa_switch *ds, int port); 505 int (*port_egress_floods)(struct dsa_switch *ds, int port, 506 bool unicast, bool multicast); 507 508 /* 509 * VLAN support 510 */ 511 int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 512 bool vlan_filtering); 513 int (*port_vlan_prepare)(struct dsa_switch *ds, int port, 514 const struct switchdev_obj_port_vlan *vlan); 515 void (*port_vlan_add)(struct dsa_switch *ds, int port, 516 const struct switchdev_obj_port_vlan *vlan); 517 int (*port_vlan_del)(struct dsa_switch *ds, int port, 518 const struct switchdev_obj_port_vlan *vlan); 519 /* 520 * Forwarding database 521 */ 522 int (*port_fdb_add)(struct dsa_switch *ds, int port, 523 const unsigned char *addr, u16 vid); 524 int (*port_fdb_del)(struct dsa_switch *ds, int port, 525 const unsigned char *addr, u16 vid); 526 int (*port_fdb_dump)(struct dsa_switch *ds, int port, 527 dsa_fdb_dump_cb_t *cb, void *data); 528 529 /* 530 * Multicast database 531 */ 532 int (*port_mdb_prepare)(struct dsa_switch *ds, int port, 533 const struct switchdev_obj_port_mdb *mdb); 534 void (*port_mdb_add)(struct dsa_switch *ds, int port, 535 const struct switchdev_obj_port_mdb *mdb); 536 int (*port_mdb_del)(struct dsa_switch *ds, int port, 537 const struct switchdev_obj_port_mdb *mdb); 538 /* 539 * RXNFC 540 */ 541 int (*get_rxnfc)(struct dsa_switch *ds, int port, 542 struct ethtool_rxnfc *nfc, u32 *rule_locs); 543 int (*set_rxnfc)(struct dsa_switch *ds, int port, 544 struct ethtool_rxnfc *nfc); 545 546 /* 547 * TC integration 548 */ 549 int (*cls_flower_add)(struct dsa_switch *ds, int port, 550 struct flow_cls_offload *cls, bool ingress); 551 int (*cls_flower_del)(struct dsa_switch *ds, int port, 552 struct flow_cls_offload *cls, bool ingress); 553 int (*cls_flower_stats)(struct dsa_switch *ds, int port, 554 struct flow_cls_offload *cls, bool ingress); 555 int (*port_mirror_add)(struct dsa_switch *ds, int port, 556 struct dsa_mall_mirror_tc_entry *mirror, 557 bool ingress); 558 void (*port_mirror_del)(struct dsa_switch *ds, int port, 559 struct dsa_mall_mirror_tc_entry *mirror); 560 int (*port_setup_tc)(struct dsa_switch *ds, int port, 561 enum tc_setup_type type, void *type_data); 562 563 /* 564 * Cross-chip operations 565 */ 566 int (*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index, 567 int port, struct net_device *br); 568 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index, 569 int port, struct net_device *br); 570 571 /* 572 * PTP functionality 573 */ 574 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 575 struct ifreq *ifr); 576 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 577 struct ifreq *ifr); 578 bool (*port_txtstamp)(struct dsa_switch *ds, int port, 579 struct sk_buff *clone, unsigned int type); 580 bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 581 struct sk_buff *skb, unsigned int type); 582 583 /* Devlink parameters */ 584 int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 585 struct devlink_param_gset_ctx *ctx); 586 int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 587 struct devlink_param_gset_ctx *ctx); 588 589 /* 590 * MTU change functionality. Switches can also adjust their MRU through 591 * this method. By MTU, one understands the SDU (L2 payload) length. 592 * If the switch needs to account for the DSA tag on the CPU port, this 593 * method needs to to do so privately. 594 */ 595 int (*port_change_mtu)(struct dsa_switch *ds, int port, 596 int new_mtu); 597 int (*port_max_mtu)(struct dsa_switch *ds, int port); 598 }; 599 600 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 601 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 602 dsa_devlink_param_get, dsa_devlink_param_set, NULL) 603 604 int dsa_devlink_param_get(struct devlink *dl, u32 id, 605 struct devlink_param_gset_ctx *ctx); 606 int dsa_devlink_param_set(struct devlink *dl, u32 id, 607 struct devlink_param_gset_ctx *ctx); 608 int dsa_devlink_params_register(struct dsa_switch *ds, 609 const struct devlink_param *params, 610 size_t params_count); 611 void dsa_devlink_params_unregister(struct dsa_switch *ds, 612 const struct devlink_param *params, 613 size_t params_count); 614 int dsa_devlink_resource_register(struct dsa_switch *ds, 615 const char *resource_name, 616 u64 resource_size, 617 u64 resource_id, 618 u64 parent_resource_id, 619 const struct devlink_resource_size_params *size_params); 620 621 void dsa_devlink_resources_unregister(struct dsa_switch *ds); 622 623 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 624 u64 resource_id, 625 devlink_resource_occ_get_t *occ_get, 626 void *occ_get_priv); 627 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 628 u64 resource_id); 629 630 struct dsa_devlink_priv { 631 struct dsa_switch *ds; 632 }; 633 634 struct dsa_switch_driver { 635 struct list_head list; 636 const struct dsa_switch_ops *ops; 637 }; 638 639 struct net_device *dsa_dev_to_net_device(struct device *dev); 640 641 /* Keep inline for faster access in hot path */ 642 static inline bool netdev_uses_dsa(struct net_device *dev) 643 { 644 #if IS_ENABLED(CONFIG_NET_DSA) 645 return dev->dsa_ptr && dev->dsa_ptr->rcv; 646 #endif 647 return false; 648 } 649 650 static inline bool dsa_can_decode(const struct sk_buff *skb, 651 struct net_device *dev) 652 { 653 #if IS_ENABLED(CONFIG_NET_DSA) 654 return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev); 655 #endif 656 return false; 657 } 658 659 void dsa_unregister_switch(struct dsa_switch *ds); 660 int dsa_register_switch(struct dsa_switch *ds); 661 #ifdef CONFIG_PM_SLEEP 662 int dsa_switch_suspend(struct dsa_switch *ds); 663 int dsa_switch_resume(struct dsa_switch *ds); 664 #else 665 static inline int dsa_switch_suspend(struct dsa_switch *ds) 666 { 667 return 0; 668 } 669 static inline int dsa_switch_resume(struct dsa_switch *ds) 670 { 671 return 0; 672 } 673 #endif /* CONFIG_PM_SLEEP */ 674 675 enum dsa_notifier_type { 676 DSA_PORT_REGISTER, 677 DSA_PORT_UNREGISTER, 678 }; 679 680 struct dsa_notifier_info { 681 struct net_device *dev; 682 }; 683 684 struct dsa_notifier_register_info { 685 struct dsa_notifier_info info; /* must be first */ 686 struct net_device *master; 687 unsigned int port_number; 688 unsigned int switch_number; 689 }; 690 691 static inline struct net_device * 692 dsa_notifier_info_to_dev(const struct dsa_notifier_info *info) 693 { 694 return info->dev; 695 } 696 697 #if IS_ENABLED(CONFIG_NET_DSA) 698 int register_dsa_notifier(struct notifier_block *nb); 699 int unregister_dsa_notifier(struct notifier_block *nb); 700 int call_dsa_notifiers(unsigned long val, struct net_device *dev, 701 struct dsa_notifier_info *info); 702 #else 703 static inline int register_dsa_notifier(struct notifier_block *nb) 704 { 705 return 0; 706 } 707 708 static inline int unregister_dsa_notifier(struct notifier_block *nb) 709 { 710 return 0; 711 } 712 713 static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev, 714 struct dsa_notifier_info *info) 715 { 716 return NOTIFY_DONE; 717 } 718 #endif 719 720 /* Broadcom tag specific helpers to insert and extract queue/port number */ 721 #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q) 722 #define BRCM_TAG_GET_PORT(v) ((v) >> 8) 723 #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff) 724 725 726 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 727 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 728 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 729 int dsa_port_get_phy_sset_count(struct dsa_port *dp); 730 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 731 732 struct dsa_tag_driver { 733 const struct dsa_device_ops *ops; 734 struct list_head list; 735 struct module *owner; 736 }; 737 738 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 739 unsigned int count, 740 struct module *owner); 741 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 742 unsigned int count); 743 744 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 745 static int __init dsa_tag_driver_module_init(void) \ 746 { \ 747 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 748 THIS_MODULE); \ 749 return 0; \ 750 } \ 751 module_init(dsa_tag_driver_module_init); \ 752 \ 753 static void __exit dsa_tag_driver_module_exit(void) \ 754 { \ 755 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 756 } \ 757 module_exit(dsa_tag_driver_module_exit) 758 759 /** 760 * module_dsa_tag_drivers() - Helper macro for registering DSA tag 761 * drivers 762 * @__ops_array: Array of tag driver strucutres 763 * 764 * Helper macro for DSA tag drivers which do not do anything special 765 * in module init/exit. Each module may only use this macro once, and 766 * calling it replaces module_init() and module_exit(). 767 */ 768 #define module_dsa_tag_drivers(__ops_array) \ 769 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 770 771 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 772 773 /* Create a static structure we can build a linked list of dsa_tag 774 * drivers 775 */ 776 #define DSA_TAG_DRIVER(__ops) \ 777 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 778 .ops = &__ops, \ 779 } 780 781 /** 782 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 783 * driver 784 * @__ops: Single tag driver structures 785 * 786 * Helper macro for DSA tag drivers which do not do anything special 787 * in module init/exit. Each module may only use this macro once, and 788 * calling it replaces module_init() and module_exit(). 789 */ 790 #define module_dsa_tag_driver(__ops) \ 791 DSA_TAG_DRIVER(__ops); \ 792 \ 793 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 794 &DSA_TAG_DRIVER_NAME(__ops) \ 795 }; \ 796 module_dsa_tag_drivers(dsa_tag_driver_array) 797 #endif 798 799