12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 291da11f8SLennert Buytenhek /* 391da11f8SLennert Buytenhek * net/dsa/slave.c - Slave device handling 4e84665c9SLennert Buytenhek * Copyright (c) 2008-2009 Marvell Semiconductor 591da11f8SLennert Buytenhek */ 691da11f8SLennert Buytenhek 791da11f8SLennert Buytenhek #include <linux/list.h> 8df02c6ffSLennert Buytenhek #include <linux/etherdevice.h> 9b73adef6SFlorian Fainelli #include <linux/netdevice.h> 1091da11f8SLennert Buytenhek #include <linux/phy.h> 11a2820543SFlorian Fainelli #include <linux/phy_fixed.h> 12aab9c406SFlorian Fainelli #include <linux/phylink.h> 130d8bcdd3SFlorian Fainelli #include <linux/of_net.h> 140d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h> 157f854420SAndrew Lunn #include <linux/mdio.h> 16b73adef6SFlorian Fainelli #include <net/rtnetlink.h> 17f50f2127SFlorian Fainelli #include <net/pkt_cls.h> 18f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h> 19b73adef6SFlorian Fainelli #include <linux/if_bridge.h> 2004ff53f9SFlorian Fainelli #include <linux/netpoll.h> 2190af1059SBrandon Streiff #include <linux/ptp_classify.h> 22ea5dd34bSVivien Didelot 2391da11f8SLennert Buytenhek #include "dsa_priv.h" 2491da11f8SLennert Buytenhek 2591da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 2691da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 2791da11f8SLennert Buytenhek { 2891da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 2991da11f8SLennert Buytenhek 300d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 319d490b4eSVivien Didelot return ds->ops->phy_read(ds, addr, reg); 3291da11f8SLennert Buytenhek 3391da11f8SLennert Buytenhek return 0xffff; 3491da11f8SLennert Buytenhek } 3591da11f8SLennert Buytenhek 3691da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 3791da11f8SLennert Buytenhek { 3891da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 3991da11f8SLennert Buytenhek 400d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 419d490b4eSVivien Didelot return ds->ops->phy_write(ds, addr, reg, val); 4291da11f8SLennert Buytenhek 4391da11f8SLennert Buytenhek return 0; 4491da11f8SLennert Buytenhek } 4591da11f8SLennert Buytenhek 4691da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 4791da11f8SLennert Buytenhek { 4891da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 4991da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 5091da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 5191da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 520b7b498dSFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 5349463b7fSVivien Didelot ds->dst->index, ds->index); 54c33063d6SAndrew Lunn ds->slave_mii_bus->parent = ds->dev; 5524df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 5691da11f8SLennert Buytenhek } 5791da11f8SLennert Buytenhek 5891da11f8SLennert Buytenhek 5991da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 60abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev) 61c0840801SLennert Buytenhek { 62d0006b00SVivien Didelot return dsa_slave_to_master(dev)->ifindex; 63c0840801SLennert Buytenhek } 64c0840801SLennert Buytenhek 6591da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 6691da11f8SLennert Buytenhek { 67d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 68d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 69df02c6ffSLennert Buytenhek int err; 70df02c6ffSLennert Buytenhek 71df02c6ffSLennert Buytenhek if (!(master->flags & IFF_UP)) 72df02c6ffSLennert Buytenhek return -ENETDOWN; 73df02c6ffSLennert Buytenhek 748feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 75a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 76df02c6ffSLennert Buytenhek if (err < 0) 77df02c6ffSLennert Buytenhek goto out; 78df02c6ffSLennert Buytenhek } 79df02c6ffSLennert Buytenhek 80df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) { 81df02c6ffSLennert Buytenhek err = dev_set_allmulti(master, 1); 82df02c6ffSLennert Buytenhek if (err < 0) 83df02c6ffSLennert Buytenhek goto del_unicast; 84df02c6ffSLennert Buytenhek } 85df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) { 86df02c6ffSLennert Buytenhek err = dev_set_promiscuity(master, 1); 87df02c6ffSLennert Buytenhek if (err < 0) 88df02c6ffSLennert Buytenhek goto clear_allmulti; 89df02c6ffSLennert Buytenhek } 90df02c6ffSLennert Buytenhek 918640f8dcSRussell King err = dsa_port_enable_rt(dp, dev->phydev); 92b2f2af21SFlorian Fainelli if (err) 93b2f2af21SFlorian Fainelli goto clear_promisc; 94b73adef6SFlorian Fainelli 9591da11f8SLennert Buytenhek return 0; 96df02c6ffSLennert Buytenhek 97b2f2af21SFlorian Fainelli clear_promisc: 98b2f2af21SFlorian Fainelli if (dev->flags & IFF_PROMISC) 994fdeddfeSGilad Ben-Yossef dev_set_promiscuity(master, -1); 100df02c6ffSLennert Buytenhek clear_allmulti: 101df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 102df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 103df02c6ffSLennert Buytenhek del_unicast: 1048feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 105a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 106df02c6ffSLennert Buytenhek out: 107df02c6ffSLennert Buytenhek return err; 10891da11f8SLennert Buytenhek } 10991da11f8SLennert Buytenhek 11091da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 11191da11f8SLennert Buytenhek { 112d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 113d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 114df02c6ffSLennert Buytenhek 1158640f8dcSRussell King dsa_port_disable_rt(dp); 1166457edfeSVivien Didelot 117df02c6ffSLennert Buytenhek dev_mc_unsync(master, dev); 118a748ee24SJiri Pirko dev_uc_unsync(master, dev); 119df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 120df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 121df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) 122df02c6ffSLennert Buytenhek dev_set_promiscuity(master, -1); 123df02c6ffSLennert Buytenhek 1248feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 125a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 126df02c6ffSLennert Buytenhek 12791da11f8SLennert Buytenhek return 0; 12891da11f8SLennert Buytenhek } 12991da11f8SLennert Buytenhek 13091da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 13191da11f8SLennert Buytenhek { 132d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 13317ab4f61SRundong Ge if (dev->flags & IFF_UP) { 13491da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 13517ab4f61SRundong Ge dev_set_allmulti(master, 13617ab4f61SRundong Ge dev->flags & IFF_ALLMULTI ? 1 : -1); 13791da11f8SLennert Buytenhek if (change & IFF_PROMISC) 13817ab4f61SRundong Ge dev_set_promiscuity(master, 13917ab4f61SRundong Ge dev->flags & IFF_PROMISC ? 1 : -1); 14017ab4f61SRundong Ge } 14191da11f8SLennert Buytenhek } 14291da11f8SLennert Buytenhek 14391da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 14491da11f8SLennert Buytenhek { 145d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 14691da11f8SLennert Buytenhek 14791da11f8SLennert Buytenhek dev_mc_sync(master, dev); 148a748ee24SJiri Pirko dev_uc_sync(master, dev); 14991da11f8SLennert Buytenhek } 15091da11f8SLennert Buytenhek 151df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 15291da11f8SLennert Buytenhek { 153d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 154df02c6ffSLennert Buytenhek struct sockaddr *addr = a; 155df02c6ffSLennert Buytenhek int err; 156df02c6ffSLennert Buytenhek 157df02c6ffSLennert Buytenhek if (!is_valid_ether_addr(addr->sa_data)) 158df02c6ffSLennert Buytenhek return -EADDRNOTAVAIL; 159df02c6ffSLennert Buytenhek 160df02c6ffSLennert Buytenhek if (!(dev->flags & IFF_UP)) 161df02c6ffSLennert Buytenhek goto out; 162df02c6ffSLennert Buytenhek 1638feedbb4SJoe Perches if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 164a748ee24SJiri Pirko err = dev_uc_add(master, addr->sa_data); 165df02c6ffSLennert Buytenhek if (err < 0) 166df02c6ffSLennert Buytenhek return err; 167df02c6ffSLennert Buytenhek } 168df02c6ffSLennert Buytenhek 1698feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 170a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 171df02c6ffSLennert Buytenhek 172df02c6ffSLennert Buytenhek out: 173d08f161aSJoe Perches ether_addr_copy(dev->dev_addr, addr->sa_data); 17491da11f8SLennert Buytenhek 17591da11f8SLennert Buytenhek return 0; 17691da11f8SLennert Buytenhek } 17791da11f8SLennert Buytenhek 1782bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx { 1792bedde1aSArkadi Sharshevsky struct net_device *dev; 1802bedde1aSArkadi Sharshevsky struct sk_buff *skb; 1812bedde1aSArkadi Sharshevsky struct netlink_callback *cb; 1822bedde1aSArkadi Sharshevsky int idx; 1832bedde1aSArkadi Sharshevsky }; 1842bedde1aSArkadi Sharshevsky 1852bedde1aSArkadi Sharshevsky static int 1862bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, 1872bedde1aSArkadi Sharshevsky bool is_static, void *data) 1882bedde1aSArkadi Sharshevsky { 1892bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx *dump = data; 1902bedde1aSArkadi Sharshevsky u32 portid = NETLINK_CB(dump->cb->skb).portid; 1912bedde1aSArkadi Sharshevsky u32 seq = dump->cb->nlh->nlmsg_seq; 1922bedde1aSArkadi Sharshevsky struct nlmsghdr *nlh; 1932bedde1aSArkadi Sharshevsky struct ndmsg *ndm; 1942bedde1aSArkadi Sharshevsky 1952bedde1aSArkadi Sharshevsky if (dump->idx < dump->cb->args[2]) 1962bedde1aSArkadi Sharshevsky goto skip; 1972bedde1aSArkadi Sharshevsky 1982bedde1aSArkadi Sharshevsky nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 1992bedde1aSArkadi Sharshevsky sizeof(*ndm), NLM_F_MULTI); 2002bedde1aSArkadi Sharshevsky if (!nlh) 2012bedde1aSArkadi Sharshevsky return -EMSGSIZE; 2022bedde1aSArkadi Sharshevsky 2032bedde1aSArkadi Sharshevsky ndm = nlmsg_data(nlh); 2042bedde1aSArkadi Sharshevsky ndm->ndm_family = AF_BRIDGE; 2052bedde1aSArkadi Sharshevsky ndm->ndm_pad1 = 0; 2062bedde1aSArkadi Sharshevsky ndm->ndm_pad2 = 0; 2072bedde1aSArkadi Sharshevsky ndm->ndm_flags = NTF_SELF; 2082bedde1aSArkadi Sharshevsky ndm->ndm_type = 0; 2092bedde1aSArkadi Sharshevsky ndm->ndm_ifindex = dump->dev->ifindex; 2102bedde1aSArkadi Sharshevsky ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 2112bedde1aSArkadi Sharshevsky 2122bedde1aSArkadi Sharshevsky if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 2132bedde1aSArkadi Sharshevsky goto nla_put_failure; 2142bedde1aSArkadi Sharshevsky 2152bedde1aSArkadi Sharshevsky if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 2162bedde1aSArkadi Sharshevsky goto nla_put_failure; 2172bedde1aSArkadi Sharshevsky 2182bedde1aSArkadi Sharshevsky nlmsg_end(dump->skb, nlh); 2192bedde1aSArkadi Sharshevsky 2202bedde1aSArkadi Sharshevsky skip: 2212bedde1aSArkadi Sharshevsky dump->idx++; 2222bedde1aSArkadi Sharshevsky return 0; 2232bedde1aSArkadi Sharshevsky 2242bedde1aSArkadi Sharshevsky nla_put_failure: 2252bedde1aSArkadi Sharshevsky nlmsg_cancel(dump->skb, nlh); 2262bedde1aSArkadi Sharshevsky return -EMSGSIZE; 2272bedde1aSArkadi Sharshevsky } 2282bedde1aSArkadi Sharshevsky 2292bedde1aSArkadi Sharshevsky static int 2302bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 2312bedde1aSArkadi Sharshevsky struct net_device *dev, struct net_device *filter_dev, 2322bedde1aSArkadi Sharshevsky int *idx) 2332bedde1aSArkadi Sharshevsky { 234d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 2352bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx dump = { 2362bedde1aSArkadi Sharshevsky .dev = dev, 2372bedde1aSArkadi Sharshevsky .skb = skb, 2382bedde1aSArkadi Sharshevsky .cb = cb, 2392bedde1aSArkadi Sharshevsky .idx = *idx, 2402bedde1aSArkadi Sharshevsky }; 2412bedde1aSArkadi Sharshevsky int err; 2422bedde1aSArkadi Sharshevsky 243de40fc5dSVivien Didelot err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump); 2442bedde1aSArkadi Sharshevsky *idx = dump.idx; 245de40fc5dSVivien Didelot 2462bedde1aSArkadi Sharshevsky return err; 2472bedde1aSArkadi Sharshevsky } 2482bedde1aSArkadi Sharshevsky 24991da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 25091da11f8SLennert Buytenhek { 2510336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 2520336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 2530336369dSBrandon Streiff int port = p->dp->index; 2540336369dSBrandon Streiff 2550336369dSBrandon Streiff /* Pass through to switch driver if it supports timestamping */ 2560336369dSBrandon Streiff switch (cmd) { 2570336369dSBrandon Streiff case SIOCGHWTSTAMP: 2580336369dSBrandon Streiff if (ds->ops->port_hwtstamp_get) 2590336369dSBrandon Streiff return ds->ops->port_hwtstamp_get(ds, port, ifr); 2600336369dSBrandon Streiff break; 2610336369dSBrandon Streiff case SIOCSHWTSTAMP: 2620336369dSBrandon Streiff if (ds->ops->port_hwtstamp_set) 2630336369dSBrandon Streiff return ds->ops->port_hwtstamp_set(ds, port, ifr); 2640336369dSBrandon Streiff break; 2650336369dSBrandon Streiff } 2660336369dSBrandon Streiff 267aab9c406SFlorian Fainelli return phylink_mii_ioctl(p->dp->pl, ifr, cmd); 26891da11f8SLennert Buytenhek } 26991da11f8SLennert Buytenhek 27035636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev, 271bae33f2bSVladimir Oltean const struct switchdev_attr *attr) 27235636062SScott Feldman { 273d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 274b8d866acSVivien Didelot int ret; 27535636062SScott Feldman 276058102a6STobias Waldekranz if (!dsa_port_offloads_netdev(dp, attr->orig_dev)) 2775696c8aeSTobias Waldekranz return -EOPNOTSUPP; 2785696c8aeSTobias Waldekranz 27935636062SScott Feldman switch (attr->id) { 2801f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 281bae33f2bSVladimir Oltean ret = dsa_port_set_state(dp, attr->u.stp_state); 28235636062SScott Feldman break; 283fb2dabadSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 284bae33f2bSVladimir Oltean ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering); 285fb2dabadSVivien Didelot break; 28634a79f63SVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 287bae33f2bSVladimir Oltean ret = dsa_port_ageing_time(dp, attr->u.ageing_time); 28834a79f63SVivien Didelot break; 289ea87005aSFlorian Fainelli case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 290bae33f2bSVladimir Oltean ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags); 291ea87005aSFlorian Fainelli break; 29257652796SRussell King case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 293bae33f2bSVladimir Oltean ret = dsa_port_bridge_flags(dp, attr->u.brport_flags); 29457652796SRussell King break; 29508cc83ccSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER: 296bae33f2bSVladimir Oltean ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter); 29708cc83ccSVivien Didelot break; 29835636062SScott Feldman default: 29935636062SScott Feldman ret = -EOPNOTSUPP; 30035636062SScott Feldman break; 30135636062SScott Feldman } 30235636062SScott Feldman 30335636062SScott Feldman return ret; 30435636062SScott Feldman } 30535636062SScott Feldman 3061ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */ 3071ce39f0eSVladimir Oltean static int 3081ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave, 3091ce39f0eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 3101ce39f0eSVladimir Oltean { 3111ce39f0eSVladimir Oltean struct net_device *upper_dev; 3121ce39f0eSVladimir Oltean struct list_head *iter; 3131ce39f0eSVladimir Oltean 3141ce39f0eSVladimir Oltean netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { 3151ce39f0eSVladimir Oltean u16 vid; 3161ce39f0eSVladimir Oltean 3171ce39f0eSVladimir Oltean if (!is_vlan_dev(upper_dev)) 3181ce39f0eSVladimir Oltean continue; 3191ce39f0eSVladimir Oltean 3201ce39f0eSVladimir Oltean vid = vlan_dev_vlan_id(upper_dev); 321b7a9e0daSVladimir Oltean if (vid == vlan->vid) 3221ce39f0eSVladimir Oltean return -EBUSY; 3231ce39f0eSVladimir Oltean } 3241ce39f0eSVladimir Oltean 3251ce39f0eSVladimir Oltean return 0; 3261ce39f0eSVladimir Oltean } 3271ce39f0eSVladimir Oltean 328bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev, 329*0ee2af4eSVladimir Oltean const struct switchdev_obj *obj, 330*0ee2af4eSVladimir Oltean struct netlink_ext_ack *extack) 331bdcff080SVivien Didelot { 3322209158cSVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 333bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 334bdcff080SVivien Didelot struct switchdev_obj_port_vlan vlan; 335b7a9e0daSVladimir Oltean int err; 336bdcff080SVivien Didelot 337058102a6STobias Waldekranz if (!dsa_port_offloads_netdev(dp, obj->orig_dev)) 338bdcff080SVivien Didelot return -EOPNOTSUPP; 339bdcff080SVivien Didelot 340*0ee2af4eSVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) { 341*0ee2af4eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 342c5335d73SVivien Didelot return 0; 343*0ee2af4eSVladimir Oltean } 344c5335d73SVivien Didelot 345bdcff080SVivien Didelot vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj); 346bdcff080SVivien Didelot 3471ce39f0eSVladimir Oltean /* Deny adding a bridge VLAN when there is already an 802.1Q upper with 3481ce39f0eSVladimir Oltean * the same VID. 3491ce39f0eSVladimir Oltean */ 350ffb68fc5SVladimir Oltean if (br_vlan_enabled(dp->bridge_dev)) { 3511ce39f0eSVladimir Oltean rcu_read_lock(); 3521ce39f0eSVladimir Oltean err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan); 3531ce39f0eSVladimir Oltean rcu_read_unlock(); 3541ce39f0eSVladimir Oltean if (err) 3551ce39f0eSVladimir Oltean return err; 3561ce39f0eSVladimir Oltean } 3571ce39f0eSVladimir Oltean 358ffb68fc5SVladimir Oltean err = dsa_port_vlan_add(dp, &vlan); 359bdcff080SVivien Didelot if (err) 360bdcff080SVivien Didelot return err; 361bdcff080SVivien Didelot 362b9499904SVivien Didelot /* We need the dedicated CPU port to be a member of the VLAN as well. 363b9499904SVivien Didelot * Even though drivers often handle CPU membership in special ways, 364b9499904SVivien Didelot * it doesn't make sense to program a PVID, so clear this flag. 365b9499904SVivien Didelot */ 366b9499904SVivien Didelot vlan.flags &= ~BRIDGE_VLAN_INFO_PVID; 367b9499904SVivien Didelot 368ffb68fc5SVladimir Oltean err = dsa_port_vlan_add(dp->cpu_dp, &vlan); 3697e1741b4SVivien Didelot if (err) 3707e1741b4SVivien Didelot return err; 3717e1741b4SVivien Didelot 372b7a9e0daSVladimir Oltean return vlan_vid_add(master, htons(ETH_P_8021Q), vlan.vid); 373bdcff080SVivien Didelot } 374bdcff080SVivien Didelot 375ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev, 376648b4a99SJiri Pirko const struct switchdev_obj *obj, 37779b139f4SVivien Didelot struct netlink_ext_ack *extack) 378ba14d9ebSVivien Didelot { 379d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 380ba14d9ebSVivien Didelot int err; 381ba14d9ebSVivien Didelot 3829e8f4a54SJiri Pirko switch (obj->id) { 3838df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 384058102a6STobias Waldekranz if (!dsa_port_offloads_netdev(dp, obj->orig_dev)) 38579b139f4SVivien Didelot return -EOPNOTSUPP; 386ffb68fc5SVladimir Oltean err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 3878df30255SVivien Didelot break; 3885f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 3895f4dbc50SAndrew Lunn /* DSA can directly translate this to a normal MDB add, 3905f4dbc50SAndrew Lunn * but on the CPU port. 3915f4dbc50SAndrew Lunn */ 392ffb68fc5SVladimir Oltean err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 3935f4dbc50SAndrew Lunn break; 39457d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 395*0ee2af4eSVladimir Oltean err = dsa_slave_vlan_add(dev, obj, extack); 39611149536SVivien Didelot break; 397ba14d9ebSVivien Didelot default: 398ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 399ba14d9ebSVivien Didelot break; 400ba14d9ebSVivien Didelot } 401ba14d9ebSVivien Didelot 402ba14d9ebSVivien Didelot return err; 403ba14d9ebSVivien Didelot } 404ba14d9ebSVivien Didelot 405bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev, 406bdcff080SVivien Didelot const struct switchdev_obj *obj) 407bdcff080SVivien Didelot { 4082209158cSVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 409bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 4102209158cSVladimir Oltean struct switchdev_obj_port_vlan *vlan; 411b7a9e0daSVladimir Oltean int err; 412bdcff080SVivien Didelot 413058102a6STobias Waldekranz if (!dsa_port_offloads_netdev(dp, obj->orig_dev)) 414bdcff080SVivien Didelot return -EOPNOTSUPP; 415bdcff080SVivien Didelot 41654a0ed0dSRussell King if (dsa_port_skip_vlan_configuration(dp)) 417c5335d73SVivien Didelot return 0; 418c5335d73SVivien Didelot 4192209158cSVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 4202209158cSVladimir Oltean 4217e1741b4SVivien Didelot /* Do not deprogram the CPU port as it may be shared with other user 4227e1741b4SVivien Didelot * ports which can be members of this VLAN as well. 4237e1741b4SVivien Didelot */ 4242209158cSVladimir Oltean err = dsa_port_vlan_del(dp, vlan); 4252209158cSVladimir Oltean if (err) 4262209158cSVladimir Oltean return err; 4272209158cSVladimir Oltean 428b7a9e0daSVladimir Oltean vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid); 4292209158cSVladimir Oltean 4302209158cSVladimir Oltean return 0; 431bdcff080SVivien Didelot } 432bdcff080SVivien Didelot 433ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev, 434648b4a99SJiri Pirko const struct switchdev_obj *obj) 435ba14d9ebSVivien Didelot { 436d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 437ba14d9ebSVivien Didelot int err; 438ba14d9ebSVivien Didelot 4399e8f4a54SJiri Pirko switch (obj->id) { 4408df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 441058102a6STobias Waldekranz if (!dsa_port_offloads_netdev(dp, obj->orig_dev)) 44279b139f4SVivien Didelot return -EOPNOTSUPP; 443bcebb976SVivien Didelot err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 4448df30255SVivien Didelot break; 4455f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 4465f4dbc50SAndrew Lunn /* DSA can directly translate this to a normal MDB add, 4475f4dbc50SAndrew Lunn * but on the CPU port. 4485f4dbc50SAndrew Lunn */ 4495f4dbc50SAndrew Lunn err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 4505f4dbc50SAndrew Lunn break; 45157d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 452bdcff080SVivien Didelot err = dsa_slave_vlan_del(dev, obj); 45311149536SVivien Didelot break; 454ba14d9ebSVivien Didelot default: 455ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 456ba14d9ebSVivien Didelot break; 457ba14d9ebSVivien Didelot } 458ba14d9ebSVivien Didelot 459ba14d9ebSVivien Didelot return err; 460ba14d9ebSVivien Didelot } 461ba14d9ebSVivien Didelot 462929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev, 463929d6c14SFlorian Fainelli struct netdev_phys_item_id *ppid) 464b73adef6SFlorian Fainelli { 465d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 466d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 467a42c8e33SAndrew Lunn struct dsa_switch_tree *dst = ds->dst; 468b73adef6SFlorian Fainelli 46915b04aceSJiri Pirko /* For non-legacy ports, devlink is used and it takes 47015b04aceSJiri Pirko * care of the name generation. This ndo implementation 47115b04aceSJiri Pirko * should be removed with legacy support. 47215b04aceSJiri Pirko */ 47315b04aceSJiri Pirko if (dp->ds->devlink) 47415b04aceSJiri Pirko return -EOPNOTSUPP; 47515b04aceSJiri Pirko 476929d6c14SFlorian Fainelli ppid->id_len = sizeof(dst->index); 477929d6c14SFlorian Fainelli memcpy(&ppid->id, &dst->index, ppid->id_len); 478929d6c14SFlorian Fainelli 479929d6c14SFlorian Fainelli return 0; 480929d6c14SFlorian Fainelli } 481929d6c14SFlorian Fainelli 4824fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 48304ff53f9SFlorian Fainelli struct sk_buff *skb) 48404ff53f9SFlorian Fainelli { 48504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 4864fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 4874fa7b718SVivien Didelot 488f78ed220SEric Dumazet return netpoll_send_skb(p->netpoll, skb); 48904ff53f9SFlorian Fainelli #else 49004ff53f9SFlorian Fainelli BUG(); 49104ff53f9SFlorian Fainelli return NETDEV_TX_OK; 492f78ed220SEric Dumazet #endif 49304ff53f9SFlorian Fainelli } 49404ff53f9SFlorian Fainelli 49590af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 49690af1059SBrandon Streiff struct sk_buff *skb) 49790af1059SBrandon Streiff { 49890af1059SBrandon Streiff struct dsa_switch *ds = p->dp->ds; 49990af1059SBrandon Streiff struct sk_buff *clone; 50090af1059SBrandon Streiff unsigned int type; 50190af1059SBrandon Streiff 50290af1059SBrandon Streiff type = ptp_classify_raw(skb); 50390af1059SBrandon Streiff if (type == PTP_CLASS_NONE) 50490af1059SBrandon Streiff return; 50590af1059SBrandon Streiff 50690af1059SBrandon Streiff if (!ds->ops->port_txtstamp) 50790af1059SBrandon Streiff return; 50890af1059SBrandon Streiff 50990af1059SBrandon Streiff clone = skb_clone_sk(skb); 51090af1059SBrandon Streiff if (!clone) 51190af1059SBrandon Streiff return; 51290af1059SBrandon Streiff 51330abc9cdSChristian Eggers if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) { 514146d442cSVladimir Oltean DSA_SKB_CB(skb)->clone = clone; 51590af1059SBrandon Streiff return; 51630abc9cdSChristian Eggers } 51790af1059SBrandon Streiff 51890af1059SBrandon Streiff kfree_skb(clone); 51990af1059SBrandon Streiff } 52090af1059SBrandon Streiff 52197a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev) 52297a69a0dSVladimir Oltean { 52397a69a0dSVladimir Oltean /* SKB for netpoll still need to be mangled with the protocol-specific 52497a69a0dSVladimir Oltean * tag to be successfully transmitted 52597a69a0dSVladimir Oltean */ 52697a69a0dSVladimir Oltean if (unlikely(netpoll_tx_running(dev))) 52797a69a0dSVladimir Oltean return dsa_slave_netpoll_send_skb(dev, skb); 52897a69a0dSVladimir Oltean 52997a69a0dSVladimir Oltean /* Queue the SKB for transmission on the parent interface, but 53097a69a0dSVladimir Oltean * do not modify its EtherType 53197a69a0dSVladimir Oltean */ 53297a69a0dSVladimir Oltean skb->dev = dsa_slave_to_master(dev); 53397a69a0dSVladimir Oltean dev_queue_xmit(skb); 53497a69a0dSVladimir Oltean 53597a69a0dSVladimir Oltean return NETDEV_TX_OK; 53697a69a0dSVladimir Oltean } 53797a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb); 53897a69a0dSVladimir Oltean 539a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev) 540a3b0b647SVladimir Oltean { 541a3b0b647SVladimir Oltean int needed_headroom = dev->needed_headroom; 542a3b0b647SVladimir Oltean int needed_tailroom = dev->needed_tailroom; 543a3b0b647SVladimir Oltean 544a3b0b647SVladimir Oltean /* For tail taggers, we need to pad short frames ourselves, to ensure 545a3b0b647SVladimir Oltean * that the tail tag does not fail at its role of being at the end of 546a3b0b647SVladimir Oltean * the packet, once the master interface pads the frame. Account for 547a3b0b647SVladimir Oltean * that pad length here, and pad later. 548a3b0b647SVladimir Oltean */ 549a3b0b647SVladimir Oltean if (unlikely(needed_tailroom && skb->len < ETH_ZLEN)) 550a3b0b647SVladimir Oltean needed_tailroom += ETH_ZLEN - skb->len; 551a3b0b647SVladimir Oltean /* skb_headroom() returns unsigned int... */ 552a3b0b647SVladimir Oltean needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0); 553a3b0b647SVladimir Oltean needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0); 554a3b0b647SVladimir Oltean 555a3b0b647SVladimir Oltean if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb))) 556a3b0b647SVladimir Oltean /* No reallocation needed, yay! */ 557a3b0b647SVladimir Oltean return 0; 558a3b0b647SVladimir Oltean 559a3b0b647SVladimir Oltean return pskb_expand_head(skb, needed_headroom, needed_tailroom, 560a3b0b647SVladimir Oltean GFP_ATOMIC); 561a3b0b647SVladimir Oltean } 562a3b0b647SVladimir Oltean 5633e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 5643e8a72d1SFlorian Fainelli { 5653e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 5664ed70ce9SFlorian Fainelli struct sk_buff *nskb; 5673e8a72d1SFlorian Fainelli 5686a900628SHeiner Kallweit dev_sw_netstats_tx_add(dev, 1, skb->len); 5693e8a72d1SFlorian Fainelli 570146d442cSVladimir Oltean DSA_SKB_CB(skb)->clone = NULL; 57187671375SVladimir Oltean 57290af1059SBrandon Streiff /* Identify PTP protocol packets, clone them, and pass them to the 57390af1059SBrandon Streiff * switch driver 57490af1059SBrandon Streiff */ 57590af1059SBrandon Streiff dsa_skb_tx_timestamp(p, skb); 57690af1059SBrandon Streiff 577a3b0b647SVladimir Oltean if (dsa_realloc_skb(skb, dev)) { 578a3b0b647SVladimir Oltean dev_kfree_skb_any(skb); 579a3b0b647SVladimir Oltean return NETDEV_TX_OK; 580a3b0b647SVladimir Oltean } 581a3b0b647SVladimir Oltean 582a3b0b647SVladimir Oltean /* needed_tailroom should still be 'warm' in the cache line from 583a3b0b647SVladimir Oltean * dsa_realloc_skb(), which has also ensured that padding is safe. 584a3b0b647SVladimir Oltean */ 585a3b0b647SVladimir Oltean if (dev->needed_tailroom) 586a3b0b647SVladimir Oltean eth_skb_pad(skb); 587a3b0b647SVladimir Oltean 588fe47d563SVivien Didelot /* Transmit function may have to reallocate the original SKB, 589fe47d563SVivien Didelot * in which case it must have freed it. Only free it here on error. 590fe47d563SVivien Didelot */ 5914ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 592fe47d563SVivien Didelot if (!nskb) { 593fe47d563SVivien Didelot kfree_skb(skb); 5944ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 595fe47d563SVivien Didelot } 5965aed85ceSFlorian Fainelli 59797a69a0dSVladimir Oltean return dsa_enqueue_skb(nskb, dev); 59897a69a0dSVladimir Oltean } 59904ff53f9SFlorian Fainelli 60091da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 60191da11f8SLennert Buytenhek 60291da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 60391da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 60491da11f8SLennert Buytenhek { 6057826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 6067826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 6077826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 60891da11f8SLennert Buytenhek } 60991da11f8SLennert Buytenhek 6103d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 6113d762a0fSGuenter Roeck { 612d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 613d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6143d762a0fSGuenter Roeck 6159d490b4eSVivien Didelot if (ds->ops->get_regs_len) 616d945097bSVivien Didelot return ds->ops->get_regs_len(ds, dp->index); 6173d762a0fSGuenter Roeck 6183d762a0fSGuenter Roeck return -EOPNOTSUPP; 6193d762a0fSGuenter Roeck } 6203d762a0fSGuenter Roeck 6213d762a0fSGuenter Roeck static void 6223d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 6233d762a0fSGuenter Roeck { 624d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 625d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6263d762a0fSGuenter Roeck 6279d490b4eSVivien Didelot if (ds->ops->get_regs) 628d945097bSVivien Didelot ds->ops->get_regs(ds, dp->index, regs, _p); 6293d762a0fSGuenter Roeck } 6303d762a0fSGuenter Roeck 631aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev) 632aab9c406SFlorian Fainelli { 633aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 634aab9c406SFlorian Fainelli 635aab9c406SFlorian Fainelli return phylink_ethtool_nway_reset(dp->pl); 636aab9c406SFlorian Fainelli } 637aab9c406SFlorian Fainelli 6386793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 6396793abb4SGuenter Roeck { 640d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 641d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6426793abb4SGuenter Roeck 6430e576044SAndrew Lunn if (ds->cd && ds->cd->eeprom_len) 644ff04955cSAndrew Lunn return ds->cd->eeprom_len; 6456793abb4SGuenter Roeck 6469d490b4eSVivien Didelot if (ds->ops->get_eeprom_len) 6479d490b4eSVivien Didelot return ds->ops->get_eeprom_len(ds); 6486793abb4SGuenter Roeck 6496793abb4SGuenter Roeck return 0; 6506793abb4SGuenter Roeck } 6516793abb4SGuenter Roeck 6526793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 6536793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 6546793abb4SGuenter Roeck { 655d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 656d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6576793abb4SGuenter Roeck 6589d490b4eSVivien Didelot if (ds->ops->get_eeprom) 6599d490b4eSVivien Didelot return ds->ops->get_eeprom(ds, eeprom, data); 6606793abb4SGuenter Roeck 6616793abb4SGuenter Roeck return -EOPNOTSUPP; 6626793abb4SGuenter Roeck } 6636793abb4SGuenter Roeck 6646793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 6656793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 6666793abb4SGuenter Roeck { 667d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 668d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6696793abb4SGuenter Roeck 6709d490b4eSVivien Didelot if (ds->ops->set_eeprom) 6719d490b4eSVivien Didelot return ds->ops->set_eeprom(ds, eeprom, data); 6726793abb4SGuenter Roeck 6736793abb4SGuenter Roeck return -EOPNOTSUPP; 6746793abb4SGuenter Roeck } 6756793abb4SGuenter Roeck 67691da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 67791da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 67891da11f8SLennert Buytenhek { 679d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 680d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 68191da11f8SLennert Buytenhek 68291da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 68391da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 68491da11f8SLennert Buytenhek 68591da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 68691da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 68791da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 68891da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 6899d490b4eSVivien Didelot if (ds->ops->get_strings) 69089f09048SFlorian Fainelli ds->ops->get_strings(ds, dp->index, stringset, 69189f09048SFlorian Fainelli data + 4 * len); 69291da11f8SLennert Buytenhek } 69391da11f8SLennert Buytenhek } 69491da11f8SLennert Buytenhek 69591da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 69691da11f8SLennert Buytenhek struct ethtool_stats *stats, 69791da11f8SLennert Buytenhek uint64_t *data) 69891da11f8SLennert Buytenhek { 699d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 700d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 7015f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 702f613ed66SFlorian Fainelli unsigned int start; 7035f6b4e14SFlorian Fainelli int i; 70491da11f8SLennert Buytenhek 7055f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 7065f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 7075f6b4e14SFlorian Fainelli 7086a900628SHeiner Kallweit s = per_cpu_ptr(dev->tstats, i); 709f613ed66SFlorian Fainelli do { 7105f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 7115f6b4e14SFlorian Fainelli tx_packets = s->tx_packets; 7125f6b4e14SFlorian Fainelli tx_bytes = s->tx_bytes; 7135f6b4e14SFlorian Fainelli rx_packets = s->rx_packets; 7145f6b4e14SFlorian Fainelli rx_bytes = s->rx_bytes; 7155f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 7165f6b4e14SFlorian Fainelli data[0] += tx_packets; 7175f6b4e14SFlorian Fainelli data[1] += tx_bytes; 7185f6b4e14SFlorian Fainelli data[2] += rx_packets; 7195f6b4e14SFlorian Fainelli data[3] += rx_bytes; 7205f6b4e14SFlorian Fainelli } 7219d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 722d945097bSVivien Didelot ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 72391da11f8SLennert Buytenhek } 72491da11f8SLennert Buytenhek 72591da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 72691da11f8SLennert Buytenhek { 727d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 728d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 72991da11f8SLennert Buytenhek 73091da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 73191da11f8SLennert Buytenhek int count; 73291da11f8SLennert Buytenhek 73391da11f8SLennert Buytenhek count = 4; 7349d490b4eSVivien Didelot if (ds->ops->get_sset_count) 73589f09048SFlorian Fainelli count += ds->ops->get_sset_count(ds, dp->index, sset); 73691da11f8SLennert Buytenhek 73791da11f8SLennert Buytenhek return count; 73891da11f8SLennert Buytenhek } 73991da11f8SLennert Buytenhek 74091da11f8SLennert Buytenhek return -EOPNOTSUPP; 74191da11f8SLennert Buytenhek } 74291da11f8SLennert Buytenhek 74319e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 74419e57c4eSFlorian Fainelli { 745d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 746d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 74719e57c4eSFlorian Fainelli 748aab9c406SFlorian Fainelli phylink_ethtool_get_wol(dp->pl, w); 749aab9c406SFlorian Fainelli 7509d490b4eSVivien Didelot if (ds->ops->get_wol) 751d945097bSVivien Didelot ds->ops->get_wol(ds, dp->index, w); 75219e57c4eSFlorian Fainelli } 75319e57c4eSFlorian Fainelli 75419e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 75519e57c4eSFlorian Fainelli { 756d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 757d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 75819e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 75919e57c4eSFlorian Fainelli 760aab9c406SFlorian Fainelli phylink_ethtool_set_wol(dp->pl, w); 761aab9c406SFlorian Fainelli 7629d490b4eSVivien Didelot if (ds->ops->set_wol) 763d945097bSVivien Didelot ret = ds->ops->set_wol(ds, dp->index, w); 76419e57c4eSFlorian Fainelli 76519e57c4eSFlorian Fainelli return ret; 76619e57c4eSFlorian Fainelli } 76719e57c4eSFlorian Fainelli 7687905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 7697905288fSFlorian Fainelli { 770d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 771d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 7727905288fSFlorian Fainelli int ret; 7737905288fSFlorian Fainelli 7747b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 77500670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 7767b9cc738SVivien Didelot return -ENODEV; 7777b9cc738SVivien Didelot 77808f50061SVivien Didelot if (!ds->ops->set_mac_eee) 7797905288fSFlorian Fainelli return -EOPNOTSUPP; 7807905288fSFlorian Fainelli 781d945097bSVivien Didelot ret = ds->ops->set_mac_eee(ds, dp->index, e); 7827905288fSFlorian Fainelli if (ret) 7837905288fSFlorian Fainelli return ret; 7847905288fSFlorian Fainelli 785aab9c406SFlorian Fainelli return phylink_ethtool_set_eee(dp->pl, e); 7867905288fSFlorian Fainelli } 7877905288fSFlorian Fainelli 7887905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 7897905288fSFlorian Fainelli { 790d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 791d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 7927905288fSFlorian Fainelli int ret; 7937905288fSFlorian Fainelli 7947b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 79500670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 7967b9cc738SVivien Didelot return -ENODEV; 7977b9cc738SVivien Didelot 79808f50061SVivien Didelot if (!ds->ops->get_mac_eee) 7997905288fSFlorian Fainelli return -EOPNOTSUPP; 8007905288fSFlorian Fainelli 801d945097bSVivien Didelot ret = ds->ops->get_mac_eee(ds, dp->index, e); 8027905288fSFlorian Fainelli if (ret) 8037905288fSFlorian Fainelli return ret; 8047905288fSFlorian Fainelli 805aab9c406SFlorian Fainelli return phylink_ethtool_get_eee(dp->pl, e); 806aab9c406SFlorian Fainelli } 807aab9c406SFlorian Fainelli 808aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev, 809aab9c406SFlorian Fainelli struct ethtool_link_ksettings *cmd) 810aab9c406SFlorian Fainelli { 811aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 812aab9c406SFlorian Fainelli 813aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_get(dp->pl, cmd); 814aab9c406SFlorian Fainelli } 815aab9c406SFlorian Fainelli 816aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev, 817aab9c406SFlorian Fainelli const struct ethtool_link_ksettings *cmd) 818aab9c406SFlorian Fainelli { 819aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 820aab9c406SFlorian Fainelli 821aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_set(dp->pl, cmd); 8227905288fSFlorian Fainelli } 8237905288fSFlorian Fainelli 824a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev, 825a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 826a2a1a13bSHeiner Kallweit { 827a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 828a2a1a13bSHeiner Kallweit 829a2a1a13bSHeiner Kallweit phylink_ethtool_get_pauseparam(dp->pl, pause); 830a2a1a13bSHeiner Kallweit } 831a2a1a13bSHeiner Kallweit 832a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev, 833a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 834a2a1a13bSHeiner Kallweit { 835a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 836a2a1a13bSHeiner Kallweit 837a2a1a13bSHeiner Kallweit return phylink_ethtool_set_pauseparam(dp->pl, pause); 838a2a1a13bSHeiner Kallweit } 839a2a1a13bSHeiner Kallweit 84004ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 84104ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 84204ff53f9SFlorian Fainelli struct netpoll_info *ni) 84304ff53f9SFlorian Fainelli { 844d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 84504ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 84604ff53f9SFlorian Fainelli struct netpoll *netpoll; 84704ff53f9SFlorian Fainelli int err = 0; 84804ff53f9SFlorian Fainelli 84904ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 85004ff53f9SFlorian Fainelli if (!netpoll) 85104ff53f9SFlorian Fainelli return -ENOMEM; 85204ff53f9SFlorian Fainelli 85304ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 85404ff53f9SFlorian Fainelli if (err) { 85504ff53f9SFlorian Fainelli kfree(netpoll); 85604ff53f9SFlorian Fainelli goto out; 85704ff53f9SFlorian Fainelli } 85804ff53f9SFlorian Fainelli 85904ff53f9SFlorian Fainelli p->netpoll = netpoll; 86004ff53f9SFlorian Fainelli out: 86104ff53f9SFlorian Fainelli return err; 86204ff53f9SFlorian Fainelli } 86304ff53f9SFlorian Fainelli 86404ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 86504ff53f9SFlorian Fainelli { 86604ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 86704ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 86804ff53f9SFlorian Fainelli 86904ff53f9SFlorian Fainelli if (!netpoll) 87004ff53f9SFlorian Fainelli return; 87104ff53f9SFlorian Fainelli 87204ff53f9SFlorian Fainelli p->netpoll = NULL; 87304ff53f9SFlorian Fainelli 874c9fbd71fSDebabrata Banerjee __netpoll_free(netpoll); 87504ff53f9SFlorian Fainelli } 87604ff53f9SFlorian Fainelli 87704ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 87804ff53f9SFlorian Fainelli { 87904ff53f9SFlorian Fainelli } 88004ff53f9SFlorian Fainelli #endif 88104ff53f9SFlorian Fainelli 88244bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev, 88344bb765cSFlorian Fainelli char *name, size_t len) 88444bb765cSFlorian Fainelli { 885d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 88644bb765cSFlorian Fainelli 887d484210bSJiri Pirko /* For non-legacy ports, devlink is used and it takes 888d484210bSJiri Pirko * care of the name generation. This ndo implementation 889d484210bSJiri Pirko * should be removed with legacy support. 890d484210bSJiri Pirko */ 891d484210bSJiri Pirko if (dp->ds->devlink) 892d484210bSJiri Pirko return -EOPNOTSUPP; 893d484210bSJiri Pirko 894d945097bSVivien Didelot if (snprintf(name, len, "p%d", dp->index) >= len) 89544bb765cSFlorian Fainelli return -EINVAL; 8963a543ef4SFlorian Fainelli 8973a543ef4SFlorian Fainelli return 0; 8983a543ef4SFlorian Fainelli } 8993a543ef4SFlorian Fainelli 900f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry * 9014fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 902f50f2127SFlorian Fainelli { 9034fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 904f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 905f50f2127SFlorian Fainelli 906f50f2127SFlorian Fainelli list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 907f50f2127SFlorian Fainelli if (mall_tc_entry->cookie == cookie) 908f50f2127SFlorian Fainelli return mall_tc_entry; 909f50f2127SFlorian Fainelli 910f50f2127SFlorian Fainelli return NULL; 911f50f2127SFlorian Fainelli } 912f50f2127SFlorian Fainelli 913e13c2075SVladimir Oltean static int 914e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev, 915f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls, 916f50f2127SFlorian Fainelli bool ingress) 917f50f2127SFlorian Fainelli { 918d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 919f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 920e13c2075SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror; 921f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 922d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 9239681e8b3SPieter Jansen van Vuuren struct flow_action_entry *act; 924d945097bSVivien Didelot struct dsa_port *to_dp; 925e13c2075SVladimir Oltean int err; 926e13c2075SVladimir Oltean 927f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_add) 92834297176SVladimir Oltean return -EOPNOTSUPP; 929f50f2127SFlorian Fainelli 93053eca1f3SJakub Kicinski if (!flow_action_basic_hw_stats_check(&cls->rule->action, 931319a1d19SJiri Pirko cls->common.extack)) 93234297176SVladimir Oltean return -EOPNOTSUPP; 933319a1d19SJiri Pirko 9349681e8b3SPieter Jansen van Vuuren act = &cls->rule->action.entries[0]; 935f50f2127SFlorian Fainelli 93665722159SVladimir Oltean if (!act->dev) 93765722159SVladimir Oltean return -EINVAL; 93865722159SVladimir Oltean 9399681e8b3SPieter Jansen van Vuuren if (!dsa_slave_dev_check(act->dev)) 940f50f2127SFlorian Fainelli return -EOPNOTSUPP; 941f50f2127SFlorian Fainelli 942f50f2127SFlorian Fainelli mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 943f50f2127SFlorian Fainelli if (!mall_tc_entry) 944f50f2127SFlorian Fainelli return -ENOMEM; 945f50f2127SFlorian Fainelli 946f50f2127SFlorian Fainelli mall_tc_entry->cookie = cls->cookie; 947f50f2127SFlorian Fainelli mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 948f50f2127SFlorian Fainelli mirror = &mall_tc_entry->mirror; 949f50f2127SFlorian Fainelli 9509681e8b3SPieter Jansen van Vuuren to_dp = dsa_slave_to_port(act->dev); 951f50f2127SFlorian Fainelli 952d945097bSVivien Didelot mirror->to_local_port = to_dp->index; 953f50f2127SFlorian Fainelli mirror->ingress = ingress; 954f50f2127SFlorian Fainelli 955d945097bSVivien Didelot err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); 956f50f2127SFlorian Fainelli if (err) { 957f50f2127SFlorian Fainelli kfree(mall_tc_entry); 958f50f2127SFlorian Fainelli return err; 959f50f2127SFlorian Fainelli } 960f50f2127SFlorian Fainelli 961f50f2127SFlorian Fainelli list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 962e13c2075SVladimir Oltean 963e13c2075SVladimir Oltean return err; 964f50f2127SFlorian Fainelli } 965f50f2127SFlorian Fainelli 96634297176SVladimir Oltean static int 96734297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev, 96834297176SVladimir Oltean struct tc_cls_matchall_offload *cls, 96934297176SVladimir Oltean bool ingress) 97034297176SVladimir Oltean { 97134297176SVladimir Oltean struct netlink_ext_ack *extack = cls->common.extack; 97234297176SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 97334297176SVladimir Oltean struct dsa_slave_priv *p = netdev_priv(dev); 97434297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer; 97534297176SVladimir Oltean struct dsa_mall_tc_entry *mall_tc_entry; 97634297176SVladimir Oltean struct dsa_switch *ds = dp->ds; 97734297176SVladimir Oltean struct flow_action_entry *act; 97834297176SVladimir Oltean int err; 97934297176SVladimir Oltean 98034297176SVladimir Oltean if (!ds->ops->port_policer_add) { 98134297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 982c75a33c8SJacob Keller "Policing offload not implemented"); 98334297176SVladimir Oltean return -EOPNOTSUPP; 98434297176SVladimir Oltean } 98534297176SVladimir Oltean 98634297176SVladimir Oltean if (!ingress) { 98734297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 988c75a33c8SJacob Keller "Only supported on ingress qdisc"); 98934297176SVladimir Oltean return -EOPNOTSUPP; 99034297176SVladimir Oltean } 99134297176SVladimir Oltean 99234297176SVladimir Oltean if (!flow_action_basic_hw_stats_check(&cls->rule->action, 99334297176SVladimir Oltean cls->common.extack)) 99434297176SVladimir Oltean return -EOPNOTSUPP; 99534297176SVladimir Oltean 99634297176SVladimir Oltean list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) { 99734297176SVladimir Oltean if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) { 99834297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 999c75a33c8SJacob Keller "Only one port policer allowed"); 100034297176SVladimir Oltean return -EEXIST; 100134297176SVladimir Oltean } 100234297176SVladimir Oltean } 100334297176SVladimir Oltean 100434297176SVladimir Oltean act = &cls->rule->action.entries[0]; 100534297176SVladimir Oltean 100634297176SVladimir Oltean mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 100734297176SVladimir Oltean if (!mall_tc_entry) 100834297176SVladimir Oltean return -ENOMEM; 100934297176SVladimir Oltean 101034297176SVladimir Oltean mall_tc_entry->cookie = cls->cookie; 101134297176SVladimir Oltean mall_tc_entry->type = DSA_PORT_MALL_POLICER; 101234297176SVladimir Oltean policer = &mall_tc_entry->policer; 101334297176SVladimir Oltean policer->rate_bytes_per_sec = act->police.rate_bytes_ps; 101434297176SVladimir Oltean policer->burst = act->police.burst; 101534297176SVladimir Oltean 101634297176SVladimir Oltean err = ds->ops->port_policer_add(ds, dp->index, policer); 101734297176SVladimir Oltean if (err) { 101834297176SVladimir Oltean kfree(mall_tc_entry); 101934297176SVladimir Oltean return err; 102034297176SVladimir Oltean } 102134297176SVladimir Oltean 102234297176SVladimir Oltean list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 102334297176SVladimir Oltean 102434297176SVladimir Oltean return err; 102534297176SVladimir Oltean } 102634297176SVladimir Oltean 1027e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev, 1028e13c2075SVladimir Oltean struct tc_cls_matchall_offload *cls, 1029e13c2075SVladimir Oltean bool ingress) 1030e13c2075SVladimir Oltean { 1031e13c2075SVladimir Oltean int err = -EOPNOTSUPP; 1032e13c2075SVladimir Oltean 1033e13c2075SVladimir Oltean if (cls->common.protocol == htons(ETH_P_ALL) && 1034e13c2075SVladimir Oltean flow_offload_has_one_action(&cls->rule->action) && 1035e13c2075SVladimir Oltean cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED) 1036e13c2075SVladimir Oltean err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress); 103734297176SVladimir Oltean else if (flow_offload_has_one_action(&cls->rule->action) && 103834297176SVladimir Oltean cls->rule->action.entries[0].id == FLOW_ACTION_POLICE) 103934297176SVladimir Oltean err = dsa_slave_add_cls_matchall_police(dev, cls, ingress); 1040e13c2075SVladimir Oltean 1041e13c2075SVladimir Oltean return err; 1042f50f2127SFlorian Fainelli } 1043f50f2127SFlorian Fainelli 1044f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev, 1045f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls) 1046f50f2127SFlorian Fainelli { 1047d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1048f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1049d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1050f50f2127SFlorian Fainelli 10514fa7b718SVivien Didelot mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 1052f50f2127SFlorian Fainelli if (!mall_tc_entry) 1053f50f2127SFlorian Fainelli return; 1054f50f2127SFlorian Fainelli 1055f50f2127SFlorian Fainelli list_del(&mall_tc_entry->list); 1056f50f2127SFlorian Fainelli 1057f50f2127SFlorian Fainelli switch (mall_tc_entry->type) { 1058f50f2127SFlorian Fainelli case DSA_PORT_MALL_MIRROR: 105934297176SVladimir Oltean if (ds->ops->port_mirror_del) 106034297176SVladimir Oltean ds->ops->port_mirror_del(ds, dp->index, 106134297176SVladimir Oltean &mall_tc_entry->mirror); 106234297176SVladimir Oltean break; 106334297176SVladimir Oltean case DSA_PORT_MALL_POLICER: 106434297176SVladimir Oltean if (ds->ops->port_policer_del) 106534297176SVladimir Oltean ds->ops->port_policer_del(ds, dp->index); 1066f50f2127SFlorian Fainelli break; 1067f50f2127SFlorian Fainelli default: 1068f50f2127SFlorian Fainelli WARN_ON(1); 1069f50f2127SFlorian Fainelli } 1070f50f2127SFlorian Fainelli 1071f50f2127SFlorian Fainelli kfree(mall_tc_entry); 1072f50f2127SFlorian Fainelli } 1073f50f2127SFlorian Fainelli 10743fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 10756b3eb752SJiri Pirko struct tc_cls_matchall_offload *cls, 10766b3eb752SJiri Pirko bool ingress) 1077f50f2127SFlorian Fainelli { 10785fd9fc4eSJiri Pirko if (cls->common.chain_index) 1079a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 1080f50f2127SFlorian Fainelli 10813fbae382SJiri Pirko switch (cls->command) { 10823fbae382SJiri Pirko case TC_CLSMATCHALL_REPLACE: 10835fd9fc4eSJiri Pirko return dsa_slave_add_cls_matchall(dev, cls, ingress); 10843fbae382SJiri Pirko case TC_CLSMATCHALL_DESTROY: 10853fbae382SJiri Pirko dsa_slave_del_cls_matchall(dev, cls); 10863fbae382SJiri Pirko return 0; 10873fbae382SJiri Pirko default: 10883fbae382SJiri Pirko return -EOPNOTSUPP; 10893fbae382SJiri Pirko } 10903fbae382SJiri Pirko } 10913fbae382SJiri Pirko 1092ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev, 1093ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1094ed11bb1fSVladimir Oltean bool ingress) 1095ed11bb1fSVladimir Oltean { 1096ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1097ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1098ed11bb1fSVladimir Oltean int port = dp->index; 1099ed11bb1fSVladimir Oltean 1100ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_add) 1101ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1102ed11bb1fSVladimir Oltean 1103ed11bb1fSVladimir Oltean return ds->ops->cls_flower_add(ds, port, cls, ingress); 1104ed11bb1fSVladimir Oltean } 1105ed11bb1fSVladimir Oltean 1106ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev, 1107ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1108ed11bb1fSVladimir Oltean bool ingress) 1109ed11bb1fSVladimir Oltean { 1110ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1111ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1112ed11bb1fSVladimir Oltean int port = dp->index; 1113ed11bb1fSVladimir Oltean 1114ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_del) 1115ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1116ed11bb1fSVladimir Oltean 1117ed11bb1fSVladimir Oltean return ds->ops->cls_flower_del(ds, port, cls, ingress); 1118ed11bb1fSVladimir Oltean } 1119ed11bb1fSVladimir Oltean 1120ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev, 1121ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1122ed11bb1fSVladimir Oltean bool ingress) 1123ed11bb1fSVladimir Oltean { 1124ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1125ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1126ed11bb1fSVladimir Oltean int port = dp->index; 1127ed11bb1fSVladimir Oltean 1128ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_stats) 1129ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1130ed11bb1fSVladimir Oltean 1131ed11bb1fSVladimir Oltean return ds->ops->cls_flower_stats(ds, port, cls, ingress); 1132ed11bb1fSVladimir Oltean } 1133ed11bb1fSVladimir Oltean 1134ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev, 1135ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1136ed11bb1fSVladimir Oltean bool ingress) 1137ed11bb1fSVladimir Oltean { 1138ed11bb1fSVladimir Oltean switch (cls->command) { 1139ed11bb1fSVladimir Oltean case FLOW_CLS_REPLACE: 1140ed11bb1fSVladimir Oltean return dsa_slave_add_cls_flower(dev, cls, ingress); 1141ed11bb1fSVladimir Oltean case FLOW_CLS_DESTROY: 1142ed11bb1fSVladimir Oltean return dsa_slave_del_cls_flower(dev, cls, ingress); 1143ed11bb1fSVladimir Oltean case FLOW_CLS_STATS: 1144ed11bb1fSVladimir Oltean return dsa_slave_stats_cls_flower(dev, cls, ingress); 1145ed11bb1fSVladimir Oltean default: 1146ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1147ed11bb1fSVladimir Oltean } 1148ed11bb1fSVladimir Oltean } 1149ed11bb1fSVladimir Oltean 11506b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 11516b3eb752SJiri Pirko void *cb_priv, bool ingress) 11526b3eb752SJiri Pirko { 11536b3eb752SJiri Pirko struct net_device *dev = cb_priv; 11546b3eb752SJiri Pirko 115544ae12a7SJiri Pirko if (!tc_can_offload(dev)) 115644ae12a7SJiri Pirko return -EOPNOTSUPP; 115744ae12a7SJiri Pirko 11586b3eb752SJiri Pirko switch (type) { 11596b3eb752SJiri Pirko case TC_SETUP_CLSMATCHALL: 11606b3eb752SJiri Pirko return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 1161ed11bb1fSVladimir Oltean case TC_SETUP_CLSFLOWER: 1162ed11bb1fSVladimir Oltean return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress); 11636b3eb752SJiri Pirko default: 11646b3eb752SJiri Pirko return -EOPNOTSUPP; 11656b3eb752SJiri Pirko } 11666b3eb752SJiri Pirko } 11676b3eb752SJiri Pirko 11686b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 11696b3eb752SJiri Pirko void *type_data, void *cb_priv) 11706b3eb752SJiri Pirko { 11716b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 11726b3eb752SJiri Pirko } 11736b3eb752SJiri Pirko 11746b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 11756b3eb752SJiri Pirko void *type_data, void *cb_priv) 11766b3eb752SJiri Pirko { 11776b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 11786b3eb752SJiri Pirko } 11796b3eb752SJiri Pirko 1180955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list); 1181955bcb6eSPablo Neira Ayuso 11826b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev, 1183955bcb6eSPablo Neira Ayuso struct flow_block_offload *f) 11846b3eb752SJiri Pirko { 1185955bcb6eSPablo Neira Ayuso struct flow_block_cb *block_cb; 1186a7323311SPablo Neira Ayuso flow_setup_cb_t *cb; 11876b3eb752SJiri Pirko 118832f8c409SPablo Neira Ayuso if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 11896b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_ig; 119032f8c409SPablo Neira Ayuso else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 11916b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_eg; 11926b3eb752SJiri Pirko else 11936b3eb752SJiri Pirko return -EOPNOTSUPP; 11946b3eb752SJiri Pirko 1195955bcb6eSPablo Neira Ayuso f->driver_block_list = &dsa_slave_block_cb_list; 1196955bcb6eSPablo Neira Ayuso 11976b3eb752SJiri Pirko switch (f->command) { 11989c0e189eSPablo Neira Ayuso case FLOW_BLOCK_BIND: 11990d4fd02eSPablo Neira Ayuso if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list)) 12000d4fd02eSPablo Neira Ayuso return -EBUSY; 12010d4fd02eSPablo Neira Ayuso 12020c7294ddSPablo Neira Ayuso block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); 1203955bcb6eSPablo Neira Ayuso if (IS_ERR(block_cb)) 1204955bcb6eSPablo Neira Ayuso return PTR_ERR(block_cb); 1205955bcb6eSPablo Neira Ayuso 1206955bcb6eSPablo Neira Ayuso flow_block_cb_add(block_cb, f); 1207955bcb6eSPablo Neira Ayuso list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list); 1208955bcb6eSPablo Neira Ayuso return 0; 12099c0e189eSPablo Neira Ayuso case FLOW_BLOCK_UNBIND: 121014bfb13fSPablo Neira Ayuso block_cb = flow_block_cb_lookup(f->block, cb, dev); 1211955bcb6eSPablo Neira Ayuso if (!block_cb) 1212955bcb6eSPablo Neira Ayuso return -ENOENT; 1213955bcb6eSPablo Neira Ayuso 1214955bcb6eSPablo Neira Ayuso flow_block_cb_remove(block_cb, f); 1215955bcb6eSPablo Neira Ayuso list_del(&block_cb->driver_list); 12166b3eb752SJiri Pirko return 0; 12176b3eb752SJiri Pirko default: 12186b3eb752SJiri Pirko return -EOPNOTSUPP; 12196b3eb752SJiri Pirko } 12206b3eb752SJiri Pirko } 12216b3eb752SJiri Pirko 12223fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 1223de4784caSJiri Pirko void *type_data) 12243fbae382SJiri Pirko { 122547d23af2SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 122647d23af2SVladimir Oltean struct dsa_switch *ds = dp->ds; 122747d23af2SVladimir Oltean 122847d23af2SVladimir Oltean if (type == TC_SETUP_BLOCK) 12296b3eb752SJiri Pirko return dsa_slave_setup_tc_block(dev, type_data); 123047d23af2SVladimir Oltean 123147d23af2SVladimir Oltean if (!ds->ops->port_setup_tc) 1232a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 123347d23af2SVladimir Oltean 123447d23af2SVladimir Oltean return ds->ops->port_setup_tc(ds, dp->index, type, type_data); 1235f50f2127SFlorian Fainelli } 1236f50f2127SFlorian Fainelli 1237bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev, 1238bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs) 1239bf9f2648SFlorian Fainelli { 1240d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1241d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1242bf9f2648SFlorian Fainelli 1243bf9f2648SFlorian Fainelli if (!ds->ops->get_rxnfc) 1244bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1245bf9f2648SFlorian Fainelli 1246d945097bSVivien Didelot return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 1247bf9f2648SFlorian Fainelli } 1248bf9f2648SFlorian Fainelli 1249bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev, 1250bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc) 1251bf9f2648SFlorian Fainelli { 1252d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1253d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1254bf9f2648SFlorian Fainelli 1255bf9f2648SFlorian Fainelli if (!ds->ops->set_rxnfc) 1256bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1257bf9f2648SFlorian Fainelli 1258d945097bSVivien Didelot return ds->ops->set_rxnfc(ds, dp->index, nfc); 1259bf9f2648SFlorian Fainelli } 1260bf9f2648SFlorian Fainelli 12610336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev, 12620336369dSBrandon Streiff struct ethtool_ts_info *ts) 12630336369dSBrandon Streiff { 12640336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 12650336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 12660336369dSBrandon Streiff 12670336369dSBrandon Streiff if (!ds->ops->get_ts_info) 12680336369dSBrandon Streiff return -EOPNOTSUPP; 12690336369dSBrandon Streiff 12700336369dSBrandon Streiff return ds->ops->get_ts_info(ds, p->dp->index, ts); 12710336369dSBrandon Streiff } 12720336369dSBrandon Streiff 1273061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 1274061f6a50SFlorian Fainelli u16 vid) 1275061f6a50SFlorian Fainelli { 12762209158cSVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1277061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 127888236591SVladimir Oltean struct switchdev_obj_port_vlan vlan = { 127988236591SVladimir Oltean .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 1280b7a9e0daSVladimir Oltean .vid = vid, 128188236591SVladimir Oltean /* This API only allows programming tagged, non-PVID VIDs */ 128288236591SVladimir Oltean .flags = 0, 128388236591SVladimir Oltean }; 1284061f6a50SFlorian Fainelli int ret; 1285061f6a50SFlorian Fainelli 128688236591SVladimir Oltean /* User port... */ 1287ffb68fc5SVladimir Oltean ret = dsa_port_vlan_add(dp, &vlan); 128888236591SVladimir Oltean if (ret) 128988236591SVladimir Oltean return ret; 129088236591SVladimir Oltean 129188236591SVladimir Oltean /* And CPU port... */ 1292ffb68fc5SVladimir Oltean ret = dsa_port_vlan_add(dp->cpu_dp, &vlan); 12939b236d2aSVladimir Oltean if (ret) 12947e1741b4SVivien Didelot return ret; 12957e1741b4SVivien Didelot 12962209158cSVladimir Oltean return vlan_vid_add(master, proto, vid); 1297061f6a50SFlorian Fainelli } 1298061f6a50SFlorian Fainelli 1299061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 1300061f6a50SFlorian Fainelli u16 vid) 1301061f6a50SFlorian Fainelli { 13022209158cSVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1303061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 130488236591SVladimir Oltean struct switchdev_obj_port_vlan vlan = { 1305b7a9e0daSVladimir Oltean .vid = vid, 130688236591SVladimir Oltean /* This API only allows programming tagged, non-PVID VIDs */ 130788236591SVladimir Oltean .flags = 0, 130888236591SVladimir Oltean }; 13092209158cSVladimir Oltean int err; 1310061f6a50SFlorian Fainelli 13117e1741b4SVivien Didelot /* Do not deprogram the CPU port as it may be shared with other user 13127e1741b4SVivien Didelot * ports which can be members of this VLAN as well. 13137e1741b4SVivien Didelot */ 13142209158cSVladimir Oltean err = dsa_port_vlan_del(dp, &vlan); 13152209158cSVladimir Oltean if (err) 13162209158cSVladimir Oltean return err; 13172209158cSVladimir Oltean 13182209158cSVladimir Oltean vlan_vid_del(master, proto, vid); 13192209158cSVladimir Oltean 13202209158cSVladimir Oltean return 0; 1321061f6a50SFlorian Fainelli } 1322061f6a50SFlorian Fainelli 1323bff33f7eSVladimir Oltean struct dsa_hw_port { 1324bff33f7eSVladimir Oltean struct list_head list; 1325bff33f7eSVladimir Oltean struct net_device *dev; 1326bff33f7eSVladimir Oltean int old_mtu; 1327bff33f7eSVladimir Oltean }; 1328bff33f7eSVladimir Oltean 1329bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu) 1330bff33f7eSVladimir Oltean { 1331bff33f7eSVladimir Oltean const struct dsa_hw_port *p; 1332bff33f7eSVladimir Oltean int err; 1333bff33f7eSVladimir Oltean 1334bff33f7eSVladimir Oltean list_for_each_entry(p, hw_port_list, list) { 1335bff33f7eSVladimir Oltean if (p->dev->mtu == mtu) 1336bff33f7eSVladimir Oltean continue; 1337bff33f7eSVladimir Oltean 1338bff33f7eSVladimir Oltean err = dev_set_mtu(p->dev, mtu); 1339bff33f7eSVladimir Oltean if (err) 1340bff33f7eSVladimir Oltean goto rollback; 1341bff33f7eSVladimir Oltean } 1342bff33f7eSVladimir Oltean 1343bff33f7eSVladimir Oltean return 0; 1344bff33f7eSVladimir Oltean 1345bff33f7eSVladimir Oltean rollback: 1346bff33f7eSVladimir Oltean list_for_each_entry_continue_reverse(p, hw_port_list, list) { 1347bff33f7eSVladimir Oltean if (p->dev->mtu == p->old_mtu) 1348bff33f7eSVladimir Oltean continue; 1349bff33f7eSVladimir Oltean 1350bff33f7eSVladimir Oltean if (dev_set_mtu(p->dev, p->old_mtu)) 1351bff33f7eSVladimir Oltean netdev_err(p->dev, "Failed to restore MTU\n"); 1352bff33f7eSVladimir Oltean } 1353bff33f7eSVladimir Oltean 1354bff33f7eSVladimir Oltean return err; 1355bff33f7eSVladimir Oltean } 1356bff33f7eSVladimir Oltean 1357bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list) 1358bff33f7eSVladimir Oltean { 1359bff33f7eSVladimir Oltean struct dsa_hw_port *p, *n; 1360bff33f7eSVladimir Oltean 1361bff33f7eSVladimir Oltean list_for_each_entry_safe(p, n, hw_port_list, list) 1362bff33f7eSVladimir Oltean kfree(p); 1363bff33f7eSVladimir Oltean } 1364bff33f7eSVladimir Oltean 1365bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */ 1366bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp) 1367bff33f7eSVladimir Oltean { 1368bff33f7eSVladimir Oltean struct list_head hw_port_list; 1369bff33f7eSVladimir Oltean struct dsa_switch_tree *dst; 1370bff33f7eSVladimir Oltean int min_mtu = ETH_MAX_MTU; 1371bff33f7eSVladimir Oltean struct dsa_port *other_dp; 1372bff33f7eSVladimir Oltean int err; 1373bff33f7eSVladimir Oltean 1374bff33f7eSVladimir Oltean if (!dp->ds->mtu_enforcement_ingress) 1375bff33f7eSVladimir Oltean return; 1376bff33f7eSVladimir Oltean 1377bff33f7eSVladimir Oltean if (!dp->bridge_dev) 1378bff33f7eSVladimir Oltean return; 1379bff33f7eSVladimir Oltean 1380bff33f7eSVladimir Oltean INIT_LIST_HEAD(&hw_port_list); 1381bff33f7eSVladimir Oltean 1382bff33f7eSVladimir Oltean /* Populate the list of ports that are part of the same bridge 1383bff33f7eSVladimir Oltean * as the newly added/modified port 1384bff33f7eSVladimir Oltean */ 1385bff33f7eSVladimir Oltean list_for_each_entry(dst, &dsa_tree_list, list) { 1386bff33f7eSVladimir Oltean list_for_each_entry(other_dp, &dst->ports, list) { 1387bff33f7eSVladimir Oltean struct dsa_hw_port *hw_port; 1388bff33f7eSVladimir Oltean struct net_device *slave; 1389bff33f7eSVladimir Oltean 1390bff33f7eSVladimir Oltean if (other_dp->type != DSA_PORT_TYPE_USER) 1391bff33f7eSVladimir Oltean continue; 1392bff33f7eSVladimir Oltean 1393bff33f7eSVladimir Oltean if (other_dp->bridge_dev != dp->bridge_dev) 1394bff33f7eSVladimir Oltean continue; 1395bff33f7eSVladimir Oltean 1396bff33f7eSVladimir Oltean if (!other_dp->ds->mtu_enforcement_ingress) 1397bff33f7eSVladimir Oltean continue; 1398bff33f7eSVladimir Oltean 1399bff33f7eSVladimir Oltean slave = other_dp->slave; 1400bff33f7eSVladimir Oltean 1401bff33f7eSVladimir Oltean if (min_mtu > slave->mtu) 1402bff33f7eSVladimir Oltean min_mtu = slave->mtu; 1403bff33f7eSVladimir Oltean 1404bff33f7eSVladimir Oltean hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL); 1405bff33f7eSVladimir Oltean if (!hw_port) 1406bff33f7eSVladimir Oltean goto out; 1407bff33f7eSVladimir Oltean 1408bff33f7eSVladimir Oltean hw_port->dev = slave; 1409bff33f7eSVladimir Oltean hw_port->old_mtu = slave->mtu; 1410bff33f7eSVladimir Oltean 1411bff33f7eSVladimir Oltean list_add(&hw_port->list, &hw_port_list); 1412bff33f7eSVladimir Oltean } 1413bff33f7eSVladimir Oltean } 1414bff33f7eSVladimir Oltean 1415bff33f7eSVladimir Oltean /* Attempt to configure the entire hardware bridge to the newly added 1416bff33f7eSVladimir Oltean * interface's MTU first, regardless of whether the intention of the 1417bff33f7eSVladimir Oltean * user was to raise or lower it. 1418bff33f7eSVladimir Oltean */ 1419bff33f7eSVladimir Oltean err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu); 1420bff33f7eSVladimir Oltean if (!err) 1421bff33f7eSVladimir Oltean goto out; 1422bff33f7eSVladimir Oltean 1423bff33f7eSVladimir Oltean /* Clearly that didn't work out so well, so just set the minimum MTU on 1424bff33f7eSVladimir Oltean * all hardware bridge ports now. If this fails too, then all ports will 1425bff33f7eSVladimir Oltean * still have their old MTU rolled back anyway. 1426bff33f7eSVladimir Oltean */ 1427bff33f7eSVladimir Oltean dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu); 1428bff33f7eSVladimir Oltean 1429bff33f7eSVladimir Oltean out: 1430bff33f7eSVladimir Oltean dsa_hw_port_list_free(&hw_port_list); 1431bff33f7eSVladimir Oltean } 1432bff33f7eSVladimir Oltean 1433bfcb8132SVladimir Oltean static int dsa_slave_change_mtu(struct net_device *dev, int new_mtu) 1434bfcb8132SVladimir Oltean { 1435bfcb8132SVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1436bfcb8132SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1437bfcb8132SVladimir Oltean struct dsa_slave_priv *p = netdev_priv(dev); 1438bfcb8132SVladimir Oltean struct dsa_switch *ds = p->dp->ds; 1439bfcb8132SVladimir Oltean struct dsa_port *cpu_dp; 1440bfcb8132SVladimir Oltean int port = p->dp->index; 1441bfcb8132SVladimir Oltean int largest_mtu = 0; 1442bfcb8132SVladimir Oltean int new_master_mtu; 1443bfcb8132SVladimir Oltean int old_master_mtu; 1444bfcb8132SVladimir Oltean int mtu_limit; 1445bfcb8132SVladimir Oltean int cpu_mtu; 1446bfcb8132SVladimir Oltean int err, i; 1447bfcb8132SVladimir Oltean 1448bfcb8132SVladimir Oltean if (!ds->ops->port_change_mtu) 1449bfcb8132SVladimir Oltean return -EOPNOTSUPP; 1450bfcb8132SVladimir Oltean 1451bfcb8132SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 1452bfcb8132SVladimir Oltean int slave_mtu; 1453bfcb8132SVladimir Oltean 1454bfcb8132SVladimir Oltean if (!dsa_is_user_port(ds, i)) 1455bfcb8132SVladimir Oltean continue; 1456bfcb8132SVladimir Oltean 1457bfcb8132SVladimir Oltean /* During probe, this function will be called for each slave 1458bfcb8132SVladimir Oltean * device, while not all of them have been allocated. That's 1459bfcb8132SVladimir Oltean * ok, it doesn't change what the maximum is, so ignore it. 1460bfcb8132SVladimir Oltean */ 1461bfcb8132SVladimir Oltean if (!dsa_to_port(ds, i)->slave) 1462bfcb8132SVladimir Oltean continue; 1463bfcb8132SVladimir Oltean 1464bfcb8132SVladimir Oltean /* Pretend that we already applied the setting, which we 1465bfcb8132SVladimir Oltean * actually haven't (still haven't done all integrity checks) 1466bfcb8132SVladimir Oltean */ 1467bfcb8132SVladimir Oltean if (i == port) 1468bfcb8132SVladimir Oltean slave_mtu = new_mtu; 1469bfcb8132SVladimir Oltean else 1470bfcb8132SVladimir Oltean slave_mtu = dsa_to_port(ds, i)->slave->mtu; 1471bfcb8132SVladimir Oltean 1472bfcb8132SVladimir Oltean if (largest_mtu < slave_mtu) 1473bfcb8132SVladimir Oltean largest_mtu = slave_mtu; 1474bfcb8132SVladimir Oltean } 1475bfcb8132SVladimir Oltean 1476bfcb8132SVladimir Oltean cpu_dp = dsa_to_port(ds, port)->cpu_dp; 1477bfcb8132SVladimir Oltean 1478bfcb8132SVladimir Oltean mtu_limit = min_t(int, master->max_mtu, dev->max_mtu); 1479bfcb8132SVladimir Oltean old_master_mtu = master->mtu; 1480bfcb8132SVladimir Oltean new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead; 1481bfcb8132SVladimir Oltean if (new_master_mtu > mtu_limit) 1482bfcb8132SVladimir Oltean return -ERANGE; 1483bfcb8132SVladimir Oltean 1484bfcb8132SVladimir Oltean /* If the master MTU isn't over limit, there's no need to check the CPU 1485bfcb8132SVladimir Oltean * MTU, since that surely isn't either. 1486bfcb8132SVladimir Oltean */ 1487bfcb8132SVladimir Oltean cpu_mtu = largest_mtu; 1488bfcb8132SVladimir Oltean 1489bfcb8132SVladimir Oltean /* Start applying stuff */ 1490bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) { 1491bfcb8132SVladimir Oltean err = dev_set_mtu(master, new_master_mtu); 1492bfcb8132SVladimir Oltean if (err < 0) 1493bfcb8132SVladimir Oltean goto out_master_failed; 1494bfcb8132SVladimir Oltean 1495bfcb8132SVladimir Oltean /* We only need to propagate the MTU of the CPU port to 1496bfcb8132SVladimir Oltean * upstream switches. 1497bfcb8132SVladimir Oltean */ 1498bfcb8132SVladimir Oltean err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true); 1499bfcb8132SVladimir Oltean if (err) 1500bfcb8132SVladimir Oltean goto out_cpu_failed; 1501bfcb8132SVladimir Oltean } 1502bfcb8132SVladimir Oltean 1503bfcb8132SVladimir Oltean err = dsa_port_mtu_change(dp, new_mtu, false); 1504bfcb8132SVladimir Oltean if (err) 1505bfcb8132SVladimir Oltean goto out_port_failed; 1506bfcb8132SVladimir Oltean 1507bfcb8132SVladimir Oltean dev->mtu = new_mtu; 1508bfcb8132SVladimir Oltean 1509bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 1510bff33f7eSVladimir Oltean 1511bfcb8132SVladimir Oltean return 0; 1512bfcb8132SVladimir Oltean 1513bfcb8132SVladimir Oltean out_port_failed: 1514bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1515bfcb8132SVladimir Oltean dsa_port_mtu_change(cpu_dp, old_master_mtu - 1516bfcb8132SVladimir Oltean cpu_dp->tag_ops->overhead, 1517bfcb8132SVladimir Oltean true); 1518bfcb8132SVladimir Oltean out_cpu_failed: 1519bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1520bfcb8132SVladimir Oltean dev_set_mtu(master, old_master_mtu); 1521bfcb8132SVladimir Oltean out_master_failed: 1522bfcb8132SVladimir Oltean return err; 1523bfcb8132SVladimir Oltean } 1524bfcb8132SVladimir Oltean 152591da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 152691da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 15273d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 15283d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 1529aab9c406SFlorian Fainelli .nway_reset = dsa_slave_nway_reset, 1530c4aef9fcSFlorian Fainelli .get_link = ethtool_op_get_link, 15316793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 15326793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 15336793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 153491da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 153591da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 153691da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 153719e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 153819e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 15397905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 15407905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 1541aab9c406SFlorian Fainelli .get_link_ksettings = dsa_slave_get_link_ksettings, 1542aab9c406SFlorian Fainelli .set_link_ksettings = dsa_slave_set_link_ksettings, 1543a2a1a13bSHeiner Kallweit .get_pauseparam = dsa_slave_get_pauseparam, 1544a2a1a13bSHeiner Kallweit .set_pauseparam = dsa_slave_set_pauseparam, 1545bf9f2648SFlorian Fainelli .get_rxnfc = dsa_slave_get_rxnfc, 1546bf9f2648SFlorian Fainelli .set_rxnfc = dsa_slave_set_rxnfc, 15470336369dSBrandon Streiff .get_ts_info = dsa_slave_get_ts_info, 154891da11f8SLennert Buytenhek }; 154991da11f8SLennert Buytenhek 15502a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/ 15514b9c9358SVladimir Oltean static int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 15522a93c1a3SFlorian Fainelli struct net_device *dev, 15532a93c1a3SFlorian Fainelli const unsigned char *addr, u16 vid, 155487b0984eSPetr Machata u16 flags, 155587b0984eSPetr Machata struct netlink_ext_ack *extack) 15562a93c1a3SFlorian Fainelli { 15572a93c1a3SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 15582a93c1a3SFlorian Fainelli 15592a93c1a3SFlorian Fainelli return dsa_port_fdb_add(dp, addr, vid); 15602a93c1a3SFlorian Fainelli } 15612a93c1a3SFlorian Fainelli 15624b9c9358SVladimir Oltean static int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 15632a93c1a3SFlorian Fainelli struct net_device *dev, 15642a93c1a3SFlorian Fainelli const unsigned char *addr, u16 vid) 15652a93c1a3SFlorian Fainelli { 15662a93c1a3SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 15672a93c1a3SFlorian Fainelli 15682a93c1a3SFlorian Fainelli return dsa_port_fdb_del(dp, addr, vid); 15692a93c1a3SFlorian Fainelli } 15702a93c1a3SFlorian Fainelli 1571716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev) 1572716efee2SJiri Pirko { 1573716efee2SJiri Pirko struct dsa_port *dp = dsa_slave_to_port(dev); 1574716efee2SJiri Pirko 1575716efee2SJiri Pirko return dp->ds->devlink ? &dp->devlink_port : NULL; 1576716efee2SJiri Pirko } 1577716efee2SJiri Pirko 1578c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev, 1579c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s) 1580c2ec5f2eSOleksij Rempel { 1581c2ec5f2eSOleksij Rempel struct dsa_port *dp = dsa_slave_to_port(dev); 1582c2ec5f2eSOleksij Rempel struct dsa_switch *ds = dp->ds; 1583c2ec5f2eSOleksij Rempel 1584c2ec5f2eSOleksij Rempel if (ds->ops->get_stats64) 1585c2ec5f2eSOleksij Rempel ds->ops->get_stats64(ds, dp->index, s); 1586c2ec5f2eSOleksij Rempel else 1587c2ec5f2eSOleksij Rempel dev_get_tstats64(dev, s); 1588c2ec5f2eSOleksij Rempel } 1589c2ec5f2eSOleksij Rempel 15903e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 1591d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 1592d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 15933e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 1594d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 1595d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 1596d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 159737b8da1aSArkadi Sharshevsky .ndo_fdb_add = dsa_legacy_fdb_add, 159837b8da1aSArkadi Sharshevsky .ndo_fdb_del = dsa_legacy_fdb_del, 15992bedde1aSArkadi Sharshevsky .ndo_fdb_dump = dsa_slave_fdb_dump, 1600d442ad4aSStephen Hemminger .ndo_do_ioctl = dsa_slave_ioctl, 1601abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 160204ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 160304ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 160404ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 160504ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 160604ff53f9SFlorian Fainelli #endif 160744bb765cSFlorian Fainelli .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, 1608f50f2127SFlorian Fainelli .ndo_setup_tc = dsa_slave_setup_tc, 1609c2ec5f2eSOleksij Rempel .ndo_get_stats64 = dsa_slave_get_stats64, 1610929d6c14SFlorian Fainelli .ndo_get_port_parent_id = dsa_slave_get_port_parent_id, 1611061f6a50SFlorian Fainelli .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid, 1612061f6a50SFlorian Fainelli .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid, 1613716efee2SJiri Pirko .ndo_get_devlink_port = dsa_slave_get_devlink_port, 1614bfcb8132SVladimir Oltean .ndo_change_mtu = dsa_slave_change_mtu, 161598237d43SScott Feldman }; 161698237d43SScott Feldman 1617f37db85dSFlorian Fainelli static struct device_type dsa_type = { 1618f37db85dSFlorian Fainelli .name = "dsa", 1619f37db85dSFlorian Fainelli }; 1620f37db85dSFlorian Fainelli 1621aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 1622aab9c406SFlorian Fainelli { 1623aab9c406SFlorian Fainelli const struct dsa_port *dp = dsa_to_port(ds, port); 1624aab9c406SFlorian Fainelli 1625765bda93SRussell King if (dp->pl) 1626aab9c406SFlorian Fainelli phylink_mac_change(dp->pl, up); 1627aab9c406SFlorian Fainelli } 1628aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change); 1629aab9c406SFlorian Fainelli 16305c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config, 1631aab9c406SFlorian Fainelli struct phylink_link_state *state) 1632aab9c406SFlorian Fainelli { 16335c05c1dbSRussell King struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1634aab9c406SFlorian Fainelli struct dsa_switch *ds = dp->ds; 1635aab9c406SFlorian Fainelli 1636aab9c406SFlorian Fainelli /* No need to check that this operation is valid, the callback would 1637aab9c406SFlorian Fainelli * not be called if it was not. 1638aab9c406SFlorian Fainelli */ 1639aab9c406SFlorian Fainelli ds->ops->phylink_fixed_state(ds, dp->index, state); 1640ce31b31cSFlorian Fainelli } 1641ce31b31cSFlorian Fainelli 164291da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 16434fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr) 1644c305c165SFlorian Fainelli { 1645d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1646d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1647c305c165SFlorian Fainelli 16480115dcd1SVivien Didelot slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 16490115dcd1SVivien Didelot if (!slave_dev->phydev) { 1650d25b8e74SRussell King netdev_err(slave_dev, "no phy at %d\n", addr); 1651c305c165SFlorian Fainelli return -ENODEV; 1652d25b8e74SRussell King } 1653c305c165SFlorian Fainelli 1654aab9c406SFlorian Fainelli return phylink_connect_phy(dp->pl, slave_dev->phydev); 1655c305c165SFlorian Fainelli } 1656c305c165SFlorian Fainelli 16574fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev) 16580d8bcdd3SFlorian Fainelli { 1659d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1660d945097bSVivien Didelot struct device_node *port_dn = dp->dn; 1661d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 16620c65b2b9SAndrew Lunn phy_interface_t mode; 16636819563eSFlorian Fainelli u32 phy_flags = 0; 16640c65b2b9SAndrew Lunn int ret; 16650d8bcdd3SFlorian Fainelli 16660c65b2b9SAndrew Lunn ret = of_get_phy_mode(port_dn, &mode); 16670c65b2b9SAndrew Lunn if (ret) 166819334920SGuenter Roeck mode = PHY_INTERFACE_MODE_NA; 16690d8bcdd3SFlorian Fainelli 167044cc27e4SIoana Ciornei dp->pl_config.dev = &slave_dev->dev; 167144cc27e4SIoana Ciornei dp->pl_config.type = PHYLINK_NETDEV; 167244cc27e4SIoana Ciornei 16735c05c1dbSRussell King /* The get_fixed_state callback takes precedence over polling the 16745c05c1dbSRussell King * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set 16755c05c1dbSRussell King * this if the switch provides such a callback. 16765c05c1dbSRussell King */ 16775c05c1dbSRussell King if (ds->ops->phylink_fixed_state) { 16785c05c1dbSRussell King dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state; 16795c05c1dbSRussell King dp->pl_config.poll_fixed_state = true; 16805c05c1dbSRussell King } 16815c05c1dbSRussell King 168244cc27e4SIoana Ciornei dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode, 168377373d49SIoana Ciornei &dsa_port_phylink_mac_ops); 1684aab9c406SFlorian Fainelli if (IS_ERR(dp->pl)) { 1685aab9c406SFlorian Fainelli netdev_err(slave_dev, 1686aab9c406SFlorian Fainelli "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); 1687aab9c406SFlorian Fainelli return PTR_ERR(dp->pl); 1688aab9c406SFlorian Fainelli } 1689aab9c406SFlorian Fainelli 16909d490b4eSVivien Didelot if (ds->ops->get_phy_flags) 1691d945097bSVivien Didelot phy_flags = ds->ops->get_phy_flags(ds, dp->index); 16926819563eSFlorian Fainelli 1693aab9c406SFlorian Fainelli ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags); 16946146dd45SVladimir Oltean if (ret == -ENODEV && ds->slave_mii_bus) { 16956146dd45SVladimir Oltean /* We could not connect to a designated PHY or SFP, so try to 16966146dd45SVladimir Oltean * use the switch internal MDIO bus instead 16970d8bcdd3SFlorian Fainelli */ 1698d945097bSVivien Didelot ret = dsa_slave_phy_connect(slave_dev, dp->index); 1699d25b8e74SRussell King if (ret) { 1700aab9c406SFlorian Fainelli netdev_err(slave_dev, 1701aab9c406SFlorian Fainelli "failed to connect to port %d: %d\n", 1702d945097bSVivien Didelot dp->index, ret); 1703aab9c406SFlorian Fainelli phylink_destroy(dp->pl); 1704c305c165SFlorian Fainelli return ret; 1705d25b8e74SRussell King } 17060d8bcdd3SFlorian Fainelli } 17079697f1cdSFlorian Fainelli 17086146dd45SVladimir Oltean return ret; 1709b31f65fbSAndrew Lunn } 17100d8bcdd3SFlorian Fainelli 17111a33e10eSCong Wang static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 17121a33e10eSCong Wang static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 17131a33e10eSCong Wang struct netdev_queue *txq, 17141a33e10eSCong Wang void *_unused) 17151a33e10eSCong Wang { 17161a33e10eSCong Wang lockdep_set_class(&txq->_xmit_lock, 17171a33e10eSCong Wang &dsa_slave_netdev_xmit_lock_key); 17181a33e10eSCong Wang } 17191a33e10eSCong Wang 172024462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 172124462549SFlorian Fainelli { 1722aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 172324462549SFlorian Fainelli 1724a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 1725a94c689eSFlorian Fainelli return 0; 1726a94c689eSFlorian Fainelli 1727f154be24SFlorian Fainelli netif_device_detach(slave_dev); 1728f154be24SFlorian Fainelli 1729aab9c406SFlorian Fainelli rtnl_lock(); 1730aab9c406SFlorian Fainelli phylink_stop(dp->pl); 1731aab9c406SFlorian Fainelli rtnl_unlock(); 173224462549SFlorian Fainelli 173324462549SFlorian Fainelli return 0; 173424462549SFlorian Fainelli } 173524462549SFlorian Fainelli 173624462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 173724462549SFlorian Fainelli { 1738aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1739aab9c406SFlorian Fainelli 1740a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 1741a94c689eSFlorian Fainelli return 0; 1742a94c689eSFlorian Fainelli 174324462549SFlorian Fainelli netif_device_attach(slave_dev); 174424462549SFlorian Fainelli 1745aab9c406SFlorian Fainelli rtnl_lock(); 1746aab9c406SFlorian Fainelli phylink_start(dp->pl); 1747aab9c406SFlorian Fainelli rtnl_unlock(); 174824462549SFlorian Fainelli 174924462549SFlorian Fainelli return 0; 175024462549SFlorian Fainelli } 175124462549SFlorian Fainelli 1752951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port) 175391da11f8SLennert Buytenhek { 175424a9332aSVivien Didelot const struct dsa_port *cpu_dp = port->cpu_dp; 1755f8b8b1cdSVivien Didelot struct net_device *master = cpu_dp->master; 17564cfbf09cSVivien Didelot struct dsa_switch *ds = port->ds; 1757951259aaSVivien Didelot const char *name = port->name; 175891da11f8SLennert Buytenhek struct net_device *slave_dev; 175991da11f8SLennert Buytenhek struct dsa_slave_priv *p; 176091da11f8SLennert Buytenhek int ret; 176191da11f8SLennert Buytenhek 176255199df6SFlorian Fainelli if (!ds->num_tx_queues) 176355199df6SFlorian Fainelli ds->num_tx_queues = 1; 176455199df6SFlorian Fainelli 176555199df6SFlorian Fainelli slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 176655199df6SFlorian Fainelli NET_NAME_UNKNOWN, ether_setup, 176755199df6SFlorian Fainelli ds->num_tx_queues, 1); 176891da11f8SLennert Buytenhek if (slave_dev == NULL) 1769d87d6f44SGuenter Roeck return -ENOMEM; 177091da11f8SLennert Buytenhek 17719b236d2aSVladimir Oltean slave_dev->features = master->vlan_features | NETIF_F_HW_TC; 17729b236d2aSVladimir Oltean if (ds->ops->port_vlan_add && ds->ops->port_vlan_del) 17739b236d2aSVladimir Oltean slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1774f50f2127SFlorian Fainelli slave_dev->hw_features |= NETIF_F_HW_TC; 17752b86cb82SVladimir Oltean slave_dev->features |= NETIF_F_LLTX; 17767ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 17774974f9b7SPetr Štetiar if (!IS_ERR_OR_NULL(port->mac)) 1778a2c7023fSXiaofei Shen ether_addr_copy(slave_dev->dev_addr, port->mac); 1779a2c7023fSXiaofei Shen else 17802fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 17810a5f107bSPhil Sutter slave_dev->priv_flags |= IFF_NO_QUEUE; 17823e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 1783bfcb8132SVladimir Oltean if (ds->ops->port_max_mtu) 1784bfcb8132SVladimir Oltean slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index); 1785a3b0b647SVladimir Oltean if (cpu_dp->tag_ops->tail_tag) 1786a3b0b647SVladimir Oltean slave_dev->needed_tailroom = cpu_dp->tag_ops->overhead; 1787a3b0b647SVladimir Oltean else 1788a3b0b647SVladimir Oltean slave_dev->needed_headroom = cpu_dp->tag_ops->overhead; 1789a3b0b647SVladimir Oltean /* Try to save one extra realloc later in the TX path (in the master) 1790a3b0b647SVladimir Oltean * by also inheriting the master's needed headroom and tailroom. 1791a3b0b647SVladimir Oltean * The 8021q driver also does this. 1792a3b0b647SVladimir Oltean */ 1793a3b0b647SVladimir Oltean slave_dev->needed_headroom += master->needed_headroom; 1794a3b0b647SVladimir Oltean slave_dev->needed_tailroom += master->needed_tailroom; 1795f37db85dSFlorian Fainelli SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 1796d442ad4aSStephen Hemminger 17971a33e10eSCong Wang netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 17981a33e10eSCong Wang NULL); 17991a33e10eSCong Wang 18004cfbf09cSVivien Didelot SET_NETDEV_DEV(slave_dev, port->ds->dev); 18014cfbf09cSVivien Didelot slave_dev->dev.of_node = port->dn; 180291da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 180391da11f8SLennert Buytenhek 180491da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 18056a900628SHeiner Kallweit slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 18066a900628SHeiner Kallweit if (!slave_dev->tstats) { 18075f6b4e14SFlorian Fainelli free_netdev(slave_dev); 18085f6b4e14SFlorian Fainelli return -ENOMEM; 18095f6b4e14SFlorian Fainelli } 1810e131a563SAlexander Lobakin 1811e131a563SAlexander Lobakin ret = gro_cells_init(&p->gcells, slave_dev); 1812e131a563SAlexander Lobakin if (ret) 1813e131a563SAlexander Lobakin goto out_free; 1814e131a563SAlexander Lobakin 18154cfbf09cSVivien Didelot p->dp = port; 1816f50f2127SFlorian Fainelli INIT_LIST_HEAD(&p->mall_tc_list); 181715240248SVivien Didelot p->xmit = cpu_dp->tag_ops->xmit; 1818f8b8b1cdSVivien Didelot port->slave = slave_dev; 181991da11f8SLennert Buytenhek 1820bfcb8132SVladimir Oltean rtnl_lock(); 1821bfcb8132SVladimir Oltean ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN); 1822bfcb8132SVladimir Oltean rtnl_unlock(); 18234349abdbSVladimir Oltean if (ret && ret != -EOPNOTSUPP) 1824bdc40a3fSRasmus Villemoes dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n", 1825bdc40a3fSRasmus Villemoes ret, ETH_DATA_LEN, port->index); 1826bfcb8132SVladimir Oltean 182791da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 182891da11f8SLennert Buytenhek 18294fa7b718SVivien Didelot ret = dsa_slave_phy_setup(slave_dev); 18300071f56eSAndrew Lunn if (ret) { 1831c9ebf126SVladimir Oltean netdev_err(slave_dev, 1832c9ebf126SVladimir Oltean "error %d setting up PHY for tree %d, switch %d, port %d\n", 1833c9ebf126SVladimir Oltean ret, ds->dst->index, ds->index, port->index); 1834e131a563SAlexander Lobakin goto out_gcells; 1835e804441cSFlorian Fainelli } 1836e804441cSFlorian Fainelli 18372f1e8ea7SVladimir Oltean rtnl_lock(); 18382f1e8ea7SVladimir Oltean 18392f1e8ea7SVladimir Oltean ret = register_netdevice(slave_dev); 1840e804441cSFlorian Fainelli if (ret) { 1841e804441cSFlorian Fainelli netdev_err(master, "error %d registering interface %s\n", 1842e804441cSFlorian Fainelli ret, slave_dev->name); 18432f1e8ea7SVladimir Oltean rtnl_unlock(); 1844e804441cSFlorian Fainelli goto out_phy; 18450071f56eSAndrew Lunn } 18460071f56eSAndrew Lunn 18472f1e8ea7SVladimir Oltean ret = netdev_upper_dev_link(master, slave_dev, NULL); 18482f1e8ea7SVladimir Oltean 18492f1e8ea7SVladimir Oltean rtnl_unlock(); 18502f1e8ea7SVladimir Oltean 18512f1e8ea7SVladimir Oltean if (ret) 18522f1e8ea7SVladimir Oltean goto out_unregister; 18532f1e8ea7SVladimir Oltean 1854d87d6f44SGuenter Roeck return 0; 1855e804441cSFlorian Fainelli 18562f1e8ea7SVladimir Oltean out_unregister: 18572f1e8ea7SVladimir Oltean unregister_netdev(slave_dev); 1858e804441cSFlorian Fainelli out_phy: 1859aab9c406SFlorian Fainelli rtnl_lock(); 1860aab9c406SFlorian Fainelli phylink_disconnect_phy(p->dp->pl); 1861aab9c406SFlorian Fainelli rtnl_unlock(); 1862aab9c406SFlorian Fainelli phylink_destroy(p->dp->pl); 1863e131a563SAlexander Lobakin out_gcells: 1864e131a563SAlexander Lobakin gro_cells_destroy(&p->gcells); 1865e804441cSFlorian Fainelli out_free: 18666a900628SHeiner Kallweit free_percpu(slave_dev->tstats); 1867e804441cSFlorian Fainelli free_netdev(slave_dev); 1868f8b8b1cdSVivien Didelot port->slave = NULL; 1869e804441cSFlorian Fainelli return ret; 187091da11f8SLennert Buytenhek } 1871b73adef6SFlorian Fainelli 1872cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev) 1873cda5c15bSNeil Armstrong { 18742f1e8ea7SVladimir Oltean struct net_device *master = dsa_slave_to_master(slave_dev); 1875d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1876cda5c15bSNeil Armstrong struct dsa_slave_priv *p = netdev_priv(slave_dev); 1877cda5c15bSNeil Armstrong 1878cda5c15bSNeil Armstrong netif_carrier_off(slave_dev); 1879aab9c406SFlorian Fainelli rtnl_lock(); 18802f1e8ea7SVladimir Oltean netdev_upper_dev_unlink(master, slave_dev); 18812f1e8ea7SVladimir Oltean unregister_netdevice(slave_dev); 1882aab9c406SFlorian Fainelli phylink_disconnect_phy(dp->pl); 1883aab9c406SFlorian Fainelli rtnl_unlock(); 1884881eadabSJohan Hovold 1885aab9c406SFlorian Fainelli phylink_destroy(dp->pl); 1886e131a563SAlexander Lobakin gro_cells_destroy(&p->gcells); 18876a900628SHeiner Kallweit free_percpu(slave_dev->tstats); 1888cda5c15bSNeil Armstrong free_netdev(slave_dev); 1889cda5c15bSNeil Armstrong } 1890cda5c15bSNeil Armstrong 18914d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev) 1892b73adef6SFlorian Fainelli { 1893b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 1894b73adef6SFlorian Fainelli } 1895a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check); 1896b73adef6SFlorian Fainelli 18978e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev, 18988e92ab3aSVivien Didelot struct netdev_notifier_changeupper_info *info) 1899b73adef6SFlorian Fainelli { 1900d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 19018e92ab3aSVivien Didelot int err = NOTIFY_DONE; 1902b73adef6SFlorian Fainelli 19038e92ab3aSVivien Didelot if (netif_is_bridge_master(info->upper_dev)) { 19048e92ab3aSVivien Didelot if (info->linking) { 190517d7802bSVivien Didelot err = dsa_port_bridge_join(dp, info->upper_dev); 1906bff33f7eSVladimir Oltean if (!err) 1907bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 19088e92ab3aSVivien Didelot err = notifier_from_errno(err); 19098e92ab3aSVivien Didelot } else { 191017d7802bSVivien Didelot dsa_port_bridge_leave(dp, info->upper_dev); 19118e92ab3aSVivien Didelot err = NOTIFY_OK; 19128e92ab3aSVivien Didelot } 1913058102a6STobias Waldekranz } else if (netif_is_lag_master(info->upper_dev)) { 1914058102a6STobias Waldekranz if (info->linking) { 1915058102a6STobias Waldekranz err = dsa_port_lag_join(dp, info->upper_dev, 1916058102a6STobias Waldekranz info->upper_info); 1917058102a6STobias Waldekranz if (err == -EOPNOTSUPP) { 1918058102a6STobias Waldekranz NL_SET_ERR_MSG_MOD(info->info.extack, 1919058102a6STobias Waldekranz "Offloading not supported"); 1920058102a6STobias Waldekranz err = 0; 1921058102a6STobias Waldekranz } 1922058102a6STobias Waldekranz err = notifier_from_errno(err); 1923058102a6STobias Waldekranz } else { 1924058102a6STobias Waldekranz dsa_port_lag_leave(dp, info->upper_dev); 1925058102a6STobias Waldekranz err = NOTIFY_OK; 1926058102a6STobias Waldekranz } 1927058102a6STobias Waldekranz } 1928058102a6STobias Waldekranz 1929058102a6STobias Waldekranz return err; 1930058102a6STobias Waldekranz } 1931058102a6STobias Waldekranz 1932058102a6STobias Waldekranz static int 1933058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev, 1934058102a6STobias Waldekranz struct netdev_notifier_changeupper_info *info) 1935058102a6STobias Waldekranz { 1936058102a6STobias Waldekranz struct net_device *lower; 1937058102a6STobias Waldekranz struct list_head *iter; 1938058102a6STobias Waldekranz int err = NOTIFY_DONE; 1939058102a6STobias Waldekranz struct dsa_port *dp; 1940058102a6STobias Waldekranz 1941058102a6STobias Waldekranz netdev_for_each_lower_dev(dev, lower, iter) { 1942058102a6STobias Waldekranz if (!dsa_slave_dev_check(lower)) 1943058102a6STobias Waldekranz continue; 1944058102a6STobias Waldekranz 1945058102a6STobias Waldekranz dp = dsa_slave_to_port(lower); 1946058102a6STobias Waldekranz if (!dp->lag_dev) 1947058102a6STobias Waldekranz /* Software LAG */ 1948058102a6STobias Waldekranz continue; 1949058102a6STobias Waldekranz 1950058102a6STobias Waldekranz err = dsa_slave_changeupper(lower, info); 1951058102a6STobias Waldekranz if (notifier_to_errno(err)) 1952058102a6STobias Waldekranz break; 19536debb68aSVivien Didelot } 1954b73adef6SFlorian Fainelli 19558e92ab3aSVivien Didelot return err; 1956b73adef6SFlorian Fainelli } 1957b73adef6SFlorian Fainelli 1958eb46e8daSVladimir Oltean static int 1959eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev, 1960eb46e8daSVladimir Oltean struct netdev_notifier_changeupper_info *info) 1961cc1d5bdaSFlorian Fainelli { 1962cc1d5bdaSFlorian Fainelli struct netlink_ext_ack *ext_ack; 1963cc1d5bdaSFlorian Fainelli struct net_device *slave; 1964cc1d5bdaSFlorian Fainelli struct dsa_port *dp; 1965cc1d5bdaSFlorian Fainelli 1966cc1d5bdaSFlorian Fainelli ext_ack = netdev_notifier_info_to_extack(&info->info); 1967cc1d5bdaSFlorian Fainelli 1968cc1d5bdaSFlorian Fainelli if (!is_vlan_dev(dev)) 1969cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1970cc1d5bdaSFlorian Fainelli 1971cc1d5bdaSFlorian Fainelli slave = vlan_dev_real_dev(dev); 1972cc1d5bdaSFlorian Fainelli if (!dsa_slave_dev_check(slave)) 1973cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1974cc1d5bdaSFlorian Fainelli 1975cc1d5bdaSFlorian Fainelli dp = dsa_slave_to_port(slave); 1976cc1d5bdaSFlorian Fainelli if (!dp->bridge_dev) 1977cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1978cc1d5bdaSFlorian Fainelli 1979cc1d5bdaSFlorian Fainelli /* Deny enslaving a VLAN device into a VLAN-aware bridge */ 1980cc1d5bdaSFlorian Fainelli if (br_vlan_enabled(dp->bridge_dev) && 1981cc1d5bdaSFlorian Fainelli netif_is_bridge_master(info->upper_dev) && info->linking) { 1982cc1d5bdaSFlorian Fainelli NL_SET_ERR_MSG_MOD(ext_ack, 1983cc1d5bdaSFlorian Fainelli "Cannot enslave VLAN device into VLAN aware bridge"); 1984cc1d5bdaSFlorian Fainelli return notifier_from_errno(-EINVAL); 1985cc1d5bdaSFlorian Fainelli } 1986cc1d5bdaSFlorian Fainelli 1987cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1988cc1d5bdaSFlorian Fainelli } 1989cc1d5bdaSFlorian Fainelli 19902b138406SVladimir Oltean static int 19912b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev, 19922b138406SVladimir Oltean struct netdev_notifier_changeupper_info *info) 19932b138406SVladimir Oltean { 19942b138406SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 19952b138406SVladimir Oltean struct net_device *br = dp->bridge_dev; 19962b138406SVladimir Oltean struct bridge_vlan_info br_info; 19972b138406SVladimir Oltean struct netlink_ext_ack *extack; 19982b138406SVladimir Oltean int err = NOTIFY_DONE; 19992b138406SVladimir Oltean u16 vid; 20002b138406SVladimir Oltean 2001adb256ebSVladimir Oltean if (!br || !br_vlan_enabled(br)) 20022b138406SVladimir Oltean return NOTIFY_DONE; 20032b138406SVladimir Oltean 20042b138406SVladimir Oltean extack = netdev_notifier_info_to_extack(&info->info); 20052b138406SVladimir Oltean vid = vlan_dev_vlan_id(info->upper_dev); 20062b138406SVladimir Oltean 20072b138406SVladimir Oltean /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 20082b138406SVladimir Oltean * device, respectively the VID is not found, returning 20092b138406SVladimir Oltean * 0 means success, which is a failure for us here. 20102b138406SVladimir Oltean */ 20112b138406SVladimir Oltean err = br_vlan_get_info(br, vid, &br_info); 20122b138406SVladimir Oltean if (err == 0) { 20132b138406SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 20142b138406SVladimir Oltean "This VLAN is already configured by the bridge"); 20152b138406SVladimir Oltean return notifier_from_errno(-EBUSY); 20162b138406SVladimir Oltean } 20172b138406SVladimir Oltean 20182b138406SVladimir Oltean return NOTIFY_DONE; 20192b138406SVladimir Oltean } 20202b138406SVladimir Oltean 202188e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb, 2022b73adef6SFlorian Fainelli unsigned long event, void *ptr) 2023b73adef6SFlorian Fainelli { 20246debb68aSVivien Didelot struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2025b73adef6SFlorian Fainelli 202683501299SVladimir Oltean switch (event) { 20272b138406SVladimir Oltean case NETDEV_PRECHANGEUPPER: { 20282b138406SVladimir Oltean struct netdev_notifier_changeupper_info *info = ptr; 2029e358bef7SVladimir Oltean struct dsa_switch *ds; 2030e358bef7SVladimir Oltean struct dsa_port *dp; 2031e358bef7SVladimir Oltean int err; 20322b138406SVladimir Oltean 203353bade8aSFlorian Fainelli if (!dsa_slave_dev_check(dev)) 2034eb46e8daSVladimir Oltean return dsa_prevent_bridging_8021q_upper(dev, ptr); 20352b138406SVladimir Oltean 2036e358bef7SVladimir Oltean dp = dsa_slave_to_port(dev); 2037e358bef7SVladimir Oltean ds = dp->ds; 2038e358bef7SVladimir Oltean 2039e358bef7SVladimir Oltean if (ds->ops->port_prechangeupper) { 2040e358bef7SVladimir Oltean err = ds->ops->port_prechangeupper(ds, dp->index, info); 2041e358bef7SVladimir Oltean if (err) 2042e358bef7SVladimir Oltean return notifier_from_errno(err); 2043e358bef7SVladimir Oltean } 2044e358bef7SVladimir Oltean 20452b138406SVladimir Oltean if (is_vlan_dev(info->upper_dev)) 20462b138406SVladimir Oltean return dsa_slave_check_8021q_upper(dev, ptr); 204783501299SVladimir Oltean break; 20482b138406SVladimir Oltean } 204983501299SVladimir Oltean case NETDEV_CHANGEUPPER: 2050058102a6STobias Waldekranz if (dsa_slave_dev_check(dev)) 20518e92ab3aSVivien Didelot return dsa_slave_changeupper(dev, ptr); 2052058102a6STobias Waldekranz 2053058102a6STobias Waldekranz if (netif_is_lag_master(dev)) 2054058102a6STobias Waldekranz return dsa_slave_lag_changeupper(dev, ptr); 2055058102a6STobias Waldekranz 2056058102a6STobias Waldekranz break; 2057058102a6STobias Waldekranz case NETDEV_CHANGELOWERSTATE: { 2058058102a6STobias Waldekranz struct netdev_notifier_changelowerstate_info *info = ptr; 2059058102a6STobias Waldekranz struct dsa_port *dp; 2060058102a6STobias Waldekranz int err; 2061058102a6STobias Waldekranz 2062058102a6STobias Waldekranz if (!dsa_slave_dev_check(dev)) 2063058102a6STobias Waldekranz break; 2064058102a6STobias Waldekranz 2065058102a6STobias Waldekranz dp = dsa_slave_to_port(dev); 2066058102a6STobias Waldekranz 2067058102a6STobias Waldekranz err = dsa_port_lag_change(dp, info->lower_state_info); 2068058102a6STobias Waldekranz return notifier_from_errno(err); 2069058102a6STobias Waldekranz } 2070cc1d5bdaSFlorian Fainelli } 2071b73adef6SFlorian Fainelli 2072b73adef6SFlorian Fainelli return NOTIFY_DONE; 2073b73adef6SFlorian Fainelli } 207488e4f0caSVivien Didelot 2075c4bb76a9SVladimir Oltean static void 2076c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work) 2077c4bb76a9SVladimir Oltean { 2078c4bb76a9SVladimir Oltean struct dsa_switch *ds = switchdev_work->ds; 2079c4bb76a9SVladimir Oltean struct switchdev_notifier_fdb_info info; 2080c4bb76a9SVladimir Oltean struct dsa_port *dp; 2081c4bb76a9SVladimir Oltean 2082c4bb76a9SVladimir Oltean if (!dsa_is_user_port(ds, switchdev_work->port)) 2083c4bb76a9SVladimir Oltean return; 2084c4bb76a9SVladimir Oltean 2085c4bb76a9SVladimir Oltean info.addr = switchdev_work->addr; 2086c4bb76a9SVladimir Oltean info.vid = switchdev_work->vid; 2087c4bb76a9SVladimir Oltean info.offloaded = true; 2088c4bb76a9SVladimir Oltean dp = dsa_to_port(ds, switchdev_work->port); 2089c4bb76a9SVladimir Oltean call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, 2090c4bb76a9SVladimir Oltean dp->slave, &info.info, NULL); 2091c4bb76a9SVladimir Oltean } 2092c9eb3e0fSArkadi Sharshevsky 2093c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work) 2094c9eb3e0fSArkadi Sharshevsky { 2095c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work = 2096c9eb3e0fSArkadi Sharshevsky container_of(work, struct dsa_switchdev_event_work, work); 2097c4bb76a9SVladimir Oltean struct dsa_switch *ds = switchdev_work->ds; 2098c4bb76a9SVladimir Oltean struct dsa_port *dp; 2099c9eb3e0fSArkadi Sharshevsky int err; 2100c9eb3e0fSArkadi Sharshevsky 2101c4bb76a9SVladimir Oltean dp = dsa_to_port(ds, switchdev_work->port); 2102c4bb76a9SVladimir Oltean 2103c9eb3e0fSArkadi Sharshevsky rtnl_lock(); 2104c9eb3e0fSArkadi Sharshevsky switch (switchdev_work->event) { 2105c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: 2106c4bb76a9SVladimir Oltean err = dsa_port_fdb_add(dp, switchdev_work->addr, 2107c4bb76a9SVladimir Oltean switchdev_work->vid); 2108c9eb3e0fSArkadi Sharshevsky if (err) { 2109c4bb76a9SVladimir Oltean dev_err(ds->dev, 2110c4bb76a9SVladimir Oltean "port %d failed to add %pM vid %d to fdb: %d\n", 2111c4bb76a9SVladimir Oltean dp->index, switchdev_work->addr, 2112c4bb76a9SVladimir Oltean switchdev_work->vid, err); 2113c9eb3e0fSArkadi Sharshevsky break; 2114c9eb3e0fSArkadi Sharshevsky } 2115c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(switchdev_work); 2116c9eb3e0fSArkadi Sharshevsky break; 2117c9eb3e0fSArkadi Sharshevsky 2118c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 2119c4bb76a9SVladimir Oltean err = dsa_port_fdb_del(dp, switchdev_work->addr, 2120c4bb76a9SVladimir Oltean switchdev_work->vid); 2121c9eb3e0fSArkadi Sharshevsky if (err) { 2122c4bb76a9SVladimir Oltean dev_err(ds->dev, 2123c4bb76a9SVladimir Oltean "port %d failed to delete %pM vid %d from fdb: %d\n", 2124c4bb76a9SVladimir Oltean dp->index, switchdev_work->addr, 2125c4bb76a9SVladimir Oltean switchdev_work->vid, err); 2126c9eb3e0fSArkadi Sharshevsky } 21272fd18650SVladimir Oltean 2128c9eb3e0fSArkadi Sharshevsky break; 2129c9eb3e0fSArkadi Sharshevsky } 2130c9eb3e0fSArkadi Sharshevsky rtnl_unlock(); 2131c9eb3e0fSArkadi Sharshevsky 2132c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 2133c4bb76a9SVladimir Oltean if (dsa_is_user_port(ds, dp->index)) 2134c4bb76a9SVladimir Oltean dev_put(dp->slave); 2135c9eb3e0fSArkadi Sharshevsky } 2136c9eb3e0fSArkadi Sharshevsky 2137d5f19486SVladimir Oltean static int dsa_lower_dev_walk(struct net_device *lower_dev, 2138d5f19486SVladimir Oltean struct netdev_nested_priv *priv) 2139d5f19486SVladimir Oltean { 2140d5f19486SVladimir Oltean if (dsa_slave_dev_check(lower_dev)) { 2141d5f19486SVladimir Oltean priv->data = (void *)netdev_priv(lower_dev); 2142d5f19486SVladimir Oltean return 1; 2143d5f19486SVladimir Oltean } 2144d5f19486SVladimir Oltean 2145d5f19486SVladimir Oltean return 0; 2146d5f19486SVladimir Oltean } 2147d5f19486SVladimir Oltean 2148d5f19486SVladimir Oltean static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev) 2149d5f19486SVladimir Oltean { 2150d5f19486SVladimir Oltean struct netdev_nested_priv priv = { 2151d5f19486SVladimir Oltean .data = NULL, 2152d5f19486SVladimir Oltean }; 2153d5f19486SVladimir Oltean 2154d5f19486SVladimir Oltean netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv); 2155d5f19486SVladimir Oltean 2156d5f19486SVladimir Oltean return (struct dsa_slave_priv *)priv.data; 2157d5f19486SVladimir Oltean } 2158d5f19486SVladimir Oltean 2159c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */ 2160c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused, 2161c9eb3e0fSArkadi Sharshevsky unsigned long event, void *ptr) 2162c9eb3e0fSArkadi Sharshevsky { 2163c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2164c4bb76a9SVladimir Oltean const struct switchdev_notifier_fdb_info *fdb_info; 2165c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work; 2166c4bb76a9SVladimir Oltean struct dsa_port *dp; 216779b139f4SVivien Didelot int err; 216879b139f4SVivien Didelot 2169447d290aSVladimir Oltean switch (event) { 2170447d290aSVladimir Oltean case SWITCHDEV_PORT_ATTR_SET: 217179b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 217279b139f4SVivien Didelot dsa_slave_dev_check, 217379b139f4SVivien Didelot dsa_slave_port_attr_set); 217479b139f4SVivien Didelot return notifier_from_errno(err); 2175447d290aSVladimir Oltean case SWITCHDEV_FDB_ADD_TO_DEVICE: 2176447d290aSVladimir Oltean case SWITCHDEV_FDB_DEL_TO_DEVICE: 2177d5f19486SVladimir Oltean fdb_info = ptr; 2178d5f19486SVladimir Oltean 2179d5f19486SVladimir Oltean if (dsa_slave_dev_check(dev)) { 2180d5f19486SVladimir Oltean if (!fdb_info->added_by_user) 2181d5f19486SVladimir Oltean return NOTIFY_OK; 2182c9eb3e0fSArkadi Sharshevsky 2183c4bb76a9SVladimir Oltean dp = dsa_slave_to_port(dev); 2184d5f19486SVladimir Oltean } else { 2185d5f19486SVladimir Oltean /* Snoop addresses learnt on foreign interfaces 2186d5f19486SVladimir Oltean * bridged with us, for switches that don't 2187d5f19486SVladimir Oltean * automatically learn SA from CPU-injected traffic 2188d5f19486SVladimir Oltean */ 2189d5f19486SVladimir Oltean struct net_device *br_dev; 2190d5f19486SVladimir Oltean struct dsa_slave_priv *p; 2191d5f19486SVladimir Oltean 2192d5f19486SVladimir Oltean br_dev = netdev_master_upper_dev_get_rcu(dev); 2193d5f19486SVladimir Oltean if (!br_dev) 2194d5f19486SVladimir Oltean return NOTIFY_DONE; 2195d5f19486SVladimir Oltean 2196d5f19486SVladimir Oltean if (!netif_is_bridge_master(br_dev)) 2197d5f19486SVladimir Oltean return NOTIFY_DONE; 2198d5f19486SVladimir Oltean 2199d5f19486SVladimir Oltean p = dsa_slave_dev_lower_find(br_dev); 2200d5f19486SVladimir Oltean if (!p) 2201d5f19486SVladimir Oltean return NOTIFY_DONE; 2202d5f19486SVladimir Oltean 2203d5f19486SVladimir Oltean dp = p->dp->cpu_dp; 2204d5f19486SVladimir Oltean 2205d5f19486SVladimir Oltean if (!dp->ds->assisted_learning_on_cpu_port) 2206d5f19486SVladimir Oltean return NOTIFY_DONE; 2207d5f19486SVladimir Oltean } 2208c4bb76a9SVladimir Oltean 22095fb4a451SVladimir Oltean if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del) 22105fb4a451SVladimir Oltean return NOTIFY_DONE; 22115fb4a451SVladimir Oltean 2212c9eb3e0fSArkadi Sharshevsky switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 2213c9eb3e0fSArkadi Sharshevsky if (!switchdev_work) 2214c9eb3e0fSArkadi Sharshevsky return NOTIFY_BAD; 2215c9eb3e0fSArkadi Sharshevsky 2216c9eb3e0fSArkadi Sharshevsky INIT_WORK(&switchdev_work->work, 2217c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_event_work); 2218c4bb76a9SVladimir Oltean switchdev_work->ds = dp->ds; 2219c4bb76a9SVladimir Oltean switchdev_work->port = dp->index; 2220c9eb3e0fSArkadi Sharshevsky switchdev_work->event = event; 2221c9eb3e0fSArkadi Sharshevsky 2222c4bb76a9SVladimir Oltean ether_addr_copy(switchdev_work->addr, 2223c4bb76a9SVladimir Oltean fdb_info->addr); 2224c4bb76a9SVladimir Oltean switchdev_work->vid = fdb_info->vid; 2225c4bb76a9SVladimir Oltean 2226d5f19486SVladimir Oltean /* Hold a reference on the slave for dsa_fdb_offload_notify */ 2227d5f19486SVladimir Oltean if (dsa_is_user_port(dp->ds, dp->index)) 2228c9eb3e0fSArkadi Sharshevsky dev_hold(dev); 2229447d290aSVladimir Oltean dsa_schedule_work(&switchdev_work->work); 2230c9eb3e0fSArkadi Sharshevsky break; 2231c9eb3e0fSArkadi Sharshevsky default: 2232c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 2233c9eb3e0fSArkadi Sharshevsky } 2234c9eb3e0fSArkadi Sharshevsky 2235c9eb3e0fSArkadi Sharshevsky return NOTIFY_OK; 2236c9eb3e0fSArkadi Sharshevsky } 2237c9eb3e0fSArkadi Sharshevsky 22382b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused, 22392b239f67SPetr Machata unsigned long event, void *ptr) 22402b239f67SPetr Machata { 22412b239f67SPetr Machata struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 224279b139f4SVivien Didelot int err; 22432b239f67SPetr Machata 22442b239f67SPetr Machata switch (event) { 224579b139f4SVivien Didelot case SWITCHDEV_PORT_OBJ_ADD: 224679b139f4SVivien Didelot err = switchdev_handle_port_obj_add(dev, ptr, 224779b139f4SVivien Didelot dsa_slave_dev_check, 224879b139f4SVivien Didelot dsa_slave_port_obj_add); 224979b139f4SVivien Didelot return notifier_from_errno(err); 22502b239f67SPetr Machata case SWITCHDEV_PORT_OBJ_DEL: 225179b139f4SVivien Didelot err = switchdev_handle_port_obj_del(dev, ptr, 225279b139f4SVivien Didelot dsa_slave_dev_check, 225379b139f4SVivien Didelot dsa_slave_port_obj_del); 225479b139f4SVivien Didelot return notifier_from_errno(err); 22559ed1ecedSFlorian Fainelli case SWITCHDEV_PORT_ATTR_SET: 225679b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 225779b139f4SVivien Didelot dsa_slave_dev_check, 225879b139f4SVivien Didelot dsa_slave_port_attr_set); 225979b139f4SVivien Didelot return notifier_from_errno(err); 22602b239f67SPetr Machata } 22612b239f67SPetr Machata 22622b239f67SPetr Machata return NOTIFY_DONE; 22632b239f67SPetr Machata } 22642b239f67SPetr Machata 226588e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = { 226688e4f0caSVivien Didelot .notifier_call = dsa_slave_netdevice_event, 226788e4f0caSVivien Didelot }; 226888e4f0caSVivien Didelot 2269c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = { 2270c9eb3e0fSArkadi Sharshevsky .notifier_call = dsa_slave_switchdev_event, 2271c9eb3e0fSArkadi Sharshevsky }; 2272c9eb3e0fSArkadi Sharshevsky 22732b239f67SPetr Machata static struct notifier_block dsa_slave_switchdev_blocking_notifier = { 22742b239f67SPetr Machata .notifier_call = dsa_slave_switchdev_blocking_event, 22752b239f67SPetr Machata }; 22762b239f67SPetr Machata 227788e4f0caSVivien Didelot int dsa_slave_register_notifier(void) 227888e4f0caSVivien Didelot { 22792b239f67SPetr Machata struct notifier_block *nb; 2280c9eb3e0fSArkadi Sharshevsky int err; 2281c9eb3e0fSArkadi Sharshevsky 2282c9eb3e0fSArkadi Sharshevsky err = register_netdevice_notifier(&dsa_slave_nb); 2283c9eb3e0fSArkadi Sharshevsky if (err) 2284c9eb3e0fSArkadi Sharshevsky return err; 2285c9eb3e0fSArkadi Sharshevsky 2286c9eb3e0fSArkadi Sharshevsky err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 2287c9eb3e0fSArkadi Sharshevsky if (err) 2288c9eb3e0fSArkadi Sharshevsky goto err_switchdev_nb; 2289c9eb3e0fSArkadi Sharshevsky 22902b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 22912b239f67SPetr Machata err = register_switchdev_blocking_notifier(nb); 22922b239f67SPetr Machata if (err) 22932b239f67SPetr Machata goto err_switchdev_blocking_nb; 22942b239f67SPetr Machata 2295c9eb3e0fSArkadi Sharshevsky return 0; 2296c9eb3e0fSArkadi Sharshevsky 22972b239f67SPetr Machata err_switchdev_blocking_nb: 22982b239f67SPetr Machata unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 2299c9eb3e0fSArkadi Sharshevsky err_switchdev_nb: 2300c9eb3e0fSArkadi Sharshevsky unregister_netdevice_notifier(&dsa_slave_nb); 2301c9eb3e0fSArkadi Sharshevsky return err; 230288e4f0caSVivien Didelot } 230388e4f0caSVivien Didelot 230488e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void) 230588e4f0caSVivien Didelot { 23062b239f67SPetr Machata struct notifier_block *nb; 230788e4f0caSVivien Didelot int err; 230888e4f0caSVivien Didelot 23092b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 23102b239f67SPetr Machata err = unregister_switchdev_blocking_notifier(nb); 23112b239f67SPetr Machata if (err) 23122b239f67SPetr Machata pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err); 23132b239f67SPetr Machata 2314c9eb3e0fSArkadi Sharshevsky err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 2315c9eb3e0fSArkadi Sharshevsky if (err) 2316c9eb3e0fSArkadi Sharshevsky pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 2317c9eb3e0fSArkadi Sharshevsky 231888e4f0caSVivien Didelot err = unregister_netdevice_notifier(&dsa_slave_nb); 231988e4f0caSVivien Didelot if (err) 232088e4f0caSVivien Didelot pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 232188e4f0caSVivien Didelot } 2322