blob: b0150f79dcddc20f7c58b9ba6bac4d2aca625bfb [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 Didelotc02c4172017-05-19 17:00:42 -0400391static int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
392 struct switchdev_trans *trans)
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500393{
Vivien Didelotc02c4172017-05-19 17:00:42 -0400394 struct dsa_switch *ds = dp->ds;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500395
396 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
397 if (switchdev_trans_ph_prepare(trans))
398 return 0;
399
Vivien Didelot9d490b42016-08-23 12:38:56 -0400400 if (ds->ops->port_vlan_filtering)
Vivien Didelotc02c4172017-05-19 17:00:42 -0400401 return ds->ops->port_vlan_filtering(ds, dp->index,
402 vlan_filtering);
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500403
404 return 0;
405}
406
Vivien Didelote893de1ba2017-03-15 15:53:48 -0400407static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
408 unsigned int ageing_time)
Vivien Didelot34a79f62016-07-18 20:45:38 -0400409{
410 int i;
411
Vivien Didelot26895e22017-01-27 15:29:37 -0500412 for (i = 0; i < ds->num_ports; ++i) {
Vivien Didelot34a79f62016-07-18 20:45:38 -0400413 struct dsa_port *dp = &ds->ports[i];
414
Vivien Didelot072bb192017-05-19 17:00:43 -0400415 if (dp->ageing_time && dp->ageing_time < ageing_time)
Vivien Didelot34a79f62016-07-18 20:45:38 -0400416 ageing_time = dp->ageing_time;
417 }
418
419 return ageing_time;
420}
421
Vivien Didelot072bb192017-05-19 17:00:43 -0400422static int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
423 struct switchdev_trans *trans)
Vivien Didelot34a79f62016-07-18 20:45:38 -0400424{
Vivien Didelot072bb192017-05-19 17:00:43 -0400425 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400426 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
Vivien Didelot072bb192017-05-19 17:00:43 -0400427 struct dsa_switch *ds = dp->ds;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400428
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400429 if (switchdev_trans_ph_prepare(trans)) {
430 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
431 return -ERANGE;
432 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
433 return -ERANGE;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400434 return 0;
Vivien Didelot0f3da6a2017-03-15 15:53:49 -0400435 }
Vivien Didelot34a79f62016-07-18 20:45:38 -0400436
437 /* Keep the fastest ageing time in case of multiple bridges */
Vivien Didelot072bb192017-05-19 17:00:43 -0400438 dp->ageing_time = ageing_time;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400439 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
440
Vivien Didelot9d490b42016-08-23 12:38:56 -0400441 if (ds->ops->set_ageing_time)
442 return ds->ops->set_ageing_time(ds, ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400443
444 return 0;
445}
446
Scott Feldman35636062015-05-10 09:47:51 -0700447static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200448 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200449 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700450{
Vivien Didelotfd364542017-05-19 17:00:36 -0400451 struct dsa_slave_priv *p = netdev_priv(dev);
452 struct dsa_port *dp = p->dp;
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400453 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700454
455 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200456 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400457 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700458 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500459 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vivien Didelotc02c4172017-05-19 17:00:42 -0400460 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
461 trans);
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500462 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400463 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Vivien Didelot072bb192017-05-19 17:00:43 -0400464 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400465 break;
Scott Feldman35636062015-05-10 09:47:51 -0700466 default:
467 ret = -EOPNOTSUPP;
468 break;
469 }
470
471 return ret;
472}
473
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400474static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200475 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200476 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400477{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400478 struct dsa_slave_priv *p = netdev_priv(dev);
479 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400480 int err;
481
482 /* For the prepare phase, ensure the full set of changes is feasable in
483 * one go in order to signal a failure properly. If an operation is not
484 * supported, return -EOPNOTSUPP.
485 */
486
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200487 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200488 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400489 err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400490 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400491 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400492 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400493 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200494 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400495 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
496 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400497 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400498 default:
499 err = -EOPNOTSUPP;
500 break;
501 }
502
503 return err;
504}
505
506static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200507 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400508{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400509 struct dsa_slave_priv *p = netdev_priv(dev);
510 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400511 int err;
512
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200513 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200514 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400515 err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400516 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400517 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400518 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400519 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200520 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400521 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400522 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400523 default:
524 err = -EOPNOTSUPP;
525 break;
526 }
527
528 return err;
529}
530
531static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200532 struct switchdev_obj *obj,
533 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400534{
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400535 struct dsa_slave_priv *p = netdev_priv(dev);
536 struct dsa_port *dp = p->dp;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400537 int err;
538
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200539 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200540 case SWITCHDEV_OBJ_ID_PORT_FDB:
Vivien Didelot3fdb0232017-05-19 17:00:39 -0400541 err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400542 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400543 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400544 err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400545 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200546 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400547 err = dsa_port_vlan_dump(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400548 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400549 default:
550 err = -EOPNOTSUPP;
551 break;
552 }
553
554 return err;
555}
556
Vivien Didelot17d78022017-05-19 17:00:38 -0400557static int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800558{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500559 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400560 .sw_index = dp->ds->index,
561 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500562 .br = br,
563 };
564 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800565
Vivien Didelot9c265422017-02-03 13:20:18 -0500566 /* Here the port is already bridged. Reflect the current configuration
567 * so that drivers can program their chips accordingly.
568 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400569 dp->bridge_dev = br;
Florian Fainellib73adef2015-02-24 13:15:33 -0800570
Vivien Didelot17d78022017-05-19 17:00:38 -0400571 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
Florian Fainellib73adef2015-02-24 13:15:33 -0800572
Vivien Didelot9c265422017-02-03 13:20:18 -0500573 /* The bridging is rolled back on error */
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500574 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400575 dp->bridge_dev = NULL;
Vivien Didelot9c265422017-02-03 13:20:18 -0500576
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500577 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800578}
579
Vivien Didelot17d78022017-05-19 17:00:38 -0400580static void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800581{
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500582 struct dsa_notifier_bridge_info info = {
Vivien Didelot17d78022017-05-19 17:00:38 -0400583 .sw_index = dp->ds->index,
584 .port = dp->index,
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500585 .br = br,
586 };
587 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800588
Vivien Didelot9c265422017-02-03 13:20:18 -0500589 /* Here the port is already unbridged. Reflect the current configuration
590 * so that drivers can program their chips accordingly.
591 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400592 dp->bridge_dev = NULL;
Florian Fainellib73adef2015-02-24 13:15:33 -0800593
Vivien Didelot17d78022017-05-19 17:00:38 -0400594 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500595 if (err)
Vivien Didelot17d78022017-05-19 17:00:38 -0400596 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
Florian Fainellib73adef2015-02-24 13:15:33 -0800597
598 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
599 * so allow it to be in BR_STATE_FORWARDING to be kept functional
600 */
Vivien Didelot17d78022017-05-19 17:00:38 -0400601 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
Florian Fainellib73adef2015-02-24 13:15:33 -0800602}
603
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700604static int dsa_slave_port_attr_get(struct net_device *dev,
605 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800606{
607 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500608 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800609
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700610 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200611 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700612 attr->u.ppid.id_len = sizeof(ds->index);
613 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700614 break;
615 default:
616 return -EOPNOTSUPP;
617 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800618
619 return 0;
620}
621
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700622static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
623 struct sk_buff *skb)
624{
625#ifdef CONFIG_NET_POLL_CONTROLLER
626 if (p->netpoll)
627 netpoll_send_skb(p->netpoll, skb);
628#else
629 BUG();
630#endif
631 return NETDEV_TX_OK;
632}
633
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700634static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
635{
636 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700637 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700638
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700639 dev->stats.tx_packets++;
640 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700641
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700642 /* Transmit function may have to reallocate the original SKB */
643 nskb = p->xmit(skb, dev);
644 if (!nskb)
645 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700646
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700647 /* SKB for netpoll still need to be mangled with the protocol-specific
648 * tag to be successfully transmitted
649 */
650 if (unlikely(netpoll_tx_running(dev)))
651 return dsa_netpoll_send_skb(p, nskb);
652
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700653 /* Queue the SKB for transmission on the parent interface, but
654 * do not modify its EtherType
655 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500656 nskb->dev = p->dp->ds->dst->master_netdev;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700657 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700658
659 return NETDEV_TX_OK;
660}
661
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000662/* ethtool operations *******************************************************/
663static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200664dsa_slave_get_link_ksettings(struct net_device *dev,
665 struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000666{
667 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellie69e4622017-02-06 15:55:23 -0800668 int err = -EOPNOTSUPP;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000669
Florian Fainellie69e4622017-02-06 15:55:23 -0800670 if (p->phy != NULL)
671 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000672
673 return err;
674}
675
676static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200677dsa_slave_set_link_ksettings(struct net_device *dev,
678 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000679{
680 struct dsa_slave_priv *p = netdev_priv(dev);
681
682 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200683 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000684
685 return -EOPNOTSUPP;
686}
687
688static void dsa_slave_get_drvinfo(struct net_device *dev,
689 struct ethtool_drvinfo *drvinfo)
690{
Jiri Pirko7826d432013-01-06 00:44:26 +0000691 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000692 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
693 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000694}
695
Guenter Roeck3d762a02014-10-29 10:45:04 -0700696static int dsa_slave_get_regs_len(struct net_device *dev)
697{
698 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500699 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700700
Vivien Didelot9d490b42016-08-23 12:38:56 -0400701 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500702 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700703
704 return -EOPNOTSUPP;
705}
706
707static void
708dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
709{
710 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500711 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700712
Vivien Didelot9d490b42016-08-23 12:38:56 -0400713 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500714 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700715}
716
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000717static int dsa_slave_nway_reset(struct net_device *dev)
718{
719 struct dsa_slave_priv *p = netdev_priv(dev);
720
721 if (p->phy != NULL)
722 return genphy_restart_aneg(p->phy);
723
724 return -EOPNOTSUPP;
725}
726
727static u32 dsa_slave_get_link(struct net_device *dev)
728{
729 struct dsa_slave_priv *p = netdev_priv(dev);
730
731 if (p->phy != NULL) {
732 genphy_update_link(p->phy);
733 return p->phy->link;
734 }
735
736 return -EOPNOTSUPP;
737}
738
Guenter Roeck6793abb2014-10-29 10:45:01 -0700739static int dsa_slave_get_eeprom_len(struct net_device *dev)
740{
741 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500742 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700743
Andrew Lunn0e576042016-06-04 21:16:52 +0200744 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200745 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700746
Vivien Didelot9d490b42016-08-23 12:38:56 -0400747 if (ds->ops->get_eeprom_len)
748 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700749
750 return 0;
751}
752
753static int dsa_slave_get_eeprom(struct net_device *dev,
754 struct ethtool_eeprom *eeprom, u8 *data)
755{
756 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500757 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700758
Vivien Didelot9d490b42016-08-23 12:38:56 -0400759 if (ds->ops->get_eeprom)
760 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700761
762 return -EOPNOTSUPP;
763}
764
765static int dsa_slave_set_eeprom(struct net_device *dev,
766 struct ethtool_eeprom *eeprom, u8 *data)
767{
768 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500769 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700770
Vivien Didelot9d490b42016-08-23 12:38:56 -0400771 if (ds->ops->set_eeprom)
772 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700773
774 return -EOPNOTSUPP;
775}
776
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000777static void dsa_slave_get_strings(struct net_device *dev,
778 uint32_t stringset, uint8_t *data)
779{
780 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500781 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000782
783 if (stringset == ETH_SS_STATS) {
784 int len = ETH_GSTRING_LEN;
785
786 strncpy(data, "tx_packets", len);
787 strncpy(data + len, "tx_bytes", len);
788 strncpy(data + 2 * len, "rx_packets", len);
789 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400790 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500791 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000792 }
793}
794
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700795static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
796 struct ethtool_stats *stats,
797 uint64_t *data)
798{
799 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400800 struct dsa_switch *ds = dst->cpu_dp->ds;
801 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700802 int count = 0;
803
804 if (dst->master_ethtool_ops.get_sset_count) {
805 count = dst->master_ethtool_ops.get_sset_count(dev,
806 ETH_SS_STATS);
807 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
808 }
809
Vivien Didelot9d490b42016-08-23 12:38:56 -0400810 if (ds->ops->get_ethtool_stats)
811 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700812}
813
814static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
815{
816 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400817 struct dsa_switch *ds = dst->cpu_dp->ds;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700818 int count = 0;
819
820 if (dst->master_ethtool_ops.get_sset_count)
821 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
822
Vivien Didelot9d490b42016-08-23 12:38:56 -0400823 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
824 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700825
826 return count;
827}
828
829static void dsa_cpu_port_get_strings(struct net_device *dev,
830 uint32_t stringset, uint8_t *data)
831{
832 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400833 struct dsa_switch *ds = dst->cpu_dp->ds;
834 s8 cpu_port = dst->cpu_dp->index;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700835 int len = ETH_GSTRING_LEN;
836 int mcount = 0, count;
837 unsigned int i;
838 uint8_t pfx[4];
839 uint8_t *ndata;
840
841 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
842 /* We do not want to be NULL-terminated, since this is a prefix */
843 pfx[sizeof(pfx) - 1] = '_';
844
845 if (dst->master_ethtool_ops.get_sset_count) {
846 mcount = dst->master_ethtool_ops.get_sset_count(dev,
847 ETH_SS_STATS);
848 dst->master_ethtool_ops.get_strings(dev, stringset, data);
849 }
850
Vivien Didelot9d490b42016-08-23 12:38:56 -0400851 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700852 ndata = data + mcount * len;
853 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
854 * the output after to prepend our CPU port prefix we
855 * constructed earlier
856 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400857 ds->ops->get_strings(ds, cpu_port, ndata);
858 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700859 for (i = 0; i < count; i++) {
860 memmove(ndata + (i * len + sizeof(pfx)),
861 ndata + i * len, len - sizeof(pfx));
862 memcpy(ndata + i * len, pfx, sizeof(pfx));
863 }
864 }
865}
866
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000867static void dsa_slave_get_ethtool_stats(struct net_device *dev,
868 struct ethtool_stats *stats,
869 uint64_t *data)
870{
871 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500872 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000873
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400874 data[0] = dev->stats.tx_packets;
875 data[1] = dev->stats.tx_bytes;
876 data[2] = dev->stats.rx_packets;
877 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400878 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500879 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000880}
881
882static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
883{
884 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500885 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000886
887 if (sset == ETH_SS_STATS) {
888 int count;
889
890 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400891 if (ds->ops->get_sset_count)
892 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000893
894 return count;
895 }
896
897 return -EOPNOTSUPP;
898}
899
Florian Fainelli19e57c42014-09-18 17:31:24 -0700900static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
901{
902 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500903 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700904
Vivien Didelot9d490b42016-08-23 12:38:56 -0400905 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500906 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700907}
908
909static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
910{
911 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500912 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700913 int ret = -EOPNOTSUPP;
914
Vivien Didelot9d490b42016-08-23 12:38:56 -0400915 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500916 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700917
918 return ret;
919}
920
Florian Fainelli79052882014-09-24 17:05:21 -0700921static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
922{
923 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500924 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700925 int ret;
926
Vivien Didelot9d490b42016-08-23 12:38:56 -0400927 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700928 return -EOPNOTSUPP;
929
Vivien Didelotafdcf152017-01-27 15:29:39 -0500930 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700931 if (ret)
932 return ret;
933
934 if (p->phy)
935 ret = phy_ethtool_set_eee(p->phy, e);
936
937 return ret;
938}
939
940static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
941{
942 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500943 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700944 int ret;
945
Vivien Didelot9d490b42016-08-23 12:38:56 -0400946 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700947 return -EOPNOTSUPP;
948
Vivien Didelotafdcf152017-01-27 15:29:39 -0500949 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700950 if (ret)
951 return ret;
952
953 if (p->phy)
954 ret = phy_ethtool_get_eee(p->phy, e);
955
956 return ret;
957}
958
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700959#ifdef CONFIG_NET_POLL_CONTROLLER
960static int dsa_slave_netpoll_setup(struct net_device *dev,
961 struct netpoll_info *ni)
962{
963 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500964 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700965 struct net_device *master = ds->dst->master_netdev;
966 struct netpoll *netpoll;
967 int err = 0;
968
969 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
970 if (!netpoll)
971 return -ENOMEM;
972
973 err = __netpoll_setup(netpoll, master);
974 if (err) {
975 kfree(netpoll);
976 goto out;
977 }
978
979 p->netpoll = netpoll;
980out:
981 return err;
982}
983
984static void dsa_slave_netpoll_cleanup(struct net_device *dev)
985{
986 struct dsa_slave_priv *p = netdev_priv(dev);
987 struct netpoll *netpoll = p->netpoll;
988
989 if (!netpoll)
990 return;
991
992 p->netpoll = NULL;
993
994 __netpoll_free_async(netpoll);
995}
996
997static void dsa_slave_poll_controller(struct net_device *dev)
998{
999}
1000#endif
1001
Florian Fainelli44bb7652017-01-10 12:32:36 -08001002static int dsa_slave_get_phys_port_name(struct net_device *dev,
1003 char *name, size_t len)
1004{
1005 struct dsa_slave_priv *p = netdev_priv(dev);
1006
Vivien Didelotafdcf152017-01-27 15:29:39 -05001007 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -08001008 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -08001009
1010 return 0;
1011}
1012
Florian Fainellif50f2122017-01-30 12:41:40 -08001013static struct dsa_mall_tc_entry *
1014dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1015 unsigned long cookie)
1016{
1017 struct dsa_mall_tc_entry *mall_tc_entry;
1018
1019 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1020 if (mall_tc_entry->cookie == cookie)
1021 return mall_tc_entry;
1022
1023 return NULL;
1024}
1025
1026static int dsa_slave_add_cls_matchall(struct net_device *dev,
1027 __be16 protocol,
1028 struct tc_cls_matchall_offload *cls,
1029 bool ingress)
1030{
1031 struct dsa_slave_priv *p = netdev_priv(dev);
1032 struct dsa_mall_tc_entry *mall_tc_entry;
1033 struct dsa_switch *ds = p->dp->ds;
1034 struct net *net = dev_net(dev);
1035 struct dsa_slave_priv *to_p;
1036 struct net_device *to_dev;
1037 const struct tc_action *a;
1038 int err = -EOPNOTSUPP;
1039 LIST_HEAD(actions);
1040 int ifindex;
1041
1042 if (!ds->ops->port_mirror_add)
1043 return err;
1044
1045 if (!tc_single_action(cls->exts))
1046 return err;
1047
1048 tcf_exts_to_list(cls->exts, &actions);
1049 a = list_first_entry(&actions, struct tc_action, list);
1050
1051 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1052 struct dsa_mall_mirror_tc_entry *mirror;
1053
1054 ifindex = tcf_mirred_ifindex(a);
1055 to_dev = __dev_get_by_index(net, ifindex);
1056 if (!to_dev)
1057 return -EINVAL;
1058
1059 if (!dsa_slave_dev_check(to_dev))
1060 return -EOPNOTSUPP;
1061
1062 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1063 if (!mall_tc_entry)
1064 return -ENOMEM;
1065
1066 mall_tc_entry->cookie = cls->cookie;
1067 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1068 mirror = &mall_tc_entry->mirror;
1069
1070 to_p = netdev_priv(to_dev);
1071
1072 mirror->to_local_port = to_p->dp->index;
1073 mirror->ingress = ingress;
1074
1075 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1076 ingress);
1077 if (err) {
1078 kfree(mall_tc_entry);
1079 return err;
1080 }
1081
1082 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1083 }
1084
1085 return 0;
1086}
1087
1088static void dsa_slave_del_cls_matchall(struct net_device *dev,
1089 struct tc_cls_matchall_offload *cls)
1090{
1091 struct dsa_slave_priv *p = netdev_priv(dev);
1092 struct dsa_mall_tc_entry *mall_tc_entry;
1093 struct dsa_switch *ds = p->dp->ds;
1094
1095 if (!ds->ops->port_mirror_del)
1096 return;
1097
1098 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1099 if (!mall_tc_entry)
1100 return;
1101
1102 list_del(&mall_tc_entry->list);
1103
1104 switch (mall_tc_entry->type) {
1105 case DSA_PORT_MALL_MIRROR:
1106 ds->ops->port_mirror_del(ds, p->dp->index,
1107 &mall_tc_entry->mirror);
1108 break;
1109 default:
1110 WARN_ON(1);
1111 }
1112
1113 kfree(mall_tc_entry);
1114}
1115
1116static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1117 __be16 protocol, struct tc_to_netdev *tc)
1118{
1119 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1120 int ret = -EOPNOTSUPP;
1121
1122 switch (tc->type) {
1123 case TC_SETUP_MATCHALL:
1124 switch (tc->cls_mall->command) {
1125 case TC_CLSMATCHALL_REPLACE:
1126 return dsa_slave_add_cls_matchall(dev, protocol,
1127 tc->cls_mall,
1128 ingress);
1129 case TC_CLSMATCHALL_DESTROY:
1130 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1131 return 0;
1132 }
1133 default:
1134 break;
1135 }
1136
1137 return ret;
1138}
1139
Florian Fainelliaf421922016-06-07 16:32:41 -07001140void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1141{
1142 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1143 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1144 ops->get_strings = dsa_cpu_port_get_strings;
1145}
1146
Florian Fainellibf9f2642017-01-30 09:48:40 -08001147static int dsa_slave_get_rxnfc(struct net_device *dev,
1148 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1149{
1150 struct dsa_slave_priv *p = netdev_priv(dev);
1151 struct dsa_switch *ds = p->dp->ds;
1152
1153 if (!ds->ops->get_rxnfc)
1154 return -EOPNOTSUPP;
1155
1156 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1157}
1158
1159static int dsa_slave_set_rxnfc(struct net_device *dev,
1160 struct ethtool_rxnfc *nfc)
1161{
1162 struct dsa_slave_priv *p = netdev_priv(dev);
1163 struct dsa_switch *ds = p->dp->ds;
1164
1165 if (!ds->ops->set_rxnfc)
1166 return -EOPNOTSUPP;
1167
1168 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1169}
1170
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001171static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001172 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001173 .get_regs_len = dsa_slave_get_regs_len,
1174 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001175 .nway_reset = dsa_slave_nway_reset,
1176 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001177 .get_eeprom_len = dsa_slave_get_eeprom_len,
1178 .get_eeprom = dsa_slave_get_eeprom,
1179 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001180 .get_strings = dsa_slave_get_strings,
1181 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1182 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001183 .set_wol = dsa_slave_set_wol,
1184 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001185 .set_eee = dsa_slave_set_eee,
1186 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +02001187 .get_link_ksettings = dsa_slave_get_link_ksettings,
1188 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001189 .get_rxnfc = dsa_slave_get_rxnfc,
1190 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001191};
1192
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001193static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001194 .ndo_open = dsa_slave_open,
1195 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001196 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001197 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1198 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001199 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001200 .ndo_fdb_add = switchdev_port_fdb_add,
1201 .ndo_fdb_del = switchdev_port_fdb_del,
1202 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001203 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001204 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001205#ifdef CONFIG_NET_POLL_CONTROLLER
1206 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1207 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1208 .ndo_poll_controller = dsa_slave_poll_controller,
1209#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001210 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1211 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1212 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -08001213 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001214 .ndo_setup_tc = dsa_slave_setup_tc,
Scott Feldman98237d42015-03-15 21:07:15 -07001215};
1216
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001217static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001218 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001219 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001220 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1221 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1222 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001223};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001224
Florian Fainellif37db852015-09-23 18:19:58 -07001225static struct device_type dsa_type = {
1226 .name = "dsa",
1227};
1228
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001229static void dsa_slave_adjust_link(struct net_device *dev)
1230{
1231 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001232 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001233 unsigned int status_changed = 0;
1234
1235 if (p->old_link != p->phy->link) {
1236 status_changed = 1;
1237 p->old_link = p->phy->link;
1238 }
1239
1240 if (p->old_duplex != p->phy->duplex) {
1241 status_changed = 1;
1242 p->old_duplex = p->phy->duplex;
1243 }
1244
1245 if (p->old_pause != p->phy->pause) {
1246 status_changed = 1;
1247 p->old_pause = p->phy->pause;
1248 }
1249
Vivien Didelot9d490b42016-08-23 12:38:56 -04001250 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001251 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001252
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001253 if (status_changed)
1254 phy_print_status(p->phy);
1255}
1256
Florian Fainellice31b312014-08-27 17:04:54 -07001257static int dsa_slave_fixed_link_update(struct net_device *dev,
1258 struct fixed_phy_status *status)
1259{
Andrew Lunnb71be352016-03-12 00:01:37 +01001260 struct dsa_slave_priv *p;
1261 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001262
Andrew Lunnb71be352016-03-12 00:01:37 +01001263 if (dev) {
1264 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001265 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001266 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001267 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001268 }
Florian Fainellice31b312014-08-27 17:04:54 -07001269
1270 return 0;
1271}
1272
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001273/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001274static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001275 struct net_device *slave_dev,
1276 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001277{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001278 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001279
Andrew Lunn7f854422016-01-06 20:11:18 +01001280 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001281 if (!p->phy) {
1282 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001283 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001284 }
Florian Fainellic305c162015-03-10 16:57:12 -07001285
1286 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001287 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1288 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001289 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1290 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001291}
1292
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001293static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001294 struct net_device *slave_dev)
1295{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001296 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001297 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001298 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001299 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001300 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001301
Vivien Didelotafdcf152017-01-27 15:29:39 -05001302 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001303 mode = of_get_phy_mode(port_dn);
1304 if (mode < 0)
1305 mode = PHY_INTERFACE_MODE_NA;
1306 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001307
1308 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001309 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001310 /* In the case of a fixed PHY, the DT node associated
1311 * to the fixed PHY is the Port DT node
1312 */
1313 ret = of_phy_register_fixed_link(port_dn);
1314 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001315 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001316 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001317 }
Florian Fainellice31b312014-08-27 17:04:54 -07001318 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001319 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001320 }
1321
Vivien Didelot9d490b42016-08-23 12:38:56 -04001322 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001323 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001324
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001325 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001326 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1327
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001328 /* If this PHY address is part of phys_mii_mask, which means
1329 * that we need to divert reads and writes to/from it, then we
1330 * want to bind this device using the slave MII bus created by
1331 * DSA to make that happen.
1332 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001333 if (!phy_is_fixed && phy_id >= 0 &&
1334 (ds->phys_mii_mask & (1 << phy_id))) {
1335 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1336 if (ret) {
1337 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001338 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001339 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001340 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001341 } else {
1342 p->phy = of_phy_connect(slave_dev, phy_dn,
1343 dsa_slave_adjust_link,
1344 phy_flags,
1345 p->phy_interface);
1346 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001347
1348 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001349 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001350
Florian Fainellice31b312014-08-27 17:04:54 -07001351 if (p->phy && phy_is_fixed)
1352 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1353
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001354 /* We could not connect to a designated PHY, so use the switch internal
1355 * MDIO bus instead
1356 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001357 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001358 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001359 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001360 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1361 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001362 if (phy_is_fixed)
1363 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001364 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001365 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001366 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001367
Andrew Lunn22209432016-01-06 20:11:13 +01001368 phy_attached_info(p->phy);
1369
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001370 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001371}
1372
Andrew Lunn448b4482015-05-06 01:09:56 +02001373static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1374static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1375 struct netdev_queue *txq,
1376 void *_unused)
1377{
1378 lockdep_set_class(&txq->_xmit_lock,
1379 &dsa_slave_netdev_xmit_lock_key);
1380}
1381
Florian Fainelli24462542014-09-18 17:31:22 -07001382int dsa_slave_suspend(struct net_device *slave_dev)
1383{
1384 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1385
Florian Fainellif154be22017-01-25 09:10:41 -08001386 netif_device_detach(slave_dev);
1387
Florian Fainelli24462542014-09-18 17:31:22 -07001388 if (p->phy) {
1389 phy_stop(p->phy);
1390 p->old_pause = -1;
1391 p->old_link = -1;
1392 p->old_duplex = -1;
1393 phy_suspend(p->phy);
1394 }
1395
1396 return 0;
1397}
1398
1399int dsa_slave_resume(struct net_device *slave_dev)
1400{
1401 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1402
1403 netif_device_attach(slave_dev);
1404
1405 if (p->phy) {
1406 phy_resume(p->phy);
1407 phy_start(p->phy);
1408 }
1409
1410 return 0;
1411}
1412
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001413int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001414 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001415{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001416 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001417 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001418 struct net_device *slave_dev;
1419 struct dsa_slave_priv *p;
1420 int ret;
1421
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001422 master = ds->dst->master_netdev;
1423 if (ds->master_netdev)
1424 master = ds->master_netdev;
1425
Tom Gundersenc835a672014-07-14 16:37:24 +02001426 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1427 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001428 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001429 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001430
Florian Fainellif50f2122017-01-30 12:41:40 -08001431 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1432 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001433 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001434 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001435 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001436 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001437 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001438 slave_dev->min_mtu = 0;
1439 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001440 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001441
Andrew Lunn448b4482015-05-06 01:09:56 +02001442 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1443 NULL);
1444
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001445 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001446 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001447 slave_dev->vlan_features = master->vlan_features;
1448
1449 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001450 p->dp = &ds->ports[port];
Florian Fainellif50f2122017-01-30 12:41:40 -08001451 INIT_LIST_HEAD(&p->mall_tc_list);
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001452 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001453
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001454 p->old_pause = -1;
1455 p->old_link = -1;
1456 p->old_duplex = -1;
1457
Andrew Lunnc8b09802016-06-04 21:16:57 +02001458 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001459 ret = register_netdev(slave_dev);
1460 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001461 netdev_err(master, "error %d registering interface %s\n",
1462 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001463 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001464 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001465 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001466 }
1467
1468 netif_carrier_off(slave_dev);
1469
Andrew Lunn0071f562016-01-06 20:11:20 +01001470 ret = dsa_slave_phy_setup(p, slave_dev);
1471 if (ret) {
1472 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001473 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001474 free_netdev(slave_dev);
1475 return ret;
1476 }
1477
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001478 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001479}
Florian Fainellib73adef2015-02-24 13:15:33 -08001480
Neil Armstrongcda5c152015-12-07 13:57:35 +01001481void dsa_slave_destroy(struct net_device *slave_dev)
1482{
1483 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001484 struct device_node *port_dn;
1485
Vivien Didelotafdcf152017-01-27 15:29:39 -05001486 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001487
1488 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001489 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001490 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001491
1492 if (of_phy_is_fixed_link(port_dn))
1493 of_phy_deregister_fixed_link(port_dn);
1494 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001495 unregister_netdev(slave_dev);
1496 free_netdev(slave_dev);
1497}
1498
Florian Fainellib73adef2015-02-24 13:15:33 -08001499static bool dsa_slave_dev_check(struct net_device *dev)
1500{
1501 return dev->netdev_ops == &dsa_slave_netdev_ops;
1502}
1503
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001504static int dsa_slave_changeupper(struct net_device *dev,
1505 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001506{
Vivien Didelot17d78022017-05-19 17:00:38 -04001507 struct dsa_slave_priv *p = netdev_priv(dev);
1508 struct dsa_port *dp = p->dp;
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001509 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001510
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001511 if (netif_is_bridge_master(info->upper_dev)) {
1512 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001513 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001514 err = notifier_from_errno(err);
1515 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001516 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001517 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001518 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001519 }
1520
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001521 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001522}
1523
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001524static int dsa_slave_netdevice_event(struct notifier_block *nb,
1525 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001526{
Vivien Didelot6debb682016-03-13 16:21:34 -04001527 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001528
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001529 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1530 return NOTIFY_DONE;
1531
1532 if (event == NETDEV_CHANGEUPPER)
1533 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001534
Florian Fainellib73adef2015-02-24 13:15:33 -08001535 return NOTIFY_DONE;
1536}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001537
1538static struct notifier_block dsa_slave_nb __read_mostly = {
1539 .notifier_call = dsa_slave_netdevice_event,
1540};
1541
1542int dsa_slave_register_notifier(void)
1543{
1544 return register_netdevice_notifier(&dsa_slave_nb);
1545}
1546
1547void dsa_slave_unregister_notifier(void)
1548{
1549 int err;
1550
1551 err = unregister_netdevice_notifier(&dsa_slave_nb);
1552 if (err)
1553 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1554}