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