blob: d7883e55fe15aac9711b7c3c31a2b484be558e95 [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 Fainellib73adef2015-02-24 13:15:33 -080019#include <net/rtnetlink.h>
Scott Feldman98237d42015-03-15 21:07:15 -070020#include <net/switchdev.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080021#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070022#include <linux/netpoll.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000023#include "dsa_priv.h"
24
25/* slave mii_bus handling ***************************************************/
26static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
27{
28 struct dsa_switch *ds = bus->priv;
29
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070030 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040031 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000032
33 return 0xffff;
34}
35
36static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
37{
38 struct dsa_switch *ds = bus->priv;
39
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070040 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040041 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000042
43 return 0;
44}
45
46void dsa_slave_mii_bus_init(struct dsa_switch *ds)
47{
48 ds->slave_mii_bus->priv = (void *)ds;
49 ds->slave_mii_bus->name = "dsa slave smi";
50 ds->slave_mii_bus->read = dsa_slave_phy_read;
51 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070052 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
53 ds->dst->tree, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020054 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050055 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000056}
57
58
59/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020060static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000061{
62 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000063
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020064 return p->parent->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000065}
66
Florian Fainellib73adef2015-02-24 13:15:33 -080067static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
68{
69 return !!p->bridge_dev;
70}
71
Vivien Didelot4acfee82016-09-22 16:49:21 -040072static void dsa_port_set_stp_state(struct dsa_switch *ds, int port, u8 state)
73{
Vivien Didelot732f7942016-09-22 16:49:22 -040074 struct dsa_port *dp = &ds->ports[port];
75
Vivien Didelot4acfee82016-09-22 16:49:21 -040076 if (ds->ops->port_stp_state_set)
77 ds->ops->port_stp_state_set(ds, port, state);
Vivien Didelot732f7942016-09-22 16:49:22 -040078
79 if (ds->ops->port_fast_age) {
80 /* Fast age FDB entries or flush appropriate forwarding database
81 * for the given port, if we are moving it from Learning or
82 * Forwarding state, to Disabled or Blocking or Listening state.
83 */
84
85 if ((dp->stp_state == BR_STATE_LEARNING ||
86 dp->stp_state == BR_STATE_FORWARDING) &&
87 (state == BR_STATE_DISABLED ||
88 state == BR_STATE_BLOCKING ||
89 state == BR_STATE_LISTENING))
90 ds->ops->port_fast_age(ds, port);
91 }
92
93 dp->stp_state = state;
Vivien Didelot4acfee82016-09-22 16:49:21 -040094}
95
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000096static int dsa_slave_open(struct net_device *dev)
97{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080098 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +000099 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -0700100 struct dsa_switch *ds = p->parent;
Florian Fainellib73adef2015-02-24 13:15:33 -0800101 u8 stp_state = dsa_port_is_bridged(p) ?
102 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800103 int err;
104
105 if (!(master->flags & IFF_UP))
106 return -ENETDOWN;
107
Joe Perches8feedbb2012-05-08 18:56:57 +0000108 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000109 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800110 if (err < 0)
111 goto out;
112 }
113
114 if (dev->flags & IFF_ALLMULTI) {
115 err = dev_set_allmulti(master, 1);
116 if (err < 0)
117 goto del_unicast;
118 }
119 if (dev->flags & IFF_PROMISC) {
120 err = dev_set_promiscuity(master, 1);
121 if (err < 0)
122 goto clear_allmulti;
123 }
124
Vivien Didelot9d490b42016-08-23 12:38:56 -0400125 if (ds->ops->port_enable) {
126 err = ds->ops->port_enable(ds, p->port, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700127 if (err)
128 goto clear_promisc;
129 }
130
Vivien Didelot4acfee82016-09-22 16:49:21 -0400131 dsa_port_set_stp_state(ds, p->port, stp_state);
Florian Fainellib73adef2015-02-24 13:15:33 -0800132
Florian Fainellif7f1de52014-09-24 17:05:17 -0700133 if (p->phy)
134 phy_start(p->phy);
135
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000136 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800137
Florian Fainellib2f2af22014-09-24 17:05:18 -0700138clear_promisc:
139 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300140 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800141clear_allmulti:
142 if (dev->flags & IFF_ALLMULTI)
143 dev_set_allmulti(master, -1);
144del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000145 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000146 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800147out:
148 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000149}
150
151static int dsa_slave_close(struct net_device *dev)
152{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800153 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000154 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -0700155 struct dsa_switch *ds = p->parent;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800156
Florian Fainellif7f1de52014-09-24 17:05:17 -0700157 if (p->phy)
158 phy_stop(p->phy);
159
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800160 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000161 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800162 if (dev->flags & IFF_ALLMULTI)
163 dev_set_allmulti(master, -1);
164 if (dev->flags & IFF_PROMISC)
165 dev_set_promiscuity(master, -1);
166
Joe Perches8feedbb2012-05-08 18:56:57 +0000167 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000168 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800169
Vivien Didelot9d490b42016-08-23 12:38:56 -0400170 if (ds->ops->port_disable)
171 ds->ops->port_disable(ds, p->port, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700172
Vivien Didelot4acfee82016-09-22 16:49:21 -0400173 dsa_port_set_stp_state(ds, p->port, BR_STATE_DISABLED);
Florian Fainellib73adef2015-02-24 13:15:33 -0800174
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000175 return 0;
176}
177
178static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
179{
180 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000181 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000182
Rundong Ge072e11e2019-02-02 14:29:35 +0000183 if (dev->flags & IFF_UP) {
184 if (change & IFF_ALLMULTI)
185 dev_set_allmulti(master,
186 dev->flags & IFF_ALLMULTI ? 1 : -1);
187 if (change & IFF_PROMISC)
188 dev_set_promiscuity(master,
189 dev->flags & IFF_PROMISC ? 1 : -1);
190 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000191}
192
193static void dsa_slave_set_rx_mode(struct net_device *dev)
194{
195 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000196 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000197
198 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000199 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000200}
201
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800202static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000203{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800204 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000205 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800206 struct sockaddr *addr = a;
207 int err;
208
209 if (!is_valid_ether_addr(addr->sa_data))
210 return -EADDRNOTAVAIL;
211
212 if (!(dev->flags & IFF_UP))
213 goto out;
214
Joe Perches8feedbb2012-05-08 18:56:57 +0000215 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000216 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800217 if (err < 0)
218 return err;
219 }
220
Joe Perches8feedbb2012-05-08 18:56:57 +0000221 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000222 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800223
224out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800225 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000226
227 return 0;
228}
229
Vivien Didelot11149532015-08-13 12:52:17 -0400230static int dsa_slave_port_vlan_add(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200231 const struct switchdev_obj_port_vlan *vlan,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200232 struct switchdev_trans *trans)
Vivien Didelot11149532015-08-13 12:52:17 -0400233{
Vivien Didelot11149532015-08-13 12:52:17 -0400234 struct dsa_slave_priv *p = netdev_priv(dev);
235 struct dsa_switch *ds = p->parent;
Vivien Didelot11149532015-08-13 12:52:17 -0400236
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200237 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400238 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
Vivien Didelot11149532015-08-13 12:52:17 -0400239 return -EOPNOTSUPP;
240
Vivien Didelot9d490b42016-08-23 12:38:56 -0400241 return ds->ops->port_vlan_prepare(ds, p->port, vlan, trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400242 }
243
Vivien Didelot9d490b42016-08-23 12:38:56 -0400244 ds->ops->port_vlan_add(ds, p->port, vlan, trans);
Vivien Didelot4d5770b2016-04-06 11:55:05 -0400245
Vivien Didelot11149532015-08-13 12:52:17 -0400246 return 0;
247}
248
249static int dsa_slave_port_vlan_del(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200250 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot11149532015-08-13 12:52:17 -0400251{
Vivien Didelot11149532015-08-13 12:52:17 -0400252 struct dsa_slave_priv *p = netdev_priv(dev);
253 struct dsa_switch *ds = p->parent;
Vivien Didelot11149532015-08-13 12:52:17 -0400254
Vivien Didelot9d490b42016-08-23 12:38:56 -0400255 if (!ds->ops->port_vlan_del)
Vivien Didelot11149532015-08-13 12:52:17 -0400256 return -EOPNOTSUPP;
257
Vivien Didelot9d490b42016-08-23 12:38:56 -0400258 return ds->ops->port_vlan_del(ds, p->port, vlan);
Vivien Didelot11149532015-08-13 12:52:17 -0400259}
260
261static int dsa_slave_port_vlan_dump(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200262 struct switchdev_obj_port_vlan *vlan,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200263 switchdev_obj_dump_cb_t *cb)
Vivien Didelot11149532015-08-13 12:52:17 -0400264{
Vivien Didelot11149532015-08-13 12:52:17 -0400265 struct dsa_slave_priv *p = netdev_priv(dev);
266 struct dsa_switch *ds = p->parent;
Vivien Didelot11149532015-08-13 12:52:17 -0400267
Vivien Didelot9d490b42016-08-23 12:38:56 -0400268 if (ds->ops->port_vlan_dump)
269 return ds->ops->port_vlan_dump(ds, p->port, vlan, cb);
Vivien Didelot65aebfc2016-02-23 12:13:54 -0500270
Vivien Didelot477b1842016-02-23 12:13:56 -0500271 return -EOPNOTSUPP;
Vivien Didelot11149532015-08-13 12:52:17 -0400272}
273
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400274static int dsa_slave_port_fdb_add(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200275 const struct switchdev_obj_port_fdb *fdb,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200276 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700277{
278 struct dsa_slave_priv *p = netdev_priv(dev);
279 struct dsa_switch *ds = p->parent;
Vivien Didelot146a3202015-10-08 11:35:12 -0400280
Vivien Didelot8497aa62016-04-06 11:55:04 -0400281 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400282 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
Vivien Didelot8497aa62016-04-06 11:55:04 -0400283 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700284
Vivien Didelot9d490b42016-08-23 12:38:56 -0400285 return ds->ops->port_fdb_prepare(ds, p->port, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400286 }
David S. Millercdf09692015-08-11 12:00:37 -0700287
Vivien Didelot9d490b42016-08-23 12:38:56 -0400288 ds->ops->port_fdb_add(ds, p->port, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400289
290 return 0;
David S. Millercdf09692015-08-11 12:00:37 -0700291}
292
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400293static int dsa_slave_port_fdb_del(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200294 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -0700295{
296 struct dsa_slave_priv *p = netdev_priv(dev);
297 struct dsa_switch *ds = p->parent;
298 int ret = -EOPNOTSUPP;
299
Vivien Didelot9d490b42016-08-23 12:38:56 -0400300 if (ds->ops->port_fdb_del)
301 ret = ds->ops->port_fdb_del(ds, p->port, fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700302
303 return ret;
304}
305
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400306static int dsa_slave_port_fdb_dump(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200307 struct switchdev_obj_port_fdb *fdb,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200308 switchdev_obj_dump_cb_t *cb)
David S. Millercdf09692015-08-11 12:00:37 -0700309{
310 struct dsa_slave_priv *p = netdev_priv(dev);
311 struct dsa_switch *ds = p->parent;
David S. Millercdf09692015-08-11 12:00:37 -0700312
Vivien Didelot9d490b42016-08-23 12:38:56 -0400313 if (ds->ops->port_fdb_dump)
314 return ds->ops->port_fdb_dump(ds, p->port, fdb, cb);
Vivien Didelotea70ba92015-10-22 09:34:38 -0400315
Vivien Didelot1a49a2f2015-10-22 09:34:43 -0400316 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700317}
318
Vivien Didelot8df30252016-08-31 11:50:03 -0400319static int dsa_slave_port_mdb_add(struct net_device *dev,
320 const struct switchdev_obj_port_mdb *mdb,
321 struct switchdev_trans *trans)
322{
323 struct dsa_slave_priv *p = netdev_priv(dev);
324 struct dsa_switch *ds = p->parent;
325
326 if (switchdev_trans_ph_prepare(trans)) {
327 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
328 return -EOPNOTSUPP;
329
330 return ds->ops->port_mdb_prepare(ds, p->port, mdb, trans);
331 }
332
333 ds->ops->port_mdb_add(ds, p->port, mdb, trans);
334
335 return 0;
336}
337
338static int dsa_slave_port_mdb_del(struct net_device *dev,
339 const struct switchdev_obj_port_mdb *mdb)
340{
341 struct dsa_slave_priv *p = netdev_priv(dev);
342 struct dsa_switch *ds = p->parent;
343
344 if (ds->ops->port_mdb_del)
345 return ds->ops->port_mdb_del(ds, p->port, mdb);
346
347 return -EOPNOTSUPP;
348}
349
350static int dsa_slave_port_mdb_dump(struct net_device *dev,
351 struct switchdev_obj_port_mdb *mdb,
352 switchdev_obj_dump_cb_t *cb)
353{
354 struct dsa_slave_priv *p = netdev_priv(dev);
355 struct dsa_switch *ds = p->parent;
356
357 if (ds->ops->port_mdb_dump)
358 return ds->ops->port_mdb_dump(ds, p->port, mdb, cb);
359
360 return -EOPNOTSUPP;
361}
362
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000363static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
364{
365 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000366
367 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000368 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000369
370 return -EOPNOTSUPP;
371}
372
Vivien Didelot43c44a92016-04-06 11:55:03 -0400373static int dsa_slave_stp_state_set(struct net_device *dev,
374 const struct switchdev_attr *attr,
375 struct switchdev_trans *trans)
Florian Fainellib73adef2015-02-24 13:15:33 -0800376{
377 struct dsa_slave_priv *p = netdev_priv(dev);
378 struct dsa_switch *ds = p->parent;
Florian Fainellib73adef2015-02-24 13:15:33 -0800379
Vivien Didelot43c44a92016-04-06 11:55:03 -0400380 if (switchdev_trans_ph_prepare(trans))
Vivien Didelot9d490b42016-08-23 12:38:56 -0400381 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
Florian Fainellib73adef2015-02-24 13:15:33 -0800382
Vivien Didelot4acfee82016-09-22 16:49:21 -0400383 dsa_port_set_stp_state(ds, p->port, attr->u.stp_state);
Vivien Didelot43c44a92016-04-06 11:55:03 -0400384
385 return 0;
Florian Fainellib73adef2015-02-24 13:15:33 -0800386}
387
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500388static int dsa_slave_vlan_filtering(struct net_device *dev,
389 const struct switchdev_attr *attr,
390 struct switchdev_trans *trans)
391{
392 struct dsa_slave_priv *p = netdev_priv(dev);
393 struct dsa_switch *ds = p->parent;
394
395 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
396 if (switchdev_trans_ph_prepare(trans))
397 return 0;
398
Vivien Didelot9d490b42016-08-23 12:38:56 -0400399 if (ds->ops->port_vlan_filtering)
400 return ds->ops->port_vlan_filtering(ds, p->port,
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500401 attr->u.vlan_filtering);
402
403 return 0;
404}
405
Vivien Didelot34a79f62016-07-18 20:45:38 -0400406static int dsa_fastest_ageing_time(struct dsa_switch *ds,
407 unsigned int ageing_time)
408{
409 int i;
410
411 for (i = 0; i < DSA_MAX_PORTS; ++i) {
412 struct dsa_port *dp = &ds->ports[i];
413
414 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
415 ageing_time = dp->ageing_time;
416 }
417
418 return ageing_time;
419}
420
421static int dsa_slave_ageing_time(struct net_device *dev,
422 const struct switchdev_attr *attr,
423 struct switchdev_trans *trans)
424{
425 struct dsa_slave_priv *p = netdev_priv(dev);
426 struct dsa_switch *ds = p->parent;
427 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
428 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
429
430 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
431 if (switchdev_trans_ph_prepare(trans))
432 return 0;
433
434 /* Keep the fastest ageing time in case of multiple bridges */
435 ds->ports[p->port].ageing_time = ageing_time;
436 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
437
Vivien Didelot9d490b42016-08-23 12:38:56 -0400438 if (ds->ops->set_ageing_time)
439 return ds->ops->set_ageing_time(ds, ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400440
441 return 0;
442}
443
Scott Feldman35636062015-05-10 09:47:51 -0700444static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200445 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200446 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700447{
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400448 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700449
450 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200451 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelot43c44a92016-04-06 11:55:03 -0400452 ret = dsa_slave_stp_state_set(dev, attr, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700453 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500454 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
455 ret = dsa_slave_vlan_filtering(dev, attr, trans);
456 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400457 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
458 ret = dsa_slave_ageing_time(dev, attr, trans);
459 break;
Scott Feldman35636062015-05-10 09:47:51 -0700460 default:
461 ret = -EOPNOTSUPP;
462 break;
463 }
464
465 return ret;
466}
467
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400468static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200469 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200470 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400471{
472 int err;
473
474 /* For the prepare phase, ensure the full set of changes is feasable in
475 * one go in order to signal a failure properly. If an operation is not
476 * supported, return -EOPNOTSUPP.
477 */
478
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200479 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200480 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200481 err = dsa_slave_port_fdb_add(dev,
482 SWITCHDEV_OBJ_PORT_FDB(obj),
483 trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400484 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400485 case SWITCHDEV_OBJ_ID_PORT_MDB:
486 err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
487 trans);
488 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200489 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200490 err = dsa_slave_port_vlan_add(dev,
491 SWITCHDEV_OBJ_PORT_VLAN(obj),
492 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400493 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400494 default:
495 err = -EOPNOTSUPP;
496 break;
497 }
498
499 return err;
500}
501
502static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200503 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400504{
505 int err;
506
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200507 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200508 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200509 err = dsa_slave_port_fdb_del(dev,
510 SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400511 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400512 case SWITCHDEV_OBJ_ID_PORT_MDB:
513 err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
514 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200515 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200516 err = dsa_slave_port_vlan_del(dev,
517 SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400518 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400519 default:
520 err = -EOPNOTSUPP;
521 break;
522 }
523
524 return err;
525}
526
527static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200528 struct switchdev_obj *obj,
529 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400530{
531 int err;
532
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200533 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200534 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200535 err = dsa_slave_port_fdb_dump(dev,
536 SWITCHDEV_OBJ_PORT_FDB(obj),
537 cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400538 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400539 case SWITCHDEV_OBJ_ID_PORT_MDB:
540 err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
541 cb);
542 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200543 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200544 err = dsa_slave_port_vlan_dump(dev,
545 SWITCHDEV_OBJ_PORT_VLAN(obj),
546 cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400547 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400548 default:
549 err = -EOPNOTSUPP;
550 break;
551 }
552
553 return err;
554}
555
Florian Fainellib73adef2015-02-24 13:15:33 -0800556static int dsa_slave_bridge_port_join(struct net_device *dev,
557 struct net_device *br)
558{
559 struct dsa_slave_priv *p = netdev_priv(dev);
560 struct dsa_switch *ds = p->parent;
561 int ret = -EOPNOTSUPP;
562
563 p->bridge_dev = br;
564
Vivien Didelot9d490b42016-08-23 12:38:56 -0400565 if (ds->ops->port_bridge_join)
566 ret = ds->ops->port_bridge_join(ds, p->port, br);
Florian Fainellib73adef2015-02-24 13:15:33 -0800567
Vivien Didelot6debb682016-03-13 16:21:34 -0400568 return ret == -EOPNOTSUPP ? 0 : ret;
Florian Fainellib73adef2015-02-24 13:15:33 -0800569}
570
Vivien Didelot16bfa702016-03-13 16:21:33 -0400571static void dsa_slave_bridge_port_leave(struct net_device *dev)
Florian Fainellib73adef2015-02-24 13:15:33 -0800572{
573 struct dsa_slave_priv *p = netdev_priv(dev);
574 struct dsa_switch *ds = p->parent;
Florian Fainellib73adef2015-02-24 13:15:33 -0800575
576
Vivien Didelot9d490b42016-08-23 12:38:56 -0400577 if (ds->ops->port_bridge_leave)
578 ds->ops->port_bridge_leave(ds, p->port);
Florian Fainellib73adef2015-02-24 13:15:33 -0800579
580 p->bridge_dev = NULL;
581
582 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
583 * so allow it to be in BR_STATE_FORWARDING to be kept functional
584 */
Vivien Didelot4acfee82016-09-22 16:49:21 -0400585 dsa_port_set_stp_state(ds, p->port, BR_STATE_FORWARDING);
Florian Fainellib73adef2015-02-24 13:15:33 -0800586}
587
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700588static int dsa_slave_port_attr_get(struct net_device *dev,
589 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800590{
591 struct dsa_slave_priv *p = netdev_priv(dev);
592 struct dsa_switch *ds = p->parent;
593
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700594 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200595 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700596 attr->u.ppid.id_len = sizeof(ds->index);
597 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700598 break;
599 default:
600 return -EOPNOTSUPP;
601 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800602
603 return 0;
604}
605
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700606static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
607 struct sk_buff *skb)
608{
609#ifdef CONFIG_NET_POLL_CONTROLLER
610 if (p->netpoll)
611 netpoll_send_skb(p->netpoll, skb);
612#else
613 BUG();
614#endif
615 return NETDEV_TX_OK;
616}
617
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700618static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
619{
620 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700621 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700622
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700623 dev->stats.tx_packets++;
624 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700625
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700626 /* Transmit function may have to reallocate the original SKB */
627 nskb = p->xmit(skb, dev);
628 if (!nskb)
629 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700630
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700631 /* SKB for netpoll still need to be mangled with the protocol-specific
632 * tag to be successfully transmitted
633 */
634 if (unlikely(netpoll_tx_running(dev)))
635 return dsa_netpoll_send_skb(p, nskb);
636
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700637 /* Queue the SKB for transmission on the parent interface, but
638 * do not modify its EtherType
639 */
640 nskb->dev = p->parent->dst->master_netdev;
641 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700642
643 return NETDEV_TX_OK;
644}
645
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000646/* ethtool operations *******************************************************/
647static int
648dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
649{
650 struct dsa_slave_priv *p = netdev_priv(dev);
651 int err;
652
653 err = -EOPNOTSUPP;
654 if (p->phy != NULL) {
655 err = phy_read_status(p->phy);
656 if (err == 0)
657 err = phy_ethtool_gset(p->phy, cmd);
658 }
659
660 return err;
661}
662
663static int
664dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
665{
666 struct dsa_slave_priv *p = netdev_priv(dev);
667
668 if (p->phy != NULL)
669 return phy_ethtool_sset(p->phy, cmd);
670
671 return -EOPNOTSUPP;
672}
673
674static void dsa_slave_get_drvinfo(struct net_device *dev,
675 struct ethtool_drvinfo *drvinfo)
676{
Jiri Pirko7826d432013-01-06 00:44:26 +0000677 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
678 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
679 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
680 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000681}
682
Guenter Roeck3d762a02014-10-29 10:45:04 -0700683static int dsa_slave_get_regs_len(struct net_device *dev)
684{
685 struct dsa_slave_priv *p = netdev_priv(dev);
686 struct dsa_switch *ds = p->parent;
687
Vivien Didelot9d490b42016-08-23 12:38:56 -0400688 if (ds->ops->get_regs_len)
689 return ds->ops->get_regs_len(ds, p->port);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700690
691 return -EOPNOTSUPP;
692}
693
694static void
695dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
696{
697 struct dsa_slave_priv *p = netdev_priv(dev);
698 struct dsa_switch *ds = p->parent;
699
Vivien Didelot9d490b42016-08-23 12:38:56 -0400700 if (ds->ops->get_regs)
701 ds->ops->get_regs(ds, p->port, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700702}
703
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000704static int dsa_slave_nway_reset(struct net_device *dev)
705{
706 struct dsa_slave_priv *p = netdev_priv(dev);
707
708 if (p->phy != NULL)
709 return genphy_restart_aneg(p->phy);
710
711 return -EOPNOTSUPP;
712}
713
714static u32 dsa_slave_get_link(struct net_device *dev)
715{
716 struct dsa_slave_priv *p = netdev_priv(dev);
717
718 if (p->phy != NULL) {
719 genphy_update_link(p->phy);
720 return p->phy->link;
721 }
722
723 return -EOPNOTSUPP;
724}
725
Guenter Roeck6793abb2014-10-29 10:45:01 -0700726static int dsa_slave_get_eeprom_len(struct net_device *dev)
727{
728 struct dsa_slave_priv *p = netdev_priv(dev);
729 struct dsa_switch *ds = p->parent;
730
Andrew Lunn0e576042016-06-04 21:16:52 +0200731 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200732 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700733
Vivien Didelot9d490b42016-08-23 12:38:56 -0400734 if (ds->ops->get_eeprom_len)
735 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700736
737 return 0;
738}
739
740static int dsa_slave_get_eeprom(struct net_device *dev,
741 struct ethtool_eeprom *eeprom, u8 *data)
742{
743 struct dsa_slave_priv *p = netdev_priv(dev);
744 struct dsa_switch *ds = p->parent;
745
Vivien Didelot9d490b42016-08-23 12:38:56 -0400746 if (ds->ops->get_eeprom)
747 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700748
749 return -EOPNOTSUPP;
750}
751
752static int dsa_slave_set_eeprom(struct net_device *dev,
753 struct ethtool_eeprom *eeprom, u8 *data)
754{
755 struct dsa_slave_priv *p = netdev_priv(dev);
756 struct dsa_switch *ds = p->parent;
757
Vivien Didelot9d490b42016-08-23 12:38:56 -0400758 if (ds->ops->set_eeprom)
759 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700760
761 return -EOPNOTSUPP;
762}
763
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000764static void dsa_slave_get_strings(struct net_device *dev,
765 uint32_t stringset, uint8_t *data)
766{
767 struct dsa_slave_priv *p = netdev_priv(dev);
768 struct dsa_switch *ds = p->parent;
769
770 if (stringset == ETH_SS_STATS) {
771 int len = ETH_GSTRING_LEN;
772
773 strncpy(data, "tx_packets", len);
774 strncpy(data + len, "tx_bytes", len);
775 strncpy(data + 2 * len, "rx_packets", len);
776 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400777 if (ds->ops->get_strings)
778 ds->ops->get_strings(ds, p->port, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000779 }
780}
781
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700782static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
783 struct ethtool_stats *stats,
784 uint64_t *data)
785{
786 struct dsa_switch_tree *dst = dev->dsa_ptr;
787 struct dsa_switch *ds = dst->ds[0];
788 s8 cpu_port = dst->cpu_port;
789 int count = 0;
790
791 if (dst->master_ethtool_ops.get_sset_count) {
792 count = dst->master_ethtool_ops.get_sset_count(dev,
793 ETH_SS_STATS);
794 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
795 }
796
Vivien Didelot9d490b42016-08-23 12:38:56 -0400797 if (ds->ops->get_ethtool_stats)
798 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700799}
800
801static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
802{
803 struct dsa_switch_tree *dst = dev->dsa_ptr;
804 struct dsa_switch *ds = dst->ds[0];
805 int count = 0;
806
807 if (dst->master_ethtool_ops.get_sset_count)
808 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
809
Vivien Didelot9d490b42016-08-23 12:38:56 -0400810 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
811 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700812
813 return count;
814}
815
816static void dsa_cpu_port_get_strings(struct net_device *dev,
817 uint32_t stringset, uint8_t *data)
818{
819 struct dsa_switch_tree *dst = dev->dsa_ptr;
820 struct dsa_switch *ds = dst->ds[0];
821 s8 cpu_port = dst->cpu_port;
822 int len = ETH_GSTRING_LEN;
823 int mcount = 0, count;
824 unsigned int i;
825 uint8_t pfx[4];
826 uint8_t *ndata;
827
828 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
829 /* We do not want to be NULL-terminated, since this is a prefix */
830 pfx[sizeof(pfx) - 1] = '_';
831
832 if (dst->master_ethtool_ops.get_sset_count) {
833 mcount = dst->master_ethtool_ops.get_sset_count(dev,
834 ETH_SS_STATS);
835 dst->master_ethtool_ops.get_strings(dev, stringset, data);
836 }
837
Vivien Didelot9d490b42016-08-23 12:38:56 -0400838 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700839 ndata = data + mcount * len;
840 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
841 * the output after to prepend our CPU port prefix we
842 * constructed earlier
843 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400844 ds->ops->get_strings(ds, cpu_port, ndata);
845 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700846 for (i = 0; i < count; i++) {
847 memmove(ndata + (i * len + sizeof(pfx)),
848 ndata + i * len, len - sizeof(pfx));
849 memcpy(ndata + i * len, pfx, sizeof(pfx));
850 }
851 }
852}
853
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000854static void dsa_slave_get_ethtool_stats(struct net_device *dev,
855 struct ethtool_stats *stats,
856 uint64_t *data)
857{
858 struct dsa_slave_priv *p = netdev_priv(dev);
859 struct dsa_switch *ds = p->parent;
860
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400861 data[0] = dev->stats.tx_packets;
862 data[1] = dev->stats.tx_bytes;
863 data[2] = dev->stats.rx_packets;
864 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400865 if (ds->ops->get_ethtool_stats)
866 ds->ops->get_ethtool_stats(ds, p->port, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000867}
868
869static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
870{
871 struct dsa_slave_priv *p = netdev_priv(dev);
872 struct dsa_switch *ds = p->parent;
873
874 if (sset == ETH_SS_STATS) {
875 int count;
876
877 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400878 if (ds->ops->get_sset_count)
879 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000880
881 return count;
882 }
883
884 return -EOPNOTSUPP;
885}
886
Florian Fainelli19e57c42014-09-18 17:31:24 -0700887static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
888{
889 struct dsa_slave_priv *p = netdev_priv(dev);
890 struct dsa_switch *ds = p->parent;
891
Vivien Didelot9d490b42016-08-23 12:38:56 -0400892 if (ds->ops->get_wol)
893 ds->ops->get_wol(ds, p->port, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700894}
895
896static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
897{
898 struct dsa_slave_priv *p = netdev_priv(dev);
899 struct dsa_switch *ds = p->parent;
900 int ret = -EOPNOTSUPP;
901
Vivien Didelot9d490b42016-08-23 12:38:56 -0400902 if (ds->ops->set_wol)
903 ret = ds->ops->set_wol(ds, p->port, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700904
905 return ret;
906}
907
Florian Fainelli79052882014-09-24 17:05:21 -0700908static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
909{
910 struct dsa_slave_priv *p = netdev_priv(dev);
911 struct dsa_switch *ds = p->parent;
912 int ret;
913
Vivien Didelot9d490b42016-08-23 12:38:56 -0400914 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700915 return -EOPNOTSUPP;
916
Vivien Didelot9d490b42016-08-23 12:38:56 -0400917 ret = ds->ops->set_eee(ds, p->port, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700918 if (ret)
919 return ret;
920
921 if (p->phy)
922 ret = phy_ethtool_set_eee(p->phy, e);
923
924 return ret;
925}
926
927static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
928{
929 struct dsa_slave_priv *p = netdev_priv(dev);
930 struct dsa_switch *ds = p->parent;
931 int ret;
932
Vivien Didelot9d490b42016-08-23 12:38:56 -0400933 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700934 return -EOPNOTSUPP;
935
Vivien Didelot9d490b42016-08-23 12:38:56 -0400936 ret = ds->ops->get_eee(ds, p->port, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700937 if (ret)
938 return ret;
939
940 if (p->phy)
941 ret = phy_ethtool_get_eee(p->phy, e);
942
943 return ret;
944}
945
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700946#ifdef CONFIG_NET_POLL_CONTROLLER
947static int dsa_slave_netpoll_setup(struct net_device *dev,
948 struct netpoll_info *ni)
949{
950 struct dsa_slave_priv *p = netdev_priv(dev);
951 struct dsa_switch *ds = p->parent;
952 struct net_device *master = ds->dst->master_netdev;
953 struct netpoll *netpoll;
954 int err = 0;
955
956 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
957 if (!netpoll)
958 return -ENOMEM;
959
960 err = __netpoll_setup(netpoll, master);
961 if (err) {
962 kfree(netpoll);
963 goto out;
964 }
965
966 p->netpoll = netpoll;
967out:
968 return err;
969}
970
971static void dsa_slave_netpoll_cleanup(struct net_device *dev)
972{
973 struct dsa_slave_priv *p = netdev_priv(dev);
974 struct netpoll *netpoll = p->netpoll;
975
976 if (!netpoll)
977 return;
978
979 p->netpoll = NULL;
980
981 __netpoll_free_async(netpoll);
982}
983
984static void dsa_slave_poll_controller(struct net_device *dev)
985{
986}
987#endif
988
Florian Fainelliaf421922016-06-07 16:32:41 -0700989void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
990{
991 ops->get_sset_count = dsa_cpu_port_get_sset_count;
992 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
993 ops->get_strings = dsa_cpu_port_get_strings;
994}
995
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000996static const struct ethtool_ops dsa_slave_ethtool_ops = {
997 .get_settings = dsa_slave_get_settings,
998 .set_settings = dsa_slave_set_settings,
999 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001000 .get_regs_len = dsa_slave_get_regs_len,
1001 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001002 .nway_reset = dsa_slave_nway_reset,
1003 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001004 .get_eeprom_len = dsa_slave_get_eeprom_len,
1005 .get_eeprom = dsa_slave_get_eeprom,
1006 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001007 .get_strings = dsa_slave_get_strings,
1008 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1009 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001010 .set_wol = dsa_slave_set_wol,
1011 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001012 .set_eee = dsa_slave_set_eee,
1013 .get_eee = dsa_slave_get_eee,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001014};
1015
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001016static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001017 .ndo_open = dsa_slave_open,
1018 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001019 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001020 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1021 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001022 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001023 .ndo_fdb_add = switchdev_port_fdb_add,
1024 .ndo_fdb_del = switchdev_port_fdb_del,
1025 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001026 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001027 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001028#ifdef CONFIG_NET_POLL_CONTROLLER
1029 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1030 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1031 .ndo_poll_controller = dsa_slave_poll_controller,
1032#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001033 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1034 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1035 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Scott Feldman98237d42015-03-15 21:07:15 -07001036};
1037
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001038static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001039 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001040 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001041 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1042 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1043 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001044};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001045
Florian Fainellif37db852015-09-23 18:19:58 -07001046static struct device_type dsa_type = {
1047 .name = "dsa",
1048};
1049
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001050static void dsa_slave_adjust_link(struct net_device *dev)
1051{
1052 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001053 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001054 unsigned int status_changed = 0;
1055
1056 if (p->old_link != p->phy->link) {
1057 status_changed = 1;
1058 p->old_link = p->phy->link;
1059 }
1060
1061 if (p->old_duplex != p->phy->duplex) {
1062 status_changed = 1;
1063 p->old_duplex = p->phy->duplex;
1064 }
1065
1066 if (p->old_pause != p->phy->pause) {
1067 status_changed = 1;
1068 p->old_pause = p->phy->pause;
1069 }
1070
Vivien Didelot9d490b42016-08-23 12:38:56 -04001071 if (ds->ops->adjust_link && status_changed)
1072 ds->ops->adjust_link(ds, p->port, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001073
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001074 if (status_changed)
1075 phy_print_status(p->phy);
1076}
1077
Florian Fainellice31b312014-08-27 17:04:54 -07001078static int dsa_slave_fixed_link_update(struct net_device *dev,
1079 struct fixed_phy_status *status)
1080{
Andrew Lunnb71be352016-03-12 00:01:37 +01001081 struct dsa_slave_priv *p;
1082 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001083
Andrew Lunnb71be352016-03-12 00:01:37 +01001084 if (dev) {
1085 p = netdev_priv(dev);
1086 ds = p->parent;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001087 if (ds->ops->fixed_link_update)
1088 ds->ops->fixed_link_update(ds, p->port, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001089 }
Florian Fainellice31b312014-08-27 17:04:54 -07001090
1091 return 0;
1092}
1093
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001094/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001095static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001096 struct net_device *slave_dev,
1097 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001098{
1099 struct dsa_switch *ds = p->parent;
1100
Andrew Lunn7f854422016-01-06 20:11:18 +01001101 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001102 if (!p->phy) {
1103 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001104 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001105 }
Florian Fainellic305c162015-03-10 16:57:12 -07001106
1107 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001108 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1109 p->phy_interface = p->phy->interface;
Florian Fainellie5a2ba92017-01-20 16:05:05 -08001110 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1111 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001112}
1113
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001114static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001115 struct net_device *slave_dev)
1116{
1117 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001118 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001119 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001120 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001121 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001122
Andrew Lunn189b0d92016-06-04 21:16:58 +02001123 port_dn = ds->ports[p->port].dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001124 mode = of_get_phy_mode(port_dn);
1125 if (mode < 0)
1126 mode = PHY_INTERFACE_MODE_NA;
1127 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001128
1129 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001130 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001131 /* In the case of a fixed PHY, the DT node associated
1132 * to the fixed PHY is the Port DT node
1133 */
1134 ret = of_phy_register_fixed_link(port_dn);
1135 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001136 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001137 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001138 }
Florian Fainellice31b312014-08-27 17:04:54 -07001139 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001140 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001141 }
1142
Vivien Didelot9d490b42016-08-23 12:38:56 -04001143 if (ds->ops->get_phy_flags)
1144 phy_flags = ds->ops->get_phy_flags(ds, p->port);
Florian Fainelli68195632014-09-19 13:07:54 -07001145
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001146 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001147 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1148
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001149 /* If this PHY address is part of phys_mii_mask, which means
1150 * that we need to divert reads and writes to/from it, then we
1151 * want to bind this device using the slave MII bus created by
1152 * DSA to make that happen.
1153 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001154 if (!phy_is_fixed && phy_id >= 0 &&
1155 (ds->phys_mii_mask & (1 << phy_id))) {
1156 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1157 if (ret) {
1158 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001159 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001160 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001161 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001162 } else {
1163 p->phy = of_phy_connect(slave_dev, phy_dn,
1164 dsa_slave_adjust_link,
1165 phy_flags,
1166 p->phy_interface);
1167 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001168
1169 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001170 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001171
Florian Fainellice31b312014-08-27 17:04:54 -07001172 if (p->phy && phy_is_fixed)
1173 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1174
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001175 /* We could not connect to a designated PHY, so use the switch internal
1176 * MDIO bus instead
1177 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001178 if (!p->phy) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001179 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
Russell Kingd25b8e72015-10-03 18:09:07 +01001180 if (ret) {
1181 netdev_err(slave_dev, "failed to connect to port %d: %d\n", p->port, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001182 if (phy_is_fixed)
1183 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001184 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001185 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001186 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001187
Andrew Lunn22209432016-01-06 20:11:13 +01001188 phy_attached_info(p->phy);
1189
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001190 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001191}
1192
Andrew Lunn448b4482015-05-06 01:09:56 +02001193static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1194static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1195 struct netdev_queue *txq,
1196 void *_unused)
1197{
1198 lockdep_set_class(&txq->_xmit_lock,
1199 &dsa_slave_netdev_xmit_lock_key);
1200}
1201
Florian Fainelli24462542014-09-18 17:31:22 -07001202int dsa_slave_suspend(struct net_device *slave_dev)
1203{
1204 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1205
Florian Fainelliab9a0f82018-07-31 17:12:52 -07001206 if (!netif_running(slave_dev))
1207 return 0;
1208
Florian Fainelli9f42bc4f2017-01-25 09:10:41 -08001209 netif_device_detach(slave_dev);
1210
Florian Fainelli24462542014-09-18 17:31:22 -07001211 if (p->phy) {
1212 phy_stop(p->phy);
1213 p->old_pause = -1;
1214 p->old_link = -1;
1215 p->old_duplex = -1;
1216 phy_suspend(p->phy);
1217 }
1218
1219 return 0;
1220}
1221
1222int dsa_slave_resume(struct net_device *slave_dev)
1223{
1224 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1225
Florian Fainelliab9a0f82018-07-31 17:12:52 -07001226 if (!netif_running(slave_dev))
1227 return 0;
1228
Florian Fainelli24462542014-09-18 17:31:22 -07001229 netif_device_attach(slave_dev);
1230
1231 if (p->phy) {
1232 phy_resume(p->phy);
1233 phy_start(p->phy);
1234 }
1235
1236 return 0;
1237}
1238
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001239int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001240 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001241{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001242 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001243 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001244 struct net_device *slave_dev;
1245 struct dsa_slave_priv *p;
1246 int ret;
1247
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001248 master = ds->dst->master_netdev;
1249 if (ds->master_netdev)
1250 master = ds->master_netdev;
1251
Tom Gundersenc835a672014-07-14 16:37:24 +02001252 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1253 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001254 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001255 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001256
1257 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001258 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001259 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001260 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001261 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001262 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Florian Fainellif37db852015-09-23 18:19:58 -07001263 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001264
Andrew Lunn448b4482015-05-06 01:09:56 +02001265 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1266 NULL);
1267
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001268 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001269 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001270 slave_dev->vlan_features = master->vlan_features;
1271
1272 p = netdev_priv(slave_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001273 p->parent = ds;
1274 p->port = port;
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001275 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001276
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001277 p->old_pause = -1;
1278 p->old_link = -1;
1279 p->old_duplex = -1;
1280
Andrew Lunnc8b09802016-06-04 21:16:57 +02001281 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001282
1283 netif_carrier_off(slave_dev);
1284
Andrew Lunn0071f562016-01-06 20:11:20 +01001285 ret = dsa_slave_phy_setup(p, slave_dev);
1286 if (ret) {
1287 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli6eab1f82017-09-25 15:55:53 -07001288 goto out_free;
1289 }
1290
1291 ret = register_netdev(slave_dev);
1292 if (ret) {
1293 netdev_err(master, "error %d registering interface %s\n",
1294 ret, slave_dev->name);
1295 goto out_phy;
Andrew Lunn0071f562016-01-06 20:11:20 +01001296 }
1297
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001298 return 0;
Florian Fainelli6eab1f82017-09-25 15:55:53 -07001299
1300out_phy:
1301 phy_disconnect(p->phy);
1302 if (of_phy_is_fixed_link(ds->ports[port].dn))
1303 of_phy_deregister_fixed_link(ds->ports[port].dn);
1304out_free:
1305 free_netdev(slave_dev);
1306 ds->ports[port].netdev = NULL;
1307 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001308}
Florian Fainellib73adef2015-02-24 13:15:33 -08001309
Neil Armstrongcda5c152015-12-07 13:57:35 +01001310void dsa_slave_destroy(struct net_device *slave_dev)
1311{
1312 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001313 struct dsa_switch *ds = p->parent;
1314 struct device_node *port_dn;
1315
1316 port_dn = ds->ports[p->port].dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001317
1318 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001319 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001320 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001321
1322 if (of_phy_is_fixed_link(port_dn))
1323 of_phy_deregister_fixed_link(port_dn);
1324 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001325 unregister_netdev(slave_dev);
1326 free_netdev(slave_dev);
1327}
1328
Florian Fainellib73adef2015-02-24 13:15:33 -08001329static bool dsa_slave_dev_check(struct net_device *dev)
1330{
1331 return dev->netdev_ops == &dsa_slave_netdev_ops;
1332}
1333
Vivien Didelot6debb682016-03-13 16:21:34 -04001334static int dsa_slave_port_upper_event(struct net_device *dev,
1335 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001336{
Vivien Didelot6debb682016-03-13 16:21:34 -04001337 struct netdev_notifier_changeupper_info *info = ptr;
1338 struct net_device *upper = info->upper_dev;
Florian Fainellib73adef2015-02-24 13:15:33 -08001339 int err = 0;
1340
Vivien Didelot6debb682016-03-13 16:21:34 -04001341 switch (event) {
1342 case NETDEV_CHANGEUPPER:
1343 if (netif_is_bridge_master(upper)) {
1344 if (info->linking)
1345 err = dsa_slave_bridge_port_join(dev, upper);
1346 else
1347 dsa_slave_bridge_port_leave(dev);
1348 }
Florian Fainellib73adef2015-02-24 13:15:33 -08001349
Vivien Didelot6debb682016-03-13 16:21:34 -04001350 break;
1351 }
1352
1353 return notifier_from_errno(err);
1354}
1355
1356static int dsa_slave_port_event(struct net_device *dev, unsigned long event,
1357 void *ptr)
1358{
1359 switch (event) {
1360 case NETDEV_CHANGEUPPER:
1361 return dsa_slave_port_upper_event(dev, event, ptr);
1362 }
1363
1364 return NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001365}
1366
1367int dsa_slave_netdevice_event(struct notifier_block *unused,
1368 unsigned long event, void *ptr)
1369{
Vivien Didelot6debb682016-03-13 16:21:34 -04001370 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001371
Vivien Didelot6debb682016-03-13 16:21:34 -04001372 if (dsa_slave_dev_check(dev))
1373 return dsa_slave_port_event(dev, event, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001374
Florian Fainellib73adef2015-02-24 13:15:33 -08001375 return NOTIFY_DONE;
1376}