net: dsa: add {get, set}_wol callbacks to slave devices
[linux/fpc-iii.git] / net / dsa / slave.c
blob43c1e4ade6892356fbc13ff5c6fefec4ea776f21
1 /*
2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 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/etherdevice.h>
13 #include <linux/phy.h>
14 #include <linux/of_net.h>
15 #include <linux/of_mdio.h>
16 #include "dsa_priv.h"
18 /* slave mii_bus handling ***************************************************/
19 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
21 struct dsa_switch *ds = bus->priv;
23 if (ds->phys_mii_mask & (1 << addr))
24 return ds->drv->phy_read(ds, addr, reg);
26 return 0xffff;
29 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
31 struct dsa_switch *ds = bus->priv;
33 if (ds->phys_mii_mask & (1 << addr))
34 return ds->drv->phy_write(ds, addr, reg, val);
36 return 0;
39 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
41 ds->slave_mii_bus->priv = (void *)ds;
42 ds->slave_mii_bus->name = "dsa slave smi";
43 ds->slave_mii_bus->read = dsa_slave_phy_read;
44 ds->slave_mii_bus->write = dsa_slave_phy_write;
45 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
46 ds->index, ds->pd->sw_addr);
47 ds->slave_mii_bus->parent = ds->master_dev;
51 /* slave device handling ****************************************************/
52 static int dsa_slave_init(struct net_device *dev)
54 struct dsa_slave_priv *p = netdev_priv(dev);
56 dev->iflink = p->parent->dst->master_netdev->ifindex;
58 return 0;
61 static int dsa_slave_open(struct net_device *dev)
63 struct dsa_slave_priv *p = netdev_priv(dev);
64 struct net_device *master = p->parent->dst->master_netdev;
65 int err;
67 if (!(master->flags & IFF_UP))
68 return -ENETDOWN;
70 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
71 err = dev_uc_add(master, dev->dev_addr);
72 if (err < 0)
73 goto out;
76 if (dev->flags & IFF_ALLMULTI) {
77 err = dev_set_allmulti(master, 1);
78 if (err < 0)
79 goto del_unicast;
81 if (dev->flags & IFF_PROMISC) {
82 err = dev_set_promiscuity(master, 1);
83 if (err < 0)
84 goto clear_allmulti;
87 return 0;
89 clear_allmulti:
90 if (dev->flags & IFF_ALLMULTI)
91 dev_set_allmulti(master, -1);
92 del_unicast:
93 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
94 dev_uc_del(master, dev->dev_addr);
95 out:
96 return err;
99 static int dsa_slave_close(struct net_device *dev)
101 struct dsa_slave_priv *p = netdev_priv(dev);
102 struct net_device *master = p->parent->dst->master_netdev;
104 dev_mc_unsync(master, dev);
105 dev_uc_unsync(master, dev);
106 if (dev->flags & IFF_ALLMULTI)
107 dev_set_allmulti(master, -1);
108 if (dev->flags & IFF_PROMISC)
109 dev_set_promiscuity(master, -1);
111 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
112 dev_uc_del(master, dev->dev_addr);
114 return 0;
117 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
119 struct dsa_slave_priv *p = netdev_priv(dev);
120 struct net_device *master = p->parent->dst->master_netdev;
122 if (change & IFF_ALLMULTI)
123 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
124 if (change & IFF_PROMISC)
125 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
128 static void dsa_slave_set_rx_mode(struct net_device *dev)
130 struct dsa_slave_priv *p = netdev_priv(dev);
131 struct net_device *master = p->parent->dst->master_netdev;
133 dev_mc_sync(master, dev);
134 dev_uc_sync(master, dev);
137 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
139 struct dsa_slave_priv *p = netdev_priv(dev);
140 struct net_device *master = p->parent->dst->master_netdev;
141 struct sockaddr *addr = a;
142 int err;
144 if (!is_valid_ether_addr(addr->sa_data))
145 return -EADDRNOTAVAIL;
147 if (!(dev->flags & IFF_UP))
148 goto out;
150 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
151 err = dev_uc_add(master, addr->sa_data);
152 if (err < 0)
153 return err;
156 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
157 dev_uc_del(master, dev->dev_addr);
159 out:
160 ether_addr_copy(dev->dev_addr, addr->sa_data);
162 return 0;
165 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
167 struct dsa_slave_priv *p = netdev_priv(dev);
169 if (p->phy != NULL)
170 return phy_mii_ioctl(p->phy, ifr, cmd);
172 return -EOPNOTSUPP;
175 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
177 struct dsa_slave_priv *p = netdev_priv(dev);
179 return p->xmit(skb, dev);
182 static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
183 struct net_device *dev)
185 struct dsa_slave_priv *p = netdev_priv(dev);
187 skb->dev = p->parent->dst->master_netdev;
188 dev_queue_xmit(skb);
190 return NETDEV_TX_OK;
194 /* ethtool operations *******************************************************/
195 static int
196 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
198 struct dsa_slave_priv *p = netdev_priv(dev);
199 int err;
201 err = -EOPNOTSUPP;
202 if (p->phy != NULL) {
203 err = phy_read_status(p->phy);
204 if (err == 0)
205 err = phy_ethtool_gset(p->phy, cmd);
208 return err;
211 static int
212 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
214 struct dsa_slave_priv *p = netdev_priv(dev);
216 if (p->phy != NULL)
217 return phy_ethtool_sset(p->phy, cmd);
219 return -EOPNOTSUPP;
222 static void dsa_slave_get_drvinfo(struct net_device *dev,
223 struct ethtool_drvinfo *drvinfo)
225 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
226 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
227 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
228 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
231 static int dsa_slave_nway_reset(struct net_device *dev)
233 struct dsa_slave_priv *p = netdev_priv(dev);
235 if (p->phy != NULL)
236 return genphy_restart_aneg(p->phy);
238 return -EOPNOTSUPP;
241 static u32 dsa_slave_get_link(struct net_device *dev)
243 struct dsa_slave_priv *p = netdev_priv(dev);
245 if (p->phy != NULL) {
246 genphy_update_link(p->phy);
247 return p->phy->link;
250 return -EOPNOTSUPP;
253 static void dsa_slave_get_strings(struct net_device *dev,
254 uint32_t stringset, uint8_t *data)
256 struct dsa_slave_priv *p = netdev_priv(dev);
257 struct dsa_switch *ds = p->parent;
259 if (stringset == ETH_SS_STATS) {
260 int len = ETH_GSTRING_LEN;
262 strncpy(data, "tx_packets", len);
263 strncpy(data + len, "tx_bytes", len);
264 strncpy(data + 2 * len, "rx_packets", len);
265 strncpy(data + 3 * len, "rx_bytes", len);
266 if (ds->drv->get_strings != NULL)
267 ds->drv->get_strings(ds, p->port, data + 4 * len);
271 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
272 struct ethtool_stats *stats,
273 uint64_t *data)
275 struct dsa_slave_priv *p = netdev_priv(dev);
276 struct dsa_switch *ds = p->parent;
278 data[0] = p->dev->stats.tx_packets;
279 data[1] = p->dev->stats.tx_bytes;
280 data[2] = p->dev->stats.rx_packets;
281 data[3] = p->dev->stats.rx_bytes;
282 if (ds->drv->get_ethtool_stats != NULL)
283 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
286 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
288 struct dsa_slave_priv *p = netdev_priv(dev);
289 struct dsa_switch *ds = p->parent;
291 if (sset == ETH_SS_STATS) {
292 int count;
294 count = 4;
295 if (ds->drv->get_sset_count != NULL)
296 count += ds->drv->get_sset_count(ds);
298 return count;
301 return -EOPNOTSUPP;
304 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
306 struct dsa_slave_priv *p = netdev_priv(dev);
307 struct dsa_switch *ds = p->parent;
309 if (ds->drv->get_wol)
310 ds->drv->get_wol(ds, p->port, w);
313 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
315 struct dsa_slave_priv *p = netdev_priv(dev);
316 struct dsa_switch *ds = p->parent;
317 int ret = -EOPNOTSUPP;
319 if (ds->drv->set_wol)
320 ret = ds->drv->set_wol(ds, p->port, w);
322 return ret;
325 static const struct ethtool_ops dsa_slave_ethtool_ops = {
326 .get_settings = dsa_slave_get_settings,
327 .set_settings = dsa_slave_set_settings,
328 .get_drvinfo = dsa_slave_get_drvinfo,
329 .nway_reset = dsa_slave_nway_reset,
330 .get_link = dsa_slave_get_link,
331 .get_strings = dsa_slave_get_strings,
332 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
333 .get_sset_count = dsa_slave_get_sset_count,
334 .set_wol = dsa_slave_set_wol,
335 .get_wol = dsa_slave_get_wol,
338 static const struct net_device_ops dsa_slave_netdev_ops = {
339 .ndo_init = dsa_slave_init,
340 .ndo_open = dsa_slave_open,
341 .ndo_stop = dsa_slave_close,
342 .ndo_start_xmit = dsa_slave_xmit,
343 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
344 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
345 .ndo_set_mac_address = dsa_slave_set_mac_address,
346 .ndo_do_ioctl = dsa_slave_ioctl,
349 static void dsa_slave_adjust_link(struct net_device *dev)
351 struct dsa_slave_priv *p = netdev_priv(dev);
352 struct dsa_switch *ds = p->parent;
353 unsigned int status_changed = 0;
355 if (p->old_link != p->phy->link) {
356 status_changed = 1;
357 p->old_link = p->phy->link;
360 if (p->old_duplex != p->phy->duplex) {
361 status_changed = 1;
362 p->old_duplex = p->phy->duplex;
365 if (p->old_pause != p->phy->pause) {
366 status_changed = 1;
367 p->old_pause = p->phy->pause;
370 if (ds->drv->adjust_link && status_changed)
371 ds->drv->adjust_link(ds, p->port, p->phy);
373 if (status_changed)
374 phy_print_status(p->phy);
377 static int dsa_slave_fixed_link_update(struct net_device *dev,
378 struct fixed_phy_status *status)
380 struct dsa_slave_priv *p = netdev_priv(dev);
381 struct dsa_switch *ds = p->parent;
383 if (ds->drv->fixed_link_update)
384 ds->drv->fixed_link_update(ds, p->port, status);
386 return 0;
389 /* slave device setup *******************************************************/
390 static void dsa_slave_phy_setup(struct dsa_slave_priv *p,
391 struct net_device *slave_dev)
393 struct dsa_switch *ds = p->parent;
394 struct dsa_chip_data *cd = ds->pd;
395 struct device_node *phy_dn, *port_dn;
396 bool phy_is_fixed = false;
397 u32 phy_flags = 0;
398 int ret;
400 port_dn = cd->port_dn[p->port];
401 p->phy_interface = of_get_phy_mode(port_dn);
403 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
404 if (of_phy_is_fixed_link(port_dn)) {
405 /* In the case of a fixed PHY, the DT node associated
406 * to the fixed PHY is the Port DT node
408 ret = of_phy_register_fixed_link(port_dn);
409 if (ret) {
410 pr_err("failed to register fixed PHY\n");
411 return;
413 phy_is_fixed = true;
414 phy_dn = port_dn;
417 if (ds->drv->get_phy_flags)
418 phy_flags = ds->drv->get_phy_flags(ds, p->port);
420 if (phy_dn)
421 p->phy = of_phy_connect(slave_dev, phy_dn,
422 dsa_slave_adjust_link, phy_flags,
423 p->phy_interface);
425 if (p->phy && phy_is_fixed)
426 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
428 /* We could not connect to a designated PHY, so use the switch internal
429 * MDIO bus instead
431 if (!p->phy)
432 p->phy = ds->slave_mii_bus->phy_map[p->port];
433 else
434 pr_info("attached PHY at address %d [%s]\n",
435 p->phy->addr, p->phy->drv->name);
438 int dsa_slave_suspend(struct net_device *slave_dev)
440 struct dsa_slave_priv *p = netdev_priv(slave_dev);
442 netif_device_detach(slave_dev);
444 if (p->phy) {
445 phy_stop(p->phy);
446 p->old_pause = -1;
447 p->old_link = -1;
448 p->old_duplex = -1;
449 phy_suspend(p->phy);
452 return 0;
455 int dsa_slave_resume(struct net_device *slave_dev)
457 struct dsa_slave_priv *p = netdev_priv(slave_dev);
459 netif_device_attach(slave_dev);
461 if (p->phy) {
462 phy_resume(p->phy);
463 phy_start(p->phy);
466 return 0;
469 struct net_device *
470 dsa_slave_create(struct dsa_switch *ds, struct device *parent,
471 int port, char *name)
473 struct net_device *master = ds->dst->master_netdev;
474 struct net_device *slave_dev;
475 struct dsa_slave_priv *p;
476 int ret;
478 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
479 NET_NAME_UNKNOWN, ether_setup);
480 if (slave_dev == NULL)
481 return slave_dev;
483 slave_dev->features = master->vlan_features;
484 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
485 eth_hw_addr_inherit(slave_dev, master);
486 slave_dev->tx_queue_len = 0;
487 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
489 SET_NETDEV_DEV(slave_dev, parent);
490 slave_dev->dev.of_node = ds->pd->port_dn[port];
491 slave_dev->vlan_features = master->vlan_features;
493 p = netdev_priv(slave_dev);
494 p->dev = slave_dev;
495 p->parent = ds;
496 p->port = port;
498 switch (ds->dst->tag_protocol) {
499 #ifdef CONFIG_NET_DSA_TAG_DSA
500 case DSA_TAG_PROTO_DSA:
501 p->xmit = dsa_netdev_ops.xmit;
502 break;
503 #endif
504 #ifdef CONFIG_NET_DSA_TAG_EDSA
505 case DSA_TAG_PROTO_EDSA:
506 p->xmit = edsa_netdev_ops.xmit;
507 break;
508 #endif
509 #ifdef CONFIG_NET_DSA_TAG_TRAILER
510 case DSA_TAG_PROTO_TRAILER:
511 p->xmit = trailer_netdev_ops.xmit;
512 break;
513 #endif
514 #ifdef CONFIG_NET_DSA_TAG_BRCM
515 case DSA_TAG_PROTO_BRCM:
516 p->xmit = brcm_netdev_ops.xmit;
517 break;
518 #endif
519 default:
520 p->xmit = dsa_slave_notag_xmit;
521 break;
524 p->old_pause = -1;
525 p->old_link = -1;
526 p->old_duplex = -1;
528 dsa_slave_phy_setup(p, slave_dev);
530 ret = register_netdev(slave_dev);
531 if (ret) {
532 printk(KERN_ERR "%s: error %d registering interface %s\n",
533 master->name, ret, slave_dev->name);
534 free_netdev(slave_dev);
535 return NULL;
538 netif_carrier_off(slave_dev);
540 if (p->phy != NULL) {
541 if (ds->drv->get_phy_flags(ds, port))
542 p->phy->dev_flags |= ds->drv->get_phy_flags(ds, port);
544 phy_attach(slave_dev, dev_name(&p->phy->dev),
545 PHY_INTERFACE_MODE_GMII);
547 p->phy->autoneg = AUTONEG_ENABLE;
548 p->phy->speed = 0;
549 p->phy->duplex = 0;
550 p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
551 phy_start_aneg(p->phy);
554 return slave_dev;