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 28*04b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that 29*04b67e18SVladimir Oltean * the tagger can perform RX source port identification. 30*04b67e18SVladimir Oltean */ 31*04b67e18SVladimir 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 69*04b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid) 70*04b67e18SVladimir Oltean { 71*04b67e18SVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 72*04b67e18SVladimir Oltean struct ocelot_vcap_block *block_vcap_es0; 73*04b67e18SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 74*04b67e18SVladimir Oltean 75*04b67e18SVladimir Oltean block_vcap_es0 = &ocelot->block[VCAP_ES0]; 76*04b67e18SVladimir Oltean 77*04b67e18SVladimir Oltean outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 78*04b67e18SVladimir Oltean port, false); 79*04b67e18SVladimir Oltean if (!outer_tagging_rule) 80*04b67e18SVladimir Oltean return -ENOENT; 81*04b67e18SVladimir Oltean 82*04b67e18SVladimir Oltean return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 83*04b67e18SVladimir Oltean } 84*04b67e18SVladimir Oltean 85*04b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2 86*04b67e18SVladimir Oltean * rules for steering those tagged packets towards the correct destination port 87*04b67e18SVladimir Oltean */ 88*04b67e18SVladimir 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 151*04b67e18SVladimir 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) 174*04b67e18SVladimir Oltean return -ENOENT; 175e21268efSVladimir Oltean 176e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 177e21268efSVladimir Oltean } 178e21268efSVladimir Oltean 179*04b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 180*04b67e18SVladimir Oltean u16 flags) 181*04b67e18SVladimir Oltean { 182*04b67e18SVladimir Oltean struct ocelot *ocelot = ds->priv; 183*04b67e18SVladimir Oltean int err; 184*04b67e18SVladimir Oltean 185*04b67e18SVladimir Oltean /* tag_8021q.c assumes we are implementing this via port VLAN 186*04b67e18SVladimir Oltean * membership, which we aren't. So we don't need to add any VCAP filter 187*04b67e18SVladimir Oltean * for the CPU port. 188*04b67e18SVladimir Oltean */ 189*04b67e18SVladimir Oltean if (!dsa_is_user_port(ds, port)) 190*04b67e18SVladimir Oltean return 0; 191*04b67e18SVladimir Oltean 192*04b67e18SVladimir Oltean err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid); 193*04b67e18SVladimir Oltean if (err) 194*04b67e18SVladimir Oltean return err; 195*04b67e18SVladimir Oltean 196*04b67e18SVladimir Oltean err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid); 197*04b67e18SVladimir Oltean if (err) { 198*04b67e18SVladimir Oltean felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid); 199*04b67e18SVladimir Oltean return err; 200*04b67e18SVladimir Oltean } 201*04b67e18SVladimir Oltean 202*04b67e18SVladimir Oltean return 0; 203*04b67e18SVladimir Oltean } 204*04b67e18SVladimir 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; 208*04b67e18SVladimir Oltean int err; 209e21268efSVladimir Oltean 210*04b67e18SVladimir Oltean if (!dsa_is_user_port(ds, port)) 211*04b67e18SVladimir Oltean return 0; 212e21268efSVladimir Oltean 213*04b67e18SVladimir Oltean err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid); 214*04b67e18SVladimir Oltean if (err) 215*04b67e18SVladimir Oltean return err; 216*04b67e18SVladimir Oltean 217*04b67e18SVladimir Oltean err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid); 218*04b67e18SVladimir Oltean if (err) { 219*04b67e18SVladimir Oltean felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid); 220*04b67e18SVladimir Oltean return err; 221*04b67e18SVladimir 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, 59556051948SVladimir Oltean const unsigned char *addr, u16 vid) 59656051948SVladimir Oltean { 59756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 59856051948SVladimir Oltean 59987b0f983SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid); 60056051948SVladimir Oltean } 60156051948SVladimir Oltean 60256051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port, 60356051948SVladimir Oltean const unsigned char *addr, u16 vid) 60456051948SVladimir Oltean { 60556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 60656051948SVladimir Oltean 60756051948SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid); 60856051948SVladimir Oltean } 60956051948SVladimir Oltean 610961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag, 611961d8b69SVladimir Oltean const unsigned char *addr, u16 vid) 612961d8b69SVladimir Oltean { 613961d8b69SVladimir Oltean struct ocelot *ocelot = ds->priv; 614961d8b69SVladimir Oltean 615961d8b69SVladimir Oltean return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid); 616961d8b69SVladimir Oltean } 617961d8b69SVladimir Oltean 618961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag, 619961d8b69SVladimir Oltean const unsigned char *addr, u16 vid) 620961d8b69SVladimir Oltean { 621961d8b69SVladimir Oltean struct ocelot *ocelot = ds->priv; 622961d8b69SVladimir Oltean 623961d8b69SVladimir Oltean return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid); 624961d8b69SVladimir Oltean } 625961d8b69SVladimir Oltean 626a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 627209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 628209edf95SVladimir Oltean { 629209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 630209edf95SVladimir Oltean 631a52b2da7SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb); 632209edf95SVladimir Oltean } 633209edf95SVladimir Oltean 634209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port, 635209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 636209edf95SVladimir Oltean { 637209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 638209edf95SVladimir Oltean 639209edf95SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb); 640209edf95SVladimir Oltean } 641209edf95SVladimir Oltean 64256051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 64356051948SVladimir Oltean u8 state) 64456051948SVladimir Oltean { 64556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 64656051948SVladimir Oltean 64756051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 64856051948SVladimir Oltean } 64956051948SVladimir Oltean 650421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 651421741eaSVladimir Oltean struct switchdev_brport_flags val, 652421741eaSVladimir Oltean struct netlink_ext_ack *extack) 653421741eaSVladimir Oltean { 654421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 655421741eaSVladimir Oltean 656421741eaSVladimir Oltean return ocelot_port_pre_bridge_flags(ocelot, port, val); 657421741eaSVladimir Oltean } 658421741eaSVladimir Oltean 659421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port, 660421741eaSVladimir Oltean struct switchdev_brport_flags val, 661421741eaSVladimir Oltean struct netlink_ext_ack *extack) 662421741eaSVladimir Oltean { 663421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 664421741eaSVladimir Oltean 665421741eaSVladimir Oltean ocelot_port_bridge_flags(ocelot, port, val); 666421741eaSVladimir Oltean 667421741eaSVladimir Oltean return 0; 668421741eaSVladimir Oltean } 669421741eaSVladimir Oltean 67056051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 671b079922bSVladimir Oltean struct dsa_bridge bridge, bool *tx_fwd_offload) 67256051948SVladimir Oltean { 67356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 67456051948SVladimir Oltean 675d3eed0e5SVladimir Oltean ocelot_port_bridge_join(ocelot, port, bridge.dev); 676e4bd44e8SVladimir Oltean 677e4bd44e8SVladimir Oltean return 0; 67856051948SVladimir Oltean } 67956051948SVladimir Oltean 68056051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 681d3eed0e5SVladimir Oltean struct dsa_bridge bridge) 68256051948SVladimir Oltean { 68356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 68456051948SVladimir Oltean 685d3eed0e5SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, bridge.dev); 68656051948SVladimir Oltean } 68756051948SVladimir Oltean 6888fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 689dedd6a00SVladimir Oltean struct dsa_lag lag, 6908fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 6918fe6832eSVladimir Oltean { 6928fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 6938fe6832eSVladimir Oltean 694dedd6a00SVladimir Oltean return ocelot_port_lag_join(ocelot, port, lag.dev, info); 6958fe6832eSVladimir Oltean } 6968fe6832eSVladimir Oltean 6978fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 698dedd6a00SVladimir Oltean struct dsa_lag lag) 6998fe6832eSVladimir Oltean { 7008fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7018fe6832eSVladimir Oltean 702dedd6a00SVladimir Oltean ocelot_port_lag_leave(ocelot, port, lag.dev); 7038fe6832eSVladimir Oltean 7048fe6832eSVladimir Oltean return 0; 7058fe6832eSVladimir Oltean } 7068fe6832eSVladimir Oltean 7078fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 7088fe6832eSVladimir Oltean { 7098fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 7108fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7118fe6832eSVladimir Oltean 7128fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 7138fe6832eSVladimir Oltean 7148fe6832eSVladimir Oltean return 0; 7158fe6832eSVladimir Oltean } 7168fe6832eSVladimir Oltean 71756051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 71801af940eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 71901af940eSVladimir Oltean struct netlink_ext_ack *extack) 72056051948SVladimir Oltean { 7212f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 722b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 7232f0402feSVladimir Oltean 7249a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 7259a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 7269a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 7279a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 7289a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 7299a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 7309a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 7319a720680SVladimir Oltean */ 7329a720680SVladimir Oltean if (port == ocelot->npi) 7339a720680SVladimir Oltean return 0; 7349a720680SVladimir Oltean 735b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 7362f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 73701af940eSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED, 73801af940eSVladimir Oltean extack); 73956051948SVladimir Oltean } 74056051948SVladimir Oltean 74189153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 74289153ed6SVladimir Oltean struct netlink_ext_ack *extack) 74356051948SVladimir Oltean { 74456051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 74556051948SVladimir Oltean 7463b95d1b2SVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 74756051948SVladimir Oltean } 74856051948SVladimir Oltean 7491958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 75031046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 75131046a5fSVladimir Oltean struct netlink_ext_ack *extack) 75256051948SVladimir Oltean { 75356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 754183be6f9SVladimir Oltean u16 flags = vlan->flags; 7551958d581SVladimir Oltean int err; 75656051948SVladimir Oltean 75701af940eSVladimir Oltean err = felix_vlan_prepare(ds, port, vlan, extack); 7581958d581SVladimir Oltean if (err) 7591958d581SVladimir Oltean return err; 7601958d581SVladimir Oltean 7611958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 762183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 763183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 76456051948SVladimir Oltean } 76556051948SVladimir Oltean 76656051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 76756051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 76856051948SVladimir Oltean { 76956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 77056051948SVladimir Oltean 771b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 77256051948SVladimir Oltean } 77356051948SVladimir Oltean 77479fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port, 77579fda660SRussell King (Oracle) struct phylink_config *config) 77679fda660SRussell King (Oracle) { 77779fda660SRussell King (Oracle) struct ocelot *ocelot = ds->priv; 77879fda660SRussell King (Oracle) 779f6f04c02SRussell King (Oracle) /* This driver does not make use of the speed, duplex, pause or the 780f6f04c02SRussell King (Oracle) * advertisement in its mac_config, so it is safe to mark this driver 781f6f04c02SRussell King (Oracle) * as non-legacy. 782f6f04c02SRussell King (Oracle) */ 783f6f04c02SRussell King (Oracle) config->legacy_pre_march2020 = false; 784f6f04c02SRussell King (Oracle) 78579fda660SRussell King (Oracle) __set_bit(ocelot->ports[port]->phy_mode, 78679fda660SRussell King (Oracle) config->supported_interfaces); 78779fda660SRussell King (Oracle) } 78879fda660SRussell King (Oracle) 789bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port, 790bdeced75SVladimir Oltean unsigned long *supported, 791bdeced75SVladimir Oltean struct phylink_link_state *state) 792bdeced75SVladimir Oltean { 793bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 794375e1314SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 795bdeced75SVladimir Oltean 796375e1314SVladimir Oltean if (felix->info->phylink_validate) 797375e1314SVladimir Oltean felix->info->phylink_validate(ocelot, port, supported, state); 798bdeced75SVladimir Oltean } 799bdeced75SVladimir Oltean 800864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds, 801864ba485SRussell King (Oracle) int port, 802864ba485SRussell King (Oracle) phy_interface_t iface) 803bdeced75SVladimir Oltean { 804bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 805bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 806864ba485SRussell King (Oracle) struct phylink_pcs *pcs = NULL; 807bdeced75SVladimir Oltean 80849af6a76SColin Foster if (felix->pcs && felix->pcs[port]) 809864ba485SRussell King (Oracle) pcs = felix->pcs[port]; 810864ba485SRussell King (Oracle) 811864ba485SRussell King (Oracle) return pcs; 812bdeced75SVladimir Oltean } 813bdeced75SVladimir Oltean 814bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 815bdeced75SVladimir Oltean unsigned int link_an_mode, 816bdeced75SVladimir Oltean phy_interface_t interface) 817bdeced75SVladimir Oltean { 818bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 819bdeced75SVladimir Oltean 820e6e12df6SVladimir Oltean ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 821e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 822bdeced75SVladimir Oltean } 823bdeced75SVladimir Oltean 824bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 825bdeced75SVladimir Oltean unsigned int link_an_mode, 826bdeced75SVladimir Oltean phy_interface_t interface, 8275b502a7bSRussell King struct phy_device *phydev, 8285b502a7bSRussell King int speed, int duplex, 8295b502a7bSRussell King bool tx_pause, bool rx_pause) 830bdeced75SVladimir Oltean { 831bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 8327e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 833bdeced75SVladimir Oltean 834e6e12df6SVladimir Oltean ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 835e6e12df6SVladimir Oltean interface, speed, duplex, tx_pause, rx_pause, 836e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 8377e14a2dcSVladimir Oltean 8387e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 8397e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 840bdeced75SVladimir Oltean } 841bdeced75SVladimir Oltean 842bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 843bd2b3161SXiaoliang Yang { 844bd2b3161SXiaoliang Yang int i; 845bd2b3161SXiaoliang Yang 846bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 847bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 848bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 849bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 850bd2b3161SXiaoliang Yang port); 851bd2b3161SXiaoliang Yang 85270d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 853bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 854bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 855bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 856bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 857bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 858bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 859bd2b3161SXiaoliang Yang port, i); 860bd2b3161SXiaoliang Yang } 861bd2b3161SXiaoliang Yang } 862bd2b3161SXiaoliang Yang 86356051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 86456051948SVladimir Oltean u32 stringset, u8 *data) 86556051948SVladimir Oltean { 86656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 86756051948SVladimir Oltean 86856051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 86956051948SVladimir Oltean } 87056051948SVladimir Oltean 87156051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 87256051948SVladimir Oltean { 87356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 87456051948SVladimir Oltean 87556051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 87656051948SVladimir Oltean } 87756051948SVladimir Oltean 87856051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 87956051948SVladimir Oltean { 88056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 88156051948SVladimir Oltean 88256051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 88356051948SVladimir Oltean } 88456051948SVladimir Oltean 88556051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 88656051948SVladimir Oltean struct ethtool_ts_info *info) 88756051948SVladimir Oltean { 88856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 88956051948SVladimir Oltean 89056051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 89156051948SVladimir Oltean } 89256051948SVladimir Oltean 893bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 894bdeced75SVladimir Oltean struct device_node *ports_node, 895bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 896bdeced75SVladimir Oltean { 897bdeced75SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 898bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 899bdeced75SVladimir Oltean struct device_node *child; 900bdeced75SVladimir Oltean 90137fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 902bdeced75SVladimir Oltean phy_interface_t phy_mode; 903bdeced75SVladimir Oltean u32 port; 904bdeced75SVladimir Oltean int err; 905bdeced75SVladimir Oltean 906bdeced75SVladimir Oltean /* Get switch port number from DT */ 907bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 908bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 909bdeced75SVladimir Oltean "(property \"reg\")\n"); 910bdeced75SVladimir Oltean of_node_put(child); 911bdeced75SVladimir Oltean return -ENODEV; 912bdeced75SVladimir Oltean } 913bdeced75SVladimir Oltean 914bdeced75SVladimir Oltean /* Get PHY mode from DT */ 915bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 916bdeced75SVladimir Oltean if (err) { 917bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 918bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 919bdeced75SVladimir Oltean port); 920bdeced75SVladimir Oltean of_node_put(child); 921bdeced75SVladimir Oltean return -ENODEV; 922bdeced75SVladimir Oltean } 923bdeced75SVladimir Oltean 924bdeced75SVladimir Oltean err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 925bdeced75SVladimir Oltean if (err < 0) { 926bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 927bdeced75SVladimir Oltean phy_modes(phy_mode), port); 92859ebb430SSumera Priyadarsini of_node_put(child); 929bdeced75SVladimir Oltean return err; 930bdeced75SVladimir Oltean } 931bdeced75SVladimir Oltean 932bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 933bdeced75SVladimir Oltean } 934bdeced75SVladimir Oltean 935bdeced75SVladimir Oltean return 0; 936bdeced75SVladimir Oltean } 937bdeced75SVladimir Oltean 938bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 939bdeced75SVladimir Oltean { 940bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 941bdeced75SVladimir Oltean struct device_node *switch_node; 942bdeced75SVladimir Oltean struct device_node *ports_node; 943bdeced75SVladimir Oltean int err; 944bdeced75SVladimir Oltean 945bdeced75SVladimir Oltean switch_node = dev->of_node; 946bdeced75SVladimir Oltean 947bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 948abecbfcdSVladimir Oltean if (!ports_node) 949abecbfcdSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 950bdeced75SVladimir Oltean if (!ports_node) { 951abecbfcdSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 952bdeced75SVladimir Oltean return -ENODEV; 953bdeced75SVladimir Oltean } 954bdeced75SVladimir Oltean 955bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 956bdeced75SVladimir Oltean of_node_put(ports_node); 957bdeced75SVladimir Oltean 958bdeced75SVladimir Oltean return err; 959bdeced75SVladimir Oltean } 960bdeced75SVladimir Oltean 96156051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 96256051948SVladimir Oltean { 96356051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 964bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 965b4024c9eSClaudiu Manoil struct resource res; 96656051948SVladimir Oltean int port, i, err; 96756051948SVladimir Oltean 96856051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 96956051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 97056051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 97156051948SVladimir Oltean if (!ocelot->ports) 97256051948SVladimir Oltean return -ENOMEM; 97356051948SVladimir Oltean 97456051948SVladimir Oltean ocelot->map = felix->info->map; 97556051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 97656051948SVladimir Oltean ocelot->num_stats = felix->info->num_stats; 97721ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 97807d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 97977043c37SXiaoliang Yang ocelot->vcap_pol.base = felix->info->vcap_pol_base; 98077043c37SXiaoliang Yang ocelot->vcap_pol.max = felix->info->vcap_pol_max; 98177043c37SXiaoliang Yang ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 98277043c37SXiaoliang Yang ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 98356051948SVladimir Oltean ocelot->ops = felix->info->ops; 984cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 985cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 986f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 98756051948SVladimir Oltean 988bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 989bdeced75SVladimir Oltean GFP_KERNEL); 990bdeced75SVladimir Oltean if (!port_phy_modes) 991bdeced75SVladimir Oltean return -ENOMEM; 992bdeced75SVladimir Oltean 993bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 994bdeced75SVladimir Oltean if (err) { 995bdeced75SVladimir Oltean kfree(port_phy_modes); 996bdeced75SVladimir Oltean return err; 997bdeced75SVladimir Oltean } 998bdeced75SVladimir Oltean 99956051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 100056051948SVladimir Oltean struct regmap *target; 100156051948SVladimir Oltean 100256051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 100356051948SVladimir Oltean continue; 100456051948SVladimir Oltean 1005b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 1006b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1007375e1314SVladimir Oltean res.start += felix->switch_base; 1008375e1314SVladimir Oltean res.end += felix->switch_base; 100956051948SVladimir Oltean 1010242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 101156051948SVladimir Oltean if (IS_ERR(target)) { 101256051948SVladimir Oltean dev_err(ocelot->dev, 101356051948SVladimir Oltean "Failed to map device memory space\n"); 1014bdeced75SVladimir Oltean kfree(port_phy_modes); 101556051948SVladimir Oltean return PTR_ERR(target); 101656051948SVladimir Oltean } 101756051948SVladimir Oltean 101856051948SVladimir Oltean ocelot->targets[i] = target; 101956051948SVladimir Oltean } 102056051948SVladimir Oltean 102156051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 102256051948SVladimir Oltean if (err) { 102356051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 1024bdeced75SVladimir Oltean kfree(port_phy_modes); 102556051948SVladimir Oltean return err; 102656051948SVladimir Oltean } 102756051948SVladimir Oltean 102856051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 102956051948SVladimir Oltean struct ocelot_port *ocelot_port; 103091c724cfSVladimir Oltean struct regmap *target; 103156051948SVladimir Oltean 103256051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 103356051948SVladimir Oltean sizeof(struct ocelot_port), 103456051948SVladimir Oltean GFP_KERNEL); 103556051948SVladimir Oltean if (!ocelot_port) { 103656051948SVladimir Oltean dev_err(ocelot->dev, 103756051948SVladimir Oltean "failed to allocate port memory\n"); 1038bdeced75SVladimir Oltean kfree(port_phy_modes); 103956051948SVladimir Oltean return -ENOMEM; 104056051948SVladimir Oltean } 104156051948SVladimir Oltean 1042b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1043b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1044375e1314SVladimir Oltean res.start += felix->switch_base; 1045375e1314SVladimir Oltean res.end += felix->switch_base; 104656051948SVladimir Oltean 1047242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 104891c724cfSVladimir Oltean if (IS_ERR(target)) { 104956051948SVladimir Oltean dev_err(ocelot->dev, 105091c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 105191c724cfSVladimir Oltean port); 1052bdeced75SVladimir Oltean kfree(port_phy_modes); 105391c724cfSVladimir Oltean return PTR_ERR(target); 105456051948SVladimir Oltean } 105556051948SVladimir Oltean 1056bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 105756051948SVladimir Oltean ocelot_port->ocelot = ocelot; 105891c724cfSVladimir Oltean ocelot_port->target = target; 105956051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 106056051948SVladimir Oltean } 106156051948SVladimir Oltean 1062bdeced75SVladimir Oltean kfree(port_phy_modes); 1063bdeced75SVladimir Oltean 1064bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1065bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1066bdeced75SVladimir Oltean if (err < 0) 1067bdeced75SVladimir Oltean return err; 1068bdeced75SVladimir Oltean } 1069bdeced75SVladimir Oltean 107056051948SVladimir Oltean return 0; 107156051948SVladimir Oltean } 107256051948SVladimir Oltean 10731328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 10741328a883SVladimir Oltean struct sk_buff *skb) 10751328a883SVladimir Oltean { 10761328a883SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 10771328a883SVladimir Oltean struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 10781328a883SVladimir Oltean struct sk_buff *skb_match = NULL, *skb_tmp; 10791328a883SVladimir Oltean unsigned long flags; 10801328a883SVladimir Oltean 10811328a883SVladimir Oltean if (!clone) 10821328a883SVladimir Oltean return; 10831328a883SVladimir Oltean 10841328a883SVladimir Oltean spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 10851328a883SVladimir Oltean 10861328a883SVladimir Oltean skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 10871328a883SVladimir Oltean if (skb != clone) 10881328a883SVladimir Oltean continue; 10891328a883SVladimir Oltean __skb_unlink(skb, &ocelot_port->tx_skbs); 10901328a883SVladimir Oltean skb_match = skb; 10911328a883SVladimir Oltean break; 10921328a883SVladimir Oltean } 10931328a883SVladimir Oltean 10941328a883SVladimir Oltean spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 10951328a883SVladimir Oltean 10961328a883SVladimir Oltean WARN_ONCE(!skb_match, 10971328a883SVladimir Oltean "Could not find skb clone in TX timestamping list\n"); 10981328a883SVladimir Oltean } 10991328a883SVladimir Oltean 110049f885b2SVladimir Oltean #define work_to_xmit_work(w) \ 110149f885b2SVladimir Oltean container_of((w), struct felix_deferred_xmit_work, work) 110249f885b2SVladimir Oltean 110349f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work) 110449f885b2SVladimir Oltean { 110549f885b2SVladimir Oltean struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 110649f885b2SVladimir Oltean struct dsa_switch *ds = xmit_work->dp->ds; 110749f885b2SVladimir Oltean struct sk_buff *skb = xmit_work->skb; 110849f885b2SVladimir Oltean u32 rew_op = ocelot_ptp_rew_op(skb); 110949f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 111049f885b2SVladimir Oltean int port = xmit_work->dp->index; 111149f885b2SVladimir Oltean int retries = 10; 111249f885b2SVladimir Oltean 111349f885b2SVladimir Oltean do { 111449f885b2SVladimir Oltean if (ocelot_can_inject(ocelot, 0)) 111549f885b2SVladimir Oltean break; 111649f885b2SVladimir Oltean 111749f885b2SVladimir Oltean cpu_relax(); 111849f885b2SVladimir Oltean } while (--retries); 111949f885b2SVladimir Oltean 112049f885b2SVladimir Oltean if (!retries) { 112149f885b2SVladimir Oltean dev_err(ocelot->dev, "port %d failed to inject skb\n", 112249f885b2SVladimir Oltean port); 11231328a883SVladimir Oltean ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 112449f885b2SVladimir Oltean kfree_skb(skb); 112549f885b2SVladimir Oltean return; 112649f885b2SVladimir Oltean } 112749f885b2SVladimir Oltean 112849f885b2SVladimir Oltean ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 112949f885b2SVladimir Oltean 113049f885b2SVladimir Oltean consume_skb(skb); 113149f885b2SVladimir Oltean kfree(xmit_work); 113249f885b2SVladimir Oltean } 113349f885b2SVladimir Oltean 113435d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds, 113535d97680SVladimir Oltean enum dsa_tag_protocol proto) 113649f885b2SVladimir Oltean { 113735d97680SVladimir Oltean struct ocelot_8021q_tagger_data *tagger_data; 113849f885b2SVladimir Oltean 113935d97680SVladimir Oltean switch (proto) { 114035d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 114135d97680SVladimir Oltean tagger_data = ocelot_8021q_tagger_data(ds); 114235d97680SVladimir Oltean tagger_data->xmit_work_fn = felix_port_deferred_xmit; 114349f885b2SVladimir Oltean return 0; 114435d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT: 114535d97680SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 114649f885b2SVladimir Oltean return 0; 114735d97680SVladimir Oltean default: 114835d97680SVladimir Oltean return -EPROTONOSUPPORT; 114949f885b2SVladimir Oltean } 115049f885b2SVladimir Oltean } 115149f885b2SVladimir Oltean 115256051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with 115356051948SVladimir Oltean * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 115456051948SVladimir Oltean * us to allocate structures twice (leak memory) and map PCI memory twice 115556051948SVladimir Oltean * (which will not work). 115656051948SVladimir Oltean */ 115756051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds) 115856051948SVladimir Oltean { 115956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 116056051948SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 11612960bb14SVladimir Oltean struct dsa_port *dp; 11622960bb14SVladimir Oltean int err; 116356051948SVladimir Oltean 116456051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 116556051948SVladimir Oltean if (err) 116656051948SVladimir Oltean return err; 116756051948SVladimir Oltean 1168d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1169d1cc0e93SVladimir Oltean if (err) 11706b73b7c9SVladimir Oltean goto out_mdiobus_free; 1171d1cc0e93SVladimir Oltean 11722b49d128SYangbo Lu if (ocelot->ptp) { 11732ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 11742b49d128SYangbo Lu if (err) { 11752b49d128SYangbo Lu dev_err(ocelot->dev, 11762b49d128SYangbo Lu "Timestamp initialization failed\n"); 11772b49d128SYangbo Lu ocelot->ptp = 0; 11782b49d128SYangbo Lu } 11792b49d128SYangbo Lu } 118056051948SVladimir Oltean 11812960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 11822960bb14SVladimir Oltean ocelot_init_port(ocelot, dp->index); 1183bd2b3161SXiaoliang Yang 1184bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1185bd2b3161SXiaoliang Yang * bits of vlan tag. 1186bd2b3161SXiaoliang Yang */ 11872960bb14SVladimir Oltean felix_port_qos_map_init(ocelot, dp->index); 118856051948SVladimir Oltean } 118956051948SVladimir Oltean 1190f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1191f59fd9caSVladimir Oltean if (err) 11926b73b7c9SVladimir Oltean goto out_deinit_ports; 1193f59fd9caSVladimir Oltean 11942960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 1195adb3dccfSVladimir Oltean /* The initial tag protocol is NPI which always returns 0, so 1196adb3dccfSVladimir Oltean * there's no real point in checking for errors. 11971cf3299bSVladimir Oltean */ 11982960bb14SVladimir Oltean felix_set_tag_protocol(ds, dp->index, felix->tag_proto); 11998d5f7954SVladimir Oltean break; 1200adb3dccfSVladimir Oltean } 12011cf3299bSVladimir Oltean 12020b912fc9SVladimir Oltean ds->mtu_enforcement_ingress = true; 1203c54913c1SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 1204bdeced75SVladimir Oltean 120556051948SVladimir Oltean return 0; 12066b73b7c9SVladimir Oltean 12076b73b7c9SVladimir Oltean out_deinit_ports: 12082960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 12092960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 12106b73b7c9SVladimir Oltean 12116b73b7c9SVladimir Oltean ocelot_deinit_timestamp(ocelot); 12126b73b7c9SVladimir Oltean ocelot_deinit(ocelot); 12136b73b7c9SVladimir Oltean 12146b73b7c9SVladimir Oltean out_mdiobus_free: 12156b73b7c9SVladimir Oltean if (felix->info->mdio_bus_free) 12166b73b7c9SVladimir Oltean felix->info->mdio_bus_free(ocelot); 12176b73b7c9SVladimir Oltean 12186b73b7c9SVladimir Oltean return err; 121956051948SVladimir Oltean } 122056051948SVladimir Oltean 122156051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 122256051948SVladimir Oltean { 122356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1224bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12252960bb14SVladimir Oltean struct dsa_port *dp; 1226bdeced75SVladimir Oltean 12272960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 12282960bb14SVladimir Oltean felix_del_tag_protocol(ds, dp->index, felix->tag_proto); 12298d5f7954SVladimir Oltean break; 1230adb3dccfSVladimir Oltean } 1231adb3dccfSVladimir Oltean 12322960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 12332960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 1234d19741b0SVladimir Oltean 123549f885b2SVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 123649f885b2SVladimir Oltean ocelot_deinit_timestamp(ocelot); 123749f885b2SVladimir Oltean ocelot_deinit(ocelot); 123849f885b2SVladimir Oltean 1239d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1240d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 124156051948SVladimir Oltean } 124256051948SVladimir Oltean 1243c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1244c0bcf537SYangbo Lu struct ifreq *ifr) 1245c0bcf537SYangbo Lu { 1246c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1247c0bcf537SYangbo Lu 1248c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1249c0bcf537SYangbo Lu } 1250c0bcf537SYangbo Lu 1251c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1252c0bcf537SYangbo Lu struct ifreq *ifr) 1253c0bcf537SYangbo Lu { 1254c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 125599348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 125699348004SVladimir Oltean bool using_tag_8021q; 125799348004SVladimir Oltean int err; 1258c0bcf537SYangbo Lu 125999348004SVladimir Oltean err = ocelot_hwstamp_set(ocelot, port, ifr); 126099348004SVladimir Oltean if (err) 126199348004SVladimir Oltean return err; 126299348004SVladimir Oltean 126399348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 126499348004SVladimir Oltean 126599348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 1266c0bcf537SYangbo Lu } 1267c0bcf537SYangbo Lu 12680a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type) 12690a6f17c6SVladimir Oltean { 12700a6f17c6SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12710a6f17c6SVladimir Oltean int err, grp = 0; 12720a6f17c6SVladimir Oltean 12730a6f17c6SVladimir Oltean if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 12740a6f17c6SVladimir Oltean return false; 12750a6f17c6SVladimir Oltean 12760a6f17c6SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 12770a6f17c6SVladimir Oltean return false; 12780a6f17c6SVladimir Oltean 12790a6f17c6SVladimir Oltean if (ptp_type == PTP_CLASS_NONE) 12800a6f17c6SVladimir Oltean return false; 12810a6f17c6SVladimir Oltean 12820a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 12830a6f17c6SVladimir Oltean struct sk_buff *skb; 12840a6f17c6SVladimir Oltean unsigned int type; 12850a6f17c6SVladimir Oltean 12860a6f17c6SVladimir Oltean err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 12870a6f17c6SVladimir Oltean if (err) 12880a6f17c6SVladimir Oltean goto out; 12890a6f17c6SVladimir Oltean 12900a6f17c6SVladimir Oltean /* We trap to the CPU port module all PTP frames, but 12910a6f17c6SVladimir Oltean * felix_rxtstamp() only gets called for event frames. 12920a6f17c6SVladimir Oltean * So we need to avoid sending duplicate general 12930a6f17c6SVladimir Oltean * message frames by running a second BPF classifier 12940a6f17c6SVladimir Oltean * here and dropping those. 12950a6f17c6SVladimir Oltean */ 12960a6f17c6SVladimir Oltean __skb_push(skb, ETH_HLEN); 12970a6f17c6SVladimir Oltean 12980a6f17c6SVladimir Oltean type = ptp_classify_raw(skb); 12990a6f17c6SVladimir Oltean 13000a6f17c6SVladimir Oltean __skb_pull(skb, ETH_HLEN); 13010a6f17c6SVladimir Oltean 13020a6f17c6SVladimir Oltean if (type == PTP_CLASS_NONE) { 13030a6f17c6SVladimir Oltean kfree_skb(skb); 13040a6f17c6SVladimir Oltean continue; 13050a6f17c6SVladimir Oltean } 13060a6f17c6SVladimir Oltean 13070a6f17c6SVladimir Oltean netif_rx(skb); 13080a6f17c6SVladimir Oltean } 13090a6f17c6SVladimir Oltean 13100a6f17c6SVladimir Oltean out: 13110a6f17c6SVladimir Oltean if (err < 0) 13120a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 13130a6f17c6SVladimir Oltean 13140a6f17c6SVladimir Oltean return true; 13150a6f17c6SVladimir Oltean } 13160a6f17c6SVladimir Oltean 1317c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1318c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1319c0bcf537SYangbo Lu { 132092f62485SVladimir Oltean u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1321c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1322c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1323c0bcf537SYangbo Lu struct timespec64 ts; 132492f62485SVladimir Oltean u32 tstamp_hi; 132592f62485SVladimir Oltean u64 tstamp; 1326c0bcf537SYangbo Lu 13270a6f17c6SVladimir Oltean /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 13280a6f17c6SVladimir Oltean * for RX timestamping. Then free it, and poll for its copy through 13290a6f17c6SVladimir Oltean * MMIO in the CPU port module, and inject that into the stack from 13300a6f17c6SVladimir Oltean * ocelot_xtr_poll(). 13310a6f17c6SVladimir Oltean */ 13320a6f17c6SVladimir Oltean if (felix_check_xtr_pkt(ocelot, type)) { 13330a6f17c6SVladimir Oltean kfree_skb(skb); 13340a6f17c6SVladimir Oltean return true; 13350a6f17c6SVladimir Oltean } 13360a6f17c6SVladimir Oltean 1337c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1338c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1339c0bcf537SYangbo Lu 1340c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1341c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1342c0bcf537SYangbo Lu tstamp_hi--; 1343c0bcf537SYangbo Lu 1344c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1345c0bcf537SYangbo Lu 1346c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1347c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1348c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1349c0bcf537SYangbo Lu return false; 1350c0bcf537SYangbo Lu } 1351c0bcf537SYangbo Lu 13525c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port, 13535c5416f5SYangbo Lu struct sk_buff *skb) 1354c0bcf537SYangbo Lu { 1355c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1356682eaad9SYangbo Lu struct sk_buff *clone = NULL; 1357c0bcf537SYangbo Lu 1358682eaad9SYangbo Lu if (!ocelot->ptp) 13595c5416f5SYangbo Lu return; 1360c0bcf537SYangbo Lu 136152849bcfSVladimir Oltean if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 136252849bcfSVladimir Oltean dev_err_ratelimited(ds->dev, 136352849bcfSVladimir Oltean "port %d delivering skb without TX timestamp\n", 136452849bcfSVladimir Oltean port); 1365682eaad9SYangbo Lu return; 136652849bcfSVladimir Oltean } 1367682eaad9SYangbo Lu 1368682eaad9SYangbo Lu if (clone) 1369c4b364ceSYangbo Lu OCELOT_SKB_CB(skb)->clone = clone; 13705c5416f5SYangbo Lu } 1371c0bcf537SYangbo Lu 13720b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 13730b912fc9SVladimir Oltean { 13740b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13750b912fc9SVladimir Oltean 13760b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 13770b912fc9SVladimir Oltean 13780b912fc9SVladimir Oltean return 0; 13790b912fc9SVladimir Oltean } 13800b912fc9SVladimir Oltean 13810b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 13820b912fc9SVladimir Oltean { 13830b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13840b912fc9SVladimir Oltean 13850b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 13860b912fc9SVladimir Oltean } 13870b912fc9SVladimir Oltean 138807d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 138907d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 139007d985eeSVladimir Oltean { 139107d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 139299348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 139399348004SVladimir Oltean bool using_tag_8021q; 139499348004SVladimir Oltean int err; 139507d985eeSVladimir Oltean 139699348004SVladimir Oltean err = ocelot_cls_flower_replace(ocelot, port, cls, ingress); 139799348004SVladimir Oltean if (err) 139899348004SVladimir Oltean return err; 139999348004SVladimir Oltean 140099348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 140199348004SVladimir Oltean 140299348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 140307d985eeSVladimir Oltean } 140407d985eeSVladimir Oltean 140507d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 140607d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 140707d985eeSVladimir Oltean { 140807d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 140907d985eeSVladimir Oltean 141007d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 141107d985eeSVladimir Oltean } 141207d985eeSVladimir Oltean 141307d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 141407d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 141507d985eeSVladimir Oltean { 141607d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 141707d985eeSVladimir Oltean 141807d985eeSVladimir Oltean return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 141907d985eeSVladimir Oltean } 142007d985eeSVladimir Oltean 1421fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1422fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1423fc411eaaSVladimir Oltean { 1424fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1425fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1426fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 14275f035af7SPo Liu .burst = policer->burst, 1428fc411eaaSVladimir Oltean }; 1429fc411eaaSVladimir Oltean 1430fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1431fc411eaaSVladimir Oltean } 1432fc411eaaSVladimir Oltean 1433fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1434fc411eaaSVladimir Oltean { 1435fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1436fc411eaaSVladimir Oltean 1437fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1438fc411eaaSVladimir Oltean } 1439fc411eaaSVladimir Oltean 1440de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1441de143c0eSXiaoliang Yang enum tc_setup_type type, 1442de143c0eSXiaoliang Yang void *type_data) 1443de143c0eSXiaoliang Yang { 1444de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1445de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1446de143c0eSXiaoliang Yang 1447de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1448de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1449de143c0eSXiaoliang Yang else 1450de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1451de143c0eSXiaoliang Yang } 1452de143c0eSXiaoliang Yang 1453f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1454f59fd9caSVladimir Oltean u16 pool_index, 1455f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1456f59fd9caSVladimir Oltean { 1457f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1458f59fd9caSVladimir Oltean 1459f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1460f59fd9caSVladimir Oltean } 1461f59fd9caSVladimir Oltean 1462f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1463f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1464f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1465f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1466f59fd9caSVladimir Oltean { 1467f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1468f59fd9caSVladimir Oltean 1469f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1470f59fd9caSVladimir Oltean threshold_type, extack); 1471f59fd9caSVladimir Oltean } 1472f59fd9caSVladimir Oltean 1473f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1474f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1475f59fd9caSVladimir Oltean u32 *p_threshold) 1476f59fd9caSVladimir Oltean { 1477f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1478f59fd9caSVladimir Oltean 1479f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1480f59fd9caSVladimir Oltean p_threshold); 1481f59fd9caSVladimir Oltean } 1482f59fd9caSVladimir Oltean 1483f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1484f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1485f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1486f59fd9caSVladimir Oltean { 1487f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1488f59fd9caSVladimir Oltean 1489f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1490f59fd9caSVladimir Oltean threshold, extack); 1491f59fd9caSVladimir Oltean } 1492f59fd9caSVladimir Oltean 1493f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1494f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1495f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1496f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1497f59fd9caSVladimir Oltean { 1498f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1499f59fd9caSVladimir Oltean 1500f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1501f59fd9caSVladimir Oltean pool_type, p_pool_index, 1502f59fd9caSVladimir Oltean p_threshold); 1503f59fd9caSVladimir Oltean } 1504f59fd9caSVladimir Oltean 1505f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1506f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1507f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1508f59fd9caSVladimir Oltean u16 pool_index, u32 threshold, 1509f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1510f59fd9caSVladimir Oltean { 1511f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1512f59fd9caSVladimir Oltean 1513f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1514f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1515f59fd9caSVladimir Oltean extack); 1516f59fd9caSVladimir Oltean } 1517f59fd9caSVladimir Oltean 1518f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1519f59fd9caSVladimir Oltean unsigned int sb_index) 1520f59fd9caSVladimir Oltean { 1521f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1522f59fd9caSVladimir Oltean 1523f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1524f59fd9caSVladimir Oltean } 1525f59fd9caSVladimir Oltean 1526f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1527f59fd9caSVladimir Oltean unsigned int sb_index) 1528f59fd9caSVladimir Oltean { 1529f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1530f59fd9caSVladimir Oltean 1531f59fd9caSVladimir Oltean return ocelot_sb_occ_max_clear(ocelot, sb_index); 1532f59fd9caSVladimir Oltean } 1533f59fd9caSVladimir Oltean 1534f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1535f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1536f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1537f59fd9caSVladimir Oltean { 1538f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1539f59fd9caSVladimir Oltean 1540f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1541f59fd9caSVladimir Oltean p_cur, p_max); 1542f59fd9caSVladimir Oltean } 1543f59fd9caSVladimir Oltean 1544f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1545f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1546f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1547f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1548f59fd9caSVladimir Oltean { 1549f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1550f59fd9caSVladimir Oltean 1551f59fd9caSVladimir Oltean return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1552f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1553f59fd9caSVladimir Oltean } 1554f59fd9caSVladimir Oltean 1555a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port, 1556a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1557a026c50bSHoratiu Vultur { 1558a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1559a026c50bSHoratiu Vultur 1560a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1561a026c50bSHoratiu Vultur } 1562a026c50bSHoratiu Vultur 1563a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port, 1564a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1565a026c50bSHoratiu Vultur { 1566a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1567a026c50bSHoratiu Vultur 1568a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1569a026c50bSHoratiu Vultur } 1570a026c50bSHoratiu Vultur 1571a026c50bSHoratiu Vultur static int 1572a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1573a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1574a026c50bSHoratiu Vultur { 1575a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1576a026c50bSHoratiu Vultur 1577a026c50bSHoratiu Vultur return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1578a026c50bSHoratiu Vultur } 1579a026c50bSHoratiu Vultur 1580a026c50bSHoratiu Vultur static int 1581a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1582a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1583a026c50bSHoratiu Vultur { 1584a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1585a026c50bSHoratiu Vultur 1586a026c50bSHoratiu Vultur return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1587a026c50bSHoratiu Vultur } 1588a026c50bSHoratiu Vultur 1589375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 159056051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1591adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 159235d97680SVladimir Oltean .connect_tag_protocol = felix_connect_tag_protocol, 159356051948SVladimir Oltean .setup = felix_setup, 159456051948SVladimir Oltean .teardown = felix_teardown, 159556051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 159656051948SVladimir Oltean .get_strings = felix_get_strings, 159756051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 159856051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 159956051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 160079fda660SRussell King (Oracle) .phylink_get_caps = felix_phylink_get_caps, 1601bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1602864ba485SRussell King (Oracle) .phylink_mac_select_pcs = felix_phylink_mac_select_pcs, 1603bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1604bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 16055cad43a5SVladimir Oltean .port_fast_age = felix_port_fast_age, 160656051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 160756051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 160856051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1609961d8b69SVladimir Oltean .lag_fdb_add = felix_lag_fdb_add, 1610961d8b69SVladimir Oltean .lag_fdb_del = felix_lag_fdb_del, 1611209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1612209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 1613421741eaSVladimir Oltean .port_pre_bridge_flags = felix_pre_bridge_flags, 1614421741eaSVladimir Oltean .port_bridge_flags = felix_bridge_flags, 161556051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 161656051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 16178fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 16188fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 16198fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 162056051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 162156051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 162256051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 162356051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1624c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1625c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1626c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1627c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 16280b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 16290b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1630fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1631fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 163207d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 163307d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 163407d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1635de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1636f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1637f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1638f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1639f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1640f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1641f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1642f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1643f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1644f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1645f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1646a026c50bSHoratiu Vultur .port_mrp_add = felix_mrp_add, 1647a026c50bSHoratiu Vultur .port_mrp_del = felix_mrp_del, 1648a026c50bSHoratiu Vultur .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1649a026c50bSHoratiu Vultur .port_mrp_del_ring_role = felix_mrp_del_ring_role, 16505da11eb4SVladimir Oltean .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 16515da11eb4SVladimir Oltean .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 165256051948SVladimir Oltean }; 1653319e4dd1SVladimir Oltean 1654319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1655319e4dd1SVladimir Oltean { 1656319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1657319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1658319e4dd1SVladimir Oltean 1659319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1660319e4dd1SVladimir Oltean return NULL; 1661319e4dd1SVladimir Oltean 1662319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1663319e4dd1SVladimir Oltean } 1664319e4dd1SVladimir Oltean 1665319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1666319e4dd1SVladimir Oltean { 1667319e4dd1SVladimir Oltean struct dsa_port *dp; 1668319e4dd1SVladimir Oltean 1669319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1670319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1671319e4dd1SVladimir Oltean return -EINVAL; 1672319e4dd1SVladimir Oltean 1673319e4dd1SVladimir Oltean return dp->index; 1674319e4dd1SVladimir Oltean } 1675