blob: 1ad62ef8c261369102d28392ed425e74f905873b [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
Vivien Didelota93ecdd2017-05-19 17:00:37 -040030static int dsa_port_notify(struct dsa_port *dp, unsigned long e, void *v)
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050031{
Vivien Didelota93ecdd2017-05-19 17:00:37 -040032 struct raw_notifier_head *nh = &dp->ds->dst->nh;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050033 int err;
34
35 err = raw_notifier_call_chain(nh, e, v);
36
37 return notifier_to_errno(err);
38}
39
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000040/* slave mii_bus handling ***************************************************/
41static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
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_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000047
48 return 0xffff;
49}
50
51static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
52{
53 struct dsa_switch *ds = bus->priv;
54
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070055 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040056 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000057
58 return 0;
59}
60
61void dsa_slave_mii_bus_init(struct dsa_switch *ds)
62{
63 ds->slave_mii_bus->priv = (void *)ds;
64 ds->slave_mii_bus->name = "dsa slave smi";
65 ds->slave_mii_bus->read = dsa_slave_phy_read;
66 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070067 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
68 ds->dst->tree, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020069 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050070 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071}
72
73
74/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020075static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000076{
77 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000078
Vivien Didelotafdcf152017-01-27 15:29:39 -050079 return p->dp->ds->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000080}
81
Vivien Didelota5e9a022017-01-27 15:29:40 -050082static inline bool dsa_port_is_bridged(struct dsa_port *dp)
Florian Fainellib73adef2015-02-24 13:15:33 -080083{
Vivien Didelota5e9a022017-01-27 15:29:40 -050084 return !!dp->bridge_dev;
Florian Fainellib73adef2015-02-24 13:15:33 -080085}
86
Vivien Didelotfd364542017-05-19 17:00:36 -040087static int dsa_port_set_state(struct dsa_port *dp, u8 state,
88 struct switchdev_trans *trans)
Vivien Didelot4acfee82016-09-22 16:49:21 -040089{
Vivien Didelotc5d35cb2017-02-03 13:20:19 -050090 struct dsa_switch *ds = dp->ds;
91 int port = dp->index;
Vivien Didelot732f7942016-09-22 16:49:22 -040092
Vivien Didelotfd364542017-05-19 17:00:36 -040093 if (switchdev_trans_ph_prepare(trans))
94 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
95
Vivien Didelot4acfee82016-09-22 16:49:21 -040096 if (ds->ops->port_stp_state_set)
97 ds->ops->port_stp_state_set(ds, port, state);
Vivien Didelot732f7942016-09-22 16:49:22 -040098
99 if (ds->ops->port_fast_age) {
100 /* Fast age FDB entries or flush appropriate forwarding database
101 * for the given port, if we are moving it from Learning or
102 * Forwarding state, to Disabled or Blocking or Listening state.
103 */
104
105 if ((dp->stp_state == BR_STATE_LEARNING ||
106 dp->stp_state == BR_STATE_FORWARDING) &&
107 (state == BR_STATE_DISABLED ||
108 state == BR_STATE_BLOCKING ||
109 state == BR_STATE_LISTENING))
110 ds->ops->port_fast_age(ds, port);
111 }
112
113 dp->stp_state = state;
Vivien Didelotfd364542017-05-19 17:00:36 -0400114
115 return 0;
116}
117
118static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
119{
120 int err;
121
122 err = dsa_port_set_state(dp, state, NULL);
123 if (err)
124 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
Vivien Didelot4acfee82016-09-22 16:49:21 -0400125}
126
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000127static int dsa_slave_open(struct net_device *dev)
128{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800129 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500130 struct net_device *master = p->dp->ds->dst->master_netdev;
131 struct dsa_switch *ds = p->dp->ds;
Vivien Didelota5e9a022017-01-27 15:29:40 -0500132 u8 stp_state = dsa_port_is_bridged(p->dp) ?
Florian Fainellib73adef2015-02-24 13:15:33 -0800133 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800134 int err;
135
136 if (!(master->flags & IFF_UP))
137 return -ENETDOWN;
138
Joe Perches8feedbb2012-05-08 18:56:57 +0000139 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000140 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800141 if (err < 0)
142 goto out;
143 }
144
145 if (dev->flags & IFF_ALLMULTI) {
146 err = dev_set_allmulti(master, 1);
147 if (err < 0)
148 goto del_unicast;
149 }
150 if (dev->flags & IFF_PROMISC) {
151 err = dev_set_promiscuity(master, 1);
152 if (err < 0)
153 goto clear_allmulti;
154 }
155
Vivien Didelot9d490b42016-08-23 12:38:56 -0400156 if (ds->ops->port_enable) {
Vivien Didelotafdcf152017-01-27 15:29:39 -0500157 err = ds->ops->port_enable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700158 if (err)
159 goto clear_promisc;
160 }
161
Vivien Didelotfd364542017-05-19 17:00:36 -0400162 dsa_port_set_state_now(p->dp, stp_state);
Florian Fainellib73adef2015-02-24 13:15:33 -0800163
Florian Fainellif7f1de52014-09-24 17:05:17 -0700164 if (p->phy)
165 phy_start(p->phy);
166
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000167 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800168
Florian Fainellib2f2af22014-09-24 17:05:18 -0700169clear_promisc:
170 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300171 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800172clear_allmulti:
173 if (dev->flags & IFF_ALLMULTI)
174 dev_set_allmulti(master, -1);
175del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000176 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000177 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800178out:
179 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000180}
181
182static int dsa_slave_close(struct net_device *dev)
183{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800184 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500185 struct net_device *master = p->dp->ds->dst->master_netdev;
186 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800187
Florian Fainellif7f1de52014-09-24 17:05:17 -0700188 if (p->phy)
189 phy_stop(p->phy);
190
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800191 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000192 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800193 if (dev->flags & IFF_ALLMULTI)
194 dev_set_allmulti(master, -1);
195 if (dev->flags & IFF_PROMISC)
196 dev_set_promiscuity(master, -1);
197
Joe 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
Vivien Didelot9d490b42016-08-23 12:38:56 -0400201 if (ds->ops->port_disable)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500202 ds->ops->port_disable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700203
Vivien Didelotfd364542017-05-19 17:00:36 -0400204 dsa_port_set_state_now(p->dp, BR_STATE_DISABLED);
Florian Fainellib73adef2015-02-24 13:15:33 -0800205
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000206 return 0;
207}
208
209static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
210{
211 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500212 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000213
214 if (change & IFF_ALLMULTI)
215 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
216 if (change & IFF_PROMISC)
217 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
218}
219
220static void dsa_slave_set_rx_mode(struct net_device *dev)
221{
222 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500223 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000224
225 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000226 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000227}
228
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800229static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000230{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800231 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500232 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800233 struct sockaddr *addr = a;
234 int err;
235
236 if (!is_valid_ether_addr(addr->sa_data))
237 return -EADDRNOTAVAIL;
238
239 if (!(dev->flags & IFF_UP))
240 goto out;
241
Joe Perches8feedbb2012-05-08 18:56:57 +0000242 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000243 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800244 if (err < 0)
245 return err;
246 }
247
Joe Perches8feedbb2012-05-08 18:56:57 +0000248 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000249 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800250
251out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800252 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000253
254 return 0;
255}
256
Vivien Didelot11149532015-08-13 12:52:17 -0400257static int dsa_slave_port_vlan_add(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200258 const struct switchdev_obj_port_vlan *vlan,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200259 struct switchdev_trans *trans)
Vivien Didelot11149532015-08-13 12:52:17 -0400260{
Vivien Didelot11149532015-08-13 12:52:17 -0400261 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500262 struct dsa_port *dp = p->dp;
263 struct dsa_switch *ds = dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400264
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200265 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400266 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
Vivien Didelot11149532015-08-13 12:52:17 -0400267 return -EOPNOTSUPP;
268
Vivien Didelotafdcf152017-01-27 15:29:39 -0500269 return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400270 }
271
Vivien Didelotafdcf152017-01-27 15:29:39 -0500272 ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
Vivien Didelot4d5770b2016-04-06 11:55:05 -0400273
Vivien Didelot11149532015-08-13 12:52:17 -0400274 return 0;
275}
276
277static int dsa_slave_port_vlan_del(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200278 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot11149532015-08-13 12:52:17 -0400279{
Vivien Didelot11149532015-08-13 12:52:17 -0400280 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500281 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400282
Vivien Didelot9d490b42016-08-23 12:38:56 -0400283 if (!ds->ops->port_vlan_del)
Vivien Didelot11149532015-08-13 12:52:17 -0400284 return -EOPNOTSUPP;
285
Vivien Didelotafdcf152017-01-27 15:29:39 -0500286 return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
Vivien Didelot11149532015-08-13 12:52:17 -0400287}
288
289static int dsa_slave_port_vlan_dump(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200290 struct switchdev_obj_port_vlan *vlan,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200291 switchdev_obj_dump_cb_t *cb)
Vivien Didelot11149532015-08-13 12:52:17 -0400292{
Vivien Didelot11149532015-08-13 12:52:17 -0400293 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500294 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400295
Vivien Didelot9d490b42016-08-23 12:38:56 -0400296 if (ds->ops->port_vlan_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500297 return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
Vivien Didelot65aebfc2016-02-23 12:13:54 -0500298
Vivien Didelot477b1842016-02-23 12:13:56 -0500299 return -EOPNOTSUPP;
Vivien Didelot11149532015-08-13 12:52:17 -0400300}
301
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400302static int dsa_slave_port_fdb_add(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200303 const struct switchdev_obj_port_fdb *fdb,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200304 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700305{
306 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500307 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot146a3202015-10-08 11:35:12 -0400308
Vivien Didelot8497aa62016-04-06 11:55:04 -0400309 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400310 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
Vivien Didelot8497aa62016-04-06 11:55:04 -0400311 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700312
Vivien Didelotafdcf152017-01-27 15:29:39 -0500313 return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400314 }
David S. Millercdf09692015-08-11 12:00:37 -0700315
Vivien Didelotafdcf152017-01-27 15:29:39 -0500316 ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400317
318 return 0;
David S. Millercdf09692015-08-11 12:00:37 -0700319}
320
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400321static int dsa_slave_port_fdb_del(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200322 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -0700323{
324 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500325 struct dsa_switch *ds = p->dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700326 int ret = -EOPNOTSUPP;
327
Vivien Didelot9d490b42016-08-23 12:38:56 -0400328 if (ds->ops->port_fdb_del)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500329 ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700330
331 return ret;
332}
333
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400334static int dsa_slave_port_fdb_dump(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200335 struct switchdev_obj_port_fdb *fdb,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200336 switchdev_obj_dump_cb_t *cb)
David S. Millercdf09692015-08-11 12:00:37 -0700337{
338 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500339 struct dsa_switch *ds = p->dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700340
Vivien Didelot9d490b42016-08-23 12:38:56 -0400341 if (ds->ops->port_fdb_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500342 return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb);
Vivien Didelotea70ba92015-10-22 09:34:38 -0400343
Vivien Didelot1a49a2f2015-10-22 09:34:43 -0400344 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700345}
346
Vivien Didelot8df30252016-08-31 11:50:03 -0400347static int dsa_slave_port_mdb_add(struct net_device *dev,
348 const struct switchdev_obj_port_mdb *mdb,
349 struct switchdev_trans *trans)
350{
351 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500352 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400353
354 if (switchdev_trans_ph_prepare(trans)) {
355 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
356 return -EOPNOTSUPP;
357
Vivien Didelotafdcf152017-01-27 15:29:39 -0500358 return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400359 }
360
Vivien Didelotafdcf152017-01-27 15:29:39 -0500361 ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400362
363 return 0;
364}
365
366static int dsa_slave_port_mdb_del(struct net_device *dev,
367 const struct switchdev_obj_port_mdb *mdb)
368{
369 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500370 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400371
372 if (ds->ops->port_mdb_del)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500373 return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400374
375 return -EOPNOTSUPP;
376}
377
378static int dsa_slave_port_mdb_dump(struct net_device *dev,
379 struct switchdev_obj_port_mdb *mdb,
380 switchdev_obj_dump_cb_t *cb)
381{
382 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500383 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400384
385 if (ds->ops->port_mdb_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500386 return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400387
388 return -EOPNOTSUPP;
389}
390
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000391static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
392{
393 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000394
395 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000396 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000397
398 return -EOPNOTSUPP;
399}
400
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500401static int dsa_slave_vlan_filtering(struct net_device *dev,
402 const struct switchdev_attr *attr,
403 struct switchdev_trans *trans)
404{
405 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500406 struct dsa_switch *ds = p->dp->ds;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500407
408 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
409 if (switchdev_trans_ph_prepare(trans))
410 return 0;
411
Vivien Didelot9d490b42016-08-23 12:38:56 -0400412 if (ds->ops->port_vlan_filtering)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500413 return ds->ops->port_vlan_filtering(ds, p->dp->index,
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500414 attr->u.vlan_filtering);
415
416 return 0;
417}
418
Vivien Didelote893de1ba2017-03-15 15:53:48 -0400419static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
420 unsigned int ageing_time)
Vivien Didelot34a79f62016-07-18 20:45:38 -0400421{
422 int i;
423
Vivien Didelot26895e22017-01-27 15:29:37 -0500424 for (i = 0; i < ds->num_ports; ++i) {
Vivien Didelot34a79f62016-07-18 20:45:38 -0400425 struct dsa_port *dp = &ds->ports[i];
426
427 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
428 ageing_time = dp->ageing_time;
429 }
430
431 return ageing_time;
432}
433
434static int dsa_slave_ageing_time(struct net_device *dev,
435 const struct switchdev_attr *attr,
436 struct switchdev_trans *trans)
437{
438 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500439 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400440 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
441 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
442
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400443 if (switchdev_trans_ph_prepare(trans)) {
444 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
445 return -ERANGE;
446 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
447 return -ERANGE;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400448 return 0;
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400449 }
Vivien Didelot34a79f62016-07-18 20:45:38 -0400450
451 /* Keep the fastest ageing time in case of multiple bridges */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500452 p->dp->ageing_time = ageing_time;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400453 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
454
Vivien Didelot9d490b42016-08-23 12:38:56 -0400455 if (ds->ops->set_ageing_time)
456 return ds->ops->set_ageing_time(ds, ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400457
458 return 0;
459}
460
Scott Feldman35636062015-05-10 09:47:51 -0700461static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200462 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200463 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700464{
Vivien Didelotfd364542017-05-19 17:00:36 -0400465 struct dsa_slave_priv *p = netdev_priv(dev);
466 struct dsa_port *dp = p->dp;
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400467 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700468
469 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200470 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400471 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700472 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500473 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
474 ret = dsa_slave_vlan_filtering(dev, attr, trans);
475 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400476 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
477 ret = dsa_slave_ageing_time(dev, attr, trans);
478 break;
Scott Feldman35636062015-05-10 09:47:51 -0700479 default:
480 ret = -EOPNOTSUPP;
481 break;
482 }
483
484 return ret;
485}
486
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400487static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200488 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200489 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400490{
491 int err;
492
493 /* For the prepare phase, ensure the full set of changes is feasable in
494 * one go in order to signal a failure properly. If an operation is not
495 * supported, return -EOPNOTSUPP.
496 */
497
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200498 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200499 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200500 err = dsa_slave_port_fdb_add(dev,
501 SWITCHDEV_OBJ_PORT_FDB(obj),
502 trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400503 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400504 case SWITCHDEV_OBJ_ID_PORT_MDB:
505 err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
506 trans);
507 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200508 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200509 err = dsa_slave_port_vlan_add(dev,
510 SWITCHDEV_OBJ_PORT_VLAN(obj),
511 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400512 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400513 default:
514 err = -EOPNOTSUPP;
515 break;
516 }
517
518 return err;
519}
520
521static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200522 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400523{
524 int err;
525
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200526 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200527 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200528 err = dsa_slave_port_fdb_del(dev,
529 SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400530 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400531 case SWITCHDEV_OBJ_ID_PORT_MDB:
532 err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
533 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200534 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200535 err = dsa_slave_port_vlan_del(dev,
536 SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400537 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400538 default:
539 err = -EOPNOTSUPP;
540 break;
541 }
542
543 return err;
544}
545
546static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200547 struct switchdev_obj *obj,
548 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400549{
550 int err;
551
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200552 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200553 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200554 err = dsa_slave_port_fdb_dump(dev,
555 SWITCHDEV_OBJ_PORT_FDB(obj),
556 cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400557 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400558 case SWITCHDEV_OBJ_ID_PORT_MDB:
559 err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
560 cb);
561 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200562 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200563 err = dsa_slave_port_vlan_dump(dev,
564 SWITCHDEV_OBJ_PORT_VLAN(obj),
565 cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400566 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400567 default:
568 err = -EOPNOTSUPP;
569 break;
570 }
571
572 return err;
573}
574
Vivien Didelot17d78022017-05-19 17:00:38 -0400575static int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800576{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500577 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400578 .sw_index = dp->ds->index,
579 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500580 .br = br,
581 };
582 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800583
Vivien Didelot9c265422017-02-03 13:20:18 -0500584 /* Here the port is already bridged. Reflect the current configuration
585 * so that drivers can program their chips accordingly.
586 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400587 dp->bridge_dev = br;
Florian Fainellib73adef2015-02-24 13:15:33 -0800588
Vivien Didelot17d78022017-05-19 17:00:38 -0400589 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
Florian Fainellib73adef2015-02-24 13:15:33 -0800590
Vivien Didelot9c265422017-02-03 13:20:18 -0500591 /* The bridging is rolled back on error */
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500592 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400593 dp->bridge_dev = NULL;
Vivien Didelot9c265422017-02-03 13:20:18 -0500594
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500595 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800596}
597
Vivien Didelot17d78022017-05-19 17:00:38 -0400598static void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800599{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500600 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400601 .sw_index = dp->ds->index,
602 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500603 .br = br,
604 };
605 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800606
Vivien Didelot9c265422017-02-03 13:20:18 -0500607 /* Here the port is already unbridged. Reflect the current configuration
608 * so that drivers can program their chips accordingly.
609 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400610 dp->bridge_dev = NULL;
Florian Fainellib73adef2015-02-24 13:15:33 -0800611
Vivien Didelot17d78022017-05-19 17:00:38 -0400612 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500613 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400614 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
Florian Fainellib73adef2015-02-24 13:15:33 -0800615
616 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
617 * so allow it to be in BR_STATE_FORWARDING to be kept functional
618 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400619 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
Florian Fainellib73adef2015-02-24 13:15:33 -0800620}
621
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700622static int dsa_slave_port_attr_get(struct net_device *dev,
623 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800624{
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 Fainellib73adef2015-02-24 13:15:33 -0800627
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700628 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200629 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700630 attr->u.ppid.id_len = sizeof(ds->index);
631 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700632 break;
633 default:
634 return -EOPNOTSUPP;
635 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800636
637 return 0;
638}
639
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700640static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
641 struct sk_buff *skb)
642{
643#ifdef CONFIG_NET_POLL_CONTROLLER
644 if (p->netpoll)
645 netpoll_send_skb(p->netpoll, skb);
646#else
647 BUG();
648#endif
649 return NETDEV_TX_OK;
650}
651
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700652static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
653{
654 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700655 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700656
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700657 dev->stats.tx_packets++;
658 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700659
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700660 /* Transmit function may have to reallocate the original SKB */
661 nskb = p->xmit(skb, dev);
662 if (!nskb)
663 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700664
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700665 /* SKB for netpoll still need to be mangled with the protocol-specific
666 * tag to be successfully transmitted
667 */
668 if (unlikely(netpoll_tx_running(dev)))
669 return dsa_netpoll_send_skb(p, nskb);
670
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700671 /* Queue the SKB for transmission on the parent interface, but
672 * do not modify its EtherType
673 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500674 nskb->dev = p->dp->ds->dst->master_netdev;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700675 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700676
677 return NETDEV_TX_OK;
678}
679
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000680/* ethtool operations *******************************************************/
681static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200682dsa_slave_get_link_ksettings(struct net_device *dev,
683 struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000684{
685 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellie69e4622017-02-06 15:55:23 -0800686 int err = -EOPNOTSUPP;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000687
Florian Fainellie69e4622017-02-06 15:55:23 -0800688 if (p->phy != NULL)
689 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000690
691 return err;
692}
693
694static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200695dsa_slave_set_link_ksettings(struct net_device *dev,
696 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000697{
698 struct dsa_slave_priv *p = netdev_priv(dev);
699
700 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200701 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000702
703 return -EOPNOTSUPP;
704}
705
706static void dsa_slave_get_drvinfo(struct net_device *dev,
707 struct ethtool_drvinfo *drvinfo)
708{
Jiri Pirko7826d432013-01-06 00:44:26 +0000709 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000710 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
711 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000712}
713
Guenter Roeck3d762a02014-10-29 10:45:04 -0700714static int dsa_slave_get_regs_len(struct net_device *dev)
715{
716 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500717 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700718
Vivien Didelot9d490b42016-08-23 12:38:56 -0400719 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500720 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700721
722 return -EOPNOTSUPP;
723}
724
725static void
726dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
727{
728 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500729 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700730
Vivien Didelot9d490b42016-08-23 12:38:56 -0400731 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500732 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700733}
734
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000735static int dsa_slave_nway_reset(struct net_device *dev)
736{
737 struct dsa_slave_priv *p = netdev_priv(dev);
738
739 if (p->phy != NULL)
740 return genphy_restart_aneg(p->phy);
741
742 return -EOPNOTSUPP;
743}
744
745static u32 dsa_slave_get_link(struct net_device *dev)
746{
747 struct dsa_slave_priv *p = netdev_priv(dev);
748
749 if (p->phy != NULL) {
750 genphy_update_link(p->phy);
751 return p->phy->link;
752 }
753
754 return -EOPNOTSUPP;
755}
756
Guenter Roeck6793abb2014-10-29 10:45:01 -0700757static int dsa_slave_get_eeprom_len(struct net_device *dev)
758{
759 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500760 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700761
Andrew Lunn0e576042016-06-04 21:16:52 +0200762 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200763 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700764
Vivien Didelot9d490b42016-08-23 12:38:56 -0400765 if (ds->ops->get_eeprom_len)
766 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700767
768 return 0;
769}
770
771static int dsa_slave_get_eeprom(struct net_device *dev,
772 struct ethtool_eeprom *eeprom, u8 *data)
773{
774 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500775 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700776
Vivien Didelot9d490b42016-08-23 12:38:56 -0400777 if (ds->ops->get_eeprom)
778 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700779
780 return -EOPNOTSUPP;
781}
782
783static int dsa_slave_set_eeprom(struct net_device *dev,
784 struct ethtool_eeprom *eeprom, u8 *data)
785{
786 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500787 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700788
Vivien Didelot9d490b42016-08-23 12:38:56 -0400789 if (ds->ops->set_eeprom)
790 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700791
792 return -EOPNOTSUPP;
793}
794
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000795static void dsa_slave_get_strings(struct net_device *dev,
796 uint32_t stringset, uint8_t *data)
797{
798 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500799 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000800
801 if (stringset == ETH_SS_STATS) {
802 int len = ETH_GSTRING_LEN;
803
804 strncpy(data, "tx_packets", len);
805 strncpy(data + len, "tx_bytes", len);
806 strncpy(data + 2 * len, "rx_packets", len);
807 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400808 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500809 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000810 }
811}
812
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700813static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
814 struct ethtool_stats *stats,
815 uint64_t *data)
816{
817 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400818 struct dsa_switch *ds = dst->cpu_dp->ds;
819 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700820 int count = 0;
821
822 if (dst->master_ethtool_ops.get_sset_count) {
823 count = dst->master_ethtool_ops.get_sset_count(dev,
824 ETH_SS_STATS);
825 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
826 }
827
Vivien Didelot9d490b42016-08-23 12:38:56 -0400828 if (ds->ops->get_ethtool_stats)
829 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700830}
831
832static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
833{
834 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400835 struct dsa_switch *ds = dst->cpu_dp->ds;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700836 int count = 0;
837
838 if (dst->master_ethtool_ops.get_sset_count)
839 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
840
Vivien Didelot9d490b42016-08-23 12:38:56 -0400841 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
842 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700843
844 return count;
845}
846
847static void dsa_cpu_port_get_strings(struct net_device *dev,
848 uint32_t stringset, uint8_t *data)
849{
850 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400851 struct dsa_switch *ds = dst->cpu_dp->ds;
852 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700853 int len = ETH_GSTRING_LEN;
854 int mcount = 0, count;
855 unsigned int i;
856 uint8_t pfx[4];
857 uint8_t *ndata;
858
859 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
860 /* We do not want to be NULL-terminated, since this is a prefix */
861 pfx[sizeof(pfx) - 1] = '_';
862
863 if (dst->master_ethtool_ops.get_sset_count) {
864 mcount = dst->master_ethtool_ops.get_sset_count(dev,
865 ETH_SS_STATS);
866 dst->master_ethtool_ops.get_strings(dev, stringset, data);
867 }
868
Vivien Didelot9d490b42016-08-23 12:38:56 -0400869 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700870 ndata = data + mcount * len;
871 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
872 * the output after to prepend our CPU port prefix we
873 * constructed earlier
874 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400875 ds->ops->get_strings(ds, cpu_port, ndata);
876 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700877 for (i = 0; i < count; i++) {
878 memmove(ndata + (i * len + sizeof(pfx)),
879 ndata + i * len, len - sizeof(pfx));
880 memcpy(ndata + i * len, pfx, sizeof(pfx));
881 }
882 }
883}
884
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000885static void dsa_slave_get_ethtool_stats(struct net_device *dev,
886 struct ethtool_stats *stats,
887 uint64_t *data)
888{
889 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500890 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000891
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400892 data[0] = dev->stats.tx_packets;
893 data[1] = dev->stats.tx_bytes;
894 data[2] = dev->stats.rx_packets;
895 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400896 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500897 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000898}
899
900static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
901{
902 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500903 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000904
905 if (sset == ETH_SS_STATS) {
906 int count;
907
908 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400909 if (ds->ops->get_sset_count)
910 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000911
912 return count;
913 }
914
915 return -EOPNOTSUPP;
916}
917
Florian Fainelli19e57c42014-09-18 17:31:24 -0700918static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
919{
920 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500921 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700922
Vivien Didelot9d490b42016-08-23 12:38:56 -0400923 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500924 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700925}
926
927static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
928{
929 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500930 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700931 int ret = -EOPNOTSUPP;
932
Vivien Didelot9d490b42016-08-23 12:38:56 -0400933 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500934 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700935
936 return ret;
937}
938
Florian Fainelli79052882014-09-24 17:05:21 -0700939static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
940{
941 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500942 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700943 int ret;
944
Vivien Didelot9d490b42016-08-23 12:38:56 -0400945 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700946 return -EOPNOTSUPP;
947
Vivien Didelotafdcf152017-01-27 15:29:39 -0500948 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700949 if (ret)
950 return ret;
951
952 if (p->phy)
953 ret = phy_ethtool_set_eee(p->phy, e);
954
955 return ret;
956}
957
958static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
959{
960 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500961 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700962 int ret;
963
Vivien Didelot9d490b42016-08-23 12:38:56 -0400964 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700965 return -EOPNOTSUPP;
966
Vivien Didelotafdcf152017-01-27 15:29:39 -0500967 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700968 if (ret)
969 return ret;
970
971 if (p->phy)
972 ret = phy_ethtool_get_eee(p->phy, e);
973
974 return ret;
975}
976
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700977#ifdef CONFIG_NET_POLL_CONTROLLER
978static int dsa_slave_netpoll_setup(struct net_device *dev,
979 struct netpoll_info *ni)
980{
981 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500982 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700983 struct net_device *master = ds->dst->master_netdev;
984 struct netpoll *netpoll;
985 int err = 0;
986
987 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
988 if (!netpoll)
989 return -ENOMEM;
990
991 err = __netpoll_setup(netpoll, master);
992 if (err) {
993 kfree(netpoll);
994 goto out;
995 }
996
997 p->netpoll = netpoll;
998out:
999 return err;
1000}
1001
1002static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1003{
1004 struct dsa_slave_priv *p = netdev_priv(dev);
1005 struct netpoll *netpoll = p->netpoll;
1006
1007 if (!netpoll)
1008 return;
1009
1010 p->netpoll = NULL;
1011
1012 __netpoll_free_async(netpoll);
1013}
1014
1015static void dsa_slave_poll_controller(struct net_device *dev)
1016{
1017}
1018#endif
1019
Florian Fainelli44bb7652017-01-10 12:32:36 -08001020static int dsa_slave_get_phys_port_name(struct net_device *dev,
1021 char *name, size_t len)
1022{
1023 struct dsa_slave_priv *p = netdev_priv(dev);
1024
Vivien Didelotafdcf152017-01-27 15:29:39 -05001025 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -08001026 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -08001027
1028 return 0;
1029}
1030
Florian Fainellif50f2122017-01-30 12:41:40 -08001031static struct dsa_mall_tc_entry *
1032dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1033 unsigned long cookie)
1034{
1035 struct dsa_mall_tc_entry *mall_tc_entry;
1036
1037 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1038 if (mall_tc_entry->cookie == cookie)
1039 return mall_tc_entry;
1040
1041 return NULL;
1042}
1043
1044static int dsa_slave_add_cls_matchall(struct net_device *dev,
1045 __be16 protocol,
1046 struct tc_cls_matchall_offload *cls,
1047 bool ingress)
1048{
1049 struct dsa_slave_priv *p = netdev_priv(dev);
1050 struct dsa_mall_tc_entry *mall_tc_entry;
1051 struct dsa_switch *ds = p->dp->ds;
1052 struct net *net = dev_net(dev);
1053 struct dsa_slave_priv *to_p;
1054 struct net_device *to_dev;
1055 const struct tc_action *a;
1056 int err = -EOPNOTSUPP;
1057 LIST_HEAD(actions);
1058 int ifindex;
1059
1060 if (!ds->ops->port_mirror_add)
1061 return err;
1062
1063 if (!tc_single_action(cls->exts))
1064 return err;
1065
1066 tcf_exts_to_list(cls->exts, &actions);
1067 a = list_first_entry(&actions, struct tc_action, list);
1068
1069 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1070 struct dsa_mall_mirror_tc_entry *mirror;
1071
1072 ifindex = tcf_mirred_ifindex(a);
1073 to_dev = __dev_get_by_index(net, ifindex);
1074 if (!to_dev)
1075 return -EINVAL;
1076
1077 if (!dsa_slave_dev_check(to_dev))
1078 return -EOPNOTSUPP;
1079
1080 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1081 if (!mall_tc_entry)
1082 return -ENOMEM;
1083
1084 mall_tc_entry->cookie = cls->cookie;
1085 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1086 mirror = &mall_tc_entry->mirror;
1087
1088 to_p = netdev_priv(to_dev);
1089
1090 mirror->to_local_port = to_p->dp->index;
1091 mirror->ingress = ingress;
1092
1093 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1094 ingress);
1095 if (err) {
1096 kfree(mall_tc_entry);
1097 return err;
1098 }
1099
1100 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1101 }
1102
1103 return 0;
1104}
1105
1106static void dsa_slave_del_cls_matchall(struct net_device *dev,
1107 struct tc_cls_matchall_offload *cls)
1108{
1109 struct dsa_slave_priv *p = netdev_priv(dev);
1110 struct dsa_mall_tc_entry *mall_tc_entry;
1111 struct dsa_switch *ds = p->dp->ds;
1112
1113 if (!ds->ops->port_mirror_del)
1114 return;
1115
1116 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1117 if (!mall_tc_entry)
1118 return;
1119
1120 list_del(&mall_tc_entry->list);
1121
1122 switch (mall_tc_entry->type) {
1123 case DSA_PORT_MALL_MIRROR:
1124 ds->ops->port_mirror_del(ds, p->dp->index,
1125 &mall_tc_entry->mirror);
1126 break;
1127 default:
1128 WARN_ON(1);
1129 }
1130
1131 kfree(mall_tc_entry);
1132}
1133
1134static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1135 __be16 protocol, struct tc_to_netdev *tc)
1136{
1137 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1138 int ret = -EOPNOTSUPP;
1139
1140 switch (tc->type) {
1141 case TC_SETUP_MATCHALL:
1142 switch (tc->cls_mall->command) {
1143 case TC_CLSMATCHALL_REPLACE:
1144 return dsa_slave_add_cls_matchall(dev, protocol,
1145 tc->cls_mall,
1146 ingress);
1147 case TC_CLSMATCHALL_DESTROY:
1148 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1149 return 0;
1150 }
1151 default:
1152 break;
1153 }
1154
1155 return ret;
1156}
1157
Florian Fainelliaf421922016-06-07 16:32:41 -07001158void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1159{
1160 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1161 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1162 ops->get_strings = dsa_cpu_port_get_strings;
1163}
1164
Florian Fainellibf9f2642017-01-30 09:48:40 -08001165static int dsa_slave_get_rxnfc(struct net_device *dev,
1166 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1167{
1168 struct dsa_slave_priv *p = netdev_priv(dev);
1169 struct dsa_switch *ds = p->dp->ds;
1170
1171 if (!ds->ops->get_rxnfc)
1172 return -EOPNOTSUPP;
1173
1174 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1175}
1176
1177static int dsa_slave_set_rxnfc(struct net_device *dev,
1178 struct ethtool_rxnfc *nfc)
1179{
1180 struct dsa_slave_priv *p = netdev_priv(dev);
1181 struct dsa_switch *ds = p->dp->ds;
1182
1183 if (!ds->ops->set_rxnfc)
1184 return -EOPNOTSUPP;
1185
1186 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1187}
1188
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001189static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001190 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001191 .get_regs_len = dsa_slave_get_regs_len,
1192 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001193 .nway_reset = dsa_slave_nway_reset,
1194 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001195 .get_eeprom_len = dsa_slave_get_eeprom_len,
1196 .get_eeprom = dsa_slave_get_eeprom,
1197 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001198 .get_strings = dsa_slave_get_strings,
1199 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1200 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001201 .set_wol = dsa_slave_set_wol,
1202 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001203 .set_eee = dsa_slave_set_eee,
1204 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +02001205 .get_link_ksettings = dsa_slave_get_link_ksettings,
1206 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001207 .get_rxnfc = dsa_slave_get_rxnfc,
1208 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001209};
1210
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001211static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001212 .ndo_open = dsa_slave_open,
1213 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001214 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001215 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1216 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001217 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001218 .ndo_fdb_add = switchdev_port_fdb_add,
1219 .ndo_fdb_del = switchdev_port_fdb_del,
1220 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001221 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001222 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001223#ifdef CONFIG_NET_POLL_CONTROLLER
1224 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1225 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1226 .ndo_poll_controller = dsa_slave_poll_controller,
1227#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001228 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1229 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1230 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -08001231 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001232 .ndo_setup_tc = dsa_slave_setup_tc,
Scott Feldman98237d42015-03-15 21:07:15 -07001233};
1234
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001235static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001236 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001237 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001238 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1239 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1240 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001241};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001242
Florian Fainellif37db852015-09-23 18:19:58 -07001243static struct device_type dsa_type = {
1244 .name = "dsa",
1245};
1246
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001247static void dsa_slave_adjust_link(struct net_device *dev)
1248{
1249 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001250 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001251 unsigned int status_changed = 0;
1252
1253 if (p->old_link != p->phy->link) {
1254 status_changed = 1;
1255 p->old_link = p->phy->link;
1256 }
1257
1258 if (p->old_duplex != p->phy->duplex) {
1259 status_changed = 1;
1260 p->old_duplex = p->phy->duplex;
1261 }
1262
1263 if (p->old_pause != p->phy->pause) {
1264 status_changed = 1;
1265 p->old_pause = p->phy->pause;
1266 }
1267
Vivien Didelot9d490b42016-08-23 12:38:56 -04001268 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001269 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001270
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001271 if (status_changed)
1272 phy_print_status(p->phy);
1273}
1274
Florian Fainellice31b312014-08-27 17:04:54 -07001275static int dsa_slave_fixed_link_update(struct net_device *dev,
1276 struct fixed_phy_status *status)
1277{
Andrew Lunnb71be352016-03-12 00:01:37 +01001278 struct dsa_slave_priv *p;
1279 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001280
Andrew Lunnb71be352016-03-12 00:01:37 +01001281 if (dev) {
1282 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001283 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001284 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001285 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001286 }
Florian Fainellice31b312014-08-27 17:04:54 -07001287
1288 return 0;
1289}
1290
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001291/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001292static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001293 struct net_device *slave_dev,
1294 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001295{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001296 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001297
Andrew Lunn7f854422016-01-06 20:11:18 +01001298 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001299 if (!p->phy) {
1300 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001301 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001302 }
Florian Fainellic305c162015-03-10 16:57:12 -07001303
1304 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001305 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1306 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001307 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1308 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001309}
1310
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001311static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001312 struct net_device *slave_dev)
1313{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001314 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001315 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001316 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001317 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001318 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001319
Vivien Didelotafdcf152017-01-27 15:29:39 -05001320 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001321 mode = of_get_phy_mode(port_dn);
1322 if (mode < 0)
1323 mode = PHY_INTERFACE_MODE_NA;
1324 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001325
1326 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001327 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001328 /* In the case of a fixed PHY, the DT node associated
1329 * to the fixed PHY is the Port DT node
1330 */
1331 ret = of_phy_register_fixed_link(port_dn);
1332 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001333 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001334 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001335 }
Florian Fainellice31b312014-08-27 17:04:54 -07001336 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001337 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001338 }
1339
Vivien Didelot9d490b42016-08-23 12:38:56 -04001340 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001341 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001342
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001343 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001344 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1345
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001346 /* If this PHY address is part of phys_mii_mask, which means
1347 * that we need to divert reads and writes to/from it, then we
1348 * want to bind this device using the slave MII bus created by
1349 * DSA to make that happen.
1350 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001351 if (!phy_is_fixed && phy_id >= 0 &&
1352 (ds->phys_mii_mask & (1 << phy_id))) {
1353 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1354 if (ret) {
1355 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001356 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001357 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001358 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001359 } else {
1360 p->phy = of_phy_connect(slave_dev, phy_dn,
1361 dsa_slave_adjust_link,
1362 phy_flags,
1363 p->phy_interface);
1364 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001365
1366 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001367 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001368
Florian Fainellice31b312014-08-27 17:04:54 -07001369 if (p->phy && phy_is_fixed)
1370 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1371
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001372 /* We could not connect to a designated PHY, so use the switch internal
1373 * MDIO bus instead
1374 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001375 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001376 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001377 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001378 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1379 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001380 if (phy_is_fixed)
1381 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001382 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001383 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001384 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001385
Andrew Lunn22209432016-01-06 20:11:13 +01001386 phy_attached_info(p->phy);
1387
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001388 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001389}
1390
Andrew Lunn448b4482015-05-06 01:09:56 +02001391static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1392static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1393 struct netdev_queue *txq,
1394 void *_unused)
1395{
1396 lockdep_set_class(&txq->_xmit_lock,
1397 &dsa_slave_netdev_xmit_lock_key);
1398}
1399
Florian Fainelli24462542014-09-18 17:31:22 -07001400int dsa_slave_suspend(struct net_device *slave_dev)
1401{
1402 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1403
Florian Fainellif154be22017-01-25 09:10:41 -08001404 netif_device_detach(slave_dev);
1405
Florian Fainelli24462542014-09-18 17:31:22 -07001406 if (p->phy) {
1407 phy_stop(p->phy);
1408 p->old_pause = -1;
1409 p->old_link = -1;
1410 p->old_duplex = -1;
1411 phy_suspend(p->phy);
1412 }
1413
1414 return 0;
1415}
1416
1417int dsa_slave_resume(struct net_device *slave_dev)
1418{
1419 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1420
1421 netif_device_attach(slave_dev);
1422
1423 if (p->phy) {
1424 phy_resume(p->phy);
1425 phy_start(p->phy);
1426 }
1427
1428 return 0;
1429}
1430
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001431int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001432 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001433{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001434 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001435 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001436 struct net_device *slave_dev;
1437 struct dsa_slave_priv *p;
1438 int ret;
1439
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001440 master = ds->dst->master_netdev;
1441 if (ds->master_netdev)
1442 master = ds->master_netdev;
1443
Tom Gundersenc835a672014-07-14 16:37:24 +02001444 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1445 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001446 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001447 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001448
Florian Fainellif50f2122017-01-30 12:41:40 -08001449 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1450 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001451 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001452 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001453 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001454 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001455 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001456 slave_dev->min_mtu = 0;
1457 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001458 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001459
Andrew Lunn448b4482015-05-06 01:09:56 +02001460 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1461 NULL);
1462
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001463 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001464 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001465 slave_dev->vlan_features = master->vlan_features;
1466
1467 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001468 p->dp = &ds->ports[port];
Florian Fainellif50f2122017-01-30 12:41:40 -08001469 INIT_LIST_HEAD(&p->mall_tc_list);
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001470 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001471
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001472 p->old_pause = -1;
1473 p->old_link = -1;
1474 p->old_duplex = -1;
1475
Andrew Lunnc8b09802016-06-04 21:16:57 +02001476 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001477 ret = register_netdev(slave_dev);
1478 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001479 netdev_err(master, "error %d registering interface %s\n",
1480 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001481 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001482 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001483 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001484 }
1485
1486 netif_carrier_off(slave_dev);
1487
Andrew Lunn0071f562016-01-06 20:11:20 +01001488 ret = dsa_slave_phy_setup(p, slave_dev);
1489 if (ret) {
1490 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001491 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001492 free_netdev(slave_dev);
1493 return ret;
1494 }
1495
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001496 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001497}
Florian Fainellib73adef2015-02-24 13:15:33 -08001498
Neil Armstrongcda5c152015-12-07 13:57:35 +01001499void dsa_slave_destroy(struct net_device *slave_dev)
1500{
1501 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001502 struct device_node *port_dn;
1503
Vivien Didelotafdcf152017-01-27 15:29:39 -05001504 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001505
1506 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001507 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001508 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001509
1510 if (of_phy_is_fixed_link(port_dn))
1511 of_phy_deregister_fixed_link(port_dn);
1512 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001513 unregister_netdev(slave_dev);
1514 free_netdev(slave_dev);
1515}
1516
Florian Fainellib73adef2015-02-24 13:15:33 -08001517static bool dsa_slave_dev_check(struct net_device *dev)
1518{
1519 return dev->netdev_ops == &dsa_slave_netdev_ops;
1520}
1521
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001522static int dsa_slave_changeupper(struct net_device *dev,
1523 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001524{
Vivien Didelot17d78022017-05-19 17:00:38 -04001525 struct dsa_slave_priv *p = netdev_priv(dev);
1526 struct dsa_port *dp = p->dp;
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001527 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001528
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001529 if (netif_is_bridge_master(info->upper_dev)) {
1530 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001531 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001532 err = notifier_from_errno(err);
1533 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001534 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001535 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001536 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001537 }
1538
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001539 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001540}
1541
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001542static int dsa_slave_netdevice_event(struct notifier_block *nb,
1543 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001544{
Vivien Didelot6debb682016-03-13 16:21:34 -04001545 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001546
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001547 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1548 return NOTIFY_DONE;
1549
1550 if (event == NETDEV_CHANGEUPPER)
1551 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001552
Florian Fainellib73adef2015-02-24 13:15:33 -08001553 return NOTIFY_DONE;
1554}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001555
1556static struct notifier_block dsa_slave_nb __read_mostly = {
1557 .notifier_call = dsa_slave_netdevice_event,
1558};
1559
1560int dsa_slave_register_notifier(void)
1561{
1562 return register_netdevice_notifier(&dsa_slave_nb);
1563}
1564
1565void dsa_slave_unregister_notifier(void)
1566{
1567 int err;
1568
1569 err = unregister_netdevice_notifier(&dsa_slave_nb);
1570 if (err)
1571 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1572}