blob: 0921d306aedf7d3978ca0379a2dcab04fdfc2f42 [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 Didelotbcebb972017-05-19 17:00:40 -0400344static int dsa_port_mdb_add(struct dsa_port *dp,
345 const struct switchdev_obj_port_mdb *mdb,
346 struct switchdev_trans *trans)
Vivien Didelot8df30252016-08-31 11:50:03 -0400347{
Vivien Didelotbcebb972017-05-19 17:00:40 -0400348 struct dsa_switch *ds = dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400349
350 if (switchdev_trans_ph_prepare(trans)) {
351 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
352 return -EOPNOTSUPP;
353
Vivien Didelotbcebb972017-05-19 17:00:40 -0400354 return ds->ops->port_mdb_prepare(ds, dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400355 }
356
Vivien Didelotbcebb972017-05-19 17:00:40 -0400357 ds->ops->port_mdb_add(ds, dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400358
359 return 0;
360}
361
Vivien Didelotbcebb972017-05-19 17:00:40 -0400362static int dsa_port_mdb_del(struct dsa_port *dp,
363 const struct switchdev_obj_port_mdb *mdb)
Vivien Didelot8df30252016-08-31 11:50:03 -0400364{
Vivien Didelotbcebb972017-05-19 17:00:40 -0400365 struct dsa_switch *ds = dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400366
367 if (ds->ops->port_mdb_del)
Vivien Didelotbcebb972017-05-19 17:00:40 -0400368 return ds->ops->port_mdb_del(ds, dp->index, mdb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400369
370 return -EOPNOTSUPP;
371}
372
Vivien Didelotbcebb972017-05-19 17:00:40 -0400373static int dsa_port_mdb_dump(struct dsa_port *dp,
374 struct switchdev_obj_port_mdb *mdb,
375 switchdev_obj_dump_cb_t *cb)
Vivien Didelot8df30252016-08-31 11:50:03 -0400376{
Vivien Didelotbcebb972017-05-19 17:00:40 -0400377 struct dsa_switch *ds = dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400378
379 if (ds->ops->port_mdb_dump)
Vivien Didelotbcebb972017-05-19 17:00:40 -0400380 return ds->ops->port_mdb_dump(ds, dp->index, mdb, cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400381
382 return -EOPNOTSUPP;
383}
384
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000385static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
386{
387 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000388
389 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000390 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000391
392 return -EOPNOTSUPP;
393}
394
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500395static int dsa_slave_vlan_filtering(struct net_device *dev,
396 const struct switchdev_attr *attr,
397 struct switchdev_trans *trans)
398{
399 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500400 struct dsa_switch *ds = p->dp->ds;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500401
402 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
403 if (switchdev_trans_ph_prepare(trans))
404 return 0;
405
Vivien Didelot9d490b42016-08-23 12:38:56 -0400406 if (ds->ops->port_vlan_filtering)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500407 return ds->ops->port_vlan_filtering(ds, p->dp->index,
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500408 attr->u.vlan_filtering);
409
410 return 0;
411}
412
Vivien Didelote893de1ba2017-03-15 15:53:48 -0400413static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
414 unsigned int ageing_time)
Vivien Didelot34a79f62016-07-18 20:45:38 -0400415{
416 int i;
417
Vivien Didelot26895e22017-01-27 15:29:37 -0500418 for (i = 0; i < ds->num_ports; ++i) {
Vivien Didelot34a79f62016-07-18 20:45:38 -0400419 struct dsa_port *dp = &ds->ports[i];
420
421 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
422 ageing_time = dp->ageing_time;
423 }
424
425 return ageing_time;
426}
427
428static int dsa_slave_ageing_time(struct net_device *dev,
429 const struct switchdev_attr *attr,
430 struct switchdev_trans *trans)
431{
432 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500433 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400434 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
435 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
436
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400437 if (switchdev_trans_ph_prepare(trans)) {
438 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
439 return -ERANGE;
440 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
441 return -ERANGE;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400442 return 0;
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400443 }
Vivien Didelot34a79f62016-07-18 20:45:38 -0400444
445 /* Keep the fastest ageing time in case of multiple bridges */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500446 p->dp->ageing_time = ageing_time;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400447 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
448
Vivien Didelot9d490b42016-08-23 12:38:56 -0400449 if (ds->ops->set_ageing_time)
450 return ds->ops->set_ageing_time(ds, ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400451
452 return 0;
453}
454
Scott Feldman35636062015-05-10 09:47:51 -0700455static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200456 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200457 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700458{
Vivien Didelotfd364542017-05-19 17:00:36 -0400459 struct dsa_slave_priv *p = netdev_priv(dev);
460 struct dsa_port *dp = p->dp;
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400461 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700462
463 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200464 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400465 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700466 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500467 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
468 ret = dsa_slave_vlan_filtering(dev, attr, trans);
469 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400470 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
471 ret = dsa_slave_ageing_time(dev, attr, trans);
472 break;
Scott Feldman35636062015-05-10 09:47:51 -0700473 default:
474 ret = -EOPNOTSUPP;
475 break;
476 }
477
478 return ret;
479}
480
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400481static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200482 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200483 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400484{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400485 struct dsa_slave_priv *p = netdev_priv(dev);
486 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400487 int err;
488
489 /* For the prepare phase, ensure the full set of changes is feasable in
490 * one go in order to signal a failure properly. If an operation is not
491 * supported, return -EOPNOTSUPP.
492 */
493
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200494 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200495 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400496 err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400497 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400498 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400499 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400500 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200501 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200502 err = dsa_slave_port_vlan_add(dev,
503 SWITCHDEV_OBJ_PORT_VLAN(obj),
504 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400505 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400506 default:
507 err = -EOPNOTSUPP;
508 break;
509 }
510
511 return err;
512}
513
514static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200515 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400516{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400517 struct dsa_slave_priv *p = netdev_priv(dev);
518 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400519 int err;
520
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200521 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200522 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400523 err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400524 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400525 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400526 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400527 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200528 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200529 err = dsa_slave_port_vlan_del(dev,
530 SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400531 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400532 default:
533 err = -EOPNOTSUPP;
534 break;
535 }
536
537 return err;
538}
539
540static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200541 struct switchdev_obj *obj,
542 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400543{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400544 struct dsa_slave_priv *p = netdev_priv(dev);
545 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400546 int err;
547
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200548 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200549 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400550 err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400551 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400552 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400553 err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400554 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200555 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200556 err = dsa_slave_port_vlan_dump(dev,
557 SWITCHDEV_OBJ_PORT_VLAN(obj),
558 cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400559 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400560 default:
561 err = -EOPNOTSUPP;
562 break;
563 }
564
565 return err;
566}
567
Vivien Didelot17d78022017-05-19 17:00:38 -0400568static int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800569{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500570 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400571 .sw_index = dp->ds->index,
572 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500573 .br = br,
574 };
575 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800576
Vivien Didelot9c265422017-02-03 13:20:18 -0500577 /* Here the port is already bridged. Reflect the current configuration
578 * so that drivers can program their chips accordingly.
579 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400580 dp->bridge_dev = br;
Florian Fainellib73adef2015-02-24 13:15:33 -0800581
Vivien Didelot17d78022017-05-19 17:00:38 -0400582 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
Florian Fainellib73adef2015-02-24 13:15:33 -0800583
Vivien Didelot9c265422017-02-03 13:20:18 -0500584 /* The bridging is rolled back on error */
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500585 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400586 dp->bridge_dev = NULL;
Vivien Didelot9c265422017-02-03 13:20:18 -0500587
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500588 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800589}
590
Vivien Didelot17d78022017-05-19 17:00:38 -0400591static void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800592{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500593 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400594 .sw_index = dp->ds->index,
595 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500596 .br = br,
597 };
598 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800599
Vivien Didelot9c265422017-02-03 13:20:18 -0500600 /* Here the port is already unbridged. Reflect the current configuration
601 * so that drivers can program their chips accordingly.
602 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400603 dp->bridge_dev = NULL;
Florian Fainellib73adef2015-02-24 13:15:33 -0800604
Vivien Didelot17d78022017-05-19 17:00:38 -0400605 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500606 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400607 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
Florian Fainellib73adef2015-02-24 13:15:33 -0800608
609 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
610 * so allow it to be in BR_STATE_FORWARDING to be kept functional
611 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400612 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
Florian Fainellib73adef2015-02-24 13:15:33 -0800613}
614
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700615static int dsa_slave_port_attr_get(struct net_device *dev,
616 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800617{
618 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500619 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800620
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700621 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200622 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700623 attr->u.ppid.id_len = sizeof(ds->index);
624 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700625 break;
626 default:
627 return -EOPNOTSUPP;
628 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800629
630 return 0;
631}
632
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700633static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
634 struct sk_buff *skb)
635{
636#ifdef CONFIG_NET_POLL_CONTROLLER
637 if (p->netpoll)
638 netpoll_send_skb(p->netpoll, skb);
639#else
640 BUG();
641#endif
642 return NETDEV_TX_OK;
643}
644
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700645static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
646{
647 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700648 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700649
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700650 dev->stats.tx_packets++;
651 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700652
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700653 /* Transmit function may have to reallocate the original SKB */
654 nskb = p->xmit(skb, dev);
655 if (!nskb)
656 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700657
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700658 /* SKB for netpoll still need to be mangled with the protocol-specific
659 * tag to be successfully transmitted
660 */
661 if (unlikely(netpoll_tx_running(dev)))
662 return dsa_netpoll_send_skb(p, nskb);
663
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700664 /* Queue the SKB for transmission on the parent interface, but
665 * do not modify its EtherType
666 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500667 nskb->dev = p->dp->ds->dst->master_netdev;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700668 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700669
670 return NETDEV_TX_OK;
671}
672
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000673/* ethtool operations *******************************************************/
674static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200675dsa_slave_get_link_ksettings(struct net_device *dev,
676 struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000677{
678 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellie69e4622017-02-06 15:55:23 -0800679 int err = -EOPNOTSUPP;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000680
Florian Fainellie69e4622017-02-06 15:55:23 -0800681 if (p->phy != NULL)
682 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000683
684 return err;
685}
686
687static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200688dsa_slave_set_link_ksettings(struct net_device *dev,
689 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000690{
691 struct dsa_slave_priv *p = netdev_priv(dev);
692
693 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200694 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000695
696 return -EOPNOTSUPP;
697}
698
699static void dsa_slave_get_drvinfo(struct net_device *dev,
700 struct ethtool_drvinfo *drvinfo)
701{
Jiri Pirko7826d432013-01-06 00:44:26 +0000702 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000703 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
704 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000705}
706
Guenter Roeck3d762a02014-10-29 10:45:04 -0700707static int dsa_slave_get_regs_len(struct net_device *dev)
708{
709 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500710 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700711
Vivien Didelot9d490b42016-08-23 12:38:56 -0400712 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500713 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700714
715 return -EOPNOTSUPP;
716}
717
718static void
719dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
720{
721 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500722 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700723
Vivien Didelot9d490b42016-08-23 12:38:56 -0400724 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500725 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700726}
727
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000728static int dsa_slave_nway_reset(struct net_device *dev)
729{
730 struct dsa_slave_priv *p = netdev_priv(dev);
731
732 if (p->phy != NULL)
733 return genphy_restart_aneg(p->phy);
734
735 return -EOPNOTSUPP;
736}
737
738static u32 dsa_slave_get_link(struct net_device *dev)
739{
740 struct dsa_slave_priv *p = netdev_priv(dev);
741
742 if (p->phy != NULL) {
743 genphy_update_link(p->phy);
744 return p->phy->link;
745 }
746
747 return -EOPNOTSUPP;
748}
749
Guenter Roeck6793abb2014-10-29 10:45:01 -0700750static int dsa_slave_get_eeprom_len(struct net_device *dev)
751{
752 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500753 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700754
Andrew Lunn0e576042016-06-04 21:16:52 +0200755 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200756 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700757
Vivien Didelot9d490b42016-08-23 12:38:56 -0400758 if (ds->ops->get_eeprom_len)
759 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700760
761 return 0;
762}
763
764static int dsa_slave_get_eeprom(struct net_device *dev,
765 struct ethtool_eeprom *eeprom, u8 *data)
766{
767 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500768 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700769
Vivien Didelot9d490b42016-08-23 12:38:56 -0400770 if (ds->ops->get_eeprom)
771 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700772
773 return -EOPNOTSUPP;
774}
775
776static int dsa_slave_set_eeprom(struct net_device *dev,
777 struct ethtool_eeprom *eeprom, u8 *data)
778{
779 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500780 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700781
Vivien Didelot9d490b42016-08-23 12:38:56 -0400782 if (ds->ops->set_eeprom)
783 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700784
785 return -EOPNOTSUPP;
786}
787
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000788static void dsa_slave_get_strings(struct net_device *dev,
789 uint32_t stringset, uint8_t *data)
790{
791 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500792 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000793
794 if (stringset == ETH_SS_STATS) {
795 int len = ETH_GSTRING_LEN;
796
797 strncpy(data, "tx_packets", len);
798 strncpy(data + len, "tx_bytes", len);
799 strncpy(data + 2 * len, "rx_packets", len);
800 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400801 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500802 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000803 }
804}
805
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700806static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
807 struct ethtool_stats *stats,
808 uint64_t *data)
809{
810 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400811 struct dsa_switch *ds = dst->cpu_dp->ds;
812 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700813 int count = 0;
814
815 if (dst->master_ethtool_ops.get_sset_count) {
816 count = dst->master_ethtool_ops.get_sset_count(dev,
817 ETH_SS_STATS);
818 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
819 }
820
Vivien Didelot9d490b42016-08-23 12:38:56 -0400821 if (ds->ops->get_ethtool_stats)
822 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700823}
824
825static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
826{
827 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400828 struct dsa_switch *ds = dst->cpu_dp->ds;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700829 int count = 0;
830
831 if (dst->master_ethtool_ops.get_sset_count)
832 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
833
Vivien Didelot9d490b42016-08-23 12:38:56 -0400834 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
835 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700836
837 return count;
838}
839
840static void dsa_cpu_port_get_strings(struct net_device *dev,
841 uint32_t stringset, uint8_t *data)
842{
843 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400844 struct dsa_switch *ds = dst->cpu_dp->ds;
845 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700846 int len = ETH_GSTRING_LEN;
847 int mcount = 0, count;
848 unsigned int i;
849 uint8_t pfx[4];
850 uint8_t *ndata;
851
852 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
853 /* We do not want to be NULL-terminated, since this is a prefix */
854 pfx[sizeof(pfx) - 1] = '_';
855
856 if (dst->master_ethtool_ops.get_sset_count) {
857 mcount = dst->master_ethtool_ops.get_sset_count(dev,
858 ETH_SS_STATS);
859 dst->master_ethtool_ops.get_strings(dev, stringset, data);
860 }
861
Vivien Didelot9d490b42016-08-23 12:38:56 -0400862 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700863 ndata = data + mcount * len;
864 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
865 * the output after to prepend our CPU port prefix we
866 * constructed earlier
867 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400868 ds->ops->get_strings(ds, cpu_port, ndata);
869 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700870 for (i = 0; i < count; i++) {
871 memmove(ndata + (i * len + sizeof(pfx)),
872 ndata + i * len, len - sizeof(pfx));
873 memcpy(ndata + i * len, pfx, sizeof(pfx));
874 }
875 }
876}
877
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000878static void dsa_slave_get_ethtool_stats(struct net_device *dev,
879 struct ethtool_stats *stats,
880 uint64_t *data)
881{
882 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500883 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000884
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400885 data[0] = dev->stats.tx_packets;
886 data[1] = dev->stats.tx_bytes;
887 data[2] = dev->stats.rx_packets;
888 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400889 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500890 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000891}
892
893static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
894{
895 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500896 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000897
898 if (sset == ETH_SS_STATS) {
899 int count;
900
901 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400902 if (ds->ops->get_sset_count)
903 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000904
905 return count;
906 }
907
908 return -EOPNOTSUPP;
909}
910
Florian Fainelli19e57c42014-09-18 17:31:24 -0700911static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
912{
913 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500914 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700915
Vivien Didelot9d490b42016-08-23 12:38:56 -0400916 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500917 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700918}
919
920static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
921{
922 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500923 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700924 int ret = -EOPNOTSUPP;
925
Vivien Didelot9d490b42016-08-23 12:38:56 -0400926 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500927 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700928
929 return ret;
930}
931
Florian Fainelli79052882014-09-24 17:05:21 -0700932static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
933{
934 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500935 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700936 int ret;
937
Vivien Didelot9d490b42016-08-23 12:38:56 -0400938 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700939 return -EOPNOTSUPP;
940
Vivien Didelotafdcf152017-01-27 15:29:39 -0500941 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700942 if (ret)
943 return ret;
944
945 if (p->phy)
946 ret = phy_ethtool_set_eee(p->phy, e);
947
948 return ret;
949}
950
951static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
952{
953 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500954 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700955 int ret;
956
Vivien Didelot9d490b42016-08-23 12:38:56 -0400957 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700958 return -EOPNOTSUPP;
959
Vivien Didelotafdcf152017-01-27 15:29:39 -0500960 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700961 if (ret)
962 return ret;
963
964 if (p->phy)
965 ret = phy_ethtool_get_eee(p->phy, e);
966
967 return ret;
968}
969
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700970#ifdef CONFIG_NET_POLL_CONTROLLER
971static int dsa_slave_netpoll_setup(struct net_device *dev,
972 struct netpoll_info *ni)
973{
974 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500975 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700976 struct net_device *master = ds->dst->master_netdev;
977 struct netpoll *netpoll;
978 int err = 0;
979
980 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
981 if (!netpoll)
982 return -ENOMEM;
983
984 err = __netpoll_setup(netpoll, master);
985 if (err) {
986 kfree(netpoll);
987 goto out;
988 }
989
990 p->netpoll = netpoll;
991out:
992 return err;
993}
994
995static void dsa_slave_netpoll_cleanup(struct net_device *dev)
996{
997 struct dsa_slave_priv *p = netdev_priv(dev);
998 struct netpoll *netpoll = p->netpoll;
999
1000 if (!netpoll)
1001 return;
1002
1003 p->netpoll = NULL;
1004
1005 __netpoll_free_async(netpoll);
1006}
1007
1008static void dsa_slave_poll_controller(struct net_device *dev)
1009{
1010}
1011#endif
1012
Florian Fainelli44bb7652017-01-10 12:32:36 -08001013static int dsa_slave_get_phys_port_name(struct net_device *dev,
1014 char *name, size_t len)
1015{
1016 struct dsa_slave_priv *p = netdev_priv(dev);
1017
Vivien Didelotafdcf152017-01-27 15:29:39 -05001018 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -08001019 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -08001020
1021 return 0;
1022}
1023
Florian Fainellif50f2122017-01-30 12:41:40 -08001024static struct dsa_mall_tc_entry *
1025dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1026 unsigned long cookie)
1027{
1028 struct dsa_mall_tc_entry *mall_tc_entry;
1029
1030 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1031 if (mall_tc_entry->cookie == cookie)
1032 return mall_tc_entry;
1033
1034 return NULL;
1035}
1036
1037static int dsa_slave_add_cls_matchall(struct net_device *dev,
1038 __be16 protocol,
1039 struct tc_cls_matchall_offload *cls,
1040 bool ingress)
1041{
1042 struct dsa_slave_priv *p = netdev_priv(dev);
1043 struct dsa_mall_tc_entry *mall_tc_entry;
1044 struct dsa_switch *ds = p->dp->ds;
1045 struct net *net = dev_net(dev);
1046 struct dsa_slave_priv *to_p;
1047 struct net_device *to_dev;
1048 const struct tc_action *a;
1049 int err = -EOPNOTSUPP;
1050 LIST_HEAD(actions);
1051 int ifindex;
1052
1053 if (!ds->ops->port_mirror_add)
1054 return err;
1055
1056 if (!tc_single_action(cls->exts))
1057 return err;
1058
1059 tcf_exts_to_list(cls->exts, &actions);
1060 a = list_first_entry(&actions, struct tc_action, list);
1061
1062 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1063 struct dsa_mall_mirror_tc_entry *mirror;
1064
1065 ifindex = tcf_mirred_ifindex(a);
1066 to_dev = __dev_get_by_index(net, ifindex);
1067 if (!to_dev)
1068 return -EINVAL;
1069
1070 if (!dsa_slave_dev_check(to_dev))
1071 return -EOPNOTSUPP;
1072
1073 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1074 if (!mall_tc_entry)
1075 return -ENOMEM;
1076
1077 mall_tc_entry->cookie = cls->cookie;
1078 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1079 mirror = &mall_tc_entry->mirror;
1080
1081 to_p = netdev_priv(to_dev);
1082
1083 mirror->to_local_port = to_p->dp->index;
1084 mirror->ingress = ingress;
1085
1086 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1087 ingress);
1088 if (err) {
1089 kfree(mall_tc_entry);
1090 return err;
1091 }
1092
1093 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1094 }
1095
1096 return 0;
1097}
1098
1099static void dsa_slave_del_cls_matchall(struct net_device *dev,
1100 struct tc_cls_matchall_offload *cls)
1101{
1102 struct dsa_slave_priv *p = netdev_priv(dev);
1103 struct dsa_mall_tc_entry *mall_tc_entry;
1104 struct dsa_switch *ds = p->dp->ds;
1105
1106 if (!ds->ops->port_mirror_del)
1107 return;
1108
1109 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1110 if (!mall_tc_entry)
1111 return;
1112
1113 list_del(&mall_tc_entry->list);
1114
1115 switch (mall_tc_entry->type) {
1116 case DSA_PORT_MALL_MIRROR:
1117 ds->ops->port_mirror_del(ds, p->dp->index,
1118 &mall_tc_entry->mirror);
1119 break;
1120 default:
1121 WARN_ON(1);
1122 }
1123
1124 kfree(mall_tc_entry);
1125}
1126
1127static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1128 __be16 protocol, struct tc_to_netdev *tc)
1129{
1130 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1131 int ret = -EOPNOTSUPP;
1132
1133 switch (tc->type) {
1134 case TC_SETUP_MATCHALL:
1135 switch (tc->cls_mall->command) {
1136 case TC_CLSMATCHALL_REPLACE:
1137 return dsa_slave_add_cls_matchall(dev, protocol,
1138 tc->cls_mall,
1139 ingress);
1140 case TC_CLSMATCHALL_DESTROY:
1141 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1142 return 0;
1143 }
1144 default:
1145 break;
1146 }
1147
1148 return ret;
1149}
1150
Florian Fainelliaf421922016-06-07 16:32:41 -07001151void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1152{
1153 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1154 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1155 ops->get_strings = dsa_cpu_port_get_strings;
1156}
1157
Florian Fainellibf9f2642017-01-30 09:48:40 -08001158static int dsa_slave_get_rxnfc(struct net_device *dev,
1159 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1160{
1161 struct dsa_slave_priv *p = netdev_priv(dev);
1162 struct dsa_switch *ds = p->dp->ds;
1163
1164 if (!ds->ops->get_rxnfc)
1165 return -EOPNOTSUPP;
1166
1167 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1168}
1169
1170static int dsa_slave_set_rxnfc(struct net_device *dev,
1171 struct ethtool_rxnfc *nfc)
1172{
1173 struct dsa_slave_priv *p = netdev_priv(dev);
1174 struct dsa_switch *ds = p->dp->ds;
1175
1176 if (!ds->ops->set_rxnfc)
1177 return -EOPNOTSUPP;
1178
1179 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1180}
1181
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001182static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001183 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001184 .get_regs_len = dsa_slave_get_regs_len,
1185 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001186 .nway_reset = dsa_slave_nway_reset,
1187 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001188 .get_eeprom_len = dsa_slave_get_eeprom_len,
1189 .get_eeprom = dsa_slave_get_eeprom,
1190 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001191 .get_strings = dsa_slave_get_strings,
1192 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1193 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001194 .set_wol = dsa_slave_set_wol,
1195 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001196 .set_eee = dsa_slave_set_eee,
1197 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +02001198 .get_link_ksettings = dsa_slave_get_link_ksettings,
1199 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001200 .get_rxnfc = dsa_slave_get_rxnfc,
1201 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001202};
1203
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001204static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001205 .ndo_open = dsa_slave_open,
1206 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001207 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001208 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1209 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001210 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001211 .ndo_fdb_add = switchdev_port_fdb_add,
1212 .ndo_fdb_del = switchdev_port_fdb_del,
1213 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001214 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001215 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001216#ifdef CONFIG_NET_POLL_CONTROLLER
1217 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1218 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1219 .ndo_poll_controller = dsa_slave_poll_controller,
1220#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001221 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1222 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1223 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -08001224 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001225 .ndo_setup_tc = dsa_slave_setup_tc,
Scott Feldman98237d42015-03-15 21:07:15 -07001226};
1227
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001228static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001229 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001230 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001231 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1232 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1233 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001234};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001235
Florian Fainellif37db852015-09-23 18:19:58 -07001236static struct device_type dsa_type = {
1237 .name = "dsa",
1238};
1239
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001240static void dsa_slave_adjust_link(struct net_device *dev)
1241{
1242 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001243 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001244 unsigned int status_changed = 0;
1245
1246 if (p->old_link != p->phy->link) {
1247 status_changed = 1;
1248 p->old_link = p->phy->link;
1249 }
1250
1251 if (p->old_duplex != p->phy->duplex) {
1252 status_changed = 1;
1253 p->old_duplex = p->phy->duplex;
1254 }
1255
1256 if (p->old_pause != p->phy->pause) {
1257 status_changed = 1;
1258 p->old_pause = p->phy->pause;
1259 }
1260
Vivien Didelot9d490b42016-08-23 12:38:56 -04001261 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001262 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001263
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001264 if (status_changed)
1265 phy_print_status(p->phy);
1266}
1267
Florian Fainellice31b312014-08-27 17:04:54 -07001268static int dsa_slave_fixed_link_update(struct net_device *dev,
1269 struct fixed_phy_status *status)
1270{
Andrew Lunnb71be352016-03-12 00:01:37 +01001271 struct dsa_slave_priv *p;
1272 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001273
Andrew Lunnb71be352016-03-12 00:01:37 +01001274 if (dev) {
1275 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001276 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001277 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001278 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001279 }
Florian Fainellice31b312014-08-27 17:04:54 -07001280
1281 return 0;
1282}
1283
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001284/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001285static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001286 struct net_device *slave_dev,
1287 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001288{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001289 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001290
Andrew Lunn7f854422016-01-06 20:11:18 +01001291 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001292 if (!p->phy) {
1293 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001294 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001295 }
Florian Fainellic305c162015-03-10 16:57:12 -07001296
1297 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001298 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1299 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001300 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1301 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001302}
1303
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001304static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001305 struct net_device *slave_dev)
1306{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001307 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001308 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001309 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001310 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001311 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001312
Vivien Didelotafdcf152017-01-27 15:29:39 -05001313 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001314 mode = of_get_phy_mode(port_dn);
1315 if (mode < 0)
1316 mode = PHY_INTERFACE_MODE_NA;
1317 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001318
1319 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001320 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001321 /* In the case of a fixed PHY, the DT node associated
1322 * to the fixed PHY is the Port DT node
1323 */
1324 ret = of_phy_register_fixed_link(port_dn);
1325 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001326 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001327 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001328 }
Florian Fainellice31b312014-08-27 17:04:54 -07001329 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001330 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001331 }
1332
Vivien Didelot9d490b42016-08-23 12:38:56 -04001333 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001334 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001335
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001336 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001337 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1338
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001339 /* If this PHY address is part of phys_mii_mask, which means
1340 * that we need to divert reads and writes to/from it, then we
1341 * want to bind this device using the slave MII bus created by
1342 * DSA to make that happen.
1343 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001344 if (!phy_is_fixed && phy_id >= 0 &&
1345 (ds->phys_mii_mask & (1 << phy_id))) {
1346 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1347 if (ret) {
1348 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001349 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001350 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001351 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001352 } else {
1353 p->phy = of_phy_connect(slave_dev, phy_dn,
1354 dsa_slave_adjust_link,
1355 phy_flags,
1356 p->phy_interface);
1357 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001358
1359 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001360 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001361
Florian Fainellice31b312014-08-27 17:04:54 -07001362 if (p->phy && phy_is_fixed)
1363 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1364
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001365 /* We could not connect to a designated PHY, so use the switch internal
1366 * MDIO bus instead
1367 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001368 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001369 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001370 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001371 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1372 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001373 if (phy_is_fixed)
1374 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001375 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001376 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001377 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001378
Andrew Lunn22209432016-01-06 20:11:13 +01001379 phy_attached_info(p->phy);
1380
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001381 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001382}
1383
Andrew Lunn448b4482015-05-06 01:09:56 +02001384static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1385static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1386 struct netdev_queue *txq,
1387 void *_unused)
1388{
1389 lockdep_set_class(&txq->_xmit_lock,
1390 &dsa_slave_netdev_xmit_lock_key);
1391}
1392
Florian Fainelli24462542014-09-18 17:31:22 -07001393int dsa_slave_suspend(struct net_device *slave_dev)
1394{
1395 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1396
Florian Fainellif154be22017-01-25 09:10:41 -08001397 netif_device_detach(slave_dev);
1398
Florian Fainelli24462542014-09-18 17:31:22 -07001399 if (p->phy) {
1400 phy_stop(p->phy);
1401 p->old_pause = -1;
1402 p->old_link = -1;
1403 p->old_duplex = -1;
1404 phy_suspend(p->phy);
1405 }
1406
1407 return 0;
1408}
1409
1410int dsa_slave_resume(struct net_device *slave_dev)
1411{
1412 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1413
1414 netif_device_attach(slave_dev);
1415
1416 if (p->phy) {
1417 phy_resume(p->phy);
1418 phy_start(p->phy);
1419 }
1420
1421 return 0;
1422}
1423
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001424int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001425 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001426{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001427 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001428 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001429 struct net_device *slave_dev;
1430 struct dsa_slave_priv *p;
1431 int ret;
1432
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001433 master = ds->dst->master_netdev;
1434 if (ds->master_netdev)
1435 master = ds->master_netdev;
1436
Tom Gundersenc835a672014-07-14 16:37:24 +02001437 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1438 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001439 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001440 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001441
Florian Fainellif50f2122017-01-30 12:41:40 -08001442 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1443 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001444 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001445 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001446 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001447 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001448 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001449 slave_dev->min_mtu = 0;
1450 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001451 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001452
Andrew Lunn448b4482015-05-06 01:09:56 +02001453 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1454 NULL);
1455
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001456 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001457 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001458 slave_dev->vlan_features = master->vlan_features;
1459
1460 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001461 p->dp = &ds->ports[port];
Florian Fainellif50f2122017-01-30 12:41:40 -08001462 INIT_LIST_HEAD(&p->mall_tc_list);
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001463 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001464
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001465 p->old_pause = -1;
1466 p->old_link = -1;
1467 p->old_duplex = -1;
1468
Andrew Lunnc8b09802016-06-04 21:16:57 +02001469 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001470 ret = register_netdev(slave_dev);
1471 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001472 netdev_err(master, "error %d registering interface %s\n",
1473 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001474 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001475 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001476 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001477 }
1478
1479 netif_carrier_off(slave_dev);
1480
Andrew Lunn0071f562016-01-06 20:11:20 +01001481 ret = dsa_slave_phy_setup(p, slave_dev);
1482 if (ret) {
1483 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001484 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001485 free_netdev(slave_dev);
1486 return ret;
1487 }
1488
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001489 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001490}
Florian Fainellib73adef2015-02-24 13:15:33 -08001491
Neil Armstrongcda5c152015-12-07 13:57:35 +01001492void dsa_slave_destroy(struct net_device *slave_dev)
1493{
1494 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001495 struct device_node *port_dn;
1496
Vivien Didelotafdcf152017-01-27 15:29:39 -05001497 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001498
1499 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001500 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001501 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001502
1503 if (of_phy_is_fixed_link(port_dn))
1504 of_phy_deregister_fixed_link(port_dn);
1505 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001506 unregister_netdev(slave_dev);
1507 free_netdev(slave_dev);
1508}
1509
Florian Fainellib73adef2015-02-24 13:15:33 -08001510static bool dsa_slave_dev_check(struct net_device *dev)
1511{
1512 return dev->netdev_ops == &dsa_slave_netdev_ops;
1513}
1514
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001515static int dsa_slave_changeupper(struct net_device *dev,
1516 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001517{
Vivien Didelot17d78022017-05-19 17:00:38 -04001518 struct dsa_slave_priv *p = netdev_priv(dev);
1519 struct dsa_port *dp = p->dp;
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001520 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001521
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001522 if (netif_is_bridge_master(info->upper_dev)) {
1523 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001524 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001525 err = notifier_from_errno(err);
1526 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001527 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001528 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001529 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001530 }
1531
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001532 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001533}
1534
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001535static int dsa_slave_netdevice_event(struct notifier_block *nb,
1536 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001537{
Vivien Didelot6debb682016-03-13 16:21:34 -04001538 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001539
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001540 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1541 return NOTIFY_DONE;
1542
1543 if (event == NETDEV_CHANGEUPPER)
1544 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001545
Florian Fainellib73adef2015-02-24 13:15:33 -08001546 return NOTIFY_DONE;
1547}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001548
1549static struct notifier_block dsa_slave_nb __read_mostly = {
1550 .notifier_call = dsa_slave_netdevice_event,
1551};
1552
1553int dsa_slave_register_notifier(void)
1554{
1555 return register_netdevice_notifier(&dsa_slave_nb);
1556}
1557
1558void dsa_slave_unregister_notifier(void)
1559{
1560 int err;
1561
1562 err = unregister_netdevice_notifier(&dsa_slave_nb);
1563 if (err)
1564 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1565}