blob: 061a49c29cef168ad8ff994398b3909f5a73b752 [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);
687 int err;
688
689 err = -EOPNOTSUPP;
690 if (p->phy != NULL) {
691 err = phy_read_status(p->phy);
692 if (err == 0)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200693 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000694 }
695
696 return err;
697}
698
699static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200700dsa_slave_set_link_ksettings(struct net_device *dev,
701 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000702{
703 struct dsa_slave_priv *p = netdev_priv(dev);
704
705 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200706 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000707
708 return -EOPNOTSUPP;
709}
710
711static void dsa_slave_get_drvinfo(struct net_device *dev,
712 struct ethtool_drvinfo *drvinfo)
713{
Jiri Pirko7826d432013-01-06 00:44:26 +0000714 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000715 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
716 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000717}
718
Guenter Roeck3d762a02014-10-29 10:45:04 -0700719static int dsa_slave_get_regs_len(struct net_device *dev)
720{
721 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500722 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700723
Vivien Didelot9d490b42016-08-23 12:38:56 -0400724 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500725 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700726
727 return -EOPNOTSUPP;
728}
729
730static void
731dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
732{
733 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500734 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700735
Vivien Didelot9d490b42016-08-23 12:38:56 -0400736 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500737 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700738}
739
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000740static int dsa_slave_nway_reset(struct net_device *dev)
741{
742 struct dsa_slave_priv *p = netdev_priv(dev);
743
744 if (p->phy != NULL)
745 return genphy_restart_aneg(p->phy);
746
747 return -EOPNOTSUPP;
748}
749
750static u32 dsa_slave_get_link(struct net_device *dev)
751{
752 struct dsa_slave_priv *p = netdev_priv(dev);
753
754 if (p->phy != NULL) {
755 genphy_update_link(p->phy);
756 return p->phy->link;
757 }
758
759 return -EOPNOTSUPP;
760}
761
Guenter Roeck6793abb2014-10-29 10:45:01 -0700762static int dsa_slave_get_eeprom_len(struct net_device *dev)
763{
764 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500765 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700766
Andrew Lunn0e576042016-06-04 21:16:52 +0200767 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200768 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700769
Vivien Didelot9d490b42016-08-23 12:38:56 -0400770 if (ds->ops->get_eeprom_len)
771 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700772
773 return 0;
774}
775
776static int dsa_slave_get_eeprom(struct net_device *dev,
777 struct ethtool_eeprom *eeprom, u8 *data)
778{
779 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500780 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700781
Vivien Didelot9d490b42016-08-23 12:38:56 -0400782 if (ds->ops->get_eeprom)
783 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700784
785 return -EOPNOTSUPP;
786}
787
788static int dsa_slave_set_eeprom(struct net_device *dev,
789 struct ethtool_eeprom *eeprom, u8 *data)
790{
791 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500792 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700793
Vivien Didelot9d490b42016-08-23 12:38:56 -0400794 if (ds->ops->set_eeprom)
795 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700796
797 return -EOPNOTSUPP;
798}
799
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000800static void dsa_slave_get_strings(struct net_device *dev,
801 uint32_t stringset, uint8_t *data)
802{
803 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500804 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000805
806 if (stringset == ETH_SS_STATS) {
807 int len = ETH_GSTRING_LEN;
808
809 strncpy(data, "tx_packets", len);
810 strncpy(data + len, "tx_bytes", len);
811 strncpy(data + 2 * len, "rx_packets", len);
812 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400813 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500814 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000815 }
816}
817
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700818static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
819 struct ethtool_stats *stats,
820 uint64_t *data)
821{
822 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500823 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700824 s8 cpu_port = dst->cpu_port;
825 int count = 0;
826
827 if (dst->master_ethtool_ops.get_sset_count) {
828 count = dst->master_ethtool_ops.get_sset_count(dev,
829 ETH_SS_STATS);
830 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
831 }
832
Vivien Didelot9d490b42016-08-23 12:38:56 -0400833 if (ds->ops->get_ethtool_stats)
834 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700835}
836
837static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
838{
839 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500840 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700841 int count = 0;
842
843 if (dst->master_ethtool_ops.get_sset_count)
844 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
845
Vivien Didelot9d490b42016-08-23 12:38:56 -0400846 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
847 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700848
849 return count;
850}
851
852static void dsa_cpu_port_get_strings(struct net_device *dev,
853 uint32_t stringset, uint8_t *data)
854{
855 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500856 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700857 s8 cpu_port = dst->cpu_port;
858 int len = ETH_GSTRING_LEN;
859 int mcount = 0, count;
860 unsigned int i;
861 uint8_t pfx[4];
862 uint8_t *ndata;
863
864 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
865 /* We do not want to be NULL-terminated, since this is a prefix */
866 pfx[sizeof(pfx) - 1] = '_';
867
868 if (dst->master_ethtool_ops.get_sset_count) {
869 mcount = dst->master_ethtool_ops.get_sset_count(dev,
870 ETH_SS_STATS);
871 dst->master_ethtool_ops.get_strings(dev, stringset, data);
872 }
873
Vivien Didelot9d490b42016-08-23 12:38:56 -0400874 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700875 ndata = data + mcount * len;
876 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
877 * the output after to prepend our CPU port prefix we
878 * constructed earlier
879 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400880 ds->ops->get_strings(ds, cpu_port, ndata);
881 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700882 for (i = 0; i < count; i++) {
883 memmove(ndata + (i * len + sizeof(pfx)),
884 ndata + i * len, len - sizeof(pfx));
885 memcpy(ndata + i * len, pfx, sizeof(pfx));
886 }
887 }
888}
889
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000890static void dsa_slave_get_ethtool_stats(struct net_device *dev,
891 struct ethtool_stats *stats,
892 uint64_t *data)
893{
894 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500895 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000896
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400897 data[0] = dev->stats.tx_packets;
898 data[1] = dev->stats.tx_bytes;
899 data[2] = dev->stats.rx_packets;
900 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400901 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500902 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000903}
904
905static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
906{
907 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500908 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000909
910 if (sset == ETH_SS_STATS) {
911 int count;
912
913 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400914 if (ds->ops->get_sset_count)
915 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000916
917 return count;
918 }
919
920 return -EOPNOTSUPP;
921}
922
Florian Fainelli19e57c42014-09-18 17:31:24 -0700923static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
924{
925 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500926 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700927
Vivien Didelot9d490b42016-08-23 12:38:56 -0400928 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500929 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700930}
931
932static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
933{
934 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500935 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700936 int ret = -EOPNOTSUPP;
937
Vivien Didelot9d490b42016-08-23 12:38:56 -0400938 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500939 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700940
941 return ret;
942}
943
Florian Fainelli79052882014-09-24 17:05:21 -0700944static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
945{
946 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500947 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700948 int ret;
949
Vivien Didelot9d490b42016-08-23 12:38:56 -0400950 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700951 return -EOPNOTSUPP;
952
Vivien Didelotafdcf152017-01-27 15:29:39 -0500953 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700954 if (ret)
955 return ret;
956
957 if (p->phy)
958 ret = phy_ethtool_set_eee(p->phy, e);
959
960 return ret;
961}
962
963static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
964{
965 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500966 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700967 int ret;
968
Vivien Didelot9d490b42016-08-23 12:38:56 -0400969 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700970 return -EOPNOTSUPP;
971
Vivien Didelotafdcf152017-01-27 15:29:39 -0500972 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700973 if (ret)
974 return ret;
975
976 if (p->phy)
977 ret = phy_ethtool_get_eee(p->phy, e);
978
979 return ret;
980}
981
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700982#ifdef CONFIG_NET_POLL_CONTROLLER
983static int dsa_slave_netpoll_setup(struct net_device *dev,
984 struct netpoll_info *ni)
985{
986 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500987 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700988 struct net_device *master = ds->dst->master_netdev;
989 struct netpoll *netpoll;
990 int err = 0;
991
992 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
993 if (!netpoll)
994 return -ENOMEM;
995
996 err = __netpoll_setup(netpoll, master);
997 if (err) {
998 kfree(netpoll);
999 goto out;
1000 }
1001
1002 p->netpoll = netpoll;
1003out:
1004 return err;
1005}
1006
1007static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1008{
1009 struct dsa_slave_priv *p = netdev_priv(dev);
1010 struct netpoll *netpoll = p->netpoll;
1011
1012 if (!netpoll)
1013 return;
1014
1015 p->netpoll = NULL;
1016
1017 __netpoll_free_async(netpoll);
1018}
1019
1020static void dsa_slave_poll_controller(struct net_device *dev)
1021{
1022}
1023#endif
1024
Florian Fainelli44bb7652017-01-10 12:32:36 -08001025static int dsa_slave_get_phys_port_name(struct net_device *dev,
1026 char *name, size_t len)
1027{
1028 struct dsa_slave_priv *p = netdev_priv(dev);
1029
Vivien Didelotafdcf152017-01-27 15:29:39 -05001030 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -08001031 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -08001032
1033 return 0;
1034}
1035
Florian Fainellif50f2122017-01-30 12:41:40 -08001036static struct dsa_mall_tc_entry *
1037dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1038 unsigned long cookie)
1039{
1040 struct dsa_mall_tc_entry *mall_tc_entry;
1041
1042 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1043 if (mall_tc_entry->cookie == cookie)
1044 return mall_tc_entry;
1045
1046 return NULL;
1047}
1048
1049static int dsa_slave_add_cls_matchall(struct net_device *dev,
1050 __be16 protocol,
1051 struct tc_cls_matchall_offload *cls,
1052 bool ingress)
1053{
1054 struct dsa_slave_priv *p = netdev_priv(dev);
1055 struct dsa_mall_tc_entry *mall_tc_entry;
1056 struct dsa_switch *ds = p->dp->ds;
1057 struct net *net = dev_net(dev);
1058 struct dsa_slave_priv *to_p;
1059 struct net_device *to_dev;
1060 const struct tc_action *a;
1061 int err = -EOPNOTSUPP;
1062 LIST_HEAD(actions);
1063 int ifindex;
1064
1065 if (!ds->ops->port_mirror_add)
1066 return err;
1067
1068 if (!tc_single_action(cls->exts))
1069 return err;
1070
1071 tcf_exts_to_list(cls->exts, &actions);
1072 a = list_first_entry(&actions, struct tc_action, list);
1073
1074 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1075 struct dsa_mall_mirror_tc_entry *mirror;
1076
1077 ifindex = tcf_mirred_ifindex(a);
1078 to_dev = __dev_get_by_index(net, ifindex);
1079 if (!to_dev)
1080 return -EINVAL;
1081
1082 if (!dsa_slave_dev_check(to_dev))
1083 return -EOPNOTSUPP;
1084
1085 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1086 if (!mall_tc_entry)
1087 return -ENOMEM;
1088
1089 mall_tc_entry->cookie = cls->cookie;
1090 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1091 mirror = &mall_tc_entry->mirror;
1092
1093 to_p = netdev_priv(to_dev);
1094
1095 mirror->to_local_port = to_p->dp->index;
1096 mirror->ingress = ingress;
1097
1098 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1099 ingress);
1100 if (err) {
1101 kfree(mall_tc_entry);
1102 return err;
1103 }
1104
1105 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1106 }
1107
1108 return 0;
1109}
1110
1111static void dsa_slave_del_cls_matchall(struct net_device *dev,
1112 struct tc_cls_matchall_offload *cls)
1113{
1114 struct dsa_slave_priv *p = netdev_priv(dev);
1115 struct dsa_mall_tc_entry *mall_tc_entry;
1116 struct dsa_switch *ds = p->dp->ds;
1117
1118 if (!ds->ops->port_mirror_del)
1119 return;
1120
1121 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1122 if (!mall_tc_entry)
1123 return;
1124
1125 list_del(&mall_tc_entry->list);
1126
1127 switch (mall_tc_entry->type) {
1128 case DSA_PORT_MALL_MIRROR:
1129 ds->ops->port_mirror_del(ds, p->dp->index,
1130 &mall_tc_entry->mirror);
1131 break;
1132 default:
1133 WARN_ON(1);
1134 }
1135
1136 kfree(mall_tc_entry);
1137}
1138
1139static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1140 __be16 protocol, struct tc_to_netdev *tc)
1141{
1142 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1143 int ret = -EOPNOTSUPP;
1144
1145 switch (tc->type) {
1146 case TC_SETUP_MATCHALL:
1147 switch (tc->cls_mall->command) {
1148 case TC_CLSMATCHALL_REPLACE:
1149 return dsa_slave_add_cls_matchall(dev, protocol,
1150 tc->cls_mall,
1151 ingress);
1152 case TC_CLSMATCHALL_DESTROY:
1153 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1154 return 0;
1155 }
1156 default:
1157 break;
1158 }
1159
1160 return ret;
1161}
1162
Florian Fainelliaf421922016-06-07 16:32:41 -07001163void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1164{
1165 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1166 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1167 ops->get_strings = dsa_cpu_port_get_strings;
1168}
1169
Florian Fainellibf9f2642017-01-30 09:48:40 -08001170static int dsa_slave_get_rxnfc(struct net_device *dev,
1171 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1172{
1173 struct dsa_slave_priv *p = netdev_priv(dev);
1174 struct dsa_switch *ds = p->dp->ds;
1175
1176 if (!ds->ops->get_rxnfc)
1177 return -EOPNOTSUPP;
1178
1179 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1180}
1181
1182static int dsa_slave_set_rxnfc(struct net_device *dev,
1183 struct ethtool_rxnfc *nfc)
1184{
1185 struct dsa_slave_priv *p = netdev_priv(dev);
1186 struct dsa_switch *ds = p->dp->ds;
1187
1188 if (!ds->ops->set_rxnfc)
1189 return -EOPNOTSUPP;
1190
1191 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1192}
1193
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001194static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001195 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001196 .get_regs_len = dsa_slave_get_regs_len,
1197 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001198 .nway_reset = dsa_slave_nway_reset,
1199 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001200 .get_eeprom_len = dsa_slave_get_eeprom_len,
1201 .get_eeprom = dsa_slave_get_eeprom,
1202 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001203 .get_strings = dsa_slave_get_strings,
1204 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1205 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001206 .set_wol = dsa_slave_set_wol,
1207 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001208 .set_eee = dsa_slave_set_eee,
1209 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +02001210 .get_link_ksettings = dsa_slave_get_link_ksettings,
1211 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001212 .get_rxnfc = dsa_slave_get_rxnfc,
1213 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001214};
1215
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001216static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001217 .ndo_open = dsa_slave_open,
1218 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001219 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001220 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1221 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001222 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001223 .ndo_fdb_add = switchdev_port_fdb_add,
1224 .ndo_fdb_del = switchdev_port_fdb_del,
1225 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001226 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001227 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001228#ifdef CONFIG_NET_POLL_CONTROLLER
1229 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1230 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1231 .ndo_poll_controller = dsa_slave_poll_controller,
1232#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001233 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1234 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1235 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -08001236 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001237 .ndo_setup_tc = dsa_slave_setup_tc,
Scott Feldman98237d42015-03-15 21:07:15 -07001238};
1239
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001240static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001241 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001242 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001243 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1244 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1245 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001246};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001247
Florian Fainellif37db852015-09-23 18:19:58 -07001248static struct device_type dsa_type = {
1249 .name = "dsa",
1250};
1251
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001252static void dsa_slave_adjust_link(struct net_device *dev)
1253{
1254 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001255 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001256 unsigned int status_changed = 0;
1257
1258 if (p->old_link != p->phy->link) {
1259 status_changed = 1;
1260 p->old_link = p->phy->link;
1261 }
1262
1263 if (p->old_duplex != p->phy->duplex) {
1264 status_changed = 1;
1265 p->old_duplex = p->phy->duplex;
1266 }
1267
1268 if (p->old_pause != p->phy->pause) {
1269 status_changed = 1;
1270 p->old_pause = p->phy->pause;
1271 }
1272
Vivien Didelot9d490b42016-08-23 12:38:56 -04001273 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001274 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001275
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001276 if (status_changed)
1277 phy_print_status(p->phy);
1278}
1279
Florian Fainellice31b312014-08-27 17:04:54 -07001280static int dsa_slave_fixed_link_update(struct net_device *dev,
1281 struct fixed_phy_status *status)
1282{
Andrew Lunnb71be352016-03-12 00:01:37 +01001283 struct dsa_slave_priv *p;
1284 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001285
Andrew Lunnb71be352016-03-12 00:01:37 +01001286 if (dev) {
1287 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001288 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001289 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001290 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001291 }
Florian Fainellice31b312014-08-27 17:04:54 -07001292
1293 return 0;
1294}
1295
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001296/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001297static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001298 struct net_device *slave_dev,
1299 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001300{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001301 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001302
Andrew Lunn7f854422016-01-06 20:11:18 +01001303 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001304 if (!p->phy) {
1305 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001306 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001307 }
Florian Fainellic305c162015-03-10 16:57:12 -07001308
1309 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001310 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1311 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001312 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1313 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001314}
1315
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001316static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001317 struct net_device *slave_dev)
1318{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001319 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001320 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001321 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001322 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001323 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001324
Vivien Didelotafdcf152017-01-27 15:29:39 -05001325 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001326 mode = of_get_phy_mode(port_dn);
1327 if (mode < 0)
1328 mode = PHY_INTERFACE_MODE_NA;
1329 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001330
1331 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001332 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001333 /* In the case of a fixed PHY, the DT node associated
1334 * to the fixed PHY is the Port DT node
1335 */
1336 ret = of_phy_register_fixed_link(port_dn);
1337 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001338 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001339 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001340 }
Florian Fainellice31b312014-08-27 17:04:54 -07001341 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001342 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001343 }
1344
Vivien Didelot9d490b42016-08-23 12:38:56 -04001345 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001346 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001347
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001348 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001349 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1350
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001351 /* If this PHY address is part of phys_mii_mask, which means
1352 * that we need to divert reads and writes to/from it, then we
1353 * want to bind this device using the slave MII bus created by
1354 * DSA to make that happen.
1355 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001356 if (!phy_is_fixed && phy_id >= 0 &&
1357 (ds->phys_mii_mask & (1 << phy_id))) {
1358 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1359 if (ret) {
1360 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001361 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001362 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001363 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001364 } else {
1365 p->phy = of_phy_connect(slave_dev, phy_dn,
1366 dsa_slave_adjust_link,
1367 phy_flags,
1368 p->phy_interface);
1369 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001370
1371 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001372 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001373
Florian Fainellice31b312014-08-27 17:04:54 -07001374 if (p->phy && phy_is_fixed)
1375 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1376
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001377 /* We could not connect to a designated PHY, so use the switch internal
1378 * MDIO bus instead
1379 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001380 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001381 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001382 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001383 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1384 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001385 if (phy_is_fixed)
1386 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001387 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001388 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001389 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001390
Andrew Lunn22209432016-01-06 20:11:13 +01001391 phy_attached_info(p->phy);
1392
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001393 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001394}
1395
Andrew Lunn448b4482015-05-06 01:09:56 +02001396static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1397static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1398 struct netdev_queue *txq,
1399 void *_unused)
1400{
1401 lockdep_set_class(&txq->_xmit_lock,
1402 &dsa_slave_netdev_xmit_lock_key);
1403}
1404
Florian Fainelli24462542014-09-18 17:31:22 -07001405int dsa_slave_suspend(struct net_device *slave_dev)
1406{
1407 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1408
Florian Fainellif154be22017-01-25 09:10:41 -08001409 netif_device_detach(slave_dev);
1410
Florian Fainelli24462542014-09-18 17:31:22 -07001411 if (p->phy) {
1412 phy_stop(p->phy);
1413 p->old_pause = -1;
1414 p->old_link = -1;
1415 p->old_duplex = -1;
1416 phy_suspend(p->phy);
1417 }
1418
1419 return 0;
1420}
1421
1422int dsa_slave_resume(struct net_device *slave_dev)
1423{
1424 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1425
1426 netif_device_attach(slave_dev);
1427
1428 if (p->phy) {
1429 phy_resume(p->phy);
1430 phy_start(p->phy);
1431 }
1432
1433 return 0;
1434}
1435
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001436int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001437 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001438{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001439 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001440 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001441 struct net_device *slave_dev;
1442 struct dsa_slave_priv *p;
1443 int ret;
1444
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001445 master = ds->dst->master_netdev;
1446 if (ds->master_netdev)
1447 master = ds->master_netdev;
1448
Tom Gundersenc835a672014-07-14 16:37:24 +02001449 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1450 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001451 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001452 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001453
Florian Fainellif50f2122017-01-30 12:41:40 -08001454 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1455 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001456 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001457 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001458 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001459 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001460 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001461 slave_dev->min_mtu = 0;
1462 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001463 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001464
Andrew Lunn448b4482015-05-06 01:09:56 +02001465 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1466 NULL);
1467
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001468 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001469 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001470 slave_dev->vlan_features = master->vlan_features;
1471
1472 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001473 p->dp = &ds->ports[port];
Florian Fainellif50f2122017-01-30 12:41:40 -08001474 INIT_LIST_HEAD(&p->mall_tc_list);
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001475 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001476
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001477 p->old_pause = -1;
1478 p->old_link = -1;
1479 p->old_duplex = -1;
1480
Andrew Lunnc8b09802016-06-04 21:16:57 +02001481 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001482 ret = register_netdev(slave_dev);
1483 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001484 netdev_err(master, "error %d registering interface %s\n",
1485 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001486 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001487 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001488 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001489 }
1490
1491 netif_carrier_off(slave_dev);
1492
Andrew Lunn0071f562016-01-06 20:11:20 +01001493 ret = dsa_slave_phy_setup(p, slave_dev);
1494 if (ret) {
1495 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001496 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001497 free_netdev(slave_dev);
1498 return ret;
1499 }
1500
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001501 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001502}
Florian Fainellib73adef2015-02-24 13:15:33 -08001503
Neil Armstrongcda5c152015-12-07 13:57:35 +01001504void dsa_slave_destroy(struct net_device *slave_dev)
1505{
1506 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001507 struct device_node *port_dn;
1508
Vivien Didelotafdcf152017-01-27 15:29:39 -05001509 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001510
1511 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001512 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001513 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001514
1515 if (of_phy_is_fixed_link(port_dn))
1516 of_phy_deregister_fixed_link(port_dn);
1517 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001518 unregister_netdev(slave_dev);
1519 free_netdev(slave_dev);
1520}
1521
Florian Fainellib73adef2015-02-24 13:15:33 -08001522static bool dsa_slave_dev_check(struct net_device *dev)
1523{
1524 return dev->netdev_ops == &dsa_slave_netdev_ops;
1525}
1526
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001527static int dsa_slave_changeupper(struct net_device *dev,
1528 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001529{
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001530 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001531
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001532 if (netif_is_bridge_master(info->upper_dev)) {
1533 if (info->linking) {
1534 err = dsa_slave_bridge_port_join(dev, info->upper_dev);
1535 err = notifier_from_errno(err);
1536 } else {
1537 dsa_slave_bridge_port_leave(dev, info->upper_dev);
1538 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001539 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001540 }
1541
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001542 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001543}
1544
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001545static int dsa_slave_netdevice_event(struct notifier_block *nb,
1546 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001547{
Vivien Didelot6debb682016-03-13 16:21:34 -04001548 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001549
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001550 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1551 return NOTIFY_DONE;
1552
1553 if (event == NETDEV_CHANGEUPPER)
1554 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001555
Florian Fainellib73adef2015-02-24 13:15:33 -08001556 return NOTIFY_DONE;
1557}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001558
1559static struct notifier_block dsa_slave_nb __read_mostly = {
1560 .notifier_call = dsa_slave_netdevice_event,
1561};
1562
1563int dsa_slave_register_notifier(void)
1564{
1565 return register_netdevice_notifier(&dsa_slave_nb);
1566}
1567
1568void dsa_slave_unregister_notifier(void)
1569{
1570 int err;
1571
1572 err = unregister_netdevice_notifier(&dsa_slave_nb);
1573 if (err)
1574 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1575}