blob: d8c3c0f00cf3f99c2c2cc2405db1d28afb2ffafb [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
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000030/* slave mii_bus handling ***************************************************/
31static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
32{
33 struct dsa_switch *ds = bus->priv;
34
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070035 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040036 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000037
38 return 0xffff;
39}
40
41static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
42{
43 struct dsa_switch *ds = bus->priv;
44
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070045 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040046 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000047
48 return 0;
49}
50
51void dsa_slave_mii_bus_init(struct dsa_switch *ds)
52{
53 ds->slave_mii_bus->priv = (void *)ds;
54 ds->slave_mii_bus->name = "dsa slave smi";
55 ds->slave_mii_bus->read = dsa_slave_phy_read;
56 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070057 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
58 ds->dst->tree, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020059 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050060 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000061}
62
63
64/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020065static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000066{
67 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000068
Vivien Didelotafdcf152017-01-27 15:29:39 -050069 return p->dp->ds->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000070}
71
Vivien Didelota5e9a022017-01-27 15:29:40 -050072static inline bool dsa_port_is_bridged(struct dsa_port *dp)
Florian Fainellib73adef2015-02-24 13:15:33 -080073{
Vivien Didelota5e9a022017-01-27 15:29:40 -050074 return !!dp->bridge_dev;
Florian Fainellib73adef2015-02-24 13:15:33 -080075}
76
Vivien Didelotc5d35cb2017-02-03 13:20:19 -050077static void dsa_slave_set_state(struct net_device *dev, u8 state)
Vivien Didelot4acfee82016-09-22 16:49:21 -040078{
Vivien Didelotc5d35cb2017-02-03 13:20:19 -050079 struct dsa_slave_priv *p = netdev_priv(dev);
80 struct dsa_port *dp = p->dp;
81 struct dsa_switch *ds = dp->ds;
82 int port = dp->index;
Vivien Didelot732f7942016-09-22 16:49:22 -040083
Vivien Didelot4acfee82016-09-22 16:49:21 -040084 if (ds->ops->port_stp_state_set)
85 ds->ops->port_stp_state_set(ds, port, state);
Vivien Didelot732f7942016-09-22 16:49:22 -040086
87 if (ds->ops->port_fast_age) {
88 /* Fast age FDB entries or flush appropriate forwarding database
89 * for the given port, if we are moving it from Learning or
90 * Forwarding state, to Disabled or Blocking or Listening state.
91 */
92
93 if ((dp->stp_state == BR_STATE_LEARNING ||
94 dp->stp_state == BR_STATE_FORWARDING) &&
95 (state == BR_STATE_DISABLED ||
96 state == BR_STATE_BLOCKING ||
97 state == BR_STATE_LISTENING))
98 ds->ops->port_fast_age(ds, port);
99 }
100
101 dp->stp_state = state;
Vivien Didelot4acfee82016-09-22 16:49:21 -0400102}
103
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000104static int dsa_slave_open(struct net_device *dev)
105{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800106 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500107 struct net_device *master = p->dp->ds->dst->master_netdev;
108 struct dsa_switch *ds = p->dp->ds;
Vivien Didelota5e9a022017-01-27 15:29:40 -0500109 u8 stp_state = dsa_port_is_bridged(p->dp) ?
Florian Fainellib73adef2015-02-24 13:15:33 -0800110 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800111 int err;
112
113 if (!(master->flags & IFF_UP))
114 return -ENETDOWN;
115
Joe Perches8feedbb2012-05-08 18:56:57 +0000116 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000117 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800118 if (err < 0)
119 goto out;
120 }
121
122 if (dev->flags & IFF_ALLMULTI) {
123 err = dev_set_allmulti(master, 1);
124 if (err < 0)
125 goto del_unicast;
126 }
127 if (dev->flags & IFF_PROMISC) {
128 err = dev_set_promiscuity(master, 1);
129 if (err < 0)
130 goto clear_allmulti;
131 }
132
Vivien Didelot9d490b42016-08-23 12:38:56 -0400133 if (ds->ops->port_enable) {
Vivien Didelotafdcf152017-01-27 15:29:39 -0500134 err = ds->ops->port_enable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700135 if (err)
136 goto clear_promisc;
137 }
138
Vivien Didelotc5d35cb2017-02-03 13:20:19 -0500139 dsa_slave_set_state(dev, stp_state);
Florian Fainellib73adef2015-02-24 13:15:33 -0800140
Florian Fainellif7f1de52014-09-24 17:05:17 -0700141 if (p->phy)
142 phy_start(p->phy);
143
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000144 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800145
Florian Fainellib2f2af22014-09-24 17:05:18 -0700146clear_promisc:
147 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300148 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800149clear_allmulti:
150 if (dev->flags & IFF_ALLMULTI)
151 dev_set_allmulti(master, -1);
152del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000153 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000154 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800155out:
156 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000157}
158
159static int dsa_slave_close(struct net_device *dev)
160{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800161 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500162 struct net_device *master = p->dp->ds->dst->master_netdev;
163 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800164
Florian Fainellif7f1de52014-09-24 17:05:17 -0700165 if (p->phy)
166 phy_stop(p->phy);
167
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800168 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000169 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800170 if (dev->flags & IFF_ALLMULTI)
171 dev_set_allmulti(master, -1);
172 if (dev->flags & IFF_PROMISC)
173 dev_set_promiscuity(master, -1);
174
Joe Perches8feedbb2012-05-08 18:56:57 +0000175 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000176 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800177
Vivien Didelot9d490b42016-08-23 12:38:56 -0400178 if (ds->ops->port_disable)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500179 ds->ops->port_disable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700180
Vivien Didelotc5d35cb2017-02-03 13:20:19 -0500181 dsa_slave_set_state(dev, BR_STATE_DISABLED);
Florian Fainellib73adef2015-02-24 13:15:33 -0800182
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000183 return 0;
184}
185
186static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
187{
188 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500189 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000190
191 if (change & IFF_ALLMULTI)
192 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
193 if (change & IFF_PROMISC)
194 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
195}
196
197static void dsa_slave_set_rx_mode(struct net_device *dev)
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 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000203 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000204}
205
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800206static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000207{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800208 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500209 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800210 struct sockaddr *addr = a;
211 int err;
212
213 if (!is_valid_ether_addr(addr->sa_data))
214 return -EADDRNOTAVAIL;
215
216 if (!(dev->flags & IFF_UP))
217 goto out;
218
Joe Perches8feedbb2012-05-08 18:56:57 +0000219 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000220 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800221 if (err < 0)
222 return err;
223 }
224
Joe Perches8feedbb2012-05-08 18:56:57 +0000225 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000226 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800227
228out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800229 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000230
231 return 0;
232}
233
Vivien Didelot11149532015-08-13 12:52:17 -0400234static int dsa_slave_port_vlan_add(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200235 const struct switchdev_obj_port_vlan *vlan,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200236 struct switchdev_trans *trans)
Vivien Didelot11149532015-08-13 12:52:17 -0400237{
Vivien Didelot11149532015-08-13 12:52:17 -0400238 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500239 struct dsa_port *dp = p->dp;
240 struct dsa_switch *ds = dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400241
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200242 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400243 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
Vivien Didelot11149532015-08-13 12:52:17 -0400244 return -EOPNOTSUPP;
245
Vivien Didelotafdcf152017-01-27 15:29:39 -0500246 return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400247 }
248
Vivien Didelotafdcf152017-01-27 15:29:39 -0500249 ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
Vivien Didelot4d5770b2016-04-06 11:55:05 -0400250
Vivien Didelot11149532015-08-13 12:52:17 -0400251 return 0;
252}
253
254static int dsa_slave_port_vlan_del(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200255 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot11149532015-08-13 12:52:17 -0400256{
Vivien Didelot11149532015-08-13 12:52:17 -0400257 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500258 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400259
Vivien Didelot9d490b42016-08-23 12:38:56 -0400260 if (!ds->ops->port_vlan_del)
Vivien Didelot11149532015-08-13 12:52:17 -0400261 return -EOPNOTSUPP;
262
Vivien Didelotafdcf152017-01-27 15:29:39 -0500263 return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
Vivien Didelot11149532015-08-13 12:52:17 -0400264}
265
266static int dsa_slave_port_vlan_dump(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200267 struct switchdev_obj_port_vlan *vlan,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200268 switchdev_obj_dump_cb_t *cb)
Vivien Didelot11149532015-08-13 12:52:17 -0400269{
Vivien Didelot11149532015-08-13 12:52:17 -0400270 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500271 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400272
Vivien Didelot9d490b42016-08-23 12:38:56 -0400273 if (ds->ops->port_vlan_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500274 return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
Vivien Didelot65aebfc2016-02-23 12:13:54 -0500275
Vivien Didelot477b1842016-02-23 12:13:56 -0500276 return -EOPNOTSUPP;
Vivien Didelot11149532015-08-13 12:52:17 -0400277}
278
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400279static int dsa_slave_port_fdb_add(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200280 const struct switchdev_obj_port_fdb *fdb,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200281 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700282{
283 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500284 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot146a3202015-10-08 11:35:12 -0400285
Vivien Didelot8497aa62016-04-06 11:55:04 -0400286 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400287 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
Vivien Didelot8497aa62016-04-06 11:55:04 -0400288 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700289
Vivien Didelotafdcf152017-01-27 15:29:39 -0500290 return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400291 }
David S. Millercdf09692015-08-11 12:00:37 -0700292
Vivien Didelotafdcf152017-01-27 15:29:39 -0500293 ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400294
295 return 0;
David S. Millercdf09692015-08-11 12:00:37 -0700296}
297
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400298static int dsa_slave_port_fdb_del(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200299 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -0700300{
301 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500302 struct dsa_switch *ds = p->dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700303 int ret = -EOPNOTSUPP;
304
Vivien Didelot9d490b42016-08-23 12:38:56 -0400305 if (ds->ops->port_fdb_del)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500306 ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700307
308 return ret;
309}
310
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400311static int dsa_slave_port_fdb_dump(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200312 struct switchdev_obj_port_fdb *fdb,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200313 switchdev_obj_dump_cb_t *cb)
David S. Millercdf09692015-08-11 12:00:37 -0700314{
315 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500316 struct dsa_switch *ds = p->dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700317
Vivien Didelot9d490b42016-08-23 12:38:56 -0400318 if (ds->ops->port_fdb_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500319 return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb);
Vivien Didelotea70ba92015-10-22 09:34:38 -0400320
Vivien Didelot1a49a2f2015-10-22 09:34:43 -0400321 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700322}
323
Vivien Didelot8df30252016-08-31 11:50:03 -0400324static int dsa_slave_port_mdb_add(struct net_device *dev,
325 const struct switchdev_obj_port_mdb *mdb,
326 struct switchdev_trans *trans)
327{
328 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500329 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400330
331 if (switchdev_trans_ph_prepare(trans)) {
332 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
333 return -EOPNOTSUPP;
334
Vivien Didelotafdcf152017-01-27 15:29:39 -0500335 return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400336 }
337
Vivien Didelotafdcf152017-01-27 15:29:39 -0500338 ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400339
340 return 0;
341}
342
343static int dsa_slave_port_mdb_del(struct net_device *dev,
344 const struct switchdev_obj_port_mdb *mdb)
345{
346 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500347 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400348
349 if (ds->ops->port_mdb_del)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500350 return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400351
352 return -EOPNOTSUPP;
353}
354
355static int dsa_slave_port_mdb_dump(struct net_device *dev,
356 struct switchdev_obj_port_mdb *mdb,
357 switchdev_obj_dump_cb_t *cb)
358{
359 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500360 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400361
362 if (ds->ops->port_mdb_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500363 return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400364
365 return -EOPNOTSUPP;
366}
367
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000368static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
369{
370 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000371
372 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000373 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000374
375 return -EOPNOTSUPP;
376}
377
Vivien Didelot43c44a92016-04-06 11:55:03 -0400378static int dsa_slave_stp_state_set(struct net_device *dev,
379 const struct switchdev_attr *attr,
380 struct switchdev_trans *trans)
Florian Fainellib73adef2015-02-24 13:15:33 -0800381{
382 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500383 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800384
Vivien Didelot43c44a92016-04-06 11:55:03 -0400385 if (switchdev_trans_ph_prepare(trans))
Vivien Didelot9d490b42016-08-23 12:38:56 -0400386 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
Florian Fainellib73adef2015-02-24 13:15:33 -0800387
Vivien Didelotc5d35cb2017-02-03 13:20:19 -0500388 dsa_slave_set_state(dev, attr->u.stp_state);
Vivien Didelot43c44a92016-04-06 11:55:03 -0400389
390 return 0;
Florian Fainellib73adef2015-02-24 13:15:33 -0800391}
392
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500393static int dsa_slave_vlan_filtering(struct net_device *dev,
394 const struct switchdev_attr *attr,
395 struct switchdev_trans *trans)
396{
397 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500398 struct dsa_switch *ds = p->dp->ds;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500399
400 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
401 if (switchdev_trans_ph_prepare(trans))
402 return 0;
403
Vivien Didelot9d490b42016-08-23 12:38:56 -0400404 if (ds->ops->port_vlan_filtering)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500405 return ds->ops->port_vlan_filtering(ds, p->dp->index,
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500406 attr->u.vlan_filtering);
407
408 return 0;
409}
410
Vivien Didelot34a79f62016-07-18 20:45:38 -0400411static int dsa_fastest_ageing_time(struct dsa_switch *ds,
412 unsigned int ageing_time)
413{
414 int i;
415
Vivien Didelot26895e22017-01-27 15:29:37 -0500416 for (i = 0; i < ds->num_ports; ++i) {
Vivien Didelot34a79f62016-07-18 20:45:38 -0400417 struct dsa_port *dp = &ds->ports[i];
418
419 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
420 ageing_time = dp->ageing_time;
421 }
422
423 return ageing_time;
424}
425
426static int dsa_slave_ageing_time(struct net_device *dev,
427 const struct switchdev_attr *attr,
428 struct switchdev_trans *trans)
429{
430 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500431 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400432 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
433 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
434
435 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
436 if (switchdev_trans_ph_prepare(trans))
437 return 0;
438
439 /* Keep the fastest ageing time in case of multiple bridges */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500440 p->dp->ageing_time = ageing_time;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400441 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
442
Vivien Didelot9d490b42016-08-23 12:38:56 -0400443 if (ds->ops->set_ageing_time)
444 return ds->ops->set_ageing_time(ds, ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400445
446 return 0;
447}
448
Scott Feldman35636062015-05-10 09:47:51 -0700449static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200450 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200451 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700452{
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400453 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700454
455 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200456 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelot43c44a92016-04-06 11:55:03 -0400457 ret = dsa_slave_stp_state_set(dev, attr, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700458 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500459 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
460 ret = dsa_slave_vlan_filtering(dev, attr, trans);
461 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400462 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
463 ret = dsa_slave_ageing_time(dev, attr, trans);
464 break;
Scott Feldman35636062015-05-10 09:47:51 -0700465 default:
466 ret = -EOPNOTSUPP;
467 break;
468 }
469
470 return ret;
471}
472
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400473static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200474 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200475 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400476{
477 int err;
478
479 /* For the prepare phase, ensure the full set of changes is feasable in
480 * one go in order to signal a failure properly. If an operation is not
481 * supported, return -EOPNOTSUPP.
482 */
483
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200484 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200485 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200486 err = dsa_slave_port_fdb_add(dev,
487 SWITCHDEV_OBJ_PORT_FDB(obj),
488 trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400489 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400490 case SWITCHDEV_OBJ_ID_PORT_MDB:
491 err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
492 trans);
493 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200494 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200495 err = dsa_slave_port_vlan_add(dev,
496 SWITCHDEV_OBJ_PORT_VLAN(obj),
497 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400498 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400499 default:
500 err = -EOPNOTSUPP;
501 break;
502 }
503
504 return err;
505}
506
507static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200508 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400509{
510 int err;
511
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200512 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200513 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200514 err = dsa_slave_port_fdb_del(dev,
515 SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400516 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400517 case SWITCHDEV_OBJ_ID_PORT_MDB:
518 err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
519 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200520 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200521 err = dsa_slave_port_vlan_del(dev,
522 SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400523 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400524 default:
525 err = -EOPNOTSUPP;
526 break;
527 }
528
529 return err;
530}
531
532static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200533 struct switchdev_obj *obj,
534 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400535{
536 int err;
537
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200538 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200539 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200540 err = dsa_slave_port_fdb_dump(dev,
541 SWITCHDEV_OBJ_PORT_FDB(obj),
542 cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400543 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400544 case SWITCHDEV_OBJ_ID_PORT_MDB:
545 err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
546 cb);
547 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200548 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200549 err = dsa_slave_port_vlan_dump(dev,
550 SWITCHDEV_OBJ_PORT_VLAN(obj),
551 cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400552 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400553 default:
554 err = -EOPNOTSUPP;
555 break;
556 }
557
558 return err;
559}
560
Florian Fainellib73adef2015-02-24 13:15:33 -0800561static int dsa_slave_bridge_port_join(struct net_device *dev,
562 struct net_device *br)
563{
564 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500565 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800566 int ret = -EOPNOTSUPP;
567
Vivien Didelot9c265422017-02-03 13:20:18 -0500568 /* Here the port is already bridged. Reflect the current configuration
569 * so that drivers can program their chips accordingly.
570 */
Vivien Didelota5e9a022017-01-27 15:29:40 -0500571 p->dp->bridge_dev = br;
Florian Fainellib73adef2015-02-24 13:15:33 -0800572
Vivien Didelot9d490b42016-08-23 12:38:56 -0400573 if (ds->ops->port_bridge_join)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500574 ret = ds->ops->port_bridge_join(ds, p->dp->index, br);
Florian Fainellib73adef2015-02-24 13:15:33 -0800575
Vivien Didelot9c265422017-02-03 13:20:18 -0500576 /* The bridging is rolled back on error */
577 if (ret && ret != -EOPNOTSUPP) {
578 p->dp->bridge_dev = NULL;
579 return ret;
580 }
581
582 return 0;
Florian Fainellib73adef2015-02-24 13:15:33 -0800583}
584
Vivien Didelotf123f2f2017-01-27 15:29:41 -0500585static void dsa_slave_bridge_port_leave(struct net_device *dev,
586 struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800587{
588 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500589 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800590
Vivien Didelot9c265422017-02-03 13:20:18 -0500591 /* Here the port is already unbridged. Reflect the current configuration
592 * so that drivers can program their chips accordingly.
593 */
Vivien Didelotf123f2f2017-01-27 15:29:41 -0500594 p->dp->bridge_dev = NULL;
Florian Fainellib73adef2015-02-24 13:15:33 -0800595
Vivien Didelot9d490b42016-08-23 12:38:56 -0400596 if (ds->ops->port_bridge_leave)
Vivien Didelotf123f2f2017-01-27 15:29:41 -0500597 ds->ops->port_bridge_leave(ds, p->dp->index, br);
Florian Fainellib73adef2015-02-24 13:15:33 -0800598
599 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
600 * so allow it to be in BR_STATE_FORWARDING to be kept functional
601 */
Vivien Didelotc5d35cb2017-02-03 13:20:19 -0500602 dsa_slave_set_state(dev, BR_STATE_FORWARDING);
Florian Fainellib73adef2015-02-24 13:15:33 -0800603}
604
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700605static int dsa_slave_port_attr_get(struct net_device *dev,
606 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800607{
608 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500609 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800610
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700611 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200612 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700613 attr->u.ppid.id_len = sizeof(ds->index);
614 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700615 break;
616 default:
617 return -EOPNOTSUPP;
618 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800619
620 return 0;
621}
622
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700623static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
624 struct sk_buff *skb)
625{
626#ifdef CONFIG_NET_POLL_CONTROLLER
627 if (p->netpoll)
628 netpoll_send_skb(p->netpoll, skb);
629#else
630 BUG();
631#endif
632 return NETDEV_TX_OK;
633}
634
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700635static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
636{
637 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700638 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700639
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700640 dev->stats.tx_packets++;
641 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700642
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700643 /* Transmit function may have to reallocate the original SKB */
644 nskb = p->xmit(skb, dev);
645 if (!nskb)
646 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700647
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700648 /* SKB for netpoll still need to be mangled with the protocol-specific
649 * tag to be successfully transmitted
650 */
651 if (unlikely(netpoll_tx_running(dev)))
652 return dsa_netpoll_send_skb(p, nskb);
653
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700654 /* Queue the SKB for transmission on the parent interface, but
655 * do not modify its EtherType
656 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500657 nskb->dev = p->dp->ds->dst->master_netdev;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700658 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700659
660 return NETDEV_TX_OK;
661}
662
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000663/* ethtool operations *******************************************************/
664static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200665dsa_slave_get_link_ksettings(struct net_device *dev,
666 struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000667{
668 struct dsa_slave_priv *p = netdev_priv(dev);
669 int err;
670
671 err = -EOPNOTSUPP;
672 if (p->phy != NULL) {
673 err = phy_read_status(p->phy);
674 if (err == 0)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200675 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000676 }
677
678 return err;
679}
680
681static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200682dsa_slave_set_link_ksettings(struct net_device *dev,
683 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000684{
685 struct dsa_slave_priv *p = netdev_priv(dev);
686
687 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200688 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000689
690 return -EOPNOTSUPP;
691}
692
693static void dsa_slave_get_drvinfo(struct net_device *dev,
694 struct ethtool_drvinfo *drvinfo)
695{
Jiri Pirko7826d432013-01-06 00:44:26 +0000696 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000697 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
698 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000699}
700
Guenter Roeck3d762a02014-10-29 10:45:04 -0700701static int dsa_slave_get_regs_len(struct net_device *dev)
702{
703 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500704 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700705
Vivien Didelot9d490b42016-08-23 12:38:56 -0400706 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500707 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700708
709 return -EOPNOTSUPP;
710}
711
712static void
713dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
714{
715 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500716 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700717
Vivien Didelot9d490b42016-08-23 12:38:56 -0400718 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500719 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700720}
721
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000722static int dsa_slave_nway_reset(struct net_device *dev)
723{
724 struct dsa_slave_priv *p = netdev_priv(dev);
725
726 if (p->phy != NULL)
727 return genphy_restart_aneg(p->phy);
728
729 return -EOPNOTSUPP;
730}
731
732static u32 dsa_slave_get_link(struct net_device *dev)
733{
734 struct dsa_slave_priv *p = netdev_priv(dev);
735
736 if (p->phy != NULL) {
737 genphy_update_link(p->phy);
738 return p->phy->link;
739 }
740
741 return -EOPNOTSUPP;
742}
743
Guenter Roeck6793abb2014-10-29 10:45:01 -0700744static int dsa_slave_get_eeprom_len(struct net_device *dev)
745{
746 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500747 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700748
Andrew Lunn0e576042016-06-04 21:16:52 +0200749 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200750 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700751
Vivien Didelot9d490b42016-08-23 12:38:56 -0400752 if (ds->ops->get_eeprom_len)
753 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700754
755 return 0;
756}
757
758static int dsa_slave_get_eeprom(struct net_device *dev,
759 struct ethtool_eeprom *eeprom, u8 *data)
760{
761 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500762 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700763
Vivien Didelot9d490b42016-08-23 12:38:56 -0400764 if (ds->ops->get_eeprom)
765 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700766
767 return -EOPNOTSUPP;
768}
769
770static int dsa_slave_set_eeprom(struct net_device *dev,
771 struct ethtool_eeprom *eeprom, u8 *data)
772{
773 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500774 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700775
Vivien Didelot9d490b42016-08-23 12:38:56 -0400776 if (ds->ops->set_eeprom)
777 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700778
779 return -EOPNOTSUPP;
780}
781
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000782static void dsa_slave_get_strings(struct net_device *dev,
783 uint32_t stringset, uint8_t *data)
784{
785 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500786 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000787
788 if (stringset == ETH_SS_STATS) {
789 int len = ETH_GSTRING_LEN;
790
791 strncpy(data, "tx_packets", len);
792 strncpy(data + len, "tx_bytes", len);
793 strncpy(data + 2 * len, "rx_packets", len);
794 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400795 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500796 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000797 }
798}
799
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700800static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
801 struct ethtool_stats *stats,
802 uint64_t *data)
803{
804 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500805 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700806 s8 cpu_port = dst->cpu_port;
807 int count = 0;
808
809 if (dst->master_ethtool_ops.get_sset_count) {
810 count = dst->master_ethtool_ops.get_sset_count(dev,
811 ETH_SS_STATS);
812 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
813 }
814
Vivien Didelot9d490b42016-08-23 12:38:56 -0400815 if (ds->ops->get_ethtool_stats)
816 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700817}
818
819static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
820{
821 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500822 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700823 int count = 0;
824
825 if (dst->master_ethtool_ops.get_sset_count)
826 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
827
Vivien Didelot9d490b42016-08-23 12:38:56 -0400828 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
829 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700830
831 return count;
832}
833
834static void dsa_cpu_port_get_strings(struct net_device *dev,
835 uint32_t stringset, uint8_t *data)
836{
837 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500838 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700839 s8 cpu_port = dst->cpu_port;
840 int len = ETH_GSTRING_LEN;
841 int mcount = 0, count;
842 unsigned int i;
843 uint8_t pfx[4];
844 uint8_t *ndata;
845
846 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
847 /* We do not want to be NULL-terminated, since this is a prefix */
848 pfx[sizeof(pfx) - 1] = '_';
849
850 if (dst->master_ethtool_ops.get_sset_count) {
851 mcount = dst->master_ethtool_ops.get_sset_count(dev,
852 ETH_SS_STATS);
853 dst->master_ethtool_ops.get_strings(dev, stringset, data);
854 }
855
Vivien Didelot9d490b42016-08-23 12:38:56 -0400856 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700857 ndata = data + mcount * len;
858 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
859 * the output after to prepend our CPU port prefix we
860 * constructed earlier
861 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400862 ds->ops->get_strings(ds, cpu_port, ndata);
863 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700864 for (i = 0; i < count; i++) {
865 memmove(ndata + (i * len + sizeof(pfx)),
866 ndata + i * len, len - sizeof(pfx));
867 memcpy(ndata + i * len, pfx, sizeof(pfx));
868 }
869 }
870}
871
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000872static void dsa_slave_get_ethtool_stats(struct net_device *dev,
873 struct ethtool_stats *stats,
874 uint64_t *data)
875{
876 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500877 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000878
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400879 data[0] = dev->stats.tx_packets;
880 data[1] = dev->stats.tx_bytes;
881 data[2] = dev->stats.rx_packets;
882 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400883 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500884 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000885}
886
887static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
888{
889 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500890 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000891
892 if (sset == ETH_SS_STATS) {
893 int count;
894
895 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400896 if (ds->ops->get_sset_count)
897 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000898
899 return count;
900 }
901
902 return -EOPNOTSUPP;
903}
904
Florian Fainelli19e57c42014-09-18 17:31:24 -0700905static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
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;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700909
Vivien Didelot9d490b42016-08-23 12:38:56 -0400910 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500911 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700912}
913
914static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
915{
916 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500917 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700918 int ret = -EOPNOTSUPP;
919
Vivien Didelot9d490b42016-08-23 12:38:56 -0400920 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500921 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700922
923 return ret;
924}
925
Florian Fainelli79052882014-09-24 17:05:21 -0700926static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
927{
928 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500929 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700930 int ret;
931
Vivien Didelot9d490b42016-08-23 12:38:56 -0400932 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700933 return -EOPNOTSUPP;
934
Vivien Didelotafdcf152017-01-27 15:29:39 -0500935 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700936 if (ret)
937 return ret;
938
939 if (p->phy)
940 ret = phy_ethtool_set_eee(p->phy, e);
941
942 return ret;
943}
944
945static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
946{
947 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500948 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700949 int ret;
950
Vivien Didelot9d490b42016-08-23 12:38:56 -0400951 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700952 return -EOPNOTSUPP;
953
Vivien Didelotafdcf152017-01-27 15:29:39 -0500954 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700955 if (ret)
956 return ret;
957
958 if (p->phy)
959 ret = phy_ethtool_get_eee(p->phy, e);
960
961 return ret;
962}
963
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700964#ifdef CONFIG_NET_POLL_CONTROLLER
965static int dsa_slave_netpoll_setup(struct net_device *dev,
966 struct netpoll_info *ni)
967{
968 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500969 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700970 struct net_device *master = ds->dst->master_netdev;
971 struct netpoll *netpoll;
972 int err = 0;
973
974 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
975 if (!netpoll)
976 return -ENOMEM;
977
978 err = __netpoll_setup(netpoll, master);
979 if (err) {
980 kfree(netpoll);
981 goto out;
982 }
983
984 p->netpoll = netpoll;
985out:
986 return err;
987}
988
989static void dsa_slave_netpoll_cleanup(struct net_device *dev)
990{
991 struct dsa_slave_priv *p = netdev_priv(dev);
992 struct netpoll *netpoll = p->netpoll;
993
994 if (!netpoll)
995 return;
996
997 p->netpoll = NULL;
998
999 __netpoll_free_async(netpoll);
1000}
1001
1002static void dsa_slave_poll_controller(struct net_device *dev)
1003{
1004}
1005#endif
1006
Florian Fainelli44bb7652017-01-10 12:32:36 -08001007static int dsa_slave_get_phys_port_name(struct net_device *dev,
1008 char *name, size_t len)
1009{
1010 struct dsa_slave_priv *p = netdev_priv(dev);
1011
Vivien Didelotafdcf152017-01-27 15:29:39 -05001012 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -08001013 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -08001014
1015 return 0;
1016}
1017
Florian Fainellif50f2122017-01-30 12:41:40 -08001018static struct dsa_mall_tc_entry *
1019dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1020 unsigned long cookie)
1021{
1022 struct dsa_mall_tc_entry *mall_tc_entry;
1023
1024 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1025 if (mall_tc_entry->cookie == cookie)
1026 return mall_tc_entry;
1027
1028 return NULL;
1029}
1030
1031static int dsa_slave_add_cls_matchall(struct net_device *dev,
1032 __be16 protocol,
1033 struct tc_cls_matchall_offload *cls,
1034 bool ingress)
1035{
1036 struct dsa_slave_priv *p = netdev_priv(dev);
1037 struct dsa_mall_tc_entry *mall_tc_entry;
1038 struct dsa_switch *ds = p->dp->ds;
1039 struct net *net = dev_net(dev);
1040 struct dsa_slave_priv *to_p;
1041 struct net_device *to_dev;
1042 const struct tc_action *a;
1043 int err = -EOPNOTSUPP;
1044 LIST_HEAD(actions);
1045 int ifindex;
1046
1047 if (!ds->ops->port_mirror_add)
1048 return err;
1049
1050 if (!tc_single_action(cls->exts))
1051 return err;
1052
1053 tcf_exts_to_list(cls->exts, &actions);
1054 a = list_first_entry(&actions, struct tc_action, list);
1055
1056 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1057 struct dsa_mall_mirror_tc_entry *mirror;
1058
1059 ifindex = tcf_mirred_ifindex(a);
1060 to_dev = __dev_get_by_index(net, ifindex);
1061 if (!to_dev)
1062 return -EINVAL;
1063
1064 if (!dsa_slave_dev_check(to_dev))
1065 return -EOPNOTSUPP;
1066
1067 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1068 if (!mall_tc_entry)
1069 return -ENOMEM;
1070
1071 mall_tc_entry->cookie = cls->cookie;
1072 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1073 mirror = &mall_tc_entry->mirror;
1074
1075 to_p = netdev_priv(to_dev);
1076
1077 mirror->to_local_port = to_p->dp->index;
1078 mirror->ingress = ingress;
1079
1080 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1081 ingress);
1082 if (err) {
1083 kfree(mall_tc_entry);
1084 return err;
1085 }
1086
1087 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1088 }
1089
1090 return 0;
1091}
1092
1093static void dsa_slave_del_cls_matchall(struct net_device *dev,
1094 struct tc_cls_matchall_offload *cls)
1095{
1096 struct dsa_slave_priv *p = netdev_priv(dev);
1097 struct dsa_mall_tc_entry *mall_tc_entry;
1098 struct dsa_switch *ds = p->dp->ds;
1099
1100 if (!ds->ops->port_mirror_del)
1101 return;
1102
1103 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1104 if (!mall_tc_entry)
1105 return;
1106
1107 list_del(&mall_tc_entry->list);
1108
1109 switch (mall_tc_entry->type) {
1110 case DSA_PORT_MALL_MIRROR:
1111 ds->ops->port_mirror_del(ds, p->dp->index,
1112 &mall_tc_entry->mirror);
1113 break;
1114 default:
1115 WARN_ON(1);
1116 }
1117
1118 kfree(mall_tc_entry);
1119}
1120
1121static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1122 __be16 protocol, struct tc_to_netdev *tc)
1123{
1124 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1125 int ret = -EOPNOTSUPP;
1126
1127 switch (tc->type) {
1128 case TC_SETUP_MATCHALL:
1129 switch (tc->cls_mall->command) {
1130 case TC_CLSMATCHALL_REPLACE:
1131 return dsa_slave_add_cls_matchall(dev, protocol,
1132 tc->cls_mall,
1133 ingress);
1134 case TC_CLSMATCHALL_DESTROY:
1135 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1136 return 0;
1137 }
1138 default:
1139 break;
1140 }
1141
1142 return ret;
1143}
1144
Florian Fainelliaf421922016-06-07 16:32:41 -07001145void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1146{
1147 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1148 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1149 ops->get_strings = dsa_cpu_port_get_strings;
1150}
1151
Florian Fainellibf9f2642017-01-30 09:48:40 -08001152static int dsa_slave_get_rxnfc(struct net_device *dev,
1153 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1154{
1155 struct dsa_slave_priv *p = netdev_priv(dev);
1156 struct dsa_switch *ds = p->dp->ds;
1157
1158 if (!ds->ops->get_rxnfc)
1159 return -EOPNOTSUPP;
1160
1161 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1162}
1163
1164static int dsa_slave_set_rxnfc(struct net_device *dev,
1165 struct ethtool_rxnfc *nfc)
1166{
1167 struct dsa_slave_priv *p = netdev_priv(dev);
1168 struct dsa_switch *ds = p->dp->ds;
1169
1170 if (!ds->ops->set_rxnfc)
1171 return -EOPNOTSUPP;
1172
1173 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1174}
1175
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001176static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001177 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001178 .get_regs_len = dsa_slave_get_regs_len,
1179 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001180 .nway_reset = dsa_slave_nway_reset,
1181 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001182 .get_eeprom_len = dsa_slave_get_eeprom_len,
1183 .get_eeprom = dsa_slave_get_eeprom,
1184 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001185 .get_strings = dsa_slave_get_strings,
1186 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1187 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001188 .set_wol = dsa_slave_set_wol,
1189 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001190 .set_eee = dsa_slave_set_eee,
1191 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +02001192 .get_link_ksettings = dsa_slave_get_link_ksettings,
1193 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001194 .get_rxnfc = dsa_slave_get_rxnfc,
1195 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001196};
1197
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001198static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001199 .ndo_open = dsa_slave_open,
1200 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001201 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001202 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1203 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001204 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001205 .ndo_fdb_add = switchdev_port_fdb_add,
1206 .ndo_fdb_del = switchdev_port_fdb_del,
1207 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001208 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001209 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001210#ifdef CONFIG_NET_POLL_CONTROLLER
1211 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1212 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1213 .ndo_poll_controller = dsa_slave_poll_controller,
1214#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001215 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1216 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1217 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -08001218 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001219 .ndo_setup_tc = dsa_slave_setup_tc,
Scott Feldman98237d42015-03-15 21:07:15 -07001220};
1221
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001222static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001223 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001224 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001225 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1226 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1227 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001228};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001229
Florian Fainellif37db852015-09-23 18:19:58 -07001230static struct device_type dsa_type = {
1231 .name = "dsa",
1232};
1233
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001234static void dsa_slave_adjust_link(struct net_device *dev)
1235{
1236 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001237 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001238 unsigned int status_changed = 0;
1239
1240 if (p->old_link != p->phy->link) {
1241 status_changed = 1;
1242 p->old_link = p->phy->link;
1243 }
1244
1245 if (p->old_duplex != p->phy->duplex) {
1246 status_changed = 1;
1247 p->old_duplex = p->phy->duplex;
1248 }
1249
1250 if (p->old_pause != p->phy->pause) {
1251 status_changed = 1;
1252 p->old_pause = p->phy->pause;
1253 }
1254
Vivien Didelot9d490b42016-08-23 12:38:56 -04001255 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001256 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001257
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001258 if (status_changed)
1259 phy_print_status(p->phy);
1260}
1261
Florian Fainellice31b312014-08-27 17:04:54 -07001262static int dsa_slave_fixed_link_update(struct net_device *dev,
1263 struct fixed_phy_status *status)
1264{
Andrew Lunnb71be352016-03-12 00:01:37 +01001265 struct dsa_slave_priv *p;
1266 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001267
Andrew Lunnb71be352016-03-12 00:01:37 +01001268 if (dev) {
1269 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001270 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001271 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001272 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001273 }
Florian Fainellice31b312014-08-27 17:04:54 -07001274
1275 return 0;
1276}
1277
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001278/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001279static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001280 struct net_device *slave_dev,
1281 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001282{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001283 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001284
Andrew Lunn7f854422016-01-06 20:11:18 +01001285 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001286 if (!p->phy) {
1287 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001288 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001289 }
Florian Fainellic305c162015-03-10 16:57:12 -07001290
1291 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001292 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1293 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001294 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1295 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001296}
1297
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001298static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001299 struct net_device *slave_dev)
1300{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001301 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001302 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001303 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001304 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001305 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001306
Vivien Didelotafdcf152017-01-27 15:29:39 -05001307 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001308 mode = of_get_phy_mode(port_dn);
1309 if (mode < 0)
1310 mode = PHY_INTERFACE_MODE_NA;
1311 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001312
1313 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001314 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001315 /* In the case of a fixed PHY, the DT node associated
1316 * to the fixed PHY is the Port DT node
1317 */
1318 ret = of_phy_register_fixed_link(port_dn);
1319 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001320 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001321 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001322 }
Florian Fainellice31b312014-08-27 17:04:54 -07001323 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001324 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001325 }
1326
Vivien Didelot9d490b42016-08-23 12:38:56 -04001327 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001328 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001329
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001330 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001331 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1332
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001333 /* If this PHY address is part of phys_mii_mask, which means
1334 * that we need to divert reads and writes to/from it, then we
1335 * want to bind this device using the slave MII bus created by
1336 * DSA to make that happen.
1337 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001338 if (!phy_is_fixed && phy_id >= 0 &&
1339 (ds->phys_mii_mask & (1 << phy_id))) {
1340 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1341 if (ret) {
1342 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001343 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001344 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001345 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001346 } else {
1347 p->phy = of_phy_connect(slave_dev, phy_dn,
1348 dsa_slave_adjust_link,
1349 phy_flags,
1350 p->phy_interface);
1351 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001352
1353 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001354 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001355
Florian Fainellice31b312014-08-27 17:04:54 -07001356 if (p->phy && phy_is_fixed)
1357 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1358
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001359 /* We could not connect to a designated PHY, so use the switch internal
1360 * MDIO bus instead
1361 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001362 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001363 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001364 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001365 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1366 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001367 if (phy_is_fixed)
1368 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001369 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001370 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001371 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001372
Andrew Lunn22209432016-01-06 20:11:13 +01001373 phy_attached_info(p->phy);
1374
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001375 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001376}
1377
Andrew Lunn448b4482015-05-06 01:09:56 +02001378static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1379static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1380 struct netdev_queue *txq,
1381 void *_unused)
1382{
1383 lockdep_set_class(&txq->_xmit_lock,
1384 &dsa_slave_netdev_xmit_lock_key);
1385}
1386
Florian Fainelli24462542014-09-18 17:31:22 -07001387int dsa_slave_suspend(struct net_device *slave_dev)
1388{
1389 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1390
Florian Fainellif154be22017-01-25 09:10:41 -08001391 netif_device_detach(slave_dev);
1392
Florian Fainelli24462542014-09-18 17:31:22 -07001393 if (p->phy) {
1394 phy_stop(p->phy);
1395 p->old_pause = -1;
1396 p->old_link = -1;
1397 p->old_duplex = -1;
1398 phy_suspend(p->phy);
1399 }
1400
1401 return 0;
1402}
1403
1404int dsa_slave_resume(struct net_device *slave_dev)
1405{
1406 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1407
1408 netif_device_attach(slave_dev);
1409
1410 if (p->phy) {
1411 phy_resume(p->phy);
1412 phy_start(p->phy);
1413 }
1414
1415 return 0;
1416}
1417
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001418int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001419 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001420{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001421 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001422 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001423 struct net_device *slave_dev;
1424 struct dsa_slave_priv *p;
1425 int ret;
1426
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001427 master = ds->dst->master_netdev;
1428 if (ds->master_netdev)
1429 master = ds->master_netdev;
1430
Tom Gundersenc835a672014-07-14 16:37:24 +02001431 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1432 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001433 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001434 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001435
Florian Fainellif50f2122017-01-30 12:41:40 -08001436 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1437 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001438 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001439 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001440 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001441 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001442 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001443 slave_dev->min_mtu = 0;
1444 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001445 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001446
Andrew Lunn448b4482015-05-06 01:09:56 +02001447 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1448 NULL);
1449
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001450 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001451 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001452 slave_dev->vlan_features = master->vlan_features;
1453
1454 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001455 p->dp = &ds->ports[port];
Florian Fainellif50f2122017-01-30 12:41:40 -08001456 INIT_LIST_HEAD(&p->mall_tc_list);
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001457 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001458
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001459 p->old_pause = -1;
1460 p->old_link = -1;
1461 p->old_duplex = -1;
1462
Andrew Lunnc8b09802016-06-04 21:16:57 +02001463 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001464 ret = register_netdev(slave_dev);
1465 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001466 netdev_err(master, "error %d registering interface %s\n",
1467 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001468 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001469 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001470 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001471 }
1472
1473 netif_carrier_off(slave_dev);
1474
Andrew Lunn0071f562016-01-06 20:11:20 +01001475 ret = dsa_slave_phy_setup(p, slave_dev);
1476 if (ret) {
1477 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001478 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001479 free_netdev(slave_dev);
1480 return ret;
1481 }
1482
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001483 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001484}
Florian Fainellib73adef2015-02-24 13:15:33 -08001485
Neil Armstrongcda5c152015-12-07 13:57:35 +01001486void dsa_slave_destroy(struct net_device *slave_dev)
1487{
1488 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001489 struct device_node *port_dn;
1490
Vivien Didelotafdcf152017-01-27 15:29:39 -05001491 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001492
1493 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001494 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001495 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001496
1497 if (of_phy_is_fixed_link(port_dn))
1498 of_phy_deregister_fixed_link(port_dn);
1499 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001500 unregister_netdev(slave_dev);
1501 free_netdev(slave_dev);
1502}
1503
Florian Fainellib73adef2015-02-24 13:15:33 -08001504static bool dsa_slave_dev_check(struct net_device *dev)
1505{
1506 return dev->netdev_ops == &dsa_slave_netdev_ops;
1507}
1508
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001509static int dsa_slave_changeupper(struct net_device *dev,
1510 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001511{
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001512 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001513
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001514 if (netif_is_bridge_master(info->upper_dev)) {
1515 if (info->linking) {
1516 err = dsa_slave_bridge_port_join(dev, info->upper_dev);
1517 err = notifier_from_errno(err);
1518 } else {
1519 dsa_slave_bridge_port_leave(dev, info->upper_dev);
1520 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001521 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001522 }
1523
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001524 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001525}
1526
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001527static int dsa_slave_netdevice_event(struct notifier_block *nb,
1528 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001529{
Vivien Didelot6debb682016-03-13 16:21:34 -04001530 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001531
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001532 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1533 return NOTIFY_DONE;
1534
1535 if (event == NETDEV_CHANGEUPPER)
1536 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001537
Florian Fainellib73adef2015-02-24 13:15:33 -08001538 return NOTIFY_DONE;
1539}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001540
1541static struct notifier_block dsa_slave_nb __read_mostly = {
1542 .notifier_call = dsa_slave_netdevice_event,
1543};
1544
1545int dsa_slave_register_notifier(void)
1546{
1547 return register_netdevice_notifier(&dsa_slave_nb);
1548}
1549
1550void dsa_slave_unregister_notifier(void)
1551{
1552 int err;
1553
1554 err = unregister_netdevice_notifier(&dsa_slave_nb);
1555 if (err)
1556 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1557}