Merge branch 'x86-build-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / net / dsa / slave.c
CommitLineData
91da11f8
LB
1/*
2 * net/dsa/slave.c - Slave device handling
e84665c9 3 * Copyright (c) 2008-2009 Marvell Semiconductor
91da11f8
LB
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>
df02c6ff 12#include <linux/etherdevice.h>
91da11f8 13#include <linux/phy.h>
0d8bcdd3
FF
14#include <linux/of_net.h>
15#include <linux/of_mdio.h>
91da11f8
LB
16#include "dsa_priv.h"
17
18/* slave mii_bus handling ***************************************************/
19static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
20{
21 struct dsa_switch *ds = bus->priv;
22
0d8bcdd3 23 if (ds->phys_mii_mask & (1 << addr))
91da11f8
LB
24 return ds->drv->phy_read(ds, addr, reg);
25
26 return 0xffff;
27}
28
29static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
30{
31 struct dsa_switch *ds = bus->priv;
32
0d8bcdd3 33 if (ds->phys_mii_mask & (1 << addr))
91da11f8
LB
34 return ds->drv->phy_write(ds, addr, reg, val);
35
36 return 0;
37}
38
39void dsa_slave_mii_bus_init(struct dsa_switch *ds)
40{
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;
f490be04
FF
45 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
46 ds->index, ds->pd->sw_addr);
b4d2394d 47 ds->slave_mii_bus->parent = ds->master_dev;
91da11f8
LB
48}
49
50
51/* slave device handling ****************************************************/
c0840801
LB
52static int dsa_slave_init(struct net_device *dev)
53{
54 struct dsa_slave_priv *p = netdev_priv(dev);
c0840801 55
e84665c9 56 dev->iflink = p->parent->dst->master_netdev->ifindex;
c0840801
LB
57
58 return 0;
59}
60
91da11f8
LB
61static int dsa_slave_open(struct net_device *dev)
62{
df02c6ff 63 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 64 struct net_device *master = p->parent->dst->master_netdev;
b2f2af21 65 struct dsa_switch *ds = p->parent;
df02c6ff
LB
66 int err;
67
68 if (!(master->flags & IFF_UP))
69 return -ENETDOWN;
70
8feedbb4 71 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
a748ee24 72 err = dev_uc_add(master, dev->dev_addr);
df02c6ff
LB
73 if (err < 0)
74 goto out;
75 }
76
77 if (dev->flags & IFF_ALLMULTI) {
78 err = dev_set_allmulti(master, 1);
79 if (err < 0)
80 goto del_unicast;
81 }
82 if (dev->flags & IFF_PROMISC) {
83 err = dev_set_promiscuity(master, 1);
84 if (err < 0)
85 goto clear_allmulti;
86 }
87
b2f2af21
FF
88 if (ds->drv->port_enable) {
89 err = ds->drv->port_enable(ds, p->port, p->phy);
90 if (err)
91 goto clear_promisc;
92 }
93
f7f1de51
FF
94 if (p->phy)
95 phy_start(p->phy);
96
91da11f8 97 return 0;
df02c6ff 98
b2f2af21
FF
99clear_promisc:
100 if (dev->flags & IFF_PROMISC)
101 dev_set_promiscuity(master, 0);
df02c6ff
LB
102clear_allmulti:
103 if (dev->flags & IFF_ALLMULTI)
104 dev_set_allmulti(master, -1);
105del_unicast:
8feedbb4 106 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
a748ee24 107 dev_uc_del(master, dev->dev_addr);
df02c6ff
LB
108out:
109 return err;
91da11f8
LB
110}
111
112static int dsa_slave_close(struct net_device *dev)
113{
df02c6ff 114 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 115 struct net_device *master = p->parent->dst->master_netdev;
b2f2af21 116 struct dsa_switch *ds = p->parent;
df02c6ff 117
f7f1de51
FF
118 if (p->phy)
119 phy_stop(p->phy);
120
df02c6ff 121 dev_mc_unsync(master, dev);
a748ee24 122 dev_uc_unsync(master, dev);
df02c6ff
LB
123 if (dev->flags & IFF_ALLMULTI)
124 dev_set_allmulti(master, -1);
125 if (dev->flags & IFF_PROMISC)
126 dev_set_promiscuity(master, -1);
127
8feedbb4 128 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
a748ee24 129 dev_uc_del(master, dev->dev_addr);
df02c6ff 130
b2f2af21
FF
131 if (ds->drv->port_disable)
132 ds->drv->port_disable(ds, p->port, p->phy);
133
91da11f8
LB
134 return 0;
135}
136
137static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
138{
139 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 140 struct net_device *master = p->parent->dst->master_netdev;
91da11f8
LB
141
142 if (change & IFF_ALLMULTI)
143 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
144 if (change & IFF_PROMISC)
145 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
146}
147
148static void dsa_slave_set_rx_mode(struct net_device *dev)
149{
150 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 151 struct net_device *master = p->parent->dst->master_netdev;
91da11f8
LB
152
153 dev_mc_sync(master, dev);
a748ee24 154 dev_uc_sync(master, dev);
91da11f8
LB
155}
156
df02c6ff 157static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
91da11f8 158{
df02c6ff 159 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 160 struct net_device *master = p->parent->dst->master_netdev;
df02c6ff
LB
161 struct sockaddr *addr = a;
162 int err;
163
164 if (!is_valid_ether_addr(addr->sa_data))
165 return -EADDRNOTAVAIL;
166
167 if (!(dev->flags & IFF_UP))
168 goto out;
169
8feedbb4 170 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
a748ee24 171 err = dev_uc_add(master, addr->sa_data);
df02c6ff
LB
172 if (err < 0)
173 return err;
174 }
175
8feedbb4 176 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
a748ee24 177 dev_uc_del(master, dev->dev_addr);
df02c6ff
LB
178
179out:
d08f161a 180 ether_addr_copy(dev->dev_addr, addr->sa_data);
91da11f8
LB
181
182 return 0;
183}
184
185static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
186{
187 struct dsa_slave_priv *p = netdev_priv(dev);
91da11f8
LB
188
189 if (p->phy != NULL)
28b04113 190 return phy_mii_ioctl(p->phy, ifr, cmd);
91da11f8
LB
191
192 return -EOPNOTSUPP;
193}
194
3e8a72d1
FF
195static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
196{
197 struct dsa_slave_priv *p = netdev_priv(dev);
3e8a72d1 198
5075314e 199 return p->xmit(skb, dev);
3e8a72d1
FF
200}
201
5aed85ce
FF
202static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
203 struct net_device *dev)
204{
205 struct dsa_slave_priv *p = netdev_priv(dev);
206
207 skb->dev = p->parent->dst->master_netdev;
208 dev_queue_xmit(skb);
209
210 return NETDEV_TX_OK;
211}
212
91da11f8
LB
213
214/* ethtool operations *******************************************************/
215static int
216dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
217{
218 struct dsa_slave_priv *p = netdev_priv(dev);
219 int err;
220
221 err = -EOPNOTSUPP;
222 if (p->phy != NULL) {
223 err = phy_read_status(p->phy);
224 if (err == 0)
225 err = phy_ethtool_gset(p->phy, cmd);
226 }
227
228 return err;
229}
230
231static int
232dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
233{
234 struct dsa_slave_priv *p = netdev_priv(dev);
235
236 if (p->phy != NULL)
237 return phy_ethtool_sset(p->phy, cmd);
238
239 return -EOPNOTSUPP;
240}
241
242static void dsa_slave_get_drvinfo(struct net_device *dev,
243 struct ethtool_drvinfo *drvinfo)
244{
7826d43f
JP
245 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
246 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
247 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
248 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
91da11f8
LB
249}
250
251static int dsa_slave_nway_reset(struct net_device *dev)
252{
253 struct dsa_slave_priv *p = netdev_priv(dev);
254
255 if (p->phy != NULL)
256 return genphy_restart_aneg(p->phy);
257
258 return -EOPNOTSUPP;
259}
260
261static u32 dsa_slave_get_link(struct net_device *dev)
262{
263 struct dsa_slave_priv *p = netdev_priv(dev);
264
265 if (p->phy != NULL) {
266 genphy_update_link(p->phy);
267 return p->phy->link;
268 }
269
270 return -EOPNOTSUPP;
271}
272
273static void dsa_slave_get_strings(struct net_device *dev,
274 uint32_t stringset, uint8_t *data)
275{
276 struct dsa_slave_priv *p = netdev_priv(dev);
277 struct dsa_switch *ds = p->parent;
278
279 if (stringset == ETH_SS_STATS) {
280 int len = ETH_GSTRING_LEN;
281
282 strncpy(data, "tx_packets", len);
283 strncpy(data + len, "tx_bytes", len);
284 strncpy(data + 2 * len, "rx_packets", len);
285 strncpy(data + 3 * len, "rx_bytes", len);
286 if (ds->drv->get_strings != NULL)
287 ds->drv->get_strings(ds, p->port, data + 4 * len);
288 }
289}
290
291static void dsa_slave_get_ethtool_stats(struct net_device *dev,
292 struct ethtool_stats *stats,
293 uint64_t *data)
294{
295 struct dsa_slave_priv *p = netdev_priv(dev);
296 struct dsa_switch *ds = p->parent;
297
298 data[0] = p->dev->stats.tx_packets;
299 data[1] = p->dev->stats.tx_bytes;
300 data[2] = p->dev->stats.rx_packets;
301 data[3] = p->dev->stats.rx_bytes;
302 if (ds->drv->get_ethtool_stats != NULL)
303 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
304}
305
306static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
307{
308 struct dsa_slave_priv *p = netdev_priv(dev);
309 struct dsa_switch *ds = p->parent;
310
311 if (sset == ETH_SS_STATS) {
312 int count;
313
314 count = 4;
315 if (ds->drv->get_sset_count != NULL)
316 count += ds->drv->get_sset_count(ds);
317
318 return count;
319 }
320
321 return -EOPNOTSUPP;
322}
323
19e57c4e
FF
324static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
325{
326 struct dsa_slave_priv *p = netdev_priv(dev);
327 struct dsa_switch *ds = p->parent;
328
329 if (ds->drv->get_wol)
330 ds->drv->get_wol(ds, p->port, w);
331}
332
333static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
334{
335 struct dsa_slave_priv *p = netdev_priv(dev);
336 struct dsa_switch *ds = p->parent;
337 int ret = -EOPNOTSUPP;
338
339 if (ds->drv->set_wol)
340 ret = ds->drv->set_wol(ds, p->port, w);
341
342 return ret;
343}
344
7905288f
FF
345static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
346{
347 struct dsa_slave_priv *p = netdev_priv(dev);
348 struct dsa_switch *ds = p->parent;
349 int ret;
350
351 if (!ds->drv->set_eee)
352 return -EOPNOTSUPP;
353
354 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
355 if (ret)
356 return ret;
357
358 if (p->phy)
359 ret = phy_ethtool_set_eee(p->phy, e);
360
361 return ret;
362}
363
364static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
365{
366 struct dsa_slave_priv *p = netdev_priv(dev);
367 struct dsa_switch *ds = p->parent;
368 int ret;
369
370 if (!ds->drv->get_eee)
371 return -EOPNOTSUPP;
372
373 ret = ds->drv->get_eee(ds, p->port, e);
374 if (ret)
375 return ret;
376
377 if (p->phy)
378 ret = phy_ethtool_get_eee(p->phy, e);
379
380 return ret;
381}
382
91da11f8
LB
383static const struct ethtool_ops dsa_slave_ethtool_ops = {
384 .get_settings = dsa_slave_get_settings,
385 .set_settings = dsa_slave_set_settings,
386 .get_drvinfo = dsa_slave_get_drvinfo,
387 .nway_reset = dsa_slave_nway_reset,
388 .get_link = dsa_slave_get_link,
91da11f8
LB
389 .get_strings = dsa_slave_get_strings,
390 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
391 .get_sset_count = dsa_slave_get_sset_count,
19e57c4e
FF
392 .set_wol = dsa_slave_set_wol,
393 .get_wol = dsa_slave_get_wol,
7905288f
FF
394 .set_eee = dsa_slave_set_eee,
395 .get_eee = dsa_slave_get_eee,
91da11f8
LB
396};
397
3e8a72d1 398static const struct net_device_ops dsa_slave_netdev_ops = {
c0840801 399 .ndo_init = dsa_slave_init,
d442ad4a
SH
400 .ndo_open = dsa_slave_open,
401 .ndo_stop = dsa_slave_close,
3e8a72d1 402 .ndo_start_xmit = dsa_slave_xmit,
d442ad4a
SH
403 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
404 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
d442ad4a
SH
405 .ndo_set_mac_address = dsa_slave_set_mac_address,
406 .ndo_do_ioctl = dsa_slave_ioctl,
407};
91da11f8 408
0d8bcdd3
FF
409static void dsa_slave_adjust_link(struct net_device *dev)
410{
411 struct dsa_slave_priv *p = netdev_priv(dev);
ec9436ba 412 struct dsa_switch *ds = p->parent;
0d8bcdd3
FF
413 unsigned int status_changed = 0;
414
415 if (p->old_link != p->phy->link) {
416 status_changed = 1;
417 p->old_link = p->phy->link;
418 }
419
420 if (p->old_duplex != p->phy->duplex) {
421 status_changed = 1;
422 p->old_duplex = p->phy->duplex;
423 }
424
425 if (p->old_pause != p->phy->pause) {
426 status_changed = 1;
427 p->old_pause = p->phy->pause;
428 }
429
ec9436ba
FF
430 if (ds->drv->adjust_link && status_changed)
431 ds->drv->adjust_link(ds, p->port, p->phy);
432
0d8bcdd3
FF
433 if (status_changed)
434 phy_print_status(p->phy);
435}
436
ce31b31c
FF
437static int dsa_slave_fixed_link_update(struct net_device *dev,
438 struct fixed_phy_status *status)
439{
440 struct dsa_slave_priv *p = netdev_priv(dev);
441 struct dsa_switch *ds = p->parent;
442
443 if (ds->drv->fixed_link_update)
444 ds->drv->fixed_link_update(ds, p->port, status);
445
446 return 0;
447}
448
91da11f8 449/* slave device setup *******************************************************/
0d8bcdd3
FF
450static void dsa_slave_phy_setup(struct dsa_slave_priv *p,
451 struct net_device *slave_dev)
452{
453 struct dsa_switch *ds = p->parent;
454 struct dsa_chip_data *cd = ds->pd;
455 struct device_node *phy_dn, *port_dn;
ce31b31c 456 bool phy_is_fixed = false;
6819563e 457 u32 phy_flags = 0;
0d8bcdd3
FF
458 int ret;
459
460 port_dn = cd->port_dn[p->port];
461 p->phy_interface = of_get_phy_mode(port_dn);
462
463 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
464 if (of_phy_is_fixed_link(port_dn)) {
465 /* In the case of a fixed PHY, the DT node associated
466 * to the fixed PHY is the Port DT node
467 */
468 ret = of_phy_register_fixed_link(port_dn);
469 if (ret) {
470 pr_err("failed to register fixed PHY\n");
471 return;
472 }
ce31b31c 473 phy_is_fixed = true;
0d8bcdd3
FF
474 phy_dn = port_dn;
475 }
476
6819563e
FF
477 if (ds->drv->get_phy_flags)
478 phy_flags = ds->drv->get_phy_flags(ds, p->port);
479
0d8bcdd3
FF
480 if (phy_dn)
481 p->phy = of_phy_connect(slave_dev, phy_dn,
6819563e 482 dsa_slave_adjust_link, phy_flags,
0d8bcdd3
FF
483 p->phy_interface);
484
ce31b31c
FF
485 if (p->phy && phy_is_fixed)
486 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
487
0d8bcdd3
FF
488 /* We could not connect to a designated PHY, so use the switch internal
489 * MDIO bus instead
490 */
491 if (!p->phy)
492 p->phy = ds->slave_mii_bus->phy_map[p->port];
493 else
494 pr_info("attached PHY at address %d [%s]\n",
495 p->phy->addr, p->phy->drv->name);
496}
497
24462549
FF
498int dsa_slave_suspend(struct net_device *slave_dev)
499{
500 struct dsa_slave_priv *p = netdev_priv(slave_dev);
501
502 netif_device_detach(slave_dev);
503
504 if (p->phy) {
505 phy_stop(p->phy);
506 p->old_pause = -1;
507 p->old_link = -1;
508 p->old_duplex = -1;
509 phy_suspend(p->phy);
510 }
511
512 return 0;
513}
514
515int dsa_slave_resume(struct net_device *slave_dev)
516{
517 struct dsa_slave_priv *p = netdev_priv(slave_dev);
518
519 netif_device_attach(slave_dev);
520
521 if (p->phy) {
522 phy_resume(p->phy);
523 phy_start(p->phy);
524 }
525
526 return 0;
527}
528
91da11f8
LB
529struct net_device *
530dsa_slave_create(struct dsa_switch *ds, struct device *parent,
531 int port, char *name)
532{
e84665c9 533 struct net_device *master = ds->dst->master_netdev;
91da11f8
LB
534 struct net_device *slave_dev;
535 struct dsa_slave_priv *p;
536 int ret;
537
c835a677
TG
538 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
539 NET_NAME_UNKNOWN, ether_setup);
91da11f8
LB
540 if (slave_dev == NULL)
541 return slave_dev;
542
543 slave_dev->features = master->vlan_features;
7ad24ea4 544 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2fcc8005 545 eth_hw_addr_inherit(slave_dev, master);
91da11f8 546 slave_dev->tx_queue_len = 0;
3e8a72d1 547 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
d442ad4a 548
5075314e
AD
549 SET_NETDEV_DEV(slave_dev, parent);
550 slave_dev->dev.of_node = ds->pd->port_dn[port];
551 slave_dev->vlan_features = master->vlan_features;
552
553 p = netdev_priv(slave_dev);
554 p->dev = slave_dev;
555 p->parent = ds;
556 p->port = port;
557
e84665c9 558 switch (ds->dst->tag_protocol) {
cf85d08f 559#ifdef CONFIG_NET_DSA_TAG_DSA
ac7a04c3 560 case DSA_TAG_PROTO_DSA:
5075314e 561 p->xmit = dsa_netdev_ops.xmit;
cf85d08f
LB
562 break;
563#endif
91da11f8 564#ifdef CONFIG_NET_DSA_TAG_EDSA
ac7a04c3 565 case DSA_TAG_PROTO_EDSA:
5075314e 566 p->xmit = edsa_netdev_ops.xmit;
91da11f8 567 break;
396138f0
LB
568#endif
569#ifdef CONFIG_NET_DSA_TAG_TRAILER
ac7a04c3 570 case DSA_TAG_PROTO_TRAILER:
5075314e 571 p->xmit = trailer_netdev_ops.xmit;
396138f0 572 break;
5037d532
FF
573#endif
574#ifdef CONFIG_NET_DSA_TAG_BRCM
ac7a04c3 575 case DSA_TAG_PROTO_BRCM:
5075314e 576 p->xmit = brcm_netdev_ops.xmit;
5037d532 577 break;
91da11f8
LB
578#endif
579 default:
5075314e 580 p->xmit = dsa_slave_notag_xmit;
5aed85ce 581 break;
91da11f8 582 }
d442ad4a 583
0d8bcdd3
FF
584 p->old_pause = -1;
585 p->old_link = -1;
586 p->old_duplex = -1;
587
588 dsa_slave_phy_setup(p, slave_dev);
91da11f8
LB
589
590 ret = register_netdev(slave_dev);
591 if (ret) {
592 printk(KERN_ERR "%s: error %d registering interface %s\n",
593 master->name, ret, slave_dev->name);
594 free_netdev(slave_dev);
595 return NULL;
596 }
597
598 netif_carrier_off(slave_dev);
599
600 if (p->phy != NULL) {
6819563e
FF
601 if (ds->drv->get_phy_flags(ds, port))
602 p->phy->dev_flags |= ds->drv->get_phy_flags(ds, port);
603
fb28ad35 604 phy_attach(slave_dev, dev_name(&p->phy->dev),
f9a8f83b 605 PHY_INTERFACE_MODE_GMII);
91da11f8
LB
606
607 p->phy->autoneg = AUTONEG_ENABLE;
608 p->phy->speed = 0;
609 p->phy->duplex = 0;
610 p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
91da11f8
LB
611 }
612
613 return slave_dev;
614}
This page took 0.419395 seconds and 5 git commands to generate.