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> 24588d0550SIoana Ciornei #include <linux/pcs-lynx.h> 25fc411eaaSVladimir Oltean #include <net/pkt_sched.h> 2656051948SVladimir Oltean #include <net/dsa.h> 2756051948SVladimir Oltean #include "felix.h" 2856051948SVladimir Oltean 29e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid, 30e21268efSVladimir Oltean bool pvid, bool untagged) 31e21268efSVladimir Oltean { 32e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 33e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 34e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 35e21268efSVladimir Oltean int key_length, upstream, err; 36e21268efSVladimir Oltean 37e21268efSVladimir Oltean /* We don't need to install the rxvlan into the other ports' filtering 38e21268efSVladimir Oltean * tables, because we're just pushing the rxvlan when sending towards 39e21268efSVladimir Oltean * the CPU 40e21268efSVladimir Oltean */ 41e21268efSVladimir Oltean if (!pvid) 42e21268efSVladimir Oltean return 0; 43e21268efSVladimir Oltean 44e21268efSVladimir Oltean key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length; 45e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 46e21268efSVladimir Oltean 47e21268efSVladimir Oltean outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), 48e21268efSVladimir Oltean GFP_KERNEL); 49e21268efSVladimir Oltean if (!outer_tagging_rule) 50e21268efSVladimir Oltean return -ENOMEM; 51e21268efSVladimir Oltean 52e21268efSVladimir Oltean outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 53e21268efSVladimir Oltean outer_tagging_rule->prio = 1; 54e21268efSVladimir Oltean outer_tagging_rule->id.cookie = port; 55e21268efSVladimir Oltean outer_tagging_rule->id.tc_offload = false; 56e21268efSVladimir Oltean outer_tagging_rule->block_id = VCAP_ES0; 57e21268efSVladimir Oltean outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 58e21268efSVladimir Oltean outer_tagging_rule->lookup = 0; 59e21268efSVladimir Oltean outer_tagging_rule->ingress_port.value = port; 60e21268efSVladimir Oltean outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0); 61e21268efSVladimir Oltean outer_tagging_rule->egress_port.value = upstream; 62e21268efSVladimir Oltean outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0); 63e21268efSVladimir Oltean outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG; 64e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD; 65e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_vid_sel = 1; 66e21268efSVladimir Oltean outer_tagging_rule->action.vid_a_val = vid; 67e21268efSVladimir Oltean 68e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL); 69e21268efSVladimir Oltean if (err) 70e21268efSVladimir Oltean kfree(outer_tagging_rule); 71e21268efSVladimir Oltean 72e21268efSVladimir Oltean return err; 73e21268efSVladimir Oltean } 74e21268efSVladimir Oltean 75e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid, 76e21268efSVladimir Oltean bool pvid, bool untagged) 77e21268efSVladimir Oltean { 78e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 79e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 80e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 81e21268efSVladimir Oltean int upstream, err; 82e21268efSVladimir Oltean 83e21268efSVladimir Oltean /* tag_8021q.c assumes we are implementing this via port VLAN 84e21268efSVladimir Oltean * membership, which we aren't. So we don't need to add any VCAP filter 85e21268efSVladimir Oltean * for the CPU port. 86e21268efSVladimir Oltean */ 87e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 88e21268efSVladimir Oltean return 0; 89e21268efSVladimir Oltean 90e21268efSVladimir Oltean untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 91e21268efSVladimir Oltean if (!untagging_rule) 92e21268efSVladimir Oltean return -ENOMEM; 93e21268efSVladimir Oltean 94e21268efSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 95e21268efSVladimir Oltean if (!redirect_rule) { 96e21268efSVladimir Oltean kfree(untagging_rule); 97e21268efSVladimir Oltean return -ENOMEM; 98e21268efSVladimir Oltean } 99e21268efSVladimir Oltean 100e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 101e21268efSVladimir Oltean 102e21268efSVladimir Oltean untagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 103e21268efSVladimir Oltean untagging_rule->ingress_port_mask = BIT(upstream); 104e21268efSVladimir Oltean untagging_rule->vlan.vid.value = vid; 105e21268efSVladimir Oltean untagging_rule->vlan.vid.mask = VLAN_VID_MASK; 106e21268efSVladimir Oltean untagging_rule->prio = 1; 107e21268efSVladimir Oltean untagging_rule->id.cookie = port; 108e21268efSVladimir Oltean untagging_rule->id.tc_offload = false; 109e21268efSVladimir Oltean untagging_rule->block_id = VCAP_IS1; 110e21268efSVladimir Oltean untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 111e21268efSVladimir Oltean untagging_rule->lookup = 0; 112e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt_ena = true; 113e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt = 1; 114e21268efSVladimir Oltean untagging_rule->action.pag_override_mask = 0xff; 115e21268efSVladimir Oltean untagging_rule->action.pag_val = port; 116e21268efSVladimir Oltean 117e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL); 118e21268efSVladimir Oltean if (err) { 119e21268efSVladimir Oltean kfree(untagging_rule); 120e21268efSVladimir Oltean kfree(redirect_rule); 121e21268efSVladimir Oltean return err; 122e21268efSVladimir Oltean } 123e21268efSVladimir Oltean 124e21268efSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 125e21268efSVladimir Oltean redirect_rule->ingress_port_mask = BIT(upstream); 126e21268efSVladimir Oltean redirect_rule->pag = port; 127e21268efSVladimir Oltean redirect_rule->prio = 1; 128e21268efSVladimir Oltean redirect_rule->id.cookie = port; 129e21268efSVladimir Oltean redirect_rule->id.tc_offload = false; 130e21268efSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 131e21268efSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 132e21268efSVladimir Oltean redirect_rule->lookup = 0; 133e21268efSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 134e21268efSVladimir Oltean redirect_rule->action.port_mask = BIT(port); 135e21268efSVladimir Oltean 136e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 137e21268efSVladimir Oltean if (err) { 138e21268efSVladimir Oltean ocelot_vcap_filter_del(ocelot, untagging_rule); 139e21268efSVladimir Oltean kfree(redirect_rule); 140e21268efSVladimir Oltean return err; 141e21268efSVladimir Oltean } 142e21268efSVladimir Oltean 143e21268efSVladimir Oltean return 0; 144e21268efSVladimir Oltean } 145e21268efSVladimir Oltean 146e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 147e21268efSVladimir Oltean u16 flags) 148e21268efSVladimir Oltean { 149e21268efSVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 150e21268efSVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 151e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 152e21268efSVladimir Oltean 153e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 154e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot), 155e21268efSVladimir Oltean port, vid, pvid, untagged); 156e21268efSVladimir Oltean 157e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 158e21268efSVladimir Oltean return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot), 159e21268efSVladimir Oltean port, vid, pvid, untagged); 160e21268efSVladimir Oltean 161e21268efSVladimir Oltean return 0; 162e21268efSVladimir Oltean } 163e21268efSVladimir Oltean 164e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid) 165e21268efSVladimir Oltean { 166e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 167e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_es0; 168e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 169e21268efSVladimir Oltean 170e21268efSVladimir Oltean block_vcap_es0 = &ocelot->block[VCAP_ES0]; 171e21268efSVladimir Oltean 172e21268efSVladimir Oltean outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 173e21268efSVladimir Oltean port, false); 174e21268efSVladimir Oltean /* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid 175e21268efSVladimir Oltean * installing outer tagging ES0 rules where they weren't needed. 176e21268efSVladimir Oltean * But in rxvlan_del, the API doesn't give us the "flags" anymore, 177e21268efSVladimir Oltean * so that forces us to be slightly sloppy here, and just assume that 178e21268efSVladimir Oltean * if we didn't find an outer_tagging_rule it means that there was 179e21268efSVladimir Oltean * none in the first place, i.e. rxvlan_del is called on a non-pvid 180e21268efSVladimir Oltean * port. This is most probably true though. 181e21268efSVladimir Oltean */ 182e21268efSVladimir Oltean if (!outer_tagging_rule) 183e21268efSVladimir Oltean return 0; 184e21268efSVladimir Oltean 185e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 186e21268efSVladimir Oltean } 187e21268efSVladimir Oltean 188e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid) 189e21268efSVladimir Oltean { 190e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 191e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 192e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 193e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 194e21268efSVladimir Oltean int err; 195e21268efSVladimir Oltean 196e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 197e21268efSVladimir Oltean return 0; 198e21268efSVladimir Oltean 199e21268efSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 200e21268efSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 201e21268efSVladimir Oltean 202e21268efSVladimir Oltean untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 203e21268efSVladimir Oltean port, false); 204e21268efSVladimir Oltean if (!untagging_rule) 205e21268efSVladimir Oltean return 0; 206e21268efSVladimir Oltean 207e21268efSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, untagging_rule); 208e21268efSVladimir Oltean if (err) 209e21268efSVladimir Oltean return err; 210e21268efSVladimir Oltean 211e21268efSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 212e21268efSVladimir Oltean port, false); 213e21268efSVladimir Oltean if (!redirect_rule) 214e21268efSVladimir Oltean return 0; 215e21268efSVladimir Oltean 216e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 217e21268efSVladimir Oltean } 218e21268efSVladimir Oltean 219e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 220e21268efSVladimir Oltean { 221e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 222e21268efSVladimir Oltean 223e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 224e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot), 225e21268efSVladimir Oltean port, vid); 226e21268efSVladimir Oltean 227e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 228e21268efSVladimir Oltean return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot), 229e21268efSVladimir Oltean port, vid); 230e21268efSVladimir Oltean 231e21268efSVladimir Oltean return 0; 232e21268efSVladimir Oltean } 233e21268efSVladimir Oltean 234e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC 235e21268efSVladimir Oltean * connected internally to the enetc or fman DSA master can be configured to 236e21268efSVladimir Oltean * use the software-defined tag_8021q frame format. As far as the hardware is 237e21268efSVladimir Oltean * concerned, it thinks it is a "dumb switch" - the queues of the CPU port 238e21268efSVladimir Oltean * module are now disconnected from it, but can still be accessed through 239e21268efSVladimir Oltean * register-based MMIO. 240e21268efSVladimir Oltean */ 241e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port) 242e21268efSVladimir Oltean { 2438abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2448abe1970SVladimir Oltean 245e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = true; 246e21268efSVladimir Oltean ocelot->npi = -1; 247e21268efSVladimir Oltean 248e21268efSVladimir Oltean /* Overwrite PGID_CPU with the non-tagging port */ 249e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU); 250e21268efSVladimir Oltean 2518abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2528abe1970SVladimir Oltean 2538abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 254e21268efSVladimir Oltean } 255e21268efSVladimir Oltean 256e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port) 257e21268efSVladimir Oltean { 2588abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2598abe1970SVladimir Oltean 260e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = false; 261e21268efSVladimir Oltean 262e21268efSVladimir Oltean /* Restore PGID_CPU */ 263e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID, 264e21268efSVladimir Oltean PGID_CPU); 265e21268efSVladimir Oltean 2668abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2678abe1970SVladimir Oltean 2688abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 269e21268efSVladimir Oltean } 270e21268efSVladimir Oltean 271c8c0ba4fSVladimir Oltean /* Set up a VCAP IS2 rule for delivering PTP frames to the CPU port module. 272c8c0ba4fSVladimir Oltean * If the quirk_no_xtr_irq is in place, then also copy those PTP frames to the 273c8c0ba4fSVladimir Oltean * tag_8021q CPU port. 274c8c0ba4fSVladimir Oltean */ 275c8c0ba4fSVladimir Oltean static int felix_setup_mmio_filtering(struct felix *felix) 276c8c0ba4fSVladimir Oltean { 2778d5f7954SVladimir Oltean unsigned long user_ports = dsa_user_ports(felix->ds); 278c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *redirect_rule; 279c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *tagging_rule; 280c8c0ba4fSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 281c8c0ba4fSVladimir Oltean struct dsa_switch *ds = felix->ds; 2828d5f7954SVladimir Oltean int cpu = -1, port, ret; 283c8c0ba4fSVladimir Oltean 284c8c0ba4fSVladimir Oltean tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 285c8c0ba4fSVladimir Oltean if (!tagging_rule) 286c8c0ba4fSVladimir Oltean return -ENOMEM; 287c8c0ba4fSVladimir Oltean 288c8c0ba4fSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 289c8c0ba4fSVladimir Oltean if (!redirect_rule) { 290c8c0ba4fSVladimir Oltean kfree(tagging_rule); 291c8c0ba4fSVladimir Oltean return -ENOMEM; 292c8c0ba4fSVladimir Oltean } 293c8c0ba4fSVladimir Oltean 294c8c0ba4fSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2958d5f7954SVladimir Oltean if (dsa_is_cpu_port(ds, port)) { 2968d5f7954SVladimir Oltean cpu = port; 2978d5f7954SVladimir Oltean break; 298c8c0ba4fSVladimir Oltean } 2998d5f7954SVladimir Oltean } 3008d5f7954SVladimir Oltean 301e8b1d769SJosé Expósito if (cpu < 0) { 302e8b1d769SJosé Expósito kfree(tagging_rule); 303e8b1d769SJosé Expósito kfree(redirect_rule); 3048d5f7954SVladimir Oltean return -EINVAL; 305e8b1d769SJosé Expósito } 306c8c0ba4fSVladimir Oltean 307c8c0ba4fSVladimir Oltean tagging_rule->key_type = OCELOT_VCAP_KEY_ETYPE; 308c8c0ba4fSVladimir Oltean *(__be16 *)tagging_rule->key.etype.etype.value = htons(ETH_P_1588); 309c8c0ba4fSVladimir Oltean *(__be16 *)tagging_rule->key.etype.etype.mask = htons(0xffff); 310c8c0ba4fSVladimir Oltean tagging_rule->ingress_port_mask = user_ports; 311c8c0ba4fSVladimir Oltean tagging_rule->prio = 1; 312c8c0ba4fSVladimir Oltean tagging_rule->id.cookie = ocelot->num_phys_ports; 313c8c0ba4fSVladimir Oltean tagging_rule->id.tc_offload = false; 314c8c0ba4fSVladimir Oltean tagging_rule->block_id = VCAP_IS1; 315c8c0ba4fSVladimir Oltean tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 316c8c0ba4fSVladimir Oltean tagging_rule->lookup = 0; 317c8c0ba4fSVladimir Oltean tagging_rule->action.pag_override_mask = 0xff; 318c8c0ba4fSVladimir Oltean tagging_rule->action.pag_val = ocelot->num_phys_ports; 319c8c0ba4fSVladimir Oltean 320c8c0ba4fSVladimir Oltean ret = ocelot_vcap_filter_add(ocelot, tagging_rule, NULL); 321c8c0ba4fSVladimir Oltean if (ret) { 322c8c0ba4fSVladimir Oltean kfree(tagging_rule); 323c8c0ba4fSVladimir Oltean kfree(redirect_rule); 324c8c0ba4fSVladimir Oltean return ret; 325c8c0ba4fSVladimir Oltean } 326c8c0ba4fSVladimir Oltean 327c8c0ba4fSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 328c8c0ba4fSVladimir Oltean redirect_rule->ingress_port_mask = user_ports; 329c8c0ba4fSVladimir Oltean redirect_rule->pag = ocelot->num_phys_ports; 330c8c0ba4fSVladimir Oltean redirect_rule->prio = 1; 331c8c0ba4fSVladimir Oltean redirect_rule->id.cookie = ocelot->num_phys_ports; 332c8c0ba4fSVladimir Oltean redirect_rule->id.tc_offload = false; 333c8c0ba4fSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 334c8c0ba4fSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 335c8c0ba4fSVladimir Oltean redirect_rule->lookup = 0; 336c8c0ba4fSVladimir Oltean redirect_rule->action.cpu_copy_ena = true; 337c8c0ba4fSVladimir Oltean if (felix->info->quirk_no_xtr_irq) { 338c8c0ba4fSVladimir Oltean /* Redirect to the tag_8021q CPU but also copy PTP packets to 339c8c0ba4fSVladimir Oltean * the CPU port module 340c8c0ba4fSVladimir Oltean */ 341c8c0ba4fSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 3428d5f7954SVladimir Oltean redirect_rule->action.port_mask = BIT(cpu); 343c8c0ba4fSVladimir Oltean } else { 344c8c0ba4fSVladimir Oltean /* Trap PTP packets only to the CPU port module (which is 345c8c0ba4fSVladimir Oltean * redirected to the NPI port) 346c8c0ba4fSVladimir Oltean */ 347c8c0ba4fSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 348c8c0ba4fSVladimir Oltean redirect_rule->action.port_mask = 0; 349c8c0ba4fSVladimir Oltean } 350c8c0ba4fSVladimir Oltean 351c8c0ba4fSVladimir Oltean ret = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 352c8c0ba4fSVladimir Oltean if (ret) { 353c8c0ba4fSVladimir Oltean ocelot_vcap_filter_del(ocelot, tagging_rule); 354c8c0ba4fSVladimir Oltean kfree(redirect_rule); 355c8c0ba4fSVladimir Oltean return ret; 356c8c0ba4fSVladimir Oltean } 357c8c0ba4fSVladimir Oltean 3580a6f17c6SVladimir Oltean /* The ownership of the CPU port module's queues might have just been 3590a6f17c6SVladimir Oltean * transferred to the tag_8021q tagger from the NPI-based tagger. 3600a6f17c6SVladimir Oltean * So there might still be all sorts of crap in the queues. On the 3610a6f17c6SVladimir Oltean * other hand, the MMIO-based matching of PTP frames is very brittle, 3620a6f17c6SVladimir Oltean * so we need to be careful that there are no extra frames to be 3630a6f17c6SVladimir Oltean * dequeued over MMIO, since we would never know to discard them. 3640a6f17c6SVladimir Oltean */ 3650a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 3660a6f17c6SVladimir Oltean 367c8c0ba4fSVladimir Oltean return 0; 368c8c0ba4fSVladimir Oltean } 369c8c0ba4fSVladimir Oltean 370c8c0ba4fSVladimir Oltean static int felix_teardown_mmio_filtering(struct felix *felix) 371c8c0ba4fSVladimir Oltean { 372c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *tagging_rule, *redirect_rule; 373c8c0ba4fSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 374c8c0ba4fSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 375c8c0ba4fSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 376c8c0ba4fSVladimir Oltean int err; 377c8c0ba4fSVladimir Oltean 378c8c0ba4fSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 379c8c0ba4fSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 380c8c0ba4fSVladimir Oltean 381c8c0ba4fSVladimir Oltean tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 382c8c0ba4fSVladimir Oltean ocelot->num_phys_ports, 383c8c0ba4fSVladimir Oltean false); 384c8c0ba4fSVladimir Oltean if (!tagging_rule) 385c8c0ba4fSVladimir Oltean return -ENOENT; 386c8c0ba4fSVladimir Oltean 387c8c0ba4fSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, tagging_rule); 388c8c0ba4fSVladimir Oltean if (err) 389c8c0ba4fSVladimir Oltean return err; 390c8c0ba4fSVladimir Oltean 391c8c0ba4fSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 392c8c0ba4fSVladimir Oltean ocelot->num_phys_ports, 393c8c0ba4fSVladimir Oltean false); 394c8c0ba4fSVladimir Oltean if (!redirect_rule) 395c8c0ba4fSVladimir Oltean return -ENOENT; 396c8c0ba4fSVladimir Oltean 397c8c0ba4fSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 398c8c0ba4fSVladimir Oltean } 399c8c0ba4fSVladimir Oltean 400e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu) 401e21268efSVladimir Oltean { 402e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 403e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 404e21268efSVladimir Oltean unsigned long cpu_flood; 405e21268efSVladimir Oltean int port, err; 406e21268efSVladimir Oltean 407e21268efSVladimir Oltean felix_8021q_cpu_port_init(ocelot, cpu); 408e21268efSVladimir Oltean 409e21268efSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 410e21268efSVladimir Oltean if (dsa_is_unused_port(ds, port)) 411e21268efSVladimir Oltean continue; 412e21268efSVladimir Oltean 413e21268efSVladimir Oltean /* This overwrites ocelot_init(): 414e21268efSVladimir Oltean * Do not forward BPDU frames to the CPU port module, 415e21268efSVladimir Oltean * for 2 reasons: 416e21268efSVladimir Oltean * - When these packets are injected from the tag_8021q 417e21268efSVladimir Oltean * CPU port, we want them to go out, not loop back 418e21268efSVladimir Oltean * into the system. 419e21268efSVladimir Oltean * - STP traffic ingressing on a user port should go to 420e21268efSVladimir Oltean * the tag_8021q CPU port, not to the hardware CPU 421e21268efSVladimir Oltean * port module. 422e21268efSVladimir Oltean */ 423e21268efSVladimir Oltean ocelot_write_gix(ocelot, 424e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 425e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, port); 426e21268efSVladimir Oltean } 427e21268efSVladimir Oltean 428c8c0ba4fSVladimir Oltean /* In tag_8021q mode, the CPU port module is unused, except for PTP 429c8c0ba4fSVladimir Oltean * frames. So we want to disable flooding of any kind to the CPU port 430c8c0ba4fSVladimir Oltean * module, since packets going there will end in a black hole. 431e21268efSVladimir Oltean */ 432e21268efSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 433e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC); 434e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC); 435b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC); 436e21268efSVladimir Oltean 4375da11eb4SVladimir Oltean err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD)); 438e21268efSVladimir Oltean if (err) 439d7b1fd52SVladimir Oltean return err; 440d7b1fd52SVladimir Oltean 441328621f6SVladimir Oltean err = felix_setup_mmio_filtering(felix); 442d7b1fd52SVladimir Oltean if (err) 443d7b1fd52SVladimir Oltean goto out_tag_8021q_unregister; 444e21268efSVladimir Oltean 445e21268efSVladimir Oltean return 0; 446e21268efSVladimir Oltean 447d7b1fd52SVladimir Oltean out_tag_8021q_unregister: 448d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 449e21268efSVladimir Oltean return err; 450e21268efSVladimir Oltean } 451e21268efSVladimir Oltean 452e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu) 453e21268efSVladimir Oltean { 454e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 455e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 456e21268efSVladimir Oltean int err, port; 457e21268efSVladimir Oltean 458c8c0ba4fSVladimir Oltean err = felix_teardown_mmio_filtering(felix); 459c8c0ba4fSVladimir Oltean if (err) 460c8c0ba4fSVladimir Oltean dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d", 461c8c0ba4fSVladimir Oltean err); 462c8c0ba4fSVladimir Oltean 463d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 464e21268efSVladimir Oltean 465e21268efSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 466e21268efSVladimir Oltean if (dsa_is_unused_port(ds, port)) 467e21268efSVladimir Oltean continue; 468e21268efSVladimir Oltean 469e21268efSVladimir Oltean /* Restore the logic from ocelot_init: 470e21268efSVladimir Oltean * do not forward BPDU frames to the front ports. 471e21268efSVladimir Oltean */ 472e21268efSVladimir Oltean ocelot_write_gix(ocelot, 473e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 474e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, 475e21268efSVladimir Oltean port); 476e21268efSVladimir Oltean } 477e21268efSVladimir Oltean 478e21268efSVladimir Oltean felix_8021q_cpu_port_deinit(ocelot, cpu); 479e21268efSVladimir Oltean } 480e21268efSVladimir Oltean 481adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This 482adb3dccfSVladimir Oltean * is the mode through which frames can be injected from and extracted to an 483adb3dccfSVladimir Oltean * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 484adb3dccfSVladimir Oltean * running Linux, and this forms a DSA setup together with the enetc or fman 485adb3dccfSVladimir Oltean * DSA master. 486adb3dccfSVladimir Oltean */ 487adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port) 488adb3dccfSVladimir Oltean { 489adb3dccfSVladimir Oltean ocelot->npi = port; 490adb3dccfSVladimir Oltean 491adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 492adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 493adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 494adb3dccfSVladimir Oltean 495adb3dccfSVladimir Oltean /* NPI port Injection/Extraction configuration */ 496adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 497adb3dccfSVladimir Oltean ocelot->npi_xtr_prefix); 498adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 499adb3dccfSVladimir Oltean ocelot->npi_inj_prefix); 500adb3dccfSVladimir Oltean 501adb3dccfSVladimir Oltean /* Disable transmission of pause frames */ 502adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 503adb3dccfSVladimir Oltean } 504adb3dccfSVladimir Oltean 505adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 506adb3dccfSVladimir Oltean { 507adb3dccfSVladimir Oltean /* Restore hardware defaults */ 508adb3dccfSVladimir Oltean int unused_port = ocelot->num_phys_ports + 2; 509adb3dccfSVladimir Oltean 510adb3dccfSVladimir Oltean ocelot->npi = -1; 511adb3dccfSVladimir Oltean 512adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 513adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 514adb3dccfSVladimir Oltean 515adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 516adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 517adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 518adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 519adb3dccfSVladimir Oltean 520adb3dccfSVladimir Oltean /* Enable transmission of pause frames */ 521adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 522adb3dccfSVladimir Oltean } 523adb3dccfSVladimir Oltean 524adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu) 525adb3dccfSVladimir Oltean { 526adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 527adb3dccfSVladimir Oltean unsigned long cpu_flood; 528adb3dccfSVladimir Oltean 529adb3dccfSVladimir Oltean felix_npi_port_init(ocelot, cpu); 530adb3dccfSVladimir Oltean 531adb3dccfSVladimir Oltean /* Include the CPU port module (and indirectly, the NPI port) 532adb3dccfSVladimir Oltean * in the forwarding mask for unknown unicast - the hardware 533adb3dccfSVladimir Oltean * default value for ANA_FLOODING_FLD_UNICAST excludes 534adb3dccfSVladimir Oltean * BIT(ocelot->num_phys_ports), and so does ocelot_init, 535adb3dccfSVladimir Oltean * since Ocelot relies on whitelisting MAC addresses towards 536adb3dccfSVladimir Oltean * PGID_CPU. 537adb3dccfSVladimir Oltean * We do this because DSA does not yet perform RX filtering, 538adb3dccfSVladimir Oltean * and the NPI port does not perform source address learning, 539adb3dccfSVladimir Oltean * so traffic sent to Linux is effectively unknown from the 540adb3dccfSVladimir Oltean * switch's perspective. 541adb3dccfSVladimir Oltean */ 542adb3dccfSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 543adb3dccfSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC); 5446edb9e8dSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC); 545b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC); 546adb3dccfSVladimir Oltean 547adb3dccfSVladimir Oltean return 0; 548adb3dccfSVladimir Oltean } 549adb3dccfSVladimir Oltean 550adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu) 551adb3dccfSVladimir Oltean { 552adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 553adb3dccfSVladimir Oltean 554adb3dccfSVladimir Oltean felix_npi_port_deinit(ocelot, cpu); 555adb3dccfSVladimir Oltean } 556adb3dccfSVladimir Oltean 557adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu, 558adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 559adb3dccfSVladimir Oltean { 560adb3dccfSVladimir Oltean int err; 561adb3dccfSVladimir Oltean 562adb3dccfSVladimir Oltean switch (proto) { 5637c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 564adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 565adb3dccfSVladimir Oltean err = felix_setup_tag_npi(ds, cpu); 566adb3dccfSVladimir Oltean break; 567e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 568e21268efSVladimir Oltean err = felix_setup_tag_8021q(ds, cpu); 569e21268efSVladimir Oltean break; 570adb3dccfSVladimir Oltean default: 571adb3dccfSVladimir Oltean err = -EPROTONOSUPPORT; 572adb3dccfSVladimir Oltean } 573adb3dccfSVladimir Oltean 574adb3dccfSVladimir Oltean return err; 575adb3dccfSVladimir Oltean } 576adb3dccfSVladimir Oltean 577adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu, 578adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 579adb3dccfSVladimir Oltean { 580adb3dccfSVladimir Oltean switch (proto) { 5817c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 582adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 583adb3dccfSVladimir Oltean felix_teardown_tag_npi(ds, cpu); 584adb3dccfSVladimir Oltean break; 585e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 586e21268efSVladimir Oltean felix_teardown_tag_8021q(ds, cpu); 587e21268efSVladimir Oltean break; 588adb3dccfSVladimir Oltean default: 589adb3dccfSVladimir Oltean break; 590adb3dccfSVladimir Oltean } 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 */ 597adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu, 598adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 599adb3dccfSVladimir Oltean { 600adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 601adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 602adb3dccfSVladimir Oltean enum dsa_tag_protocol old_proto = felix->tag_proto; 603adb3dccfSVladimir Oltean int err; 604adb3dccfSVladimir Oltean 6057c4bb540SVladimir Oltean if (proto != DSA_TAG_PROTO_SEVILLE && 6067c4bb540SVladimir Oltean proto != DSA_TAG_PROTO_OCELOT && 607e21268efSVladimir Oltean proto != DSA_TAG_PROTO_OCELOT_8021Q) 608adb3dccfSVladimir Oltean return -EPROTONOSUPPORT; 609adb3dccfSVladimir Oltean 610adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, cpu, old_proto); 611adb3dccfSVladimir Oltean 612adb3dccfSVladimir Oltean err = felix_set_tag_protocol(ds, cpu, proto); 613adb3dccfSVladimir Oltean if (err) { 614adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, cpu, old_proto); 615adb3dccfSVladimir Oltean return err; 616adb3dccfSVladimir Oltean } 617adb3dccfSVladimir Oltean 618adb3dccfSVladimir Oltean felix->tag_proto = proto; 619adb3dccfSVladimir Oltean 620adb3dccfSVladimir Oltean return 0; 621adb3dccfSVladimir Oltean } 622adb3dccfSVladimir Oltean 62356051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 6244d776482SFlorian Fainelli int port, 6254d776482SFlorian Fainelli enum dsa_tag_protocol mp) 62656051948SVladimir Oltean { 627adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 628adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 629adb3dccfSVladimir Oltean 630adb3dccfSVladimir Oltean return felix->tag_proto; 63156051948SVladimir Oltean } 63256051948SVladimir Oltean 63356051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds, 63456051948SVladimir Oltean unsigned int ageing_time) 63556051948SVladimir Oltean { 63656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 63756051948SVladimir Oltean 63856051948SVladimir Oltean ocelot_set_ageing_time(ocelot, ageing_time); 63956051948SVladimir Oltean 64056051948SVladimir Oltean return 0; 64156051948SVladimir Oltean } 64256051948SVladimir Oltean 64356051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port, 64456051948SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 64556051948SVladimir Oltean { 64656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 64756051948SVladimir Oltean 64856051948SVladimir Oltean return ocelot_fdb_dump(ocelot, port, cb, data); 64956051948SVladimir Oltean } 65056051948SVladimir Oltean 65156051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port, 65256051948SVladimir Oltean const unsigned char *addr, u16 vid) 65356051948SVladimir Oltean { 65456051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 65556051948SVladimir Oltean 65687b0f983SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid); 65756051948SVladimir Oltean } 65856051948SVladimir Oltean 65956051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port, 66056051948SVladimir Oltean const unsigned char *addr, u16 vid) 66156051948SVladimir Oltean { 66256051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 66356051948SVladimir Oltean 66456051948SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid); 66556051948SVladimir Oltean } 66656051948SVladimir Oltean 667a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 668209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 669209edf95SVladimir Oltean { 670209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 671209edf95SVladimir Oltean 672a52b2da7SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb); 673209edf95SVladimir Oltean } 674209edf95SVladimir Oltean 675209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port, 676209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 677209edf95SVladimir Oltean { 678209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 679209edf95SVladimir Oltean 680209edf95SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb); 681209edf95SVladimir Oltean } 682209edf95SVladimir Oltean 68356051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 68456051948SVladimir Oltean u8 state) 68556051948SVladimir Oltean { 68656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 68756051948SVladimir Oltean 68856051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 68956051948SVladimir Oltean } 69056051948SVladimir Oltean 691421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 692421741eaSVladimir Oltean struct switchdev_brport_flags val, 693421741eaSVladimir Oltean struct netlink_ext_ack *extack) 694421741eaSVladimir Oltean { 695421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 696421741eaSVladimir Oltean 697421741eaSVladimir Oltean return ocelot_port_pre_bridge_flags(ocelot, port, val); 698421741eaSVladimir Oltean } 699421741eaSVladimir Oltean 700421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port, 701421741eaSVladimir Oltean struct switchdev_brport_flags val, 702421741eaSVladimir Oltean struct netlink_ext_ack *extack) 703421741eaSVladimir Oltean { 704421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 705421741eaSVladimir Oltean 706421741eaSVladimir Oltean ocelot_port_bridge_flags(ocelot, port, val); 707421741eaSVladimir Oltean 708421741eaSVladimir Oltean return 0; 709421741eaSVladimir Oltean } 710421741eaSVladimir Oltean 71156051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 712b079922bSVladimir Oltean struct dsa_bridge bridge, bool *tx_fwd_offload) 71356051948SVladimir Oltean { 71456051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 71556051948SVladimir Oltean 716d3eed0e5SVladimir Oltean ocelot_port_bridge_join(ocelot, port, bridge.dev); 717e4bd44e8SVladimir Oltean 718e4bd44e8SVladimir Oltean return 0; 71956051948SVladimir Oltean } 72056051948SVladimir Oltean 72156051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 722d3eed0e5SVladimir Oltean struct dsa_bridge bridge) 72356051948SVladimir Oltean { 72456051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 72556051948SVladimir Oltean 726d3eed0e5SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, bridge.dev); 72756051948SVladimir Oltean } 72856051948SVladimir Oltean 7298fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 7308fe6832eSVladimir Oltean struct net_device *bond, 7318fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 7328fe6832eSVladimir Oltean { 7338fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7348fe6832eSVladimir Oltean 7358fe6832eSVladimir Oltean return ocelot_port_lag_join(ocelot, port, bond, info); 7368fe6832eSVladimir Oltean } 7378fe6832eSVladimir Oltean 7388fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 7398fe6832eSVladimir Oltean struct net_device *bond) 7408fe6832eSVladimir Oltean { 7418fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7428fe6832eSVladimir Oltean 7438fe6832eSVladimir Oltean ocelot_port_lag_leave(ocelot, port, bond); 7448fe6832eSVladimir Oltean 7458fe6832eSVladimir Oltean return 0; 7468fe6832eSVladimir Oltean } 7478fe6832eSVladimir Oltean 7488fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 7498fe6832eSVladimir Oltean { 7508fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 7518fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7528fe6832eSVladimir Oltean 7538fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 7548fe6832eSVladimir Oltean 7558fe6832eSVladimir Oltean return 0; 7568fe6832eSVladimir Oltean } 7578fe6832eSVladimir Oltean 75856051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 75901af940eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 76001af940eSVladimir Oltean struct netlink_ext_ack *extack) 76156051948SVladimir Oltean { 7622f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 763b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 7642f0402feSVladimir Oltean 7659a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 7669a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 7679a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 7689a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 7699a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 7709a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 7719a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 7729a720680SVladimir Oltean */ 7739a720680SVladimir Oltean if (port == ocelot->npi) 7749a720680SVladimir Oltean return 0; 7759a720680SVladimir Oltean 776b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 7772f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 77801af940eSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED, 77901af940eSVladimir Oltean extack); 78056051948SVladimir Oltean } 78156051948SVladimir Oltean 78289153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 78389153ed6SVladimir Oltean struct netlink_ext_ack *extack) 78456051948SVladimir Oltean { 78556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 78656051948SVladimir Oltean 7873b95d1b2SVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 78856051948SVladimir Oltean } 78956051948SVladimir Oltean 7901958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 79131046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 79231046a5fSVladimir Oltean struct netlink_ext_ack *extack) 79356051948SVladimir Oltean { 79456051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 795183be6f9SVladimir Oltean u16 flags = vlan->flags; 7961958d581SVladimir Oltean int err; 79756051948SVladimir Oltean 79801af940eSVladimir Oltean err = felix_vlan_prepare(ds, port, vlan, extack); 7991958d581SVladimir Oltean if (err) 8001958d581SVladimir Oltean return err; 8011958d581SVladimir Oltean 8021958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 803183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 804183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 80556051948SVladimir Oltean } 80656051948SVladimir Oltean 80756051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 80856051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 80956051948SVladimir Oltean { 81056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 81156051948SVladimir Oltean 812b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 81356051948SVladimir Oltean } 81456051948SVladimir Oltean 815bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port, 816bdeced75SVladimir Oltean unsigned long *supported, 817bdeced75SVladimir Oltean struct phylink_link_state *state) 818bdeced75SVladimir Oltean { 819bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 820375e1314SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 821bdeced75SVladimir Oltean 822375e1314SVladimir Oltean if (felix->info->phylink_validate) 823375e1314SVladimir Oltean felix->info->phylink_validate(ocelot, port, supported, state); 824bdeced75SVladimir Oltean } 825bdeced75SVladimir Oltean 826bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port, 827bdeced75SVladimir Oltean unsigned int link_an_mode, 828bdeced75SVladimir Oltean const struct phylink_link_state *state) 829bdeced75SVladimir Oltean { 830bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 831bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 832588d0550SIoana Ciornei struct dsa_port *dp = dsa_to_port(ds, port); 833bdeced75SVladimir Oltean 83449af6a76SColin Foster if (felix->pcs && felix->pcs[port]) 835588d0550SIoana Ciornei phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs); 836bdeced75SVladimir Oltean } 837bdeced75SVladimir Oltean 838bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 839bdeced75SVladimir Oltean unsigned int link_an_mode, 840bdeced75SVladimir Oltean phy_interface_t interface) 841bdeced75SVladimir Oltean { 842bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 843bdeced75SVladimir Oltean 844e6e12df6SVladimir Oltean ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 845e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 846bdeced75SVladimir Oltean } 847bdeced75SVladimir Oltean 848bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 849bdeced75SVladimir Oltean unsigned int link_an_mode, 850bdeced75SVladimir Oltean phy_interface_t interface, 8515b502a7bSRussell King struct phy_device *phydev, 8525b502a7bSRussell King int speed, int duplex, 8535b502a7bSRussell King bool tx_pause, bool rx_pause) 854bdeced75SVladimir Oltean { 855bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 8567e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 857bdeced75SVladimir Oltean 858e6e12df6SVladimir Oltean ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 859e6e12df6SVladimir Oltean interface, speed, duplex, tx_pause, rx_pause, 860e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 8617e14a2dcSVladimir Oltean 8627e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 8637e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 864bdeced75SVladimir Oltean } 865bdeced75SVladimir Oltean 866bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 867bd2b3161SXiaoliang Yang { 868bd2b3161SXiaoliang Yang int i; 869bd2b3161SXiaoliang Yang 870bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 871bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 872bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 873bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 874bd2b3161SXiaoliang Yang port); 875bd2b3161SXiaoliang Yang 87670d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 877bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 878bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 879bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 880bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 881bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 882bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 883bd2b3161SXiaoliang Yang port, i); 884bd2b3161SXiaoliang Yang } 885bd2b3161SXiaoliang Yang } 886bd2b3161SXiaoliang Yang 88756051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 88856051948SVladimir Oltean u32 stringset, u8 *data) 88956051948SVladimir Oltean { 89056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 89156051948SVladimir Oltean 89256051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 89356051948SVladimir Oltean } 89456051948SVladimir Oltean 89556051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 89656051948SVladimir Oltean { 89756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 89856051948SVladimir Oltean 89956051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 90056051948SVladimir Oltean } 90156051948SVladimir Oltean 90256051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 90356051948SVladimir Oltean { 90456051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 90556051948SVladimir Oltean 90656051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 90756051948SVladimir Oltean } 90856051948SVladimir Oltean 90956051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 91056051948SVladimir Oltean struct ethtool_ts_info *info) 91156051948SVladimir Oltean { 91256051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 91356051948SVladimir Oltean 91456051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 91556051948SVladimir Oltean } 91656051948SVladimir Oltean 917bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 918bdeced75SVladimir Oltean struct device_node *ports_node, 919bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 920bdeced75SVladimir Oltean { 921bdeced75SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 922bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 923bdeced75SVladimir Oltean struct device_node *child; 924bdeced75SVladimir Oltean 92537fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 926bdeced75SVladimir Oltean phy_interface_t phy_mode; 927bdeced75SVladimir Oltean u32 port; 928bdeced75SVladimir Oltean int err; 929bdeced75SVladimir Oltean 930bdeced75SVladimir Oltean /* Get switch port number from DT */ 931bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 932bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 933bdeced75SVladimir Oltean "(property \"reg\")\n"); 934bdeced75SVladimir Oltean of_node_put(child); 935bdeced75SVladimir Oltean return -ENODEV; 936bdeced75SVladimir Oltean } 937bdeced75SVladimir Oltean 938bdeced75SVladimir Oltean /* Get PHY mode from DT */ 939bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 940bdeced75SVladimir Oltean if (err) { 941bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 942bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 943bdeced75SVladimir Oltean port); 944bdeced75SVladimir Oltean of_node_put(child); 945bdeced75SVladimir Oltean return -ENODEV; 946bdeced75SVladimir Oltean } 947bdeced75SVladimir Oltean 948bdeced75SVladimir Oltean err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 949bdeced75SVladimir Oltean if (err < 0) { 950bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 951bdeced75SVladimir Oltean phy_modes(phy_mode), port); 95259ebb430SSumera Priyadarsini of_node_put(child); 953bdeced75SVladimir Oltean return err; 954bdeced75SVladimir Oltean } 955bdeced75SVladimir Oltean 956bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 957bdeced75SVladimir Oltean } 958bdeced75SVladimir Oltean 959bdeced75SVladimir Oltean return 0; 960bdeced75SVladimir Oltean } 961bdeced75SVladimir Oltean 962bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 963bdeced75SVladimir Oltean { 964bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 965bdeced75SVladimir Oltean struct device_node *switch_node; 966bdeced75SVladimir Oltean struct device_node *ports_node; 967bdeced75SVladimir Oltean int err; 968bdeced75SVladimir Oltean 969bdeced75SVladimir Oltean switch_node = dev->of_node; 970bdeced75SVladimir Oltean 971bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 972abecbfcdSVladimir Oltean if (!ports_node) 973abecbfcdSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 974bdeced75SVladimir Oltean if (!ports_node) { 975abecbfcdSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 976bdeced75SVladimir Oltean return -ENODEV; 977bdeced75SVladimir Oltean } 978bdeced75SVladimir Oltean 979bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 980bdeced75SVladimir Oltean of_node_put(ports_node); 981bdeced75SVladimir Oltean 982bdeced75SVladimir Oltean return err; 983bdeced75SVladimir Oltean } 984bdeced75SVladimir Oltean 98556051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 98656051948SVladimir Oltean { 98756051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 988bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 989b4024c9eSClaudiu Manoil struct resource res; 99056051948SVladimir Oltean int port, i, err; 99156051948SVladimir Oltean 99256051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 99356051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 99456051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 99556051948SVladimir Oltean if (!ocelot->ports) 99656051948SVladimir Oltean return -ENOMEM; 99756051948SVladimir Oltean 99856051948SVladimir Oltean ocelot->map = felix->info->map; 99956051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 100056051948SVladimir Oltean ocelot->num_stats = felix->info->num_stats; 100121ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 100207d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 100377043c37SXiaoliang Yang ocelot->vcap_pol.base = felix->info->vcap_pol_base; 100477043c37SXiaoliang Yang ocelot->vcap_pol.max = felix->info->vcap_pol_max; 100577043c37SXiaoliang Yang ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 100677043c37SXiaoliang Yang ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 100756051948SVladimir Oltean ocelot->ops = felix->info->ops; 1008cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 1009cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 1010f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 101156051948SVladimir Oltean 1012bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 1013bdeced75SVladimir Oltean GFP_KERNEL); 1014bdeced75SVladimir Oltean if (!port_phy_modes) 1015bdeced75SVladimir Oltean return -ENOMEM; 1016bdeced75SVladimir Oltean 1017bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 1018bdeced75SVladimir Oltean if (err) { 1019bdeced75SVladimir Oltean kfree(port_phy_modes); 1020bdeced75SVladimir Oltean return err; 1021bdeced75SVladimir Oltean } 1022bdeced75SVladimir Oltean 102356051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 102456051948SVladimir Oltean struct regmap *target; 102556051948SVladimir Oltean 102656051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 102756051948SVladimir Oltean continue; 102856051948SVladimir Oltean 1029b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 1030b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1031375e1314SVladimir Oltean res.start += felix->switch_base; 1032375e1314SVladimir Oltean res.end += felix->switch_base; 103356051948SVladimir Oltean 1034242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 103556051948SVladimir Oltean if (IS_ERR(target)) { 103656051948SVladimir Oltean dev_err(ocelot->dev, 103756051948SVladimir Oltean "Failed to map device memory space\n"); 1038bdeced75SVladimir Oltean kfree(port_phy_modes); 103956051948SVladimir Oltean return PTR_ERR(target); 104056051948SVladimir Oltean } 104156051948SVladimir Oltean 104256051948SVladimir Oltean ocelot->targets[i] = target; 104356051948SVladimir Oltean } 104456051948SVladimir Oltean 104556051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 104656051948SVladimir Oltean if (err) { 104756051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 1048bdeced75SVladimir Oltean kfree(port_phy_modes); 104956051948SVladimir Oltean return err; 105056051948SVladimir Oltean } 105156051948SVladimir Oltean 105256051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 105356051948SVladimir Oltean struct ocelot_port *ocelot_port; 105491c724cfSVladimir Oltean struct regmap *target; 105556051948SVladimir Oltean 105656051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 105756051948SVladimir Oltean sizeof(struct ocelot_port), 105856051948SVladimir Oltean GFP_KERNEL); 105956051948SVladimir Oltean if (!ocelot_port) { 106056051948SVladimir Oltean dev_err(ocelot->dev, 106156051948SVladimir Oltean "failed to allocate port memory\n"); 1062bdeced75SVladimir Oltean kfree(port_phy_modes); 106356051948SVladimir Oltean return -ENOMEM; 106456051948SVladimir Oltean } 106556051948SVladimir Oltean 1066b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1067b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1068375e1314SVladimir Oltean res.start += felix->switch_base; 1069375e1314SVladimir Oltean res.end += felix->switch_base; 107056051948SVladimir Oltean 1071242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 107291c724cfSVladimir Oltean if (IS_ERR(target)) { 107356051948SVladimir Oltean dev_err(ocelot->dev, 107491c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 107591c724cfSVladimir Oltean port); 1076bdeced75SVladimir Oltean kfree(port_phy_modes); 107791c724cfSVladimir Oltean return PTR_ERR(target); 107856051948SVladimir Oltean } 107956051948SVladimir Oltean 1080bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 108156051948SVladimir Oltean ocelot_port->ocelot = ocelot; 108291c724cfSVladimir Oltean ocelot_port->target = target; 108356051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 108456051948SVladimir Oltean } 108556051948SVladimir Oltean 1086bdeced75SVladimir Oltean kfree(port_phy_modes); 1087bdeced75SVladimir Oltean 1088bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1089bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1090bdeced75SVladimir Oltean if (err < 0) 1091bdeced75SVladimir Oltean return err; 1092bdeced75SVladimir Oltean } 1093bdeced75SVladimir Oltean 109456051948SVladimir Oltean return 0; 109556051948SVladimir Oltean } 109656051948SVladimir Oltean 10971328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 10981328a883SVladimir Oltean struct sk_buff *skb) 10991328a883SVladimir Oltean { 11001328a883SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 11011328a883SVladimir Oltean struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 11021328a883SVladimir Oltean struct sk_buff *skb_match = NULL, *skb_tmp; 11031328a883SVladimir Oltean unsigned long flags; 11041328a883SVladimir Oltean 11051328a883SVladimir Oltean if (!clone) 11061328a883SVladimir Oltean return; 11071328a883SVladimir Oltean 11081328a883SVladimir Oltean spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 11091328a883SVladimir Oltean 11101328a883SVladimir Oltean skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 11111328a883SVladimir Oltean if (skb != clone) 11121328a883SVladimir Oltean continue; 11131328a883SVladimir Oltean __skb_unlink(skb, &ocelot_port->tx_skbs); 11141328a883SVladimir Oltean skb_match = skb; 11151328a883SVladimir Oltean break; 11161328a883SVladimir Oltean } 11171328a883SVladimir Oltean 11181328a883SVladimir Oltean spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 11191328a883SVladimir Oltean 11201328a883SVladimir Oltean WARN_ONCE(!skb_match, 11211328a883SVladimir Oltean "Could not find skb clone in TX timestamping list\n"); 11221328a883SVladimir Oltean } 11231328a883SVladimir Oltean 112449f885b2SVladimir Oltean #define work_to_xmit_work(w) \ 112549f885b2SVladimir Oltean container_of((w), struct felix_deferred_xmit_work, work) 112649f885b2SVladimir Oltean 112749f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work) 112849f885b2SVladimir Oltean { 112949f885b2SVladimir Oltean struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 113049f885b2SVladimir Oltean struct dsa_switch *ds = xmit_work->dp->ds; 113149f885b2SVladimir Oltean struct sk_buff *skb = xmit_work->skb; 113249f885b2SVladimir Oltean u32 rew_op = ocelot_ptp_rew_op(skb); 113349f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 113449f885b2SVladimir Oltean int port = xmit_work->dp->index; 113549f885b2SVladimir Oltean int retries = 10; 113649f885b2SVladimir Oltean 113749f885b2SVladimir Oltean do { 113849f885b2SVladimir Oltean if (ocelot_can_inject(ocelot, 0)) 113949f885b2SVladimir Oltean break; 114049f885b2SVladimir Oltean 114149f885b2SVladimir Oltean cpu_relax(); 114249f885b2SVladimir Oltean } while (--retries); 114349f885b2SVladimir Oltean 114449f885b2SVladimir Oltean if (!retries) { 114549f885b2SVladimir Oltean dev_err(ocelot->dev, "port %d failed to inject skb\n", 114649f885b2SVladimir Oltean port); 11471328a883SVladimir Oltean ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 114849f885b2SVladimir Oltean kfree_skb(skb); 114949f885b2SVladimir Oltean return; 115049f885b2SVladimir Oltean } 115149f885b2SVladimir Oltean 115249f885b2SVladimir Oltean ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 115349f885b2SVladimir Oltean 115449f885b2SVladimir Oltean consume_skb(skb); 115549f885b2SVladimir Oltean kfree(xmit_work); 115649f885b2SVladimir Oltean } 115749f885b2SVladimir Oltean 1158*35d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds, 1159*35d97680SVladimir Oltean enum dsa_tag_protocol proto) 116049f885b2SVladimir Oltean { 1161*35d97680SVladimir Oltean struct ocelot_8021q_tagger_data *tagger_data; 116249f885b2SVladimir Oltean 1163*35d97680SVladimir Oltean switch (proto) { 1164*35d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 1165*35d97680SVladimir Oltean tagger_data = ocelot_8021q_tagger_data(ds); 1166*35d97680SVladimir Oltean tagger_data->xmit_work_fn = felix_port_deferred_xmit; 116749f885b2SVladimir Oltean return 0; 1168*35d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT: 1169*35d97680SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 117049f885b2SVladimir Oltean return 0; 1171*35d97680SVladimir Oltean default: 1172*35d97680SVladimir Oltean return -EPROTONOSUPPORT; 117349f885b2SVladimir Oltean } 117449f885b2SVladimir Oltean } 117549f885b2SVladimir Oltean 117656051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with 117756051948SVladimir Oltean * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 117856051948SVladimir Oltean * us to allocate structures twice (leak memory) and map PCI memory twice 117956051948SVladimir Oltean * (which will not work). 118056051948SVladimir Oltean */ 118156051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds) 118256051948SVladimir Oltean { 118356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 118456051948SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 118556051948SVladimir Oltean int port, err; 118656051948SVladimir Oltean 118756051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 118856051948SVladimir Oltean if (err) 118956051948SVladimir Oltean return err; 119056051948SVladimir Oltean 1191d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1192d1cc0e93SVladimir Oltean if (err) 11936b73b7c9SVladimir Oltean goto out_mdiobus_free; 1194d1cc0e93SVladimir Oltean 11952b49d128SYangbo Lu if (ocelot->ptp) { 11962ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 11972b49d128SYangbo Lu if (err) { 11982b49d128SYangbo Lu dev_err(ocelot->dev, 11992b49d128SYangbo Lu "Timestamp initialization failed\n"); 12002b49d128SYangbo Lu ocelot->ptp = 0; 12012b49d128SYangbo Lu } 12022b49d128SYangbo Lu } 120356051948SVladimir Oltean 120456051948SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1205adb3dccfSVladimir Oltean if (dsa_is_unused_port(ds, port)) 1206adb3dccfSVladimir Oltean continue; 120756051948SVladimir Oltean 1208adb3dccfSVladimir Oltean ocelot_init_port(ocelot, port); 1209bd2b3161SXiaoliang Yang 1210bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1211bd2b3161SXiaoliang Yang * bits of vlan tag. 1212bd2b3161SXiaoliang Yang */ 1213bd2b3161SXiaoliang Yang felix_port_qos_map_init(ocelot, port); 121456051948SVladimir Oltean } 121556051948SVladimir Oltean 1216f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1217f59fd9caSVladimir Oltean if (err) 12186b73b7c9SVladimir Oltean goto out_deinit_ports; 1219f59fd9caSVladimir Oltean 1220adb3dccfSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1221adb3dccfSVladimir Oltean if (!dsa_is_cpu_port(ds, port)) 1222adb3dccfSVladimir Oltean continue; 1223adb3dccfSVladimir Oltean 1224adb3dccfSVladimir Oltean /* The initial tag protocol is NPI which always returns 0, so 1225adb3dccfSVladimir Oltean * there's no real point in checking for errors. 12261cf3299bSVladimir Oltean */ 1227adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, port, felix->tag_proto); 12288d5f7954SVladimir Oltean break; 1229adb3dccfSVladimir Oltean } 12301cf3299bSVladimir Oltean 12310b912fc9SVladimir Oltean ds->mtu_enforcement_ingress = true; 1232c54913c1SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 1233bdeced75SVladimir Oltean 123456051948SVladimir Oltean return 0; 12356b73b7c9SVladimir Oltean 12366b73b7c9SVladimir Oltean out_deinit_ports: 12376b73b7c9SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 12386b73b7c9SVladimir Oltean if (dsa_is_unused_port(ds, port)) 12396b73b7c9SVladimir Oltean continue; 12406b73b7c9SVladimir Oltean 12416b73b7c9SVladimir Oltean ocelot_deinit_port(ocelot, port); 12426b73b7c9SVladimir Oltean } 12436b73b7c9SVladimir Oltean 12446b73b7c9SVladimir Oltean ocelot_deinit_timestamp(ocelot); 12456b73b7c9SVladimir Oltean ocelot_deinit(ocelot); 12466b73b7c9SVladimir Oltean 12476b73b7c9SVladimir Oltean out_mdiobus_free: 12486b73b7c9SVladimir Oltean if (felix->info->mdio_bus_free) 12496b73b7c9SVladimir Oltean felix->info->mdio_bus_free(ocelot); 12506b73b7c9SVladimir Oltean 12516b73b7c9SVladimir Oltean return err; 125256051948SVladimir Oltean } 125356051948SVladimir Oltean 125456051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 125556051948SVladimir Oltean { 125656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1257bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1258e5fb512dSVladimir Oltean int port; 1259bdeced75SVladimir Oltean 1260adb3dccfSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1261adb3dccfSVladimir Oltean if (!dsa_is_cpu_port(ds, port)) 1262adb3dccfSVladimir Oltean continue; 1263adb3dccfSVladimir Oltean 1264adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, port, felix->tag_proto); 12658d5f7954SVladimir Oltean break; 1266adb3dccfSVladimir Oltean } 1267adb3dccfSVladimir Oltean 126842b5adbbSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 126942b5adbbSVladimir Oltean if (dsa_is_unused_port(ds, port)) 127042b5adbbSVladimir Oltean continue; 127142b5adbbSVladimir Oltean 1272e5fb512dSVladimir Oltean ocelot_deinit_port(ocelot, port); 127342b5adbbSVladimir Oltean } 1274d19741b0SVladimir Oltean 127549f885b2SVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 127649f885b2SVladimir Oltean ocelot_deinit_timestamp(ocelot); 127749f885b2SVladimir Oltean ocelot_deinit(ocelot); 127849f885b2SVladimir Oltean 1279d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1280d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 128156051948SVladimir Oltean } 128256051948SVladimir Oltean 1283c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1284c0bcf537SYangbo Lu struct ifreq *ifr) 1285c0bcf537SYangbo Lu { 1286c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1287c0bcf537SYangbo Lu 1288c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1289c0bcf537SYangbo Lu } 1290c0bcf537SYangbo Lu 1291c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1292c0bcf537SYangbo Lu struct ifreq *ifr) 1293c0bcf537SYangbo Lu { 1294c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1295c0bcf537SYangbo Lu 1296c0bcf537SYangbo Lu return ocelot_hwstamp_set(ocelot, port, ifr); 1297c0bcf537SYangbo Lu } 1298c0bcf537SYangbo Lu 12990a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type) 13000a6f17c6SVladimir Oltean { 13010a6f17c6SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 13020a6f17c6SVladimir Oltean int err, grp = 0; 13030a6f17c6SVladimir Oltean 13040a6f17c6SVladimir Oltean if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 13050a6f17c6SVladimir Oltean return false; 13060a6f17c6SVladimir Oltean 13070a6f17c6SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 13080a6f17c6SVladimir Oltean return false; 13090a6f17c6SVladimir Oltean 13100a6f17c6SVladimir Oltean if (ptp_type == PTP_CLASS_NONE) 13110a6f17c6SVladimir Oltean return false; 13120a6f17c6SVladimir Oltean 13130a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 13140a6f17c6SVladimir Oltean struct sk_buff *skb; 13150a6f17c6SVladimir Oltean unsigned int type; 13160a6f17c6SVladimir Oltean 13170a6f17c6SVladimir Oltean err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 13180a6f17c6SVladimir Oltean if (err) 13190a6f17c6SVladimir Oltean goto out; 13200a6f17c6SVladimir Oltean 13210a6f17c6SVladimir Oltean /* We trap to the CPU port module all PTP frames, but 13220a6f17c6SVladimir Oltean * felix_rxtstamp() only gets called for event frames. 13230a6f17c6SVladimir Oltean * So we need to avoid sending duplicate general 13240a6f17c6SVladimir Oltean * message frames by running a second BPF classifier 13250a6f17c6SVladimir Oltean * here and dropping those. 13260a6f17c6SVladimir Oltean */ 13270a6f17c6SVladimir Oltean __skb_push(skb, ETH_HLEN); 13280a6f17c6SVladimir Oltean 13290a6f17c6SVladimir Oltean type = ptp_classify_raw(skb); 13300a6f17c6SVladimir Oltean 13310a6f17c6SVladimir Oltean __skb_pull(skb, ETH_HLEN); 13320a6f17c6SVladimir Oltean 13330a6f17c6SVladimir Oltean if (type == PTP_CLASS_NONE) { 13340a6f17c6SVladimir Oltean kfree_skb(skb); 13350a6f17c6SVladimir Oltean continue; 13360a6f17c6SVladimir Oltean } 13370a6f17c6SVladimir Oltean 13380a6f17c6SVladimir Oltean netif_rx(skb); 13390a6f17c6SVladimir Oltean } 13400a6f17c6SVladimir Oltean 13410a6f17c6SVladimir Oltean out: 13420a6f17c6SVladimir Oltean if (err < 0) 13430a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 13440a6f17c6SVladimir Oltean 13450a6f17c6SVladimir Oltean return true; 13460a6f17c6SVladimir Oltean } 13470a6f17c6SVladimir Oltean 1348c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1349c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1350c0bcf537SYangbo Lu { 135192f62485SVladimir Oltean u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1352c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1353c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1354c0bcf537SYangbo Lu struct timespec64 ts; 135592f62485SVladimir Oltean u32 tstamp_hi; 135692f62485SVladimir Oltean u64 tstamp; 1357c0bcf537SYangbo Lu 13580a6f17c6SVladimir Oltean /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 13590a6f17c6SVladimir Oltean * for RX timestamping. Then free it, and poll for its copy through 13600a6f17c6SVladimir Oltean * MMIO in the CPU port module, and inject that into the stack from 13610a6f17c6SVladimir Oltean * ocelot_xtr_poll(). 13620a6f17c6SVladimir Oltean */ 13630a6f17c6SVladimir Oltean if (felix_check_xtr_pkt(ocelot, type)) { 13640a6f17c6SVladimir Oltean kfree_skb(skb); 13650a6f17c6SVladimir Oltean return true; 13660a6f17c6SVladimir Oltean } 13670a6f17c6SVladimir Oltean 1368c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1369c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1370c0bcf537SYangbo Lu 1371c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1372c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1373c0bcf537SYangbo Lu tstamp_hi--; 1374c0bcf537SYangbo Lu 1375c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1376c0bcf537SYangbo Lu 1377c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1378c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1379c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1380c0bcf537SYangbo Lu return false; 1381c0bcf537SYangbo Lu } 1382c0bcf537SYangbo Lu 13835c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port, 13845c5416f5SYangbo Lu struct sk_buff *skb) 1385c0bcf537SYangbo Lu { 1386c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1387682eaad9SYangbo Lu struct sk_buff *clone = NULL; 1388c0bcf537SYangbo Lu 1389682eaad9SYangbo Lu if (!ocelot->ptp) 13905c5416f5SYangbo Lu return; 1391c0bcf537SYangbo Lu 139252849bcfSVladimir Oltean if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 139352849bcfSVladimir Oltean dev_err_ratelimited(ds->dev, 139452849bcfSVladimir Oltean "port %d delivering skb without TX timestamp\n", 139552849bcfSVladimir Oltean port); 1396682eaad9SYangbo Lu return; 139752849bcfSVladimir Oltean } 1398682eaad9SYangbo Lu 1399682eaad9SYangbo Lu if (clone) 1400c4b364ceSYangbo Lu OCELOT_SKB_CB(skb)->clone = clone; 14015c5416f5SYangbo Lu } 1402c0bcf537SYangbo Lu 14030b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 14040b912fc9SVladimir Oltean { 14050b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 14060b912fc9SVladimir Oltean 14070b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 14080b912fc9SVladimir Oltean 14090b912fc9SVladimir Oltean return 0; 14100b912fc9SVladimir Oltean } 14110b912fc9SVladimir Oltean 14120b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 14130b912fc9SVladimir Oltean { 14140b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 14150b912fc9SVladimir Oltean 14160b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 14170b912fc9SVladimir Oltean } 14180b912fc9SVladimir Oltean 141907d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 142007d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 142107d985eeSVladimir Oltean { 142207d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 142307d985eeSVladimir Oltean 142407d985eeSVladimir Oltean return ocelot_cls_flower_replace(ocelot, port, cls, ingress); 142507d985eeSVladimir Oltean } 142607d985eeSVladimir Oltean 142707d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 142807d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 142907d985eeSVladimir Oltean { 143007d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 143107d985eeSVladimir Oltean 143207d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 143307d985eeSVladimir Oltean } 143407d985eeSVladimir Oltean 143507d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 143607d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 143707d985eeSVladimir Oltean { 143807d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 143907d985eeSVladimir Oltean 144007d985eeSVladimir Oltean return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 144107d985eeSVladimir Oltean } 144207d985eeSVladimir Oltean 1443fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1444fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1445fc411eaaSVladimir Oltean { 1446fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1447fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1448fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 14495f035af7SPo Liu .burst = policer->burst, 1450fc411eaaSVladimir Oltean }; 1451fc411eaaSVladimir Oltean 1452fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1453fc411eaaSVladimir Oltean } 1454fc411eaaSVladimir Oltean 1455fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1456fc411eaaSVladimir Oltean { 1457fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1458fc411eaaSVladimir Oltean 1459fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1460fc411eaaSVladimir Oltean } 1461fc411eaaSVladimir Oltean 1462de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1463de143c0eSXiaoliang Yang enum tc_setup_type type, 1464de143c0eSXiaoliang Yang void *type_data) 1465de143c0eSXiaoliang Yang { 1466de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1467de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1468de143c0eSXiaoliang Yang 1469de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1470de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1471de143c0eSXiaoliang Yang else 1472de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1473de143c0eSXiaoliang Yang } 1474de143c0eSXiaoliang Yang 1475f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1476f59fd9caSVladimir Oltean u16 pool_index, 1477f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1478f59fd9caSVladimir Oltean { 1479f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1480f59fd9caSVladimir Oltean 1481f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1482f59fd9caSVladimir Oltean } 1483f59fd9caSVladimir Oltean 1484f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1485f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1486f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1487f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1488f59fd9caSVladimir Oltean { 1489f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1490f59fd9caSVladimir Oltean 1491f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1492f59fd9caSVladimir Oltean threshold_type, extack); 1493f59fd9caSVladimir Oltean } 1494f59fd9caSVladimir Oltean 1495f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1496f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1497f59fd9caSVladimir Oltean u32 *p_threshold) 1498f59fd9caSVladimir Oltean { 1499f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1500f59fd9caSVladimir Oltean 1501f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1502f59fd9caSVladimir Oltean p_threshold); 1503f59fd9caSVladimir Oltean } 1504f59fd9caSVladimir Oltean 1505f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1506f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1507f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1508f59fd9caSVladimir Oltean { 1509f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1510f59fd9caSVladimir Oltean 1511f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1512f59fd9caSVladimir Oltean threshold, extack); 1513f59fd9caSVladimir Oltean } 1514f59fd9caSVladimir Oltean 1515f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1516f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1517f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1518f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1519f59fd9caSVladimir Oltean { 1520f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1521f59fd9caSVladimir Oltean 1522f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1523f59fd9caSVladimir Oltean pool_type, p_pool_index, 1524f59fd9caSVladimir Oltean p_threshold); 1525f59fd9caSVladimir Oltean } 1526f59fd9caSVladimir Oltean 1527f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1528f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1529f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1530f59fd9caSVladimir Oltean u16 pool_index, u32 threshold, 1531f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1532f59fd9caSVladimir Oltean { 1533f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1534f59fd9caSVladimir Oltean 1535f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1536f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1537f59fd9caSVladimir Oltean extack); 1538f59fd9caSVladimir Oltean } 1539f59fd9caSVladimir Oltean 1540f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1541f59fd9caSVladimir Oltean unsigned int sb_index) 1542f59fd9caSVladimir Oltean { 1543f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1544f59fd9caSVladimir Oltean 1545f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1546f59fd9caSVladimir Oltean } 1547f59fd9caSVladimir Oltean 1548f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1549f59fd9caSVladimir Oltean unsigned int sb_index) 1550f59fd9caSVladimir Oltean { 1551f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1552f59fd9caSVladimir Oltean 1553f59fd9caSVladimir Oltean return ocelot_sb_occ_max_clear(ocelot, sb_index); 1554f59fd9caSVladimir Oltean } 1555f59fd9caSVladimir Oltean 1556f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1557f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1558f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1559f59fd9caSVladimir Oltean { 1560f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1561f59fd9caSVladimir Oltean 1562f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1563f59fd9caSVladimir Oltean p_cur, p_max); 1564f59fd9caSVladimir Oltean } 1565f59fd9caSVladimir Oltean 1566f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1567f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1568f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1569f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1570f59fd9caSVladimir Oltean { 1571f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1572f59fd9caSVladimir Oltean 1573f59fd9caSVladimir Oltean return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1574f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1575f59fd9caSVladimir Oltean } 1576f59fd9caSVladimir Oltean 1577a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port, 1578a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1579a026c50bSHoratiu Vultur { 1580a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1581a026c50bSHoratiu Vultur 1582a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1583a026c50bSHoratiu Vultur } 1584a026c50bSHoratiu Vultur 1585a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port, 1586a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1587a026c50bSHoratiu Vultur { 1588a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1589a026c50bSHoratiu Vultur 1590a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1591a026c50bSHoratiu Vultur } 1592a026c50bSHoratiu Vultur 1593a026c50bSHoratiu Vultur static int 1594a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1595a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1596a026c50bSHoratiu Vultur { 1597a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1598a026c50bSHoratiu Vultur 1599a026c50bSHoratiu Vultur return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1600a026c50bSHoratiu Vultur } 1601a026c50bSHoratiu Vultur 1602a026c50bSHoratiu Vultur static int 1603a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1604a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1605a026c50bSHoratiu Vultur { 1606a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1607a026c50bSHoratiu Vultur 1608a026c50bSHoratiu Vultur return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1609a026c50bSHoratiu Vultur } 1610a026c50bSHoratiu Vultur 1611375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 161256051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1613adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 1614*35d97680SVladimir Oltean .connect_tag_protocol = felix_connect_tag_protocol, 161556051948SVladimir Oltean .setup = felix_setup, 161656051948SVladimir Oltean .teardown = felix_teardown, 161756051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 161856051948SVladimir Oltean .get_strings = felix_get_strings, 161956051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 162056051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 162156051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 1622bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1623bdeced75SVladimir Oltean .phylink_mac_config = felix_phylink_mac_config, 1624bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1625bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 162656051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 162756051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 162856051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1629209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1630209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 1631421741eaSVladimir Oltean .port_pre_bridge_flags = felix_pre_bridge_flags, 1632421741eaSVladimir Oltean .port_bridge_flags = felix_bridge_flags, 163356051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 163456051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 16358fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 16368fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 16378fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 163856051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 163956051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 164056051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 164156051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1642c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1643c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1644c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1645c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 16460b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 16470b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1648fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1649fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 165007d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 165107d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 165207d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1653de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1654f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1655f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1656f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1657f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1658f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1659f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1660f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1661f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1662f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1663f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1664a026c50bSHoratiu Vultur .port_mrp_add = felix_mrp_add, 1665a026c50bSHoratiu Vultur .port_mrp_del = felix_mrp_del, 1666a026c50bSHoratiu Vultur .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1667a026c50bSHoratiu Vultur .port_mrp_del_ring_role = felix_mrp_del_ring_role, 16685da11eb4SVladimir Oltean .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 16695da11eb4SVladimir Oltean .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 167056051948SVladimir Oltean }; 1671319e4dd1SVladimir Oltean 1672319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1673319e4dd1SVladimir Oltean { 1674319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1675319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1676319e4dd1SVladimir Oltean 1677319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1678319e4dd1SVladimir Oltean return NULL; 1679319e4dd1SVladimir Oltean 1680319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1681319e4dd1SVladimir Oltean } 1682319e4dd1SVladimir Oltean 1683319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1684319e4dd1SVladimir Oltean { 1685319e4dd1SVladimir Oltean struct dsa_port *dp; 1686319e4dd1SVladimir Oltean 1687319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1688319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1689319e4dd1SVladimir Oltean return -EINVAL; 1690319e4dd1SVladimir Oltean 1691319e4dd1SVladimir Oltean return dp->index; 1692319e4dd1SVladimir Oltean } 1693