156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0 2adb3dccfSVladimir Oltean /* Copyright 2019-2021 NXP Semiconductors 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> 1784705fc1SMaxim Kochetkov #include <linux/platform_device.h> 18c0bcf537SYangbo Lu #include <linux/packing.h> 1956051948SVladimir Oltean #include <linux/module.h> 20bdeced75SVladimir Oltean #include <linux/of_net.h> 2156051948SVladimir Oltean #include <linux/pci.h> 2256051948SVladimir Oltean #include <linux/of.h> 23588d0550SIoana Ciornei #include <linux/pcs-lynx.h> 24fc411eaaSVladimir Oltean #include <net/pkt_sched.h> 2556051948SVladimir Oltean #include <net/dsa.h> 2656051948SVladimir Oltean #include "felix.h" 2756051948SVladimir Oltean 28e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid, 29e21268efSVladimir Oltean bool pvid, bool untagged) 30e21268efSVladimir Oltean { 31e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 32e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 33e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 34e21268efSVladimir Oltean int key_length, upstream, err; 35e21268efSVladimir Oltean 36e21268efSVladimir Oltean /* We don't need to install the rxvlan into the other ports' filtering 37e21268efSVladimir Oltean * tables, because we're just pushing the rxvlan when sending towards 38e21268efSVladimir Oltean * the CPU 39e21268efSVladimir Oltean */ 40e21268efSVladimir Oltean if (!pvid) 41e21268efSVladimir Oltean return 0; 42e21268efSVladimir Oltean 43e21268efSVladimir Oltean key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length; 44e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 45e21268efSVladimir Oltean 46e21268efSVladimir Oltean outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), 47e21268efSVladimir Oltean GFP_KERNEL); 48e21268efSVladimir Oltean if (!outer_tagging_rule) 49e21268efSVladimir Oltean return -ENOMEM; 50e21268efSVladimir Oltean 51e21268efSVladimir Oltean outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 52e21268efSVladimir Oltean outer_tagging_rule->prio = 1; 53e21268efSVladimir Oltean outer_tagging_rule->id.cookie = port; 54e21268efSVladimir Oltean outer_tagging_rule->id.tc_offload = false; 55e21268efSVladimir Oltean outer_tagging_rule->block_id = VCAP_ES0; 56e21268efSVladimir Oltean outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 57e21268efSVladimir Oltean outer_tagging_rule->lookup = 0; 58e21268efSVladimir Oltean outer_tagging_rule->ingress_port.value = port; 59e21268efSVladimir Oltean outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0); 60e21268efSVladimir Oltean outer_tagging_rule->egress_port.value = upstream; 61e21268efSVladimir Oltean outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0); 62e21268efSVladimir Oltean outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG; 63e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD; 64e21268efSVladimir Oltean outer_tagging_rule->action.tag_a_vid_sel = 1; 65e21268efSVladimir Oltean outer_tagging_rule->action.vid_a_val = vid; 66e21268efSVladimir Oltean 67e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL); 68e21268efSVladimir Oltean if (err) 69e21268efSVladimir Oltean kfree(outer_tagging_rule); 70e21268efSVladimir Oltean 71e21268efSVladimir Oltean return err; 72e21268efSVladimir Oltean } 73e21268efSVladimir Oltean 74e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid, 75e21268efSVladimir Oltean bool pvid, bool untagged) 76e21268efSVladimir Oltean { 77e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 78e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 79e21268efSVladimir Oltean struct dsa_switch *ds = felix->ds; 80e21268efSVladimir Oltean int upstream, err; 81e21268efSVladimir Oltean 82e21268efSVladimir Oltean /* tag_8021q.c assumes we are implementing this via port VLAN 83e21268efSVladimir Oltean * membership, which we aren't. So we don't need to add any VCAP filter 84e21268efSVladimir Oltean * for the CPU port. 85e21268efSVladimir Oltean */ 86e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 87e21268efSVladimir Oltean return 0; 88e21268efSVladimir Oltean 89e21268efSVladimir Oltean untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 90e21268efSVladimir Oltean if (!untagging_rule) 91e21268efSVladimir Oltean return -ENOMEM; 92e21268efSVladimir Oltean 93e21268efSVladimir Oltean redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 94e21268efSVladimir Oltean if (!redirect_rule) { 95e21268efSVladimir Oltean kfree(untagging_rule); 96e21268efSVladimir Oltean return -ENOMEM; 97e21268efSVladimir Oltean } 98e21268efSVladimir Oltean 99e21268efSVladimir Oltean upstream = dsa_upstream_port(ds, port); 100e21268efSVladimir Oltean 101e21268efSVladimir Oltean untagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 102e21268efSVladimir Oltean untagging_rule->ingress_port_mask = BIT(upstream); 103e21268efSVladimir Oltean untagging_rule->vlan.vid.value = vid; 104e21268efSVladimir Oltean untagging_rule->vlan.vid.mask = VLAN_VID_MASK; 105e21268efSVladimir Oltean untagging_rule->prio = 1; 106e21268efSVladimir Oltean untagging_rule->id.cookie = port; 107e21268efSVladimir Oltean untagging_rule->id.tc_offload = false; 108e21268efSVladimir Oltean untagging_rule->block_id = VCAP_IS1; 109e21268efSVladimir Oltean untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 110e21268efSVladimir Oltean untagging_rule->lookup = 0; 111e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt_ena = true; 112e21268efSVladimir Oltean untagging_rule->action.vlan_pop_cnt = 1; 113e21268efSVladimir Oltean untagging_rule->action.pag_override_mask = 0xff; 114e21268efSVladimir Oltean untagging_rule->action.pag_val = port; 115e21268efSVladimir Oltean 116e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL); 117e21268efSVladimir Oltean if (err) { 118e21268efSVladimir Oltean kfree(untagging_rule); 119e21268efSVladimir Oltean kfree(redirect_rule); 120e21268efSVladimir Oltean return err; 121e21268efSVladimir Oltean } 122e21268efSVladimir Oltean 123e21268efSVladimir Oltean redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 124e21268efSVladimir Oltean redirect_rule->ingress_port_mask = BIT(upstream); 125e21268efSVladimir Oltean redirect_rule->pag = port; 126e21268efSVladimir Oltean redirect_rule->prio = 1; 127e21268efSVladimir Oltean redirect_rule->id.cookie = port; 128e21268efSVladimir Oltean redirect_rule->id.tc_offload = false; 129e21268efSVladimir Oltean redirect_rule->block_id = VCAP_IS2; 130e21268efSVladimir Oltean redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 131e21268efSVladimir Oltean redirect_rule->lookup = 0; 132e21268efSVladimir Oltean redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 133e21268efSVladimir Oltean redirect_rule->action.port_mask = BIT(port); 134e21268efSVladimir Oltean 135e21268efSVladimir Oltean err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 136e21268efSVladimir Oltean if (err) { 137e21268efSVladimir Oltean ocelot_vcap_filter_del(ocelot, untagging_rule); 138e21268efSVladimir Oltean kfree(redirect_rule); 139e21268efSVladimir Oltean return err; 140e21268efSVladimir Oltean } 141e21268efSVladimir Oltean 142e21268efSVladimir Oltean return 0; 143e21268efSVladimir Oltean } 144e21268efSVladimir Oltean 145e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 146e21268efSVladimir Oltean u16 flags) 147e21268efSVladimir Oltean { 148e21268efSVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 149e21268efSVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 150e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 151e21268efSVladimir Oltean 152e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 153e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot), 154e21268efSVladimir Oltean port, vid, pvid, untagged); 155e21268efSVladimir Oltean 156e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 157e21268efSVladimir Oltean return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot), 158e21268efSVladimir Oltean port, vid, pvid, untagged); 159e21268efSVladimir Oltean 160e21268efSVladimir Oltean return 0; 161e21268efSVladimir Oltean } 162e21268efSVladimir Oltean 163e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid) 164e21268efSVladimir Oltean { 165e21268efSVladimir Oltean struct ocelot_vcap_filter *outer_tagging_rule; 166e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_es0; 167e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 168e21268efSVladimir Oltean 169e21268efSVladimir Oltean block_vcap_es0 = &ocelot->block[VCAP_ES0]; 170e21268efSVladimir Oltean 171e21268efSVladimir Oltean outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 172e21268efSVladimir Oltean port, false); 173e21268efSVladimir Oltean /* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid 174e21268efSVladimir Oltean * installing outer tagging ES0 rules where they weren't needed. 175e21268efSVladimir Oltean * But in rxvlan_del, the API doesn't give us the "flags" anymore, 176e21268efSVladimir Oltean * so that forces us to be slightly sloppy here, and just assume that 177e21268efSVladimir Oltean * if we didn't find an outer_tagging_rule it means that there was 178e21268efSVladimir Oltean * none in the first place, i.e. rxvlan_del is called on a non-pvid 179e21268efSVladimir Oltean * port. This is most probably true though. 180e21268efSVladimir Oltean */ 181e21268efSVladimir Oltean if (!outer_tagging_rule) 182e21268efSVladimir Oltean return 0; 183e21268efSVladimir Oltean 184e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 185e21268efSVladimir Oltean } 186e21268efSVladimir Oltean 187e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid) 188e21268efSVladimir Oltean { 189e21268efSVladimir Oltean struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 190e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is1; 191e21268efSVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 192e21268efSVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 193e21268efSVladimir Oltean int err; 194e21268efSVladimir Oltean 195e21268efSVladimir Oltean if (ocelot->ports[port]->is_dsa_8021q_cpu) 196e21268efSVladimir Oltean return 0; 197e21268efSVladimir Oltean 198e21268efSVladimir Oltean block_vcap_is1 = &ocelot->block[VCAP_IS1]; 199e21268efSVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 200e21268efSVladimir Oltean 201e21268efSVladimir Oltean untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 202e21268efSVladimir Oltean port, false); 203e21268efSVladimir Oltean if (!untagging_rule) 204e21268efSVladimir Oltean return 0; 205e21268efSVladimir Oltean 206e21268efSVladimir Oltean err = ocelot_vcap_filter_del(ocelot, untagging_rule); 207e21268efSVladimir Oltean if (err) 208e21268efSVladimir Oltean return err; 209e21268efSVladimir Oltean 210e21268efSVladimir Oltean redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 211e21268efSVladimir Oltean port, false); 212e21268efSVladimir Oltean if (!redirect_rule) 213e21268efSVladimir Oltean return 0; 214e21268efSVladimir Oltean 215e21268efSVladimir Oltean return ocelot_vcap_filter_del(ocelot, redirect_rule); 216e21268efSVladimir Oltean } 217e21268efSVladimir Oltean 218e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 219e21268efSVladimir Oltean { 220e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 221e21268efSVladimir Oltean 222e21268efSVladimir Oltean if (vid_is_dsa_8021q_rxvlan(vid)) 223e21268efSVladimir Oltean return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot), 224e21268efSVladimir Oltean port, vid); 225e21268efSVladimir Oltean 226e21268efSVladimir Oltean if (vid_is_dsa_8021q_txvlan(vid)) 227e21268efSVladimir Oltean return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot), 228e21268efSVladimir Oltean port, vid); 229e21268efSVladimir Oltean 230e21268efSVladimir Oltean return 0; 231e21268efSVladimir Oltean } 232e21268efSVladimir Oltean 233e21268efSVladimir Oltean static const struct dsa_8021q_ops felix_tag_8021q_ops = { 234e21268efSVladimir Oltean .vlan_add = felix_tag_8021q_vlan_add, 235e21268efSVladimir Oltean .vlan_del = felix_tag_8021q_vlan_del, 236e21268efSVladimir Oltean }; 237e21268efSVladimir Oltean 238e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC 239e21268efSVladimir Oltean * connected internally to the enetc or fman DSA master can be configured to 240e21268efSVladimir Oltean * use the software-defined tag_8021q frame format. As far as the hardware is 241e21268efSVladimir Oltean * concerned, it thinks it is a "dumb switch" - the queues of the CPU port 242e21268efSVladimir Oltean * module are now disconnected from it, but can still be accessed through 243e21268efSVladimir Oltean * register-based MMIO. 244e21268efSVladimir Oltean */ 245e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port) 246e21268efSVladimir Oltean { 247e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = true; 248e21268efSVladimir Oltean ocelot->npi = -1; 249e21268efSVladimir Oltean 250e21268efSVladimir Oltean /* Overwrite PGID_CPU with the non-tagging port */ 251e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU); 252e21268efSVladimir Oltean 253e21268efSVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 254e21268efSVladimir Oltean } 255e21268efSVladimir Oltean 256e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port) 257e21268efSVladimir Oltean { 258e21268efSVladimir Oltean ocelot->ports[port]->is_dsa_8021q_cpu = false; 259e21268efSVladimir Oltean 260e21268efSVladimir Oltean /* Restore PGID_CPU */ 261e21268efSVladimir Oltean ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID, 262e21268efSVladimir Oltean PGID_CPU); 263e21268efSVladimir Oltean 264e21268efSVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 265e21268efSVladimir Oltean } 266e21268efSVladimir Oltean 267e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu) 268e21268efSVladimir Oltean { 269e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 270e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 271e21268efSVladimir Oltean unsigned long cpu_flood; 272e21268efSVladimir Oltean int port, err; 273e21268efSVladimir Oltean 274e21268efSVladimir Oltean felix_8021q_cpu_port_init(ocelot, cpu); 275e21268efSVladimir Oltean 276e21268efSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 277e21268efSVladimir Oltean if (dsa_is_unused_port(ds, port)) 278e21268efSVladimir Oltean continue; 279e21268efSVladimir Oltean 280e21268efSVladimir Oltean /* This overwrites ocelot_init(): 281e21268efSVladimir Oltean * Do not forward BPDU frames to the CPU port module, 282e21268efSVladimir Oltean * for 2 reasons: 283e21268efSVladimir Oltean * - When these packets are injected from the tag_8021q 284e21268efSVladimir Oltean * CPU port, we want them to go out, not loop back 285e21268efSVladimir Oltean * into the system. 286e21268efSVladimir Oltean * - STP traffic ingressing on a user port should go to 287e21268efSVladimir Oltean * the tag_8021q CPU port, not to the hardware CPU 288e21268efSVladimir Oltean * port module. 289e21268efSVladimir Oltean */ 290e21268efSVladimir Oltean ocelot_write_gix(ocelot, 291e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 292e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, port); 293e21268efSVladimir Oltean } 294e21268efSVladimir Oltean 295e21268efSVladimir Oltean /* In tag_8021q mode, the CPU port module is unused. So we 296e21268efSVladimir Oltean * want to disable flooding of any kind to the CPU port module, 297e21268efSVladimir Oltean * since packets going there will end in a black hole. 298e21268efSVladimir Oltean */ 299e21268efSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 300e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC); 301e21268efSVladimir Oltean ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC); 302e21268efSVladimir Oltean 303e21268efSVladimir Oltean felix->dsa_8021q_ctx = kzalloc(sizeof(*felix->dsa_8021q_ctx), 304e21268efSVladimir Oltean GFP_KERNEL); 305e21268efSVladimir Oltean if (!felix->dsa_8021q_ctx) 306e21268efSVladimir Oltean return -ENOMEM; 307e21268efSVladimir Oltean 308e21268efSVladimir Oltean felix->dsa_8021q_ctx->ops = &felix_tag_8021q_ops; 309e21268efSVladimir Oltean felix->dsa_8021q_ctx->proto = htons(ETH_P_8021AD); 310e21268efSVladimir Oltean felix->dsa_8021q_ctx->ds = ds; 311e21268efSVladimir Oltean 312e21268efSVladimir Oltean err = dsa_8021q_setup(felix->dsa_8021q_ctx, true); 313e21268efSVladimir Oltean if (err) 314e21268efSVladimir Oltean goto out_free_dsa_8021_ctx; 315e21268efSVladimir Oltean 316e21268efSVladimir Oltean return 0; 317e21268efSVladimir Oltean 318e21268efSVladimir Oltean out_free_dsa_8021_ctx: 319e21268efSVladimir Oltean kfree(felix->dsa_8021q_ctx); 320e21268efSVladimir Oltean return err; 321e21268efSVladimir Oltean } 322e21268efSVladimir Oltean 323e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu) 324e21268efSVladimir Oltean { 325e21268efSVladimir Oltean struct ocelot *ocelot = ds->priv; 326e21268efSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 327e21268efSVladimir Oltean int err, port; 328e21268efSVladimir Oltean 329e21268efSVladimir Oltean err = dsa_8021q_setup(felix->dsa_8021q_ctx, false); 330e21268efSVladimir Oltean if (err) 331e21268efSVladimir Oltean dev_err(ds->dev, "dsa_8021q_setup returned %d", err); 332e21268efSVladimir Oltean 333e21268efSVladimir Oltean kfree(felix->dsa_8021q_ctx); 334e21268efSVladimir Oltean 335e21268efSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 336e21268efSVladimir Oltean if (dsa_is_unused_port(ds, port)) 337e21268efSVladimir Oltean continue; 338e21268efSVladimir Oltean 339e21268efSVladimir Oltean /* Restore the logic from ocelot_init: 340e21268efSVladimir Oltean * do not forward BPDU frames to the front ports. 341e21268efSVladimir Oltean */ 342e21268efSVladimir Oltean ocelot_write_gix(ocelot, 343e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 344e21268efSVladimir Oltean ANA_PORT_CPU_FWD_BPDU_CFG, 345e21268efSVladimir Oltean port); 346e21268efSVladimir Oltean } 347e21268efSVladimir Oltean 348e21268efSVladimir Oltean felix_8021q_cpu_port_deinit(ocelot, cpu); 349e21268efSVladimir Oltean } 350e21268efSVladimir Oltean 351adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This 352adb3dccfSVladimir Oltean * is the mode through which frames can be injected from and extracted to an 353adb3dccfSVladimir Oltean * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 354adb3dccfSVladimir Oltean * running Linux, and this forms a DSA setup together with the enetc or fman 355adb3dccfSVladimir Oltean * DSA master. 356adb3dccfSVladimir Oltean */ 357adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port) 358adb3dccfSVladimir Oltean { 359adb3dccfSVladimir Oltean ocelot->npi = port; 360adb3dccfSVladimir Oltean 361adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 362adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 363adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 364adb3dccfSVladimir Oltean 365adb3dccfSVladimir Oltean /* NPI port Injection/Extraction configuration */ 366adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 367adb3dccfSVladimir Oltean ocelot->npi_xtr_prefix); 368adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 369adb3dccfSVladimir Oltean ocelot->npi_inj_prefix); 370adb3dccfSVladimir Oltean 371adb3dccfSVladimir Oltean /* Disable transmission of pause frames */ 372adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 373adb3dccfSVladimir Oltean } 374adb3dccfSVladimir Oltean 375adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 376adb3dccfSVladimir Oltean { 377adb3dccfSVladimir Oltean /* Restore hardware defaults */ 378adb3dccfSVladimir Oltean int unused_port = ocelot->num_phys_ports + 2; 379adb3dccfSVladimir Oltean 380adb3dccfSVladimir Oltean ocelot->npi = -1; 381adb3dccfSVladimir Oltean 382adb3dccfSVladimir Oltean ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 383adb3dccfSVladimir Oltean QSYS_EXT_CPU_CFG); 384adb3dccfSVladimir Oltean 385adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 386adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 387adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 388adb3dccfSVladimir Oltean OCELOT_TAG_PREFIX_DISABLED); 389adb3dccfSVladimir Oltean 390adb3dccfSVladimir Oltean /* Enable transmission of pause frames */ 391adb3dccfSVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 392adb3dccfSVladimir Oltean } 393adb3dccfSVladimir Oltean 394adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu) 395adb3dccfSVladimir Oltean { 396adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 397adb3dccfSVladimir Oltean unsigned long cpu_flood; 398adb3dccfSVladimir Oltean 399adb3dccfSVladimir Oltean felix_npi_port_init(ocelot, cpu); 400adb3dccfSVladimir Oltean 401adb3dccfSVladimir Oltean /* Include the CPU port module (and indirectly, the NPI port) 402adb3dccfSVladimir Oltean * in the forwarding mask for unknown unicast - the hardware 403adb3dccfSVladimir Oltean * default value for ANA_FLOODING_FLD_UNICAST excludes 404adb3dccfSVladimir Oltean * BIT(ocelot->num_phys_ports), and so does ocelot_init, 405adb3dccfSVladimir Oltean * since Ocelot relies on whitelisting MAC addresses towards 406adb3dccfSVladimir Oltean * PGID_CPU. 407adb3dccfSVladimir Oltean * We do this because DSA does not yet perform RX filtering, 408adb3dccfSVladimir Oltean * and the NPI port does not perform source address learning, 409adb3dccfSVladimir Oltean * so traffic sent to Linux is effectively unknown from the 410adb3dccfSVladimir Oltean * switch's perspective. 411adb3dccfSVladimir Oltean */ 412adb3dccfSVladimir Oltean cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 413adb3dccfSVladimir Oltean ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC); 414adb3dccfSVladimir Oltean 415adb3dccfSVladimir Oltean return 0; 416adb3dccfSVladimir Oltean } 417adb3dccfSVladimir Oltean 418adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu) 419adb3dccfSVladimir Oltean { 420adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 421adb3dccfSVladimir Oltean 422adb3dccfSVladimir Oltean felix_npi_port_deinit(ocelot, cpu); 423adb3dccfSVladimir Oltean } 424adb3dccfSVladimir Oltean 425adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu, 426adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 427adb3dccfSVladimir Oltean { 428adb3dccfSVladimir Oltean int err; 429adb3dccfSVladimir Oltean 430adb3dccfSVladimir Oltean switch (proto) { 431adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 432adb3dccfSVladimir Oltean err = felix_setup_tag_npi(ds, cpu); 433adb3dccfSVladimir Oltean break; 434e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 435e21268efSVladimir Oltean err = felix_setup_tag_8021q(ds, cpu); 436e21268efSVladimir Oltean break; 437adb3dccfSVladimir Oltean default: 438adb3dccfSVladimir Oltean err = -EPROTONOSUPPORT; 439adb3dccfSVladimir Oltean } 440adb3dccfSVladimir Oltean 441adb3dccfSVladimir Oltean return err; 442adb3dccfSVladimir Oltean } 443adb3dccfSVladimir Oltean 444adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu, 445adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 446adb3dccfSVladimir Oltean { 447adb3dccfSVladimir Oltean switch (proto) { 448adb3dccfSVladimir Oltean case DSA_TAG_PROTO_OCELOT: 449adb3dccfSVladimir Oltean felix_teardown_tag_npi(ds, cpu); 450adb3dccfSVladimir Oltean break; 451e21268efSVladimir Oltean case DSA_TAG_PROTO_OCELOT_8021Q: 452e21268efSVladimir Oltean felix_teardown_tag_8021q(ds, cpu); 453e21268efSVladimir Oltean break; 454adb3dccfSVladimir Oltean default: 455adb3dccfSVladimir Oltean break; 456adb3dccfSVladimir Oltean } 457adb3dccfSVladimir Oltean } 458adb3dccfSVladimir Oltean 459e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the 460e21268efSVladimir Oltean * tag_8021q setup can fail, the NPI setup can't. So either the change is made, 461e21268efSVladimir Oltean * or the restoration is guaranteed to work. 462e21268efSVladimir Oltean */ 463adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu, 464adb3dccfSVladimir Oltean enum dsa_tag_protocol proto) 465adb3dccfSVladimir Oltean { 466adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 467adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 468adb3dccfSVladimir Oltean enum dsa_tag_protocol old_proto = felix->tag_proto; 469adb3dccfSVladimir Oltean int err; 470adb3dccfSVladimir Oltean 471e21268efSVladimir Oltean if (proto != DSA_TAG_PROTO_OCELOT && 472e21268efSVladimir Oltean proto != DSA_TAG_PROTO_OCELOT_8021Q) 473adb3dccfSVladimir Oltean return -EPROTONOSUPPORT; 474adb3dccfSVladimir Oltean 475adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, cpu, old_proto); 476adb3dccfSVladimir Oltean 477adb3dccfSVladimir Oltean err = felix_set_tag_protocol(ds, cpu, proto); 478adb3dccfSVladimir Oltean if (err) { 479adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, cpu, old_proto); 480adb3dccfSVladimir Oltean return err; 481adb3dccfSVladimir Oltean } 482adb3dccfSVladimir Oltean 483adb3dccfSVladimir Oltean felix->tag_proto = proto; 484adb3dccfSVladimir Oltean 485adb3dccfSVladimir Oltean return 0; 486adb3dccfSVladimir Oltean } 487adb3dccfSVladimir Oltean 48856051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 4894d776482SFlorian Fainelli int port, 4904d776482SFlorian Fainelli enum dsa_tag_protocol mp) 49156051948SVladimir Oltean { 492adb3dccfSVladimir Oltean struct ocelot *ocelot = ds->priv; 493adb3dccfSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 494adb3dccfSVladimir Oltean 495adb3dccfSVladimir Oltean return felix->tag_proto; 49656051948SVladimir Oltean } 49756051948SVladimir Oltean 49856051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds, 49956051948SVladimir Oltean unsigned int ageing_time) 50056051948SVladimir Oltean { 50156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 50256051948SVladimir Oltean 50356051948SVladimir Oltean ocelot_set_ageing_time(ocelot, ageing_time); 50456051948SVladimir Oltean 50556051948SVladimir Oltean return 0; 50656051948SVladimir Oltean } 50756051948SVladimir Oltean 50856051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port, 50956051948SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 51056051948SVladimir Oltean { 51156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 51256051948SVladimir Oltean 51356051948SVladimir Oltean return ocelot_fdb_dump(ocelot, port, cb, data); 51456051948SVladimir Oltean } 51556051948SVladimir Oltean 51656051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port, 51756051948SVladimir Oltean const unsigned char *addr, u16 vid) 51856051948SVladimir Oltean { 51956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 52056051948SVladimir Oltean 52187b0f983SVladimir Oltean return ocelot_fdb_add(ocelot, port, addr, vid); 52256051948SVladimir Oltean } 52356051948SVladimir Oltean 52456051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port, 52556051948SVladimir Oltean const unsigned char *addr, u16 vid) 52656051948SVladimir Oltean { 52756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 52856051948SVladimir Oltean 52956051948SVladimir Oltean return ocelot_fdb_del(ocelot, port, addr, vid); 53056051948SVladimir Oltean } 53156051948SVladimir Oltean 532a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port, 533209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 534209edf95SVladimir Oltean { 535209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 536209edf95SVladimir Oltean 537a52b2da7SVladimir Oltean return ocelot_port_mdb_add(ocelot, port, mdb); 538209edf95SVladimir Oltean } 539209edf95SVladimir Oltean 540209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port, 541209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 542209edf95SVladimir Oltean { 543209edf95SVladimir Oltean struct ocelot *ocelot = ds->priv; 544209edf95SVladimir Oltean 545209edf95SVladimir Oltean return ocelot_port_mdb_del(ocelot, port, mdb); 546209edf95SVladimir Oltean } 547209edf95SVladimir Oltean 54856051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 54956051948SVladimir Oltean u8 state) 55056051948SVladimir Oltean { 55156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 55256051948SVladimir Oltean 55356051948SVladimir Oltean return ocelot_bridge_stp_state_set(ocelot, port, state); 55456051948SVladimir Oltean } 55556051948SVladimir Oltean 55656051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port, 55756051948SVladimir Oltean struct net_device *br) 55856051948SVladimir Oltean { 55956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 56056051948SVladimir Oltean 56156051948SVladimir Oltean return ocelot_port_bridge_join(ocelot, port, br); 56256051948SVladimir Oltean } 56356051948SVladimir Oltean 56456051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port, 56556051948SVladimir Oltean struct net_device *br) 56656051948SVladimir Oltean { 56756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 56856051948SVladimir Oltean 56956051948SVladimir Oltean ocelot_port_bridge_leave(ocelot, port, br); 57056051948SVladimir Oltean } 57156051948SVladimir Oltean 572*8fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port, 573*8fe6832eSVladimir Oltean struct net_device *bond, 574*8fe6832eSVladimir Oltean struct netdev_lag_upper_info *info) 575*8fe6832eSVladimir Oltean { 576*8fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 577*8fe6832eSVladimir Oltean 578*8fe6832eSVladimir Oltean return ocelot_port_lag_join(ocelot, port, bond, info); 579*8fe6832eSVladimir Oltean } 580*8fe6832eSVladimir Oltean 581*8fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port, 582*8fe6832eSVladimir Oltean struct net_device *bond) 583*8fe6832eSVladimir Oltean { 584*8fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 585*8fe6832eSVladimir Oltean 586*8fe6832eSVladimir Oltean ocelot_port_lag_leave(ocelot, port, bond); 587*8fe6832eSVladimir Oltean 588*8fe6832eSVladimir Oltean return 0; 589*8fe6832eSVladimir Oltean } 590*8fe6832eSVladimir Oltean 591*8fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port) 592*8fe6832eSVladimir Oltean { 593*8fe6832eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 594*8fe6832eSVladimir Oltean struct ocelot *ocelot = ds->priv; 595*8fe6832eSVladimir Oltean 596*8fe6832eSVladimir Oltean ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 597*8fe6832eSVladimir Oltean 598*8fe6832eSVladimir Oltean return 0; 599*8fe6832eSVladimir Oltean } 600*8fe6832eSVladimir Oltean 60156051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port, 60256051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 60356051948SVladimir Oltean { 6042f0402feSVladimir Oltean struct ocelot *ocelot = ds->priv; 605b7a9e0daSVladimir Oltean u16 flags = vlan->flags; 6062f0402feSVladimir Oltean 6079a720680SVladimir Oltean /* Ocelot switches copy frames as-is to the CPU, so the flags: 6089a720680SVladimir Oltean * egress-untagged or not, pvid or not, make no difference. This 6099a720680SVladimir Oltean * behavior is already better than what DSA just tries to approximate 6109a720680SVladimir Oltean * when it installs the VLAN with the same flags on the CPU port. 6119a720680SVladimir Oltean * Just accept any configuration, and don't let ocelot deny installing 6129a720680SVladimir Oltean * multiple native VLANs on the NPI port, because the switch doesn't 6139a720680SVladimir Oltean * look at the port tag settings towards the NPI interface anyway. 6149a720680SVladimir Oltean */ 6159a720680SVladimir Oltean if (port == ocelot->npi) 6169a720680SVladimir Oltean return 0; 6179a720680SVladimir Oltean 618b7a9e0daSVladimir Oltean return ocelot_vlan_prepare(ocelot, port, vlan->vid, 6192f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 6202f0402feSVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 62156051948SVladimir Oltean } 62256051948SVladimir Oltean 623bae33f2bSVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled) 62456051948SVladimir Oltean { 62556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 62656051948SVladimir Oltean 627bae33f2bSVladimir Oltean return ocelot_port_vlan_filtering(ocelot, port, enabled); 62856051948SVladimir Oltean } 62956051948SVladimir Oltean 6301958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port, 63156051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 63256051948SVladimir Oltean { 63356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 634183be6f9SVladimir Oltean u16 flags = vlan->flags; 6351958d581SVladimir Oltean int err; 63656051948SVladimir Oltean 6371958d581SVladimir Oltean err = felix_vlan_prepare(ds, port, vlan); 6381958d581SVladimir Oltean if (err) 6391958d581SVladimir Oltean return err; 6401958d581SVladimir Oltean 6411958d581SVladimir Oltean return ocelot_vlan_add(ocelot, port, vlan->vid, 642183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_PVID, 643183be6f9SVladimir Oltean flags & BRIDGE_VLAN_INFO_UNTAGGED); 64456051948SVladimir Oltean } 64556051948SVladimir Oltean 64656051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port, 64756051948SVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 64856051948SVladimir Oltean { 64956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 65056051948SVladimir Oltean 651b7a9e0daSVladimir Oltean return ocelot_vlan_del(ocelot, port, vlan->vid); 65256051948SVladimir Oltean } 65356051948SVladimir Oltean 65456051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port, 65556051948SVladimir Oltean struct phy_device *phy) 65656051948SVladimir Oltean { 65756051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 65856051948SVladimir Oltean 65956051948SVladimir Oltean ocelot_port_enable(ocelot, port, phy); 66056051948SVladimir Oltean 66156051948SVladimir Oltean return 0; 66256051948SVladimir Oltean } 66356051948SVladimir Oltean 66456051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port) 66556051948SVladimir Oltean { 66656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 66756051948SVladimir Oltean 66856051948SVladimir Oltean return ocelot_port_disable(ocelot, port); 66956051948SVladimir Oltean } 67056051948SVladimir Oltean 671bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port, 672bdeced75SVladimir Oltean unsigned long *supported, 673bdeced75SVladimir Oltean struct phylink_link_state *state) 674bdeced75SVladimir Oltean { 675bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 676375e1314SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 677bdeced75SVladimir Oltean 678375e1314SVladimir Oltean if (felix->info->phylink_validate) 679375e1314SVladimir Oltean felix->info->phylink_validate(ocelot, port, supported, state); 680bdeced75SVladimir Oltean } 681bdeced75SVladimir Oltean 682bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port, 683bdeced75SVladimir Oltean unsigned int link_an_mode, 684bdeced75SVladimir Oltean const struct phylink_link_state *state) 685bdeced75SVladimir Oltean { 686bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 687bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 688588d0550SIoana Ciornei struct dsa_port *dp = dsa_to_port(ds, port); 689bdeced75SVladimir Oltean 690588d0550SIoana Ciornei if (felix->pcs[port]) 691588d0550SIoana Ciornei phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs); 692bdeced75SVladimir Oltean } 693bdeced75SVladimir Oltean 694bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 695bdeced75SVladimir Oltean unsigned int link_an_mode, 696bdeced75SVladimir Oltean phy_interface_t interface) 697bdeced75SVladimir Oltean { 698bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 699bdeced75SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 700bdeced75SVladimir Oltean 701bdeced75SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG); 702886e1387SVladimir Oltean ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); 703bdeced75SVladimir Oltean } 704bdeced75SVladimir Oltean 705bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 706bdeced75SVladimir Oltean unsigned int link_an_mode, 707bdeced75SVladimir Oltean phy_interface_t interface, 7085b502a7bSRussell King struct phy_device *phydev, 7095b502a7bSRussell King int speed, int duplex, 7105b502a7bSRussell King bool tx_pause, bool rx_pause) 711bdeced75SVladimir Oltean { 712bdeced75SVladimir Oltean struct ocelot *ocelot = ds->priv; 713bdeced75SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 7147e14a2dcSVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 7157e14a2dcSVladimir Oltean u32 mac_fc_cfg; 716bdeced75SVladimir Oltean 7177e14a2dcSVladimir Oltean /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and 7187e14a2dcSVladimir Oltean * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is 7197e14a2dcSVladimir Oltean * integrated is that the MAC speed is fixed and it's the PCS who is 7207e14a2dcSVladimir Oltean * performing the rate adaptation, so we have to write "1000Mbps" into 7217e14a2dcSVladimir Oltean * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default 7227e14a2dcSVladimir Oltean * value). 7237e14a2dcSVladimir Oltean */ 7247e14a2dcSVladimir Oltean ocelot_port_writel(ocelot_port, 7257e14a2dcSVladimir Oltean DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000), 7267e14a2dcSVladimir Oltean DEV_CLOCK_CFG); 7277e14a2dcSVladimir Oltean 7287e14a2dcSVladimir Oltean switch (speed) { 7297e14a2dcSVladimir Oltean case SPEED_10: 7307e14a2dcSVladimir Oltean mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3); 7317e14a2dcSVladimir Oltean break; 7327e14a2dcSVladimir Oltean case SPEED_100: 7337e14a2dcSVladimir Oltean mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2); 7347e14a2dcSVladimir Oltean break; 7357e14a2dcSVladimir Oltean case SPEED_1000: 7367e14a2dcSVladimir Oltean case SPEED_2500: 7377e14a2dcSVladimir Oltean mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1); 7387e14a2dcSVladimir Oltean break; 7397e14a2dcSVladimir Oltean default: 7407e14a2dcSVladimir Oltean dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n", 7417e14a2dcSVladimir Oltean port, speed); 7427e14a2dcSVladimir Oltean return; 7437e14a2dcSVladimir Oltean } 7447e14a2dcSVladimir Oltean 7457e14a2dcSVladimir Oltean /* handle Rx pause in all cases, with 2500base-X this is used for rate 7467e14a2dcSVladimir Oltean * adaptation. 7477e14a2dcSVladimir Oltean */ 7487e14a2dcSVladimir Oltean mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA; 7497e14a2dcSVladimir Oltean 7507e14a2dcSVladimir Oltean if (tx_pause) 7517e14a2dcSVladimir Oltean mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA | 7527e14a2dcSVladimir Oltean SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | 7537e14a2dcSVladimir Oltean SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | 7547e14a2dcSVladimir Oltean SYS_MAC_FC_CFG_ZERO_PAUSE_ENA; 7557e14a2dcSVladimir Oltean 7567e14a2dcSVladimir Oltean /* Flow control. Link speed is only used here to evaluate the time 7577e14a2dcSVladimir Oltean * specification in incoming pause frames. 7587e14a2dcSVladimir Oltean */ 7597e14a2dcSVladimir Oltean ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port); 7607e14a2dcSVladimir Oltean 7617e14a2dcSVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); 7627e14a2dcSVladimir Oltean 7637e14a2dcSVladimir Oltean /* Undo the effects of felix_phylink_mac_link_down: 7647e14a2dcSVladimir Oltean * enable MAC module 7657e14a2dcSVladimir Oltean */ 766bdeced75SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | 767bdeced75SVladimir Oltean DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); 768bdeced75SVladimir Oltean 769bdeced75SVladimir Oltean /* Enable receiving frames on the port, and activate auto-learning of 770bdeced75SVladimir Oltean * MAC addresses. 771bdeced75SVladimir Oltean */ 772bdeced75SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 773bdeced75SVladimir Oltean ANA_PORT_PORT_CFG_RECV_ENA | 774bdeced75SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 775bdeced75SVladimir Oltean ANA_PORT_PORT_CFG, port); 776bdeced75SVladimir Oltean 777bdeced75SVladimir Oltean /* Core: Enable port for frame transfer */ 778886e1387SVladimir Oltean ocelot_fields_write(ocelot, port, 779886e1387SVladimir Oltean QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 7807e14a2dcSVladimir Oltean 7817e14a2dcSVladimir Oltean if (felix->info->port_sched_speed_set) 7827e14a2dcSVladimir Oltean felix->info->port_sched_speed_set(ocelot, port, speed); 783bdeced75SVladimir Oltean } 784bdeced75SVladimir Oltean 785bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 786bd2b3161SXiaoliang Yang { 787bd2b3161SXiaoliang Yang int i; 788bd2b3161SXiaoliang Yang 789bd2b3161SXiaoliang Yang ocelot_rmw_gix(ocelot, 790bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 791bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG_QOS_PCP_ENA, 792bd2b3161SXiaoliang Yang ANA_PORT_QOS_CFG, 793bd2b3161SXiaoliang Yang port); 794bd2b3161SXiaoliang Yang 79570d39a6eSVladimir Oltean for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 796bd2b3161SXiaoliang Yang ocelot_rmw_ix(ocelot, 797bd2b3161SXiaoliang Yang (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 798bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 799bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 800bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 801bd2b3161SXiaoliang Yang ANA_PORT_PCP_DEI_MAP, 802bd2b3161SXiaoliang Yang port, i); 803bd2b3161SXiaoliang Yang } 804bd2b3161SXiaoliang Yang } 805bd2b3161SXiaoliang Yang 80656051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port, 80756051948SVladimir Oltean u32 stringset, u8 *data) 80856051948SVladimir Oltean { 80956051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 81056051948SVladimir Oltean 81156051948SVladimir Oltean return ocelot_get_strings(ocelot, port, stringset, data); 81256051948SVladimir Oltean } 81356051948SVladimir Oltean 81456051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 81556051948SVladimir Oltean { 81656051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 81756051948SVladimir Oltean 81856051948SVladimir Oltean ocelot_get_ethtool_stats(ocelot, port, data); 81956051948SVladimir Oltean } 82056051948SVladimir Oltean 82156051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 82256051948SVladimir Oltean { 82356051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 82456051948SVladimir Oltean 82556051948SVladimir Oltean return ocelot_get_sset_count(ocelot, port, sset); 82656051948SVladimir Oltean } 82756051948SVladimir Oltean 82856051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port, 82956051948SVladimir Oltean struct ethtool_ts_info *info) 83056051948SVladimir Oltean { 83156051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 83256051948SVladimir Oltean 83356051948SVladimir Oltean return ocelot_get_ts_info(ocelot, port, info); 83456051948SVladimir Oltean } 83556051948SVladimir Oltean 836bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix, 837bdeced75SVladimir Oltean struct device_node *ports_node, 838bdeced75SVladimir Oltean phy_interface_t *port_phy_modes) 839bdeced75SVladimir Oltean { 840bdeced75SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 841bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 842bdeced75SVladimir Oltean struct device_node *child; 843bdeced75SVladimir Oltean 84437fe45adSVladimir Oltean for_each_available_child_of_node(ports_node, child) { 845bdeced75SVladimir Oltean phy_interface_t phy_mode; 846bdeced75SVladimir Oltean u32 port; 847bdeced75SVladimir Oltean int err; 848bdeced75SVladimir Oltean 849bdeced75SVladimir Oltean /* Get switch port number from DT */ 850bdeced75SVladimir Oltean if (of_property_read_u32(child, "reg", &port) < 0) { 851bdeced75SVladimir Oltean dev_err(dev, "Port number not defined in device tree " 852bdeced75SVladimir Oltean "(property \"reg\")\n"); 853bdeced75SVladimir Oltean of_node_put(child); 854bdeced75SVladimir Oltean return -ENODEV; 855bdeced75SVladimir Oltean } 856bdeced75SVladimir Oltean 857bdeced75SVladimir Oltean /* Get PHY mode from DT */ 858bdeced75SVladimir Oltean err = of_get_phy_mode(child, &phy_mode); 859bdeced75SVladimir Oltean if (err) { 860bdeced75SVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 861bdeced75SVladimir Oltean "phy-interface-type property for port %d\n", 862bdeced75SVladimir Oltean port); 863bdeced75SVladimir Oltean of_node_put(child); 864bdeced75SVladimir Oltean return -ENODEV; 865bdeced75SVladimir Oltean } 866bdeced75SVladimir Oltean 867bdeced75SVladimir Oltean err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 868bdeced75SVladimir Oltean if (err < 0) { 869bdeced75SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d\n", 870bdeced75SVladimir Oltean phy_modes(phy_mode), port); 87159ebb430SSumera Priyadarsini of_node_put(child); 872bdeced75SVladimir Oltean return err; 873bdeced75SVladimir Oltean } 874bdeced75SVladimir Oltean 875bdeced75SVladimir Oltean port_phy_modes[port] = phy_mode; 876bdeced75SVladimir Oltean } 877bdeced75SVladimir Oltean 878bdeced75SVladimir Oltean return 0; 879bdeced75SVladimir Oltean } 880bdeced75SVladimir Oltean 881bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 882bdeced75SVladimir Oltean { 883bdeced75SVladimir Oltean struct device *dev = felix->ocelot.dev; 884bdeced75SVladimir Oltean struct device_node *switch_node; 885bdeced75SVladimir Oltean struct device_node *ports_node; 886bdeced75SVladimir Oltean int err; 887bdeced75SVladimir Oltean 888bdeced75SVladimir Oltean switch_node = dev->of_node; 889bdeced75SVladimir Oltean 890bdeced75SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 891bdeced75SVladimir Oltean if (!ports_node) { 892bdeced75SVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 893bdeced75SVladimir Oltean return -ENODEV; 894bdeced75SVladimir Oltean } 895bdeced75SVladimir Oltean 896bdeced75SVladimir Oltean err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 897bdeced75SVladimir Oltean of_node_put(ports_node); 898bdeced75SVladimir Oltean 899bdeced75SVladimir Oltean return err; 900bdeced75SVladimir Oltean } 901bdeced75SVladimir Oltean 90256051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports) 90356051948SVladimir Oltean { 90456051948SVladimir Oltean struct ocelot *ocelot = &felix->ocelot; 905bdeced75SVladimir Oltean phy_interface_t *port_phy_modes; 906b4024c9eSClaudiu Manoil struct resource res; 90756051948SVladimir Oltean int port, i, err; 90856051948SVladimir Oltean 90956051948SVladimir Oltean ocelot->num_phys_ports = num_phys_ports; 91056051948SVladimir Oltean ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 91156051948SVladimir Oltean sizeof(struct ocelot_port *), GFP_KERNEL); 91256051948SVladimir Oltean if (!ocelot->ports) 91356051948SVladimir Oltean return -ENOMEM; 91456051948SVladimir Oltean 91556051948SVladimir Oltean ocelot->map = felix->info->map; 91656051948SVladimir Oltean ocelot->stats_layout = felix->info->stats_layout; 91756051948SVladimir Oltean ocelot->num_stats = felix->info->num_stats; 91821ce7f3eSVladimir Oltean ocelot->num_mact_rows = felix->info->num_mact_rows; 91907d985eeSVladimir Oltean ocelot->vcap = felix->info->vcap; 92056051948SVladimir Oltean ocelot->ops = felix->info->ops; 921cacea62fSVladimir Oltean ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 922cacea62fSVladimir Oltean ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 923f59fd9caSVladimir Oltean ocelot->devlink = felix->ds->devlink; 92456051948SVladimir Oltean 925bdeced75SVladimir Oltean port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 926bdeced75SVladimir Oltean GFP_KERNEL); 927bdeced75SVladimir Oltean if (!port_phy_modes) 928bdeced75SVladimir Oltean return -ENOMEM; 929bdeced75SVladimir Oltean 930bdeced75SVladimir Oltean err = felix_parse_dt(felix, port_phy_modes); 931bdeced75SVladimir Oltean if (err) { 932bdeced75SVladimir Oltean kfree(port_phy_modes); 933bdeced75SVladimir Oltean return err; 934bdeced75SVladimir Oltean } 935bdeced75SVladimir Oltean 93656051948SVladimir Oltean for (i = 0; i < TARGET_MAX; i++) { 93756051948SVladimir Oltean struct regmap *target; 93856051948SVladimir Oltean 93956051948SVladimir Oltean if (!felix->info->target_io_res[i].name) 94056051948SVladimir Oltean continue; 94156051948SVladimir Oltean 942b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 943b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 944375e1314SVladimir Oltean res.start += felix->switch_base; 945375e1314SVladimir Oltean res.end += felix->switch_base; 94656051948SVladimir Oltean 947b4024c9eSClaudiu Manoil target = ocelot_regmap_init(ocelot, &res); 94856051948SVladimir Oltean if (IS_ERR(target)) { 94956051948SVladimir Oltean dev_err(ocelot->dev, 95056051948SVladimir Oltean "Failed to map device memory space\n"); 951bdeced75SVladimir Oltean kfree(port_phy_modes); 95256051948SVladimir Oltean return PTR_ERR(target); 95356051948SVladimir Oltean } 95456051948SVladimir Oltean 95556051948SVladimir Oltean ocelot->targets[i] = target; 95656051948SVladimir Oltean } 95756051948SVladimir Oltean 95856051948SVladimir Oltean err = ocelot_regfields_init(ocelot, felix->info->regfields); 95956051948SVladimir Oltean if (err) { 96056051948SVladimir Oltean dev_err(ocelot->dev, "failed to init reg fields map\n"); 961bdeced75SVladimir Oltean kfree(port_phy_modes); 96256051948SVladimir Oltean return err; 96356051948SVladimir Oltean } 96456051948SVladimir Oltean 96556051948SVladimir Oltean for (port = 0; port < num_phys_ports; port++) { 96656051948SVladimir Oltean struct ocelot_port *ocelot_port; 96791c724cfSVladimir Oltean struct regmap *target; 96867c24049SVladimir Oltean u8 *template; 96956051948SVladimir Oltean 97056051948SVladimir Oltean ocelot_port = devm_kzalloc(ocelot->dev, 97156051948SVladimir Oltean sizeof(struct ocelot_port), 97256051948SVladimir Oltean GFP_KERNEL); 97356051948SVladimir Oltean if (!ocelot_port) { 97456051948SVladimir Oltean dev_err(ocelot->dev, 97556051948SVladimir Oltean "failed to allocate port memory\n"); 976bdeced75SVladimir Oltean kfree(port_phy_modes); 97756051948SVladimir Oltean return -ENOMEM; 97856051948SVladimir Oltean } 97956051948SVladimir Oltean 980b4024c9eSClaudiu Manoil memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 981b4024c9eSClaudiu Manoil res.flags = IORESOURCE_MEM; 982375e1314SVladimir Oltean res.start += felix->switch_base; 983375e1314SVladimir Oltean res.end += felix->switch_base; 98456051948SVladimir Oltean 98591c724cfSVladimir Oltean target = ocelot_regmap_init(ocelot, &res); 98691c724cfSVladimir Oltean if (IS_ERR(target)) { 98756051948SVladimir Oltean dev_err(ocelot->dev, 98891c724cfSVladimir Oltean "Failed to map memory space for port %d\n", 98991c724cfSVladimir Oltean port); 990bdeced75SVladimir Oltean kfree(port_phy_modes); 99191c724cfSVladimir Oltean return PTR_ERR(target); 99256051948SVladimir Oltean } 99356051948SVladimir Oltean 9945124197cSVladimir Oltean template = devm_kzalloc(ocelot->dev, OCELOT_TOTAL_TAG_LEN, 99567c24049SVladimir Oltean GFP_KERNEL); 99667c24049SVladimir Oltean if (!template) { 99767c24049SVladimir Oltean dev_err(ocelot->dev, 99867c24049SVladimir Oltean "Failed to allocate memory for DSA tag\n"); 99967c24049SVladimir Oltean kfree(port_phy_modes); 100067c24049SVladimir Oltean return -ENOMEM; 100167c24049SVladimir Oltean } 100267c24049SVladimir Oltean 1003bdeced75SVladimir Oltean ocelot_port->phy_mode = port_phy_modes[port]; 100456051948SVladimir Oltean ocelot_port->ocelot = ocelot; 100591c724cfSVladimir Oltean ocelot_port->target = target; 100667c24049SVladimir Oltean ocelot_port->xmit_template = template; 100756051948SVladimir Oltean ocelot->ports[port] = ocelot_port; 100867c24049SVladimir Oltean 100967c24049SVladimir Oltean felix->info->xmit_template_populate(ocelot, port); 101056051948SVladimir Oltean } 101156051948SVladimir Oltean 1012bdeced75SVladimir Oltean kfree(port_phy_modes); 1013bdeced75SVladimir Oltean 1014bdeced75SVladimir Oltean if (felix->info->mdio_bus_alloc) { 1015bdeced75SVladimir Oltean err = felix->info->mdio_bus_alloc(ocelot); 1016bdeced75SVladimir Oltean if (err < 0) 1017bdeced75SVladimir Oltean return err; 1018bdeced75SVladimir Oltean } 1019bdeced75SVladimir Oltean 102056051948SVladimir Oltean return 0; 102156051948SVladimir Oltean } 102256051948SVladimir Oltean 102356051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with 102456051948SVladimir Oltean * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 102556051948SVladimir Oltean * us to allocate structures twice (leak memory) and map PCI memory twice 102656051948SVladimir Oltean * (which will not work). 102756051948SVladimir Oltean */ 102856051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds) 102956051948SVladimir Oltean { 103056051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 103156051948SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 103256051948SVladimir Oltean int port, err; 103356051948SVladimir Oltean 103456051948SVladimir Oltean err = felix_init_structs(felix, ds->num_ports); 103556051948SVladimir Oltean if (err) 103656051948SVladimir Oltean return err; 103756051948SVladimir Oltean 1038d1cc0e93SVladimir Oltean err = ocelot_init(ocelot); 1039d1cc0e93SVladimir Oltean if (err) 1040d1cc0e93SVladimir Oltean return err; 1041d1cc0e93SVladimir Oltean 10422b49d128SYangbo Lu if (ocelot->ptp) { 10432ac7c6c5SVladimir Oltean err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 10442b49d128SYangbo Lu if (err) { 10452b49d128SYangbo Lu dev_err(ocelot->dev, 10462b49d128SYangbo Lu "Timestamp initialization failed\n"); 10472b49d128SYangbo Lu ocelot->ptp = 0; 10482b49d128SYangbo Lu } 10492b49d128SYangbo Lu } 105056051948SVladimir Oltean 105156051948SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1052adb3dccfSVladimir Oltean if (dsa_is_unused_port(ds, port)) 1053adb3dccfSVladimir Oltean continue; 105456051948SVladimir Oltean 1055adb3dccfSVladimir Oltean ocelot_init_port(ocelot, port); 1056bd2b3161SXiaoliang Yang 1057bd2b3161SXiaoliang Yang /* Set the default QoS Classification based on PCP and DEI 1058bd2b3161SXiaoliang Yang * bits of vlan tag. 1059bd2b3161SXiaoliang Yang */ 1060bd2b3161SXiaoliang Yang felix_port_qos_map_init(ocelot, port); 106156051948SVladimir Oltean } 106256051948SVladimir Oltean 1063f59fd9caSVladimir Oltean err = ocelot_devlink_sb_register(ocelot); 1064f59fd9caSVladimir Oltean if (err) 1065f59fd9caSVladimir Oltean return err; 1066f59fd9caSVladimir Oltean 1067adb3dccfSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1068adb3dccfSVladimir Oltean if (!dsa_is_cpu_port(ds, port)) 1069adb3dccfSVladimir Oltean continue; 1070adb3dccfSVladimir Oltean 1071adb3dccfSVladimir Oltean /* The initial tag protocol is NPI which always returns 0, so 1072adb3dccfSVladimir Oltean * there's no real point in checking for errors. 10731cf3299bSVladimir Oltean */ 1074adb3dccfSVladimir Oltean felix_set_tag_protocol(ds, port, felix->tag_proto); 1075adb3dccfSVladimir Oltean } 10761cf3299bSVladimir Oltean 10770b912fc9SVladimir Oltean ds->mtu_enforcement_ingress = true; 1078c54913c1SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 1079bdeced75SVladimir Oltean 108056051948SVladimir Oltean return 0; 108156051948SVladimir Oltean } 108256051948SVladimir Oltean 108356051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds) 108456051948SVladimir Oltean { 108556051948SVladimir Oltean struct ocelot *ocelot = ds->priv; 1086bdeced75SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1087e5fb512dSVladimir Oltean int port; 1088bdeced75SVladimir Oltean 1089adb3dccfSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1090adb3dccfSVladimir Oltean if (!dsa_is_cpu_port(ds, port)) 1091adb3dccfSVladimir Oltean continue; 1092adb3dccfSVladimir Oltean 1093adb3dccfSVladimir Oltean felix_del_tag_protocol(ds, port, felix->tag_proto); 1094adb3dccfSVladimir Oltean } 1095adb3dccfSVladimir Oltean 1096f59fd9caSVladimir Oltean ocelot_devlink_sb_unregister(ocelot); 1097d19741b0SVladimir Oltean ocelot_deinit_timestamp(ocelot); 1098d19741b0SVladimir Oltean ocelot_deinit(ocelot); 109956051948SVladimir Oltean 1100e5fb512dSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) 1101e5fb512dSVladimir Oltean ocelot_deinit_port(ocelot, port); 1102d19741b0SVladimir Oltean 1103d19741b0SVladimir Oltean if (felix->info->mdio_bus_free) 1104d19741b0SVladimir Oltean felix->info->mdio_bus_free(ocelot); 110556051948SVladimir Oltean } 110656051948SVladimir Oltean 1107c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1108c0bcf537SYangbo Lu struct ifreq *ifr) 1109c0bcf537SYangbo Lu { 1110c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1111c0bcf537SYangbo Lu 1112c0bcf537SYangbo Lu return ocelot_hwstamp_get(ocelot, port, ifr); 1113c0bcf537SYangbo Lu } 1114c0bcf537SYangbo Lu 1115c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1116c0bcf537SYangbo Lu struct ifreq *ifr) 1117c0bcf537SYangbo Lu { 1118c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1119c0bcf537SYangbo Lu 1120c0bcf537SYangbo Lu return ocelot_hwstamp_set(ocelot, port, ifr); 1121c0bcf537SYangbo Lu } 1122c0bcf537SYangbo Lu 1123c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1124c0bcf537SYangbo Lu struct sk_buff *skb, unsigned int type) 1125c0bcf537SYangbo Lu { 1126c0bcf537SYangbo Lu struct skb_shared_hwtstamps *shhwtstamps; 1127c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1128c0bcf537SYangbo Lu u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN; 1129c0bcf537SYangbo Lu u32 tstamp_lo, tstamp_hi; 1130c0bcf537SYangbo Lu struct timespec64 ts; 1131c0bcf537SYangbo Lu u64 tstamp, val; 1132c0bcf537SYangbo Lu 1133c0bcf537SYangbo Lu ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1134c0bcf537SYangbo Lu tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1135c0bcf537SYangbo Lu 1136c0bcf537SYangbo Lu packing(extraction, &val, 116, 85, OCELOT_TAG_LEN, UNPACK, 0); 1137c0bcf537SYangbo Lu tstamp_lo = (u32)val; 1138c0bcf537SYangbo Lu 1139c0bcf537SYangbo Lu tstamp_hi = tstamp >> 32; 1140c0bcf537SYangbo Lu if ((tstamp & 0xffffffff) < tstamp_lo) 1141c0bcf537SYangbo Lu tstamp_hi--; 1142c0bcf537SYangbo Lu 1143c0bcf537SYangbo Lu tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1144c0bcf537SYangbo Lu 1145c0bcf537SYangbo Lu shhwtstamps = skb_hwtstamps(skb); 1146c0bcf537SYangbo Lu memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1147c0bcf537SYangbo Lu shhwtstamps->hwtstamp = tstamp; 1148c0bcf537SYangbo Lu return false; 1149c0bcf537SYangbo Lu } 1150c0bcf537SYangbo Lu 11513243e04aSChen Wandun static bool felix_txtstamp(struct dsa_switch *ds, int port, 1152c0bcf537SYangbo Lu struct sk_buff *clone, unsigned int type) 1153c0bcf537SYangbo Lu { 1154c0bcf537SYangbo Lu struct ocelot *ocelot = ds->priv; 1155c0bcf537SYangbo Lu struct ocelot_port *ocelot_port = ocelot->ports[port]; 1156c0bcf537SYangbo Lu 1157e2f9a8feSVladimir Oltean if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) && 1158e2f9a8feSVladimir Oltean ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) { 1159e2f9a8feSVladimir Oltean ocelot_port_add_txtstamp_skb(ocelot, port, clone); 1160c0bcf537SYangbo Lu return true; 1161e2f9a8feSVladimir Oltean } 1162c0bcf537SYangbo Lu 1163c0bcf537SYangbo Lu return false; 1164c0bcf537SYangbo Lu } 1165c0bcf537SYangbo Lu 11660b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 11670b912fc9SVladimir Oltean { 11680b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 11690b912fc9SVladimir Oltean 11700b912fc9SVladimir Oltean ocelot_port_set_maxlen(ocelot, port, new_mtu); 11710b912fc9SVladimir Oltean 11720b912fc9SVladimir Oltean return 0; 11730b912fc9SVladimir Oltean } 11740b912fc9SVladimir Oltean 11750b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port) 11760b912fc9SVladimir Oltean { 11770b912fc9SVladimir Oltean struct ocelot *ocelot = ds->priv; 11780b912fc9SVladimir Oltean 11790b912fc9SVladimir Oltean return ocelot_get_max_mtu(ocelot, port); 11800b912fc9SVladimir Oltean } 11810b912fc9SVladimir Oltean 118207d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port, 118307d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 118407d985eeSVladimir Oltean { 118507d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 118607d985eeSVladimir Oltean 118707d985eeSVladimir Oltean return ocelot_cls_flower_replace(ocelot, port, cls, ingress); 118807d985eeSVladimir Oltean } 118907d985eeSVladimir Oltean 119007d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port, 119107d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 119207d985eeSVladimir Oltean { 119307d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 119407d985eeSVladimir Oltean 119507d985eeSVladimir Oltean return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 119607d985eeSVladimir Oltean } 119707d985eeSVladimir Oltean 119807d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 119907d985eeSVladimir Oltean struct flow_cls_offload *cls, bool ingress) 120007d985eeSVladimir Oltean { 120107d985eeSVladimir Oltean struct ocelot *ocelot = ds->priv; 120207d985eeSVladimir Oltean 120307d985eeSVladimir Oltean return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 120407d985eeSVladimir Oltean } 120507d985eeSVladimir Oltean 1206fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port, 1207fc411eaaSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 1208fc411eaaSVladimir Oltean { 1209fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1210fc411eaaSVladimir Oltean struct ocelot_policer pol = { 1211fc411eaaSVladimir Oltean .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 12125f035af7SPo Liu .burst = policer->burst, 1213fc411eaaSVladimir Oltean }; 1214fc411eaaSVladimir Oltean 1215fc411eaaSVladimir Oltean return ocelot_port_policer_add(ocelot, port, &pol); 1216fc411eaaSVladimir Oltean } 1217fc411eaaSVladimir Oltean 1218fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port) 1219fc411eaaSVladimir Oltean { 1220fc411eaaSVladimir Oltean struct ocelot *ocelot = ds->priv; 1221fc411eaaSVladimir Oltean 1222fc411eaaSVladimir Oltean ocelot_port_policer_del(ocelot, port); 1223fc411eaaSVladimir Oltean } 1224fc411eaaSVladimir Oltean 1225de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1226de143c0eSXiaoliang Yang enum tc_setup_type type, 1227de143c0eSXiaoliang Yang void *type_data) 1228de143c0eSXiaoliang Yang { 1229de143c0eSXiaoliang Yang struct ocelot *ocelot = ds->priv; 1230de143c0eSXiaoliang Yang struct felix *felix = ocelot_to_felix(ocelot); 1231de143c0eSXiaoliang Yang 1232de143c0eSXiaoliang Yang if (felix->info->port_setup_tc) 1233de143c0eSXiaoliang Yang return felix->info->port_setup_tc(ds, port, type, type_data); 1234de143c0eSXiaoliang Yang else 1235de143c0eSXiaoliang Yang return -EOPNOTSUPP; 1236de143c0eSXiaoliang Yang } 1237de143c0eSXiaoliang Yang 1238f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1239f59fd9caSVladimir Oltean u16 pool_index, 1240f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info) 1241f59fd9caSVladimir Oltean { 1242f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1243f59fd9caSVladimir Oltean 1244f59fd9caSVladimir Oltean return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1245f59fd9caSVladimir Oltean } 1246f59fd9caSVladimir Oltean 1247f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1248f59fd9caSVladimir Oltean u16 pool_index, u32 size, 1249f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type, 1250f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1251f59fd9caSVladimir Oltean { 1252f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1253f59fd9caSVladimir Oltean 1254f59fd9caSVladimir Oltean return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1255f59fd9caSVladimir Oltean threshold_type, extack); 1256f59fd9caSVladimir Oltean } 1257f59fd9caSVladimir Oltean 1258f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1259f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1260f59fd9caSVladimir Oltean u32 *p_threshold) 1261f59fd9caSVladimir Oltean { 1262f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1263f59fd9caSVladimir Oltean 1264f59fd9caSVladimir Oltean return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1265f59fd9caSVladimir Oltean p_threshold); 1266f59fd9caSVladimir Oltean } 1267f59fd9caSVladimir Oltean 1268f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1269f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1270f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack) 1271f59fd9caSVladimir Oltean { 1272f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1273f59fd9caSVladimir Oltean 1274f59fd9caSVladimir Oltean return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1275f59fd9caSVladimir Oltean threshold, extack); 1276f59fd9caSVladimir Oltean } 1277f59fd9caSVladimir Oltean 1278f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1279f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1280f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1281f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold) 1282f59fd9caSVladimir Oltean { 1283f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1284f59fd9caSVladimir Oltean 1285f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1286f59fd9caSVladimir Oltean pool_type, p_pool_index, 1287f59fd9caSVladimir Oltean p_threshold); 1288f59fd9caSVladimir Oltean } 1289f59fd9caSVladimir Oltean 1290f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1291f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1292f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1293f59fd9caSVladimir Oltean u16 pool_index, u32 threshold, 1294f59fd9caSVladimir Oltean struct netlink_ext_ack *extack) 1295f59fd9caSVladimir Oltean { 1296f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1297f59fd9caSVladimir Oltean 1298f59fd9caSVladimir Oltean return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1299f59fd9caSVladimir Oltean pool_type, pool_index, threshold, 1300f59fd9caSVladimir Oltean extack); 1301f59fd9caSVladimir Oltean } 1302f59fd9caSVladimir Oltean 1303f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1304f59fd9caSVladimir Oltean unsigned int sb_index) 1305f59fd9caSVladimir Oltean { 1306f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1307f59fd9caSVladimir Oltean 1308f59fd9caSVladimir Oltean return ocelot_sb_occ_snapshot(ocelot, sb_index); 1309f59fd9caSVladimir Oltean } 1310f59fd9caSVladimir Oltean 1311f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1312f59fd9caSVladimir Oltean unsigned int sb_index) 1313f59fd9caSVladimir Oltean { 1314f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1315f59fd9caSVladimir Oltean 1316f59fd9caSVladimir Oltean return ocelot_sb_occ_max_clear(ocelot, sb_index); 1317f59fd9caSVladimir Oltean } 1318f59fd9caSVladimir Oltean 1319f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1320f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index, 1321f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1322f59fd9caSVladimir Oltean { 1323f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1324f59fd9caSVladimir Oltean 1325f59fd9caSVladimir Oltean return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1326f59fd9caSVladimir Oltean p_cur, p_max); 1327f59fd9caSVladimir Oltean } 1328f59fd9caSVladimir Oltean 1329f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1330f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index, 1331f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type, 1332f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max) 1333f59fd9caSVladimir Oltean { 1334f59fd9caSVladimir Oltean struct ocelot *ocelot = ds->priv; 1335f59fd9caSVladimir Oltean 1336f59fd9caSVladimir Oltean return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1337f59fd9caSVladimir Oltean pool_type, p_cur, p_max); 1338f59fd9caSVladimir Oltean } 1339f59fd9caSVladimir Oltean 1340375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = { 134156051948SVladimir Oltean .get_tag_protocol = felix_get_tag_protocol, 1342adb3dccfSVladimir Oltean .change_tag_protocol = felix_change_tag_protocol, 134356051948SVladimir Oltean .setup = felix_setup, 134456051948SVladimir Oltean .teardown = felix_teardown, 134556051948SVladimir Oltean .set_ageing_time = felix_set_ageing_time, 134656051948SVladimir Oltean .get_strings = felix_get_strings, 134756051948SVladimir Oltean .get_ethtool_stats = felix_get_ethtool_stats, 134856051948SVladimir Oltean .get_sset_count = felix_get_sset_count, 134956051948SVladimir Oltean .get_ts_info = felix_get_ts_info, 1350bdeced75SVladimir Oltean .phylink_validate = felix_phylink_validate, 1351bdeced75SVladimir Oltean .phylink_mac_config = felix_phylink_mac_config, 1352bdeced75SVladimir Oltean .phylink_mac_link_down = felix_phylink_mac_link_down, 1353bdeced75SVladimir Oltean .phylink_mac_link_up = felix_phylink_mac_link_up, 135456051948SVladimir Oltean .port_enable = felix_port_enable, 135556051948SVladimir Oltean .port_disable = felix_port_disable, 135656051948SVladimir Oltean .port_fdb_dump = felix_fdb_dump, 135756051948SVladimir Oltean .port_fdb_add = felix_fdb_add, 135856051948SVladimir Oltean .port_fdb_del = felix_fdb_del, 1359209edf95SVladimir Oltean .port_mdb_add = felix_mdb_add, 1360209edf95SVladimir Oltean .port_mdb_del = felix_mdb_del, 136156051948SVladimir Oltean .port_bridge_join = felix_bridge_join, 136256051948SVladimir Oltean .port_bridge_leave = felix_bridge_leave, 1363*8fe6832eSVladimir Oltean .port_lag_join = felix_lag_join, 1364*8fe6832eSVladimir Oltean .port_lag_leave = felix_lag_leave, 1365*8fe6832eSVladimir Oltean .port_lag_change = felix_lag_change, 136656051948SVladimir Oltean .port_stp_state_set = felix_bridge_stp_state_set, 136756051948SVladimir Oltean .port_vlan_filtering = felix_vlan_filtering, 136856051948SVladimir Oltean .port_vlan_add = felix_vlan_add, 136956051948SVladimir Oltean .port_vlan_del = felix_vlan_del, 1370c0bcf537SYangbo Lu .port_hwtstamp_get = felix_hwtstamp_get, 1371c0bcf537SYangbo Lu .port_hwtstamp_set = felix_hwtstamp_set, 1372c0bcf537SYangbo Lu .port_rxtstamp = felix_rxtstamp, 1373c0bcf537SYangbo Lu .port_txtstamp = felix_txtstamp, 13740b912fc9SVladimir Oltean .port_change_mtu = felix_change_mtu, 13750b912fc9SVladimir Oltean .port_max_mtu = felix_get_max_mtu, 1376fc411eaaSVladimir Oltean .port_policer_add = felix_port_policer_add, 1377fc411eaaSVladimir Oltean .port_policer_del = felix_port_policer_del, 137807d985eeSVladimir Oltean .cls_flower_add = felix_cls_flower_add, 137907d985eeSVladimir Oltean .cls_flower_del = felix_cls_flower_del, 138007d985eeSVladimir Oltean .cls_flower_stats = felix_cls_flower_stats, 1381de143c0eSXiaoliang Yang .port_setup_tc = felix_port_setup_tc, 1382f59fd9caSVladimir Oltean .devlink_sb_pool_get = felix_sb_pool_get, 1383f59fd9caSVladimir Oltean .devlink_sb_pool_set = felix_sb_pool_set, 1384f59fd9caSVladimir Oltean .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1385f59fd9caSVladimir Oltean .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1386f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1387f59fd9caSVladimir Oltean .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1388f59fd9caSVladimir Oltean .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1389f59fd9caSVladimir Oltean .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1390f59fd9caSVladimir Oltean .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1391f59fd9caSVladimir Oltean .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 139256051948SVladimir Oltean }; 1393319e4dd1SVladimir Oltean 1394319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1395319e4dd1SVladimir Oltean { 1396319e4dd1SVladimir Oltean struct felix *felix = ocelot_to_felix(ocelot); 1397319e4dd1SVladimir Oltean struct dsa_switch *ds = felix->ds; 1398319e4dd1SVladimir Oltean 1399319e4dd1SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1400319e4dd1SVladimir Oltean return NULL; 1401319e4dd1SVladimir Oltean 1402319e4dd1SVladimir Oltean return dsa_to_port(ds, port)->slave; 1403319e4dd1SVladimir Oltean } 1404319e4dd1SVladimir Oltean 1405319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev) 1406319e4dd1SVladimir Oltean { 1407319e4dd1SVladimir Oltean struct dsa_port *dp; 1408319e4dd1SVladimir Oltean 1409319e4dd1SVladimir Oltean dp = dsa_port_from_netdev(dev); 1410319e4dd1SVladimir Oltean if (IS_ERR(dp)) 1411319e4dd1SVladimir Oltean return -EINVAL; 1412319e4dd1SVladimir Oltean 1413319e4dd1SVladimir Oltean return dp->index; 1414319e4dd1SVladimir Oltean } 1415