xref: /openbmc/linux/net/dsa/slave.c (revision e84665c9cb4db963393fafad6fefe5efdd7e4a09)
191da11f8SLennert Buytenhek /*
291da11f8SLennert Buytenhek  * net/dsa/slave.c - Slave device handling
3*e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
491da11f8SLennert Buytenhek  *
591da11f8SLennert Buytenhek  * This program is free software; you can redistribute it and/or modify
691da11f8SLennert Buytenhek  * it under the terms of the GNU General Public License as published by
791da11f8SLennert Buytenhek  * the Free Software Foundation; either version 2 of the License, or
891da11f8SLennert Buytenhek  * (at your option) any later version.
991da11f8SLennert Buytenhek  */
1091da11f8SLennert Buytenhek 
1191da11f8SLennert Buytenhek #include <linux/list.h>
1291da11f8SLennert Buytenhek #include <linux/netdevice.h>
13df02c6ffSLennert Buytenhek #include <linux/etherdevice.h>
1491da11f8SLennert Buytenhek #include <linux/phy.h>
1591da11f8SLennert Buytenhek #include "dsa_priv.h"
1691da11f8SLennert Buytenhek 
1791da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
1891da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
1991da11f8SLennert Buytenhek {
2091da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
2191da11f8SLennert Buytenhek 
22*e84665c9SLennert Buytenhek 	if (ds->phys_port_mask & (1 << addr))
2391da11f8SLennert Buytenhek 		return ds->drv->phy_read(ds, addr, reg);
2491da11f8SLennert Buytenhek 
2591da11f8SLennert Buytenhek 	return 0xffff;
2691da11f8SLennert Buytenhek }
2791da11f8SLennert Buytenhek 
2891da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
2991da11f8SLennert Buytenhek {
3091da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3191da11f8SLennert Buytenhek 
32*e84665c9SLennert Buytenhek 	if (ds->phys_port_mask & (1 << addr))
3391da11f8SLennert Buytenhek 		return ds->drv->phy_write(ds, addr, reg, val);
3491da11f8SLennert Buytenhek 
3591da11f8SLennert Buytenhek 	return 0;
3691da11f8SLennert Buytenhek }
3791da11f8SLennert Buytenhek 
3891da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
3991da11f8SLennert Buytenhek {
4091da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
4191da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
4291da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
4391da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
4491da11f8SLennert Buytenhek 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "%s:%.2x",
4591da11f8SLennert Buytenhek 			ds->master_mii_bus->id, ds->pd->sw_addr);
46*e84665c9SLennert Buytenhek 	ds->slave_mii_bus->parent = &ds->master_mii_bus->dev;
4791da11f8SLennert Buytenhek }
4891da11f8SLennert Buytenhek 
4991da11f8SLennert Buytenhek 
5091da11f8SLennert Buytenhek /* slave device handling ****************************************************/
51c0840801SLennert Buytenhek static int dsa_slave_init(struct net_device *dev)
52c0840801SLennert Buytenhek {
53c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
54c0840801SLennert Buytenhek 
55*e84665c9SLennert Buytenhek 	dev->iflink = p->parent->dst->master_netdev->ifindex;
56c0840801SLennert Buytenhek 
57c0840801SLennert Buytenhek 	return 0;
58c0840801SLennert Buytenhek }
59c0840801SLennert Buytenhek 
6091da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
6191da11f8SLennert Buytenhek {
62df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
63*e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
64df02c6ffSLennert Buytenhek 	int err;
65df02c6ffSLennert Buytenhek 
66df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
67df02c6ffSLennert Buytenhek 		return -ENETDOWN;
68df02c6ffSLennert Buytenhek 
69df02c6ffSLennert Buytenhek 	if (compare_ether_addr(dev->dev_addr, master->dev_addr)) {
70df02c6ffSLennert Buytenhek 		err = dev_unicast_add(master, dev->dev_addr, ETH_ALEN);
71df02c6ffSLennert Buytenhek 		if (err < 0)
72df02c6ffSLennert Buytenhek 			goto out;
73df02c6ffSLennert Buytenhek 	}
74df02c6ffSLennert Buytenhek 
75df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
76df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
77df02c6ffSLennert Buytenhek 		if (err < 0)
78df02c6ffSLennert Buytenhek 			goto del_unicast;
79df02c6ffSLennert Buytenhek 	}
80df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
81df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
82df02c6ffSLennert Buytenhek 		if (err < 0)
83df02c6ffSLennert Buytenhek 			goto clear_allmulti;
84df02c6ffSLennert Buytenhek 	}
85df02c6ffSLennert Buytenhek 
8691da11f8SLennert Buytenhek 	return 0;
87df02c6ffSLennert Buytenhek 
88df02c6ffSLennert Buytenhek clear_allmulti:
89df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
90df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
91df02c6ffSLennert Buytenhek del_unicast:
92df02c6ffSLennert Buytenhek 	if (compare_ether_addr(dev->dev_addr, master->dev_addr))
93df02c6ffSLennert Buytenhek 		dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
94df02c6ffSLennert Buytenhek out:
95df02c6ffSLennert Buytenhek 	return err;
9691da11f8SLennert Buytenhek }
9791da11f8SLennert Buytenhek 
9891da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
9991da11f8SLennert Buytenhek {
100df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
101*e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
102df02c6ffSLennert Buytenhek 
103df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
104df02c6ffSLennert Buytenhek 	dev_unicast_unsync(master, dev);
105df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
106df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
107df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
108df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
109df02c6ffSLennert Buytenhek 
110df02c6ffSLennert Buytenhek 	if (compare_ether_addr(dev->dev_addr, master->dev_addr))
111df02c6ffSLennert Buytenhek 		dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
112df02c6ffSLennert Buytenhek 
11391da11f8SLennert Buytenhek 	return 0;
11491da11f8SLennert Buytenhek }
11591da11f8SLennert Buytenhek 
11691da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
11791da11f8SLennert Buytenhek {
11891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
119*e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
12091da11f8SLennert Buytenhek 
12191da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
12291da11f8SLennert Buytenhek 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
12391da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
12491da11f8SLennert Buytenhek 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
12591da11f8SLennert Buytenhek }
12691da11f8SLennert Buytenhek 
12791da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
12891da11f8SLennert Buytenhek {
12991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
130*e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
13191da11f8SLennert Buytenhek 
13291da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
13391da11f8SLennert Buytenhek 	dev_unicast_sync(master, dev);
13491da11f8SLennert Buytenhek }
13591da11f8SLennert Buytenhek 
136df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
13791da11f8SLennert Buytenhek {
138df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
139*e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
140df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
141df02c6ffSLennert Buytenhek 	int err;
142df02c6ffSLennert Buytenhek 
143df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
144df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
145df02c6ffSLennert Buytenhek 
146df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
147df02c6ffSLennert Buytenhek 		goto out;
148df02c6ffSLennert Buytenhek 
149df02c6ffSLennert Buytenhek 	if (compare_ether_addr(addr->sa_data, master->dev_addr)) {
150df02c6ffSLennert Buytenhek 		err = dev_unicast_add(master, addr->sa_data, ETH_ALEN);
151df02c6ffSLennert Buytenhek 		if (err < 0)
152df02c6ffSLennert Buytenhek 			return err;
153df02c6ffSLennert Buytenhek 	}
154df02c6ffSLennert Buytenhek 
155df02c6ffSLennert Buytenhek 	if (compare_ether_addr(dev->dev_addr, master->dev_addr))
156df02c6ffSLennert Buytenhek 		dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
157df02c6ffSLennert Buytenhek 
158df02c6ffSLennert Buytenhek out:
159df02c6ffSLennert Buytenhek 	memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
16091da11f8SLennert Buytenhek 
16191da11f8SLennert Buytenhek 	return 0;
16291da11f8SLennert Buytenhek }
16391da11f8SLennert Buytenhek 
16491da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
16591da11f8SLennert Buytenhek {
16691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
16791da11f8SLennert Buytenhek 	struct mii_ioctl_data *mii_data = if_mii(ifr);
16891da11f8SLennert Buytenhek 
16991da11f8SLennert Buytenhek 	if (p->phy != NULL)
17091da11f8SLennert Buytenhek 		return phy_mii_ioctl(p->phy, mii_data, cmd);
17191da11f8SLennert Buytenhek 
17291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
17391da11f8SLennert Buytenhek }
17491da11f8SLennert Buytenhek 
17591da11f8SLennert Buytenhek 
17691da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
17791da11f8SLennert Buytenhek static int
17891da11f8SLennert Buytenhek dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
17991da11f8SLennert Buytenhek {
18091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
18191da11f8SLennert Buytenhek 	int err;
18291da11f8SLennert Buytenhek 
18391da11f8SLennert Buytenhek 	err = -EOPNOTSUPP;
18491da11f8SLennert Buytenhek 	if (p->phy != NULL) {
18591da11f8SLennert Buytenhek 		err = phy_read_status(p->phy);
18691da11f8SLennert Buytenhek 		if (err == 0)
18791da11f8SLennert Buytenhek 			err = phy_ethtool_gset(p->phy, cmd);
18891da11f8SLennert Buytenhek 	}
18991da11f8SLennert Buytenhek 
19091da11f8SLennert Buytenhek 	return err;
19191da11f8SLennert Buytenhek }
19291da11f8SLennert Buytenhek 
19391da11f8SLennert Buytenhek static int
19491da11f8SLennert Buytenhek dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
19591da11f8SLennert Buytenhek {
19691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
19791da11f8SLennert Buytenhek 
19891da11f8SLennert Buytenhek 	if (p->phy != NULL)
19991da11f8SLennert Buytenhek 		return phy_ethtool_sset(p->phy, cmd);
20091da11f8SLennert Buytenhek 
20191da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
20291da11f8SLennert Buytenhek }
20391da11f8SLennert Buytenhek 
20491da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
20591da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
20691da11f8SLennert Buytenhek {
20791da11f8SLennert Buytenhek 	strncpy(drvinfo->driver, "dsa", 32);
20891da11f8SLennert Buytenhek 	strncpy(drvinfo->version, dsa_driver_version, 32);
20991da11f8SLennert Buytenhek 	strncpy(drvinfo->fw_version, "N/A", 32);
21091da11f8SLennert Buytenhek 	strncpy(drvinfo->bus_info, "platform", 32);
21191da11f8SLennert Buytenhek }
21291da11f8SLennert Buytenhek 
21391da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
21491da11f8SLennert Buytenhek {
21591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
21691da11f8SLennert Buytenhek 
21791da11f8SLennert Buytenhek 	if (p->phy != NULL)
21891da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
21991da11f8SLennert Buytenhek 
22091da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
22191da11f8SLennert Buytenhek }
22291da11f8SLennert Buytenhek 
22391da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
22491da11f8SLennert Buytenhek {
22591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
22691da11f8SLennert Buytenhek 
22791da11f8SLennert Buytenhek 	if (p->phy != NULL) {
22891da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
22991da11f8SLennert Buytenhek 		return p->phy->link;
23091da11f8SLennert Buytenhek 	}
23191da11f8SLennert Buytenhek 
23291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
23391da11f8SLennert Buytenhek }
23491da11f8SLennert Buytenhek 
23591da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
23691da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
23791da11f8SLennert Buytenhek {
23891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
23991da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
24091da11f8SLennert Buytenhek 
24191da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
24291da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
24391da11f8SLennert Buytenhek 
24491da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
24591da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
24691da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
24791da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
24891da11f8SLennert Buytenhek 		if (ds->drv->get_strings != NULL)
24991da11f8SLennert Buytenhek 			ds->drv->get_strings(ds, p->port, data + 4 * len);
25091da11f8SLennert Buytenhek 	}
25191da11f8SLennert Buytenhek }
25291da11f8SLennert Buytenhek 
25391da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
25491da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
25591da11f8SLennert Buytenhek 					uint64_t *data)
25691da11f8SLennert Buytenhek {
25791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
25891da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
25991da11f8SLennert Buytenhek 
26091da11f8SLennert Buytenhek 	data[0] = p->dev->stats.tx_packets;
26191da11f8SLennert Buytenhek 	data[1] = p->dev->stats.tx_bytes;
26291da11f8SLennert Buytenhek 	data[2] = p->dev->stats.rx_packets;
26391da11f8SLennert Buytenhek 	data[3] = p->dev->stats.rx_bytes;
26491da11f8SLennert Buytenhek 	if (ds->drv->get_ethtool_stats != NULL)
26591da11f8SLennert Buytenhek 		ds->drv->get_ethtool_stats(ds, p->port, data + 4);
26691da11f8SLennert Buytenhek }
26791da11f8SLennert Buytenhek 
26891da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
26991da11f8SLennert Buytenhek {
27091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
27191da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
27291da11f8SLennert Buytenhek 
27391da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
27491da11f8SLennert Buytenhek 		int count;
27591da11f8SLennert Buytenhek 
27691da11f8SLennert Buytenhek 		count = 4;
27791da11f8SLennert Buytenhek 		if (ds->drv->get_sset_count != NULL)
27891da11f8SLennert Buytenhek 			count += ds->drv->get_sset_count(ds);
27991da11f8SLennert Buytenhek 
28091da11f8SLennert Buytenhek 		return count;
28191da11f8SLennert Buytenhek 	}
28291da11f8SLennert Buytenhek 
28391da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
28491da11f8SLennert Buytenhek }
28591da11f8SLennert Buytenhek 
28691da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
28791da11f8SLennert Buytenhek 	.get_settings		= dsa_slave_get_settings,
28891da11f8SLennert Buytenhek 	.set_settings		= dsa_slave_set_settings,
28991da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
29091da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
29191da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
29291da11f8SLennert Buytenhek 	.set_sg			= ethtool_op_set_sg,
29391da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
29491da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
29591da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
29691da11f8SLennert Buytenhek };
29791da11f8SLennert Buytenhek 
298d442ad4aSStephen Hemminger #ifdef CONFIG_NET_DSA_TAG_DSA
299d442ad4aSStephen Hemminger static const struct net_device_ops dsa_netdev_ops = {
300c0840801SLennert Buytenhek 	.ndo_init		= dsa_slave_init,
301d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
302d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
303d442ad4aSStephen Hemminger 	.ndo_start_xmit		= dsa_xmit,
304d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
305d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
306d442ad4aSStephen Hemminger 	.ndo_set_multicast_list = dsa_slave_set_rx_mode,
307d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
308d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
309d442ad4aSStephen Hemminger };
310d442ad4aSStephen Hemminger #endif
311d442ad4aSStephen Hemminger #ifdef CONFIG_NET_DSA_TAG_EDSA
312d442ad4aSStephen Hemminger static const struct net_device_ops edsa_netdev_ops = {
313c0840801SLennert Buytenhek 	.ndo_init		= dsa_slave_init,
314d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
315d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
316d442ad4aSStephen Hemminger 	.ndo_start_xmit		= edsa_xmit,
317d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
318d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
319d442ad4aSStephen Hemminger 	.ndo_set_multicast_list = dsa_slave_set_rx_mode,
320d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
321d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
322d442ad4aSStephen Hemminger };
323d442ad4aSStephen Hemminger #endif
324d442ad4aSStephen Hemminger #ifdef CONFIG_NET_DSA_TAG_TRAILER
325d442ad4aSStephen Hemminger static const struct net_device_ops trailer_netdev_ops = {
326c0840801SLennert Buytenhek 	.ndo_init		= dsa_slave_init,
327d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
328d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
329d442ad4aSStephen Hemminger 	.ndo_start_xmit		= trailer_xmit,
330d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
331d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
332d442ad4aSStephen Hemminger 	.ndo_set_multicast_list = dsa_slave_set_rx_mode,
333d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
334d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
335d442ad4aSStephen Hemminger };
336d442ad4aSStephen Hemminger #endif
33791da11f8SLennert Buytenhek 
33891da11f8SLennert Buytenhek /* slave device setup *******************************************************/
33991da11f8SLennert Buytenhek struct net_device *
34091da11f8SLennert Buytenhek dsa_slave_create(struct dsa_switch *ds, struct device *parent,
34191da11f8SLennert Buytenhek 		 int port, char *name)
34291da11f8SLennert Buytenhek {
343*e84665c9SLennert Buytenhek 	struct net_device *master = ds->dst->master_netdev;
34491da11f8SLennert Buytenhek 	struct net_device *slave_dev;
34591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
34691da11f8SLennert Buytenhek 	int ret;
34791da11f8SLennert Buytenhek 
34891da11f8SLennert Buytenhek 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv),
34991da11f8SLennert Buytenhek 				 name, ether_setup);
35091da11f8SLennert Buytenhek 	if (slave_dev == NULL)
35191da11f8SLennert Buytenhek 		return slave_dev;
35291da11f8SLennert Buytenhek 
35391da11f8SLennert Buytenhek 	slave_dev->features = master->vlan_features;
35491da11f8SLennert Buytenhek 	SET_ETHTOOL_OPS(slave_dev, &dsa_slave_ethtool_ops);
35591da11f8SLennert Buytenhek 	memcpy(slave_dev->dev_addr, master->dev_addr, ETH_ALEN);
35691da11f8SLennert Buytenhek 	slave_dev->tx_queue_len = 0;
357d442ad4aSStephen Hemminger 
358*e84665c9SLennert Buytenhek 	switch (ds->dst->tag_protocol) {
359cf85d08fSLennert Buytenhek #ifdef CONFIG_NET_DSA_TAG_DSA
360cf85d08fSLennert Buytenhek 	case htons(ETH_P_DSA):
361d442ad4aSStephen Hemminger 		slave_dev->netdev_ops = &dsa_netdev_ops;
362cf85d08fSLennert Buytenhek 		break;
363cf85d08fSLennert Buytenhek #endif
36491da11f8SLennert Buytenhek #ifdef CONFIG_NET_DSA_TAG_EDSA
36591da11f8SLennert Buytenhek 	case htons(ETH_P_EDSA):
366d442ad4aSStephen Hemminger 		slave_dev->netdev_ops = &edsa_netdev_ops;
36791da11f8SLennert Buytenhek 		break;
36891da11f8SLennert Buytenhek #endif
369396138f0SLennert Buytenhek #ifdef CONFIG_NET_DSA_TAG_TRAILER
370396138f0SLennert Buytenhek 	case htons(ETH_P_TRAILER):
371d442ad4aSStephen Hemminger 		slave_dev->netdev_ops = &trailer_netdev_ops;
372396138f0SLennert Buytenhek 		break;
373396138f0SLennert Buytenhek #endif
37491da11f8SLennert Buytenhek 	default:
37591da11f8SLennert Buytenhek 		BUG();
37691da11f8SLennert Buytenhek 	}
377d442ad4aSStephen Hemminger 
37891da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
37991da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
38091da11f8SLennert Buytenhek 
38191da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
38291da11f8SLennert Buytenhek 	p->dev = slave_dev;
38391da11f8SLennert Buytenhek 	p->parent = ds;
38491da11f8SLennert Buytenhek 	p->port = port;
38591da11f8SLennert Buytenhek 	p->phy = ds->slave_mii_bus->phy_map[port];
38691da11f8SLennert Buytenhek 
38791da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
38891da11f8SLennert Buytenhek 	if (ret) {
38991da11f8SLennert Buytenhek 		printk(KERN_ERR "%s: error %d registering interface %s\n",
39091da11f8SLennert Buytenhek 				master->name, ret, slave_dev->name);
39191da11f8SLennert Buytenhek 		free_netdev(slave_dev);
39291da11f8SLennert Buytenhek 		return NULL;
39391da11f8SLennert Buytenhek 	}
39491da11f8SLennert Buytenhek 
39591da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
39691da11f8SLennert Buytenhek 
39791da11f8SLennert Buytenhek 	if (p->phy != NULL) {
398fb28ad35SKay Sievers 		phy_attach(slave_dev, dev_name(&p->phy->dev),
39991da11f8SLennert Buytenhek 			   0, PHY_INTERFACE_MODE_GMII);
40091da11f8SLennert Buytenhek 
40191da11f8SLennert Buytenhek 		p->phy->autoneg = AUTONEG_ENABLE;
40291da11f8SLennert Buytenhek 		p->phy->speed = 0;
40391da11f8SLennert Buytenhek 		p->phy->duplex = 0;
40491da11f8SLennert Buytenhek 		p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
40591da11f8SLennert Buytenhek 		phy_start_aneg(p->phy);
40691da11f8SLennert Buytenhek 	}
40791da11f8SLennert Buytenhek 
40891da11f8SLennert Buytenhek 	return slave_dev;
40991da11f8SLennert Buytenhek }
410