blob: 887e2669551930701a9ac98d3bb2229595006e6e [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/slave.c - Slave device handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 *
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 Buytenhekdf02c6f2008-11-10 21:53:12 -080012#include <linux/etherdevice.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080013#include <linux/netdevice.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include <linux/phy.h>
Florian Fainellia2820542014-10-17 16:02:13 -070015#include <linux/phy_fixed.h>
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070016#include <linux/of_net.h>
17#include <linux/of_mdio.h>
Andrew Lunn7f854422016-01-06 20:11:18 +010018#include <linux/mdio.h>
Florian Fainellif50f2122017-01-30 12:41:40 -080019#include <linux/list.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080020#include <net/rtnetlink.h>
Florian Fainellif50f2122017-01-30 12:41:40 -080021#include <net/pkt_cls.h>
22#include <net/tc_act/tc_mirred.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080023#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070024#include <linux/netpoll.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040025
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000026#include "dsa_priv.h"
27
Florian Fainellif50f2122017-01-30 12:41:40 -080028static bool dsa_slave_dev_check(struct net_device *dev);
29
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000030/* slave mii_bus handling ***************************************************/
31static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
32{
33 struct dsa_switch *ds = bus->priv;
34
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070035 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040036 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000037
38 return 0xffff;
39}
40
41static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
42{
43 struct dsa_switch *ds = bus->priv;
44
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070045 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040046 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000047
48 return 0;
49}
50
51void dsa_slave_mii_bus_init(struct dsa_switch *ds)
52{
53 ds->slave_mii_bus->priv = (void *)ds;
54 ds->slave_mii_bus->name = "dsa slave smi";
55 ds->slave_mii_bus->read = dsa_slave_phy_read;
56 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070057 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
58 ds->dst->tree, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020059 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050060 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000061}
62
63
64/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020065static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000066{
67 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000068
Vivien Didelotafdcf152017-01-27 15:29:39 -050069 return p->dp->ds->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000070}
71
Vivien Didelota5e9a022017-01-27 15:29:40 -050072static inline bool dsa_port_is_bridged(struct dsa_port *dp)
Florian Fainellib73adef2015-02-24 13:15:33 -080073{
Vivien Didelota5e9a022017-01-27 15:29:40 -050074 return !!dp->bridge_dev;
Florian Fainellib73adef2015-02-24 13:15:33 -080075}
76
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000077static int dsa_slave_open(struct net_device *dev)
78{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080079 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -050080 struct net_device *master = p->dp->ds->dst->master_netdev;
81 struct dsa_switch *ds = p->dp->ds;
Vivien Didelota5e9a022017-01-27 15:29:40 -050082 u8 stp_state = dsa_port_is_bridged(p->dp) ?
Florian Fainellib73adef2015-02-24 13:15:33 -080083 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080084 int err;
85
86 if (!(master->flags & IFF_UP))
87 return -ENETDOWN;
88
Joe Perches8feedbb2012-05-08 18:56:57 +000089 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000090 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080091 if (err < 0)
92 goto out;
93 }
94
95 if (dev->flags & IFF_ALLMULTI) {
96 err = dev_set_allmulti(master, 1);
97 if (err < 0)
98 goto del_unicast;
99 }
100 if (dev->flags & IFF_PROMISC) {
101 err = dev_set_promiscuity(master, 1);
102 if (err < 0)
103 goto clear_allmulti;
104 }
105
Vivien Didelot9d490b42016-08-23 12:38:56 -0400106 if (ds->ops->port_enable) {
Vivien Didelotafdcf152017-01-27 15:29:39 -0500107 err = ds->ops->port_enable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700108 if (err)
109 goto clear_promisc;
110 }
111
Vivien Didelotfd364542017-05-19 17:00:36 -0400112 dsa_port_set_state_now(p->dp, stp_state);
Florian Fainellib73adef2015-02-24 13:15:33 -0800113
Florian Fainellif7f1de52014-09-24 17:05:17 -0700114 if (p->phy)
115 phy_start(p->phy);
116
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000117 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800118
Florian Fainellib2f2af22014-09-24 17:05:18 -0700119clear_promisc:
120 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300121 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800122clear_allmulti:
123 if (dev->flags & IFF_ALLMULTI)
124 dev_set_allmulti(master, -1);
125del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000126 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000127 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800128out:
129 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000130}
131
132static int dsa_slave_close(struct net_device *dev)
133{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800134 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500135 struct net_device *master = p->dp->ds->dst->master_netdev;
136 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800137
Florian Fainellif7f1de52014-09-24 17:05:17 -0700138 if (p->phy)
139 phy_stop(p->phy);
140
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800141 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000142 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800143 if (dev->flags & IFF_ALLMULTI)
144 dev_set_allmulti(master, -1);
145 if (dev->flags & IFF_PROMISC)
146 dev_set_promiscuity(master, -1);
147
Joe Perches8feedbb2012-05-08 18:56:57 +0000148 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000149 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800150
Vivien Didelot9d490b42016-08-23 12:38:56 -0400151 if (ds->ops->port_disable)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500152 ds->ops->port_disable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700153
Vivien Didelotfd364542017-05-19 17:00:36 -0400154 dsa_port_set_state_now(p->dp, BR_STATE_DISABLED);
Florian Fainellib73adef2015-02-24 13:15:33 -0800155
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000156 return 0;
157}
158
159static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
160{
161 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500162 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000163
164 if (change & IFF_ALLMULTI)
165 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
166 if (change & IFF_PROMISC)
167 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
168}
169
170static void dsa_slave_set_rx_mode(struct net_device *dev)
171{
172 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500173 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000174
175 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000176 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000177}
178
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800179static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000180{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800181 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500182 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800183 struct sockaddr *addr = a;
184 int err;
185
186 if (!is_valid_ether_addr(addr->sa_data))
187 return -EADDRNOTAVAIL;
188
189 if (!(dev->flags & IFF_UP))
190 goto out;
191
Joe Perches8feedbb2012-05-08 18:56:57 +0000192 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000193 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800194 if (err < 0)
195 return err;
196 }
197
Joe Perches8feedbb2012-05-08 18:56:57 +0000198 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000199 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800200
201out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800202 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000203
204 return 0;
205}
206
207static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
208{
209 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000210
211 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000212 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000213
214 return -EOPNOTSUPP;
215}
216
Scott Feldman35636062015-05-10 09:47:51 -0700217static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200218 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200219 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700220{
Vivien Didelotfd364542017-05-19 17:00:36 -0400221 struct dsa_slave_priv *p = netdev_priv(dev);
222 struct dsa_port *dp = p->dp;
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400223 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700224
225 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200226 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400227 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700228 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500229 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vivien Didelotc02c4172017-05-19 17:00:42 -0400230 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
231 trans);
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500232 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400233 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Vivien Didelot072bb192017-05-19 17:00:43 -0400234 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400235 break;
Scott Feldman35636062015-05-10 09:47:51 -0700236 default:
237 ret = -EOPNOTSUPP;
238 break;
239 }
240
241 return ret;
242}
243
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400244static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200245 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200246 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400247{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400248 struct dsa_slave_priv *p = netdev_priv(dev);
249 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400250 int err;
251
252 /* For the prepare phase, ensure the full set of changes is feasable in
253 * one go in order to signal a failure properly. If an operation is not
254 * supported, return -EOPNOTSUPP.
255 */
256
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200257 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200258 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400259 err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400260 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400261 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400262 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400263 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200264 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400265 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
266 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400267 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400268 default:
269 err = -EOPNOTSUPP;
270 break;
271 }
272
273 return err;
274}
275
276static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200277 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400278{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400279 struct dsa_slave_priv *p = netdev_priv(dev);
280 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400281 int err;
282
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200283 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200284 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400285 err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400286 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400287 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400288 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400289 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200290 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400291 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400292 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400293 default:
294 err = -EOPNOTSUPP;
295 break;
296 }
297
298 return err;
299}
300
301static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200302 struct switchdev_obj *obj,
303 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400304{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400305 struct dsa_slave_priv *p = netdev_priv(dev);
306 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400307 int err;
308
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200309 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200310 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400311 err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400312 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400313 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400314 err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400315 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200316 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400317 err = dsa_port_vlan_dump(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400318 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400319 default:
320 err = -EOPNOTSUPP;
321 break;
322 }
323
324 return err;
325}
326
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700327static int dsa_slave_port_attr_get(struct net_device *dev,
328 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800329{
330 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500331 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800332
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700333 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200334 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700335 attr->u.ppid.id_len = sizeof(ds->index);
336 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700337 break;
338 default:
339 return -EOPNOTSUPP;
340 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800341
342 return 0;
343}
344
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700345static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
346 struct sk_buff *skb)
347{
348#ifdef CONFIG_NET_POLL_CONTROLLER
349 if (p->netpoll)
350 netpoll_send_skb(p->netpoll, skb);
351#else
352 BUG();
353#endif
354 return NETDEV_TX_OK;
355}
356
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700357static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
358{
359 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700360 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700361
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700362 dev->stats.tx_packets++;
363 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700364
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700365 /* Transmit function may have to reallocate the original SKB */
366 nskb = p->xmit(skb, dev);
367 if (!nskb)
368 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700369
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700370 /* SKB for netpoll still need to be mangled with the protocol-specific
371 * tag to be successfully transmitted
372 */
373 if (unlikely(netpoll_tx_running(dev)))
374 return dsa_netpoll_send_skb(p, nskb);
375
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700376 /* Queue the SKB for transmission on the parent interface, but
377 * do not modify its EtherType
378 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500379 nskb->dev = p->dp->ds->dst->master_netdev;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700380 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700381
382 return NETDEV_TX_OK;
383}
384
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000385/* ethtool operations *******************************************************/
386static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200387dsa_slave_get_link_ksettings(struct net_device *dev,
388 struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000389{
390 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellie69e4622017-02-06 15:55:23 -0800391 int err = -EOPNOTSUPP;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000392
Florian Fainellie69e4622017-02-06 15:55:23 -0800393 if (p->phy != NULL)
394 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000395
396 return err;
397}
398
399static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200400dsa_slave_set_link_ksettings(struct net_device *dev,
401 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000402{
403 struct dsa_slave_priv *p = netdev_priv(dev);
404
405 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200406 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000407
408 return -EOPNOTSUPP;
409}
410
411static void dsa_slave_get_drvinfo(struct net_device *dev,
412 struct ethtool_drvinfo *drvinfo)
413{
Jiri Pirko7826d432013-01-06 00:44:26 +0000414 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000415 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
416 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000417}
418
Guenter Roeck3d762a02014-10-29 10:45:04 -0700419static int dsa_slave_get_regs_len(struct net_device *dev)
420{
421 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500422 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700423
Vivien Didelot9d490b42016-08-23 12:38:56 -0400424 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500425 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700426
427 return -EOPNOTSUPP;
428}
429
430static void
431dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
432{
433 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500434 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700435
Vivien Didelot9d490b42016-08-23 12:38:56 -0400436 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500437 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700438}
439
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000440static int dsa_slave_nway_reset(struct net_device *dev)
441{
442 struct dsa_slave_priv *p = netdev_priv(dev);
443
444 if (p->phy != NULL)
445 return genphy_restart_aneg(p->phy);
446
447 return -EOPNOTSUPP;
448}
449
450static u32 dsa_slave_get_link(struct net_device *dev)
451{
452 struct dsa_slave_priv *p = netdev_priv(dev);
453
454 if (p->phy != NULL) {
455 genphy_update_link(p->phy);
456 return p->phy->link;
457 }
458
459 return -EOPNOTSUPP;
460}
461
Guenter Roeck6793abb2014-10-29 10:45:01 -0700462static int dsa_slave_get_eeprom_len(struct net_device *dev)
463{
464 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500465 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700466
Andrew Lunn0e576042016-06-04 21:16:52 +0200467 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200468 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700469
Vivien Didelot9d490b42016-08-23 12:38:56 -0400470 if (ds->ops->get_eeprom_len)
471 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700472
473 return 0;
474}
475
476static int dsa_slave_get_eeprom(struct net_device *dev,
477 struct ethtool_eeprom *eeprom, u8 *data)
478{
479 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500480 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700481
Vivien Didelot9d490b42016-08-23 12:38:56 -0400482 if (ds->ops->get_eeprom)
483 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700484
485 return -EOPNOTSUPP;
486}
487
488static int dsa_slave_set_eeprom(struct net_device *dev,
489 struct ethtool_eeprom *eeprom, u8 *data)
490{
491 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500492 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700493
Vivien Didelot9d490b42016-08-23 12:38:56 -0400494 if (ds->ops->set_eeprom)
495 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700496
497 return -EOPNOTSUPP;
498}
499
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000500static void dsa_slave_get_strings(struct net_device *dev,
501 uint32_t stringset, uint8_t *data)
502{
503 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500504 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000505
506 if (stringset == ETH_SS_STATS) {
507 int len = ETH_GSTRING_LEN;
508
509 strncpy(data, "tx_packets", len);
510 strncpy(data + len, "tx_bytes", len);
511 strncpy(data + 2 * len, "rx_packets", len);
512 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400513 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500514 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000515 }
516}
517
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700518static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
519 struct ethtool_stats *stats,
520 uint64_t *data)
521{
522 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400523 struct dsa_switch *ds = dst->cpu_dp->ds;
524 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700525 int count = 0;
526
527 if (dst->master_ethtool_ops.get_sset_count) {
528 count = dst->master_ethtool_ops.get_sset_count(dev,
529 ETH_SS_STATS);
530 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
531 }
532
Vivien Didelot9d490b42016-08-23 12:38:56 -0400533 if (ds->ops->get_ethtool_stats)
534 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700535}
536
537static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
538{
539 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400540 struct dsa_switch *ds = dst->cpu_dp->ds;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700541 int count = 0;
542
543 if (dst->master_ethtool_ops.get_sset_count)
544 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
545
Vivien Didelot9d490b42016-08-23 12:38:56 -0400546 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
547 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700548
549 return count;
550}
551
552static void dsa_cpu_port_get_strings(struct net_device *dev,
553 uint32_t stringset, uint8_t *data)
554{
555 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400556 struct dsa_switch *ds = dst->cpu_dp->ds;
557 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700558 int len = ETH_GSTRING_LEN;
559 int mcount = 0, count;
560 unsigned int i;
561 uint8_t pfx[4];
562 uint8_t *ndata;
563
564 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
565 /* We do not want to be NULL-terminated, since this is a prefix */
566 pfx[sizeof(pfx) - 1] = '_';
567
568 if (dst->master_ethtool_ops.get_sset_count) {
569 mcount = dst->master_ethtool_ops.get_sset_count(dev,
570 ETH_SS_STATS);
571 dst->master_ethtool_ops.get_strings(dev, stringset, data);
572 }
573
Vivien Didelot9d490b42016-08-23 12:38:56 -0400574 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700575 ndata = data + mcount * len;
576 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
577 * the output after to prepend our CPU port prefix we
578 * constructed earlier
579 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400580 ds->ops->get_strings(ds, cpu_port, ndata);
581 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700582 for (i = 0; i < count; i++) {
583 memmove(ndata + (i * len + sizeof(pfx)),
584 ndata + i * len, len - sizeof(pfx));
585 memcpy(ndata + i * len, pfx, sizeof(pfx));
586 }
587 }
588}
589
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000590static void dsa_slave_get_ethtool_stats(struct net_device *dev,
591 struct ethtool_stats *stats,
592 uint64_t *data)
593{
594 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500595 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000596
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400597 data[0] = dev->stats.tx_packets;
598 data[1] = dev->stats.tx_bytes;
599 data[2] = dev->stats.rx_packets;
600 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400601 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500602 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000603}
604
605static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
606{
607 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500608 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000609
610 if (sset == ETH_SS_STATS) {
611 int count;
612
613 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400614 if (ds->ops->get_sset_count)
615 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000616
617 return count;
618 }
619
620 return -EOPNOTSUPP;
621}
622
Florian Fainelli19e57c42014-09-18 17:31:24 -0700623static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
624{
625 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500626 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700627
Vivien Didelot9d490b42016-08-23 12:38:56 -0400628 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500629 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700630}
631
632static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
633{
634 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500635 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700636 int ret = -EOPNOTSUPP;
637
Vivien Didelot9d490b42016-08-23 12:38:56 -0400638 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500639 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700640
641 return ret;
642}
643
Florian Fainelli79052882014-09-24 17:05:21 -0700644static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
645{
646 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500647 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700648 int ret;
649
Vivien Didelot9d490b42016-08-23 12:38:56 -0400650 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700651 return -EOPNOTSUPP;
652
Vivien Didelotafdcf152017-01-27 15:29:39 -0500653 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700654 if (ret)
655 return ret;
656
657 if (p->phy)
658 ret = phy_ethtool_set_eee(p->phy, e);
659
660 return ret;
661}
662
663static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
664{
665 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500666 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700667 int ret;
668
Vivien Didelot9d490b42016-08-23 12:38:56 -0400669 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700670 return -EOPNOTSUPP;
671
Vivien Didelotafdcf152017-01-27 15:29:39 -0500672 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700673 if (ret)
674 return ret;
675
676 if (p->phy)
677 ret = phy_ethtool_get_eee(p->phy, e);
678
679 return ret;
680}
681
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700682#ifdef CONFIG_NET_POLL_CONTROLLER
683static int dsa_slave_netpoll_setup(struct net_device *dev,
684 struct netpoll_info *ni)
685{
686 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500687 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700688 struct net_device *master = ds->dst->master_netdev;
689 struct netpoll *netpoll;
690 int err = 0;
691
692 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
693 if (!netpoll)
694 return -ENOMEM;
695
696 err = __netpoll_setup(netpoll, master);
697 if (err) {
698 kfree(netpoll);
699 goto out;
700 }
701
702 p->netpoll = netpoll;
703out:
704 return err;
705}
706
707static void dsa_slave_netpoll_cleanup(struct net_device *dev)
708{
709 struct dsa_slave_priv *p = netdev_priv(dev);
710 struct netpoll *netpoll = p->netpoll;
711
712 if (!netpoll)
713 return;
714
715 p->netpoll = NULL;
716
717 __netpoll_free_async(netpoll);
718}
719
720static void dsa_slave_poll_controller(struct net_device *dev)
721{
722}
723#endif
724
Florian Fainelli44bb7652017-01-10 12:32:36 -0800725static int dsa_slave_get_phys_port_name(struct net_device *dev,
726 char *name, size_t len)
727{
728 struct dsa_slave_priv *p = netdev_priv(dev);
729
Vivien Didelotafdcf152017-01-27 15:29:39 -0500730 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -0800731 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -0800732
733 return 0;
734}
735
Florian Fainellif50f2122017-01-30 12:41:40 -0800736static struct dsa_mall_tc_entry *
737dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
738 unsigned long cookie)
739{
740 struct dsa_mall_tc_entry *mall_tc_entry;
741
742 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
743 if (mall_tc_entry->cookie == cookie)
744 return mall_tc_entry;
745
746 return NULL;
747}
748
749static int dsa_slave_add_cls_matchall(struct net_device *dev,
750 __be16 protocol,
751 struct tc_cls_matchall_offload *cls,
752 bool ingress)
753{
754 struct dsa_slave_priv *p = netdev_priv(dev);
755 struct dsa_mall_tc_entry *mall_tc_entry;
756 struct dsa_switch *ds = p->dp->ds;
757 struct net *net = dev_net(dev);
758 struct dsa_slave_priv *to_p;
759 struct net_device *to_dev;
760 const struct tc_action *a;
761 int err = -EOPNOTSUPP;
762 LIST_HEAD(actions);
763 int ifindex;
764
765 if (!ds->ops->port_mirror_add)
766 return err;
767
768 if (!tc_single_action(cls->exts))
769 return err;
770
771 tcf_exts_to_list(cls->exts, &actions);
772 a = list_first_entry(&actions, struct tc_action, list);
773
774 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
775 struct dsa_mall_mirror_tc_entry *mirror;
776
777 ifindex = tcf_mirred_ifindex(a);
778 to_dev = __dev_get_by_index(net, ifindex);
779 if (!to_dev)
780 return -EINVAL;
781
782 if (!dsa_slave_dev_check(to_dev))
783 return -EOPNOTSUPP;
784
785 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
786 if (!mall_tc_entry)
787 return -ENOMEM;
788
789 mall_tc_entry->cookie = cls->cookie;
790 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
791 mirror = &mall_tc_entry->mirror;
792
793 to_p = netdev_priv(to_dev);
794
795 mirror->to_local_port = to_p->dp->index;
796 mirror->ingress = ingress;
797
798 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
799 ingress);
800 if (err) {
801 kfree(mall_tc_entry);
802 return err;
803 }
804
805 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
806 }
807
808 return 0;
809}
810
811static void dsa_slave_del_cls_matchall(struct net_device *dev,
812 struct tc_cls_matchall_offload *cls)
813{
814 struct dsa_slave_priv *p = netdev_priv(dev);
815 struct dsa_mall_tc_entry *mall_tc_entry;
816 struct dsa_switch *ds = p->dp->ds;
817
818 if (!ds->ops->port_mirror_del)
819 return;
820
821 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
822 if (!mall_tc_entry)
823 return;
824
825 list_del(&mall_tc_entry->list);
826
827 switch (mall_tc_entry->type) {
828 case DSA_PORT_MALL_MIRROR:
829 ds->ops->port_mirror_del(ds, p->dp->index,
830 &mall_tc_entry->mirror);
831 break;
832 default:
833 WARN_ON(1);
834 }
835
836 kfree(mall_tc_entry);
837}
838
839static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
840 __be16 protocol, struct tc_to_netdev *tc)
841{
842 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
843 int ret = -EOPNOTSUPP;
844
845 switch (tc->type) {
846 case TC_SETUP_MATCHALL:
847 switch (tc->cls_mall->command) {
848 case TC_CLSMATCHALL_REPLACE:
849 return dsa_slave_add_cls_matchall(dev, protocol,
850 tc->cls_mall,
851 ingress);
852 case TC_CLSMATCHALL_DESTROY:
853 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
854 return 0;
855 }
856 default:
857 break;
858 }
859
860 return ret;
861}
862
Florian Fainelliaf421922016-06-07 16:32:41 -0700863void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
864{
865 ops->get_sset_count = dsa_cpu_port_get_sset_count;
866 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
867 ops->get_strings = dsa_cpu_port_get_strings;
868}
869
Florian Fainellibf9f2642017-01-30 09:48:40 -0800870static int dsa_slave_get_rxnfc(struct net_device *dev,
871 struct ethtool_rxnfc *nfc, u32 *rule_locs)
872{
873 struct dsa_slave_priv *p = netdev_priv(dev);
874 struct dsa_switch *ds = p->dp->ds;
875
876 if (!ds->ops->get_rxnfc)
877 return -EOPNOTSUPP;
878
879 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
880}
881
882static int dsa_slave_set_rxnfc(struct net_device *dev,
883 struct ethtool_rxnfc *nfc)
884{
885 struct dsa_slave_priv *p = netdev_priv(dev);
886 struct dsa_switch *ds = p->dp->ds;
887
888 if (!ds->ops->set_rxnfc)
889 return -EOPNOTSUPP;
890
891 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
892}
893
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000894static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000895 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700896 .get_regs_len = dsa_slave_get_regs_len,
897 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000898 .nway_reset = dsa_slave_nway_reset,
899 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700900 .get_eeprom_len = dsa_slave_get_eeprom_len,
901 .get_eeprom = dsa_slave_get_eeprom,
902 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000903 .get_strings = dsa_slave_get_strings,
904 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
905 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700906 .set_wol = dsa_slave_set_wol,
907 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700908 .set_eee = dsa_slave_set_eee,
909 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200910 .get_link_ksettings = dsa_slave_get_link_ksettings,
911 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -0800912 .get_rxnfc = dsa_slave_get_rxnfc,
913 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000914};
915
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700916static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800917 .ndo_open = dsa_slave_open,
918 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700919 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800920 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
921 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800922 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400923 .ndo_fdb_add = switchdev_port_fdb_add,
924 .ndo_fdb_del = switchdev_port_fdb_del,
925 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800926 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +0200927 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700928#ifdef CONFIG_NET_POLL_CONTROLLER
929 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
930 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
931 .ndo_poll_controller = dsa_slave_poll_controller,
932#endif
Vivien Didelot11149532015-08-13 12:52:17 -0400933 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
934 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
935 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -0800936 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -0800937 .ndo_setup_tc = dsa_slave_setup_tc,
Scott Feldman98237d42015-03-15 21:07:15 -0700938};
939
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700940static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700941 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -0700942 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400943 .switchdev_port_obj_add = dsa_slave_port_obj_add,
944 .switchdev_port_obj_del = dsa_slave_port_obj_del,
945 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800946};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000947
Florian Fainellif37db852015-09-23 18:19:58 -0700948static struct device_type dsa_type = {
949 .name = "dsa",
950};
951
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700952static void dsa_slave_adjust_link(struct net_device *dev)
953{
954 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500955 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700956 unsigned int status_changed = 0;
957
958 if (p->old_link != p->phy->link) {
959 status_changed = 1;
960 p->old_link = p->phy->link;
961 }
962
963 if (p->old_duplex != p->phy->duplex) {
964 status_changed = 1;
965 p->old_duplex = p->phy->duplex;
966 }
967
968 if (p->old_pause != p->phy->pause) {
969 status_changed = 1;
970 p->old_pause = p->phy->pause;
971 }
972
Vivien Didelot9d490b42016-08-23 12:38:56 -0400973 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500974 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700975
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700976 if (status_changed)
977 phy_print_status(p->phy);
978}
979
Florian Fainellice31b312014-08-27 17:04:54 -0700980static int dsa_slave_fixed_link_update(struct net_device *dev,
981 struct fixed_phy_status *status)
982{
Andrew Lunnb71be352016-03-12 00:01:37 +0100983 struct dsa_slave_priv *p;
984 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -0700985
Andrew Lunnb71be352016-03-12 00:01:37 +0100986 if (dev) {
987 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500988 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400989 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500990 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +0100991 }
Florian Fainellice31b312014-08-27 17:04:54 -0700992
993 return 0;
994}
995
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000996/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -0700997static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700998 struct net_device *slave_dev,
999 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001000{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001001 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001002
Andrew Lunn7f854422016-01-06 20:11:18 +01001003 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001004 if (!p->phy) {
1005 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001006 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001007 }
Florian Fainellic305c162015-03-10 16:57:12 -07001008
1009 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001010 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1011 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001012 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1013 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001014}
1015
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001016static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001017 struct net_device *slave_dev)
1018{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001019 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001020 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001021 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001022 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001023 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001024
Vivien Didelotafdcf152017-01-27 15:29:39 -05001025 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001026 mode = of_get_phy_mode(port_dn);
1027 if (mode < 0)
1028 mode = PHY_INTERFACE_MODE_NA;
1029 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001030
1031 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001032 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001033 /* In the case of a fixed PHY, the DT node associated
1034 * to the fixed PHY is the Port DT node
1035 */
1036 ret = of_phy_register_fixed_link(port_dn);
1037 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001038 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001039 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001040 }
Florian Fainellice31b312014-08-27 17:04:54 -07001041 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001042 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001043 }
1044
Vivien Didelot9d490b42016-08-23 12:38:56 -04001045 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001046 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001047
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001048 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001049 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1050
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001051 /* If this PHY address is part of phys_mii_mask, which means
1052 * that we need to divert reads and writes to/from it, then we
1053 * want to bind this device using the slave MII bus created by
1054 * DSA to make that happen.
1055 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001056 if (!phy_is_fixed && phy_id >= 0 &&
1057 (ds->phys_mii_mask & (1 << phy_id))) {
1058 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1059 if (ret) {
1060 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001061 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001062 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001063 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001064 } else {
1065 p->phy = of_phy_connect(slave_dev, phy_dn,
1066 dsa_slave_adjust_link,
1067 phy_flags,
1068 p->phy_interface);
1069 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001070
1071 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001072 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001073
Florian Fainellice31b312014-08-27 17:04:54 -07001074 if (p->phy && phy_is_fixed)
1075 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1076
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001077 /* We could not connect to a designated PHY, so use the switch internal
1078 * MDIO bus instead
1079 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001080 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001081 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001082 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001083 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1084 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001085 if (phy_is_fixed)
1086 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001087 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001088 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001089 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001090
Andrew Lunn22209432016-01-06 20:11:13 +01001091 phy_attached_info(p->phy);
1092
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001093 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001094}
1095
Andrew Lunn448b4482015-05-06 01:09:56 +02001096static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1097static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1098 struct netdev_queue *txq,
1099 void *_unused)
1100{
1101 lockdep_set_class(&txq->_xmit_lock,
1102 &dsa_slave_netdev_xmit_lock_key);
1103}
1104
Florian Fainelli24462542014-09-18 17:31:22 -07001105int dsa_slave_suspend(struct net_device *slave_dev)
1106{
1107 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1108
Florian Fainellif154be22017-01-25 09:10:41 -08001109 netif_device_detach(slave_dev);
1110
Florian Fainelli24462542014-09-18 17:31:22 -07001111 if (p->phy) {
1112 phy_stop(p->phy);
1113 p->old_pause = -1;
1114 p->old_link = -1;
1115 p->old_duplex = -1;
1116 phy_suspend(p->phy);
1117 }
1118
1119 return 0;
1120}
1121
1122int dsa_slave_resume(struct net_device *slave_dev)
1123{
1124 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1125
1126 netif_device_attach(slave_dev);
1127
1128 if (p->phy) {
1129 phy_resume(p->phy);
1130 phy_start(p->phy);
1131 }
1132
1133 return 0;
1134}
1135
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001136int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001137 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001138{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001139 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001140 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001141 struct net_device *slave_dev;
1142 struct dsa_slave_priv *p;
1143 int ret;
1144
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001145 master = ds->dst->master_netdev;
1146 if (ds->master_netdev)
1147 master = ds->master_netdev;
1148
Tom Gundersenc835a672014-07-14 16:37:24 +02001149 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1150 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001151 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001152 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001153
Florian Fainellif50f2122017-01-30 12:41:40 -08001154 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1155 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001156 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001157 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001158 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001159 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001160 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001161 slave_dev->min_mtu = 0;
1162 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001163 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001164
Andrew Lunn448b4482015-05-06 01:09:56 +02001165 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1166 NULL);
1167
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001168 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001169 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001170 slave_dev->vlan_features = master->vlan_features;
1171
1172 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001173 p->dp = &ds->ports[port];
Florian Fainellif50f2122017-01-30 12:41:40 -08001174 INIT_LIST_HEAD(&p->mall_tc_list);
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001175 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001176
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001177 p->old_pause = -1;
1178 p->old_link = -1;
1179 p->old_duplex = -1;
1180
Andrew Lunnc8b09802016-06-04 21:16:57 +02001181 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001182 ret = register_netdev(slave_dev);
1183 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001184 netdev_err(master, "error %d registering interface %s\n",
1185 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001186 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001187 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001188 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001189 }
1190
1191 netif_carrier_off(slave_dev);
1192
Andrew Lunn0071f562016-01-06 20:11:20 +01001193 ret = dsa_slave_phy_setup(p, slave_dev);
1194 if (ret) {
1195 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001196 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001197 free_netdev(slave_dev);
1198 return ret;
1199 }
1200
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001201 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001202}
Florian Fainellib73adef2015-02-24 13:15:33 -08001203
Neil Armstrongcda5c152015-12-07 13:57:35 +01001204void dsa_slave_destroy(struct net_device *slave_dev)
1205{
1206 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001207 struct device_node *port_dn;
1208
Vivien Didelotafdcf152017-01-27 15:29:39 -05001209 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001210
1211 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001212 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001213 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001214
1215 if (of_phy_is_fixed_link(port_dn))
1216 of_phy_deregister_fixed_link(port_dn);
1217 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001218 unregister_netdev(slave_dev);
1219 free_netdev(slave_dev);
1220}
1221
Florian Fainellib73adef2015-02-24 13:15:33 -08001222static bool dsa_slave_dev_check(struct net_device *dev)
1223{
1224 return dev->netdev_ops == &dsa_slave_netdev_ops;
1225}
1226
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001227static int dsa_slave_changeupper(struct net_device *dev,
1228 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001229{
Vivien Didelot17d78022017-05-19 17:00:38 -04001230 struct dsa_slave_priv *p = netdev_priv(dev);
1231 struct dsa_port *dp = p->dp;
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001232 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001233
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001234 if (netif_is_bridge_master(info->upper_dev)) {
1235 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001236 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001237 err = notifier_from_errno(err);
1238 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001239 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001240 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001241 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001242 }
1243
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001244 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001245}
1246
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001247static int dsa_slave_netdevice_event(struct notifier_block *nb,
1248 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001249{
Vivien Didelot6debb682016-03-13 16:21:34 -04001250 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001251
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001252 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1253 return NOTIFY_DONE;
1254
1255 if (event == NETDEV_CHANGEUPPER)
1256 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001257
Florian Fainellib73adef2015-02-24 13:15:33 -08001258 return NOTIFY_DONE;
1259}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001260
1261static struct notifier_block dsa_slave_nb __read_mostly = {
1262 .notifier_call = dsa_slave_netdevice_event,
1263};
1264
1265int dsa_slave_register_notifier(void)
1266{
1267 return register_netdevice_notifier(&dsa_slave_nb);
1268}
1269
1270void dsa_slave_unregister_notifier(void)
1271{
1272 int err;
1273
1274 err = unregister_netdevice_notifier(&dsa_slave_nb);
1275 if (err)
1276 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1277}