blob: de39da69fd336cd8df5ab321d60790e919ed33eb [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 Didelot01676d12017-05-19 17:00:41 -0400257static int dsa_port_vlan_add(struct dsa_port *dp,
258 const struct switchdev_obj_port_vlan *vlan,
259 struct switchdev_trans *trans)
Vivien Didelot11149532015-08-13 12:52:17 -0400260{
Vivien Didelotafdcf152017-01-27 15:29:39 -0500261 struct dsa_switch *ds = dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400262
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200263 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400264 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
Vivien Didelot11149532015-08-13 12:52:17 -0400265 return -EOPNOTSUPP;
266
Vivien Didelotafdcf152017-01-27 15:29:39 -0500267 return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400268 }
269
Vivien Didelotafdcf152017-01-27 15:29:39 -0500270 ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
Vivien Didelot4d5770b2016-04-06 11:55:05 -0400271
Vivien Didelot11149532015-08-13 12:52:17 -0400272 return 0;
273}
274
Vivien Didelot01676d12017-05-19 17:00:41 -0400275static int dsa_port_vlan_del(struct dsa_port *dp,
276 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot11149532015-08-13 12:52:17 -0400277{
Vivien Didelot01676d12017-05-19 17:00:41 -0400278 struct dsa_switch *ds = dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400279
Vivien Didelot9d490b42016-08-23 12:38:56 -0400280 if (!ds->ops->port_vlan_del)
Vivien Didelot11149532015-08-13 12:52:17 -0400281 return -EOPNOTSUPP;
282
Vivien Didelot01676d12017-05-19 17:00:41 -0400283 return ds->ops->port_vlan_del(ds, dp->index, vlan);
Vivien Didelot11149532015-08-13 12:52:17 -0400284}
285
Vivien Didelot01676d12017-05-19 17:00:41 -0400286static int dsa_port_vlan_dump(struct dsa_port *dp,
287 struct switchdev_obj_port_vlan *vlan,
288 switchdev_obj_dump_cb_t *cb)
Vivien Didelot11149532015-08-13 12:52:17 -0400289{
Vivien Didelot01676d12017-05-19 17:00:41 -0400290 struct dsa_switch *ds = dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400291
Vivien Didelot9d490b42016-08-23 12:38:56 -0400292 if (ds->ops->port_vlan_dump)
Vivien Didelot01676d12017-05-19 17:00:41 -0400293 return ds->ops->port_vlan_dump(ds, dp->index, vlan, cb);
Vivien Didelot65aebfc2016-02-23 12:13:54 -0500294
Vivien Didelot477b1842016-02-23 12:13:56 -0500295 return -EOPNOTSUPP;
Vivien Didelot11149532015-08-13 12:52:17 -0400296}
297
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400298static int dsa_port_fdb_add(struct dsa_port *dp,
299 const struct switchdev_obj_port_fdb *fdb,
300 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700301{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400302 struct dsa_switch *ds = dp->ds;
Vivien Didelot146a3202015-10-08 11:35:12 -0400303
Vivien Didelot8497aa62016-04-06 11:55:04 -0400304 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400305 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
Vivien Didelot8497aa62016-04-06 11:55:04 -0400306 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700307
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400308 return ds->ops->port_fdb_prepare(ds, dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400309 }
David S. Millercdf09692015-08-11 12:00:37 -0700310
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400311 ds->ops->port_fdb_add(ds, dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400312
313 return 0;
David S. Millercdf09692015-08-11 12:00:37 -0700314}
315
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400316static int dsa_port_fdb_del(struct dsa_port *dp,
317 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -0700318{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400319 struct dsa_switch *ds = dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700320 int ret = -EOPNOTSUPP;
321
Vivien Didelot9d490b42016-08-23 12:38:56 -0400322 if (ds->ops->port_fdb_del)
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400323 ret = ds->ops->port_fdb_del(ds, dp->index, fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700324
325 return ret;
326}
327
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400328static int dsa_port_fdb_dump(struct dsa_port *dp,
329 struct switchdev_obj_port_fdb *fdb,
330 switchdev_obj_dump_cb_t *cb)
David S. Millercdf09692015-08-11 12:00:37 -0700331{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400332 struct dsa_switch *ds = dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700333
Vivien Didelot9d490b42016-08-23 12:38:56 -0400334 if (ds->ops->port_fdb_dump)
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400335 return ds->ops->port_fdb_dump(ds, dp->index, fdb, cb);
Vivien Didelotea70ba92015-10-22 09:34:38 -0400336
Vivien Didelot1a49a2f2015-10-22 09:34:43 -0400337 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700338}
339
Vivien Didelotbcebb972017-05-19 17:00:40 -0400340static int dsa_port_mdb_add(struct dsa_port *dp,
341 const struct switchdev_obj_port_mdb *mdb,
342 struct switchdev_trans *trans)
Vivien Didelot8df30252016-08-31 11:50:03 -0400343{
Vivien Didelotbcebb972017-05-19 17:00:40 -0400344 struct dsa_switch *ds = dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400345
346 if (switchdev_trans_ph_prepare(trans)) {
347 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
348 return -EOPNOTSUPP;
349
Vivien Didelotbcebb972017-05-19 17:00:40 -0400350 return ds->ops->port_mdb_prepare(ds, dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400351 }
352
Vivien Didelotbcebb972017-05-19 17:00:40 -0400353 ds->ops->port_mdb_add(ds, dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400354
355 return 0;
356}
357
Vivien Didelotbcebb972017-05-19 17:00:40 -0400358static int dsa_port_mdb_del(struct dsa_port *dp,
359 const struct switchdev_obj_port_mdb *mdb)
Vivien Didelot8df30252016-08-31 11:50:03 -0400360{
Vivien Didelotbcebb972017-05-19 17:00:40 -0400361 struct dsa_switch *ds = dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400362
363 if (ds->ops->port_mdb_del)
Vivien Didelotbcebb972017-05-19 17:00:40 -0400364 return ds->ops->port_mdb_del(ds, dp->index, mdb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400365
366 return -EOPNOTSUPP;
367}
368
Vivien Didelotbcebb972017-05-19 17:00:40 -0400369static int dsa_port_mdb_dump(struct dsa_port *dp,
370 struct switchdev_obj_port_mdb *mdb,
371 switchdev_obj_dump_cb_t *cb)
Vivien Didelot8df30252016-08-31 11:50:03 -0400372{
Vivien Didelotbcebb972017-05-19 17:00:40 -0400373 struct dsa_switch *ds = dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400374
375 if (ds->ops->port_mdb_dump)
Vivien Didelotbcebb972017-05-19 17:00:40 -0400376 return ds->ops->port_mdb_dump(ds, dp->index, mdb, cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400377
378 return -EOPNOTSUPP;
379}
380
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000381static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
382{
383 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000384
385 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000386 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000387
388 return -EOPNOTSUPP;
389}
390
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500391static int dsa_slave_vlan_filtering(struct net_device *dev,
392 const struct switchdev_attr *attr,
393 struct switchdev_trans *trans)
394{
395 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500396 struct dsa_switch *ds = p->dp->ds;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500397
398 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
399 if (switchdev_trans_ph_prepare(trans))
400 return 0;
401
Vivien Didelot9d490b42016-08-23 12:38:56 -0400402 if (ds->ops->port_vlan_filtering)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500403 return ds->ops->port_vlan_filtering(ds, p->dp->index,
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500404 attr->u.vlan_filtering);
405
406 return 0;
407}
408
Vivien Didelote893de1ba2017-03-15 15:53:48 -0400409static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
410 unsigned int ageing_time)
Vivien Didelot34a79f62016-07-18 20:45:38 -0400411{
412 int i;
413
Vivien Didelot26895e22017-01-27 15:29:37 -0500414 for (i = 0; i < ds->num_ports; ++i) {
Vivien Didelot34a79f62016-07-18 20:45:38 -0400415 struct dsa_port *dp = &ds->ports[i];
416
417 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
418 ageing_time = dp->ageing_time;
419 }
420
421 return ageing_time;
422}
423
424static int dsa_slave_ageing_time(struct net_device *dev,
425 const struct switchdev_attr *attr,
426 struct switchdev_trans *trans)
427{
428 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500429 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400430 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
431 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
432
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400433 if (switchdev_trans_ph_prepare(trans)) {
434 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
435 return -ERANGE;
436 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
437 return -ERANGE;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400438 return 0;
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400439 }
Vivien Didelot34a79f62016-07-18 20:45:38 -0400440
441 /* Keep the fastest ageing time in case of multiple bridges */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500442 p->dp->ageing_time = ageing_time;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400443 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
444
Vivien Didelot9d490b42016-08-23 12:38:56 -0400445 if (ds->ops->set_ageing_time)
446 return ds->ops->set_ageing_time(ds, ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400447
448 return 0;
449}
450
Scott Feldman35636062015-05-10 09:47:51 -0700451static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200452 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200453 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700454{
Vivien Didelotfd364542017-05-19 17:00:36 -0400455 struct dsa_slave_priv *p = netdev_priv(dev);
456 struct dsa_port *dp = p->dp;
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400457 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700458
459 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200460 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400461 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700462 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500463 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
464 ret = dsa_slave_vlan_filtering(dev, attr, trans);
465 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400466 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
467 ret = dsa_slave_ageing_time(dev, attr, trans);
468 break;
Scott Feldman35636062015-05-10 09:47:51 -0700469 default:
470 ret = -EOPNOTSUPP;
471 break;
472 }
473
474 return ret;
475}
476
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400477static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200478 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200479 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400480{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400481 struct dsa_slave_priv *p = netdev_priv(dev);
482 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400483 int err;
484
485 /* For the prepare phase, ensure the full set of changes is feasable in
486 * one go in order to signal a failure properly. If an operation is not
487 * supported, return -EOPNOTSUPP.
488 */
489
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200490 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200491 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400492 err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400493 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400494 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400495 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400496 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200497 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400498 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
499 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400500 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400501 default:
502 err = -EOPNOTSUPP;
503 break;
504 }
505
506 return err;
507}
508
509static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200510 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400511{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400512 struct dsa_slave_priv *p = netdev_priv(dev);
513 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400514 int err;
515
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200516 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200517 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400518 err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400519 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400520 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400521 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400522 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200523 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400524 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400525 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400526 default:
527 err = -EOPNOTSUPP;
528 break;
529 }
530
531 return err;
532}
533
534static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200535 struct switchdev_obj *obj,
536 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400537{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400538 struct dsa_slave_priv *p = netdev_priv(dev);
539 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400540 int err;
541
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200542 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200543 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400544 err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400545 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400546 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400547 err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400548 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200549 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400550 err = dsa_port_vlan_dump(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400551 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400552 default:
553 err = -EOPNOTSUPP;
554 break;
555 }
556
557 return err;
558}
559
Vivien Didelot17d78022017-05-19 17:00:38 -0400560static int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800561{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500562 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400563 .sw_index = dp->ds->index,
564 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500565 .br = br,
566 };
567 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800568
Vivien Didelot9c265422017-02-03 13:20:18 -0500569 /* Here the port is already bridged. Reflect the current configuration
570 * so that drivers can program their chips accordingly.
571 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400572 dp->bridge_dev = br;
Florian Fainellib73adef2015-02-24 13:15:33 -0800573
Vivien Didelot17d78022017-05-19 17:00:38 -0400574 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
Florian Fainellib73adef2015-02-24 13:15:33 -0800575
Vivien Didelot9c265422017-02-03 13:20:18 -0500576 /* The bridging is rolled back on error */
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500577 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400578 dp->bridge_dev = NULL;
Vivien Didelot9c265422017-02-03 13:20:18 -0500579
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500580 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800581}
582
Vivien Didelot17d78022017-05-19 17:00:38 -0400583static void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800584{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500585 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400586 .sw_index = dp->ds->index,
587 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500588 .br = br,
589 };
590 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800591
Vivien Didelot9c265422017-02-03 13:20:18 -0500592 /* Here the port is already unbridged. Reflect the current configuration
593 * so that drivers can program their chips accordingly.
594 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400595 dp->bridge_dev = NULL;
Florian Fainellib73adef2015-02-24 13:15:33 -0800596
Vivien Didelot17d78022017-05-19 17:00:38 -0400597 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500598 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400599 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
Florian Fainellib73adef2015-02-24 13:15:33 -0800600
601 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
602 * so allow it to be in BR_STATE_FORWARDING to be kept functional
603 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400604 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
Florian Fainellib73adef2015-02-24 13:15:33 -0800605}
606
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700607static int dsa_slave_port_attr_get(struct net_device *dev,
608 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800609{
610 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500611 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800612
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700613 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200614 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700615 attr->u.ppid.id_len = sizeof(ds->index);
616 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700617 break;
618 default:
619 return -EOPNOTSUPP;
620 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800621
622 return 0;
623}
624
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700625static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
626 struct sk_buff *skb)
627{
628#ifdef CONFIG_NET_POLL_CONTROLLER
629 if (p->netpoll)
630 netpoll_send_skb(p->netpoll, skb);
631#else
632 BUG();
633#endif
634 return NETDEV_TX_OK;
635}
636
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700637static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
638{
639 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700640 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700641
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700642 dev->stats.tx_packets++;
643 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700644
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700645 /* Transmit function may have to reallocate the original SKB */
646 nskb = p->xmit(skb, dev);
647 if (!nskb)
648 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700649
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700650 /* SKB for netpoll still need to be mangled with the protocol-specific
651 * tag to be successfully transmitted
652 */
653 if (unlikely(netpoll_tx_running(dev)))
654 return dsa_netpoll_send_skb(p, nskb);
655
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700656 /* Queue the SKB for transmission on the parent interface, but
657 * do not modify its EtherType
658 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500659 nskb->dev = p->dp->ds->dst->master_netdev;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700660 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700661
662 return NETDEV_TX_OK;
663}
664
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000665/* ethtool operations *******************************************************/
666static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200667dsa_slave_get_link_ksettings(struct net_device *dev,
668 struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000669{
670 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellie69e4622017-02-06 15:55:23 -0800671 int err = -EOPNOTSUPP;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000672
Florian Fainellie69e4622017-02-06 15:55:23 -0800673 if (p->phy != NULL)
674 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000675
676 return err;
677}
678
679static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200680dsa_slave_set_link_ksettings(struct net_device *dev,
681 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000682{
683 struct dsa_slave_priv *p = netdev_priv(dev);
684
685 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200686 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000687
688 return -EOPNOTSUPP;
689}
690
691static void dsa_slave_get_drvinfo(struct net_device *dev,
692 struct ethtool_drvinfo *drvinfo)
693{
Jiri Pirko7826d432013-01-06 00:44:26 +0000694 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000695 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
696 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000697}
698
Guenter Roeck3d762a02014-10-29 10:45:04 -0700699static int dsa_slave_get_regs_len(struct net_device *dev)
700{
701 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500702 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700703
Vivien Didelot9d490b42016-08-23 12:38:56 -0400704 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500705 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700706
707 return -EOPNOTSUPP;
708}
709
710static void
711dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
712{
713 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500714 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700715
Vivien Didelot9d490b42016-08-23 12:38:56 -0400716 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500717 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700718}
719
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000720static int dsa_slave_nway_reset(struct net_device *dev)
721{
722 struct dsa_slave_priv *p = netdev_priv(dev);
723
724 if (p->phy != NULL)
725 return genphy_restart_aneg(p->phy);
726
727 return -EOPNOTSUPP;
728}
729
730static u32 dsa_slave_get_link(struct net_device *dev)
731{
732 struct dsa_slave_priv *p = netdev_priv(dev);
733
734 if (p->phy != NULL) {
735 genphy_update_link(p->phy);
736 return p->phy->link;
737 }
738
739 return -EOPNOTSUPP;
740}
741
Guenter Roeck6793abb2014-10-29 10:45:01 -0700742static int dsa_slave_get_eeprom_len(struct net_device *dev)
743{
744 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500745 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700746
Andrew Lunn0e576042016-06-04 21:16:52 +0200747 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200748 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700749
Vivien Didelot9d490b42016-08-23 12:38:56 -0400750 if (ds->ops->get_eeprom_len)
751 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700752
753 return 0;
754}
755
756static int dsa_slave_get_eeprom(struct net_device *dev,
757 struct ethtool_eeprom *eeprom, u8 *data)
758{
759 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500760 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700761
Vivien Didelot9d490b42016-08-23 12:38:56 -0400762 if (ds->ops->get_eeprom)
763 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700764
765 return -EOPNOTSUPP;
766}
767
768static int dsa_slave_set_eeprom(struct net_device *dev,
769 struct ethtool_eeprom *eeprom, u8 *data)
770{
771 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500772 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700773
Vivien Didelot9d490b42016-08-23 12:38:56 -0400774 if (ds->ops->set_eeprom)
775 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700776
777 return -EOPNOTSUPP;
778}
779
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000780static void dsa_slave_get_strings(struct net_device *dev,
781 uint32_t stringset, uint8_t *data)
782{
783 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500784 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000785
786 if (stringset == ETH_SS_STATS) {
787 int len = ETH_GSTRING_LEN;
788
789 strncpy(data, "tx_packets", len);
790 strncpy(data + len, "tx_bytes", len);
791 strncpy(data + 2 * len, "rx_packets", len);
792 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400793 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500794 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000795 }
796}
797
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700798static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
799 struct ethtool_stats *stats,
800 uint64_t *data)
801{
802 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400803 struct dsa_switch *ds = dst->cpu_dp->ds;
804 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700805 int count = 0;
806
807 if (dst->master_ethtool_ops.get_sset_count) {
808 count = dst->master_ethtool_ops.get_sset_count(dev,
809 ETH_SS_STATS);
810 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
811 }
812
Vivien Didelot9d490b42016-08-23 12:38:56 -0400813 if (ds->ops->get_ethtool_stats)
814 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700815}
816
817static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
818{
819 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400820 struct dsa_switch *ds = dst->cpu_dp->ds;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700821 int count = 0;
822
823 if (dst->master_ethtool_ops.get_sset_count)
824 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
825
Vivien Didelot9d490b42016-08-23 12:38:56 -0400826 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
827 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700828
829 return count;
830}
831
832static void dsa_cpu_port_get_strings(struct net_device *dev,
833 uint32_t stringset, uint8_t *data)
834{
835 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400836 struct dsa_switch *ds = dst->cpu_dp->ds;
837 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700838 int len = ETH_GSTRING_LEN;
839 int mcount = 0, count;
840 unsigned int i;
841 uint8_t pfx[4];
842 uint8_t *ndata;
843
844 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
845 /* We do not want to be NULL-terminated, since this is a prefix */
846 pfx[sizeof(pfx) - 1] = '_';
847
848 if (dst->master_ethtool_ops.get_sset_count) {
849 mcount = dst->master_ethtool_ops.get_sset_count(dev,
850 ETH_SS_STATS);
851 dst->master_ethtool_ops.get_strings(dev, stringset, data);
852 }
853
Vivien Didelot9d490b42016-08-23 12:38:56 -0400854 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700855 ndata = data + mcount * len;
856 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
857 * the output after to prepend our CPU port prefix we
858 * constructed earlier
859 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400860 ds->ops->get_strings(ds, cpu_port, ndata);
861 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700862 for (i = 0; i < count; i++) {
863 memmove(ndata + (i * len + sizeof(pfx)),
864 ndata + i * len, len - sizeof(pfx));
865 memcpy(ndata + i * len, pfx, sizeof(pfx));
866 }
867 }
868}
869
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000870static void dsa_slave_get_ethtool_stats(struct net_device *dev,
871 struct ethtool_stats *stats,
872 uint64_t *data)
873{
874 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500875 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000876
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400877 data[0] = dev->stats.tx_packets;
878 data[1] = dev->stats.tx_bytes;
879 data[2] = dev->stats.rx_packets;
880 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400881 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500882 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000883}
884
885static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
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
890 if (sset == ETH_SS_STATS) {
891 int count;
892
893 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400894 if (ds->ops->get_sset_count)
895 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000896
897 return count;
898 }
899
900 return -EOPNOTSUPP;
901}
902
Florian Fainelli19e57c42014-09-18 17:31:24 -0700903static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
904{
905 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500906 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700907
Vivien Didelot9d490b42016-08-23 12:38:56 -0400908 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500909 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700910}
911
912static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
913{
914 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500915 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700916 int ret = -EOPNOTSUPP;
917
Vivien Didelot9d490b42016-08-23 12:38:56 -0400918 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500919 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700920
921 return ret;
922}
923
Florian Fainelli79052882014-09-24 17:05:21 -0700924static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
925{
926 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500927 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700928 int ret;
929
Vivien Didelot9d490b42016-08-23 12:38:56 -0400930 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700931 return -EOPNOTSUPP;
932
Vivien Didelotafdcf152017-01-27 15:29:39 -0500933 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700934 if (ret)
935 return ret;
936
937 if (p->phy)
938 ret = phy_ethtool_set_eee(p->phy, e);
939
940 return ret;
941}
942
943static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
944{
945 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500946 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700947 int ret;
948
Vivien Didelot9d490b42016-08-23 12:38:56 -0400949 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700950 return -EOPNOTSUPP;
951
Vivien Didelotafdcf152017-01-27 15:29:39 -0500952 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700953 if (ret)
954 return ret;
955
956 if (p->phy)
957 ret = phy_ethtool_get_eee(p->phy, e);
958
959 return ret;
960}
961
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700962#ifdef CONFIG_NET_POLL_CONTROLLER
963static int dsa_slave_netpoll_setup(struct net_device *dev,
964 struct netpoll_info *ni)
965{
966 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500967 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700968 struct net_device *master = ds->dst->master_netdev;
969 struct netpoll *netpoll;
970 int err = 0;
971
972 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
973 if (!netpoll)
974 return -ENOMEM;
975
976 err = __netpoll_setup(netpoll, master);
977 if (err) {
978 kfree(netpoll);
979 goto out;
980 }
981
982 p->netpoll = netpoll;
983out:
984 return err;
985}
986
987static void dsa_slave_netpoll_cleanup(struct net_device *dev)
988{
989 struct dsa_slave_priv *p = netdev_priv(dev);
990 struct netpoll *netpoll = p->netpoll;
991
992 if (!netpoll)
993 return;
994
995 p->netpoll = NULL;
996
997 __netpoll_free_async(netpoll);
998}
999
1000static void dsa_slave_poll_controller(struct net_device *dev)
1001{
1002}
1003#endif
1004
Florian Fainelli44bb7652017-01-10 12:32:36 -08001005static int dsa_slave_get_phys_port_name(struct net_device *dev,
1006 char *name, size_t len)
1007{
1008 struct dsa_slave_priv *p = netdev_priv(dev);
1009
Vivien Didelotafdcf152017-01-27 15:29:39 -05001010 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -08001011 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -08001012
1013 return 0;
1014}
1015
Florian Fainellif50f2122017-01-30 12:41:40 -08001016static struct dsa_mall_tc_entry *
1017dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1018 unsigned long cookie)
1019{
1020 struct dsa_mall_tc_entry *mall_tc_entry;
1021
1022 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1023 if (mall_tc_entry->cookie == cookie)
1024 return mall_tc_entry;
1025
1026 return NULL;
1027}
1028
1029static int dsa_slave_add_cls_matchall(struct net_device *dev,
1030 __be16 protocol,
1031 struct tc_cls_matchall_offload *cls,
1032 bool ingress)
1033{
1034 struct dsa_slave_priv *p = netdev_priv(dev);
1035 struct dsa_mall_tc_entry *mall_tc_entry;
1036 struct dsa_switch *ds = p->dp->ds;
1037 struct net *net = dev_net(dev);
1038 struct dsa_slave_priv *to_p;
1039 struct net_device *to_dev;
1040 const struct tc_action *a;
1041 int err = -EOPNOTSUPP;
1042 LIST_HEAD(actions);
1043 int ifindex;
1044
1045 if (!ds->ops->port_mirror_add)
1046 return err;
1047
1048 if (!tc_single_action(cls->exts))
1049 return err;
1050
1051 tcf_exts_to_list(cls->exts, &actions);
1052 a = list_first_entry(&actions, struct tc_action, list);
1053
1054 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1055 struct dsa_mall_mirror_tc_entry *mirror;
1056
1057 ifindex = tcf_mirred_ifindex(a);
1058 to_dev = __dev_get_by_index(net, ifindex);
1059 if (!to_dev)
1060 return -EINVAL;
1061
1062 if (!dsa_slave_dev_check(to_dev))
1063 return -EOPNOTSUPP;
1064
1065 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1066 if (!mall_tc_entry)
1067 return -ENOMEM;
1068
1069 mall_tc_entry->cookie = cls->cookie;
1070 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1071 mirror = &mall_tc_entry->mirror;
1072
1073 to_p = netdev_priv(to_dev);
1074
1075 mirror->to_local_port = to_p->dp->index;
1076 mirror->ingress = ingress;
1077
1078 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1079 ingress);
1080 if (err) {
1081 kfree(mall_tc_entry);
1082 return err;
1083 }
1084
1085 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1086 }
1087
1088 return 0;
1089}
1090
1091static void dsa_slave_del_cls_matchall(struct net_device *dev,
1092 struct tc_cls_matchall_offload *cls)
1093{
1094 struct dsa_slave_priv *p = netdev_priv(dev);
1095 struct dsa_mall_tc_entry *mall_tc_entry;
1096 struct dsa_switch *ds = p->dp->ds;
1097
1098 if (!ds->ops->port_mirror_del)
1099 return;
1100
1101 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1102 if (!mall_tc_entry)
1103 return;
1104
1105 list_del(&mall_tc_entry->list);
1106
1107 switch (mall_tc_entry->type) {
1108 case DSA_PORT_MALL_MIRROR:
1109 ds->ops->port_mirror_del(ds, p->dp->index,
1110 &mall_tc_entry->mirror);
1111 break;
1112 default:
1113 WARN_ON(1);
1114 }
1115
1116 kfree(mall_tc_entry);
1117}
1118
1119static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1120 __be16 protocol, struct tc_to_netdev *tc)
1121{
1122 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1123 int ret = -EOPNOTSUPP;
1124
1125 switch (tc->type) {
1126 case TC_SETUP_MATCHALL:
1127 switch (tc->cls_mall->command) {
1128 case TC_CLSMATCHALL_REPLACE:
1129 return dsa_slave_add_cls_matchall(dev, protocol,
1130 tc->cls_mall,
1131 ingress);
1132 case TC_CLSMATCHALL_DESTROY:
1133 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1134 return 0;
1135 }
1136 default:
1137 break;
1138 }
1139
1140 return ret;
1141}
1142
Florian Fainelliaf421922016-06-07 16:32:41 -07001143void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1144{
1145 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1146 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1147 ops->get_strings = dsa_cpu_port_get_strings;
1148}
1149
Florian Fainellibf9f2642017-01-30 09:48:40 -08001150static int dsa_slave_get_rxnfc(struct net_device *dev,
1151 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1152{
1153 struct dsa_slave_priv *p = netdev_priv(dev);
1154 struct dsa_switch *ds = p->dp->ds;
1155
1156 if (!ds->ops->get_rxnfc)
1157 return -EOPNOTSUPP;
1158
1159 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1160}
1161
1162static int dsa_slave_set_rxnfc(struct net_device *dev,
1163 struct ethtool_rxnfc *nfc)
1164{
1165 struct dsa_slave_priv *p = netdev_priv(dev);
1166 struct dsa_switch *ds = p->dp->ds;
1167
1168 if (!ds->ops->set_rxnfc)
1169 return -EOPNOTSUPP;
1170
1171 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1172}
1173
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001174static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001175 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001176 .get_regs_len = dsa_slave_get_regs_len,
1177 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001178 .nway_reset = dsa_slave_nway_reset,
1179 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001180 .get_eeprom_len = dsa_slave_get_eeprom_len,
1181 .get_eeprom = dsa_slave_get_eeprom,
1182 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001183 .get_strings = dsa_slave_get_strings,
1184 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1185 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001186 .set_wol = dsa_slave_set_wol,
1187 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001188 .set_eee = dsa_slave_set_eee,
1189 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +02001190 .get_link_ksettings = dsa_slave_get_link_ksettings,
1191 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001192 .get_rxnfc = dsa_slave_get_rxnfc,
1193 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001194};
1195
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001196static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001197 .ndo_open = dsa_slave_open,
1198 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001199 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001200 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1201 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001202 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001203 .ndo_fdb_add = switchdev_port_fdb_add,
1204 .ndo_fdb_del = switchdev_port_fdb_del,
1205 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001206 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001207 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001208#ifdef CONFIG_NET_POLL_CONTROLLER
1209 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1210 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1211 .ndo_poll_controller = dsa_slave_poll_controller,
1212#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001213 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1214 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1215 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -08001216 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001217 .ndo_setup_tc = dsa_slave_setup_tc,
Scott Feldman98237d42015-03-15 21:07:15 -07001218};
1219
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001220static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001221 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001222 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001223 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1224 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1225 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001226};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001227
Florian Fainellif37db852015-09-23 18:19:58 -07001228static struct device_type dsa_type = {
1229 .name = "dsa",
1230};
1231
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001232static void dsa_slave_adjust_link(struct net_device *dev)
1233{
1234 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001235 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001236 unsigned int status_changed = 0;
1237
1238 if (p->old_link != p->phy->link) {
1239 status_changed = 1;
1240 p->old_link = p->phy->link;
1241 }
1242
1243 if (p->old_duplex != p->phy->duplex) {
1244 status_changed = 1;
1245 p->old_duplex = p->phy->duplex;
1246 }
1247
1248 if (p->old_pause != p->phy->pause) {
1249 status_changed = 1;
1250 p->old_pause = p->phy->pause;
1251 }
1252
Vivien Didelot9d490b42016-08-23 12:38:56 -04001253 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001254 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001255
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001256 if (status_changed)
1257 phy_print_status(p->phy);
1258}
1259
Florian Fainellice31b312014-08-27 17:04:54 -07001260static int dsa_slave_fixed_link_update(struct net_device *dev,
1261 struct fixed_phy_status *status)
1262{
Andrew Lunnb71be352016-03-12 00:01:37 +01001263 struct dsa_slave_priv *p;
1264 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001265
Andrew Lunnb71be352016-03-12 00:01:37 +01001266 if (dev) {
1267 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001268 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001269 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001270 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001271 }
Florian Fainellice31b312014-08-27 17:04:54 -07001272
1273 return 0;
1274}
1275
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001276/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001277static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001278 struct net_device *slave_dev,
1279 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001280{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001281 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001282
Andrew Lunn7f854422016-01-06 20:11:18 +01001283 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001284 if (!p->phy) {
1285 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001286 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001287 }
Florian Fainellic305c162015-03-10 16:57:12 -07001288
1289 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001290 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1291 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001292 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1293 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001294}
1295
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001296static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001297 struct net_device *slave_dev)
1298{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001299 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001300 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001301 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001302 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001303 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001304
Vivien Didelotafdcf152017-01-27 15:29:39 -05001305 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001306 mode = of_get_phy_mode(port_dn);
1307 if (mode < 0)
1308 mode = PHY_INTERFACE_MODE_NA;
1309 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001310
1311 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001312 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001313 /* In the case of a fixed PHY, the DT node associated
1314 * to the fixed PHY is the Port DT node
1315 */
1316 ret = of_phy_register_fixed_link(port_dn);
1317 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001318 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001319 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001320 }
Florian Fainellice31b312014-08-27 17:04:54 -07001321 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001322 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001323 }
1324
Vivien Didelot9d490b42016-08-23 12:38:56 -04001325 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001326 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001327
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001328 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001329 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1330
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001331 /* If this PHY address is part of phys_mii_mask, which means
1332 * that we need to divert reads and writes to/from it, then we
1333 * want to bind this device using the slave MII bus created by
1334 * DSA to make that happen.
1335 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001336 if (!phy_is_fixed && phy_id >= 0 &&
1337 (ds->phys_mii_mask & (1 << phy_id))) {
1338 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1339 if (ret) {
1340 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001341 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001342 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001343 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001344 } else {
1345 p->phy = of_phy_connect(slave_dev, phy_dn,
1346 dsa_slave_adjust_link,
1347 phy_flags,
1348 p->phy_interface);
1349 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001350
1351 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001352 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001353
Florian Fainellice31b312014-08-27 17:04:54 -07001354 if (p->phy && phy_is_fixed)
1355 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1356
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001357 /* We could not connect to a designated PHY, so use the switch internal
1358 * MDIO bus instead
1359 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001360 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001361 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001362 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001363 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1364 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001365 if (phy_is_fixed)
1366 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001367 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001368 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001369 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001370
Andrew Lunn22209432016-01-06 20:11:13 +01001371 phy_attached_info(p->phy);
1372
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001373 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001374}
1375
Andrew Lunn448b4482015-05-06 01:09:56 +02001376static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1377static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1378 struct netdev_queue *txq,
1379 void *_unused)
1380{
1381 lockdep_set_class(&txq->_xmit_lock,
1382 &dsa_slave_netdev_xmit_lock_key);
1383}
1384
Florian Fainelli24462542014-09-18 17:31:22 -07001385int dsa_slave_suspend(struct net_device *slave_dev)
1386{
1387 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1388
Florian Fainellif154be22017-01-25 09:10:41 -08001389 netif_device_detach(slave_dev);
1390
Florian Fainelli24462542014-09-18 17:31:22 -07001391 if (p->phy) {
1392 phy_stop(p->phy);
1393 p->old_pause = -1;
1394 p->old_link = -1;
1395 p->old_duplex = -1;
1396 phy_suspend(p->phy);
1397 }
1398
1399 return 0;
1400}
1401
1402int dsa_slave_resume(struct net_device *slave_dev)
1403{
1404 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1405
1406 netif_device_attach(slave_dev);
1407
1408 if (p->phy) {
1409 phy_resume(p->phy);
1410 phy_start(p->phy);
1411 }
1412
1413 return 0;
1414}
1415
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001416int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001417 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001418{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001419 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001420 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001421 struct net_device *slave_dev;
1422 struct dsa_slave_priv *p;
1423 int ret;
1424
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001425 master = ds->dst->master_netdev;
1426 if (ds->master_netdev)
1427 master = ds->master_netdev;
1428
Tom Gundersenc835a672014-07-14 16:37:24 +02001429 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1430 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001431 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001432 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001433
Florian Fainellif50f2122017-01-30 12:41:40 -08001434 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1435 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001436 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001437 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001438 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001439 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001440 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001441 slave_dev->min_mtu = 0;
1442 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001443 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001444
Andrew Lunn448b4482015-05-06 01:09:56 +02001445 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1446 NULL);
1447
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001448 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001449 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001450 slave_dev->vlan_features = master->vlan_features;
1451
1452 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001453 p->dp = &ds->ports[port];
Florian Fainellif50f2122017-01-30 12:41:40 -08001454 INIT_LIST_HEAD(&p->mall_tc_list);
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001455 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001456
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001457 p->old_pause = -1;
1458 p->old_link = -1;
1459 p->old_duplex = -1;
1460
Andrew Lunnc8b09802016-06-04 21:16:57 +02001461 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001462 ret = register_netdev(slave_dev);
1463 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001464 netdev_err(master, "error %d registering interface %s\n",
1465 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001466 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001467 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001468 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001469 }
1470
1471 netif_carrier_off(slave_dev);
1472
Andrew Lunn0071f562016-01-06 20:11:20 +01001473 ret = dsa_slave_phy_setup(p, slave_dev);
1474 if (ret) {
1475 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001476 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001477 free_netdev(slave_dev);
1478 return ret;
1479 }
1480
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001481 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001482}
Florian Fainellib73adef2015-02-24 13:15:33 -08001483
Neil Armstrongcda5c152015-12-07 13:57:35 +01001484void dsa_slave_destroy(struct net_device *slave_dev)
1485{
1486 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001487 struct device_node *port_dn;
1488
Vivien Didelotafdcf152017-01-27 15:29:39 -05001489 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001490
1491 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001492 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001493 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001494
1495 if (of_phy_is_fixed_link(port_dn))
1496 of_phy_deregister_fixed_link(port_dn);
1497 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001498 unregister_netdev(slave_dev);
1499 free_netdev(slave_dev);
1500}
1501
Florian Fainellib73adef2015-02-24 13:15:33 -08001502static bool dsa_slave_dev_check(struct net_device *dev)
1503{
1504 return dev->netdev_ops == &dsa_slave_netdev_ops;
1505}
1506
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001507static int dsa_slave_changeupper(struct net_device *dev,
1508 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001509{
Vivien Didelot17d78022017-05-19 17:00:38 -04001510 struct dsa_slave_priv *p = netdev_priv(dev);
1511 struct dsa_port *dp = p->dp;
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001512 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001513
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001514 if (netif_is_bridge_master(info->upper_dev)) {
1515 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001516 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001517 err = notifier_from_errno(err);
1518 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001519 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001520 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001521 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001522 }
1523
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001524 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001525}
1526
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001527static int dsa_slave_netdevice_event(struct notifier_block *nb,
1528 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001529{
Vivien Didelot6debb682016-03-13 16:21:34 -04001530 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001531
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001532 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1533 return NOTIFY_DONE;
1534
1535 if (event == NETDEV_CHANGEUPPER)
1536 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001537
Florian Fainellib73adef2015-02-24 13:15:33 -08001538 return NOTIFY_DONE;
1539}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001540
1541static struct notifier_block dsa_slave_nb __read_mostly = {
1542 .notifier_call = dsa_slave_netdevice_event,
1543};
1544
1545int dsa_slave_register_notifier(void)
1546{
1547 return register_netdevice_notifier(&dsa_slave_nb);
1548}
1549
1550void dsa_slave_unregister_notifier(void)
1551{
1552 int err;
1553
1554 err = unregister_netdevice_notifier(&dsa_slave_nb);
1555 if (err)
1556 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1557}