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 28f9cef64fSVladimir Oltean /* Translate the DSA database API into the ocelot switch library API, 29f9cef64fSVladimir Oltean * which uses VID 0 for all ports that aren't part of a bridge, 30f9cef64fSVladimir Oltean * and expects the bridge_dev to be NULL in that case. 31f9cef64fSVladimir Oltean */ 32f9cef64fSVladimir Oltean static struct net_device *felix_classify_db(struct dsa_db db) 33f9cef64fSVladimir Oltean { 34f9cef64fSVladimir Oltean switch (db.type) { 35f9cef64fSVladimir Oltean case DSA_DB_PORT: 36f9cef64fSVladimir Oltean case DSA_DB_LAG: 37f9cef64fSVladimir Oltean return NULL; 38f9cef64fSVladimir Oltean case DSA_DB_BRIDGE: 39f9cef64fSVladimir Oltean return db.bridge.dev; 40f9cef64fSVladimir Oltean default: 41f9cef64fSVladimir Oltean return ERR_PTR(-EOPNOTSUPP); 42f9cef64fSVladimir Oltean } 43f9cef64fSVladimir Oltean } 44f9cef64fSVladimir Oltean 4504b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that 4604b67e18SVladimir Oltean * the tagger can perform RX source port identification. 4704b67e18SVladimir Oltean */ 48a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct dsa_switch *ds, int port, 49a4e044dcSVladimir Oltean int upstream, u16 vid) 50e21268efSVladimir Oltean { 51e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 52a4e044dcSVladimir Oltean struct ocelot *ocelot = ds->priv; 53a4e044dcSVladimir Oltean unsigned long cookie; 54a4e044dcSVladimir Oltean int key_length, err; 55e21268efSVladimir Oltean 56e21268efSVladimir Oltean key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length; 57e21268efSVladimir Oltean 58e21268efSVladimir Oltean outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), 59e21268efSVladimir Oltean GFP_KERNEL); 60e21268efSVladimir Oltean if (!outer_tagging_rule) 61e21268efSVladimir Oltean return -ENOMEM; 62e21268efSVladimir Oltean 63a4e044dcSVladimir Oltean cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port, upstream); 64a4e044dcSVladimir Oltean 65e21268efSVladimir Oltean outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 66e21268efSVladimir Oltean outer_tagging_rule->prio = 1; 67a4e044dcSVladimir Oltean outer_tagging_rule->id.cookie = cookie; 68e21268efSVladimir Oltean outer_tagging_rule->id.tc_offload = false; 69e21268efSVladimir Oltean outer_tagging_rule->block_id = VCAP_ES0; 70e21268efSVladimir Oltean outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 71e21268efSVladimir Oltean outer_tagging_rule->lookup = 0; 72e21268efSVladimir Oltean outer_tagging_rule->ingress_port.value = port; 73e21268efSVladimir Oltean outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0); 74e21268efSVladimir Oltean outer_tagging_rule->egress_port.value = upstream; 75e21268efSVladimir Oltean outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0); 76e21268efSVladimir Oltean outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG; 77e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD; 78e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_vid_sel = 1; 79e21268efSVladimir Oltean outer_tagging_rule->action.vid_a_val = vid; 80e21268efSVladimir Oltean 81e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL); 82e21268efSVladimir Oltean if (err) 83e21268efSVladimir Oltean kfree(outer_tagging_rule); 84e21268efSVladimir Oltean 85e21268efSVladimir Oltean return err; 86e21268efSVladimir Oltean } 87e21268efSVladimir Oltean 88a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct dsa_switch *ds, int port, 89a4e044dcSVladimir Oltean int upstream, u16 vid) 9004b67e18SVladimir Oltean { 9104b67e18SVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 9204b67e18SVladimir Oltean struct ocelot_vcap_block *block_vcap_es0; 93a4e044dcSVladimir Oltean struct ocelot *ocelot = ds->priv; 94a4e044dcSVladimir Oltean unsigned long cookie; 9504b67e18SVladimir Oltean 9604b67e18SVladimir Oltean block_vcap_es0 = &ocelot->block[VCAP_ES0]; 97a4e044dcSVladimir Oltean cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port, upstream); 9804b67e18SVladimir Oltean 9904b67e18SVladimir Oltean outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 100a4e044dcSVladimir Oltean cookie, false); 10104b67e18SVladimir Oltean if (!outer_tagging_rule) 10204b67e18SVladimir Oltean return -ENOENT; 10304b67e18SVladimir Oltean 10404b67e18SVladimir Oltean return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 10504b67e18SVladimir Oltean } 10604b67e18SVladimir Oltean 10704b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2 10804b67e18SVladimir Oltean * rules for steering those tagged packets towards the correct destination port 10904b67e18SVladimir Oltean */ 110a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct dsa_switch *ds, int port, 111a4e044dcSVladimir Oltean u16 vid) 112e21268efSVladimir Oltean { 113e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 114a4e044dcSVladimir Oltean unsigned long cpu_ports = dsa_cpu_ports(ds); 115a4e044dcSVladimir Oltean struct ocelot *ocelot = ds->priv; 116a4e044dcSVladimir Oltean unsigned long cookie; 117a4e044dcSVladimir Oltean int err; 118e21268efSVladimir Oltean 119e21268efSVladimir Oltean untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 120e21268efSVladimir Oltean if (!untagging_rule) 121e21268efSVladimir Oltean return -ENOMEM; 122e21268efSVladimir Oltean 123e21268efSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 124e21268efSVladimir Oltean if (!redirect_rule) { 125e21268efSVladimir Oltean kfree(untagging_rule); 126e21268efSVladimir Oltean return -ENOMEM; 127e21268efSVladimir Oltean } 128e21268efSVladimir Oltean 129a4e044dcSVladimir Oltean cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port); 130e21268efSVladimir Oltean 131e21268efSVladimir Oltean untagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 132a4e044dcSVladimir Oltean untagging_rule->ingress_port_mask = cpu_ports; 133e21268efSVladimir Oltean untagging_rule->vlan.vid.value = vid; 134e21268efSVladimir Oltean untagging_rule->vlan.vid.mask = VLAN_VID_MASK; 135e21268efSVladimir Oltean untagging_rule->prio = 1; 136a4e044dcSVladimir Oltean untagging_rule->id.cookie = cookie; 137e21268efSVladimir Oltean untagging_rule->id.tc_offload = false; 138e21268efSVladimir Oltean untagging_rule->block_id = VCAP_IS1; 139e21268efSVladimir Oltean untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 140e21268efSVladimir Oltean untagging_rule->lookup = 0; 141e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt_ena = true; 142e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt = 1; 143e21268efSVladimir Oltean untagging_rule->action.pag_override_mask = 0xff; 144e21268efSVladimir Oltean untagging_rule->action.pag_val = port; 145e21268efSVladimir Oltean 146e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL); 147e21268efSVladimir Oltean if (err) { 148e21268efSVladimir Oltean kfree(untagging_rule); 149e21268efSVladimir Oltean kfree(redirect_rule); 150e21268efSVladimir Oltean return err; 151e21268efSVladimir Oltean } 152e21268efSVladimir Oltean 153a4e044dcSVladimir Oltean cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port); 154a4e044dcSVladimir Oltean 155e21268efSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 156a4e044dcSVladimir Oltean redirect_rule->ingress_port_mask = cpu_ports; 157e21268efSVladimir Oltean redirect_rule->pag = port; 158e21268efSVladimir Oltean redirect_rule->prio = 1; 159a4e044dcSVladimir Oltean redirect_rule->id.cookie = cookie; 160e21268efSVladimir Oltean redirect_rule->id.tc_offload = false; 161e21268efSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 162e21268efSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 163e21268efSVladimir Oltean redirect_rule->lookup = 0; 164e21268efSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 165e21268efSVladimir Oltean redirect_rule->action.port_mask = BIT(port); 166e21268efSVladimir Oltean 167e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 168e21268efSVladimir Oltean if (err) { 169e21268efSVladimir Oltean ocelot_vcap_filter_del(ocelot, untagging_rule); 170e21268efSVladimir Oltean kfree(redirect_rule); 171e21268efSVladimir Oltean return err; 172e21268efSVladimir Oltean } 173e21268efSVladimir Oltean 174e21268efSVladimir Oltean return 0; 175e21268efSVladimir Oltean } 176e21268efSVladimir Oltean 177a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct dsa_switch *ds, int port, u16 vid) 178e21268efSVladimir Oltean { 179e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 180e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 181e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 182a4e044dcSVladimir Oltean struct ocelot *ocelot = ds->priv; 183a4e044dcSVladimir Oltean unsigned long cookie; 184e21268efSVladimir Oltean int err; 185e21268efSVladimir Oltean 186e21268efSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 187e21268efSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 188e21268efSVladimir Oltean 189a4e044dcSVladimir Oltean cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port); 190e21268efSVladimir Oltean untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 191a4e044dcSVladimir Oltean cookie, false); 192e21268efSVladimir Oltean if (!untagging_rule) 19308f44db3SVladimir Oltean return -ENOENT; 194e21268efSVladimir Oltean 195e21268efSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, untagging_rule); 196e21268efSVladimir Oltean if (err) 197e21268efSVladimir Oltean return err; 198e21268efSVladimir Oltean 199a4e044dcSVladimir Oltean cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port); 200e21268efSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 201a4e044dcSVladimir Oltean cookie, false); 202e21268efSVladimir Oltean if (!redirect_rule) 20304b67e18SVladimir Oltean return -ENOENT; 204e21268efSVladimir Oltean 205e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 206e21268efSVladimir Oltean } 207e21268efSVladimir Oltean 20804b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 20904b67e18SVladimir Oltean u16 flags) 21004b67e18SVladimir Oltean { 211a4e044dcSVladimir Oltean struct dsa_port *cpu_dp; 21204b67e18SVladimir Oltean int err; 21304b67e18SVladimir Oltean 21404b67e18SVladimir Oltean /* tag_8021q.c assumes we are implementing this via port VLAN 21504b67e18SVladimir Oltean * membership, which we aren't. So we don't need to add any VCAP filter 21604b67e18SVladimir Oltean * for the CPU port. 21704b67e18SVladimir Oltean */ 21804b67e18SVladimir Oltean if (!dsa_is_user_port(ds, port)) 21904b67e18SVladimir Oltean return 0; 22004b67e18SVladimir Oltean 221a4e044dcSVladimir Oltean dsa_switch_for_each_cpu_port(cpu_dp, ds) { 222a4e044dcSVladimir Oltean err = felix_tag_8021q_vlan_add_rx(ds, port, cpu_dp->index, vid); 22304b67e18SVladimir Oltean if (err) 22404b67e18SVladimir Oltean return err; 22504b67e18SVladimir Oltean } 22604b67e18SVladimir Oltean 227a4e044dcSVladimir Oltean err = felix_tag_8021q_vlan_add_tx(ds, port, vid); 228a4e044dcSVladimir Oltean if (err) 229a4e044dcSVladimir Oltean goto add_tx_failed; 230a4e044dcSVladimir Oltean 23104b67e18SVladimir Oltean return 0; 232a4e044dcSVladimir Oltean 233a4e044dcSVladimir Oltean add_tx_failed: 234a4e044dcSVladimir Oltean dsa_switch_for_each_cpu_port(cpu_dp, ds) 235a4e044dcSVladimir Oltean felix_tag_8021q_vlan_del_rx(ds, port, cpu_dp->index, vid); 236a4e044dcSVladimir Oltean 237a4e044dcSVladimir Oltean return err; 23804b67e18SVladimir Oltean } 23904b67e18SVladimir Oltean 240e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 241e21268efSVladimir Oltean { 242a4e044dcSVladimir Oltean struct dsa_port *cpu_dp; 24304b67e18SVladimir Oltean int err; 244e21268efSVladimir Oltean 24504b67e18SVladimir Oltean if (!dsa_is_user_port(ds, port)) 24604b67e18SVladimir Oltean return 0; 247e21268efSVladimir Oltean 248a4e044dcSVladimir Oltean dsa_switch_for_each_cpu_port(cpu_dp, ds) { 249a4e044dcSVladimir Oltean err = felix_tag_8021q_vlan_del_rx(ds, port, cpu_dp->index, vid); 25004b67e18SVladimir Oltean if (err) 25104b67e18SVladimir Oltean return err; 25204b67e18SVladimir Oltean } 253e21268efSVladimir Oltean 254a4e044dcSVladimir Oltean err = felix_tag_8021q_vlan_del_tx(ds, port, vid); 255a4e044dcSVladimir Oltean if (err) 256a4e044dcSVladimir Oltean goto del_tx_failed; 257a4e044dcSVladimir Oltean 258e21268efSVladimir Oltean return 0; 259a4e044dcSVladimir Oltean 260a4e044dcSVladimir Oltean del_tx_failed: 261a4e044dcSVladimir Oltean dsa_switch_for_each_cpu_port(cpu_dp, ds) 262a4e044dcSVladimir Oltean felix_tag_8021q_vlan_add_rx(ds, port, cpu_dp->index, vid); 263a4e044dcSVladimir Oltean 264a4e044dcSVladimir Oltean return err; 265e21268efSVladimir Oltean } 266e21268efSVladimir Oltean 267c352e5e8SVladimir Oltean static int felix_trap_get_cpu_port(struct dsa_switch *ds, 268c352e5e8SVladimir Oltean const struct ocelot_vcap_filter *trap) 269c352e5e8SVladimir Oltean { 270c352e5e8SVladimir Oltean struct dsa_port *dp; 271c352e5e8SVladimir Oltean int first_port; 272c352e5e8SVladimir Oltean 273c352e5e8SVladimir Oltean if (WARN_ON(!trap->ingress_port_mask)) 274c352e5e8SVladimir Oltean return -1; 275c352e5e8SVladimir Oltean 276c352e5e8SVladimir Oltean first_port = __ffs(trap->ingress_port_mask); 277c352e5e8SVladimir Oltean dp = dsa_to_port(ds, first_port); 278c352e5e8SVladimir Oltean 279c352e5e8SVladimir Oltean return dp->cpu_dp->index; 280c352e5e8SVladimir Oltean } 281c352e5e8SVladimir Oltean 28299348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be 28399348004SVladimir Oltean * replicated over Ethernet as well, otherwise we'd get no notification of 28499348004SVladimir Oltean * their arrival when using the ocelot-8021q tagging protocol. 285c8c0ba4fSVladimir Oltean */ 28699348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds, 28799348004SVladimir Oltean bool using_tag_8021q) 288c8c0ba4fSVladimir Oltean { 28999348004SVladimir Oltean struct ocelot *ocelot = ds->priv; 29099348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 291e1846cffSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 29299348004SVladimir Oltean struct ocelot_vcap_filter *trap; 29399348004SVladimir Oltean enum ocelot_mask_mode mask_mode; 29499348004SVladimir Oltean unsigned long port_mask; 29599348004SVladimir Oltean bool cpu_copy_ena; 296c352e5e8SVladimir Oltean int err; 297c8c0ba4fSVladimir Oltean 29899348004SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 29999348004SVladimir Oltean return 0; 300c8c0ba4fSVladimir Oltean 301d78637a8SVladimir Oltean /* We are sure that "cpu" was found, otherwise 302d78637a8SVladimir Oltean * dsa_tree_setup_default_cpu() would have failed earlier. 303d78637a8SVladimir Oltean */ 304e1846cffSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 305c8c0ba4fSVladimir Oltean 30699348004SVladimir Oltean /* Make sure all traps are set up for that destination */ 307e1846cffSVladimir Oltean list_for_each_entry(trap, &block_vcap_is2->rules, list) { 308e1846cffSVladimir Oltean if (!trap->is_trap) 309e1846cffSVladimir Oltean continue; 310e1846cffSVladimir Oltean 31199348004SVladimir Oltean /* Figure out the current trapping destination */ 31299348004SVladimir Oltean if (using_tag_8021q) { 31399348004SVladimir Oltean /* Redirect to the tag_8021q CPU port. If timestamps 31499348004SVladimir Oltean * are necessary, also copy trapped packets to the CPU 31599348004SVladimir Oltean * port module. 316c8c0ba4fSVladimir Oltean */ 31799348004SVladimir Oltean mask_mode = OCELOT_MASK_MODE_REDIRECT; 318c352e5e8SVladimir Oltean port_mask = BIT(felix_trap_get_cpu_port(ds, trap)); 31999348004SVladimir Oltean cpu_copy_ena = !!trap->take_ts; 320c8c0ba4fSVladimir Oltean } else { 32199348004SVladimir Oltean /* Trap packets only to the CPU port module, which is 32299348004SVladimir Oltean * redirected to the NPI port (the DSA CPU port) 323c8c0ba4fSVladimir Oltean */ 32499348004SVladimir Oltean mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 32599348004SVladimir Oltean port_mask = 0; 32699348004SVladimir Oltean cpu_copy_ena = true; 327c8c0ba4fSVladimir Oltean } 328c8c0ba4fSVladimir Oltean 32999348004SVladimir Oltean if (trap->action.mask_mode == mask_mode && 33099348004SVladimir Oltean trap->action.port_mask == port_mask && 33199348004SVladimir Oltean trap->action.cpu_copy_ena == cpu_copy_ena) 33299348004SVladimir Oltean continue; 333c8c0ba4fSVladimir Oltean 33499348004SVladimir Oltean trap->action.mask_mode = mask_mode; 33599348004SVladimir Oltean trap->action.port_mask = port_mask; 33699348004SVladimir Oltean trap->action.cpu_copy_ena = cpu_copy_ena; 3370a6f17c6SVladimir Oltean 33899348004SVladimir Oltean err = ocelot_vcap_filter_replace(ocelot, trap); 339c8c0ba4fSVladimir Oltean if (err) 340c8c0ba4fSVladimir Oltean return err; 34199348004SVladimir Oltean } 342c8c0ba4fSVladimir Oltean 34399348004SVladimir Oltean return 0; 344c8c0ba4fSVladimir Oltean } 345c8c0ba4fSVladimir Oltean 346adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This 347adb3dccfSVladimir Oltean * is the mode through which frames can be injected from and extracted to an 348adb3dccfSVladimir Oltean * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 349adb3dccfSVladimir Oltean * running Linux, and this forms a DSA setup together with the enetc or fman 350adb3dccfSVladimir Oltean * DSA master. 351adb3dccfSVladimir Oltean */ 352adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port) 353adb3dccfSVladimir Oltean { 354adb3dccfSVladimir Oltean ocelot->npi = port; 355adb3dccfSVladimir Oltean 356adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 357adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 358adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 359adb3dccfSVladimir Oltean 360adb3dccfSVladimir Oltean /* NPI port Injection/Extraction configuration */ 361adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 362adb3dccfSVladimir Oltean ocelot->npi_xtr_prefix); 363adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 364adb3dccfSVladimir Oltean ocelot->npi_inj_prefix); 365adb3dccfSVladimir Oltean 366adb3dccfSVladimir Oltean /* Disable transmission of pause frames */ 367adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 368adb3dccfSVladimir Oltean } 369adb3dccfSVladimir Oltean 370adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 371adb3dccfSVladimir Oltean { 372adb3dccfSVladimir Oltean /* Restore hardware defaults */ 373adb3dccfSVladimir Oltean int unused_port = ocelot->num_phys_ports + 2; 374adb3dccfSVladimir Oltean 375adb3dccfSVladimir Oltean ocelot->npi = -1; 376adb3dccfSVladimir Oltean 377adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 378adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 379adb3dccfSVladimir Oltean 380adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 381adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 382adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 383adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 384adb3dccfSVladimir Oltean 385adb3dccfSVladimir Oltean /* Enable transmission of pause frames */ 386adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 387adb3dccfSVladimir Oltean } 388adb3dccfSVladimir Oltean 3897a29d220SVladimir Oltean static int felix_tag_npi_setup(struct dsa_switch *ds) 390adb3dccfSVladimir Oltean { 3917a29d220SVladimir Oltean struct dsa_port *dp, *first_cpu_dp = NULL; 392adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 393f9cef64fSVladimir Oltean 3947a29d220SVladimir Oltean dsa_switch_for_each_user_port(dp, ds) { 3957a29d220SVladimir Oltean if (first_cpu_dp && dp->cpu_dp != first_cpu_dp) { 3967a29d220SVladimir Oltean dev_err(ds->dev, "Multiple NPI ports not supported\n"); 3977a29d220SVladimir Oltean return -EINVAL; 3987a29d220SVladimir Oltean } 399b903a6bdSVladimir Oltean 4007a29d220SVladimir Oltean first_cpu_dp = dp->cpu_dp; 4017a29d220SVladimir Oltean } 402adb3dccfSVladimir Oltean 4037a29d220SVladimir Oltean if (!first_cpu_dp) 4047a29d220SVladimir Oltean return -EINVAL; 4057a29d220SVladimir Oltean 4067a29d220SVladimir Oltean felix_npi_port_init(ocelot, first_cpu_dp->index); 407adb3dccfSVladimir Oltean 408adb3dccfSVladimir Oltean return 0; 409adb3dccfSVladimir Oltean } 410adb3dccfSVladimir Oltean 4117a29d220SVladimir Oltean static void felix_tag_npi_teardown(struct dsa_switch *ds) 412adb3dccfSVladimir Oltean { 413adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 414adb3dccfSVladimir Oltean 4157a29d220SVladimir Oltean felix_npi_port_deinit(ocelot, ocelot->npi); 416adb3dccfSVladimir Oltean } 417adb3dccfSVladimir Oltean 4187a29d220SVladimir Oltean static unsigned long felix_tag_npi_get_host_fwd_mask(struct dsa_switch *ds) 419adb3dccfSVladimir Oltean { 4207a29d220SVladimir Oltean struct ocelot *ocelot = ds->priv; 4217a29d220SVladimir Oltean 4227a29d220SVladimir Oltean return BIT(ocelot->num_phys_ports); 4237a29d220SVladimir Oltean } 4247a29d220SVladimir Oltean 4258c166acbSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC 4268c166acbSVladimir Oltean * connected internally to the enetc or fman DSA master can be configured to 4278c166acbSVladimir Oltean * use the software-defined tag_8021q frame format. As far as the hardware is 4288c166acbSVladimir Oltean * concerned, it thinks it is a "dumb switch" - the queues of the CPU port 4298c166acbSVladimir Oltean * module are now disconnected from it, but can still be accessed through 4308c166acbSVladimir Oltean * register-based MMIO. 4318c166acbSVladimir Oltean */ 4327a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_npi_proto_ops = { 4337a29d220SVladimir Oltean .setup = felix_tag_npi_setup, 4347a29d220SVladimir Oltean .teardown = felix_tag_npi_teardown, 4357a29d220SVladimir Oltean .get_host_fwd_mask = felix_tag_npi_get_host_fwd_mask, 4367a29d220SVladimir Oltean }; 4377a29d220SVladimir Oltean 4387a29d220SVladimir Oltean static int felix_tag_8021q_setup(struct dsa_switch *ds) 4397a29d220SVladimir Oltean { 4407a29d220SVladimir Oltean struct ocelot *ocelot = ds->priv; 441c295f983SVladimir Oltean struct dsa_port *dp; 442adb3dccfSVladimir Oltean int err; 443adb3dccfSVladimir Oltean 4447a29d220SVladimir Oltean err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD)); 4457a29d220SVladimir Oltean if (err) 446adb3dccfSVladimir Oltean return err; 4477a29d220SVladimir Oltean 44836a0bf44SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) 44936a0bf44SVladimir Oltean ocelot_port_setup_dsa_8021q_cpu(ocelot, dp->index); 45036a0bf44SVladimir Oltean 451c295f983SVladimir Oltean dsa_switch_for_each_user_port(dp, ds) 452c295f983SVladimir Oltean ocelot_port_assign_dsa_8021q_cpu(ocelot, dp->index, 453c295f983SVladimir Oltean dp->cpu_dp->index); 4547a29d220SVladimir Oltean 455c295f983SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 4567a29d220SVladimir Oltean /* This overwrites ocelot_init(): 4577a29d220SVladimir Oltean * Do not forward BPDU frames to the CPU port module, 4587a29d220SVladimir Oltean * for 2 reasons: 4597a29d220SVladimir Oltean * - When these packets are injected from the tag_8021q 4607a29d220SVladimir Oltean * CPU port, we want them to go out, not loop back 4617a29d220SVladimir Oltean * into the system. 4627a29d220SVladimir Oltean * - STP traffic ingressing on a user port should go to 4637a29d220SVladimir Oltean * the tag_8021q CPU port, not to the hardware CPU 4647a29d220SVladimir Oltean * port module. 4657a29d220SVladimir Oltean */ 4667a29d220SVladimir Oltean ocelot_write_gix(ocelot, 4677a29d220SVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 4687a29d220SVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, dp->index); 4697a29d220SVladimir Oltean 4707a29d220SVladimir Oltean /* The ownership of the CPU port module's queues might have just been 4717a29d220SVladimir Oltean * transferred to the tag_8021q tagger from the NPI-based tagger. 4727a29d220SVladimir Oltean * So there might still be all sorts of crap in the queues. On the 4737a29d220SVladimir Oltean * other hand, the MMIO-based matching of PTP frames is very brittle, 4747a29d220SVladimir Oltean * so we need to be careful that there are no extra frames to be 4757a29d220SVladimir Oltean * dequeued over MMIO, since we would never know to discard them. 4767a29d220SVladimir Oltean */ 4777a29d220SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 4787a29d220SVladimir Oltean 4797a29d220SVladimir Oltean return 0; 4807a29d220SVladimir Oltean } 4817a29d220SVladimir Oltean 4827a29d220SVladimir Oltean static void felix_tag_8021q_teardown(struct dsa_switch *ds) 483adb3dccfSVladimir Oltean { 4847a29d220SVladimir Oltean struct ocelot *ocelot = ds->priv; 485c295f983SVladimir Oltean struct dsa_port *dp; 4867a29d220SVladimir Oltean 487c295f983SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 4887a29d220SVladimir Oltean /* Restore the logic from ocelot_init: 4897a29d220SVladimir Oltean * do not forward BPDU frames to the front ports. 4907a29d220SVladimir Oltean */ 4917a29d220SVladimir Oltean ocelot_write_gix(ocelot, 4927a29d220SVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 4937a29d220SVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, 4947a29d220SVladimir Oltean dp->index); 4957a29d220SVladimir Oltean 496c295f983SVladimir Oltean dsa_switch_for_each_user_port(dp, ds) 497c295f983SVladimir Oltean ocelot_port_unassign_dsa_8021q_cpu(ocelot, dp->index); 4987a29d220SVladimir Oltean 49936a0bf44SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) 50036a0bf44SVladimir Oltean ocelot_port_teardown_dsa_8021q_cpu(ocelot, dp->index); 50136a0bf44SVladimir Oltean 5027a29d220SVladimir Oltean dsa_tag_8021q_unregister(ds); 5037a29d220SVladimir Oltean } 5047a29d220SVladimir Oltean 5057a29d220SVladimir Oltean static unsigned long felix_tag_8021q_get_host_fwd_mask(struct dsa_switch *ds) 5067a29d220SVladimir Oltean { 5077a29d220SVladimir Oltean return dsa_cpu_ports(ds); 5087a29d220SVladimir Oltean } 5097a29d220SVladimir Oltean 5107a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_8021q_proto_ops = { 5117a29d220SVladimir Oltean .setup = felix_tag_8021q_setup, 5127a29d220SVladimir Oltean .teardown = felix_tag_8021q_teardown, 5137a29d220SVladimir Oltean .get_host_fwd_mask = felix_tag_8021q_get_host_fwd_mask, 5147a29d220SVladimir Oltean }; 5157a29d220SVladimir Oltean 5167a29d220SVladimir Oltean static void felix_set_host_flood(struct dsa_switch *ds, unsigned long mask, 5177a29d220SVladimir Oltean bool uc, bool mc, bool bc) 5187a29d220SVladimir Oltean { 5197a29d220SVladimir Oltean struct ocelot *ocelot = ds->priv; 5207a29d220SVladimir Oltean unsigned long val; 5217a29d220SVladimir Oltean 5227a29d220SVladimir Oltean val = uc ? mask : 0; 5237a29d220SVladimir Oltean ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_UC); 5247a29d220SVladimir Oltean 5257a29d220SVladimir Oltean val = mc ? mask : 0; 5267a29d220SVladimir Oltean ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MC); 5277a29d220SVladimir Oltean ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV4); 5287a29d220SVladimir Oltean ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV6); 529129b7532SVladimir Oltean 530129b7532SVladimir Oltean val = bc ? mask : 0; 531129b7532SVladimir Oltean ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_BC); 5327a29d220SVladimir Oltean } 5337a29d220SVladimir Oltean 5347a29d220SVladimir Oltean static void 5357a29d220SVladimir Oltean felix_migrate_host_flood(struct dsa_switch *ds, 5367a29d220SVladimir Oltean const struct felix_tag_proto_ops *proto_ops, 5377a29d220SVladimir Oltean const struct felix_tag_proto_ops *old_proto_ops) 5387a29d220SVladimir Oltean { 5397a29d220SVladimir Oltean struct ocelot *ocelot = ds->priv; 5407a29d220SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 5417a29d220SVladimir Oltean unsigned long mask; 5427a29d220SVladimir Oltean 5437a29d220SVladimir Oltean if (old_proto_ops) { 5447a29d220SVladimir Oltean mask = old_proto_ops->get_host_fwd_mask(ds); 5457a29d220SVladimir Oltean felix_set_host_flood(ds, mask, false, false, false); 5467a29d220SVladimir Oltean } 5477a29d220SVladimir Oltean 5487a29d220SVladimir Oltean mask = proto_ops->get_host_fwd_mask(ds); 5497a29d220SVladimir Oltean felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask, 5507a29d220SVladimir Oltean !!felix->host_flood_mc_mask, true); 5517a29d220SVladimir Oltean } 5527a29d220SVladimir Oltean 5537a29d220SVladimir Oltean static int felix_migrate_mdbs(struct dsa_switch *ds, 5547a29d220SVladimir Oltean const struct felix_tag_proto_ops *proto_ops, 5557a29d220SVladimir Oltean const struct felix_tag_proto_ops *old_proto_ops) 5567a29d220SVladimir Oltean { 5577a29d220SVladimir Oltean struct ocelot *ocelot = ds->priv; 5587a29d220SVladimir Oltean unsigned long from, to; 5597a29d220SVladimir Oltean 5607a29d220SVladimir Oltean if (!old_proto_ops) 5617a29d220SVladimir Oltean return 0; 5627a29d220SVladimir Oltean 5637a29d220SVladimir Oltean from = old_proto_ops->get_host_fwd_mask(ds); 5647a29d220SVladimir Oltean to = proto_ops->get_host_fwd_mask(ds); 5657a29d220SVladimir Oltean 5667a29d220SVladimir Oltean return ocelot_migrate_mdbs(ocelot, from, to); 5677a29d220SVladimir Oltean } 5687a29d220SVladimir Oltean 5697a29d220SVladimir Oltean /* Configure the shared hardware resources for a transition between 5707a29d220SVladimir Oltean * @old_proto_ops and @proto_ops. 5717a29d220SVladimir Oltean * Manual migration is needed because as far as DSA is concerned, no change of 5727a29d220SVladimir Oltean * the CPU port is taking place here, just of the tagging protocol. 5737a29d220SVladimir Oltean */ 5747a29d220SVladimir Oltean static int 5757a29d220SVladimir Oltean felix_tag_proto_setup_shared(struct dsa_switch *ds, 5767a29d220SVladimir Oltean const struct felix_tag_proto_ops *proto_ops, 5777a29d220SVladimir Oltean const struct felix_tag_proto_ops *old_proto_ops) 5787a29d220SVladimir Oltean { 5797a29d220SVladimir Oltean bool using_tag_8021q = (proto_ops == &felix_tag_8021q_proto_ops); 5807a29d220SVladimir Oltean int err; 5817a29d220SVladimir Oltean 5827a29d220SVladimir Oltean err = felix_migrate_mdbs(ds, proto_ops, old_proto_ops); 5837a29d220SVladimir Oltean if (err) 5847a29d220SVladimir Oltean return err; 5857a29d220SVladimir Oltean 5867a29d220SVladimir Oltean felix_update_trapping_destinations(ds, using_tag_8021q); 5877a29d220SVladimir Oltean 5887a29d220SVladimir Oltean felix_migrate_host_flood(ds, proto_ops, old_proto_ops); 5897a29d220SVladimir Oltean 5907a29d220SVladimir Oltean return 0; 591adb3dccfSVladimir Oltean } 592adb3dccfSVladimir Oltean 593e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the 594e21268efSVladimir Oltean * tag_8021q setup can fail, the NPI setup can't. So either the change is made, 595e21268efSVladimir Oltean * or the restoration is guaranteed to work. 596e21268efSVladimir Oltean */ 597bacf93b0SVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, 598adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 599adb3dccfSVladimir Oltean { 6007a29d220SVladimir Oltean const struct felix_tag_proto_ops *old_proto_ops, *proto_ops; 601adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 602adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 603adb3dccfSVladimir Oltean int err; 604adb3dccfSVladimir Oltean 6057a29d220SVladimir Oltean switch (proto) { 6067a29d220SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 6077a29d220SVladimir Oltean case DSA_TAG_PROTO_OCELOT: 6087a29d220SVladimir Oltean proto_ops = &felix_tag_npi_proto_ops; 609bacf93b0SVladimir Oltean break; 6107a29d220SVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 6117a29d220SVladimir Oltean proto_ops = &felix_tag_8021q_proto_ops; 6127a29d220SVladimir Oltean break; 6137a29d220SVladimir Oltean default: 6147a29d220SVladimir Oltean return -EPROTONOSUPPORT; 615bacf93b0SVladimir Oltean } 616bacf93b0SVladimir Oltean 6177a29d220SVladimir Oltean old_proto_ops = felix->tag_proto_ops; 6187a29d220SVladimir Oltean 6194c46bb49SVladimir Oltean if (proto_ops == old_proto_ops) 6204c46bb49SVladimir Oltean return 0; 6214c46bb49SVladimir Oltean 6227a29d220SVladimir Oltean err = proto_ops->setup(ds); 6237a29d220SVladimir Oltean if (err) 6247a29d220SVladimir Oltean goto setup_failed; 6257a29d220SVladimir Oltean 6267a29d220SVladimir Oltean err = felix_tag_proto_setup_shared(ds, proto_ops, old_proto_ops); 6277a29d220SVladimir Oltean if (err) 6287a29d220SVladimir Oltean goto setup_shared_failed; 6297a29d220SVladimir Oltean 6307a29d220SVladimir Oltean if (old_proto_ops) 6317a29d220SVladimir Oltean old_proto_ops->teardown(ds); 6327a29d220SVladimir Oltean 6337a29d220SVladimir Oltean felix->tag_proto_ops = proto_ops; 634adb3dccfSVladimir Oltean felix->tag_proto = proto; 635adb3dccfSVladimir Oltean 636adb3dccfSVladimir Oltean return 0; 6377a29d220SVladimir Oltean 6387a29d220SVladimir Oltean setup_shared_failed: 6397a29d220SVladimir Oltean proto_ops->teardown(ds); 6407a29d220SVladimir Oltean setup_failed: 6417a29d220SVladimir Oltean return err; 642adb3dccfSVladimir Oltean } 643adb3dccfSVladimir Oltean 64456051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 6454d776482SFlorian Fainelli int port, 6464d776482SFlorian Fainelli enum dsa_tag_protocol mp) 64756051948SVladimir Oltean { 648adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 649adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 650adb3dccfSVladimir Oltean 651adb3dccfSVladimir Oltean return felix->tag_proto; 65256051948SVladimir Oltean } 65356051948SVladimir Oltean 65472c3b0c7SVladimir Oltean static void felix_port_set_host_flood(struct dsa_switch *ds, int port, 65572c3b0c7SVladimir Oltean bool uc, bool mc) 65672c3b0c7SVladimir Oltean { 65772c3b0c7SVladimir Oltean struct ocelot *ocelot = ds->priv; 65872c3b0c7SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 6597a29d220SVladimir Oltean unsigned long mask; 66072c3b0c7SVladimir Oltean 66172c3b0c7SVladimir Oltean if (uc) 66272c3b0c7SVladimir Oltean felix->host_flood_uc_mask |= BIT(port); 66372c3b0c7SVladimir Oltean else 66472c3b0c7SVladimir Oltean felix->host_flood_uc_mask &= ~BIT(port); 66572c3b0c7SVladimir Oltean 66672c3b0c7SVladimir Oltean if (mc) 66772c3b0c7SVladimir Oltean felix->host_flood_mc_mask |= BIT(port); 66872c3b0c7SVladimir Oltean else 66972c3b0c7SVladimir Oltean felix->host_flood_mc_mask &= ~BIT(port); 67072c3b0c7SVladimir Oltean 6717a29d220SVladimir Oltean mask = felix->tag_proto_ops->get_host_fwd_mask(ds); 6727a29d220SVladimir Oltean felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask, 6737a29d220SVladimir Oltean !!felix->host_flood_mc_mask, true); 67472c3b0c7SVladimir Oltean } 67572c3b0c7SVladimir Oltean 67656051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds, 67756051948SVladimir Oltean unsigned int ageing_time) 67856051948SVladimir Oltean { 67956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 68056051948SVladimir Oltean 68156051948SVladimir Oltean ocelot_set_ageing_time(ocelot, ageing_time); 68256051948SVladimir Oltean 68356051948SVladimir Oltean return 0; 68456051948SVladimir Oltean } 68556051948SVladimir Oltean 6865cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port) 6875cad43a5SVladimir Oltean { 6885cad43a5SVladimir Oltean struct ocelot *ocelot = ds->priv; 6895cad43a5SVladimir Oltean int err; 6905cad43a5SVladimir Oltean 6915cad43a5SVladimir Oltean err = ocelot_mact_flush(ocelot, port); 6925cad43a5SVladimir Oltean if (err) 6935cad43a5SVladimir Oltean dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n", 6945cad43a5SVladimir Oltean port, ERR_PTR(err)); 6955cad43a5SVladimir Oltean } 6965cad43a5SVladimir Oltean 69756051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port, 69856051948SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 69956051948SVladimir Oltean { 70056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 70156051948SVladimir Oltean 70256051948SVladimir Oltean return ocelot_fdb_dump(ocelot, port, cb, data); 70356051948SVladimir Oltean } 70456051948SVladimir Oltean 70556051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port, 706c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 707c2693363SVladimir Oltean struct dsa_db db) 70856051948SVladimir Oltean { 70954c31984SVladimir Oltean struct net_device *bridge_dev = felix_classify_db(db); 710e9b3ba43SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 71156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 71256051948SVladimir Oltean 71354c31984SVladimir Oltean if (IS_ERR(bridge_dev)) 71454c31984SVladimir Oltean return PTR_ERR(bridge_dev); 71554c31984SVladimir Oltean 716e9b3ba43SVladimir Oltean if (dsa_port_is_cpu(dp) && !bridge_dev && 7177e580490SVladimir Oltean dsa_fdb_present_in_other_db(ds, port, addr, vid, db)) 7187e580490SVladimir Oltean return 0; 7197e580490SVladimir Oltean 720e9b3ba43SVladimir Oltean if (dsa_port_is_cpu(dp)) 721e9b3ba43SVladimir Oltean port = PGID_CPU; 722e9b3ba43SVladimir Oltean 72354c31984SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev); 72456051948SVladimir Oltean } 72556051948SVladimir Oltean 72656051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port, 727c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 728c2693363SVladimir Oltean struct dsa_db db) 72956051948SVladimir Oltean { 73054c31984SVladimir Oltean struct net_device *bridge_dev = felix_classify_db(db); 731e9b3ba43SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 73256051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 73356051948SVladimir Oltean 73454c31984SVladimir Oltean if (IS_ERR(bridge_dev)) 73554c31984SVladimir Oltean return PTR_ERR(bridge_dev); 73654c31984SVladimir Oltean 737e9b3ba43SVladimir Oltean if (dsa_port_is_cpu(dp) && !bridge_dev && 7387e580490SVladimir Oltean dsa_fdb_present_in_other_db(ds, port, addr, vid, db)) 7397e580490SVladimir Oltean return 0; 7407e580490SVladimir Oltean 741e9b3ba43SVladimir Oltean if (dsa_port_is_cpu(dp)) 742e9b3ba43SVladimir Oltean port = PGID_CPU; 743e9b3ba43SVladimir Oltean 74454c31984SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev); 74556051948SVladimir Oltean } 74656051948SVladimir Oltean 747961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag, 748c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 749c2693363SVladimir Oltean struct dsa_db db) 750961d8b69SVladimir Oltean { 75154c31984SVladimir Oltean struct net_device *bridge_dev = felix_classify_db(db); 752961d8b69SVladimir Oltean struct ocelot *ocelot = ds->priv; 753961d8b69SVladimir Oltean 75454c31984SVladimir Oltean if (IS_ERR(bridge_dev)) 75554c31984SVladimir Oltean return PTR_ERR(bridge_dev); 75654c31984SVladimir Oltean 75754c31984SVladimir Oltean return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev); 758961d8b69SVladimir Oltean } 759961d8b69SVladimir Oltean 760961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag, 761c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 762c2693363SVladimir Oltean struct dsa_db db) 763961d8b69SVladimir Oltean { 76454c31984SVladimir Oltean struct net_device *bridge_dev = felix_classify_db(db); 765961d8b69SVladimir Oltean struct ocelot *ocelot = ds->priv; 766961d8b69SVladimir Oltean 76754c31984SVladimir Oltean if (IS_ERR(bridge_dev)) 76854c31984SVladimir Oltean return PTR_ERR(bridge_dev); 76954c31984SVladimir Oltean 77054c31984SVladimir Oltean return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev); 771961d8b69SVladimir Oltean } 772961d8b69SVladimir Oltean 773a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 774c2693363SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 775c2693363SVladimir Oltean struct dsa_db db) 776209edf95SVladimir Oltean { 77754c31984SVladimir Oltean struct net_device *bridge_dev = felix_classify_db(db); 778209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 779209edf95SVladimir Oltean 78054c31984SVladimir Oltean if (IS_ERR(bridge_dev)) 78154c31984SVladimir Oltean return PTR_ERR(bridge_dev); 78254c31984SVladimir Oltean 7837e580490SVladimir Oltean if (dsa_is_cpu_port(ds, port) && !bridge_dev && 7847e580490SVladimir Oltean dsa_mdb_present_in_other_db(ds, port, mdb, db)) 7857e580490SVladimir Oltean return 0; 7867e580490SVladimir Oltean 7870ddf83cdSVladimir Oltean if (port == ocelot->npi) 7880ddf83cdSVladimir Oltean port = ocelot->num_phys_ports; 7890ddf83cdSVladimir Oltean 79054c31984SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev); 791209edf95SVladimir Oltean } 792209edf95SVladimir Oltean 793209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port, 794c2693363SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 795c2693363SVladimir Oltean struct dsa_db db) 796209edf95SVladimir Oltean { 79754c31984SVladimir Oltean struct net_device *bridge_dev = felix_classify_db(db); 798209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 799209edf95SVladimir Oltean 80054c31984SVladimir Oltean if (IS_ERR(bridge_dev)) 80154c31984SVladimir Oltean return PTR_ERR(bridge_dev); 80254c31984SVladimir Oltean 8037e580490SVladimir Oltean if (dsa_is_cpu_port(ds, port) && !bridge_dev && 8047e580490SVladimir Oltean dsa_mdb_present_in_other_db(ds, port, mdb, db)) 8057e580490SVladimir Oltean return 0; 8067e580490SVladimir Oltean 8070ddf83cdSVladimir Oltean if (port == ocelot->npi) 8080ddf83cdSVladimir Oltean port = ocelot->num_phys_ports; 8090ddf83cdSVladimir Oltean 81054c31984SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev); 811209edf95SVladimir Oltean } 812209edf95SVladimir Oltean 81356051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 81456051948SVladimir Oltean u8 state) 81556051948SVladimir Oltean { 81656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 81756051948SVladimir Oltean 81856051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 81956051948SVladimir Oltean } 82056051948SVladimir Oltean 821421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 822421741eaSVladimir Oltean struct switchdev_brport_flags val, 823421741eaSVladimir Oltean struct netlink_ext_ack *extack) 824421741eaSVladimir Oltean { 825421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 826421741eaSVladimir Oltean 827421741eaSVladimir Oltean return ocelot_port_pre_bridge_flags(ocelot, port, val); 828421741eaSVladimir Oltean } 829421741eaSVladimir Oltean 830421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port, 831421741eaSVladimir Oltean struct switchdev_brport_flags val, 832421741eaSVladimir Oltean struct netlink_ext_ack *extack) 833421741eaSVladimir Oltean { 834421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 835421741eaSVladimir Oltean 836910ee6ccSVladimir Oltean if (port == ocelot->npi) 837910ee6ccSVladimir Oltean port = ocelot->num_phys_ports; 838910ee6ccSVladimir Oltean 839421741eaSVladimir Oltean ocelot_port_bridge_flags(ocelot, port, val); 840421741eaSVladimir Oltean 841421741eaSVladimir Oltean return 0; 842421741eaSVladimir Oltean } 843421741eaSVladimir Oltean 84456051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 84506b9cce4SVladimir Oltean struct dsa_bridge bridge, bool *tx_fwd_offload, 84606b9cce4SVladimir Oltean struct netlink_ext_ack *extack) 84756051948SVladimir Oltean { 84856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 84956051948SVladimir Oltean 85054c31984SVladimir Oltean return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num, 85154c31984SVladimir Oltean extack); 85256051948SVladimir Oltean } 85356051948SVladimir Oltean 85456051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 855d3eed0e5SVladimir Oltean struct dsa_bridge bridge) 85656051948SVladimir Oltean { 85756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 85856051948SVladimir Oltean 859d3eed0e5SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, bridge.dev); 86056051948SVladimir Oltean } 86156051948SVladimir Oltean 8628fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 863dedd6a00SVladimir Oltean struct dsa_lag lag, 8648fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 8658fe6832eSVladimir Oltean { 8668fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 8678fe6832eSVladimir Oltean 868dedd6a00SVladimir Oltean return ocelot_port_lag_join(ocelot, port, lag.dev, info); 8698fe6832eSVladimir Oltean } 8708fe6832eSVladimir Oltean 8718fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 872dedd6a00SVladimir Oltean struct dsa_lag lag) 8738fe6832eSVladimir Oltean { 8748fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 8758fe6832eSVladimir Oltean 876dedd6a00SVladimir Oltean ocelot_port_lag_leave(ocelot, port, lag.dev); 8778fe6832eSVladimir Oltean 8788fe6832eSVladimir Oltean return 0; 8798fe6832eSVladimir Oltean } 8808fe6832eSVladimir Oltean 8818fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 8828fe6832eSVladimir Oltean { 8838fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 8848fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 8858fe6832eSVladimir Oltean 8868fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 8878fe6832eSVladimir Oltean 8888fe6832eSVladimir Oltean return 0; 8898fe6832eSVladimir Oltean } 8908fe6832eSVladimir Oltean 89156051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 89201af940eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 89301af940eSVladimir Oltean struct netlink_ext_ack *extack) 89456051948SVladimir Oltean { 8952f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 896b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 8972f0402feSVladimir Oltean 8989a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 8999a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 9009a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 9019a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 9029a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 9039a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 9049a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 9059a720680SVladimir Oltean */ 9069a720680SVladimir Oltean if (port == ocelot->npi) 9079a720680SVladimir Oltean return 0; 9089a720680SVladimir Oltean 909b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 9102f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 91101af940eSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED, 91201af940eSVladimir Oltean extack); 91356051948SVladimir Oltean } 91456051948SVladimir Oltean 91589153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 91689153ed6SVladimir Oltean struct netlink_ext_ack *extack) 91756051948SVladimir Oltean { 91856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 91956051948SVladimir Oltean 9203b95d1b2SVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 92156051948SVladimir Oltean } 92256051948SVladimir Oltean 9231958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 92431046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 92531046a5fSVladimir Oltean struct netlink_ext_ack *extack) 92656051948SVladimir Oltean { 92756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 928183be6f9SVladimir Oltean u16 flags = vlan->flags; 9291958d581SVladimir Oltean int err; 93056051948SVladimir Oltean 93101af940eSVladimir Oltean err = felix_vlan_prepare(ds, port, vlan, extack); 9321958d581SVladimir Oltean if (err) 9331958d581SVladimir Oltean return err; 9341958d581SVladimir Oltean 9351958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 936183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 937183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 93856051948SVladimir Oltean } 93956051948SVladimir Oltean 94056051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 94156051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 94256051948SVladimir Oltean { 94356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 94456051948SVladimir Oltean 945b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 94656051948SVladimir Oltean } 94756051948SVladimir Oltean 94879fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port, 94979fda660SRussell King (Oracle) struct phylink_config *config) 95079fda660SRussell King (Oracle) { 95179fda660SRussell King (Oracle) struct ocelot *ocelot = ds->priv; 95279fda660SRussell King (Oracle) 953f6f04c02SRussell King (Oracle) /* This driver does not make use of the speed, duplex, pause or the 954f6f04c02SRussell King (Oracle) * advertisement in its mac_config, so it is safe to mark this driver 955f6f04c02SRussell King (Oracle) * as non-legacy. 956f6f04c02SRussell King (Oracle) */ 957f6f04c02SRussell King (Oracle) config->legacy_pre_march2020 = false; 958f6f04c02SRussell King (Oracle) 95979fda660SRussell King (Oracle) __set_bit(ocelot->ports[port]->phy_mode, 96079fda660SRussell King (Oracle) config->supported_interfaces); 96179fda660SRussell King (Oracle) } 96279fda660SRussell King (Oracle) 963bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port, 964bdeced75SVladimir Oltean unsigned long *supported, 965bdeced75SVladimir Oltean struct phylink_link_state *state) 966bdeced75SVladimir Oltean { 967bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 968375e1314SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 969bdeced75SVladimir Oltean 970375e1314SVladimir Oltean if (felix->info->phylink_validate) 971375e1314SVladimir Oltean felix->info->phylink_validate(ocelot, port, supported, state); 972bdeced75SVladimir Oltean } 973bdeced75SVladimir Oltean 974864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds, 975864ba485SRussell King (Oracle) int port, 976864ba485SRussell King (Oracle) phy_interface_t iface) 977bdeced75SVladimir Oltean { 978bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 979bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 980864ba485SRussell King (Oracle) struct phylink_pcs *pcs = NULL; 981bdeced75SVladimir Oltean 98249af6a76SColin Foster if (felix->pcs && felix->pcs[port]) 983864ba485SRussell King (Oracle) pcs = felix->pcs[port]; 984864ba485SRussell King (Oracle) 985864ba485SRussell King (Oracle) return pcs; 986bdeced75SVladimir Oltean } 987bdeced75SVladimir Oltean 988bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 989bdeced75SVladimir Oltean unsigned int link_an_mode, 990bdeced75SVladimir Oltean phy_interface_t interface) 991bdeced75SVladimir Oltean { 992bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 993bdeced75SVladimir Oltean 994e6e12df6SVladimir Oltean ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 995e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 996bdeced75SVladimir Oltean } 997bdeced75SVladimir Oltean 998bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 999bdeced75SVladimir Oltean unsigned int link_an_mode, 1000bdeced75SVladimir Oltean phy_interface_t interface, 10015b502a7bSRussell King struct phy_device *phydev, 10025b502a7bSRussell King int speed, int duplex, 10035b502a7bSRussell King bool tx_pause, bool rx_pause) 1004bdeced75SVladimir Oltean { 1005bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 10067e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1007bdeced75SVladimir Oltean 1008e6e12df6SVladimir Oltean ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 1009e6e12df6SVladimir Oltean interface, speed, duplex, tx_pause, rx_pause, 1010e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 10117e14a2dcSVladimir Oltean 10127e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 10137e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 1014bdeced75SVladimir Oltean } 1015bdeced75SVladimir Oltean 1016bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 1017bd2b3161SXiaoliang Yang { 1018bd2b3161SXiaoliang Yang int i; 1019bd2b3161SXiaoliang Yang 1020bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 1021bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 1022bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 1023bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 1024bd2b3161SXiaoliang Yang port); 1025bd2b3161SXiaoliang Yang 102670d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 1027bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 1028bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 1029bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 1030bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 1031bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 1032bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 1033bd2b3161SXiaoliang Yang port, i); 1034bd2b3161SXiaoliang Yang } 1035bd2b3161SXiaoliang Yang } 1036bd2b3161SXiaoliang Yang 1037776b71e5SVladimir Oltean static void felix_get_stats64(struct dsa_switch *ds, int port, 1038776b71e5SVladimir Oltean struct rtnl_link_stats64 *stats) 1039776b71e5SVladimir Oltean { 1040776b71e5SVladimir Oltean struct ocelot *ocelot = ds->priv; 1041776b71e5SVladimir Oltean 1042776b71e5SVladimir Oltean ocelot_port_get_stats64(ocelot, port, stats); 1043776b71e5SVladimir Oltean } 1044776b71e5SVladimir Oltean 1045*e32036e1SVladimir Oltean static void felix_get_pause_stats(struct dsa_switch *ds, int port, 1046*e32036e1SVladimir Oltean struct ethtool_pause_stats *pause_stats) 1047*e32036e1SVladimir Oltean { 1048*e32036e1SVladimir Oltean struct ocelot *ocelot = ds->priv; 1049*e32036e1SVladimir Oltean 1050*e32036e1SVladimir Oltean ocelot_port_get_pause_stats(ocelot, port, pause_stats); 1051*e32036e1SVladimir Oltean } 1052*e32036e1SVladimir Oltean 1053*e32036e1SVladimir Oltean static void felix_get_rmon_stats(struct dsa_switch *ds, int port, 1054*e32036e1SVladimir Oltean struct ethtool_rmon_stats *rmon_stats, 1055*e32036e1SVladimir Oltean const struct ethtool_rmon_hist_range **ranges) 1056*e32036e1SVladimir Oltean { 1057*e32036e1SVladimir Oltean struct ocelot *ocelot = ds->priv; 1058*e32036e1SVladimir Oltean 1059*e32036e1SVladimir Oltean ocelot_port_get_rmon_stats(ocelot, port, rmon_stats, ranges); 1060*e32036e1SVladimir Oltean } 1061*e32036e1SVladimir Oltean 1062*e32036e1SVladimir Oltean static void felix_get_eth_ctrl_stats(struct dsa_switch *ds, int port, 1063*e32036e1SVladimir Oltean struct ethtool_eth_ctrl_stats *ctrl_stats) 1064*e32036e1SVladimir Oltean { 1065*e32036e1SVladimir Oltean struct ocelot *ocelot = ds->priv; 1066*e32036e1SVladimir Oltean 1067*e32036e1SVladimir Oltean ocelot_port_get_eth_ctrl_stats(ocelot, port, ctrl_stats); 1068*e32036e1SVladimir Oltean } 1069*e32036e1SVladimir Oltean 1070*e32036e1SVladimir Oltean static void felix_get_eth_mac_stats(struct dsa_switch *ds, int port, 1071*e32036e1SVladimir Oltean struct ethtool_eth_mac_stats *mac_stats) 1072*e32036e1SVladimir Oltean { 1073*e32036e1SVladimir Oltean struct ocelot *ocelot = ds->priv; 1074*e32036e1SVladimir Oltean 1075*e32036e1SVladimir Oltean ocelot_port_get_eth_mac_stats(ocelot, port, mac_stats); 1076*e32036e1SVladimir Oltean } 1077*e32036e1SVladimir Oltean 1078*e32036e1SVladimir Oltean static void felix_get_eth_phy_stats(struct dsa_switch *ds, int port, 1079*e32036e1SVladimir Oltean struct ethtool_eth_phy_stats *phy_stats) 1080*e32036e1SVladimir Oltean { 1081*e32036e1SVladimir Oltean struct ocelot *ocelot = ds->priv; 1082*e32036e1SVladimir Oltean 1083*e32036e1SVladimir Oltean ocelot_port_get_eth_phy_stats(ocelot, port, phy_stats); 1084*e32036e1SVladimir Oltean } 1085*e32036e1SVladimir Oltean 108656051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 108756051948SVladimir Oltean u32 stringset, u8 *data) 108856051948SVladimir Oltean { 108956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 109056051948SVladimir Oltean 109156051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 109256051948SVladimir Oltean } 109356051948SVladimir Oltean 109456051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 109556051948SVladimir Oltean { 109656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 109756051948SVladimir Oltean 109856051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 109956051948SVladimir Oltean } 110056051948SVladimir Oltean 110156051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 110256051948SVladimir Oltean { 110356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 110456051948SVladimir Oltean 110556051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 110656051948SVladimir Oltean } 110756051948SVladimir Oltean 110856051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 110956051948SVladimir Oltean struct ethtool_ts_info *info) 111056051948SVladimir Oltean { 111156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 111256051948SVladimir Oltean 111356051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 111456051948SVladimir Oltean } 111556051948SVladimir Oltean 1116acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = { 1117acf242fcSColin Foster [PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL, 1118acf242fcSColin Foster [PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII, 1119acf242fcSColin Foster [PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII, 1120acf242fcSColin Foster [PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII, 112111ecf341SVladimir Oltean [PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX, 1122acf242fcSColin Foster [PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX, 1123acf242fcSColin Foster }; 1124acf242fcSColin Foster 1125acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port, 1126acf242fcSColin Foster phy_interface_t phy_mode) 1127acf242fcSColin Foster { 1128acf242fcSColin Foster u32 modes = felix->info->port_modes[port]; 1129acf242fcSColin Foster 1130acf242fcSColin Foster if (felix_phy_match_table[phy_mode] & modes) 1131acf242fcSColin Foster return 0; 1132acf242fcSColin Foster return -EOPNOTSUPP; 1133acf242fcSColin Foster } 1134acf242fcSColin Foster 1135bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 1136bdeced75SVladimir Oltean struct device_node *ports_node, 1137bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 1138bdeced75SVladimir Oltean { 1139bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 1140bdeced75SVladimir Oltean struct device_node *child; 1141bdeced75SVladimir Oltean 114237fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 1143bdeced75SVladimir Oltean phy_interface_t phy_mode; 1144bdeced75SVladimir Oltean u32 port; 1145bdeced75SVladimir Oltean int err; 1146bdeced75SVladimir Oltean 1147bdeced75SVladimir Oltean /* Get switch port number from DT */ 1148bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 1149bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 1150bdeced75SVladimir Oltean "(property \"reg\")\n"); 1151bdeced75SVladimir Oltean of_node_put(child); 1152bdeced75SVladimir Oltean return -ENODEV; 1153bdeced75SVladimir Oltean } 1154bdeced75SVladimir Oltean 1155bdeced75SVladimir Oltean /* Get PHY mode from DT */ 1156bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 1157bdeced75SVladimir Oltean if (err) { 1158bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 1159bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 1160bdeced75SVladimir Oltean port); 1161bdeced75SVladimir Oltean of_node_put(child); 1162bdeced75SVladimir Oltean return -ENODEV; 1163bdeced75SVladimir Oltean } 1164bdeced75SVladimir Oltean 1165acf242fcSColin Foster err = felix_validate_phy_mode(felix, port, phy_mode); 1166bdeced75SVladimir Oltean if (err < 0) { 1167bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 1168bdeced75SVladimir Oltean phy_modes(phy_mode), port); 116959ebb430SSumera Priyadarsini of_node_put(child); 1170bdeced75SVladimir Oltean return err; 1171bdeced75SVladimir Oltean } 1172bdeced75SVladimir Oltean 1173bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 1174bdeced75SVladimir Oltean } 1175bdeced75SVladimir Oltean 1176bdeced75SVladimir Oltean return 0; 1177bdeced75SVladimir Oltean } 1178bdeced75SVladimir Oltean 1179bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 1180bdeced75SVladimir Oltean { 1181bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 1182bdeced75SVladimir Oltean struct device_node *switch_node; 1183bdeced75SVladimir Oltean struct device_node *ports_node; 1184bdeced75SVladimir Oltean int err; 1185bdeced75SVladimir Oltean 1186bdeced75SVladimir Oltean switch_node = dev->of_node; 1187bdeced75SVladimir Oltean 1188bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 1189abecbfcdSVladimir Oltean if (!ports_node) 1190abecbfcdSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 1191bdeced75SVladimir Oltean if (!ports_node) { 1192abecbfcdSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 1193bdeced75SVladimir Oltean return -ENODEV; 1194bdeced75SVladimir Oltean } 1195bdeced75SVladimir Oltean 1196bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 1197bdeced75SVladimir Oltean of_node_put(ports_node); 1198bdeced75SVladimir Oltean 1199bdeced75SVladimir Oltean return err; 1200bdeced75SVladimir Oltean } 1201bdeced75SVladimir Oltean 120256051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 120356051948SVladimir Oltean { 120456051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 1205bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 1206b4024c9eSClaudiu Manoil struct resource res; 120756051948SVladimir Oltean int port, i, err; 120856051948SVladimir Oltean 120956051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 121056051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 121156051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 121256051948SVladimir Oltean if (!ocelot->ports) 121356051948SVladimir Oltean return -ENOMEM; 121456051948SVladimir Oltean 121556051948SVladimir Oltean ocelot->map = felix->info->map; 121656051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 121721ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 121807d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 121977043c37SXiaoliang Yang ocelot->vcap_pol.base = felix->info->vcap_pol_base; 122077043c37SXiaoliang Yang ocelot->vcap_pol.max = felix->info->vcap_pol_max; 122177043c37SXiaoliang Yang ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 122277043c37SXiaoliang Yang ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 122356051948SVladimir Oltean ocelot->ops = felix->info->ops; 1224cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 1225cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 1226f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 122756051948SVladimir Oltean 1228bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 1229bdeced75SVladimir Oltean GFP_KERNEL); 1230bdeced75SVladimir Oltean if (!port_phy_modes) 1231bdeced75SVladimir Oltean return -ENOMEM; 1232bdeced75SVladimir Oltean 1233bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 1234bdeced75SVladimir Oltean if (err) { 1235bdeced75SVladimir Oltean kfree(port_phy_modes); 1236bdeced75SVladimir Oltean return err; 1237bdeced75SVladimir Oltean } 1238bdeced75SVladimir Oltean 123956051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 124056051948SVladimir Oltean struct regmap *target; 124156051948SVladimir Oltean 124256051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 124356051948SVladimir Oltean continue; 124456051948SVladimir Oltean 1245b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 1246b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1247375e1314SVladimir Oltean res.start += felix->switch_base; 1248375e1314SVladimir Oltean res.end += felix->switch_base; 124956051948SVladimir Oltean 1250242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 125156051948SVladimir Oltean if (IS_ERR(target)) { 125256051948SVladimir Oltean dev_err(ocelot->dev, 125356051948SVladimir Oltean "Failed to map device memory space\n"); 1254bdeced75SVladimir Oltean kfree(port_phy_modes); 125556051948SVladimir Oltean return PTR_ERR(target); 125656051948SVladimir Oltean } 125756051948SVladimir Oltean 125856051948SVladimir Oltean ocelot->targets[i] = target; 125956051948SVladimir Oltean } 126056051948SVladimir Oltean 126156051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 126256051948SVladimir Oltean if (err) { 126356051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 1264bdeced75SVladimir Oltean kfree(port_phy_modes); 126556051948SVladimir Oltean return err; 126656051948SVladimir Oltean } 126756051948SVladimir Oltean 126856051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 126956051948SVladimir Oltean struct ocelot_port *ocelot_port; 127091c724cfSVladimir Oltean struct regmap *target; 127156051948SVladimir Oltean 127256051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 127356051948SVladimir Oltean sizeof(struct ocelot_port), 127456051948SVladimir Oltean GFP_KERNEL); 127556051948SVladimir Oltean if (!ocelot_port) { 127656051948SVladimir Oltean dev_err(ocelot->dev, 127756051948SVladimir Oltean "failed to allocate port memory\n"); 1278bdeced75SVladimir Oltean kfree(port_phy_modes); 127956051948SVladimir Oltean return -ENOMEM; 128056051948SVladimir Oltean } 128156051948SVladimir Oltean 1282b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1283b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1284375e1314SVladimir Oltean res.start += felix->switch_base; 1285375e1314SVladimir Oltean res.end += felix->switch_base; 128656051948SVladimir Oltean 1287242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 128891c724cfSVladimir Oltean if (IS_ERR(target)) { 128956051948SVladimir Oltean dev_err(ocelot->dev, 129091c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 129191c724cfSVladimir Oltean port); 1292bdeced75SVladimir Oltean kfree(port_phy_modes); 129391c724cfSVladimir Oltean return PTR_ERR(target); 129456051948SVladimir Oltean } 129556051948SVladimir Oltean 1296bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 129756051948SVladimir Oltean ocelot_port->ocelot = ocelot; 129891c724cfSVladimir Oltean ocelot_port->target = target; 12997e708760SVladimir Oltean ocelot_port->index = port; 130056051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 130156051948SVladimir Oltean } 130256051948SVladimir Oltean 1303bdeced75SVladimir Oltean kfree(port_phy_modes); 1304bdeced75SVladimir Oltean 1305bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1306bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1307bdeced75SVladimir Oltean if (err < 0) 1308bdeced75SVladimir Oltean return err; 1309bdeced75SVladimir Oltean } 1310bdeced75SVladimir Oltean 131156051948SVladimir Oltean return 0; 131256051948SVladimir Oltean } 131356051948SVladimir Oltean 13141328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 13151328a883SVladimir Oltean struct sk_buff *skb) 13161328a883SVladimir Oltean { 13171328a883SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 13181328a883SVladimir Oltean struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 13191328a883SVladimir Oltean struct sk_buff *skb_match = NULL, *skb_tmp; 13201328a883SVladimir Oltean unsigned long flags; 13211328a883SVladimir Oltean 13221328a883SVladimir Oltean if (!clone) 13231328a883SVladimir Oltean return; 13241328a883SVladimir Oltean 13251328a883SVladimir Oltean spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 13261328a883SVladimir Oltean 13271328a883SVladimir Oltean skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 13281328a883SVladimir Oltean if (skb != clone) 13291328a883SVladimir Oltean continue; 13301328a883SVladimir Oltean __skb_unlink(skb, &ocelot_port->tx_skbs); 13311328a883SVladimir Oltean skb_match = skb; 13321328a883SVladimir Oltean break; 13331328a883SVladimir Oltean } 13341328a883SVladimir Oltean 13351328a883SVladimir Oltean spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 13361328a883SVladimir Oltean 13371328a883SVladimir Oltean WARN_ONCE(!skb_match, 13381328a883SVladimir Oltean "Could not find skb clone in TX timestamping list\n"); 13391328a883SVladimir Oltean } 13401328a883SVladimir Oltean 134149f885b2SVladimir Oltean #define work_to_xmit_work(w) \ 134249f885b2SVladimir Oltean container_of((w), struct felix_deferred_xmit_work, work) 134349f885b2SVladimir Oltean 134449f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work) 134549f885b2SVladimir Oltean { 134649f885b2SVladimir Oltean struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 134749f885b2SVladimir Oltean struct dsa_switch *ds = xmit_work->dp->ds; 134849f885b2SVladimir Oltean struct sk_buff *skb = xmit_work->skb; 134949f885b2SVladimir Oltean u32 rew_op = ocelot_ptp_rew_op(skb); 135049f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 135149f885b2SVladimir Oltean int port = xmit_work->dp->index; 135249f885b2SVladimir Oltean int retries = 10; 135349f885b2SVladimir Oltean 135449f885b2SVladimir Oltean do { 135549f885b2SVladimir Oltean if (ocelot_can_inject(ocelot, 0)) 135649f885b2SVladimir Oltean break; 135749f885b2SVladimir Oltean 135849f885b2SVladimir Oltean cpu_relax(); 135949f885b2SVladimir Oltean } while (--retries); 136049f885b2SVladimir Oltean 136149f885b2SVladimir Oltean if (!retries) { 136249f885b2SVladimir Oltean dev_err(ocelot->dev, "port %d failed to inject skb\n", 136349f885b2SVladimir Oltean port); 13641328a883SVladimir Oltean ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 136549f885b2SVladimir Oltean kfree_skb(skb); 136649f885b2SVladimir Oltean return; 136749f885b2SVladimir Oltean } 136849f885b2SVladimir Oltean 136949f885b2SVladimir Oltean ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 137049f885b2SVladimir Oltean 137149f885b2SVladimir Oltean consume_skb(skb); 137249f885b2SVladimir Oltean kfree(xmit_work); 137349f885b2SVladimir Oltean } 137449f885b2SVladimir Oltean 137535d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds, 137635d97680SVladimir Oltean enum dsa_tag_protocol proto) 137749f885b2SVladimir Oltean { 137835d97680SVladimir Oltean struct ocelot_8021q_tagger_data *tagger_data; 137949f885b2SVladimir Oltean 138035d97680SVladimir Oltean switch (proto) { 138135d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 138235d97680SVladimir Oltean tagger_data = ocelot_8021q_tagger_data(ds); 138335d97680SVladimir Oltean tagger_data->xmit_work_fn = felix_port_deferred_xmit; 138449f885b2SVladimir Oltean return 0; 138535d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT: 138635d97680SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 138749f885b2SVladimir Oltean return 0; 138835d97680SVladimir Oltean default: 138935d97680SVladimir Oltean return -EPROTONOSUPPORT; 139049f885b2SVladimir Oltean } 139149f885b2SVladimir Oltean } 139249f885b2SVladimir Oltean 139356051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with 139456051948SVladimir Oltean * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 139556051948SVladimir Oltean * us to allocate structures twice (leak memory) and map PCI memory twice 139656051948SVladimir Oltean * (which will not work). 139756051948SVladimir Oltean */ 139856051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds) 139956051948SVladimir Oltean { 140056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 140156051948SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 14022960bb14SVladimir Oltean struct dsa_port *dp; 14032960bb14SVladimir Oltean int err; 140456051948SVladimir Oltean 140556051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 140656051948SVladimir Oltean if (err) 140756051948SVladimir Oltean return err; 140856051948SVladimir Oltean 1409d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1410d1cc0e93SVladimir Oltean if (err) 14116b73b7c9SVladimir Oltean goto out_mdiobus_free; 1412d1cc0e93SVladimir Oltean 14132b49d128SYangbo Lu if (ocelot->ptp) { 14142ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 14152b49d128SYangbo Lu if (err) { 14162b49d128SYangbo Lu dev_err(ocelot->dev, 14172b49d128SYangbo Lu "Timestamp initialization failed\n"); 14182b49d128SYangbo Lu ocelot->ptp = 0; 14192b49d128SYangbo Lu } 14202b49d128SYangbo Lu } 142156051948SVladimir Oltean 14222960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 14232960bb14SVladimir Oltean ocelot_init_port(ocelot, dp->index); 1424bd2b3161SXiaoliang Yang 1425bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1426bd2b3161SXiaoliang Yang * bits of vlan tag. 1427bd2b3161SXiaoliang Yang */ 14282960bb14SVladimir Oltean felix_port_qos_map_init(ocelot, dp->index); 142956051948SVladimir Oltean } 143056051948SVladimir Oltean 1431f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1432f59fd9caSVladimir Oltean if (err) 14336b73b7c9SVladimir Oltean goto out_deinit_ports; 1434f59fd9caSVladimir Oltean 14357a29d220SVladimir Oltean /* The initial tag protocol is NPI which won't fail during initial 14367a29d220SVladimir Oltean * setup, there's no real point in checking for errors. 14371cf3299bSVladimir Oltean */ 14387a29d220SVladimir Oltean felix_change_tag_protocol(ds, felix->tag_proto); 14391cf3299bSVladimir Oltean 14400b912fc9SVladimir Oltean ds->mtu_enforcement_ingress = true; 1441c54913c1SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 144254c31984SVladimir Oltean ds->fdb_isolation = true; 144354c31984SVladimir Oltean ds->max_num_bridges = ds->num_ports; 1444bdeced75SVladimir Oltean 144556051948SVladimir Oltean return 0; 14466b73b7c9SVladimir Oltean 14476b73b7c9SVladimir Oltean out_deinit_ports: 14482960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 14492960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 14506b73b7c9SVladimir Oltean 14516b73b7c9SVladimir Oltean ocelot_deinit_timestamp(ocelot); 14526b73b7c9SVladimir Oltean ocelot_deinit(ocelot); 14536b73b7c9SVladimir Oltean 14546b73b7c9SVladimir Oltean out_mdiobus_free: 14556b73b7c9SVladimir Oltean if (felix->info->mdio_bus_free) 14566b73b7c9SVladimir Oltean felix->info->mdio_bus_free(ocelot); 14576b73b7c9SVladimir Oltean 14586b73b7c9SVladimir Oltean return err; 145956051948SVladimir Oltean } 146056051948SVladimir Oltean 146156051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 146256051948SVladimir Oltean { 146356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1464bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 14652960bb14SVladimir Oltean struct dsa_port *dp; 1466bdeced75SVladimir Oltean 14677a29d220SVladimir Oltean if (felix->tag_proto_ops) 14687a29d220SVladimir Oltean felix->tag_proto_ops->teardown(ds); 1469adb3dccfSVladimir Oltean 14702960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 14712960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 1472d19741b0SVladimir Oltean 147349f885b2SVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 147449f885b2SVladimir Oltean ocelot_deinit_timestamp(ocelot); 147549f885b2SVladimir Oltean ocelot_deinit(ocelot); 147649f885b2SVladimir Oltean 1477d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1478d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 147956051948SVladimir Oltean } 148056051948SVladimir Oltean 1481c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1482c0bcf537SYangbo Lu struct ifreq *ifr) 1483c0bcf537SYangbo Lu { 1484c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1485c0bcf537SYangbo Lu 1486c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1487c0bcf537SYangbo Lu } 1488c0bcf537SYangbo Lu 1489c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1490c0bcf537SYangbo Lu struct ifreq *ifr) 1491c0bcf537SYangbo Lu { 1492c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 149399348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 149499348004SVladimir Oltean bool using_tag_8021q; 149599348004SVladimir Oltean int err; 1496c0bcf537SYangbo Lu 149799348004SVladimir Oltean err = ocelot_hwstamp_set(ocelot, port, ifr); 149899348004SVladimir Oltean if (err) 149999348004SVladimir Oltean return err; 150099348004SVladimir Oltean 150199348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 150299348004SVladimir Oltean 150399348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 1504c0bcf537SYangbo Lu } 1505c0bcf537SYangbo Lu 1506d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot) 15070a6f17c6SVladimir Oltean { 15080a6f17c6SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1509dbd03285SVladimir Oltean int err = 0, grp = 0; 15100a6f17c6SVladimir Oltean 15110a6f17c6SVladimir Oltean if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 15120a6f17c6SVladimir Oltean return false; 15130a6f17c6SVladimir Oltean 15140a6f17c6SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 15150a6f17c6SVladimir Oltean return false; 15160a6f17c6SVladimir Oltean 15170a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 15180a6f17c6SVladimir Oltean struct sk_buff *skb; 15190a6f17c6SVladimir Oltean unsigned int type; 15200a6f17c6SVladimir Oltean 15210a6f17c6SVladimir Oltean err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 15220a6f17c6SVladimir Oltean if (err) 15230a6f17c6SVladimir Oltean goto out; 15240a6f17c6SVladimir Oltean 15250a6f17c6SVladimir Oltean /* We trap to the CPU port module all PTP frames, but 15260a6f17c6SVladimir Oltean * felix_rxtstamp() only gets called for event frames. 15270a6f17c6SVladimir Oltean * So we need to avoid sending duplicate general 15280a6f17c6SVladimir Oltean * message frames by running a second BPF classifier 15290a6f17c6SVladimir Oltean * here and dropping those. 15300a6f17c6SVladimir Oltean */ 15310a6f17c6SVladimir Oltean __skb_push(skb, ETH_HLEN); 15320a6f17c6SVladimir Oltean 15330a6f17c6SVladimir Oltean type = ptp_classify_raw(skb); 15340a6f17c6SVladimir Oltean 15350a6f17c6SVladimir Oltean __skb_pull(skb, ETH_HLEN); 15360a6f17c6SVladimir Oltean 15370a6f17c6SVladimir Oltean if (type == PTP_CLASS_NONE) { 15380a6f17c6SVladimir Oltean kfree_skb(skb); 15390a6f17c6SVladimir Oltean continue; 15400a6f17c6SVladimir Oltean } 15410a6f17c6SVladimir Oltean 15420a6f17c6SVladimir Oltean netif_rx(skb); 15430a6f17c6SVladimir Oltean } 15440a6f17c6SVladimir Oltean 15450a6f17c6SVladimir Oltean out: 15465d3bb7ddSVladimir Oltean if (err < 0) { 15475d3bb7ddSVladimir Oltean dev_err_ratelimited(ocelot->dev, 15485d3bb7ddSVladimir Oltean "Error during packet extraction: %pe\n", 15495d3bb7ddSVladimir Oltean ERR_PTR(err)); 15500a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 15515d3bb7ddSVladimir Oltean } 15520a6f17c6SVladimir Oltean 15530a6f17c6SVladimir Oltean return true; 15540a6f17c6SVladimir Oltean } 15550a6f17c6SVladimir Oltean 1556c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1557c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1558c0bcf537SYangbo Lu { 155992f62485SVladimir Oltean u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1560c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1561c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1562c0bcf537SYangbo Lu struct timespec64 ts; 156392f62485SVladimir Oltean u32 tstamp_hi; 156492f62485SVladimir Oltean u64 tstamp; 1565c0bcf537SYangbo Lu 15660a6f17c6SVladimir Oltean /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 15670a6f17c6SVladimir Oltean * for RX timestamping. Then free it, and poll for its copy through 15680a6f17c6SVladimir Oltean * MMIO in the CPU port module, and inject that into the stack from 15690a6f17c6SVladimir Oltean * ocelot_xtr_poll(). 15700a6f17c6SVladimir Oltean */ 1571d219b4b6SVladimir Oltean if (felix_check_xtr_pkt(ocelot)) { 15720a6f17c6SVladimir Oltean kfree_skb(skb); 15730a6f17c6SVladimir Oltean return true; 15740a6f17c6SVladimir Oltean } 15750a6f17c6SVladimir Oltean 1576c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1577c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1578c0bcf537SYangbo Lu 1579c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1580c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1581c0bcf537SYangbo Lu tstamp_hi--; 1582c0bcf537SYangbo Lu 1583c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1584c0bcf537SYangbo Lu 1585c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1586c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1587c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1588c0bcf537SYangbo Lu return false; 1589c0bcf537SYangbo Lu } 1590c0bcf537SYangbo Lu 15915c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port, 15925c5416f5SYangbo Lu struct sk_buff *skb) 1593c0bcf537SYangbo Lu { 1594c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1595682eaad9SYangbo Lu struct sk_buff *clone = NULL; 1596c0bcf537SYangbo Lu 1597682eaad9SYangbo Lu if (!ocelot->ptp) 15985c5416f5SYangbo Lu return; 1599c0bcf537SYangbo Lu 160052849bcfSVladimir Oltean if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 160152849bcfSVladimir Oltean dev_err_ratelimited(ds->dev, 160252849bcfSVladimir Oltean "port %d delivering skb without TX timestamp\n", 160352849bcfSVladimir Oltean port); 1604682eaad9SYangbo Lu return; 160552849bcfSVladimir Oltean } 1606682eaad9SYangbo Lu 1607682eaad9SYangbo Lu if (clone) 1608c4b364ceSYangbo Lu OCELOT_SKB_CB(skb)->clone = clone; 16095c5416f5SYangbo Lu } 1610c0bcf537SYangbo Lu 16110b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 16120b912fc9SVladimir Oltean { 16130b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 161455a515b1SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 161555a515b1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 16160b912fc9SVladimir Oltean 16170b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 16180b912fc9SVladimir Oltean 161955a515b1SVladimir Oltean mutex_lock(&ocelot->tas_lock); 162055a515b1SVladimir Oltean 162155a515b1SVladimir Oltean if (ocelot_port->taprio && felix->info->tas_guard_bands_update) 162255a515b1SVladimir Oltean felix->info->tas_guard_bands_update(ocelot, port); 162355a515b1SVladimir Oltean 162455a515b1SVladimir Oltean mutex_unlock(&ocelot->tas_lock); 162555a515b1SVladimir Oltean 16260b912fc9SVladimir Oltean return 0; 16270b912fc9SVladimir Oltean } 16280b912fc9SVladimir Oltean 16290b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 16300b912fc9SVladimir Oltean { 16310b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 16320b912fc9SVladimir Oltean 16330b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 16340b912fc9SVladimir Oltean } 16350b912fc9SVladimir Oltean 163607d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 163707d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 163807d985eeSVladimir Oltean { 163907d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 164099348004SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 164199348004SVladimir Oltean bool using_tag_8021q; 164299348004SVladimir Oltean int err; 164307d985eeSVladimir Oltean 164499348004SVladimir Oltean err = ocelot_cls_flower_replace(ocelot, port, cls, ingress); 164599348004SVladimir Oltean if (err) 164699348004SVladimir Oltean return err; 164799348004SVladimir Oltean 164899348004SVladimir Oltean using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 164999348004SVladimir Oltean 165099348004SVladimir Oltean return felix_update_trapping_destinations(ds, using_tag_8021q); 165107d985eeSVladimir Oltean } 165207d985eeSVladimir Oltean 165307d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 165407d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 165507d985eeSVladimir Oltean { 165607d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 165707d985eeSVladimir Oltean 165807d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 165907d985eeSVladimir Oltean } 166007d985eeSVladimir Oltean 166107d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 166207d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 166307d985eeSVladimir Oltean { 166407d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 166507d985eeSVladimir Oltean 166607d985eeSVladimir Oltean return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 166707d985eeSVladimir Oltean } 166807d985eeSVladimir Oltean 1669fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1670fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1671fc411eaaSVladimir Oltean { 1672fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1673fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1674fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 16755f035af7SPo Liu .burst = policer->burst, 1676fc411eaaSVladimir Oltean }; 1677fc411eaaSVladimir Oltean 1678fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1679fc411eaaSVladimir Oltean } 1680fc411eaaSVladimir Oltean 1681fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1682fc411eaaSVladimir Oltean { 1683fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1684fc411eaaSVladimir Oltean 1685fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1686fc411eaaSVladimir Oltean } 1687fc411eaaSVladimir Oltean 16885e497497SVladimir Oltean static int felix_port_mirror_add(struct dsa_switch *ds, int port, 16895e497497SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 16905e497497SVladimir Oltean bool ingress, struct netlink_ext_ack *extack) 16915e497497SVladimir Oltean { 16925e497497SVladimir Oltean struct ocelot *ocelot = ds->priv; 16935e497497SVladimir Oltean 16945e497497SVladimir Oltean return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port, 16955e497497SVladimir Oltean ingress, extack); 16965e497497SVladimir Oltean } 16975e497497SVladimir Oltean 16985e497497SVladimir Oltean static void felix_port_mirror_del(struct dsa_switch *ds, int port, 16995e497497SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 17005e497497SVladimir Oltean { 17015e497497SVladimir Oltean struct ocelot *ocelot = ds->priv; 17025e497497SVladimir Oltean 17035e497497SVladimir Oltean ocelot_port_mirror_del(ocelot, port, mirror->ingress); 17045e497497SVladimir Oltean } 17055e497497SVladimir Oltean 1706de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1707de143c0eSXiaoliang Yang enum tc_setup_type type, 1708de143c0eSXiaoliang Yang void *type_data) 1709de143c0eSXiaoliang Yang { 1710de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1711de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1712de143c0eSXiaoliang Yang 1713de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1714de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1715de143c0eSXiaoliang Yang else 1716de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1717de143c0eSXiaoliang Yang } 1718de143c0eSXiaoliang Yang 1719f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1720f59fd9caSVladimir Oltean u16 pool_index, 1721f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1722f59fd9caSVladimir Oltean { 1723f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1724f59fd9caSVladimir Oltean 1725f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1726f59fd9caSVladimir Oltean } 1727f59fd9caSVladimir Oltean 1728f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1729f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1730f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1731f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1732f59fd9caSVladimir Oltean { 1733f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1734f59fd9caSVladimir Oltean 1735f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1736f59fd9caSVladimir Oltean threshold_type, extack); 1737f59fd9caSVladimir Oltean } 1738f59fd9caSVladimir Oltean 1739f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1740f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1741f59fd9caSVladimir Oltean u32 *p_threshold) 1742f59fd9caSVladimir Oltean { 1743f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1744f59fd9caSVladimir Oltean 1745f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1746f59fd9caSVladimir Oltean p_threshold); 1747f59fd9caSVladimir Oltean } 1748f59fd9caSVladimir Oltean 1749f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1750f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1751f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1752f59fd9caSVladimir Oltean { 1753f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1754f59fd9caSVladimir Oltean 1755f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1756f59fd9caSVladimir Oltean threshold, extack); 1757f59fd9caSVladimir Oltean } 1758f59fd9caSVladimir Oltean 1759f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1760f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1761f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1762f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1763f59fd9caSVladimir Oltean { 1764f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1765f59fd9caSVladimir Oltean 1766f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1767f59fd9caSVladimir Oltean pool_type, p_pool_index, 1768f59fd9caSVladimir Oltean p_threshold); 1769f59fd9caSVladimir Oltean } 1770f59fd9caSVladimir Oltean 1771f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1772f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1773f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1774f59fd9caSVladimir Oltean u16 pool_index, u32 threshold, 1775f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1776f59fd9caSVladimir Oltean { 1777f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1778f59fd9caSVladimir Oltean 1779f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1780f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1781f59fd9caSVladimir Oltean extack); 1782f59fd9caSVladimir Oltean } 1783f59fd9caSVladimir Oltean 1784f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1785f59fd9caSVladimir Oltean unsigned int sb_index) 1786f59fd9caSVladimir Oltean { 1787f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1788f59fd9caSVladimir Oltean 1789f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1790f59fd9caSVladimir Oltean } 1791f59fd9caSVladimir Oltean 1792f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1793f59fd9caSVladimir Oltean unsigned int sb_index) 1794f59fd9caSVladimir Oltean { 1795f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1796f59fd9caSVladimir Oltean 1797f59fd9caSVladimir Oltean return ocelot_sb_occ_max_clear(ocelot, sb_index); 1798f59fd9caSVladimir Oltean } 1799f59fd9caSVladimir Oltean 1800f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1801f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1802f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1803f59fd9caSVladimir Oltean { 1804f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1805f59fd9caSVladimir Oltean 1806f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1807f59fd9caSVladimir Oltean p_cur, p_max); 1808f59fd9caSVladimir Oltean } 1809f59fd9caSVladimir Oltean 1810f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1811f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1812f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1813f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1814f59fd9caSVladimir Oltean { 1815f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1816f59fd9caSVladimir Oltean 1817f59fd9caSVladimir Oltean return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1818f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1819f59fd9caSVladimir Oltean } 1820f59fd9caSVladimir Oltean 1821a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port, 1822a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1823a026c50bSHoratiu Vultur { 1824a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1825a026c50bSHoratiu Vultur 1826a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1827a026c50bSHoratiu Vultur } 1828a026c50bSHoratiu Vultur 1829a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port, 1830a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1831a026c50bSHoratiu Vultur { 1832a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1833a026c50bSHoratiu Vultur 1834a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1835a026c50bSHoratiu Vultur } 1836a026c50bSHoratiu Vultur 1837a026c50bSHoratiu Vultur static int 1838a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1839a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1840a026c50bSHoratiu Vultur { 1841a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1842a026c50bSHoratiu Vultur 1843a026c50bSHoratiu Vultur return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1844a026c50bSHoratiu Vultur } 1845a026c50bSHoratiu Vultur 1846a026c50bSHoratiu Vultur static int 1847a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1848a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1849a026c50bSHoratiu Vultur { 1850a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1851a026c50bSHoratiu Vultur 1852a026c50bSHoratiu Vultur return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1853a026c50bSHoratiu Vultur } 1854a026c50bSHoratiu Vultur 1855978777d0SVladimir Oltean static int felix_port_get_default_prio(struct dsa_switch *ds, int port) 1856978777d0SVladimir Oltean { 1857978777d0SVladimir Oltean struct ocelot *ocelot = ds->priv; 1858978777d0SVladimir Oltean 1859978777d0SVladimir Oltean return ocelot_port_get_default_prio(ocelot, port); 1860978777d0SVladimir Oltean } 1861978777d0SVladimir Oltean 1862978777d0SVladimir Oltean static int felix_port_set_default_prio(struct dsa_switch *ds, int port, 1863978777d0SVladimir Oltean u8 prio) 1864978777d0SVladimir Oltean { 1865978777d0SVladimir Oltean struct ocelot *ocelot = ds->priv; 1866978777d0SVladimir Oltean 1867978777d0SVladimir Oltean return ocelot_port_set_default_prio(ocelot, port, prio); 1868978777d0SVladimir Oltean } 1869978777d0SVladimir Oltean 1870978777d0SVladimir Oltean static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp) 1871978777d0SVladimir Oltean { 1872978777d0SVladimir Oltean struct ocelot *ocelot = ds->priv; 1873978777d0SVladimir Oltean 1874978777d0SVladimir Oltean return ocelot_port_get_dscp_prio(ocelot, port, dscp); 1875978777d0SVladimir Oltean } 1876978777d0SVladimir Oltean 1877978777d0SVladimir Oltean static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp, 1878978777d0SVladimir Oltean u8 prio) 1879978777d0SVladimir Oltean { 1880978777d0SVladimir Oltean struct ocelot *ocelot = ds->priv; 1881978777d0SVladimir Oltean 1882978777d0SVladimir Oltean return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio); 1883978777d0SVladimir Oltean } 1884978777d0SVladimir Oltean 1885978777d0SVladimir Oltean static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp, 1886978777d0SVladimir Oltean u8 prio) 1887978777d0SVladimir Oltean { 1888978777d0SVladimir Oltean struct ocelot *ocelot = ds->priv; 1889978777d0SVladimir Oltean 1890978777d0SVladimir Oltean return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio); 1891978777d0SVladimir Oltean } 1892978777d0SVladimir Oltean 1893375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 189456051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1895adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 189635d97680SVladimir Oltean .connect_tag_protocol = felix_connect_tag_protocol, 189756051948SVladimir Oltean .setup = felix_setup, 189856051948SVladimir Oltean .teardown = felix_teardown, 189956051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 1900776b71e5SVladimir Oltean .get_stats64 = felix_get_stats64, 1901*e32036e1SVladimir Oltean .get_pause_stats = felix_get_pause_stats, 1902*e32036e1SVladimir Oltean .get_rmon_stats = felix_get_rmon_stats, 1903*e32036e1SVladimir Oltean .get_eth_ctrl_stats = felix_get_eth_ctrl_stats, 1904*e32036e1SVladimir Oltean .get_eth_mac_stats = felix_get_eth_mac_stats, 1905*e32036e1SVladimir Oltean .get_eth_phy_stats = felix_get_eth_phy_stats, 190656051948SVladimir Oltean .get_strings = felix_get_strings, 190756051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 190856051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 190956051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 191079fda660SRussell King (Oracle) .phylink_get_caps = felix_phylink_get_caps, 1911bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1912864ba485SRussell King (Oracle) .phylink_mac_select_pcs = felix_phylink_mac_select_pcs, 1913bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1914bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 19155cad43a5SVladimir Oltean .port_fast_age = felix_port_fast_age, 191656051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 191756051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 191856051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1919961d8b69SVladimir Oltean .lag_fdb_add = felix_lag_fdb_add, 1920961d8b69SVladimir Oltean .lag_fdb_del = felix_lag_fdb_del, 1921209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1922209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 1923421741eaSVladimir Oltean .port_pre_bridge_flags = felix_pre_bridge_flags, 1924421741eaSVladimir Oltean .port_bridge_flags = felix_bridge_flags, 192556051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 192656051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 19278fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 19288fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 19298fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 193056051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 193156051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 193256051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 193356051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1934c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1935c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1936c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1937c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 19380b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 19390b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1940fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1941fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 19425e497497SVladimir Oltean .port_mirror_add = felix_port_mirror_add, 19435e497497SVladimir Oltean .port_mirror_del = felix_port_mirror_del, 194407d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 194507d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 194607d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1947de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1948f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1949f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1950f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1951f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1952f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1953f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1954f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1955f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1956f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1957f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1958a026c50bSHoratiu Vultur .port_mrp_add = felix_mrp_add, 1959a026c50bSHoratiu Vultur .port_mrp_del = felix_mrp_del, 1960a026c50bSHoratiu Vultur .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1961a026c50bSHoratiu Vultur .port_mrp_del_ring_role = felix_mrp_del_ring_role, 19625da11eb4SVladimir Oltean .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 19635da11eb4SVladimir Oltean .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 1964978777d0SVladimir Oltean .port_get_default_prio = felix_port_get_default_prio, 1965978777d0SVladimir Oltean .port_set_default_prio = felix_port_set_default_prio, 1966978777d0SVladimir Oltean .port_get_dscp_prio = felix_port_get_dscp_prio, 1967978777d0SVladimir Oltean .port_add_dscp_prio = felix_port_add_dscp_prio, 1968978777d0SVladimir Oltean .port_del_dscp_prio = felix_port_del_dscp_prio, 196972c3b0c7SVladimir Oltean .port_set_host_flood = felix_port_set_host_flood, 197056051948SVladimir Oltean }; 1971319e4dd1SVladimir Oltean 1972319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1973319e4dd1SVladimir Oltean { 1974319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1975319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1976319e4dd1SVladimir Oltean 1977319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1978319e4dd1SVladimir Oltean return NULL; 1979319e4dd1SVladimir Oltean 1980319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1981319e4dd1SVladimir Oltean } 1982319e4dd1SVladimir Oltean 1983319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1984319e4dd1SVladimir Oltean { 1985319e4dd1SVladimir Oltean struct dsa_port *dp; 1986319e4dd1SVladimir Oltean 1987319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1988319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1989319e4dd1SVladimir Oltean return -EINVAL; 1990319e4dd1SVladimir Oltean 1991319e4dd1SVladimir Oltean return dp->index; 1992319e4dd1SVladimir Oltean } 1993