blob: e9c3ea09cc096f714392d6707ab0b2bceba279e8 [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 Didelot3fdb0232017-05-19 17:00:39 -0400302static int dsa_port_fdb_add(struct dsa_port *dp,
303 const struct switchdev_obj_port_fdb *fdb,
304 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700305{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400306 struct dsa_switch *ds = dp->ds;
Vivien Didelot146a3202015-10-08 11:35:12 -0400307
Vivien Didelot8497aa62016-04-06 11:55:04 -0400308 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400309 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
Vivien Didelot8497aa62016-04-06 11:55:04 -0400310 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700311
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400312 return ds->ops->port_fdb_prepare(ds, dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400313 }
David S. Millercdf09692015-08-11 12:00:37 -0700314
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400315 ds->ops->port_fdb_add(ds, dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400316
317 return 0;
David S. Millercdf09692015-08-11 12:00:37 -0700318}
319
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400320static int dsa_port_fdb_del(struct dsa_port *dp,
321 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -0700322{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400323 struct dsa_switch *ds = dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700324 int ret = -EOPNOTSUPP;
325
Vivien Didelot9d490b42016-08-23 12:38:56 -0400326 if (ds->ops->port_fdb_del)
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400327 ret = ds->ops->port_fdb_del(ds, dp->index, fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700328
329 return ret;
330}
331
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400332static int dsa_port_fdb_dump(struct dsa_port *dp,
333 struct switchdev_obj_port_fdb *fdb,
334 switchdev_obj_dump_cb_t *cb)
David S. Millercdf09692015-08-11 12:00:37 -0700335{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400336 struct dsa_switch *ds = dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700337
Vivien Didelot9d490b42016-08-23 12:38:56 -0400338 if (ds->ops->port_fdb_dump)
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400339 return ds->ops->port_fdb_dump(ds, dp->index, fdb, cb);
Vivien Didelotea70ba92015-10-22 09:34:38 -0400340
Vivien Didelot1a49a2f2015-10-22 09:34:43 -0400341 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700342}
343
Vivien Didelot8df30252016-08-31 11:50:03 -0400344static int dsa_slave_port_mdb_add(struct net_device *dev,
345 const struct switchdev_obj_port_mdb *mdb,
346 struct switchdev_trans *trans)
347{
348 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500349 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400350
351 if (switchdev_trans_ph_prepare(trans)) {
352 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
353 return -EOPNOTSUPP;
354
Vivien Didelotafdcf152017-01-27 15:29:39 -0500355 return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400356 }
357
Vivien Didelotafdcf152017-01-27 15:29:39 -0500358 ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400359
360 return 0;
361}
362
363static int dsa_slave_port_mdb_del(struct net_device *dev,
364 const struct switchdev_obj_port_mdb *mdb)
365{
366 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500367 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400368
369 if (ds->ops->port_mdb_del)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500370 return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400371
372 return -EOPNOTSUPP;
373}
374
375static int dsa_slave_port_mdb_dump(struct net_device *dev,
376 struct switchdev_obj_port_mdb *mdb,
377 switchdev_obj_dump_cb_t *cb)
378{
379 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500380 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400381
382 if (ds->ops->port_mdb_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500383 return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400384
385 return -EOPNOTSUPP;
386}
387
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000388static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
389{
390 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000391
392 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000393 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000394
395 return -EOPNOTSUPP;
396}
397
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500398static int dsa_slave_vlan_filtering(struct net_device *dev,
399 const struct switchdev_attr *attr,
400 struct switchdev_trans *trans)
401{
402 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500403 struct dsa_switch *ds = p->dp->ds;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500404
405 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
406 if (switchdev_trans_ph_prepare(trans))
407 return 0;
408
Vivien Didelot9d490b42016-08-23 12:38:56 -0400409 if (ds->ops->port_vlan_filtering)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500410 return ds->ops->port_vlan_filtering(ds, p->dp->index,
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500411 attr->u.vlan_filtering);
412
413 return 0;
414}
415
Vivien Didelote893de1ba2017-03-15 15:53:48 -0400416static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
417 unsigned int ageing_time)
Vivien Didelot34a79f62016-07-18 20:45:38 -0400418{
419 int i;
420
Vivien Didelot26895e22017-01-27 15:29:37 -0500421 for (i = 0; i < ds->num_ports; ++i) {
Vivien Didelot34a79f62016-07-18 20:45:38 -0400422 struct dsa_port *dp = &ds->ports[i];
423
424 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
425 ageing_time = dp->ageing_time;
426 }
427
428 return ageing_time;
429}
430
431static int dsa_slave_ageing_time(struct net_device *dev,
432 const struct switchdev_attr *attr,
433 struct switchdev_trans *trans)
434{
435 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500436 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400437 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
438 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
439
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400440 if (switchdev_trans_ph_prepare(trans)) {
441 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
442 return -ERANGE;
443 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
444 return -ERANGE;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400445 return 0;
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400446 }
Vivien Didelot34a79f62016-07-18 20:45:38 -0400447
448 /* Keep the fastest ageing time in case of multiple bridges */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500449 p->dp->ageing_time = ageing_time;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400450 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
451
Vivien Didelot9d490b42016-08-23 12:38:56 -0400452 if (ds->ops->set_ageing_time)
453 return ds->ops->set_ageing_time(ds, ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400454
455 return 0;
456}
457
Scott Feldman35636062015-05-10 09:47:51 -0700458static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200459 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200460 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700461{
Vivien Didelotfd364542017-05-19 17:00:36 -0400462 struct dsa_slave_priv *p = netdev_priv(dev);
463 struct dsa_port *dp = p->dp;
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400464 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700465
466 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200467 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400468 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700469 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500470 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
471 ret = dsa_slave_vlan_filtering(dev, attr, trans);
472 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400473 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
474 ret = dsa_slave_ageing_time(dev, attr, trans);
475 break;
Scott Feldman35636062015-05-10 09:47:51 -0700476 default:
477 ret = -EOPNOTSUPP;
478 break;
479 }
480
481 return ret;
482}
483
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400484static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200485 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200486 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400487{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400488 struct dsa_slave_priv *p = netdev_priv(dev);
489 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400490 int err;
491
492 /* For the prepare phase, ensure the full set of changes is feasable in
493 * one go in order to signal a failure properly. If an operation is not
494 * supported, return -EOPNOTSUPP.
495 */
496
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200497 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200498 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400499 err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400500 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400501 case SWITCHDEV_OBJ_ID_PORT_MDB:
502 err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
503 trans);
504 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200505 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200506 err = dsa_slave_port_vlan_add(dev,
507 SWITCHDEV_OBJ_PORT_VLAN(obj),
508 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400509 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400510 default:
511 err = -EOPNOTSUPP;
512 break;
513 }
514
515 return err;
516}
517
518static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200519 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400520{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400521 struct dsa_slave_priv *p = netdev_priv(dev);
522 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400523 int err;
524
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200525 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200526 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400527 err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400528 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400529 case SWITCHDEV_OBJ_ID_PORT_MDB:
530 err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
531 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200532 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200533 err = dsa_slave_port_vlan_del(dev,
534 SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400535 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400536 default:
537 err = -EOPNOTSUPP;
538 break;
539 }
540
541 return err;
542}
543
544static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200545 struct switchdev_obj *obj,
546 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400547{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400548 struct dsa_slave_priv *p = netdev_priv(dev);
549 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400550 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:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400554 err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400555 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400556 case SWITCHDEV_OBJ_ID_PORT_MDB:
557 err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
558 cb);
559 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200560 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200561 err = dsa_slave_port_vlan_dump(dev,
562 SWITCHDEV_OBJ_PORT_VLAN(obj),
563 cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400564 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400565 default:
566 err = -EOPNOTSUPP;
567 break;
568 }
569
570 return err;
571}
572
Vivien Didelot17d78022017-05-19 17:00:38 -0400573static int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800574{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500575 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400576 .sw_index = dp->ds->index,
577 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500578 .br = br,
579 };
580 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800581
Vivien Didelot9c265422017-02-03 13:20:18 -0500582 /* Here the port is already bridged. Reflect the current configuration
583 * so that drivers can program their chips accordingly.
584 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400585 dp->bridge_dev = br;
Florian Fainellib73adef2015-02-24 13:15:33 -0800586
Vivien Didelot17d78022017-05-19 17:00:38 -0400587 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
Florian Fainellib73adef2015-02-24 13:15:33 -0800588
Vivien Didelot9c265422017-02-03 13:20:18 -0500589 /* The bridging is rolled back on error */
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500590 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400591 dp->bridge_dev = NULL;
Vivien Didelot9c265422017-02-03 13:20:18 -0500592
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500593 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800594}
595
Vivien Didelot17d78022017-05-19 17:00:38 -0400596static void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800597{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500598 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400599 .sw_index = dp->ds->index,
600 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500601 .br = br,
602 };
603 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800604
Vivien Didelot9c265422017-02-03 13:20:18 -0500605 /* Here the port is already unbridged. Reflect the current configuration
606 * so that drivers can program their chips accordingly.
607 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400608 dp->bridge_dev = NULL;
Florian Fainellib73adef2015-02-24 13:15:33 -0800609
Vivien Didelot17d78022017-05-19 17:00:38 -0400610 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500611 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400612 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
Florian Fainellib73adef2015-02-24 13:15:33 -0800613
614 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
615 * so allow it to be in BR_STATE_FORWARDING to be kept functional
616 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400617 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
Florian Fainellib73adef2015-02-24 13:15:33 -0800618}
619
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700620static int dsa_slave_port_attr_get(struct net_device *dev,
621 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800622{
623 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500624 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800625
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700626 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200627 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700628 attr->u.ppid.id_len = sizeof(ds->index);
629 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700630 break;
631 default:
632 return -EOPNOTSUPP;
633 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800634
635 return 0;
636}
637
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700638static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
639 struct sk_buff *skb)
640{
641#ifdef CONFIG_NET_POLL_CONTROLLER
642 if (p->netpoll)
643 netpoll_send_skb(p->netpoll, skb);
644#else
645 BUG();
646#endif
647 return NETDEV_TX_OK;
648}
649
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700650static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
651{
652 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700653 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700654
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700655 dev->stats.tx_packets++;
656 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700657
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700658 /* Transmit function may have to reallocate the original SKB */
659 nskb = p->xmit(skb, dev);
660 if (!nskb)
661 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700662
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700663 /* SKB for netpoll still need to be mangled with the protocol-specific
664 * tag to be successfully transmitted
665 */
666 if (unlikely(netpoll_tx_running(dev)))
667 return dsa_netpoll_send_skb(p, nskb);
668
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700669 /* Queue the SKB for transmission on the parent interface, but
670 * do not modify its EtherType
671 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500672 nskb->dev = p->dp->ds->dst->master_netdev;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700673 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700674
675 return NETDEV_TX_OK;
676}
677
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000678/* ethtool operations *******************************************************/
679static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200680dsa_slave_get_link_ksettings(struct net_device *dev,
681 struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000682{
683 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellie69e4622017-02-06 15:55:23 -0800684 int err = -EOPNOTSUPP;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000685
Florian Fainellie69e4622017-02-06 15:55:23 -0800686 if (p->phy != NULL)
687 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000688
689 return err;
690}
691
692static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200693dsa_slave_set_link_ksettings(struct net_device *dev,
694 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000695{
696 struct dsa_slave_priv *p = netdev_priv(dev);
697
698 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200699 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000700
701 return -EOPNOTSUPP;
702}
703
704static void dsa_slave_get_drvinfo(struct net_device *dev,
705 struct ethtool_drvinfo *drvinfo)
706{
Jiri Pirko7826d432013-01-06 00:44:26 +0000707 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000708 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
709 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000710}
711
Guenter Roeck3d762a02014-10-29 10:45:04 -0700712static int dsa_slave_get_regs_len(struct net_device *dev)
713{
714 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500715 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700716
Vivien Didelot9d490b42016-08-23 12:38:56 -0400717 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500718 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700719
720 return -EOPNOTSUPP;
721}
722
723static void
724dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
725{
726 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500727 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700728
Vivien Didelot9d490b42016-08-23 12:38:56 -0400729 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500730 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700731}
732
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000733static int dsa_slave_nway_reset(struct net_device *dev)
734{
735 struct dsa_slave_priv *p = netdev_priv(dev);
736
737 if (p->phy != NULL)
738 return genphy_restart_aneg(p->phy);
739
740 return -EOPNOTSUPP;
741}
742
743static u32 dsa_slave_get_link(struct net_device *dev)
744{
745 struct dsa_slave_priv *p = netdev_priv(dev);
746
747 if (p->phy != NULL) {
748 genphy_update_link(p->phy);
749 return p->phy->link;
750 }
751
752 return -EOPNOTSUPP;
753}
754
Guenter Roeck6793abb2014-10-29 10:45:01 -0700755static int dsa_slave_get_eeprom_len(struct net_device *dev)
756{
757 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500758 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700759
Andrew Lunn0e576042016-06-04 21:16:52 +0200760 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200761 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700762
Vivien Didelot9d490b42016-08-23 12:38:56 -0400763 if (ds->ops->get_eeprom_len)
764 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700765
766 return 0;
767}
768
769static int dsa_slave_get_eeprom(struct net_device *dev,
770 struct ethtool_eeprom *eeprom, u8 *data)
771{
772 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500773 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700774
Vivien Didelot9d490b42016-08-23 12:38:56 -0400775 if (ds->ops->get_eeprom)
776 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700777
778 return -EOPNOTSUPP;
779}
780
781static int dsa_slave_set_eeprom(struct net_device *dev,
782 struct ethtool_eeprom *eeprom, u8 *data)
783{
784 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500785 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700786
Vivien Didelot9d490b42016-08-23 12:38:56 -0400787 if (ds->ops->set_eeprom)
788 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700789
790 return -EOPNOTSUPP;
791}
792
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000793static void dsa_slave_get_strings(struct net_device *dev,
794 uint32_t stringset, uint8_t *data)
795{
796 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500797 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000798
799 if (stringset == ETH_SS_STATS) {
800 int len = ETH_GSTRING_LEN;
801
802 strncpy(data, "tx_packets", len);
803 strncpy(data + len, "tx_bytes", len);
804 strncpy(data + 2 * len, "rx_packets", len);
805 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400806 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500807 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000808 }
809}
810
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700811static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
812 struct ethtool_stats *stats,
813 uint64_t *data)
814{
815 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400816 struct dsa_switch *ds = dst->cpu_dp->ds;
817 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700818 int count = 0;
819
820 if (dst->master_ethtool_ops.get_sset_count) {
821 count = dst->master_ethtool_ops.get_sset_count(dev,
822 ETH_SS_STATS);
823 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
824 }
825
Vivien Didelot9d490b42016-08-23 12:38:56 -0400826 if (ds->ops->get_ethtool_stats)
827 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700828}
829
830static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
831{
832 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400833 struct dsa_switch *ds = dst->cpu_dp->ds;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700834 int count = 0;
835
836 if (dst->master_ethtool_ops.get_sset_count)
837 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
838
Vivien Didelot9d490b42016-08-23 12:38:56 -0400839 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
840 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700841
842 return count;
843}
844
845static void dsa_cpu_port_get_strings(struct net_device *dev,
846 uint32_t stringset, uint8_t *data)
847{
848 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400849 struct dsa_switch *ds = dst->cpu_dp->ds;
850 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700851 int len = ETH_GSTRING_LEN;
852 int mcount = 0, count;
853 unsigned int i;
854 uint8_t pfx[4];
855 uint8_t *ndata;
856
857 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
858 /* We do not want to be NULL-terminated, since this is a prefix */
859 pfx[sizeof(pfx) - 1] = '_';
860
861 if (dst->master_ethtool_ops.get_sset_count) {
862 mcount = dst->master_ethtool_ops.get_sset_count(dev,
863 ETH_SS_STATS);
864 dst->master_ethtool_ops.get_strings(dev, stringset, data);
865 }
866
Vivien Didelot9d490b42016-08-23 12:38:56 -0400867 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700868 ndata = data + mcount * len;
869 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
870 * the output after to prepend our CPU port prefix we
871 * constructed earlier
872 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400873 ds->ops->get_strings(ds, cpu_port, ndata);
874 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700875 for (i = 0; i < count; i++) {
876 memmove(ndata + (i * len + sizeof(pfx)),
877 ndata + i * len, len - sizeof(pfx));
878 memcpy(ndata + i * len, pfx, sizeof(pfx));
879 }
880 }
881}
882
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000883static void dsa_slave_get_ethtool_stats(struct net_device *dev,
884 struct ethtool_stats *stats,
885 uint64_t *data)
886{
887 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500888 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000889
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400890 data[0] = dev->stats.tx_packets;
891 data[1] = dev->stats.tx_bytes;
892 data[2] = dev->stats.rx_packets;
893 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400894 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500895 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000896}
897
898static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
899{
900 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500901 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000902
903 if (sset == ETH_SS_STATS) {
904 int count;
905
906 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400907 if (ds->ops->get_sset_count)
908 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000909
910 return count;
911 }
912
913 return -EOPNOTSUPP;
914}
915
Florian Fainelli19e57c42014-09-18 17:31:24 -0700916static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
917{
918 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500919 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700920
Vivien Didelot9d490b42016-08-23 12:38:56 -0400921 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500922 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700923}
924
925static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
926{
927 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500928 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700929 int ret = -EOPNOTSUPP;
930
Vivien Didelot9d490b42016-08-23 12:38:56 -0400931 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500932 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700933
934 return ret;
935}
936
Florian Fainelli79052882014-09-24 17:05:21 -0700937static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
938{
939 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500940 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700941 int ret;
942
Vivien Didelot9d490b42016-08-23 12:38:56 -0400943 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700944 return -EOPNOTSUPP;
945
Vivien Didelotafdcf152017-01-27 15:29:39 -0500946 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700947 if (ret)
948 return ret;
949
950 if (p->phy)
951 ret = phy_ethtool_set_eee(p->phy, e);
952
953 return ret;
954}
955
956static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
957{
958 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500959 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700960 int ret;
961
Vivien Didelot9d490b42016-08-23 12:38:56 -0400962 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700963 return -EOPNOTSUPP;
964
Vivien Didelotafdcf152017-01-27 15:29:39 -0500965 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700966 if (ret)
967 return ret;
968
969 if (p->phy)
970 ret = phy_ethtool_get_eee(p->phy, e);
971
972 return ret;
973}
974
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700975#ifdef CONFIG_NET_POLL_CONTROLLER
976static int dsa_slave_netpoll_setup(struct net_device *dev,
977 struct netpoll_info *ni)
978{
979 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500980 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700981 struct net_device *master = ds->dst->master_netdev;
982 struct netpoll *netpoll;
983 int err = 0;
984
985 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
986 if (!netpoll)
987 return -ENOMEM;
988
989 err = __netpoll_setup(netpoll, master);
990 if (err) {
991 kfree(netpoll);
992 goto out;
993 }
994
995 p->netpoll = netpoll;
996out:
997 return err;
998}
999
1000static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1001{
1002 struct dsa_slave_priv *p = netdev_priv(dev);
1003 struct netpoll *netpoll = p->netpoll;
1004
1005 if (!netpoll)
1006 return;
1007
1008 p->netpoll = NULL;
1009
1010 __netpoll_free_async(netpoll);
1011}
1012
1013static void dsa_slave_poll_controller(struct net_device *dev)
1014{
1015}
1016#endif
1017
Florian Fainelli44bb7652017-01-10 12:32:36 -08001018static int dsa_slave_get_phys_port_name(struct net_device *dev,
1019 char *name, size_t len)
1020{
1021 struct dsa_slave_priv *p = netdev_priv(dev);
1022
Vivien Didelotafdcf152017-01-27 15:29:39 -05001023 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -08001024 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -08001025
1026 return 0;
1027}
1028
Florian Fainellif50f2122017-01-30 12:41:40 -08001029static struct dsa_mall_tc_entry *
1030dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1031 unsigned long cookie)
1032{
1033 struct dsa_mall_tc_entry *mall_tc_entry;
1034
1035 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1036 if (mall_tc_entry->cookie == cookie)
1037 return mall_tc_entry;
1038
1039 return NULL;
1040}
1041
1042static int dsa_slave_add_cls_matchall(struct net_device *dev,
1043 __be16 protocol,
1044 struct tc_cls_matchall_offload *cls,
1045 bool ingress)
1046{
1047 struct dsa_slave_priv *p = netdev_priv(dev);
1048 struct dsa_mall_tc_entry *mall_tc_entry;
1049 struct dsa_switch *ds = p->dp->ds;
1050 struct net *net = dev_net(dev);
1051 struct dsa_slave_priv *to_p;
1052 struct net_device *to_dev;
1053 const struct tc_action *a;
1054 int err = -EOPNOTSUPP;
1055 LIST_HEAD(actions);
1056 int ifindex;
1057
1058 if (!ds->ops->port_mirror_add)
1059 return err;
1060
1061 if (!tc_single_action(cls->exts))
1062 return err;
1063
1064 tcf_exts_to_list(cls->exts, &actions);
1065 a = list_first_entry(&actions, struct tc_action, list);
1066
1067 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1068 struct dsa_mall_mirror_tc_entry *mirror;
1069
1070 ifindex = tcf_mirred_ifindex(a);
1071 to_dev = __dev_get_by_index(net, ifindex);
1072 if (!to_dev)
1073 return -EINVAL;
1074
1075 if (!dsa_slave_dev_check(to_dev))
1076 return -EOPNOTSUPP;
1077
1078 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1079 if (!mall_tc_entry)
1080 return -ENOMEM;
1081
1082 mall_tc_entry->cookie = cls->cookie;
1083 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1084 mirror = &mall_tc_entry->mirror;
1085
1086 to_p = netdev_priv(to_dev);
1087
1088 mirror->to_local_port = to_p->dp->index;
1089 mirror->ingress = ingress;
1090
1091 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1092 ingress);
1093 if (err) {
1094 kfree(mall_tc_entry);
1095 return err;
1096 }
1097
1098 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1099 }
1100
1101 return 0;
1102}
1103
1104static void dsa_slave_del_cls_matchall(struct net_device *dev,
1105 struct tc_cls_matchall_offload *cls)
1106{
1107 struct dsa_slave_priv *p = netdev_priv(dev);
1108 struct dsa_mall_tc_entry *mall_tc_entry;
1109 struct dsa_switch *ds = p->dp->ds;
1110
1111 if (!ds->ops->port_mirror_del)
1112 return;
1113
1114 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1115 if (!mall_tc_entry)
1116 return;
1117
1118 list_del(&mall_tc_entry->list);
1119
1120 switch (mall_tc_entry->type) {
1121 case DSA_PORT_MALL_MIRROR:
1122 ds->ops->port_mirror_del(ds, p->dp->index,
1123 &mall_tc_entry->mirror);
1124 break;
1125 default:
1126 WARN_ON(1);
1127 }
1128
1129 kfree(mall_tc_entry);
1130}
1131
1132static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1133 __be16 protocol, struct tc_to_netdev *tc)
1134{
1135 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1136 int ret = -EOPNOTSUPP;
1137
1138 switch (tc->type) {
1139 case TC_SETUP_MATCHALL:
1140 switch (tc->cls_mall->command) {
1141 case TC_CLSMATCHALL_REPLACE:
1142 return dsa_slave_add_cls_matchall(dev, protocol,
1143 tc->cls_mall,
1144 ingress);
1145 case TC_CLSMATCHALL_DESTROY:
1146 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1147 return 0;
1148 }
1149 default:
1150 break;
1151 }
1152
1153 return ret;
1154}
1155
Florian Fainelliaf421922016-06-07 16:32:41 -07001156void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1157{
1158 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1159 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1160 ops->get_strings = dsa_cpu_port_get_strings;
1161}
1162
Florian Fainellibf9f2642017-01-30 09:48:40 -08001163static int dsa_slave_get_rxnfc(struct net_device *dev,
1164 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1165{
1166 struct dsa_slave_priv *p = netdev_priv(dev);
1167 struct dsa_switch *ds = p->dp->ds;
1168
1169 if (!ds->ops->get_rxnfc)
1170 return -EOPNOTSUPP;
1171
1172 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1173}
1174
1175static int dsa_slave_set_rxnfc(struct net_device *dev,
1176 struct ethtool_rxnfc *nfc)
1177{
1178 struct dsa_slave_priv *p = netdev_priv(dev);
1179 struct dsa_switch *ds = p->dp->ds;
1180
1181 if (!ds->ops->set_rxnfc)
1182 return -EOPNOTSUPP;
1183
1184 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1185}
1186
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001187static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001188 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001189 .get_regs_len = dsa_slave_get_regs_len,
1190 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001191 .nway_reset = dsa_slave_nway_reset,
1192 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001193 .get_eeprom_len = dsa_slave_get_eeprom_len,
1194 .get_eeprom = dsa_slave_get_eeprom,
1195 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001196 .get_strings = dsa_slave_get_strings,
1197 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1198 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001199 .set_wol = dsa_slave_set_wol,
1200 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001201 .set_eee = dsa_slave_set_eee,
1202 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +02001203 .get_link_ksettings = dsa_slave_get_link_ksettings,
1204 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001205 .get_rxnfc = dsa_slave_get_rxnfc,
1206 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001207};
1208
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001209static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001210 .ndo_open = dsa_slave_open,
1211 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001212 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001213 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1214 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001215 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001216 .ndo_fdb_add = switchdev_port_fdb_add,
1217 .ndo_fdb_del = switchdev_port_fdb_del,
1218 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001219 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001220 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001221#ifdef CONFIG_NET_POLL_CONTROLLER
1222 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1223 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1224 .ndo_poll_controller = dsa_slave_poll_controller,
1225#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001226 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1227 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1228 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -08001229 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001230 .ndo_setup_tc = dsa_slave_setup_tc,
Scott Feldman98237d42015-03-15 21:07:15 -07001231};
1232
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001233static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001234 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001235 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001236 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1237 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1238 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001239};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001240
Florian Fainellif37db852015-09-23 18:19:58 -07001241static struct device_type dsa_type = {
1242 .name = "dsa",
1243};
1244
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001245static void dsa_slave_adjust_link(struct net_device *dev)
1246{
1247 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001248 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001249 unsigned int status_changed = 0;
1250
1251 if (p->old_link != p->phy->link) {
1252 status_changed = 1;
1253 p->old_link = p->phy->link;
1254 }
1255
1256 if (p->old_duplex != p->phy->duplex) {
1257 status_changed = 1;
1258 p->old_duplex = p->phy->duplex;
1259 }
1260
1261 if (p->old_pause != p->phy->pause) {
1262 status_changed = 1;
1263 p->old_pause = p->phy->pause;
1264 }
1265
Vivien Didelot9d490b42016-08-23 12:38:56 -04001266 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001267 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001268
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001269 if (status_changed)
1270 phy_print_status(p->phy);
1271}
1272
Florian Fainellice31b312014-08-27 17:04:54 -07001273static int dsa_slave_fixed_link_update(struct net_device *dev,
1274 struct fixed_phy_status *status)
1275{
Andrew Lunnb71be352016-03-12 00:01:37 +01001276 struct dsa_slave_priv *p;
1277 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001278
Andrew Lunnb71be352016-03-12 00:01:37 +01001279 if (dev) {
1280 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001281 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001282 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001283 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001284 }
Florian Fainellice31b312014-08-27 17:04:54 -07001285
1286 return 0;
1287}
1288
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001289/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001290static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001291 struct net_device *slave_dev,
1292 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001293{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001294 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001295
Andrew Lunn7f854422016-01-06 20:11:18 +01001296 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001297 if (!p->phy) {
1298 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001299 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001300 }
Florian Fainellic305c162015-03-10 16:57:12 -07001301
1302 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001303 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1304 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001305 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1306 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001307}
1308
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001309static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001310 struct net_device *slave_dev)
1311{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001312 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001313 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001314 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001315 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001316 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001317
Vivien Didelotafdcf152017-01-27 15:29:39 -05001318 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001319 mode = of_get_phy_mode(port_dn);
1320 if (mode < 0)
1321 mode = PHY_INTERFACE_MODE_NA;
1322 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001323
1324 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001325 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001326 /* In the case of a fixed PHY, the DT node associated
1327 * to the fixed PHY is the Port DT node
1328 */
1329 ret = of_phy_register_fixed_link(port_dn);
1330 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001331 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001332 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001333 }
Florian Fainellice31b312014-08-27 17:04:54 -07001334 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001335 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001336 }
1337
Vivien Didelot9d490b42016-08-23 12:38:56 -04001338 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001339 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001340
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001341 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001342 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1343
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001344 /* If this PHY address is part of phys_mii_mask, which means
1345 * that we need to divert reads and writes to/from it, then we
1346 * want to bind this device using the slave MII bus created by
1347 * DSA to make that happen.
1348 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001349 if (!phy_is_fixed && phy_id >= 0 &&
1350 (ds->phys_mii_mask & (1 << phy_id))) {
1351 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1352 if (ret) {
1353 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001354 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001355 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001356 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001357 } else {
1358 p->phy = of_phy_connect(slave_dev, phy_dn,
1359 dsa_slave_adjust_link,
1360 phy_flags,
1361 p->phy_interface);
1362 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001363
1364 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001365 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001366
Florian Fainellice31b312014-08-27 17:04:54 -07001367 if (p->phy && phy_is_fixed)
1368 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1369
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001370 /* We could not connect to a designated PHY, so use the switch internal
1371 * MDIO bus instead
1372 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001373 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001374 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001375 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001376 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1377 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001378 if (phy_is_fixed)
1379 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001380 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001381 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001382 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001383
Andrew Lunn22209432016-01-06 20:11:13 +01001384 phy_attached_info(p->phy);
1385
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001386 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001387}
1388
Andrew Lunn448b4482015-05-06 01:09:56 +02001389static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1390static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1391 struct netdev_queue *txq,
1392 void *_unused)
1393{
1394 lockdep_set_class(&txq->_xmit_lock,
1395 &dsa_slave_netdev_xmit_lock_key);
1396}
1397
Florian Fainelli24462542014-09-18 17:31:22 -07001398int dsa_slave_suspend(struct net_device *slave_dev)
1399{
1400 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1401
Florian Fainellif154be22017-01-25 09:10:41 -08001402 netif_device_detach(slave_dev);
1403
Florian Fainelli24462542014-09-18 17:31:22 -07001404 if (p->phy) {
1405 phy_stop(p->phy);
1406 p->old_pause = -1;
1407 p->old_link = -1;
1408 p->old_duplex = -1;
1409 phy_suspend(p->phy);
1410 }
1411
1412 return 0;
1413}
1414
1415int dsa_slave_resume(struct net_device *slave_dev)
1416{
1417 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1418
1419 netif_device_attach(slave_dev);
1420
1421 if (p->phy) {
1422 phy_resume(p->phy);
1423 phy_start(p->phy);
1424 }
1425
1426 return 0;
1427}
1428
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001429int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001430 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001431{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001432 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001433 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001434 struct net_device *slave_dev;
1435 struct dsa_slave_priv *p;
1436 int ret;
1437
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001438 master = ds->dst->master_netdev;
1439 if (ds->master_netdev)
1440 master = ds->master_netdev;
1441
Tom Gundersenc835a672014-07-14 16:37:24 +02001442 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1443 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001444 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001445 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001446
Florian Fainellif50f2122017-01-30 12:41:40 -08001447 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1448 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001449 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001450 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001451 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001452 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001453 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001454 slave_dev->min_mtu = 0;
1455 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001456 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001457
Andrew Lunn448b4482015-05-06 01:09:56 +02001458 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1459 NULL);
1460
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001461 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001462 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001463 slave_dev->vlan_features = master->vlan_features;
1464
1465 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001466 p->dp = &ds->ports[port];
Florian Fainellif50f2122017-01-30 12:41:40 -08001467 INIT_LIST_HEAD(&p->mall_tc_list);
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001468 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001469
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001470 p->old_pause = -1;
1471 p->old_link = -1;
1472 p->old_duplex = -1;
1473
Andrew Lunnc8b09802016-06-04 21:16:57 +02001474 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001475 ret = register_netdev(slave_dev);
1476 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001477 netdev_err(master, "error %d registering interface %s\n",
1478 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001479 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001480 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001481 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001482 }
1483
1484 netif_carrier_off(slave_dev);
1485
Andrew Lunn0071f562016-01-06 20:11:20 +01001486 ret = dsa_slave_phy_setup(p, slave_dev);
1487 if (ret) {
1488 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001489 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001490 free_netdev(slave_dev);
1491 return ret;
1492 }
1493
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001494 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001495}
Florian Fainellib73adef2015-02-24 13:15:33 -08001496
Neil Armstrongcda5c152015-12-07 13:57:35 +01001497void dsa_slave_destroy(struct net_device *slave_dev)
1498{
1499 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001500 struct device_node *port_dn;
1501
Vivien Didelotafdcf152017-01-27 15:29:39 -05001502 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001503
1504 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001505 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001506 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001507
1508 if (of_phy_is_fixed_link(port_dn))
1509 of_phy_deregister_fixed_link(port_dn);
1510 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001511 unregister_netdev(slave_dev);
1512 free_netdev(slave_dev);
1513}
1514
Florian Fainellib73adef2015-02-24 13:15:33 -08001515static bool dsa_slave_dev_check(struct net_device *dev)
1516{
1517 return dev->netdev_ops == &dsa_slave_netdev_ops;
1518}
1519
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001520static int dsa_slave_changeupper(struct net_device *dev,
1521 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001522{
Vivien Didelot17d78022017-05-19 17:00:38 -04001523 struct dsa_slave_priv *p = netdev_priv(dev);
1524 struct dsa_port *dp = p->dp;
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001525 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001526
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001527 if (netif_is_bridge_master(info->upper_dev)) {
1528 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001529 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001530 err = notifier_from_errno(err);
1531 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001532 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001533 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001534 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001535 }
1536
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001537 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001538}
1539
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001540static int dsa_slave_netdevice_event(struct notifier_block *nb,
1541 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001542{
Vivien Didelot6debb682016-03-13 16:21:34 -04001543 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001544
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001545 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1546 return NOTIFY_DONE;
1547
1548 if (event == NETDEV_CHANGEUPPER)
1549 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001550
Florian Fainellib73adef2015-02-24 13:15:33 -08001551 return NOTIFY_DONE;
1552}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001553
1554static struct notifier_block dsa_slave_nb __read_mostly = {
1555 .notifier_call = dsa_slave_netdevice_event,
1556};
1557
1558int dsa_slave_register_notifier(void)
1559{
1560 return register_netdevice_notifier(&dsa_slave_nb);
1561}
1562
1563void dsa_slave_unregister_notifier(void)
1564{
1565 int err;
1566
1567 err = unregister_netdevice_notifier(&dsa_slave_nb);
1568 if (err)
1569 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1570}