156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0 23c9cfb52SVladimir Oltean /* Copyright 2019-2021 NXP 3375e1314SVladimir Oltean * 4375e1314SVladimir Oltean * This is an umbrella module for all network switches that are 5375e1314SVladimir Oltean * register-compatible with Ocelot and that perform I/O to their host CPU 6375e1314SVladimir Oltean * through an NPI (Node Processor Interface) Ethernet port. 756051948SVladimir Oltean */ 856051948SVladimir Oltean #include <uapi/linux/if_bridge.h> 907d985eeSVladimir Oltean #include <soc/mscc/ocelot_vcap.h> 10bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h> 11bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h> 12bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h> 13bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h> 142b49d128SYangbo Lu #include <soc/mscc/ocelot_ptp.h> 1556051948SVladimir Oltean #include <soc/mscc/ocelot.h> 16e21268efSVladimir Oltean #include <linux/dsa/8021q.h> 1740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h> 1884705fc1SMaxim Kochetkov #include <linux/platform_device.h> 190a6f17c6SVladimir Oltean #include <linux/ptp_classify.h> 2056051948SVladimir Oltean #include <linux/module.h> 21bdeced75SVladimir Oltean #include <linux/of_net.h> 2256051948SVladimir Oltean #include <linux/pci.h> 2356051948SVladimir Oltean #include <linux/of.h> 24fc411eaaSVladimir Oltean #include <net/pkt_sched.h> 2556051948SVladimir Oltean #include <net/dsa.h> 2656051948SVladimir Oltean #include "felix.h" 2756051948SVladimir Oltean 28e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid, 29e21268efSVladimir Oltean bool pvid, bool untagged) 30e21268efSVladimir Oltean { 31e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 32e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 33e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 34e21268efSVladimir Oltean int key_length, upstream, err; 35e21268efSVladimir Oltean 36e21268efSVladimir Oltean /* We don't need to install the rxvlan into the other ports' filtering 37e21268efSVladimir Oltean * tables, because we're just pushing the rxvlan when sending towards 38e21268efSVladimir Oltean * the CPU 39e21268efSVladimir Oltean */ 40e21268efSVladimir Oltean if (!pvid) 41e21268efSVladimir Oltean return 0; 42e21268efSVladimir Oltean 43e21268efSVladimir Oltean key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length; 44e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 45e21268efSVladimir Oltean 46e21268efSVladimir Oltean outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), 47e21268efSVladimir Oltean GFP_KERNEL); 48e21268efSVladimir Oltean if (!outer_tagging_rule) 49e21268efSVladimir Oltean return -ENOMEM; 50e21268efSVladimir Oltean 51e21268efSVladimir Oltean outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 52e21268efSVladimir Oltean outer_tagging_rule->prio = 1; 53c518afecSVladimir Oltean outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port); 54e21268efSVladimir Oltean outer_tagging_rule->id.tc_offload = false; 55e21268efSVladimir Oltean outer_tagging_rule->block_id = VCAP_ES0; 56e21268efSVladimir Oltean outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 57e21268efSVladimir Oltean outer_tagging_rule->lookup = 0; 58e21268efSVladimir Oltean outer_tagging_rule->ingress_port.value = port; 59e21268efSVladimir Oltean outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0); 60e21268efSVladimir Oltean outer_tagging_rule->egress_port.value = upstream; 61e21268efSVladimir Oltean outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0); 62e21268efSVladimir Oltean outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG; 63e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD; 64e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_vid_sel = 1; 65e21268efSVladimir Oltean outer_tagging_rule->action.vid_a_val = vid; 66e21268efSVladimir Oltean 67e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL); 68e21268efSVladimir Oltean if (err) 69e21268efSVladimir Oltean kfree(outer_tagging_rule); 70e21268efSVladimir Oltean 71e21268efSVladimir Oltean return err; 72e21268efSVladimir Oltean } 73e21268efSVladimir Oltean 74e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid, 75e21268efSVladimir Oltean bool pvid, bool untagged) 76e21268efSVladimir Oltean { 77e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 78e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 79e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 80e21268efSVladimir Oltean int upstream, err; 81e21268efSVladimir Oltean 82e21268efSVladimir Oltean /* tag_8021q.c assumes we are implementing this via port VLAN 83e21268efSVladimir Oltean * membership, which we aren't. So we don't need to add any VCAP filter 84e21268efSVladimir Oltean * for the CPU port. 85e21268efSVladimir Oltean */ 86e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 87e21268efSVladimir Oltean return 0; 88e21268efSVladimir Oltean 89e21268efSVladimir Oltean untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 90e21268efSVladimir Oltean if (!untagging_rule) 91e21268efSVladimir Oltean return -ENOMEM; 92e21268efSVladimir Oltean 93e21268efSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 94e21268efSVladimir Oltean if (!redirect_rule) { 95e21268efSVladimir Oltean kfree(untagging_rule); 96e21268efSVladimir Oltean return -ENOMEM; 97e21268efSVladimir Oltean } 98e21268efSVladimir Oltean 99e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 100e21268efSVladimir Oltean 101e21268efSVladimir Oltean untagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 102e21268efSVladimir Oltean untagging_rule->ingress_port_mask = BIT(upstream); 103e21268efSVladimir Oltean untagging_rule->vlan.vid.value = vid; 104e21268efSVladimir Oltean untagging_rule->vlan.vid.mask = VLAN_VID_MASK; 105e21268efSVladimir Oltean untagging_rule->prio = 1; 106c518afecSVladimir Oltean untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port); 107e21268efSVladimir Oltean untagging_rule->id.tc_offload = false; 108e21268efSVladimir Oltean untagging_rule->block_id = VCAP_IS1; 109e21268efSVladimir Oltean untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 110e21268efSVladimir Oltean untagging_rule->lookup = 0; 111e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt_ena = true; 112e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt = 1; 113e21268efSVladimir Oltean untagging_rule->action.pag_override_mask = 0xff; 114e21268efSVladimir Oltean untagging_rule->action.pag_val = port; 115e21268efSVladimir Oltean 116e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL); 117e21268efSVladimir Oltean if (err) { 118e21268efSVladimir Oltean kfree(untagging_rule); 119e21268efSVladimir Oltean kfree(redirect_rule); 120e21268efSVladimir Oltean return err; 121e21268efSVladimir Oltean } 122e21268efSVladimir Oltean 123e21268efSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 124e21268efSVladimir Oltean redirect_rule->ingress_port_mask = BIT(upstream); 125e21268efSVladimir Oltean redirect_rule->pag = port; 126e21268efSVladimir Oltean redirect_rule->prio = 1; 127c518afecSVladimir Oltean redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port); 128e21268efSVladimir Oltean redirect_rule->id.tc_offload = false; 129e21268efSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 130e21268efSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 131e21268efSVladimir Oltean redirect_rule->lookup = 0; 132e21268efSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 133e21268efSVladimir Oltean redirect_rule->action.port_mask = BIT(port); 134e21268efSVladimir Oltean 135e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 136e21268efSVladimir Oltean if (err) { 137e21268efSVladimir Oltean ocelot_vcap_filter_del(ocelot, untagging_rule); 138e21268efSVladimir Oltean kfree(redirect_rule); 139e21268efSVladimir Oltean return err; 140e21268efSVladimir Oltean } 141e21268efSVladimir Oltean 142e21268efSVladimir Oltean return 0; 143e21268efSVladimir Oltean } 144e21268efSVladimir Oltean 145e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 146e21268efSVladimir Oltean u16 flags) 147e21268efSVladimir Oltean { 148e21268efSVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 149e21268efSVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 150e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 151e21268efSVladimir Oltean 152e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 153e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot), 154e21268efSVladimir Oltean port, vid, pvid, untagged); 155e21268efSVladimir Oltean 156e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 157e21268efSVladimir Oltean return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot), 158e21268efSVladimir Oltean port, vid, pvid, untagged); 159e21268efSVladimir Oltean 160e21268efSVladimir Oltean return 0; 161e21268efSVladimir Oltean } 162e21268efSVladimir Oltean 163e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid) 164e21268efSVladimir Oltean { 165e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 166e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_es0; 167e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 168e21268efSVladimir Oltean 169e21268efSVladimir Oltean block_vcap_es0 = &ocelot->block[VCAP_ES0]; 170e21268efSVladimir Oltean 171e21268efSVladimir Oltean outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 172e21268efSVladimir Oltean port, false); 173e21268efSVladimir Oltean /* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid 174e21268efSVladimir Oltean * installing outer tagging ES0 rules where they weren't needed. 175e21268efSVladimir Oltean * But in rxvlan_del, the API doesn't give us the "flags" anymore, 176e21268efSVladimir Oltean * so that forces us to be slightly sloppy here, and just assume that 177e21268efSVladimir Oltean * if we didn't find an outer_tagging_rule it means that there was 178e21268efSVladimir Oltean * none in the first place, i.e. rxvlan_del is called on a non-pvid 179e21268efSVladimir Oltean * port. This is most probably true though. 180e21268efSVladimir Oltean */ 181e21268efSVladimir Oltean if (!outer_tagging_rule) 182e21268efSVladimir Oltean return 0; 183e21268efSVladimir Oltean 184e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 185e21268efSVladimir Oltean } 186e21268efSVladimir Oltean 187e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid) 188e21268efSVladimir Oltean { 189e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 190e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 191e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 192e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 193e21268efSVladimir Oltean int err; 194e21268efSVladimir Oltean 195e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 196e21268efSVladimir Oltean return 0; 197e21268efSVladimir Oltean 198e21268efSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 199e21268efSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 200e21268efSVladimir Oltean 201e21268efSVladimir Oltean untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 202e21268efSVladimir Oltean port, false); 203e21268efSVladimir Oltean if (!untagging_rule) 204e21268efSVladimir Oltean return 0; 205e21268efSVladimir Oltean 206e21268efSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, untagging_rule); 207e21268efSVladimir Oltean if (err) 208e21268efSVladimir Oltean return err; 209e21268efSVladimir Oltean 210e21268efSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 211e21268efSVladimir Oltean port, false); 212e21268efSVladimir Oltean if (!redirect_rule) 213e21268efSVladimir Oltean return 0; 214e21268efSVladimir Oltean 215e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 216e21268efSVladimir Oltean } 217e21268efSVladimir Oltean 218e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 219e21268efSVladimir Oltean { 220e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 221e21268efSVladimir Oltean 222e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 223e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot), 224e21268efSVladimir Oltean port, vid); 225e21268efSVladimir Oltean 226e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 227e21268efSVladimir Oltean return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot), 228e21268efSVladimir Oltean port, vid); 229e21268efSVladimir Oltean 230e21268efSVladimir Oltean return 0; 231e21268efSVladimir Oltean } 232e21268efSVladimir Oltean 233e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC 234e21268efSVladimir Oltean * connected internally to the enetc or fman DSA master can be configured to 235e21268efSVladimir Oltean * use the software-defined tag_8021q frame format. As far as the hardware is 236e21268efSVladimir Oltean * concerned, it thinks it is a "dumb switch" - the queues of the CPU port 237e21268efSVladimir Oltean * module are now disconnected from it, but can still be accessed through 238e21268efSVladimir Oltean * register-based MMIO. 239e21268efSVladimir Oltean */ 240e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port) 241e21268efSVladimir Oltean { 2428abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2438abe1970SVladimir Oltean 244e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = true; 245e21268efSVladimir Oltean ocelot->npi = -1; 246e21268efSVladimir Oltean 247e21268efSVladimir Oltean /* Overwrite PGID_CPU with the non-tagging port */ 248e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU); 249e21268efSVladimir Oltean 2508abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2518abe1970SVladimir Oltean 2528abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 253e21268efSVladimir Oltean } 254e21268efSVladimir Oltean 255e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port) 256e21268efSVladimir Oltean { 2578abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2588abe1970SVladimir Oltean 259e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = false; 260e21268efSVladimir Oltean 261e21268efSVladimir Oltean /* Restore PGID_CPU */ 262e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID, 263e21268efSVladimir Oltean PGID_CPU); 264e21268efSVladimir Oltean 2658abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2668abe1970SVladimir Oltean 2678abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 268e21268efSVladimir Oltean } 269e21268efSVladimir Oltean 270c8c0ba4fSVladimir Oltean /* Set up a VCAP IS2 rule for delivering PTP frames to the CPU port module. 271c8c0ba4fSVladimir Oltean * If the quirk_no_xtr_irq is in place, then also copy those PTP frames to the 272c8c0ba4fSVladimir Oltean * tag_8021q CPU port. 273c8c0ba4fSVladimir Oltean */ 274c8c0ba4fSVladimir Oltean static int felix_setup_mmio_filtering(struct felix *felix) 275c8c0ba4fSVladimir Oltean { 2768d5f7954SVladimir Oltean unsigned long user_ports = dsa_user_ports(felix->ds); 277c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *redirect_rule; 278c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *tagging_rule; 279c8c0ba4fSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 280c8c0ba4fSVladimir Oltean struct dsa_switch *ds = felix->ds; 281*2960bb14SVladimir Oltean struct dsa_port *dp; 282*2960bb14SVladimir Oltean int cpu = -1, 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 294*2960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 295*2960bb14SVladimir Oltean cpu = dp->index; 2968d5f7954SVladimir Oltean break; 297c8c0ba4fSVladimir Oltean } 2988d5f7954SVladimir Oltean 299e8b1d769SJosé Expósito if (cpu < 0) { 300e8b1d769SJosé Expósito kfree(tagging_rule); 301e8b1d769SJosé Expósito kfree(redirect_rule); 3028d5f7954SVladimir Oltean return -EINVAL; 303e8b1d769SJosé Expósito } 304c8c0ba4fSVladimir Oltean 305c8c0ba4fSVladimir Oltean tagging_rule->key_type = OCELOT_VCAP_KEY_ETYPE; 306c8c0ba4fSVladimir Oltean *(__be16 *)tagging_rule->key.etype.etype.value = htons(ETH_P_1588); 307c8c0ba4fSVladimir Oltean *(__be16 *)tagging_rule->key.etype.etype.mask = htons(0xffff); 308c8c0ba4fSVladimir Oltean tagging_rule->ingress_port_mask = user_ports; 309c8c0ba4fSVladimir Oltean tagging_rule->prio = 1; 310c518afecSVladimir Oltean tagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_PTP_MMIO(ocelot); 311c8c0ba4fSVladimir Oltean tagging_rule->id.tc_offload = false; 312c8c0ba4fSVladimir Oltean tagging_rule->block_id = VCAP_IS1; 313c8c0ba4fSVladimir Oltean tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 314c8c0ba4fSVladimir Oltean tagging_rule->lookup = 0; 315c8c0ba4fSVladimir Oltean tagging_rule->action.pag_override_mask = 0xff; 316c8c0ba4fSVladimir Oltean tagging_rule->action.pag_val = ocelot->num_phys_ports; 317c8c0ba4fSVladimir Oltean 318c8c0ba4fSVladimir Oltean ret = ocelot_vcap_filter_add(ocelot, tagging_rule, NULL); 319c8c0ba4fSVladimir Oltean if (ret) { 320c8c0ba4fSVladimir Oltean kfree(tagging_rule); 321c8c0ba4fSVladimir Oltean kfree(redirect_rule); 322c8c0ba4fSVladimir Oltean return ret; 323c8c0ba4fSVladimir Oltean } 324c8c0ba4fSVladimir Oltean 325c8c0ba4fSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 326c8c0ba4fSVladimir Oltean redirect_rule->ingress_port_mask = user_ports; 327c8c0ba4fSVladimir Oltean redirect_rule->pag = ocelot->num_phys_ports; 328c8c0ba4fSVladimir Oltean redirect_rule->prio = 1; 329c518afecSVladimir Oltean redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_PTP_MMIO(ocelot); 330c8c0ba4fSVladimir Oltean redirect_rule->id.tc_offload = false; 331c8c0ba4fSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 332c8c0ba4fSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 333c8c0ba4fSVladimir Oltean redirect_rule->lookup = 0; 334c8c0ba4fSVladimir Oltean redirect_rule->action.cpu_copy_ena = true; 335c8c0ba4fSVladimir Oltean if (felix->info->quirk_no_xtr_irq) { 336c8c0ba4fSVladimir Oltean /* Redirect to the tag_8021q CPU but also copy PTP packets to 337c8c0ba4fSVladimir Oltean * the CPU port module 338c8c0ba4fSVladimir Oltean */ 339c8c0ba4fSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 3408d5f7954SVladimir Oltean redirect_rule->action.port_mask = BIT(cpu); 341c8c0ba4fSVladimir Oltean } else { 342c8c0ba4fSVladimir Oltean /* Trap PTP packets only to the CPU port module (which is 343c8c0ba4fSVladimir Oltean * redirected to the NPI port) 344c8c0ba4fSVladimir Oltean */ 345c8c0ba4fSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 346c8c0ba4fSVladimir Oltean redirect_rule->action.port_mask = 0; 347c8c0ba4fSVladimir Oltean } 348c8c0ba4fSVladimir Oltean 349c8c0ba4fSVladimir Oltean ret = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 350c8c0ba4fSVladimir Oltean if (ret) { 351c8c0ba4fSVladimir Oltean ocelot_vcap_filter_del(ocelot, tagging_rule); 352c8c0ba4fSVladimir Oltean kfree(redirect_rule); 353c8c0ba4fSVladimir Oltean return ret; 354c8c0ba4fSVladimir Oltean } 355c8c0ba4fSVladimir Oltean 3560a6f17c6SVladimir Oltean /* The ownership of the CPU port module's queues might have just been 3570a6f17c6SVladimir Oltean * transferred to the tag_8021q tagger from the NPI-based tagger. 3580a6f17c6SVladimir Oltean * So there might still be all sorts of crap in the queues. On the 3590a6f17c6SVladimir Oltean * other hand, the MMIO-based matching of PTP frames is very brittle, 3600a6f17c6SVladimir Oltean * so we need to be careful that there are no extra frames to be 3610a6f17c6SVladimir Oltean * dequeued over MMIO, since we would never know to discard them. 3620a6f17c6SVladimir Oltean */ 3630a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 3640a6f17c6SVladimir Oltean 365c8c0ba4fSVladimir Oltean return 0; 366c8c0ba4fSVladimir Oltean } 367c8c0ba4fSVladimir Oltean 368c8c0ba4fSVladimir Oltean static int felix_teardown_mmio_filtering(struct felix *felix) 369c8c0ba4fSVladimir Oltean { 370c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *tagging_rule, *redirect_rule; 371c8c0ba4fSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 372c8c0ba4fSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 373c8c0ba4fSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 374c8c0ba4fSVladimir Oltean int err; 375c8c0ba4fSVladimir Oltean 376c8c0ba4fSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 377c8c0ba4fSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 378c8c0ba4fSVladimir Oltean 379c8c0ba4fSVladimir Oltean tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 380c8c0ba4fSVladimir Oltean ocelot->num_phys_ports, 381c8c0ba4fSVladimir Oltean false); 382c8c0ba4fSVladimir Oltean if (!tagging_rule) 383c8c0ba4fSVladimir Oltean return -ENOENT; 384c8c0ba4fSVladimir Oltean 385c8c0ba4fSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, tagging_rule); 386c8c0ba4fSVladimir Oltean if (err) 387c8c0ba4fSVladimir Oltean return err; 388c8c0ba4fSVladimir Oltean 389c8c0ba4fSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 390c8c0ba4fSVladimir Oltean ocelot->num_phys_ports, 391c8c0ba4fSVladimir Oltean false); 392c8c0ba4fSVladimir Oltean if (!redirect_rule) 393c8c0ba4fSVladimir Oltean return -ENOENT; 394c8c0ba4fSVladimir Oltean 395c8c0ba4fSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 396c8c0ba4fSVladimir Oltean } 397c8c0ba4fSVladimir Oltean 398e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu) 399e21268efSVladimir Oltean { 400e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 401e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 402e21268efSVladimir Oltean unsigned long cpu_flood; 403*2960bb14SVladimir Oltean struct dsa_port *dp; 404*2960bb14SVladimir Oltean int err; 405e21268efSVladimir Oltean 406e21268efSVladimir Oltean felix_8021q_cpu_port_init(ocelot, cpu); 407e21268efSVladimir Oltean 408*2960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 409e21268efSVladimir Oltean /* This overwrites ocelot_init(): 410e21268efSVladimir Oltean * Do not forward BPDU frames to the CPU port module, 411e21268efSVladimir Oltean * for 2 reasons: 412e21268efSVladimir Oltean * - When these packets are injected from the tag_8021q 413e21268efSVladimir Oltean * CPU port, we want them to go out, not loop back 414e21268efSVladimir Oltean * into the system. 415e21268efSVladimir Oltean * - STP traffic ingressing on a user port should go to 416e21268efSVladimir Oltean * the tag_8021q CPU port, not to the hardware CPU 417e21268efSVladimir Oltean * port module. 418e21268efSVladimir Oltean */ 419e21268efSVladimir Oltean ocelot_write_gix(ocelot, 420e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 421*2960bb14SVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, dp->index); 422e21268efSVladimir Oltean } 423e21268efSVladimir Oltean 424c8c0ba4fSVladimir Oltean /* In tag_8021q mode, the CPU port module is unused, except for PTP 425c8c0ba4fSVladimir Oltean * frames. So we want to disable flooding of any kind to the CPU port 426c8c0ba4fSVladimir Oltean * module, since packets going there will end in a black hole. 427e21268efSVladimir Oltean */ 428e21268efSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 429e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC); 430e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC); 431b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC); 432e21268efSVladimir Oltean 4335da11eb4SVladimir Oltean err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD)); 434e21268efSVladimir Oltean if (err) 435d7b1fd52SVladimir Oltean return err; 436d7b1fd52SVladimir Oltean 437328621f6SVladimir Oltean err = felix_setup_mmio_filtering(felix); 438d7b1fd52SVladimir Oltean if (err) 439d7b1fd52SVladimir Oltean goto out_tag_8021q_unregister; 440e21268efSVladimir Oltean 441e21268efSVladimir Oltean return 0; 442e21268efSVladimir Oltean 443d7b1fd52SVladimir Oltean out_tag_8021q_unregister: 444d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 445e21268efSVladimir Oltean return err; 446e21268efSVladimir Oltean } 447e21268efSVladimir Oltean 448e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu) 449e21268efSVladimir Oltean { 450e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 451e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 452*2960bb14SVladimir Oltean struct dsa_port *dp; 453*2960bb14SVladimir Oltean int err; 454e21268efSVladimir Oltean 455c8c0ba4fSVladimir Oltean err = felix_teardown_mmio_filtering(felix); 456c8c0ba4fSVladimir Oltean if (err) 457c8c0ba4fSVladimir Oltean dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d", 458c8c0ba4fSVladimir Oltean err); 459c8c0ba4fSVladimir Oltean 460d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 461e21268efSVladimir Oltean 462*2960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 463e21268efSVladimir Oltean /* Restore the logic from ocelot_init: 464e21268efSVladimir Oltean * do not forward BPDU frames to the front ports. 465e21268efSVladimir Oltean */ 466e21268efSVladimir Oltean ocelot_write_gix(ocelot, 467e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 468e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, 469*2960bb14SVladimir Oltean dp->index); 470e21268efSVladimir Oltean } 471e21268efSVladimir Oltean 472e21268efSVladimir Oltean felix_8021q_cpu_port_deinit(ocelot, cpu); 473e21268efSVladimir Oltean } 474e21268efSVladimir Oltean 475adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This 476adb3dccfSVladimir Oltean * is the mode through which frames can be injected from and extracted to an 477adb3dccfSVladimir Oltean * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 478adb3dccfSVladimir Oltean * running Linux, and this forms a DSA setup together with the enetc or fman 479adb3dccfSVladimir Oltean * DSA master. 480adb3dccfSVladimir Oltean */ 481adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port) 482adb3dccfSVladimir Oltean { 483adb3dccfSVladimir Oltean ocelot->npi = port; 484adb3dccfSVladimir Oltean 485adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 486adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 487adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 488adb3dccfSVladimir Oltean 489adb3dccfSVladimir Oltean /* NPI port Injection/Extraction configuration */ 490adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 491adb3dccfSVladimir Oltean ocelot->npi_xtr_prefix); 492adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 493adb3dccfSVladimir Oltean ocelot->npi_inj_prefix); 494adb3dccfSVladimir Oltean 495adb3dccfSVladimir Oltean /* Disable transmission of pause frames */ 496adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 497adb3dccfSVladimir Oltean } 498adb3dccfSVladimir Oltean 499adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 500adb3dccfSVladimir Oltean { 501adb3dccfSVladimir Oltean /* Restore hardware defaults */ 502adb3dccfSVladimir Oltean int unused_port = ocelot->num_phys_ports + 2; 503adb3dccfSVladimir Oltean 504adb3dccfSVladimir Oltean ocelot->npi = -1; 505adb3dccfSVladimir Oltean 506adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 507adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 508adb3dccfSVladimir Oltean 509adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 510adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 511adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 512adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 513adb3dccfSVladimir Oltean 514adb3dccfSVladimir Oltean /* Enable transmission of pause frames */ 515adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 516adb3dccfSVladimir Oltean } 517adb3dccfSVladimir Oltean 518adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu) 519adb3dccfSVladimir Oltean { 520adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 521adb3dccfSVladimir Oltean unsigned long cpu_flood; 522adb3dccfSVladimir Oltean 523adb3dccfSVladimir Oltean felix_npi_port_init(ocelot, cpu); 524adb3dccfSVladimir Oltean 525adb3dccfSVladimir Oltean /* Include the CPU port module (and indirectly, the NPI port) 526adb3dccfSVladimir Oltean * in the forwarding mask for unknown unicast - the hardware 527adb3dccfSVladimir Oltean * default value for ANA_FLOODING_FLD_UNICAST excludes 528adb3dccfSVladimir Oltean * BIT(ocelot->num_phys_ports), and so does ocelot_init, 529adb3dccfSVladimir Oltean * since Ocelot relies on whitelisting MAC addresses towards 530adb3dccfSVladimir Oltean * PGID_CPU. 531adb3dccfSVladimir Oltean * We do this because DSA does not yet perform RX filtering, 532adb3dccfSVladimir Oltean * and the NPI port does not perform source address learning, 533adb3dccfSVladimir Oltean * so traffic sent to Linux is effectively unknown from the 534adb3dccfSVladimir Oltean * switch's perspective. 535adb3dccfSVladimir Oltean */ 536adb3dccfSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 537adb3dccfSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC); 5386edb9e8dSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC); 539b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC); 540adb3dccfSVladimir Oltean 541adb3dccfSVladimir Oltean return 0; 542adb3dccfSVladimir Oltean } 543adb3dccfSVladimir Oltean 544adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu) 545adb3dccfSVladimir Oltean { 546adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 547adb3dccfSVladimir Oltean 548adb3dccfSVladimir Oltean felix_npi_port_deinit(ocelot, cpu); 549adb3dccfSVladimir Oltean } 550adb3dccfSVladimir Oltean 551adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu, 552adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 553adb3dccfSVladimir Oltean { 554adb3dccfSVladimir Oltean int err; 555adb3dccfSVladimir Oltean 556adb3dccfSVladimir Oltean switch (proto) { 5577c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 558adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 559adb3dccfSVladimir Oltean err = felix_setup_tag_npi(ds, cpu); 560adb3dccfSVladimir Oltean break; 561e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 562e21268efSVladimir Oltean err = felix_setup_tag_8021q(ds, cpu); 563e21268efSVladimir Oltean break; 564adb3dccfSVladimir Oltean default: 565adb3dccfSVladimir Oltean err = -EPROTONOSUPPORT; 566adb3dccfSVladimir Oltean } 567adb3dccfSVladimir Oltean 568adb3dccfSVladimir Oltean return err; 569adb3dccfSVladimir Oltean } 570adb3dccfSVladimir Oltean 571adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu, 572adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 573adb3dccfSVladimir Oltean { 574adb3dccfSVladimir Oltean switch (proto) { 5757c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 576adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 577adb3dccfSVladimir Oltean felix_teardown_tag_npi(ds, cpu); 578adb3dccfSVladimir Oltean break; 579e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 580e21268efSVladimir Oltean felix_teardown_tag_8021q(ds, cpu); 581e21268efSVladimir Oltean break; 582adb3dccfSVladimir Oltean default: 583adb3dccfSVladimir Oltean break; 584adb3dccfSVladimir Oltean } 585adb3dccfSVladimir Oltean } 586adb3dccfSVladimir Oltean 587e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the 588e21268efSVladimir Oltean * tag_8021q setup can fail, the NPI setup can't. So either the change is made, 589e21268efSVladimir Oltean * or the restoration is guaranteed to work. 590e21268efSVladimir Oltean */ 591adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu, 592adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 593adb3dccfSVladimir Oltean { 594adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 595adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 596adb3dccfSVladimir Oltean enum dsa_tag_protocol old_proto = felix->tag_proto; 597adb3dccfSVladimir Oltean int err; 598adb3dccfSVladimir Oltean 5997c4bb540SVladimir Oltean if (proto != DSA_TAG_PROTO_SEVILLE && 6007c4bb540SVladimir Oltean proto != DSA_TAG_PROTO_OCELOT && 601e21268efSVladimir Oltean proto != DSA_TAG_PROTO_OCELOT_8021Q) 602adb3dccfSVladimir Oltean return -EPROTONOSUPPORT; 603adb3dccfSVladimir Oltean 604adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, cpu, old_proto); 605adb3dccfSVladimir Oltean 606adb3dccfSVladimir Oltean err = felix_set_tag_protocol(ds, cpu, proto); 607adb3dccfSVladimir Oltean if (err) { 608adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, cpu, old_proto); 609adb3dccfSVladimir Oltean return err; 610adb3dccfSVladimir Oltean } 611adb3dccfSVladimir Oltean 612adb3dccfSVladimir Oltean felix->tag_proto = proto; 613adb3dccfSVladimir Oltean 614adb3dccfSVladimir Oltean return 0; 615adb3dccfSVladimir Oltean } 616adb3dccfSVladimir Oltean 61756051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 6184d776482SFlorian Fainelli int port, 6194d776482SFlorian Fainelli enum dsa_tag_protocol mp) 62056051948SVladimir Oltean { 621adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 622adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 623adb3dccfSVladimir Oltean 624adb3dccfSVladimir Oltean return felix->tag_proto; 62556051948SVladimir Oltean } 62656051948SVladimir Oltean 62756051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds, 62856051948SVladimir Oltean unsigned int ageing_time) 62956051948SVladimir Oltean { 63056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 63156051948SVladimir Oltean 63256051948SVladimir Oltean ocelot_set_ageing_time(ocelot, ageing_time); 63356051948SVladimir Oltean 63456051948SVladimir Oltean return 0; 63556051948SVladimir Oltean } 63656051948SVladimir Oltean 6375cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port) 6385cad43a5SVladimir Oltean { 6395cad43a5SVladimir Oltean struct ocelot *ocelot = ds->priv; 6405cad43a5SVladimir Oltean int err; 6415cad43a5SVladimir Oltean 6425cad43a5SVladimir Oltean err = ocelot_mact_flush(ocelot, port); 6435cad43a5SVladimir Oltean if (err) 6445cad43a5SVladimir Oltean dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n", 6455cad43a5SVladimir Oltean port, ERR_PTR(err)); 6465cad43a5SVladimir Oltean } 6475cad43a5SVladimir Oltean 64856051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port, 64956051948SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 65056051948SVladimir Oltean { 65156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 65256051948SVladimir Oltean 65356051948SVladimir Oltean return ocelot_fdb_dump(ocelot, port, cb, data); 65456051948SVladimir Oltean } 65556051948SVladimir Oltean 65656051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port, 65756051948SVladimir Oltean const unsigned char *addr, u16 vid) 65856051948SVladimir Oltean { 65956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 66056051948SVladimir Oltean 66187b0f983SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid); 66256051948SVladimir Oltean } 66356051948SVladimir Oltean 66456051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port, 66556051948SVladimir Oltean const unsigned char *addr, u16 vid) 66656051948SVladimir Oltean { 66756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 66856051948SVladimir Oltean 66956051948SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid); 67056051948SVladimir Oltean } 67156051948SVladimir Oltean 672a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 673209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 674209edf95SVladimir Oltean { 675209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 676209edf95SVladimir Oltean 677a52b2da7SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb); 678209edf95SVladimir Oltean } 679209edf95SVladimir Oltean 680209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port, 681209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 682209edf95SVladimir Oltean { 683209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 684209edf95SVladimir Oltean 685209edf95SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb); 686209edf95SVladimir Oltean } 687209edf95SVladimir Oltean 68856051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 68956051948SVladimir Oltean u8 state) 69056051948SVladimir Oltean { 69156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 69256051948SVladimir Oltean 69356051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 69456051948SVladimir Oltean } 69556051948SVladimir Oltean 696421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 697421741eaSVladimir Oltean struct switchdev_brport_flags val, 698421741eaSVladimir Oltean struct netlink_ext_ack *extack) 699421741eaSVladimir Oltean { 700421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 701421741eaSVladimir Oltean 702421741eaSVladimir Oltean return ocelot_port_pre_bridge_flags(ocelot, port, val); 703421741eaSVladimir Oltean } 704421741eaSVladimir Oltean 705421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port, 706421741eaSVladimir Oltean struct switchdev_brport_flags val, 707421741eaSVladimir Oltean struct netlink_ext_ack *extack) 708421741eaSVladimir Oltean { 709421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 710421741eaSVladimir Oltean 711421741eaSVladimir Oltean ocelot_port_bridge_flags(ocelot, port, val); 712421741eaSVladimir Oltean 713421741eaSVladimir Oltean return 0; 714421741eaSVladimir Oltean } 715421741eaSVladimir Oltean 71656051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 717b079922bSVladimir Oltean struct dsa_bridge bridge, bool *tx_fwd_offload) 71856051948SVladimir Oltean { 71956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 72056051948SVladimir Oltean 721d3eed0e5SVladimir Oltean ocelot_port_bridge_join(ocelot, port, bridge.dev); 722e4bd44e8SVladimir Oltean 723e4bd44e8SVladimir Oltean return 0; 72456051948SVladimir Oltean } 72556051948SVladimir Oltean 72656051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 727d3eed0e5SVladimir Oltean struct dsa_bridge bridge) 72856051948SVladimir Oltean { 72956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 73056051948SVladimir Oltean 731d3eed0e5SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, bridge.dev); 73256051948SVladimir Oltean } 73356051948SVladimir Oltean 7348fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 7358fe6832eSVladimir Oltean struct net_device *bond, 7368fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 7378fe6832eSVladimir Oltean { 7388fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7398fe6832eSVladimir Oltean 7408fe6832eSVladimir Oltean return ocelot_port_lag_join(ocelot, port, bond, info); 7418fe6832eSVladimir Oltean } 7428fe6832eSVladimir Oltean 7438fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 7448fe6832eSVladimir Oltean struct net_device *bond) 7458fe6832eSVladimir Oltean { 7468fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7478fe6832eSVladimir Oltean 7488fe6832eSVladimir Oltean ocelot_port_lag_leave(ocelot, port, bond); 7498fe6832eSVladimir Oltean 7508fe6832eSVladimir Oltean return 0; 7518fe6832eSVladimir Oltean } 7528fe6832eSVladimir Oltean 7538fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 7548fe6832eSVladimir Oltean { 7558fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 7568fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7578fe6832eSVladimir Oltean 7588fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 7598fe6832eSVladimir Oltean 7608fe6832eSVladimir Oltean return 0; 7618fe6832eSVladimir Oltean } 7628fe6832eSVladimir Oltean 76356051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 76401af940eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 76501af940eSVladimir Oltean struct netlink_ext_ack *extack) 76656051948SVladimir Oltean { 7672f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 768b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 7692f0402feSVladimir Oltean 7709a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 7719a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 7729a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 7739a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 7749a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 7759a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 7769a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 7779a720680SVladimir Oltean */ 7789a720680SVladimir Oltean if (port == ocelot->npi) 7799a720680SVladimir Oltean return 0; 7809a720680SVladimir Oltean 781b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 7822f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 78301af940eSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED, 78401af940eSVladimir Oltean extack); 78556051948SVladimir Oltean } 78656051948SVladimir Oltean 78789153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 78889153ed6SVladimir Oltean struct netlink_ext_ack *extack) 78956051948SVladimir Oltean { 79056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 79156051948SVladimir Oltean 7923b95d1b2SVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 79356051948SVladimir Oltean } 79456051948SVladimir Oltean 7951958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 79631046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 79731046a5fSVladimir Oltean struct netlink_ext_ack *extack) 79856051948SVladimir Oltean { 79956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 800183be6f9SVladimir Oltean u16 flags = vlan->flags; 8011958d581SVladimir Oltean int err; 80256051948SVladimir Oltean 80301af940eSVladimir Oltean err = felix_vlan_prepare(ds, port, vlan, extack); 8041958d581SVladimir Oltean if (err) 8051958d581SVladimir Oltean return err; 8061958d581SVladimir Oltean 8071958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 808183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 809183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 81056051948SVladimir Oltean } 81156051948SVladimir Oltean 81256051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 81356051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 81456051948SVladimir Oltean { 81556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 81656051948SVladimir Oltean 817b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 81856051948SVladimir Oltean } 81956051948SVladimir Oltean 820bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port, 821bdeced75SVladimir Oltean unsigned long *supported, 822bdeced75SVladimir Oltean struct phylink_link_state *state) 823bdeced75SVladimir Oltean { 824bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 825375e1314SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 826bdeced75SVladimir Oltean 827375e1314SVladimir Oltean if (felix->info->phylink_validate) 828375e1314SVladimir Oltean felix->info->phylink_validate(ocelot, port, supported, state); 829bdeced75SVladimir Oltean } 830bdeced75SVladimir Oltean 831bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port, 832bdeced75SVladimir Oltean unsigned int link_an_mode, 833bdeced75SVladimir Oltean const struct phylink_link_state *state) 834bdeced75SVladimir Oltean { 835bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 836bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 837588d0550SIoana Ciornei struct dsa_port *dp = dsa_to_port(ds, port); 838bdeced75SVladimir Oltean 83949af6a76SColin Foster if (felix->pcs && felix->pcs[port]) 840e7026f15SColin Foster phylink_set_pcs(dp->pl, felix->pcs[port]); 841bdeced75SVladimir Oltean } 842bdeced75SVladimir Oltean 843bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 844bdeced75SVladimir Oltean unsigned int link_an_mode, 845bdeced75SVladimir Oltean phy_interface_t interface) 846bdeced75SVladimir Oltean { 847bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 848bdeced75SVladimir Oltean 849e6e12df6SVladimir Oltean ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 850e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 851bdeced75SVladimir Oltean } 852bdeced75SVladimir Oltean 853bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 854bdeced75SVladimir Oltean unsigned int link_an_mode, 855bdeced75SVladimir Oltean phy_interface_t interface, 8565b502a7bSRussell King struct phy_device *phydev, 8575b502a7bSRussell King int speed, int duplex, 8585b502a7bSRussell King bool tx_pause, bool rx_pause) 859bdeced75SVladimir Oltean { 860bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 8617e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 862bdeced75SVladimir Oltean 863e6e12df6SVladimir Oltean ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 864e6e12df6SVladimir Oltean interface, speed, duplex, tx_pause, rx_pause, 865e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 8667e14a2dcSVladimir Oltean 8677e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 8687e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 869bdeced75SVladimir Oltean } 870bdeced75SVladimir Oltean 871bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 872bd2b3161SXiaoliang Yang { 873bd2b3161SXiaoliang Yang int i; 874bd2b3161SXiaoliang Yang 875bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 876bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 877bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 878bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 879bd2b3161SXiaoliang Yang port); 880bd2b3161SXiaoliang Yang 88170d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 882bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 883bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 884bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 885bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 886bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 887bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 888bd2b3161SXiaoliang Yang port, i); 889bd2b3161SXiaoliang Yang } 890bd2b3161SXiaoliang Yang } 891bd2b3161SXiaoliang Yang 89256051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 89356051948SVladimir Oltean u32 stringset, u8 *data) 89456051948SVladimir Oltean { 89556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 89656051948SVladimir Oltean 89756051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 89856051948SVladimir Oltean } 89956051948SVladimir Oltean 90056051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 90156051948SVladimir Oltean { 90256051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 90356051948SVladimir Oltean 90456051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 90556051948SVladimir Oltean } 90656051948SVladimir Oltean 90756051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 90856051948SVladimir Oltean { 90956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 91056051948SVladimir Oltean 91156051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 91256051948SVladimir Oltean } 91356051948SVladimir Oltean 91456051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 91556051948SVladimir Oltean struct ethtool_ts_info *info) 91656051948SVladimir Oltean { 91756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 91856051948SVladimir Oltean 91956051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 92056051948SVladimir Oltean } 92156051948SVladimir Oltean 922bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 923bdeced75SVladimir Oltean struct device_node *ports_node, 924bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 925bdeced75SVladimir Oltean { 926bdeced75SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 927bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 928bdeced75SVladimir Oltean struct device_node *child; 929bdeced75SVladimir Oltean 93037fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 931bdeced75SVladimir Oltean phy_interface_t phy_mode; 932bdeced75SVladimir Oltean u32 port; 933bdeced75SVladimir Oltean int err; 934bdeced75SVladimir Oltean 935bdeced75SVladimir Oltean /* Get switch port number from DT */ 936bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 937bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 938bdeced75SVladimir Oltean "(property \"reg\")\n"); 939bdeced75SVladimir Oltean of_node_put(child); 940bdeced75SVladimir Oltean return -ENODEV; 941bdeced75SVladimir Oltean } 942bdeced75SVladimir Oltean 943bdeced75SVladimir Oltean /* Get PHY mode from DT */ 944bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 945bdeced75SVladimir Oltean if (err) { 946bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 947bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 948bdeced75SVladimir Oltean port); 949bdeced75SVladimir Oltean of_node_put(child); 950bdeced75SVladimir Oltean return -ENODEV; 951bdeced75SVladimir Oltean } 952bdeced75SVladimir Oltean 953bdeced75SVladimir Oltean err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 954bdeced75SVladimir Oltean if (err < 0) { 955bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 956bdeced75SVladimir Oltean phy_modes(phy_mode), port); 95759ebb430SSumera Priyadarsini of_node_put(child); 958bdeced75SVladimir Oltean return err; 959bdeced75SVladimir Oltean } 960bdeced75SVladimir Oltean 961bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 962bdeced75SVladimir Oltean } 963bdeced75SVladimir Oltean 964bdeced75SVladimir Oltean return 0; 965bdeced75SVladimir Oltean } 966bdeced75SVladimir Oltean 967bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 968bdeced75SVladimir Oltean { 969bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 970bdeced75SVladimir Oltean struct device_node *switch_node; 971bdeced75SVladimir Oltean struct device_node *ports_node; 972bdeced75SVladimir Oltean int err; 973bdeced75SVladimir Oltean 974bdeced75SVladimir Oltean switch_node = dev->of_node; 975bdeced75SVladimir Oltean 976bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 977abecbfcdSVladimir Oltean if (!ports_node) 978abecbfcdSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 979bdeced75SVladimir Oltean if (!ports_node) { 980abecbfcdSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 981bdeced75SVladimir Oltean return -ENODEV; 982bdeced75SVladimir Oltean } 983bdeced75SVladimir Oltean 984bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 985bdeced75SVladimir Oltean of_node_put(ports_node); 986bdeced75SVladimir Oltean 987bdeced75SVladimir Oltean return err; 988bdeced75SVladimir Oltean } 989bdeced75SVladimir Oltean 99056051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 99156051948SVladimir Oltean { 99256051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 993bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 994b4024c9eSClaudiu Manoil struct resource res; 99556051948SVladimir Oltean int port, i, err; 99656051948SVladimir Oltean 99756051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 99856051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 99956051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 100056051948SVladimir Oltean if (!ocelot->ports) 100156051948SVladimir Oltean return -ENOMEM; 100256051948SVladimir Oltean 100356051948SVladimir Oltean ocelot->map = felix->info->map; 100456051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 100556051948SVladimir Oltean ocelot->num_stats = felix->info->num_stats; 100621ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 100707d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 100877043c37SXiaoliang Yang ocelot->vcap_pol.base = felix->info->vcap_pol_base; 100977043c37SXiaoliang Yang ocelot->vcap_pol.max = felix->info->vcap_pol_max; 101077043c37SXiaoliang Yang ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 101177043c37SXiaoliang Yang ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 101256051948SVladimir Oltean ocelot->ops = felix->info->ops; 1013cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 1014cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 1015f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 101656051948SVladimir Oltean 1017bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 1018bdeced75SVladimir Oltean GFP_KERNEL); 1019bdeced75SVladimir Oltean if (!port_phy_modes) 1020bdeced75SVladimir Oltean return -ENOMEM; 1021bdeced75SVladimir Oltean 1022bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 1023bdeced75SVladimir Oltean if (err) { 1024bdeced75SVladimir Oltean kfree(port_phy_modes); 1025bdeced75SVladimir Oltean return err; 1026bdeced75SVladimir Oltean } 1027bdeced75SVladimir Oltean 102856051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 102956051948SVladimir Oltean struct regmap *target; 103056051948SVladimir Oltean 103156051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 103256051948SVladimir Oltean continue; 103356051948SVladimir Oltean 1034b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 1035b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1036375e1314SVladimir Oltean res.start += felix->switch_base; 1037375e1314SVladimir Oltean res.end += felix->switch_base; 103856051948SVladimir Oltean 1039242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 104056051948SVladimir Oltean if (IS_ERR(target)) { 104156051948SVladimir Oltean dev_err(ocelot->dev, 104256051948SVladimir Oltean "Failed to map device memory space\n"); 1043bdeced75SVladimir Oltean kfree(port_phy_modes); 104456051948SVladimir Oltean return PTR_ERR(target); 104556051948SVladimir Oltean } 104656051948SVladimir Oltean 104756051948SVladimir Oltean ocelot->targets[i] = target; 104856051948SVladimir Oltean } 104956051948SVladimir Oltean 105056051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 105156051948SVladimir Oltean if (err) { 105256051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 1053bdeced75SVladimir Oltean kfree(port_phy_modes); 105456051948SVladimir Oltean return err; 105556051948SVladimir Oltean } 105656051948SVladimir Oltean 105756051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 105856051948SVladimir Oltean struct ocelot_port *ocelot_port; 105991c724cfSVladimir Oltean struct regmap *target; 106056051948SVladimir Oltean 106156051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 106256051948SVladimir Oltean sizeof(struct ocelot_port), 106356051948SVladimir Oltean GFP_KERNEL); 106456051948SVladimir Oltean if (!ocelot_port) { 106556051948SVladimir Oltean dev_err(ocelot->dev, 106656051948SVladimir Oltean "failed to allocate port memory\n"); 1067bdeced75SVladimir Oltean kfree(port_phy_modes); 106856051948SVladimir Oltean return -ENOMEM; 106956051948SVladimir Oltean } 107056051948SVladimir Oltean 1071b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1072b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1073375e1314SVladimir Oltean res.start += felix->switch_base; 1074375e1314SVladimir Oltean res.end += felix->switch_base; 107556051948SVladimir Oltean 1076242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 107791c724cfSVladimir Oltean if (IS_ERR(target)) { 107856051948SVladimir Oltean dev_err(ocelot->dev, 107991c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 108091c724cfSVladimir Oltean port); 1081bdeced75SVladimir Oltean kfree(port_phy_modes); 108291c724cfSVladimir Oltean return PTR_ERR(target); 108356051948SVladimir Oltean } 108456051948SVladimir Oltean 1085bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 108656051948SVladimir Oltean ocelot_port->ocelot = ocelot; 108791c724cfSVladimir Oltean ocelot_port->target = target; 108856051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 108956051948SVladimir Oltean } 109056051948SVladimir Oltean 1091bdeced75SVladimir Oltean kfree(port_phy_modes); 1092bdeced75SVladimir Oltean 1093bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1094bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1095bdeced75SVladimir Oltean if (err < 0) 1096bdeced75SVladimir Oltean return err; 1097bdeced75SVladimir Oltean } 1098bdeced75SVladimir Oltean 109956051948SVladimir Oltean return 0; 110056051948SVladimir Oltean } 110156051948SVladimir Oltean 11021328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 11031328a883SVladimir Oltean struct sk_buff *skb) 11041328a883SVladimir Oltean { 11051328a883SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 11061328a883SVladimir Oltean struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 11071328a883SVladimir Oltean struct sk_buff *skb_match = NULL, *skb_tmp; 11081328a883SVladimir Oltean unsigned long flags; 11091328a883SVladimir Oltean 11101328a883SVladimir Oltean if (!clone) 11111328a883SVladimir Oltean return; 11121328a883SVladimir Oltean 11131328a883SVladimir Oltean spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 11141328a883SVladimir Oltean 11151328a883SVladimir Oltean skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 11161328a883SVladimir Oltean if (skb != clone) 11171328a883SVladimir Oltean continue; 11181328a883SVladimir Oltean __skb_unlink(skb, &ocelot_port->tx_skbs); 11191328a883SVladimir Oltean skb_match = skb; 11201328a883SVladimir Oltean break; 11211328a883SVladimir Oltean } 11221328a883SVladimir Oltean 11231328a883SVladimir Oltean spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 11241328a883SVladimir Oltean 11251328a883SVladimir Oltean WARN_ONCE(!skb_match, 11261328a883SVladimir Oltean "Could not find skb clone in TX timestamping list\n"); 11271328a883SVladimir Oltean } 11281328a883SVladimir Oltean 112949f885b2SVladimir Oltean #define work_to_xmit_work(w) \ 113049f885b2SVladimir Oltean container_of((w), struct felix_deferred_xmit_work, work) 113149f885b2SVladimir Oltean 113249f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work) 113349f885b2SVladimir Oltean { 113449f885b2SVladimir Oltean struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 113549f885b2SVladimir Oltean struct dsa_switch *ds = xmit_work->dp->ds; 113649f885b2SVladimir Oltean struct sk_buff *skb = xmit_work->skb; 113749f885b2SVladimir Oltean u32 rew_op = ocelot_ptp_rew_op(skb); 113849f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 113949f885b2SVladimir Oltean int port = xmit_work->dp->index; 114049f885b2SVladimir Oltean int retries = 10; 114149f885b2SVladimir Oltean 114249f885b2SVladimir Oltean do { 114349f885b2SVladimir Oltean if (ocelot_can_inject(ocelot, 0)) 114449f885b2SVladimir Oltean break; 114549f885b2SVladimir Oltean 114649f885b2SVladimir Oltean cpu_relax(); 114749f885b2SVladimir Oltean } while (--retries); 114849f885b2SVladimir Oltean 114949f885b2SVladimir Oltean if (!retries) { 115049f885b2SVladimir Oltean dev_err(ocelot->dev, "port %d failed to inject skb\n", 115149f885b2SVladimir Oltean port); 11521328a883SVladimir Oltean ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 115349f885b2SVladimir Oltean kfree_skb(skb); 115449f885b2SVladimir Oltean return; 115549f885b2SVladimir Oltean } 115649f885b2SVladimir Oltean 115749f885b2SVladimir Oltean ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 115849f885b2SVladimir Oltean 115949f885b2SVladimir Oltean consume_skb(skb); 116049f885b2SVladimir Oltean kfree(xmit_work); 116149f885b2SVladimir Oltean } 116249f885b2SVladimir Oltean 116335d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds, 116435d97680SVladimir Oltean enum dsa_tag_protocol proto) 116549f885b2SVladimir Oltean { 116635d97680SVladimir Oltean struct ocelot_8021q_tagger_data *tagger_data; 116749f885b2SVladimir Oltean 116835d97680SVladimir Oltean switch (proto) { 116935d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 117035d97680SVladimir Oltean tagger_data = ocelot_8021q_tagger_data(ds); 117135d97680SVladimir Oltean tagger_data->xmit_work_fn = felix_port_deferred_xmit; 117249f885b2SVladimir Oltean return 0; 117335d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT: 117435d97680SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 117549f885b2SVladimir Oltean return 0; 117635d97680SVladimir Oltean default: 117735d97680SVladimir Oltean return -EPROTONOSUPPORT; 117849f885b2SVladimir Oltean } 117949f885b2SVladimir Oltean } 118049f885b2SVladimir Oltean 118156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with 118256051948SVladimir Oltean * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 118356051948SVladimir Oltean * us to allocate structures twice (leak memory) and map PCI memory twice 118456051948SVladimir Oltean * (which will not work). 118556051948SVladimir Oltean */ 118656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds) 118756051948SVladimir Oltean { 118856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 118956051948SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1190*2960bb14SVladimir Oltean struct dsa_port *dp; 1191*2960bb14SVladimir Oltean int err; 119256051948SVladimir Oltean 119356051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 119456051948SVladimir Oltean if (err) 119556051948SVladimir Oltean return err; 119656051948SVladimir Oltean 1197d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1198d1cc0e93SVladimir Oltean if (err) 11996b73b7c9SVladimir Oltean goto out_mdiobus_free; 1200d1cc0e93SVladimir Oltean 12012b49d128SYangbo Lu if (ocelot->ptp) { 12022ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 12032b49d128SYangbo Lu if (err) { 12042b49d128SYangbo Lu dev_err(ocelot->dev, 12052b49d128SYangbo Lu "Timestamp initialization failed\n"); 12062b49d128SYangbo Lu ocelot->ptp = 0; 12072b49d128SYangbo Lu } 12082b49d128SYangbo Lu } 120956051948SVladimir Oltean 1210*2960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 1211*2960bb14SVladimir Oltean ocelot_init_port(ocelot, dp->index); 1212bd2b3161SXiaoliang Yang 1213bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1214bd2b3161SXiaoliang Yang * bits of vlan tag. 1215bd2b3161SXiaoliang Yang */ 1216*2960bb14SVladimir Oltean felix_port_qos_map_init(ocelot, dp->index); 121756051948SVladimir Oltean } 121856051948SVladimir Oltean 1219f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1220f59fd9caSVladimir Oltean if (err) 12216b73b7c9SVladimir Oltean goto out_deinit_ports; 1222f59fd9caSVladimir Oltean 1223*2960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 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 */ 1227*2960bb14SVladimir Oltean felix_set_tag_protocol(ds, dp->index, 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: 1237*2960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 1238*2960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 12396b73b7c9SVladimir Oltean 12406b73b7c9SVladimir Oltean ocelot_deinit_timestamp(ocelot); 12416b73b7c9SVladimir Oltean ocelot_deinit(ocelot); 12426b73b7c9SVladimir Oltean 12436b73b7c9SVladimir Oltean out_mdiobus_free: 12446b73b7c9SVladimir Oltean if (felix->info->mdio_bus_free) 12456b73b7c9SVladimir Oltean felix->info->mdio_bus_free(ocelot); 12466b73b7c9SVladimir Oltean 12476b73b7c9SVladimir Oltean return err; 124856051948SVladimir Oltean } 124956051948SVladimir Oltean 125056051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 125156051948SVladimir Oltean { 125256051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1253bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1254*2960bb14SVladimir Oltean struct dsa_port *dp; 1255bdeced75SVladimir Oltean 1256*2960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 1257*2960bb14SVladimir Oltean felix_del_tag_protocol(ds, dp->index, felix->tag_proto); 12588d5f7954SVladimir Oltean break; 1259adb3dccfSVladimir Oltean } 1260adb3dccfSVladimir Oltean 1261*2960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 1262*2960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 1263d19741b0SVladimir Oltean 126449f885b2SVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 126549f885b2SVladimir Oltean ocelot_deinit_timestamp(ocelot); 126649f885b2SVladimir Oltean ocelot_deinit(ocelot); 126749f885b2SVladimir Oltean 1268d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1269d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 127056051948SVladimir Oltean } 127156051948SVladimir Oltean 1272c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1273c0bcf537SYangbo Lu struct ifreq *ifr) 1274c0bcf537SYangbo Lu { 1275c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1276c0bcf537SYangbo Lu 1277c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1278c0bcf537SYangbo Lu } 1279c0bcf537SYangbo Lu 1280c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1281c0bcf537SYangbo Lu struct ifreq *ifr) 1282c0bcf537SYangbo Lu { 1283c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1284c0bcf537SYangbo Lu 1285c0bcf537SYangbo Lu return ocelot_hwstamp_set(ocelot, port, ifr); 1286c0bcf537SYangbo Lu } 1287c0bcf537SYangbo Lu 12880a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type) 12890a6f17c6SVladimir Oltean { 12900a6f17c6SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12910a6f17c6SVladimir Oltean int err, grp = 0; 12920a6f17c6SVladimir Oltean 12930a6f17c6SVladimir Oltean if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 12940a6f17c6SVladimir Oltean return false; 12950a6f17c6SVladimir Oltean 12960a6f17c6SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 12970a6f17c6SVladimir Oltean return false; 12980a6f17c6SVladimir Oltean 12990a6f17c6SVladimir Oltean if (ptp_type == PTP_CLASS_NONE) 13000a6f17c6SVladimir Oltean return false; 13010a6f17c6SVladimir Oltean 13020a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 13030a6f17c6SVladimir Oltean struct sk_buff *skb; 13040a6f17c6SVladimir Oltean unsigned int type; 13050a6f17c6SVladimir Oltean 13060a6f17c6SVladimir Oltean err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 13070a6f17c6SVladimir Oltean if (err) 13080a6f17c6SVladimir Oltean goto out; 13090a6f17c6SVladimir Oltean 13100a6f17c6SVladimir Oltean /* We trap to the CPU port module all PTP frames, but 13110a6f17c6SVladimir Oltean * felix_rxtstamp() only gets called for event frames. 13120a6f17c6SVladimir Oltean * So we need to avoid sending duplicate general 13130a6f17c6SVladimir Oltean * message frames by running a second BPF classifier 13140a6f17c6SVladimir Oltean * here and dropping those. 13150a6f17c6SVladimir Oltean */ 13160a6f17c6SVladimir Oltean __skb_push(skb, ETH_HLEN); 13170a6f17c6SVladimir Oltean 13180a6f17c6SVladimir Oltean type = ptp_classify_raw(skb); 13190a6f17c6SVladimir Oltean 13200a6f17c6SVladimir Oltean __skb_pull(skb, ETH_HLEN); 13210a6f17c6SVladimir Oltean 13220a6f17c6SVladimir Oltean if (type == PTP_CLASS_NONE) { 13230a6f17c6SVladimir Oltean kfree_skb(skb); 13240a6f17c6SVladimir Oltean continue; 13250a6f17c6SVladimir Oltean } 13260a6f17c6SVladimir Oltean 13270a6f17c6SVladimir Oltean netif_rx(skb); 13280a6f17c6SVladimir Oltean } 13290a6f17c6SVladimir Oltean 13300a6f17c6SVladimir Oltean out: 13310a6f17c6SVladimir Oltean if (err < 0) 13320a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 13330a6f17c6SVladimir Oltean 13340a6f17c6SVladimir Oltean return true; 13350a6f17c6SVladimir Oltean } 13360a6f17c6SVladimir Oltean 1337c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1338c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1339c0bcf537SYangbo Lu { 134092f62485SVladimir Oltean u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1341c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1342c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1343c0bcf537SYangbo Lu struct timespec64 ts; 134492f62485SVladimir Oltean u32 tstamp_hi; 134592f62485SVladimir Oltean u64 tstamp; 1346c0bcf537SYangbo Lu 13470a6f17c6SVladimir Oltean /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 13480a6f17c6SVladimir Oltean * for RX timestamping. Then free it, and poll for its copy through 13490a6f17c6SVladimir Oltean * MMIO in the CPU port module, and inject that into the stack from 13500a6f17c6SVladimir Oltean * ocelot_xtr_poll(). 13510a6f17c6SVladimir Oltean */ 13520a6f17c6SVladimir Oltean if (felix_check_xtr_pkt(ocelot, type)) { 13530a6f17c6SVladimir Oltean kfree_skb(skb); 13540a6f17c6SVladimir Oltean return true; 13550a6f17c6SVladimir Oltean } 13560a6f17c6SVladimir Oltean 1357c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1358c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1359c0bcf537SYangbo Lu 1360c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1361c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1362c0bcf537SYangbo Lu tstamp_hi--; 1363c0bcf537SYangbo Lu 1364c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1365c0bcf537SYangbo Lu 1366c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1367c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1368c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1369c0bcf537SYangbo Lu return false; 1370c0bcf537SYangbo Lu } 1371c0bcf537SYangbo Lu 13725c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port, 13735c5416f5SYangbo Lu struct sk_buff *skb) 1374c0bcf537SYangbo Lu { 1375c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1376682eaad9SYangbo Lu struct sk_buff *clone = NULL; 1377c0bcf537SYangbo Lu 1378682eaad9SYangbo Lu if (!ocelot->ptp) 13795c5416f5SYangbo Lu return; 1380c0bcf537SYangbo Lu 138152849bcfSVladimir Oltean if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 138252849bcfSVladimir Oltean dev_err_ratelimited(ds->dev, 138352849bcfSVladimir Oltean "port %d delivering skb without TX timestamp\n", 138452849bcfSVladimir Oltean port); 1385682eaad9SYangbo Lu return; 138652849bcfSVladimir Oltean } 1387682eaad9SYangbo Lu 1388682eaad9SYangbo Lu if (clone) 1389c4b364ceSYangbo Lu OCELOT_SKB_CB(skb)->clone = clone; 13905c5416f5SYangbo Lu } 1391c0bcf537SYangbo Lu 13920b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 13930b912fc9SVladimir Oltean { 13940b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13950b912fc9SVladimir Oltean 13960b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 13970b912fc9SVladimir Oltean 13980b912fc9SVladimir Oltean return 0; 13990b912fc9SVladimir Oltean } 14000b912fc9SVladimir Oltean 14010b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 14020b912fc9SVladimir Oltean { 14030b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 14040b912fc9SVladimir Oltean 14050b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 14060b912fc9SVladimir Oltean } 14070b912fc9SVladimir Oltean 140807d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 140907d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 141007d985eeSVladimir Oltean { 141107d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 141207d985eeSVladimir Oltean 141307d985eeSVladimir Oltean return ocelot_cls_flower_replace(ocelot, port, cls, ingress); 141407d985eeSVladimir Oltean } 141507d985eeSVladimir Oltean 141607d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 141707d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 141807d985eeSVladimir Oltean { 141907d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 142007d985eeSVladimir Oltean 142107d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 142207d985eeSVladimir Oltean } 142307d985eeSVladimir Oltean 142407d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 142507d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 142607d985eeSVladimir Oltean { 142707d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 142807d985eeSVladimir Oltean 142907d985eeSVladimir Oltean return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 143007d985eeSVladimir Oltean } 143107d985eeSVladimir Oltean 1432fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1433fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1434fc411eaaSVladimir Oltean { 1435fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1436fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1437fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 14385f035af7SPo Liu .burst = policer->burst, 1439fc411eaaSVladimir Oltean }; 1440fc411eaaSVladimir Oltean 1441fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1442fc411eaaSVladimir Oltean } 1443fc411eaaSVladimir Oltean 1444fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1445fc411eaaSVladimir Oltean { 1446fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1447fc411eaaSVladimir Oltean 1448fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1449fc411eaaSVladimir Oltean } 1450fc411eaaSVladimir Oltean 1451de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1452de143c0eSXiaoliang Yang enum tc_setup_type type, 1453de143c0eSXiaoliang Yang void *type_data) 1454de143c0eSXiaoliang Yang { 1455de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1456de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1457de143c0eSXiaoliang Yang 1458de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1459de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1460de143c0eSXiaoliang Yang else 1461de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1462de143c0eSXiaoliang Yang } 1463de143c0eSXiaoliang Yang 1464f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1465f59fd9caSVladimir Oltean u16 pool_index, 1466f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1467f59fd9caSVladimir Oltean { 1468f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1469f59fd9caSVladimir Oltean 1470f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1471f59fd9caSVladimir Oltean } 1472f59fd9caSVladimir Oltean 1473f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1474f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1475f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1476f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1477f59fd9caSVladimir Oltean { 1478f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1479f59fd9caSVladimir Oltean 1480f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1481f59fd9caSVladimir Oltean threshold_type, extack); 1482f59fd9caSVladimir Oltean } 1483f59fd9caSVladimir Oltean 1484f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1485f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1486f59fd9caSVladimir Oltean u32 *p_threshold) 1487f59fd9caSVladimir Oltean { 1488f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1489f59fd9caSVladimir Oltean 1490f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1491f59fd9caSVladimir Oltean p_threshold); 1492f59fd9caSVladimir Oltean } 1493f59fd9caSVladimir Oltean 1494f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1495f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1496f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1497f59fd9caSVladimir Oltean { 1498f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1499f59fd9caSVladimir Oltean 1500f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1501f59fd9caSVladimir Oltean threshold, extack); 1502f59fd9caSVladimir Oltean } 1503f59fd9caSVladimir Oltean 1504f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1505f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1506f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1507f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1508f59fd9caSVladimir Oltean { 1509f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1510f59fd9caSVladimir Oltean 1511f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1512f59fd9caSVladimir Oltean pool_type, p_pool_index, 1513f59fd9caSVladimir Oltean p_threshold); 1514f59fd9caSVladimir Oltean } 1515f59fd9caSVladimir Oltean 1516f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1517f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1518f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1519f59fd9caSVladimir Oltean u16 pool_index, u32 threshold, 1520f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1521f59fd9caSVladimir Oltean { 1522f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1523f59fd9caSVladimir Oltean 1524f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1525f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1526f59fd9caSVladimir Oltean extack); 1527f59fd9caSVladimir Oltean } 1528f59fd9caSVladimir Oltean 1529f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1530f59fd9caSVladimir Oltean unsigned int sb_index) 1531f59fd9caSVladimir Oltean { 1532f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1533f59fd9caSVladimir Oltean 1534f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1535f59fd9caSVladimir Oltean } 1536f59fd9caSVladimir Oltean 1537f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1538f59fd9caSVladimir Oltean unsigned int sb_index) 1539f59fd9caSVladimir Oltean { 1540f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1541f59fd9caSVladimir Oltean 1542f59fd9caSVladimir Oltean return ocelot_sb_occ_max_clear(ocelot, sb_index); 1543f59fd9caSVladimir Oltean } 1544f59fd9caSVladimir Oltean 1545f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1546f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1547f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1548f59fd9caSVladimir Oltean { 1549f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1550f59fd9caSVladimir Oltean 1551f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1552f59fd9caSVladimir Oltean p_cur, p_max); 1553f59fd9caSVladimir Oltean } 1554f59fd9caSVladimir Oltean 1555f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1556f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1557f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 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_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1563f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1564f59fd9caSVladimir Oltean } 1565f59fd9caSVladimir Oltean 1566a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port, 1567a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1568a026c50bSHoratiu Vultur { 1569a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1570a026c50bSHoratiu Vultur 1571a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1572a026c50bSHoratiu Vultur } 1573a026c50bSHoratiu Vultur 1574a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port, 1575a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1576a026c50bSHoratiu Vultur { 1577a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1578a026c50bSHoratiu Vultur 1579a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1580a026c50bSHoratiu Vultur } 1581a026c50bSHoratiu Vultur 1582a026c50bSHoratiu Vultur static int 1583a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1584a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1585a026c50bSHoratiu Vultur { 1586a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1587a026c50bSHoratiu Vultur 1588a026c50bSHoratiu Vultur return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1589a026c50bSHoratiu Vultur } 1590a026c50bSHoratiu Vultur 1591a026c50bSHoratiu Vultur static int 1592a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1593a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1594a026c50bSHoratiu Vultur { 1595a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1596a026c50bSHoratiu Vultur 1597a026c50bSHoratiu Vultur return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1598a026c50bSHoratiu Vultur } 1599a026c50bSHoratiu Vultur 1600375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 160156051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1602adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 160335d97680SVladimir Oltean .connect_tag_protocol = felix_connect_tag_protocol, 160456051948SVladimir Oltean .setup = felix_setup, 160556051948SVladimir Oltean .teardown = felix_teardown, 160656051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 160756051948SVladimir Oltean .get_strings = felix_get_strings, 160856051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 160956051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 161056051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 1611bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1612bdeced75SVladimir Oltean .phylink_mac_config = felix_phylink_mac_config, 1613bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1614bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 16155cad43a5SVladimir Oltean .port_fast_age = felix_port_fast_age, 161656051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 161756051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 161856051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1619209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1620209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 1621421741eaSVladimir Oltean .port_pre_bridge_flags = felix_pre_bridge_flags, 1622421741eaSVladimir Oltean .port_bridge_flags = felix_bridge_flags, 162356051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 162456051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 16258fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 16268fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 16278fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 162856051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 162956051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 163056051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 163156051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1632c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1633c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1634c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1635c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 16360b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 16370b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1638fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1639fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 164007d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 164107d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 164207d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1643de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1644f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1645f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1646f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1647f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1648f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1649f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1650f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1651f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1652f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1653f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1654a026c50bSHoratiu Vultur .port_mrp_add = felix_mrp_add, 1655a026c50bSHoratiu Vultur .port_mrp_del = felix_mrp_del, 1656a026c50bSHoratiu Vultur .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1657a026c50bSHoratiu Vultur .port_mrp_del_ring_role = felix_mrp_del_ring_role, 16585da11eb4SVladimir Oltean .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 16595da11eb4SVladimir Oltean .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 166056051948SVladimir Oltean }; 1661319e4dd1SVladimir Oltean 1662319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1663319e4dd1SVladimir Oltean { 1664319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1665319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1666319e4dd1SVladimir Oltean 1667319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1668319e4dd1SVladimir Oltean return NULL; 1669319e4dd1SVladimir Oltean 1670319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1671319e4dd1SVladimir Oltean } 1672319e4dd1SVladimir Oltean 1673319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1674319e4dd1SVladimir Oltean { 1675319e4dd1SVladimir Oltean struct dsa_port *dp; 1676319e4dd1SVladimir Oltean 1677319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1678319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1679319e4dd1SVladimir Oltean return -EINVAL; 1680319e4dd1SVladimir Oltean 1681319e4dd1SVladimir Oltean return dp->index; 1682319e4dd1SVladimir Oltean } 1683