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 /* We don't need to install the rxvlan into the other ports' filtering 37e21268efSVladimir Oltean * tables, because we're just pushing the rxvlan when sending towards 38e21268efSVladimir Oltean * the CPU 39e21268efSVladimir Oltean */ 40e21268efSVladimir Oltean if (!pvid) 41e21268efSVladimir Oltean return 0; 42e21268efSVladimir Oltean 43e21268efSVladimir Oltean key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length; 44e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 45e21268efSVladimir Oltean 46e21268efSVladimir Oltean outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), 47e21268efSVladimir Oltean GFP_KERNEL); 48e21268efSVladimir Oltean if (!outer_tagging_rule) 49e21268efSVladimir Oltean return -ENOMEM; 50e21268efSVladimir Oltean 51e21268efSVladimir Oltean outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 52e21268efSVladimir Oltean outer_tagging_rule->prio = 1; 53c518afecSVladimir Oltean outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port); 54e21268efSVladimir Oltean outer_tagging_rule->id.tc_offload = false; 55e21268efSVladimir Oltean outer_tagging_rule->block_id = VCAP_ES0; 56e21268efSVladimir Oltean outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 57e21268efSVladimir Oltean outer_tagging_rule->lookup = 0; 58e21268efSVladimir Oltean outer_tagging_rule->ingress_port.value = port; 59e21268efSVladimir Oltean outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0); 60e21268efSVladimir Oltean outer_tagging_rule->egress_port.value = upstream; 61e21268efSVladimir Oltean outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0); 62e21268efSVladimir Oltean outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG; 63e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD; 64e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_vid_sel = 1; 65e21268efSVladimir Oltean outer_tagging_rule->action.vid_a_val = vid; 66e21268efSVladimir Oltean 67e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL); 68e21268efSVladimir Oltean if (err) 69e21268efSVladimir Oltean kfree(outer_tagging_rule); 70e21268efSVladimir Oltean 71e21268efSVladimir Oltean return err; 72e21268efSVladimir Oltean } 73e21268efSVladimir Oltean 74e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid, 75e21268efSVladimir Oltean bool pvid, bool untagged) 76e21268efSVladimir Oltean { 77e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 78e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 79e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 80e21268efSVladimir Oltean int upstream, err; 81e21268efSVladimir Oltean 82e21268efSVladimir Oltean /* tag_8021q.c assumes we are implementing this via port VLAN 83e21268efSVladimir Oltean * membership, which we aren't. So we don't need to add any VCAP filter 84e21268efSVladimir Oltean * for the CPU port. 85e21268efSVladimir Oltean */ 86e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 87e21268efSVladimir Oltean return 0; 88e21268efSVladimir Oltean 89e21268efSVladimir Oltean untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 90e21268efSVladimir Oltean if (!untagging_rule) 91e21268efSVladimir Oltean return -ENOMEM; 92e21268efSVladimir Oltean 93e21268efSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 94e21268efSVladimir Oltean if (!redirect_rule) { 95e21268efSVladimir Oltean kfree(untagging_rule); 96e21268efSVladimir Oltean return -ENOMEM; 97e21268efSVladimir Oltean } 98e21268efSVladimir Oltean 99e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 100e21268efSVladimir Oltean 101e21268efSVladimir Oltean untagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 102e21268efSVladimir Oltean untagging_rule->ingress_port_mask = BIT(upstream); 103e21268efSVladimir Oltean untagging_rule->vlan.vid.value = vid; 104e21268efSVladimir Oltean untagging_rule->vlan.vid.mask = VLAN_VID_MASK; 105e21268efSVladimir Oltean untagging_rule->prio = 1; 106c518afecSVladimir Oltean untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port); 107e21268efSVladimir Oltean untagging_rule->id.tc_offload = false; 108e21268efSVladimir Oltean untagging_rule->block_id = VCAP_IS1; 109e21268efSVladimir Oltean untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 110e21268efSVladimir Oltean untagging_rule->lookup = 0; 111e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt_ena = true; 112e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt = 1; 113e21268efSVladimir Oltean untagging_rule->action.pag_override_mask = 0xff; 114e21268efSVladimir Oltean untagging_rule->action.pag_val = port; 115e21268efSVladimir Oltean 116e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL); 117e21268efSVladimir Oltean if (err) { 118e21268efSVladimir Oltean kfree(untagging_rule); 119e21268efSVladimir Oltean kfree(redirect_rule); 120e21268efSVladimir Oltean return err; 121e21268efSVladimir Oltean } 122e21268efSVladimir Oltean 123e21268efSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 124e21268efSVladimir Oltean redirect_rule->ingress_port_mask = BIT(upstream); 125e21268efSVladimir Oltean redirect_rule->pag = port; 126e21268efSVladimir Oltean redirect_rule->prio = 1; 127c518afecSVladimir Oltean redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port); 128e21268efSVladimir Oltean redirect_rule->id.tc_offload = false; 129e21268efSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 130e21268efSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 131e21268efSVladimir Oltean redirect_rule->lookup = 0; 132e21268efSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 133e21268efSVladimir Oltean redirect_rule->action.port_mask = BIT(port); 134e21268efSVladimir Oltean 135e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 136e21268efSVladimir Oltean if (err) { 137e21268efSVladimir Oltean ocelot_vcap_filter_del(ocelot, untagging_rule); 138e21268efSVladimir Oltean kfree(redirect_rule); 139e21268efSVladimir Oltean return err; 140e21268efSVladimir Oltean } 141e21268efSVladimir Oltean 142e21268efSVladimir Oltean return 0; 143e21268efSVladimir Oltean } 144e21268efSVladimir Oltean 145e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 146e21268efSVladimir Oltean u16 flags) 147e21268efSVladimir Oltean { 148e21268efSVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 149e21268efSVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 150e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 151e21268efSVladimir Oltean 152e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 153e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot), 154e21268efSVladimir Oltean port, vid, pvid, untagged); 155e21268efSVladimir Oltean 156e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 157e21268efSVladimir Oltean return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot), 158e21268efSVladimir Oltean port, vid, pvid, untagged); 159e21268efSVladimir Oltean 160e21268efSVladimir Oltean return 0; 161e21268efSVladimir Oltean } 162e21268efSVladimir Oltean 163e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid) 164e21268efSVladimir Oltean { 165e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 166e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_es0; 167e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 168e21268efSVladimir Oltean 169e21268efSVladimir Oltean block_vcap_es0 = &ocelot->block[VCAP_ES0]; 170e21268efSVladimir Oltean 171e21268efSVladimir Oltean outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 172e21268efSVladimir Oltean port, false); 173e21268efSVladimir Oltean /* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid 174e21268efSVladimir Oltean * installing outer tagging ES0 rules where they weren't needed. 175e21268efSVladimir Oltean * But in rxvlan_del, the API doesn't give us the "flags" anymore, 176e21268efSVladimir Oltean * so that forces us to be slightly sloppy here, and just assume that 177e21268efSVladimir Oltean * if we didn't find an outer_tagging_rule it means that there was 178e21268efSVladimir Oltean * none in the first place, i.e. rxvlan_del is called on a non-pvid 179e21268efSVladimir Oltean * port. This is most probably true though. 180e21268efSVladimir Oltean */ 181e21268efSVladimir Oltean if (!outer_tagging_rule) 182e21268efSVladimir Oltean return 0; 183e21268efSVladimir Oltean 184e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 185e21268efSVladimir Oltean } 186e21268efSVladimir Oltean 187e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid) 188e21268efSVladimir Oltean { 189e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 190e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 191e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 192e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 193e21268efSVladimir Oltean int err; 194e21268efSVladimir Oltean 195e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 196e21268efSVladimir Oltean return 0; 197e21268efSVladimir Oltean 198e21268efSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 199e21268efSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 200e21268efSVladimir Oltean 201e21268efSVladimir Oltean untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 202e21268efSVladimir Oltean port, false); 203e21268efSVladimir Oltean if (!untagging_rule) 204e21268efSVladimir Oltean return 0; 205e21268efSVladimir Oltean 206e21268efSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, untagging_rule); 207e21268efSVladimir Oltean if (err) 208e21268efSVladimir Oltean return err; 209e21268efSVladimir Oltean 210e21268efSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 211e21268efSVladimir Oltean port, false); 212e21268efSVladimir Oltean if (!redirect_rule) 213e21268efSVladimir Oltean return 0; 214e21268efSVladimir Oltean 215e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 216e21268efSVladimir Oltean } 217e21268efSVladimir Oltean 218e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 219e21268efSVladimir Oltean { 220e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 221e21268efSVladimir Oltean 222e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 223e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot), 224e21268efSVladimir Oltean port, vid); 225e21268efSVladimir Oltean 226e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 227e21268efSVladimir Oltean return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot), 228e21268efSVladimir Oltean port, vid); 229e21268efSVladimir Oltean 230e21268efSVladimir Oltean return 0; 231e21268efSVladimir Oltean } 232e21268efSVladimir Oltean 233e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC 234e21268efSVladimir Oltean * connected internally to the enetc or fman DSA master can be configured to 235e21268efSVladimir Oltean * use the software-defined tag_8021q frame format. As far as the hardware is 236e21268efSVladimir Oltean * concerned, it thinks it is a "dumb switch" - the queues of the CPU port 237e21268efSVladimir Oltean * module are now disconnected from it, but can still be accessed through 238e21268efSVladimir Oltean * register-based MMIO. 239e21268efSVladimir Oltean */ 240e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(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 = true; 245e21268efSVladimir Oltean ocelot->npi = -1; 246e21268efSVladimir Oltean 247e21268efSVladimir Oltean /* Overwrite PGID_CPU with the non-tagging port */ 248e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, 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 255e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port) 256e21268efSVladimir Oltean { 2578abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2588abe1970SVladimir Oltean 259e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = false; 260e21268efSVladimir Oltean 261e21268efSVladimir Oltean /* Restore PGID_CPU */ 262e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID, 263e21268efSVladimir Oltean PGID_CPU); 264e21268efSVladimir Oltean 2658abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2668abe1970SVladimir Oltean 2678abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 268e21268efSVladimir Oltean } 269e21268efSVladimir Oltean 27099348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be 27199348004SVladimir Oltean * replicated over Ethernet as well, otherwise we'd get no notification of 27299348004SVladimir Oltean * their arrival when using the ocelot-8021q tagging protocol. 273c8c0ba4fSVladimir Oltean */ 27499348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds, 27599348004SVladimir Oltean bool using_tag_8021q) 276c8c0ba4fSVladimir Oltean { 27799348004SVladimir Oltean struct ocelot *ocelot = ds->priv; 27899348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 27999348004SVladimir Oltean struct ocelot_vcap_filter *trap; 28099348004SVladimir Oltean enum ocelot_mask_mode mask_mode; 28199348004SVladimir Oltean unsigned long port_mask; 2822960bb14SVladimir Oltean struct dsa_port *dp; 28399348004SVladimir Oltean bool cpu_copy_ena; 28499348004SVladimir Oltean int cpu = -1, err; 285c8c0ba4fSVladimir Oltean 28699348004SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 28799348004SVladimir Oltean return 0; 288c8c0ba4fSVladimir Oltean 28999348004SVladimir Oltean /* Figure out the current CPU port */ 2902960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 2912960bb14SVladimir Oltean cpu = dp->index; 2928d5f7954SVladimir Oltean break; 293c8c0ba4fSVladimir Oltean } 2948d5f7954SVladimir Oltean 295d78637a8SVladimir Oltean /* We are sure that "cpu" was found, otherwise 296d78637a8SVladimir Oltean * dsa_tree_setup_default_cpu() would have failed earlier. 297d78637a8SVladimir Oltean */ 298c8c0ba4fSVladimir Oltean 29999348004SVladimir Oltean /* Make sure all traps are set up for that destination */ 30099348004SVladimir Oltean list_for_each_entry(trap, &ocelot->traps, trap_list) { 30199348004SVladimir Oltean /* Figure out the current trapping destination */ 30299348004SVladimir Oltean if (using_tag_8021q) { 30399348004SVladimir Oltean /* Redirect to the tag_8021q CPU port. If timestamps 30499348004SVladimir Oltean * are necessary, also copy trapped packets to the CPU 30599348004SVladimir Oltean * port module. 306c8c0ba4fSVladimir Oltean */ 30799348004SVladimir Oltean mask_mode = OCELOT_MASK_MODE_REDIRECT; 30899348004SVladimir Oltean port_mask = BIT(cpu); 30999348004SVladimir Oltean cpu_copy_ena = !!trap->take_ts; 310c8c0ba4fSVladimir Oltean } else { 31199348004SVladimir Oltean /* Trap packets only to the CPU port module, which is 31299348004SVladimir Oltean * redirected to the NPI port (the DSA CPU port) 313c8c0ba4fSVladimir Oltean */ 31499348004SVladimir Oltean mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 31599348004SVladimir Oltean port_mask = 0; 31699348004SVladimir Oltean cpu_copy_ena = true; 317c8c0ba4fSVladimir Oltean } 318c8c0ba4fSVladimir Oltean 31999348004SVladimir Oltean if (trap->action.mask_mode == mask_mode && 32099348004SVladimir Oltean trap->action.port_mask == port_mask && 32199348004SVladimir Oltean trap->action.cpu_copy_ena == cpu_copy_ena) 32299348004SVladimir Oltean continue; 323c8c0ba4fSVladimir Oltean 32499348004SVladimir Oltean trap->action.mask_mode = mask_mode; 32599348004SVladimir Oltean trap->action.port_mask = port_mask; 32699348004SVladimir Oltean trap->action.cpu_copy_ena = cpu_copy_ena; 3270a6f17c6SVladimir Oltean 32899348004SVladimir Oltean err = ocelot_vcap_filter_replace(ocelot, trap); 329c8c0ba4fSVladimir Oltean if (err) 330c8c0ba4fSVladimir Oltean return err; 33199348004SVladimir Oltean } 332c8c0ba4fSVladimir Oltean 33399348004SVladimir Oltean return 0; 334c8c0ba4fSVladimir Oltean } 335c8c0ba4fSVladimir Oltean 336e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu) 337e21268efSVladimir Oltean { 338e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 339e21268efSVladimir Oltean unsigned long cpu_flood; 3402960bb14SVladimir Oltean struct dsa_port *dp; 3412960bb14SVladimir Oltean int err; 342e21268efSVladimir Oltean 343e21268efSVladimir Oltean felix_8021q_cpu_port_init(ocelot, cpu); 344e21268efSVladimir Oltean 3452960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 346e21268efSVladimir Oltean /* This overwrites ocelot_init(): 347e21268efSVladimir Oltean * Do not forward BPDU frames to the CPU port module, 348e21268efSVladimir Oltean * for 2 reasons: 349e21268efSVladimir Oltean * - When these packets are injected from the tag_8021q 350e21268efSVladimir Oltean * CPU port, we want them to go out, not loop back 351e21268efSVladimir Oltean * into the system. 352e21268efSVladimir Oltean * - STP traffic ingressing on a user port should go to 353e21268efSVladimir Oltean * the tag_8021q CPU port, not to the hardware CPU 354e21268efSVladimir Oltean * port module. 355e21268efSVladimir Oltean */ 356e21268efSVladimir Oltean ocelot_write_gix(ocelot, 357e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 3582960bb14SVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, dp->index); 359e21268efSVladimir Oltean } 360e21268efSVladimir Oltean 361c8c0ba4fSVladimir Oltean /* In tag_8021q mode, the CPU port module is unused, except for PTP 362c8c0ba4fSVladimir Oltean * frames. So we want to disable flooding of any kind to the CPU port 363c8c0ba4fSVladimir Oltean * module, since packets going there will end in a black hole. 364e21268efSVladimir Oltean */ 365e21268efSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 366e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC); 367e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC); 368b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC); 369e21268efSVladimir Oltean 3705da11eb4SVladimir Oltean err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD)); 371e21268efSVladimir Oltean if (err) 372d7b1fd52SVladimir Oltean return err; 373d7b1fd52SVladimir Oltean 37499348004SVladimir Oltean err = felix_update_trapping_destinations(ds, true); 375d7b1fd52SVladimir Oltean if (err) 376d7b1fd52SVladimir Oltean goto out_tag_8021q_unregister; 377e21268efSVladimir Oltean 37899348004SVladimir Oltean /* The ownership of the CPU port module's queues might have just been 37999348004SVladimir Oltean * transferred to the tag_8021q tagger from the NPI-based tagger. 38099348004SVladimir Oltean * So there might still be all sorts of crap in the queues. On the 38199348004SVladimir Oltean * other hand, the MMIO-based matching of PTP frames is very brittle, 38299348004SVladimir Oltean * so we need to be careful that there are no extra frames to be 38399348004SVladimir Oltean * dequeued over MMIO, since we would never know to discard them. 38499348004SVladimir Oltean */ 38599348004SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 38699348004SVladimir Oltean 387e21268efSVladimir Oltean return 0; 388e21268efSVladimir Oltean 389d7b1fd52SVladimir Oltean out_tag_8021q_unregister: 390d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 391e21268efSVladimir Oltean return err; 392e21268efSVladimir Oltean } 393e21268efSVladimir Oltean 394e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu) 395e21268efSVladimir Oltean { 396e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 3972960bb14SVladimir Oltean struct dsa_port *dp; 3982960bb14SVladimir Oltean int err; 399e21268efSVladimir Oltean 40099348004SVladimir Oltean err = felix_update_trapping_destinations(ds, false); 401c8c0ba4fSVladimir Oltean if (err) 402c8c0ba4fSVladimir Oltean dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d", 403c8c0ba4fSVladimir Oltean err); 404c8c0ba4fSVladimir Oltean 405d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 406e21268efSVladimir Oltean 4072960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 408e21268efSVladimir Oltean /* Restore the logic from ocelot_init: 409e21268efSVladimir Oltean * do not forward BPDU frames to the front ports. 410e21268efSVladimir Oltean */ 411e21268efSVladimir Oltean ocelot_write_gix(ocelot, 412e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 413e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, 4142960bb14SVladimir Oltean dp->index); 415e21268efSVladimir Oltean } 416e21268efSVladimir Oltean 417e21268efSVladimir Oltean felix_8021q_cpu_port_deinit(ocelot, cpu); 418e21268efSVladimir Oltean } 419e21268efSVladimir Oltean 420adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This 421adb3dccfSVladimir Oltean * is the mode through which frames can be injected from and extracted to an 422adb3dccfSVladimir Oltean * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 423adb3dccfSVladimir Oltean * running Linux, and this forms a DSA setup together with the enetc or fman 424adb3dccfSVladimir Oltean * DSA master. 425adb3dccfSVladimir Oltean */ 426adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port) 427adb3dccfSVladimir Oltean { 428adb3dccfSVladimir Oltean ocelot->npi = port; 429adb3dccfSVladimir Oltean 430adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 431adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 432adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 433adb3dccfSVladimir Oltean 434adb3dccfSVladimir Oltean /* NPI port Injection/Extraction configuration */ 435adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 436adb3dccfSVladimir Oltean ocelot->npi_xtr_prefix); 437adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 438adb3dccfSVladimir Oltean ocelot->npi_inj_prefix); 439adb3dccfSVladimir Oltean 440adb3dccfSVladimir Oltean /* Disable transmission of pause frames */ 441adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 442adb3dccfSVladimir Oltean } 443adb3dccfSVladimir Oltean 444adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 445adb3dccfSVladimir Oltean { 446adb3dccfSVladimir Oltean /* Restore hardware defaults */ 447adb3dccfSVladimir Oltean int unused_port = ocelot->num_phys_ports + 2; 448adb3dccfSVladimir Oltean 449adb3dccfSVladimir Oltean ocelot->npi = -1; 450adb3dccfSVladimir Oltean 451adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 452adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 453adb3dccfSVladimir Oltean 454adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 455adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 456adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 457adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 458adb3dccfSVladimir Oltean 459adb3dccfSVladimir Oltean /* Enable transmission of pause frames */ 460adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 461adb3dccfSVladimir Oltean } 462adb3dccfSVladimir Oltean 463adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu) 464adb3dccfSVladimir Oltean { 465adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 466adb3dccfSVladimir Oltean unsigned long cpu_flood; 467adb3dccfSVladimir Oltean 468adb3dccfSVladimir Oltean felix_npi_port_init(ocelot, cpu); 469adb3dccfSVladimir Oltean 470adb3dccfSVladimir Oltean /* Include the CPU port module (and indirectly, the NPI port) 471adb3dccfSVladimir Oltean * in the forwarding mask for unknown unicast - the hardware 472adb3dccfSVladimir Oltean * default value for ANA_FLOODING_FLD_UNICAST excludes 473adb3dccfSVladimir Oltean * BIT(ocelot->num_phys_ports), and so does ocelot_init, 474adb3dccfSVladimir Oltean * since Ocelot relies on whitelisting MAC addresses towards 475adb3dccfSVladimir Oltean * PGID_CPU. 476adb3dccfSVladimir Oltean * We do this because DSA does not yet perform RX filtering, 477adb3dccfSVladimir Oltean * and the NPI port does not perform source address learning, 478adb3dccfSVladimir Oltean * so traffic sent to Linux is effectively unknown from the 479adb3dccfSVladimir Oltean * switch's perspective. 480adb3dccfSVladimir Oltean */ 481adb3dccfSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 482adb3dccfSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC); 4836edb9e8dSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC); 484b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC); 485adb3dccfSVladimir Oltean 486adb3dccfSVladimir Oltean return 0; 487adb3dccfSVladimir Oltean } 488adb3dccfSVladimir Oltean 489adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu) 490adb3dccfSVladimir Oltean { 491adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 492adb3dccfSVladimir Oltean 493adb3dccfSVladimir Oltean felix_npi_port_deinit(ocelot, cpu); 494adb3dccfSVladimir Oltean } 495adb3dccfSVladimir Oltean 496adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu, 497adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 498adb3dccfSVladimir Oltean { 499adb3dccfSVladimir Oltean int err; 500adb3dccfSVladimir Oltean 501adb3dccfSVladimir Oltean switch (proto) { 5027c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 503adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 504adb3dccfSVladimir Oltean err = felix_setup_tag_npi(ds, cpu); 505adb3dccfSVladimir Oltean break; 506e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 507e21268efSVladimir Oltean err = felix_setup_tag_8021q(ds, cpu); 508e21268efSVladimir Oltean break; 509adb3dccfSVladimir Oltean default: 510adb3dccfSVladimir Oltean err = -EPROTONOSUPPORT; 511adb3dccfSVladimir Oltean } 512adb3dccfSVladimir Oltean 513adb3dccfSVladimir Oltean return err; 514adb3dccfSVladimir Oltean } 515adb3dccfSVladimir Oltean 516adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu, 517adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 518adb3dccfSVladimir Oltean { 519adb3dccfSVladimir Oltean switch (proto) { 5207c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 521adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 522adb3dccfSVladimir Oltean felix_teardown_tag_npi(ds, cpu); 523adb3dccfSVladimir Oltean break; 524e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 525e21268efSVladimir Oltean felix_teardown_tag_8021q(ds, cpu); 526e21268efSVladimir Oltean break; 527adb3dccfSVladimir Oltean default: 528adb3dccfSVladimir Oltean break; 529adb3dccfSVladimir Oltean } 530adb3dccfSVladimir Oltean } 531adb3dccfSVladimir Oltean 532e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the 533e21268efSVladimir Oltean * tag_8021q setup can fail, the NPI setup can't. So either the change is made, 534e21268efSVladimir Oltean * or the restoration is guaranteed to work. 535e21268efSVladimir Oltean */ 536adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu, 537adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 538adb3dccfSVladimir Oltean { 539adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 540adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 541adb3dccfSVladimir Oltean enum dsa_tag_protocol old_proto = felix->tag_proto; 542adb3dccfSVladimir Oltean int err; 543adb3dccfSVladimir Oltean 5447c4bb540SVladimir Oltean if (proto != DSA_TAG_PROTO_SEVILLE && 5457c4bb540SVladimir Oltean proto != DSA_TAG_PROTO_OCELOT && 546e21268efSVladimir Oltean proto != DSA_TAG_PROTO_OCELOT_8021Q) 547adb3dccfSVladimir Oltean return -EPROTONOSUPPORT; 548adb3dccfSVladimir Oltean 549adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, cpu, old_proto); 550adb3dccfSVladimir Oltean 551adb3dccfSVladimir Oltean err = felix_set_tag_protocol(ds, cpu, proto); 552adb3dccfSVladimir Oltean if (err) { 553adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, cpu, old_proto); 554adb3dccfSVladimir Oltean return err; 555adb3dccfSVladimir Oltean } 556adb3dccfSVladimir Oltean 557adb3dccfSVladimir Oltean felix->tag_proto = proto; 558adb3dccfSVladimir Oltean 559adb3dccfSVladimir Oltean return 0; 560adb3dccfSVladimir Oltean } 561adb3dccfSVladimir Oltean 56256051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 5634d776482SFlorian Fainelli int port, 5644d776482SFlorian Fainelli enum dsa_tag_protocol mp) 56556051948SVladimir Oltean { 566adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 567adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 568adb3dccfSVladimir Oltean 569adb3dccfSVladimir Oltean return felix->tag_proto; 57056051948SVladimir Oltean } 57156051948SVladimir Oltean 57256051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds, 57356051948SVladimir Oltean unsigned int ageing_time) 57456051948SVladimir Oltean { 57556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 57656051948SVladimir Oltean 57756051948SVladimir Oltean ocelot_set_ageing_time(ocelot, ageing_time); 57856051948SVladimir Oltean 57956051948SVladimir Oltean return 0; 58056051948SVladimir Oltean } 58156051948SVladimir Oltean 5825cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port) 5835cad43a5SVladimir Oltean { 5845cad43a5SVladimir Oltean struct ocelot *ocelot = ds->priv; 5855cad43a5SVladimir Oltean int err; 5865cad43a5SVladimir Oltean 5875cad43a5SVladimir Oltean err = ocelot_mact_flush(ocelot, port); 5885cad43a5SVladimir Oltean if (err) 5895cad43a5SVladimir Oltean dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n", 5905cad43a5SVladimir Oltean port, ERR_PTR(err)); 5915cad43a5SVladimir Oltean } 5925cad43a5SVladimir Oltean 59356051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port, 59456051948SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 59556051948SVladimir Oltean { 59656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 59756051948SVladimir Oltean 59856051948SVladimir Oltean return ocelot_fdb_dump(ocelot, port, cb, data); 59956051948SVladimir Oltean } 60056051948SVladimir Oltean 60156051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port, 60256051948SVladimir Oltean const unsigned char *addr, u16 vid) 60356051948SVladimir Oltean { 60456051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 60556051948SVladimir Oltean 60687b0f983SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid); 60756051948SVladimir Oltean } 60856051948SVladimir Oltean 60956051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port, 61056051948SVladimir Oltean const unsigned char *addr, u16 vid) 61156051948SVladimir Oltean { 61256051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 61356051948SVladimir Oltean 61456051948SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid); 61556051948SVladimir Oltean } 61656051948SVladimir Oltean 617*961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag, 618*961d8b69SVladimir Oltean const unsigned char *addr, u16 vid) 619*961d8b69SVladimir Oltean { 620*961d8b69SVladimir Oltean struct ocelot *ocelot = ds->priv; 621*961d8b69SVladimir Oltean 622*961d8b69SVladimir Oltean return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid); 623*961d8b69SVladimir Oltean } 624*961d8b69SVladimir Oltean 625*961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag, 626*961d8b69SVladimir Oltean const unsigned char *addr, u16 vid) 627*961d8b69SVladimir Oltean { 628*961d8b69SVladimir Oltean struct ocelot *ocelot = ds->priv; 629*961d8b69SVladimir Oltean 630*961d8b69SVladimir Oltean return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid); 631*961d8b69SVladimir Oltean } 632*961d8b69SVladimir Oltean 633a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 634209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 635209edf95SVladimir Oltean { 636209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 637209edf95SVladimir Oltean 638a52b2da7SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb); 639209edf95SVladimir Oltean } 640209edf95SVladimir Oltean 641209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port, 642209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 643209edf95SVladimir Oltean { 644209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 645209edf95SVladimir Oltean 646209edf95SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb); 647209edf95SVladimir Oltean } 648209edf95SVladimir Oltean 64956051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 65056051948SVladimir Oltean u8 state) 65156051948SVladimir Oltean { 65256051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 65356051948SVladimir Oltean 65456051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 65556051948SVladimir Oltean } 65656051948SVladimir Oltean 657421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 658421741eaSVladimir Oltean struct switchdev_brport_flags val, 659421741eaSVladimir Oltean struct netlink_ext_ack *extack) 660421741eaSVladimir Oltean { 661421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 662421741eaSVladimir Oltean 663421741eaSVladimir Oltean return ocelot_port_pre_bridge_flags(ocelot, port, val); 664421741eaSVladimir Oltean } 665421741eaSVladimir Oltean 666421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port, 667421741eaSVladimir Oltean struct switchdev_brport_flags val, 668421741eaSVladimir Oltean struct netlink_ext_ack *extack) 669421741eaSVladimir Oltean { 670421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 671421741eaSVladimir Oltean 672421741eaSVladimir Oltean ocelot_port_bridge_flags(ocelot, port, val); 673421741eaSVladimir Oltean 674421741eaSVladimir Oltean return 0; 675421741eaSVladimir Oltean } 676421741eaSVladimir Oltean 67756051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 678b079922bSVladimir Oltean struct dsa_bridge bridge, bool *tx_fwd_offload) 67956051948SVladimir Oltean { 68056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 68156051948SVladimir Oltean 682d3eed0e5SVladimir Oltean ocelot_port_bridge_join(ocelot, port, bridge.dev); 683e4bd44e8SVladimir Oltean 684e4bd44e8SVladimir Oltean return 0; 68556051948SVladimir Oltean } 68656051948SVladimir Oltean 68756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 688d3eed0e5SVladimir Oltean struct dsa_bridge bridge) 68956051948SVladimir Oltean { 69056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 69156051948SVladimir Oltean 692d3eed0e5SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, bridge.dev); 69356051948SVladimir Oltean } 69456051948SVladimir Oltean 6958fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 696dedd6a00SVladimir Oltean struct dsa_lag lag, 6978fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 6988fe6832eSVladimir Oltean { 6998fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7008fe6832eSVladimir Oltean 701dedd6a00SVladimir Oltean return ocelot_port_lag_join(ocelot, port, lag.dev, info); 7028fe6832eSVladimir Oltean } 7038fe6832eSVladimir Oltean 7048fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 705dedd6a00SVladimir Oltean struct dsa_lag lag) 7068fe6832eSVladimir Oltean { 7078fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7088fe6832eSVladimir Oltean 709dedd6a00SVladimir Oltean ocelot_port_lag_leave(ocelot, port, lag.dev); 7108fe6832eSVladimir Oltean 7118fe6832eSVladimir Oltean return 0; 7128fe6832eSVladimir Oltean } 7138fe6832eSVladimir Oltean 7148fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 7158fe6832eSVladimir Oltean { 7168fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 7178fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7188fe6832eSVladimir Oltean 7198fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 7208fe6832eSVladimir Oltean 7218fe6832eSVladimir Oltean return 0; 7228fe6832eSVladimir Oltean } 7238fe6832eSVladimir Oltean 72456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 72501af940eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 72601af940eSVladimir Oltean struct netlink_ext_ack *extack) 72756051948SVladimir Oltean { 7282f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 729b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 7302f0402feSVladimir Oltean 7319a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 7329a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 7339a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 7349a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 7359a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 7369a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 7379a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 7389a720680SVladimir Oltean */ 7399a720680SVladimir Oltean if (port == ocelot->npi) 7409a720680SVladimir Oltean return 0; 7419a720680SVladimir Oltean 742b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 7432f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 74401af940eSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED, 74501af940eSVladimir Oltean extack); 74656051948SVladimir Oltean } 74756051948SVladimir Oltean 74889153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 74989153ed6SVladimir Oltean struct netlink_ext_ack *extack) 75056051948SVladimir Oltean { 75156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 75256051948SVladimir Oltean 7533b95d1b2SVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 75456051948SVladimir Oltean } 75556051948SVladimir Oltean 7561958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 75731046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 75831046a5fSVladimir Oltean struct netlink_ext_ack *extack) 75956051948SVladimir Oltean { 76056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 761183be6f9SVladimir Oltean u16 flags = vlan->flags; 7621958d581SVladimir Oltean int err; 76356051948SVladimir Oltean 76401af940eSVladimir Oltean err = felix_vlan_prepare(ds, port, vlan, extack); 7651958d581SVladimir Oltean if (err) 7661958d581SVladimir Oltean return err; 7671958d581SVladimir Oltean 7681958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 769183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 770183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 77156051948SVladimir Oltean } 77256051948SVladimir Oltean 77356051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 77456051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 77556051948SVladimir Oltean { 77656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 77756051948SVladimir Oltean 778b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 77956051948SVladimir Oltean } 78056051948SVladimir Oltean 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 792bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port, 793bdeced75SVladimir Oltean unsigned int link_an_mode, 794bdeced75SVladimir Oltean const struct phylink_link_state *state) 795bdeced75SVladimir Oltean { 796bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 797bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 798588d0550SIoana Ciornei struct dsa_port *dp = dsa_to_port(ds, port); 799bdeced75SVladimir Oltean 80049af6a76SColin Foster if (felix->pcs && felix->pcs[port]) 801e7026f15SColin Foster phylink_set_pcs(dp->pl, felix->pcs[port]); 802bdeced75SVladimir Oltean } 803bdeced75SVladimir Oltean 804bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 805bdeced75SVladimir Oltean unsigned int link_an_mode, 806bdeced75SVladimir Oltean phy_interface_t interface) 807bdeced75SVladimir Oltean { 808bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 809bdeced75SVladimir Oltean 810e6e12df6SVladimir Oltean ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 811e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 812bdeced75SVladimir Oltean } 813bdeced75SVladimir Oltean 814bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 815bdeced75SVladimir Oltean unsigned int link_an_mode, 816bdeced75SVladimir Oltean phy_interface_t interface, 8175b502a7bSRussell King struct phy_device *phydev, 8185b502a7bSRussell King int speed, int duplex, 8195b502a7bSRussell King bool tx_pause, bool rx_pause) 820bdeced75SVladimir Oltean { 821bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 8227e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 823bdeced75SVladimir Oltean 824e6e12df6SVladimir Oltean ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 825e6e12df6SVladimir Oltean interface, speed, duplex, tx_pause, rx_pause, 826e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 8277e14a2dcSVladimir Oltean 8287e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 8297e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 830bdeced75SVladimir Oltean } 831bdeced75SVladimir Oltean 832bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 833bd2b3161SXiaoliang Yang { 834bd2b3161SXiaoliang Yang int i; 835bd2b3161SXiaoliang Yang 836bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 837bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 838bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 839bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 840bd2b3161SXiaoliang Yang port); 841bd2b3161SXiaoliang Yang 84270d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 843bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 844bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 845bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 846bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 847bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 848bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 849bd2b3161SXiaoliang Yang port, i); 850bd2b3161SXiaoliang Yang } 851bd2b3161SXiaoliang Yang } 852bd2b3161SXiaoliang Yang 85356051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 85456051948SVladimir Oltean u32 stringset, u8 *data) 85556051948SVladimir Oltean { 85656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 85756051948SVladimir Oltean 85856051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 85956051948SVladimir Oltean } 86056051948SVladimir Oltean 86156051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 86256051948SVladimir Oltean { 86356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 86456051948SVladimir Oltean 86556051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 86656051948SVladimir Oltean } 86756051948SVladimir Oltean 86856051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 86956051948SVladimir Oltean { 87056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 87156051948SVladimir Oltean 87256051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 87356051948SVladimir Oltean } 87456051948SVladimir Oltean 87556051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 87656051948SVladimir Oltean struct ethtool_ts_info *info) 87756051948SVladimir Oltean { 87856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 87956051948SVladimir Oltean 88056051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 88156051948SVladimir Oltean } 88256051948SVladimir Oltean 883bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 884bdeced75SVladimir Oltean struct device_node *ports_node, 885bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 886bdeced75SVladimir Oltean { 887bdeced75SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 888bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 889bdeced75SVladimir Oltean struct device_node *child; 890bdeced75SVladimir Oltean 89137fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 892bdeced75SVladimir Oltean phy_interface_t phy_mode; 893bdeced75SVladimir Oltean u32 port; 894bdeced75SVladimir Oltean int err; 895bdeced75SVladimir Oltean 896bdeced75SVladimir Oltean /* Get switch port number from DT */ 897bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 898bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 899bdeced75SVladimir Oltean "(property \"reg\")\n"); 900bdeced75SVladimir Oltean of_node_put(child); 901bdeced75SVladimir Oltean return -ENODEV; 902bdeced75SVladimir Oltean } 903bdeced75SVladimir Oltean 904bdeced75SVladimir Oltean /* Get PHY mode from DT */ 905bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 906bdeced75SVladimir Oltean if (err) { 907bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 908bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 909bdeced75SVladimir Oltean port); 910bdeced75SVladimir Oltean of_node_put(child); 911bdeced75SVladimir Oltean return -ENODEV; 912bdeced75SVladimir Oltean } 913bdeced75SVladimir Oltean 914bdeced75SVladimir Oltean err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 915bdeced75SVladimir Oltean if (err < 0) { 916bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 917bdeced75SVladimir Oltean phy_modes(phy_mode), port); 91859ebb430SSumera Priyadarsini of_node_put(child); 919bdeced75SVladimir Oltean return err; 920bdeced75SVladimir Oltean } 921bdeced75SVladimir Oltean 922bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 923bdeced75SVladimir Oltean } 924bdeced75SVladimir Oltean 925bdeced75SVladimir Oltean return 0; 926bdeced75SVladimir Oltean } 927bdeced75SVladimir Oltean 928bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 929bdeced75SVladimir Oltean { 930bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 931bdeced75SVladimir Oltean struct device_node *switch_node; 932bdeced75SVladimir Oltean struct device_node *ports_node; 933bdeced75SVladimir Oltean int err; 934bdeced75SVladimir Oltean 935bdeced75SVladimir Oltean switch_node = dev->of_node; 936bdeced75SVladimir Oltean 937bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 938abecbfcdSVladimir Oltean if (!ports_node) 939abecbfcdSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 940bdeced75SVladimir Oltean if (!ports_node) { 941abecbfcdSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 942bdeced75SVladimir Oltean return -ENODEV; 943bdeced75SVladimir Oltean } 944bdeced75SVladimir Oltean 945bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 946bdeced75SVladimir Oltean of_node_put(ports_node); 947bdeced75SVladimir Oltean 948bdeced75SVladimir Oltean return err; 949bdeced75SVladimir Oltean } 950bdeced75SVladimir Oltean 95156051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 95256051948SVladimir Oltean { 95356051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 954bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 955b4024c9eSClaudiu Manoil struct resource res; 95656051948SVladimir Oltean int port, i, err; 95756051948SVladimir Oltean 95856051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 95956051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 96056051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 96156051948SVladimir Oltean if (!ocelot->ports) 96256051948SVladimir Oltean return -ENOMEM; 96356051948SVladimir Oltean 96456051948SVladimir Oltean ocelot->map = felix->info->map; 96556051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 96656051948SVladimir Oltean ocelot->num_stats = felix->info->num_stats; 96721ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 96807d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 96977043c37SXiaoliang Yang ocelot->vcap_pol.base = felix->info->vcap_pol_base; 97077043c37SXiaoliang Yang ocelot->vcap_pol.max = felix->info->vcap_pol_max; 97177043c37SXiaoliang Yang ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 97277043c37SXiaoliang Yang ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 97356051948SVladimir Oltean ocelot->ops = felix->info->ops; 974cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 975cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 976f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 97756051948SVladimir Oltean 978bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 979bdeced75SVladimir Oltean GFP_KERNEL); 980bdeced75SVladimir Oltean if (!port_phy_modes) 981bdeced75SVladimir Oltean return -ENOMEM; 982bdeced75SVladimir Oltean 983bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 984bdeced75SVladimir Oltean if (err) { 985bdeced75SVladimir Oltean kfree(port_phy_modes); 986bdeced75SVladimir Oltean return err; 987bdeced75SVladimir Oltean } 988bdeced75SVladimir Oltean 98956051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 99056051948SVladimir Oltean struct regmap *target; 99156051948SVladimir Oltean 99256051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 99356051948SVladimir Oltean continue; 99456051948SVladimir Oltean 995b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 996b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 997375e1314SVladimir Oltean res.start += felix->switch_base; 998375e1314SVladimir Oltean res.end += felix->switch_base; 99956051948SVladimir Oltean 1000242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 100156051948SVladimir Oltean if (IS_ERR(target)) { 100256051948SVladimir Oltean dev_err(ocelot->dev, 100356051948SVladimir Oltean "Failed to map device memory space\n"); 1004bdeced75SVladimir Oltean kfree(port_phy_modes); 100556051948SVladimir Oltean return PTR_ERR(target); 100656051948SVladimir Oltean } 100756051948SVladimir Oltean 100856051948SVladimir Oltean ocelot->targets[i] = target; 100956051948SVladimir Oltean } 101056051948SVladimir Oltean 101156051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 101256051948SVladimir Oltean if (err) { 101356051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 1014bdeced75SVladimir Oltean kfree(port_phy_modes); 101556051948SVladimir Oltean return err; 101656051948SVladimir Oltean } 101756051948SVladimir Oltean 101856051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 101956051948SVladimir Oltean struct ocelot_port *ocelot_port; 102091c724cfSVladimir Oltean struct regmap *target; 102156051948SVladimir Oltean 102256051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 102356051948SVladimir Oltean sizeof(struct ocelot_port), 102456051948SVladimir Oltean GFP_KERNEL); 102556051948SVladimir Oltean if (!ocelot_port) { 102656051948SVladimir Oltean dev_err(ocelot->dev, 102756051948SVladimir Oltean "failed to allocate port memory\n"); 1028bdeced75SVladimir Oltean kfree(port_phy_modes); 102956051948SVladimir Oltean return -ENOMEM; 103056051948SVladimir Oltean } 103156051948SVladimir Oltean 1032b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1033b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1034375e1314SVladimir Oltean res.start += felix->switch_base; 1035375e1314SVladimir Oltean res.end += felix->switch_base; 103656051948SVladimir Oltean 1037242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 103891c724cfSVladimir Oltean if (IS_ERR(target)) { 103956051948SVladimir Oltean dev_err(ocelot->dev, 104091c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 104191c724cfSVladimir Oltean port); 1042bdeced75SVladimir Oltean kfree(port_phy_modes); 104391c724cfSVladimir Oltean return PTR_ERR(target); 104456051948SVladimir Oltean } 104556051948SVladimir Oltean 1046bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 104756051948SVladimir Oltean ocelot_port->ocelot = ocelot; 104891c724cfSVladimir Oltean ocelot_port->target = target; 104956051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 105056051948SVladimir Oltean } 105156051948SVladimir Oltean 1052bdeced75SVladimir Oltean kfree(port_phy_modes); 1053bdeced75SVladimir Oltean 1054bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1055bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1056bdeced75SVladimir Oltean if (err < 0) 1057bdeced75SVladimir Oltean return err; 1058bdeced75SVladimir Oltean } 1059bdeced75SVladimir Oltean 106056051948SVladimir Oltean return 0; 106156051948SVladimir Oltean } 106256051948SVladimir Oltean 10631328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 10641328a883SVladimir Oltean struct sk_buff *skb) 10651328a883SVladimir Oltean { 10661328a883SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 10671328a883SVladimir Oltean struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 10681328a883SVladimir Oltean struct sk_buff *skb_match = NULL, *skb_tmp; 10691328a883SVladimir Oltean unsigned long flags; 10701328a883SVladimir Oltean 10711328a883SVladimir Oltean if (!clone) 10721328a883SVladimir Oltean return; 10731328a883SVladimir Oltean 10741328a883SVladimir Oltean spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 10751328a883SVladimir Oltean 10761328a883SVladimir Oltean skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 10771328a883SVladimir Oltean if (skb != clone) 10781328a883SVladimir Oltean continue; 10791328a883SVladimir Oltean __skb_unlink(skb, &ocelot_port->tx_skbs); 10801328a883SVladimir Oltean skb_match = skb; 10811328a883SVladimir Oltean break; 10821328a883SVladimir Oltean } 10831328a883SVladimir Oltean 10841328a883SVladimir Oltean spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 10851328a883SVladimir Oltean 10861328a883SVladimir Oltean WARN_ONCE(!skb_match, 10871328a883SVladimir Oltean "Could not find skb clone in TX timestamping list\n"); 10881328a883SVladimir Oltean } 10891328a883SVladimir Oltean 109049f885b2SVladimir Oltean #define work_to_xmit_work(w) \ 109149f885b2SVladimir Oltean container_of((w), struct felix_deferred_xmit_work, work) 109249f885b2SVladimir Oltean 109349f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work) 109449f885b2SVladimir Oltean { 109549f885b2SVladimir Oltean struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 109649f885b2SVladimir Oltean struct dsa_switch *ds = xmit_work->dp->ds; 109749f885b2SVladimir Oltean struct sk_buff *skb = xmit_work->skb; 109849f885b2SVladimir Oltean u32 rew_op = ocelot_ptp_rew_op(skb); 109949f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 110049f885b2SVladimir Oltean int port = xmit_work->dp->index; 110149f885b2SVladimir Oltean int retries = 10; 110249f885b2SVladimir Oltean 110349f885b2SVladimir Oltean do { 110449f885b2SVladimir Oltean if (ocelot_can_inject(ocelot, 0)) 110549f885b2SVladimir Oltean break; 110649f885b2SVladimir Oltean 110749f885b2SVladimir Oltean cpu_relax(); 110849f885b2SVladimir Oltean } while (--retries); 110949f885b2SVladimir Oltean 111049f885b2SVladimir Oltean if (!retries) { 111149f885b2SVladimir Oltean dev_err(ocelot->dev, "port %d failed to inject skb\n", 111249f885b2SVladimir Oltean port); 11131328a883SVladimir Oltean ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 111449f885b2SVladimir Oltean kfree_skb(skb); 111549f885b2SVladimir Oltean return; 111649f885b2SVladimir Oltean } 111749f885b2SVladimir Oltean 111849f885b2SVladimir Oltean ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 111949f885b2SVladimir Oltean 112049f885b2SVladimir Oltean consume_skb(skb); 112149f885b2SVladimir Oltean kfree(xmit_work); 112249f885b2SVladimir Oltean } 112349f885b2SVladimir Oltean 112435d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds, 112535d97680SVladimir Oltean enum dsa_tag_protocol proto) 112649f885b2SVladimir Oltean { 112735d97680SVladimir Oltean struct ocelot_8021q_tagger_data *tagger_data; 112849f885b2SVladimir Oltean 112935d97680SVladimir Oltean switch (proto) { 113035d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 113135d97680SVladimir Oltean tagger_data = ocelot_8021q_tagger_data(ds); 113235d97680SVladimir Oltean tagger_data->xmit_work_fn = felix_port_deferred_xmit; 113349f885b2SVladimir Oltean return 0; 113435d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT: 113535d97680SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 113649f885b2SVladimir Oltean return 0; 113735d97680SVladimir Oltean default: 113835d97680SVladimir Oltean return -EPROTONOSUPPORT; 113949f885b2SVladimir Oltean } 114049f885b2SVladimir Oltean } 114149f885b2SVladimir Oltean 114256051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with 114356051948SVladimir Oltean * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 114456051948SVladimir Oltean * us to allocate structures twice (leak memory) and map PCI memory twice 114556051948SVladimir Oltean * (which will not work). 114656051948SVladimir Oltean */ 114756051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds) 114856051948SVladimir Oltean { 114956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 115056051948SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 11512960bb14SVladimir Oltean struct dsa_port *dp; 11522960bb14SVladimir Oltean int err; 115356051948SVladimir Oltean 115456051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 115556051948SVladimir Oltean if (err) 115656051948SVladimir Oltean return err; 115756051948SVladimir Oltean 1158d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1159d1cc0e93SVladimir Oltean if (err) 11606b73b7c9SVladimir Oltean goto out_mdiobus_free; 1161d1cc0e93SVladimir Oltean 11622b49d128SYangbo Lu if (ocelot->ptp) { 11632ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 11642b49d128SYangbo Lu if (err) { 11652b49d128SYangbo Lu dev_err(ocelot->dev, 11662b49d128SYangbo Lu "Timestamp initialization failed\n"); 11672b49d128SYangbo Lu ocelot->ptp = 0; 11682b49d128SYangbo Lu } 11692b49d128SYangbo Lu } 117056051948SVladimir Oltean 11712960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 11722960bb14SVladimir Oltean ocelot_init_port(ocelot, dp->index); 1173bd2b3161SXiaoliang Yang 1174bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1175bd2b3161SXiaoliang Yang * bits of vlan tag. 1176bd2b3161SXiaoliang Yang */ 11772960bb14SVladimir Oltean felix_port_qos_map_init(ocelot, dp->index); 117856051948SVladimir Oltean } 117956051948SVladimir Oltean 1180f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1181f59fd9caSVladimir Oltean if (err) 11826b73b7c9SVladimir Oltean goto out_deinit_ports; 1183f59fd9caSVladimir Oltean 11842960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 1185adb3dccfSVladimir Oltean /* The initial tag protocol is NPI which always returns 0, so 1186adb3dccfSVladimir Oltean * there's no real point in checking for errors. 11871cf3299bSVladimir Oltean */ 11882960bb14SVladimir Oltean felix_set_tag_protocol(ds, dp->index, felix->tag_proto); 11898d5f7954SVladimir Oltean break; 1190adb3dccfSVladimir Oltean } 11911cf3299bSVladimir Oltean 11920b912fc9SVladimir Oltean ds->mtu_enforcement_ingress = true; 1193c54913c1SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 1194bdeced75SVladimir Oltean 119556051948SVladimir Oltean return 0; 11966b73b7c9SVladimir Oltean 11976b73b7c9SVladimir Oltean out_deinit_ports: 11982960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 11992960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 12006b73b7c9SVladimir Oltean 12016b73b7c9SVladimir Oltean ocelot_deinit_timestamp(ocelot); 12026b73b7c9SVladimir Oltean ocelot_deinit(ocelot); 12036b73b7c9SVladimir Oltean 12046b73b7c9SVladimir Oltean out_mdiobus_free: 12056b73b7c9SVladimir Oltean if (felix->info->mdio_bus_free) 12066b73b7c9SVladimir Oltean felix->info->mdio_bus_free(ocelot); 12076b73b7c9SVladimir Oltean 12086b73b7c9SVladimir Oltean return err; 120956051948SVladimir Oltean } 121056051948SVladimir Oltean 121156051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 121256051948SVladimir Oltean { 121356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1214bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12152960bb14SVladimir Oltean struct dsa_port *dp; 1216bdeced75SVladimir Oltean 12172960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 12182960bb14SVladimir Oltean felix_del_tag_protocol(ds, dp->index, felix->tag_proto); 12198d5f7954SVladimir Oltean break; 1220adb3dccfSVladimir Oltean } 1221adb3dccfSVladimir Oltean 12222960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 12232960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 1224d19741b0SVladimir Oltean 122549f885b2SVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 122649f885b2SVladimir Oltean ocelot_deinit_timestamp(ocelot); 122749f885b2SVladimir Oltean ocelot_deinit(ocelot); 122849f885b2SVladimir Oltean 1229d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1230d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 123156051948SVladimir Oltean } 123256051948SVladimir Oltean 1233c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1234c0bcf537SYangbo Lu struct ifreq *ifr) 1235c0bcf537SYangbo Lu { 1236c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1237c0bcf537SYangbo Lu 1238c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1239c0bcf537SYangbo Lu } 1240c0bcf537SYangbo Lu 1241c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1242c0bcf537SYangbo Lu struct ifreq *ifr) 1243c0bcf537SYangbo Lu { 1244c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 124599348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 124699348004SVladimir Oltean bool using_tag_8021q; 124799348004SVladimir Oltean int err; 1248c0bcf537SYangbo Lu 124999348004SVladimir Oltean err = ocelot_hwstamp_set(ocelot, port, ifr); 125099348004SVladimir Oltean if (err) 125199348004SVladimir Oltean return err; 125299348004SVladimir Oltean 125399348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 125499348004SVladimir Oltean 125599348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 1256c0bcf537SYangbo Lu } 1257c0bcf537SYangbo Lu 12580a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type) 12590a6f17c6SVladimir Oltean { 12600a6f17c6SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12610a6f17c6SVladimir Oltean int err, grp = 0; 12620a6f17c6SVladimir Oltean 12630a6f17c6SVladimir Oltean if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 12640a6f17c6SVladimir Oltean return false; 12650a6f17c6SVladimir Oltean 12660a6f17c6SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 12670a6f17c6SVladimir Oltean return false; 12680a6f17c6SVladimir Oltean 12690a6f17c6SVladimir Oltean if (ptp_type == PTP_CLASS_NONE) 12700a6f17c6SVladimir Oltean return false; 12710a6f17c6SVladimir Oltean 12720a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 12730a6f17c6SVladimir Oltean struct sk_buff *skb; 12740a6f17c6SVladimir Oltean unsigned int type; 12750a6f17c6SVladimir Oltean 12760a6f17c6SVladimir Oltean err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 12770a6f17c6SVladimir Oltean if (err) 12780a6f17c6SVladimir Oltean goto out; 12790a6f17c6SVladimir Oltean 12800a6f17c6SVladimir Oltean /* We trap to the CPU port module all PTP frames, but 12810a6f17c6SVladimir Oltean * felix_rxtstamp() only gets called for event frames. 12820a6f17c6SVladimir Oltean * So we need to avoid sending duplicate general 12830a6f17c6SVladimir Oltean * message frames by running a second BPF classifier 12840a6f17c6SVladimir Oltean * here and dropping those. 12850a6f17c6SVladimir Oltean */ 12860a6f17c6SVladimir Oltean __skb_push(skb, ETH_HLEN); 12870a6f17c6SVladimir Oltean 12880a6f17c6SVladimir Oltean type = ptp_classify_raw(skb); 12890a6f17c6SVladimir Oltean 12900a6f17c6SVladimir Oltean __skb_pull(skb, ETH_HLEN); 12910a6f17c6SVladimir Oltean 12920a6f17c6SVladimir Oltean if (type == PTP_CLASS_NONE) { 12930a6f17c6SVladimir Oltean kfree_skb(skb); 12940a6f17c6SVladimir Oltean continue; 12950a6f17c6SVladimir Oltean } 12960a6f17c6SVladimir Oltean 12970a6f17c6SVladimir Oltean netif_rx(skb); 12980a6f17c6SVladimir Oltean } 12990a6f17c6SVladimir Oltean 13000a6f17c6SVladimir Oltean out: 13010a6f17c6SVladimir Oltean if (err < 0) 13020a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 13030a6f17c6SVladimir Oltean 13040a6f17c6SVladimir Oltean return true; 13050a6f17c6SVladimir Oltean } 13060a6f17c6SVladimir Oltean 1307c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1308c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1309c0bcf537SYangbo Lu { 131092f62485SVladimir Oltean u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1311c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1312c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1313c0bcf537SYangbo Lu struct timespec64 ts; 131492f62485SVladimir Oltean u32 tstamp_hi; 131592f62485SVladimir Oltean u64 tstamp; 1316c0bcf537SYangbo Lu 13170a6f17c6SVladimir Oltean /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 13180a6f17c6SVladimir Oltean * for RX timestamping. Then free it, and poll for its copy through 13190a6f17c6SVladimir Oltean * MMIO in the CPU port module, and inject that into the stack from 13200a6f17c6SVladimir Oltean * ocelot_xtr_poll(). 13210a6f17c6SVladimir Oltean */ 13220a6f17c6SVladimir Oltean if (felix_check_xtr_pkt(ocelot, type)) { 13230a6f17c6SVladimir Oltean kfree_skb(skb); 13240a6f17c6SVladimir Oltean return true; 13250a6f17c6SVladimir Oltean } 13260a6f17c6SVladimir Oltean 1327c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1328c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1329c0bcf537SYangbo Lu 1330c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1331c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1332c0bcf537SYangbo Lu tstamp_hi--; 1333c0bcf537SYangbo Lu 1334c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1335c0bcf537SYangbo Lu 1336c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1337c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1338c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1339c0bcf537SYangbo Lu return false; 1340c0bcf537SYangbo Lu } 1341c0bcf537SYangbo Lu 13425c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port, 13435c5416f5SYangbo Lu struct sk_buff *skb) 1344c0bcf537SYangbo Lu { 1345c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1346682eaad9SYangbo Lu struct sk_buff *clone = NULL; 1347c0bcf537SYangbo Lu 1348682eaad9SYangbo Lu if (!ocelot->ptp) 13495c5416f5SYangbo Lu return; 1350c0bcf537SYangbo Lu 135152849bcfSVladimir Oltean if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 135252849bcfSVladimir Oltean dev_err_ratelimited(ds->dev, 135352849bcfSVladimir Oltean "port %d delivering skb without TX timestamp\n", 135452849bcfSVladimir Oltean port); 1355682eaad9SYangbo Lu return; 135652849bcfSVladimir Oltean } 1357682eaad9SYangbo Lu 1358682eaad9SYangbo Lu if (clone) 1359c4b364ceSYangbo Lu OCELOT_SKB_CB(skb)->clone = clone; 13605c5416f5SYangbo Lu } 1361c0bcf537SYangbo Lu 13620b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 13630b912fc9SVladimir Oltean { 13640b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13650b912fc9SVladimir Oltean 13660b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 13670b912fc9SVladimir Oltean 13680b912fc9SVladimir Oltean return 0; 13690b912fc9SVladimir Oltean } 13700b912fc9SVladimir Oltean 13710b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 13720b912fc9SVladimir Oltean { 13730b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13740b912fc9SVladimir Oltean 13750b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 13760b912fc9SVladimir Oltean } 13770b912fc9SVladimir Oltean 137807d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 137907d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 138007d985eeSVladimir Oltean { 138107d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 138299348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 138399348004SVladimir Oltean bool using_tag_8021q; 138499348004SVladimir Oltean int err; 138507d985eeSVladimir Oltean 138699348004SVladimir Oltean err = ocelot_cls_flower_replace(ocelot, port, cls, ingress); 138799348004SVladimir Oltean if (err) 138899348004SVladimir Oltean return err; 138999348004SVladimir Oltean 139099348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 139199348004SVladimir Oltean 139299348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 139307d985eeSVladimir Oltean } 139407d985eeSVladimir Oltean 139507d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 139607d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 139707d985eeSVladimir Oltean { 139807d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 139907d985eeSVladimir Oltean 140007d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 140107d985eeSVladimir Oltean } 140207d985eeSVladimir Oltean 140307d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 140407d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 140507d985eeSVladimir Oltean { 140607d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 140707d985eeSVladimir Oltean 140807d985eeSVladimir Oltean return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 140907d985eeSVladimir Oltean } 141007d985eeSVladimir Oltean 1411fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1412fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1413fc411eaaSVladimir Oltean { 1414fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1415fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1416fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 14175f035af7SPo Liu .burst = policer->burst, 1418fc411eaaSVladimir Oltean }; 1419fc411eaaSVladimir Oltean 1420fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1421fc411eaaSVladimir Oltean } 1422fc411eaaSVladimir Oltean 1423fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1424fc411eaaSVladimir Oltean { 1425fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1426fc411eaaSVladimir Oltean 1427fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1428fc411eaaSVladimir Oltean } 1429fc411eaaSVladimir Oltean 1430de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1431de143c0eSXiaoliang Yang enum tc_setup_type type, 1432de143c0eSXiaoliang Yang void *type_data) 1433de143c0eSXiaoliang Yang { 1434de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1435de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1436de143c0eSXiaoliang Yang 1437de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1438de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1439de143c0eSXiaoliang Yang else 1440de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1441de143c0eSXiaoliang Yang } 1442de143c0eSXiaoliang Yang 1443f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1444f59fd9caSVladimir Oltean u16 pool_index, 1445f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1446f59fd9caSVladimir Oltean { 1447f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1448f59fd9caSVladimir Oltean 1449f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1450f59fd9caSVladimir Oltean } 1451f59fd9caSVladimir Oltean 1452f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1453f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1454f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1455f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1456f59fd9caSVladimir Oltean { 1457f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1458f59fd9caSVladimir Oltean 1459f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1460f59fd9caSVladimir Oltean threshold_type, extack); 1461f59fd9caSVladimir Oltean } 1462f59fd9caSVladimir Oltean 1463f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1464f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1465f59fd9caSVladimir Oltean u32 *p_threshold) 1466f59fd9caSVladimir Oltean { 1467f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1468f59fd9caSVladimir Oltean 1469f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1470f59fd9caSVladimir Oltean p_threshold); 1471f59fd9caSVladimir Oltean } 1472f59fd9caSVladimir Oltean 1473f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1474f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1475f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1476f59fd9caSVladimir Oltean { 1477f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1478f59fd9caSVladimir Oltean 1479f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1480f59fd9caSVladimir Oltean threshold, extack); 1481f59fd9caSVladimir Oltean } 1482f59fd9caSVladimir Oltean 1483f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1484f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1485f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1486f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1487f59fd9caSVladimir Oltean { 1488f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1489f59fd9caSVladimir Oltean 1490f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1491f59fd9caSVladimir Oltean pool_type, p_pool_index, 1492f59fd9caSVladimir Oltean p_threshold); 1493f59fd9caSVladimir Oltean } 1494f59fd9caSVladimir Oltean 1495f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1496f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1497f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1498f59fd9caSVladimir Oltean u16 pool_index, u32 threshold, 1499f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1500f59fd9caSVladimir Oltean { 1501f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1502f59fd9caSVladimir Oltean 1503f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1504f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1505f59fd9caSVladimir Oltean extack); 1506f59fd9caSVladimir Oltean } 1507f59fd9caSVladimir Oltean 1508f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1509f59fd9caSVladimir Oltean unsigned int sb_index) 1510f59fd9caSVladimir Oltean { 1511f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1512f59fd9caSVladimir Oltean 1513f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1514f59fd9caSVladimir Oltean } 1515f59fd9caSVladimir Oltean 1516f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1517f59fd9caSVladimir Oltean unsigned int sb_index) 1518f59fd9caSVladimir Oltean { 1519f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1520f59fd9caSVladimir Oltean 1521f59fd9caSVladimir Oltean return ocelot_sb_occ_max_clear(ocelot, sb_index); 1522f59fd9caSVladimir Oltean } 1523f59fd9caSVladimir Oltean 1524f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1525f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1526f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1527f59fd9caSVladimir Oltean { 1528f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1529f59fd9caSVladimir Oltean 1530f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1531f59fd9caSVladimir Oltean p_cur, p_max); 1532f59fd9caSVladimir Oltean } 1533f59fd9caSVladimir Oltean 1534f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1535f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1536f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1537f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1538f59fd9caSVladimir Oltean { 1539f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1540f59fd9caSVladimir Oltean 1541f59fd9caSVladimir Oltean return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1542f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1543f59fd9caSVladimir Oltean } 1544f59fd9caSVladimir Oltean 1545a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port, 1546a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1547a026c50bSHoratiu Vultur { 1548a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1549a026c50bSHoratiu Vultur 1550a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1551a026c50bSHoratiu Vultur } 1552a026c50bSHoratiu Vultur 1553a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port, 1554a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1555a026c50bSHoratiu Vultur { 1556a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1557a026c50bSHoratiu Vultur 1558a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1559a026c50bSHoratiu Vultur } 1560a026c50bSHoratiu Vultur 1561a026c50bSHoratiu Vultur static int 1562a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1563a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1564a026c50bSHoratiu Vultur { 1565a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1566a026c50bSHoratiu Vultur 1567a026c50bSHoratiu Vultur return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1568a026c50bSHoratiu Vultur } 1569a026c50bSHoratiu Vultur 1570a026c50bSHoratiu Vultur static int 1571a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1572a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1573a026c50bSHoratiu Vultur { 1574a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1575a026c50bSHoratiu Vultur 1576a026c50bSHoratiu Vultur return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1577a026c50bSHoratiu Vultur } 1578a026c50bSHoratiu Vultur 1579375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 158056051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1581adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 158235d97680SVladimir Oltean .connect_tag_protocol = felix_connect_tag_protocol, 158356051948SVladimir Oltean .setup = felix_setup, 158456051948SVladimir Oltean .teardown = felix_teardown, 158556051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 158656051948SVladimir Oltean .get_strings = felix_get_strings, 158756051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 158856051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 158956051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 1590bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1591bdeced75SVladimir Oltean .phylink_mac_config = felix_phylink_mac_config, 1592bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1593bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 15945cad43a5SVladimir Oltean .port_fast_age = felix_port_fast_age, 159556051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 159656051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 159756051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1598*961d8b69SVladimir Oltean .lag_fdb_add = felix_lag_fdb_add, 1599*961d8b69SVladimir Oltean .lag_fdb_del = felix_lag_fdb_del, 1600209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1601209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 1602421741eaSVladimir Oltean .port_pre_bridge_flags = felix_pre_bridge_flags, 1603421741eaSVladimir Oltean .port_bridge_flags = felix_bridge_flags, 160456051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 160556051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 16068fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 16078fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 16088fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 160956051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 161056051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 161156051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 161256051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1613c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1614c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1615c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1616c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 16170b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 16180b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1619fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1620fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 162107d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 162207d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 162307d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1624de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1625f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1626f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1627f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1628f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1629f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1630f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1631f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1632f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1633f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1634f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1635a026c50bSHoratiu Vultur .port_mrp_add = felix_mrp_add, 1636a026c50bSHoratiu Vultur .port_mrp_del = felix_mrp_del, 1637a026c50bSHoratiu Vultur .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1638a026c50bSHoratiu Vultur .port_mrp_del_ring_role = felix_mrp_del_ring_role, 16395da11eb4SVladimir Oltean .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 16405da11eb4SVladimir Oltean .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 164156051948SVladimir Oltean }; 1642319e4dd1SVladimir Oltean 1643319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1644319e4dd1SVladimir Oltean { 1645319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1646319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1647319e4dd1SVladimir Oltean 1648319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1649319e4dd1SVladimir Oltean return NULL; 1650319e4dd1SVladimir Oltean 1651319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1652319e4dd1SVladimir Oltean } 1653319e4dd1SVladimir Oltean 1654319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1655319e4dd1SVladimir Oltean { 1656319e4dd1SVladimir Oltean struct dsa_port *dp; 1657319e4dd1SVladimir Oltean 1658319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1659319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1660319e4dd1SVladimir Oltean return -EINVAL; 1661319e4dd1SVladimir Oltean 1662319e4dd1SVladimir Oltean return dp->index; 1663319e4dd1SVladimir Oltean } 1664