blob: a3a410d20da07ad44100defd79562f8c6d9ded3f [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008 Marvell Semiconductor
4 *
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
22 if (ds->valid_port_mask & (1 << addr))
23 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
32 if (ds->valid_port_mask & (1 << addr))
33 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;
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);
47}
48
49
50/* slave device handling ****************************************************/
51static int dsa_slave_open(struct net_device *dev)
52{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080053 struct dsa_slave_priv *p = netdev_priv(dev);
54 struct net_device *master = p->parent->master_netdev;
55 int err;
56
57 if (!(master->flags & IFF_UP))
58 return -ENETDOWN;
59
60 if (compare_ether_addr(dev->dev_addr, master->dev_addr)) {
61 err = dev_unicast_add(master, dev->dev_addr, ETH_ALEN);
62 if (err < 0)
63 goto out;
64 }
65
66 if (dev->flags & IFF_ALLMULTI) {
67 err = dev_set_allmulti(master, 1);
68 if (err < 0)
69 goto del_unicast;
70 }
71 if (dev->flags & IFF_PROMISC) {
72 err = dev_set_promiscuity(master, 1);
73 if (err < 0)
74 goto clear_allmulti;
75 }
76
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000077 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080078
79clear_allmulti:
80 if (dev->flags & IFF_ALLMULTI)
81 dev_set_allmulti(master, -1);
82del_unicast:
83 if (compare_ether_addr(dev->dev_addr, master->dev_addr))
84 dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
85out:
86 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000087}
88
89static int dsa_slave_close(struct net_device *dev)
90{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080091 struct dsa_slave_priv *p = netdev_priv(dev);
92 struct net_device *master = p->parent->master_netdev;
93
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);
100
101 if (compare_ether_addr(dev->dev_addr, master->dev_addr))
102 dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
103
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000104 return 0;
105}
106
107static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
108{
109 struct dsa_slave_priv *p = netdev_priv(dev);
110 struct net_device *master = p->parent->master_netdev;
111
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);
116}
117
118static void dsa_slave_set_rx_mode(struct net_device *dev)
119{
120 struct dsa_slave_priv *p = netdev_priv(dev);
121 struct net_device *master = p->parent->master_netdev;
122
123 dev_mc_sync(master, dev);
124 dev_unicast_sync(master, dev);
125}
126
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800127static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000128{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800129 struct dsa_slave_priv *p = netdev_priv(dev);
130 struct net_device *master = p->parent->master_netdev;
131 struct sockaddr *addr = a;
132 int err;
133
134 if (!is_valid_ether_addr(addr->sa_data))
135 return -EADDRNOTAVAIL;
136
137 if (!(dev->flags & IFF_UP))
138 goto out;
139
140 if (compare_ether_addr(addr->sa_data, master->dev_addr)) {
141 err = dev_unicast_add(master, addr->sa_data, ETH_ALEN);
142 if (err < 0)
143 return err;
144 }
145
146 if (compare_ether_addr(dev->dev_addr, master->dev_addr))
147 dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
148
149out:
150 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000151
152 return 0;
153}
154
155static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
156{
157 struct dsa_slave_priv *p = netdev_priv(dev);
158 struct mii_ioctl_data *mii_data = if_mii(ifr);
159
160 if (p->phy != NULL)
161 return phy_mii_ioctl(p->phy, mii_data, cmd);
162
163 return -EOPNOTSUPP;
164}
165
166
167/* ethtool operations *******************************************************/
168static int
169dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
170{
171 struct dsa_slave_priv *p = netdev_priv(dev);
172 int err;
173
174 err = -EOPNOTSUPP;
175 if (p->phy != NULL) {
176 err = phy_read_status(p->phy);
177 if (err == 0)
178 err = phy_ethtool_gset(p->phy, cmd);
179 }
180
181 return err;
182}
183
184static int
185dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
186{
187 struct dsa_slave_priv *p = netdev_priv(dev);
188
189 if (p->phy != NULL)
190 return phy_ethtool_sset(p->phy, cmd);
191
192 return -EOPNOTSUPP;
193}
194
195static void dsa_slave_get_drvinfo(struct net_device *dev,
196 struct ethtool_drvinfo *drvinfo)
197{
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);
202}
203
204static int dsa_slave_nway_reset(struct net_device *dev)
205{
206 struct dsa_slave_priv *p = netdev_priv(dev);
207
208 if (p->phy != NULL)
209 return genphy_restart_aneg(p->phy);
210
211 return -EOPNOTSUPP;
212}
213
214static u32 dsa_slave_get_link(struct net_device *dev)
215{
216 struct dsa_slave_priv *p = netdev_priv(dev);
217
218 if (p->phy != NULL) {
219 genphy_update_link(p->phy);
220 return p->phy->link;
221 }
222
223 return -EOPNOTSUPP;
224}
225
226static void dsa_slave_get_strings(struct net_device *dev,
227 uint32_t stringset, uint8_t *data)
228{
229 struct dsa_slave_priv *p = netdev_priv(dev);
230 struct dsa_switch *ds = p->parent;
231
232 if (stringset == ETH_SS_STATS) {
233 int len = ETH_GSTRING_LEN;
234
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);
241 }
242}
243
244static void dsa_slave_get_ethtool_stats(struct net_device *dev,
245 struct ethtool_stats *stats,
246 uint64_t *data)
247{
248 struct dsa_slave_priv *p = netdev_priv(dev);
249 struct dsa_switch *ds = p->parent;
250
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);
257}
258
259static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
260{
261 struct dsa_slave_priv *p = netdev_priv(dev);
262 struct dsa_switch *ds = p->parent;
263
264 if (sset == ETH_SS_STATS) {
265 int count;
266
267 count = 4;
268 if (ds->drv->get_sset_count != NULL)
269 count += ds->drv->get_sset_count(ds);
270
271 return count;
272 }
273
274 return -EOPNOTSUPP;
275}
276
277static 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,
287};
288
289
290/* slave device setup *******************************************************/
291struct net_device *
292dsa_slave_create(struct dsa_switch *ds, struct device *parent,
293 int port, char *name)
294{
295 struct net_device *master = ds->master_netdev;
296 struct net_device *slave_dev;
297 struct dsa_slave_priv *p;
298 int ret;
299
300 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv),
301 name, ether_setup);
302 if (slave_dev == NULL)
303 return slave_dev;
304
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) {
Lennert Buytenhekcf85d082008-10-07 13:45:02 +0000310#ifdef CONFIG_NET_DSA_TAG_DSA
311 case htons(ETH_P_DSA):
312 slave_dev->hard_start_xmit = dsa_xmit;
313 break;
314#endif
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000315#ifdef CONFIG_NET_DSA_TAG_EDSA
316 case htons(ETH_P_EDSA):
317 slave_dev->hard_start_xmit = edsa_xmit;
318 break;
319#endif
Lennert Buytenhek396138f2008-10-07 13:46:07 +0000320#ifdef CONFIG_NET_DSA_TAG_TRAILER
321 case htons(ETH_P_TRAILER):
322 slave_dev->hard_start_xmit = trailer_xmit;
323 break;
324#endif
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000325 default:
326 BUG();
327 }
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;
337
338 p = netdev_priv(slave_dev);
339 p->dev = slave_dev;
340 p->parent = ds;
341 p->port = port;
342 p->phy = ds->slave_mii_bus->phy_map[port];
343
344 ret = register_netdev(slave_dev);
345 if (ret) {
346 printk(KERN_ERR "%s: error %d registering interface %s\n",
347 master->name, ret, slave_dev->name);
348 free_netdev(slave_dev);
349 return NULL;
350 }
351
352 netif_carrier_off(slave_dev);
353
354 if (p->phy != NULL) {
Kay Sieversfb28ad32008-11-10 13:55:14 -0800355 phy_attach(slave_dev, dev_name(&p->phy->dev),
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000356 0, PHY_INTERFACE_MODE_GMII);
357
358 p->phy->autoneg = AUTONEG_ENABLE;
359 p->phy->speed = 0;
360 p->phy->duplex = 0;
361 p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
362 phy_start_aneg(p->phy);
363 }
364
365 return slave_dev;
366}