blob: 6881889e1a9bc3ef6a02d7dc3086a370522ff03b [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 Fainellib73adef2015-02-24 13:15:33 -080019#include <net/rtnetlink.h>
Scott Feldman98237d42015-03-15 21:07:15 -070020#include <net/switchdev.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080021#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070022#include <linux/netpoll.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000023#include "dsa_priv.h"
24
25/* slave mii_bus handling ***************************************************/
26static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
27{
28 struct dsa_switch *ds = bus->priv;
29
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070030 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040031 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000032
33 return 0xffff;
34}
35
36static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
37{
38 struct dsa_switch *ds = bus->priv;
39
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070040 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040041 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000042
43 return 0;
44}
45
46void dsa_slave_mii_bus_init(struct dsa_switch *ds)
47{
48 ds->slave_mii_bus->priv = (void *)ds;
49 ds->slave_mii_bus->name = "dsa slave smi";
50 ds->slave_mii_bus->read = dsa_slave_phy_read;
51 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070052 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
53 ds->dst->tree, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020054 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050055 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000056}
57
58
59/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020060static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000061{
62 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000063
Vivien Didelotafdcf152017-01-27 15:29:39 -050064 return p->dp->ds->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000065}
66
Vivien Didelota5e9a022017-01-27 15:29:40 -050067static inline bool dsa_port_is_bridged(struct dsa_port *dp)
Florian Fainellib73adef2015-02-24 13:15:33 -080068{
Vivien Didelota5e9a022017-01-27 15:29:40 -050069 return !!dp->bridge_dev;
Florian Fainellib73adef2015-02-24 13:15:33 -080070}
71
Vivien Didelot4acfee82016-09-22 16:49:21 -040072static void dsa_port_set_stp_state(struct dsa_switch *ds, int port, u8 state)
73{
Vivien Didelot732f7942016-09-22 16:49:22 -040074 struct dsa_port *dp = &ds->ports[port];
75
Vivien Didelot4acfee82016-09-22 16:49:21 -040076 if (ds->ops->port_stp_state_set)
77 ds->ops->port_stp_state_set(ds, port, state);
Vivien Didelot732f7942016-09-22 16:49:22 -040078
79 if (ds->ops->port_fast_age) {
80 /* Fast age FDB entries or flush appropriate forwarding database
81 * for the given port, if we are moving it from Learning or
82 * Forwarding state, to Disabled or Blocking or Listening state.
83 */
84
85 if ((dp->stp_state == BR_STATE_LEARNING ||
86 dp->stp_state == BR_STATE_FORWARDING) &&
87 (state == BR_STATE_DISABLED ||
88 state == BR_STATE_BLOCKING ||
89 state == BR_STATE_LISTENING))
90 ds->ops->port_fast_age(ds, port);
91 }
92
93 dp->stp_state = state;
Vivien Didelot4acfee82016-09-22 16:49:21 -040094}
95
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000096static int dsa_slave_open(struct net_device *dev)
97{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080098 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -050099 struct net_device *master = p->dp->ds->dst->master_netdev;
100 struct dsa_switch *ds = p->dp->ds;
Vivien Didelota5e9a022017-01-27 15:29:40 -0500101 u8 stp_state = dsa_port_is_bridged(p->dp) ?
Florian Fainellib73adef2015-02-24 13:15:33 -0800102 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800103 int err;
104
105 if (!(master->flags & IFF_UP))
106 return -ENETDOWN;
107
Joe Perches8feedbb2012-05-08 18:56:57 +0000108 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000109 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800110 if (err < 0)
111 goto out;
112 }
113
114 if (dev->flags & IFF_ALLMULTI) {
115 err = dev_set_allmulti(master, 1);
116 if (err < 0)
117 goto del_unicast;
118 }
119 if (dev->flags & IFF_PROMISC) {
120 err = dev_set_promiscuity(master, 1);
121 if (err < 0)
122 goto clear_allmulti;
123 }
124
Vivien Didelot9d490b42016-08-23 12:38:56 -0400125 if (ds->ops->port_enable) {
Vivien Didelotafdcf152017-01-27 15:29:39 -0500126 err = ds->ops->port_enable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700127 if (err)
128 goto clear_promisc;
129 }
130
Vivien Didelotafdcf152017-01-27 15:29:39 -0500131 dsa_port_set_stp_state(ds, p->dp->index, stp_state);
Florian Fainellib73adef2015-02-24 13:15:33 -0800132
Florian Fainellif7f1de52014-09-24 17:05:17 -0700133 if (p->phy)
134 phy_start(p->phy);
135
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000136 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800137
Florian Fainellib2f2af22014-09-24 17:05:18 -0700138clear_promisc:
139 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300140 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800141clear_allmulti:
142 if (dev->flags & IFF_ALLMULTI)
143 dev_set_allmulti(master, -1);
144del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000145 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000146 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800147out:
148 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000149}
150
151static int dsa_slave_close(struct net_device *dev)
152{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800153 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500154 struct net_device *master = p->dp->ds->dst->master_netdev;
155 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800156
Florian Fainellif7f1de52014-09-24 17:05:17 -0700157 if (p->phy)
158 phy_stop(p->phy);
159
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800160 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000161 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800162 if (dev->flags & IFF_ALLMULTI)
163 dev_set_allmulti(master, -1);
164 if (dev->flags & IFF_PROMISC)
165 dev_set_promiscuity(master, -1);
166
Joe Perches8feedbb2012-05-08 18:56:57 +0000167 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000168 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800169
Vivien Didelot9d490b42016-08-23 12:38:56 -0400170 if (ds->ops->port_disable)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500171 ds->ops->port_disable(ds, p->dp->index, p->phy);
Florian Fainellib2f2af22014-09-24 17:05:18 -0700172
Vivien Didelotafdcf152017-01-27 15:29:39 -0500173 dsa_port_set_stp_state(ds, p->dp->index, BR_STATE_DISABLED);
Florian Fainellib73adef2015-02-24 13:15:33 -0800174
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000175 return 0;
176}
177
178static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
179{
180 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500181 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000182
183 if (change & IFF_ALLMULTI)
184 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
185 if (change & IFF_PROMISC)
186 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
187}
188
189static void dsa_slave_set_rx_mode(struct net_device *dev)
190{
191 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500192 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000193
194 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000195 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000196}
197
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800198static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000199{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800200 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500201 struct net_device *master = p->dp->ds->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800202 struct sockaddr *addr = a;
203 int err;
204
205 if (!is_valid_ether_addr(addr->sa_data))
206 return -EADDRNOTAVAIL;
207
208 if (!(dev->flags & IFF_UP))
209 goto out;
210
Joe Perches8feedbb2012-05-08 18:56:57 +0000211 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000212 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800213 if (err < 0)
214 return err;
215 }
216
Joe Perches8feedbb2012-05-08 18:56:57 +0000217 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000218 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800219
220out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800221 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000222
223 return 0;
224}
225
Vivien Didelot11149532015-08-13 12:52:17 -0400226static int dsa_slave_port_vlan_add(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200227 const struct switchdev_obj_port_vlan *vlan,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200228 struct switchdev_trans *trans)
Vivien Didelot11149532015-08-13 12:52:17 -0400229{
Vivien Didelot11149532015-08-13 12:52:17 -0400230 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500231 struct dsa_port *dp = p->dp;
232 struct dsa_switch *ds = dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400233
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200234 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400235 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
Vivien Didelot11149532015-08-13 12:52:17 -0400236 return -EOPNOTSUPP;
237
Vivien Didelotafdcf152017-01-27 15:29:39 -0500238 return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400239 }
240
Vivien Didelotafdcf152017-01-27 15:29:39 -0500241 ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
Vivien Didelot4d5770b2016-04-06 11:55:05 -0400242
Vivien Didelot11149532015-08-13 12:52:17 -0400243 return 0;
244}
245
246static int dsa_slave_port_vlan_del(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200247 const struct switchdev_obj_port_vlan *vlan)
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_switch *ds = p->dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400251
Vivien Didelot9d490b42016-08-23 12:38:56 -0400252 if (!ds->ops->port_vlan_del)
Vivien Didelot11149532015-08-13 12:52:17 -0400253 return -EOPNOTSUPP;
254
Vivien Didelotafdcf152017-01-27 15:29:39 -0500255 return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
Vivien Didelot11149532015-08-13 12:52:17 -0400256}
257
258static int dsa_slave_port_vlan_dump(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200259 struct switchdev_obj_port_vlan *vlan,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200260 switchdev_obj_dump_cb_t *cb)
Vivien Didelot11149532015-08-13 12:52:17 -0400261{
Vivien Didelot11149532015-08-13 12:52:17 -0400262 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500263 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot11149532015-08-13 12:52:17 -0400264
Vivien Didelot9d490b42016-08-23 12:38:56 -0400265 if (ds->ops->port_vlan_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500266 return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
Vivien Didelot65aebfc2016-02-23 12:13:54 -0500267
Vivien Didelot477b1842016-02-23 12:13:56 -0500268 return -EOPNOTSUPP;
Vivien Didelot11149532015-08-13 12:52:17 -0400269}
270
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400271static int dsa_slave_port_fdb_add(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200272 const struct switchdev_obj_port_fdb *fdb,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200273 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700274{
275 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500276 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot146a3202015-10-08 11:35:12 -0400277
Vivien Didelot8497aa62016-04-06 11:55:04 -0400278 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400279 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
Vivien Didelot8497aa62016-04-06 11:55:04 -0400280 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700281
Vivien Didelotafdcf152017-01-27 15:29:39 -0500282 return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400283 }
David S. Millercdf09692015-08-11 12:00:37 -0700284
Vivien Didelotafdcf152017-01-27 15:29:39 -0500285 ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans);
Vivien Didelot8497aa62016-04-06 11:55:04 -0400286
287 return 0;
David S. Millercdf09692015-08-11 12:00:37 -0700288}
289
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400290static int dsa_slave_port_fdb_del(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200291 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -0700292{
293 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500294 struct dsa_switch *ds = p->dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700295 int ret = -EOPNOTSUPP;
296
Vivien Didelot9d490b42016-08-23 12:38:56 -0400297 if (ds->ops->port_fdb_del)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500298 ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700299
300 return ret;
301}
302
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400303static int dsa_slave_port_fdb_dump(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200304 struct switchdev_obj_port_fdb *fdb,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200305 switchdev_obj_dump_cb_t *cb)
David S. Millercdf09692015-08-11 12:00:37 -0700306{
307 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500308 struct dsa_switch *ds = p->dp->ds;
David S. Millercdf09692015-08-11 12:00:37 -0700309
Vivien Didelot9d490b42016-08-23 12:38:56 -0400310 if (ds->ops->port_fdb_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500311 return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb);
Vivien Didelotea70ba92015-10-22 09:34:38 -0400312
Vivien Didelot1a49a2f2015-10-22 09:34:43 -0400313 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700314}
315
Vivien Didelot8df30252016-08-31 11:50:03 -0400316static int dsa_slave_port_mdb_add(struct net_device *dev,
317 const struct switchdev_obj_port_mdb *mdb,
318 struct switchdev_trans *trans)
319{
320 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500321 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400322
323 if (switchdev_trans_ph_prepare(trans)) {
324 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
325 return -EOPNOTSUPP;
326
Vivien Didelotafdcf152017-01-27 15:29:39 -0500327 return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400328 }
329
Vivien Didelotafdcf152017-01-27 15:29:39 -0500330 ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400331
332 return 0;
333}
334
335static int dsa_slave_port_mdb_del(struct net_device *dev,
336 const struct switchdev_obj_port_mdb *mdb)
337{
338 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500339 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400340
341 if (ds->ops->port_mdb_del)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500342 return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400343
344 return -EOPNOTSUPP;
345}
346
347static int dsa_slave_port_mdb_dump(struct net_device *dev,
348 struct switchdev_obj_port_mdb *mdb,
349 switchdev_obj_dump_cb_t *cb)
350{
351 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500352 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot8df30252016-08-31 11:50:03 -0400353
354 if (ds->ops->port_mdb_dump)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500355 return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
Vivien Didelot8df30252016-08-31 11:50:03 -0400356
357 return -EOPNOTSUPP;
358}
359
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000360static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
361{
362 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000363
364 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000365 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000366
367 return -EOPNOTSUPP;
368}
369
Vivien Didelot43c44a92016-04-06 11:55:03 -0400370static int dsa_slave_stp_state_set(struct net_device *dev,
371 const struct switchdev_attr *attr,
372 struct switchdev_trans *trans)
Florian Fainellib73adef2015-02-24 13:15:33 -0800373{
374 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500375 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800376
Vivien Didelot43c44a92016-04-06 11:55:03 -0400377 if (switchdev_trans_ph_prepare(trans))
Vivien Didelot9d490b42016-08-23 12:38:56 -0400378 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
Florian Fainellib73adef2015-02-24 13:15:33 -0800379
Vivien Didelotafdcf152017-01-27 15:29:39 -0500380 dsa_port_set_stp_state(ds, p->dp->index, attr->u.stp_state);
Vivien Didelot43c44a92016-04-06 11:55:03 -0400381
382 return 0;
Florian Fainellib73adef2015-02-24 13:15:33 -0800383}
384
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500385static int dsa_slave_vlan_filtering(struct net_device *dev,
386 const struct switchdev_attr *attr,
387 struct switchdev_trans *trans)
388{
389 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500390 struct dsa_switch *ds = p->dp->ds;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500391
392 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
393 if (switchdev_trans_ph_prepare(trans))
394 return 0;
395
Vivien Didelot9d490b42016-08-23 12:38:56 -0400396 if (ds->ops->port_vlan_filtering)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500397 return ds->ops->port_vlan_filtering(ds, p->dp->index,
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500398 attr->u.vlan_filtering);
399
400 return 0;
401}
402
Vivien Didelot34a79f62016-07-18 20:45:38 -0400403static int dsa_fastest_ageing_time(struct dsa_switch *ds,
404 unsigned int ageing_time)
405{
406 int i;
407
Vivien Didelot26895e22017-01-27 15:29:37 -0500408 for (i = 0; i < ds->num_ports; ++i) {
Vivien Didelot34a79f62016-07-18 20:45:38 -0400409 struct dsa_port *dp = &ds->ports[i];
410
411 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
412 ageing_time = dp->ageing_time;
413 }
414
415 return ageing_time;
416}
417
418static int dsa_slave_ageing_time(struct net_device *dev,
419 const struct switchdev_attr *attr,
420 struct switchdev_trans *trans)
421{
422 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500423 struct dsa_switch *ds = p->dp->ds;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400424 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
425 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
426
427 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
428 if (switchdev_trans_ph_prepare(trans))
429 return 0;
430
431 /* Keep the fastest ageing time in case of multiple bridges */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500432 p->dp->ageing_time = ageing_time;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400433 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
434
Vivien Didelot9d490b42016-08-23 12:38:56 -0400435 if (ds->ops->set_ageing_time)
436 return ds->ops->set_ageing_time(ds, ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400437
438 return 0;
439}
440
Scott Feldman35636062015-05-10 09:47:51 -0700441static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200442 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200443 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700444{
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400445 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700446
447 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200448 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelot43c44a92016-04-06 11:55:03 -0400449 ret = dsa_slave_stp_state_set(dev, attr, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700450 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500451 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
452 ret = dsa_slave_vlan_filtering(dev, attr, trans);
453 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400454 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
455 ret = dsa_slave_ageing_time(dev, attr, trans);
456 break;
Scott Feldman35636062015-05-10 09:47:51 -0700457 default:
458 ret = -EOPNOTSUPP;
459 break;
460 }
461
462 return ret;
463}
464
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400465static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200466 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200467 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400468{
469 int err;
470
471 /* For the prepare phase, ensure the full set of changes is feasable in
472 * one go in order to signal a failure properly. If an operation is not
473 * supported, return -EOPNOTSUPP.
474 */
475
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200476 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200477 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200478 err = dsa_slave_port_fdb_add(dev,
479 SWITCHDEV_OBJ_PORT_FDB(obj),
480 trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400481 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400482 case SWITCHDEV_OBJ_ID_PORT_MDB:
483 err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
484 trans);
485 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200486 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200487 err = dsa_slave_port_vlan_add(dev,
488 SWITCHDEV_OBJ_PORT_VLAN(obj),
489 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400490 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400491 default:
492 err = -EOPNOTSUPP;
493 break;
494 }
495
496 return err;
497}
498
499static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200500 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400501{
502 int err;
503
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200504 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200505 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200506 err = dsa_slave_port_fdb_del(dev,
507 SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400508 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400509 case SWITCHDEV_OBJ_ID_PORT_MDB:
510 err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
511 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200512 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200513 err = dsa_slave_port_vlan_del(dev,
514 SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400515 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400516 default:
517 err = -EOPNOTSUPP;
518 break;
519 }
520
521 return err;
522}
523
524static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200525 struct switchdev_obj *obj,
526 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400527{
528 int err;
529
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200530 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200531 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200532 err = dsa_slave_port_fdb_dump(dev,
533 SWITCHDEV_OBJ_PORT_FDB(obj),
534 cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400535 break;
Vivien Didelot8df30252016-08-31 11:50:03 -0400536 case SWITCHDEV_OBJ_ID_PORT_MDB:
537 err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
538 cb);
539 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200540 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200541 err = dsa_slave_port_vlan_dump(dev,
542 SWITCHDEV_OBJ_PORT_VLAN(obj),
543 cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400544 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400545 default:
546 err = -EOPNOTSUPP;
547 break;
548 }
549
550 return err;
551}
552
Florian Fainellib73adef2015-02-24 13:15:33 -0800553static int dsa_slave_bridge_port_join(struct net_device *dev,
554 struct net_device *br)
555{
556 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500557 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800558 int ret = -EOPNOTSUPP;
559
Vivien Didelota5e9a022017-01-27 15:29:40 -0500560 p->dp->bridge_dev = br;
Florian Fainellib73adef2015-02-24 13:15:33 -0800561
Vivien Didelot9d490b42016-08-23 12:38:56 -0400562 if (ds->ops->port_bridge_join)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500563 ret = ds->ops->port_bridge_join(ds, p->dp->index, br);
Florian Fainellib73adef2015-02-24 13:15:33 -0800564
Vivien Didelot6debb682016-03-13 16:21:34 -0400565 return ret == -EOPNOTSUPP ? 0 : ret;
Florian Fainellib73adef2015-02-24 13:15:33 -0800566}
567
Vivien Didelotf123f2f2017-01-27 15:29:41 -0500568static void dsa_slave_bridge_port_leave(struct net_device *dev,
569 struct net_device *br)
Florian Fainellib73adef2015-02-24 13:15:33 -0800570{
571 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500572 struct dsa_switch *ds = p->dp->ds;
Florian Fainellib73adef2015-02-24 13:15:33 -0800573
Vivien Didelotf123f2f2017-01-27 15:29:41 -0500574 p->dp->bridge_dev = NULL;
Florian Fainellib73adef2015-02-24 13:15:33 -0800575
Vivien Didelot9d490b42016-08-23 12:38:56 -0400576 if (ds->ops->port_bridge_leave)
Vivien Didelotf123f2f2017-01-27 15:29:41 -0500577 ds->ops->port_bridge_leave(ds, p->dp->index, br);
Florian Fainellib73adef2015-02-24 13:15:33 -0800578
579 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
580 * so allow it to be in BR_STATE_FORWARDING to be kept functional
581 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500582 dsa_port_set_stp_state(ds, p->dp->index, BR_STATE_FORWARDING);
Florian Fainellib73adef2015-02-24 13:15:33 -0800583}
584
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700585static int dsa_slave_port_attr_get(struct net_device *dev,
586 struct switchdev_attr *attr)
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
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700591 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200592 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700593 attr->u.ppid.id_len = sizeof(ds->index);
594 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700595 break;
596 default:
597 return -EOPNOTSUPP;
598 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800599
600 return 0;
601}
602
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700603static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
604 struct sk_buff *skb)
605{
606#ifdef CONFIG_NET_POLL_CONTROLLER
607 if (p->netpoll)
608 netpoll_send_skb(p->netpoll, skb);
609#else
610 BUG();
611#endif
612 return NETDEV_TX_OK;
613}
614
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700615static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
616{
617 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700618 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700619
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700620 dev->stats.tx_packets++;
621 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700622
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700623 /* Transmit function may have to reallocate the original SKB */
624 nskb = p->xmit(skb, dev);
625 if (!nskb)
626 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700627
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700628 /* SKB for netpoll still need to be mangled with the protocol-specific
629 * tag to be successfully transmitted
630 */
631 if (unlikely(netpoll_tx_running(dev)))
632 return dsa_netpoll_send_skb(p, nskb);
633
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700634 /* Queue the SKB for transmission on the parent interface, but
635 * do not modify its EtherType
636 */
Vivien Didelotafdcf152017-01-27 15:29:39 -0500637 nskb->dev = p->dp->ds->dst->master_netdev;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700638 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700639
640 return NETDEV_TX_OK;
641}
642
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000643/* ethtool operations *******************************************************/
644static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200645dsa_slave_get_link_ksettings(struct net_device *dev,
646 struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000647{
648 struct dsa_slave_priv *p = netdev_priv(dev);
649 int err;
650
651 err = -EOPNOTSUPP;
652 if (p->phy != NULL) {
653 err = phy_read_status(p->phy);
654 if (err == 0)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200655 err = phy_ethtool_ksettings_get(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000656 }
657
658 return err;
659}
660
661static int
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200662dsa_slave_set_link_ksettings(struct net_device *dev,
663 const struct ethtool_link_ksettings *cmd)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000664{
665 struct dsa_slave_priv *p = netdev_priv(dev);
666
667 if (p->phy != NULL)
Philippe Reynesbb10bb32016-10-09 17:00:53 +0200668 return phy_ethtool_ksettings_set(p->phy, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000669
670 return -EOPNOTSUPP;
671}
672
673static void dsa_slave_get_drvinfo(struct net_device *dev,
674 struct ethtool_drvinfo *drvinfo)
675{
Jiri Pirko7826d432013-01-06 00:44:26 +0000676 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000677 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
678 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000679}
680
Guenter Roeck3d762a02014-10-29 10:45:04 -0700681static int dsa_slave_get_regs_len(struct net_device *dev)
682{
683 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500684 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700685
Vivien Didelot9d490b42016-08-23 12:38:56 -0400686 if (ds->ops->get_regs_len)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500687 return ds->ops->get_regs_len(ds, p->dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700688
689 return -EOPNOTSUPP;
690}
691
692static void
693dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
694{
695 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500696 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700697
Vivien Didelot9d490b42016-08-23 12:38:56 -0400698 if (ds->ops->get_regs)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500699 ds->ops->get_regs(ds, p->dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700700}
701
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000702static int dsa_slave_nway_reset(struct net_device *dev)
703{
704 struct dsa_slave_priv *p = netdev_priv(dev);
705
706 if (p->phy != NULL)
707 return genphy_restart_aneg(p->phy);
708
709 return -EOPNOTSUPP;
710}
711
712static u32 dsa_slave_get_link(struct net_device *dev)
713{
714 struct dsa_slave_priv *p = netdev_priv(dev);
715
716 if (p->phy != NULL) {
717 genphy_update_link(p->phy);
718 return p->phy->link;
719 }
720
721 return -EOPNOTSUPP;
722}
723
Guenter Roeck6793abb2014-10-29 10:45:01 -0700724static int dsa_slave_get_eeprom_len(struct net_device *dev)
725{
726 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500727 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700728
Andrew Lunn0e576042016-06-04 21:16:52 +0200729 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200730 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700731
Vivien Didelot9d490b42016-08-23 12:38:56 -0400732 if (ds->ops->get_eeprom_len)
733 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700734
735 return 0;
736}
737
738static int dsa_slave_get_eeprom(struct net_device *dev,
739 struct ethtool_eeprom *eeprom, u8 *data)
740{
741 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500742 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700743
Vivien Didelot9d490b42016-08-23 12:38:56 -0400744 if (ds->ops->get_eeprom)
745 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700746
747 return -EOPNOTSUPP;
748}
749
750static int dsa_slave_set_eeprom(struct net_device *dev,
751 struct ethtool_eeprom *eeprom, u8 *data)
752{
753 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500754 struct dsa_switch *ds = p->dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700755
Vivien Didelot9d490b42016-08-23 12:38:56 -0400756 if (ds->ops->set_eeprom)
757 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700758
759 return -EOPNOTSUPP;
760}
761
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000762static void dsa_slave_get_strings(struct net_device *dev,
763 uint32_t stringset, uint8_t *data)
764{
765 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500766 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000767
768 if (stringset == ETH_SS_STATS) {
769 int len = ETH_GSTRING_LEN;
770
771 strncpy(data, "tx_packets", len);
772 strncpy(data + len, "tx_bytes", len);
773 strncpy(data + 2 * len, "rx_packets", len);
774 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400775 if (ds->ops->get_strings)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500776 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000777 }
778}
779
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700780static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
781 struct ethtool_stats *stats,
782 uint64_t *data)
783{
784 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500785 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700786 s8 cpu_port = dst->cpu_port;
787 int count = 0;
788
789 if (dst->master_ethtool_ops.get_sset_count) {
790 count = dst->master_ethtool_ops.get_sset_count(dev,
791 ETH_SS_STATS);
792 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
793 }
794
Vivien Didelot9d490b42016-08-23 12:38:56 -0400795 if (ds->ops->get_ethtool_stats)
796 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700797}
798
799static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
800{
801 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500802 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700803 int count = 0;
804
805 if (dst->master_ethtool_ops.get_sset_count)
806 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
807
Vivien Didelot9d490b42016-08-23 12:38:56 -0400808 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
809 count += ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700810
811 return count;
812}
813
814static void dsa_cpu_port_get_strings(struct net_device *dev,
815 uint32_t stringset, uint8_t *data)
816{
817 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot9520ed82017-01-17 20:41:39 -0500818 struct dsa_switch *ds = dst->cpu_switch;
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700819 s8 cpu_port = dst->cpu_port;
820 int len = ETH_GSTRING_LEN;
821 int mcount = 0, count;
822 unsigned int i;
823 uint8_t pfx[4];
824 uint8_t *ndata;
825
826 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
827 /* We do not want to be NULL-terminated, since this is a prefix */
828 pfx[sizeof(pfx) - 1] = '_';
829
830 if (dst->master_ethtool_ops.get_sset_count) {
831 mcount = dst->master_ethtool_ops.get_sset_count(dev,
832 ETH_SS_STATS);
833 dst->master_ethtool_ops.get_strings(dev, stringset, data);
834 }
835
Vivien Didelot9d490b42016-08-23 12:38:56 -0400836 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700837 ndata = data + mcount * len;
838 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
839 * the output after to prepend our CPU port prefix we
840 * constructed earlier
841 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400842 ds->ops->get_strings(ds, cpu_port, ndata);
843 count = ds->ops->get_sset_count(ds);
Florian Fainellibadf3ad2016-04-27 11:45:14 -0700844 for (i = 0; i < count; i++) {
845 memmove(ndata + (i * len + sizeof(pfx)),
846 ndata + i * len, len - sizeof(pfx));
847 memcpy(ndata + i * len, pfx, sizeof(pfx));
848 }
849 }
850}
851
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000852static void dsa_slave_get_ethtool_stats(struct net_device *dev,
853 struct ethtool_stats *stats,
854 uint64_t *data)
855{
856 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500857 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000858
Vivien Didelot46e7b8d2016-04-18 16:10:24 -0400859 data[0] = dev->stats.tx_packets;
860 data[1] = dev->stats.tx_bytes;
861 data[2] = dev->stats.rx_packets;
862 data[3] = dev->stats.rx_bytes;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400863 if (ds->ops->get_ethtool_stats)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500864 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000865}
866
867static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
868{
869 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500870 struct dsa_switch *ds = p->dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000871
872 if (sset == ETH_SS_STATS) {
873 int count;
874
875 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400876 if (ds->ops->get_sset_count)
877 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000878
879 return count;
880 }
881
882 return -EOPNOTSUPP;
883}
884
Florian Fainelli19e57c42014-09-18 17:31:24 -0700885static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
886{
887 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500888 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700889
Vivien Didelot9d490b42016-08-23 12:38:56 -0400890 if (ds->ops->get_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500891 ds->ops->get_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700892}
893
894static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
895{
896 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500897 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700898 int ret = -EOPNOTSUPP;
899
Vivien Didelot9d490b42016-08-23 12:38:56 -0400900 if (ds->ops->set_wol)
Vivien Didelotafdcf152017-01-27 15:29:39 -0500901 ret = ds->ops->set_wol(ds, p->dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700902
903 return ret;
904}
905
Florian Fainelli79052882014-09-24 17:05:21 -0700906static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
907{
908 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500909 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700910 int ret;
911
Vivien Didelot9d490b42016-08-23 12:38:56 -0400912 if (!ds->ops->set_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700913 return -EOPNOTSUPP;
914
Vivien Didelotafdcf152017-01-27 15:29:39 -0500915 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700916 if (ret)
917 return ret;
918
919 if (p->phy)
920 ret = phy_ethtool_set_eee(p->phy, e);
921
922 return ret;
923}
924
925static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
926{
927 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500928 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700929 int ret;
930
Vivien Didelot9d490b42016-08-23 12:38:56 -0400931 if (!ds->ops->get_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700932 return -EOPNOTSUPP;
933
Vivien Didelotafdcf152017-01-27 15:29:39 -0500934 ret = ds->ops->get_eee(ds, p->dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700935 if (ret)
936 return ret;
937
938 if (p->phy)
939 ret = phy_ethtool_get_eee(p->phy, e);
940
941 return ret;
942}
943
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700944#ifdef CONFIG_NET_POLL_CONTROLLER
945static int dsa_slave_netpoll_setup(struct net_device *dev,
946 struct netpoll_info *ni)
947{
948 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -0500949 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700950 struct net_device *master = ds->dst->master_netdev;
951 struct netpoll *netpoll;
952 int err = 0;
953
954 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
955 if (!netpoll)
956 return -ENOMEM;
957
958 err = __netpoll_setup(netpoll, master);
959 if (err) {
960 kfree(netpoll);
961 goto out;
962 }
963
964 p->netpoll = netpoll;
965out:
966 return err;
967}
968
969static void dsa_slave_netpoll_cleanup(struct net_device *dev)
970{
971 struct dsa_slave_priv *p = netdev_priv(dev);
972 struct netpoll *netpoll = p->netpoll;
973
974 if (!netpoll)
975 return;
976
977 p->netpoll = NULL;
978
979 __netpoll_free_async(netpoll);
980}
981
982static void dsa_slave_poll_controller(struct net_device *dev)
983{
984}
985#endif
986
Florian Fainelli44bb7652017-01-10 12:32:36 -0800987static int dsa_slave_get_phys_port_name(struct net_device *dev,
988 char *name, size_t len)
989{
990 struct dsa_slave_priv *p = netdev_priv(dev);
991
Vivien Didelotafdcf152017-01-27 15:29:39 -0500992 if (snprintf(name, len, "p%d", p->dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -0800993 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -0800994
995 return 0;
996}
997
Florian Fainelliaf421922016-06-07 16:32:41 -0700998void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
999{
1000 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1001 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1002 ops->get_strings = dsa_cpu_port_get_strings;
1003}
1004
Florian Fainellibf9f2642017-01-30 09:48:40 -08001005static int dsa_slave_get_rxnfc(struct net_device *dev,
1006 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1007{
1008 struct dsa_slave_priv *p = netdev_priv(dev);
1009 struct dsa_switch *ds = p->dp->ds;
1010
1011 if (!ds->ops->get_rxnfc)
1012 return -EOPNOTSUPP;
1013
1014 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1015}
1016
1017static int dsa_slave_set_rxnfc(struct net_device *dev,
1018 struct ethtool_rxnfc *nfc)
1019{
1020 struct dsa_slave_priv *p = netdev_priv(dev);
1021 struct dsa_switch *ds = p->dp->ds;
1022
1023 if (!ds->ops->set_rxnfc)
1024 return -EOPNOTSUPP;
1025
1026 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1027}
1028
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001029static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001030 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001031 .get_regs_len = dsa_slave_get_regs_len,
1032 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001033 .nway_reset = dsa_slave_nway_reset,
1034 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001035 .get_eeprom_len = dsa_slave_get_eeprom_len,
1036 .get_eeprom = dsa_slave_get_eeprom,
1037 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001038 .get_strings = dsa_slave_get_strings,
1039 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1040 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001041 .set_wol = dsa_slave_set_wol,
1042 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001043 .set_eee = dsa_slave_set_eee,
1044 .get_eee = dsa_slave_get_eee,
Philippe Reynesbb10bb32016-10-09 17:00:53 +02001045 .get_link_ksettings = dsa_slave_get_link_ksettings,
1046 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001047 .get_rxnfc = dsa_slave_get_rxnfc,
1048 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001049};
1050
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001051static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001052 .ndo_open = dsa_slave_open,
1053 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001054 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001055 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1056 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001057 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001058 .ndo_fdb_add = switchdev_port_fdb_add,
1059 .ndo_fdb_del = switchdev_port_fdb_del,
1060 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001061 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001062 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001063#ifdef CONFIG_NET_POLL_CONTROLLER
1064 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1065 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1066 .ndo_poll_controller = dsa_slave_poll_controller,
1067#endif
Vivien Didelot11149532015-08-13 12:52:17 -04001068 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1069 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1070 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Florian Fainelli44bb7652017-01-10 12:32:36 -08001071 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Scott Feldman98237d42015-03-15 21:07:15 -07001072};
1073
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001074static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001075 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001076 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001077 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1078 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1079 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001080};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001081
Florian Fainellif37db852015-09-23 18:19:58 -07001082static struct device_type dsa_type = {
1083 .name = "dsa",
1084};
1085
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001086static void dsa_slave_adjust_link(struct net_device *dev)
1087{
1088 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001089 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001090 unsigned int status_changed = 0;
1091
1092 if (p->old_link != p->phy->link) {
1093 status_changed = 1;
1094 p->old_link = p->phy->link;
1095 }
1096
1097 if (p->old_duplex != p->phy->duplex) {
1098 status_changed = 1;
1099 p->old_duplex = p->phy->duplex;
1100 }
1101
1102 if (p->old_pause != p->phy->pause) {
1103 status_changed = 1;
1104 p->old_pause = p->phy->pause;
1105 }
1106
Vivien Didelot9d490b42016-08-23 12:38:56 -04001107 if (ds->ops->adjust_link && status_changed)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001108 ds->ops->adjust_link(ds, p->dp->index, p->phy);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001109
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001110 if (status_changed)
1111 phy_print_status(p->phy);
1112}
1113
Florian Fainellice31b312014-08-27 17:04:54 -07001114static int dsa_slave_fixed_link_update(struct net_device *dev,
1115 struct fixed_phy_status *status)
1116{
Andrew Lunnb71be352016-03-12 00:01:37 +01001117 struct dsa_slave_priv *p;
1118 struct dsa_switch *ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001119
Andrew Lunnb71be352016-03-12 00:01:37 +01001120 if (dev) {
1121 p = netdev_priv(dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001122 ds = p->dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001123 if (ds->ops->fixed_link_update)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001124 ds->ops->fixed_link_update(ds, p->dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001125 }
Florian Fainellice31b312014-08-27 17:04:54 -07001126
1127 return 0;
1128}
1129
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001130/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001131static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001132 struct net_device *slave_dev,
1133 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001134{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001135 struct dsa_switch *ds = p->dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001136
Andrew Lunn7f854422016-01-06 20:11:18 +01001137 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
Russell Kingd25b8e72015-10-03 18:09:07 +01001138 if (!p->phy) {
1139 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001140 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001141 }
Florian Fainellic305c162015-03-10 16:57:12 -07001142
1143 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001144 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1145 p->phy_interface = p->phy->interface;
Florian Fainelli4078b762017-01-20 16:05:05 -08001146 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1147 p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001148}
1149
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001150static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001151 struct net_device *slave_dev)
1152{
Vivien Didelotafdcf152017-01-27 15:29:39 -05001153 struct dsa_switch *ds = p->dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001154 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001155 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001156 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001157 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001158
Vivien Didelotafdcf152017-01-27 15:29:39 -05001159 port_dn = p->dp->dn;
Guenter Roeck19334922015-02-16 21:23:51 -08001160 mode = of_get_phy_mode(port_dn);
1161 if (mode < 0)
1162 mode = PHY_INTERFACE_MODE_NA;
1163 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001164
1165 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001166 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001167 /* In the case of a fixed PHY, the DT node associated
1168 * to the fixed PHY is the Port DT node
1169 */
1170 ret = of_phy_register_fixed_link(port_dn);
1171 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001172 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001173 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001174 }
Florian Fainellice31b312014-08-27 17:04:54 -07001175 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001176 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001177 }
1178
Vivien Didelot9d490b42016-08-23 12:38:56 -04001179 if (ds->ops->get_phy_flags)
Vivien Didelotafdcf152017-01-27 15:29:39 -05001180 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001181
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001182 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001183 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1184
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001185 /* If this PHY address is part of phys_mii_mask, which means
1186 * that we need to divert reads and writes to/from it, then we
1187 * want to bind this device using the slave MII bus created by
1188 * DSA to make that happen.
1189 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001190 if (!phy_is_fixed && phy_id >= 0 &&
1191 (ds->phys_mii_mask & (1 << phy_id))) {
1192 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1193 if (ret) {
1194 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001195 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001196 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001197 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001198 } else {
1199 p->phy = of_phy_connect(slave_dev, phy_dn,
1200 dsa_slave_adjust_link,
1201 phy_flags,
1202 p->phy_interface);
1203 }
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001204
1205 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001206 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001207
Florian Fainellice31b312014-08-27 17:04:54 -07001208 if (p->phy && phy_is_fixed)
1209 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1210
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001211 /* We could not connect to a designated PHY, so use the switch internal
1212 * MDIO bus instead
1213 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001214 if (!p->phy) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001215 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001216 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001217 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1218 p->dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001219 if (phy_is_fixed)
1220 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001221 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001222 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001223 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001224
Andrew Lunn22209432016-01-06 20:11:13 +01001225 phy_attached_info(p->phy);
1226
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001227 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001228}
1229
Andrew Lunn448b4482015-05-06 01:09:56 +02001230static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1231static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1232 struct netdev_queue *txq,
1233 void *_unused)
1234{
1235 lockdep_set_class(&txq->_xmit_lock,
1236 &dsa_slave_netdev_xmit_lock_key);
1237}
1238
Florian Fainelli24462542014-09-18 17:31:22 -07001239int dsa_slave_suspend(struct net_device *slave_dev)
1240{
1241 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1242
Florian Fainellif154be22017-01-25 09:10:41 -08001243 netif_device_detach(slave_dev);
1244
Florian Fainelli24462542014-09-18 17:31:22 -07001245 if (p->phy) {
1246 phy_stop(p->phy);
1247 p->old_pause = -1;
1248 p->old_link = -1;
1249 p->old_duplex = -1;
1250 phy_suspend(p->phy);
1251 }
1252
1253 return 0;
1254}
1255
1256int dsa_slave_resume(struct net_device *slave_dev)
1257{
1258 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1259
1260 netif_device_attach(slave_dev);
1261
1262 if (p->phy) {
1263 phy_resume(p->phy);
1264 phy_start(p->phy);
1265 }
1266
1267 return 0;
1268}
1269
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001270int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001271 int port, const char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001272{
Florian Fainellibadf3ad2016-04-27 11:45:14 -07001273 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001274 struct net_device *master;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001275 struct net_device *slave_dev;
1276 struct dsa_slave_priv *p;
1277 int ret;
1278
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001279 master = ds->dst->master_netdev;
1280 if (ds->master_netdev)
1281 master = ds->master_netdev;
1282
Tom Gundersenc835a672014-07-14 16:37:24 +02001283 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1284 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001285 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001286 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001287
1288 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001289 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001290 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001291 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001292 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001293 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001294 slave_dev->min_mtu = 0;
1295 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001296 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001297
Andrew Lunn448b4482015-05-06 01:09:56 +02001298 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1299 NULL);
1300
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001301 SET_NETDEV_DEV(slave_dev, parent);
Andrew Lunn189b0d92016-06-04 21:16:58 +02001302 slave_dev->dev.of_node = ds->ports[port].dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001303 slave_dev->vlan_features = master->vlan_features;
1304
1305 p = netdev_priv(slave_dev);
Vivien Didelotafdcf152017-01-27 15:29:39 -05001306 p->dp = &ds->ports[port];
Andrew Lunn39a7f2a2016-06-04 21:17:03 +02001307 p->xmit = dst->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001308
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001309 p->old_pause = -1;
1310 p->old_link = -1;
1311 p->old_duplex = -1;
1312
Andrew Lunnc8b09802016-06-04 21:16:57 +02001313 ds->ports[port].netdev = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001314 ret = register_netdev(slave_dev);
1315 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001316 netdev_err(master, "error %d registering interface %s\n",
1317 ret, slave_dev->name);
Andrew Lunnc8b09802016-06-04 21:16:57 +02001318 ds->ports[port].netdev = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001319 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001320 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001321 }
1322
1323 netif_carrier_off(slave_dev);
1324
Andrew Lunn0071f562016-01-06 20:11:20 +01001325 ret = dsa_slave_phy_setup(p, slave_dev);
1326 if (ret) {
1327 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli73dcb552016-02-17 18:43:22 -08001328 unregister_netdev(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001329 free_netdev(slave_dev);
1330 return ret;
1331 }
1332
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001333 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001334}
Florian Fainellib73adef2015-02-24 13:15:33 -08001335
Neil Armstrongcda5c152015-12-07 13:57:35 +01001336void dsa_slave_destroy(struct net_device *slave_dev)
1337{
1338 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001339 struct device_node *port_dn;
1340
Vivien Didelotafdcf152017-01-27 15:29:39 -05001341 port_dn = p->dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001342
1343 netif_carrier_off(slave_dev);
Johan Hovold881eada2016-11-28 19:25:09 +01001344 if (p->phy) {
Neil Armstrongcda5c152015-12-07 13:57:35 +01001345 phy_disconnect(p->phy);
Johan Hovold881eada2016-11-28 19:25:09 +01001346
1347 if (of_phy_is_fixed_link(port_dn))
1348 of_phy_deregister_fixed_link(port_dn);
1349 }
Neil Armstrongcda5c152015-12-07 13:57:35 +01001350 unregister_netdev(slave_dev);
1351 free_netdev(slave_dev);
1352}
1353
Florian Fainellib73adef2015-02-24 13:15:33 -08001354static bool dsa_slave_dev_check(struct net_device *dev)
1355{
1356 return dev->netdev_ops == &dsa_slave_netdev_ops;
1357}
1358
Vivien Didelot6debb682016-03-13 16:21:34 -04001359static int dsa_slave_port_upper_event(struct net_device *dev,
1360 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001361{
Vivien Didelot6debb682016-03-13 16:21:34 -04001362 struct netdev_notifier_changeupper_info *info = ptr;
1363 struct net_device *upper = info->upper_dev;
Florian Fainellib73adef2015-02-24 13:15:33 -08001364 int err = 0;
1365
Vivien Didelot6debb682016-03-13 16:21:34 -04001366 switch (event) {
1367 case NETDEV_CHANGEUPPER:
1368 if (netif_is_bridge_master(upper)) {
1369 if (info->linking)
1370 err = dsa_slave_bridge_port_join(dev, upper);
1371 else
Vivien Didelotf123f2f2017-01-27 15:29:41 -05001372 dsa_slave_bridge_port_leave(dev, upper);
Vivien Didelot6debb682016-03-13 16:21:34 -04001373 }
Florian Fainellib73adef2015-02-24 13:15:33 -08001374
Vivien Didelot6debb682016-03-13 16:21:34 -04001375 break;
1376 }
1377
1378 return notifier_from_errno(err);
1379}
1380
1381static int dsa_slave_port_event(struct net_device *dev, unsigned long event,
1382 void *ptr)
1383{
1384 switch (event) {
1385 case NETDEV_CHANGEUPPER:
1386 return dsa_slave_port_upper_event(dev, event, ptr);
1387 }
1388
1389 return NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001390}
1391
1392int dsa_slave_netdevice_event(struct notifier_block *unused,
1393 unsigned long event, void *ptr)
1394{
Vivien Didelot6debb682016-03-13 16:21:34 -04001395 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001396
Vivien Didelot6debb682016-03-13 16:21:34 -04001397 if (dsa_slave_dev_check(dev))
1398 return dsa_slave_port_event(dev, event, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001399
Florian Fainellib73adef2015-02-24 13:15:33 -08001400 return NOTIFY_DONE;
1401}