12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2f2f23566SVivien Didelot /* 3f2f23566SVivien Didelot * Handling of a master device, switching frames via its switch fabric CPU port 4f2f23566SVivien Didelot * 5f2f23566SVivien Didelot * Copyright (c) 2017 Savoir-faire Linux Inc. 6f2f23566SVivien Didelot * Vivien Didelot <vivien.didelot@savoirfairelinux.com> 7f2f23566SVivien Didelot */ 8f2f23566SVivien Didelot 994ef6fadSVladimir Oltean #include <linux/ethtool.h> 1094ef6fadSVladimir Oltean #include <linux/netdevice.h> 1194ef6fadSVladimir Oltean #include <linux/netlink.h> 1294ef6fadSVladimir Oltean #include <net/dsa.h> 1394ef6fadSVladimir Oltean 14*47d2ce03SVladimir Oltean #include "dsa.h" 1594ef6fadSVladimir Oltean #include "master.h" 16022bba63SVladimir Oltean #include "port.h" 17bd954b82SVladimir Oltean #include "tag.h" 18f2f23566SVivien Didelot 1948e23311SVivien Didelot static int dsa_master_get_regs_len(struct net_device *dev) 2048e23311SVivien Didelot { 2148e23311SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 2248e23311SVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 2348e23311SVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 2448e23311SVivien Didelot int port = cpu_dp->index; 2548e23311SVivien Didelot int ret = 0; 2648e23311SVivien Didelot int len; 2748e23311SVivien Didelot 2848e23311SVivien Didelot if (ops->get_regs_len) { 2948e23311SVivien Didelot len = ops->get_regs_len(dev); 3048e23311SVivien Didelot if (len < 0) 3148e23311SVivien Didelot return len; 3248e23311SVivien Didelot ret += len; 3348e23311SVivien Didelot } 3448e23311SVivien Didelot 3548e23311SVivien Didelot ret += sizeof(struct ethtool_drvinfo); 3648e23311SVivien Didelot ret += sizeof(struct ethtool_regs); 3748e23311SVivien Didelot 3848e23311SVivien Didelot if (ds->ops->get_regs_len) { 3948e23311SVivien Didelot len = ds->ops->get_regs_len(ds, port); 4048e23311SVivien Didelot if (len < 0) 4148e23311SVivien Didelot return len; 4248e23311SVivien Didelot ret += len; 4348e23311SVivien Didelot } 4448e23311SVivien Didelot 4548e23311SVivien Didelot return ret; 4648e23311SVivien Didelot } 4748e23311SVivien Didelot 4848e23311SVivien Didelot static void dsa_master_get_regs(struct net_device *dev, 4948e23311SVivien Didelot struct ethtool_regs *regs, void *data) 5048e23311SVivien Didelot { 5148e23311SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 5248e23311SVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 5348e23311SVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 5448e23311SVivien Didelot struct ethtool_drvinfo *cpu_info; 5548e23311SVivien Didelot struct ethtool_regs *cpu_regs; 5648e23311SVivien Didelot int port = cpu_dp->index; 5748e23311SVivien Didelot int len; 5848e23311SVivien Didelot 5948e23311SVivien Didelot if (ops->get_regs_len && ops->get_regs) { 6048e23311SVivien Didelot len = ops->get_regs_len(dev); 6148e23311SVivien Didelot if (len < 0) 6248e23311SVivien Didelot return; 6348e23311SVivien Didelot regs->len = len; 6448e23311SVivien Didelot ops->get_regs(dev, regs, data); 6548e23311SVivien Didelot data += regs->len; 6648e23311SVivien Didelot } 6748e23311SVivien Didelot 6848e23311SVivien Didelot cpu_info = (struct ethtool_drvinfo *)data; 69e4d44b3dSWolfram Sang strscpy(cpu_info->driver, "dsa", sizeof(cpu_info->driver)); 7048e23311SVivien Didelot data += sizeof(*cpu_info); 7148e23311SVivien Didelot cpu_regs = (struct ethtool_regs *)data; 7248e23311SVivien Didelot data += sizeof(*cpu_regs); 7348e23311SVivien Didelot 7448e23311SVivien Didelot if (ds->ops->get_regs_len && ds->ops->get_regs) { 7548e23311SVivien Didelot len = ds->ops->get_regs_len(ds, port); 7648e23311SVivien Didelot if (len < 0) 7748e23311SVivien Didelot return; 7848e23311SVivien Didelot cpu_regs->len = len; 7948e23311SVivien Didelot ds->ops->get_regs(ds, port, cpu_regs, data); 8048e23311SVivien Didelot } 8148e23311SVivien Didelot } 8248e23311SVivien Didelot 83f2f23566SVivien Didelot static void dsa_master_get_ethtool_stats(struct net_device *dev, 84f2f23566SVivien Didelot struct ethtool_stats *stats, 85f2f23566SVivien Didelot uint64_t *data) 86f2f23566SVivien Didelot { 872f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 887ec764eeSVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 897ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 907ec764eeSVivien Didelot int port = cpu_dp->index; 91f2f23566SVivien Didelot int count = 0; 92f2f23566SVivien Didelot 931d1e79f1SFlorian Fainelli if (ops->get_sset_count && ops->get_ethtool_stats) { 94f2f23566SVivien Didelot count = ops->get_sset_count(dev, ETH_SS_STATS); 95f2f23566SVivien Didelot ops->get_ethtool_stats(dev, stats, data); 96f2f23566SVivien Didelot } 97f2f23566SVivien Didelot 98f2f23566SVivien Didelot if (ds->ops->get_ethtool_stats) 997ec764eeSVivien Didelot ds->ops->get_ethtool_stats(ds, port, data + count); 100f2f23566SVivien Didelot } 101f2f23566SVivien Didelot 102cf963573SFlorian Fainelli static void dsa_master_get_ethtool_phy_stats(struct net_device *dev, 103cf963573SFlorian Fainelli struct ethtool_stats *stats, 104cf963573SFlorian Fainelli uint64_t *data) 105cf963573SFlorian Fainelli { 106cf963573SFlorian Fainelli struct dsa_port *cpu_dp = dev->dsa_ptr; 107cf963573SFlorian Fainelli const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 108cf963573SFlorian Fainelli struct dsa_switch *ds = cpu_dp->ds; 109cf963573SFlorian Fainelli int port = cpu_dp->index; 110cf963573SFlorian Fainelli int count = 0; 111cf963573SFlorian Fainelli 112cf963573SFlorian Fainelli if (dev->phydev && !ops->get_ethtool_phy_stats) { 113cf963573SFlorian Fainelli count = phy_ethtool_get_sset_count(dev->phydev); 114cf963573SFlorian Fainelli if (count >= 0) 115cf963573SFlorian Fainelli phy_ethtool_get_stats(dev->phydev, stats, data); 116cf963573SFlorian Fainelli } else if (ops->get_sset_count && ops->get_ethtool_phy_stats) { 117cf963573SFlorian Fainelli count = ops->get_sset_count(dev, ETH_SS_PHY_STATS); 118cf963573SFlorian Fainelli ops->get_ethtool_phy_stats(dev, stats, data); 119cf963573SFlorian Fainelli } 120cf963573SFlorian Fainelli 121cf963573SFlorian Fainelli if (count < 0) 122cf963573SFlorian Fainelli count = 0; 123cf963573SFlorian Fainelli 124cf963573SFlorian Fainelli if (ds->ops->get_ethtool_phy_stats) 125cf963573SFlorian Fainelli ds->ops->get_ethtool_phy_stats(ds, port, data + count); 126cf963573SFlorian Fainelli } 127cf963573SFlorian Fainelli 128f2f23566SVivien Didelot static int dsa_master_get_sset_count(struct net_device *dev, int sset) 129f2f23566SVivien Didelot { 1302f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 1317ec764eeSVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 1327ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 133f2f23566SVivien Didelot int count = 0; 134f2f23566SVivien Didelot 135cf963573SFlorian Fainelli if (sset == ETH_SS_PHY_STATS && dev->phydev && 136cf963573SFlorian Fainelli !ops->get_ethtool_phy_stats) 137cf963573SFlorian Fainelli count = phy_ethtool_get_sset_count(dev->phydev); 138cf963573SFlorian Fainelli else if (ops->get_sset_count) 13989f09048SFlorian Fainelli count = ops->get_sset_count(dev, sset); 140cf963573SFlorian Fainelli 14189f09048SFlorian Fainelli if (count < 0) 14289f09048SFlorian Fainelli count = 0; 143f2f23566SVivien Didelot 14489f09048SFlorian Fainelli if (ds->ops->get_sset_count) 14589f09048SFlorian Fainelli count += ds->ops->get_sset_count(ds, cpu_dp->index, sset); 146f2f23566SVivien Didelot 147f2f23566SVivien Didelot return count; 148f2f23566SVivien Didelot } 149f2f23566SVivien Didelot 150f2f23566SVivien Didelot static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset, 151f2f23566SVivien Didelot uint8_t *data) 152f2f23566SVivien Didelot { 1532f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 1547ec764eeSVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 1557ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 1567ec764eeSVivien Didelot int port = cpu_dp->index; 157f2f23566SVivien Didelot int len = ETH_GSTRING_LEN; 158a269333fSDan Carpenter int mcount = 0, count, i; 159f2f23566SVivien Didelot uint8_t pfx[4]; 160f2f23566SVivien Didelot uint8_t *ndata; 161f2f23566SVivien Didelot 1627ec764eeSVivien Didelot snprintf(pfx, sizeof(pfx), "p%.2d", port); 163f2f23566SVivien Didelot /* We do not want to be NULL-terminated, since this is a prefix */ 164f2f23566SVivien Didelot pfx[sizeof(pfx) - 1] = '_'; 165f2f23566SVivien Didelot 166cf963573SFlorian Fainelli if (stringset == ETH_SS_PHY_STATS && dev->phydev && 167cf963573SFlorian Fainelli !ops->get_ethtool_phy_stats) { 168cf963573SFlorian Fainelli mcount = phy_ethtool_get_sset_count(dev->phydev); 169cf963573SFlorian Fainelli if (mcount < 0) 170cf963573SFlorian Fainelli mcount = 0; 171cf963573SFlorian Fainelli else 172cf963573SFlorian Fainelli phy_ethtool_get_strings(dev->phydev, data); 173cf963573SFlorian Fainelli } else if (ops->get_sset_count && ops->get_strings) { 17489f09048SFlorian Fainelli mcount = ops->get_sset_count(dev, stringset); 17589f09048SFlorian Fainelli if (mcount < 0) 17689f09048SFlorian Fainelli mcount = 0; 177f2f23566SVivien Didelot ops->get_strings(dev, stringset, data); 178f2f23566SVivien Didelot } 179f2f23566SVivien Didelot 18089f09048SFlorian Fainelli if (ds->ops->get_strings) { 181f2f23566SVivien Didelot ndata = data + mcount * len; 182f2f23566SVivien Didelot /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle 183f2f23566SVivien Didelot * the output after to prepend our CPU port prefix we 184f2f23566SVivien Didelot * constructed earlier 185f2f23566SVivien Didelot */ 18689f09048SFlorian Fainelli ds->ops->get_strings(ds, port, stringset, ndata); 18789f09048SFlorian Fainelli count = ds->ops->get_sset_count(ds, port, stringset); 188a269333fSDan Carpenter if (count < 0) 189a269333fSDan Carpenter return; 190f2f23566SVivien Didelot for (i = 0; i < count; i++) { 191f2f23566SVivien Didelot memmove(ndata + (i * len + sizeof(pfx)), 192f2f23566SVivien Didelot ndata + i * len, len - sizeof(pfx)); 193f2f23566SVivien Didelot memcpy(ndata + i * len, pfx, sizeof(pfx)); 194f2f23566SVivien Didelot } 195f2f23566SVivien Didelot } 196f2f23566SVivien Didelot } 197f2f23566SVivien Didelot 198f685e609SVladimir Oltean static int dsa_master_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 199f685e609SVladimir Oltean { 200f685e609SVladimir Oltean struct dsa_port *cpu_dp = dev->dsa_ptr; 201f685e609SVladimir Oltean struct dsa_switch *ds = cpu_dp->ds; 202f685e609SVladimir Oltean struct dsa_switch_tree *dst; 203f685e609SVladimir Oltean int err = -EOPNOTSUPP; 204f685e609SVladimir Oltean struct dsa_port *dp; 205f685e609SVladimir Oltean 206f685e609SVladimir Oltean dst = ds->dst; 207f685e609SVladimir Oltean 208f685e609SVladimir Oltean switch (cmd) { 209f685e609SVladimir Oltean case SIOCGHWTSTAMP: 210f685e609SVladimir Oltean case SIOCSHWTSTAMP: 211f685e609SVladimir Oltean /* Deny PTP operations on master if there is at least one 212f685e609SVladimir Oltean * switch in the tree that is PTP capable. 213f685e609SVladimir Oltean */ 214f685e609SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 215ed1fe1beSVladimir Oltean if (dsa_port_supports_hwtstamp(dp, ifr)) 216f685e609SVladimir Oltean return -EBUSY; 217f685e609SVladimir Oltean break; 218f685e609SVladimir Oltean } 219f685e609SVladimir Oltean 220a7605370SArnd Bergmann if (dev->netdev_ops->ndo_eth_ioctl) 221a7605370SArnd Bergmann err = dev->netdev_ops->ndo_eth_ioctl(dev, ifr, cmd); 222f685e609SVladimir Oltean 223f685e609SVladimir Oltean return err; 224f685e609SVladimir Oltean } 225f685e609SVladimir Oltean 2269c0c7014SFlorian Fainelli static const struct dsa_netdevice_ops dsa_netdev_ops = { 227a7605370SArnd Bergmann .ndo_eth_ioctl = dsa_master_ioctl, 2289c0c7014SFlorian Fainelli }; 2299c0c7014SFlorian Fainelli 23017a22fcfSVivien Didelot static int dsa_master_ethtool_setup(struct net_device *dev) 231f2f23566SVivien Didelot { 2322f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 2337ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 234f2f23566SVivien Didelot struct ethtool_ops *ops; 235f2f23566SVivien Didelot 236cfeb84a5SVladimir Oltean if (netif_is_lag_master(dev)) 237cfeb84a5SVladimir Oltean return 0; 238cfeb84a5SVladimir Oltean 239f2f23566SVivien Didelot ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL); 240f2f23566SVivien Didelot if (!ops) 241f2f23566SVivien Didelot return -ENOMEM; 242f2f23566SVivien Didelot 2437ec764eeSVivien Didelot cpu_dp->orig_ethtool_ops = dev->ethtool_ops; 2447ec764eeSVivien Didelot if (cpu_dp->orig_ethtool_ops) 2457ec764eeSVivien Didelot memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops)); 246f2f23566SVivien Didelot 24748e23311SVivien Didelot ops->get_regs_len = dsa_master_get_regs_len; 24848e23311SVivien Didelot ops->get_regs = dsa_master_get_regs; 249f2f23566SVivien Didelot ops->get_sset_count = dsa_master_get_sset_count; 250f2f23566SVivien Didelot ops->get_ethtool_stats = dsa_master_get_ethtool_stats; 251f2f23566SVivien Didelot ops->get_strings = dsa_master_get_strings; 252cf963573SFlorian Fainelli ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats; 253f2f23566SVivien Didelot 254f2f23566SVivien Didelot dev->ethtool_ops = ops; 255f2f23566SVivien Didelot 256f2f23566SVivien Didelot return 0; 257f2f23566SVivien Didelot } 258f2f23566SVivien Didelot 25917a22fcfSVivien Didelot static void dsa_master_ethtool_teardown(struct net_device *dev) 260f2f23566SVivien Didelot { 2612f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 262f2f23566SVivien Didelot 263cfeb84a5SVladimir Oltean if (netif_is_lag_master(dev)) 264cfeb84a5SVladimir Oltean return; 265cfeb84a5SVladimir Oltean 2667ec764eeSVivien Didelot dev->ethtool_ops = cpu_dp->orig_ethtool_ops; 2677ec764eeSVivien Didelot cpu_dp->orig_ethtool_ops = NULL; 268f2f23566SVivien Didelot } 26917a22fcfSVivien Didelot 2709c0c7014SFlorian Fainelli static void dsa_netdev_ops_set(struct net_device *dev, 2719c0c7014SFlorian Fainelli const struct dsa_netdevice_ops *ops) 272da7b9e9bSFlorian Fainelli { 273cfeb84a5SVladimir Oltean if (netif_is_lag_master(dev)) 274cfeb84a5SVladimir Oltean return; 275cfeb84a5SVladimir Oltean 2769c0c7014SFlorian Fainelli dev->dsa_ptr->netdev_ops = ops; 277da7b9e9bSFlorian Fainelli } 278da7b9e9bSFlorian Fainelli 2798940e6b6SVladimir Oltean /* Keep the master always promiscuous if the tagging protocol requires that 2808940e6b6SVladimir Oltean * (garbles MAC DA) or if it doesn't support unicast filtering, case in which 2818940e6b6SVladimir Oltean * it would revert to promiscuous mode as soon as we call dev_uc_add() on it 2828940e6b6SVladimir Oltean * anyway. 2838940e6b6SVladimir Oltean */ 284c3975400SVladimir Oltean static void dsa_master_set_promiscuity(struct net_device *dev, int inc) 285c3975400SVladimir Oltean { 286c3975400SVladimir Oltean const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops; 287c3975400SVladimir Oltean 2888940e6b6SVladimir Oltean if ((dev->priv_flags & IFF_UNICAST_FLT) && !ops->promisc_on_master) 289c3975400SVladimir Oltean return; 290c3975400SVladimir Oltean 291c146f9bcSVladimir Oltean ASSERT_RTNL(); 292c146f9bcSVladimir Oltean 293c3975400SVladimir Oltean dev_set_promiscuity(dev, inc); 294c3975400SVladimir Oltean } 295c3975400SVladimir Oltean 296a3d7e01dSFlorian Fainelli static ssize_t tagging_show(struct device *d, struct device_attribute *attr, 297a3d7e01dSFlorian Fainelli char *buf) 298a3d7e01dSFlorian Fainelli { 299a3d7e01dSFlorian Fainelli struct net_device *dev = to_net_dev(d); 300a3d7e01dSFlorian Fainelli struct dsa_port *cpu_dp = dev->dsa_ptr; 301a3d7e01dSFlorian Fainelli 302a3d7e01dSFlorian Fainelli return sprintf(buf, "%s\n", 303a3d7e01dSFlorian Fainelli dsa_tag_protocol_to_str(cpu_dp->tag_ops)); 304a3d7e01dSFlorian Fainelli } 30553da0ebaSVladimir Oltean 30653da0ebaSVladimir Oltean static ssize_t tagging_store(struct device *d, struct device_attribute *attr, 30753da0ebaSVladimir Oltean const char *buf, size_t count) 30853da0ebaSVladimir Oltean { 30953da0ebaSVladimir Oltean const struct dsa_device_ops *new_tag_ops, *old_tag_ops; 310e8666130SVladimir Oltean const char *end = strchrnul(buf, '\n'), *name; 31153da0ebaSVladimir Oltean struct net_device *dev = to_net_dev(d); 31253da0ebaSVladimir Oltean struct dsa_port *cpu_dp = dev->dsa_ptr; 313e8666130SVladimir Oltean size_t len = end - buf; 31453da0ebaSVladimir Oltean int err; 31553da0ebaSVladimir Oltean 316e8666130SVladimir Oltean /* Empty string passed */ 317e8666130SVladimir Oltean if (!len) 318e8666130SVladimir Oltean return -ENOPROTOOPT; 319e8666130SVladimir Oltean 320e8666130SVladimir Oltean name = kstrndup(buf, len, GFP_KERNEL); 321e8666130SVladimir Oltean if (!name) 322e8666130SVladimir Oltean return -ENOMEM; 323e8666130SVladimir Oltean 32453da0ebaSVladimir Oltean old_tag_ops = cpu_dp->tag_ops; 3250184c07aSVladimir Oltean new_tag_ops = dsa_tag_driver_get_by_name(name); 326e8666130SVladimir Oltean kfree(name); 3270184c07aSVladimir Oltean /* Bad tagger name? */ 32853da0ebaSVladimir Oltean if (IS_ERR(new_tag_ops)) 32953da0ebaSVladimir Oltean return PTR_ERR(new_tag_ops); 33053da0ebaSVladimir Oltean 33153da0ebaSVladimir Oltean if (new_tag_ops == old_tag_ops) 33253da0ebaSVladimir Oltean /* Drop the temporarily held duplicate reference, since 33353da0ebaSVladimir Oltean * the DSA switch tree uses this tagger. 33453da0ebaSVladimir Oltean */ 33553da0ebaSVladimir Oltean goto out; 33653da0ebaSVladimir Oltean 337f41ec1fdSVladimir Oltean err = dsa_tree_change_tag_proto(cpu_dp->ds->dst, new_tag_ops, 33853da0ebaSVladimir Oltean old_tag_ops); 33953da0ebaSVladimir Oltean if (err) { 34053da0ebaSVladimir Oltean /* On failure the old tagger is restored, so we don't need the 34153da0ebaSVladimir Oltean * driver for the new one. 34253da0ebaSVladimir Oltean */ 34353da0ebaSVladimir Oltean dsa_tag_driver_put(new_tag_ops); 34453da0ebaSVladimir Oltean return err; 34553da0ebaSVladimir Oltean } 34653da0ebaSVladimir Oltean 34753da0ebaSVladimir Oltean /* On success we no longer need the module for the old tagging protocol 34853da0ebaSVladimir Oltean */ 34953da0ebaSVladimir Oltean out: 35053da0ebaSVladimir Oltean dsa_tag_driver_put(old_tag_ops); 35153da0ebaSVladimir Oltean return count; 35253da0ebaSVladimir Oltean } 35353da0ebaSVladimir Oltean static DEVICE_ATTR_RW(tagging); 354a3d7e01dSFlorian Fainelli 355a3d7e01dSFlorian Fainelli static struct attribute *dsa_slave_attrs[] = { 356a3d7e01dSFlorian Fainelli &dev_attr_tagging.attr, 357a3d7e01dSFlorian Fainelli NULL 358a3d7e01dSFlorian Fainelli }; 359a3d7e01dSFlorian Fainelli 360a3d7e01dSFlorian Fainelli static const struct attribute_group dsa_group = { 361a3d7e01dSFlorian Fainelli .name = "dsa", 362a3d7e01dSFlorian Fainelli .attrs = dsa_slave_attrs, 363a3d7e01dSFlorian Fainelli }; 364a3d7e01dSFlorian Fainelli 365066dfc42SVladimir Oltean static void dsa_master_reset_mtu(struct net_device *dev) 366066dfc42SVladimir Oltean { 367066dfc42SVladimir Oltean int err; 368066dfc42SVladimir Oltean 369066dfc42SVladimir Oltean err = dev_set_mtu(dev, ETH_DATA_LEN); 370066dfc42SVladimir Oltean if (err) 371066dfc42SVladimir Oltean netdev_dbg(dev, 372066dfc42SVladimir Oltean "Unable to reset MTU to exclude DSA overheads\n"); 373066dfc42SVladimir Oltean } 374066dfc42SVladimir Oltean 37517a22fcfSVivien Didelot int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp) 37617a22fcfSVivien Didelot { 377066dfc42SVladimir Oltean const struct dsa_device_ops *tag_ops = cpu_dp->tag_ops; 37807b90056SVladimir Oltean struct dsa_switch *ds = cpu_dp->ds; 37907b90056SVladimir Oltean struct device_link *consumer_link; 380066dfc42SVladimir Oltean int mtu, ret; 381066dfc42SVladimir Oltean 382066dfc42SVladimir Oltean mtu = ETH_DATA_LEN + dsa_tag_protocol_overhead(tag_ops); 383a3d7e01dSFlorian Fainelli 38407b90056SVladimir Oltean /* The DSA master must use SET_NETDEV_DEV for this to work. */ 38513eccc1bSVladimir Oltean if (!netif_is_lag_master(dev)) { 38607b90056SVladimir Oltean consumer_link = device_link_add(ds->dev, dev->dev.parent, 38707b90056SVladimir Oltean DL_FLAG_AUTOREMOVE_CONSUMER); 38807b90056SVladimir Oltean if (!consumer_link) 38907b90056SVladimir Oltean netdev_err(dev, 39007b90056SVladimir Oltean "Failed to create a device link to DSA switch %s\n", 39107b90056SVladimir Oltean dev_name(ds->dev)); 39213eccc1bSVladimir Oltean } 39307b90056SVladimir Oltean 394066dfc42SVladimir Oltean /* The switch driver may not implement ->port_change_mtu(), case in 395066dfc42SVladimir Oltean * which dsa_slave_change_mtu() will not update the master MTU either, 396066dfc42SVladimir Oltean * so we need to do that here. 397066dfc42SVladimir Oltean */ 398066dfc42SVladimir Oltean ret = dev_set_mtu(dev, mtu); 399066dfc42SVladimir Oltean if (ret) 400066dfc42SVladimir Oltean netdev_warn(dev, "error %d setting MTU to %d to include DSA overhead\n", 401066dfc42SVladimir Oltean ret, mtu); 402066dfc42SVladimir Oltean 40317a22fcfSVivien Didelot /* If we use a tagging format that doesn't have an ethertype 40417a22fcfSVivien Didelot * field, make sure that all packets from this point on get 40517a22fcfSVivien Didelot * sent to the tag format's receive function. 40617a22fcfSVivien Didelot */ 40717a22fcfSVivien Didelot wmb(); 40817a22fcfSVivien Didelot 40917a22fcfSVivien Didelot dev->dsa_ptr = cpu_dp; 410c3975400SVladimir Oltean 411c3975400SVladimir Oltean dsa_master_set_promiscuity(dev, 1); 412c3975400SVladimir Oltean 413a3d7e01dSFlorian Fainelli ret = dsa_master_ethtool_setup(dev); 414a3d7e01dSFlorian Fainelli if (ret) 415c3975400SVladimir Oltean goto out_err_reset_promisc; 416a3d7e01dSFlorian Fainelli 4179c0c7014SFlorian Fainelli dsa_netdev_ops_set(dev, &dsa_netdev_ops); 418da7b9e9bSFlorian Fainelli 419a3d7e01dSFlorian Fainelli ret = sysfs_create_group(&dev->dev.kobj, &dsa_group); 420a3d7e01dSFlorian Fainelli if (ret) 421da7b9e9bSFlorian Fainelli goto out_err_ndo_teardown; 422a3d7e01dSFlorian Fainelli 423a3d7e01dSFlorian Fainelli return ret; 424da7b9e9bSFlorian Fainelli 425da7b9e9bSFlorian Fainelli out_err_ndo_teardown: 4269c0c7014SFlorian Fainelli dsa_netdev_ops_set(dev, NULL); 427da7b9e9bSFlorian Fainelli dsa_master_ethtool_teardown(dev); 428c3975400SVladimir Oltean out_err_reset_promisc: 429c3975400SVladimir Oltean dsa_master_set_promiscuity(dev, -1); 430da7b9e9bSFlorian Fainelli return ret; 43117a22fcfSVivien Didelot } 43217a22fcfSVivien Didelot 43317a22fcfSVivien Didelot void dsa_master_teardown(struct net_device *dev) 43417a22fcfSVivien Didelot { 435a3d7e01dSFlorian Fainelli sysfs_remove_group(&dev->dev.kobj, &dsa_group); 4369c0c7014SFlorian Fainelli dsa_netdev_ops_set(dev, NULL); 43717a22fcfSVivien Didelot dsa_master_ethtool_teardown(dev); 438066dfc42SVladimir Oltean dsa_master_reset_mtu(dev); 439c3975400SVladimir Oltean dsa_master_set_promiscuity(dev, -1); 44017a22fcfSVivien Didelot 44117a22fcfSVivien Didelot dev->dsa_ptr = NULL; 44217a22fcfSVivien Didelot 44317a22fcfSVivien Didelot /* If we used a tagging format that doesn't have an ethertype 44417a22fcfSVivien Didelot * field, make sure that all packets from this point get sent 44517a22fcfSVivien Didelot * without the tag and go through the regular receive path. 44617a22fcfSVivien Didelot */ 44717a22fcfSVivien Didelot wmb(); 44817a22fcfSVivien Didelot } 449acc43b7bSVladimir Oltean 450acc43b7bSVladimir Oltean int dsa_master_lag_setup(struct net_device *lag_dev, struct dsa_port *cpu_dp, 451acc43b7bSVladimir Oltean struct netdev_lag_upper_info *uinfo, 452acc43b7bSVladimir Oltean struct netlink_ext_ack *extack) 453acc43b7bSVladimir Oltean { 454acc43b7bSVladimir Oltean bool master_setup = false; 455acc43b7bSVladimir Oltean int err; 456acc43b7bSVladimir Oltean 457acc43b7bSVladimir Oltean if (!netdev_uses_dsa(lag_dev)) { 458acc43b7bSVladimir Oltean err = dsa_master_setup(lag_dev, cpu_dp); 459acc43b7bSVladimir Oltean if (err) 460acc43b7bSVladimir Oltean return err; 461acc43b7bSVladimir Oltean 462acc43b7bSVladimir Oltean master_setup = true; 463acc43b7bSVladimir Oltean } 464acc43b7bSVladimir Oltean 465acc43b7bSVladimir Oltean err = dsa_port_lag_join(cpu_dp, lag_dev, uinfo, extack); 466acc43b7bSVladimir Oltean if (err) { 467acc43b7bSVladimir Oltean if (extack && !extack->_msg) 468acc43b7bSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 469acc43b7bSVladimir Oltean "CPU port failed to join LAG"); 470acc43b7bSVladimir Oltean goto out_master_teardown; 471acc43b7bSVladimir Oltean } 472acc43b7bSVladimir Oltean 473acc43b7bSVladimir Oltean return 0; 474acc43b7bSVladimir Oltean 475acc43b7bSVladimir Oltean out_master_teardown: 476acc43b7bSVladimir Oltean if (master_setup) 477acc43b7bSVladimir Oltean dsa_master_teardown(lag_dev); 478acc43b7bSVladimir Oltean return err; 479acc43b7bSVladimir Oltean } 480acc43b7bSVladimir Oltean 481acc43b7bSVladimir Oltean /* Tear down a master if there isn't any other user port on it, 482acc43b7bSVladimir Oltean * optionally also destroying LAG information. 483acc43b7bSVladimir Oltean */ 484acc43b7bSVladimir Oltean void dsa_master_lag_teardown(struct net_device *lag_dev, 485acc43b7bSVladimir Oltean struct dsa_port *cpu_dp) 486acc43b7bSVladimir Oltean { 487acc43b7bSVladimir Oltean struct net_device *upper; 488acc43b7bSVladimir Oltean struct list_head *iter; 489acc43b7bSVladimir Oltean 490acc43b7bSVladimir Oltean dsa_port_lag_leave(cpu_dp, lag_dev); 491acc43b7bSVladimir Oltean 492acc43b7bSVladimir Oltean netdev_for_each_upper_dev_rcu(lag_dev, upper, iter) 493acc43b7bSVladimir Oltean if (dsa_slave_dev_check(upper)) 494acc43b7bSVladimir Oltean return; 495acc43b7bSVladimir Oltean 496acc43b7bSVladimir Oltean dsa_master_teardown(lag_dev); 497acc43b7bSVladimir Oltean } 498