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