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 28e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid, 29e21268efSVladimir Oltean bool pvid, bool untagged) 30e21268efSVladimir Oltean { 31e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 32e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 33e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 34e21268efSVladimir Oltean int key_length, upstream, err; 35e21268efSVladimir Oltean 36e21268efSVladimir Oltean key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length; 37e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 38e21268efSVladimir Oltean 39e21268efSVladimir Oltean outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), 40e21268efSVladimir Oltean GFP_KERNEL); 41e21268efSVladimir Oltean if (!outer_tagging_rule) 42e21268efSVladimir Oltean return -ENOMEM; 43e21268efSVladimir Oltean 44e21268efSVladimir Oltean outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 45e21268efSVladimir Oltean outer_tagging_rule->prio = 1; 46c518afecSVladimir Oltean outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port); 47e21268efSVladimir Oltean outer_tagging_rule->id.tc_offload = false; 48e21268efSVladimir Oltean outer_tagging_rule->block_id = VCAP_ES0; 49e21268efSVladimir Oltean outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 50e21268efSVladimir Oltean outer_tagging_rule->lookup = 0; 51e21268efSVladimir Oltean outer_tagging_rule->ingress_port.value = port; 52e21268efSVladimir Oltean outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0); 53e21268efSVladimir Oltean outer_tagging_rule->egress_port.value = upstream; 54e21268efSVladimir Oltean outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0); 55e21268efSVladimir Oltean outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG; 56e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD; 57e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_vid_sel = 1; 58e21268efSVladimir Oltean outer_tagging_rule->action.vid_a_val = vid; 59e21268efSVladimir Oltean 60e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL); 61e21268efSVladimir Oltean if (err) 62e21268efSVladimir Oltean kfree(outer_tagging_rule); 63e21268efSVladimir Oltean 64e21268efSVladimir Oltean return err; 65e21268efSVladimir Oltean } 66e21268efSVladimir Oltean 67e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid, 68e21268efSVladimir Oltean bool pvid, bool untagged) 69e21268efSVladimir Oltean { 70e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 71e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 72e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 73e21268efSVladimir Oltean int upstream, err; 74e21268efSVladimir Oltean 75e21268efSVladimir Oltean /* tag_8021q.c assumes we are implementing this via port VLAN 76e21268efSVladimir Oltean * membership, which we aren't. So we don't need to add any VCAP filter 77e21268efSVladimir Oltean * for the CPU port. 78e21268efSVladimir Oltean */ 79e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 80e21268efSVladimir Oltean return 0; 81e21268efSVladimir Oltean 82e21268efSVladimir Oltean untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 83e21268efSVladimir Oltean if (!untagging_rule) 84e21268efSVladimir Oltean return -ENOMEM; 85e21268efSVladimir Oltean 86e21268efSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 87e21268efSVladimir Oltean if (!redirect_rule) { 88e21268efSVladimir Oltean kfree(untagging_rule); 89e21268efSVladimir Oltean return -ENOMEM; 90e21268efSVladimir Oltean } 91e21268efSVladimir Oltean 92e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 93e21268efSVladimir Oltean 94e21268efSVladimir Oltean untagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 95e21268efSVladimir Oltean untagging_rule->ingress_port_mask = BIT(upstream); 96e21268efSVladimir Oltean untagging_rule->vlan.vid.value = vid; 97e21268efSVladimir Oltean untagging_rule->vlan.vid.mask = VLAN_VID_MASK; 98e21268efSVladimir Oltean untagging_rule->prio = 1; 99c518afecSVladimir Oltean untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port); 100e21268efSVladimir Oltean untagging_rule->id.tc_offload = false; 101e21268efSVladimir Oltean untagging_rule->block_id = VCAP_IS1; 102e21268efSVladimir Oltean untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 103e21268efSVladimir Oltean untagging_rule->lookup = 0; 104e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt_ena = true; 105e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt = 1; 106e21268efSVladimir Oltean untagging_rule->action.pag_override_mask = 0xff; 107e21268efSVladimir Oltean untagging_rule->action.pag_val = port; 108e21268efSVladimir Oltean 109e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL); 110e21268efSVladimir Oltean if (err) { 111e21268efSVladimir Oltean kfree(untagging_rule); 112e21268efSVladimir Oltean kfree(redirect_rule); 113e21268efSVladimir Oltean return err; 114e21268efSVladimir Oltean } 115e21268efSVladimir Oltean 116e21268efSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 117e21268efSVladimir Oltean redirect_rule->ingress_port_mask = BIT(upstream); 118e21268efSVladimir Oltean redirect_rule->pag = port; 119e21268efSVladimir Oltean redirect_rule->prio = 1; 120c518afecSVladimir Oltean redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port); 121e21268efSVladimir Oltean redirect_rule->id.tc_offload = false; 122e21268efSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 123e21268efSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 124e21268efSVladimir Oltean redirect_rule->lookup = 0; 125e21268efSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 126e21268efSVladimir Oltean redirect_rule->action.port_mask = BIT(port); 127e21268efSVladimir Oltean 128e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 129e21268efSVladimir Oltean if (err) { 130e21268efSVladimir Oltean ocelot_vcap_filter_del(ocelot, untagging_rule); 131e21268efSVladimir Oltean kfree(redirect_rule); 132e21268efSVladimir Oltean return err; 133e21268efSVladimir Oltean } 134e21268efSVladimir Oltean 135e21268efSVladimir Oltean return 0; 136e21268efSVladimir Oltean } 137e21268efSVladimir Oltean 138e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 139e21268efSVladimir Oltean u16 flags) 140e21268efSVladimir Oltean { 141e21268efSVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 142e21268efSVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 143e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 144e21268efSVladimir Oltean 145e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 146e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot), 147e21268efSVladimir Oltean port, vid, pvid, untagged); 148e21268efSVladimir Oltean 149e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 150e21268efSVladimir Oltean return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot), 151e21268efSVladimir Oltean port, vid, pvid, untagged); 152e21268efSVladimir Oltean 153e21268efSVladimir Oltean return 0; 154e21268efSVladimir Oltean } 155e21268efSVladimir Oltean 156e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid) 157e21268efSVladimir Oltean { 158e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 159e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_es0; 160e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 161e21268efSVladimir Oltean 162e21268efSVladimir Oltean block_vcap_es0 = &ocelot->block[VCAP_ES0]; 163e21268efSVladimir Oltean 164e21268efSVladimir Oltean outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 165e21268efSVladimir Oltean port, false); 166e21268efSVladimir Oltean if (!outer_tagging_rule) 167*08f44db3SVladimir Oltean return -ENOENT; 168e21268efSVladimir Oltean 169e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 170e21268efSVladimir Oltean } 171e21268efSVladimir Oltean 172e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid) 173e21268efSVladimir Oltean { 174e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 175e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 176e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 177e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 178e21268efSVladimir Oltean int err; 179e21268efSVladimir Oltean 180e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 181e21268efSVladimir Oltean return 0; 182e21268efSVladimir Oltean 183e21268efSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 184e21268efSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 185e21268efSVladimir Oltean 186e21268efSVladimir Oltean untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 187e21268efSVladimir Oltean port, false); 188e21268efSVladimir Oltean if (!untagging_rule) 189*08f44db3SVladimir Oltean return -ENOENT; 190e21268efSVladimir Oltean 191e21268efSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, untagging_rule); 192e21268efSVladimir Oltean if (err) 193e21268efSVladimir Oltean return err; 194e21268efSVladimir Oltean 195e21268efSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 196e21268efSVladimir Oltean port, false); 197e21268efSVladimir Oltean if (!redirect_rule) 198e21268efSVladimir Oltean return 0; 199e21268efSVladimir Oltean 200e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 201e21268efSVladimir Oltean } 202e21268efSVladimir Oltean 203e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 204e21268efSVladimir Oltean { 205e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 206e21268efSVladimir Oltean 207e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 208e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot), 209e21268efSVladimir Oltean port, vid); 210e21268efSVladimir Oltean 211e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 212e21268efSVladimir Oltean return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot), 213e21268efSVladimir Oltean port, vid); 214e21268efSVladimir Oltean 215e21268efSVladimir Oltean return 0; 216e21268efSVladimir Oltean } 217e21268efSVladimir Oltean 218e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC 219e21268efSVladimir Oltean * connected internally to the enetc or fman DSA master can be configured to 220e21268efSVladimir Oltean * use the software-defined tag_8021q frame format. As far as the hardware is 221e21268efSVladimir Oltean * concerned, it thinks it is a "dumb switch" - the queues of the CPU port 222e21268efSVladimir Oltean * module are now disconnected from it, but can still be accessed through 223e21268efSVladimir Oltean * register-based MMIO. 224e21268efSVladimir Oltean */ 225e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port) 226e21268efSVladimir Oltean { 2278abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2288abe1970SVladimir Oltean 229e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = true; 230e21268efSVladimir Oltean ocelot->npi = -1; 231e21268efSVladimir Oltean 232e21268efSVladimir Oltean /* Overwrite PGID_CPU with the non-tagging port */ 233e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU); 234e21268efSVladimir Oltean 2358abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2368abe1970SVladimir Oltean 2378abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 238e21268efSVladimir Oltean } 239e21268efSVladimir Oltean 240e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port) 241e21268efSVladimir Oltean { 2428abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2438abe1970SVladimir Oltean 244e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = false; 245e21268efSVladimir Oltean 246e21268efSVladimir Oltean /* Restore PGID_CPU */ 247e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID, 248e21268efSVladimir Oltean PGID_CPU); 249e21268efSVladimir Oltean 2508abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2518abe1970SVladimir Oltean 2528abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 253e21268efSVladimir Oltean } 254e21268efSVladimir Oltean 25599348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be 25699348004SVladimir Oltean * replicated over Ethernet as well, otherwise we'd get no notification of 25799348004SVladimir Oltean * their arrival when using the ocelot-8021q tagging protocol. 258c8c0ba4fSVladimir Oltean */ 25999348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds, 26099348004SVladimir Oltean bool using_tag_8021q) 261c8c0ba4fSVladimir Oltean { 26299348004SVladimir Oltean struct ocelot *ocelot = ds->priv; 26399348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 26499348004SVladimir Oltean struct ocelot_vcap_filter *trap; 26599348004SVladimir Oltean enum ocelot_mask_mode mask_mode; 26699348004SVladimir Oltean unsigned long port_mask; 2672960bb14SVladimir Oltean struct dsa_port *dp; 26899348004SVladimir Oltean bool cpu_copy_ena; 26999348004SVladimir Oltean int cpu = -1, err; 270c8c0ba4fSVladimir Oltean 27199348004SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 27299348004SVladimir Oltean return 0; 273c8c0ba4fSVladimir Oltean 27499348004SVladimir Oltean /* Figure out the current CPU port */ 2752960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 2762960bb14SVladimir Oltean cpu = dp->index; 2778d5f7954SVladimir Oltean break; 278c8c0ba4fSVladimir Oltean } 2798d5f7954SVladimir Oltean 280d78637a8SVladimir Oltean /* We are sure that "cpu" was found, otherwise 281d78637a8SVladimir Oltean * dsa_tree_setup_default_cpu() would have failed earlier. 282d78637a8SVladimir Oltean */ 283c8c0ba4fSVladimir Oltean 28499348004SVladimir Oltean /* Make sure all traps are set up for that destination */ 28599348004SVladimir Oltean list_for_each_entry(trap, &ocelot->traps, trap_list) { 28699348004SVladimir Oltean /* Figure out the current trapping destination */ 28799348004SVladimir Oltean if (using_tag_8021q) { 28899348004SVladimir Oltean /* Redirect to the tag_8021q CPU port. If timestamps 28999348004SVladimir Oltean * are necessary, also copy trapped packets to the CPU 29099348004SVladimir Oltean * port module. 291c8c0ba4fSVladimir Oltean */ 29299348004SVladimir Oltean mask_mode = OCELOT_MASK_MODE_REDIRECT; 29399348004SVladimir Oltean port_mask = BIT(cpu); 29499348004SVladimir Oltean cpu_copy_ena = !!trap->take_ts; 295c8c0ba4fSVladimir Oltean } else { 29699348004SVladimir Oltean /* Trap packets only to the CPU port module, which is 29799348004SVladimir Oltean * redirected to the NPI port (the DSA CPU port) 298c8c0ba4fSVladimir Oltean */ 29999348004SVladimir Oltean mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 30099348004SVladimir Oltean port_mask = 0; 30199348004SVladimir Oltean cpu_copy_ena = true; 302c8c0ba4fSVladimir Oltean } 303c8c0ba4fSVladimir Oltean 30499348004SVladimir Oltean if (trap->action.mask_mode == mask_mode && 30599348004SVladimir Oltean trap->action.port_mask == port_mask && 30699348004SVladimir Oltean trap->action.cpu_copy_ena == cpu_copy_ena) 30799348004SVladimir Oltean continue; 308c8c0ba4fSVladimir Oltean 30999348004SVladimir Oltean trap->action.mask_mode = mask_mode; 31099348004SVladimir Oltean trap->action.port_mask = port_mask; 31199348004SVladimir Oltean trap->action.cpu_copy_ena = cpu_copy_ena; 3120a6f17c6SVladimir Oltean 31399348004SVladimir Oltean err = ocelot_vcap_filter_replace(ocelot, trap); 314c8c0ba4fSVladimir Oltean if (err) 315c8c0ba4fSVladimir Oltean return err; 31699348004SVladimir Oltean } 317c8c0ba4fSVladimir Oltean 31899348004SVladimir Oltean return 0; 319c8c0ba4fSVladimir Oltean } 320c8c0ba4fSVladimir Oltean 321e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu) 322e21268efSVladimir Oltean { 323e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 324e21268efSVladimir Oltean unsigned long cpu_flood; 3252960bb14SVladimir Oltean struct dsa_port *dp; 3262960bb14SVladimir Oltean int err; 327e21268efSVladimir Oltean 328e21268efSVladimir Oltean felix_8021q_cpu_port_init(ocelot, cpu); 329e21268efSVladimir Oltean 3302960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 331e21268efSVladimir Oltean /* This overwrites ocelot_init(): 332e21268efSVladimir Oltean * Do not forward BPDU frames to the CPU port module, 333e21268efSVladimir Oltean * for 2 reasons: 334e21268efSVladimir Oltean * - When these packets are injected from the tag_8021q 335e21268efSVladimir Oltean * CPU port, we want them to go out, not loop back 336e21268efSVladimir Oltean * into the system. 337e21268efSVladimir Oltean * - STP traffic ingressing on a user port should go to 338e21268efSVladimir Oltean * the tag_8021q CPU port, not to the hardware CPU 339e21268efSVladimir Oltean * port module. 340e21268efSVladimir Oltean */ 341e21268efSVladimir Oltean ocelot_write_gix(ocelot, 342e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 3432960bb14SVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, dp->index); 344e21268efSVladimir Oltean } 345e21268efSVladimir Oltean 346c8c0ba4fSVladimir Oltean /* In tag_8021q mode, the CPU port module is unused, except for PTP 347c8c0ba4fSVladimir Oltean * frames. So we want to disable flooding of any kind to the CPU port 348c8c0ba4fSVladimir Oltean * module, since packets going there will end in a black hole. 349e21268efSVladimir Oltean */ 350e21268efSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 351e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC); 352e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC); 353b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC); 354e21268efSVladimir Oltean 3555da11eb4SVladimir Oltean err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD)); 356e21268efSVladimir Oltean if (err) 357d7b1fd52SVladimir Oltean return err; 358d7b1fd52SVladimir Oltean 35999348004SVladimir Oltean err = felix_update_trapping_destinations(ds, true); 360d7b1fd52SVladimir Oltean if (err) 361d7b1fd52SVladimir Oltean goto out_tag_8021q_unregister; 362e21268efSVladimir Oltean 36399348004SVladimir Oltean /* The ownership of the CPU port module's queues might have just been 36499348004SVladimir Oltean * transferred to the tag_8021q tagger from the NPI-based tagger. 36599348004SVladimir Oltean * So there might still be all sorts of crap in the queues. On the 36699348004SVladimir Oltean * other hand, the MMIO-based matching of PTP frames is very brittle, 36799348004SVladimir Oltean * so we need to be careful that there are no extra frames to be 36899348004SVladimir Oltean * dequeued over MMIO, since we would never know to discard them. 36999348004SVladimir Oltean */ 37099348004SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 37199348004SVladimir Oltean 372e21268efSVladimir Oltean return 0; 373e21268efSVladimir Oltean 374d7b1fd52SVladimir Oltean out_tag_8021q_unregister: 375d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 376e21268efSVladimir Oltean return err; 377e21268efSVladimir Oltean } 378e21268efSVladimir Oltean 379e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu) 380e21268efSVladimir Oltean { 381e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 3822960bb14SVladimir Oltean struct dsa_port *dp; 3832960bb14SVladimir Oltean int err; 384e21268efSVladimir Oltean 38599348004SVladimir Oltean err = felix_update_trapping_destinations(ds, false); 386c8c0ba4fSVladimir Oltean if (err) 387c8c0ba4fSVladimir Oltean dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d", 388c8c0ba4fSVladimir Oltean err); 389c8c0ba4fSVladimir Oltean 390d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 391e21268efSVladimir Oltean 3922960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 393e21268efSVladimir Oltean /* Restore the logic from ocelot_init: 394e21268efSVladimir Oltean * do not forward BPDU frames to the front ports. 395e21268efSVladimir Oltean */ 396e21268efSVladimir Oltean ocelot_write_gix(ocelot, 397e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 398e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, 3992960bb14SVladimir Oltean dp->index); 400e21268efSVladimir Oltean } 401e21268efSVladimir Oltean 402e21268efSVladimir Oltean felix_8021q_cpu_port_deinit(ocelot, cpu); 403e21268efSVladimir Oltean } 404e21268efSVladimir Oltean 405adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This 406adb3dccfSVladimir Oltean * is the mode through which frames can be injected from and extracted to an 407adb3dccfSVladimir Oltean * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 408adb3dccfSVladimir Oltean * running Linux, and this forms a DSA setup together with the enetc or fman 409adb3dccfSVladimir Oltean * DSA master. 410adb3dccfSVladimir Oltean */ 411adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port) 412adb3dccfSVladimir Oltean { 413adb3dccfSVladimir Oltean ocelot->npi = port; 414adb3dccfSVladimir Oltean 415adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 416adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 417adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 418adb3dccfSVladimir Oltean 419adb3dccfSVladimir Oltean /* NPI port Injection/Extraction configuration */ 420adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 421adb3dccfSVladimir Oltean ocelot->npi_xtr_prefix); 422adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 423adb3dccfSVladimir Oltean ocelot->npi_inj_prefix); 424adb3dccfSVladimir Oltean 425adb3dccfSVladimir Oltean /* Disable transmission of pause frames */ 426adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 427adb3dccfSVladimir Oltean } 428adb3dccfSVladimir Oltean 429adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 430adb3dccfSVladimir Oltean { 431adb3dccfSVladimir Oltean /* Restore hardware defaults */ 432adb3dccfSVladimir Oltean int unused_port = ocelot->num_phys_ports + 2; 433adb3dccfSVladimir Oltean 434adb3dccfSVladimir Oltean ocelot->npi = -1; 435adb3dccfSVladimir Oltean 436adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 437adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 438adb3dccfSVladimir Oltean 439adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 440adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 441adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 442adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 443adb3dccfSVladimir Oltean 444adb3dccfSVladimir Oltean /* Enable transmission of pause frames */ 445adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 446adb3dccfSVladimir Oltean } 447adb3dccfSVladimir Oltean 448adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu) 449adb3dccfSVladimir Oltean { 450adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 451adb3dccfSVladimir Oltean unsigned long cpu_flood; 452adb3dccfSVladimir Oltean 453adb3dccfSVladimir Oltean felix_npi_port_init(ocelot, cpu); 454adb3dccfSVladimir Oltean 455adb3dccfSVladimir Oltean /* Include the CPU port module (and indirectly, the NPI port) 456adb3dccfSVladimir Oltean * in the forwarding mask for unknown unicast - the hardware 457adb3dccfSVladimir Oltean * default value for ANA_FLOODING_FLD_UNICAST excludes 458adb3dccfSVladimir Oltean * BIT(ocelot->num_phys_ports), and so does ocelot_init, 459adb3dccfSVladimir Oltean * since Ocelot relies on whitelisting MAC addresses towards 460adb3dccfSVladimir Oltean * PGID_CPU. 461adb3dccfSVladimir Oltean * We do this because DSA does not yet perform RX filtering, 462adb3dccfSVladimir Oltean * and the NPI port does not perform source address learning, 463adb3dccfSVladimir Oltean * so traffic sent to Linux is effectively unknown from the 464adb3dccfSVladimir Oltean * switch's perspective. 465adb3dccfSVladimir Oltean */ 466adb3dccfSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 467adb3dccfSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC); 4686edb9e8dSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC); 469b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC); 470adb3dccfSVladimir Oltean 471adb3dccfSVladimir Oltean return 0; 472adb3dccfSVladimir Oltean } 473adb3dccfSVladimir Oltean 474adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu) 475adb3dccfSVladimir Oltean { 476adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 477adb3dccfSVladimir Oltean 478adb3dccfSVladimir Oltean felix_npi_port_deinit(ocelot, cpu); 479adb3dccfSVladimir Oltean } 480adb3dccfSVladimir Oltean 481adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu, 482adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 483adb3dccfSVladimir Oltean { 484adb3dccfSVladimir Oltean int err; 485adb3dccfSVladimir Oltean 486adb3dccfSVladimir Oltean switch (proto) { 4877c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 488adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 489adb3dccfSVladimir Oltean err = felix_setup_tag_npi(ds, cpu); 490adb3dccfSVladimir Oltean break; 491e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 492e21268efSVladimir Oltean err = felix_setup_tag_8021q(ds, cpu); 493e21268efSVladimir Oltean break; 494adb3dccfSVladimir Oltean default: 495adb3dccfSVladimir Oltean err = -EPROTONOSUPPORT; 496adb3dccfSVladimir Oltean } 497adb3dccfSVladimir Oltean 498adb3dccfSVladimir Oltean return err; 499adb3dccfSVladimir Oltean } 500adb3dccfSVladimir Oltean 501adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu, 502adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 503adb3dccfSVladimir Oltean { 504adb3dccfSVladimir Oltean switch (proto) { 5057c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 506adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 507adb3dccfSVladimir Oltean felix_teardown_tag_npi(ds, cpu); 508adb3dccfSVladimir Oltean break; 509e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 510e21268efSVladimir Oltean felix_teardown_tag_8021q(ds, cpu); 511e21268efSVladimir Oltean break; 512adb3dccfSVladimir Oltean default: 513adb3dccfSVladimir Oltean break; 514adb3dccfSVladimir Oltean } 515adb3dccfSVladimir Oltean } 516adb3dccfSVladimir Oltean 517e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the 518e21268efSVladimir Oltean * tag_8021q setup can fail, the NPI setup can't. So either the change is made, 519e21268efSVladimir Oltean * or the restoration is guaranteed to work. 520e21268efSVladimir Oltean */ 521adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu, 522adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 523adb3dccfSVladimir Oltean { 524adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 525adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 526adb3dccfSVladimir Oltean enum dsa_tag_protocol old_proto = felix->tag_proto; 527adb3dccfSVladimir Oltean int err; 528adb3dccfSVladimir Oltean 5297c4bb540SVladimir Oltean if (proto != DSA_TAG_PROTO_SEVILLE && 5307c4bb540SVladimir Oltean proto != DSA_TAG_PROTO_OCELOT && 531e21268efSVladimir Oltean proto != DSA_TAG_PROTO_OCELOT_8021Q) 532adb3dccfSVladimir Oltean return -EPROTONOSUPPORT; 533adb3dccfSVladimir Oltean 534adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, cpu, old_proto); 535adb3dccfSVladimir Oltean 536adb3dccfSVladimir Oltean err = felix_set_tag_protocol(ds, cpu, proto); 537adb3dccfSVladimir Oltean if (err) { 538adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, cpu, old_proto); 539adb3dccfSVladimir Oltean return err; 540adb3dccfSVladimir Oltean } 541adb3dccfSVladimir Oltean 542adb3dccfSVladimir Oltean felix->tag_proto = proto; 543adb3dccfSVladimir Oltean 544adb3dccfSVladimir Oltean return 0; 545adb3dccfSVladimir Oltean } 546adb3dccfSVladimir Oltean 54756051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 5484d776482SFlorian Fainelli int port, 5494d776482SFlorian Fainelli enum dsa_tag_protocol mp) 55056051948SVladimir Oltean { 551adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 552adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 553adb3dccfSVladimir Oltean 554adb3dccfSVladimir Oltean return felix->tag_proto; 55556051948SVladimir Oltean } 55656051948SVladimir Oltean 55756051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds, 55856051948SVladimir Oltean unsigned int ageing_time) 55956051948SVladimir Oltean { 56056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 56156051948SVladimir Oltean 56256051948SVladimir Oltean ocelot_set_ageing_time(ocelot, ageing_time); 56356051948SVladimir Oltean 56456051948SVladimir Oltean return 0; 56556051948SVladimir Oltean } 56656051948SVladimir Oltean 5675cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port) 5685cad43a5SVladimir Oltean { 5695cad43a5SVladimir Oltean struct ocelot *ocelot = ds->priv; 5705cad43a5SVladimir Oltean int err; 5715cad43a5SVladimir Oltean 5725cad43a5SVladimir Oltean err = ocelot_mact_flush(ocelot, port); 5735cad43a5SVladimir Oltean if (err) 5745cad43a5SVladimir Oltean dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n", 5755cad43a5SVladimir Oltean port, ERR_PTR(err)); 5765cad43a5SVladimir Oltean } 5775cad43a5SVladimir Oltean 57856051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port, 57956051948SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 58056051948SVladimir Oltean { 58156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 58256051948SVladimir Oltean 58356051948SVladimir Oltean return ocelot_fdb_dump(ocelot, port, cb, data); 58456051948SVladimir Oltean } 58556051948SVladimir Oltean 58656051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port, 58756051948SVladimir Oltean const unsigned char *addr, u16 vid) 58856051948SVladimir Oltean { 58956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 59056051948SVladimir Oltean 59187b0f983SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid); 59256051948SVladimir Oltean } 59356051948SVladimir Oltean 59456051948SVladimir Oltean static int felix_fdb_del(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 59956051948SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid); 60056051948SVladimir Oltean } 60156051948SVladimir Oltean 602961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag, 603961d8b69SVladimir Oltean const unsigned char *addr, u16 vid) 604961d8b69SVladimir Oltean { 605961d8b69SVladimir Oltean struct ocelot *ocelot = ds->priv; 606961d8b69SVladimir Oltean 607961d8b69SVladimir Oltean return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid); 608961d8b69SVladimir Oltean } 609961d8b69SVladimir Oltean 610961d8b69SVladimir Oltean static int felix_lag_fdb_del(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_del(ocelot, lag.dev, addr, vid); 616961d8b69SVladimir Oltean } 617961d8b69SVladimir Oltean 618a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 619209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 620209edf95SVladimir Oltean { 621209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 622209edf95SVladimir Oltean 623a52b2da7SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb); 624209edf95SVladimir Oltean } 625209edf95SVladimir Oltean 626209edf95SVladimir Oltean static int felix_mdb_del(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 631209edf95SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb); 632209edf95SVladimir Oltean } 633209edf95SVladimir Oltean 63456051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 63556051948SVladimir Oltean u8 state) 63656051948SVladimir Oltean { 63756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 63856051948SVladimir Oltean 63956051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 64056051948SVladimir Oltean } 64156051948SVladimir Oltean 642421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 643421741eaSVladimir Oltean struct switchdev_brport_flags val, 644421741eaSVladimir Oltean struct netlink_ext_ack *extack) 645421741eaSVladimir Oltean { 646421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 647421741eaSVladimir Oltean 648421741eaSVladimir Oltean return ocelot_port_pre_bridge_flags(ocelot, port, val); 649421741eaSVladimir Oltean } 650421741eaSVladimir Oltean 651421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port, 652421741eaSVladimir Oltean struct switchdev_brport_flags val, 653421741eaSVladimir Oltean struct netlink_ext_ack *extack) 654421741eaSVladimir Oltean { 655421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 656421741eaSVladimir Oltean 657421741eaSVladimir Oltean ocelot_port_bridge_flags(ocelot, port, val); 658421741eaSVladimir Oltean 659421741eaSVladimir Oltean return 0; 660421741eaSVladimir Oltean } 661421741eaSVladimir Oltean 66256051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 663b079922bSVladimir Oltean struct dsa_bridge bridge, bool *tx_fwd_offload) 66456051948SVladimir Oltean { 66556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 66656051948SVladimir Oltean 667d3eed0e5SVladimir Oltean ocelot_port_bridge_join(ocelot, port, bridge.dev); 668e4bd44e8SVladimir Oltean 669e4bd44e8SVladimir Oltean return 0; 67056051948SVladimir Oltean } 67156051948SVladimir Oltean 67256051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 673d3eed0e5SVladimir Oltean struct dsa_bridge bridge) 67456051948SVladimir Oltean { 67556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 67656051948SVladimir Oltean 677d3eed0e5SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, bridge.dev); 67856051948SVladimir Oltean } 67956051948SVladimir Oltean 6808fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 681dedd6a00SVladimir Oltean struct dsa_lag lag, 6828fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 6838fe6832eSVladimir Oltean { 6848fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 6858fe6832eSVladimir Oltean 686dedd6a00SVladimir Oltean return ocelot_port_lag_join(ocelot, port, lag.dev, info); 6878fe6832eSVladimir Oltean } 6888fe6832eSVladimir Oltean 6898fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 690dedd6a00SVladimir Oltean struct dsa_lag lag) 6918fe6832eSVladimir Oltean { 6928fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 6938fe6832eSVladimir Oltean 694dedd6a00SVladimir Oltean ocelot_port_lag_leave(ocelot, port, lag.dev); 6958fe6832eSVladimir Oltean 6968fe6832eSVladimir Oltean return 0; 6978fe6832eSVladimir Oltean } 6988fe6832eSVladimir Oltean 6998fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 7008fe6832eSVladimir Oltean { 7018fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 7028fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7038fe6832eSVladimir Oltean 7048fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 7058fe6832eSVladimir Oltean 7068fe6832eSVladimir Oltean return 0; 7078fe6832eSVladimir Oltean } 7088fe6832eSVladimir Oltean 70956051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 71001af940eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 71101af940eSVladimir Oltean struct netlink_ext_ack *extack) 71256051948SVladimir Oltean { 7132f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 714b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 7152f0402feSVladimir Oltean 7169a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 7179a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 7189a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 7199a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 7209a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 7219a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 7229a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 7239a720680SVladimir Oltean */ 7249a720680SVladimir Oltean if (port == ocelot->npi) 7259a720680SVladimir Oltean return 0; 7269a720680SVladimir Oltean 727b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 7282f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 72901af940eSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED, 73001af940eSVladimir Oltean extack); 73156051948SVladimir Oltean } 73256051948SVladimir Oltean 73389153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 73489153ed6SVladimir Oltean struct netlink_ext_ack *extack) 73556051948SVladimir Oltean { 73656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 73756051948SVladimir Oltean 7383b95d1b2SVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 73956051948SVladimir Oltean } 74056051948SVladimir Oltean 7411958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 74231046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 74331046a5fSVladimir Oltean struct netlink_ext_ack *extack) 74456051948SVladimir Oltean { 74556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 746183be6f9SVladimir Oltean u16 flags = vlan->flags; 7471958d581SVladimir Oltean int err; 74856051948SVladimir Oltean 74901af940eSVladimir Oltean err = felix_vlan_prepare(ds, port, vlan, extack); 7501958d581SVladimir Oltean if (err) 7511958d581SVladimir Oltean return err; 7521958d581SVladimir Oltean 7531958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 754183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 755183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 75656051948SVladimir Oltean } 75756051948SVladimir Oltean 75856051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 75956051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 76056051948SVladimir Oltean { 76156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 76256051948SVladimir Oltean 763b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 76456051948SVladimir Oltean } 76556051948SVladimir Oltean 76679fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port, 76779fda660SRussell King (Oracle) struct phylink_config *config) 76879fda660SRussell King (Oracle) { 76979fda660SRussell King (Oracle) struct ocelot *ocelot = ds->priv; 77079fda660SRussell King (Oracle) 771f6f04c02SRussell King (Oracle) /* This driver does not make use of the speed, duplex, pause or the 772f6f04c02SRussell King (Oracle) * advertisement in its mac_config, so it is safe to mark this driver 773f6f04c02SRussell King (Oracle) * as non-legacy. 774f6f04c02SRussell King (Oracle) */ 775f6f04c02SRussell King (Oracle) config->legacy_pre_march2020 = false; 776f6f04c02SRussell King (Oracle) 77779fda660SRussell King (Oracle) __set_bit(ocelot->ports[port]->phy_mode, 77879fda660SRussell King (Oracle) config->supported_interfaces); 77979fda660SRussell King (Oracle) } 78079fda660SRussell King (Oracle) 781bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port, 782bdeced75SVladimir Oltean unsigned long *supported, 783bdeced75SVladimir Oltean struct phylink_link_state *state) 784bdeced75SVladimir Oltean { 785bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 786375e1314SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 787bdeced75SVladimir Oltean 788375e1314SVladimir Oltean if (felix->info->phylink_validate) 789375e1314SVladimir Oltean felix->info->phylink_validate(ocelot, port, supported, state); 790bdeced75SVladimir Oltean } 791bdeced75SVladimir Oltean 792864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds, 793864ba485SRussell King (Oracle) int port, 794864ba485SRussell King (Oracle) phy_interface_t iface) 795bdeced75SVladimir Oltean { 796bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 797bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 798864ba485SRussell King (Oracle) struct phylink_pcs *pcs = NULL; 799bdeced75SVladimir Oltean 80049af6a76SColin Foster if (felix->pcs && felix->pcs[port]) 801864ba485SRussell King (Oracle) pcs = felix->pcs[port]; 802864ba485SRussell King (Oracle) 803864ba485SRussell King (Oracle) return pcs; 804bdeced75SVladimir Oltean } 805bdeced75SVladimir Oltean 806bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 807bdeced75SVladimir Oltean unsigned int link_an_mode, 808bdeced75SVladimir Oltean phy_interface_t interface) 809bdeced75SVladimir Oltean { 810bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 811bdeced75SVladimir Oltean 812e6e12df6SVladimir Oltean ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 813e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 814bdeced75SVladimir Oltean } 815bdeced75SVladimir Oltean 816bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 817bdeced75SVladimir Oltean unsigned int link_an_mode, 818bdeced75SVladimir Oltean phy_interface_t interface, 8195b502a7bSRussell King struct phy_device *phydev, 8205b502a7bSRussell King int speed, int duplex, 8215b502a7bSRussell King bool tx_pause, bool rx_pause) 822bdeced75SVladimir Oltean { 823bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 8247e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 825bdeced75SVladimir Oltean 826e6e12df6SVladimir Oltean ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 827e6e12df6SVladimir Oltean interface, speed, duplex, tx_pause, rx_pause, 828e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 8297e14a2dcSVladimir Oltean 8307e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 8317e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 832bdeced75SVladimir Oltean } 833bdeced75SVladimir Oltean 834bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 835bd2b3161SXiaoliang Yang { 836bd2b3161SXiaoliang Yang int i; 837bd2b3161SXiaoliang Yang 838bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 839bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 840bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 841bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 842bd2b3161SXiaoliang Yang port); 843bd2b3161SXiaoliang Yang 84470d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 845bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 846bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 847bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 848bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 849bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 850bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 851bd2b3161SXiaoliang Yang port, i); 852bd2b3161SXiaoliang Yang } 853bd2b3161SXiaoliang Yang } 854bd2b3161SXiaoliang Yang 85556051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 85656051948SVladimir Oltean u32 stringset, u8 *data) 85756051948SVladimir Oltean { 85856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 85956051948SVladimir Oltean 86056051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 86156051948SVladimir Oltean } 86256051948SVladimir Oltean 86356051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 86456051948SVladimir Oltean { 86556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 86656051948SVladimir Oltean 86756051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 86856051948SVladimir Oltean } 86956051948SVladimir Oltean 87056051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 87156051948SVladimir Oltean { 87256051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 87356051948SVladimir Oltean 87456051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 87556051948SVladimir Oltean } 87656051948SVladimir Oltean 87756051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 87856051948SVladimir Oltean struct ethtool_ts_info *info) 87956051948SVladimir Oltean { 88056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 88156051948SVladimir Oltean 88256051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 88356051948SVladimir Oltean } 88456051948SVladimir Oltean 885bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 886bdeced75SVladimir Oltean struct device_node *ports_node, 887bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 888bdeced75SVladimir Oltean { 889bdeced75SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 890bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 891bdeced75SVladimir Oltean struct device_node *child; 892bdeced75SVladimir Oltean 89337fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 894bdeced75SVladimir Oltean phy_interface_t phy_mode; 895bdeced75SVladimir Oltean u32 port; 896bdeced75SVladimir Oltean int err; 897bdeced75SVladimir Oltean 898bdeced75SVladimir Oltean /* Get switch port number from DT */ 899bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 900bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 901bdeced75SVladimir Oltean "(property \"reg\")\n"); 902bdeced75SVladimir Oltean of_node_put(child); 903bdeced75SVladimir Oltean return -ENODEV; 904bdeced75SVladimir Oltean } 905bdeced75SVladimir Oltean 906bdeced75SVladimir Oltean /* Get PHY mode from DT */ 907bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 908bdeced75SVladimir Oltean if (err) { 909bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 910bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 911bdeced75SVladimir Oltean port); 912bdeced75SVladimir Oltean of_node_put(child); 913bdeced75SVladimir Oltean return -ENODEV; 914bdeced75SVladimir Oltean } 915bdeced75SVladimir Oltean 916bdeced75SVladimir Oltean err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 917bdeced75SVladimir Oltean if (err < 0) { 918bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 919bdeced75SVladimir Oltean phy_modes(phy_mode), port); 92059ebb430SSumera Priyadarsini of_node_put(child); 921bdeced75SVladimir Oltean return err; 922bdeced75SVladimir Oltean } 923bdeced75SVladimir Oltean 924bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 925bdeced75SVladimir Oltean } 926bdeced75SVladimir Oltean 927bdeced75SVladimir Oltean return 0; 928bdeced75SVladimir Oltean } 929bdeced75SVladimir Oltean 930bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 931bdeced75SVladimir Oltean { 932bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 933bdeced75SVladimir Oltean struct device_node *switch_node; 934bdeced75SVladimir Oltean struct device_node *ports_node; 935bdeced75SVladimir Oltean int err; 936bdeced75SVladimir Oltean 937bdeced75SVladimir Oltean switch_node = dev->of_node; 938bdeced75SVladimir Oltean 939bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 940abecbfcdSVladimir Oltean if (!ports_node) 941abecbfcdSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 942bdeced75SVladimir Oltean if (!ports_node) { 943abecbfcdSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 944bdeced75SVladimir Oltean return -ENODEV; 945bdeced75SVladimir Oltean } 946bdeced75SVladimir Oltean 947bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 948bdeced75SVladimir Oltean of_node_put(ports_node); 949bdeced75SVladimir Oltean 950bdeced75SVladimir Oltean return err; 951bdeced75SVladimir Oltean } 952bdeced75SVladimir Oltean 95356051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 95456051948SVladimir Oltean { 95556051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 956bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 957b4024c9eSClaudiu Manoil struct resource res; 95856051948SVladimir Oltean int port, i, err; 95956051948SVladimir Oltean 96056051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 96156051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 96256051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 96356051948SVladimir Oltean if (!ocelot->ports) 96456051948SVladimir Oltean return -ENOMEM; 96556051948SVladimir Oltean 96656051948SVladimir Oltean ocelot->map = felix->info->map; 96756051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 96856051948SVladimir Oltean ocelot->num_stats = felix->info->num_stats; 96921ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 97007d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 97177043c37SXiaoliang Yang ocelot->vcap_pol.base = felix->info->vcap_pol_base; 97277043c37SXiaoliang Yang ocelot->vcap_pol.max = felix->info->vcap_pol_max; 97377043c37SXiaoliang Yang ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 97477043c37SXiaoliang Yang ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 97556051948SVladimir Oltean ocelot->ops = felix->info->ops; 976cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 977cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 978f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 97956051948SVladimir Oltean 980bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 981bdeced75SVladimir Oltean GFP_KERNEL); 982bdeced75SVladimir Oltean if (!port_phy_modes) 983bdeced75SVladimir Oltean return -ENOMEM; 984bdeced75SVladimir Oltean 985bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 986bdeced75SVladimir Oltean if (err) { 987bdeced75SVladimir Oltean kfree(port_phy_modes); 988bdeced75SVladimir Oltean return err; 989bdeced75SVladimir Oltean } 990bdeced75SVladimir Oltean 99156051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 99256051948SVladimir Oltean struct regmap *target; 99356051948SVladimir Oltean 99456051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 99556051948SVladimir Oltean continue; 99656051948SVladimir Oltean 997b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 998b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 999375e1314SVladimir Oltean res.start += felix->switch_base; 1000375e1314SVladimir Oltean res.end += felix->switch_base; 100156051948SVladimir Oltean 1002242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 100356051948SVladimir Oltean if (IS_ERR(target)) { 100456051948SVladimir Oltean dev_err(ocelot->dev, 100556051948SVladimir Oltean "Failed to map device memory space\n"); 1006bdeced75SVladimir Oltean kfree(port_phy_modes); 100756051948SVladimir Oltean return PTR_ERR(target); 100856051948SVladimir Oltean } 100956051948SVladimir Oltean 101056051948SVladimir Oltean ocelot->targets[i] = target; 101156051948SVladimir Oltean } 101256051948SVladimir Oltean 101356051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 101456051948SVladimir Oltean if (err) { 101556051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 1016bdeced75SVladimir Oltean kfree(port_phy_modes); 101756051948SVladimir Oltean return err; 101856051948SVladimir Oltean } 101956051948SVladimir Oltean 102056051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 102156051948SVladimir Oltean struct ocelot_port *ocelot_port; 102291c724cfSVladimir Oltean struct regmap *target; 102356051948SVladimir Oltean 102456051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 102556051948SVladimir Oltean sizeof(struct ocelot_port), 102656051948SVladimir Oltean GFP_KERNEL); 102756051948SVladimir Oltean if (!ocelot_port) { 102856051948SVladimir Oltean dev_err(ocelot->dev, 102956051948SVladimir Oltean "failed to allocate port memory\n"); 1030bdeced75SVladimir Oltean kfree(port_phy_modes); 103156051948SVladimir Oltean return -ENOMEM; 103256051948SVladimir Oltean } 103356051948SVladimir Oltean 1034b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1035b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1036375e1314SVladimir Oltean res.start += felix->switch_base; 1037375e1314SVladimir Oltean res.end += felix->switch_base; 103856051948SVladimir Oltean 1039242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 104091c724cfSVladimir Oltean if (IS_ERR(target)) { 104156051948SVladimir Oltean dev_err(ocelot->dev, 104291c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 104391c724cfSVladimir Oltean port); 1044bdeced75SVladimir Oltean kfree(port_phy_modes); 104591c724cfSVladimir Oltean return PTR_ERR(target); 104656051948SVladimir Oltean } 104756051948SVladimir Oltean 1048bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 104956051948SVladimir Oltean ocelot_port->ocelot = ocelot; 105091c724cfSVladimir Oltean ocelot_port->target = target; 105156051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 105256051948SVladimir Oltean } 105356051948SVladimir Oltean 1054bdeced75SVladimir Oltean kfree(port_phy_modes); 1055bdeced75SVladimir Oltean 1056bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1057bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1058bdeced75SVladimir Oltean if (err < 0) 1059bdeced75SVladimir Oltean return err; 1060bdeced75SVladimir Oltean } 1061bdeced75SVladimir Oltean 106256051948SVladimir Oltean return 0; 106356051948SVladimir Oltean } 106456051948SVladimir Oltean 10651328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 10661328a883SVladimir Oltean struct sk_buff *skb) 10671328a883SVladimir Oltean { 10681328a883SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 10691328a883SVladimir Oltean struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 10701328a883SVladimir Oltean struct sk_buff *skb_match = NULL, *skb_tmp; 10711328a883SVladimir Oltean unsigned long flags; 10721328a883SVladimir Oltean 10731328a883SVladimir Oltean if (!clone) 10741328a883SVladimir Oltean return; 10751328a883SVladimir Oltean 10761328a883SVladimir Oltean spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 10771328a883SVladimir Oltean 10781328a883SVladimir Oltean skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 10791328a883SVladimir Oltean if (skb != clone) 10801328a883SVladimir Oltean continue; 10811328a883SVladimir Oltean __skb_unlink(skb, &ocelot_port->tx_skbs); 10821328a883SVladimir Oltean skb_match = skb; 10831328a883SVladimir Oltean break; 10841328a883SVladimir Oltean } 10851328a883SVladimir Oltean 10861328a883SVladimir Oltean spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 10871328a883SVladimir Oltean 10881328a883SVladimir Oltean WARN_ONCE(!skb_match, 10891328a883SVladimir Oltean "Could not find skb clone in TX timestamping list\n"); 10901328a883SVladimir Oltean } 10911328a883SVladimir Oltean 109249f885b2SVladimir Oltean #define work_to_xmit_work(w) \ 109349f885b2SVladimir Oltean container_of((w), struct felix_deferred_xmit_work, work) 109449f885b2SVladimir Oltean 109549f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work) 109649f885b2SVladimir Oltean { 109749f885b2SVladimir Oltean struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 109849f885b2SVladimir Oltean struct dsa_switch *ds = xmit_work->dp->ds; 109949f885b2SVladimir Oltean struct sk_buff *skb = xmit_work->skb; 110049f885b2SVladimir Oltean u32 rew_op = ocelot_ptp_rew_op(skb); 110149f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 110249f885b2SVladimir Oltean int port = xmit_work->dp->index; 110349f885b2SVladimir Oltean int retries = 10; 110449f885b2SVladimir Oltean 110549f885b2SVladimir Oltean do { 110649f885b2SVladimir Oltean if (ocelot_can_inject(ocelot, 0)) 110749f885b2SVladimir Oltean break; 110849f885b2SVladimir Oltean 110949f885b2SVladimir Oltean cpu_relax(); 111049f885b2SVladimir Oltean } while (--retries); 111149f885b2SVladimir Oltean 111249f885b2SVladimir Oltean if (!retries) { 111349f885b2SVladimir Oltean dev_err(ocelot->dev, "port %d failed to inject skb\n", 111449f885b2SVladimir Oltean port); 11151328a883SVladimir Oltean ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 111649f885b2SVladimir Oltean kfree_skb(skb); 111749f885b2SVladimir Oltean return; 111849f885b2SVladimir Oltean } 111949f885b2SVladimir Oltean 112049f885b2SVladimir Oltean ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 112149f885b2SVladimir Oltean 112249f885b2SVladimir Oltean consume_skb(skb); 112349f885b2SVladimir Oltean kfree(xmit_work); 112449f885b2SVladimir Oltean } 112549f885b2SVladimir Oltean 112635d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds, 112735d97680SVladimir Oltean enum dsa_tag_protocol proto) 112849f885b2SVladimir Oltean { 112935d97680SVladimir Oltean struct ocelot_8021q_tagger_data *tagger_data; 113049f885b2SVladimir Oltean 113135d97680SVladimir Oltean switch (proto) { 113235d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 113335d97680SVladimir Oltean tagger_data = ocelot_8021q_tagger_data(ds); 113435d97680SVladimir Oltean tagger_data->xmit_work_fn = felix_port_deferred_xmit; 113549f885b2SVladimir Oltean return 0; 113635d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT: 113735d97680SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 113849f885b2SVladimir Oltean return 0; 113935d97680SVladimir Oltean default: 114035d97680SVladimir Oltean return -EPROTONOSUPPORT; 114149f885b2SVladimir Oltean } 114249f885b2SVladimir Oltean } 114349f885b2SVladimir Oltean 114456051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with 114556051948SVladimir Oltean * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 114656051948SVladimir Oltean * us to allocate structures twice (leak memory) and map PCI memory twice 114756051948SVladimir Oltean * (which will not work). 114856051948SVladimir Oltean */ 114956051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds) 115056051948SVladimir Oltean { 115156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 115256051948SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 11532960bb14SVladimir Oltean struct dsa_port *dp; 11542960bb14SVladimir Oltean int err; 115556051948SVladimir Oltean 115656051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 115756051948SVladimir Oltean if (err) 115856051948SVladimir Oltean return err; 115956051948SVladimir Oltean 1160d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1161d1cc0e93SVladimir Oltean if (err) 11626b73b7c9SVladimir Oltean goto out_mdiobus_free; 1163d1cc0e93SVladimir Oltean 11642b49d128SYangbo Lu if (ocelot->ptp) { 11652ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 11662b49d128SYangbo Lu if (err) { 11672b49d128SYangbo Lu dev_err(ocelot->dev, 11682b49d128SYangbo Lu "Timestamp initialization failed\n"); 11692b49d128SYangbo Lu ocelot->ptp = 0; 11702b49d128SYangbo Lu } 11712b49d128SYangbo Lu } 117256051948SVladimir Oltean 11732960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 11742960bb14SVladimir Oltean ocelot_init_port(ocelot, dp->index); 1175bd2b3161SXiaoliang Yang 1176bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1177bd2b3161SXiaoliang Yang * bits of vlan tag. 1178bd2b3161SXiaoliang Yang */ 11792960bb14SVladimir Oltean felix_port_qos_map_init(ocelot, dp->index); 118056051948SVladimir Oltean } 118156051948SVladimir Oltean 1182f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1183f59fd9caSVladimir Oltean if (err) 11846b73b7c9SVladimir Oltean goto out_deinit_ports; 1185f59fd9caSVladimir Oltean 11862960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 1187adb3dccfSVladimir Oltean /* The initial tag protocol is NPI which always returns 0, so 1188adb3dccfSVladimir Oltean * there's no real point in checking for errors. 11891cf3299bSVladimir Oltean */ 11902960bb14SVladimir Oltean felix_set_tag_protocol(ds, dp->index, felix->tag_proto); 11918d5f7954SVladimir Oltean break; 1192adb3dccfSVladimir Oltean } 11931cf3299bSVladimir Oltean 11940b912fc9SVladimir Oltean ds->mtu_enforcement_ingress = true; 1195c54913c1SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 1196bdeced75SVladimir Oltean 119756051948SVladimir Oltean return 0; 11986b73b7c9SVladimir Oltean 11996b73b7c9SVladimir Oltean out_deinit_ports: 12002960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 12012960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 12026b73b7c9SVladimir Oltean 12036b73b7c9SVladimir Oltean ocelot_deinit_timestamp(ocelot); 12046b73b7c9SVladimir Oltean ocelot_deinit(ocelot); 12056b73b7c9SVladimir Oltean 12066b73b7c9SVladimir Oltean out_mdiobus_free: 12076b73b7c9SVladimir Oltean if (felix->info->mdio_bus_free) 12086b73b7c9SVladimir Oltean felix->info->mdio_bus_free(ocelot); 12096b73b7c9SVladimir Oltean 12106b73b7c9SVladimir Oltean return err; 121156051948SVladimir Oltean } 121256051948SVladimir Oltean 121356051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 121456051948SVladimir Oltean { 121556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1216bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12172960bb14SVladimir Oltean struct dsa_port *dp; 1218bdeced75SVladimir Oltean 12192960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 12202960bb14SVladimir Oltean felix_del_tag_protocol(ds, dp->index, felix->tag_proto); 12218d5f7954SVladimir Oltean break; 1222adb3dccfSVladimir Oltean } 1223adb3dccfSVladimir Oltean 12242960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 12252960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 1226d19741b0SVladimir Oltean 122749f885b2SVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 122849f885b2SVladimir Oltean ocelot_deinit_timestamp(ocelot); 122949f885b2SVladimir Oltean ocelot_deinit(ocelot); 123049f885b2SVladimir Oltean 1231d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1232d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 123356051948SVladimir Oltean } 123456051948SVladimir Oltean 1235c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1236c0bcf537SYangbo Lu struct ifreq *ifr) 1237c0bcf537SYangbo Lu { 1238c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1239c0bcf537SYangbo Lu 1240c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1241c0bcf537SYangbo Lu } 1242c0bcf537SYangbo Lu 1243c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1244c0bcf537SYangbo Lu struct ifreq *ifr) 1245c0bcf537SYangbo Lu { 1246c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 124799348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 124899348004SVladimir Oltean bool using_tag_8021q; 124999348004SVladimir Oltean int err; 1250c0bcf537SYangbo Lu 125199348004SVladimir Oltean err = ocelot_hwstamp_set(ocelot, port, ifr); 125299348004SVladimir Oltean if (err) 125399348004SVladimir Oltean return err; 125499348004SVladimir Oltean 125599348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 125699348004SVladimir Oltean 125799348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 1258c0bcf537SYangbo Lu } 1259c0bcf537SYangbo Lu 12600a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type) 12610a6f17c6SVladimir Oltean { 12620a6f17c6SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12630a6f17c6SVladimir Oltean int err, grp = 0; 12640a6f17c6SVladimir Oltean 12650a6f17c6SVladimir Oltean if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 12660a6f17c6SVladimir Oltean return false; 12670a6f17c6SVladimir Oltean 12680a6f17c6SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 12690a6f17c6SVladimir Oltean return false; 12700a6f17c6SVladimir Oltean 12710a6f17c6SVladimir Oltean if (ptp_type == PTP_CLASS_NONE) 12720a6f17c6SVladimir Oltean return false; 12730a6f17c6SVladimir Oltean 12740a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 12750a6f17c6SVladimir Oltean struct sk_buff *skb; 12760a6f17c6SVladimir Oltean unsigned int type; 12770a6f17c6SVladimir Oltean 12780a6f17c6SVladimir Oltean err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 12790a6f17c6SVladimir Oltean if (err) 12800a6f17c6SVladimir Oltean goto out; 12810a6f17c6SVladimir Oltean 12820a6f17c6SVladimir Oltean /* We trap to the CPU port module all PTP frames, but 12830a6f17c6SVladimir Oltean * felix_rxtstamp() only gets called for event frames. 12840a6f17c6SVladimir Oltean * So we need to avoid sending duplicate general 12850a6f17c6SVladimir Oltean * message frames by running a second BPF classifier 12860a6f17c6SVladimir Oltean * here and dropping those. 12870a6f17c6SVladimir Oltean */ 12880a6f17c6SVladimir Oltean __skb_push(skb, ETH_HLEN); 12890a6f17c6SVladimir Oltean 12900a6f17c6SVladimir Oltean type = ptp_classify_raw(skb); 12910a6f17c6SVladimir Oltean 12920a6f17c6SVladimir Oltean __skb_pull(skb, ETH_HLEN); 12930a6f17c6SVladimir Oltean 12940a6f17c6SVladimir Oltean if (type == PTP_CLASS_NONE) { 12950a6f17c6SVladimir Oltean kfree_skb(skb); 12960a6f17c6SVladimir Oltean continue; 12970a6f17c6SVladimir Oltean } 12980a6f17c6SVladimir Oltean 12990a6f17c6SVladimir Oltean netif_rx(skb); 13000a6f17c6SVladimir Oltean } 13010a6f17c6SVladimir Oltean 13020a6f17c6SVladimir Oltean out: 13030a6f17c6SVladimir Oltean if (err < 0) 13040a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 13050a6f17c6SVladimir Oltean 13060a6f17c6SVladimir Oltean return true; 13070a6f17c6SVladimir Oltean } 13080a6f17c6SVladimir Oltean 1309c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1310c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1311c0bcf537SYangbo Lu { 131292f62485SVladimir Oltean u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1313c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1314c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1315c0bcf537SYangbo Lu struct timespec64 ts; 131692f62485SVladimir Oltean u32 tstamp_hi; 131792f62485SVladimir Oltean u64 tstamp; 1318c0bcf537SYangbo Lu 13190a6f17c6SVladimir Oltean /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 13200a6f17c6SVladimir Oltean * for RX timestamping. Then free it, and poll for its copy through 13210a6f17c6SVladimir Oltean * MMIO in the CPU port module, and inject that into the stack from 13220a6f17c6SVladimir Oltean * ocelot_xtr_poll(). 13230a6f17c6SVladimir Oltean */ 13240a6f17c6SVladimir Oltean if (felix_check_xtr_pkt(ocelot, type)) { 13250a6f17c6SVladimir Oltean kfree_skb(skb); 13260a6f17c6SVladimir Oltean return true; 13270a6f17c6SVladimir Oltean } 13280a6f17c6SVladimir Oltean 1329c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1330c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1331c0bcf537SYangbo Lu 1332c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1333c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1334c0bcf537SYangbo Lu tstamp_hi--; 1335c0bcf537SYangbo Lu 1336c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1337c0bcf537SYangbo Lu 1338c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1339c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1340c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1341c0bcf537SYangbo Lu return false; 1342c0bcf537SYangbo Lu } 1343c0bcf537SYangbo Lu 13445c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port, 13455c5416f5SYangbo Lu struct sk_buff *skb) 1346c0bcf537SYangbo Lu { 1347c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1348682eaad9SYangbo Lu struct sk_buff *clone = NULL; 1349c0bcf537SYangbo Lu 1350682eaad9SYangbo Lu if (!ocelot->ptp) 13515c5416f5SYangbo Lu return; 1352c0bcf537SYangbo Lu 135352849bcfSVladimir Oltean if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 135452849bcfSVladimir Oltean dev_err_ratelimited(ds->dev, 135552849bcfSVladimir Oltean "port %d delivering skb without TX timestamp\n", 135652849bcfSVladimir Oltean port); 1357682eaad9SYangbo Lu return; 135852849bcfSVladimir Oltean } 1359682eaad9SYangbo Lu 1360682eaad9SYangbo Lu if (clone) 1361c4b364ceSYangbo Lu OCELOT_SKB_CB(skb)->clone = clone; 13625c5416f5SYangbo Lu } 1363c0bcf537SYangbo Lu 13640b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 13650b912fc9SVladimir Oltean { 13660b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13670b912fc9SVladimir Oltean 13680b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 13690b912fc9SVladimir Oltean 13700b912fc9SVladimir Oltean return 0; 13710b912fc9SVladimir Oltean } 13720b912fc9SVladimir Oltean 13730b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 13740b912fc9SVladimir Oltean { 13750b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13760b912fc9SVladimir Oltean 13770b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 13780b912fc9SVladimir Oltean } 13790b912fc9SVladimir Oltean 138007d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 138107d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 138207d985eeSVladimir Oltean { 138307d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 138499348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 138599348004SVladimir Oltean bool using_tag_8021q; 138699348004SVladimir Oltean int err; 138707d985eeSVladimir Oltean 138899348004SVladimir Oltean err = ocelot_cls_flower_replace(ocelot, port, cls, ingress); 138999348004SVladimir Oltean if (err) 139099348004SVladimir Oltean return err; 139199348004SVladimir Oltean 139299348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 139399348004SVladimir Oltean 139499348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 139507d985eeSVladimir Oltean } 139607d985eeSVladimir Oltean 139707d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 139807d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 139907d985eeSVladimir Oltean { 140007d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 140107d985eeSVladimir Oltean 140207d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 140307d985eeSVladimir Oltean } 140407d985eeSVladimir Oltean 140507d985eeSVladimir Oltean static int felix_cls_flower_stats(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_stats(ocelot, port, cls, ingress); 141107d985eeSVladimir Oltean } 141207d985eeSVladimir Oltean 1413fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1414fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1415fc411eaaSVladimir Oltean { 1416fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1417fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1418fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 14195f035af7SPo Liu .burst = policer->burst, 1420fc411eaaSVladimir Oltean }; 1421fc411eaaSVladimir Oltean 1422fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1423fc411eaaSVladimir Oltean } 1424fc411eaaSVladimir Oltean 1425fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1426fc411eaaSVladimir Oltean { 1427fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1428fc411eaaSVladimir Oltean 1429fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1430fc411eaaSVladimir Oltean } 1431fc411eaaSVladimir Oltean 1432de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1433de143c0eSXiaoliang Yang enum tc_setup_type type, 1434de143c0eSXiaoliang Yang void *type_data) 1435de143c0eSXiaoliang Yang { 1436de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1437de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1438de143c0eSXiaoliang Yang 1439de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1440de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1441de143c0eSXiaoliang Yang else 1442de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1443de143c0eSXiaoliang Yang } 1444de143c0eSXiaoliang Yang 1445f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1446f59fd9caSVladimir Oltean u16 pool_index, 1447f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1448f59fd9caSVladimir Oltean { 1449f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1450f59fd9caSVladimir Oltean 1451f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1452f59fd9caSVladimir Oltean } 1453f59fd9caSVladimir Oltean 1454f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1455f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1456f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1457f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1458f59fd9caSVladimir Oltean { 1459f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1460f59fd9caSVladimir Oltean 1461f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1462f59fd9caSVladimir Oltean threshold_type, extack); 1463f59fd9caSVladimir Oltean } 1464f59fd9caSVladimir Oltean 1465f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1466f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1467f59fd9caSVladimir Oltean u32 *p_threshold) 1468f59fd9caSVladimir Oltean { 1469f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1470f59fd9caSVladimir Oltean 1471f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1472f59fd9caSVladimir Oltean p_threshold); 1473f59fd9caSVladimir Oltean } 1474f59fd9caSVladimir Oltean 1475f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1476f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1477f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1478f59fd9caSVladimir Oltean { 1479f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1480f59fd9caSVladimir Oltean 1481f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1482f59fd9caSVladimir Oltean threshold, extack); 1483f59fd9caSVladimir Oltean } 1484f59fd9caSVladimir Oltean 1485f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1486f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1487f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1488f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1489f59fd9caSVladimir Oltean { 1490f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1491f59fd9caSVladimir Oltean 1492f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1493f59fd9caSVladimir Oltean pool_type, p_pool_index, 1494f59fd9caSVladimir Oltean p_threshold); 1495f59fd9caSVladimir Oltean } 1496f59fd9caSVladimir Oltean 1497f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1498f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1499f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1500f59fd9caSVladimir Oltean u16 pool_index, u32 threshold, 1501f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1502f59fd9caSVladimir Oltean { 1503f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1504f59fd9caSVladimir Oltean 1505f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1506f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1507f59fd9caSVladimir Oltean extack); 1508f59fd9caSVladimir Oltean } 1509f59fd9caSVladimir Oltean 1510f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1511f59fd9caSVladimir Oltean unsigned int sb_index) 1512f59fd9caSVladimir Oltean { 1513f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1514f59fd9caSVladimir Oltean 1515f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1516f59fd9caSVladimir Oltean } 1517f59fd9caSVladimir Oltean 1518f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(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_max_clear(ocelot, sb_index); 1524f59fd9caSVladimir Oltean } 1525f59fd9caSVladimir Oltean 1526f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1527f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1528f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1529f59fd9caSVladimir Oltean { 1530f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1531f59fd9caSVladimir Oltean 1532f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1533f59fd9caSVladimir Oltean p_cur, p_max); 1534f59fd9caSVladimir Oltean } 1535f59fd9caSVladimir Oltean 1536f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1537f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1538f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1539f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1540f59fd9caSVladimir Oltean { 1541f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1542f59fd9caSVladimir Oltean 1543f59fd9caSVladimir Oltean return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1544f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1545f59fd9caSVladimir Oltean } 1546f59fd9caSVladimir Oltean 1547a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port, 1548a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1549a026c50bSHoratiu Vultur { 1550a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1551a026c50bSHoratiu Vultur 1552a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1553a026c50bSHoratiu Vultur } 1554a026c50bSHoratiu Vultur 1555a026c50bSHoratiu Vultur static int felix_mrp_del(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 1564a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1565a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1566a026c50bSHoratiu Vultur { 1567a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1568a026c50bSHoratiu Vultur 1569a026c50bSHoratiu Vultur return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1570a026c50bSHoratiu Vultur } 1571a026c50bSHoratiu Vultur 1572a026c50bSHoratiu Vultur static int 1573a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1574a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1575a026c50bSHoratiu Vultur { 1576a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1577a026c50bSHoratiu Vultur 1578a026c50bSHoratiu Vultur return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1579a026c50bSHoratiu Vultur } 1580a026c50bSHoratiu Vultur 1581375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 158256051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1583adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 158435d97680SVladimir Oltean .connect_tag_protocol = felix_connect_tag_protocol, 158556051948SVladimir Oltean .setup = felix_setup, 158656051948SVladimir Oltean .teardown = felix_teardown, 158756051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 158856051948SVladimir Oltean .get_strings = felix_get_strings, 158956051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 159056051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 159156051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 159279fda660SRussell King (Oracle) .phylink_get_caps = felix_phylink_get_caps, 1593bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1594864ba485SRussell King (Oracle) .phylink_mac_select_pcs = felix_phylink_mac_select_pcs, 1595bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1596bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 15975cad43a5SVladimir Oltean .port_fast_age = felix_port_fast_age, 159856051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 159956051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 160056051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1601961d8b69SVladimir Oltean .lag_fdb_add = felix_lag_fdb_add, 1602961d8b69SVladimir Oltean .lag_fdb_del = felix_lag_fdb_del, 1603209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1604209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 1605421741eaSVladimir Oltean .port_pre_bridge_flags = felix_pre_bridge_flags, 1606421741eaSVladimir Oltean .port_bridge_flags = felix_bridge_flags, 160756051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 160856051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 16098fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 16108fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 16118fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 161256051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 161356051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 161456051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 161556051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1616c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1617c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1618c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1619c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 16200b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 16210b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1622fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1623fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 162407d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 162507d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 162607d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1627de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1628f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1629f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1630f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1631f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1632f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1633f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1634f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1635f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1636f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1637f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1638a026c50bSHoratiu Vultur .port_mrp_add = felix_mrp_add, 1639a026c50bSHoratiu Vultur .port_mrp_del = felix_mrp_del, 1640a026c50bSHoratiu Vultur .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1641a026c50bSHoratiu Vultur .port_mrp_del_ring_role = felix_mrp_del_ring_role, 16425da11eb4SVladimir Oltean .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 16435da11eb4SVladimir Oltean .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 164456051948SVladimir Oltean }; 1645319e4dd1SVladimir Oltean 1646319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1647319e4dd1SVladimir Oltean { 1648319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1649319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1650319e4dd1SVladimir Oltean 1651319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1652319e4dd1SVladimir Oltean return NULL; 1653319e4dd1SVladimir Oltean 1654319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1655319e4dd1SVladimir Oltean } 1656319e4dd1SVladimir Oltean 1657319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1658319e4dd1SVladimir Oltean { 1659319e4dd1SVladimir Oltean struct dsa_port *dp; 1660319e4dd1SVladimir Oltean 1661319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1662319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1663319e4dd1SVladimir Oltean return -EINVAL; 1664319e4dd1SVladimir Oltean 1665319e4dd1SVladimir Oltean return dp->index; 1666319e4dd1SVladimir Oltean } 1667