Linux Audio

Check our new training course

Loading...
v3.5.6
  1/*
  2 * net/dsa/slave.c - Slave device handling
  3 * Copyright (c) 2008-2009 Marvell Semiconductor
  4 *
  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>
 12#include <linux/netdevice.h>
 13#include <linux/etherdevice.h>
 
 14#include <linux/phy.h>
 
 
 
 
 
 
 
 
 
 
 
 15#include "dsa_priv.h"
 16
 
 
 17/* slave mii_bus handling ***************************************************/
 18static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
 19{
 20	struct dsa_switch *ds = bus->priv;
 21
 22	if (ds->phys_port_mask & (1 << addr))
 23		return ds->drv->phy_read(ds, addr, reg);
 24
 25	return 0xffff;
 26}
 27
 28static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
 29{
 30	struct dsa_switch *ds = bus->priv;
 31
 32	if (ds->phys_port_mask & (1 << addr))
 33		return ds->drv->phy_write(ds, addr, reg, val);
 34
 35	return 0;
 36}
 37
 38void dsa_slave_mii_bus_init(struct dsa_switch *ds)
 39{
 40	ds->slave_mii_bus->priv = (void *)ds;
 41	ds->slave_mii_bus->name = "dsa slave smi";
 42	ds->slave_mii_bus->read = dsa_slave_phy_read;
 43	ds->slave_mii_bus->write = dsa_slave_phy_write;
 44	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "%s:%.2x",
 45			ds->master_mii_bus->id, ds->pd->sw_addr);
 46	ds->slave_mii_bus->parent = &ds->master_mii_bus->dev;
 
 47}
 48
 49
 50/* slave device handling ****************************************************/
 51static int dsa_slave_init(struct net_device *dev)
 52{
 53	struct dsa_slave_priv *p = netdev_priv(dev);
 54
 55	dev->iflink = p->parent->dst->master_netdev->ifindex;
 56
 57	return 0;
 58}
 59
 60static int dsa_slave_open(struct net_device *dev)
 61{
 62	struct dsa_slave_priv *p = netdev_priv(dev);
 63	struct net_device *master = p->parent->dst->master_netdev;
 64	int err;
 65
 66	if (!(master->flags & IFF_UP))
 67		return -ENETDOWN;
 68
 69	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
 70		err = dev_uc_add(master, dev->dev_addr);
 71		if (err < 0)
 72			goto out;
 73	}
 74
 75	if (dev->flags & IFF_ALLMULTI) {
 76		err = dev_set_allmulti(master, 1);
 77		if (err < 0)
 78			goto del_unicast;
 79	}
 80	if (dev->flags & IFF_PROMISC) {
 81		err = dev_set_promiscuity(master, 1);
 82		if (err < 0)
 83			goto clear_allmulti;
 84	}
 85
 
 
 
 
 
 
 
 86	return 0;
 87
 
 
 
 88clear_allmulti:
 89	if (dev->flags & IFF_ALLMULTI)
 90		dev_set_allmulti(master, -1);
 91del_unicast:
 92	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
 93		dev_uc_del(master, dev->dev_addr);
 94out:
 95	return err;
 96}
 97
 98static int dsa_slave_close(struct net_device *dev)
 99{
100	struct dsa_slave_priv *p = netdev_priv(dev);
101	struct net_device *master = p->parent->dst->master_netdev;
 
 
 
 
 
102
103	dev_mc_unsync(master, dev);
104	dev_uc_unsync(master, dev);
105	if (dev->flags & IFF_ALLMULTI)
106		dev_set_allmulti(master, -1);
107	if (dev->flags & IFF_PROMISC)
108		dev_set_promiscuity(master, -1);
109
110	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
111		dev_uc_del(master, dev->dev_addr);
112
113	return 0;
114}
115
116static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
117{
118	struct dsa_slave_priv *p = netdev_priv(dev);
119	struct net_device *master = p->parent->dst->master_netdev;
120
121	if (change & IFF_ALLMULTI)
122		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
123	if (change & IFF_PROMISC)
124		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
125}
126
127static void dsa_slave_set_rx_mode(struct net_device *dev)
128{
129	struct dsa_slave_priv *p = netdev_priv(dev);
130	struct net_device *master = p->parent->dst->master_netdev;
131
132	dev_mc_sync(master, dev);
133	dev_uc_sync(master, dev);
134}
135
136static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
137{
138	struct dsa_slave_priv *p = netdev_priv(dev);
139	struct net_device *master = p->parent->dst->master_netdev;
140	struct sockaddr *addr = a;
141	int err;
142
143	if (!is_valid_ether_addr(addr->sa_data))
144		return -EADDRNOTAVAIL;
145
146	if (!(dev->flags & IFF_UP))
147		goto out;
148
149	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
150		err = dev_uc_add(master, addr->sa_data);
151		if (err < 0)
152			return err;
153	}
154
155	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
156		dev_uc_del(master, dev->dev_addr);
157
158out:
159	memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
 
 
 
 
 
 
161	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162}
163
164static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
165{
166	struct dsa_slave_priv *p = netdev_priv(dev);
 
 
167
168	if (p->phy != NULL)
169		return phy_mii_ioctl(p->phy, ifr, cmd);
 
 
 
 
 
 
 
 
 
170
171	return -EOPNOTSUPP;
 
 
 
172}
173
 
 
 
 
 
 
174
175/* ethtool operations *******************************************************/
176static int
177dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178{
179	struct dsa_slave_priv *p = netdev_priv(dev);
180	int err;
181
182	err = -EOPNOTSUPP;
183	if (p->phy != NULL) {
184		err = phy_read_status(p->phy);
185		if (err == 0)
186			err = phy_ethtool_gset(p->phy, cmd);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187	}
188
189	return err;
190}
191
192static int
193dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
194{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195	struct dsa_slave_priv *p = netdev_priv(dev);
196
197	if (p->phy != NULL)
198		return phy_ethtool_sset(p->phy, cmd);
 
 
 
 
 
199
200	return -EOPNOTSUPP;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201}
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203static void dsa_slave_get_drvinfo(struct net_device *dev,
204				  struct ethtool_drvinfo *drvinfo)
205{
206	strncpy(drvinfo->driver, "dsa", 32);
207	strncpy(drvinfo->version, dsa_driver_version, 32);
208	strncpy(drvinfo->fw_version, "N/A", 32);
209	strncpy(drvinfo->bus_info, "platform", 32);
210}
211
212static int dsa_slave_nway_reset(struct net_device *dev)
213{
214	struct dsa_slave_priv *p = netdev_priv(dev);
 
215
216	if (p->phy != NULL)
217		return genphy_restart_aneg(p->phy);
218
219	return -EOPNOTSUPP;
220}
221
 
 
 
 
 
 
 
 
 
 
222static u32 dsa_slave_get_link(struct net_device *dev)
223{
224	struct dsa_slave_priv *p = netdev_priv(dev);
 
225
226	if (p->phy != NULL) {
227		genphy_update_link(p->phy);
228		return p->phy->link;
229	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
231	return -EOPNOTSUPP;
232}
233
234static void dsa_slave_get_strings(struct net_device *dev,
235				  uint32_t stringset, uint8_t *data)
236{
237	struct dsa_slave_priv *p = netdev_priv(dev);
238	struct dsa_switch *ds = p->parent;
239
240	if (stringset == ETH_SS_STATS) {
241		int len = ETH_GSTRING_LEN;
242
243		strncpy(data, "tx_packets", len);
244		strncpy(data + len, "tx_bytes", len);
245		strncpy(data + 2 * len, "rx_packets", len);
246		strncpy(data + 3 * len, "rx_bytes", len);
247		if (ds->drv->get_strings != NULL)
248			ds->drv->get_strings(ds, p->port, data + 4 * len);
249	}
250}
251
252static void dsa_slave_get_ethtool_stats(struct net_device *dev,
253					struct ethtool_stats *stats,
254					uint64_t *data)
255{
 
256	struct dsa_slave_priv *p = netdev_priv(dev);
257	struct dsa_switch *ds = p->parent;
258
259	data[0] = p->dev->stats.tx_packets;
260	data[1] = p->dev->stats.tx_bytes;
261	data[2] = p->dev->stats.rx_packets;
262	data[3] = p->dev->stats.rx_bytes;
263	if (ds->drv->get_ethtool_stats != NULL)
264		ds->drv->get_ethtool_stats(ds, p->port, data + 4);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265}
266
267static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
268{
269	struct dsa_slave_priv *p = netdev_priv(dev);
270	struct dsa_switch *ds = p->parent;
271
272	if (sset == ETH_SS_STATS) {
273		int count;
274
275		count = 4;
276		if (ds->drv->get_sset_count != NULL)
277			count += ds->drv->get_sset_count(ds);
278
279		return count;
280	}
281
282	return -EOPNOTSUPP;
283}
284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285static const struct ethtool_ops dsa_slave_ethtool_ops = {
286	.get_settings		= dsa_slave_get_settings,
287	.set_settings		= dsa_slave_set_settings,
288	.get_drvinfo		= dsa_slave_get_drvinfo,
289	.nway_reset		= dsa_slave_nway_reset,
 
 
290	.get_link		= dsa_slave_get_link,
 
 
 
291	.get_strings		= dsa_slave_get_strings,
292	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
293	.get_sset_count		= dsa_slave_get_sset_count,
 
 
 
 
 
 
 
 
 
294};
295
296#ifdef CONFIG_NET_DSA_TAG_DSA
297static const struct net_device_ops dsa_netdev_ops = {
298	.ndo_init		= dsa_slave_init,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299	.ndo_open	 	= dsa_slave_open,
300	.ndo_stop		= dsa_slave_close,
301	.ndo_start_xmit		= dsa_xmit,
302	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
303	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
304	.ndo_set_mac_address	= dsa_slave_set_mac_address,
 
 
 
305	.ndo_do_ioctl		= dsa_slave_ioctl,
306};
 
 
 
 
307#endif
308#ifdef CONFIG_NET_DSA_TAG_EDSA
309static const struct net_device_ops edsa_netdev_ops = {
310	.ndo_init		= dsa_slave_init,
311	.ndo_open	 	= dsa_slave_open,
312	.ndo_stop		= dsa_slave_close,
313	.ndo_start_xmit		= edsa_xmit,
314	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
315	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
316	.ndo_set_mac_address	= dsa_slave_set_mac_address,
317	.ndo_do_ioctl		= dsa_slave_ioctl,
318};
319#endif
320#ifdef CONFIG_NET_DSA_TAG_TRAILER
321static const struct net_device_ops trailer_netdev_ops = {
322	.ndo_init		= dsa_slave_init,
323	.ndo_open	 	= dsa_slave_open,
324	.ndo_stop		= dsa_slave_close,
325	.ndo_start_xmit		= trailer_xmit,
326	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
327	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
328	.ndo_set_mac_address	= dsa_slave_set_mac_address,
329	.ndo_do_ioctl		= dsa_slave_ioctl,
330};
331#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
333/* slave device setup *******************************************************/
334struct net_device *
335dsa_slave_create(struct dsa_switch *ds, struct device *parent,
336		 int port, char *name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337{
338	struct net_device *master = ds->dst->master_netdev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339	struct net_device *slave_dev;
340	struct dsa_slave_priv *p;
341	int ret;
342
343	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv),
344				 name, ether_setup);
 
 
 
 
345	if (slave_dev == NULL)
346		return slave_dev;
347
348	slave_dev->features = master->vlan_features;
349	SET_ETHTOOL_OPS(slave_dev, &dsa_slave_ethtool_ops);
350	memcpy(slave_dev->dev_addr, master->dev_addr, ETH_ALEN);
351	slave_dev->tx_queue_len = 0;
352
353	switch (ds->dst->tag_protocol) {
354#ifdef CONFIG_NET_DSA_TAG_DSA
355	case htons(ETH_P_DSA):
356		slave_dev->netdev_ops = &dsa_netdev_ops;
357		break;
358#endif
359#ifdef CONFIG_NET_DSA_TAG_EDSA
360	case htons(ETH_P_EDSA):
361		slave_dev->netdev_ops = &edsa_netdev_ops;
362		break;
363#endif
364#ifdef CONFIG_NET_DSA_TAG_TRAILER
365	case htons(ETH_P_TRAILER):
366		slave_dev->netdev_ops = &trailer_netdev_ops;
367		break;
368#endif
369	default:
370		BUG();
371	}
372
373	SET_NETDEV_DEV(slave_dev, parent);
 
 
 
 
374	slave_dev->vlan_features = master->vlan_features;
375
376	p = netdev_priv(slave_dev);
377	p->dev = slave_dev;
378	p->parent = ds;
379	p->port = port;
380	p->phy = ds->slave_mii_bus->phy_map[port];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
382	ret = register_netdev(slave_dev);
383	if (ret) {
384		printk(KERN_ERR "%s: error %d registering interface %s\n",
385				master->name, ret, slave_dev->name);
386		free_netdev(slave_dev);
387		return NULL;
388	}
389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390	netif_carrier_off(slave_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
392	if (p->phy != NULL) {
393		phy_attach(slave_dev, dev_name(&p->phy->dev),
394			   0, PHY_INTERFACE_MODE_GMII);
395
396		p->phy->autoneg = AUTONEG_ENABLE;
397		p->phy->speed = 0;
398		p->phy->duplex = 0;
399		p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
400		phy_start_aneg(p->phy);
401	}
402
403	return slave_dev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404}
v4.17
   1/*
   2 * net/dsa/slave.c - Slave device handling
   3 * Copyright (c) 2008-2009 Marvell Semiconductor
   4 *
   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>
 
  12#include <linux/etherdevice.h>
  13#include <linux/netdevice.h>
  14#include <linux/phy.h>
  15#include <linux/phy_fixed.h>
  16#include <linux/of_net.h>
  17#include <linux/of_mdio.h>
  18#include <linux/mdio.h>
  19#include <net/rtnetlink.h>
  20#include <net/pkt_cls.h>
  21#include <net/tc_act/tc_mirred.h>
  22#include <linux/if_bridge.h>
  23#include <linux/netpoll.h>
  24#include <linux/ptp_classify.h>
  25
  26#include "dsa_priv.h"
  27
  28static bool dsa_slave_dev_check(struct net_device *dev);
  29
  30/* 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
  35	if (ds->phys_mii_mask & (1 << addr))
  36		return ds->ops->phy_read(ds, addr, reg);
  37
  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
  45	if (ds->phys_mii_mask & (1 << addr))
  46		return ds->ops->phy_write(ds, addr, reg, val);
  47
  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;
  57	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
  58		 ds->dst->index, ds->index);
  59	ds->slave_mii_bus->parent = ds->dev;
  60	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
  61}
  62
  63
  64/* slave device handling ****************************************************/
  65static int dsa_slave_get_iflink(const struct net_device *dev)
  66{
  67	return dsa_slave_to_master(dev)->ifindex;
 
 
 
 
  68}
  69
  70static int dsa_slave_open(struct net_device *dev)
  71{
  72	struct net_device *master = dsa_slave_to_master(dev);
  73	struct dsa_port *dp = dsa_slave_to_port(dev);
  74	int err;
  75
  76	if (!(master->flags & IFF_UP))
  77		return -ENETDOWN;
  78
  79	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
  80		err = dev_uc_add(master, dev->dev_addr);
  81		if (err < 0)
  82			goto out;
  83	}
  84
  85	if (dev->flags & IFF_ALLMULTI) {
  86		err = dev_set_allmulti(master, 1);
  87		if (err < 0)
  88			goto del_unicast;
  89	}
  90	if (dev->flags & IFF_PROMISC) {
  91		err = dev_set_promiscuity(master, 1);
  92		if (err < 0)
  93			goto clear_allmulti;
  94	}
  95
  96	err = dsa_port_enable(dp, dev->phydev);
  97	if (err)
  98		goto clear_promisc;
  99
 100	if (dev->phydev)
 101		phy_start(dev->phydev);
 102
 103	return 0;
 104
 105clear_promisc:
 106	if (dev->flags & IFF_PROMISC)
 107		dev_set_promiscuity(master, -1);
 108clear_allmulti:
 109	if (dev->flags & IFF_ALLMULTI)
 110		dev_set_allmulti(master, -1);
 111del_unicast:
 112	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
 113		dev_uc_del(master, dev->dev_addr);
 114out:
 115	return err;
 116}
 117
 118static int dsa_slave_close(struct net_device *dev)
 119{
 120	struct net_device *master = dsa_slave_to_master(dev);
 121	struct dsa_port *dp = dsa_slave_to_port(dev);
 122
 123	if (dev->phydev)
 124		phy_stop(dev->phydev);
 125
 126	dsa_port_disable(dp, dev->phydev);
 127
 128	dev_mc_unsync(master, dev);
 129	dev_uc_unsync(master, dev);
 130	if (dev->flags & IFF_ALLMULTI)
 131		dev_set_allmulti(master, -1);
 132	if (dev->flags & IFF_PROMISC)
 133		dev_set_promiscuity(master, -1);
 134
 135	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
 136		dev_uc_del(master, dev->dev_addr);
 137
 138	return 0;
 139}
 140
 141static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
 142{
 143	struct net_device *master = dsa_slave_to_master(dev);
 
 144
 145	if (change & IFF_ALLMULTI)
 146		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
 147	if (change & IFF_PROMISC)
 148		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
 149}
 150
 151static void dsa_slave_set_rx_mode(struct net_device *dev)
 152{
 153	struct net_device *master = dsa_slave_to_master(dev);
 
 154
 155	dev_mc_sync(master, dev);
 156	dev_uc_sync(master, dev);
 157}
 158
 159static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
 160{
 161	struct net_device *master = dsa_slave_to_master(dev);
 
 162	struct sockaddr *addr = a;
 163	int err;
 164
 165	if (!is_valid_ether_addr(addr->sa_data))
 166		return -EADDRNOTAVAIL;
 167
 168	if (!(dev->flags & IFF_UP))
 169		goto out;
 170
 171	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
 172		err = dev_uc_add(master, addr->sa_data);
 173		if (err < 0)
 174			return err;
 175	}
 176
 177	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
 178		dev_uc_del(master, dev->dev_addr);
 179
 180out:
 181	ether_addr_copy(dev->dev_addr, addr->sa_data);
 182
 183	return 0;
 184}
 185
 186struct dsa_slave_dump_ctx {
 187	struct net_device *dev;
 188	struct sk_buff *skb;
 189	struct netlink_callback *cb;
 190	int idx;
 191};
 192
 193static int
 194dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
 195			   bool is_static, void *data)
 196{
 197	struct dsa_slave_dump_ctx *dump = data;
 198	u32 portid = NETLINK_CB(dump->cb->skb).portid;
 199	u32 seq = dump->cb->nlh->nlmsg_seq;
 200	struct nlmsghdr *nlh;
 201	struct ndmsg *ndm;
 202
 203	if (dump->idx < dump->cb->args[2])
 204		goto skip;
 205
 206	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
 207			sizeof(*ndm), NLM_F_MULTI);
 208	if (!nlh)
 209		return -EMSGSIZE;
 210
 211	ndm = nlmsg_data(nlh);
 212	ndm->ndm_family  = AF_BRIDGE;
 213	ndm->ndm_pad1    = 0;
 214	ndm->ndm_pad2    = 0;
 215	ndm->ndm_flags   = NTF_SELF;
 216	ndm->ndm_type    = 0;
 217	ndm->ndm_ifindex = dump->dev->ifindex;
 218	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
 219
 220	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
 221		goto nla_put_failure;
 222
 223	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
 224		goto nla_put_failure;
 225
 226	nlmsg_end(dump->skb, nlh);
 227
 228skip:
 229	dump->idx++;
 230	return 0;
 231
 232nla_put_failure:
 233	nlmsg_cancel(dump->skb, nlh);
 234	return -EMSGSIZE;
 235}
 236
 237static int
 238dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
 239		   struct net_device *dev, struct net_device *filter_dev,
 240		   int *idx)
 241{
 242	struct dsa_port *dp = dsa_slave_to_port(dev);
 243	struct dsa_slave_dump_ctx dump = {
 244		.dev = dev,
 245		.skb = skb,
 246		.cb = cb,
 247		.idx = *idx,
 248	};
 249	int err;
 250
 251	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
 252	*idx = dump.idx;
 253
 254	return err;
 255}
 256
 257static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 258{
 259	struct dsa_slave_priv *p = netdev_priv(dev);
 260	struct dsa_switch *ds = p->dp->ds;
 261	int port = p->dp->index;
 262
 263	/* Pass through to switch driver if it supports timestamping */
 264	switch (cmd) {
 265	case SIOCGHWTSTAMP:
 266		if (ds->ops->port_hwtstamp_get)
 267			return ds->ops->port_hwtstamp_get(ds, port, ifr);
 268		break;
 269	case SIOCSHWTSTAMP:
 270		if (ds->ops->port_hwtstamp_set)
 271			return ds->ops->port_hwtstamp_set(ds, port, ifr);
 272		break;
 273	}
 274
 275	if (!dev->phydev)
 276		return -ENODEV;
 277
 278	return phy_mii_ioctl(dev->phydev, ifr, cmd);
 279}
 280
 281static int dsa_slave_port_attr_set(struct net_device *dev,
 282				   const struct switchdev_attr *attr,
 283				   struct switchdev_trans *trans)
 284{
 285	struct dsa_port *dp = dsa_slave_to_port(dev);
 286	int ret;
 287
 288	switch (attr->id) {
 289	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
 290		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
 291		break;
 292	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
 293		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
 294					      trans);
 295		break;
 296	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
 297		ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
 298		break;
 299	default:
 300		ret = -EOPNOTSUPP;
 301		break;
 302	}
 303
 304	return ret;
 305}
 306
 307static int dsa_slave_port_obj_add(struct net_device *dev,
 308				  const struct switchdev_obj *obj,
 309				  struct switchdev_trans *trans)
 310{
 311	struct dsa_port *dp = dsa_slave_to_port(dev);
 312	int err;
 313
 314	/* For the prepare phase, ensure the full set of changes is feasable in
 315	 * one go in order to signal a failure properly. If an operation is not
 316	 * supported, return -EOPNOTSUPP.
 317	 */
 318
 319	switch (obj->id) {
 320	case SWITCHDEV_OBJ_ID_PORT_MDB:
 321		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
 322		break;
 323	case SWITCHDEV_OBJ_ID_HOST_MDB:
 324		/* DSA can directly translate this to a normal MDB add,
 325		 * but on the CPU port.
 326		 */
 327		err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
 328				       trans);
 329		break;
 330	case SWITCHDEV_OBJ_ID_PORT_VLAN:
 331		err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
 332					trans);
 333		break;
 334	default:
 335		err = -EOPNOTSUPP;
 336		break;
 337	}
 338
 339	return err;
 340}
 341
 342static int dsa_slave_port_obj_del(struct net_device *dev,
 343				  const struct switchdev_obj *obj)
 344{
 345	struct dsa_port *dp = dsa_slave_to_port(dev);
 346	int err;
 347
 348	switch (obj->id) {
 349	case SWITCHDEV_OBJ_ID_PORT_MDB:
 350		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
 351		break;
 352	case SWITCHDEV_OBJ_ID_HOST_MDB:
 353		/* DSA can directly translate this to a normal MDB add,
 354		 * but on the CPU port.
 355		 */
 356		err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
 357		break;
 358	case SWITCHDEV_OBJ_ID_PORT_VLAN:
 359		err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
 360		break;
 361	default:
 362		err = -EOPNOTSUPP;
 363		break;
 364	}
 365
 366	return err;
 367}
 368
 369static int dsa_slave_port_attr_get(struct net_device *dev,
 370				   struct switchdev_attr *attr)
 371{
 372	struct dsa_port *dp = dsa_slave_to_port(dev);
 373	struct dsa_switch *ds = dp->ds;
 374	struct dsa_switch_tree *dst = ds->dst;
 375
 376	switch (attr->id) {
 377	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
 378		attr->u.ppid.id_len = sizeof(dst->index);
 379		memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
 380		break;
 381	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
 382		attr->u.brport_flags_support = 0;
 383		break;
 384	default:
 385		return -EOPNOTSUPP;
 386	}
 387
 388	return 0;
 389}
 390
 391static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
 392						     struct sk_buff *skb)
 393{
 394#ifdef CONFIG_NET_POLL_CONTROLLER
 395	struct dsa_slave_priv *p = netdev_priv(dev);
 396
 397	if (p->netpoll)
 398		netpoll_send_skb(p->netpoll, skb);
 399#else
 400	BUG();
 401#endif
 402	return NETDEV_TX_OK;
 403}
 404
 405static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
 406				 struct sk_buff *skb)
 407{
 408	struct dsa_switch *ds = p->dp->ds;
 409	struct sk_buff *clone;
 410	unsigned int type;
 411
 412	type = ptp_classify_raw(skb);
 413	if (type == PTP_CLASS_NONE)
 414		return;
 415
 416	if (!ds->ops->port_txtstamp)
 417		return;
 418
 419	clone = skb_clone_sk(skb);
 420	if (!clone)
 421		return;
 422
 423	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
 424		return;
 425
 426	kfree_skb(clone);
 427}
 428
 429static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
 430{
 431	struct dsa_slave_priv *p = netdev_priv(dev);
 432	struct pcpu_sw_netstats *s;
 433	struct sk_buff *nskb;
 434
 435	s = this_cpu_ptr(p->stats64);
 436	u64_stats_update_begin(&s->syncp);
 437	s->tx_packets++;
 438	s->tx_bytes += skb->len;
 439	u64_stats_update_end(&s->syncp);
 440
 441	/* Identify PTP protocol packets, clone them, and pass them to the
 442	 * switch driver
 443	 */
 444	dsa_skb_tx_timestamp(p, skb);
 445
 446	/* Transmit function may have to reallocate the original SKB,
 447	 * in which case it must have freed it. Only free it here on error.
 448	 */
 449	nskb = p->xmit(skb, dev);
 450	if (!nskb) {
 451		kfree_skb(skb);
 452		return NETDEV_TX_OK;
 453	}
 454
 455	/* SKB for netpoll still need to be mangled with the protocol-specific
 456	 * tag to be successfully transmitted
 457	 */
 458	if (unlikely(netpoll_tx_running(dev)))
 459		return dsa_slave_netpoll_send_skb(dev, nskb);
 460
 461	/* Queue the SKB for transmission on the parent interface, but
 462	 * do not modify its EtherType
 463	 */
 464	nskb->dev = dsa_slave_to_master(dev);
 465	dev_queue_xmit(nskb);
 466
 467	return NETDEV_TX_OK;
 468}
 469
 470/* ethtool operations *******************************************************/
 471
 472static void dsa_slave_get_drvinfo(struct net_device *dev,
 473				  struct ethtool_drvinfo *drvinfo)
 474{
 475	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
 476	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
 477	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
 
 478}
 479
 480static int dsa_slave_get_regs_len(struct net_device *dev)
 481{
 482	struct dsa_port *dp = dsa_slave_to_port(dev);
 483	struct dsa_switch *ds = dp->ds;
 484
 485	if (ds->ops->get_regs_len)
 486		return ds->ops->get_regs_len(ds, dp->index);
 487
 488	return -EOPNOTSUPP;
 489}
 490
 491static void
 492dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
 493{
 494	struct dsa_port *dp = dsa_slave_to_port(dev);
 495	struct dsa_switch *ds = dp->ds;
 496
 497	if (ds->ops->get_regs)
 498		ds->ops->get_regs(ds, dp->index, regs, _p);
 499}
 500
 501static u32 dsa_slave_get_link(struct net_device *dev)
 502{
 503	if (!dev->phydev)
 504		return -ENODEV;
 505
 506	genphy_update_link(dev->phydev);
 507
 508	return dev->phydev->link;
 509}
 510
 511static int dsa_slave_get_eeprom_len(struct net_device *dev)
 512{
 513	struct dsa_port *dp = dsa_slave_to_port(dev);
 514	struct dsa_switch *ds = dp->ds;
 515
 516	if (ds->cd && ds->cd->eeprom_len)
 517		return ds->cd->eeprom_len;
 518
 519	if (ds->ops->get_eeprom_len)
 520		return ds->ops->get_eeprom_len(ds);
 521
 522	return 0;
 523}
 524
 525static int dsa_slave_get_eeprom(struct net_device *dev,
 526				struct ethtool_eeprom *eeprom, u8 *data)
 527{
 528	struct dsa_port *dp = dsa_slave_to_port(dev);
 529	struct dsa_switch *ds = dp->ds;
 530
 531	if (ds->ops->get_eeprom)
 532		return ds->ops->get_eeprom(ds, eeprom, data);
 533
 534	return -EOPNOTSUPP;
 535}
 536
 537static int dsa_slave_set_eeprom(struct net_device *dev,
 538				struct ethtool_eeprom *eeprom, u8 *data)
 539{
 540	struct dsa_port *dp = dsa_slave_to_port(dev);
 541	struct dsa_switch *ds = dp->ds;
 542
 543	if (ds->ops->set_eeprom)
 544		return ds->ops->set_eeprom(ds, eeprom, data);
 545
 546	return -EOPNOTSUPP;
 547}
 548
 549static void dsa_slave_get_strings(struct net_device *dev,
 550				  uint32_t stringset, uint8_t *data)
 551{
 552	struct dsa_port *dp = dsa_slave_to_port(dev);
 553	struct dsa_switch *ds = dp->ds;
 554
 555	if (stringset == ETH_SS_STATS) {
 556		int len = ETH_GSTRING_LEN;
 557
 558		strncpy(data, "tx_packets", len);
 559		strncpy(data + len, "tx_bytes", len);
 560		strncpy(data + 2 * len, "rx_packets", len);
 561		strncpy(data + 3 * len, "rx_bytes", len);
 562		if (ds->ops->get_strings)
 563			ds->ops->get_strings(ds, dp->index, data + 4 * len);
 564	}
 565}
 566
 567static void dsa_slave_get_ethtool_stats(struct net_device *dev,
 568					struct ethtool_stats *stats,
 569					uint64_t *data)
 570{
 571	struct dsa_port *dp = dsa_slave_to_port(dev);
 572	struct dsa_slave_priv *p = netdev_priv(dev);
 573	struct dsa_switch *ds = dp->ds;
 574	struct pcpu_sw_netstats *s;
 575	unsigned int start;
 576	int i;
 577
 578	for_each_possible_cpu(i) {
 579		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
 580
 581		s = per_cpu_ptr(p->stats64, i);
 582		do {
 583			start = u64_stats_fetch_begin_irq(&s->syncp);
 584			tx_packets = s->tx_packets;
 585			tx_bytes = s->tx_bytes;
 586			rx_packets = s->rx_packets;
 587			rx_bytes = s->rx_bytes;
 588		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
 589		data[0] += tx_packets;
 590		data[1] += tx_bytes;
 591		data[2] += rx_packets;
 592		data[3] += rx_bytes;
 593	}
 594	if (ds->ops->get_ethtool_stats)
 595		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
 596}
 597
 598static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
 599{
 600	struct dsa_port *dp = dsa_slave_to_port(dev);
 601	struct dsa_switch *ds = dp->ds;
 602
 603	if (sset == ETH_SS_STATS) {
 604		int count;
 605
 606		count = 4;
 607		if (ds->ops->get_sset_count)
 608			count += ds->ops->get_sset_count(ds, dp->index);
 609
 610		return count;
 611	}
 612
 613	return -EOPNOTSUPP;
 614}
 615
 616static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
 617{
 618	struct dsa_port *dp = dsa_slave_to_port(dev);
 619	struct dsa_switch *ds = dp->ds;
 620
 621	if (ds->ops->get_wol)
 622		ds->ops->get_wol(ds, dp->index, w);
 623}
 624
 625static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
 626{
 627	struct dsa_port *dp = dsa_slave_to_port(dev);
 628	struct dsa_switch *ds = dp->ds;
 629	int ret = -EOPNOTSUPP;
 630
 631	if (ds->ops->set_wol)
 632		ret = ds->ops->set_wol(ds, dp->index, w);
 633
 634	return ret;
 635}
 636
 637static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
 638{
 639	struct dsa_port *dp = dsa_slave_to_port(dev);
 640	struct dsa_switch *ds = dp->ds;
 641	int ret;
 642
 643	/* Port's PHY and MAC both need to be EEE capable */
 644	if (!dev->phydev)
 645		return -ENODEV;
 646
 647	if (!ds->ops->set_mac_eee)
 648		return -EOPNOTSUPP;
 649
 650	ret = ds->ops->set_mac_eee(ds, dp->index, e);
 651	if (ret)
 652		return ret;
 653
 654	if (e->eee_enabled) {
 655		ret = phy_init_eee(dev->phydev, 0);
 656		if (ret)
 657			return ret;
 658	}
 659
 660	return phy_ethtool_set_eee(dev->phydev, e);
 661}
 662
 663static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
 664{
 665	struct dsa_port *dp = dsa_slave_to_port(dev);
 666	struct dsa_switch *ds = dp->ds;
 667	int ret;
 668
 669	/* Port's PHY and MAC both need to be EEE capable */
 670	if (!dev->phydev)
 671		return -ENODEV;
 672
 673	if (!ds->ops->get_mac_eee)
 674		return -EOPNOTSUPP;
 675
 676	ret = ds->ops->get_mac_eee(ds, dp->index, e);
 677	if (ret)
 678		return ret;
 679
 680	return phy_ethtool_get_eee(dev->phydev, e);
 681}
 682
 683#ifdef CONFIG_NET_POLL_CONTROLLER
 684static int dsa_slave_netpoll_setup(struct net_device *dev,
 685				   struct netpoll_info *ni)
 686{
 687	struct net_device *master = dsa_slave_to_master(dev);
 688	struct dsa_slave_priv *p = netdev_priv(dev);
 689	struct netpoll *netpoll;
 690	int err = 0;
 691
 692	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
 693	if (!netpoll)
 694		return -ENOMEM;
 695
 696	err = __netpoll_setup(netpoll, master);
 697	if (err) {
 698		kfree(netpoll);
 699		goto out;
 700	}
 701
 702	p->netpoll = netpoll;
 703out:
 704	return err;
 705}
 706
 707static void dsa_slave_netpoll_cleanup(struct net_device *dev)
 708{
 709	struct dsa_slave_priv *p = netdev_priv(dev);
 710	struct netpoll *netpoll = p->netpoll;
 711
 712	if (!netpoll)
 713		return;
 714
 715	p->netpoll = NULL;
 716
 717	__netpoll_free_async(netpoll);
 718}
 719
 720static void dsa_slave_poll_controller(struct net_device *dev)
 721{
 722}
 723#endif
 724
 725static int dsa_slave_get_phys_port_name(struct net_device *dev,
 726					char *name, size_t len)
 727{
 728	struct dsa_port *dp = dsa_slave_to_port(dev);
 729
 730	if (snprintf(name, len, "p%d", dp->index) >= len)
 731		return -EINVAL;
 732
 733	return 0;
 734}
 735
 736static struct dsa_mall_tc_entry *
 737dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
 738{
 739	struct dsa_slave_priv *p = netdev_priv(dev);
 740	struct dsa_mall_tc_entry *mall_tc_entry;
 741
 742	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
 743		if (mall_tc_entry->cookie == cookie)
 744			return mall_tc_entry;
 745
 746	return NULL;
 747}
 748
 749static int dsa_slave_add_cls_matchall(struct net_device *dev,
 750				      struct tc_cls_matchall_offload *cls,
 751				      bool ingress)
 752{
 753	struct dsa_port *dp = dsa_slave_to_port(dev);
 754	struct dsa_slave_priv *p = netdev_priv(dev);
 755	struct dsa_mall_tc_entry *mall_tc_entry;
 756	__be16 protocol = cls->common.protocol;
 757	struct dsa_switch *ds = dp->ds;
 758	struct net_device *to_dev;
 759	const struct tc_action *a;
 760	struct dsa_port *to_dp;
 761	int err = -EOPNOTSUPP;
 762	LIST_HEAD(actions);
 763
 764	if (!ds->ops->port_mirror_add)
 765		return err;
 766
 767	if (!tcf_exts_has_one_action(cls->exts))
 768		return err;
 769
 770	tcf_exts_to_list(cls->exts, &actions);
 771	a = list_first_entry(&actions, struct tc_action, list);
 772
 773	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
 774		struct dsa_mall_mirror_tc_entry *mirror;
 775
 776		to_dev = tcf_mirred_dev(a);
 777		if (!to_dev)
 778			return -EINVAL;
 779
 780		if (!dsa_slave_dev_check(to_dev))
 781			return -EOPNOTSUPP;
 782
 783		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
 784		if (!mall_tc_entry)
 785			return -ENOMEM;
 786
 787		mall_tc_entry->cookie = cls->cookie;
 788		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
 789		mirror = &mall_tc_entry->mirror;
 790
 791		to_dp = dsa_slave_to_port(to_dev);
 792
 793		mirror->to_local_port = to_dp->index;
 794		mirror->ingress = ingress;
 795
 796		err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
 797		if (err) {
 798			kfree(mall_tc_entry);
 799			return err;
 800		}
 801
 802		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
 803	}
 804
 805	return 0;
 806}
 807
 808static void dsa_slave_del_cls_matchall(struct net_device *dev,
 809				       struct tc_cls_matchall_offload *cls)
 810{
 811	struct dsa_port *dp = dsa_slave_to_port(dev);
 812	struct dsa_mall_tc_entry *mall_tc_entry;
 813	struct dsa_switch *ds = dp->ds;
 814
 815	if (!ds->ops->port_mirror_del)
 816		return;
 817
 818	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
 819	if (!mall_tc_entry)
 820		return;
 821
 822	list_del(&mall_tc_entry->list);
 823
 824	switch (mall_tc_entry->type) {
 825	case DSA_PORT_MALL_MIRROR:
 826		ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
 827		break;
 828	default:
 829		WARN_ON(1);
 830	}
 831
 832	kfree(mall_tc_entry);
 833}
 834
 835static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
 836					   struct tc_cls_matchall_offload *cls,
 837					   bool ingress)
 838{
 839	if (cls->common.chain_index)
 840		return -EOPNOTSUPP;
 841
 842	switch (cls->command) {
 843	case TC_CLSMATCHALL_REPLACE:
 844		return dsa_slave_add_cls_matchall(dev, cls, ingress);
 845	case TC_CLSMATCHALL_DESTROY:
 846		dsa_slave_del_cls_matchall(dev, cls);
 847		return 0;
 848	default:
 849		return -EOPNOTSUPP;
 850	}
 851}
 852
 853static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
 854				       void *cb_priv, bool ingress)
 855{
 856	struct net_device *dev = cb_priv;
 857
 858	if (!tc_can_offload(dev))
 859		return -EOPNOTSUPP;
 860
 861	switch (type) {
 862	case TC_SETUP_CLSMATCHALL:
 863		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
 864	default:
 865		return -EOPNOTSUPP;
 866	}
 867}
 868
 869static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
 870					  void *type_data, void *cb_priv)
 871{
 872	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
 873}
 874
 875static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
 876					  void *type_data, void *cb_priv)
 877{
 878	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
 879}
 880
 881static int dsa_slave_setup_tc_block(struct net_device *dev,
 882				    struct tc_block_offload *f)
 883{
 884	tc_setup_cb_t *cb;
 885
 886	if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
 887		cb = dsa_slave_setup_tc_block_cb_ig;
 888	else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
 889		cb = dsa_slave_setup_tc_block_cb_eg;
 890	else
 891		return -EOPNOTSUPP;
 892
 893	switch (f->command) {
 894	case TC_BLOCK_BIND:
 895		return tcf_block_cb_register(f->block, cb, dev, dev);
 896	case TC_BLOCK_UNBIND:
 897		tcf_block_cb_unregister(f->block, cb, dev);
 898		return 0;
 899	default:
 900		return -EOPNOTSUPP;
 901	}
 902}
 903
 904static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
 905			      void *type_data)
 906{
 907	switch (type) {
 908	case TC_SETUP_BLOCK:
 909		return dsa_slave_setup_tc_block(dev, type_data);
 910	default:
 911		return -EOPNOTSUPP;
 912	}
 913}
 914
 915static void dsa_slave_get_stats64(struct net_device *dev,
 916				  struct rtnl_link_stats64 *stats)
 917{
 918	struct dsa_slave_priv *p = netdev_priv(dev);
 919	struct pcpu_sw_netstats *s;
 920	unsigned int start;
 921	int i;
 922
 923	netdev_stats_to_stats64(stats, &dev->stats);
 924	for_each_possible_cpu(i) {
 925		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
 926
 927		s = per_cpu_ptr(p->stats64, i);
 928		do {
 929			start = u64_stats_fetch_begin_irq(&s->syncp);
 930			tx_packets = s->tx_packets;
 931			tx_bytes = s->tx_bytes;
 932			rx_packets = s->rx_packets;
 933			rx_bytes = s->rx_bytes;
 934		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
 935
 936		stats->tx_packets += tx_packets;
 937		stats->tx_bytes += tx_bytes;
 938		stats->rx_packets += rx_packets;
 939		stats->rx_bytes += rx_bytes;
 940	}
 941}
 942
 943static int dsa_slave_get_rxnfc(struct net_device *dev,
 944			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
 945{
 946	struct dsa_port *dp = dsa_slave_to_port(dev);
 947	struct dsa_switch *ds = dp->ds;
 948
 949	if (!ds->ops->get_rxnfc)
 950		return -EOPNOTSUPP;
 951
 952	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
 953}
 954
 955static int dsa_slave_set_rxnfc(struct net_device *dev,
 956			       struct ethtool_rxnfc *nfc)
 957{
 958	struct dsa_port *dp = dsa_slave_to_port(dev);
 959	struct dsa_switch *ds = dp->ds;
 960
 961	if (!ds->ops->set_rxnfc)
 962		return -EOPNOTSUPP;
 963
 964	return ds->ops->set_rxnfc(ds, dp->index, nfc);
 965}
 966
 967static int dsa_slave_get_ts_info(struct net_device *dev,
 968				 struct ethtool_ts_info *ts)
 969{
 970	struct dsa_slave_priv *p = netdev_priv(dev);
 971	struct dsa_switch *ds = p->dp->ds;
 972
 973	if (!ds->ops->get_ts_info)
 974		return -EOPNOTSUPP;
 975
 976	return ds->ops->get_ts_info(ds, p->dp->index, ts);
 977}
 978
 979static const struct ethtool_ops dsa_slave_ethtool_ops = {
 
 
 980	.get_drvinfo		= dsa_slave_get_drvinfo,
 981	.get_regs_len		= dsa_slave_get_regs_len,
 982	.get_regs		= dsa_slave_get_regs,
 983	.nway_reset		= phy_ethtool_nway_reset,
 984	.get_link		= dsa_slave_get_link,
 985	.get_eeprom_len		= dsa_slave_get_eeprom_len,
 986	.get_eeprom		= dsa_slave_get_eeprom,
 987	.set_eeprom		= dsa_slave_set_eeprom,
 988	.get_strings		= dsa_slave_get_strings,
 989	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
 990	.get_sset_count		= dsa_slave_get_sset_count,
 991	.set_wol		= dsa_slave_set_wol,
 992	.get_wol		= dsa_slave_get_wol,
 993	.set_eee		= dsa_slave_set_eee,
 994	.get_eee		= dsa_slave_get_eee,
 995	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
 996	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
 997	.get_rxnfc		= dsa_slave_get_rxnfc,
 998	.set_rxnfc		= dsa_slave_set_rxnfc,
 999	.get_ts_info		= dsa_slave_get_ts_info,
1000};
1001
1002/* legacy way, bypassing the bridge *****************************************/
1003int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1004		       struct net_device *dev,
1005		       const unsigned char *addr, u16 vid,
1006		       u16 flags)
1007{
1008	struct dsa_port *dp = dsa_slave_to_port(dev);
1009
1010	return dsa_port_fdb_add(dp, addr, vid);
1011}
1012
1013int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1014		       struct net_device *dev,
1015		       const unsigned char *addr, u16 vid)
1016{
1017	struct dsa_port *dp = dsa_slave_to_port(dev);
1018
1019	return dsa_port_fdb_del(dp, addr, vid);
1020}
1021
1022static const struct net_device_ops dsa_slave_netdev_ops = {
1023	.ndo_open	 	= dsa_slave_open,
1024	.ndo_stop		= dsa_slave_close,
1025	.ndo_start_xmit		= dsa_slave_xmit,
1026	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1027	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1028	.ndo_set_mac_address	= dsa_slave_set_mac_address,
1029	.ndo_fdb_add		= dsa_legacy_fdb_add,
1030	.ndo_fdb_del		= dsa_legacy_fdb_del,
1031	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1032	.ndo_do_ioctl		= dsa_slave_ioctl,
1033	.ndo_get_iflink		= dsa_slave_get_iflink,
1034#ifdef CONFIG_NET_POLL_CONTROLLER
1035	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
1036	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
1037	.ndo_poll_controller	= dsa_slave_poll_controller,
1038#endif
1039	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1040	.ndo_setup_tc		= dsa_slave_setup_tc,
1041	.ndo_get_stats64	= dsa_slave_get_stats64,
 
 
 
 
 
 
 
1042};
1043
1044static const struct switchdev_ops dsa_slave_switchdev_ops = {
1045	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
1046	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
1047	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
1048	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
1049};
1050
1051static struct device_type dsa_type = {
1052	.name	= "dsa",
 
1053};
1054
1055static void dsa_slave_adjust_link(struct net_device *dev)
1056{
1057	struct dsa_port *dp = dsa_slave_to_port(dev);
1058	struct dsa_slave_priv *p = netdev_priv(dev);
1059	struct dsa_switch *ds = dp->ds;
1060	unsigned int status_changed = 0;
1061
1062	if (p->old_link != dev->phydev->link) {
1063		status_changed = 1;
1064		p->old_link = dev->phydev->link;
1065	}
1066
1067	if (p->old_duplex != dev->phydev->duplex) {
1068		status_changed = 1;
1069		p->old_duplex = dev->phydev->duplex;
1070	}
1071
1072	if (p->old_pause != dev->phydev->pause) {
1073		status_changed = 1;
1074		p->old_pause = dev->phydev->pause;
1075	}
1076
1077	if (ds->ops->adjust_link && status_changed)
1078		ds->ops->adjust_link(ds, dp->index, dev->phydev);
1079
1080	if (status_changed)
1081		phy_print_status(dev->phydev);
1082}
1083
1084static int dsa_slave_fixed_link_update(struct net_device *dev,
1085				       struct fixed_phy_status *status)
1086{
1087	struct dsa_switch *ds;
1088	struct dsa_port *dp;
1089
1090	if (dev) {
1091		dp = dsa_slave_to_port(dev);
1092		ds = dp->ds;
1093		if (ds->ops->fixed_link_update)
1094			ds->ops->fixed_link_update(ds, dp->index, status);
1095	}
1096
1097	return 0;
1098}
1099
1100/* slave device setup *******************************************************/
1101static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1102{
1103	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1104	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1105	struct dsa_switch *ds = dp->ds;
1106
1107	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1108	if (!slave_dev->phydev) {
1109		netdev_err(slave_dev, "no phy at %d\n", addr);
1110		return -ENODEV;
1111	}
1112
1113	/* Use already configured phy mode */
1114	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1115		p->phy_interface = slave_dev->phydev->interface;
1116
1117	return phy_connect_direct(slave_dev, slave_dev->phydev,
1118				  dsa_slave_adjust_link, p->phy_interface);
1119}
1120
1121static int dsa_slave_phy_setup(struct net_device *slave_dev)
1122{
1123	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1124	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1125	struct device_node *port_dn = dp->dn;
1126	struct dsa_switch *ds = dp->ds;
1127	struct device_node *phy_dn;
1128	bool phy_is_fixed = false;
1129	u32 phy_flags = 0;
1130	int mode, ret;
1131
1132	mode = of_get_phy_mode(port_dn);
1133	if (mode < 0)
1134		mode = PHY_INTERFACE_MODE_NA;
1135	p->phy_interface = mode;
1136
1137	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1138	if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1139		/* In the case of a fixed PHY, the DT node associated
1140		 * to the fixed PHY is the Port DT node
1141		 */
1142		ret = of_phy_register_fixed_link(port_dn);
1143		if (ret) {
1144			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1145			return ret;
1146		}
1147		phy_is_fixed = true;
1148		phy_dn = of_node_get(port_dn);
1149	}
1150
1151	if (ds->ops->get_phy_flags)
1152		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1153
1154	if (phy_dn) {
1155		slave_dev->phydev = of_phy_connect(slave_dev, phy_dn,
1156						   dsa_slave_adjust_link,
1157						   phy_flags,
1158						   p->phy_interface);
1159		of_node_put(phy_dn);
1160	}
1161
1162	if (slave_dev->phydev && phy_is_fixed)
1163		fixed_phy_set_link_update(slave_dev->phydev,
1164					  dsa_slave_fixed_link_update);
1165
1166	/* We could not connect to a designated PHY, so use the switch internal
1167	 * MDIO bus instead
1168	 */
1169	if (!slave_dev->phydev) {
1170		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1171		if (ret) {
1172			netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1173				   dp->index, ret);
1174			if (phy_is_fixed)
1175				of_phy_deregister_fixed_link(port_dn);
1176			return ret;
1177		}
1178	}
1179
1180	phy_attached_info(slave_dev->phydev);
1181
1182	return 0;
1183}
1184
1185static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1186static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1187					    struct netdev_queue *txq,
1188					    void *_unused)
1189{
1190	lockdep_set_class(&txq->_xmit_lock,
1191			  &dsa_slave_netdev_xmit_lock_key);
1192}
1193
1194int dsa_slave_suspend(struct net_device *slave_dev)
1195{
1196	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1197
1198	netif_device_detach(slave_dev);
1199
1200	if (slave_dev->phydev) {
1201		phy_stop(slave_dev->phydev);
1202		p->old_pause = -1;
1203		p->old_link = -1;
1204		p->old_duplex = -1;
1205		phy_suspend(slave_dev->phydev);
1206	}
1207
1208	return 0;
1209}
1210
1211int dsa_slave_resume(struct net_device *slave_dev)
1212{
1213	netif_device_attach(slave_dev);
1214
1215	if (slave_dev->phydev) {
1216		phy_resume(slave_dev->phydev);
1217		phy_start(slave_dev->phydev);
1218	}
1219
1220	return 0;
1221}
1222
1223static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1224{
1225	struct net_device *master = dsa_slave_to_master(dev);
1226	struct dsa_port *dp = dsa_slave_to_port(dev);
1227	struct dsa_notifier_register_info rinfo = {
1228		.switch_number = dp->ds->index,
1229		.port_number = dp->index,
1230		.master = master,
1231		.info.dev = dev,
1232	};
1233
1234	call_dsa_notifiers(val, dev, &rinfo.info);
1235}
1236
1237int dsa_slave_create(struct dsa_port *port)
1238{
1239	const struct dsa_port *cpu_dp = port->cpu_dp;
1240	struct net_device *master = cpu_dp->master;
1241	struct dsa_switch *ds = port->ds;
1242	const char *name = port->name;
1243	struct net_device *slave_dev;
1244	struct dsa_slave_priv *p;
1245	int ret;
1246
1247	if (!ds->num_tx_queues)
1248		ds->num_tx_queues = 1;
1249
1250	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1251				     NET_NAME_UNKNOWN, ether_setup,
1252				     ds->num_tx_queues, 1);
1253	if (slave_dev == NULL)
1254		return -ENOMEM;
1255
1256	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1257	slave_dev->hw_features |= NETIF_F_HW_TC;
1258	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1259	eth_hw_addr_inherit(slave_dev, master);
1260	slave_dev->priv_flags |= IFF_NO_QUEUE;
1261	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1262	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1263	slave_dev->min_mtu = 0;
1264	slave_dev->max_mtu = ETH_MAX_MTU;
1265	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1266
1267	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1268				 NULL);
1269
1270	SET_NETDEV_DEV(slave_dev, port->ds->dev);
1271	slave_dev->dev.of_node = port->dn;
1272	slave_dev->vlan_features = master->vlan_features;
1273
1274	p = netdev_priv(slave_dev);
1275	p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1276	if (!p->stats64) {
1277		free_netdev(slave_dev);
1278		return -ENOMEM;
1279	}
1280	p->dp = port;
1281	INIT_LIST_HEAD(&p->mall_tc_list);
1282	p->xmit = cpu_dp->tag_ops->xmit;
1283
1284	p->old_pause = -1;
1285	p->old_link = -1;
1286	p->old_duplex = -1;
1287
1288	port->slave = slave_dev;
1289
1290	netif_carrier_off(slave_dev);
1291
1292	ret = dsa_slave_phy_setup(slave_dev);
1293	if (ret) {
1294		netdev_err(master, "error %d setting up slave phy\n", ret);
1295		goto out_free;
1296	}
1297
1298	dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1299
1300	ret = register_netdev(slave_dev);
1301	if (ret) {
1302		netdev_err(master, "error %d registering interface %s\n",
1303			   ret, slave_dev->name);
1304		goto out_phy;
 
1305	}
1306
1307	return 0;
1308
1309out_phy:
1310	phy_disconnect(slave_dev->phydev);
1311	if (of_phy_is_fixed_link(port->dn))
1312		of_phy_deregister_fixed_link(port->dn);
1313out_free:
1314	free_percpu(p->stats64);
1315	free_netdev(slave_dev);
1316	port->slave = NULL;
1317	return ret;
1318}
1319
1320void dsa_slave_destroy(struct net_device *slave_dev)
1321{
1322	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1323	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1324	struct device_node *port_dn = dp->dn;
1325
1326	netif_carrier_off(slave_dev);
1327	if (slave_dev->phydev) {
1328		phy_disconnect(slave_dev->phydev);
1329
1330		if (of_phy_is_fixed_link(port_dn))
1331			of_phy_deregister_fixed_link(port_dn);
1332	}
1333	dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1334	unregister_netdev(slave_dev);
1335	free_percpu(p->stats64);
1336	free_netdev(slave_dev);
1337}
1338
1339static bool dsa_slave_dev_check(struct net_device *dev)
1340{
1341	return dev->netdev_ops == &dsa_slave_netdev_ops;
1342}
1343
1344static int dsa_slave_changeupper(struct net_device *dev,
1345				 struct netdev_notifier_changeupper_info *info)
1346{
1347	struct dsa_port *dp = dsa_slave_to_port(dev);
1348	int err = NOTIFY_DONE;
1349
1350	if (netif_is_bridge_master(info->upper_dev)) {
1351		if (info->linking) {
1352			err = dsa_port_bridge_join(dp, info->upper_dev);
1353			err = notifier_from_errno(err);
1354		} else {
1355			dsa_port_bridge_leave(dp, info->upper_dev);
1356			err = NOTIFY_OK;
1357		}
 
1358	}
1359
1360	return err;
1361}
1362
1363static int dsa_slave_netdevice_event(struct notifier_block *nb,
1364				     unsigned long event, void *ptr)
1365{
1366	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1367
1368	if (!dsa_slave_dev_check(dev))
1369		return NOTIFY_DONE;
1370
1371	if (event == NETDEV_CHANGEUPPER)
1372		return dsa_slave_changeupper(dev, ptr);
1373
1374	return NOTIFY_DONE;
1375}
1376
1377struct dsa_switchdev_event_work {
1378	struct work_struct work;
1379	struct switchdev_notifier_fdb_info fdb_info;
1380	struct net_device *dev;
1381	unsigned long event;
1382};
1383
1384static void dsa_slave_switchdev_event_work(struct work_struct *work)
1385{
1386	struct dsa_switchdev_event_work *switchdev_work =
1387		container_of(work, struct dsa_switchdev_event_work, work);
1388	struct net_device *dev = switchdev_work->dev;
1389	struct switchdev_notifier_fdb_info *fdb_info;
1390	struct dsa_port *dp = dsa_slave_to_port(dev);
1391	int err;
1392
1393	rtnl_lock();
1394	switch (switchdev_work->event) {
1395	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1396		fdb_info = &switchdev_work->fdb_info;
1397		err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1398		if (err) {
1399			netdev_dbg(dev, "fdb add failed err=%d\n", err);
1400			break;
1401		}
1402		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1403					 &fdb_info->info);
1404		break;
1405
1406	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1407		fdb_info = &switchdev_work->fdb_info;
1408		err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1409		if (err) {
1410			netdev_dbg(dev, "fdb del failed err=%d\n", err);
1411			dev_close(dev);
1412		}
1413		break;
1414	}
1415	rtnl_unlock();
1416
1417	kfree(switchdev_work->fdb_info.addr);
1418	kfree(switchdev_work);
1419	dev_put(dev);
1420}
1421
1422static int
1423dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1424				  switchdev_work,
1425				  const struct switchdev_notifier_fdb_info *
1426				  fdb_info)
1427{
1428	memcpy(&switchdev_work->fdb_info, fdb_info,
1429	       sizeof(switchdev_work->fdb_info));
1430	switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1431	if (!switchdev_work->fdb_info.addr)
1432		return -ENOMEM;
1433	ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1434			fdb_info->addr);
1435	return 0;
1436}
1437
1438/* Called under rcu_read_lock() */
1439static int dsa_slave_switchdev_event(struct notifier_block *unused,
1440				     unsigned long event, void *ptr)
1441{
1442	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1443	struct dsa_switchdev_event_work *switchdev_work;
1444
1445	if (!dsa_slave_dev_check(dev))
1446		return NOTIFY_DONE;
1447
1448	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1449	if (!switchdev_work)
1450		return NOTIFY_BAD;
1451
1452	INIT_WORK(&switchdev_work->work,
1453		  dsa_slave_switchdev_event_work);
1454	switchdev_work->dev = dev;
1455	switchdev_work->event = event;
1456
1457	switch (event) {
1458	case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1459	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1460		if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1461						      ptr))
1462			goto err_fdb_work_init;
1463		dev_hold(dev);
1464		break;
1465	default:
1466		kfree(switchdev_work);
1467		return NOTIFY_DONE;
1468	}
1469
1470	dsa_schedule_work(&switchdev_work->work);
1471	return NOTIFY_OK;
1472
1473err_fdb_work_init:
1474	kfree(switchdev_work);
1475	return NOTIFY_BAD;
1476}
1477
1478static struct notifier_block dsa_slave_nb __read_mostly = {
1479	.notifier_call  = dsa_slave_netdevice_event,
1480};
1481
1482static struct notifier_block dsa_slave_switchdev_notifier = {
1483	.notifier_call = dsa_slave_switchdev_event,
1484};
1485
1486int dsa_slave_register_notifier(void)
1487{
1488	int err;
1489
1490	err = register_netdevice_notifier(&dsa_slave_nb);
1491	if (err)
1492		return err;
1493
1494	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1495	if (err)
1496		goto err_switchdev_nb;
1497
1498	return 0;
1499
1500err_switchdev_nb:
1501	unregister_netdevice_notifier(&dsa_slave_nb);
1502	return err;
1503}
1504
1505void dsa_slave_unregister_notifier(void)
1506{
1507	int err;
1508
1509	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1510	if (err)
1511		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1512
1513	err = unregister_netdevice_notifier(&dsa_slave_nb);
1514	if (err)
1515		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1516}