156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0 23c9cfb52SVladimir Oltean /* Copyright 2019-2021 NXP 3375e1314SVladimir Oltean * 4375e1314SVladimir Oltean * This is an umbrella module for all network switches that are 5375e1314SVladimir Oltean * register-compatible with Ocelot and that perform I/O to their host CPU 6375e1314SVladimir Oltean * through an NPI (Node Processor Interface) Ethernet port. 756051948SVladimir Oltean */ 856051948SVladimir Oltean #include <uapi/linux/if_bridge.h> 907d985eeSVladimir Oltean #include <soc/mscc/ocelot_vcap.h> 10bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h> 11bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h> 12bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h> 13bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h> 142b49d128SYangbo Lu #include <soc/mscc/ocelot_ptp.h> 1556051948SVladimir Oltean #include <soc/mscc/ocelot.h> 16e21268efSVladimir Oltean #include <linux/dsa/8021q.h> 1740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h> 1884705fc1SMaxim Kochetkov #include <linux/platform_device.h> 190a6f17c6SVladimir Oltean #include <linux/ptp_classify.h> 2056051948SVladimir Oltean #include <linux/module.h> 21bdeced75SVladimir Oltean #include <linux/of_net.h> 2256051948SVladimir Oltean #include <linux/pci.h> 2356051948SVladimir Oltean #include <linux/of.h> 24fc411eaaSVladimir Oltean #include <net/pkt_sched.h> 2556051948SVladimir Oltean #include <net/dsa.h> 2656051948SVladimir Oltean #include "felix.h" 2756051948SVladimir Oltean 2804b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that 2904b67e18SVladimir Oltean * the tagger can perform RX source port identification. 3004b67e18SVladimir Oltean */ 3104b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct felix *felix, int port, u16 vid) 32e21268efSVladimir Oltean { 33e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 34e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 35e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 36e21268efSVladimir Oltean int key_length, upstream, err; 37e21268efSVladimir Oltean 38e21268efSVladimir Oltean key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length; 39e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 40e21268efSVladimir Oltean 41e21268efSVladimir Oltean outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), 42e21268efSVladimir Oltean GFP_KERNEL); 43e21268efSVladimir Oltean if (!outer_tagging_rule) 44e21268efSVladimir Oltean return -ENOMEM; 45e21268efSVladimir Oltean 46e21268efSVladimir Oltean outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 47e21268efSVladimir Oltean outer_tagging_rule->prio = 1; 48c518afecSVladimir Oltean outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port); 49e21268efSVladimir Oltean outer_tagging_rule->id.tc_offload = false; 50e21268efSVladimir Oltean outer_tagging_rule->block_id = VCAP_ES0; 51e21268efSVladimir Oltean outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 52e21268efSVladimir Oltean outer_tagging_rule->lookup = 0; 53e21268efSVladimir Oltean outer_tagging_rule->ingress_port.value = port; 54e21268efSVladimir Oltean outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0); 55e21268efSVladimir Oltean outer_tagging_rule->egress_port.value = upstream; 56e21268efSVladimir Oltean outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0); 57e21268efSVladimir Oltean outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG; 58e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD; 59e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_vid_sel = 1; 60e21268efSVladimir Oltean outer_tagging_rule->action.vid_a_val = vid; 61e21268efSVladimir Oltean 62e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL); 63e21268efSVladimir Oltean if (err) 64e21268efSVladimir Oltean kfree(outer_tagging_rule); 65e21268efSVladimir Oltean 66e21268efSVladimir Oltean return err; 67e21268efSVladimir Oltean } 68e21268efSVladimir Oltean 6904b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid) 7004b67e18SVladimir Oltean { 7104b67e18SVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 7204b67e18SVladimir Oltean struct ocelot_vcap_block *block_vcap_es0; 7304b67e18SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 7404b67e18SVladimir Oltean 7504b67e18SVladimir Oltean block_vcap_es0 = &ocelot->block[VCAP_ES0]; 7604b67e18SVladimir Oltean 7704b67e18SVladimir Oltean outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 7804b67e18SVladimir Oltean port, false); 7904b67e18SVladimir Oltean if (!outer_tagging_rule) 8004b67e18SVladimir Oltean return -ENOENT; 8104b67e18SVladimir Oltean 8204b67e18SVladimir Oltean return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 8304b67e18SVladimir Oltean } 8404b67e18SVladimir Oltean 8504b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2 8604b67e18SVladimir Oltean * rules for steering those tagged packets towards the correct destination port 8704b67e18SVladimir Oltean */ 8804b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct felix *felix, int port, u16 vid) 89e21268efSVladimir Oltean { 90e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 91e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 92e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 93e21268efSVladimir Oltean int upstream, err; 94e21268efSVladimir Oltean 95e21268efSVladimir Oltean untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 96e21268efSVladimir Oltean if (!untagging_rule) 97e21268efSVladimir Oltean return -ENOMEM; 98e21268efSVladimir Oltean 99e21268efSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 100e21268efSVladimir Oltean if (!redirect_rule) { 101e21268efSVladimir Oltean kfree(untagging_rule); 102e21268efSVladimir Oltean return -ENOMEM; 103e21268efSVladimir Oltean } 104e21268efSVladimir Oltean 105e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 106e21268efSVladimir Oltean 107e21268efSVladimir Oltean untagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 108e21268efSVladimir Oltean untagging_rule->ingress_port_mask = BIT(upstream); 109e21268efSVladimir Oltean untagging_rule->vlan.vid.value = vid; 110e21268efSVladimir Oltean untagging_rule->vlan.vid.mask = VLAN_VID_MASK; 111e21268efSVladimir Oltean untagging_rule->prio = 1; 112c518afecSVladimir Oltean untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port); 113e21268efSVladimir Oltean untagging_rule->id.tc_offload = false; 114e21268efSVladimir Oltean untagging_rule->block_id = VCAP_IS1; 115e21268efSVladimir Oltean untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 116e21268efSVladimir Oltean untagging_rule->lookup = 0; 117e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt_ena = true; 118e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt = 1; 119e21268efSVladimir Oltean untagging_rule->action.pag_override_mask = 0xff; 120e21268efSVladimir Oltean untagging_rule->action.pag_val = port; 121e21268efSVladimir Oltean 122e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL); 123e21268efSVladimir Oltean if (err) { 124e21268efSVladimir Oltean kfree(untagging_rule); 125e21268efSVladimir Oltean kfree(redirect_rule); 126e21268efSVladimir Oltean return err; 127e21268efSVladimir Oltean } 128e21268efSVladimir Oltean 129e21268efSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 130e21268efSVladimir Oltean redirect_rule->ingress_port_mask = BIT(upstream); 131e21268efSVladimir Oltean redirect_rule->pag = port; 132e21268efSVladimir Oltean redirect_rule->prio = 1; 133c518afecSVladimir Oltean redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port); 134e21268efSVladimir Oltean redirect_rule->id.tc_offload = false; 135e21268efSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 136e21268efSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 137e21268efSVladimir Oltean redirect_rule->lookup = 0; 138e21268efSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 139e21268efSVladimir Oltean redirect_rule->action.port_mask = BIT(port); 140e21268efSVladimir Oltean 141e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 142e21268efSVladimir Oltean if (err) { 143e21268efSVladimir Oltean ocelot_vcap_filter_del(ocelot, untagging_rule); 144e21268efSVladimir Oltean kfree(redirect_rule); 145e21268efSVladimir Oltean return err; 146e21268efSVladimir Oltean } 147e21268efSVladimir Oltean 148e21268efSVladimir Oltean return 0; 149e21268efSVladimir Oltean } 150e21268efSVladimir Oltean 15104b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct felix *felix, int port, u16 vid) 152e21268efSVladimir Oltean { 153e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 154e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 155e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 156e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 157e21268efSVladimir Oltean int err; 158e21268efSVladimir Oltean 159e21268efSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 160e21268efSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 161e21268efSVladimir Oltean 162e21268efSVladimir Oltean untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 163e21268efSVladimir Oltean port, false); 164e21268efSVladimir Oltean if (!untagging_rule) 16508f44db3SVladimir Oltean return -ENOENT; 166e21268efSVladimir Oltean 167e21268efSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, untagging_rule); 168e21268efSVladimir Oltean if (err) 169e21268efSVladimir Oltean return err; 170e21268efSVladimir Oltean 171e21268efSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 172e21268efSVladimir Oltean port, false); 173e21268efSVladimir Oltean if (!redirect_rule) 17404b67e18SVladimir Oltean return -ENOENT; 175e21268efSVladimir Oltean 176e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 177e21268efSVladimir Oltean } 178e21268efSVladimir Oltean 17904b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 18004b67e18SVladimir Oltean u16 flags) 18104b67e18SVladimir Oltean { 18204b67e18SVladimir Oltean struct ocelot *ocelot = ds->priv; 18304b67e18SVladimir Oltean int err; 18404b67e18SVladimir Oltean 18504b67e18SVladimir Oltean /* tag_8021q.c assumes we are implementing this via port VLAN 18604b67e18SVladimir Oltean * membership, which we aren't. So we don't need to add any VCAP filter 18704b67e18SVladimir Oltean * for the CPU port. 18804b67e18SVladimir Oltean */ 18904b67e18SVladimir Oltean if (!dsa_is_user_port(ds, port)) 19004b67e18SVladimir Oltean return 0; 19104b67e18SVladimir Oltean 19204b67e18SVladimir Oltean err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid); 19304b67e18SVladimir Oltean if (err) 19404b67e18SVladimir Oltean return err; 19504b67e18SVladimir Oltean 19604b67e18SVladimir Oltean err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid); 19704b67e18SVladimir Oltean if (err) { 19804b67e18SVladimir Oltean felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid); 19904b67e18SVladimir Oltean return err; 20004b67e18SVladimir Oltean } 20104b67e18SVladimir Oltean 20204b67e18SVladimir Oltean return 0; 20304b67e18SVladimir Oltean } 20404b67e18SVladimir Oltean 205e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 206e21268efSVladimir Oltean { 207e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 20804b67e18SVladimir Oltean int err; 209e21268efSVladimir Oltean 21004b67e18SVladimir Oltean if (!dsa_is_user_port(ds, port)) 21104b67e18SVladimir Oltean return 0; 212e21268efSVladimir Oltean 21304b67e18SVladimir Oltean err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid); 21404b67e18SVladimir Oltean if (err) 21504b67e18SVladimir Oltean return err; 21604b67e18SVladimir Oltean 21704b67e18SVladimir Oltean err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid); 21804b67e18SVladimir Oltean if (err) { 21904b67e18SVladimir Oltean felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid); 22004b67e18SVladimir Oltean return err; 22104b67e18SVladimir Oltean } 222e21268efSVladimir Oltean 223e21268efSVladimir Oltean return 0; 224e21268efSVladimir Oltean } 225e21268efSVladimir Oltean 226e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC 227e21268efSVladimir Oltean * connected internally to the enetc or fman DSA master can be configured to 228e21268efSVladimir Oltean * use the software-defined tag_8021q frame format. As far as the hardware is 229e21268efSVladimir Oltean * concerned, it thinks it is a "dumb switch" - the queues of the CPU port 230e21268efSVladimir Oltean * module are now disconnected from it, but can still be accessed through 231e21268efSVladimir Oltean * register-based MMIO. 232e21268efSVladimir Oltean */ 233e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port) 234e21268efSVladimir Oltean { 2358abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2368abe1970SVladimir Oltean 237e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = true; 238e21268efSVladimir Oltean ocelot->npi = -1; 239e21268efSVladimir Oltean 240e21268efSVladimir Oltean /* Overwrite PGID_CPU with the non-tagging port */ 241e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU); 242e21268efSVladimir Oltean 2438abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2448abe1970SVladimir Oltean 2458abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 246e21268efSVladimir Oltean } 247e21268efSVladimir Oltean 248e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port) 249e21268efSVladimir Oltean { 2508abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2518abe1970SVladimir Oltean 252e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = false; 253e21268efSVladimir Oltean 254e21268efSVladimir Oltean /* Restore PGID_CPU */ 255e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID, 256e21268efSVladimir Oltean PGID_CPU); 257e21268efSVladimir Oltean 2588abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2598abe1970SVladimir Oltean 2608abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 261e21268efSVladimir Oltean } 262e21268efSVladimir Oltean 26399348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be 26499348004SVladimir Oltean * replicated over Ethernet as well, otherwise we'd get no notification of 26599348004SVladimir Oltean * their arrival when using the ocelot-8021q tagging protocol. 266c8c0ba4fSVladimir Oltean */ 26799348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds, 26899348004SVladimir Oltean bool using_tag_8021q) 269c8c0ba4fSVladimir Oltean { 27099348004SVladimir Oltean struct ocelot *ocelot = ds->priv; 27199348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 27299348004SVladimir Oltean struct ocelot_vcap_filter *trap; 27399348004SVladimir Oltean enum ocelot_mask_mode mask_mode; 27499348004SVladimir Oltean unsigned long port_mask; 2752960bb14SVladimir Oltean struct dsa_port *dp; 27699348004SVladimir Oltean bool cpu_copy_ena; 27799348004SVladimir Oltean int cpu = -1, err; 278c8c0ba4fSVladimir Oltean 27999348004SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 28099348004SVladimir Oltean return 0; 281c8c0ba4fSVladimir Oltean 28299348004SVladimir Oltean /* Figure out the current CPU port */ 2832960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 2842960bb14SVladimir Oltean cpu = dp->index; 2858d5f7954SVladimir Oltean break; 286c8c0ba4fSVladimir Oltean } 2878d5f7954SVladimir Oltean 288d78637a8SVladimir Oltean /* We are sure that "cpu" was found, otherwise 289d78637a8SVladimir Oltean * dsa_tree_setup_default_cpu() would have failed earlier. 290d78637a8SVladimir Oltean */ 291c8c0ba4fSVladimir Oltean 29299348004SVladimir Oltean /* Make sure all traps are set up for that destination */ 29399348004SVladimir Oltean list_for_each_entry(trap, &ocelot->traps, trap_list) { 29499348004SVladimir Oltean /* Figure out the current trapping destination */ 29599348004SVladimir Oltean if (using_tag_8021q) { 29699348004SVladimir Oltean /* Redirect to the tag_8021q CPU port. If timestamps 29799348004SVladimir Oltean * are necessary, also copy trapped packets to the CPU 29899348004SVladimir Oltean * port module. 299c8c0ba4fSVladimir Oltean */ 30099348004SVladimir Oltean mask_mode = OCELOT_MASK_MODE_REDIRECT; 30199348004SVladimir Oltean port_mask = BIT(cpu); 30299348004SVladimir Oltean cpu_copy_ena = !!trap->take_ts; 303c8c0ba4fSVladimir Oltean } else { 30499348004SVladimir Oltean /* Trap packets only to the CPU port module, which is 30599348004SVladimir Oltean * redirected to the NPI port (the DSA CPU port) 306c8c0ba4fSVladimir Oltean */ 30799348004SVladimir Oltean mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 30899348004SVladimir Oltean port_mask = 0; 30999348004SVladimir Oltean cpu_copy_ena = true; 310c8c0ba4fSVladimir Oltean } 311c8c0ba4fSVladimir Oltean 31299348004SVladimir Oltean if (trap->action.mask_mode == mask_mode && 31399348004SVladimir Oltean trap->action.port_mask == port_mask && 31499348004SVladimir Oltean trap->action.cpu_copy_ena == cpu_copy_ena) 31599348004SVladimir Oltean continue; 316c8c0ba4fSVladimir Oltean 31799348004SVladimir Oltean trap->action.mask_mode = mask_mode; 31899348004SVladimir Oltean trap->action.port_mask = port_mask; 31999348004SVladimir Oltean trap->action.cpu_copy_ena = cpu_copy_ena; 3200a6f17c6SVladimir Oltean 32199348004SVladimir Oltean err = ocelot_vcap_filter_replace(ocelot, trap); 322c8c0ba4fSVladimir Oltean if (err) 323c8c0ba4fSVladimir Oltean return err; 32499348004SVladimir Oltean } 325c8c0ba4fSVladimir Oltean 32699348004SVladimir Oltean return 0; 327c8c0ba4fSVladimir Oltean } 328c8c0ba4fSVladimir Oltean 329e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu) 330e21268efSVladimir Oltean { 331e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 332e21268efSVladimir Oltean unsigned long cpu_flood; 3332960bb14SVladimir Oltean struct dsa_port *dp; 3342960bb14SVladimir Oltean int err; 335e21268efSVladimir Oltean 336e21268efSVladimir Oltean felix_8021q_cpu_port_init(ocelot, cpu); 337e21268efSVladimir Oltean 3382960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 339e21268efSVladimir Oltean /* This overwrites ocelot_init(): 340e21268efSVladimir Oltean * Do not forward BPDU frames to the CPU port module, 341e21268efSVladimir Oltean * for 2 reasons: 342e21268efSVladimir Oltean * - When these packets are injected from the tag_8021q 343e21268efSVladimir Oltean * CPU port, we want them to go out, not loop back 344e21268efSVladimir Oltean * into the system. 345e21268efSVladimir Oltean * - STP traffic ingressing on a user port should go to 346e21268efSVladimir Oltean * the tag_8021q CPU port, not to the hardware CPU 347e21268efSVladimir Oltean * port module. 348e21268efSVladimir Oltean */ 349e21268efSVladimir Oltean ocelot_write_gix(ocelot, 350e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 3512960bb14SVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, dp->index); 352e21268efSVladimir Oltean } 353e21268efSVladimir Oltean 354c8c0ba4fSVladimir Oltean /* In tag_8021q mode, the CPU port module is unused, except for PTP 355c8c0ba4fSVladimir Oltean * frames. So we want to disable flooding of any kind to the CPU port 356c8c0ba4fSVladimir Oltean * module, since packets going there will end in a black hole. 357e21268efSVladimir Oltean */ 358e21268efSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 359e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC); 360e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC); 361b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC); 362e21268efSVladimir Oltean 3635da11eb4SVladimir Oltean err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD)); 364e21268efSVladimir Oltean if (err) 365d7b1fd52SVladimir Oltean return err; 366d7b1fd52SVladimir Oltean 36799348004SVladimir Oltean err = felix_update_trapping_destinations(ds, true); 368d7b1fd52SVladimir Oltean if (err) 369d7b1fd52SVladimir Oltean goto out_tag_8021q_unregister; 370e21268efSVladimir Oltean 37199348004SVladimir Oltean /* The ownership of the CPU port module's queues might have just been 37299348004SVladimir Oltean * transferred to the tag_8021q tagger from the NPI-based tagger. 37399348004SVladimir Oltean * So there might still be all sorts of crap in the queues. On the 37499348004SVladimir Oltean * other hand, the MMIO-based matching of PTP frames is very brittle, 37599348004SVladimir Oltean * so we need to be careful that there are no extra frames to be 37699348004SVladimir Oltean * dequeued over MMIO, since we would never know to discard them. 37799348004SVladimir Oltean */ 37899348004SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 37999348004SVladimir Oltean 380e21268efSVladimir Oltean return 0; 381e21268efSVladimir Oltean 382d7b1fd52SVladimir Oltean out_tag_8021q_unregister: 383d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 384e21268efSVladimir Oltean return err; 385e21268efSVladimir Oltean } 386e21268efSVladimir Oltean 387e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu) 388e21268efSVladimir Oltean { 389e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 3902960bb14SVladimir Oltean struct dsa_port *dp; 3912960bb14SVladimir Oltean int err; 392e21268efSVladimir Oltean 39399348004SVladimir Oltean err = felix_update_trapping_destinations(ds, false); 394c8c0ba4fSVladimir Oltean if (err) 395c8c0ba4fSVladimir Oltean dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d", 396c8c0ba4fSVladimir Oltean err); 397c8c0ba4fSVladimir Oltean 398d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 399e21268efSVladimir Oltean 4002960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 401e21268efSVladimir Oltean /* Restore the logic from ocelot_init: 402e21268efSVladimir Oltean * do not forward BPDU frames to the front ports. 403e21268efSVladimir Oltean */ 404e21268efSVladimir Oltean ocelot_write_gix(ocelot, 405e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 406e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, 4072960bb14SVladimir Oltean dp->index); 408e21268efSVladimir Oltean } 409e21268efSVladimir Oltean 410e21268efSVladimir Oltean felix_8021q_cpu_port_deinit(ocelot, cpu); 411e21268efSVladimir Oltean } 412e21268efSVladimir Oltean 413adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This 414adb3dccfSVladimir Oltean * is the mode through which frames can be injected from and extracted to an 415adb3dccfSVladimir Oltean * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 416adb3dccfSVladimir Oltean * running Linux, and this forms a DSA setup together with the enetc or fman 417adb3dccfSVladimir Oltean * DSA master. 418adb3dccfSVladimir Oltean */ 419adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port) 420adb3dccfSVladimir Oltean { 421adb3dccfSVladimir Oltean ocelot->npi = port; 422adb3dccfSVladimir Oltean 423adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 424adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 425adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 426adb3dccfSVladimir Oltean 427adb3dccfSVladimir Oltean /* NPI port Injection/Extraction configuration */ 428adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 429adb3dccfSVladimir Oltean ocelot->npi_xtr_prefix); 430adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 431adb3dccfSVladimir Oltean ocelot->npi_inj_prefix); 432adb3dccfSVladimir Oltean 433adb3dccfSVladimir Oltean /* Disable transmission of pause frames */ 434adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 435adb3dccfSVladimir Oltean } 436adb3dccfSVladimir Oltean 437adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 438adb3dccfSVladimir Oltean { 439adb3dccfSVladimir Oltean /* Restore hardware defaults */ 440adb3dccfSVladimir Oltean int unused_port = ocelot->num_phys_ports + 2; 441adb3dccfSVladimir Oltean 442adb3dccfSVladimir Oltean ocelot->npi = -1; 443adb3dccfSVladimir Oltean 444adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 445adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 446adb3dccfSVladimir Oltean 447adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 448adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 449adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 450adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 451adb3dccfSVladimir Oltean 452adb3dccfSVladimir Oltean /* Enable transmission of pause frames */ 453adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 454adb3dccfSVladimir Oltean } 455adb3dccfSVladimir Oltean 456adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu) 457adb3dccfSVladimir Oltean { 458adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 459adb3dccfSVladimir Oltean unsigned long cpu_flood; 460adb3dccfSVladimir Oltean 461adb3dccfSVladimir Oltean felix_npi_port_init(ocelot, cpu); 462adb3dccfSVladimir Oltean 463adb3dccfSVladimir Oltean /* Include the CPU port module (and indirectly, the NPI port) 464adb3dccfSVladimir Oltean * in the forwarding mask for unknown unicast - the hardware 465adb3dccfSVladimir Oltean * default value for ANA_FLOODING_FLD_UNICAST excludes 466adb3dccfSVladimir Oltean * BIT(ocelot->num_phys_ports), and so does ocelot_init, 467adb3dccfSVladimir Oltean * since Ocelot relies on whitelisting MAC addresses towards 468adb3dccfSVladimir Oltean * PGID_CPU. 469adb3dccfSVladimir Oltean * We do this because DSA does not yet perform RX filtering, 470adb3dccfSVladimir Oltean * and the NPI port does not perform source address learning, 471adb3dccfSVladimir Oltean * so traffic sent to Linux is effectively unknown from the 472adb3dccfSVladimir Oltean * switch's perspective. 473adb3dccfSVladimir Oltean */ 474adb3dccfSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 475adb3dccfSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC); 4766edb9e8dSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC); 477b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC); 478adb3dccfSVladimir Oltean 479adb3dccfSVladimir Oltean return 0; 480adb3dccfSVladimir Oltean } 481adb3dccfSVladimir Oltean 482adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu) 483adb3dccfSVladimir Oltean { 484adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 485adb3dccfSVladimir Oltean 486adb3dccfSVladimir Oltean felix_npi_port_deinit(ocelot, cpu); 487adb3dccfSVladimir Oltean } 488adb3dccfSVladimir Oltean 489adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu, 490adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 491adb3dccfSVladimir Oltean { 492adb3dccfSVladimir Oltean int err; 493adb3dccfSVladimir Oltean 494adb3dccfSVladimir Oltean switch (proto) { 4957c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 496adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 497adb3dccfSVladimir Oltean err = felix_setup_tag_npi(ds, cpu); 498adb3dccfSVladimir Oltean break; 499e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 500e21268efSVladimir Oltean err = felix_setup_tag_8021q(ds, cpu); 501e21268efSVladimir Oltean break; 502adb3dccfSVladimir Oltean default: 503adb3dccfSVladimir Oltean err = -EPROTONOSUPPORT; 504adb3dccfSVladimir Oltean } 505adb3dccfSVladimir Oltean 506adb3dccfSVladimir Oltean return err; 507adb3dccfSVladimir Oltean } 508adb3dccfSVladimir Oltean 509adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu, 510adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 511adb3dccfSVladimir Oltean { 512adb3dccfSVladimir Oltean switch (proto) { 5137c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 514adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 515adb3dccfSVladimir Oltean felix_teardown_tag_npi(ds, cpu); 516adb3dccfSVladimir Oltean break; 517e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 518e21268efSVladimir Oltean felix_teardown_tag_8021q(ds, cpu); 519e21268efSVladimir Oltean break; 520adb3dccfSVladimir Oltean default: 521adb3dccfSVladimir Oltean break; 522adb3dccfSVladimir Oltean } 523adb3dccfSVladimir Oltean } 524adb3dccfSVladimir Oltean 525e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the 526e21268efSVladimir Oltean * tag_8021q setup can fail, the NPI setup can't. So either the change is made, 527e21268efSVladimir Oltean * or the restoration is guaranteed to work. 528e21268efSVladimir Oltean */ 529adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu, 530adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 531adb3dccfSVladimir Oltean { 532adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 533adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 534adb3dccfSVladimir Oltean enum dsa_tag_protocol old_proto = felix->tag_proto; 535adb3dccfSVladimir Oltean int err; 536adb3dccfSVladimir Oltean 5377c4bb540SVladimir Oltean if (proto != DSA_TAG_PROTO_SEVILLE && 5387c4bb540SVladimir Oltean proto != DSA_TAG_PROTO_OCELOT && 539e21268efSVladimir Oltean proto != DSA_TAG_PROTO_OCELOT_8021Q) 540adb3dccfSVladimir Oltean return -EPROTONOSUPPORT; 541adb3dccfSVladimir Oltean 542adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, cpu, old_proto); 543adb3dccfSVladimir Oltean 544adb3dccfSVladimir Oltean err = felix_set_tag_protocol(ds, cpu, proto); 545adb3dccfSVladimir Oltean if (err) { 546adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, cpu, old_proto); 547adb3dccfSVladimir Oltean return err; 548adb3dccfSVladimir Oltean } 549adb3dccfSVladimir Oltean 550adb3dccfSVladimir Oltean felix->tag_proto = proto; 551adb3dccfSVladimir Oltean 552adb3dccfSVladimir Oltean return 0; 553adb3dccfSVladimir Oltean } 554adb3dccfSVladimir Oltean 55556051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 5564d776482SFlorian Fainelli int port, 5574d776482SFlorian Fainelli enum dsa_tag_protocol mp) 55856051948SVladimir Oltean { 559adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 560adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 561adb3dccfSVladimir Oltean 562adb3dccfSVladimir Oltean return felix->tag_proto; 56356051948SVladimir Oltean } 56456051948SVladimir Oltean 56556051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds, 56656051948SVladimir Oltean unsigned int ageing_time) 56756051948SVladimir Oltean { 56856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 56956051948SVladimir Oltean 57056051948SVladimir Oltean ocelot_set_ageing_time(ocelot, ageing_time); 57156051948SVladimir Oltean 57256051948SVladimir Oltean return 0; 57356051948SVladimir Oltean } 57456051948SVladimir Oltean 5755cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port) 5765cad43a5SVladimir Oltean { 5775cad43a5SVladimir Oltean struct ocelot *ocelot = ds->priv; 5785cad43a5SVladimir Oltean int err; 5795cad43a5SVladimir Oltean 5805cad43a5SVladimir Oltean err = ocelot_mact_flush(ocelot, port); 5815cad43a5SVladimir Oltean if (err) 5825cad43a5SVladimir Oltean dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n", 5835cad43a5SVladimir Oltean port, ERR_PTR(err)); 5845cad43a5SVladimir Oltean } 5855cad43a5SVladimir Oltean 58656051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port, 58756051948SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 58856051948SVladimir Oltean { 58956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 59056051948SVladimir Oltean 59156051948SVladimir Oltean return ocelot_fdb_dump(ocelot, port, cb, data); 59256051948SVladimir Oltean } 59356051948SVladimir Oltean 59456051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port, 595c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 596c2693363SVladimir Oltean struct dsa_db db) 59756051948SVladimir Oltean { 59856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 59956051948SVladimir Oltean 60087b0f983SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid); 60156051948SVladimir Oltean } 60256051948SVladimir Oltean 60356051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port, 604c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 605c2693363SVladimir Oltean struct dsa_db db) 60656051948SVladimir Oltean { 60756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 60856051948SVladimir Oltean 60956051948SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid); 61056051948SVladimir Oltean } 61156051948SVladimir Oltean 612961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag, 613c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 614c2693363SVladimir Oltean struct dsa_db db) 615961d8b69SVladimir Oltean { 616961d8b69SVladimir Oltean struct ocelot *ocelot = ds->priv; 617961d8b69SVladimir Oltean 618961d8b69SVladimir Oltean return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid); 619961d8b69SVladimir Oltean } 620961d8b69SVladimir Oltean 621961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag, 622c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 623c2693363SVladimir Oltean struct dsa_db db) 624961d8b69SVladimir Oltean { 625961d8b69SVladimir Oltean struct ocelot *ocelot = ds->priv; 626961d8b69SVladimir Oltean 627961d8b69SVladimir Oltean return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid); 628961d8b69SVladimir Oltean } 629961d8b69SVladimir Oltean 630a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 631c2693363SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 632c2693363SVladimir Oltean struct dsa_db db) 633209edf95SVladimir Oltean { 634209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 635209edf95SVladimir Oltean 636a52b2da7SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb); 637209edf95SVladimir Oltean } 638209edf95SVladimir Oltean 639209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port, 640c2693363SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 641c2693363SVladimir Oltean struct dsa_db db) 642209edf95SVladimir Oltean { 643209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 644209edf95SVladimir Oltean 645209edf95SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb); 646209edf95SVladimir Oltean } 647209edf95SVladimir Oltean 64856051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 64956051948SVladimir Oltean u8 state) 65056051948SVladimir Oltean { 65156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 65256051948SVladimir Oltean 65356051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 65456051948SVladimir Oltean } 65556051948SVladimir Oltean 656421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 657421741eaSVladimir Oltean struct switchdev_brport_flags val, 658421741eaSVladimir Oltean struct netlink_ext_ack *extack) 659421741eaSVladimir Oltean { 660421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 661421741eaSVladimir Oltean 662421741eaSVladimir Oltean return ocelot_port_pre_bridge_flags(ocelot, port, val); 663421741eaSVladimir Oltean } 664421741eaSVladimir Oltean 665421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port, 666421741eaSVladimir Oltean struct switchdev_brport_flags val, 667421741eaSVladimir Oltean struct netlink_ext_ack *extack) 668421741eaSVladimir Oltean { 669421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 670421741eaSVladimir Oltean 671421741eaSVladimir Oltean ocelot_port_bridge_flags(ocelot, port, val); 672421741eaSVladimir Oltean 673421741eaSVladimir Oltean return 0; 674421741eaSVladimir Oltean } 675421741eaSVladimir Oltean 67656051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 677*06b9cce4SVladimir Oltean struct dsa_bridge bridge, bool *tx_fwd_offload, 678*06b9cce4SVladimir Oltean struct netlink_ext_ack *extack) 67956051948SVladimir Oltean { 68056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 68156051948SVladimir Oltean 682d3eed0e5SVladimir Oltean ocelot_port_bridge_join(ocelot, port, bridge.dev); 683e4bd44e8SVladimir Oltean 684e4bd44e8SVladimir Oltean return 0; 68556051948SVladimir Oltean } 68656051948SVladimir Oltean 68756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 688d3eed0e5SVladimir Oltean struct dsa_bridge bridge) 68956051948SVladimir Oltean { 69056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 69156051948SVladimir Oltean 692d3eed0e5SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, bridge.dev); 69356051948SVladimir Oltean } 69456051948SVladimir Oltean 6958fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 696dedd6a00SVladimir Oltean struct dsa_lag lag, 6978fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 6988fe6832eSVladimir Oltean { 6998fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7008fe6832eSVladimir Oltean 701dedd6a00SVladimir Oltean return ocelot_port_lag_join(ocelot, port, lag.dev, info); 7028fe6832eSVladimir Oltean } 7038fe6832eSVladimir Oltean 7048fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 705dedd6a00SVladimir Oltean struct dsa_lag lag) 7068fe6832eSVladimir Oltean { 7078fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7088fe6832eSVladimir Oltean 709dedd6a00SVladimir Oltean ocelot_port_lag_leave(ocelot, port, lag.dev); 7108fe6832eSVladimir Oltean 7118fe6832eSVladimir Oltean return 0; 7128fe6832eSVladimir Oltean } 7138fe6832eSVladimir Oltean 7148fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 7158fe6832eSVladimir Oltean { 7168fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 7178fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7188fe6832eSVladimir Oltean 7198fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 7208fe6832eSVladimir Oltean 7218fe6832eSVladimir Oltean return 0; 7228fe6832eSVladimir Oltean } 7238fe6832eSVladimir Oltean 72456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 72501af940eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 72601af940eSVladimir Oltean struct netlink_ext_ack *extack) 72756051948SVladimir Oltean { 7282f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 729b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 7302f0402feSVladimir Oltean 7319a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 7329a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 7339a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 7349a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 7359a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 7369a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 7379a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 7389a720680SVladimir Oltean */ 7399a720680SVladimir Oltean if (port == ocelot->npi) 7409a720680SVladimir Oltean return 0; 7419a720680SVladimir Oltean 742b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 7432f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 74401af940eSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED, 74501af940eSVladimir Oltean extack); 74656051948SVladimir Oltean } 74756051948SVladimir Oltean 74889153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 74989153ed6SVladimir Oltean struct netlink_ext_ack *extack) 75056051948SVladimir Oltean { 75156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 75256051948SVladimir Oltean 7533b95d1b2SVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 75456051948SVladimir Oltean } 75556051948SVladimir Oltean 7561958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 75731046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 75831046a5fSVladimir Oltean struct netlink_ext_ack *extack) 75956051948SVladimir Oltean { 76056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 761183be6f9SVladimir Oltean u16 flags = vlan->flags; 7621958d581SVladimir Oltean int err; 76356051948SVladimir Oltean 76401af940eSVladimir Oltean err = felix_vlan_prepare(ds, port, vlan, extack); 7651958d581SVladimir Oltean if (err) 7661958d581SVladimir Oltean return err; 7671958d581SVladimir Oltean 7681958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 769183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 770183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 77156051948SVladimir Oltean } 77256051948SVladimir Oltean 77356051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 77456051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 77556051948SVladimir Oltean { 77656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 77756051948SVladimir Oltean 778b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 77956051948SVladimir Oltean } 78056051948SVladimir Oltean 78179fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port, 78279fda660SRussell King (Oracle) struct phylink_config *config) 78379fda660SRussell King (Oracle) { 78479fda660SRussell King (Oracle) struct ocelot *ocelot = ds->priv; 78579fda660SRussell King (Oracle) 786f6f04c02SRussell King (Oracle) /* This driver does not make use of the speed, duplex, pause or the 787f6f04c02SRussell King (Oracle) * advertisement in its mac_config, so it is safe to mark this driver 788f6f04c02SRussell King (Oracle) * as non-legacy. 789f6f04c02SRussell King (Oracle) */ 790f6f04c02SRussell King (Oracle) config->legacy_pre_march2020 = false; 791f6f04c02SRussell King (Oracle) 79279fda660SRussell King (Oracle) __set_bit(ocelot->ports[port]->phy_mode, 79379fda660SRussell King (Oracle) config->supported_interfaces); 79479fda660SRussell King (Oracle) } 79579fda660SRussell King (Oracle) 796bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port, 797bdeced75SVladimir Oltean unsigned long *supported, 798bdeced75SVladimir Oltean struct phylink_link_state *state) 799bdeced75SVladimir Oltean { 800bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 801375e1314SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 802bdeced75SVladimir Oltean 803375e1314SVladimir Oltean if (felix->info->phylink_validate) 804375e1314SVladimir Oltean felix->info->phylink_validate(ocelot, port, supported, state); 805bdeced75SVladimir Oltean } 806bdeced75SVladimir Oltean 807864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds, 808864ba485SRussell King (Oracle) int port, 809864ba485SRussell King (Oracle) phy_interface_t iface) 810bdeced75SVladimir Oltean { 811bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 812bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 813864ba485SRussell King (Oracle) struct phylink_pcs *pcs = NULL; 814bdeced75SVladimir Oltean 81549af6a76SColin Foster if (felix->pcs && felix->pcs[port]) 816864ba485SRussell King (Oracle) pcs = felix->pcs[port]; 817864ba485SRussell King (Oracle) 818864ba485SRussell King (Oracle) return pcs; 819bdeced75SVladimir Oltean } 820bdeced75SVladimir Oltean 821bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 822bdeced75SVladimir Oltean unsigned int link_an_mode, 823bdeced75SVladimir Oltean phy_interface_t interface) 824bdeced75SVladimir Oltean { 825bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 826bdeced75SVladimir Oltean 827e6e12df6SVladimir Oltean ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 828e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 829bdeced75SVladimir Oltean } 830bdeced75SVladimir Oltean 831bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 832bdeced75SVladimir Oltean unsigned int link_an_mode, 833bdeced75SVladimir Oltean phy_interface_t interface, 8345b502a7bSRussell King struct phy_device *phydev, 8355b502a7bSRussell King int speed, int duplex, 8365b502a7bSRussell King bool tx_pause, bool rx_pause) 837bdeced75SVladimir Oltean { 838bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 8397e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 840bdeced75SVladimir Oltean 841e6e12df6SVladimir Oltean ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 842e6e12df6SVladimir Oltean interface, speed, duplex, tx_pause, rx_pause, 843e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 8447e14a2dcSVladimir Oltean 8457e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 8467e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 847bdeced75SVladimir Oltean } 848bdeced75SVladimir Oltean 849bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 850bd2b3161SXiaoliang Yang { 851bd2b3161SXiaoliang Yang int i; 852bd2b3161SXiaoliang Yang 853bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 854bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 855bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 856bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 857bd2b3161SXiaoliang Yang port); 858bd2b3161SXiaoliang Yang 85970d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 860bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 861bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 862bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 863bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 864bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 865bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 866bd2b3161SXiaoliang Yang port, i); 867bd2b3161SXiaoliang Yang } 868bd2b3161SXiaoliang Yang } 869bd2b3161SXiaoliang Yang 87056051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 87156051948SVladimir Oltean u32 stringset, u8 *data) 87256051948SVladimir Oltean { 87356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 87456051948SVladimir Oltean 87556051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 87656051948SVladimir Oltean } 87756051948SVladimir Oltean 87856051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 87956051948SVladimir Oltean { 88056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 88156051948SVladimir Oltean 88256051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 88356051948SVladimir Oltean } 88456051948SVladimir Oltean 88556051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 88656051948SVladimir Oltean { 88756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 88856051948SVladimir Oltean 88956051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 89056051948SVladimir Oltean } 89156051948SVladimir Oltean 89256051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 89356051948SVladimir Oltean struct ethtool_ts_info *info) 89456051948SVladimir Oltean { 89556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 89656051948SVladimir Oltean 89756051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 89856051948SVladimir Oltean } 89956051948SVladimir Oltean 900bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 901bdeced75SVladimir Oltean struct device_node *ports_node, 902bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 903bdeced75SVladimir Oltean { 904bdeced75SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 905bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 906bdeced75SVladimir Oltean struct device_node *child; 907bdeced75SVladimir Oltean 90837fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 909bdeced75SVladimir Oltean phy_interface_t phy_mode; 910bdeced75SVladimir Oltean u32 port; 911bdeced75SVladimir Oltean int err; 912bdeced75SVladimir Oltean 913bdeced75SVladimir Oltean /* Get switch port number from DT */ 914bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 915bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 916bdeced75SVladimir Oltean "(property \"reg\")\n"); 917bdeced75SVladimir Oltean of_node_put(child); 918bdeced75SVladimir Oltean return -ENODEV; 919bdeced75SVladimir Oltean } 920bdeced75SVladimir Oltean 921bdeced75SVladimir Oltean /* Get PHY mode from DT */ 922bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 923bdeced75SVladimir Oltean if (err) { 924bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 925bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 926bdeced75SVladimir Oltean port); 927bdeced75SVladimir Oltean of_node_put(child); 928bdeced75SVladimir Oltean return -ENODEV; 929bdeced75SVladimir Oltean } 930bdeced75SVladimir Oltean 931bdeced75SVladimir Oltean err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 932bdeced75SVladimir Oltean if (err < 0) { 933bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 934bdeced75SVladimir Oltean phy_modes(phy_mode), port); 93559ebb430SSumera Priyadarsini of_node_put(child); 936bdeced75SVladimir Oltean return err; 937bdeced75SVladimir Oltean } 938bdeced75SVladimir Oltean 939bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 940bdeced75SVladimir Oltean } 941bdeced75SVladimir Oltean 942bdeced75SVladimir Oltean return 0; 943bdeced75SVladimir Oltean } 944bdeced75SVladimir Oltean 945bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 946bdeced75SVladimir Oltean { 947bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 948bdeced75SVladimir Oltean struct device_node *switch_node; 949bdeced75SVladimir Oltean struct device_node *ports_node; 950bdeced75SVladimir Oltean int err; 951bdeced75SVladimir Oltean 952bdeced75SVladimir Oltean switch_node = dev->of_node; 953bdeced75SVladimir Oltean 954bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 955abecbfcdSVladimir Oltean if (!ports_node) 956abecbfcdSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 957bdeced75SVladimir Oltean if (!ports_node) { 958abecbfcdSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 959bdeced75SVladimir Oltean return -ENODEV; 960bdeced75SVladimir Oltean } 961bdeced75SVladimir Oltean 962bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 963bdeced75SVladimir Oltean of_node_put(ports_node); 964bdeced75SVladimir Oltean 965bdeced75SVladimir Oltean return err; 966bdeced75SVladimir Oltean } 967bdeced75SVladimir Oltean 96856051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 96956051948SVladimir Oltean { 97056051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 971bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 972b4024c9eSClaudiu Manoil struct resource res; 97356051948SVladimir Oltean int port, i, err; 97456051948SVladimir Oltean 97556051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 97656051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 97756051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 97856051948SVladimir Oltean if (!ocelot->ports) 97956051948SVladimir Oltean return -ENOMEM; 98056051948SVladimir Oltean 98156051948SVladimir Oltean ocelot->map = felix->info->map; 98256051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 98356051948SVladimir Oltean ocelot->num_stats = felix->info->num_stats; 98421ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 98507d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 98677043c37SXiaoliang Yang ocelot->vcap_pol.base = felix->info->vcap_pol_base; 98777043c37SXiaoliang Yang ocelot->vcap_pol.max = felix->info->vcap_pol_max; 98877043c37SXiaoliang Yang ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 98977043c37SXiaoliang Yang ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 99056051948SVladimir Oltean ocelot->ops = felix->info->ops; 991cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 992cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 993f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 99456051948SVladimir Oltean 995bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 996bdeced75SVladimir Oltean GFP_KERNEL); 997bdeced75SVladimir Oltean if (!port_phy_modes) 998bdeced75SVladimir Oltean return -ENOMEM; 999bdeced75SVladimir Oltean 1000bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 1001bdeced75SVladimir Oltean if (err) { 1002bdeced75SVladimir Oltean kfree(port_phy_modes); 1003bdeced75SVladimir Oltean return err; 1004bdeced75SVladimir Oltean } 1005bdeced75SVladimir Oltean 100656051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 100756051948SVladimir Oltean struct regmap *target; 100856051948SVladimir Oltean 100956051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 101056051948SVladimir Oltean continue; 101156051948SVladimir Oltean 1012b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 1013b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1014375e1314SVladimir Oltean res.start += felix->switch_base; 1015375e1314SVladimir Oltean res.end += felix->switch_base; 101656051948SVladimir Oltean 1017242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 101856051948SVladimir Oltean if (IS_ERR(target)) { 101956051948SVladimir Oltean dev_err(ocelot->dev, 102056051948SVladimir Oltean "Failed to map device memory space\n"); 1021bdeced75SVladimir Oltean kfree(port_phy_modes); 102256051948SVladimir Oltean return PTR_ERR(target); 102356051948SVladimir Oltean } 102456051948SVladimir Oltean 102556051948SVladimir Oltean ocelot->targets[i] = target; 102656051948SVladimir Oltean } 102756051948SVladimir Oltean 102856051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 102956051948SVladimir Oltean if (err) { 103056051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 1031bdeced75SVladimir Oltean kfree(port_phy_modes); 103256051948SVladimir Oltean return err; 103356051948SVladimir Oltean } 103456051948SVladimir Oltean 103556051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 103656051948SVladimir Oltean struct ocelot_port *ocelot_port; 103791c724cfSVladimir Oltean struct regmap *target; 103856051948SVladimir Oltean 103956051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 104056051948SVladimir Oltean sizeof(struct ocelot_port), 104156051948SVladimir Oltean GFP_KERNEL); 104256051948SVladimir Oltean if (!ocelot_port) { 104356051948SVladimir Oltean dev_err(ocelot->dev, 104456051948SVladimir Oltean "failed to allocate port memory\n"); 1045bdeced75SVladimir Oltean kfree(port_phy_modes); 104656051948SVladimir Oltean return -ENOMEM; 104756051948SVladimir Oltean } 104856051948SVladimir Oltean 1049b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1050b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1051375e1314SVladimir Oltean res.start += felix->switch_base; 1052375e1314SVladimir Oltean res.end += felix->switch_base; 105356051948SVladimir Oltean 1054242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 105591c724cfSVladimir Oltean if (IS_ERR(target)) { 105656051948SVladimir Oltean dev_err(ocelot->dev, 105791c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 105891c724cfSVladimir Oltean port); 1059bdeced75SVladimir Oltean kfree(port_phy_modes); 106091c724cfSVladimir Oltean return PTR_ERR(target); 106156051948SVladimir Oltean } 106256051948SVladimir Oltean 1063bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 106456051948SVladimir Oltean ocelot_port->ocelot = ocelot; 106591c724cfSVladimir Oltean ocelot_port->target = target; 106656051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 106756051948SVladimir Oltean } 106856051948SVladimir Oltean 1069bdeced75SVladimir Oltean kfree(port_phy_modes); 1070bdeced75SVladimir Oltean 1071bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1072bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1073bdeced75SVladimir Oltean if (err < 0) 1074bdeced75SVladimir Oltean return err; 1075bdeced75SVladimir Oltean } 1076bdeced75SVladimir Oltean 107756051948SVladimir Oltean return 0; 107856051948SVladimir Oltean } 107956051948SVladimir Oltean 10801328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 10811328a883SVladimir Oltean struct sk_buff *skb) 10821328a883SVladimir Oltean { 10831328a883SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 10841328a883SVladimir Oltean struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 10851328a883SVladimir Oltean struct sk_buff *skb_match = NULL, *skb_tmp; 10861328a883SVladimir Oltean unsigned long flags; 10871328a883SVladimir Oltean 10881328a883SVladimir Oltean if (!clone) 10891328a883SVladimir Oltean return; 10901328a883SVladimir Oltean 10911328a883SVladimir Oltean spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 10921328a883SVladimir Oltean 10931328a883SVladimir Oltean skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 10941328a883SVladimir Oltean if (skb != clone) 10951328a883SVladimir Oltean continue; 10961328a883SVladimir Oltean __skb_unlink(skb, &ocelot_port->tx_skbs); 10971328a883SVladimir Oltean skb_match = skb; 10981328a883SVladimir Oltean break; 10991328a883SVladimir Oltean } 11001328a883SVladimir Oltean 11011328a883SVladimir Oltean spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 11021328a883SVladimir Oltean 11031328a883SVladimir Oltean WARN_ONCE(!skb_match, 11041328a883SVladimir Oltean "Could not find skb clone in TX timestamping list\n"); 11051328a883SVladimir Oltean } 11061328a883SVladimir Oltean 110749f885b2SVladimir Oltean #define work_to_xmit_work(w) \ 110849f885b2SVladimir Oltean container_of((w), struct felix_deferred_xmit_work, work) 110949f885b2SVladimir Oltean 111049f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work) 111149f885b2SVladimir Oltean { 111249f885b2SVladimir Oltean struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 111349f885b2SVladimir Oltean struct dsa_switch *ds = xmit_work->dp->ds; 111449f885b2SVladimir Oltean struct sk_buff *skb = xmit_work->skb; 111549f885b2SVladimir Oltean u32 rew_op = ocelot_ptp_rew_op(skb); 111649f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 111749f885b2SVladimir Oltean int port = xmit_work->dp->index; 111849f885b2SVladimir Oltean int retries = 10; 111949f885b2SVladimir Oltean 112049f885b2SVladimir Oltean do { 112149f885b2SVladimir Oltean if (ocelot_can_inject(ocelot, 0)) 112249f885b2SVladimir Oltean break; 112349f885b2SVladimir Oltean 112449f885b2SVladimir Oltean cpu_relax(); 112549f885b2SVladimir Oltean } while (--retries); 112649f885b2SVladimir Oltean 112749f885b2SVladimir Oltean if (!retries) { 112849f885b2SVladimir Oltean dev_err(ocelot->dev, "port %d failed to inject skb\n", 112949f885b2SVladimir Oltean port); 11301328a883SVladimir Oltean ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 113149f885b2SVladimir Oltean kfree_skb(skb); 113249f885b2SVladimir Oltean return; 113349f885b2SVladimir Oltean } 113449f885b2SVladimir Oltean 113549f885b2SVladimir Oltean ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 113649f885b2SVladimir Oltean 113749f885b2SVladimir Oltean consume_skb(skb); 113849f885b2SVladimir Oltean kfree(xmit_work); 113949f885b2SVladimir Oltean } 114049f885b2SVladimir Oltean 114135d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds, 114235d97680SVladimir Oltean enum dsa_tag_protocol proto) 114349f885b2SVladimir Oltean { 114435d97680SVladimir Oltean struct ocelot_8021q_tagger_data *tagger_data; 114549f885b2SVladimir Oltean 114635d97680SVladimir Oltean switch (proto) { 114735d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 114835d97680SVladimir Oltean tagger_data = ocelot_8021q_tagger_data(ds); 114935d97680SVladimir Oltean tagger_data->xmit_work_fn = felix_port_deferred_xmit; 115049f885b2SVladimir Oltean return 0; 115135d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT: 115235d97680SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 115349f885b2SVladimir Oltean return 0; 115435d97680SVladimir Oltean default: 115535d97680SVladimir Oltean return -EPROTONOSUPPORT; 115649f885b2SVladimir Oltean } 115749f885b2SVladimir Oltean } 115849f885b2SVladimir Oltean 115956051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with 116056051948SVladimir Oltean * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 116156051948SVladimir Oltean * us to allocate structures twice (leak memory) and map PCI memory twice 116256051948SVladimir Oltean * (which will not work). 116356051948SVladimir Oltean */ 116456051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds) 116556051948SVladimir Oltean { 116656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 116756051948SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 11682960bb14SVladimir Oltean struct dsa_port *dp; 11692960bb14SVladimir Oltean int err; 117056051948SVladimir Oltean 117156051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 117256051948SVladimir Oltean if (err) 117356051948SVladimir Oltean return err; 117456051948SVladimir Oltean 1175d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1176d1cc0e93SVladimir Oltean if (err) 11776b73b7c9SVladimir Oltean goto out_mdiobus_free; 1178d1cc0e93SVladimir Oltean 11792b49d128SYangbo Lu if (ocelot->ptp) { 11802ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 11812b49d128SYangbo Lu if (err) { 11822b49d128SYangbo Lu dev_err(ocelot->dev, 11832b49d128SYangbo Lu "Timestamp initialization failed\n"); 11842b49d128SYangbo Lu ocelot->ptp = 0; 11852b49d128SYangbo Lu } 11862b49d128SYangbo Lu } 118756051948SVladimir Oltean 11882960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 11892960bb14SVladimir Oltean ocelot_init_port(ocelot, dp->index); 1190bd2b3161SXiaoliang Yang 1191bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1192bd2b3161SXiaoliang Yang * bits of vlan tag. 1193bd2b3161SXiaoliang Yang */ 11942960bb14SVladimir Oltean felix_port_qos_map_init(ocelot, dp->index); 119556051948SVladimir Oltean } 119656051948SVladimir Oltean 1197f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1198f59fd9caSVladimir Oltean if (err) 11996b73b7c9SVladimir Oltean goto out_deinit_ports; 1200f59fd9caSVladimir Oltean 12012960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 1202adb3dccfSVladimir Oltean /* The initial tag protocol is NPI which always returns 0, so 1203adb3dccfSVladimir Oltean * there's no real point in checking for errors. 12041cf3299bSVladimir Oltean */ 12052960bb14SVladimir Oltean felix_set_tag_protocol(ds, dp->index, felix->tag_proto); 12068d5f7954SVladimir Oltean break; 1207adb3dccfSVladimir Oltean } 12081cf3299bSVladimir Oltean 12090b912fc9SVladimir Oltean ds->mtu_enforcement_ingress = true; 1210c54913c1SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 1211bdeced75SVladimir Oltean 121256051948SVladimir Oltean return 0; 12136b73b7c9SVladimir Oltean 12146b73b7c9SVladimir Oltean out_deinit_ports: 12152960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 12162960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 12176b73b7c9SVladimir Oltean 12186b73b7c9SVladimir Oltean ocelot_deinit_timestamp(ocelot); 12196b73b7c9SVladimir Oltean ocelot_deinit(ocelot); 12206b73b7c9SVladimir Oltean 12216b73b7c9SVladimir Oltean out_mdiobus_free: 12226b73b7c9SVladimir Oltean if (felix->info->mdio_bus_free) 12236b73b7c9SVladimir Oltean felix->info->mdio_bus_free(ocelot); 12246b73b7c9SVladimir Oltean 12256b73b7c9SVladimir Oltean return err; 122656051948SVladimir Oltean } 122756051948SVladimir Oltean 122856051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 122956051948SVladimir Oltean { 123056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1231bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12322960bb14SVladimir Oltean struct dsa_port *dp; 1233bdeced75SVladimir Oltean 12342960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 12352960bb14SVladimir Oltean felix_del_tag_protocol(ds, dp->index, felix->tag_proto); 12368d5f7954SVladimir Oltean break; 1237adb3dccfSVladimir Oltean } 1238adb3dccfSVladimir Oltean 12392960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 12402960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 1241d19741b0SVladimir Oltean 124249f885b2SVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 124349f885b2SVladimir Oltean ocelot_deinit_timestamp(ocelot); 124449f885b2SVladimir Oltean ocelot_deinit(ocelot); 124549f885b2SVladimir Oltean 1246d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1247d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 124856051948SVladimir Oltean } 124956051948SVladimir Oltean 1250c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1251c0bcf537SYangbo Lu struct ifreq *ifr) 1252c0bcf537SYangbo Lu { 1253c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1254c0bcf537SYangbo Lu 1255c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1256c0bcf537SYangbo Lu } 1257c0bcf537SYangbo Lu 1258c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1259c0bcf537SYangbo Lu struct ifreq *ifr) 1260c0bcf537SYangbo Lu { 1261c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 126299348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 126399348004SVladimir Oltean bool using_tag_8021q; 126499348004SVladimir Oltean int err; 1265c0bcf537SYangbo Lu 126699348004SVladimir Oltean err = ocelot_hwstamp_set(ocelot, port, ifr); 126799348004SVladimir Oltean if (err) 126899348004SVladimir Oltean return err; 126999348004SVladimir Oltean 127099348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 127199348004SVladimir Oltean 127299348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 1273c0bcf537SYangbo Lu } 1274c0bcf537SYangbo Lu 12750a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type) 12760a6f17c6SVladimir Oltean { 12770a6f17c6SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12780a6f17c6SVladimir Oltean int err, grp = 0; 12790a6f17c6SVladimir Oltean 12800a6f17c6SVladimir Oltean if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 12810a6f17c6SVladimir Oltean return false; 12820a6f17c6SVladimir Oltean 12830a6f17c6SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 12840a6f17c6SVladimir Oltean return false; 12850a6f17c6SVladimir Oltean 12860a6f17c6SVladimir Oltean if (ptp_type == PTP_CLASS_NONE) 12870a6f17c6SVladimir Oltean return false; 12880a6f17c6SVladimir Oltean 12890a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 12900a6f17c6SVladimir Oltean struct sk_buff *skb; 12910a6f17c6SVladimir Oltean unsigned int type; 12920a6f17c6SVladimir Oltean 12930a6f17c6SVladimir Oltean err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 12940a6f17c6SVladimir Oltean if (err) 12950a6f17c6SVladimir Oltean goto out; 12960a6f17c6SVladimir Oltean 12970a6f17c6SVladimir Oltean /* We trap to the CPU port module all PTP frames, but 12980a6f17c6SVladimir Oltean * felix_rxtstamp() only gets called for event frames. 12990a6f17c6SVladimir Oltean * So we need to avoid sending duplicate general 13000a6f17c6SVladimir Oltean * message frames by running a second BPF classifier 13010a6f17c6SVladimir Oltean * here and dropping those. 13020a6f17c6SVladimir Oltean */ 13030a6f17c6SVladimir Oltean __skb_push(skb, ETH_HLEN); 13040a6f17c6SVladimir Oltean 13050a6f17c6SVladimir Oltean type = ptp_classify_raw(skb); 13060a6f17c6SVladimir Oltean 13070a6f17c6SVladimir Oltean __skb_pull(skb, ETH_HLEN); 13080a6f17c6SVladimir Oltean 13090a6f17c6SVladimir Oltean if (type == PTP_CLASS_NONE) { 13100a6f17c6SVladimir Oltean kfree_skb(skb); 13110a6f17c6SVladimir Oltean continue; 13120a6f17c6SVladimir Oltean } 13130a6f17c6SVladimir Oltean 13140a6f17c6SVladimir Oltean netif_rx(skb); 13150a6f17c6SVladimir Oltean } 13160a6f17c6SVladimir Oltean 13170a6f17c6SVladimir Oltean out: 13180a6f17c6SVladimir Oltean if (err < 0) 13190a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 13200a6f17c6SVladimir Oltean 13210a6f17c6SVladimir Oltean return true; 13220a6f17c6SVladimir Oltean } 13230a6f17c6SVladimir Oltean 1324c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1325c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1326c0bcf537SYangbo Lu { 132792f62485SVladimir Oltean u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1328c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1329c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1330c0bcf537SYangbo Lu struct timespec64 ts; 133192f62485SVladimir Oltean u32 tstamp_hi; 133292f62485SVladimir Oltean u64 tstamp; 1333c0bcf537SYangbo Lu 13340a6f17c6SVladimir Oltean /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 13350a6f17c6SVladimir Oltean * for RX timestamping. Then free it, and poll for its copy through 13360a6f17c6SVladimir Oltean * MMIO in the CPU port module, and inject that into the stack from 13370a6f17c6SVladimir Oltean * ocelot_xtr_poll(). 13380a6f17c6SVladimir Oltean */ 13390a6f17c6SVladimir Oltean if (felix_check_xtr_pkt(ocelot, type)) { 13400a6f17c6SVladimir Oltean kfree_skb(skb); 13410a6f17c6SVladimir Oltean return true; 13420a6f17c6SVladimir Oltean } 13430a6f17c6SVladimir Oltean 1344c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1345c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1346c0bcf537SYangbo Lu 1347c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1348c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1349c0bcf537SYangbo Lu tstamp_hi--; 1350c0bcf537SYangbo Lu 1351c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1352c0bcf537SYangbo Lu 1353c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1354c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1355c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1356c0bcf537SYangbo Lu return false; 1357c0bcf537SYangbo Lu } 1358c0bcf537SYangbo Lu 13595c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port, 13605c5416f5SYangbo Lu struct sk_buff *skb) 1361c0bcf537SYangbo Lu { 1362c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1363682eaad9SYangbo Lu struct sk_buff *clone = NULL; 1364c0bcf537SYangbo Lu 1365682eaad9SYangbo Lu if (!ocelot->ptp) 13665c5416f5SYangbo Lu return; 1367c0bcf537SYangbo Lu 136852849bcfSVladimir Oltean if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 136952849bcfSVladimir Oltean dev_err_ratelimited(ds->dev, 137052849bcfSVladimir Oltean "port %d delivering skb without TX timestamp\n", 137152849bcfSVladimir Oltean port); 1372682eaad9SYangbo Lu return; 137352849bcfSVladimir Oltean } 1374682eaad9SYangbo Lu 1375682eaad9SYangbo Lu if (clone) 1376c4b364ceSYangbo Lu OCELOT_SKB_CB(skb)->clone = clone; 13775c5416f5SYangbo Lu } 1378c0bcf537SYangbo Lu 13790b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 13800b912fc9SVladimir Oltean { 13810b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13820b912fc9SVladimir Oltean 13830b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 13840b912fc9SVladimir Oltean 13850b912fc9SVladimir Oltean return 0; 13860b912fc9SVladimir Oltean } 13870b912fc9SVladimir Oltean 13880b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 13890b912fc9SVladimir Oltean { 13900b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13910b912fc9SVladimir Oltean 13920b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 13930b912fc9SVladimir Oltean } 13940b912fc9SVladimir Oltean 139507d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 139607d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 139707d985eeSVladimir Oltean { 139807d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 139999348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 140099348004SVladimir Oltean bool using_tag_8021q; 140199348004SVladimir Oltean int err; 140207d985eeSVladimir Oltean 140399348004SVladimir Oltean err = ocelot_cls_flower_replace(ocelot, port, cls, ingress); 140499348004SVladimir Oltean if (err) 140599348004SVladimir Oltean return err; 140699348004SVladimir Oltean 140799348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 140899348004SVladimir Oltean 140999348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 141007d985eeSVladimir Oltean } 141107d985eeSVladimir Oltean 141207d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 141307d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 141407d985eeSVladimir Oltean { 141507d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 141607d985eeSVladimir Oltean 141707d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 141807d985eeSVladimir Oltean } 141907d985eeSVladimir Oltean 142007d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 142107d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 142207d985eeSVladimir Oltean { 142307d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 142407d985eeSVladimir Oltean 142507d985eeSVladimir Oltean return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 142607d985eeSVladimir Oltean } 142707d985eeSVladimir Oltean 1428fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1429fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1430fc411eaaSVladimir Oltean { 1431fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1432fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1433fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 14345f035af7SPo Liu .burst = policer->burst, 1435fc411eaaSVladimir Oltean }; 1436fc411eaaSVladimir Oltean 1437fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1438fc411eaaSVladimir Oltean } 1439fc411eaaSVladimir Oltean 1440fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1441fc411eaaSVladimir Oltean { 1442fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1443fc411eaaSVladimir Oltean 1444fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1445fc411eaaSVladimir Oltean } 1446fc411eaaSVladimir Oltean 1447de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1448de143c0eSXiaoliang Yang enum tc_setup_type type, 1449de143c0eSXiaoliang Yang void *type_data) 1450de143c0eSXiaoliang Yang { 1451de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1452de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1453de143c0eSXiaoliang Yang 1454de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1455de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1456de143c0eSXiaoliang Yang else 1457de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1458de143c0eSXiaoliang Yang } 1459de143c0eSXiaoliang Yang 1460f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1461f59fd9caSVladimir Oltean u16 pool_index, 1462f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1463f59fd9caSVladimir Oltean { 1464f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1465f59fd9caSVladimir Oltean 1466f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1467f59fd9caSVladimir Oltean } 1468f59fd9caSVladimir Oltean 1469f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1470f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1471f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1472f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1473f59fd9caSVladimir Oltean { 1474f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1475f59fd9caSVladimir Oltean 1476f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1477f59fd9caSVladimir Oltean threshold_type, extack); 1478f59fd9caSVladimir Oltean } 1479f59fd9caSVladimir Oltean 1480f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1481f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1482f59fd9caSVladimir Oltean u32 *p_threshold) 1483f59fd9caSVladimir Oltean { 1484f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1485f59fd9caSVladimir Oltean 1486f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1487f59fd9caSVladimir Oltean p_threshold); 1488f59fd9caSVladimir Oltean } 1489f59fd9caSVladimir Oltean 1490f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1491f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1492f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1493f59fd9caSVladimir Oltean { 1494f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1495f59fd9caSVladimir Oltean 1496f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1497f59fd9caSVladimir Oltean threshold, extack); 1498f59fd9caSVladimir Oltean } 1499f59fd9caSVladimir Oltean 1500f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1501f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1502f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1503f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1504f59fd9caSVladimir Oltean { 1505f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1506f59fd9caSVladimir Oltean 1507f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1508f59fd9caSVladimir Oltean pool_type, p_pool_index, 1509f59fd9caSVladimir Oltean p_threshold); 1510f59fd9caSVladimir Oltean } 1511f59fd9caSVladimir Oltean 1512f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1513f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1514f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1515f59fd9caSVladimir Oltean u16 pool_index, u32 threshold, 1516f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1517f59fd9caSVladimir Oltean { 1518f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1519f59fd9caSVladimir Oltean 1520f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1521f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1522f59fd9caSVladimir Oltean extack); 1523f59fd9caSVladimir Oltean } 1524f59fd9caSVladimir Oltean 1525f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1526f59fd9caSVladimir Oltean unsigned int sb_index) 1527f59fd9caSVladimir Oltean { 1528f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1529f59fd9caSVladimir Oltean 1530f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1531f59fd9caSVladimir Oltean } 1532f59fd9caSVladimir Oltean 1533f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1534f59fd9caSVladimir Oltean unsigned int sb_index) 1535f59fd9caSVladimir Oltean { 1536f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1537f59fd9caSVladimir Oltean 1538f59fd9caSVladimir Oltean return ocelot_sb_occ_max_clear(ocelot, sb_index); 1539f59fd9caSVladimir Oltean } 1540f59fd9caSVladimir Oltean 1541f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1542f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1543f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1544f59fd9caSVladimir Oltean { 1545f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1546f59fd9caSVladimir Oltean 1547f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1548f59fd9caSVladimir Oltean p_cur, p_max); 1549f59fd9caSVladimir Oltean } 1550f59fd9caSVladimir Oltean 1551f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1552f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1553f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1554f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1555f59fd9caSVladimir Oltean { 1556f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1557f59fd9caSVladimir Oltean 1558f59fd9caSVladimir Oltean return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1559f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1560f59fd9caSVladimir Oltean } 1561f59fd9caSVladimir Oltean 1562a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port, 1563a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1564a026c50bSHoratiu Vultur { 1565a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1566a026c50bSHoratiu Vultur 1567a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1568a026c50bSHoratiu Vultur } 1569a026c50bSHoratiu Vultur 1570a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port, 1571a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1572a026c50bSHoratiu Vultur { 1573a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1574a026c50bSHoratiu Vultur 1575a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1576a026c50bSHoratiu Vultur } 1577a026c50bSHoratiu Vultur 1578a026c50bSHoratiu Vultur static int 1579a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1580a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1581a026c50bSHoratiu Vultur { 1582a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1583a026c50bSHoratiu Vultur 1584a026c50bSHoratiu Vultur return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1585a026c50bSHoratiu Vultur } 1586a026c50bSHoratiu Vultur 1587a026c50bSHoratiu Vultur static int 1588a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1589a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1590a026c50bSHoratiu Vultur { 1591a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1592a026c50bSHoratiu Vultur 1593a026c50bSHoratiu Vultur return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1594a026c50bSHoratiu Vultur } 1595a026c50bSHoratiu Vultur 1596375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 159756051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1598adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 159935d97680SVladimir Oltean .connect_tag_protocol = felix_connect_tag_protocol, 160056051948SVladimir Oltean .setup = felix_setup, 160156051948SVladimir Oltean .teardown = felix_teardown, 160256051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 160356051948SVladimir Oltean .get_strings = felix_get_strings, 160456051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 160556051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 160656051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 160779fda660SRussell King (Oracle) .phylink_get_caps = felix_phylink_get_caps, 1608bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1609864ba485SRussell King (Oracle) .phylink_mac_select_pcs = felix_phylink_mac_select_pcs, 1610bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1611bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 16125cad43a5SVladimir Oltean .port_fast_age = felix_port_fast_age, 161356051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 161456051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 161556051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1616961d8b69SVladimir Oltean .lag_fdb_add = felix_lag_fdb_add, 1617961d8b69SVladimir Oltean .lag_fdb_del = felix_lag_fdb_del, 1618209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1619209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 1620421741eaSVladimir Oltean .port_pre_bridge_flags = felix_pre_bridge_flags, 1621421741eaSVladimir Oltean .port_bridge_flags = felix_bridge_flags, 162256051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 162356051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 16248fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 16258fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 16268fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 162756051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 162856051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 162956051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 163056051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1631c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1632c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1633c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1634c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 16350b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 16360b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1637fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1638fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 163907d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 164007d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 164107d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1642de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1643f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1644f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1645f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1646f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1647f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1648f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1649f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1650f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1651f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1652f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1653a026c50bSHoratiu Vultur .port_mrp_add = felix_mrp_add, 1654a026c50bSHoratiu Vultur .port_mrp_del = felix_mrp_del, 1655a026c50bSHoratiu Vultur .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1656a026c50bSHoratiu Vultur .port_mrp_del_ring_role = felix_mrp_del_ring_role, 16575da11eb4SVladimir Oltean .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 16585da11eb4SVladimir Oltean .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 165956051948SVladimir Oltean }; 1660319e4dd1SVladimir Oltean 1661319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1662319e4dd1SVladimir Oltean { 1663319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1664319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1665319e4dd1SVladimir Oltean 1666319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1667319e4dd1SVladimir Oltean return NULL; 1668319e4dd1SVladimir Oltean 1669319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1670319e4dd1SVladimir Oltean } 1671319e4dd1SVladimir Oltean 1672319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1673319e4dd1SVladimir Oltean { 1674319e4dd1SVladimir Oltean struct dsa_port *dp; 1675319e4dd1SVladimir Oltean 1676319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1677319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1678319e4dd1SVladimir Oltean return -EINVAL; 1679319e4dd1SVladimir Oltean 1680319e4dd1SVladimir Oltean return dp->index; 1681319e4dd1SVladimir Oltean } 1682