Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/slave.c - Slave device handling |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/list.h> |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 12 | #include <linux/etherdevice.h> |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 13 | #include <linux/netdevice.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 14 | #include <linux/phy.h> |
Florian Fainelli | a282054 | 2014-10-17 16:02:13 -0700 | [diff] [blame] | 15 | #include <linux/phy_fixed.h> |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 16 | #include <linux/of_net.h> |
| 17 | #include <linux/of_mdio.h> |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 18 | #include <linux/mdio.h> |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 19 | #include <linux/list.h> |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 20 | #include <net/rtnetlink.h> |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 21 | #include <net/pkt_cls.h> |
| 22 | #include <net/tc_act/tc_mirred.h> |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 23 | #include <linux/if_bridge.h> |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 24 | #include <linux/netpoll.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 25 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 26 | #include "dsa_priv.h" |
| 27 | |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 28 | static bool dsa_slave_dev_check(struct net_device *dev); |
| 29 | |
Vivien Didelot | a93ecdd | 2017-05-19 17:00:37 -0400 | [diff] [blame] | 30 | static int dsa_port_notify(struct dsa_port *dp, unsigned long e, void *v) |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 31 | { |
Vivien Didelot | a93ecdd | 2017-05-19 17:00:37 -0400 | [diff] [blame] | 32 | struct raw_notifier_head *nh = &dp->ds->dst->nh; |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 33 | int err; |
| 34 | |
| 35 | err = raw_notifier_call_chain(nh, e, v); |
| 36 | |
| 37 | return notifier_to_errno(err); |
| 38 | } |
| 39 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 40 | /* slave mii_bus handling ***************************************************/ |
| 41 | static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) |
| 42 | { |
| 43 | struct dsa_switch *ds = bus->priv; |
| 44 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 45 | if (ds->phys_mii_mask & (1 << addr)) |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 46 | return ds->ops->phy_read(ds, addr, reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 47 | |
| 48 | return 0xffff; |
| 49 | } |
| 50 | |
| 51 | static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) |
| 52 | { |
| 53 | struct dsa_switch *ds = bus->priv; |
| 54 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 55 | if (ds->phys_mii_mask & (1 << addr)) |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 56 | return ds->ops->phy_write(ds, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | void dsa_slave_mii_bus_init(struct dsa_switch *ds) |
| 62 | { |
| 63 | ds->slave_mii_bus->priv = (void *)ds; |
| 64 | ds->slave_mii_bus->name = "dsa slave smi"; |
| 65 | ds->slave_mii_bus->read = dsa_slave_phy_read; |
| 66 | ds->slave_mii_bus->write = dsa_slave_phy_write; |
Florian Fainelli | 0b7b498 | 2016-06-07 16:32:38 -0700 | [diff] [blame] | 67 | snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", |
| 68 | ds->dst->tree, ds->index); |
Andrew Lunn | c33063d | 2016-05-10 23:27:23 +0200 | [diff] [blame] | 69 | ds->slave_mii_bus->parent = ds->dev; |
Vivien Didelot | 24df898 | 2015-01-20 19:13:32 -0500 | [diff] [blame] | 70 | ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | /* slave device handling ****************************************************/ |
Nicolas Dichtel | abd2be0 | 2015-04-02 17:07:08 +0200 | [diff] [blame] | 75 | static int dsa_slave_get_iflink(const struct net_device *dev) |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 76 | { |
| 77 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 78 | |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 79 | return p->dp->ds->dst->master_netdev->ifindex; |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Vivien Didelot | a5e9a02 | 2017-01-27 15:29:40 -0500 | [diff] [blame] | 82 | static inline bool dsa_port_is_bridged(struct dsa_port *dp) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 83 | { |
Vivien Didelot | a5e9a02 | 2017-01-27 15:29:40 -0500 | [diff] [blame] | 84 | return !!dp->bridge_dev; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Vivien Didelot | fd36454 | 2017-05-19 17:00:36 -0400 | [diff] [blame] | 87 | static int dsa_port_set_state(struct dsa_port *dp, u8 state, |
| 88 | struct switchdev_trans *trans) |
Vivien Didelot | 4acfee8 | 2016-09-22 16:49:21 -0400 | [diff] [blame] | 89 | { |
Vivien Didelot | c5d35cb | 2017-02-03 13:20:19 -0500 | [diff] [blame] | 90 | struct dsa_switch *ds = dp->ds; |
| 91 | int port = dp->index; |
Vivien Didelot | 732f794 | 2016-09-22 16:49:22 -0400 | [diff] [blame] | 92 | |
Vivien Didelot | fd36454 | 2017-05-19 17:00:36 -0400 | [diff] [blame] | 93 | if (switchdev_trans_ph_prepare(trans)) |
| 94 | return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP; |
| 95 | |
Vivien Didelot | 4acfee8 | 2016-09-22 16:49:21 -0400 | [diff] [blame] | 96 | if (ds->ops->port_stp_state_set) |
| 97 | ds->ops->port_stp_state_set(ds, port, state); |
Vivien Didelot | 732f794 | 2016-09-22 16:49:22 -0400 | [diff] [blame] | 98 | |
| 99 | if (ds->ops->port_fast_age) { |
| 100 | /* Fast age FDB entries or flush appropriate forwarding database |
| 101 | * for the given port, if we are moving it from Learning or |
| 102 | * Forwarding state, to Disabled or Blocking or Listening state. |
| 103 | */ |
| 104 | |
| 105 | if ((dp->stp_state == BR_STATE_LEARNING || |
| 106 | dp->stp_state == BR_STATE_FORWARDING) && |
| 107 | (state == BR_STATE_DISABLED || |
| 108 | state == BR_STATE_BLOCKING || |
| 109 | state == BR_STATE_LISTENING)) |
| 110 | ds->ops->port_fast_age(ds, port); |
| 111 | } |
| 112 | |
| 113 | dp->stp_state = state; |
Vivien Didelot | fd36454 | 2017-05-19 17:00:36 -0400 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static void dsa_port_set_state_now(struct dsa_port *dp, u8 state) |
| 119 | { |
| 120 | int err; |
| 121 | |
| 122 | err = dsa_port_set_state(dp, state, NULL); |
| 123 | if (err) |
| 124 | pr_err("DSA: failed to set STP state %u (%d)\n", state, err); |
Vivien Didelot | 4acfee8 | 2016-09-22 16:49:21 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 127 | static int dsa_slave_open(struct net_device *dev) |
| 128 | { |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 129 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 130 | struct net_device *master = p->dp->ds->dst->master_netdev; |
| 131 | struct dsa_switch *ds = p->dp->ds; |
Vivien Didelot | a5e9a02 | 2017-01-27 15:29:40 -0500 | [diff] [blame] | 132 | u8 stp_state = dsa_port_is_bridged(p->dp) ? |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 133 | BR_STATE_BLOCKING : BR_STATE_FORWARDING; |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 134 | int err; |
| 135 | |
| 136 | if (!(master->flags & IFF_UP)) |
| 137 | return -ENETDOWN; |
| 138 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 139 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 140 | err = dev_uc_add(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 141 | if (err < 0) |
| 142 | goto out; |
| 143 | } |
| 144 | |
| 145 | if (dev->flags & IFF_ALLMULTI) { |
| 146 | err = dev_set_allmulti(master, 1); |
| 147 | if (err < 0) |
| 148 | goto del_unicast; |
| 149 | } |
| 150 | if (dev->flags & IFF_PROMISC) { |
| 151 | err = dev_set_promiscuity(master, 1); |
| 152 | if (err < 0) |
| 153 | goto clear_allmulti; |
| 154 | } |
| 155 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 156 | if (ds->ops->port_enable) { |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 157 | err = ds->ops->port_enable(ds, p->dp->index, p->phy); |
Florian Fainelli | b2f2af2 | 2014-09-24 17:05:18 -0700 | [diff] [blame] | 158 | if (err) |
| 159 | goto clear_promisc; |
| 160 | } |
| 161 | |
Vivien Didelot | fd36454 | 2017-05-19 17:00:36 -0400 | [diff] [blame] | 162 | dsa_port_set_state_now(p->dp, stp_state); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 163 | |
Florian Fainelli | f7f1de5 | 2014-09-24 17:05:17 -0700 | [diff] [blame] | 164 | if (p->phy) |
| 165 | phy_start(p->phy); |
| 166 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 167 | return 0; |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 168 | |
Florian Fainelli | b2f2af2 | 2014-09-24 17:05:18 -0700 | [diff] [blame] | 169 | clear_promisc: |
| 170 | if (dev->flags & IFF_PROMISC) |
Gilad Ben-Yossef | 4fdeddf | 2015-06-25 16:50:13 +0300 | [diff] [blame] | 171 | dev_set_promiscuity(master, -1); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 172 | clear_allmulti: |
| 173 | if (dev->flags & IFF_ALLMULTI) |
| 174 | dev_set_allmulti(master, -1); |
| 175 | del_unicast: |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 176 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 177 | dev_uc_del(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 178 | out: |
| 179 | return err; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int dsa_slave_close(struct net_device *dev) |
| 183 | { |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 184 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 185 | struct net_device *master = p->dp->ds->dst->master_netdev; |
| 186 | struct dsa_switch *ds = p->dp->ds; |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 187 | |
Florian Fainelli | f7f1de5 | 2014-09-24 17:05:17 -0700 | [diff] [blame] | 188 | if (p->phy) |
| 189 | phy_stop(p->phy); |
| 190 | |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 191 | dev_mc_unsync(master, dev); |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 192 | dev_uc_unsync(master, dev); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 193 | if (dev->flags & IFF_ALLMULTI) |
| 194 | dev_set_allmulti(master, -1); |
| 195 | if (dev->flags & IFF_PROMISC) |
| 196 | dev_set_promiscuity(master, -1); |
| 197 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 198 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 199 | dev_uc_del(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 200 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 201 | if (ds->ops->port_disable) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 202 | ds->ops->port_disable(ds, p->dp->index, p->phy); |
Florian Fainelli | b2f2af2 | 2014-09-24 17:05:18 -0700 | [diff] [blame] | 203 | |
Vivien Didelot | fd36454 | 2017-05-19 17:00:36 -0400 | [diff] [blame] | 204 | dsa_port_set_state_now(p->dp, BR_STATE_DISABLED); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 205 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static void dsa_slave_change_rx_flags(struct net_device *dev, int change) |
| 210 | { |
| 211 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 212 | struct net_device *master = p->dp->ds->dst->master_netdev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 213 | |
| 214 | if (change & IFF_ALLMULTI) |
| 215 | dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 216 | if (change & IFF_PROMISC) |
| 217 | dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); |
| 218 | } |
| 219 | |
| 220 | static void dsa_slave_set_rx_mode(struct net_device *dev) |
| 221 | { |
| 222 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 223 | struct net_device *master = p->dp->ds->dst->master_netdev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 224 | |
| 225 | dev_mc_sync(master, dev); |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 226 | dev_uc_sync(master, dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 229 | static int dsa_slave_set_mac_address(struct net_device *dev, void *a) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 230 | { |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 231 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 232 | struct net_device *master = p->dp->ds->dst->master_netdev; |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 233 | struct sockaddr *addr = a; |
| 234 | int err; |
| 235 | |
| 236 | if (!is_valid_ether_addr(addr->sa_data)) |
| 237 | return -EADDRNOTAVAIL; |
| 238 | |
| 239 | if (!(dev->flags & IFF_UP)) |
| 240 | goto out; |
| 241 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 242 | if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 243 | err = dev_uc_add(master, addr->sa_data); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 244 | if (err < 0) |
| 245 | return err; |
| 246 | } |
| 247 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 248 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 249 | dev_uc_del(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 250 | |
| 251 | out: |
Joe Perches | d08f161 | 2014-01-20 09:52:20 -0800 | [diff] [blame] | 252 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 257 | static int dsa_port_vlan_add(struct dsa_port *dp, |
| 258 | const struct switchdev_obj_port_vlan *vlan, |
| 259 | struct switchdev_trans *trans) |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 260 | { |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 261 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 262 | |
Jiri Pirko | 79a62eb | 2015-09-24 10:02:48 +0200 | [diff] [blame] | 263 | if (switchdev_trans_ph_prepare(trans)) { |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 264 | if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add) |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 265 | return -EOPNOTSUPP; |
| 266 | |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 267 | return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans); |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 270 | ds->ops->port_vlan_add(ds, dp->index, vlan, trans); |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 271 | |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 275 | static int dsa_port_vlan_del(struct dsa_port *dp, |
| 276 | const struct switchdev_obj_port_vlan *vlan) |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 277 | { |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 278 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 279 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 280 | if (!ds->ops->port_vlan_del) |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 281 | return -EOPNOTSUPP; |
| 282 | |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 283 | return ds->ops->port_vlan_del(ds, dp->index, vlan); |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 286 | static int dsa_port_vlan_dump(struct dsa_port *dp, |
| 287 | struct switchdev_obj_port_vlan *vlan, |
| 288 | switchdev_obj_dump_cb_t *cb) |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 289 | { |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 290 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 291 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 292 | if (ds->ops->port_vlan_dump) |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 293 | return ds->ops->port_vlan_dump(ds, dp->index, vlan, cb); |
Vivien Didelot | 65aebfc | 2016-02-23 12:13:54 -0500 | [diff] [blame] | 294 | |
Vivien Didelot | 477b184 | 2016-02-23 12:13:56 -0500 | [diff] [blame] | 295 | return -EOPNOTSUPP; |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 298 | static int dsa_port_fdb_add(struct dsa_port *dp, |
| 299 | const struct switchdev_obj_port_fdb *fdb, |
| 300 | struct switchdev_trans *trans) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 301 | { |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 302 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | 146a320 | 2015-10-08 11:35:12 -0400 | [diff] [blame] | 303 | |
Vivien Didelot | 8497aa6 | 2016-04-06 11:55:04 -0400 | [diff] [blame] | 304 | if (switchdev_trans_ph_prepare(trans)) { |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 305 | if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add) |
Vivien Didelot | 8497aa6 | 2016-04-06 11:55:04 -0400 | [diff] [blame] | 306 | return -EOPNOTSUPP; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 307 | |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 308 | return ds->ops->port_fdb_prepare(ds, dp->index, fdb, trans); |
Vivien Didelot | 8497aa6 | 2016-04-06 11:55:04 -0400 | [diff] [blame] | 309 | } |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 310 | |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 311 | ds->ops->port_fdb_add(ds, dp->index, fdb, trans); |
Vivien Didelot | 8497aa6 | 2016-04-06 11:55:04 -0400 | [diff] [blame] | 312 | |
| 313 | return 0; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 316 | static int dsa_port_fdb_del(struct dsa_port *dp, |
| 317 | const struct switchdev_obj_port_fdb *fdb) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 318 | { |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 319 | struct dsa_switch *ds = dp->ds; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 320 | int ret = -EOPNOTSUPP; |
| 321 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 322 | if (ds->ops->port_fdb_del) |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 323 | ret = ds->ops->port_fdb_del(ds, dp->index, fdb); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 324 | |
| 325 | return ret; |
| 326 | } |
| 327 | |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 328 | static int dsa_port_fdb_dump(struct dsa_port *dp, |
| 329 | struct switchdev_obj_port_fdb *fdb, |
| 330 | switchdev_obj_dump_cb_t *cb) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 331 | { |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 332 | struct dsa_switch *ds = dp->ds; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 333 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 334 | if (ds->ops->port_fdb_dump) |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 335 | return ds->ops->port_fdb_dump(ds, dp->index, fdb, cb); |
Vivien Didelot | ea70ba9 | 2015-10-22 09:34:38 -0400 | [diff] [blame] | 336 | |
Vivien Didelot | 1a49a2f | 2015-10-22 09:34:43 -0400 | [diff] [blame] | 337 | return -EOPNOTSUPP; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 340 | static int dsa_port_mdb_add(struct dsa_port *dp, |
| 341 | const struct switchdev_obj_port_mdb *mdb, |
| 342 | struct switchdev_trans *trans) |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 343 | { |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 344 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 345 | |
| 346 | if (switchdev_trans_ph_prepare(trans)) { |
| 347 | if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add) |
| 348 | return -EOPNOTSUPP; |
| 349 | |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 350 | return ds->ops->port_mdb_prepare(ds, dp->index, mdb, trans); |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 353 | ds->ops->port_mdb_add(ds, dp->index, mdb, trans); |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 358 | static int dsa_port_mdb_del(struct dsa_port *dp, |
| 359 | const struct switchdev_obj_port_mdb *mdb) |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 360 | { |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 361 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 362 | |
| 363 | if (ds->ops->port_mdb_del) |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 364 | return ds->ops->port_mdb_del(ds, dp->index, mdb); |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 365 | |
| 366 | return -EOPNOTSUPP; |
| 367 | } |
| 368 | |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 369 | static int dsa_port_mdb_dump(struct dsa_port *dp, |
| 370 | struct switchdev_obj_port_mdb *mdb, |
| 371 | switchdev_obj_dump_cb_t *cb) |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 372 | { |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 373 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 374 | |
| 375 | if (ds->ops->port_mdb_dump) |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 376 | return ds->ops->port_mdb_dump(ds, dp->index, mdb, cb); |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 377 | |
| 378 | return -EOPNOTSUPP; |
| 379 | } |
| 380 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 381 | static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 382 | { |
| 383 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 384 | |
| 385 | if (p->phy != NULL) |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 386 | return phy_mii_ioctl(p->phy, ifr, cmd); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 387 | |
| 388 | return -EOPNOTSUPP; |
| 389 | } |
| 390 | |
Vivien Didelot | c02c417 | 2017-05-19 17:00:42 -0400 | [diff] [blame] | 391 | static int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering, |
| 392 | struct switchdev_trans *trans) |
Vivien Didelot | fb2daba | 2016-02-26 13:16:00 -0500 | [diff] [blame] | 393 | { |
Vivien Didelot | c02c417 | 2017-05-19 17:00:42 -0400 | [diff] [blame] | 394 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | fb2daba | 2016-02-26 13:16:00 -0500 | [diff] [blame] | 395 | |
| 396 | /* bridge skips -EOPNOTSUPP, so skip the prepare phase */ |
| 397 | if (switchdev_trans_ph_prepare(trans)) |
| 398 | return 0; |
| 399 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 400 | if (ds->ops->port_vlan_filtering) |
Vivien Didelot | c02c417 | 2017-05-19 17:00:42 -0400 | [diff] [blame] | 401 | return ds->ops->port_vlan_filtering(ds, dp->index, |
| 402 | vlan_filtering); |
Vivien Didelot | fb2daba | 2016-02-26 13:16:00 -0500 | [diff] [blame] | 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Vivien Didelot | e893de1ba | 2017-03-15 15:53:48 -0400 | [diff] [blame] | 407 | static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds, |
| 408 | unsigned int ageing_time) |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 409 | { |
| 410 | int i; |
| 411 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 412 | for (i = 0; i < ds->num_ports; ++i) { |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 413 | struct dsa_port *dp = &ds->ports[i]; |
| 414 | |
Vivien Didelot | 072bb19 | 2017-05-19 17:00:43 -0400 | [diff] [blame^] | 415 | if (dp->ageing_time && dp->ageing_time < ageing_time) |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 416 | ageing_time = dp->ageing_time; |
| 417 | } |
| 418 | |
| 419 | return ageing_time; |
| 420 | } |
| 421 | |
Vivien Didelot | 072bb19 | 2017-05-19 17:00:43 -0400 | [diff] [blame^] | 422 | static int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock, |
| 423 | struct switchdev_trans *trans) |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 424 | { |
Vivien Didelot | 072bb19 | 2017-05-19 17:00:43 -0400 | [diff] [blame^] | 425 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 426 | unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); |
Vivien Didelot | 072bb19 | 2017-05-19 17:00:43 -0400 | [diff] [blame^] | 427 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 428 | |
Vivien Didelot | 0f3da6a | 2017-03-15 15:53:49 -0400 | [diff] [blame] | 429 | if (switchdev_trans_ph_prepare(trans)) { |
| 430 | if (ds->ageing_time_min && ageing_time < ds->ageing_time_min) |
| 431 | return -ERANGE; |
| 432 | if (ds->ageing_time_max && ageing_time > ds->ageing_time_max) |
| 433 | return -ERANGE; |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 434 | return 0; |
Vivien Didelot | 0f3da6a | 2017-03-15 15:53:49 -0400 | [diff] [blame] | 435 | } |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 436 | |
| 437 | /* Keep the fastest ageing time in case of multiple bridges */ |
Vivien Didelot | 072bb19 | 2017-05-19 17:00:43 -0400 | [diff] [blame^] | 438 | dp->ageing_time = ageing_time; |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 439 | ageing_time = dsa_fastest_ageing_time(ds, ageing_time); |
| 440 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 441 | if (ds->ops->set_ageing_time) |
| 442 | return ds->ops->set_ageing_time(ds, ageing_time); |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 447 | static int dsa_slave_port_attr_set(struct net_device *dev, |
Jiri Pirko | f7fadf3 | 2015-10-14 19:40:49 +0200 | [diff] [blame] | 448 | const struct switchdev_attr *attr, |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 449 | struct switchdev_trans *trans) |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 450 | { |
Vivien Didelot | fd36454 | 2017-05-19 17:00:36 -0400 | [diff] [blame] | 451 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 452 | struct dsa_port *dp = p->dp; |
Vivien Didelot | b8d866a | 2015-09-29 12:38:36 -0400 | [diff] [blame] | 453 | int ret; |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 454 | |
| 455 | switch (attr->id) { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 456 | case SWITCHDEV_ATTR_ID_PORT_STP_STATE: |
Vivien Didelot | fd36454 | 2017-05-19 17:00:36 -0400 | [diff] [blame] | 457 | ret = dsa_port_set_state(dp, attr->u.stp_state, trans); |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 458 | break; |
Vivien Didelot | fb2daba | 2016-02-26 13:16:00 -0500 | [diff] [blame] | 459 | case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: |
Vivien Didelot | c02c417 | 2017-05-19 17:00:42 -0400 | [diff] [blame] | 460 | ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, |
| 461 | trans); |
Vivien Didelot | fb2daba | 2016-02-26 13:16:00 -0500 | [diff] [blame] | 462 | break; |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 463 | case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: |
Vivien Didelot | 072bb19 | 2017-05-19 17:00:43 -0400 | [diff] [blame^] | 464 | ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans); |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 465 | break; |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 466 | default: |
| 467 | ret = -EOPNOTSUPP; |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | return ret; |
| 472 | } |
| 473 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 474 | static int dsa_slave_port_obj_add(struct net_device *dev, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 475 | const struct switchdev_obj *obj, |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 476 | struct switchdev_trans *trans) |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 477 | { |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 478 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 479 | struct dsa_port *dp = p->dp; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 480 | int err; |
| 481 | |
| 482 | /* For the prepare phase, ensure the full set of changes is feasable in |
| 483 | * one go in order to signal a failure properly. If an operation is not |
| 484 | * supported, return -EOPNOTSUPP. |
| 485 | */ |
| 486 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 487 | switch (obj->id) { |
Jiri Pirko | 57d8083 | 2015-10-01 11:03:41 +0200 | [diff] [blame] | 488 | case SWITCHDEV_OBJ_ID_PORT_FDB: |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 489 | err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans); |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 490 | break; |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 491 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 492 | err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans); |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 493 | break; |
Jiri Pirko | 57d8083 | 2015-10-01 11:03:41 +0200 | [diff] [blame] | 494 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 495 | err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), |
| 496 | trans); |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 497 | break; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 498 | default: |
| 499 | err = -EOPNOTSUPP; |
| 500 | break; |
| 501 | } |
| 502 | |
| 503 | return err; |
| 504 | } |
| 505 | |
| 506 | static int dsa_slave_port_obj_del(struct net_device *dev, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 507 | const struct switchdev_obj *obj) |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 508 | { |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 509 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 510 | struct dsa_port *dp = p->dp; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 511 | int err; |
| 512 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 513 | switch (obj->id) { |
Jiri Pirko | 57d8083 | 2015-10-01 11:03:41 +0200 | [diff] [blame] | 514 | case SWITCHDEV_OBJ_ID_PORT_FDB: |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 515 | err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj)); |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 516 | break; |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 517 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 518 | err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 519 | break; |
Jiri Pirko | 57d8083 | 2015-10-01 11:03:41 +0200 | [diff] [blame] | 520 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 521 | err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj)); |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 522 | break; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 523 | default: |
| 524 | err = -EOPNOTSUPP; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | return err; |
| 529 | } |
| 530 | |
| 531 | static int dsa_slave_port_obj_dump(struct net_device *dev, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 532 | struct switchdev_obj *obj, |
| 533 | switchdev_obj_dump_cb_t *cb) |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 534 | { |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 535 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 536 | struct dsa_port *dp = p->dp; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 537 | int err; |
| 538 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 539 | switch (obj->id) { |
Jiri Pirko | 57d8083 | 2015-10-01 11:03:41 +0200 | [diff] [blame] | 540 | case SWITCHDEV_OBJ_ID_PORT_FDB: |
Vivien Didelot | 3fdb023 | 2017-05-19 17:00:39 -0400 | [diff] [blame] | 541 | err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb); |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 542 | break; |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 543 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 544 | err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb); |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 545 | break; |
Jiri Pirko | 57d8083 | 2015-10-01 11:03:41 +0200 | [diff] [blame] | 546 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
Vivien Didelot | 01676d1 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 547 | err = dsa_port_vlan_dump(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), cb); |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 548 | break; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 549 | default: |
| 550 | err = -EOPNOTSUPP; |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | return err; |
| 555 | } |
| 556 | |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 557 | static int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 558 | { |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 559 | struct dsa_notifier_bridge_info info = { |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 560 | .sw_index = dp->ds->index, |
| 561 | .port = dp->index, |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 562 | .br = br, |
| 563 | }; |
| 564 | int err; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 565 | |
Vivien Didelot | 9c26542 | 2017-02-03 13:20:18 -0500 | [diff] [blame] | 566 | /* Here the port is already bridged. Reflect the current configuration |
| 567 | * so that drivers can program their chips accordingly. |
| 568 | */ |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 569 | dp->bridge_dev = br; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 570 | |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 571 | err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 572 | |
Vivien Didelot | 9c26542 | 2017-02-03 13:20:18 -0500 | [diff] [blame] | 573 | /* The bridging is rolled back on error */ |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 574 | if (err) |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 575 | dp->bridge_dev = NULL; |
Vivien Didelot | 9c26542 | 2017-02-03 13:20:18 -0500 | [diff] [blame] | 576 | |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 577 | return err; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 580 | static void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 581 | { |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 582 | struct dsa_notifier_bridge_info info = { |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 583 | .sw_index = dp->ds->index, |
| 584 | .port = dp->index, |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 585 | .br = br, |
| 586 | }; |
| 587 | int err; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 588 | |
Vivien Didelot | 9c26542 | 2017-02-03 13:20:18 -0500 | [diff] [blame] | 589 | /* Here the port is already unbridged. Reflect the current configuration |
| 590 | * so that drivers can program their chips accordingly. |
| 591 | */ |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 592 | dp->bridge_dev = NULL; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 593 | |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 594 | err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 595 | if (err) |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 596 | pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n"); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 597 | |
| 598 | /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, |
| 599 | * so allow it to be in BR_STATE_FORWARDING to be kept functional |
| 600 | */ |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 601 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 604 | static int dsa_slave_port_attr_get(struct net_device *dev, |
| 605 | struct switchdev_attr *attr) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 606 | { |
| 607 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 608 | struct dsa_switch *ds = p->dp->ds; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 609 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 610 | switch (attr->id) { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 611 | case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 612 | attr->u.ppid.id_len = sizeof(ds->index); |
| 613 | memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len); |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 614 | break; |
| 615 | default: |
| 616 | return -EOPNOTSUPP; |
| 617 | } |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 622 | static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p, |
| 623 | struct sk_buff *skb) |
| 624 | { |
| 625 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 626 | if (p->netpoll) |
| 627 | netpoll_send_skb(p->netpoll, skb); |
| 628 | #else |
| 629 | BUG(); |
| 630 | #endif |
| 631 | return NETDEV_TX_OK; |
| 632 | } |
| 633 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 634 | static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) |
| 635 | { |
| 636 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 637 | struct sk_buff *nskb; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 638 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 639 | dev->stats.tx_packets++; |
| 640 | dev->stats.tx_bytes += skb->len; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 641 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 642 | /* Transmit function may have to reallocate the original SKB */ |
| 643 | nskb = p->xmit(skb, dev); |
| 644 | if (!nskb) |
| 645 | return NETDEV_TX_OK; |
Florian Fainelli | 5aed85c | 2014-08-27 17:04:52 -0700 | [diff] [blame] | 646 | |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 647 | /* SKB for netpoll still need to be mangled with the protocol-specific |
| 648 | * tag to be successfully transmitted |
| 649 | */ |
| 650 | if (unlikely(netpoll_tx_running(dev))) |
| 651 | return dsa_netpoll_send_skb(p, nskb); |
| 652 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 653 | /* Queue the SKB for transmission on the parent interface, but |
| 654 | * do not modify its EtherType |
| 655 | */ |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 656 | nskb->dev = p->dp->ds->dst->master_netdev; |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 657 | dev_queue_xmit(nskb); |
Florian Fainelli | 5aed85c | 2014-08-27 17:04:52 -0700 | [diff] [blame] | 658 | |
| 659 | return NETDEV_TX_OK; |
| 660 | } |
| 661 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 662 | /* ethtool operations *******************************************************/ |
| 663 | static int |
Philippe Reynes | bb10bb3 | 2016-10-09 17:00:53 +0200 | [diff] [blame] | 664 | dsa_slave_get_link_ksettings(struct net_device *dev, |
| 665 | struct ethtool_link_ksettings *cmd) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 666 | { |
| 667 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | e69e462 | 2017-02-06 15:55:23 -0800 | [diff] [blame] | 668 | int err = -EOPNOTSUPP; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 669 | |
Florian Fainelli | e69e462 | 2017-02-06 15:55:23 -0800 | [diff] [blame] | 670 | if (p->phy != NULL) |
| 671 | err = phy_ethtool_ksettings_get(p->phy, cmd); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 672 | |
| 673 | return err; |
| 674 | } |
| 675 | |
| 676 | static int |
Philippe Reynes | bb10bb3 | 2016-10-09 17:00:53 +0200 | [diff] [blame] | 677 | dsa_slave_set_link_ksettings(struct net_device *dev, |
| 678 | const struct ethtool_link_ksettings *cmd) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 679 | { |
| 680 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 681 | |
| 682 | if (p->phy != NULL) |
Philippe Reynes | bb10bb3 | 2016-10-09 17:00:53 +0200 | [diff] [blame] | 683 | return phy_ethtool_ksettings_set(p->phy, cmd); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 684 | |
| 685 | return -EOPNOTSUPP; |
| 686 | } |
| 687 | |
| 688 | static void dsa_slave_get_drvinfo(struct net_device *dev, |
| 689 | struct ethtool_drvinfo *drvinfo) |
| 690 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 691 | strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 692 | strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); |
| 693 | strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 696 | static int dsa_slave_get_regs_len(struct net_device *dev) |
| 697 | { |
| 698 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 699 | struct dsa_switch *ds = p->dp->ds; |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 700 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 701 | if (ds->ops->get_regs_len) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 702 | return ds->ops->get_regs_len(ds, p->dp->index); |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 703 | |
| 704 | return -EOPNOTSUPP; |
| 705 | } |
| 706 | |
| 707 | static void |
| 708 | dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) |
| 709 | { |
| 710 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 711 | struct dsa_switch *ds = p->dp->ds; |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 712 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 713 | if (ds->ops->get_regs) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 714 | ds->ops->get_regs(ds, p->dp->index, regs, _p); |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 717 | static int dsa_slave_nway_reset(struct net_device *dev) |
| 718 | { |
| 719 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 720 | |
| 721 | if (p->phy != NULL) |
| 722 | return genphy_restart_aneg(p->phy); |
| 723 | |
| 724 | return -EOPNOTSUPP; |
| 725 | } |
| 726 | |
| 727 | static u32 dsa_slave_get_link(struct net_device *dev) |
| 728 | { |
| 729 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 730 | |
| 731 | if (p->phy != NULL) { |
| 732 | genphy_update_link(p->phy); |
| 733 | return p->phy->link; |
| 734 | } |
| 735 | |
| 736 | return -EOPNOTSUPP; |
| 737 | } |
| 738 | |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 739 | static int dsa_slave_get_eeprom_len(struct net_device *dev) |
| 740 | { |
| 741 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 742 | struct dsa_switch *ds = p->dp->ds; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 743 | |
Andrew Lunn | 0e57604 | 2016-06-04 21:16:52 +0200 | [diff] [blame] | 744 | if (ds->cd && ds->cd->eeprom_len) |
Andrew Lunn | ff04955 | 2016-05-10 23:27:24 +0200 | [diff] [blame] | 745 | return ds->cd->eeprom_len; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 746 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 747 | if (ds->ops->get_eeprom_len) |
| 748 | return ds->ops->get_eeprom_len(ds); |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | static int dsa_slave_get_eeprom(struct net_device *dev, |
| 754 | struct ethtool_eeprom *eeprom, u8 *data) |
| 755 | { |
| 756 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 757 | struct dsa_switch *ds = p->dp->ds; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 758 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 759 | if (ds->ops->get_eeprom) |
| 760 | return ds->ops->get_eeprom(ds, eeprom, data); |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 761 | |
| 762 | return -EOPNOTSUPP; |
| 763 | } |
| 764 | |
| 765 | static int dsa_slave_set_eeprom(struct net_device *dev, |
| 766 | struct ethtool_eeprom *eeprom, u8 *data) |
| 767 | { |
| 768 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 769 | struct dsa_switch *ds = p->dp->ds; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 770 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 771 | if (ds->ops->set_eeprom) |
| 772 | return ds->ops->set_eeprom(ds, eeprom, data); |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 773 | |
| 774 | return -EOPNOTSUPP; |
| 775 | } |
| 776 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 777 | static void dsa_slave_get_strings(struct net_device *dev, |
| 778 | uint32_t stringset, uint8_t *data) |
| 779 | { |
| 780 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 781 | struct dsa_switch *ds = p->dp->ds; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 782 | |
| 783 | if (stringset == ETH_SS_STATS) { |
| 784 | int len = ETH_GSTRING_LEN; |
| 785 | |
| 786 | strncpy(data, "tx_packets", len); |
| 787 | strncpy(data + len, "tx_bytes", len); |
| 788 | strncpy(data + 2 * len, "rx_packets", len); |
| 789 | strncpy(data + 3 * len, "rx_bytes", len); |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 790 | if (ds->ops->get_strings) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 791 | ds->ops->get_strings(ds, p->dp->index, data + 4 * len); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Florian Fainelli | badf3ad | 2016-04-27 11:45:14 -0700 | [diff] [blame] | 795 | static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev, |
| 796 | struct ethtool_stats *stats, |
| 797 | uint64_t *data) |
| 798 | { |
| 799 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
Vivien Didelot | 8b0d3ea | 2017-05-16 14:10:33 -0400 | [diff] [blame] | 800 | struct dsa_switch *ds = dst->cpu_dp->ds; |
| 801 | s8 cpu_port = dst->cpu_dp->index; |
Florian Fainelli | badf3ad | 2016-04-27 11:45:14 -0700 | [diff] [blame] | 802 | int count = 0; |
| 803 | |
| 804 | if (dst->master_ethtool_ops.get_sset_count) { |
| 805 | count = dst->master_ethtool_ops.get_sset_count(dev, |
| 806 | ETH_SS_STATS); |
| 807 | dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data); |
| 808 | } |
| 809 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 810 | if (ds->ops->get_ethtool_stats) |
| 811 | ds->ops->get_ethtool_stats(ds, cpu_port, data + count); |
Florian Fainelli | badf3ad | 2016-04-27 11:45:14 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset) |
| 815 | { |
| 816 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
Vivien Didelot | 8b0d3ea | 2017-05-16 14:10:33 -0400 | [diff] [blame] | 817 | struct dsa_switch *ds = dst->cpu_dp->ds; |
Florian Fainelli | badf3ad | 2016-04-27 11:45:14 -0700 | [diff] [blame] | 818 | int count = 0; |
| 819 | |
| 820 | if (dst->master_ethtool_ops.get_sset_count) |
| 821 | count += dst->master_ethtool_ops.get_sset_count(dev, sset); |
| 822 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 823 | if (sset == ETH_SS_STATS && ds->ops->get_sset_count) |
| 824 | count += ds->ops->get_sset_count(ds); |
Florian Fainelli | badf3ad | 2016-04-27 11:45:14 -0700 | [diff] [blame] | 825 | |
| 826 | return count; |
| 827 | } |
| 828 | |
| 829 | static void dsa_cpu_port_get_strings(struct net_device *dev, |
| 830 | uint32_t stringset, uint8_t *data) |
| 831 | { |
| 832 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
Vivien Didelot | 8b0d3ea | 2017-05-16 14:10:33 -0400 | [diff] [blame] | 833 | struct dsa_switch *ds = dst->cpu_dp->ds; |
| 834 | s8 cpu_port = dst->cpu_dp->index; |
Florian Fainelli | badf3ad | 2016-04-27 11:45:14 -0700 | [diff] [blame] | 835 | int len = ETH_GSTRING_LEN; |
| 836 | int mcount = 0, count; |
| 837 | unsigned int i; |
| 838 | uint8_t pfx[4]; |
| 839 | uint8_t *ndata; |
| 840 | |
| 841 | snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port); |
| 842 | /* We do not want to be NULL-terminated, since this is a prefix */ |
| 843 | pfx[sizeof(pfx) - 1] = '_'; |
| 844 | |
| 845 | if (dst->master_ethtool_ops.get_sset_count) { |
| 846 | mcount = dst->master_ethtool_ops.get_sset_count(dev, |
| 847 | ETH_SS_STATS); |
| 848 | dst->master_ethtool_ops.get_strings(dev, stringset, data); |
| 849 | } |
| 850 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 851 | if (stringset == ETH_SS_STATS && ds->ops->get_strings) { |
Florian Fainelli | badf3ad | 2016-04-27 11:45:14 -0700 | [diff] [blame] | 852 | ndata = data + mcount * len; |
| 853 | /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle |
| 854 | * the output after to prepend our CPU port prefix we |
| 855 | * constructed earlier |
| 856 | */ |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 857 | ds->ops->get_strings(ds, cpu_port, ndata); |
| 858 | count = ds->ops->get_sset_count(ds); |
Florian Fainelli | badf3ad | 2016-04-27 11:45:14 -0700 | [diff] [blame] | 859 | for (i = 0; i < count; i++) { |
| 860 | memmove(ndata + (i * len + sizeof(pfx)), |
| 861 | ndata + i * len, len - sizeof(pfx)); |
| 862 | memcpy(ndata + i * len, pfx, sizeof(pfx)); |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 867 | static void dsa_slave_get_ethtool_stats(struct net_device *dev, |
| 868 | struct ethtool_stats *stats, |
| 869 | uint64_t *data) |
| 870 | { |
| 871 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 872 | struct dsa_switch *ds = p->dp->ds; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 873 | |
Vivien Didelot | 46e7b8d | 2016-04-18 16:10:24 -0400 | [diff] [blame] | 874 | data[0] = dev->stats.tx_packets; |
| 875 | data[1] = dev->stats.tx_bytes; |
| 876 | data[2] = dev->stats.rx_packets; |
| 877 | data[3] = dev->stats.rx_bytes; |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 878 | if (ds->ops->get_ethtool_stats) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 879 | ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | static int dsa_slave_get_sset_count(struct net_device *dev, int sset) |
| 883 | { |
| 884 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 885 | struct dsa_switch *ds = p->dp->ds; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 886 | |
| 887 | if (sset == ETH_SS_STATS) { |
| 888 | int count; |
| 889 | |
| 890 | count = 4; |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 891 | if (ds->ops->get_sset_count) |
| 892 | count += ds->ops->get_sset_count(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 893 | |
| 894 | return count; |
| 895 | } |
| 896 | |
| 897 | return -EOPNOTSUPP; |
| 898 | } |
| 899 | |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 900 | static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 901 | { |
| 902 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 903 | struct dsa_switch *ds = p->dp->ds; |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 904 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 905 | if (ds->ops->get_wol) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 906 | ds->ops->get_wol(ds, p->dp->index, w); |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 910 | { |
| 911 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 912 | struct dsa_switch *ds = p->dp->ds; |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 913 | int ret = -EOPNOTSUPP; |
| 914 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 915 | if (ds->ops->set_wol) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 916 | ret = ds->ops->set_wol(ds, p->dp->index, w); |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 917 | |
| 918 | return ret; |
| 919 | } |
| 920 | |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 921 | static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) |
| 922 | { |
| 923 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 924 | struct dsa_switch *ds = p->dp->ds; |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 925 | int ret; |
| 926 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 927 | if (!ds->ops->set_eee) |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 928 | return -EOPNOTSUPP; |
| 929 | |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 930 | ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e); |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 931 | if (ret) |
| 932 | return ret; |
| 933 | |
| 934 | if (p->phy) |
| 935 | ret = phy_ethtool_set_eee(p->phy, e); |
| 936 | |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) |
| 941 | { |
| 942 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 943 | struct dsa_switch *ds = p->dp->ds; |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 944 | int ret; |
| 945 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 946 | if (!ds->ops->get_eee) |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 947 | return -EOPNOTSUPP; |
| 948 | |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 949 | ret = ds->ops->get_eee(ds, p->dp->index, e); |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 950 | if (ret) |
| 951 | return ret; |
| 952 | |
| 953 | if (p->phy) |
| 954 | ret = phy_ethtool_get_eee(p->phy, e); |
| 955 | |
| 956 | return ret; |
| 957 | } |
| 958 | |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 959 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 960 | static int dsa_slave_netpoll_setup(struct net_device *dev, |
| 961 | struct netpoll_info *ni) |
| 962 | { |
| 963 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 964 | struct dsa_switch *ds = p->dp->ds; |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 965 | struct net_device *master = ds->dst->master_netdev; |
| 966 | struct netpoll *netpoll; |
| 967 | int err = 0; |
| 968 | |
| 969 | netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); |
| 970 | if (!netpoll) |
| 971 | return -ENOMEM; |
| 972 | |
| 973 | err = __netpoll_setup(netpoll, master); |
| 974 | if (err) { |
| 975 | kfree(netpoll); |
| 976 | goto out; |
| 977 | } |
| 978 | |
| 979 | p->netpoll = netpoll; |
| 980 | out: |
| 981 | return err; |
| 982 | } |
| 983 | |
| 984 | static void dsa_slave_netpoll_cleanup(struct net_device *dev) |
| 985 | { |
| 986 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 987 | struct netpoll *netpoll = p->netpoll; |
| 988 | |
| 989 | if (!netpoll) |
| 990 | return; |
| 991 | |
| 992 | p->netpoll = NULL; |
| 993 | |
| 994 | __netpoll_free_async(netpoll); |
| 995 | } |
| 996 | |
| 997 | static void dsa_slave_poll_controller(struct net_device *dev) |
| 998 | { |
| 999 | } |
| 1000 | #endif |
| 1001 | |
Florian Fainelli | 44bb765 | 2017-01-10 12:32:36 -0800 | [diff] [blame] | 1002 | static int dsa_slave_get_phys_port_name(struct net_device *dev, |
| 1003 | char *name, size_t len) |
| 1004 | { |
| 1005 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1006 | |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1007 | if (snprintf(name, len, "p%d", p->dp->index) >= len) |
Florian Fainelli | 44bb765 | 2017-01-10 12:32:36 -0800 | [diff] [blame] | 1008 | return -EINVAL; |
Florian Fainelli | 3a543ef | 2016-12-29 14:20:56 -0800 | [diff] [blame] | 1009 | |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 1013 | static struct dsa_mall_tc_entry * |
| 1014 | dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p, |
| 1015 | unsigned long cookie) |
| 1016 | { |
| 1017 | struct dsa_mall_tc_entry *mall_tc_entry; |
| 1018 | |
| 1019 | list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) |
| 1020 | if (mall_tc_entry->cookie == cookie) |
| 1021 | return mall_tc_entry; |
| 1022 | |
| 1023 | return NULL; |
| 1024 | } |
| 1025 | |
| 1026 | static int dsa_slave_add_cls_matchall(struct net_device *dev, |
| 1027 | __be16 protocol, |
| 1028 | struct tc_cls_matchall_offload *cls, |
| 1029 | bool ingress) |
| 1030 | { |
| 1031 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1032 | struct dsa_mall_tc_entry *mall_tc_entry; |
| 1033 | struct dsa_switch *ds = p->dp->ds; |
| 1034 | struct net *net = dev_net(dev); |
| 1035 | struct dsa_slave_priv *to_p; |
| 1036 | struct net_device *to_dev; |
| 1037 | const struct tc_action *a; |
| 1038 | int err = -EOPNOTSUPP; |
| 1039 | LIST_HEAD(actions); |
| 1040 | int ifindex; |
| 1041 | |
| 1042 | if (!ds->ops->port_mirror_add) |
| 1043 | return err; |
| 1044 | |
| 1045 | if (!tc_single_action(cls->exts)) |
| 1046 | return err; |
| 1047 | |
| 1048 | tcf_exts_to_list(cls->exts, &actions); |
| 1049 | a = list_first_entry(&actions, struct tc_action, list); |
| 1050 | |
| 1051 | if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) { |
| 1052 | struct dsa_mall_mirror_tc_entry *mirror; |
| 1053 | |
| 1054 | ifindex = tcf_mirred_ifindex(a); |
| 1055 | to_dev = __dev_get_by_index(net, ifindex); |
| 1056 | if (!to_dev) |
| 1057 | return -EINVAL; |
| 1058 | |
| 1059 | if (!dsa_slave_dev_check(to_dev)) |
| 1060 | return -EOPNOTSUPP; |
| 1061 | |
| 1062 | mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); |
| 1063 | if (!mall_tc_entry) |
| 1064 | return -ENOMEM; |
| 1065 | |
| 1066 | mall_tc_entry->cookie = cls->cookie; |
| 1067 | mall_tc_entry->type = DSA_PORT_MALL_MIRROR; |
| 1068 | mirror = &mall_tc_entry->mirror; |
| 1069 | |
| 1070 | to_p = netdev_priv(to_dev); |
| 1071 | |
| 1072 | mirror->to_local_port = to_p->dp->index; |
| 1073 | mirror->ingress = ingress; |
| 1074 | |
| 1075 | err = ds->ops->port_mirror_add(ds, p->dp->index, mirror, |
| 1076 | ingress); |
| 1077 | if (err) { |
| 1078 | kfree(mall_tc_entry); |
| 1079 | return err; |
| 1080 | } |
| 1081 | |
| 1082 | list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); |
| 1083 | } |
| 1084 | |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | static void dsa_slave_del_cls_matchall(struct net_device *dev, |
| 1089 | struct tc_cls_matchall_offload *cls) |
| 1090 | { |
| 1091 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1092 | struct dsa_mall_tc_entry *mall_tc_entry; |
| 1093 | struct dsa_switch *ds = p->dp->ds; |
| 1094 | |
| 1095 | if (!ds->ops->port_mirror_del) |
| 1096 | return; |
| 1097 | |
| 1098 | mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie); |
| 1099 | if (!mall_tc_entry) |
| 1100 | return; |
| 1101 | |
| 1102 | list_del(&mall_tc_entry->list); |
| 1103 | |
| 1104 | switch (mall_tc_entry->type) { |
| 1105 | case DSA_PORT_MALL_MIRROR: |
| 1106 | ds->ops->port_mirror_del(ds, p->dp->index, |
| 1107 | &mall_tc_entry->mirror); |
| 1108 | break; |
| 1109 | default: |
| 1110 | WARN_ON(1); |
| 1111 | } |
| 1112 | |
| 1113 | kfree(mall_tc_entry); |
| 1114 | } |
| 1115 | |
| 1116 | static int dsa_slave_setup_tc(struct net_device *dev, u32 handle, |
| 1117 | __be16 protocol, struct tc_to_netdev *tc) |
| 1118 | { |
| 1119 | bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS); |
| 1120 | int ret = -EOPNOTSUPP; |
| 1121 | |
| 1122 | switch (tc->type) { |
| 1123 | case TC_SETUP_MATCHALL: |
| 1124 | switch (tc->cls_mall->command) { |
| 1125 | case TC_CLSMATCHALL_REPLACE: |
| 1126 | return dsa_slave_add_cls_matchall(dev, protocol, |
| 1127 | tc->cls_mall, |
| 1128 | ingress); |
| 1129 | case TC_CLSMATCHALL_DESTROY: |
| 1130 | dsa_slave_del_cls_matchall(dev, tc->cls_mall); |
| 1131 | return 0; |
| 1132 | } |
| 1133 | default: |
| 1134 | break; |
| 1135 | } |
| 1136 | |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
Florian Fainelli | af42192 | 2016-06-07 16:32:41 -0700 | [diff] [blame] | 1140 | void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops) |
| 1141 | { |
| 1142 | ops->get_sset_count = dsa_cpu_port_get_sset_count; |
| 1143 | ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats; |
| 1144 | ops->get_strings = dsa_cpu_port_get_strings; |
| 1145 | } |
| 1146 | |
Florian Fainelli | bf9f264 | 2017-01-30 09:48:40 -0800 | [diff] [blame] | 1147 | static int dsa_slave_get_rxnfc(struct net_device *dev, |
| 1148 | struct ethtool_rxnfc *nfc, u32 *rule_locs) |
| 1149 | { |
| 1150 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1151 | struct dsa_switch *ds = p->dp->ds; |
| 1152 | |
| 1153 | if (!ds->ops->get_rxnfc) |
| 1154 | return -EOPNOTSUPP; |
| 1155 | |
| 1156 | return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs); |
| 1157 | } |
| 1158 | |
| 1159 | static int dsa_slave_set_rxnfc(struct net_device *dev, |
| 1160 | struct ethtool_rxnfc *nfc) |
| 1161 | { |
| 1162 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1163 | struct dsa_switch *ds = p->dp->ds; |
| 1164 | |
| 1165 | if (!ds->ops->set_rxnfc) |
| 1166 | return -EOPNOTSUPP; |
| 1167 | |
| 1168 | return ds->ops->set_rxnfc(ds, p->dp->index, nfc); |
| 1169 | } |
| 1170 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1171 | static const struct ethtool_ops dsa_slave_ethtool_ops = { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1172 | .get_drvinfo = dsa_slave_get_drvinfo, |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 1173 | .get_regs_len = dsa_slave_get_regs_len, |
| 1174 | .get_regs = dsa_slave_get_regs, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1175 | .nway_reset = dsa_slave_nway_reset, |
| 1176 | .get_link = dsa_slave_get_link, |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 1177 | .get_eeprom_len = dsa_slave_get_eeprom_len, |
| 1178 | .get_eeprom = dsa_slave_get_eeprom, |
| 1179 | .set_eeprom = dsa_slave_set_eeprom, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1180 | .get_strings = dsa_slave_get_strings, |
| 1181 | .get_ethtool_stats = dsa_slave_get_ethtool_stats, |
| 1182 | .get_sset_count = dsa_slave_get_sset_count, |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 1183 | .set_wol = dsa_slave_set_wol, |
| 1184 | .get_wol = dsa_slave_get_wol, |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 1185 | .set_eee = dsa_slave_set_eee, |
| 1186 | .get_eee = dsa_slave_get_eee, |
Philippe Reynes | bb10bb3 | 2016-10-09 17:00:53 +0200 | [diff] [blame] | 1187 | .get_link_ksettings = dsa_slave_get_link_ksettings, |
| 1188 | .set_link_ksettings = dsa_slave_set_link_ksettings, |
Florian Fainelli | bf9f264 | 2017-01-30 09:48:40 -0800 | [diff] [blame] | 1189 | .get_rxnfc = dsa_slave_get_rxnfc, |
| 1190 | .set_rxnfc = dsa_slave_set_rxnfc, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1191 | }; |
| 1192 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 1193 | static const struct net_device_ops dsa_slave_netdev_ops = { |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 1194 | .ndo_open = dsa_slave_open, |
| 1195 | .ndo_stop = dsa_slave_close, |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 1196 | .ndo_start_xmit = dsa_slave_xmit, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 1197 | .ndo_change_rx_flags = dsa_slave_change_rx_flags, |
| 1198 | .ndo_set_rx_mode = dsa_slave_set_rx_mode, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 1199 | .ndo_set_mac_address = dsa_slave_set_mac_address, |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 1200 | .ndo_fdb_add = switchdev_port_fdb_add, |
| 1201 | .ndo_fdb_del = switchdev_port_fdb_del, |
| 1202 | .ndo_fdb_dump = switchdev_port_fdb_dump, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 1203 | .ndo_do_ioctl = dsa_slave_ioctl, |
Nicolas Dichtel | abd2be0 | 2015-04-02 17:07:08 +0200 | [diff] [blame] | 1204 | .ndo_get_iflink = dsa_slave_get_iflink, |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 1205 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1206 | .ndo_netpoll_setup = dsa_slave_netpoll_setup, |
| 1207 | .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, |
| 1208 | .ndo_poll_controller = dsa_slave_poll_controller, |
| 1209 | #endif |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 1210 | .ndo_bridge_getlink = switchdev_port_bridge_getlink, |
| 1211 | .ndo_bridge_setlink = switchdev_port_bridge_setlink, |
| 1212 | .ndo_bridge_dellink = switchdev_port_bridge_dellink, |
Florian Fainelli | 44bb765 | 2017-01-10 12:32:36 -0800 | [diff] [blame] | 1213 | .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 1214 | .ndo_setup_tc = dsa_slave_setup_tc, |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 1215 | }; |
| 1216 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 1217 | static const struct switchdev_ops dsa_slave_switchdev_ops = { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1218 | .switchdev_port_attr_get = dsa_slave_port_attr_get, |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 1219 | .switchdev_port_attr_set = dsa_slave_port_attr_set, |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 1220 | .switchdev_port_obj_add = dsa_slave_port_obj_add, |
| 1221 | .switchdev_port_obj_del = dsa_slave_port_obj_del, |
| 1222 | .switchdev_port_obj_dump = dsa_slave_port_obj_dump, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 1223 | }; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1224 | |
Florian Fainelli | f37db85 | 2015-09-23 18:19:58 -0700 | [diff] [blame] | 1225 | static struct device_type dsa_type = { |
| 1226 | .name = "dsa", |
| 1227 | }; |
| 1228 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1229 | static void dsa_slave_adjust_link(struct net_device *dev) |
| 1230 | { |
| 1231 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1232 | struct dsa_switch *ds = p->dp->ds; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1233 | unsigned int status_changed = 0; |
| 1234 | |
| 1235 | if (p->old_link != p->phy->link) { |
| 1236 | status_changed = 1; |
| 1237 | p->old_link = p->phy->link; |
| 1238 | } |
| 1239 | |
| 1240 | if (p->old_duplex != p->phy->duplex) { |
| 1241 | status_changed = 1; |
| 1242 | p->old_duplex = p->phy->duplex; |
| 1243 | } |
| 1244 | |
| 1245 | if (p->old_pause != p->phy->pause) { |
| 1246 | status_changed = 1; |
| 1247 | p->old_pause = p->phy->pause; |
| 1248 | } |
| 1249 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 1250 | if (ds->ops->adjust_link && status_changed) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1251 | ds->ops->adjust_link(ds, p->dp->index, p->phy); |
Florian Fainelli | ec9436b | 2014-08-27 17:04:53 -0700 | [diff] [blame] | 1252 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1253 | if (status_changed) |
| 1254 | phy_print_status(p->phy); |
| 1255 | } |
| 1256 | |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1257 | static int dsa_slave_fixed_link_update(struct net_device *dev, |
| 1258 | struct fixed_phy_status *status) |
| 1259 | { |
Andrew Lunn | b71be35 | 2016-03-12 00:01:37 +0100 | [diff] [blame] | 1260 | struct dsa_slave_priv *p; |
| 1261 | struct dsa_switch *ds; |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1262 | |
Andrew Lunn | b71be35 | 2016-03-12 00:01:37 +0100 | [diff] [blame] | 1263 | if (dev) { |
| 1264 | p = netdev_priv(dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1265 | ds = p->dp->ds; |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 1266 | if (ds->ops->fixed_link_update) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1267 | ds->ops->fixed_link_update(ds, p->dp->index, status); |
Andrew Lunn | b71be35 | 2016-03-12 00:01:37 +0100 | [diff] [blame] | 1268 | } |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1269 | |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1273 | /* slave device setup *******************************************************/ |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1274 | static int dsa_slave_phy_connect(struct dsa_slave_priv *p, |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1275 | struct net_device *slave_dev, |
| 1276 | int addr) |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1277 | { |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1278 | struct dsa_switch *ds = p->dp->ds; |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1279 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 1280 | p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr); |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1281 | if (!p->phy) { |
| 1282 | netdev_err(slave_dev, "no phy at %d\n", addr); |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1283 | return -ENODEV; |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1284 | } |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1285 | |
| 1286 | /* Use already configured phy mode */ |
Florian Fainelli | 211c504 | 2015-08-08 12:58:57 -0700 | [diff] [blame] | 1287 | if (p->phy_interface == PHY_INTERFACE_MODE_NA) |
| 1288 | p->phy_interface = p->phy->interface; |
Florian Fainelli | 4078b76 | 2017-01-20 16:05:05 -0800 | [diff] [blame] | 1289 | return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, |
| 1290 | p->phy_interface); |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1293 | static int dsa_slave_phy_setup(struct dsa_slave_priv *p, |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1294 | struct net_device *slave_dev) |
| 1295 | { |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1296 | struct dsa_switch *ds = p->dp->ds; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1297 | struct device_node *phy_dn, *port_dn; |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1298 | bool phy_is_fixed = false; |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 1299 | u32 phy_flags = 0; |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 1300 | int mode, ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1301 | |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1302 | port_dn = p->dp->dn; |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 1303 | mode = of_get_phy_mode(port_dn); |
| 1304 | if (mode < 0) |
| 1305 | mode = PHY_INTERFACE_MODE_NA; |
| 1306 | p->phy_interface = mode; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1307 | |
| 1308 | phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); |
Johan Hovold | 0d8f3c6 | 2016-11-28 19:24:54 +0100 | [diff] [blame] | 1309 | if (!phy_dn && of_phy_is_fixed_link(port_dn)) { |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1310 | /* In the case of a fixed PHY, the DT node associated |
| 1311 | * to the fixed PHY is the Port DT node |
| 1312 | */ |
| 1313 | ret = of_phy_register_fixed_link(port_dn); |
| 1314 | if (ret) { |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1315 | netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret); |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1316 | return ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1317 | } |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1318 | phy_is_fixed = true; |
Johan Hovold | 0d8f3c6 | 2016-11-28 19:24:54 +0100 | [diff] [blame] | 1319 | phy_dn = of_node_get(port_dn); |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 1322 | if (ds->ops->get_phy_flags) |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1323 | phy_flags = ds->ops->get_phy_flags(ds, p->dp->index); |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 1324 | |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1325 | if (phy_dn) { |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1326 | int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn); |
| 1327 | |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1328 | /* If this PHY address is part of phys_mii_mask, which means |
| 1329 | * that we need to divert reads and writes to/from it, then we |
| 1330 | * want to bind this device using the slave MII bus created by |
| 1331 | * DSA to make that happen. |
| 1332 | */ |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1333 | if (!phy_is_fixed && phy_id >= 0 && |
| 1334 | (ds->phys_mii_mask & (1 << phy_id))) { |
| 1335 | ret = dsa_slave_phy_connect(p, slave_dev, phy_id); |
| 1336 | if (ret) { |
| 1337 | netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret); |
Johan Hovold | 0d8f3c6 | 2016-11-28 19:24:54 +0100 | [diff] [blame] | 1338 | of_node_put(phy_dn); |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1339 | return ret; |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1340 | } |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1341 | } else { |
| 1342 | p->phy = of_phy_connect(slave_dev, phy_dn, |
| 1343 | dsa_slave_adjust_link, |
| 1344 | phy_flags, |
| 1345 | p->phy_interface); |
| 1346 | } |
Johan Hovold | 0d8f3c6 | 2016-11-28 19:24:54 +0100 | [diff] [blame] | 1347 | |
| 1348 | of_node_put(phy_dn); |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1349 | } |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1350 | |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1351 | if (p->phy && phy_is_fixed) |
| 1352 | fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); |
| 1353 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1354 | /* We could not connect to a designated PHY, so use the switch internal |
| 1355 | * MDIO bus instead |
| 1356 | */ |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 1357 | if (!p->phy) { |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1358 | ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index); |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1359 | if (ret) { |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1360 | netdev_err(slave_dev, "failed to connect to port %d: %d\n", |
| 1361 | p->dp->index, ret); |
Johan Hovold | 881eada | 2016-11-28 19:25:09 +0100 | [diff] [blame] | 1362 | if (phy_is_fixed) |
| 1363 | of_phy_deregister_fixed_link(port_dn); |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1364 | return ret; |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1365 | } |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 1366 | } |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1367 | |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1368 | phy_attached_info(p->phy); |
| 1369 | |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1370 | return 0; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Andrew Lunn | 448b448 | 2015-05-06 01:09:56 +0200 | [diff] [blame] | 1373 | static struct lock_class_key dsa_slave_netdev_xmit_lock_key; |
| 1374 | static void dsa_slave_set_lockdep_class_one(struct net_device *dev, |
| 1375 | struct netdev_queue *txq, |
| 1376 | void *_unused) |
| 1377 | { |
| 1378 | lockdep_set_class(&txq->_xmit_lock, |
| 1379 | &dsa_slave_netdev_xmit_lock_key); |
| 1380 | } |
| 1381 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 1382 | int dsa_slave_suspend(struct net_device *slave_dev) |
| 1383 | { |
| 1384 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 1385 | |
Florian Fainelli | f154be2 | 2017-01-25 09:10:41 -0800 | [diff] [blame] | 1386 | netif_device_detach(slave_dev); |
| 1387 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 1388 | if (p->phy) { |
| 1389 | phy_stop(p->phy); |
| 1390 | p->old_pause = -1; |
| 1391 | p->old_link = -1; |
| 1392 | p->old_duplex = -1; |
| 1393 | phy_suspend(p->phy); |
| 1394 | } |
| 1395 | |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | int dsa_slave_resume(struct net_device *slave_dev) |
| 1400 | { |
| 1401 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 1402 | |
| 1403 | netif_device_attach(slave_dev); |
| 1404 | |
| 1405 | if (p->phy) { |
| 1406 | phy_resume(p->phy); |
| 1407 | phy_start(p->phy); |
| 1408 | } |
| 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1413 | int dsa_slave_create(struct dsa_switch *ds, struct device *parent, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1414 | int port, const char *name) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1415 | { |
Florian Fainelli | badf3ad | 2016-04-27 11:45:14 -0700 | [diff] [blame] | 1416 | struct dsa_switch_tree *dst = ds->dst; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1417 | struct net_device *master; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1418 | struct net_device *slave_dev; |
| 1419 | struct dsa_slave_priv *p; |
| 1420 | int ret; |
| 1421 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1422 | master = ds->dst->master_netdev; |
| 1423 | if (ds->master_netdev) |
| 1424 | master = ds->master_netdev; |
| 1425 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 1426 | slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, |
| 1427 | NET_NAME_UNKNOWN, ether_setup); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1428 | if (slave_dev == NULL) |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1429 | return -ENOMEM; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1430 | |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 1431 | slave_dev->features = master->vlan_features | NETIF_F_HW_TC; |
| 1432 | slave_dev->hw_features |= NETIF_F_HW_TC; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1433 | slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; |
Bjørn Mork | 2fcc800 | 2013-08-30 18:08:46 +0200 | [diff] [blame] | 1434 | eth_hw_addr_inherit(slave_dev, master); |
Phil Sutter | 0a5f107 | 2015-08-18 10:30:41 +0200 | [diff] [blame] | 1435 | slave_dev->priv_flags |= IFF_NO_QUEUE; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 1436 | slave_dev->netdev_ops = &dsa_slave_netdev_ops; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 1437 | slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; |
Jarod Wilson | 8b1efc0 | 2016-10-20 23:25:27 -0400 | [diff] [blame] | 1438 | slave_dev->min_mtu = 0; |
| 1439 | slave_dev->max_mtu = ETH_MAX_MTU; |
Florian Fainelli | f37db85 | 2015-09-23 18:19:58 -0700 | [diff] [blame] | 1440 | SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 1441 | |
Andrew Lunn | 448b448 | 2015-05-06 01:09:56 +0200 | [diff] [blame] | 1442 | netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, |
| 1443 | NULL); |
| 1444 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1445 | SET_NETDEV_DEV(slave_dev, parent); |
Andrew Lunn | 189b0d9 | 2016-06-04 21:16:58 +0200 | [diff] [blame] | 1446 | slave_dev->dev.of_node = ds->ports[port].dn; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1447 | slave_dev->vlan_features = master->vlan_features; |
| 1448 | |
| 1449 | p = netdev_priv(slave_dev); |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1450 | p->dp = &ds->ports[port]; |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 1451 | INIT_LIST_HEAD(&p->mall_tc_list); |
Andrew Lunn | 39a7f2a | 2016-06-04 21:17:03 +0200 | [diff] [blame] | 1452 | p->xmit = dst->tag_ops->xmit; |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 1453 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1454 | p->old_pause = -1; |
| 1455 | p->old_link = -1; |
| 1456 | p->old_duplex = -1; |
| 1457 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1458 | ds->ports[port].netdev = slave_dev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1459 | ret = register_netdev(slave_dev); |
| 1460 | if (ret) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 1461 | netdev_err(master, "error %d registering interface %s\n", |
| 1462 | ret, slave_dev->name); |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1463 | ds->ports[port].netdev = NULL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1464 | free_netdev(slave_dev); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1465 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | netif_carrier_off(slave_dev); |
| 1469 | |
Andrew Lunn | 0071f56 | 2016-01-06 20:11:20 +0100 | [diff] [blame] | 1470 | ret = dsa_slave_phy_setup(p, slave_dev); |
| 1471 | if (ret) { |
| 1472 | netdev_err(master, "error %d setting up slave phy\n", ret); |
Florian Fainelli | 73dcb55 | 2016-02-17 18:43:22 -0800 | [diff] [blame] | 1473 | unregister_netdev(slave_dev); |
Andrew Lunn | 0071f56 | 2016-01-06 20:11:20 +0100 | [diff] [blame] | 1474 | free_netdev(slave_dev); |
| 1475 | return ret; |
| 1476 | } |
| 1477 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1478 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1479 | } |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1480 | |
Neil Armstrong | cda5c15 | 2015-12-07 13:57:35 +0100 | [diff] [blame] | 1481 | void dsa_slave_destroy(struct net_device *slave_dev) |
| 1482 | { |
| 1483 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
Johan Hovold | 881eada | 2016-11-28 19:25:09 +0100 | [diff] [blame] | 1484 | struct device_node *port_dn; |
| 1485 | |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1486 | port_dn = p->dp->dn; |
Neil Armstrong | cda5c15 | 2015-12-07 13:57:35 +0100 | [diff] [blame] | 1487 | |
| 1488 | netif_carrier_off(slave_dev); |
Johan Hovold | 881eada | 2016-11-28 19:25:09 +0100 | [diff] [blame] | 1489 | if (p->phy) { |
Neil Armstrong | cda5c15 | 2015-12-07 13:57:35 +0100 | [diff] [blame] | 1490 | phy_disconnect(p->phy); |
Johan Hovold | 881eada | 2016-11-28 19:25:09 +0100 | [diff] [blame] | 1491 | |
| 1492 | if (of_phy_is_fixed_link(port_dn)) |
| 1493 | of_phy_deregister_fixed_link(port_dn); |
| 1494 | } |
Neil Armstrong | cda5c15 | 2015-12-07 13:57:35 +0100 | [diff] [blame] | 1495 | unregister_netdev(slave_dev); |
| 1496 | free_netdev(slave_dev); |
| 1497 | } |
| 1498 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1499 | static bool dsa_slave_dev_check(struct net_device *dev) |
| 1500 | { |
| 1501 | return dev->netdev_ops == &dsa_slave_netdev_ops; |
| 1502 | } |
| 1503 | |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1504 | static int dsa_slave_changeupper(struct net_device *dev, |
| 1505 | struct netdev_notifier_changeupper_info *info) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1506 | { |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 1507 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1508 | struct dsa_port *dp = p->dp; |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1509 | int err = NOTIFY_DONE; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1510 | |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1511 | if (netif_is_bridge_master(info->upper_dev)) { |
| 1512 | if (info->linking) { |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 1513 | err = dsa_port_bridge_join(dp, info->upper_dev); |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1514 | err = notifier_from_errno(err); |
| 1515 | } else { |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 1516 | dsa_port_bridge_leave(dp, info->upper_dev); |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1517 | err = NOTIFY_OK; |
Vivien Didelot | 6debb68 | 2016-03-13 16:21:34 -0400 | [diff] [blame] | 1518 | } |
Vivien Didelot | 6debb68 | 2016-03-13 16:21:34 -0400 | [diff] [blame] | 1519 | } |
| 1520 | |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1521 | return err; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1522 | } |
| 1523 | |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 1524 | static int dsa_slave_netdevice_event(struct notifier_block *nb, |
| 1525 | unsigned long event, void *ptr) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1526 | { |
Vivien Didelot | 6debb68 | 2016-03-13 16:21:34 -0400 | [diff] [blame] | 1527 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1528 | |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1529 | if (dev->netdev_ops != &dsa_slave_netdev_ops) |
| 1530 | return NOTIFY_DONE; |
| 1531 | |
| 1532 | if (event == NETDEV_CHANGEUPPER) |
| 1533 | return dsa_slave_changeupper(dev, ptr); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1534 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1535 | return NOTIFY_DONE; |
| 1536 | } |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 1537 | |
| 1538 | static struct notifier_block dsa_slave_nb __read_mostly = { |
| 1539 | .notifier_call = dsa_slave_netdevice_event, |
| 1540 | }; |
| 1541 | |
| 1542 | int dsa_slave_register_notifier(void) |
| 1543 | { |
| 1544 | return register_netdevice_notifier(&dsa_slave_nb); |
| 1545 | } |
| 1546 | |
| 1547 | void dsa_slave_unregister_notifier(void) |
| 1548 | { |
| 1549 | int err; |
| 1550 | |
| 1551 | err = unregister_netdevice_notifier(&dsa_slave_nb); |
| 1552 | if (err) |
| 1553 | pr_err("DSA: failed to unregister slave notifier (%d)\n", err); |
| 1554 | } |