2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008 Marvell Semiconductor
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.
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/phy.h>
17 /* slave mii_bus handling ***************************************************/
18 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
20 struct dsa_switch *ds = bus->priv;
22 if (ds->valid_port_mask & (1 << addr))
23 return ds->drv->phy_read(ds, addr, reg);
28 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
30 struct dsa_switch *ds = bus->priv;
32 if (ds->valid_port_mask & (1 << addr))
33 return ds->drv->phy_write(ds, addr, reg, val);
38 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
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);
50 /* slave device handling ****************************************************/
51 static int dsa_slave_open(struct net_device *dev)
53 struct dsa_slave_priv *p = netdev_priv(dev);
54 struct net_device *master = p->parent->master_netdev;
57 if (!(master->flags & IFF_UP))
60 if (compare_ether_addr(dev->dev_addr, master->dev_addr)) {
61 err = dev_unicast_add(master, dev->dev_addr, ETH_ALEN);
66 if (dev->flags & IFF_ALLMULTI) {
67 err = dev_set_allmulti(master, 1);
71 if (dev->flags & IFF_PROMISC) {
72 err = dev_set_promiscuity(master, 1);
80 if (dev->flags & IFF_ALLMULTI)
81 dev_set_allmulti(master, -1);
83 if (compare_ether_addr(dev->dev_addr, master->dev_addr))
84 dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
89 static int dsa_slave_close(struct net_device *dev)
91 struct dsa_slave_priv *p = netdev_priv(dev);
92 struct net_device *master = p->parent->master_netdev;
94 dev_mc_unsync(master, dev);
95 dev_unicast_unsync(master, dev);
96 if (dev->flags & IFF_ALLMULTI)
97 dev_set_allmulti(master, -1);
98 if (dev->flags & IFF_PROMISC)
99 dev_set_promiscuity(master, -1);
101 if (compare_ether_addr(dev->dev_addr, master->dev_addr))
102 dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
107 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
109 struct dsa_slave_priv *p = netdev_priv(dev);
110 struct net_device *master = p->parent->master_netdev;
112 if (change & IFF_ALLMULTI)
113 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
114 if (change & IFF_PROMISC)
115 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
118 static void dsa_slave_set_rx_mode(struct net_device *dev)
120 struct dsa_slave_priv *p = netdev_priv(dev);
121 struct net_device *master = p->parent->master_netdev;
123 dev_mc_sync(master, dev);
124 dev_unicast_sync(master, dev);
127 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
129 struct dsa_slave_priv *p = netdev_priv(dev);
130 struct net_device *master = p->parent->master_netdev;
131 struct sockaddr *addr = a;
134 if (!is_valid_ether_addr(addr->sa_data))
135 return -EADDRNOTAVAIL;
137 if (!(dev->flags & IFF_UP))
140 if (compare_ether_addr(addr->sa_data, master->dev_addr)) {
141 err = dev_unicast_add(master, addr->sa_data, ETH_ALEN);
146 if (compare_ether_addr(dev->dev_addr, master->dev_addr))
147 dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
150 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
155 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
157 struct dsa_slave_priv *p = netdev_priv(dev);
158 struct mii_ioctl_data *mii_data = if_mii(ifr);
161 return phy_mii_ioctl(p->phy, mii_data, cmd);
167 /* ethtool operations *******************************************************/
169 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
171 struct dsa_slave_priv *p = netdev_priv(dev);
175 if (p->phy != NULL) {
176 err = phy_read_status(p->phy);
178 err = phy_ethtool_gset(p->phy, cmd);
185 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
187 struct dsa_slave_priv *p = netdev_priv(dev);
190 return phy_ethtool_sset(p->phy, cmd);
195 static void dsa_slave_get_drvinfo(struct net_device *dev,
196 struct ethtool_drvinfo *drvinfo)
198 strncpy(drvinfo->driver, "dsa", 32);
199 strncpy(drvinfo->version, dsa_driver_version, 32);
200 strncpy(drvinfo->fw_version, "N/A", 32);
201 strncpy(drvinfo->bus_info, "platform", 32);
204 static int dsa_slave_nway_reset(struct net_device *dev)
206 struct dsa_slave_priv *p = netdev_priv(dev);
209 return genphy_restart_aneg(p->phy);
214 static u32 dsa_slave_get_link(struct net_device *dev)
216 struct dsa_slave_priv *p = netdev_priv(dev);
218 if (p->phy != NULL) {
219 genphy_update_link(p->phy);
226 static void dsa_slave_get_strings(struct net_device *dev,
227 uint32_t stringset, uint8_t *data)
229 struct dsa_slave_priv *p = netdev_priv(dev);
230 struct dsa_switch *ds = p->parent;
232 if (stringset == ETH_SS_STATS) {
233 int len = ETH_GSTRING_LEN;
235 strncpy(data, "tx_packets", len);
236 strncpy(data + len, "tx_bytes", len);
237 strncpy(data + 2 * len, "rx_packets", len);
238 strncpy(data + 3 * len, "rx_bytes", len);
239 if (ds->drv->get_strings != NULL)
240 ds->drv->get_strings(ds, p->port, data + 4 * len);
244 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
245 struct ethtool_stats *stats,
248 struct dsa_slave_priv *p = netdev_priv(dev);
249 struct dsa_switch *ds = p->parent;
251 data[0] = p->dev->stats.tx_packets;
252 data[1] = p->dev->stats.tx_bytes;
253 data[2] = p->dev->stats.rx_packets;
254 data[3] = p->dev->stats.rx_bytes;
255 if (ds->drv->get_ethtool_stats != NULL)
256 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
259 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
261 struct dsa_slave_priv *p = netdev_priv(dev);
262 struct dsa_switch *ds = p->parent;
264 if (sset == ETH_SS_STATS) {
268 if (ds->drv->get_sset_count != NULL)
269 count += ds->drv->get_sset_count(ds);
277 static const struct ethtool_ops dsa_slave_ethtool_ops = {
278 .get_settings = dsa_slave_get_settings,
279 .set_settings = dsa_slave_set_settings,
280 .get_drvinfo = dsa_slave_get_drvinfo,
281 .nway_reset = dsa_slave_nway_reset,
282 .get_link = dsa_slave_get_link,
283 .set_sg = ethtool_op_set_sg,
284 .get_strings = dsa_slave_get_strings,
285 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
286 .get_sset_count = dsa_slave_get_sset_count,
290 /* slave device setup *******************************************************/
292 dsa_slave_create(struct dsa_switch *ds, struct device *parent,
293 int port, char *name)
295 struct net_device *master = ds->master_netdev;
296 struct net_device *slave_dev;
297 struct dsa_slave_priv *p;
300 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv),
302 if (slave_dev == NULL)
305 slave_dev->features = master->vlan_features;
306 SET_ETHTOOL_OPS(slave_dev, &dsa_slave_ethtool_ops);
307 memcpy(slave_dev->dev_addr, master->dev_addr, ETH_ALEN);
308 slave_dev->tx_queue_len = 0;
309 switch (ds->tag_protocol) {
310 #ifdef CONFIG_NET_DSA_TAG_DSA
311 case htons(ETH_P_DSA):
312 slave_dev->hard_start_xmit = dsa_xmit;
315 #ifdef CONFIG_NET_DSA_TAG_EDSA
316 case htons(ETH_P_EDSA):
317 slave_dev->hard_start_xmit = edsa_xmit;
320 #ifdef CONFIG_NET_DSA_TAG_TRAILER
321 case htons(ETH_P_TRAILER):
322 slave_dev->hard_start_xmit = trailer_xmit;
328 slave_dev->open = dsa_slave_open;
329 slave_dev->stop = dsa_slave_close;
330 slave_dev->change_rx_flags = dsa_slave_change_rx_flags;
331 slave_dev->set_rx_mode = dsa_slave_set_rx_mode;
332 slave_dev->set_multicast_list = dsa_slave_set_rx_mode;
333 slave_dev->set_mac_address = dsa_slave_set_mac_address;
334 slave_dev->do_ioctl = dsa_slave_ioctl;
335 SET_NETDEV_DEV(slave_dev, parent);
336 slave_dev->vlan_features = master->vlan_features;
338 p = netdev_priv(slave_dev);
342 p->phy = ds->slave_mii_bus->phy_map[port];
344 ret = register_netdev(slave_dev);
346 printk(KERN_ERR "%s: error %d registering interface %s\n",
347 master->name, ret, slave_dev->name);
348 free_netdev(slave_dev);
352 netif_carrier_off(slave_dev);
354 if (p->phy != NULL) {
355 phy_attach(slave_dev, dev_name(&p->phy->dev),
356 0, PHY_INTERFACE_MODE_GMII);
358 p->phy->autoneg = AUTONEG_ENABLE;
361 p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
362 phy_start_aneg(p->phy);