Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
[linux-2.6/mini2440.git] / net / dsa / slave.c
bloba68fd79e9eca0456c45b777be2e1a9140a5a8a3f
1 /*
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.
9 */
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/phy.h>
15 #include "dsa_priv.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);
25 return 0xffff;
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);
35 return 0;
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;
55 int err;
57 if (!(master->flags & IFF_UP))
58 return -ENETDOWN;
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;
66 if (dev->flags & IFF_ALLMULTI) {
67 err = dev_set_allmulti(master, 1);
68 if (err < 0)
69 goto del_unicast;
71 if (dev->flags & IFF_PROMISC) {
72 err = dev_set_promiscuity(master, 1);
73 if (err < 0)
74 goto clear_allmulti;
77 return 0;
79 clear_allmulti:
80 if (dev->flags & IFF_ALLMULTI)
81 dev_set_allmulti(master, -1);
82 del_unicast:
83 if (compare_ether_addr(dev->dev_addr, master->dev_addr))
84 dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
85 out:
86 return err;
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);
104 return 0;
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;
132 int err;
134 if (!is_valid_ether_addr(addr->sa_data))
135 return -EADDRNOTAVAIL;
137 if (!(dev->flags & IFF_UP))
138 goto out;
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;
146 if (compare_ether_addr(dev->dev_addr, master->dev_addr))
147 dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
149 out:
150 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
152 return 0;
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);
160 if (p->phy != NULL)
161 return phy_mii_ioctl(p->phy, mii_data, cmd);
163 return -EOPNOTSUPP;
167 /* ethtool operations *******************************************************/
168 static int
169 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
171 struct dsa_slave_priv *p = netdev_priv(dev);
172 int err;
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);
181 return err;
184 static int
185 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
187 struct dsa_slave_priv *p = netdev_priv(dev);
189 if (p->phy != NULL)
190 return phy_ethtool_sset(p->phy, cmd);
192 return -EOPNOTSUPP;
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);
208 if (p->phy != NULL)
209 return genphy_restart_aneg(p->phy);
211 return -EOPNOTSUPP;
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);
220 return p->phy->link;
223 return -EOPNOTSUPP;
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,
246 uint64_t *data)
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) {
265 int count;
267 count = 4;
268 if (ds->drv->get_sset_count != NULL)
269 count += ds->drv->get_sset_count(ds);
271 return count;
274 return -EOPNOTSUPP;
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,
289 #ifdef CONFIG_NET_DSA_TAG_DSA
290 static const struct net_device_ops dsa_netdev_ops = {
291 .ndo_open = dsa_slave_open,
292 .ndo_stop = dsa_slave_close,
293 .ndo_start_xmit = dsa_xmit,
294 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
295 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
296 .ndo_set_multicast_list = dsa_slave_set_rx_mode,
297 .ndo_set_mac_address = dsa_slave_set_mac_address,
298 .ndo_do_ioctl = dsa_slave_ioctl,
300 #endif
301 #ifdef CONFIG_NET_DSA_TAG_EDSA
302 static const struct net_device_ops edsa_netdev_ops = {
303 .ndo_open = dsa_slave_open,
304 .ndo_stop = dsa_slave_close,
305 .ndo_start_xmit = edsa_xmit,
306 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
307 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
308 .ndo_set_multicast_list = dsa_slave_set_rx_mode,
309 .ndo_set_mac_address = dsa_slave_set_mac_address,
310 .ndo_do_ioctl = dsa_slave_ioctl,
312 #endif
313 #ifdef CONFIG_NET_DSA_TAG_TRAILER
314 static const struct net_device_ops trailer_netdev_ops = {
315 .ndo_open = dsa_slave_open,
316 .ndo_stop = dsa_slave_close,
317 .ndo_start_xmit = trailer_xmit,
318 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
319 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
320 .ndo_set_multicast_list = dsa_slave_set_rx_mode,
321 .ndo_set_mac_address = dsa_slave_set_mac_address,
322 .ndo_do_ioctl = dsa_slave_ioctl,
324 #endif
326 /* slave device setup *******************************************************/
327 struct net_device *
328 dsa_slave_create(struct dsa_switch *ds, struct device *parent,
329 int port, char *name)
331 struct net_device *master = ds->master_netdev;
332 struct net_device *slave_dev;
333 struct dsa_slave_priv *p;
334 int ret;
336 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv),
337 name, ether_setup);
338 if (slave_dev == NULL)
339 return slave_dev;
341 slave_dev->features = master->vlan_features;
342 SET_ETHTOOL_OPS(slave_dev, &dsa_slave_ethtool_ops);
343 memcpy(slave_dev->dev_addr, master->dev_addr, ETH_ALEN);
344 slave_dev->tx_queue_len = 0;
346 switch (ds->tag_protocol) {
347 #ifdef CONFIG_NET_DSA_TAG_DSA
348 case htons(ETH_P_DSA):
349 slave_dev->netdev_ops = &dsa_netdev_ops;
350 break;
351 #endif
352 #ifdef CONFIG_NET_DSA_TAG_EDSA
353 case htons(ETH_P_EDSA):
354 slave_dev->netdev_ops = &edsa_netdev_ops;
355 break;
356 #endif
357 #ifdef CONFIG_NET_DSA_TAG_TRAILER
358 case htons(ETH_P_TRAILER):
359 slave_dev->netdev_ops = &trailer_netdev_ops;
360 break;
361 #endif
362 default:
363 BUG();
366 SET_NETDEV_DEV(slave_dev, parent);
367 slave_dev->vlan_features = master->vlan_features;
369 p = netdev_priv(slave_dev);
370 p->dev = slave_dev;
371 p->parent = ds;
372 p->port = port;
373 p->phy = ds->slave_mii_bus->phy_map[port];
375 ret = register_netdev(slave_dev);
376 if (ret) {
377 printk(KERN_ERR "%s: error %d registering interface %s\n",
378 master->name, ret, slave_dev->name);
379 free_netdev(slave_dev);
380 return NULL;
383 netif_carrier_off(slave_dev);
385 if (p->phy != NULL) {
386 phy_attach(slave_dev, dev_name(&p->phy->dev),
387 0, PHY_INTERFACE_MODE_GMII);
389 p->phy->autoneg = AUTONEG_ENABLE;
390 p->phy->speed = 0;
391 p->phy->duplex = 0;
392 p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
393 phy_start_aneg(p->phy);
396 return slave_dev;