xref: /openbmc/linux/net/dsa/slave.c (revision 2446254915a7d6f08bba9a755a34cc0402880472)
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>
12df02c6ffSLennert Buytenhek #include <linux/etherdevice.h>
1391da11f8SLennert Buytenhek #include <linux/phy.h>
140d8bcdd3SFlorian Fainelli #include <linux/of_net.h>
150d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h>
1691da11f8SLennert Buytenhek #include "dsa_priv.h"
1791da11f8SLennert Buytenhek 
1891da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
1991da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2091da11f8SLennert Buytenhek {
2191da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
2291da11f8SLennert Buytenhek 
230d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
2491da11f8SLennert Buytenhek 		return ds->drv->phy_read(ds, addr, reg);
2591da11f8SLennert Buytenhek 
2691da11f8SLennert Buytenhek 	return 0xffff;
2791da11f8SLennert Buytenhek }
2891da11f8SLennert Buytenhek 
2991da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
3091da11f8SLennert Buytenhek {
3191da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3291da11f8SLennert Buytenhek 
330d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
3491da11f8SLennert Buytenhek 		return ds->drv->phy_write(ds, addr, reg, val);
3591da11f8SLennert Buytenhek 
3691da11f8SLennert Buytenhek 	return 0;
3791da11f8SLennert Buytenhek }
3891da11f8SLennert Buytenhek 
3991da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
4091da11f8SLennert Buytenhek {
4191da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
4291da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
4391da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
4491da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
45f490be04SFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
46f490be04SFlorian Fainelli 			ds->index, ds->pd->sw_addr);
47b4d2394dSAlexander Duyck 	ds->slave_mii_bus->parent = ds->master_dev;
4891da11f8SLennert Buytenhek }
4991da11f8SLennert Buytenhek 
5091da11f8SLennert Buytenhek 
5191da11f8SLennert Buytenhek /* slave device handling ****************************************************/
52c0840801SLennert Buytenhek static int dsa_slave_init(struct net_device *dev)
53c0840801SLennert Buytenhek {
54c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
55c0840801SLennert Buytenhek 
56e84665c9SLennert Buytenhek 	dev->iflink = p->parent->dst->master_netdev->ifindex;
57c0840801SLennert Buytenhek 
58c0840801SLennert Buytenhek 	return 0;
59c0840801SLennert Buytenhek }
60c0840801SLennert Buytenhek 
6191da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
6291da11f8SLennert Buytenhek {
63df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
64e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
65df02c6ffSLennert Buytenhek 	int err;
66df02c6ffSLennert Buytenhek 
67df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
68df02c6ffSLennert Buytenhek 		return -ENETDOWN;
69df02c6ffSLennert Buytenhek 
708feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
71a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
72df02c6ffSLennert Buytenhek 		if (err < 0)
73df02c6ffSLennert Buytenhek 			goto out;
74df02c6ffSLennert Buytenhek 	}
75df02c6ffSLennert Buytenhek 
76df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
77df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
78df02c6ffSLennert Buytenhek 		if (err < 0)
79df02c6ffSLennert Buytenhek 			goto del_unicast;
80df02c6ffSLennert Buytenhek 	}
81df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
82df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
83df02c6ffSLennert Buytenhek 		if (err < 0)
84df02c6ffSLennert Buytenhek 			goto clear_allmulti;
85df02c6ffSLennert Buytenhek 	}
86df02c6ffSLennert Buytenhek 
8791da11f8SLennert Buytenhek 	return 0;
88df02c6ffSLennert Buytenhek 
89df02c6ffSLennert Buytenhek clear_allmulti:
90df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
91df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
92df02c6ffSLennert Buytenhek del_unicast:
938feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
94a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
95df02c6ffSLennert Buytenhek out:
96df02c6ffSLennert Buytenhek 	return err;
9791da11f8SLennert Buytenhek }
9891da11f8SLennert Buytenhek 
9991da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
10091da11f8SLennert Buytenhek {
101df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
102e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
103df02c6ffSLennert Buytenhek 
104df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
105a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
106df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
107df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
108df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
109df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
110df02c6ffSLennert Buytenhek 
1118feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
112a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
113df02c6ffSLennert Buytenhek 
11491da11f8SLennert Buytenhek 	return 0;
11591da11f8SLennert Buytenhek }
11691da11f8SLennert Buytenhek 
11791da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
11891da11f8SLennert Buytenhek {
11991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
120e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
12191da11f8SLennert Buytenhek 
12291da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
12391da11f8SLennert Buytenhek 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
12491da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
12591da11f8SLennert Buytenhek 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
12691da11f8SLennert Buytenhek }
12791da11f8SLennert Buytenhek 
12891da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
12991da11f8SLennert Buytenhek {
13091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
131e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
13291da11f8SLennert Buytenhek 
13391da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
134a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
13591da11f8SLennert Buytenhek }
13691da11f8SLennert Buytenhek 
137df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
13891da11f8SLennert Buytenhek {
139df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
140e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
141df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
142df02c6ffSLennert Buytenhek 	int err;
143df02c6ffSLennert Buytenhek 
144df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
145df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
146df02c6ffSLennert Buytenhek 
147df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
148df02c6ffSLennert Buytenhek 		goto out;
149df02c6ffSLennert Buytenhek 
1508feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
151a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
152df02c6ffSLennert Buytenhek 		if (err < 0)
153df02c6ffSLennert Buytenhek 			return err;
154df02c6ffSLennert Buytenhek 	}
155df02c6ffSLennert Buytenhek 
1568feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
157a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
158df02c6ffSLennert Buytenhek 
159df02c6ffSLennert Buytenhek out:
160d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
16191da11f8SLennert Buytenhek 
16291da11f8SLennert Buytenhek 	return 0;
16391da11f8SLennert Buytenhek }
16491da11f8SLennert Buytenhek 
16591da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
16691da11f8SLennert Buytenhek {
16791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
16891da11f8SLennert Buytenhek 
16991da11f8SLennert Buytenhek 	if (p->phy != NULL)
17028b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
17191da11f8SLennert Buytenhek 
17291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
17391da11f8SLennert Buytenhek }
17491da11f8SLennert Buytenhek 
1753e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
1763e8a72d1SFlorian Fainelli {
1773e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1783e8a72d1SFlorian Fainelli 
1795075314eSAlexander Duyck 	return p->xmit(skb, dev);
1803e8a72d1SFlorian Fainelli }
1813e8a72d1SFlorian Fainelli 
1825aed85ceSFlorian Fainelli static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
1835aed85ceSFlorian Fainelli 					struct net_device *dev)
1845aed85ceSFlorian Fainelli {
1855aed85ceSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1865aed85ceSFlorian Fainelli 
1875aed85ceSFlorian Fainelli 	skb->dev = p->parent->dst->master_netdev;
1885aed85ceSFlorian Fainelli 	dev_queue_xmit(skb);
1895aed85ceSFlorian Fainelli 
1905aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
1915aed85ceSFlorian Fainelli }
1925aed85ceSFlorian Fainelli 
19391da11f8SLennert Buytenhek 
19491da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
19591da11f8SLennert Buytenhek static int
19691da11f8SLennert Buytenhek dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
19791da11f8SLennert Buytenhek {
19891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
19991da11f8SLennert Buytenhek 	int err;
20091da11f8SLennert Buytenhek 
20191da11f8SLennert Buytenhek 	err = -EOPNOTSUPP;
20291da11f8SLennert Buytenhek 	if (p->phy != NULL) {
20391da11f8SLennert Buytenhek 		err = phy_read_status(p->phy);
20491da11f8SLennert Buytenhek 		if (err == 0)
20591da11f8SLennert Buytenhek 			err = phy_ethtool_gset(p->phy, cmd);
20691da11f8SLennert Buytenhek 	}
20791da11f8SLennert Buytenhek 
20891da11f8SLennert Buytenhek 	return err;
20991da11f8SLennert Buytenhek }
21091da11f8SLennert Buytenhek 
21191da11f8SLennert Buytenhek static int
21291da11f8SLennert Buytenhek dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
21391da11f8SLennert Buytenhek {
21491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
21591da11f8SLennert Buytenhek 
21691da11f8SLennert Buytenhek 	if (p->phy != NULL)
21791da11f8SLennert Buytenhek 		return phy_ethtool_sset(p->phy, cmd);
21891da11f8SLennert Buytenhek 
21991da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
22091da11f8SLennert Buytenhek }
22191da11f8SLennert Buytenhek 
22291da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
22391da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
22491da11f8SLennert Buytenhek {
2257826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
2267826d43fSJiri Pirko 	strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
2277826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
2287826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
22991da11f8SLennert Buytenhek }
23091da11f8SLennert Buytenhek 
23191da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
23291da11f8SLennert Buytenhek {
23391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
23491da11f8SLennert Buytenhek 
23591da11f8SLennert Buytenhek 	if (p->phy != NULL)
23691da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
23791da11f8SLennert Buytenhek 
23891da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
23991da11f8SLennert Buytenhek }
24091da11f8SLennert Buytenhek 
24191da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
24291da11f8SLennert Buytenhek {
24391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
24491da11f8SLennert Buytenhek 
24591da11f8SLennert Buytenhek 	if (p->phy != NULL) {
24691da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
24791da11f8SLennert Buytenhek 		return p->phy->link;
24891da11f8SLennert Buytenhek 	}
24991da11f8SLennert Buytenhek 
25091da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
25191da11f8SLennert Buytenhek }
25291da11f8SLennert Buytenhek 
25391da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
25491da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
25591da11f8SLennert Buytenhek {
25691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
25791da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
25891da11f8SLennert Buytenhek 
25991da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
26091da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
26191da11f8SLennert Buytenhek 
26291da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
26391da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
26491da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
26591da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
26691da11f8SLennert Buytenhek 		if (ds->drv->get_strings != NULL)
26791da11f8SLennert Buytenhek 			ds->drv->get_strings(ds, p->port, data + 4 * len);
26891da11f8SLennert Buytenhek 	}
26991da11f8SLennert Buytenhek }
27091da11f8SLennert Buytenhek 
27191da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
27291da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
27391da11f8SLennert Buytenhek 					uint64_t *data)
27491da11f8SLennert Buytenhek {
27591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
27691da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
27791da11f8SLennert Buytenhek 
27891da11f8SLennert Buytenhek 	data[0] = p->dev->stats.tx_packets;
27991da11f8SLennert Buytenhek 	data[1] = p->dev->stats.tx_bytes;
28091da11f8SLennert Buytenhek 	data[2] = p->dev->stats.rx_packets;
28191da11f8SLennert Buytenhek 	data[3] = p->dev->stats.rx_bytes;
28291da11f8SLennert Buytenhek 	if (ds->drv->get_ethtool_stats != NULL)
28391da11f8SLennert Buytenhek 		ds->drv->get_ethtool_stats(ds, p->port, data + 4);
28491da11f8SLennert Buytenhek }
28591da11f8SLennert Buytenhek 
28691da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
28791da11f8SLennert Buytenhek {
28891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
28991da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
29091da11f8SLennert Buytenhek 
29191da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
29291da11f8SLennert Buytenhek 		int count;
29391da11f8SLennert Buytenhek 
29491da11f8SLennert Buytenhek 		count = 4;
29591da11f8SLennert Buytenhek 		if (ds->drv->get_sset_count != NULL)
29691da11f8SLennert Buytenhek 			count += ds->drv->get_sset_count(ds);
29791da11f8SLennert Buytenhek 
29891da11f8SLennert Buytenhek 		return count;
29991da11f8SLennert Buytenhek 	}
30091da11f8SLennert Buytenhek 
30191da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
30291da11f8SLennert Buytenhek }
30391da11f8SLennert Buytenhek 
30491da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
30591da11f8SLennert Buytenhek 	.get_settings		= dsa_slave_get_settings,
30691da11f8SLennert Buytenhek 	.set_settings		= dsa_slave_set_settings,
30791da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
30891da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
30991da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
31091da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
31191da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
31291da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
31391da11f8SLennert Buytenhek };
31491da11f8SLennert Buytenhek 
3153e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
316c0840801SLennert Buytenhek 	.ndo_init		= dsa_slave_init,
317d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
318d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
3193e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
320d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
321d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
322d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
323d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
324d442ad4aSStephen Hemminger };
32591da11f8SLennert Buytenhek 
3260d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
3270d8bcdd3SFlorian Fainelli {
3280d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
329ec9436baSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
3300d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
3310d8bcdd3SFlorian Fainelli 
3320d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
3330d8bcdd3SFlorian Fainelli 		status_changed = 1;
3340d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
3350d8bcdd3SFlorian Fainelli 	}
3360d8bcdd3SFlorian Fainelli 
3370d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
3380d8bcdd3SFlorian Fainelli 		status_changed = 1;
3390d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
3400d8bcdd3SFlorian Fainelli 	}
3410d8bcdd3SFlorian Fainelli 
3420d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
3430d8bcdd3SFlorian Fainelli 		status_changed = 1;
3440d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
3450d8bcdd3SFlorian Fainelli 	}
3460d8bcdd3SFlorian Fainelli 
347ec9436baSFlorian Fainelli 	if (ds->drv->adjust_link && status_changed)
348ec9436baSFlorian Fainelli 		ds->drv->adjust_link(ds, p->port, p->phy);
349ec9436baSFlorian Fainelli 
3500d8bcdd3SFlorian Fainelli 	if (status_changed)
3510d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
3520d8bcdd3SFlorian Fainelli }
3530d8bcdd3SFlorian Fainelli 
354ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
355ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
356ce31b31cSFlorian Fainelli {
357ce31b31cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
358ce31b31cSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
359ce31b31cSFlorian Fainelli 
360ce31b31cSFlorian Fainelli 	if (ds->drv->fixed_link_update)
361ce31b31cSFlorian Fainelli 		ds->drv->fixed_link_update(ds, p->port, status);
362ce31b31cSFlorian Fainelli 
363ce31b31cSFlorian Fainelli 	return 0;
364ce31b31cSFlorian Fainelli }
365ce31b31cSFlorian Fainelli 
36691da11f8SLennert Buytenhek /* slave device setup *******************************************************/
3670d8bcdd3SFlorian Fainelli static void dsa_slave_phy_setup(struct dsa_slave_priv *p,
3680d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
3690d8bcdd3SFlorian Fainelli {
3700d8bcdd3SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
3710d8bcdd3SFlorian Fainelli 	struct dsa_chip_data *cd = ds->pd;
3720d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
373ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
3746819563eSFlorian Fainelli 	u32 phy_flags = 0;
3750d8bcdd3SFlorian Fainelli 	int ret;
3760d8bcdd3SFlorian Fainelli 
3770d8bcdd3SFlorian Fainelli 	port_dn = cd->port_dn[p->port];
3780d8bcdd3SFlorian Fainelli 	p->phy_interface = of_get_phy_mode(port_dn);
3790d8bcdd3SFlorian Fainelli 
3800d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
3810d8bcdd3SFlorian Fainelli 	if (of_phy_is_fixed_link(port_dn)) {
3820d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
3830d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
3840d8bcdd3SFlorian Fainelli 		 */
3850d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
3860d8bcdd3SFlorian Fainelli 		if (ret) {
3870d8bcdd3SFlorian Fainelli 			pr_err("failed to register fixed PHY\n");
3880d8bcdd3SFlorian Fainelli 			return;
3890d8bcdd3SFlorian Fainelli 		}
390ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
3910d8bcdd3SFlorian Fainelli 		phy_dn = port_dn;
3920d8bcdd3SFlorian Fainelli 	}
3930d8bcdd3SFlorian Fainelli 
3946819563eSFlorian Fainelli 	if (ds->drv->get_phy_flags)
3956819563eSFlorian Fainelli 		phy_flags = ds->drv->get_phy_flags(ds, p->port);
3966819563eSFlorian Fainelli 
3970d8bcdd3SFlorian Fainelli 	if (phy_dn)
3980d8bcdd3SFlorian Fainelli 		p->phy = of_phy_connect(slave_dev, phy_dn,
3996819563eSFlorian Fainelli 					dsa_slave_adjust_link, phy_flags,
4000d8bcdd3SFlorian Fainelli 					p->phy_interface);
4010d8bcdd3SFlorian Fainelli 
402ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
403ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
404ce31b31cSFlorian Fainelli 
4050d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
4060d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
4070d8bcdd3SFlorian Fainelli 	 */
4080d8bcdd3SFlorian Fainelli 	if (!p->phy)
4090d8bcdd3SFlorian Fainelli 		p->phy = ds->slave_mii_bus->phy_map[p->port];
4100d8bcdd3SFlorian Fainelli 	else
4110d8bcdd3SFlorian Fainelli 		pr_info("attached PHY at address %d [%s]\n",
4120d8bcdd3SFlorian Fainelli 			p->phy->addr, p->phy->drv->name);
4130d8bcdd3SFlorian Fainelli }
4140d8bcdd3SFlorian Fainelli 
415*24462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
416*24462549SFlorian Fainelli {
417*24462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
418*24462549SFlorian Fainelli 
419*24462549SFlorian Fainelli 	netif_device_detach(slave_dev);
420*24462549SFlorian Fainelli 
421*24462549SFlorian Fainelli 	if (p->phy) {
422*24462549SFlorian Fainelli 		phy_stop(p->phy);
423*24462549SFlorian Fainelli 		p->old_pause = -1;
424*24462549SFlorian Fainelli 		p->old_link = -1;
425*24462549SFlorian Fainelli 		p->old_duplex = -1;
426*24462549SFlorian Fainelli 		phy_suspend(p->phy);
427*24462549SFlorian Fainelli 	}
428*24462549SFlorian Fainelli 
429*24462549SFlorian Fainelli 	return 0;
430*24462549SFlorian Fainelli }
431*24462549SFlorian Fainelli 
432*24462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
433*24462549SFlorian Fainelli {
434*24462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
435*24462549SFlorian Fainelli 
436*24462549SFlorian Fainelli 	netif_device_attach(slave_dev);
437*24462549SFlorian Fainelli 
438*24462549SFlorian Fainelli 	if (p->phy) {
439*24462549SFlorian Fainelli 		phy_resume(p->phy);
440*24462549SFlorian Fainelli 		phy_start(p->phy);
441*24462549SFlorian Fainelli 	}
442*24462549SFlorian Fainelli 
443*24462549SFlorian Fainelli 	return 0;
444*24462549SFlorian Fainelli }
445*24462549SFlorian Fainelli 
44691da11f8SLennert Buytenhek struct net_device *
44791da11f8SLennert Buytenhek dsa_slave_create(struct dsa_switch *ds, struct device *parent,
44891da11f8SLennert Buytenhek 		 int port, char *name)
44991da11f8SLennert Buytenhek {
450e84665c9SLennert Buytenhek 	struct net_device *master = ds->dst->master_netdev;
45191da11f8SLennert Buytenhek 	struct net_device *slave_dev;
45291da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
45391da11f8SLennert Buytenhek 	int ret;
45491da11f8SLennert Buytenhek 
455c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
456c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
45791da11f8SLennert Buytenhek 	if (slave_dev == NULL)
45891da11f8SLennert Buytenhek 		return slave_dev;
45991da11f8SLennert Buytenhek 
46091da11f8SLennert Buytenhek 	slave_dev->features = master->vlan_features;
4617ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
4622fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
46391da11f8SLennert Buytenhek 	slave_dev->tx_queue_len = 0;
4643e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
465d442ad4aSStephen Hemminger 
46691da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
467bd47497aSFlorian Fainelli 	slave_dev->dev.of_node = ds->pd->port_dn[port];
46891da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
46991da11f8SLennert Buytenhek 
47091da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
47191da11f8SLennert Buytenhek 	p->dev = slave_dev;
47291da11f8SLennert Buytenhek 	p->parent = ds;
47391da11f8SLennert Buytenhek 	p->port = port;
4740d8bcdd3SFlorian Fainelli 
4755075314eSAlexander Duyck 	switch (ds->dst->tag_protocol) {
4765075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_DSA
4775075314eSAlexander Duyck 	case DSA_TAG_PROTO_DSA:
4785075314eSAlexander Duyck 		p->xmit = dsa_netdev_ops.xmit;
4795075314eSAlexander Duyck 		break;
4805075314eSAlexander Duyck #endif
4815075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_EDSA
4825075314eSAlexander Duyck 	case DSA_TAG_PROTO_EDSA:
4835075314eSAlexander Duyck 		p->xmit = edsa_netdev_ops.xmit;
4845075314eSAlexander Duyck 		break;
4855075314eSAlexander Duyck #endif
4865075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_TRAILER
4875075314eSAlexander Duyck 	case DSA_TAG_PROTO_TRAILER:
4885075314eSAlexander Duyck 		p->xmit = trailer_netdev_ops.xmit;
4895075314eSAlexander Duyck 		break;
4905075314eSAlexander Duyck #endif
4915075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_BRCM
4925075314eSAlexander Duyck 	case DSA_TAG_PROTO_BRCM:
4935075314eSAlexander Duyck 		p->xmit = brcm_netdev_ops.xmit;
4945075314eSAlexander Duyck 		break;
4955075314eSAlexander Duyck #endif
4965075314eSAlexander Duyck 	default:
4975075314eSAlexander Duyck 		p->xmit	= dsa_slave_notag_xmit;
4985075314eSAlexander Duyck 		break;
4995075314eSAlexander Duyck 	}
5005075314eSAlexander Duyck 
5010d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
5020d8bcdd3SFlorian Fainelli 	p->old_link = -1;
5030d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
5040d8bcdd3SFlorian Fainelli 
5050d8bcdd3SFlorian Fainelli 	dsa_slave_phy_setup(p, slave_dev);
50691da11f8SLennert Buytenhek 
50791da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
50891da11f8SLennert Buytenhek 	if (ret) {
50991da11f8SLennert Buytenhek 		printk(KERN_ERR "%s: error %d registering interface %s\n",
51091da11f8SLennert Buytenhek 				master->name, ret, slave_dev->name);
51191da11f8SLennert Buytenhek 		free_netdev(slave_dev);
51291da11f8SLennert Buytenhek 		return NULL;
51391da11f8SLennert Buytenhek 	}
51491da11f8SLennert Buytenhek 
51591da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
51691da11f8SLennert Buytenhek 
51791da11f8SLennert Buytenhek 	if (p->phy != NULL) {
5186819563eSFlorian Fainelli 		if (ds->drv->get_phy_flags(ds, port))
5196819563eSFlorian Fainelli 			p->phy->dev_flags |= ds->drv->get_phy_flags(ds, port);
5206819563eSFlorian Fainelli 
521fb28ad35SKay Sievers 		phy_attach(slave_dev, dev_name(&p->phy->dev),
522f9a8f83bSFlorian Fainelli 			   PHY_INTERFACE_MODE_GMII);
52391da11f8SLennert Buytenhek 
52491da11f8SLennert Buytenhek 		p->phy->autoneg = AUTONEG_ENABLE;
52591da11f8SLennert Buytenhek 		p->phy->speed = 0;
52691da11f8SLennert Buytenhek 		p->phy->duplex = 0;
52791da11f8SLennert Buytenhek 		p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
52891da11f8SLennert Buytenhek 		phy_start_aneg(p->phy);
52991da11f8SLennert Buytenhek 	}
53091da11f8SLennert Buytenhek 
53191da11f8SLennert Buytenhek 	return slave_dev;
53291da11f8SLennert Buytenhek }
533