156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0 23c9cfb52SVladimir Oltean /* Copyright 2019-2021 NXP 3375e1314SVladimir Oltean * 4375e1314SVladimir Oltean * This is an umbrella module for all network switches that are 5375e1314SVladimir Oltean * register-compatible with Ocelot and that perform I/O to their host CPU 6375e1314SVladimir Oltean * through an NPI (Node Processor Interface) Ethernet port. 756051948SVladimir Oltean */ 856051948SVladimir Oltean #include <uapi/linux/if_bridge.h> 907d985eeSVladimir Oltean #include <soc/mscc/ocelot_vcap.h> 10bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h> 11bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h> 12bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h> 13bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h> 142b49d128SYangbo Lu #include <soc/mscc/ocelot_ptp.h> 1556051948SVladimir Oltean #include <soc/mscc/ocelot.h> 16e21268efSVladimir Oltean #include <linux/dsa/8021q.h> 1740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h> 1884705fc1SMaxim Kochetkov #include <linux/platform_device.h> 190a6f17c6SVladimir Oltean #include <linux/ptp_classify.h> 2056051948SVladimir Oltean #include <linux/module.h> 21bdeced75SVladimir Oltean #include <linux/of_net.h> 2256051948SVladimir Oltean #include <linux/pci.h> 2356051948SVladimir Oltean #include <linux/of.h> 24588d0550SIoana Ciornei #include <linux/pcs-lynx.h> 25fc411eaaSVladimir Oltean #include <net/pkt_sched.h> 2656051948SVladimir Oltean #include <net/dsa.h> 2756051948SVladimir Oltean #include "felix.h" 2856051948SVladimir Oltean 29e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid, 30e21268efSVladimir Oltean bool pvid, bool untagged) 31e21268efSVladimir Oltean { 32e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 33e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 34e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 35e21268efSVladimir Oltean int key_length, upstream, err; 36e21268efSVladimir Oltean 37e21268efSVladimir Oltean /* We don't need to install the rxvlan into the other ports' filtering 38e21268efSVladimir Oltean * tables, because we're just pushing the rxvlan when sending towards 39e21268efSVladimir Oltean * the CPU 40e21268efSVladimir Oltean */ 41e21268efSVladimir Oltean if (!pvid) 42e21268efSVladimir Oltean return 0; 43e21268efSVladimir Oltean 44e21268efSVladimir Oltean key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length; 45e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 46e21268efSVladimir Oltean 47e21268efSVladimir Oltean outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), 48e21268efSVladimir Oltean GFP_KERNEL); 49e21268efSVladimir Oltean if (!outer_tagging_rule) 50e21268efSVladimir Oltean return -ENOMEM; 51e21268efSVladimir Oltean 52e21268efSVladimir Oltean outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 53e21268efSVladimir Oltean outer_tagging_rule->prio = 1; 54e21268efSVladimir Oltean outer_tagging_rule->id.cookie = port; 55e21268efSVladimir Oltean outer_tagging_rule->id.tc_offload = false; 56e21268efSVladimir Oltean outer_tagging_rule->block_id = VCAP_ES0; 57e21268efSVladimir Oltean outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 58e21268efSVladimir Oltean outer_tagging_rule->lookup = 0; 59e21268efSVladimir Oltean outer_tagging_rule->ingress_port.value = port; 60e21268efSVladimir Oltean outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0); 61e21268efSVladimir Oltean outer_tagging_rule->egress_port.value = upstream; 62e21268efSVladimir Oltean outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0); 63e21268efSVladimir Oltean outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG; 64e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD; 65e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_vid_sel = 1; 66e21268efSVladimir Oltean outer_tagging_rule->action.vid_a_val = vid; 67e21268efSVladimir Oltean 68e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL); 69e21268efSVladimir Oltean if (err) 70e21268efSVladimir Oltean kfree(outer_tagging_rule); 71e21268efSVladimir Oltean 72e21268efSVladimir Oltean return err; 73e21268efSVladimir Oltean } 74e21268efSVladimir Oltean 75e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid, 76e21268efSVladimir Oltean bool pvid, bool untagged) 77e21268efSVladimir Oltean { 78e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 79e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 80e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 81e21268efSVladimir Oltean int upstream, err; 82e21268efSVladimir Oltean 83e21268efSVladimir Oltean /* tag_8021q.c assumes we are implementing this via port VLAN 84e21268efSVladimir Oltean * membership, which we aren't. So we don't need to add any VCAP filter 85e21268efSVladimir Oltean * for the CPU port. 86e21268efSVladimir Oltean */ 87e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 88e21268efSVladimir Oltean return 0; 89e21268efSVladimir Oltean 90e21268efSVladimir Oltean untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 91e21268efSVladimir Oltean if (!untagging_rule) 92e21268efSVladimir Oltean return -ENOMEM; 93e21268efSVladimir Oltean 94e21268efSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 95e21268efSVladimir Oltean if (!redirect_rule) { 96e21268efSVladimir Oltean kfree(untagging_rule); 97e21268efSVladimir Oltean return -ENOMEM; 98e21268efSVladimir Oltean } 99e21268efSVladimir Oltean 100e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 101e21268efSVladimir Oltean 102e21268efSVladimir Oltean untagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 103e21268efSVladimir Oltean untagging_rule->ingress_port_mask = BIT(upstream); 104e21268efSVladimir Oltean untagging_rule->vlan.vid.value = vid; 105e21268efSVladimir Oltean untagging_rule->vlan.vid.mask = VLAN_VID_MASK; 106e21268efSVladimir Oltean untagging_rule->prio = 1; 107e21268efSVladimir Oltean untagging_rule->id.cookie = port; 108e21268efSVladimir Oltean untagging_rule->id.tc_offload = false; 109e21268efSVladimir Oltean untagging_rule->block_id = VCAP_IS1; 110e21268efSVladimir Oltean untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 111e21268efSVladimir Oltean untagging_rule->lookup = 0; 112e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt_ena = true; 113e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt = 1; 114e21268efSVladimir Oltean untagging_rule->action.pag_override_mask = 0xff; 115e21268efSVladimir Oltean untagging_rule->action.pag_val = port; 116e21268efSVladimir Oltean 117e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL); 118e21268efSVladimir Oltean if (err) { 119e21268efSVladimir Oltean kfree(untagging_rule); 120e21268efSVladimir Oltean kfree(redirect_rule); 121e21268efSVladimir Oltean return err; 122e21268efSVladimir Oltean } 123e21268efSVladimir Oltean 124e21268efSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 125e21268efSVladimir Oltean redirect_rule->ingress_port_mask = BIT(upstream); 126e21268efSVladimir Oltean redirect_rule->pag = port; 127e21268efSVladimir Oltean redirect_rule->prio = 1; 128e21268efSVladimir Oltean redirect_rule->id.cookie = port; 129e21268efSVladimir Oltean redirect_rule->id.tc_offload = false; 130e21268efSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 131e21268efSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 132e21268efSVladimir Oltean redirect_rule->lookup = 0; 133e21268efSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 134e21268efSVladimir Oltean redirect_rule->action.port_mask = BIT(port); 135e21268efSVladimir Oltean 136e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 137e21268efSVladimir Oltean if (err) { 138e21268efSVladimir Oltean ocelot_vcap_filter_del(ocelot, untagging_rule); 139e21268efSVladimir Oltean kfree(redirect_rule); 140e21268efSVladimir Oltean return err; 141e21268efSVladimir Oltean } 142e21268efSVladimir Oltean 143e21268efSVladimir Oltean return 0; 144e21268efSVladimir Oltean } 145e21268efSVladimir Oltean 146e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 147e21268efSVladimir Oltean u16 flags) 148e21268efSVladimir Oltean { 149e21268efSVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 150e21268efSVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 151e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 152e21268efSVladimir Oltean 153e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 154e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot), 155e21268efSVladimir Oltean port, vid, pvid, untagged); 156e21268efSVladimir Oltean 157e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 158e21268efSVladimir Oltean return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot), 159e21268efSVladimir Oltean port, vid, pvid, untagged); 160e21268efSVladimir Oltean 161e21268efSVladimir Oltean return 0; 162e21268efSVladimir Oltean } 163e21268efSVladimir Oltean 164e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid) 165e21268efSVladimir Oltean { 166e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 167e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_es0; 168e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 169e21268efSVladimir Oltean 170e21268efSVladimir Oltean block_vcap_es0 = &ocelot->block[VCAP_ES0]; 171e21268efSVladimir Oltean 172e21268efSVladimir Oltean outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 173e21268efSVladimir Oltean port, false); 174e21268efSVladimir Oltean /* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid 175e21268efSVladimir Oltean * installing outer tagging ES0 rules where they weren't needed. 176e21268efSVladimir Oltean * But in rxvlan_del, the API doesn't give us the "flags" anymore, 177e21268efSVladimir Oltean * so that forces us to be slightly sloppy here, and just assume that 178e21268efSVladimir Oltean * if we didn't find an outer_tagging_rule it means that there was 179e21268efSVladimir Oltean * none in the first place, i.e. rxvlan_del is called on a non-pvid 180e21268efSVladimir Oltean * port. This is most probably true though. 181e21268efSVladimir Oltean */ 182e21268efSVladimir Oltean if (!outer_tagging_rule) 183e21268efSVladimir Oltean return 0; 184e21268efSVladimir Oltean 185e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 186e21268efSVladimir Oltean } 187e21268efSVladimir Oltean 188e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid) 189e21268efSVladimir Oltean { 190e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 191e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 192e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 193e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 194e21268efSVladimir Oltean int err; 195e21268efSVladimir Oltean 196e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 197e21268efSVladimir Oltean return 0; 198e21268efSVladimir Oltean 199e21268efSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 200e21268efSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 201e21268efSVladimir Oltean 202e21268efSVladimir Oltean untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 203e21268efSVladimir Oltean port, false); 204e21268efSVladimir Oltean if (!untagging_rule) 205e21268efSVladimir Oltean return 0; 206e21268efSVladimir Oltean 207e21268efSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, untagging_rule); 208e21268efSVladimir Oltean if (err) 209e21268efSVladimir Oltean return err; 210e21268efSVladimir Oltean 211e21268efSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 212e21268efSVladimir Oltean port, false); 213e21268efSVladimir Oltean if (!redirect_rule) 214e21268efSVladimir Oltean return 0; 215e21268efSVladimir Oltean 216e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 217e21268efSVladimir Oltean } 218e21268efSVladimir Oltean 219e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 220e21268efSVladimir Oltean { 221e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 222e21268efSVladimir Oltean 223e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 224e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot), 225e21268efSVladimir Oltean port, vid); 226e21268efSVladimir Oltean 227e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 228e21268efSVladimir Oltean return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot), 229e21268efSVladimir Oltean port, vid); 230e21268efSVladimir Oltean 231e21268efSVladimir Oltean return 0; 232e21268efSVladimir Oltean } 233e21268efSVladimir Oltean 234e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC 235e21268efSVladimir Oltean * connected internally to the enetc or fman DSA master can be configured to 236e21268efSVladimir Oltean * use the software-defined tag_8021q frame format. As far as the hardware is 237e21268efSVladimir Oltean * concerned, it thinks it is a "dumb switch" - the queues of the CPU port 238e21268efSVladimir Oltean * module are now disconnected from it, but can still be accessed through 239e21268efSVladimir Oltean * register-based MMIO. 240e21268efSVladimir Oltean */ 241e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port) 242e21268efSVladimir Oltean { 243e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = true; 244e21268efSVladimir Oltean ocelot->npi = -1; 245e21268efSVladimir Oltean 246e21268efSVladimir Oltean /* Overwrite PGID_CPU with the non-tagging port */ 247e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU); 248e21268efSVladimir Oltean 249e21268efSVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 250e21268efSVladimir Oltean } 251e21268efSVladimir Oltean 252e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port) 253e21268efSVladimir Oltean { 254e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = false; 255e21268efSVladimir Oltean 256e21268efSVladimir Oltean /* Restore PGID_CPU */ 257e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID, 258e21268efSVladimir Oltean PGID_CPU); 259e21268efSVladimir Oltean 260e21268efSVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 261e21268efSVladimir Oltean } 262e21268efSVladimir Oltean 263c8c0ba4fSVladimir Oltean /* Set up a VCAP IS2 rule for delivering PTP frames to the CPU port module. 264c8c0ba4fSVladimir Oltean * If the quirk_no_xtr_irq is in place, then also copy those PTP frames to the 265c8c0ba4fSVladimir Oltean * tag_8021q CPU port. 266c8c0ba4fSVladimir Oltean */ 267c8c0ba4fSVladimir Oltean static int felix_setup_mmio_filtering(struct felix *felix) 268c8c0ba4fSVladimir Oltean { 2698d5f7954SVladimir Oltean unsigned long user_ports = dsa_user_ports(felix->ds); 270c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *redirect_rule; 271c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *tagging_rule; 272c8c0ba4fSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 273c8c0ba4fSVladimir Oltean struct dsa_switch *ds = felix->ds; 2748d5f7954SVladimir Oltean int cpu = -1, port, ret; 275c8c0ba4fSVladimir Oltean 276c8c0ba4fSVladimir Oltean tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 277c8c0ba4fSVladimir Oltean if (!tagging_rule) 278c8c0ba4fSVladimir Oltean return -ENOMEM; 279c8c0ba4fSVladimir Oltean 280c8c0ba4fSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 281c8c0ba4fSVladimir Oltean if (!redirect_rule) { 282c8c0ba4fSVladimir Oltean kfree(tagging_rule); 283c8c0ba4fSVladimir Oltean return -ENOMEM; 284c8c0ba4fSVladimir Oltean } 285c8c0ba4fSVladimir Oltean 286c8c0ba4fSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2878d5f7954SVladimir Oltean if (dsa_is_cpu_port(ds, port)) { 2888d5f7954SVladimir Oltean cpu = port; 2898d5f7954SVladimir Oltean break; 290c8c0ba4fSVladimir Oltean } 2918d5f7954SVladimir Oltean } 2928d5f7954SVladimir Oltean 2938d5f7954SVladimir Oltean if (cpu < 0) 2948d5f7954SVladimir Oltean return -EINVAL; 295c8c0ba4fSVladimir Oltean 296c8c0ba4fSVladimir Oltean tagging_rule->key_type = OCELOT_VCAP_KEY_ETYPE; 297c8c0ba4fSVladimir Oltean *(__be16 *)tagging_rule->key.etype.etype.value = htons(ETH_P_1588); 298c8c0ba4fSVladimir Oltean *(__be16 *)tagging_rule->key.etype.etype.mask = htons(0xffff); 299c8c0ba4fSVladimir Oltean tagging_rule->ingress_port_mask = user_ports; 300c8c0ba4fSVladimir Oltean tagging_rule->prio = 1; 301c8c0ba4fSVladimir Oltean tagging_rule->id.cookie = ocelot->num_phys_ports; 302c8c0ba4fSVladimir Oltean tagging_rule->id.tc_offload = false; 303c8c0ba4fSVladimir Oltean tagging_rule->block_id = VCAP_IS1; 304c8c0ba4fSVladimir Oltean tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 305c8c0ba4fSVladimir Oltean tagging_rule->lookup = 0; 306c8c0ba4fSVladimir Oltean tagging_rule->action.pag_override_mask = 0xff; 307c8c0ba4fSVladimir Oltean tagging_rule->action.pag_val = ocelot->num_phys_ports; 308c8c0ba4fSVladimir Oltean 309c8c0ba4fSVladimir Oltean ret = ocelot_vcap_filter_add(ocelot, tagging_rule, NULL); 310c8c0ba4fSVladimir Oltean if (ret) { 311c8c0ba4fSVladimir Oltean kfree(tagging_rule); 312c8c0ba4fSVladimir Oltean kfree(redirect_rule); 313c8c0ba4fSVladimir Oltean return ret; 314c8c0ba4fSVladimir Oltean } 315c8c0ba4fSVladimir Oltean 316c8c0ba4fSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 317c8c0ba4fSVladimir Oltean redirect_rule->ingress_port_mask = user_ports; 318c8c0ba4fSVladimir Oltean redirect_rule->pag = ocelot->num_phys_ports; 319c8c0ba4fSVladimir Oltean redirect_rule->prio = 1; 320c8c0ba4fSVladimir Oltean redirect_rule->id.cookie = ocelot->num_phys_ports; 321c8c0ba4fSVladimir Oltean redirect_rule->id.tc_offload = false; 322c8c0ba4fSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 323c8c0ba4fSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 324c8c0ba4fSVladimir Oltean redirect_rule->lookup = 0; 325c8c0ba4fSVladimir Oltean redirect_rule->action.cpu_copy_ena = true; 326c8c0ba4fSVladimir Oltean if (felix->info->quirk_no_xtr_irq) { 327c8c0ba4fSVladimir Oltean /* Redirect to the tag_8021q CPU but also copy PTP packets to 328c8c0ba4fSVladimir Oltean * the CPU port module 329c8c0ba4fSVladimir Oltean */ 330c8c0ba4fSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 3318d5f7954SVladimir Oltean redirect_rule->action.port_mask = BIT(cpu); 332c8c0ba4fSVladimir Oltean } else { 333c8c0ba4fSVladimir Oltean /* Trap PTP packets only to the CPU port module (which is 334c8c0ba4fSVladimir Oltean * redirected to the NPI port) 335c8c0ba4fSVladimir Oltean */ 336c8c0ba4fSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 337c8c0ba4fSVladimir Oltean redirect_rule->action.port_mask = 0; 338c8c0ba4fSVladimir Oltean } 339c8c0ba4fSVladimir Oltean 340c8c0ba4fSVladimir Oltean ret = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 341c8c0ba4fSVladimir Oltean if (ret) { 342c8c0ba4fSVladimir Oltean ocelot_vcap_filter_del(ocelot, tagging_rule); 343c8c0ba4fSVladimir Oltean kfree(redirect_rule); 344c8c0ba4fSVladimir Oltean return ret; 345c8c0ba4fSVladimir Oltean } 346c8c0ba4fSVladimir Oltean 3470a6f17c6SVladimir Oltean /* The ownership of the CPU port module's queues might have just been 3480a6f17c6SVladimir Oltean * transferred to the tag_8021q tagger from the NPI-based tagger. 3490a6f17c6SVladimir Oltean * So there might still be all sorts of crap in the queues. On the 3500a6f17c6SVladimir Oltean * other hand, the MMIO-based matching of PTP frames is very brittle, 3510a6f17c6SVladimir Oltean * so we need to be careful that there are no extra frames to be 3520a6f17c6SVladimir Oltean * dequeued over MMIO, since we would never know to discard them. 3530a6f17c6SVladimir Oltean */ 3540a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 3550a6f17c6SVladimir Oltean 356c8c0ba4fSVladimir Oltean return 0; 357c8c0ba4fSVladimir Oltean } 358c8c0ba4fSVladimir Oltean 359c8c0ba4fSVladimir Oltean static int felix_teardown_mmio_filtering(struct felix *felix) 360c8c0ba4fSVladimir Oltean { 361c8c0ba4fSVladimir Oltean struct ocelot_vcap_filter *tagging_rule, *redirect_rule; 362c8c0ba4fSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 363c8c0ba4fSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 364c8c0ba4fSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 365c8c0ba4fSVladimir Oltean int err; 366c8c0ba4fSVladimir Oltean 367c8c0ba4fSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 368c8c0ba4fSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 369c8c0ba4fSVladimir Oltean 370c8c0ba4fSVladimir Oltean tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 371c8c0ba4fSVladimir Oltean ocelot->num_phys_ports, 372c8c0ba4fSVladimir Oltean false); 373c8c0ba4fSVladimir Oltean if (!tagging_rule) 374c8c0ba4fSVladimir Oltean return -ENOENT; 375c8c0ba4fSVladimir Oltean 376c8c0ba4fSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, tagging_rule); 377c8c0ba4fSVladimir Oltean if (err) 378c8c0ba4fSVladimir Oltean return err; 379c8c0ba4fSVladimir Oltean 380c8c0ba4fSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 381c8c0ba4fSVladimir Oltean ocelot->num_phys_ports, 382c8c0ba4fSVladimir Oltean false); 383c8c0ba4fSVladimir Oltean if (!redirect_rule) 384c8c0ba4fSVladimir Oltean return -ENOENT; 385c8c0ba4fSVladimir Oltean 386c8c0ba4fSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 387c8c0ba4fSVladimir Oltean } 388c8c0ba4fSVladimir Oltean 389e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu) 390e21268efSVladimir Oltean { 391e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 392e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 393e21268efSVladimir Oltean unsigned long cpu_flood; 394e21268efSVladimir Oltean int port, err; 395e21268efSVladimir Oltean 396e21268efSVladimir Oltean felix_8021q_cpu_port_init(ocelot, cpu); 397e21268efSVladimir Oltean 398e21268efSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 399e21268efSVladimir Oltean if (dsa_is_unused_port(ds, port)) 400e21268efSVladimir Oltean continue; 401e21268efSVladimir Oltean 402e21268efSVladimir Oltean /* This overwrites ocelot_init(): 403e21268efSVladimir Oltean * Do not forward BPDU frames to the CPU port module, 404e21268efSVladimir Oltean * for 2 reasons: 405e21268efSVladimir Oltean * - When these packets are injected from the tag_8021q 406e21268efSVladimir Oltean * CPU port, we want them to go out, not loop back 407e21268efSVladimir Oltean * into the system. 408e21268efSVladimir Oltean * - STP traffic ingressing on a user port should go to 409e21268efSVladimir Oltean * the tag_8021q CPU port, not to the hardware CPU 410e21268efSVladimir Oltean * port module. 411e21268efSVladimir Oltean */ 412e21268efSVladimir Oltean ocelot_write_gix(ocelot, 413e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 414e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, port); 415e21268efSVladimir Oltean } 416e21268efSVladimir Oltean 417c8c0ba4fSVladimir Oltean /* In tag_8021q mode, the CPU port module is unused, except for PTP 418c8c0ba4fSVladimir Oltean * frames. So we want to disable flooding of any kind to the CPU port 419c8c0ba4fSVladimir Oltean * module, since packets going there will end in a black hole. 420e21268efSVladimir Oltean */ 421e21268efSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 422e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC); 423e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC); 424b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC); 425e21268efSVladimir Oltean 4265da11eb4SVladimir Oltean err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD)); 427e21268efSVladimir Oltean if (err) 428d7b1fd52SVladimir Oltean return err; 429d7b1fd52SVladimir Oltean 430328621f6SVladimir Oltean err = felix_setup_mmio_filtering(felix); 431d7b1fd52SVladimir Oltean if (err) 432d7b1fd52SVladimir Oltean goto out_tag_8021q_unregister; 433e21268efSVladimir Oltean 434e21268efSVladimir Oltean return 0; 435e21268efSVladimir Oltean 436d7b1fd52SVladimir Oltean out_tag_8021q_unregister: 437d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 438e21268efSVladimir Oltean return err; 439e21268efSVladimir Oltean } 440e21268efSVladimir Oltean 441e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu) 442e21268efSVladimir Oltean { 443e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 444e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 445e21268efSVladimir Oltean int err, port; 446e21268efSVladimir Oltean 447c8c0ba4fSVladimir Oltean err = felix_teardown_mmio_filtering(felix); 448c8c0ba4fSVladimir Oltean if (err) 449c8c0ba4fSVladimir Oltean dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d", 450c8c0ba4fSVladimir Oltean err); 451c8c0ba4fSVladimir Oltean 452d7b1fd52SVladimir Oltean dsa_tag_8021q_unregister(ds); 453e21268efSVladimir Oltean 454e21268efSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 455e21268efSVladimir Oltean if (dsa_is_unused_port(ds, port)) 456e21268efSVladimir Oltean continue; 457e21268efSVladimir Oltean 458e21268efSVladimir Oltean /* Restore the logic from ocelot_init: 459e21268efSVladimir Oltean * do not forward BPDU frames to the front ports. 460e21268efSVladimir Oltean */ 461e21268efSVladimir Oltean ocelot_write_gix(ocelot, 462e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 463e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, 464e21268efSVladimir Oltean port); 465e21268efSVladimir Oltean } 466e21268efSVladimir Oltean 467e21268efSVladimir Oltean felix_8021q_cpu_port_deinit(ocelot, cpu); 468e21268efSVladimir Oltean } 469e21268efSVladimir Oltean 470adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This 471adb3dccfSVladimir Oltean * is the mode through which frames can be injected from and extracted to an 472adb3dccfSVladimir Oltean * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 473adb3dccfSVladimir Oltean * running Linux, and this forms a DSA setup together with the enetc or fman 474adb3dccfSVladimir Oltean * DSA master. 475adb3dccfSVladimir Oltean */ 476adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port) 477adb3dccfSVladimir Oltean { 478adb3dccfSVladimir Oltean ocelot->npi = port; 479adb3dccfSVladimir Oltean 480adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 481adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 482adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 483adb3dccfSVladimir Oltean 484adb3dccfSVladimir Oltean /* NPI port Injection/Extraction configuration */ 485adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 486adb3dccfSVladimir Oltean ocelot->npi_xtr_prefix); 487adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 488adb3dccfSVladimir Oltean ocelot->npi_inj_prefix); 489adb3dccfSVladimir Oltean 490adb3dccfSVladimir Oltean /* Disable transmission of pause frames */ 491adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 492adb3dccfSVladimir Oltean } 493adb3dccfSVladimir Oltean 494adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 495adb3dccfSVladimir Oltean { 496adb3dccfSVladimir Oltean /* Restore hardware defaults */ 497adb3dccfSVladimir Oltean int unused_port = ocelot->num_phys_ports + 2; 498adb3dccfSVladimir Oltean 499adb3dccfSVladimir Oltean ocelot->npi = -1; 500adb3dccfSVladimir Oltean 501adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 502adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 503adb3dccfSVladimir Oltean 504adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 505adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 506adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 507adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 508adb3dccfSVladimir Oltean 509adb3dccfSVladimir Oltean /* Enable transmission of pause frames */ 510adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 511adb3dccfSVladimir Oltean } 512adb3dccfSVladimir Oltean 513adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu) 514adb3dccfSVladimir Oltean { 515adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 516adb3dccfSVladimir Oltean unsigned long cpu_flood; 517adb3dccfSVladimir Oltean 518adb3dccfSVladimir Oltean felix_npi_port_init(ocelot, cpu); 519adb3dccfSVladimir Oltean 520adb3dccfSVladimir Oltean /* Include the CPU port module (and indirectly, the NPI port) 521adb3dccfSVladimir Oltean * in the forwarding mask for unknown unicast - the hardware 522adb3dccfSVladimir Oltean * default value for ANA_FLOODING_FLD_UNICAST excludes 523adb3dccfSVladimir Oltean * BIT(ocelot->num_phys_ports), and so does ocelot_init, 524adb3dccfSVladimir Oltean * since Ocelot relies on whitelisting MAC addresses towards 525adb3dccfSVladimir Oltean * PGID_CPU. 526adb3dccfSVladimir Oltean * We do this because DSA does not yet perform RX filtering, 527adb3dccfSVladimir Oltean * and the NPI port does not perform source address learning, 528adb3dccfSVladimir Oltean * so traffic sent to Linux is effectively unknown from the 529adb3dccfSVladimir Oltean * switch's perspective. 530adb3dccfSVladimir Oltean */ 531adb3dccfSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 532adb3dccfSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC); 5336edb9e8dSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC); 534b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC); 535adb3dccfSVladimir Oltean 536adb3dccfSVladimir Oltean return 0; 537adb3dccfSVladimir Oltean } 538adb3dccfSVladimir Oltean 539adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu) 540adb3dccfSVladimir Oltean { 541adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 542adb3dccfSVladimir Oltean 543adb3dccfSVladimir Oltean felix_npi_port_deinit(ocelot, cpu); 544adb3dccfSVladimir Oltean } 545adb3dccfSVladimir Oltean 546adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu, 547adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 548adb3dccfSVladimir Oltean { 549adb3dccfSVladimir Oltean int err; 550adb3dccfSVladimir Oltean 551adb3dccfSVladimir Oltean switch (proto) { 5527c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 553adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 554adb3dccfSVladimir Oltean err = felix_setup_tag_npi(ds, cpu); 555adb3dccfSVladimir Oltean break; 556e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 557e21268efSVladimir Oltean err = felix_setup_tag_8021q(ds, cpu); 558e21268efSVladimir Oltean break; 559adb3dccfSVladimir Oltean default: 560adb3dccfSVladimir Oltean err = -EPROTONOSUPPORT; 561adb3dccfSVladimir Oltean } 562adb3dccfSVladimir Oltean 563adb3dccfSVladimir Oltean return err; 564adb3dccfSVladimir Oltean } 565adb3dccfSVladimir Oltean 566adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu, 567adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 568adb3dccfSVladimir Oltean { 569adb3dccfSVladimir Oltean switch (proto) { 5707c4bb540SVladimir Oltean case DSA_TAG_PROTO_SEVILLE: 571adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 572adb3dccfSVladimir Oltean felix_teardown_tag_npi(ds, cpu); 573adb3dccfSVladimir Oltean break; 574e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 575e21268efSVladimir Oltean felix_teardown_tag_8021q(ds, cpu); 576e21268efSVladimir Oltean break; 577adb3dccfSVladimir Oltean default: 578adb3dccfSVladimir Oltean break; 579adb3dccfSVladimir Oltean } 580adb3dccfSVladimir Oltean } 581adb3dccfSVladimir Oltean 582e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the 583e21268efSVladimir Oltean * tag_8021q setup can fail, the NPI setup can't. So either the change is made, 584e21268efSVladimir Oltean * or the restoration is guaranteed to work. 585e21268efSVladimir Oltean */ 586adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu, 587adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 588adb3dccfSVladimir Oltean { 589adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 590adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 591adb3dccfSVladimir Oltean enum dsa_tag_protocol old_proto = felix->tag_proto; 592adb3dccfSVladimir Oltean int err; 593adb3dccfSVladimir Oltean 5947c4bb540SVladimir Oltean if (proto != DSA_TAG_PROTO_SEVILLE && 5957c4bb540SVladimir Oltean proto != DSA_TAG_PROTO_OCELOT && 596e21268efSVladimir Oltean proto != DSA_TAG_PROTO_OCELOT_8021Q) 597adb3dccfSVladimir Oltean return -EPROTONOSUPPORT; 598adb3dccfSVladimir Oltean 599adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, cpu, old_proto); 600adb3dccfSVladimir Oltean 601adb3dccfSVladimir Oltean err = felix_set_tag_protocol(ds, cpu, proto); 602adb3dccfSVladimir Oltean if (err) { 603adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, cpu, old_proto); 604adb3dccfSVladimir Oltean return err; 605adb3dccfSVladimir Oltean } 606adb3dccfSVladimir Oltean 607adb3dccfSVladimir Oltean felix->tag_proto = proto; 608adb3dccfSVladimir Oltean 609adb3dccfSVladimir Oltean return 0; 610adb3dccfSVladimir Oltean } 611adb3dccfSVladimir Oltean 61256051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 6134d776482SFlorian Fainelli int port, 6144d776482SFlorian Fainelli enum dsa_tag_protocol mp) 61556051948SVladimir Oltean { 616adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 617adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 618adb3dccfSVladimir Oltean 619adb3dccfSVladimir Oltean return felix->tag_proto; 62056051948SVladimir Oltean } 62156051948SVladimir Oltean 62256051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds, 62356051948SVladimir Oltean unsigned int ageing_time) 62456051948SVladimir Oltean { 62556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 62656051948SVladimir Oltean 62756051948SVladimir Oltean ocelot_set_ageing_time(ocelot, ageing_time); 62856051948SVladimir Oltean 62956051948SVladimir Oltean return 0; 63056051948SVladimir Oltean } 63156051948SVladimir Oltean 63256051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port, 63356051948SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 63456051948SVladimir Oltean { 63556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 63656051948SVladimir Oltean 63756051948SVladimir Oltean return ocelot_fdb_dump(ocelot, port, cb, data); 63856051948SVladimir Oltean } 63956051948SVladimir Oltean 64056051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port, 64156051948SVladimir Oltean const unsigned char *addr, u16 vid) 64256051948SVladimir Oltean { 64356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 64456051948SVladimir Oltean 64587b0f983SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid); 64656051948SVladimir Oltean } 64756051948SVladimir Oltean 64856051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port, 64956051948SVladimir Oltean const unsigned char *addr, u16 vid) 65056051948SVladimir Oltean { 65156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 65256051948SVladimir Oltean 65356051948SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid); 65456051948SVladimir Oltean } 65556051948SVladimir Oltean 656a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 657209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 658209edf95SVladimir Oltean { 659209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 660209edf95SVladimir Oltean 661a52b2da7SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb); 662209edf95SVladimir Oltean } 663209edf95SVladimir Oltean 664209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port, 665209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 666209edf95SVladimir Oltean { 667209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 668209edf95SVladimir Oltean 669209edf95SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb); 670209edf95SVladimir Oltean } 671209edf95SVladimir Oltean 67256051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 67356051948SVladimir Oltean u8 state) 67456051948SVladimir Oltean { 67556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 67656051948SVladimir Oltean 67756051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 67856051948SVladimir Oltean } 67956051948SVladimir Oltean 680421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 681421741eaSVladimir Oltean struct switchdev_brport_flags val, 682421741eaSVladimir Oltean struct netlink_ext_ack *extack) 683421741eaSVladimir Oltean { 684421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 685421741eaSVladimir Oltean 686421741eaSVladimir Oltean return ocelot_port_pre_bridge_flags(ocelot, port, val); 687421741eaSVladimir Oltean } 688421741eaSVladimir Oltean 689421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port, 690421741eaSVladimir Oltean struct switchdev_brport_flags val, 691421741eaSVladimir Oltean struct netlink_ext_ack *extack) 692421741eaSVladimir Oltean { 693421741eaSVladimir Oltean struct ocelot *ocelot = ds->priv; 694421741eaSVladimir Oltean 695421741eaSVladimir Oltean ocelot_port_bridge_flags(ocelot, port, val); 696421741eaSVladimir Oltean 697421741eaSVladimir Oltean return 0; 698421741eaSVladimir Oltean } 699421741eaSVladimir Oltean 70056051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 70156051948SVladimir Oltean struct net_device *br) 70256051948SVladimir Oltean { 70356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 70456051948SVladimir Oltean 705e4bd44e8SVladimir Oltean ocelot_port_bridge_join(ocelot, port, br); 706e4bd44e8SVladimir Oltean 707e4bd44e8SVladimir Oltean return 0; 70856051948SVladimir Oltean } 70956051948SVladimir Oltean 71056051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 71156051948SVladimir Oltean struct net_device *br) 71256051948SVladimir Oltean { 71356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 71456051948SVladimir Oltean 71556051948SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, br); 71656051948SVladimir Oltean } 71756051948SVladimir Oltean 7188fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 7198fe6832eSVladimir Oltean struct net_device *bond, 7208fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 7218fe6832eSVladimir Oltean { 7228fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7238fe6832eSVladimir Oltean 7248fe6832eSVladimir Oltean return ocelot_port_lag_join(ocelot, port, bond, info); 7258fe6832eSVladimir Oltean } 7268fe6832eSVladimir Oltean 7278fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 7288fe6832eSVladimir Oltean struct net_device *bond) 7298fe6832eSVladimir Oltean { 7308fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7318fe6832eSVladimir Oltean 7328fe6832eSVladimir Oltean ocelot_port_lag_leave(ocelot, port, bond); 7338fe6832eSVladimir Oltean 7348fe6832eSVladimir Oltean return 0; 7358fe6832eSVladimir Oltean } 7368fe6832eSVladimir Oltean 7378fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 7388fe6832eSVladimir Oltean { 7398fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 7408fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 7418fe6832eSVladimir Oltean 7428fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 7438fe6832eSVladimir Oltean 7448fe6832eSVladimir Oltean return 0; 7458fe6832eSVladimir Oltean } 7468fe6832eSVladimir Oltean 74756051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 74801af940eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 74901af940eSVladimir Oltean struct netlink_ext_ack *extack) 75056051948SVladimir Oltean { 7512f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 752b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 7532f0402feSVladimir Oltean 7549a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 7559a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 7569a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 7579a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 7589a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 7599a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 7609a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 7619a720680SVladimir Oltean */ 7629a720680SVladimir Oltean if (port == ocelot->npi) 7639a720680SVladimir Oltean return 0; 7649a720680SVladimir Oltean 765b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 7662f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 76701af940eSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED, 76801af940eSVladimir Oltean extack); 76956051948SVladimir Oltean } 77056051948SVladimir Oltean 77189153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 77289153ed6SVladimir Oltean struct netlink_ext_ack *extack) 77356051948SVladimir Oltean { 77456051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 77556051948SVladimir Oltean 7763b95d1b2SVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 77756051948SVladimir Oltean } 77856051948SVladimir Oltean 7791958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 78031046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 78131046a5fSVladimir Oltean struct netlink_ext_ack *extack) 78256051948SVladimir Oltean { 78356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 784183be6f9SVladimir Oltean u16 flags = vlan->flags; 7851958d581SVladimir Oltean int err; 78656051948SVladimir Oltean 78701af940eSVladimir Oltean err = felix_vlan_prepare(ds, port, vlan, extack); 7881958d581SVladimir Oltean if (err) 7891958d581SVladimir Oltean return err; 7901958d581SVladimir Oltean 7911958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 792183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 793183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 79456051948SVladimir Oltean } 79556051948SVladimir Oltean 79656051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 79756051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 79856051948SVladimir Oltean { 79956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 80056051948SVladimir Oltean 801b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 80256051948SVladimir Oltean } 80356051948SVladimir Oltean 804bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port, 805bdeced75SVladimir Oltean unsigned long *supported, 806bdeced75SVladimir Oltean struct phylink_link_state *state) 807bdeced75SVladimir Oltean { 808bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 809375e1314SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 810bdeced75SVladimir Oltean 811375e1314SVladimir Oltean if (felix->info->phylink_validate) 812375e1314SVladimir Oltean felix->info->phylink_validate(ocelot, port, supported, state); 813bdeced75SVladimir Oltean } 814bdeced75SVladimir Oltean 815bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port, 816bdeced75SVladimir Oltean unsigned int link_an_mode, 817bdeced75SVladimir Oltean const struct phylink_link_state *state) 818bdeced75SVladimir Oltean { 819bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 820bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 821588d0550SIoana Ciornei struct dsa_port *dp = dsa_to_port(ds, port); 822bdeced75SVladimir Oltean 823588d0550SIoana Ciornei if (felix->pcs[port]) 824588d0550SIoana Ciornei phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs); 825bdeced75SVladimir Oltean } 826bdeced75SVladimir Oltean 827bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 828bdeced75SVladimir Oltean unsigned int link_an_mode, 829bdeced75SVladimir Oltean phy_interface_t interface) 830bdeced75SVladimir Oltean { 831bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 832bdeced75SVladimir Oltean 833e6e12df6SVladimir Oltean ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 834e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 835bdeced75SVladimir Oltean } 836bdeced75SVladimir Oltean 837bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 838bdeced75SVladimir Oltean unsigned int link_an_mode, 839bdeced75SVladimir Oltean phy_interface_t interface, 8405b502a7bSRussell King struct phy_device *phydev, 8415b502a7bSRussell King int speed, int duplex, 8425b502a7bSRussell King bool tx_pause, bool rx_pause) 843bdeced75SVladimir Oltean { 844bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 8457e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 846bdeced75SVladimir Oltean 847e6e12df6SVladimir Oltean ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 848e6e12df6SVladimir Oltean interface, speed, duplex, tx_pause, rx_pause, 849e6e12df6SVladimir Oltean FELIX_MAC_QUIRKS); 8507e14a2dcSVladimir Oltean 8517e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 8527e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 853bdeced75SVladimir Oltean } 854bdeced75SVladimir Oltean 855bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 856bd2b3161SXiaoliang Yang { 857bd2b3161SXiaoliang Yang int i; 858bd2b3161SXiaoliang Yang 859bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 860bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 861bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 862bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 863bd2b3161SXiaoliang Yang port); 864bd2b3161SXiaoliang Yang 86570d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 866bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 867bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 868bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 869bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 870bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 871bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 872bd2b3161SXiaoliang Yang port, i); 873bd2b3161SXiaoliang Yang } 874bd2b3161SXiaoliang Yang } 875bd2b3161SXiaoliang Yang 87656051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 87756051948SVladimir Oltean u32 stringset, u8 *data) 87856051948SVladimir Oltean { 87956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 88056051948SVladimir Oltean 88156051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 88256051948SVladimir Oltean } 88356051948SVladimir Oltean 88456051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 88556051948SVladimir Oltean { 88656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 88756051948SVladimir Oltean 88856051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 88956051948SVladimir Oltean } 89056051948SVladimir Oltean 89156051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 89256051948SVladimir Oltean { 89356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 89456051948SVladimir Oltean 89556051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 89656051948SVladimir Oltean } 89756051948SVladimir Oltean 89856051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 89956051948SVladimir Oltean struct ethtool_ts_info *info) 90056051948SVladimir Oltean { 90156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 90256051948SVladimir Oltean 90356051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 90456051948SVladimir Oltean } 90556051948SVladimir Oltean 906bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 907bdeced75SVladimir Oltean struct device_node *ports_node, 908bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 909bdeced75SVladimir Oltean { 910bdeced75SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 911bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 912bdeced75SVladimir Oltean struct device_node *child; 913bdeced75SVladimir Oltean 91437fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 915bdeced75SVladimir Oltean phy_interface_t phy_mode; 916bdeced75SVladimir Oltean u32 port; 917bdeced75SVladimir Oltean int err; 918bdeced75SVladimir Oltean 919bdeced75SVladimir Oltean /* Get switch port number from DT */ 920bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 921bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 922bdeced75SVladimir Oltean "(property \"reg\")\n"); 923bdeced75SVladimir Oltean of_node_put(child); 924bdeced75SVladimir Oltean return -ENODEV; 925bdeced75SVladimir Oltean } 926bdeced75SVladimir Oltean 927bdeced75SVladimir Oltean /* Get PHY mode from DT */ 928bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 929bdeced75SVladimir Oltean if (err) { 930bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 931bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 932bdeced75SVladimir Oltean port); 933bdeced75SVladimir Oltean of_node_put(child); 934bdeced75SVladimir Oltean return -ENODEV; 935bdeced75SVladimir Oltean } 936bdeced75SVladimir Oltean 937bdeced75SVladimir Oltean err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 938bdeced75SVladimir Oltean if (err < 0) { 939bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 940bdeced75SVladimir Oltean phy_modes(phy_mode), port); 94159ebb430SSumera Priyadarsini of_node_put(child); 942bdeced75SVladimir Oltean return err; 943bdeced75SVladimir Oltean } 944bdeced75SVladimir Oltean 945bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 946bdeced75SVladimir Oltean } 947bdeced75SVladimir Oltean 948bdeced75SVladimir Oltean return 0; 949bdeced75SVladimir Oltean } 950bdeced75SVladimir Oltean 951bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 952bdeced75SVladimir Oltean { 953bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 954bdeced75SVladimir Oltean struct device_node *switch_node; 955bdeced75SVladimir Oltean struct device_node *ports_node; 956bdeced75SVladimir Oltean int err; 957bdeced75SVladimir Oltean 958bdeced75SVladimir Oltean switch_node = dev->of_node; 959bdeced75SVladimir Oltean 960bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 961abecbfcdSVladimir Oltean if (!ports_node) 962abecbfcdSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 963bdeced75SVladimir Oltean if (!ports_node) { 964abecbfcdSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 965bdeced75SVladimir Oltean return -ENODEV; 966bdeced75SVladimir Oltean } 967bdeced75SVladimir Oltean 968bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 969bdeced75SVladimir Oltean of_node_put(ports_node); 970bdeced75SVladimir Oltean 971bdeced75SVladimir Oltean return err; 972bdeced75SVladimir Oltean } 973bdeced75SVladimir Oltean 97456051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 97556051948SVladimir Oltean { 97656051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 977bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 978b4024c9eSClaudiu Manoil struct resource res; 97956051948SVladimir Oltean int port, i, err; 98056051948SVladimir Oltean 98156051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 98256051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 98356051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 98456051948SVladimir Oltean if (!ocelot->ports) 98556051948SVladimir Oltean return -ENOMEM; 98656051948SVladimir Oltean 98756051948SVladimir Oltean ocelot->map = felix->info->map; 98856051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 98956051948SVladimir Oltean ocelot->num_stats = felix->info->num_stats; 99021ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 99107d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 992*77043c37SXiaoliang Yang ocelot->vcap_pol.base = felix->info->vcap_pol_base; 993*77043c37SXiaoliang Yang ocelot->vcap_pol.max = felix->info->vcap_pol_max; 994*77043c37SXiaoliang Yang ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 995*77043c37SXiaoliang Yang ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 99656051948SVladimir Oltean ocelot->ops = felix->info->ops; 997cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 998cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 999f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 100056051948SVladimir Oltean 1001bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 1002bdeced75SVladimir Oltean GFP_KERNEL); 1003bdeced75SVladimir Oltean if (!port_phy_modes) 1004bdeced75SVladimir Oltean return -ENOMEM; 1005bdeced75SVladimir Oltean 1006bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 1007bdeced75SVladimir Oltean if (err) { 1008bdeced75SVladimir Oltean kfree(port_phy_modes); 1009bdeced75SVladimir Oltean return err; 1010bdeced75SVladimir Oltean } 1011bdeced75SVladimir Oltean 101256051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 101356051948SVladimir Oltean struct regmap *target; 101456051948SVladimir Oltean 101556051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 101656051948SVladimir Oltean continue; 101756051948SVladimir Oltean 1018b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 1019b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1020375e1314SVladimir Oltean res.start += felix->switch_base; 1021375e1314SVladimir Oltean res.end += felix->switch_base; 102256051948SVladimir Oltean 1023b4024c9eSClaudiu Manoil target = ocelot_regmap_init(ocelot, &res); 102456051948SVladimir Oltean if (IS_ERR(target)) { 102556051948SVladimir Oltean dev_err(ocelot->dev, 102656051948SVladimir Oltean "Failed to map device memory space\n"); 1027bdeced75SVladimir Oltean kfree(port_phy_modes); 102856051948SVladimir Oltean return PTR_ERR(target); 102956051948SVladimir Oltean } 103056051948SVladimir Oltean 103156051948SVladimir Oltean ocelot->targets[i] = target; 103256051948SVladimir Oltean } 103356051948SVladimir Oltean 103456051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 103556051948SVladimir Oltean if (err) { 103656051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 1037bdeced75SVladimir Oltean kfree(port_phy_modes); 103856051948SVladimir Oltean return err; 103956051948SVladimir Oltean } 104056051948SVladimir Oltean 104156051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 104256051948SVladimir Oltean struct ocelot_port *ocelot_port; 104391c724cfSVladimir Oltean struct regmap *target; 104456051948SVladimir Oltean 104556051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 104656051948SVladimir Oltean sizeof(struct ocelot_port), 104756051948SVladimir Oltean GFP_KERNEL); 104856051948SVladimir Oltean if (!ocelot_port) { 104956051948SVladimir Oltean dev_err(ocelot->dev, 105056051948SVladimir Oltean "failed to allocate port memory\n"); 1051bdeced75SVladimir Oltean kfree(port_phy_modes); 105256051948SVladimir Oltean return -ENOMEM; 105356051948SVladimir Oltean } 105456051948SVladimir Oltean 1055b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1056b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 1057375e1314SVladimir Oltean res.start += felix->switch_base; 1058375e1314SVladimir Oltean res.end += felix->switch_base; 105956051948SVladimir Oltean 106091c724cfSVladimir Oltean target = ocelot_regmap_init(ocelot, &res); 106191c724cfSVladimir Oltean if (IS_ERR(target)) { 106256051948SVladimir Oltean dev_err(ocelot->dev, 106391c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 106491c724cfSVladimir Oltean port); 1065bdeced75SVladimir Oltean kfree(port_phy_modes); 106691c724cfSVladimir Oltean return PTR_ERR(target); 106756051948SVladimir Oltean } 106856051948SVladimir Oltean 1069bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 107056051948SVladimir Oltean ocelot_port->ocelot = ocelot; 107191c724cfSVladimir Oltean ocelot_port->target = target; 107256051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 107356051948SVladimir Oltean } 107456051948SVladimir Oltean 1075bdeced75SVladimir Oltean kfree(port_phy_modes); 1076bdeced75SVladimir Oltean 1077bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1078bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1079bdeced75SVladimir Oltean if (err < 0) 1080bdeced75SVladimir Oltean return err; 1081bdeced75SVladimir Oltean } 1082bdeced75SVladimir Oltean 108356051948SVladimir Oltean return 0; 108456051948SVladimir Oltean } 108556051948SVladimir Oltean 10861328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 10871328a883SVladimir Oltean struct sk_buff *skb) 10881328a883SVladimir Oltean { 10891328a883SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 10901328a883SVladimir Oltean struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 10911328a883SVladimir Oltean struct sk_buff *skb_match = NULL, *skb_tmp; 10921328a883SVladimir Oltean unsigned long flags; 10931328a883SVladimir Oltean 10941328a883SVladimir Oltean if (!clone) 10951328a883SVladimir Oltean return; 10961328a883SVladimir Oltean 10971328a883SVladimir Oltean spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 10981328a883SVladimir Oltean 10991328a883SVladimir Oltean skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 11001328a883SVladimir Oltean if (skb != clone) 11011328a883SVladimir Oltean continue; 11021328a883SVladimir Oltean __skb_unlink(skb, &ocelot_port->tx_skbs); 11031328a883SVladimir Oltean skb_match = skb; 11041328a883SVladimir Oltean break; 11051328a883SVladimir Oltean } 11061328a883SVladimir Oltean 11071328a883SVladimir Oltean spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 11081328a883SVladimir Oltean 11091328a883SVladimir Oltean WARN_ONCE(!skb_match, 11101328a883SVladimir Oltean "Could not find skb clone in TX timestamping list\n"); 11111328a883SVladimir Oltean } 11121328a883SVladimir Oltean 111349f885b2SVladimir Oltean #define work_to_xmit_work(w) \ 111449f885b2SVladimir Oltean container_of((w), struct felix_deferred_xmit_work, work) 111549f885b2SVladimir Oltean 111649f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work) 111749f885b2SVladimir Oltean { 111849f885b2SVladimir Oltean struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 111949f885b2SVladimir Oltean struct dsa_switch *ds = xmit_work->dp->ds; 112049f885b2SVladimir Oltean struct sk_buff *skb = xmit_work->skb; 112149f885b2SVladimir Oltean u32 rew_op = ocelot_ptp_rew_op(skb); 112249f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 112349f885b2SVladimir Oltean int port = xmit_work->dp->index; 112449f885b2SVladimir Oltean int retries = 10; 112549f885b2SVladimir Oltean 112649f885b2SVladimir Oltean do { 112749f885b2SVladimir Oltean if (ocelot_can_inject(ocelot, 0)) 112849f885b2SVladimir Oltean break; 112949f885b2SVladimir Oltean 113049f885b2SVladimir Oltean cpu_relax(); 113149f885b2SVladimir Oltean } while (--retries); 113249f885b2SVladimir Oltean 113349f885b2SVladimir Oltean if (!retries) { 113449f885b2SVladimir Oltean dev_err(ocelot->dev, "port %d failed to inject skb\n", 113549f885b2SVladimir Oltean port); 11361328a883SVladimir Oltean ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 113749f885b2SVladimir Oltean kfree_skb(skb); 113849f885b2SVladimir Oltean return; 113949f885b2SVladimir Oltean } 114049f885b2SVladimir Oltean 114149f885b2SVladimir Oltean ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 114249f885b2SVladimir Oltean 114349f885b2SVladimir Oltean consume_skb(skb); 114449f885b2SVladimir Oltean kfree(xmit_work); 114549f885b2SVladimir Oltean } 114649f885b2SVladimir Oltean 114749f885b2SVladimir Oltean static int felix_port_setup_tagger_data(struct dsa_switch *ds, int port) 114849f885b2SVladimir Oltean { 114949f885b2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 115049f885b2SVladimir Oltean struct ocelot *ocelot = ds->priv; 115149f885b2SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 115249f885b2SVladimir Oltean struct felix_port *felix_port; 115349f885b2SVladimir Oltean 115449f885b2SVladimir Oltean if (!dsa_port_is_user(dp)) 115549f885b2SVladimir Oltean return 0; 115649f885b2SVladimir Oltean 115749f885b2SVladimir Oltean felix_port = kzalloc(sizeof(*felix_port), GFP_KERNEL); 115849f885b2SVladimir Oltean if (!felix_port) 115949f885b2SVladimir Oltean return -ENOMEM; 116049f885b2SVladimir Oltean 116149f885b2SVladimir Oltean felix_port->xmit_worker = felix->xmit_worker; 116249f885b2SVladimir Oltean felix_port->xmit_work_fn = felix_port_deferred_xmit; 116349f885b2SVladimir Oltean 116449f885b2SVladimir Oltean dp->priv = felix_port; 116549f885b2SVladimir Oltean 116649f885b2SVladimir Oltean return 0; 116749f885b2SVladimir Oltean } 116849f885b2SVladimir Oltean 116949f885b2SVladimir Oltean static void felix_port_teardown_tagger_data(struct dsa_switch *ds, int port) 117049f885b2SVladimir Oltean { 117149f885b2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 117249f885b2SVladimir Oltean struct felix_port *felix_port = dp->priv; 117349f885b2SVladimir Oltean 117449f885b2SVladimir Oltean if (!felix_port) 117549f885b2SVladimir Oltean return; 117649f885b2SVladimir Oltean 117749f885b2SVladimir Oltean dp->priv = NULL; 117849f885b2SVladimir Oltean kfree(felix_port); 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); 119056051948SVladimir Oltean int port, err; 119156051948SVladimir Oltean 119256051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 119356051948SVladimir Oltean if (err) 119456051948SVladimir Oltean return err; 119556051948SVladimir Oltean 1196d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1197d1cc0e93SVladimir Oltean if (err) 11986b73b7c9SVladimir Oltean goto out_mdiobus_free; 1199d1cc0e93SVladimir Oltean 12002b49d128SYangbo Lu if (ocelot->ptp) { 12012ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 12022b49d128SYangbo Lu if (err) { 12032b49d128SYangbo Lu dev_err(ocelot->dev, 12042b49d128SYangbo Lu "Timestamp initialization failed\n"); 12052b49d128SYangbo Lu ocelot->ptp = 0; 12062b49d128SYangbo Lu } 12072b49d128SYangbo Lu } 120856051948SVladimir Oltean 120949f885b2SVladimir Oltean felix->xmit_worker = kthread_create_worker(0, "felix_xmit"); 121049f885b2SVladimir Oltean if (IS_ERR(felix->xmit_worker)) { 121149f885b2SVladimir Oltean err = PTR_ERR(felix->xmit_worker); 121249f885b2SVladimir Oltean goto out_deinit_timestamp; 121349f885b2SVladimir Oltean } 121449f885b2SVladimir Oltean 121556051948SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1216adb3dccfSVladimir Oltean if (dsa_is_unused_port(ds, port)) 1217adb3dccfSVladimir Oltean continue; 121856051948SVladimir Oltean 1219adb3dccfSVladimir Oltean ocelot_init_port(ocelot, port); 1220bd2b3161SXiaoliang Yang 1221bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1222bd2b3161SXiaoliang Yang * bits of vlan tag. 1223bd2b3161SXiaoliang Yang */ 1224bd2b3161SXiaoliang Yang felix_port_qos_map_init(ocelot, port); 122549f885b2SVladimir Oltean 122649f885b2SVladimir Oltean err = felix_port_setup_tagger_data(ds, port); 122749f885b2SVladimir Oltean if (err) { 122849f885b2SVladimir Oltean dev_err(ds->dev, 122949f885b2SVladimir Oltean "port %d failed to set up tagger data: %pe\n", 123049f885b2SVladimir Oltean port, ERR_PTR(err)); 123149f885b2SVladimir Oltean goto out_deinit_ports; 123249f885b2SVladimir Oltean } 123356051948SVladimir Oltean } 123456051948SVladimir Oltean 1235f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1236f59fd9caSVladimir Oltean if (err) 12376b73b7c9SVladimir Oltean goto out_deinit_ports; 1238f59fd9caSVladimir Oltean 1239adb3dccfSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1240adb3dccfSVladimir Oltean if (!dsa_is_cpu_port(ds, port)) 1241adb3dccfSVladimir Oltean continue; 1242adb3dccfSVladimir Oltean 1243adb3dccfSVladimir Oltean /* The initial tag protocol is NPI which always returns 0, so 1244adb3dccfSVladimir Oltean * there's no real point in checking for errors. 12451cf3299bSVladimir Oltean */ 1246adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, port, felix->tag_proto); 12478d5f7954SVladimir Oltean break; 1248adb3dccfSVladimir Oltean } 12491cf3299bSVladimir Oltean 12500b912fc9SVladimir Oltean ds->mtu_enforcement_ingress = true; 1251c54913c1SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 1252bdeced75SVladimir Oltean 125356051948SVladimir Oltean return 0; 12546b73b7c9SVladimir Oltean 12556b73b7c9SVladimir Oltean out_deinit_ports: 12566b73b7c9SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 12576b73b7c9SVladimir Oltean if (dsa_is_unused_port(ds, port)) 12586b73b7c9SVladimir Oltean continue; 12596b73b7c9SVladimir Oltean 126049f885b2SVladimir Oltean felix_port_teardown_tagger_data(ds, port); 12616b73b7c9SVladimir Oltean ocelot_deinit_port(ocelot, port); 12626b73b7c9SVladimir Oltean } 12636b73b7c9SVladimir Oltean 126449f885b2SVladimir Oltean kthread_destroy_worker(felix->xmit_worker); 126549f885b2SVladimir Oltean 126649f885b2SVladimir Oltean out_deinit_timestamp: 12676b73b7c9SVladimir Oltean ocelot_deinit_timestamp(ocelot); 12686b73b7c9SVladimir Oltean ocelot_deinit(ocelot); 12696b73b7c9SVladimir Oltean 12706b73b7c9SVladimir Oltean out_mdiobus_free: 12716b73b7c9SVladimir Oltean if (felix->info->mdio_bus_free) 12726b73b7c9SVladimir Oltean felix->info->mdio_bus_free(ocelot); 12736b73b7c9SVladimir Oltean 12746b73b7c9SVladimir Oltean return err; 127556051948SVladimir Oltean } 127656051948SVladimir Oltean 127756051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 127856051948SVladimir Oltean { 127956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1280bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1281e5fb512dSVladimir Oltean int port; 1282bdeced75SVladimir Oltean 1283adb3dccfSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1284adb3dccfSVladimir Oltean if (!dsa_is_cpu_port(ds, port)) 1285adb3dccfSVladimir Oltean continue; 1286adb3dccfSVladimir Oltean 1287adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, port, felix->tag_proto); 12888d5f7954SVladimir Oltean break; 1289adb3dccfSVladimir Oltean } 1290adb3dccfSVladimir Oltean 129142b5adbbSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 129242b5adbbSVladimir Oltean if (dsa_is_unused_port(ds, port)) 129342b5adbbSVladimir Oltean continue; 129442b5adbbSVladimir Oltean 129549f885b2SVladimir Oltean felix_port_teardown_tagger_data(ds, port); 1296e5fb512dSVladimir Oltean ocelot_deinit_port(ocelot, port); 129742b5adbbSVladimir Oltean } 1298d19741b0SVladimir Oltean 129949f885b2SVladimir Oltean kthread_destroy_worker(felix->xmit_worker); 130049f885b2SVladimir Oltean 130149f885b2SVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 130249f885b2SVladimir Oltean ocelot_deinit_timestamp(ocelot); 130349f885b2SVladimir Oltean ocelot_deinit(ocelot); 130449f885b2SVladimir Oltean 1305d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1306d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 130756051948SVladimir Oltean } 130856051948SVladimir Oltean 1309c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1310c0bcf537SYangbo Lu struct ifreq *ifr) 1311c0bcf537SYangbo Lu { 1312c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1313c0bcf537SYangbo Lu 1314c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1315c0bcf537SYangbo Lu } 1316c0bcf537SYangbo Lu 1317c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1318c0bcf537SYangbo Lu struct ifreq *ifr) 1319c0bcf537SYangbo Lu { 1320c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1321c0bcf537SYangbo Lu 1322c0bcf537SYangbo Lu return ocelot_hwstamp_set(ocelot, port, ifr); 1323c0bcf537SYangbo Lu } 1324c0bcf537SYangbo Lu 13250a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type) 13260a6f17c6SVladimir Oltean { 13270a6f17c6SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 13280a6f17c6SVladimir Oltean int err, grp = 0; 13290a6f17c6SVladimir Oltean 13300a6f17c6SVladimir Oltean if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 13310a6f17c6SVladimir Oltean return false; 13320a6f17c6SVladimir Oltean 13330a6f17c6SVladimir Oltean if (!felix->info->quirk_no_xtr_irq) 13340a6f17c6SVladimir Oltean return false; 13350a6f17c6SVladimir Oltean 13360a6f17c6SVladimir Oltean if (ptp_type == PTP_CLASS_NONE) 13370a6f17c6SVladimir Oltean return false; 13380a6f17c6SVladimir Oltean 13390a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 13400a6f17c6SVladimir Oltean struct sk_buff *skb; 13410a6f17c6SVladimir Oltean unsigned int type; 13420a6f17c6SVladimir Oltean 13430a6f17c6SVladimir Oltean err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 13440a6f17c6SVladimir Oltean if (err) 13450a6f17c6SVladimir Oltean goto out; 13460a6f17c6SVladimir Oltean 13470a6f17c6SVladimir Oltean /* We trap to the CPU port module all PTP frames, but 13480a6f17c6SVladimir Oltean * felix_rxtstamp() only gets called for event frames. 13490a6f17c6SVladimir Oltean * So we need to avoid sending duplicate general 13500a6f17c6SVladimir Oltean * message frames by running a second BPF classifier 13510a6f17c6SVladimir Oltean * here and dropping those. 13520a6f17c6SVladimir Oltean */ 13530a6f17c6SVladimir Oltean __skb_push(skb, ETH_HLEN); 13540a6f17c6SVladimir Oltean 13550a6f17c6SVladimir Oltean type = ptp_classify_raw(skb); 13560a6f17c6SVladimir Oltean 13570a6f17c6SVladimir Oltean __skb_pull(skb, ETH_HLEN); 13580a6f17c6SVladimir Oltean 13590a6f17c6SVladimir Oltean if (type == PTP_CLASS_NONE) { 13600a6f17c6SVladimir Oltean kfree_skb(skb); 13610a6f17c6SVladimir Oltean continue; 13620a6f17c6SVladimir Oltean } 13630a6f17c6SVladimir Oltean 13640a6f17c6SVladimir Oltean netif_rx(skb); 13650a6f17c6SVladimir Oltean } 13660a6f17c6SVladimir Oltean 13670a6f17c6SVladimir Oltean out: 13680a6f17c6SVladimir Oltean if (err < 0) 13690a6f17c6SVladimir Oltean ocelot_drain_cpu_queue(ocelot, 0); 13700a6f17c6SVladimir Oltean 13710a6f17c6SVladimir Oltean return true; 13720a6f17c6SVladimir Oltean } 13730a6f17c6SVladimir Oltean 1374c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1375c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1376c0bcf537SYangbo Lu { 137792f62485SVladimir Oltean u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1378c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1379c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1380c0bcf537SYangbo Lu struct timespec64 ts; 138192f62485SVladimir Oltean u32 tstamp_hi; 138292f62485SVladimir Oltean u64 tstamp; 1383c0bcf537SYangbo Lu 13840a6f17c6SVladimir Oltean /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 13850a6f17c6SVladimir Oltean * for RX timestamping. Then free it, and poll for its copy through 13860a6f17c6SVladimir Oltean * MMIO in the CPU port module, and inject that into the stack from 13870a6f17c6SVladimir Oltean * ocelot_xtr_poll(). 13880a6f17c6SVladimir Oltean */ 13890a6f17c6SVladimir Oltean if (felix_check_xtr_pkt(ocelot, type)) { 13900a6f17c6SVladimir Oltean kfree_skb(skb); 13910a6f17c6SVladimir Oltean return true; 13920a6f17c6SVladimir Oltean } 13930a6f17c6SVladimir Oltean 1394c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1395c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1396c0bcf537SYangbo Lu 1397c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1398c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1399c0bcf537SYangbo Lu tstamp_hi--; 1400c0bcf537SYangbo Lu 1401c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1402c0bcf537SYangbo Lu 1403c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1404c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1405c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1406c0bcf537SYangbo Lu return false; 1407c0bcf537SYangbo Lu } 1408c0bcf537SYangbo Lu 14095c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port, 14105c5416f5SYangbo Lu struct sk_buff *skb) 1411c0bcf537SYangbo Lu { 1412c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1413682eaad9SYangbo Lu struct sk_buff *clone = NULL; 1414c0bcf537SYangbo Lu 1415682eaad9SYangbo Lu if (!ocelot->ptp) 14165c5416f5SYangbo Lu return; 1417c0bcf537SYangbo Lu 141852849bcfSVladimir Oltean if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 141952849bcfSVladimir Oltean dev_err_ratelimited(ds->dev, 142052849bcfSVladimir Oltean "port %d delivering skb without TX timestamp\n", 142152849bcfSVladimir Oltean port); 1422682eaad9SYangbo Lu return; 142352849bcfSVladimir Oltean } 1424682eaad9SYangbo Lu 1425682eaad9SYangbo Lu if (clone) 1426c4b364ceSYangbo Lu OCELOT_SKB_CB(skb)->clone = clone; 14275c5416f5SYangbo Lu } 1428c0bcf537SYangbo Lu 14290b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 14300b912fc9SVladimir Oltean { 14310b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 14320b912fc9SVladimir Oltean 14330b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 14340b912fc9SVladimir Oltean 14350b912fc9SVladimir Oltean return 0; 14360b912fc9SVladimir Oltean } 14370b912fc9SVladimir Oltean 14380b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 14390b912fc9SVladimir Oltean { 14400b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 14410b912fc9SVladimir Oltean 14420b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 14430b912fc9SVladimir Oltean } 14440b912fc9SVladimir Oltean 144507d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 144607d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 144707d985eeSVladimir Oltean { 144807d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 144907d985eeSVladimir Oltean 145007d985eeSVladimir Oltean return ocelot_cls_flower_replace(ocelot, port, cls, ingress); 145107d985eeSVladimir Oltean } 145207d985eeSVladimir Oltean 145307d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 145407d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 145507d985eeSVladimir Oltean { 145607d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 145707d985eeSVladimir Oltean 145807d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 145907d985eeSVladimir Oltean } 146007d985eeSVladimir Oltean 146107d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 146207d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 146307d985eeSVladimir Oltean { 146407d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 146507d985eeSVladimir Oltean 146607d985eeSVladimir Oltean return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 146707d985eeSVladimir Oltean } 146807d985eeSVladimir Oltean 1469fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1470fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1471fc411eaaSVladimir Oltean { 1472fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1473fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1474fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 14755f035af7SPo Liu .burst = policer->burst, 1476fc411eaaSVladimir Oltean }; 1477fc411eaaSVladimir Oltean 1478fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1479fc411eaaSVladimir Oltean } 1480fc411eaaSVladimir Oltean 1481fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1482fc411eaaSVladimir Oltean { 1483fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1484fc411eaaSVladimir Oltean 1485fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1486fc411eaaSVladimir Oltean } 1487fc411eaaSVladimir Oltean 1488de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1489de143c0eSXiaoliang Yang enum tc_setup_type type, 1490de143c0eSXiaoliang Yang void *type_data) 1491de143c0eSXiaoliang Yang { 1492de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1493de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1494de143c0eSXiaoliang Yang 1495de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1496de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1497de143c0eSXiaoliang Yang else 1498de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1499de143c0eSXiaoliang Yang } 1500de143c0eSXiaoliang Yang 1501f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1502f59fd9caSVladimir Oltean u16 pool_index, 1503f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1504f59fd9caSVladimir Oltean { 1505f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1506f59fd9caSVladimir Oltean 1507f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1508f59fd9caSVladimir Oltean } 1509f59fd9caSVladimir Oltean 1510f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1511f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1512f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1513f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1514f59fd9caSVladimir Oltean { 1515f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1516f59fd9caSVladimir Oltean 1517f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1518f59fd9caSVladimir Oltean threshold_type, extack); 1519f59fd9caSVladimir Oltean } 1520f59fd9caSVladimir Oltean 1521f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1522f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1523f59fd9caSVladimir Oltean u32 *p_threshold) 1524f59fd9caSVladimir Oltean { 1525f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1526f59fd9caSVladimir Oltean 1527f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1528f59fd9caSVladimir Oltean p_threshold); 1529f59fd9caSVladimir Oltean } 1530f59fd9caSVladimir Oltean 1531f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1532f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1533f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1534f59fd9caSVladimir Oltean { 1535f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1536f59fd9caSVladimir Oltean 1537f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1538f59fd9caSVladimir Oltean threshold, extack); 1539f59fd9caSVladimir Oltean } 1540f59fd9caSVladimir Oltean 1541f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1542f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1543f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1544f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1545f59fd9caSVladimir Oltean { 1546f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1547f59fd9caSVladimir Oltean 1548f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1549f59fd9caSVladimir Oltean pool_type, p_pool_index, 1550f59fd9caSVladimir Oltean p_threshold); 1551f59fd9caSVladimir Oltean } 1552f59fd9caSVladimir Oltean 1553f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(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 u16 pool_index, u32 threshold, 1557f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1558f59fd9caSVladimir Oltean { 1559f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1560f59fd9caSVladimir Oltean 1561f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1562f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1563f59fd9caSVladimir Oltean extack); 1564f59fd9caSVladimir Oltean } 1565f59fd9caSVladimir Oltean 1566f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1567f59fd9caSVladimir Oltean unsigned int sb_index) 1568f59fd9caSVladimir Oltean { 1569f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1570f59fd9caSVladimir Oltean 1571f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1572f59fd9caSVladimir Oltean } 1573f59fd9caSVladimir Oltean 1574f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1575f59fd9caSVladimir Oltean unsigned int sb_index) 1576f59fd9caSVladimir Oltean { 1577f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1578f59fd9caSVladimir Oltean 1579f59fd9caSVladimir Oltean return ocelot_sb_occ_max_clear(ocelot, sb_index); 1580f59fd9caSVladimir Oltean } 1581f59fd9caSVladimir Oltean 1582f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1583f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1584f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1585f59fd9caSVladimir Oltean { 1586f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1587f59fd9caSVladimir Oltean 1588f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1589f59fd9caSVladimir Oltean p_cur, p_max); 1590f59fd9caSVladimir Oltean } 1591f59fd9caSVladimir Oltean 1592f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1593f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1594f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1595f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1596f59fd9caSVladimir Oltean { 1597f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1598f59fd9caSVladimir Oltean 1599f59fd9caSVladimir Oltean return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1600f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1601f59fd9caSVladimir Oltean } 1602f59fd9caSVladimir Oltean 1603a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port, 1604a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1605a026c50bSHoratiu Vultur { 1606a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1607a026c50bSHoratiu Vultur 1608a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1609a026c50bSHoratiu Vultur } 1610a026c50bSHoratiu Vultur 1611a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port, 1612a026c50bSHoratiu Vultur const struct switchdev_obj_mrp *mrp) 1613a026c50bSHoratiu Vultur { 1614a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1615a026c50bSHoratiu Vultur 1616a026c50bSHoratiu Vultur return ocelot_mrp_add(ocelot, port, mrp); 1617a026c50bSHoratiu Vultur } 1618a026c50bSHoratiu Vultur 1619a026c50bSHoratiu Vultur static int 1620a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1621a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1622a026c50bSHoratiu Vultur { 1623a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1624a026c50bSHoratiu Vultur 1625a026c50bSHoratiu Vultur return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1626a026c50bSHoratiu Vultur } 1627a026c50bSHoratiu Vultur 1628a026c50bSHoratiu Vultur static int 1629a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1630a026c50bSHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp) 1631a026c50bSHoratiu Vultur { 1632a026c50bSHoratiu Vultur struct ocelot *ocelot = ds->priv; 1633a026c50bSHoratiu Vultur 1634a026c50bSHoratiu Vultur return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1635a026c50bSHoratiu Vultur } 1636a026c50bSHoratiu Vultur 1637375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 163856051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1639adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 164056051948SVladimir Oltean .setup = felix_setup, 164156051948SVladimir Oltean .teardown = felix_teardown, 164256051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 164356051948SVladimir Oltean .get_strings = felix_get_strings, 164456051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 164556051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 164656051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 1647bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1648bdeced75SVladimir Oltean .phylink_mac_config = felix_phylink_mac_config, 1649bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1650bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 165156051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 165256051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 165356051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1654209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1655209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 1656421741eaSVladimir Oltean .port_pre_bridge_flags = felix_pre_bridge_flags, 1657421741eaSVladimir Oltean .port_bridge_flags = felix_bridge_flags, 165856051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 165956051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 16608fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 16618fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 16628fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 166356051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 166456051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 166556051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 166656051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1667c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1668c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1669c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1670c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 16710b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 16720b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1673fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1674fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 167507d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 167607d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 167707d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1678de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1679f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1680f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1681f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1682f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1683f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1684f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1685f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1686f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1687f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1688f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1689a026c50bSHoratiu Vultur .port_mrp_add = felix_mrp_add, 1690a026c50bSHoratiu Vultur .port_mrp_del = felix_mrp_del, 1691a026c50bSHoratiu Vultur .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1692a026c50bSHoratiu Vultur .port_mrp_del_ring_role = felix_mrp_del_ring_role, 16935da11eb4SVladimir Oltean .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 16945da11eb4SVladimir Oltean .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 169556051948SVladimir Oltean }; 1696319e4dd1SVladimir Oltean 1697319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1698319e4dd1SVladimir Oltean { 1699319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1700319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1701319e4dd1SVladimir Oltean 1702319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1703319e4dd1SVladimir Oltean return NULL; 1704319e4dd1SVladimir Oltean 1705319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1706319e4dd1SVladimir Oltean } 1707319e4dd1SVladimir Oltean 1708319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1709319e4dd1SVladimir Oltean { 1710319e4dd1SVladimir Oltean struct dsa_port *dp; 1711319e4dd1SVladimir Oltean 1712319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1713319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1714319e4dd1SVladimir Oltean return -EINVAL; 1715319e4dd1SVladimir Oltean 1716319e4dd1SVladimir Oltean return dp->index; 1717319e4dd1SVladimir Oltean } 1718