blob: c34872e1febc4b75d1b69b18a8a1189405ca30fa [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>
Scott Feldman98237d42015-03-15 21:07:15 -070021#include <net/switchdev.h>
Florian Fainellif50f2122017-01-30 12:41:40 -080022#include <net/pkt_cls.h>
23#include <net/tc_act/tc_mirred.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080024#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070025#include <linux/netpoll.h>
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 Didelot04d3a4c2017-02-03 13:20:21 -050030static int dsa_slave_notify(struct net_device *dev, unsigned long e, void *v)
31{
32 struct dsa_slave_priv *p = netdev_priv(dev);
33 struct raw_notifier_head *nh = &p->dp->ds->dst->nh;
34 int err;
35
36 err = raw_notifier_call_chain(nh, e, v);
37
38 return notifier_to_errno(err);
39}
40
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000041/* slave mii_bus handling ***************************************************/
42static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
43{
44 struct dsa_switch *ds = bus->priv;
45
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070046 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040047 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000048
49 return 0xffff;
50}
51
52static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
53{
54 struct dsa_switch *ds = bus->priv;
55
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070056 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040057 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000058
59 return 0;
60}
61
62void dsa_slave_mii_bus_init(struct dsa_switch *ds)
63{
64 ds->slave_mii_bus->priv = (void *)ds;
65 ds->slave_mii_bus->name = "dsa slave smi";
66 ds->slave_mii_bus->read = dsa_slave_phy_read;
67 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070068 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
69 ds->dst->tree, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020070 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050071 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072}
73
74
75/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020076static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000077{
78 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000079
Vivien Didelotafdcf152017-01-27 15:29:39 -050080 return p->dp->ds->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000081}
82
Vivien Didelota5e9a022017-01-27 15:29:40 -050083static inline bool dsa_port_is_bridged(struct dsa_port *dp)
Florian Fainellib73adef2015-02-24 13:15:33 -080084{
Vivien Didelota5e9a022017-01-27 15:29:40 -050085 return !!dp->bridge_dev;
Florian Fainellib73adef2015-02-24 13:15:33 -080086}
87
Vivien Didelotc5d35cb2017-02-03 13:20:19 -050088static void dsa_slave_set_state(struct net_device *dev, u8 state)
Vivien Didelot4acfee82016-09-22 16:49:21 -040089{
Vivien Didelotc5d35cb2017-02-03 13:20:19 -050090 struct dsa_slave_priv *p = netdev_priv(dev);
91 struct dsa_port *dp = p->dp;
92 struct dsa_switch *ds = dp->ds;
93 int port = dp->index;
Vivien Didelot732f7942016-09-22 16:49:22 -040094
Vivien Didelot4acfee82016-09-22 16:49:21 -040095 if (ds->ops->port_stp_state_set)
96 ds->ops->port_stp_state_set(ds, port, state);
Vivien Didelot732f7942016-09-22 16:49:22 -040097
98 if (ds->ops->port_fast_age) {
99 /* Fast age FDB entries or flush appropriate forwarding database
100 * for the given port, if we are moving it from Learning or
101 * Forwarding state, to Disabled or Blocking or Listening state.
102 */
103
104 if ((dp->stp_state == BR_STATE_LEARNING ||
105 dp->stp_state == BR_STATE_FORWARDING) &&
106 (state == BR_STATE_DISABLED ||
107 state == BR_STATE_BLOCKING ||
108 state == BR_STATE_LISTENING))
109 ds->ops->port_fast_age(ds, port);
110 }
111
112 dp->stp_state = state;
Vivien Didelot4acfee82016-09-22 16:49:21 -0400113}
114
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000115static int dsa_slave_open(struct net_device *dev)
116{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800117 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500118 struct net_device *master = p->dp->ds->dst->master_netdev;
119 struct dsa_switch *ds = p->dp->ds;
Vivien Didelota5e9a022017-01-27 15:29:40 -0500120 u8 stp_state = dsa_port_is_bridged(p->dp) ?
Florian Fainellib73adef2015-02-24 13:15:33 -0800121 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800122 int err;
123
124 if (!(master->flags & IFF_UP))
125 return -ENETDOWN;
126
Joe Perches8feedbb2012-05-08 18:56:57 +0000127 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000128 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800129 if (err < 0)
130 goto out;
131 }
132
133 if (dev->flags & IFF_ALLMULTI) {
134 err = dev_set_allmulti(master, 1);
135 if (err < 0)
136 goto del_unicast;
137 }
138 if (dev->flags & IFF_PROMISC) {
139 err = dev_set_promiscuity(master, 1);
140 if (err < 0)
141 goto clear_allmulti;
142 }
143
Vivien Didelot9d490b42016-08-23 12:38:56 -0400144 if (ds->ops->port_enable) {
Vivien Didelotafdcf152017-01-27 15:29:39 -0500145 err = ds->ops->port_enable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700146 if (err)
147 goto clear_promisc;
148 }
149
Vivien Didelotc5d35cb2017-02-03 13:20:19 -0500150 dsa_slave_set_state(dev, stp_state);
Florian Fainellib73adef2015-02-24 13:15:33 -0800151
Florian Fainellif7f1de52014-09-24 17:05:17 -0700152 if (p->phy)
153 phy_start(p->phy);
154
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000155 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800156
Florian Fainellib2f2af22014-09-24 17:05:18 -0700157clear_promisc:
158 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300159 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800160clear_allmulti:
161 if (dev->flags & IFF_ALLMULTI)
162 dev_set_allmulti(master, -1);
163del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000164 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000165 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800166out:
167 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000168}
169
170static int dsa_slave_close(struct net_device *dev)
171{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800172 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500173 struct net_device *master = p->dp->ds->dst->master_netdev;
174 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800175
Florian Fainellif7f1de52014-09-24 17:05:17 -0700176 if (p->phy)
177 phy_stop(p->phy);
178
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800179 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000180 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800181 if (dev->flags & IFF_ALLMULTI)
182 dev_set_allmulti(master, -1);
183 if (dev->flags & IFF_PROMISC)
184 dev_set_promiscuity(master, -1);
185
Joe Perches8feedbb2012-05-08 18:56:57 +0000186 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000187 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800188
Vivien Didelot9d490b42016-08-23 12:38:56 -0400189 if (ds->ops->port_disable)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500190 ds->ops->port_disable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700191
Vivien Didelotc5d35cb2017-02-03 13:20:19 -0500192 dsa_slave_set_state(dev, BR_STATE_DISABLED);
Florian Fainellib73adef2015-02-24 13:15:33 -0800193
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000194 return 0;
195}
196
197static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
198{
199 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500200 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000201
202 if (change & IFF_ALLMULTI)
203 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
204 if (change & IFF_PROMISC)
205 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
206}
207
208static void dsa_slave_set_rx_mode(struct net_device *dev)
209{
210 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500211 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000212
213 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000214 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000215}
216
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800217static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800219 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500220 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800221 struct sockaddr *addr = a;
222 int err;
223
224 if (!is_valid_ether_addr(addr->sa_data))
225 return -EADDRNOTAVAIL;
226
227 if (!(dev->flags & IFF_UP))
228 goto out;
229
Joe Perches8feedbb2012-05-08 18:56:57 +0000230 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000231 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800232 if (err < 0)
233 return err;
234 }
235
Joe Perches8feedbb2012-05-08 18:56:57 +0000236 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000237 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800238
239out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800240 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000241
242 return 0;
243}
244
Vivien Didelot11149532015-08-13 12:52:17 -0400245static int dsa_slave_port_vlan_add(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200246 const struct switchdev_obj_port_vlan *vlan,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200247 struct switchdev_trans *trans)
Vivien Didelot11149532015-08-13 12:52:17 -0400248{
Vivien Didelot11149532015-08-13 12:52:17 -0400249 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500250 struct dsa_port *dp = p->dp;
251 struct dsa_switch *ds = dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400252
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200253 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400254 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
Vivien Didelot11149532015-08-13 12:52:17 -0400255 return -EOPNOTSUPP;
256
Vivien Didelotafdcf152017-01-27 15:29:39 -0500257 return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400258 }
259
Vivien Didelotafdcf152017-01-27 15:29:39 -0500260 ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
Vivien Didelot4d5770b2016-04-06 11:55:05 -0400261
Vivien Didelot11149532015-08-13 12:52:17 -0400262 return 0;
263}
264
265static int dsa_slave_port_vlan_del(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200266 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot11149532015-08-13 12:52:17 -0400267{
Vivien Didelot11149532015-08-13 12:52:17 -0400268 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500269 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400270
Vivien Didelot9d490b42016-08-23 12:38:56 -0400271 if (!ds->ops->port_vlan_del)
Vivien Didelot11149532015-08-13 12:52:17 -0400272 return -EOPNOTSUPP;
273
Vivien Didelotafdcf152017-01-27 15:29:39 -0500274 return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
Vivien Didelot11149532015-08-13 12:52:17 -0400275}
276
277static int dsa_slave_port_vlan_dump(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200278 struct switchdev_obj_port_vlan *vlan,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200279 switchdev_obj_dump_cb_t *cb)
Vivien Didelot11149532015-08-13 12:52:17 -0400280{
Vivien Didelot11149532015-08-13 12:52:17 -0400281 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500282 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400283
Vivien Didelot9d490b42016-08-23 12:38:56 -0400284 if (ds->ops->port_vlan_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500285 return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
Vivien Didelot65aebfc2016-02-23 12:13:54 -0500286
Vivien Didelot477b1842016-02-23 12:13:56 -0500287 return -EOPNOTSUPP;
Vivien Didelot11149532015-08-13 12:52:17 -0400288}
289
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400290static int dsa_slave_port_fdb_add(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200291 const struct switchdev_obj_port_fdb *fdb,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200292 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700293{
294 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500295 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot146a3202015-10-08 11:35:12 -0400296
Vivien Didelot8497aa62016-04-06 11:55:04 -0400297 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400298 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
Vivien Didelot8497aa62016-04-06 11:55:04 -0400299 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700300
Vivien Didelotafdcf152017-01-27 15:29:39 -0500301 return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400302 }
David S. Millercdf09692015-08-11 12:00:37 -0700303
Vivien Didelotafdcf152017-01-27 15:29:39 -0500304 ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400305
306 return 0;
David S. Millercdf09692015-08-11 12:00:37 -0700307}
308
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400309static int dsa_slave_port_fdb_del(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200310 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -0700311{
312 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500313 struct dsa_switch *ds = p->dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700314 int ret = -EOPNOTSUPP;
315
Vivien Didelot9d490b42016-08-23 12:38:56 -0400316 if (ds->ops->port_fdb_del)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500317 ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700318
319 return ret;
320}
321
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400322static int dsa_slave_port_fdb_dump(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200323 struct switchdev_obj_port_fdb *fdb,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200324 switchdev_obj_dump_cb_t *cb)
David S. Millercdf09692015-08-11 12:00:37 -0700325{
326 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500327 struct dsa_switch *ds = p->dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700328
Vivien Didelot9d490b42016-08-23 12:38:56 -0400329 if (ds->ops->port_fdb_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500330 return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb);
Vivien Didelotea70ba92015-10-22 09:34:38 -0400331
Vivien Didelot1a49a2f2015-10-22 09:34:43 -0400332 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700333}
334
Vivien Didelot8df30252016-08-31 11:50:03 -0400335static int dsa_slave_port_mdb_add(struct net_device *dev,
336 const struct switchdev_obj_port_mdb *mdb,
337 struct switchdev_trans *trans)
338{
339 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500340 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400341
342 if (switchdev_trans_ph_prepare(trans)) {
343 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
344 return -EOPNOTSUPP;
345
Vivien Didelotafdcf152017-01-27 15:29:39 -0500346 return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400347 }
348
Vivien Didelotafdcf152017-01-27 15:29:39 -0500349 ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400350
351 return 0;
352}
353
354static int dsa_slave_port_mdb_del(struct net_device *dev,
355 const struct switchdev_obj_port_mdb *mdb)
356{
357 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500358 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400359
360 if (ds->ops->port_mdb_del)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500361 return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400362
363 return -EOPNOTSUPP;
364}
365
366static int dsa_slave_port_mdb_dump(struct net_device *dev,
367 struct switchdev_obj_port_mdb *mdb,
368 switchdev_obj_dump_cb_t *cb)
369{
370 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500371 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400372
373 if (ds->ops->port_mdb_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500374 return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400375
376 return -EOPNOTSUPP;
377}
378
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000379static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
380{
381 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000382
383 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000384 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000385
386 return -EOPNOTSUPP;
387}
388
Vivien Didelot43c44a92016-04-06 11:55:03 -0400389static int dsa_slave_stp_state_set(struct net_device *dev,
390 const struct switchdev_attr *attr,
391 struct switchdev_trans *trans)
Florian Fainellib73adef2015-02-24 13:15:33 -0800392{
393 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500394 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800395
Vivien Didelot43c44a92016-04-06 11:55:03 -0400396 if (switchdev_trans_ph_prepare(trans))
Vivien Didelot9d490b42016-08-23 12:38:56 -0400397 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
Florian Fainellib73adef2015-02-24 13:15:33 -0800398
Vivien Didelotc5d35cb2017-02-03 13:20:19 -0500399 dsa_slave_set_state(dev, attr->u.stp_state);
Vivien Didelot43c44a92016-04-06 11:55:03 -0400400
401 return 0;
Florian Fainellib73adef2015-02-24 13:15:33 -0800402}
403
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500404static int dsa_slave_vlan_filtering(struct net_device *dev,
405 const struct switchdev_attr *attr,
406 struct switchdev_trans *trans)
407{
408 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500409 struct dsa_switch *ds = p->dp->ds;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500410
411 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
412 if (switchdev_trans_ph_prepare(trans))
413 return 0;
414
Vivien Didelot9d490b42016-08-23 12:38:56 -0400415 if (ds->ops->port_vlan_filtering)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500416 return ds->ops->port_vlan_filtering(ds, p->dp->index,
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500417 attr->u.vlan_filtering);
418
419 return 0;
420}
421
Vivien Didelot34a79f62016-07-18 20:45:38 -0400422static int dsa_fastest_ageing_time(struct dsa_switch *ds,
423 unsigned int ageing_time)
424{
425 int i;
426
Vivien Didelot26895e22017-01-27 15:29:37 -0500427 for (i = 0; i < ds->num_ports; ++i) {
Vivien Didelot34a79f62016-07-18 20:45:38 -0400428 struct dsa_port *dp = &ds->ports[i];
429
430 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
431 ageing_time = dp->ageing_time;
432 }
433
434 return ageing_time;
435}
436
437static int dsa_slave_ageing_time(struct net_device *dev,
438 const struct switchdev_attr *attr,
439 struct switchdev_trans *trans)
440{
441 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500442 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400443 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
444 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
445
446 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
447 if (switchdev_trans_ph_prepare(trans))
448 return 0;
449
450 /* Keep the fastest ageing time in case of multiple bridges */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500451 p->dp->ageing_time = ageing_time;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400452 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
453
Vivien Didelot9d490b42016-08-23 12:38:56 -0400454 if (ds->ops->set_ageing_time)
455 return ds->ops->set_ageing_time(ds, ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400456
457 return 0;
458}
459
Scott Feldman35636062015-05-10 09:47:51 -0700460static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200461 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200462 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700463{
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400464 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700465
466 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200467 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelot43c44a92016-04-06 11:55:03 -0400468 ret = dsa_slave_stp_state_set(dev, attr, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700469 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500470 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
471 ret = dsa_slave_vlan_filtering(dev, attr, trans);
472 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400473 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
474 ret = dsa_slave_ageing_time(dev, attr, trans);
475 break;
Scott Feldman35636062015-05-10 09:47:51 -0700476 default:
477 ret = -EOPNOTSUPP;
478 break;
479 }
480
481 return ret;
482}
483
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400484static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200485 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200486 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400487{
488 int err;
489
490 /* For the prepare phase, ensure the full set of changes is feasable in
491 * one go in order to signal a failure properly. If an operation is not
492 * supported, return -EOPNOTSUPP.
493 */
494
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200495 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200496 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200497 err = dsa_slave_port_fdb_add(dev,
498 SWITCHDEV_OBJ_PORT_FDB(obj),
499 trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400500 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400501 case SWITCHDEV_OBJ_ID_PORT_MDB:
502 err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
503 trans);
504 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200505 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200506 err = dsa_slave_port_vlan_add(dev,
507 SWITCHDEV_OBJ_PORT_VLAN(obj),
508 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400509 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400510 default:
511 err = -EOPNOTSUPP;
512 break;
513 }
514
515 return err;
516}
517
518static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200519 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400520{
521 int err;
522
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200523 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200524 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200525 err = dsa_slave_port_fdb_del(dev,
526 SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400527 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400528 case SWITCHDEV_OBJ_ID_PORT_MDB:
529 err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
530 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200531 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200532 err = dsa_slave_port_vlan_del(dev,
533 SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400534 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400535 default:
536 err = -EOPNOTSUPP;
537 break;
538 }
539
540 return err;
541}
542
543static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200544 struct switchdev_obj *obj,
545 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400546{
547 int err;
548
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200549 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200550 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200551 err = dsa_slave_port_fdb_dump(dev,
552 SWITCHDEV_OBJ_PORT_FDB(obj),
553 cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400554 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400555 case SWITCHDEV_OBJ_ID_PORT_MDB:
556 err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
557 cb);
558 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200559 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200560 err = dsa_slave_port_vlan_dump(dev,
561 SWITCHDEV_OBJ_PORT_VLAN(obj),
562 cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400563 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400564 default:
565 err = -EOPNOTSUPP;
566 break;
567 }
568
569 return err;
570}
571
Florian Fainellib73adef2015-02-24 13:15:33 -0800572static int dsa_slave_bridge_port_join(struct net_device *dev,
573 struct net_device *br)
574{
575 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500576 struct dsa_notifier_bridge_info info = {
577 .sw_index = p->dp->ds->index,
578 .port = p->dp->index,
579 .br = br,
580 };
581 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800582
Vivien Didelot9c265422017-02-03 13:20:18 -0500583 /* Here the port is already bridged. Reflect the current configuration
584 * so that drivers can program their chips accordingly.
585 */
Vivien Didelota5e9a022017-01-27 15:29:40 -0500586 p->dp->bridge_dev = br;
Florian Fainellib73adef2015-02-24 13:15:33 -0800587
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500588 err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_JOIN, &info);
Florian Fainellib73adef2015-02-24 13:15:33 -0800589
Vivien Didelot9c265422017-02-03 13:20:18 -0500590 /* The bridging is rolled back on error */
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500591 if (err)
Vivien Didelot9c265422017-02-03 13:20:18 -0500592 p->dp->bridge_dev = NULL;
Vivien Didelot9c265422017-02-03 13:20:18 -0500593
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500594 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800595}
596
Vivien Didelotf123f2f2017-01-27 15:29:41 -0500597static void dsa_slave_bridge_port_leave(struct net_device *dev,
598 struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800599{
600 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500601 struct dsa_notifier_bridge_info info = {
602 .sw_index = p->dp->ds->index,
603 .port = p->dp->index,
604 .br = br,
605 };
606 int err;
Florian Fainellib73adef2015-02-24 13:15:33 -0800607
Vivien Didelot9c265422017-02-03 13:20:18 -0500608 /* Here the port is already unbridged. Reflect the current configuration
609 * so that drivers can program their chips accordingly.
610 */
Vivien Didelotf123f2f2017-01-27 15:29:41 -0500611 p->dp->bridge_dev = NULL;
Florian Fainellib73adef2015-02-24 13:15:33 -0800612
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500613 err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
614 if (err)
615 netdev_err(dev, "failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
Florian Fainellib73adef2015-02-24 13:15:33 -0800616
617 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
618 * so allow it to be in BR_STATE_FORWARDING to be kept functional
619 */
Vivien Didelotc5d35cb2017-02-03 13:20:19 -0500620 dsa_slave_set_state(dev, BR_STATE_FORWARDING);
Florian Fainellib73adef2015-02-24 13:15:33 -0800621}
622
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700623static int dsa_slave_port_attr_get(struct net_device *dev,
624 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800625{
626 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500627 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800628
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700629 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200630 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700631 attr->u.ppid.id_len = sizeof(ds->index);
632 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700633 break;
634 default:
635 return -EOPNOTSUPP;
636 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800637
638 return 0;
639}
640
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700641static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
642 struct sk_buff *skb)
643{
644#ifdef CONFIG_NET_POLL_CONTROLLER
645 if (p->netpoll)
646 netpoll_send_skb(p->netpoll, skb);
647#else
648 BUG();
649#endif
650 return NETDEV_TX_OK;
651}
652
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700653static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
654{
655 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700656 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700657
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700658 dev->stats.tx_packets++;
659 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700660
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700661 /* Transmit function may have to reallocate the original SKB */
662 nskb = p->xmit(skb, dev);
663 if (!nskb)
664 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700665
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700666 /* SKB for netpoll still need to be mangled with the protocol-specific
667 * tag to be successfully transmitted
668 */
669 if (unlikely(netpoll_tx_running(dev)))
670 return dsa_netpoll_send_skb(p, nskb);
671
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700672 /* Queue the SKB for transmission on the parent interface, but
673 * do not modify its EtherType
674 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500675 nskb->dev = p->dp->ds->dst->master_netdev;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700676 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700677
678 return NETDEV_TX_OK;
679}
680
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000681/* ethtool operations *******************************************************/
682static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200683dsa_slave_get_link_ksettings(struct net_device *dev,
684 struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000685{
686 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellie69e4622017-02-06 15:55:23 -0800687 int err = -EOPNOTSUPP;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000688
Florian Fainellie69e4622017-02-06 15:55:23 -0800689 if (p->phy != NULL)
690 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000691
692 return err;
693}
694
695static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200696dsa_slave_set_link_ksettings(struct net_device *dev,
697 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000698{
699 struct dsa_slave_priv *p = netdev_priv(dev);
700
701 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200702 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000703
704 return -EOPNOTSUPP;
705}
706
707static void dsa_slave_get_drvinfo(struct net_device *dev,
708 struct ethtool_drvinfo *drvinfo)
709{
Jiri Pirko7826d432013-01-06 00:44:26 +0000710 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000711 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
712 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000713}
714
Guenter Roeck3d762a02014-10-29 10:45:04 -0700715static int dsa_slave_get_regs_len(struct net_device *dev)
716{
717 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500718 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700719
Vivien Didelot9d490b42016-08-23 12:38:56 -0400720 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500721 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700722
723 return -EOPNOTSUPP;
724}
725
726static void
727dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
728{
729 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500730 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700731
Vivien Didelot9d490b42016-08-23 12:38:56 -0400732 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500733 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700734}
735
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000736static int dsa_slave_nway_reset(struct net_device *dev)
737{
738 struct dsa_slave_priv *p = netdev_priv(dev);
739
740 if (p->phy != NULL)
741 return genphy_restart_aneg(p->phy);
742
743 return -EOPNOTSUPP;
744}
745
746static u32 dsa_slave_get_link(struct net_device *dev)
747{
748 struct dsa_slave_priv *p = netdev_priv(dev);
749
750 if (p->phy != NULL) {
751 genphy_update_link(p->phy);
752 return p->phy->link;
753 }
754
755 return -EOPNOTSUPP;
756}
757
Guenter Roeck6793abb2014-10-29 10:45:01 -0700758static int dsa_slave_get_eeprom_len(struct net_device *dev)
759{
760 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500761 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700762
Andrew Lunn0e576042016-06-04 21:16:52 +0200763 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200764 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700765
Vivien Didelot9d490b42016-08-23 12:38:56 -0400766 if (ds->ops->get_eeprom_len)
767 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700768
769 return 0;
770}
771
772static int dsa_slave_get_eeprom(struct net_device *dev,
773 struct ethtool_eeprom *eeprom, u8 *data)
774{
775 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500776 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700777
Vivien Didelot9d490b42016-08-23 12:38:56 -0400778 if (ds->ops->get_eeprom)
779 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700780
781 return -EOPNOTSUPP;
782}
783
784static int dsa_slave_set_eeprom(struct net_device *dev,
785 struct ethtool_eeprom *eeprom, u8 *data)
786{
787 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500788 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700789
Vivien Didelot9d490b42016-08-23 12:38:56 -0400790 if (ds->ops->set_eeprom)
791 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700792
793 return -EOPNOTSUPP;
794}
795
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000796static void dsa_slave_get_strings(struct net_device *dev,
797 uint32_t stringset, uint8_t *data)
798{
799 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500800 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000801
802 if (stringset == ETH_SS_STATS) {
803 int len = ETH_GSTRING_LEN;
804
805 strncpy(data, "tx_packets", len);
806 strncpy(data + len, "tx_bytes", len);
807 strncpy(data + 2 * len, "rx_packets", len);
808 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400809 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500810 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000811 }
812}
813
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700814static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
815 struct ethtool_stats *stats,
816 uint64_t *data)
817{
818 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500819 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700820 s8 cpu_port = dst->cpu_port;
821 int count = 0;
822
823 if (dst->master_ethtool_ops.get_sset_count) {
824 count = dst->master_ethtool_ops.get_sset_count(dev,
825 ETH_SS_STATS);
826 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
827 }
828
Vivien Didelot9d490b42016-08-23 12:38:56 -0400829 if (ds->ops->get_ethtool_stats)
830 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700831}
832
833static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
834{
835 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500836 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700837 int count = 0;
838
839 if (dst->master_ethtool_ops.get_sset_count)
840 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
841
Vivien Didelot9d490b42016-08-23 12:38:56 -0400842 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
843 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700844
845 return count;
846}
847
848static void dsa_cpu_port_get_strings(struct net_device *dev,
849 uint32_t stringset, uint8_t *data)
850{
851 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500852 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700853 s8 cpu_port = dst->cpu_port;
854 int len = ETH_GSTRING_LEN;
855 int mcount = 0, count;
856 unsigned int i;
857 uint8_t pfx[4];
858 uint8_t *ndata;
859
860 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
861 /* We do not want to be NULL-terminated, since this is a prefix */
862 pfx[sizeof(pfx) - 1] = '_';
863
864 if (dst->master_ethtool_ops.get_sset_count) {
865 mcount = dst->master_ethtool_ops.get_sset_count(dev,
866 ETH_SS_STATS);
867 dst->master_ethtool_ops.get_strings(dev, stringset, data);
868 }
869
Vivien Didelot9d490b42016-08-23 12:38:56 -0400870 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700871 ndata = data + mcount * len;
872 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
873 * the output after to prepend our CPU port prefix we
874 * constructed earlier
875 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400876 ds->ops->get_strings(ds, cpu_port, ndata);
877 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700878 for (i = 0; i < count; i++) {
879 memmove(ndata + (i * len + sizeof(pfx)),
880 ndata + i * len, len - sizeof(pfx));
881 memcpy(ndata + i * len, pfx, sizeof(pfx));
882 }
883 }
884}
885
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000886static void dsa_slave_get_ethtool_stats(struct net_device *dev,
887 struct ethtool_stats *stats,
888 uint64_t *data)
889{
890 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500891 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000892
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400893 data[0] = dev->stats.tx_packets;
894 data[1] = dev->stats.tx_bytes;
895 data[2] = dev->stats.rx_packets;
896 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400897 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500898 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000899}
900
901static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
902{
903 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500904 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000905
906 if (sset == ETH_SS_STATS) {
907 int count;
908
909 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400910 if (ds->ops->get_sset_count)
911 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000912
913 return count;
914 }
915
916 return -EOPNOTSUPP;
917}
918
Florian Fainelli19e57c42014-09-18 17:31:24 -0700919static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
920{
921 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500922 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700923
Vivien Didelot9d490b42016-08-23 12:38:56 -0400924 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500925 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700926}
927
928static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
929{
930 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500931 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700932 int ret = -EOPNOTSUPP;
933
Vivien Didelot9d490b42016-08-23 12:38:56 -0400934 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500935 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700936
937 return ret;
938}
939
Florian Fainelli79052882014-09-24 17:05:21 -0700940static int dsa_slave_set_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->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700947 return -EOPNOTSUPP;
948
Vivien Didelotafdcf152017-01-27 15:29:39 -0500949 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700950 if (ret)
951 return ret;
952
953 if (p->phy)
954 ret = phy_ethtool_set_eee(p->phy, e);
955
956 return ret;
957}
958
959static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
960{
961 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500962 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700963 int ret;
964
Vivien Didelot9d490b42016-08-23 12:38:56 -0400965 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700966 return -EOPNOTSUPP;
967
Vivien Didelotafdcf152017-01-27 15:29:39 -0500968 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700969 if (ret)
970 return ret;
971
972 if (p->phy)
973 ret = phy_ethtool_get_eee(p->phy, e);
974
975 return ret;
976}
977
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700978#ifdef CONFIG_NET_POLL_CONTROLLER
979static int dsa_slave_netpoll_setup(struct net_device *dev,
980 struct netpoll_info *ni)
981{
982 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500983 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700984 struct net_device *master = ds->dst->master_netdev;
985 struct netpoll *netpoll;
986 int err = 0;
987
988 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
989 if (!netpoll)
990 return -ENOMEM;
991
992 err = __netpoll_setup(netpoll, master);
993 if (err) {
994 kfree(netpoll);
995 goto out;
996 }
997
998 p->netpoll = netpoll;
999out:
1000 return err;
1001}
1002
1003static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1004{
1005 struct dsa_slave_priv *p = netdev_priv(dev);
1006 struct netpoll *netpoll = p->netpoll;
1007
1008 if (!netpoll)
1009 return;
1010
1011 p->netpoll = NULL;
1012
1013 __netpoll_free_async(netpoll);
1014}
1015
1016static void dsa_slave_poll_controller(struct net_device *dev)
1017{
1018}
1019#endif
1020
Florian Fainelli44bb7652017-01-10 12:32:36 -08001021static int dsa_slave_get_phys_port_name(struct net_device *dev,
1022 char *name, size_t len)
1023{
1024 struct dsa_slave_priv *p = netdev_priv(dev);
1025
Vivien Didelotafdcf152017-01-27 15:29:39 -05001026 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -08001027 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -08001028
1029 return 0;
1030}
1031
Florian Fainellif50f2122017-01-30 12:41:40 -08001032static struct dsa_mall_tc_entry *
1033dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1034 unsigned long cookie)
1035{
1036 struct dsa_mall_tc_entry *mall_tc_entry;
1037
1038 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1039 if (mall_tc_entry->cookie == cookie)
1040 return mall_tc_entry;
1041
1042 return NULL;
1043}
1044
1045static int dsa_slave_add_cls_matchall(struct net_device *dev,
1046 __be16 protocol,
1047 struct tc_cls_matchall_offload *cls,
1048 bool ingress)
1049{
1050 struct dsa_slave_priv *p = netdev_priv(dev);
1051 struct dsa_mall_tc_entry *mall_tc_entry;
1052 struct dsa_switch *ds = p->dp->ds;
1053 struct net *net = dev_net(dev);
1054 struct dsa_slave_priv *to_p;
1055 struct net_device *to_dev;
1056 const struct tc_action *a;
1057 int err = -EOPNOTSUPP;
1058 LIST_HEAD(actions);
1059 int ifindex;
1060
1061 if (!ds->ops->port_mirror_add)
1062 return err;
1063
1064 if (!tc_single_action(cls->exts))
1065 return err;
1066
1067 tcf_exts_to_list(cls->exts, &actions);
1068 a = list_first_entry(&actions, struct tc_action, list);
1069
1070 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1071 struct dsa_mall_mirror_tc_entry *mirror;
1072
1073 ifindex = tcf_mirred_ifindex(a);
1074 to_dev = __dev_get_by_index(net, ifindex);
1075 if (!to_dev)
1076 return -EINVAL;
1077
1078 if (!dsa_slave_dev_check(to_dev))
1079 return -EOPNOTSUPP;
1080
1081 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1082 if (!mall_tc_entry)
1083 return -ENOMEM;
1084
1085 mall_tc_entry->cookie = cls->cookie;
1086 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1087 mirror = &mall_tc_entry->mirror;
1088
1089 to_p = netdev_priv(to_dev);
1090
1091 mirror->to_local_port = to_p->dp->index;
1092 mirror->ingress = ingress;
1093
1094 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1095 ingress);
1096 if (err) {
1097 kfree(mall_tc_entry);
1098 return err;
1099 }
1100
1101 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1102 }
1103
1104 return 0;
1105}
1106
1107static void dsa_slave_del_cls_matchall(struct net_device *dev,
1108 struct tc_cls_matchall_offload *cls)
1109{
1110 struct dsa_slave_priv *p = netdev_priv(dev);
1111 struct dsa_mall_tc_entry *mall_tc_entry;
1112 struct dsa_switch *ds = p->dp->ds;
1113
1114 if (!ds->ops->port_mirror_del)
1115 return;
1116
1117 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1118 if (!mall_tc_entry)
1119 return;
1120
1121 list_del(&mall_tc_entry->list);
1122
1123 switch (mall_tc_entry->type) {
1124 case DSA_PORT_MALL_MIRROR:
1125 ds->ops->port_mirror_del(ds, p->dp->index,
1126 &mall_tc_entry->mirror);
1127 break;
1128 default:
1129 WARN_ON(1);
1130 }
1131
1132 kfree(mall_tc_entry);
1133}
1134
1135static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1136 __be16 protocol, struct tc_to_netdev *tc)
1137{
1138 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1139 int ret = -EOPNOTSUPP;
1140
1141 switch (tc->type) {
1142 case TC_SETUP_MATCHALL:
1143 switch (tc->cls_mall->command) {
1144 case TC_CLSMATCHALL_REPLACE:
1145 return dsa_slave_add_cls_matchall(dev, protocol,
1146 tc->cls_mall,
1147 ingress);
1148 case TC_CLSMATCHALL_DESTROY:
1149 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1150 return 0;
1151 }
1152 default:
1153 break;
1154 }
1155
1156 return ret;
1157}
1158
Florian Fainelliaf421922016-06-07 16:32:41 -07001159void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1160{
1161 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1162 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1163 ops->get_strings = dsa_cpu_port_get_strings;
1164}
1165
Florian Fainellibf9f2642017-01-30 09:48:40 -08001166static int dsa_slave_get_rxnfc(struct net_device *dev,
1167 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1168{
1169 struct dsa_slave_priv *p = netdev_priv(dev);
1170 struct dsa_switch *ds = p->dp->ds;
1171
1172 if (!ds->ops->get_rxnfc)
1173 return -EOPNOTSUPP;
1174
1175 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1176}
1177
1178static int dsa_slave_set_rxnfc(struct net_device *dev,
1179 struct ethtool_rxnfc *nfc)
1180{
1181 struct dsa_slave_priv *p = netdev_priv(dev);
1182 struct dsa_switch *ds = p->dp->ds;
1183
1184 if (!ds->ops->set_rxnfc)
1185 return -EOPNOTSUPP;
1186
1187 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1188}
1189
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001190static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001191 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001192 .get_regs_len = dsa_slave_get_regs_len,
1193 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001194 .nway_reset = dsa_slave_nway_reset,
1195 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001196 .get_eeprom_len = dsa_slave_get_eeprom_len,
1197 .get_eeprom = dsa_slave_get_eeprom,
1198 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001199 .get_strings = dsa_slave_get_strings,
1200 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1201 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001202 .set_wol = dsa_slave_set_wol,
1203 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001204 .set_eee = dsa_slave_set_eee,
1205 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +02001206 .get_link_ksettings = dsa_slave_get_link_ksettings,
1207 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001208 .get_rxnfc = dsa_slave_get_rxnfc,
1209 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001210};
1211
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001212static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001213 .ndo_open = dsa_slave_open,
1214 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001215 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001216 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1217 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001218 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001219 .ndo_fdb_add = switchdev_port_fdb_add,
1220 .ndo_fdb_del = switchdev_port_fdb_del,
1221 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001222 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001223 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001224#ifdef CONFIG_NET_POLL_CONTROLLER
1225 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1226 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1227 .ndo_poll_controller = dsa_slave_poll_controller,
1228#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001229 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1230 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1231 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -08001232 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001233 .ndo_setup_tc = dsa_slave_setup_tc,
Scott Feldman98237d42015-03-15 21:07:15 -07001234};
1235
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001236static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001237 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001238 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001239 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1240 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1241 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001242};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001243
Florian Fainellif37db852015-09-23 18:19:58 -07001244static struct device_type dsa_type = {
1245 .name = "dsa",
1246};
1247
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001248static void dsa_slave_adjust_link(struct net_device *dev)
1249{
1250 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001251 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001252 unsigned int status_changed = 0;
1253
1254 if (p->old_link != p->phy->link) {
1255 status_changed = 1;
1256 p->old_link = p->phy->link;
1257 }
1258
1259 if (p->old_duplex != p->phy->duplex) {
1260 status_changed = 1;
1261 p->old_duplex = p->phy->duplex;
1262 }
1263
1264 if (p->old_pause != p->phy->pause) {
1265 status_changed = 1;
1266 p->old_pause = p->phy->pause;
1267 }
1268
Vivien Didelot9d490b42016-08-23 12:38:56 -04001269 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001270 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001271
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001272 if (status_changed)
1273 phy_print_status(p->phy);
1274}
1275
Florian Fainellice31b312014-08-27 17:04:54 -07001276static int dsa_slave_fixed_link_update(struct net_device *dev,
1277 struct fixed_phy_status *status)
1278{
Andrew Lunnb71be352016-03-12 00:01:37 +01001279 struct dsa_slave_priv *p;
1280 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001281
Andrew Lunnb71be352016-03-12 00:01:37 +01001282 if (dev) {
1283 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001284 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001285 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001286 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001287 }
Florian Fainellice31b312014-08-27 17:04:54 -07001288
1289 return 0;
1290}
1291
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001292/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001293static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001294 struct net_device *slave_dev,
1295 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001296{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001297 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001298
Andrew Lunn7f854422016-01-06 20:11:18 +01001299 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001300 if (!p->phy) {
1301 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001302 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001303 }
Florian Fainellic305c162015-03-10 16:57:12 -07001304
1305 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001306 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1307 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001308 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1309 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001310}
1311
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001312static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001313 struct net_device *slave_dev)
1314{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001315 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001316 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001317 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001318 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001319 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001320
Vivien Didelotafdcf152017-01-27 15:29:39 -05001321 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001322 mode = of_get_phy_mode(port_dn);
1323 if (mode < 0)
1324 mode = PHY_INTERFACE_MODE_NA;
1325 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001326
1327 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001328 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001329 /* In the case of a fixed PHY, the DT node associated
1330 * to the fixed PHY is the Port DT node
1331 */
1332 ret = of_phy_register_fixed_link(port_dn);
1333 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001334 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001335 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001336 }
Florian Fainellice31b312014-08-27 17:04:54 -07001337 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001338 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001339 }
1340
Vivien Didelot9d490b42016-08-23 12:38:56 -04001341 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001342 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001343
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001344 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001345 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1346
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001347 /* If this PHY address is part of phys_mii_mask, which means
1348 * that we need to divert reads and writes to/from it, then we
1349 * want to bind this device using the slave MII bus created by
1350 * DSA to make that happen.
1351 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001352 if (!phy_is_fixed && phy_id >= 0 &&
1353 (ds->phys_mii_mask & (1 << phy_id))) {
1354 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1355 if (ret) {
1356 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001357 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001358 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001359 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001360 } else {
1361 p->phy = of_phy_connect(slave_dev, phy_dn,
1362 dsa_slave_adjust_link,
1363 phy_flags,
1364 p->phy_interface);
1365 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001366
1367 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001368 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001369
Florian Fainellice31b312014-08-27 17:04:54 -07001370 if (p->phy && phy_is_fixed)
1371 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1372
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001373 /* We could not connect to a designated PHY, so use the switch internal
1374 * MDIO bus instead
1375 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001376 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001377 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001378 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001379 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1380 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001381 if (phy_is_fixed)
1382 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001383 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001384 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001385 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001386
Andrew Lunn22209432016-01-06 20:11:13 +01001387 phy_attached_info(p->phy);
1388
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001389 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001390}
1391
Andrew Lunn448b4482015-05-06 01:09:56 +02001392static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1393static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1394 struct netdev_queue *txq,
1395 void *_unused)
1396{
1397 lockdep_set_class(&txq->_xmit_lock,
1398 &dsa_slave_netdev_xmit_lock_key);
1399}
1400
Florian Fainelli24462542014-09-18 17:31:22 -07001401int dsa_slave_suspend(struct net_device *slave_dev)
1402{
1403 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1404
Florian Fainellif154be22017-01-25 09:10:41 -08001405 netif_device_detach(slave_dev);
1406
Florian Fainelli24462542014-09-18 17:31:22 -07001407 if (p->phy) {
1408 phy_stop(p->phy);
1409 p->old_pause = -1;
1410 p->old_link = -1;
1411 p->old_duplex = -1;
1412 phy_suspend(p->phy);
1413 }
1414
1415 return 0;
1416}
1417
1418int dsa_slave_resume(struct net_device *slave_dev)
1419{
1420 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1421
1422 netif_device_attach(slave_dev);
1423
1424 if (p->phy) {
1425 phy_resume(p->phy);
1426 phy_start(p->phy);
1427 }
1428
1429 return 0;
1430}
1431
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001432int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001433 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001434{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001435 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001436 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001437 struct net_device *slave_dev;
1438 struct dsa_slave_priv *p;
1439 int ret;
1440
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001441 master = ds->dst->master_netdev;
1442 if (ds->master_netdev)
1443 master = ds->master_netdev;
1444
Tom Gundersenc835a672014-07-14 16:37:24 +02001445 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1446 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001447 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001448 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001449
Florian Fainellif50f2122017-01-30 12:41:40 -08001450 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1451 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001452 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001453 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001454 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001455 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001456 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001457 slave_dev->min_mtu = 0;
1458 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001459 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001460
Andrew Lunn448b4482015-05-06 01:09:56 +02001461 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1462 NULL);
1463
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001464 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001465 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001466 slave_dev->vlan_features = master->vlan_features;
1467
1468 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001469 p->dp = &ds->ports[port];
Florian Fainellif50f2122017-01-30 12:41:40 -08001470 INIT_LIST_HEAD(&p->mall_tc_list);
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001471 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001472
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001473 p->old_pause = -1;
1474 p->old_link = -1;
1475 p->old_duplex = -1;
1476
Andrew Lunnc8b09802016-06-04 21:16:57 +02001477 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001478 ret = register_netdev(slave_dev);
1479 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001480 netdev_err(master, "error %d registering interface %s\n",
1481 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001482 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001483 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001484 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001485 }
1486
1487 netif_carrier_off(slave_dev);
1488
Andrew Lunn0071f562016-01-06 20:11:20 +01001489 ret = dsa_slave_phy_setup(p, slave_dev);
1490 if (ret) {
1491 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001492 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001493 free_netdev(slave_dev);
1494 return ret;
1495 }
1496
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001497 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001498}
Florian Fainellib73adef2015-02-24 13:15:33 -08001499
Neil Armstrongcda5c152015-12-07 13:57:35 +01001500void dsa_slave_destroy(struct net_device *slave_dev)
1501{
1502 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001503 struct device_node *port_dn;
1504
Vivien Didelotafdcf152017-01-27 15:29:39 -05001505 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001506
1507 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001508 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001509 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001510
1511 if (of_phy_is_fixed_link(port_dn))
1512 of_phy_deregister_fixed_link(port_dn);
1513 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001514 unregister_netdev(slave_dev);
1515 free_netdev(slave_dev);
1516}
1517
Florian Fainellib73adef2015-02-24 13:15:33 -08001518static bool dsa_slave_dev_check(struct net_device *dev)
1519{
1520 return dev->netdev_ops == &dsa_slave_netdev_ops;
1521}
1522
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001523static int dsa_slave_changeupper(struct net_device *dev,
1524 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001525{
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001526 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001527
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001528 if (netif_is_bridge_master(info->upper_dev)) {
1529 if (info->linking) {
1530 err = dsa_slave_bridge_port_join(dev, info->upper_dev);
1531 err = notifier_from_errno(err);
1532 } else {
1533 dsa_slave_bridge_port_leave(dev, info->upper_dev);
1534 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001535 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001536 }
1537
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001538 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001539}
1540
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001541static int dsa_slave_netdevice_event(struct notifier_block *nb,
1542 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001543{
Vivien Didelot6debb682016-03-13 16:21:34 -04001544 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001545
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001546 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1547 return NOTIFY_DONE;
1548
1549 if (event == NETDEV_CHANGEUPPER)
1550 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001551
Florian Fainellib73adef2015-02-24 13:15:33 -08001552 return NOTIFY_DONE;
1553}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001554
1555static struct notifier_block dsa_slave_nb __read_mostly = {
1556 .notifier_call = dsa_slave_netdevice_event,
1557};
1558
1559int dsa_slave_register_notifier(void)
1560{
1561 return register_netdevice_notifier(&dsa_slave_nb);
1562}
1563
1564void dsa_slave_unregister_notifier(void)
1565{
1566 int err;
1567
1568 err = unregister_netdevice_notifier(&dsa_slave_nb);
1569 if (err)
1570 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1571}