xref: /openbmc/linux/net/dsa/slave.c (revision 5aed85cec29882d1c4b4b2a01cb75a99efdbe4ed)
191da11f8SLennert Buytenhek /*
291da11f8SLennert Buytenhek  * net/dsa/slave.c - Slave device handling
3e84665c9SLennert 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>
150d8bcdd3SFlorian Fainelli #include <linux/of_net.h>
160d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h>
1791da11f8SLennert Buytenhek #include "dsa_priv.h"
1891da11f8SLennert Buytenhek 
1991da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
2091da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2191da11f8SLennert Buytenhek {
2291da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
2391da11f8SLennert Buytenhek 
240d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
2591da11f8SLennert Buytenhek 		return ds->drv->phy_read(ds, addr, reg);
2691da11f8SLennert Buytenhek 
2791da11f8SLennert Buytenhek 	return 0xffff;
2891da11f8SLennert Buytenhek }
2991da11f8SLennert Buytenhek 
3091da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
3191da11f8SLennert Buytenhek {
3291da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3391da11f8SLennert Buytenhek 
340d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
3591da11f8SLennert Buytenhek 		return ds->drv->phy_write(ds, addr, reg, val);
3691da11f8SLennert Buytenhek 
3791da11f8SLennert Buytenhek 	return 0;
3891da11f8SLennert Buytenhek }
3991da11f8SLennert Buytenhek 
4091da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
4191da11f8SLennert Buytenhek {
4291da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
4391da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
4491da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
4591da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
46f490be04SFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
47f490be04SFlorian Fainelli 			ds->index, ds->pd->sw_addr);
48e84665c9SLennert Buytenhek 	ds->slave_mii_bus->parent = &ds->master_mii_bus->dev;
4991da11f8SLennert Buytenhek }
5091da11f8SLennert Buytenhek 
5191da11f8SLennert Buytenhek 
5291da11f8SLennert Buytenhek /* slave device handling ****************************************************/
53c0840801SLennert Buytenhek static int dsa_slave_init(struct net_device *dev)
54c0840801SLennert Buytenhek {
55c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
56c0840801SLennert Buytenhek 
57e84665c9SLennert Buytenhek 	dev->iflink = p->parent->dst->master_netdev->ifindex;
58c0840801SLennert Buytenhek 
59c0840801SLennert Buytenhek 	return 0;
60c0840801SLennert Buytenhek }
61c0840801SLennert Buytenhek 
6291da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
6391da11f8SLennert Buytenhek {
64df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
65e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
66df02c6ffSLennert Buytenhek 	int err;
67df02c6ffSLennert Buytenhek 
68df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
69df02c6ffSLennert Buytenhek 		return -ENETDOWN;
70df02c6ffSLennert Buytenhek 
718feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
72a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
73df02c6ffSLennert Buytenhek 		if (err < 0)
74df02c6ffSLennert Buytenhek 			goto out;
75df02c6ffSLennert Buytenhek 	}
76df02c6ffSLennert Buytenhek 
77df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
78df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
79df02c6ffSLennert Buytenhek 		if (err < 0)
80df02c6ffSLennert Buytenhek 			goto del_unicast;
81df02c6ffSLennert Buytenhek 	}
82df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
83df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
84df02c6ffSLennert Buytenhek 		if (err < 0)
85df02c6ffSLennert Buytenhek 			goto clear_allmulti;
86df02c6ffSLennert Buytenhek 	}
87df02c6ffSLennert Buytenhek 
8891da11f8SLennert Buytenhek 	return 0;
89df02c6ffSLennert Buytenhek 
90df02c6ffSLennert Buytenhek clear_allmulti:
91df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
92df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
93df02c6ffSLennert Buytenhek del_unicast:
948feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
95a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
96df02c6ffSLennert Buytenhek out:
97df02c6ffSLennert Buytenhek 	return err;
9891da11f8SLennert Buytenhek }
9991da11f8SLennert Buytenhek 
10091da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
10191da11f8SLennert Buytenhek {
102df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
103e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
104df02c6ffSLennert Buytenhek 
105df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
106a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
107df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
108df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
109df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
110df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
111df02c6ffSLennert Buytenhek 
1128feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
113a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
114df02c6ffSLennert Buytenhek 
11591da11f8SLennert Buytenhek 	return 0;
11691da11f8SLennert Buytenhek }
11791da11f8SLennert Buytenhek 
11891da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
11991da11f8SLennert Buytenhek {
12091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
121e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
12291da11f8SLennert Buytenhek 
12391da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
12491da11f8SLennert Buytenhek 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
12591da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
12691da11f8SLennert Buytenhek 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
12791da11f8SLennert Buytenhek }
12891da11f8SLennert Buytenhek 
12991da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
13091da11f8SLennert Buytenhek {
13191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
132e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
13391da11f8SLennert Buytenhek 
13491da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
135a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
13691da11f8SLennert Buytenhek }
13791da11f8SLennert Buytenhek 
138df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
13991da11f8SLennert Buytenhek {
140df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
141e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
142df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
143df02c6ffSLennert Buytenhek 	int err;
144df02c6ffSLennert Buytenhek 
145df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
146df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
147df02c6ffSLennert Buytenhek 
148df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
149df02c6ffSLennert Buytenhek 		goto out;
150df02c6ffSLennert Buytenhek 
1518feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
152a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
153df02c6ffSLennert Buytenhek 		if (err < 0)
154df02c6ffSLennert Buytenhek 			return err;
155df02c6ffSLennert Buytenhek 	}
156df02c6ffSLennert Buytenhek 
1578feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
158a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
159df02c6ffSLennert Buytenhek 
160df02c6ffSLennert Buytenhek out:
161d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
16291da11f8SLennert Buytenhek 
16391da11f8SLennert Buytenhek 	return 0;
16491da11f8SLennert Buytenhek }
16591da11f8SLennert Buytenhek 
16691da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
16791da11f8SLennert Buytenhek {
16891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
16991da11f8SLennert Buytenhek 
17091da11f8SLennert Buytenhek 	if (p->phy != NULL)
17128b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
17291da11f8SLennert Buytenhek 
17391da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
17491da11f8SLennert Buytenhek }
17591da11f8SLennert Buytenhek 
1763e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
1773e8a72d1SFlorian Fainelli {
1783e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1793e8a72d1SFlorian Fainelli 	struct dsa_switch_tree *dst = p->parent->dst;
1803e8a72d1SFlorian Fainelli 
1813e8a72d1SFlorian Fainelli 	return dst->ops->xmit(skb, dev);
1823e8a72d1SFlorian Fainelli }
1833e8a72d1SFlorian Fainelli 
184*5aed85ceSFlorian Fainelli static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
185*5aed85ceSFlorian Fainelli 					struct net_device *dev)
186*5aed85ceSFlorian Fainelli {
187*5aed85ceSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
188*5aed85ceSFlorian Fainelli 
189*5aed85ceSFlorian Fainelli 	skb->dev = p->parent->dst->master_netdev;
190*5aed85ceSFlorian Fainelli 	dev_queue_xmit(skb);
191*5aed85ceSFlorian Fainelli 
192*5aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
193*5aed85ceSFlorian Fainelli }
194*5aed85ceSFlorian Fainelli 
19591da11f8SLennert Buytenhek 
19691da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
19791da11f8SLennert Buytenhek static int
19891da11f8SLennert Buytenhek dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
19991da11f8SLennert Buytenhek {
20091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
20191da11f8SLennert Buytenhek 	int err;
20291da11f8SLennert Buytenhek 
20391da11f8SLennert Buytenhek 	err = -EOPNOTSUPP;
20491da11f8SLennert Buytenhek 	if (p->phy != NULL) {
20591da11f8SLennert Buytenhek 		err = phy_read_status(p->phy);
20691da11f8SLennert Buytenhek 		if (err == 0)
20791da11f8SLennert Buytenhek 			err = phy_ethtool_gset(p->phy, cmd);
20891da11f8SLennert Buytenhek 	}
20991da11f8SLennert Buytenhek 
21091da11f8SLennert Buytenhek 	return err;
21191da11f8SLennert Buytenhek }
21291da11f8SLennert Buytenhek 
21391da11f8SLennert Buytenhek static int
21491da11f8SLennert Buytenhek dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
21591da11f8SLennert Buytenhek {
21691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
21791da11f8SLennert Buytenhek 
21891da11f8SLennert Buytenhek 	if (p->phy != NULL)
21991da11f8SLennert Buytenhek 		return phy_ethtool_sset(p->phy, cmd);
22091da11f8SLennert Buytenhek 
22191da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
22291da11f8SLennert Buytenhek }
22391da11f8SLennert Buytenhek 
22491da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
22591da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
22691da11f8SLennert Buytenhek {
2277826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
2287826d43fSJiri Pirko 	strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
2297826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
2307826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
23191da11f8SLennert Buytenhek }
23291da11f8SLennert Buytenhek 
23391da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
23491da11f8SLennert Buytenhek {
23591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
23691da11f8SLennert Buytenhek 
23791da11f8SLennert Buytenhek 	if (p->phy != NULL)
23891da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
23991da11f8SLennert Buytenhek 
24091da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
24191da11f8SLennert Buytenhek }
24291da11f8SLennert Buytenhek 
24391da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
24491da11f8SLennert Buytenhek {
24591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
24691da11f8SLennert Buytenhek 
24791da11f8SLennert Buytenhek 	if (p->phy != NULL) {
24891da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
24991da11f8SLennert Buytenhek 		return p->phy->link;
25091da11f8SLennert Buytenhek 	}
25191da11f8SLennert Buytenhek 
25291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
25391da11f8SLennert Buytenhek }
25491da11f8SLennert Buytenhek 
25591da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
25691da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
25791da11f8SLennert Buytenhek {
25891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
25991da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
26091da11f8SLennert Buytenhek 
26191da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
26291da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
26391da11f8SLennert Buytenhek 
26491da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
26591da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
26691da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
26791da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
26891da11f8SLennert Buytenhek 		if (ds->drv->get_strings != NULL)
26991da11f8SLennert Buytenhek 			ds->drv->get_strings(ds, p->port, data + 4 * len);
27091da11f8SLennert Buytenhek 	}
27191da11f8SLennert Buytenhek }
27291da11f8SLennert Buytenhek 
27391da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
27491da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
27591da11f8SLennert Buytenhek 					uint64_t *data)
27691da11f8SLennert Buytenhek {
27791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
27891da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
27991da11f8SLennert Buytenhek 
28091da11f8SLennert Buytenhek 	data[0] = p->dev->stats.tx_packets;
28191da11f8SLennert Buytenhek 	data[1] = p->dev->stats.tx_bytes;
28291da11f8SLennert Buytenhek 	data[2] = p->dev->stats.rx_packets;
28391da11f8SLennert Buytenhek 	data[3] = p->dev->stats.rx_bytes;
28491da11f8SLennert Buytenhek 	if (ds->drv->get_ethtool_stats != NULL)
28591da11f8SLennert Buytenhek 		ds->drv->get_ethtool_stats(ds, p->port, data + 4);
28691da11f8SLennert Buytenhek }
28791da11f8SLennert Buytenhek 
28891da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
28991da11f8SLennert Buytenhek {
29091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
29191da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
29291da11f8SLennert Buytenhek 
29391da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
29491da11f8SLennert Buytenhek 		int count;
29591da11f8SLennert Buytenhek 
29691da11f8SLennert Buytenhek 		count = 4;
29791da11f8SLennert Buytenhek 		if (ds->drv->get_sset_count != NULL)
29891da11f8SLennert Buytenhek 			count += ds->drv->get_sset_count(ds);
29991da11f8SLennert Buytenhek 
30091da11f8SLennert Buytenhek 		return count;
30191da11f8SLennert Buytenhek 	}
30291da11f8SLennert Buytenhek 
30391da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
30491da11f8SLennert Buytenhek }
30591da11f8SLennert Buytenhek 
30691da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
30791da11f8SLennert Buytenhek 	.get_settings		= dsa_slave_get_settings,
30891da11f8SLennert Buytenhek 	.set_settings		= dsa_slave_set_settings,
30991da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
31091da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
31191da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
31291da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
31391da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
31491da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
31591da11f8SLennert Buytenhek };
31691da11f8SLennert Buytenhek 
3173e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
318c0840801SLennert Buytenhek 	.ndo_init		= dsa_slave_init,
319d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
320d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
3213e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
322d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
323d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
324d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
325d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
326d442ad4aSStephen Hemminger };
32791da11f8SLennert Buytenhek 
328*5aed85ceSFlorian Fainelli static const struct dsa_device_ops notag_netdev_ops = {
329*5aed85ceSFlorian Fainelli 	.xmit	= dsa_slave_notag_xmit,
330*5aed85ceSFlorian Fainelli 	.rcv	= NULL,
331*5aed85ceSFlorian Fainelli };
332*5aed85ceSFlorian Fainelli 
3330d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
3340d8bcdd3SFlorian Fainelli {
3350d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
3360d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
3370d8bcdd3SFlorian Fainelli 
3380d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
3390d8bcdd3SFlorian Fainelli 		status_changed = 1;
3400d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
3410d8bcdd3SFlorian Fainelli 	}
3420d8bcdd3SFlorian Fainelli 
3430d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
3440d8bcdd3SFlorian Fainelli 		status_changed = 1;
3450d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
3460d8bcdd3SFlorian Fainelli 	}
3470d8bcdd3SFlorian Fainelli 
3480d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
3490d8bcdd3SFlorian Fainelli 		status_changed = 1;
3500d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
3510d8bcdd3SFlorian Fainelli 	}
3520d8bcdd3SFlorian Fainelli 
3530d8bcdd3SFlorian Fainelli 	if (status_changed)
3540d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
3550d8bcdd3SFlorian Fainelli }
3560d8bcdd3SFlorian Fainelli 
35791da11f8SLennert Buytenhek /* slave device setup *******************************************************/
3580d8bcdd3SFlorian Fainelli static void dsa_slave_phy_setup(struct dsa_slave_priv *p,
3590d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
3600d8bcdd3SFlorian Fainelli {
3610d8bcdd3SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
3620d8bcdd3SFlorian Fainelli 	struct dsa_chip_data *cd = ds->pd;
3630d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
3640d8bcdd3SFlorian Fainelli 	int ret;
3650d8bcdd3SFlorian Fainelli 
3660d8bcdd3SFlorian Fainelli 	port_dn = cd->port_dn[p->port];
3670d8bcdd3SFlorian Fainelli 	p->phy_interface = of_get_phy_mode(port_dn);
3680d8bcdd3SFlorian Fainelli 
3690d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
3700d8bcdd3SFlorian Fainelli 	if (of_phy_is_fixed_link(port_dn)) {
3710d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
3720d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
3730d8bcdd3SFlorian Fainelli 		 */
3740d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
3750d8bcdd3SFlorian Fainelli 		if (ret) {
3760d8bcdd3SFlorian Fainelli 			pr_err("failed to register fixed PHY\n");
3770d8bcdd3SFlorian Fainelli 			return;
3780d8bcdd3SFlorian Fainelli 		}
3790d8bcdd3SFlorian Fainelli 		phy_dn = port_dn;
3800d8bcdd3SFlorian Fainelli 	}
3810d8bcdd3SFlorian Fainelli 
3820d8bcdd3SFlorian Fainelli 	if (phy_dn)
3830d8bcdd3SFlorian Fainelli 		p->phy = of_phy_connect(slave_dev, phy_dn,
3840d8bcdd3SFlorian Fainelli 					dsa_slave_adjust_link, 0,
3850d8bcdd3SFlorian Fainelli 					p->phy_interface);
3860d8bcdd3SFlorian Fainelli 
3870d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
3880d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
3890d8bcdd3SFlorian Fainelli 	 */
3900d8bcdd3SFlorian Fainelli 	if (!p->phy)
3910d8bcdd3SFlorian Fainelli 		p->phy = ds->slave_mii_bus->phy_map[p->port];
3920d8bcdd3SFlorian Fainelli 	else
3930d8bcdd3SFlorian Fainelli 		pr_info("attached PHY at address %d [%s]\n",
3940d8bcdd3SFlorian Fainelli 			p->phy->addr, p->phy->drv->name);
3950d8bcdd3SFlorian Fainelli }
3960d8bcdd3SFlorian Fainelli 
39791da11f8SLennert Buytenhek struct net_device *
39891da11f8SLennert Buytenhek dsa_slave_create(struct dsa_switch *ds, struct device *parent,
39991da11f8SLennert Buytenhek 		 int port, char *name)
40091da11f8SLennert Buytenhek {
401e84665c9SLennert Buytenhek 	struct net_device *master = ds->dst->master_netdev;
40291da11f8SLennert Buytenhek 	struct net_device *slave_dev;
40391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
40491da11f8SLennert Buytenhek 	int ret;
40591da11f8SLennert Buytenhek 
406c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
407c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
40891da11f8SLennert Buytenhek 	if (slave_dev == NULL)
40991da11f8SLennert Buytenhek 		return slave_dev;
41091da11f8SLennert Buytenhek 
41191da11f8SLennert Buytenhek 	slave_dev->features = master->vlan_features;
4127ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
4132fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
41491da11f8SLennert Buytenhek 	slave_dev->tx_queue_len = 0;
4153e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
416d442ad4aSStephen Hemminger 
417e84665c9SLennert Buytenhek 	switch (ds->dst->tag_protocol) {
418cf85d08fSLennert Buytenhek #ifdef CONFIG_NET_DSA_TAG_DSA
419cf85d08fSLennert Buytenhek 	case htons(ETH_P_DSA):
4203e8a72d1SFlorian Fainelli 		ds->dst->ops = &dsa_netdev_ops;
421cf85d08fSLennert Buytenhek 		break;
422cf85d08fSLennert Buytenhek #endif
42391da11f8SLennert Buytenhek #ifdef CONFIG_NET_DSA_TAG_EDSA
42491da11f8SLennert Buytenhek 	case htons(ETH_P_EDSA):
4253e8a72d1SFlorian Fainelli 		ds->dst->ops = &edsa_netdev_ops;
42691da11f8SLennert Buytenhek 		break;
42791da11f8SLennert Buytenhek #endif
428396138f0SLennert Buytenhek #ifdef CONFIG_NET_DSA_TAG_TRAILER
429396138f0SLennert Buytenhek 	case htons(ETH_P_TRAILER):
4303e8a72d1SFlorian Fainelli 		ds->dst->ops = &trailer_netdev_ops;
431396138f0SLennert Buytenhek 		break;
432396138f0SLennert Buytenhek #endif
43391da11f8SLennert Buytenhek 	default:
434*5aed85ceSFlorian Fainelli 		ds->dst->ops = &notag_netdev_ops;
435*5aed85ceSFlorian Fainelli 		break;
43691da11f8SLennert Buytenhek 	}
437d442ad4aSStephen Hemminger 
43891da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
439bd47497aSFlorian Fainelli 	slave_dev->dev.of_node = ds->pd->port_dn[port];
44091da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
44191da11f8SLennert Buytenhek 
44291da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
44391da11f8SLennert Buytenhek 	p->dev = slave_dev;
44491da11f8SLennert Buytenhek 	p->parent = ds;
44591da11f8SLennert Buytenhek 	p->port = port;
4460d8bcdd3SFlorian Fainelli 
4470d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
4480d8bcdd3SFlorian Fainelli 	p->old_link = -1;
4490d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
4500d8bcdd3SFlorian Fainelli 
4510d8bcdd3SFlorian Fainelli 	dsa_slave_phy_setup(p, slave_dev);
45291da11f8SLennert Buytenhek 
45391da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
45491da11f8SLennert Buytenhek 	if (ret) {
45591da11f8SLennert Buytenhek 		printk(KERN_ERR "%s: error %d registering interface %s\n",
45691da11f8SLennert Buytenhek 				master->name, ret, slave_dev->name);
45791da11f8SLennert Buytenhek 		free_netdev(slave_dev);
45891da11f8SLennert Buytenhek 		return NULL;
45991da11f8SLennert Buytenhek 	}
46091da11f8SLennert Buytenhek 
46191da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
46291da11f8SLennert Buytenhek 
46391da11f8SLennert Buytenhek 	if (p->phy != NULL) {
464fb28ad35SKay Sievers 		phy_attach(slave_dev, dev_name(&p->phy->dev),
465f9a8f83bSFlorian Fainelli 			   PHY_INTERFACE_MODE_GMII);
46691da11f8SLennert Buytenhek 
46791da11f8SLennert Buytenhek 		p->phy->autoneg = AUTONEG_ENABLE;
46891da11f8SLennert Buytenhek 		p->phy->speed = 0;
46991da11f8SLennert Buytenhek 		p->phy->duplex = 0;
47091da11f8SLennert Buytenhek 		p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
47191da11f8SLennert Buytenhek 		phy_start_aneg(p->phy);
47291da11f8SLennert Buytenhek 	}
47391da11f8SLennert Buytenhek 
47491da11f8SLennert Buytenhek 	return slave_dev;
47591da11f8SLennert Buytenhek }
476