blob: 5688c34253e58e6591e901bb26b9b0a761c505b3 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/slave.c - Slave device handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 *
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>
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080013#include <linux/etherdevice.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#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
Lennert Buytenheke84665c2009-03-20 09:52:09 +000022 if (ds->phys_port_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000023 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
Lennert Buytenheke84665c2009-03-20 09:52:09 +000032 if (ds->phys_port_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000033 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;
Florian Fainellif490be02013-01-21 09:58:50 +000044 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
45 ds->index, ds->pd->sw_addr);
Lennert Buytenheke84665c2009-03-20 09:52:09 +000046 ds->slave_mii_bus->parent = &ds->master_mii_bus->dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000047}
48
49
50/* slave device handling ****************************************************/
Lennert Buytenhekc0840802009-03-20 09:49:49 +000051static int dsa_slave_init(struct net_device *dev)
52{
53 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000054
Lennert Buytenheke84665c2009-03-20 09:52:09 +000055 dev->iflink = p->parent->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000056
57 return 0;
58}
59
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000060static int dsa_slave_open(struct net_device *dev)
61{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080062 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +000063 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080064 int err;
65
66 if (!(master->flags & IFF_UP))
67 return -ENETDOWN;
68
Joe Perches8feedbb2012-05-08 18:56:57 +000069 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000070 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080071 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
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000086 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080087
88clear_allmulti:
89 if (dev->flags & IFF_ALLMULTI)
90 dev_set_allmulti(master, -1);
91del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +000092 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +000093 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080094out:
95 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000096}
97
98static int dsa_slave_close(struct net_device *dev)
99{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800100 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000101 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800102
103 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000104 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800105 if (dev->flags & IFF_ALLMULTI)
106 dev_set_allmulti(master, -1);
107 if (dev->flags & IFF_PROMISC)
108 dev_set_promiscuity(master, -1);
109
Joe Perches8feedbb2012-05-08 18:56:57 +0000110 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000111 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800112
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000113 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);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000119 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000120
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);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000130 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000131
132 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000133 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000134}
135
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800136static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000137{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800138 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000139 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800140 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
Joe Perches8feedbb2012-05-08 18:56:57 +0000149 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000150 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800151 if (err < 0)
152 return err;
153 }
154
Joe Perches8feedbb2012-05-08 18:56:57 +0000155 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000156 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800157
158out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800159 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000160
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);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000167
168 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000169 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000170
171 return -EOPNOTSUPP;
172}
173
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700174static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
175{
176 struct dsa_slave_priv *p = netdev_priv(dev);
177 struct dsa_switch_tree *dst = p->parent->dst;
178
179 return dst->ops->xmit(skb, dev);
180}
181
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000182
183/* ethtool operations *******************************************************/
184static int
185dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
186{
187 struct dsa_slave_priv *p = netdev_priv(dev);
188 int err;
189
190 err = -EOPNOTSUPP;
191 if (p->phy != NULL) {
192 err = phy_read_status(p->phy);
193 if (err == 0)
194 err = phy_ethtool_gset(p->phy, cmd);
195 }
196
197 return err;
198}
199
200static int
201dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
202{
203 struct dsa_slave_priv *p = netdev_priv(dev);
204
205 if (p->phy != NULL)
206 return phy_ethtool_sset(p->phy, cmd);
207
208 return -EOPNOTSUPP;
209}
210
211static void dsa_slave_get_drvinfo(struct net_device *dev,
212 struct ethtool_drvinfo *drvinfo)
213{
Jiri Pirko7826d432013-01-06 00:44:26 +0000214 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
215 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
216 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
217 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218}
219
220static int dsa_slave_nway_reset(struct net_device *dev)
221{
222 struct dsa_slave_priv *p = netdev_priv(dev);
223
224 if (p->phy != NULL)
225 return genphy_restart_aneg(p->phy);
226
227 return -EOPNOTSUPP;
228}
229
230static u32 dsa_slave_get_link(struct net_device *dev)
231{
232 struct dsa_slave_priv *p = netdev_priv(dev);
233
234 if (p->phy != NULL) {
235 genphy_update_link(p->phy);
236 return p->phy->link;
237 }
238
239 return -EOPNOTSUPP;
240}
241
242static void dsa_slave_get_strings(struct net_device *dev,
243 uint32_t stringset, uint8_t *data)
244{
245 struct dsa_slave_priv *p = netdev_priv(dev);
246 struct dsa_switch *ds = p->parent;
247
248 if (stringset == ETH_SS_STATS) {
249 int len = ETH_GSTRING_LEN;
250
251 strncpy(data, "tx_packets", len);
252 strncpy(data + len, "tx_bytes", len);
253 strncpy(data + 2 * len, "rx_packets", len);
254 strncpy(data + 3 * len, "rx_bytes", len);
255 if (ds->drv->get_strings != NULL)
256 ds->drv->get_strings(ds, p->port, data + 4 * len);
257 }
258}
259
260static void dsa_slave_get_ethtool_stats(struct net_device *dev,
261 struct ethtool_stats *stats,
262 uint64_t *data)
263{
264 struct dsa_slave_priv *p = netdev_priv(dev);
265 struct dsa_switch *ds = p->parent;
266
267 data[0] = p->dev->stats.tx_packets;
268 data[1] = p->dev->stats.tx_bytes;
269 data[2] = p->dev->stats.rx_packets;
270 data[3] = p->dev->stats.rx_bytes;
271 if (ds->drv->get_ethtool_stats != NULL)
272 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
273}
274
275static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
276{
277 struct dsa_slave_priv *p = netdev_priv(dev);
278 struct dsa_switch *ds = p->parent;
279
280 if (sset == ETH_SS_STATS) {
281 int count;
282
283 count = 4;
284 if (ds->drv->get_sset_count != NULL)
285 count += ds->drv->get_sset_count(ds);
286
287 return count;
288 }
289
290 return -EOPNOTSUPP;
291}
292
293static const struct ethtool_ops dsa_slave_ethtool_ops = {
294 .get_settings = dsa_slave_get_settings,
295 .set_settings = dsa_slave_set_settings,
296 .get_drvinfo = dsa_slave_get_drvinfo,
297 .nway_reset = dsa_slave_nway_reset,
298 .get_link = dsa_slave_get_link,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000299 .get_strings = dsa_slave_get_strings,
300 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
301 .get_sset_count = dsa_slave_get_sset_count,
302};
303
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700304static const struct net_device_ops dsa_slave_netdev_ops = {
Lennert Buytenhekc0840802009-03-20 09:49:49 +0000305 .ndo_init = dsa_slave_init,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800306 .ndo_open = dsa_slave_open,
307 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700308 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800309 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
310 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800311 .ndo_set_mac_address = dsa_slave_set_mac_address,
312 .ndo_do_ioctl = dsa_slave_ioctl,
313};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000314
315/* slave device setup *******************************************************/
316struct net_device *
317dsa_slave_create(struct dsa_switch *ds, struct device *parent,
318 int port, char *name)
319{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000320 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000321 struct net_device *slave_dev;
322 struct dsa_slave_priv *p;
323 int ret;
324
Tom Gundersenc835a672014-07-14 16:37:24 +0200325 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
326 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000327 if (slave_dev == NULL)
328 return slave_dev;
329
330 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000331 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +0200332 eth_hw_addr_inherit(slave_dev, master);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000333 slave_dev->tx_queue_len = 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700334 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800335
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000336 switch (ds->dst->tag_protocol) {
Lennert Buytenhekcf85d082008-10-07 13:45:02 +0000337#ifdef CONFIG_NET_DSA_TAG_DSA
338 case htons(ETH_P_DSA):
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700339 ds->dst->ops = &dsa_netdev_ops;
Lennert Buytenhekcf85d082008-10-07 13:45:02 +0000340 break;
341#endif
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000342#ifdef CONFIG_NET_DSA_TAG_EDSA
343 case htons(ETH_P_EDSA):
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700344 ds->dst->ops = &edsa_netdev_ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000345 break;
346#endif
Lennert Buytenhek396138f2008-10-07 13:46:07 +0000347#ifdef CONFIG_NET_DSA_TAG_TRAILER
348 case htons(ETH_P_TRAILER):
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700349 ds->dst->ops = &trailer_netdev_ops;
Lennert Buytenhek396138f2008-10-07 13:46:07 +0000350 break;
351#endif
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000352 default:
353 BUG();
354 }
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800355
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000356 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -0700357 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000358 slave_dev->vlan_features = master->vlan_features;
359
360 p = netdev_priv(slave_dev);
361 p->dev = slave_dev;
362 p->parent = ds;
363 p->port = port;
364 p->phy = ds->slave_mii_bus->phy_map[port];
365
366 ret = register_netdev(slave_dev);
367 if (ret) {
368 printk(KERN_ERR "%s: error %d registering interface %s\n",
369 master->name, ret, slave_dev->name);
370 free_netdev(slave_dev);
371 return NULL;
372 }
373
374 netif_carrier_off(slave_dev);
375
376 if (p->phy != NULL) {
Kay Sieversfb28ad32008-11-10 13:55:14 -0800377 phy_attach(slave_dev, dev_name(&p->phy->dev),
Florian Fainellif9a8f832013-01-14 00:52:52 +0000378 PHY_INTERFACE_MODE_GMII);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000379
380 p->phy->autoneg = AUTONEG_ENABLE;
381 p->phy->speed = 0;
382 p->phy->duplex = 0;
383 p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
384 phy_start_aneg(p->phy);
385 }
386
387 return slave_dev;
388}