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; 2812960bb14SVladimir Oltean struct dsa_port *dp; 2822960bb14SVladimir 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 2942960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 2952960bb14SVladimir Oltean cpu = dp->index; 2968d5f7954SVladimir Oltean break; 297c8c0ba4fSVladimir Oltean } 2988d5f7954SVladimir Oltean 299*d78637a8SVladimir Oltean /* We are sure that "cpu" was found, otherwise 300*d78637a8SVladimir Oltean * dsa_tree_setup_default_cpu() would have failed earlier. 301*d78637a8SVladimir Oltean */ 302c8c0ba4fSVladimir Oltean 303c8c0ba4fSVladimir Oltean tagging_rule->key_type = OCELOT_VCAP_KEY_ETYPE; 304c8c0ba4fSVladimir Oltean *(__be16 *)tagging_rule->key.etype.etype.value = htons(ETH_P_1588); 305c8c0ba4fSVladimir Oltean *(__be16 *)tagging_rule->key.etype.etype.mask = htons(0xffff); 306c8c0ba4fSVladimir Oltean tagging_rule->ingress_port_mask = user_ports; 307c8c0ba4fSVladimir Oltean tagging_rule->prio = 1; 308c518afecSVladimir Oltean tagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_PTP_MMIO(ocelot); 309c8c0ba4fSVladimir Oltean tagging_rule->id.tc_offload = false; 310c8c0ba4fSVladimir Oltean tagging_rule->block_id = VCAP_IS1; 311c8c0ba4fSVladimir Oltean tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 312c8c0ba4fSVladimir Oltean tagging_rule->lookup = 0; 313c8c0ba4fSVladimir Oltean tagging_rule->action.pag_override_mask = 0xff; 314c8c0ba4fSVladimir Oltean tagging_rule->action.pag_val = ocelot->num_phys_ports; 315c8c0ba4fSVladimir Oltean 316c8c0ba4fSVladimir Oltean ret = ocelot_vcap_filter_add(ocelot, tagging_rule, NULL); 317c8c0ba4fSVladimir Oltean if (ret) { 318c8c0ba4fSVladimir Oltean kfree(tagging_rule); 319c8c0ba4fSVladimir Oltean kfree(redirect_rule); 320c8c0ba4fSVladimir Oltean return ret; 321c8c0ba4fSVladimir Oltean } 322c8c0ba4fSVladimir Oltean 323c8c0ba4fSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 324c8c0ba4fSVladimir Oltean redirect_rule->ingress_port_mask = user_ports; 325c8c0ba4fSVladimir Oltean redirect_rule->pag = ocelot->num_phys_ports; 326c8c0ba4fSVladimir Oltean redirect_rule->prio = 1; 327c518afecSVladimir Oltean redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_PTP_MMIO(ocelot); 328c8c0ba4fSVladimir Oltean redirect_rule->id.tc_offload = false; 329c8c0ba4fSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 330c8c0ba4fSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 331c8c0ba4fSVladimir Oltean redirect_rule->lookup = 0; 332c8c0ba4fSVladimir Oltean redirect_rule->action.cpu_copy_ena = true; 333c8c0ba4fSVladimir Oltean if (felix->info->quirk_no_xtr_irq) { 334c8c0ba4fSVladimir Oltean /* Redirect to the tag_8021q CPU but also copy PTP packets to 335c8c0ba4fSVladimir Oltean * the CPU port module 336c8c0ba4fSVladimir Oltean */ 337c8c0ba4fSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 3388d5f7954SVladimir Oltean redirect_rule->action.port_mask = BIT(cpu); 339c8c0ba4fSVladimir Oltean } else { 340c8c0ba4fSVladimir Oltean /* Trap PTP packets only to the CPU port module (which is 341c8c0ba4fSVladimir Oltean * redirected to the NPI port) 342c8c0ba4fSVladimir Oltean */ 343c8c0ba4fSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 344c8c0ba4fSVladimir Oltean redirect_rule->action.port_mask = 0; 345c8c0ba4fSVladimir Oltean } 346c8c0ba4fSVladimir Oltean 347c8c0ba4fSVladimir Oltean ret = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 348c8c0ba4fSVladimir Oltean if (ret) { 349c8c0ba4fSVladimir Oltean ocelot_vcap_filter_del(ocelot, tagging_rule); 350c8c0ba4fSVladimir Oltean kfree(redirect_rule); 351c8c0ba4fSVladimir Oltean return ret; 352c8c0ba4fSVladimir Oltean } 353c8c0ba4fSVladimir Oltean 3540a6f17c6SVladimir Oltean /* The ownership of the CPU port module's queues might have just been 3550a6f17c6SVladimir Oltean * transferred to the tag_8021q tagger from the NPI-based tagger. 3560a6f17c6SVladimir Oltean * So there might still be all sorts of crap in the queues. On the 3570a6f17c6SVladimir Oltean * other hand, the MMIO-based matching of PTP frames is very brittle, 3580a6f17c6SVladimir Oltean * so we need to be careful that there are no extra frames to be 3590a6f17c6SVladimir Oltean * dequeued over MMIO, since we would never know to discard them. 3600a6f17c6SVladimir Oltean */ 3610a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 3620a6f17c6SVladimir Oltean 363c8c0ba4fSVladimir Oltean return 0; 364c8c0ba4fSVladimir Oltean } 365c8c0ba4fSVladimir Oltean 366c8c0ba4fSVladimir Oltean static int felix_teardown_mmio_filtering(struct felix *felix) 367c8c0ba4fSVladimir Oltean { 368c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *tagging_rule, *redirect_rule; 369c8c0ba4fSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 370c8c0ba4fSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 371c8c0ba4fSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 372c8c0ba4fSVladimir Oltean int err; 373c8c0ba4fSVladimir Oltean 374c8c0ba4fSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 375c8c0ba4fSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 376c8c0ba4fSVladimir Oltean 377c8c0ba4fSVladimir Oltean tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 378c8c0ba4fSVladimir Oltean ocelot->num_phys_ports, 379c8c0ba4fSVladimir Oltean false); 380c8c0ba4fSVladimir Oltean if (!tagging_rule) 381c8c0ba4fSVladimir Oltean return -ENOENT; 382c8c0ba4fSVladimir Oltean 383c8c0ba4fSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, tagging_rule); 384c8c0ba4fSVladimir Oltean if (err) 385c8c0ba4fSVladimir Oltean return err; 386c8c0ba4fSVladimir Oltean 387c8c0ba4fSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 388c8c0ba4fSVladimir Oltean ocelot->num_phys_ports, 389c8c0ba4fSVladimir Oltean false); 390c8c0ba4fSVladimir Oltean if (!redirect_rule) 391c8c0ba4fSVladimir Oltean return -ENOENT; 392c8c0ba4fSVladimir Oltean 393c8c0ba4fSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 394c8c0ba4fSVladimir Oltean } 395c8c0ba4fSVladimir Oltean 396e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu) 397e21268efSVladimir Oltean { 398e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 399e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 400e21268efSVladimir Oltean unsigned long cpu_flood; 4012960bb14SVladimir Oltean struct dsa_port *dp; 4022960bb14SVladimir Oltean int err; 403e21268efSVladimir Oltean 404e21268efSVladimir Oltean felix_8021q_cpu_port_init(ocelot, cpu); 405e21268efSVladimir Oltean 4062960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 407e21268efSVladimir Oltean /* This overwrites ocelot_init(): 408e21268efSVladimir Oltean * Do not forward BPDU frames to the CPU port module, 409e21268efSVladimir Oltean * for 2 reasons: 410e21268efSVladimir Oltean * - When these packets are injected from the tag_8021q 411e21268efSVladimir Oltean * CPU port, we want them to go out, not loop back 412e21268efSVladimir Oltean * into the system. 413e21268efSVladimir Oltean * - STP traffic ingressing on a user port should go to 414e21268efSVladimir Oltean * the tag_8021q CPU port, not to the hardware CPU 415e21268efSVladimir Oltean * port module. 416e21268efSVladimir Oltean */ 417e21268efSVladimir Oltean ocelot_write_gix(ocelot, 418e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 4192960bb14SVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, dp->index); 420e21268efSVladimir Oltean } 421e21268efSVladimir Oltean 422c8c0ba4fSVladimir Oltean /* In tag_8021q mode, the CPU port module is unused, except for PTP 423c8c0ba4fSVladimir Oltean * frames. So we want to disable flooding of any kind to the CPU port 424c8c0ba4fSVladimir Oltean * module, since packets going there will end in a black hole. 425e21268efSVladimir Oltean */ 426e21268efSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 427e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC); 428e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC); 429b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC); 430e21268efSVladimir Oltean 4315da11eb4SVladimir Oltean err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD)); 432e21268efSVladimir Oltean if (err) 433d7b1fd52SVladimir Oltean return err; 434d7b1fd52SVladimir Oltean 435328621f6SVladimir Oltean err = felix_setup_mmio_filtering(felix); 436d7b1fd52SVladimir Oltean if (err) 437d7b1fd52SVladimir Oltean goto out_tag_8021q_unregister; 438e21268efSVladimir Oltean 439e21268efSVladimir Oltean return 0; 440e21268efSVladimir Oltean 441d7b1fd52SVladimir Oltean out_tag_8021q_unregister: 442d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 443e21268efSVladimir Oltean return err; 444e21268efSVladimir Oltean } 445e21268efSVladimir Oltean 446e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu) 447e21268efSVladimir Oltean { 448e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 449e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 4502960bb14SVladimir Oltean struct dsa_port *dp; 4512960bb14SVladimir Oltean int err; 452e21268efSVladimir Oltean 453c8c0ba4fSVladimir Oltean err = felix_teardown_mmio_filtering(felix); 454c8c0ba4fSVladimir Oltean if (err) 455c8c0ba4fSVladimir Oltean dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d", 456c8c0ba4fSVladimir Oltean err); 457c8c0ba4fSVladimir Oltean 458d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 459e21268efSVladimir Oltean 4602960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 461e21268efSVladimir Oltean /* Restore the logic from ocelot_init: 462e21268efSVladimir Oltean * do not forward BPDU frames to the front ports. 463e21268efSVladimir Oltean */ 464e21268efSVladimir Oltean ocelot_write_gix(ocelot, 465e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 466e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, 4672960bb14SVladimir Oltean dp->index); 468e21268efSVladimir Oltean } 469e21268efSVladimir Oltean 470e21268efSVladimir Oltean felix_8021q_cpu_port_deinit(ocelot, cpu); 471e21268efSVladimir Oltean } 472e21268efSVladimir Oltean 473adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This 474adb3dccfSVladimir Oltean * is the mode through which frames can be injected from and extracted to an 475adb3dccfSVladimir Oltean * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 476adb3dccfSVladimir Oltean * running Linux, and this forms a DSA setup together with the enetc or fman 477adb3dccfSVladimir Oltean * DSA master. 478adb3dccfSVladimir Oltean */ 479adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port) 480adb3dccfSVladimir Oltean { 481adb3dccfSVladimir Oltean ocelot->npi = port; 482adb3dccfSVladimir Oltean 483adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 484adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 485adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 486adb3dccfSVladimir Oltean 487adb3dccfSVladimir Oltean /* NPI port Injection/Extraction configuration */ 488adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 489adb3dccfSVladimir Oltean ocelot->npi_xtr_prefix); 490adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 491adb3dccfSVladimir Oltean ocelot->npi_inj_prefix); 492adb3dccfSVladimir Oltean 493adb3dccfSVladimir Oltean /* Disable transmission of pause frames */ 494adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 495adb3dccfSVladimir Oltean } 496adb3dccfSVladimir Oltean 497adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 498adb3dccfSVladimir Oltean { 499adb3dccfSVladimir Oltean /* Restore hardware defaults */ 500adb3dccfSVladimir Oltean int unused_port = ocelot->num_phys_ports + 2; 501adb3dccfSVladimir Oltean 502adb3dccfSVladimir Oltean ocelot->npi = -1; 503adb3dccfSVladimir Oltean 504adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 505adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 506adb3dccfSVladimir Oltean 507adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 508adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 509adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 510adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 511adb3dccfSVladimir Oltean 512adb3dccfSVladimir Oltean /* Enable transmission of pause frames */ 513adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 514adb3dccfSVladimir Oltean } 515adb3dccfSVladimir Oltean 516adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu) 517adb3dccfSVladimir Oltean { 518adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 519adb3dccfSVladimir Oltean unsigned long cpu_flood; 520adb3dccfSVladimir Oltean 521adb3dccfSVladimir Oltean felix_npi_port_init(ocelot, cpu); 522adb3dccfSVladimir Oltean 523adb3dccfSVladimir Oltean /* Include the CPU port module (and indirectly, the NPI port) 524adb3dccfSVladimir Oltean * in the forwarding mask for unknown unicast - the hardware 525adb3dccfSVladimir Oltean * default value for ANA_FLOODING_FLD_UNICAST excludes 526adb3dccfSVladimir Oltean * BIT(ocelot->num_phys_ports), and so does ocelot_init, 527adb3dccfSVladimir Oltean * since Ocelot relies on whitelisting MAC addresses towards 528adb3dccfSVladimir Oltean * PGID_CPU. 529adb3dccfSVladimir Oltean * We do this because DSA does not yet perform RX filtering, 530adb3dccfSVladimir Oltean * and the NPI port does not perform source address learning, 531adb3dccfSVladimir Oltean * so traffic sent to Linux is effectively unknown from the 532adb3dccfSVladimir Oltean * switch's perspective. 533adb3dccfSVladimir Oltean */ 534adb3dccfSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 535adb3dccfSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC); 5366edb9e8dSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC); 537b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC); 538adb3dccfSVladimir Oltean 539adb3dccfSVladimir Oltean return 0; 540adb3dccfSVladimir Oltean } 541adb3dccfSVladimir Oltean 542adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu) 543adb3dccfSVladimir Oltean { 544adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 545adb3dccfSVladimir Oltean 546adb3dccfSVladimir Oltean felix_npi_port_deinit(ocelot, cpu); 547adb3dccfSVladimir Oltean } 548adb3dccfSVladimir Oltean 549adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu, 550adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 551adb3dccfSVladimir Oltean { 552adb3dccfSVladimir Oltean int err; 553adb3dccfSVladimir Oltean 554adb3dccfSVladimir Oltean switch (proto) { 5557c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 556adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 557adb3dccfSVladimir Oltean err = felix_setup_tag_npi(ds, cpu); 558adb3dccfSVladimir Oltean break; 559e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 560e21268efSVladimir Oltean err = felix_setup_tag_8021q(ds, cpu); 561e21268efSVladimir Oltean break; 562adb3dccfSVladimir Oltean default: 563adb3dccfSVladimir Oltean err = -EPROTONOSUPPORT; 564adb3dccfSVladimir Oltean } 565adb3dccfSVladimir Oltean 566adb3dccfSVladimir Oltean return err; 567adb3dccfSVladimir Oltean } 568adb3dccfSVladimir Oltean 569adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu, 570adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 571adb3dccfSVladimir Oltean { 572adb3dccfSVladimir Oltean switch (proto) { 5737c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 574adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 575adb3dccfSVladimir Oltean felix_teardown_tag_npi(ds, cpu); 576adb3dccfSVladimir Oltean break; 577e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 578e21268efSVladimir Oltean felix_teardown_tag_8021q(ds, cpu); 579e21268efSVladimir Oltean break; 580adb3dccfSVladimir Oltean default: 581adb3dccfSVladimir Oltean break; 582adb3dccfSVladimir Oltean } 583adb3dccfSVladimir Oltean } 584adb3dccfSVladimir Oltean 585e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the 586e21268efSVladimir Oltean * tag_8021q setup can fail, the NPI setup can't. So either the change is made, 587e21268efSVladimir Oltean * or the restoration is guaranteed to work. 588e21268efSVladimir Oltean */ 589adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu, 590adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 591adb3dccfSVladimir Oltean { 592adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 593adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 594adb3dccfSVladimir Oltean enum dsa_tag_protocol old_proto = felix->tag_proto; 595adb3dccfSVladimir Oltean int err; 596adb3dccfSVladimir Oltean 5977c4bb540SVladimir Oltean if (proto != DSA_TAG_PROTO_SEVILLE && 5987c4bb540SVladimir Oltean proto != DSA_TAG_PROTO_OCELOT && 599e21268efSVladimir Oltean proto != DSA_TAG_PROTO_OCELOT_8021Q) 600adb3dccfSVladimir Oltean return -EPROTONOSUPPORT; 601adb3dccfSVladimir Oltean 602adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, cpu, old_proto); 603adb3dccfSVladimir Oltean 604adb3dccfSVladimir Oltean err = felix_set_tag_protocol(ds, cpu, proto); 605adb3dccfSVladimir Oltean if (err) { 606adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, cpu, old_proto); 607adb3dccfSVladimir Oltean return err; 608adb3dccfSVladimir Oltean } 609adb3dccfSVladimir Oltean 610adb3dccfSVladimir Oltean felix->tag_proto = proto; 611adb3dccfSVladimir Oltean 612adb3dccfSVladimir Oltean return 0; 613adb3dccfSVladimir Oltean } 614adb3dccfSVladimir Oltean 61556051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 6164d776482SFlorian Fainelli int port, 6174d776482SFlorian Fainelli enum dsa_tag_protocol mp) 61856051948SVladimir Oltean { 619adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 620adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 621adb3dccfSVladimir Oltean 622adb3dccfSVladimir Oltean return felix->tag_proto; 62356051948SVladimir Oltean } 62456051948SVladimir Oltean 62556051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds, 62656051948SVladimir Oltean unsigned int ageing_time) 62756051948SVladimir Oltean { 62856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 62956051948SVladimir Oltean 63056051948SVladimir Oltean ocelot_set_ageing_time(ocelot, ageing_time); 63156051948SVladimir Oltean 63256051948SVladimir Oltean return 0; 63356051948SVladimir Oltean } 63456051948SVladimir Oltean 6355cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port) 6365cad43a5SVladimir Oltean { 6375cad43a5SVladimir Oltean struct ocelot *ocelot = ds->priv; 6385cad43a5SVladimir Oltean int err; 6395cad43a5SVladimir Oltean 6405cad43a5SVladimir Oltean err = ocelot_mact_flush(ocelot, port); 6415cad43a5SVladimir Oltean if (err) 6425cad43a5SVladimir Oltean dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n", 6435cad43a5SVladimir Oltean port, ERR_PTR(err)); 6445cad43a5SVladimir Oltean } 6455cad43a5SVladimir Oltean 64656051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port, 64756051948SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 64856051948SVladimir Oltean { 64956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 65056051948SVladimir Oltean 65156051948SVladimir Oltean return ocelot_fdb_dump(ocelot, port, cb, data); 65256051948SVladimir Oltean } 65356051948SVladimir Oltean 65456051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port, 65556051948SVladimir Oltean const unsigned char *addr, u16 vid) 65656051948SVladimir Oltean { 65756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 65856051948SVladimir Oltean 65987b0f983SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid); 66056051948SVladimir Oltean } 66156051948SVladimir Oltean 66256051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port, 66356051948SVladimir Oltean const unsigned char *addr, u16 vid) 66456051948SVladimir Oltean { 66556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 66656051948SVladimir Oltean 66756051948SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid); 66856051948SVladimir Oltean } 66956051948SVladimir Oltean 670a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 671209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 672209edf95SVladimir Oltean { 673209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 674209edf95SVladimir Oltean 675a52b2da7SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb); 676209edf95SVladimir Oltean } 677209edf95SVladimir Oltean 678209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port, 679209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 680209edf95SVladimir Oltean { 681209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 682209edf95SVladimir Oltean 683209edf95SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb); 684209edf95SVladimir Oltean } 685209edf95SVladimir Oltean 68656051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 68756051948SVladimir Oltean u8 state) 68856051948SVladimir Oltean { 68956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 69056051948SVladimir Oltean 69156051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 69256051948SVladimir Oltean } 69356051948SVladimir Oltean 694421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 695421741eaSVladimir Oltean struct switchdev_brport_flags val, 696421741eaSVladimir Oltean struct netlink_ext_ack *extack) 697421741eaSVladimir Oltean { 698421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 699421741eaSVladimir Oltean 700421741eaSVladimir Oltean return ocelot_port_pre_bridge_flags(ocelot, port, val); 701421741eaSVladimir Oltean } 702421741eaSVladimir Oltean 703421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port, 704421741eaSVladimir Oltean struct switchdev_brport_flags val, 705421741eaSVladimir Oltean struct netlink_ext_ack *extack) 706421741eaSVladimir Oltean { 707421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 708421741eaSVladimir Oltean 709421741eaSVladimir Oltean ocelot_port_bridge_flags(ocelot, port, val); 710421741eaSVladimir Oltean 711421741eaSVladimir Oltean return 0; 712421741eaSVladimir Oltean } 713421741eaSVladimir Oltean 71456051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 715b079922bSVladimir Oltean struct dsa_bridge bridge, bool *tx_fwd_offload) 71656051948SVladimir Oltean { 71756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 71856051948SVladimir Oltean 719d3eed0e5SVladimir Oltean ocelot_port_bridge_join(ocelot, port, bridge.dev); 720e4bd44e8SVladimir Oltean 721e4bd44e8SVladimir Oltean return 0; 72256051948SVladimir Oltean } 72356051948SVladimir Oltean 72456051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 725d3eed0e5SVladimir Oltean struct dsa_bridge bridge) 72656051948SVladimir Oltean { 72756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 72856051948SVladimir Oltean 729d3eed0e5SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, bridge.dev); 73056051948SVladimir Oltean } 73156051948SVladimir Oltean 7328fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 7338fe6832eSVladimir Oltean struct net_device *bond, 7348fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 7358fe6832eSVladimir Oltean { 7368fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7378fe6832eSVladimir Oltean 7388fe6832eSVladimir Oltean return ocelot_port_lag_join(ocelot, port, bond, info); 7398fe6832eSVladimir Oltean } 7408fe6832eSVladimir Oltean 7418fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 7428fe6832eSVladimir Oltean struct net_device *bond) 7438fe6832eSVladimir Oltean { 7448fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7458fe6832eSVladimir Oltean 7468fe6832eSVladimir Oltean ocelot_port_lag_leave(ocelot, port, bond); 7478fe6832eSVladimir Oltean 7488fe6832eSVladimir Oltean return 0; 7498fe6832eSVladimir Oltean } 7508fe6832eSVladimir Oltean 7518fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 7528fe6832eSVladimir Oltean { 7538fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 7548fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7558fe6832eSVladimir Oltean 7568fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 7578fe6832eSVladimir Oltean 7588fe6832eSVladimir Oltean return 0; 7598fe6832eSVladimir Oltean } 7608fe6832eSVladimir Oltean 76156051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 76201af940eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 76301af940eSVladimir Oltean struct netlink_ext_ack *extack) 76456051948SVladimir Oltean { 7652f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 766b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 7672f0402feSVladimir Oltean 7689a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 7699a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 7709a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 7719a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 7729a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 7739a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 7749a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 7759a720680SVladimir Oltean */ 7769a720680SVladimir Oltean if (port == ocelot->npi) 7779a720680SVladimir Oltean return 0; 7789a720680SVladimir Oltean 779b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 7802f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 78101af940eSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED, 78201af940eSVladimir Oltean extack); 78356051948SVladimir Oltean } 78456051948SVladimir Oltean 78589153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 78689153ed6SVladimir Oltean struct netlink_ext_ack *extack) 78756051948SVladimir Oltean { 78856051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 78956051948SVladimir Oltean 7903b95d1b2SVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 79156051948SVladimir Oltean } 79256051948SVladimir Oltean 7931958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 79431046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 79531046a5fSVladimir Oltean struct netlink_ext_ack *extack) 79656051948SVladimir Oltean { 79756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 798183be6f9SVladimir Oltean u16 flags = vlan->flags; 7991958d581SVladimir Oltean int err; 80056051948SVladimir Oltean 80101af940eSVladimir Oltean err = felix_vlan_prepare(ds, port, vlan, extack); 8021958d581SVladimir Oltean if (err) 8031958d581SVladimir Oltean return err; 8041958d581SVladimir Oltean 8051958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 806183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 807183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 80856051948SVladimir Oltean } 80956051948SVladimir Oltean 81056051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 81156051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 81256051948SVladimir Oltean { 81356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 81456051948SVladimir Oltean 815b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 81656051948SVladimir Oltean } 81756051948SVladimir Oltean 818bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port, 819bdeced75SVladimir Oltean unsigned long *supported, 820bdeced75SVladimir Oltean struct phylink_link_state *state) 821bdeced75SVladimir Oltean { 822bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 823375e1314SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 824bdeced75SVladimir Oltean 825375e1314SVladimir Oltean if (felix->info->phylink_validate) 826375e1314SVladimir Oltean felix->info->phylink_validate(ocelot, port, supported, state); 827bdeced75SVladimir Oltean } 828bdeced75SVladimir Oltean 829bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port, 830bdeced75SVladimir Oltean unsigned int link_an_mode, 831bdeced75SVladimir Oltean const struct phylink_link_state *state) 832bdeced75SVladimir Oltean { 833bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 834bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 835588d0550SIoana Ciornei struct dsa_port *dp = dsa_to_port(ds, port); 836bdeced75SVladimir Oltean 83749af6a76SColin Foster if (felix->pcs && felix->pcs[port]) 838e7026f15SColin Foster phylink_set_pcs(dp->pl, felix->pcs[port]); 839bdeced75SVladimir Oltean } 840bdeced75SVladimir Oltean 841bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 842bdeced75SVladimir Oltean unsigned int link_an_mode, 843bdeced75SVladimir Oltean phy_interface_t interface) 844bdeced75SVladimir Oltean { 845bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 846bdeced75SVladimir Oltean 847e6e12df6SVladimir Oltean ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 848e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 849bdeced75SVladimir Oltean } 850bdeced75SVladimir Oltean 851bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 852bdeced75SVladimir Oltean unsigned int link_an_mode, 853bdeced75SVladimir Oltean phy_interface_t interface, 8545b502a7bSRussell King struct phy_device *phydev, 8555b502a7bSRussell King int speed, int duplex, 8565b502a7bSRussell King bool tx_pause, bool rx_pause) 857bdeced75SVladimir Oltean { 858bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 8597e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 860bdeced75SVladimir Oltean 861e6e12df6SVladimir Oltean ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 862e6e12df6SVladimir Oltean interface, speed, duplex, tx_pause, rx_pause, 863e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 8647e14a2dcSVladimir Oltean 8657e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 8667e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 867bdeced75SVladimir Oltean } 868bdeced75SVladimir Oltean 869bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 870bd2b3161SXiaoliang Yang { 871bd2b3161SXiaoliang Yang int i; 872bd2b3161SXiaoliang Yang 873bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 874bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 875bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 876bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 877bd2b3161SXiaoliang Yang port); 878bd2b3161SXiaoliang Yang 87970d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 880bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 881bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 882bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 883bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 884bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 885bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 886bd2b3161SXiaoliang Yang port, i); 887bd2b3161SXiaoliang Yang } 888bd2b3161SXiaoliang Yang } 889bd2b3161SXiaoliang Yang 89056051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 89156051948SVladimir Oltean u32 stringset, u8 *data) 89256051948SVladimir Oltean { 89356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 89456051948SVladimir Oltean 89556051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 89656051948SVladimir Oltean } 89756051948SVladimir Oltean 89856051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 89956051948SVladimir Oltean { 90056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 90156051948SVladimir Oltean 90256051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 90356051948SVladimir Oltean } 90456051948SVladimir Oltean 90556051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 90656051948SVladimir Oltean { 90756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 90856051948SVladimir Oltean 90956051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 91056051948SVladimir Oltean } 91156051948SVladimir Oltean 91256051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 91356051948SVladimir Oltean struct ethtool_ts_info *info) 91456051948SVladimir Oltean { 91556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 91656051948SVladimir Oltean 91756051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 91856051948SVladimir Oltean } 91956051948SVladimir Oltean 920bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 921bdeced75SVladimir Oltean struct device_node *ports_node, 922bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 923bdeced75SVladimir Oltean { 924bdeced75SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 925bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 926bdeced75SVladimir Oltean struct device_node *child; 927bdeced75SVladimir Oltean 92837fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 929bdeced75SVladimir Oltean phy_interface_t phy_mode; 930bdeced75SVladimir Oltean u32 port; 931bdeced75SVladimir Oltean int err; 932bdeced75SVladimir Oltean 933bdeced75SVladimir Oltean /* Get switch port number from DT */ 934bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 935bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 936bdeced75SVladimir Oltean "(property \"reg\")\n"); 937bdeced75SVladimir Oltean of_node_put(child); 938bdeced75SVladimir Oltean return -ENODEV; 939bdeced75SVladimir Oltean } 940bdeced75SVladimir Oltean 941bdeced75SVladimir Oltean /* Get PHY mode from DT */ 942bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 943bdeced75SVladimir Oltean if (err) { 944bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 945bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 946bdeced75SVladimir Oltean port); 947bdeced75SVladimir Oltean of_node_put(child); 948bdeced75SVladimir Oltean return -ENODEV; 949bdeced75SVladimir Oltean } 950bdeced75SVladimir Oltean 951bdeced75SVladimir Oltean err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 952bdeced75SVladimir Oltean if (err < 0) { 953bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 954bdeced75SVladimir Oltean phy_modes(phy_mode), port); 95559ebb430SSumera Priyadarsini of_node_put(child); 956bdeced75SVladimir Oltean return err; 957bdeced75SVladimir Oltean } 958bdeced75SVladimir Oltean 959bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 960bdeced75SVladimir Oltean } 961bdeced75SVladimir Oltean 962bdeced75SVladimir Oltean return 0; 963bdeced75SVladimir Oltean } 964bdeced75SVladimir Oltean 965bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 966bdeced75SVladimir Oltean { 967bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 968bdeced75SVladimir Oltean struct device_node *switch_node; 969bdeced75SVladimir Oltean struct device_node *ports_node; 970bdeced75SVladimir Oltean int err; 971bdeced75SVladimir Oltean 972bdeced75SVladimir Oltean switch_node = dev->of_node; 973bdeced75SVladimir Oltean 974bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 975abecbfcdSVladimir Oltean if (!ports_node) 976abecbfcdSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 977bdeced75SVladimir Oltean if (!ports_node) { 978abecbfcdSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 979bdeced75SVladimir Oltean return -ENODEV; 980bdeced75SVladimir Oltean } 981bdeced75SVladimir Oltean 982bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 983bdeced75SVladimir Oltean of_node_put(ports_node); 984bdeced75SVladimir Oltean 985bdeced75SVladimir Oltean return err; 986bdeced75SVladimir Oltean } 987bdeced75SVladimir Oltean 98856051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 98956051948SVladimir Oltean { 99056051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 991bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 992b4024c9eSClaudiu Manoil struct resource res; 99356051948SVladimir Oltean int port, i, err; 99456051948SVladimir Oltean 99556051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 99656051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 99756051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 99856051948SVladimir Oltean if (!ocelot->ports) 99956051948SVladimir Oltean return -ENOMEM; 100056051948SVladimir Oltean 100156051948SVladimir Oltean ocelot->map = felix->info->map; 100256051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 100356051948SVladimir Oltean ocelot->num_stats = felix->info->num_stats; 100421ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 100507d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 100677043c37SXiaoliang Yang ocelot->vcap_pol.base = felix->info->vcap_pol_base; 100777043c37SXiaoliang Yang ocelot->vcap_pol.max = felix->info->vcap_pol_max; 100877043c37SXiaoliang Yang ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 100977043c37SXiaoliang Yang ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 101056051948SVladimir Oltean ocelot->ops = felix->info->ops; 1011cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 1012cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 1013f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 101456051948SVladimir Oltean 1015bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 1016bdeced75SVladimir Oltean GFP_KERNEL); 1017bdeced75SVladimir Oltean if (!port_phy_modes) 1018bdeced75SVladimir Oltean return -ENOMEM; 1019bdeced75SVladimir Oltean 1020bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 1021bdeced75SVladimir Oltean if (err) { 1022bdeced75SVladimir Oltean kfree(port_phy_modes); 1023bdeced75SVladimir Oltean return err; 1024bdeced75SVladimir Oltean } 1025bdeced75SVladimir Oltean 102656051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 102756051948SVladimir Oltean struct regmap *target; 102856051948SVladimir Oltean 102956051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 103056051948SVladimir Oltean continue; 103156051948SVladimir Oltean 1032b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 1033b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1034375e1314SVladimir Oltean res.start += felix->switch_base; 1035375e1314SVladimir Oltean res.end += felix->switch_base; 103656051948SVladimir Oltean 1037242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 103856051948SVladimir Oltean if (IS_ERR(target)) { 103956051948SVladimir Oltean dev_err(ocelot->dev, 104056051948SVladimir Oltean "Failed to map device memory space\n"); 1041bdeced75SVladimir Oltean kfree(port_phy_modes); 104256051948SVladimir Oltean return PTR_ERR(target); 104356051948SVladimir Oltean } 104456051948SVladimir Oltean 104556051948SVladimir Oltean ocelot->targets[i] = target; 104656051948SVladimir Oltean } 104756051948SVladimir Oltean 104856051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 104956051948SVladimir Oltean if (err) { 105056051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 1051bdeced75SVladimir Oltean kfree(port_phy_modes); 105256051948SVladimir Oltean return err; 105356051948SVladimir Oltean } 105456051948SVladimir Oltean 105556051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 105656051948SVladimir Oltean struct ocelot_port *ocelot_port; 105791c724cfSVladimir Oltean struct regmap *target; 105856051948SVladimir Oltean 105956051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 106056051948SVladimir Oltean sizeof(struct ocelot_port), 106156051948SVladimir Oltean GFP_KERNEL); 106256051948SVladimir Oltean if (!ocelot_port) { 106356051948SVladimir Oltean dev_err(ocelot->dev, 106456051948SVladimir Oltean "failed to allocate port memory\n"); 1065bdeced75SVladimir Oltean kfree(port_phy_modes); 106656051948SVladimir Oltean return -ENOMEM; 106756051948SVladimir Oltean } 106856051948SVladimir Oltean 1069b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1070b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1071375e1314SVladimir Oltean res.start += felix->switch_base; 1072375e1314SVladimir Oltean res.end += felix->switch_base; 107356051948SVladimir Oltean 1074242bd0c1SColin Foster target = felix->info->init_regmap(ocelot, &res); 107591c724cfSVladimir Oltean if (IS_ERR(target)) { 107656051948SVladimir Oltean dev_err(ocelot->dev, 107791c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 107891c724cfSVladimir Oltean port); 1079bdeced75SVladimir Oltean kfree(port_phy_modes); 108091c724cfSVladimir Oltean return PTR_ERR(target); 108156051948SVladimir Oltean } 108256051948SVladimir Oltean 1083bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 108456051948SVladimir Oltean ocelot_port->ocelot = ocelot; 108591c724cfSVladimir Oltean ocelot_port->target = target; 108656051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 108756051948SVladimir Oltean } 108856051948SVladimir Oltean 1089bdeced75SVladimir Oltean kfree(port_phy_modes); 1090bdeced75SVladimir Oltean 1091bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1092bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1093bdeced75SVladimir Oltean if (err < 0) 1094bdeced75SVladimir Oltean return err; 1095bdeced75SVladimir Oltean } 1096bdeced75SVladimir Oltean 109756051948SVladimir Oltean return 0; 109856051948SVladimir Oltean } 109956051948SVladimir Oltean 11001328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 11011328a883SVladimir Oltean struct sk_buff *skb) 11021328a883SVladimir Oltean { 11031328a883SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 11041328a883SVladimir Oltean struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 11051328a883SVladimir Oltean struct sk_buff *skb_match = NULL, *skb_tmp; 11061328a883SVladimir Oltean unsigned long flags; 11071328a883SVladimir Oltean 11081328a883SVladimir Oltean if (!clone) 11091328a883SVladimir Oltean return; 11101328a883SVladimir Oltean 11111328a883SVladimir Oltean spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 11121328a883SVladimir Oltean 11131328a883SVladimir Oltean skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 11141328a883SVladimir Oltean if (skb != clone) 11151328a883SVladimir Oltean continue; 11161328a883SVladimir Oltean __skb_unlink(skb, &ocelot_port->tx_skbs); 11171328a883SVladimir Oltean skb_match = skb; 11181328a883SVladimir Oltean break; 11191328a883SVladimir Oltean } 11201328a883SVladimir Oltean 11211328a883SVladimir Oltean spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 11221328a883SVladimir Oltean 11231328a883SVladimir Oltean WARN_ONCE(!skb_match, 11241328a883SVladimir Oltean "Could not find skb clone in TX timestamping list\n"); 11251328a883SVladimir Oltean } 11261328a883SVladimir Oltean 112749f885b2SVladimir Oltean #define work_to_xmit_work(w) \ 112849f885b2SVladimir Oltean container_of((w), struct felix_deferred_xmit_work, work) 112949f885b2SVladimir Oltean 113049f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work) 113149f885b2SVladimir Oltean { 113249f885b2SVladimir Oltean struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 113349f885b2SVladimir Oltean struct dsa_switch *ds = xmit_work->dp->ds; 113449f885b2SVladimir Oltean struct sk_buff *skb = xmit_work->skb; 113549f885b2SVladimir Oltean u32 rew_op = ocelot_ptp_rew_op(skb); 113649f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 113749f885b2SVladimir Oltean int port = xmit_work->dp->index; 113849f885b2SVladimir Oltean int retries = 10; 113949f885b2SVladimir Oltean 114049f885b2SVladimir Oltean do { 114149f885b2SVladimir Oltean if (ocelot_can_inject(ocelot, 0)) 114249f885b2SVladimir Oltean break; 114349f885b2SVladimir Oltean 114449f885b2SVladimir Oltean cpu_relax(); 114549f885b2SVladimir Oltean } while (--retries); 114649f885b2SVladimir Oltean 114749f885b2SVladimir Oltean if (!retries) { 114849f885b2SVladimir Oltean dev_err(ocelot->dev, "port %d failed to inject skb\n", 114949f885b2SVladimir Oltean port); 11501328a883SVladimir Oltean ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 115149f885b2SVladimir Oltean kfree_skb(skb); 115249f885b2SVladimir Oltean return; 115349f885b2SVladimir Oltean } 115449f885b2SVladimir Oltean 115549f885b2SVladimir Oltean ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 115649f885b2SVladimir Oltean 115749f885b2SVladimir Oltean consume_skb(skb); 115849f885b2SVladimir Oltean kfree(xmit_work); 115949f885b2SVladimir Oltean } 116049f885b2SVladimir Oltean 116135d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds, 116235d97680SVladimir Oltean enum dsa_tag_protocol proto) 116349f885b2SVladimir Oltean { 116435d97680SVladimir Oltean struct ocelot_8021q_tagger_data *tagger_data; 116549f885b2SVladimir Oltean 116635d97680SVladimir Oltean switch (proto) { 116735d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 116835d97680SVladimir Oltean tagger_data = ocelot_8021q_tagger_data(ds); 116935d97680SVladimir Oltean tagger_data->xmit_work_fn = felix_port_deferred_xmit; 117049f885b2SVladimir Oltean return 0; 117135d97680SVladimir Oltean case DSA_TAG_PROTO_OCELOT: 117235d97680SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 117349f885b2SVladimir Oltean return 0; 117435d97680SVladimir Oltean default: 117535d97680SVladimir Oltean return -EPROTONOSUPPORT; 117649f885b2SVladimir Oltean } 117749f885b2SVladimir Oltean } 117849f885b2SVladimir Oltean 117956051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with 118056051948SVladimir Oltean * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 118156051948SVladimir Oltean * us to allocate structures twice (leak memory) and map PCI memory twice 118256051948SVladimir Oltean * (which will not work). 118356051948SVladimir Oltean */ 118456051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds) 118556051948SVladimir Oltean { 118656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 118756051948SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 11882960bb14SVladimir Oltean struct dsa_port *dp; 11892960bb14SVladimir Oltean int err; 119056051948SVladimir Oltean 119156051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 119256051948SVladimir Oltean if (err) 119356051948SVladimir Oltean return err; 119456051948SVladimir Oltean 1195d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1196d1cc0e93SVladimir Oltean if (err) 11976b73b7c9SVladimir Oltean goto out_mdiobus_free; 1198d1cc0e93SVladimir Oltean 11992b49d128SYangbo Lu if (ocelot->ptp) { 12002ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 12012b49d128SYangbo Lu if (err) { 12022b49d128SYangbo Lu dev_err(ocelot->dev, 12032b49d128SYangbo Lu "Timestamp initialization failed\n"); 12042b49d128SYangbo Lu ocelot->ptp = 0; 12052b49d128SYangbo Lu } 12062b49d128SYangbo Lu } 120756051948SVladimir Oltean 12082960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) { 12092960bb14SVladimir Oltean ocelot_init_port(ocelot, dp->index); 1210bd2b3161SXiaoliang Yang 1211bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1212bd2b3161SXiaoliang Yang * bits of vlan tag. 1213bd2b3161SXiaoliang Yang */ 12142960bb14SVladimir Oltean felix_port_qos_map_init(ocelot, dp->index); 121556051948SVladimir Oltean } 121656051948SVladimir Oltean 1217f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1218f59fd9caSVladimir Oltean if (err) 12196b73b7c9SVladimir Oltean goto out_deinit_ports; 1220f59fd9caSVladimir Oltean 12212960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 1222adb3dccfSVladimir Oltean /* The initial tag protocol is NPI which always returns 0, so 1223adb3dccfSVladimir Oltean * there's no real point in checking for errors. 12241cf3299bSVladimir Oltean */ 12252960bb14SVladimir Oltean felix_set_tag_protocol(ds, dp->index, felix->tag_proto); 12268d5f7954SVladimir Oltean break; 1227adb3dccfSVladimir Oltean } 12281cf3299bSVladimir Oltean 12290b912fc9SVladimir Oltean ds->mtu_enforcement_ingress = true; 1230c54913c1SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 1231bdeced75SVladimir Oltean 123256051948SVladimir Oltean return 0; 12336b73b7c9SVladimir Oltean 12346b73b7c9SVladimir Oltean out_deinit_ports: 12352960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 12362960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 12376b73b7c9SVladimir Oltean 12386b73b7c9SVladimir Oltean ocelot_deinit_timestamp(ocelot); 12396b73b7c9SVladimir Oltean ocelot_deinit(ocelot); 12406b73b7c9SVladimir Oltean 12416b73b7c9SVladimir Oltean out_mdiobus_free: 12426b73b7c9SVladimir Oltean if (felix->info->mdio_bus_free) 12436b73b7c9SVladimir Oltean felix->info->mdio_bus_free(ocelot); 12446b73b7c9SVladimir Oltean 12456b73b7c9SVladimir Oltean return err; 124656051948SVladimir Oltean } 124756051948SVladimir Oltean 124856051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 124956051948SVladimir Oltean { 125056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1251bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12522960bb14SVladimir Oltean struct dsa_port *dp; 1253bdeced75SVladimir Oltean 12542960bb14SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds) { 12552960bb14SVladimir Oltean felix_del_tag_protocol(ds, dp->index, felix->tag_proto); 12568d5f7954SVladimir Oltean break; 1257adb3dccfSVladimir Oltean } 1258adb3dccfSVladimir Oltean 12592960bb14SVladimir Oltean dsa_switch_for_each_available_port(dp, ds) 12602960bb14SVladimir Oltean ocelot_deinit_port(ocelot, dp->index); 1261d19741b0SVladimir Oltean 126249f885b2SVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 126349f885b2SVladimir Oltean ocelot_deinit_timestamp(ocelot); 126449f885b2SVladimir Oltean ocelot_deinit(ocelot); 126549f885b2SVladimir Oltean 1266d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1267d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 126856051948SVladimir Oltean } 126956051948SVladimir Oltean 1270c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1271c0bcf537SYangbo Lu struct ifreq *ifr) 1272c0bcf537SYangbo Lu { 1273c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1274c0bcf537SYangbo Lu 1275c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1276c0bcf537SYangbo Lu } 1277c0bcf537SYangbo Lu 1278c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1279c0bcf537SYangbo Lu struct ifreq *ifr) 1280c0bcf537SYangbo Lu { 1281c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1282c0bcf537SYangbo Lu 1283c0bcf537SYangbo Lu return ocelot_hwstamp_set(ocelot, port, ifr); 1284c0bcf537SYangbo Lu } 1285c0bcf537SYangbo Lu 12860a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type) 12870a6f17c6SVladimir Oltean { 12880a6f17c6SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 12890a6f17c6SVladimir Oltean int err, grp = 0; 12900a6f17c6SVladimir Oltean 12910a6f17c6SVladimir Oltean if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 12920a6f17c6SVladimir Oltean return false; 12930a6f17c6SVladimir Oltean 12940a6f17c6SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 12950a6f17c6SVladimir Oltean return false; 12960a6f17c6SVladimir Oltean 12970a6f17c6SVladimir Oltean if (ptp_type == PTP_CLASS_NONE) 12980a6f17c6SVladimir Oltean return false; 12990a6f17c6SVladimir Oltean 13000a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 13010a6f17c6SVladimir Oltean struct sk_buff *skb; 13020a6f17c6SVladimir Oltean unsigned int type; 13030a6f17c6SVladimir Oltean 13040a6f17c6SVladimir Oltean err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 13050a6f17c6SVladimir Oltean if (err) 13060a6f17c6SVladimir Oltean goto out; 13070a6f17c6SVladimir Oltean 13080a6f17c6SVladimir Oltean /* We trap to the CPU port module all PTP frames, but 13090a6f17c6SVladimir Oltean * felix_rxtstamp() only gets called for event frames. 13100a6f17c6SVladimir Oltean * So we need to avoid sending duplicate general 13110a6f17c6SVladimir Oltean * message frames by running a second BPF classifier 13120a6f17c6SVladimir Oltean * here and dropping those. 13130a6f17c6SVladimir Oltean */ 13140a6f17c6SVladimir Oltean __skb_push(skb, ETH_HLEN); 13150a6f17c6SVladimir Oltean 13160a6f17c6SVladimir Oltean type = ptp_classify_raw(skb); 13170a6f17c6SVladimir Oltean 13180a6f17c6SVladimir Oltean __skb_pull(skb, ETH_HLEN); 13190a6f17c6SVladimir Oltean 13200a6f17c6SVladimir Oltean if (type == PTP_CLASS_NONE) { 13210a6f17c6SVladimir Oltean kfree_skb(skb); 13220a6f17c6SVladimir Oltean continue; 13230a6f17c6SVladimir Oltean } 13240a6f17c6SVladimir Oltean 13250a6f17c6SVladimir Oltean netif_rx(skb); 13260a6f17c6SVladimir Oltean } 13270a6f17c6SVladimir Oltean 13280a6f17c6SVladimir Oltean out: 13290a6f17c6SVladimir Oltean if (err < 0) 13300a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 13310a6f17c6SVladimir Oltean 13320a6f17c6SVladimir Oltean return true; 13330a6f17c6SVladimir Oltean } 13340a6f17c6SVladimir Oltean 1335c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1336c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1337c0bcf537SYangbo Lu { 133892f62485SVladimir Oltean u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1339c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1340c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1341c0bcf537SYangbo Lu struct timespec64 ts; 134292f62485SVladimir Oltean u32 tstamp_hi; 134392f62485SVladimir Oltean u64 tstamp; 1344c0bcf537SYangbo Lu 13450a6f17c6SVladimir Oltean /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 13460a6f17c6SVladimir Oltean * for RX timestamping. Then free it, and poll for its copy through 13470a6f17c6SVladimir Oltean * MMIO in the CPU port module, and inject that into the stack from 13480a6f17c6SVladimir Oltean * ocelot_xtr_poll(). 13490a6f17c6SVladimir Oltean */ 13500a6f17c6SVladimir Oltean if (felix_check_xtr_pkt(ocelot, type)) { 13510a6f17c6SVladimir Oltean kfree_skb(skb); 13520a6f17c6SVladimir Oltean return true; 13530a6f17c6SVladimir Oltean } 13540a6f17c6SVladimir Oltean 1355c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1356c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1357c0bcf537SYangbo Lu 1358c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1359c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1360c0bcf537SYangbo Lu tstamp_hi--; 1361c0bcf537SYangbo Lu 1362c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1363c0bcf537SYangbo Lu 1364c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1365c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1366c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1367c0bcf537SYangbo Lu return false; 1368c0bcf537SYangbo Lu } 1369c0bcf537SYangbo Lu 13705c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port, 13715c5416f5SYangbo Lu struct sk_buff *skb) 1372c0bcf537SYangbo Lu { 1373c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1374682eaad9SYangbo Lu struct sk_buff *clone = NULL; 1375c0bcf537SYangbo Lu 1376682eaad9SYangbo Lu if (!ocelot->ptp) 13775c5416f5SYangbo Lu return; 1378c0bcf537SYangbo Lu 137952849bcfSVladimir Oltean if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 138052849bcfSVladimir Oltean dev_err_ratelimited(ds->dev, 138152849bcfSVladimir Oltean "port %d delivering skb without TX timestamp\n", 138252849bcfSVladimir Oltean port); 1383682eaad9SYangbo Lu return; 138452849bcfSVladimir Oltean } 1385682eaad9SYangbo Lu 1386682eaad9SYangbo Lu if (clone) 1387c4b364ceSYangbo Lu OCELOT_SKB_CB(skb)->clone = clone; 13885c5416f5SYangbo Lu } 1389c0bcf537SYangbo Lu 13900b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 13910b912fc9SVladimir Oltean { 13920b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 13930b912fc9SVladimir Oltean 13940b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 13950b912fc9SVladimir Oltean 13960b912fc9SVladimir Oltean return 0; 13970b912fc9SVladimir Oltean } 13980b912fc9SVladimir Oltean 13990b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 14000b912fc9SVladimir Oltean { 14010b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 14020b912fc9SVladimir Oltean 14030b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 14040b912fc9SVladimir Oltean } 14050b912fc9SVladimir Oltean 140607d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 140707d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 140807d985eeSVladimir Oltean { 140907d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 141007d985eeSVladimir Oltean 141107d985eeSVladimir Oltean return ocelot_cls_flower_replace(ocelot, port, cls, ingress); 141207d985eeSVladimir Oltean } 141307d985eeSVladimir Oltean 141407d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 141507d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 141607d985eeSVladimir Oltean { 141707d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 141807d985eeSVladimir Oltean 141907d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 142007d985eeSVladimir Oltean } 142107d985eeSVladimir Oltean 142207d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 142307d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 142407d985eeSVladimir Oltean { 142507d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 142607d985eeSVladimir Oltean 142707d985eeSVladimir Oltean return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 142807d985eeSVladimir Oltean } 142907d985eeSVladimir Oltean 1430fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1431fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1432fc411eaaSVladimir Oltean { 1433fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1434fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1435fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 14365f035af7SPo Liu .burst = policer->burst, 1437fc411eaaSVladimir Oltean }; 1438fc411eaaSVladimir Oltean 1439fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1440fc411eaaSVladimir Oltean } 1441fc411eaaSVladimir Oltean 1442fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1443fc411eaaSVladimir Oltean { 1444fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1445fc411eaaSVladimir Oltean 1446fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1447fc411eaaSVladimir Oltean } 1448fc411eaaSVladimir Oltean 1449de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1450de143c0eSXiaoliang Yang enum tc_setup_type type, 1451de143c0eSXiaoliang Yang void *type_data) 1452de143c0eSXiaoliang Yang { 1453de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1454de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1455de143c0eSXiaoliang Yang 1456de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1457de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1458de143c0eSXiaoliang Yang else 1459de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1460de143c0eSXiaoliang Yang } 1461de143c0eSXiaoliang Yang 1462f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1463f59fd9caSVladimir Oltean u16 pool_index, 1464f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1465f59fd9caSVladimir Oltean { 1466f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1467f59fd9caSVladimir Oltean 1468f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1469f59fd9caSVladimir Oltean } 1470f59fd9caSVladimir Oltean 1471f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1472f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1473f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1474f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1475f59fd9caSVladimir Oltean { 1476f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1477f59fd9caSVladimir Oltean 1478f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1479f59fd9caSVladimir Oltean threshold_type, extack); 1480f59fd9caSVladimir Oltean } 1481f59fd9caSVladimir Oltean 1482f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1483f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1484f59fd9caSVladimir Oltean u32 *p_threshold) 1485f59fd9caSVladimir Oltean { 1486f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1487f59fd9caSVladimir Oltean 1488f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1489f59fd9caSVladimir Oltean p_threshold); 1490f59fd9caSVladimir Oltean } 1491f59fd9caSVladimir Oltean 1492f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1493f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1494f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1495f59fd9caSVladimir Oltean { 1496f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1497f59fd9caSVladimir Oltean 1498f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1499f59fd9caSVladimir Oltean threshold, extack); 1500f59fd9caSVladimir Oltean } 1501f59fd9caSVladimir Oltean 1502f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1503f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1504f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1505f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1506f59fd9caSVladimir Oltean { 1507f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1508f59fd9caSVladimir Oltean 1509f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1510f59fd9caSVladimir Oltean pool_type, p_pool_index, 1511f59fd9caSVladimir Oltean p_threshold); 1512f59fd9caSVladimir Oltean } 1513f59fd9caSVladimir Oltean 1514f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1515f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1516f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1517f59fd9caSVladimir Oltean u16 pool_index, u32 threshold, 1518f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1519f59fd9caSVladimir Oltean { 1520f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1521f59fd9caSVladimir Oltean 1522f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1523f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1524f59fd9caSVladimir Oltean extack); 1525f59fd9caSVladimir Oltean } 1526f59fd9caSVladimir Oltean 1527f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1528f59fd9caSVladimir Oltean unsigned int sb_index) 1529f59fd9caSVladimir Oltean { 1530f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1531f59fd9caSVladimir Oltean 1532f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1533f59fd9caSVladimir Oltean } 1534f59fd9caSVladimir Oltean 1535f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1536f59fd9caSVladimir Oltean unsigned int sb_index) 1537f59fd9caSVladimir Oltean { 1538f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1539f59fd9caSVladimir Oltean 1540f59fd9caSVladimir Oltean return ocelot_sb_occ_max_clear(ocelot, sb_index); 1541f59fd9caSVladimir Oltean } 1542f59fd9caSVladimir Oltean 1543f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1544f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1545f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1546f59fd9caSVladimir Oltean { 1547f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1548f59fd9caSVladimir Oltean 1549f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1550f59fd9caSVladimir Oltean p_cur, p_max); 1551f59fd9caSVladimir Oltean } 1552f59fd9caSVladimir Oltean 1553f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1554f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1555f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1556f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1557f59fd9caSVladimir Oltean { 1558f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1559f59fd9caSVladimir Oltean 1560f59fd9caSVladimir Oltean return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1561f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1562f59fd9caSVladimir Oltean } 1563f59fd9caSVladimir Oltean 1564a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port, 1565a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1566a026c50bSHoratiu Vultur { 1567a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1568a026c50bSHoratiu Vultur 1569a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1570a026c50bSHoratiu Vultur } 1571a026c50bSHoratiu Vultur 1572a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port, 1573a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1574a026c50bSHoratiu Vultur { 1575a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1576a026c50bSHoratiu Vultur 1577a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1578a026c50bSHoratiu Vultur } 1579a026c50bSHoratiu Vultur 1580a026c50bSHoratiu Vultur static int 1581a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1582a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1583a026c50bSHoratiu Vultur { 1584a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1585a026c50bSHoratiu Vultur 1586a026c50bSHoratiu Vultur return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1587a026c50bSHoratiu Vultur } 1588a026c50bSHoratiu Vultur 1589a026c50bSHoratiu Vultur static int 1590a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1591a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1592a026c50bSHoratiu Vultur { 1593a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1594a026c50bSHoratiu Vultur 1595a026c50bSHoratiu Vultur return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1596a026c50bSHoratiu Vultur } 1597a026c50bSHoratiu Vultur 1598375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 159956051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1600adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 160135d97680SVladimir Oltean .connect_tag_protocol = felix_connect_tag_protocol, 160256051948SVladimir Oltean .setup = felix_setup, 160356051948SVladimir Oltean .teardown = felix_teardown, 160456051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 160556051948SVladimir Oltean .get_strings = felix_get_strings, 160656051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 160756051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 160856051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 1609bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1610bdeced75SVladimir Oltean .phylink_mac_config = felix_phylink_mac_config, 1611bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1612bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 16135cad43a5SVladimir Oltean .port_fast_age = felix_port_fast_age, 161456051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 161556051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 161656051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1617209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1618209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 1619421741eaSVladimir Oltean .port_pre_bridge_flags = felix_pre_bridge_flags, 1620421741eaSVladimir Oltean .port_bridge_flags = felix_bridge_flags, 162156051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 162256051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 16238fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 16248fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 16258fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 162656051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 162756051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 162856051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 162956051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1630c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1631c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1632c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1633c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 16340b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 16350b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1636fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1637fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 163807d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 163907d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 164007d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1641de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1642f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1643f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1644f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1645f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1646f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1647f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1648f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1649f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1650f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1651f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1652a026c50bSHoratiu Vultur .port_mrp_add = felix_mrp_add, 1653a026c50bSHoratiu Vultur .port_mrp_del = felix_mrp_del, 1654a026c50bSHoratiu Vultur .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1655a026c50bSHoratiu Vultur .port_mrp_del_ring_role = felix_mrp_del_ring_role, 16565da11eb4SVladimir Oltean .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 16575da11eb4SVladimir Oltean .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 165856051948SVladimir Oltean }; 1659319e4dd1SVladimir Oltean 1660319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1661319e4dd1SVladimir Oltean { 1662319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1663319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1664319e4dd1SVladimir Oltean 1665319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1666319e4dd1SVladimir Oltean return NULL; 1667319e4dd1SVladimir Oltean 1668319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1669319e4dd1SVladimir Oltean } 1670319e4dd1SVladimir Oltean 1671319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1672319e4dd1SVladimir Oltean { 1673319e4dd1SVladimir Oltean struct dsa_port *dp; 1674319e4dd1SVladimir Oltean 1675319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1676319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1677319e4dd1SVladimir Oltean return -EINVAL; 1678319e4dd1SVladimir Oltean 1679319e4dd1SVladimir Oltean return dp->index; 1680319e4dd1SVladimir Oltean } 1681