191da11f8SLennert Buytenhek /* 291da11f8SLennert Buytenhek * net/dsa/slave.c - Slave device handling 3e84665c9SLennert Buytenhek * Copyright (c) 2008-2009 Marvell Semiconductor 491da11f8SLennert Buytenhek * 591da11f8SLennert Buytenhek * This program is free software; you can redistribute it and/or modify 691da11f8SLennert Buytenhek * it under the terms of the GNU General Public License as published by 791da11f8SLennert Buytenhek * the Free Software Foundation; either version 2 of the License, or 891da11f8SLennert Buytenhek * (at your option) any later version. 991da11f8SLennert Buytenhek */ 1091da11f8SLennert Buytenhek 1191da11f8SLennert Buytenhek #include <linux/list.h> 12df02c6ffSLennert Buytenhek #include <linux/etherdevice.h> 13b73adef6SFlorian Fainelli #include <linux/netdevice.h> 1491da11f8SLennert Buytenhek #include <linux/phy.h> 15a2820543SFlorian Fainelli #include <linux/phy_fixed.h> 160d8bcdd3SFlorian Fainelli #include <linux/of_net.h> 170d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h> 187f854420SAndrew Lunn #include <linux/mdio.h> 19f50f2127SFlorian Fainelli #include <linux/list.h> 20b73adef6SFlorian Fainelli #include <net/rtnetlink.h> 2198237d43SScott Feldman #include <net/switchdev.h> 22f50f2127SFlorian Fainelli #include <net/pkt_cls.h> 23f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h> 24b73adef6SFlorian Fainelli #include <linux/if_bridge.h> 2504ff53f9SFlorian Fainelli #include <linux/netpoll.h> 2691da11f8SLennert Buytenhek #include "dsa_priv.h" 2791da11f8SLennert Buytenhek 28f50f2127SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev); 29f50f2127SFlorian Fainelli 3004d3a4c6SVivien Didelot static int dsa_slave_notify(struct net_device *dev, unsigned long e, void *v) 3104d3a4c6SVivien Didelot { 3204d3a4c6SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 3304d3a4c6SVivien Didelot struct raw_notifier_head *nh = &p->dp->ds->dst->nh; 3404d3a4c6SVivien Didelot int err; 3504d3a4c6SVivien Didelot 3604d3a4c6SVivien Didelot err = raw_notifier_call_chain(nh, e, v); 3704d3a4c6SVivien Didelot 3804d3a4c6SVivien Didelot return notifier_to_errno(err); 3904d3a4c6SVivien Didelot } 4004d3a4c6SVivien Didelot 4191da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 4291da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 4391da11f8SLennert Buytenhek { 4491da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 4591da11f8SLennert Buytenhek 460d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 479d490b4eSVivien Didelot return ds->ops->phy_read(ds, addr, reg); 4891da11f8SLennert Buytenhek 4991da11f8SLennert Buytenhek return 0xffff; 5091da11f8SLennert Buytenhek } 5191da11f8SLennert Buytenhek 5291da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 5391da11f8SLennert Buytenhek { 5491da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 5591da11f8SLennert Buytenhek 560d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 579d490b4eSVivien Didelot return ds->ops->phy_write(ds, addr, reg, val); 5891da11f8SLennert Buytenhek 5991da11f8SLennert Buytenhek return 0; 6091da11f8SLennert Buytenhek } 6191da11f8SLennert Buytenhek 6291da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 6391da11f8SLennert Buytenhek { 6491da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 6591da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 6691da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 6791da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 680b7b498dSFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 690b7b498dSFlorian Fainelli ds->dst->tree, ds->index); 70c33063d6SAndrew Lunn ds->slave_mii_bus->parent = ds->dev; 7124df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 7291da11f8SLennert Buytenhek } 7391da11f8SLennert Buytenhek 7491da11f8SLennert Buytenhek 7591da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 76abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev) 77c0840801SLennert Buytenhek { 78c0840801SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 79c0840801SLennert Buytenhek 80afdcf151SVivien Didelot return p->dp->ds->dst->master_netdev->ifindex; 81c0840801SLennert Buytenhek } 82c0840801SLennert Buytenhek 83a5e9a02eSVivien Didelot static inline bool dsa_port_is_bridged(struct dsa_port *dp) 84b73adef6SFlorian Fainelli { 85a5e9a02eSVivien Didelot return !!dp->bridge_dev; 86b73adef6SFlorian Fainelli } 87b73adef6SFlorian Fainelli 88c5d35cb3SVivien Didelot static void dsa_slave_set_state(struct net_device *dev, u8 state) 894acfee81SVivien Didelot { 90c5d35cb3SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 91c5d35cb3SVivien Didelot struct dsa_port *dp = p->dp; 92c5d35cb3SVivien Didelot struct dsa_switch *ds = dp->ds; 93c5d35cb3SVivien Didelot int port = dp->index; 94732f794cSVivien Didelot 954acfee81SVivien Didelot if (ds->ops->port_stp_state_set) 964acfee81SVivien Didelot ds->ops->port_stp_state_set(ds, port, state); 97732f794cSVivien Didelot 98732f794cSVivien Didelot if (ds->ops->port_fast_age) { 99732f794cSVivien Didelot /* Fast age FDB entries or flush appropriate forwarding database 100732f794cSVivien Didelot * for the given port, if we are moving it from Learning or 101732f794cSVivien Didelot * Forwarding state, to Disabled or Blocking or Listening state. 102732f794cSVivien Didelot */ 103732f794cSVivien Didelot 104732f794cSVivien Didelot if ((dp->stp_state == BR_STATE_LEARNING || 105732f794cSVivien Didelot dp->stp_state == BR_STATE_FORWARDING) && 106732f794cSVivien Didelot (state == BR_STATE_DISABLED || 107732f794cSVivien Didelot state == BR_STATE_BLOCKING || 108732f794cSVivien Didelot state == BR_STATE_LISTENING)) 109732f794cSVivien Didelot ds->ops->port_fast_age(ds, port); 110732f794cSVivien Didelot } 111732f794cSVivien Didelot 112732f794cSVivien Didelot dp->stp_state = state; 1134acfee81SVivien Didelot } 1144acfee81SVivien Didelot 11591da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 11691da11f8SLennert Buytenhek { 117df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 118afdcf151SVivien Didelot struct net_device *master = p->dp->ds->dst->master_netdev; 119afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 120a5e9a02eSVivien Didelot u8 stp_state = dsa_port_is_bridged(p->dp) ? 121b73adef6SFlorian Fainelli BR_STATE_BLOCKING : BR_STATE_FORWARDING; 122df02c6ffSLennert Buytenhek int err; 123df02c6ffSLennert Buytenhek 124df02c6ffSLennert Buytenhek if (!(master->flags & IFF_UP)) 125df02c6ffSLennert Buytenhek return -ENETDOWN; 126df02c6ffSLennert Buytenhek 1278feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 128a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 129df02c6ffSLennert Buytenhek if (err < 0) 130df02c6ffSLennert Buytenhek goto out; 131df02c6ffSLennert Buytenhek } 132df02c6ffSLennert Buytenhek 133df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) { 134df02c6ffSLennert Buytenhek err = dev_set_allmulti(master, 1); 135df02c6ffSLennert Buytenhek if (err < 0) 136df02c6ffSLennert Buytenhek goto del_unicast; 137df02c6ffSLennert Buytenhek } 138df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) { 139df02c6ffSLennert Buytenhek err = dev_set_promiscuity(master, 1); 140df02c6ffSLennert Buytenhek if (err < 0) 141df02c6ffSLennert Buytenhek goto clear_allmulti; 142df02c6ffSLennert Buytenhek } 143df02c6ffSLennert Buytenhek 1449d490b4eSVivien Didelot if (ds->ops->port_enable) { 145afdcf151SVivien Didelot err = ds->ops->port_enable(ds, p->dp->index, p->phy); 146b2f2af21SFlorian Fainelli if (err) 147b2f2af21SFlorian Fainelli goto clear_promisc; 148b2f2af21SFlorian Fainelli } 149b2f2af21SFlorian Fainelli 150c5d35cb3SVivien Didelot dsa_slave_set_state(dev, stp_state); 151b73adef6SFlorian Fainelli 152f7f1de51SFlorian Fainelli if (p->phy) 153f7f1de51SFlorian Fainelli phy_start(p->phy); 154f7f1de51SFlorian Fainelli 15591da11f8SLennert Buytenhek return 0; 156df02c6ffSLennert Buytenhek 157b2f2af21SFlorian Fainelli clear_promisc: 158b2f2af21SFlorian Fainelli if (dev->flags & IFF_PROMISC) 1594fdeddfeSGilad Ben-Yossef dev_set_promiscuity(master, -1); 160df02c6ffSLennert Buytenhek clear_allmulti: 161df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 162df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 163df02c6ffSLennert Buytenhek del_unicast: 1648feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 165a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 166df02c6ffSLennert Buytenhek out: 167df02c6ffSLennert Buytenhek return err; 16891da11f8SLennert Buytenhek } 16991da11f8SLennert Buytenhek 17091da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 17191da11f8SLennert Buytenhek { 172df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 173afdcf151SVivien Didelot struct net_device *master = p->dp->ds->dst->master_netdev; 174afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 175df02c6ffSLennert Buytenhek 176f7f1de51SFlorian Fainelli if (p->phy) 177f7f1de51SFlorian Fainelli phy_stop(p->phy); 178f7f1de51SFlorian Fainelli 179df02c6ffSLennert Buytenhek dev_mc_unsync(master, dev); 180a748ee24SJiri Pirko dev_uc_unsync(master, dev); 181df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 182df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 183df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) 184df02c6ffSLennert Buytenhek dev_set_promiscuity(master, -1); 185df02c6ffSLennert Buytenhek 1868feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 187a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 188df02c6ffSLennert Buytenhek 1899d490b4eSVivien Didelot if (ds->ops->port_disable) 190afdcf151SVivien Didelot ds->ops->port_disable(ds, p->dp->index, p->phy); 191b2f2af21SFlorian Fainelli 192c5d35cb3SVivien Didelot dsa_slave_set_state(dev, BR_STATE_DISABLED); 193b73adef6SFlorian Fainelli 19491da11f8SLennert Buytenhek return 0; 19591da11f8SLennert Buytenhek } 19691da11f8SLennert Buytenhek 19791da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 19891da11f8SLennert Buytenhek { 19991da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 200afdcf151SVivien Didelot struct net_device *master = p->dp->ds->dst->master_netdev; 20191da11f8SLennert Buytenhek 20291da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 20391da11f8SLennert Buytenhek dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); 20491da11f8SLennert Buytenhek if (change & IFF_PROMISC) 20591da11f8SLennert Buytenhek dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); 20691da11f8SLennert Buytenhek } 20791da11f8SLennert Buytenhek 20891da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 20991da11f8SLennert Buytenhek { 21091da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 211afdcf151SVivien Didelot struct net_device *master = p->dp->ds->dst->master_netdev; 21291da11f8SLennert Buytenhek 21391da11f8SLennert Buytenhek dev_mc_sync(master, dev); 214a748ee24SJiri Pirko dev_uc_sync(master, dev); 21591da11f8SLennert Buytenhek } 21691da11f8SLennert Buytenhek 217df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 21891da11f8SLennert Buytenhek { 219df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 220afdcf151SVivien Didelot struct net_device *master = p->dp->ds->dst->master_netdev; 221df02c6ffSLennert Buytenhek struct sockaddr *addr = a; 222df02c6ffSLennert Buytenhek int err; 223df02c6ffSLennert Buytenhek 224df02c6ffSLennert Buytenhek if (!is_valid_ether_addr(addr->sa_data)) 225df02c6ffSLennert Buytenhek return -EADDRNOTAVAIL; 226df02c6ffSLennert Buytenhek 227df02c6ffSLennert Buytenhek if (!(dev->flags & IFF_UP)) 228df02c6ffSLennert Buytenhek goto out; 229df02c6ffSLennert Buytenhek 2308feedbb4SJoe Perches if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 231a748ee24SJiri Pirko err = dev_uc_add(master, addr->sa_data); 232df02c6ffSLennert Buytenhek if (err < 0) 233df02c6ffSLennert Buytenhek return err; 234df02c6ffSLennert Buytenhek } 235df02c6ffSLennert Buytenhek 2368feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 237a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 238df02c6ffSLennert Buytenhek 239df02c6ffSLennert Buytenhek out: 240d08f161aSJoe Perches ether_addr_copy(dev->dev_addr, addr->sa_data); 24191da11f8SLennert Buytenhek 24291da11f8SLennert Buytenhek return 0; 24391da11f8SLennert Buytenhek } 24491da11f8SLennert Buytenhek 24511149536SVivien Didelot static int dsa_slave_port_vlan_add(struct net_device *dev, 2468f24f309SJiri Pirko const struct switchdev_obj_port_vlan *vlan, 247f8db8348SJiri Pirko struct switchdev_trans *trans) 24811149536SVivien Didelot { 24911149536SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 250afdcf151SVivien Didelot struct dsa_port *dp = p->dp; 251afdcf151SVivien Didelot struct dsa_switch *ds = dp->ds; 25211149536SVivien Didelot 25379a62eb2SJiri Pirko if (switchdev_trans_ph_prepare(trans)) { 2549d490b4eSVivien Didelot if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add) 25511149536SVivien Didelot return -EOPNOTSUPP; 25611149536SVivien Didelot 257afdcf151SVivien Didelot return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans); 25811149536SVivien Didelot } 25911149536SVivien Didelot 260afdcf151SVivien Didelot ds->ops->port_vlan_add(ds, dp->index, vlan, trans); 2614d5770b3SVivien Didelot 26211149536SVivien Didelot return 0; 26311149536SVivien Didelot } 26411149536SVivien Didelot 26511149536SVivien Didelot static int dsa_slave_port_vlan_del(struct net_device *dev, 2668f24f309SJiri Pirko const struct switchdev_obj_port_vlan *vlan) 26711149536SVivien Didelot { 26811149536SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 269afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 27011149536SVivien Didelot 2719d490b4eSVivien Didelot if (!ds->ops->port_vlan_del) 27211149536SVivien Didelot return -EOPNOTSUPP; 27311149536SVivien Didelot 274afdcf151SVivien Didelot return ds->ops->port_vlan_del(ds, p->dp->index, vlan); 27511149536SVivien Didelot } 27611149536SVivien Didelot 27711149536SVivien Didelot static int dsa_slave_port_vlan_dump(struct net_device *dev, 2788f24f309SJiri Pirko struct switchdev_obj_port_vlan *vlan, 279648b4a99SJiri Pirko switchdev_obj_dump_cb_t *cb) 28011149536SVivien Didelot { 28111149536SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 282afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 28311149536SVivien Didelot 2849d490b4eSVivien Didelot if (ds->ops->port_vlan_dump) 285afdcf151SVivien Didelot return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb); 28665aebfc0SVivien Didelot 28711149536SVivien Didelot return -EOPNOTSUPP; 28811149536SVivien Didelot } 28911149536SVivien Didelot 290ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_add(struct net_device *dev, 29152ba57cfSJiri Pirko const struct switchdev_obj_port_fdb *fdb, 292f8db8348SJiri Pirko struct switchdev_trans *trans) 293cdf09697SDavid S. Miller { 294cdf09697SDavid S. Miller struct dsa_slave_priv *p = netdev_priv(dev); 295afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 296146a3206SVivien Didelot 2978497aa61SVivien Didelot if (switchdev_trans_ph_prepare(trans)) { 2989d490b4eSVivien Didelot if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add) 299146a3206SVivien Didelot return -EOPNOTSUPP; 300cdf09697SDavid S. Miller 301afdcf151SVivien Didelot return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans); 3028497aa61SVivien Didelot } 303cdf09697SDavid S. Miller 304afdcf151SVivien Didelot ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans); 3058497aa61SVivien Didelot 3068497aa61SVivien Didelot return 0; 307cdf09697SDavid S. Miller } 308cdf09697SDavid S. Miller 309ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_del(struct net_device *dev, 31052ba57cfSJiri Pirko const struct switchdev_obj_port_fdb *fdb) 311cdf09697SDavid S. Miller { 312cdf09697SDavid S. Miller struct dsa_slave_priv *p = netdev_priv(dev); 313afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 314cdf09697SDavid S. Miller int ret = -EOPNOTSUPP; 315cdf09697SDavid S. Miller 3169d490b4eSVivien Didelot if (ds->ops->port_fdb_del) 317afdcf151SVivien Didelot ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb); 318cdf09697SDavid S. Miller 319cdf09697SDavid S. Miller return ret; 320cdf09697SDavid S. Miller } 321cdf09697SDavid S. Miller 322ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_dump(struct net_device *dev, 32352ba57cfSJiri Pirko struct switchdev_obj_port_fdb *fdb, 324648b4a99SJiri Pirko switchdev_obj_dump_cb_t *cb) 325cdf09697SDavid S. Miller { 326cdf09697SDavid S. Miller struct dsa_slave_priv *p = netdev_priv(dev); 327afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 328cdf09697SDavid S. Miller 3299d490b4eSVivien Didelot if (ds->ops->port_fdb_dump) 330afdcf151SVivien Didelot return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb); 331ea70ba98SVivien Didelot 332cdf09697SDavid S. Miller return -EOPNOTSUPP; 333cdf09697SDavid S. Miller } 334cdf09697SDavid S. Miller 3358df30255SVivien Didelot static int dsa_slave_port_mdb_add(struct net_device *dev, 3368df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb, 3378df30255SVivien Didelot struct switchdev_trans *trans) 3388df30255SVivien Didelot { 3398df30255SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 340afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 3418df30255SVivien Didelot 3428df30255SVivien Didelot if (switchdev_trans_ph_prepare(trans)) { 3438df30255SVivien Didelot if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add) 3448df30255SVivien Didelot return -EOPNOTSUPP; 3458df30255SVivien Didelot 346afdcf151SVivien Didelot return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans); 3478df30255SVivien Didelot } 3488df30255SVivien Didelot 349afdcf151SVivien Didelot ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans); 3508df30255SVivien Didelot 3518df30255SVivien Didelot return 0; 3528df30255SVivien Didelot } 3538df30255SVivien Didelot 3548df30255SVivien Didelot static int dsa_slave_port_mdb_del(struct net_device *dev, 3558df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb) 3568df30255SVivien Didelot { 3578df30255SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 358afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 3598df30255SVivien Didelot 3608df30255SVivien Didelot if (ds->ops->port_mdb_del) 361afdcf151SVivien Didelot return ds->ops->port_mdb_del(ds, p->dp->index, mdb); 3628df30255SVivien Didelot 3638df30255SVivien Didelot return -EOPNOTSUPP; 3648df30255SVivien Didelot } 3658df30255SVivien Didelot 3668df30255SVivien Didelot static int dsa_slave_port_mdb_dump(struct net_device *dev, 3678df30255SVivien Didelot struct switchdev_obj_port_mdb *mdb, 3688df30255SVivien Didelot switchdev_obj_dump_cb_t *cb) 3698df30255SVivien Didelot { 3708df30255SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 371afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 3728df30255SVivien Didelot 3738df30255SVivien Didelot if (ds->ops->port_mdb_dump) 374afdcf151SVivien Didelot return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb); 3758df30255SVivien Didelot 3768df30255SVivien Didelot return -EOPNOTSUPP; 3778df30255SVivien Didelot } 3788df30255SVivien Didelot 37991da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 38091da11f8SLennert Buytenhek { 38191da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 38291da11f8SLennert Buytenhek 38391da11f8SLennert Buytenhek if (p->phy != NULL) 38428b04113SRichard Cochran return phy_mii_ioctl(p->phy, ifr, cmd); 38591da11f8SLennert Buytenhek 38691da11f8SLennert Buytenhek return -EOPNOTSUPP; 38791da11f8SLennert Buytenhek } 38891da11f8SLennert Buytenhek 38943c44a9fSVivien Didelot static int dsa_slave_stp_state_set(struct net_device *dev, 39043c44a9fSVivien Didelot const struct switchdev_attr *attr, 39143c44a9fSVivien Didelot struct switchdev_trans *trans) 392b73adef6SFlorian Fainelli { 393b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 394afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 395b73adef6SFlorian Fainelli 39643c44a9fSVivien Didelot if (switchdev_trans_ph_prepare(trans)) 3979d490b4eSVivien Didelot return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP; 398b73adef6SFlorian Fainelli 399c5d35cb3SVivien Didelot dsa_slave_set_state(dev, attr->u.stp_state); 40043c44a9fSVivien Didelot 40143c44a9fSVivien Didelot return 0; 402b73adef6SFlorian Fainelli } 403b73adef6SFlorian Fainelli 404fb2dabadSVivien Didelot static int dsa_slave_vlan_filtering(struct net_device *dev, 405fb2dabadSVivien Didelot const struct switchdev_attr *attr, 406fb2dabadSVivien Didelot struct switchdev_trans *trans) 407fb2dabadSVivien Didelot { 408fb2dabadSVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 409afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 410fb2dabadSVivien Didelot 411fb2dabadSVivien Didelot /* bridge skips -EOPNOTSUPP, so skip the prepare phase */ 412fb2dabadSVivien Didelot if (switchdev_trans_ph_prepare(trans)) 413fb2dabadSVivien Didelot return 0; 414fb2dabadSVivien Didelot 4159d490b4eSVivien Didelot if (ds->ops->port_vlan_filtering) 416afdcf151SVivien Didelot return ds->ops->port_vlan_filtering(ds, p->dp->index, 417fb2dabadSVivien Didelot attr->u.vlan_filtering); 418fb2dabadSVivien Didelot 419fb2dabadSVivien Didelot return 0; 420fb2dabadSVivien Didelot } 421fb2dabadSVivien Didelot 422e893de1bSVivien Didelot static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds, 42334a79f63SVivien Didelot unsigned int ageing_time) 42434a79f63SVivien Didelot { 42534a79f63SVivien Didelot int i; 42634a79f63SVivien Didelot 42726895e29SVivien Didelot for (i = 0; i < ds->num_ports; ++i) { 42834a79f63SVivien Didelot struct dsa_port *dp = &ds->ports[i]; 42934a79f63SVivien Didelot 43034a79f63SVivien Didelot if (dp && dp->ageing_time && dp->ageing_time < ageing_time) 43134a79f63SVivien Didelot ageing_time = dp->ageing_time; 43234a79f63SVivien Didelot } 43334a79f63SVivien Didelot 43434a79f63SVivien Didelot return ageing_time; 43534a79f63SVivien Didelot } 43634a79f63SVivien Didelot 43734a79f63SVivien Didelot static int dsa_slave_ageing_time(struct net_device *dev, 43834a79f63SVivien Didelot const struct switchdev_attr *attr, 43934a79f63SVivien Didelot struct switchdev_trans *trans) 44034a79f63SVivien Didelot { 44134a79f63SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 442afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 44334a79f63SVivien Didelot unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time); 44434a79f63SVivien Didelot unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); 44534a79f63SVivien Didelot 446*0f3da6afSVivien Didelot if (switchdev_trans_ph_prepare(trans)) { 447*0f3da6afSVivien Didelot if (ds->ageing_time_min && ageing_time < ds->ageing_time_min) 448*0f3da6afSVivien Didelot return -ERANGE; 449*0f3da6afSVivien Didelot if (ds->ageing_time_max && ageing_time > ds->ageing_time_max) 450*0f3da6afSVivien Didelot return -ERANGE; 45134a79f63SVivien Didelot return 0; 452*0f3da6afSVivien Didelot } 45334a79f63SVivien Didelot 45434a79f63SVivien Didelot /* Keep the fastest ageing time in case of multiple bridges */ 455afdcf151SVivien Didelot p->dp->ageing_time = ageing_time; 45634a79f63SVivien Didelot ageing_time = dsa_fastest_ageing_time(ds, ageing_time); 45734a79f63SVivien Didelot 4589d490b4eSVivien Didelot if (ds->ops->set_ageing_time) 4599d490b4eSVivien Didelot return ds->ops->set_ageing_time(ds, ageing_time); 46034a79f63SVivien Didelot 46134a79f63SVivien Didelot return 0; 46234a79f63SVivien Didelot } 46334a79f63SVivien Didelot 46435636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev, 465f7fadf30SJiri Pirko const struct switchdev_attr *attr, 4667ea6eb3fSJiri Pirko struct switchdev_trans *trans) 46735636062SScott Feldman { 468b8d866acSVivien Didelot int ret; 46935636062SScott Feldman 47035636062SScott Feldman switch (attr->id) { 4711f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 47243c44a9fSVivien Didelot ret = dsa_slave_stp_state_set(dev, attr, trans); 47335636062SScott Feldman break; 474fb2dabadSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 475fb2dabadSVivien Didelot ret = dsa_slave_vlan_filtering(dev, attr, trans); 476fb2dabadSVivien Didelot break; 47734a79f63SVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 47834a79f63SVivien Didelot ret = dsa_slave_ageing_time(dev, attr, trans); 47934a79f63SVivien Didelot break; 48035636062SScott Feldman default: 48135636062SScott Feldman ret = -EOPNOTSUPP; 48235636062SScott Feldman break; 48335636062SScott Feldman } 48435636062SScott Feldman 48535636062SScott Feldman return ret; 48635636062SScott Feldman } 48735636062SScott Feldman 488ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev, 489648b4a99SJiri Pirko const struct switchdev_obj *obj, 4907ea6eb3fSJiri Pirko struct switchdev_trans *trans) 491ba14d9ebSVivien Didelot { 492ba14d9ebSVivien Didelot int err; 493ba14d9ebSVivien Didelot 494ba14d9ebSVivien Didelot /* For the prepare phase, ensure the full set of changes is feasable in 495ba14d9ebSVivien Didelot * one go in order to signal a failure properly. If an operation is not 496ba14d9ebSVivien Didelot * supported, return -EOPNOTSUPP. 497ba14d9ebSVivien Didelot */ 498ba14d9ebSVivien Didelot 4999e8f4a54SJiri Pirko switch (obj->id) { 50057d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_FDB: 501648b4a99SJiri Pirko err = dsa_slave_port_fdb_add(dev, 502648b4a99SJiri Pirko SWITCHDEV_OBJ_PORT_FDB(obj), 503648b4a99SJiri Pirko trans); 504ba14d9ebSVivien Didelot break; 5058df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 5068df30255SVivien Didelot err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj), 5078df30255SVivien Didelot trans); 5088df30255SVivien Didelot break; 50957d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 510648b4a99SJiri Pirko err = dsa_slave_port_vlan_add(dev, 511648b4a99SJiri Pirko SWITCHDEV_OBJ_PORT_VLAN(obj), 512648b4a99SJiri Pirko trans); 51311149536SVivien Didelot break; 514ba14d9ebSVivien Didelot default: 515ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 516ba14d9ebSVivien Didelot break; 517ba14d9ebSVivien Didelot } 518ba14d9ebSVivien Didelot 519ba14d9ebSVivien Didelot return err; 520ba14d9ebSVivien Didelot } 521ba14d9ebSVivien Didelot 522ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev, 523648b4a99SJiri Pirko const struct switchdev_obj *obj) 524ba14d9ebSVivien Didelot { 525ba14d9ebSVivien Didelot int err; 526ba14d9ebSVivien Didelot 5279e8f4a54SJiri Pirko switch (obj->id) { 52857d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_FDB: 529648b4a99SJiri Pirko err = dsa_slave_port_fdb_del(dev, 530648b4a99SJiri Pirko SWITCHDEV_OBJ_PORT_FDB(obj)); 531ba14d9ebSVivien Didelot break; 5328df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 5338df30255SVivien Didelot err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj)); 5348df30255SVivien Didelot break; 53557d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 536648b4a99SJiri Pirko err = dsa_slave_port_vlan_del(dev, 537648b4a99SJiri Pirko SWITCHDEV_OBJ_PORT_VLAN(obj)); 53811149536SVivien Didelot break; 539ba14d9ebSVivien Didelot default: 540ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 541ba14d9ebSVivien Didelot break; 542ba14d9ebSVivien Didelot } 543ba14d9ebSVivien Didelot 544ba14d9ebSVivien Didelot return err; 545ba14d9ebSVivien Didelot } 546ba14d9ebSVivien Didelot 547ba14d9ebSVivien Didelot static int dsa_slave_port_obj_dump(struct net_device *dev, 548648b4a99SJiri Pirko struct switchdev_obj *obj, 549648b4a99SJiri Pirko switchdev_obj_dump_cb_t *cb) 550ba14d9ebSVivien Didelot { 551ba14d9ebSVivien Didelot int err; 552ba14d9ebSVivien Didelot 5539e8f4a54SJiri Pirko switch (obj->id) { 55457d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_FDB: 555648b4a99SJiri Pirko err = dsa_slave_port_fdb_dump(dev, 556648b4a99SJiri Pirko SWITCHDEV_OBJ_PORT_FDB(obj), 557648b4a99SJiri Pirko cb); 558ba14d9ebSVivien Didelot break; 5598df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 5608df30255SVivien Didelot err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj), 5618df30255SVivien Didelot cb); 5628df30255SVivien Didelot break; 56357d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 564648b4a99SJiri Pirko err = dsa_slave_port_vlan_dump(dev, 565648b4a99SJiri Pirko SWITCHDEV_OBJ_PORT_VLAN(obj), 566648b4a99SJiri Pirko cb); 56711149536SVivien Didelot break; 568ba14d9ebSVivien Didelot default: 569ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 570ba14d9ebSVivien Didelot break; 571ba14d9ebSVivien Didelot } 572ba14d9ebSVivien Didelot 573ba14d9ebSVivien Didelot return err; 574ba14d9ebSVivien Didelot } 575ba14d9ebSVivien Didelot 576b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_join(struct net_device *dev, 577b73adef6SFlorian Fainelli struct net_device *br) 578b73adef6SFlorian Fainelli { 579b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 58004d3a4c6SVivien Didelot struct dsa_notifier_bridge_info info = { 58104d3a4c6SVivien Didelot .sw_index = p->dp->ds->index, 58204d3a4c6SVivien Didelot .port = p->dp->index, 58304d3a4c6SVivien Didelot .br = br, 58404d3a4c6SVivien Didelot }; 58504d3a4c6SVivien Didelot int err; 586b73adef6SFlorian Fainelli 5879c265426SVivien Didelot /* Here the port is already bridged. Reflect the current configuration 5889c265426SVivien Didelot * so that drivers can program their chips accordingly. 5899c265426SVivien Didelot */ 590a5e9a02eSVivien Didelot p->dp->bridge_dev = br; 591b73adef6SFlorian Fainelli 59204d3a4c6SVivien Didelot err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_JOIN, &info); 593b73adef6SFlorian Fainelli 5949c265426SVivien Didelot /* The bridging is rolled back on error */ 59504d3a4c6SVivien Didelot if (err) 5969c265426SVivien Didelot p->dp->bridge_dev = NULL; 5979c265426SVivien Didelot 59804d3a4c6SVivien Didelot return err; 599b73adef6SFlorian Fainelli } 600b73adef6SFlorian Fainelli 601f123f2fbSVivien Didelot static void dsa_slave_bridge_port_leave(struct net_device *dev, 602f123f2fbSVivien Didelot struct net_device *br) 603b73adef6SFlorian Fainelli { 604b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 60504d3a4c6SVivien Didelot struct dsa_notifier_bridge_info info = { 60604d3a4c6SVivien Didelot .sw_index = p->dp->ds->index, 60704d3a4c6SVivien Didelot .port = p->dp->index, 60804d3a4c6SVivien Didelot .br = br, 60904d3a4c6SVivien Didelot }; 61004d3a4c6SVivien Didelot int err; 611b73adef6SFlorian Fainelli 6129c265426SVivien Didelot /* Here the port is already unbridged. Reflect the current configuration 6139c265426SVivien Didelot * so that drivers can program their chips accordingly. 6149c265426SVivien Didelot */ 615f123f2fbSVivien Didelot p->dp->bridge_dev = NULL; 616b73adef6SFlorian Fainelli 61704d3a4c6SVivien Didelot err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_LEAVE, &info); 61804d3a4c6SVivien Didelot if (err) 61904d3a4c6SVivien Didelot netdev_err(dev, "failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n"); 620b73adef6SFlorian Fainelli 621b73adef6SFlorian Fainelli /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, 622b73adef6SFlorian Fainelli * so allow it to be in BR_STATE_FORWARDING to be kept functional 623b73adef6SFlorian Fainelli */ 624c5d35cb3SVivien Didelot dsa_slave_set_state(dev, BR_STATE_FORWARDING); 625b73adef6SFlorian Fainelli } 626b73adef6SFlorian Fainelli 627f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev, 628f8e20a9fSScott Feldman struct switchdev_attr *attr) 629b73adef6SFlorian Fainelli { 630b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 631afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 632b73adef6SFlorian Fainelli 633f8e20a9fSScott Feldman switch (attr->id) { 6341f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: 63542275bd8SScott Feldman attr->u.ppid.id_len = sizeof(ds->index); 63642275bd8SScott Feldman memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len); 637f8e20a9fSScott Feldman break; 638f8e20a9fSScott Feldman default: 639f8e20a9fSScott Feldman return -EOPNOTSUPP; 640f8e20a9fSScott Feldman } 641b73adef6SFlorian Fainelli 642b73adef6SFlorian Fainelli return 0; 643b73adef6SFlorian Fainelli } 644b73adef6SFlorian Fainelli 64504ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p, 64604ff53f9SFlorian Fainelli struct sk_buff *skb) 64704ff53f9SFlorian Fainelli { 64804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 64904ff53f9SFlorian Fainelli if (p->netpoll) 65004ff53f9SFlorian Fainelli netpoll_send_skb(p->netpoll, skb); 65104ff53f9SFlorian Fainelli #else 65204ff53f9SFlorian Fainelli BUG(); 65304ff53f9SFlorian Fainelli #endif 65404ff53f9SFlorian Fainelli return NETDEV_TX_OK; 65504ff53f9SFlorian Fainelli } 65604ff53f9SFlorian Fainelli 6573e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 6583e8a72d1SFlorian Fainelli { 6593e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 6604ed70ce9SFlorian Fainelli struct sk_buff *nskb; 6613e8a72d1SFlorian Fainelli 6624ed70ce9SFlorian Fainelli dev->stats.tx_packets++; 6634ed70ce9SFlorian Fainelli dev->stats.tx_bytes += skb->len; 6643e8a72d1SFlorian Fainelli 6654ed70ce9SFlorian Fainelli /* Transmit function may have to reallocate the original SKB */ 6664ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 6674ed70ce9SFlorian Fainelli if (!nskb) 6684ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 6695aed85ceSFlorian Fainelli 67004ff53f9SFlorian Fainelli /* SKB for netpoll still need to be mangled with the protocol-specific 67104ff53f9SFlorian Fainelli * tag to be successfully transmitted 67204ff53f9SFlorian Fainelli */ 67304ff53f9SFlorian Fainelli if (unlikely(netpoll_tx_running(dev))) 67404ff53f9SFlorian Fainelli return dsa_netpoll_send_skb(p, nskb); 67504ff53f9SFlorian Fainelli 6764ed70ce9SFlorian Fainelli /* Queue the SKB for transmission on the parent interface, but 6774ed70ce9SFlorian Fainelli * do not modify its EtherType 6784ed70ce9SFlorian Fainelli */ 679afdcf151SVivien Didelot nskb->dev = p->dp->ds->dst->master_netdev; 6804ed70ce9SFlorian Fainelli dev_queue_xmit(nskb); 6815aed85ceSFlorian Fainelli 6825aed85ceSFlorian Fainelli return NETDEV_TX_OK; 6835aed85ceSFlorian Fainelli } 6845aed85ceSFlorian Fainelli 68591da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 68691da11f8SLennert Buytenhek static int 687bb10bb3eSPhilippe Reynes dsa_slave_get_link_ksettings(struct net_device *dev, 688bb10bb3eSPhilippe Reynes struct ethtool_link_ksettings *cmd) 68991da11f8SLennert Buytenhek { 69091da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 691e69e4626SFlorian Fainelli int err = -EOPNOTSUPP; 69291da11f8SLennert Buytenhek 693e69e4626SFlorian Fainelli if (p->phy != NULL) 694bb10bb3eSPhilippe Reynes err = phy_ethtool_ksettings_get(p->phy, cmd); 69591da11f8SLennert Buytenhek 69691da11f8SLennert Buytenhek return err; 69791da11f8SLennert Buytenhek } 69891da11f8SLennert Buytenhek 69991da11f8SLennert Buytenhek static int 700bb10bb3eSPhilippe Reynes dsa_slave_set_link_ksettings(struct net_device *dev, 701bb10bb3eSPhilippe Reynes const struct ethtool_link_ksettings *cmd) 70291da11f8SLennert Buytenhek { 70391da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 70491da11f8SLennert Buytenhek 70591da11f8SLennert Buytenhek if (p->phy != NULL) 706bb10bb3eSPhilippe Reynes return phy_ethtool_ksettings_set(p->phy, cmd); 70791da11f8SLennert Buytenhek 70891da11f8SLennert Buytenhek return -EOPNOTSUPP; 70991da11f8SLennert Buytenhek } 71091da11f8SLennert Buytenhek 71191da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 71291da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 71391da11f8SLennert Buytenhek { 7147826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 7157826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 7167826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 71791da11f8SLennert Buytenhek } 71891da11f8SLennert Buytenhek 7193d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 7203d762a0fSGuenter Roeck { 7213d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 722afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 7233d762a0fSGuenter Roeck 7249d490b4eSVivien Didelot if (ds->ops->get_regs_len) 725afdcf151SVivien Didelot return ds->ops->get_regs_len(ds, p->dp->index); 7263d762a0fSGuenter Roeck 7273d762a0fSGuenter Roeck return -EOPNOTSUPP; 7283d762a0fSGuenter Roeck } 7293d762a0fSGuenter Roeck 7303d762a0fSGuenter Roeck static void 7313d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 7323d762a0fSGuenter Roeck { 7333d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 734afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 7353d762a0fSGuenter Roeck 7369d490b4eSVivien Didelot if (ds->ops->get_regs) 737afdcf151SVivien Didelot ds->ops->get_regs(ds, p->dp->index, regs, _p); 7383d762a0fSGuenter Roeck } 7393d762a0fSGuenter Roeck 74091da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev) 74191da11f8SLennert Buytenhek { 74291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 74391da11f8SLennert Buytenhek 74491da11f8SLennert Buytenhek if (p->phy != NULL) 74591da11f8SLennert Buytenhek return genphy_restart_aneg(p->phy); 74691da11f8SLennert Buytenhek 74791da11f8SLennert Buytenhek return -EOPNOTSUPP; 74891da11f8SLennert Buytenhek } 74991da11f8SLennert Buytenhek 75091da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev) 75191da11f8SLennert Buytenhek { 75291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 75391da11f8SLennert Buytenhek 75491da11f8SLennert Buytenhek if (p->phy != NULL) { 75591da11f8SLennert Buytenhek genphy_update_link(p->phy); 75691da11f8SLennert Buytenhek return p->phy->link; 75791da11f8SLennert Buytenhek } 75891da11f8SLennert Buytenhek 75991da11f8SLennert Buytenhek return -EOPNOTSUPP; 76091da11f8SLennert Buytenhek } 76191da11f8SLennert Buytenhek 7626793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 7636793abb4SGuenter Roeck { 7646793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 765afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 7666793abb4SGuenter Roeck 7670e576044SAndrew Lunn if (ds->cd && ds->cd->eeprom_len) 768ff04955cSAndrew Lunn return ds->cd->eeprom_len; 7696793abb4SGuenter Roeck 7709d490b4eSVivien Didelot if (ds->ops->get_eeprom_len) 7719d490b4eSVivien Didelot return ds->ops->get_eeprom_len(ds); 7726793abb4SGuenter Roeck 7736793abb4SGuenter Roeck return 0; 7746793abb4SGuenter Roeck } 7756793abb4SGuenter Roeck 7766793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 7776793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 7786793abb4SGuenter Roeck { 7796793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 780afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 7816793abb4SGuenter Roeck 7829d490b4eSVivien Didelot if (ds->ops->get_eeprom) 7839d490b4eSVivien Didelot return ds->ops->get_eeprom(ds, eeprom, data); 7846793abb4SGuenter Roeck 7856793abb4SGuenter Roeck return -EOPNOTSUPP; 7866793abb4SGuenter Roeck } 7876793abb4SGuenter Roeck 7886793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 7896793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 7906793abb4SGuenter Roeck { 7916793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 792afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 7936793abb4SGuenter Roeck 7949d490b4eSVivien Didelot if (ds->ops->set_eeprom) 7959d490b4eSVivien Didelot return ds->ops->set_eeprom(ds, eeprom, data); 7966793abb4SGuenter Roeck 7976793abb4SGuenter Roeck return -EOPNOTSUPP; 7986793abb4SGuenter Roeck } 7996793abb4SGuenter Roeck 80091da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 80191da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 80291da11f8SLennert Buytenhek { 80391da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 804afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 80591da11f8SLennert Buytenhek 80691da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 80791da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 80891da11f8SLennert Buytenhek 80991da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 81091da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 81191da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 81291da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 8139d490b4eSVivien Didelot if (ds->ops->get_strings) 814afdcf151SVivien Didelot ds->ops->get_strings(ds, p->dp->index, data + 4 * len); 81591da11f8SLennert Buytenhek } 81691da11f8SLennert Buytenhek } 81791da11f8SLennert Buytenhek 818badf3adaSFlorian Fainelli static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev, 819badf3adaSFlorian Fainelli struct ethtool_stats *stats, 820badf3adaSFlorian Fainelli uint64_t *data) 821badf3adaSFlorian Fainelli { 822badf3adaSFlorian Fainelli struct dsa_switch_tree *dst = dev->dsa_ptr; 8239520ed8fSVivien Didelot struct dsa_switch *ds = dst->cpu_switch; 824badf3adaSFlorian Fainelli s8 cpu_port = dst->cpu_port; 825badf3adaSFlorian Fainelli int count = 0; 826badf3adaSFlorian Fainelli 827badf3adaSFlorian Fainelli if (dst->master_ethtool_ops.get_sset_count) { 828badf3adaSFlorian Fainelli count = dst->master_ethtool_ops.get_sset_count(dev, 829badf3adaSFlorian Fainelli ETH_SS_STATS); 830badf3adaSFlorian Fainelli dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data); 831badf3adaSFlorian Fainelli } 832badf3adaSFlorian Fainelli 8339d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 8349d490b4eSVivien Didelot ds->ops->get_ethtool_stats(ds, cpu_port, data + count); 835badf3adaSFlorian Fainelli } 836badf3adaSFlorian Fainelli 837badf3adaSFlorian Fainelli static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset) 838badf3adaSFlorian Fainelli { 839badf3adaSFlorian Fainelli struct dsa_switch_tree *dst = dev->dsa_ptr; 8409520ed8fSVivien Didelot struct dsa_switch *ds = dst->cpu_switch; 841badf3adaSFlorian Fainelli int count = 0; 842badf3adaSFlorian Fainelli 843badf3adaSFlorian Fainelli if (dst->master_ethtool_ops.get_sset_count) 844badf3adaSFlorian Fainelli count += dst->master_ethtool_ops.get_sset_count(dev, sset); 845badf3adaSFlorian Fainelli 8469d490b4eSVivien Didelot if (sset == ETH_SS_STATS && ds->ops->get_sset_count) 8479d490b4eSVivien Didelot count += ds->ops->get_sset_count(ds); 848badf3adaSFlorian Fainelli 849badf3adaSFlorian Fainelli return count; 850badf3adaSFlorian Fainelli } 851badf3adaSFlorian Fainelli 852badf3adaSFlorian Fainelli static void dsa_cpu_port_get_strings(struct net_device *dev, 853badf3adaSFlorian Fainelli uint32_t stringset, uint8_t *data) 854badf3adaSFlorian Fainelli { 855badf3adaSFlorian Fainelli struct dsa_switch_tree *dst = dev->dsa_ptr; 8569520ed8fSVivien Didelot struct dsa_switch *ds = dst->cpu_switch; 857badf3adaSFlorian Fainelli s8 cpu_port = dst->cpu_port; 858badf3adaSFlorian Fainelli int len = ETH_GSTRING_LEN; 859badf3adaSFlorian Fainelli int mcount = 0, count; 860badf3adaSFlorian Fainelli unsigned int i; 861badf3adaSFlorian Fainelli uint8_t pfx[4]; 862badf3adaSFlorian Fainelli uint8_t *ndata; 863badf3adaSFlorian Fainelli 864badf3adaSFlorian Fainelli snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port); 865badf3adaSFlorian Fainelli /* We do not want to be NULL-terminated, since this is a prefix */ 866badf3adaSFlorian Fainelli pfx[sizeof(pfx) - 1] = '_'; 867badf3adaSFlorian Fainelli 868badf3adaSFlorian Fainelli if (dst->master_ethtool_ops.get_sset_count) { 869badf3adaSFlorian Fainelli mcount = dst->master_ethtool_ops.get_sset_count(dev, 870badf3adaSFlorian Fainelli ETH_SS_STATS); 871badf3adaSFlorian Fainelli dst->master_ethtool_ops.get_strings(dev, stringset, data); 872badf3adaSFlorian Fainelli } 873badf3adaSFlorian Fainelli 8749d490b4eSVivien Didelot if (stringset == ETH_SS_STATS && ds->ops->get_strings) { 875badf3adaSFlorian Fainelli ndata = data + mcount * len; 876badf3adaSFlorian Fainelli /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle 877badf3adaSFlorian Fainelli * the output after to prepend our CPU port prefix we 878badf3adaSFlorian Fainelli * constructed earlier 879badf3adaSFlorian Fainelli */ 8809d490b4eSVivien Didelot ds->ops->get_strings(ds, cpu_port, ndata); 8819d490b4eSVivien Didelot count = ds->ops->get_sset_count(ds); 882badf3adaSFlorian Fainelli for (i = 0; i < count; i++) { 883badf3adaSFlorian Fainelli memmove(ndata + (i * len + sizeof(pfx)), 884badf3adaSFlorian Fainelli ndata + i * len, len - sizeof(pfx)); 885badf3adaSFlorian Fainelli memcpy(ndata + i * len, pfx, sizeof(pfx)); 886badf3adaSFlorian Fainelli } 887badf3adaSFlorian Fainelli } 888badf3adaSFlorian Fainelli } 889badf3adaSFlorian Fainelli 89091da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 89191da11f8SLennert Buytenhek struct ethtool_stats *stats, 89291da11f8SLennert Buytenhek uint64_t *data) 89391da11f8SLennert Buytenhek { 89491da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 895afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 89691da11f8SLennert Buytenhek 89746e7b8d8SVivien Didelot data[0] = dev->stats.tx_packets; 89846e7b8d8SVivien Didelot data[1] = dev->stats.tx_bytes; 89946e7b8d8SVivien Didelot data[2] = dev->stats.rx_packets; 90046e7b8d8SVivien Didelot data[3] = dev->stats.rx_bytes; 9019d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 902afdcf151SVivien Didelot ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4); 90391da11f8SLennert Buytenhek } 90491da11f8SLennert Buytenhek 90591da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 90691da11f8SLennert Buytenhek { 90791da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 908afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 90991da11f8SLennert Buytenhek 91091da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 91191da11f8SLennert Buytenhek int count; 91291da11f8SLennert Buytenhek 91391da11f8SLennert Buytenhek count = 4; 9149d490b4eSVivien Didelot if (ds->ops->get_sset_count) 9159d490b4eSVivien Didelot count += ds->ops->get_sset_count(ds); 91691da11f8SLennert Buytenhek 91791da11f8SLennert Buytenhek return count; 91891da11f8SLennert Buytenhek } 91991da11f8SLennert Buytenhek 92091da11f8SLennert Buytenhek return -EOPNOTSUPP; 92191da11f8SLennert Buytenhek } 92291da11f8SLennert Buytenhek 92319e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 92419e57c4eSFlorian Fainelli { 92519e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 926afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 92719e57c4eSFlorian Fainelli 9289d490b4eSVivien Didelot if (ds->ops->get_wol) 929afdcf151SVivien Didelot ds->ops->get_wol(ds, p->dp->index, w); 93019e57c4eSFlorian Fainelli } 93119e57c4eSFlorian Fainelli 93219e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 93319e57c4eSFlorian Fainelli { 93419e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 935afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 93619e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 93719e57c4eSFlorian Fainelli 9389d490b4eSVivien Didelot if (ds->ops->set_wol) 939afdcf151SVivien Didelot ret = ds->ops->set_wol(ds, p->dp->index, w); 94019e57c4eSFlorian Fainelli 94119e57c4eSFlorian Fainelli return ret; 94219e57c4eSFlorian Fainelli } 94319e57c4eSFlorian Fainelli 9447905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 9457905288fSFlorian Fainelli { 9467905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 947afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 9487905288fSFlorian Fainelli int ret; 9497905288fSFlorian Fainelli 9509d490b4eSVivien Didelot if (!ds->ops->set_eee) 9517905288fSFlorian Fainelli return -EOPNOTSUPP; 9527905288fSFlorian Fainelli 953afdcf151SVivien Didelot ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e); 9547905288fSFlorian Fainelli if (ret) 9557905288fSFlorian Fainelli return ret; 9567905288fSFlorian Fainelli 9577905288fSFlorian Fainelli if (p->phy) 9587905288fSFlorian Fainelli ret = phy_ethtool_set_eee(p->phy, e); 9597905288fSFlorian Fainelli 9607905288fSFlorian Fainelli return ret; 9617905288fSFlorian Fainelli } 9627905288fSFlorian Fainelli 9637905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 9647905288fSFlorian Fainelli { 9657905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 966afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 9677905288fSFlorian Fainelli int ret; 9687905288fSFlorian Fainelli 9699d490b4eSVivien Didelot if (!ds->ops->get_eee) 9707905288fSFlorian Fainelli return -EOPNOTSUPP; 9717905288fSFlorian Fainelli 972afdcf151SVivien Didelot ret = ds->ops->get_eee(ds, p->dp->index, e); 9737905288fSFlorian Fainelli if (ret) 9747905288fSFlorian Fainelli return ret; 9757905288fSFlorian Fainelli 9767905288fSFlorian Fainelli if (p->phy) 9777905288fSFlorian Fainelli ret = phy_ethtool_get_eee(p->phy, e); 9787905288fSFlorian Fainelli 9797905288fSFlorian Fainelli return ret; 9807905288fSFlorian Fainelli } 9817905288fSFlorian Fainelli 98204ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 98304ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 98404ff53f9SFlorian Fainelli struct netpoll_info *ni) 98504ff53f9SFlorian Fainelli { 98604ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 987afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 98804ff53f9SFlorian Fainelli struct net_device *master = ds->dst->master_netdev; 98904ff53f9SFlorian Fainelli struct netpoll *netpoll; 99004ff53f9SFlorian Fainelli int err = 0; 99104ff53f9SFlorian Fainelli 99204ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 99304ff53f9SFlorian Fainelli if (!netpoll) 99404ff53f9SFlorian Fainelli return -ENOMEM; 99504ff53f9SFlorian Fainelli 99604ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 99704ff53f9SFlorian Fainelli if (err) { 99804ff53f9SFlorian Fainelli kfree(netpoll); 99904ff53f9SFlorian Fainelli goto out; 100004ff53f9SFlorian Fainelli } 100104ff53f9SFlorian Fainelli 100204ff53f9SFlorian Fainelli p->netpoll = netpoll; 100304ff53f9SFlorian Fainelli out: 100404ff53f9SFlorian Fainelli return err; 100504ff53f9SFlorian Fainelli } 100604ff53f9SFlorian Fainelli 100704ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 100804ff53f9SFlorian Fainelli { 100904ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 101004ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 101104ff53f9SFlorian Fainelli 101204ff53f9SFlorian Fainelli if (!netpoll) 101304ff53f9SFlorian Fainelli return; 101404ff53f9SFlorian Fainelli 101504ff53f9SFlorian Fainelli p->netpoll = NULL; 101604ff53f9SFlorian Fainelli 101704ff53f9SFlorian Fainelli __netpoll_free_async(netpoll); 101804ff53f9SFlorian Fainelli } 101904ff53f9SFlorian Fainelli 102004ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 102104ff53f9SFlorian Fainelli { 102204ff53f9SFlorian Fainelli } 102304ff53f9SFlorian Fainelli #endif 102404ff53f9SFlorian Fainelli 102544bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev, 102644bb765cSFlorian Fainelli char *name, size_t len) 102744bb765cSFlorian Fainelli { 102844bb765cSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 102944bb765cSFlorian Fainelli 1030afdcf151SVivien Didelot if (snprintf(name, len, "p%d", p->dp->index) >= len) 103144bb765cSFlorian Fainelli return -EINVAL; 10323a543ef4SFlorian Fainelli 10333a543ef4SFlorian Fainelli return 0; 10343a543ef4SFlorian Fainelli } 10353a543ef4SFlorian Fainelli 1036f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry * 1037f50f2127SFlorian Fainelli dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p, 1038f50f2127SFlorian Fainelli unsigned long cookie) 1039f50f2127SFlorian Fainelli { 1040f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1041f50f2127SFlorian Fainelli 1042f50f2127SFlorian Fainelli list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 1043f50f2127SFlorian Fainelli if (mall_tc_entry->cookie == cookie) 1044f50f2127SFlorian Fainelli return mall_tc_entry; 1045f50f2127SFlorian Fainelli 1046f50f2127SFlorian Fainelli return NULL; 1047f50f2127SFlorian Fainelli } 1048f50f2127SFlorian Fainelli 1049f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev, 1050f50f2127SFlorian Fainelli __be16 protocol, 1051f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls, 1052f50f2127SFlorian Fainelli bool ingress) 1053f50f2127SFlorian Fainelli { 1054f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 1055f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1056f50f2127SFlorian Fainelli struct dsa_switch *ds = p->dp->ds; 1057f50f2127SFlorian Fainelli struct net *net = dev_net(dev); 1058f50f2127SFlorian Fainelli struct dsa_slave_priv *to_p; 1059f50f2127SFlorian Fainelli struct net_device *to_dev; 1060f50f2127SFlorian Fainelli const struct tc_action *a; 1061f50f2127SFlorian Fainelli int err = -EOPNOTSUPP; 1062f50f2127SFlorian Fainelli LIST_HEAD(actions); 1063f50f2127SFlorian Fainelli int ifindex; 1064f50f2127SFlorian Fainelli 1065f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_add) 1066f50f2127SFlorian Fainelli return err; 1067f50f2127SFlorian Fainelli 1068f50f2127SFlorian Fainelli if (!tc_single_action(cls->exts)) 1069f50f2127SFlorian Fainelli return err; 1070f50f2127SFlorian Fainelli 1071f50f2127SFlorian Fainelli tcf_exts_to_list(cls->exts, &actions); 1072f50f2127SFlorian Fainelli a = list_first_entry(&actions, struct tc_action, list); 1073f50f2127SFlorian Fainelli 1074f50f2127SFlorian Fainelli if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) { 1075f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror; 1076f50f2127SFlorian Fainelli 1077f50f2127SFlorian Fainelli ifindex = tcf_mirred_ifindex(a); 1078f50f2127SFlorian Fainelli to_dev = __dev_get_by_index(net, ifindex); 1079f50f2127SFlorian Fainelli if (!to_dev) 1080f50f2127SFlorian Fainelli return -EINVAL; 1081f50f2127SFlorian Fainelli 1082f50f2127SFlorian Fainelli if (!dsa_slave_dev_check(to_dev)) 1083f50f2127SFlorian Fainelli return -EOPNOTSUPP; 1084f50f2127SFlorian Fainelli 1085f50f2127SFlorian Fainelli mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1086f50f2127SFlorian Fainelli if (!mall_tc_entry) 1087f50f2127SFlorian Fainelli return -ENOMEM; 1088f50f2127SFlorian Fainelli 1089f50f2127SFlorian Fainelli mall_tc_entry->cookie = cls->cookie; 1090f50f2127SFlorian Fainelli mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 1091f50f2127SFlorian Fainelli mirror = &mall_tc_entry->mirror; 1092f50f2127SFlorian Fainelli 1093f50f2127SFlorian Fainelli to_p = netdev_priv(to_dev); 1094f50f2127SFlorian Fainelli 1095f50f2127SFlorian Fainelli mirror->to_local_port = to_p->dp->index; 1096f50f2127SFlorian Fainelli mirror->ingress = ingress; 1097f50f2127SFlorian Fainelli 1098f50f2127SFlorian Fainelli err = ds->ops->port_mirror_add(ds, p->dp->index, mirror, 1099f50f2127SFlorian Fainelli ingress); 1100f50f2127SFlorian Fainelli if (err) { 1101f50f2127SFlorian Fainelli kfree(mall_tc_entry); 1102f50f2127SFlorian Fainelli return err; 1103f50f2127SFlorian Fainelli } 1104f50f2127SFlorian Fainelli 1105f50f2127SFlorian Fainelli list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1106f50f2127SFlorian Fainelli } 1107f50f2127SFlorian Fainelli 1108f50f2127SFlorian Fainelli return 0; 1109f50f2127SFlorian Fainelli } 1110f50f2127SFlorian Fainelli 1111f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev, 1112f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls) 1113f50f2127SFlorian Fainelli { 1114f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 1115f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1116f50f2127SFlorian Fainelli struct dsa_switch *ds = p->dp->ds; 1117f50f2127SFlorian Fainelli 1118f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_del) 1119f50f2127SFlorian Fainelli return; 1120f50f2127SFlorian Fainelli 1121f50f2127SFlorian Fainelli mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie); 1122f50f2127SFlorian Fainelli if (!mall_tc_entry) 1123f50f2127SFlorian Fainelli return; 1124f50f2127SFlorian Fainelli 1125f50f2127SFlorian Fainelli list_del(&mall_tc_entry->list); 1126f50f2127SFlorian Fainelli 1127f50f2127SFlorian Fainelli switch (mall_tc_entry->type) { 1128f50f2127SFlorian Fainelli case DSA_PORT_MALL_MIRROR: 1129f50f2127SFlorian Fainelli ds->ops->port_mirror_del(ds, p->dp->index, 1130f50f2127SFlorian Fainelli &mall_tc_entry->mirror); 1131f50f2127SFlorian Fainelli break; 1132f50f2127SFlorian Fainelli default: 1133f50f2127SFlorian Fainelli WARN_ON(1); 1134f50f2127SFlorian Fainelli } 1135f50f2127SFlorian Fainelli 1136f50f2127SFlorian Fainelli kfree(mall_tc_entry); 1137f50f2127SFlorian Fainelli } 1138f50f2127SFlorian Fainelli 1139f50f2127SFlorian Fainelli static int dsa_slave_setup_tc(struct net_device *dev, u32 handle, 1140f50f2127SFlorian Fainelli __be16 protocol, struct tc_to_netdev *tc) 1141f50f2127SFlorian Fainelli { 1142f50f2127SFlorian Fainelli bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS); 1143f50f2127SFlorian Fainelli int ret = -EOPNOTSUPP; 1144f50f2127SFlorian Fainelli 1145f50f2127SFlorian Fainelli switch (tc->type) { 1146f50f2127SFlorian Fainelli case TC_SETUP_MATCHALL: 1147f50f2127SFlorian Fainelli switch (tc->cls_mall->command) { 1148f50f2127SFlorian Fainelli case TC_CLSMATCHALL_REPLACE: 1149f50f2127SFlorian Fainelli return dsa_slave_add_cls_matchall(dev, protocol, 1150f50f2127SFlorian Fainelli tc->cls_mall, 1151f50f2127SFlorian Fainelli ingress); 1152f50f2127SFlorian Fainelli case TC_CLSMATCHALL_DESTROY: 1153f50f2127SFlorian Fainelli dsa_slave_del_cls_matchall(dev, tc->cls_mall); 1154f50f2127SFlorian Fainelli return 0; 1155f50f2127SFlorian Fainelli } 1156f50f2127SFlorian Fainelli default: 1157f50f2127SFlorian Fainelli break; 1158f50f2127SFlorian Fainelli } 1159f50f2127SFlorian Fainelli 1160f50f2127SFlorian Fainelli return ret; 1161f50f2127SFlorian Fainelli } 1162f50f2127SFlorian Fainelli 1163af42192cSFlorian Fainelli void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops) 1164af42192cSFlorian Fainelli { 1165af42192cSFlorian Fainelli ops->get_sset_count = dsa_cpu_port_get_sset_count; 1166af42192cSFlorian Fainelli ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats; 1167af42192cSFlorian Fainelli ops->get_strings = dsa_cpu_port_get_strings; 1168af42192cSFlorian Fainelli } 1169af42192cSFlorian Fainelli 1170bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev, 1171bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs) 1172bf9f2648SFlorian Fainelli { 1173bf9f2648SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 1174bf9f2648SFlorian Fainelli struct dsa_switch *ds = p->dp->ds; 1175bf9f2648SFlorian Fainelli 1176bf9f2648SFlorian Fainelli if (!ds->ops->get_rxnfc) 1177bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1178bf9f2648SFlorian Fainelli 1179bf9f2648SFlorian Fainelli return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs); 1180bf9f2648SFlorian Fainelli } 1181bf9f2648SFlorian Fainelli 1182bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev, 1183bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc) 1184bf9f2648SFlorian Fainelli { 1185bf9f2648SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 1186bf9f2648SFlorian Fainelli struct dsa_switch *ds = p->dp->ds; 1187bf9f2648SFlorian Fainelli 1188bf9f2648SFlorian Fainelli if (!ds->ops->set_rxnfc) 1189bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1190bf9f2648SFlorian Fainelli 1191bf9f2648SFlorian Fainelli return ds->ops->set_rxnfc(ds, p->dp->index, nfc); 1192bf9f2648SFlorian Fainelli } 1193bf9f2648SFlorian Fainelli 119491da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 119591da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 11963d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 11973d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 119891da11f8SLennert Buytenhek .nway_reset = dsa_slave_nway_reset, 119991da11f8SLennert Buytenhek .get_link = dsa_slave_get_link, 12006793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 12016793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 12026793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 120391da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 120491da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 120591da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 120619e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 120719e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 12087905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 12097905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 1210bb10bb3eSPhilippe Reynes .get_link_ksettings = dsa_slave_get_link_ksettings, 1211bb10bb3eSPhilippe Reynes .set_link_ksettings = dsa_slave_set_link_ksettings, 1212bf9f2648SFlorian Fainelli .get_rxnfc = dsa_slave_get_rxnfc, 1213bf9f2648SFlorian Fainelli .set_rxnfc = dsa_slave_set_rxnfc, 121491da11f8SLennert Buytenhek }; 121591da11f8SLennert Buytenhek 12163e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 1217d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 1218d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 12193e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 1220d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 1221d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 1222d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 1223ba14d9ebSVivien Didelot .ndo_fdb_add = switchdev_port_fdb_add, 1224ba14d9ebSVivien Didelot .ndo_fdb_del = switchdev_port_fdb_del, 1225ba14d9ebSVivien Didelot .ndo_fdb_dump = switchdev_port_fdb_dump, 1226d442ad4aSStephen Hemminger .ndo_do_ioctl = dsa_slave_ioctl, 1227abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 122804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 122904ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 123004ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 123104ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 123204ff53f9SFlorian Fainelli #endif 123311149536SVivien Didelot .ndo_bridge_getlink = switchdev_port_bridge_getlink, 123411149536SVivien Didelot .ndo_bridge_setlink = switchdev_port_bridge_setlink, 123511149536SVivien Didelot .ndo_bridge_dellink = switchdev_port_bridge_dellink, 123644bb765cSFlorian Fainelli .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, 1237f50f2127SFlorian Fainelli .ndo_setup_tc = dsa_slave_setup_tc, 123898237d43SScott Feldman }; 123998237d43SScott Feldman 12409d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = { 1241f8e20a9fSScott Feldman .switchdev_port_attr_get = dsa_slave_port_attr_get, 124235636062SScott Feldman .switchdev_port_attr_set = dsa_slave_port_attr_set, 1243ba14d9ebSVivien Didelot .switchdev_port_obj_add = dsa_slave_port_obj_add, 1244ba14d9ebSVivien Didelot .switchdev_port_obj_del = dsa_slave_port_obj_del, 1245ba14d9ebSVivien Didelot .switchdev_port_obj_dump = dsa_slave_port_obj_dump, 1246d442ad4aSStephen Hemminger }; 124791da11f8SLennert Buytenhek 1248f37db85dSFlorian Fainelli static struct device_type dsa_type = { 1249f37db85dSFlorian Fainelli .name = "dsa", 1250f37db85dSFlorian Fainelli }; 1251f37db85dSFlorian Fainelli 12520d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev) 12530d8bcdd3SFlorian Fainelli { 12540d8bcdd3SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 1255afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 12560d8bcdd3SFlorian Fainelli unsigned int status_changed = 0; 12570d8bcdd3SFlorian Fainelli 12580d8bcdd3SFlorian Fainelli if (p->old_link != p->phy->link) { 12590d8bcdd3SFlorian Fainelli status_changed = 1; 12600d8bcdd3SFlorian Fainelli p->old_link = p->phy->link; 12610d8bcdd3SFlorian Fainelli } 12620d8bcdd3SFlorian Fainelli 12630d8bcdd3SFlorian Fainelli if (p->old_duplex != p->phy->duplex) { 12640d8bcdd3SFlorian Fainelli status_changed = 1; 12650d8bcdd3SFlorian Fainelli p->old_duplex = p->phy->duplex; 12660d8bcdd3SFlorian Fainelli } 12670d8bcdd3SFlorian Fainelli 12680d8bcdd3SFlorian Fainelli if (p->old_pause != p->phy->pause) { 12690d8bcdd3SFlorian Fainelli status_changed = 1; 12700d8bcdd3SFlorian Fainelli p->old_pause = p->phy->pause; 12710d8bcdd3SFlorian Fainelli } 12720d8bcdd3SFlorian Fainelli 12739d490b4eSVivien Didelot if (ds->ops->adjust_link && status_changed) 1274afdcf151SVivien Didelot ds->ops->adjust_link(ds, p->dp->index, p->phy); 1275ec9436baSFlorian Fainelli 12760d8bcdd3SFlorian Fainelli if (status_changed) 12770d8bcdd3SFlorian Fainelli phy_print_status(p->phy); 12780d8bcdd3SFlorian Fainelli } 12790d8bcdd3SFlorian Fainelli 1280ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev, 1281ce31b31cSFlorian Fainelli struct fixed_phy_status *status) 1282ce31b31cSFlorian Fainelli { 1283b71be352SAndrew Lunn struct dsa_slave_priv *p; 1284b71be352SAndrew Lunn struct dsa_switch *ds; 1285ce31b31cSFlorian Fainelli 1286b71be352SAndrew Lunn if (dev) { 1287b71be352SAndrew Lunn p = netdev_priv(dev); 1288afdcf151SVivien Didelot ds = p->dp->ds; 12899d490b4eSVivien Didelot if (ds->ops->fixed_link_update) 1290afdcf151SVivien Didelot ds->ops->fixed_link_update(ds, p->dp->index, status); 1291b71be352SAndrew Lunn } 1292ce31b31cSFlorian Fainelli 1293ce31b31cSFlorian Fainelli return 0; 1294ce31b31cSFlorian Fainelli } 1295ce31b31cSFlorian Fainelli 129691da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 1297c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p, 1298cd28a1a9SFlorian Fainelli struct net_device *slave_dev, 1299cd28a1a9SFlorian Fainelli int addr) 1300c305c165SFlorian Fainelli { 1301afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 1302c305c165SFlorian Fainelli 13037f854420SAndrew Lunn p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr); 1304d25b8e74SRussell King if (!p->phy) { 1305d25b8e74SRussell King netdev_err(slave_dev, "no phy at %d\n", addr); 1306c305c165SFlorian Fainelli return -ENODEV; 1307d25b8e74SRussell King } 1308c305c165SFlorian Fainelli 1309c305c165SFlorian Fainelli /* Use already configured phy mode */ 1310211c504aSFlorian Fainelli if (p->phy_interface == PHY_INTERFACE_MODE_NA) 1311c305c165SFlorian Fainelli p->phy_interface = p->phy->interface; 13124078b76cSFlorian Fainelli return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, 1313c305c165SFlorian Fainelli p->phy_interface); 1314c305c165SFlorian Fainelli } 1315c305c165SFlorian Fainelli 13169697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p, 13170d8bcdd3SFlorian Fainelli struct net_device *slave_dev) 13180d8bcdd3SFlorian Fainelli { 1319afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 13200d8bcdd3SFlorian Fainelli struct device_node *phy_dn, *port_dn; 1321ce31b31cSFlorian Fainelli bool phy_is_fixed = false; 13226819563eSFlorian Fainelli u32 phy_flags = 0; 132319334920SGuenter Roeck int mode, ret; 13240d8bcdd3SFlorian Fainelli 1325afdcf151SVivien Didelot port_dn = p->dp->dn; 132619334920SGuenter Roeck mode = of_get_phy_mode(port_dn); 132719334920SGuenter Roeck if (mode < 0) 132819334920SGuenter Roeck mode = PHY_INTERFACE_MODE_NA; 132919334920SGuenter Roeck p->phy_interface = mode; 13300d8bcdd3SFlorian Fainelli 13310d8bcdd3SFlorian Fainelli phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); 13320d8f3c67SJohan Hovold if (!phy_dn && of_phy_is_fixed_link(port_dn)) { 13330d8bcdd3SFlorian Fainelli /* In the case of a fixed PHY, the DT node associated 13340d8bcdd3SFlorian Fainelli * to the fixed PHY is the Port DT node 13350d8bcdd3SFlorian Fainelli */ 13360d8bcdd3SFlorian Fainelli ret = of_phy_register_fixed_link(port_dn); 13370d8bcdd3SFlorian Fainelli if (ret) { 1338d25b8e74SRussell King netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret); 13399697f1cdSFlorian Fainelli return ret; 13400d8bcdd3SFlorian Fainelli } 1341ce31b31cSFlorian Fainelli phy_is_fixed = true; 13420d8f3c67SJohan Hovold phy_dn = of_node_get(port_dn); 13430d8bcdd3SFlorian Fainelli } 13440d8bcdd3SFlorian Fainelli 13459d490b4eSVivien Didelot if (ds->ops->get_phy_flags) 1346afdcf151SVivien Didelot phy_flags = ds->ops->get_phy_flags(ds, p->dp->index); 13476819563eSFlorian Fainelli 1348cd28a1a9SFlorian Fainelli if (phy_dn) { 1349d25b8e74SRussell King int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn); 1350d25b8e74SRussell King 1351cd28a1a9SFlorian Fainelli /* If this PHY address is part of phys_mii_mask, which means 1352cd28a1a9SFlorian Fainelli * that we need to divert reads and writes to/from it, then we 1353cd28a1a9SFlorian Fainelli * want to bind this device using the slave MII bus created by 1354cd28a1a9SFlorian Fainelli * DSA to make that happen. 1355cd28a1a9SFlorian Fainelli */ 1356d25b8e74SRussell King if (!phy_is_fixed && phy_id >= 0 && 1357d25b8e74SRussell King (ds->phys_mii_mask & (1 << phy_id))) { 1358d25b8e74SRussell King ret = dsa_slave_phy_connect(p, slave_dev, phy_id); 1359d25b8e74SRussell King if (ret) { 1360d25b8e74SRussell King netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret); 13610d8f3c67SJohan Hovold of_node_put(phy_dn); 1362cd28a1a9SFlorian Fainelli return ret; 1363d25b8e74SRussell King } 1364cd28a1a9SFlorian Fainelli } else { 13650d8bcdd3SFlorian Fainelli p->phy = of_phy_connect(slave_dev, phy_dn, 1366cd28a1a9SFlorian Fainelli dsa_slave_adjust_link, 1367cd28a1a9SFlorian Fainelli phy_flags, 13680d8bcdd3SFlorian Fainelli p->phy_interface); 1369cd28a1a9SFlorian Fainelli } 13700d8f3c67SJohan Hovold 13710d8f3c67SJohan Hovold of_node_put(phy_dn); 1372cd28a1a9SFlorian Fainelli } 13730d8bcdd3SFlorian Fainelli 1374ce31b31cSFlorian Fainelli if (p->phy && phy_is_fixed) 1375ce31b31cSFlorian Fainelli fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); 1376ce31b31cSFlorian Fainelli 13770d8bcdd3SFlorian Fainelli /* We could not connect to a designated PHY, so use the switch internal 13780d8bcdd3SFlorian Fainelli * MDIO bus instead 13790d8bcdd3SFlorian Fainelli */ 1380b31f65fbSAndrew Lunn if (!p->phy) { 1381afdcf151SVivien Didelot ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index); 1382d25b8e74SRussell King if (ret) { 1383afdcf151SVivien Didelot netdev_err(slave_dev, "failed to connect to port %d: %d\n", 1384afdcf151SVivien Didelot p->dp->index, ret); 1385881eadabSJohan Hovold if (phy_is_fixed) 1386881eadabSJohan Hovold of_phy_deregister_fixed_link(port_dn); 1387c305c165SFlorian Fainelli return ret; 1388d25b8e74SRussell King } 13890d8bcdd3SFlorian Fainelli } 13909697f1cdSFlorian Fainelli 13912220943aSAndrew Lunn phy_attached_info(p->phy); 13922220943aSAndrew Lunn 13939697f1cdSFlorian Fainelli return 0; 1394b31f65fbSAndrew Lunn } 13950d8bcdd3SFlorian Fainelli 1396448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 1397448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 1398448b4482SAndrew Lunn struct netdev_queue *txq, 1399448b4482SAndrew Lunn void *_unused) 1400448b4482SAndrew Lunn { 1401448b4482SAndrew Lunn lockdep_set_class(&txq->_xmit_lock, 1402448b4482SAndrew Lunn &dsa_slave_netdev_xmit_lock_key); 1403448b4482SAndrew Lunn } 1404448b4482SAndrew Lunn 140524462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 140624462549SFlorian Fainelli { 140724462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 140824462549SFlorian Fainelli 1409f154be24SFlorian Fainelli netif_device_detach(slave_dev); 1410f154be24SFlorian Fainelli 141124462549SFlorian Fainelli if (p->phy) { 141224462549SFlorian Fainelli phy_stop(p->phy); 141324462549SFlorian Fainelli p->old_pause = -1; 141424462549SFlorian Fainelli p->old_link = -1; 141524462549SFlorian Fainelli p->old_duplex = -1; 141624462549SFlorian Fainelli phy_suspend(p->phy); 141724462549SFlorian Fainelli } 141824462549SFlorian Fainelli 141924462549SFlorian Fainelli return 0; 142024462549SFlorian Fainelli } 142124462549SFlorian Fainelli 142224462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 142324462549SFlorian Fainelli { 142424462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 142524462549SFlorian Fainelli 142624462549SFlorian Fainelli netif_device_attach(slave_dev); 142724462549SFlorian Fainelli 142824462549SFlorian Fainelli if (p->phy) { 142924462549SFlorian Fainelli phy_resume(p->phy); 143024462549SFlorian Fainelli phy_start(p->phy); 143124462549SFlorian Fainelli } 143224462549SFlorian Fainelli 143324462549SFlorian Fainelli return 0; 143424462549SFlorian Fainelli } 143524462549SFlorian Fainelli 1436d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent, 143783c0afaeSAndrew Lunn int port, const char *name) 143891da11f8SLennert Buytenhek { 1439badf3adaSFlorian Fainelli struct dsa_switch_tree *dst = ds->dst; 144083c0afaeSAndrew Lunn struct net_device *master; 144191da11f8SLennert Buytenhek struct net_device *slave_dev; 144291da11f8SLennert Buytenhek struct dsa_slave_priv *p; 144391da11f8SLennert Buytenhek int ret; 144491da11f8SLennert Buytenhek 144583c0afaeSAndrew Lunn master = ds->dst->master_netdev; 144683c0afaeSAndrew Lunn if (ds->master_netdev) 144783c0afaeSAndrew Lunn master = ds->master_netdev; 144883c0afaeSAndrew Lunn 1449c835a677STom Gundersen slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, 1450c835a677STom Gundersen NET_NAME_UNKNOWN, ether_setup); 145191da11f8SLennert Buytenhek if (slave_dev == NULL) 1452d87d6f44SGuenter Roeck return -ENOMEM; 145391da11f8SLennert Buytenhek 1454f50f2127SFlorian Fainelli slave_dev->features = master->vlan_features | NETIF_F_HW_TC; 1455f50f2127SFlorian Fainelli slave_dev->hw_features |= NETIF_F_HW_TC; 14567ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 14572fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 14580a5f107bSPhil Sutter slave_dev->priv_flags |= IFF_NO_QUEUE; 14593e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 14609d47c0a2SJiri Pirko slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; 14618b1efc0fSJarod Wilson slave_dev->min_mtu = 0; 14628b1efc0fSJarod Wilson slave_dev->max_mtu = ETH_MAX_MTU; 1463f37db85dSFlorian Fainelli SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 1464d442ad4aSStephen Hemminger 1465448b4482SAndrew Lunn netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 1466448b4482SAndrew Lunn NULL); 1467448b4482SAndrew Lunn 146891da11f8SLennert Buytenhek SET_NETDEV_DEV(slave_dev, parent); 1469189b0d93SAndrew Lunn slave_dev->dev.of_node = ds->ports[port].dn; 147091da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 147191da11f8SLennert Buytenhek 147291da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 1473afdcf151SVivien Didelot p->dp = &ds->ports[port]; 1474f50f2127SFlorian Fainelli INIT_LIST_HEAD(&p->mall_tc_list); 147539a7f2a4SAndrew Lunn p->xmit = dst->tag_ops->xmit; 14765075314eSAlexander Duyck 14770d8bcdd3SFlorian Fainelli p->old_pause = -1; 14780d8bcdd3SFlorian Fainelli p->old_link = -1; 14790d8bcdd3SFlorian Fainelli p->old_duplex = -1; 14800d8bcdd3SFlorian Fainelli 1481c8b09808SAndrew Lunn ds->ports[port].netdev = slave_dev; 148291da11f8SLennert Buytenhek ret = register_netdev(slave_dev); 148391da11f8SLennert Buytenhek if (ret) { 1484a2ae6007SJoe Perches netdev_err(master, "error %d registering interface %s\n", 1485a2ae6007SJoe Perches ret, slave_dev->name); 1486c8b09808SAndrew Lunn ds->ports[port].netdev = NULL; 148791da11f8SLennert Buytenhek free_netdev(slave_dev); 1488d87d6f44SGuenter Roeck return ret; 148991da11f8SLennert Buytenhek } 149091da11f8SLennert Buytenhek 149191da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 149291da11f8SLennert Buytenhek 14930071f56eSAndrew Lunn ret = dsa_slave_phy_setup(p, slave_dev); 14940071f56eSAndrew Lunn if (ret) { 14950071f56eSAndrew Lunn netdev_err(master, "error %d setting up slave phy\n", ret); 149673dcb556SFlorian Fainelli unregister_netdev(slave_dev); 14970071f56eSAndrew Lunn free_netdev(slave_dev); 14980071f56eSAndrew Lunn return ret; 14990071f56eSAndrew Lunn } 15000071f56eSAndrew Lunn 1501d87d6f44SGuenter Roeck return 0; 150291da11f8SLennert Buytenhek } 1503b73adef6SFlorian Fainelli 1504cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev) 1505cda5c15bSNeil Armstrong { 1506cda5c15bSNeil Armstrong struct dsa_slave_priv *p = netdev_priv(slave_dev); 1507881eadabSJohan Hovold struct device_node *port_dn; 1508881eadabSJohan Hovold 1509afdcf151SVivien Didelot port_dn = p->dp->dn; 1510cda5c15bSNeil Armstrong 1511cda5c15bSNeil Armstrong netif_carrier_off(slave_dev); 1512881eadabSJohan Hovold if (p->phy) { 1513cda5c15bSNeil Armstrong phy_disconnect(p->phy); 1514881eadabSJohan Hovold 1515881eadabSJohan Hovold if (of_phy_is_fixed_link(port_dn)) 1516881eadabSJohan Hovold of_phy_deregister_fixed_link(port_dn); 1517881eadabSJohan Hovold } 1518cda5c15bSNeil Armstrong unregister_netdev(slave_dev); 1519cda5c15bSNeil Armstrong free_netdev(slave_dev); 1520cda5c15bSNeil Armstrong } 1521cda5c15bSNeil Armstrong 1522b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev) 1523b73adef6SFlorian Fainelli { 1524b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 1525b73adef6SFlorian Fainelli } 1526b73adef6SFlorian Fainelli 15278e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev, 15288e92ab3aSVivien Didelot struct netdev_notifier_changeupper_info *info) 1529b73adef6SFlorian Fainelli { 15308e92ab3aSVivien Didelot int err = NOTIFY_DONE; 1531b73adef6SFlorian Fainelli 15328e92ab3aSVivien Didelot if (netif_is_bridge_master(info->upper_dev)) { 15338e92ab3aSVivien Didelot if (info->linking) { 15348e92ab3aSVivien Didelot err = dsa_slave_bridge_port_join(dev, info->upper_dev); 15358e92ab3aSVivien Didelot err = notifier_from_errno(err); 15368e92ab3aSVivien Didelot } else { 15378e92ab3aSVivien Didelot dsa_slave_bridge_port_leave(dev, info->upper_dev); 15388e92ab3aSVivien Didelot err = NOTIFY_OK; 15398e92ab3aSVivien Didelot } 15406debb68aSVivien Didelot } 1541b73adef6SFlorian Fainelli 15428e92ab3aSVivien Didelot return err; 1543b73adef6SFlorian Fainelli } 1544b73adef6SFlorian Fainelli 154588e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb, 1546b73adef6SFlorian Fainelli unsigned long event, void *ptr) 1547b73adef6SFlorian Fainelli { 15486debb68aSVivien Didelot struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1549b73adef6SFlorian Fainelli 15508e92ab3aSVivien Didelot if (dev->netdev_ops != &dsa_slave_netdev_ops) 15518e92ab3aSVivien Didelot return NOTIFY_DONE; 15528e92ab3aSVivien Didelot 15538e92ab3aSVivien Didelot if (event == NETDEV_CHANGEUPPER) 15548e92ab3aSVivien Didelot return dsa_slave_changeupper(dev, ptr); 1555b73adef6SFlorian Fainelli 1556b73adef6SFlorian Fainelli return NOTIFY_DONE; 1557b73adef6SFlorian Fainelli } 155888e4f0caSVivien Didelot 155988e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = { 156088e4f0caSVivien Didelot .notifier_call = dsa_slave_netdevice_event, 156188e4f0caSVivien Didelot }; 156288e4f0caSVivien Didelot 156388e4f0caSVivien Didelot int dsa_slave_register_notifier(void) 156488e4f0caSVivien Didelot { 156588e4f0caSVivien Didelot return register_netdevice_notifier(&dsa_slave_nb); 156688e4f0caSVivien Didelot } 156788e4f0caSVivien Didelot 156888e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void) 156988e4f0caSVivien Didelot { 157088e4f0caSVivien Didelot int err; 157188e4f0caSVivien Didelot 157288e4f0caSVivien Didelot err = unregister_netdevice_notifier(&dsa_slave_nb); 157388e4f0caSVivien Didelot if (err) 157488e4f0caSVivien Didelot pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 157588e4f0caSVivien Didelot } 1576