blob: 554c2a961ad5663017281df477fa60380a2336b7 [file] [log] [blame]
Kyle Swenson8d8f6542021-03-15 11:02:55 -06001/*
2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 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/etherdevice.h>
13#include <linux/netdevice.h>
14#include <linux/phy.h>
15#include <linux/phy_fixed.h>
16#include <linux/of_net.h>
17#include <linux/of_mdio.h>
18#include <net/rtnetlink.h>
19#include <net/switchdev.h>
20#include <linux/if_bridge.h>
21#include <linux/netpoll.h>
22#include "dsa_priv.h"
23
24/* slave mii_bus handling ***************************************************/
25static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
26{
27 struct dsa_switch *ds = bus->priv;
28
29 if (ds->phys_mii_mask & (1 << addr))
30 return ds->drv->phy_read(ds, addr, reg);
31
32 return 0xffff;
33}
34
35static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
36{
37 struct dsa_switch *ds = bus->priv;
38
39 if (ds->phys_mii_mask & (1 << addr))
40 return ds->drv->phy_write(ds, addr, reg, val);
41
42 return 0;
43}
44
45void dsa_slave_mii_bus_init(struct dsa_switch *ds)
46{
47 ds->slave_mii_bus->priv = (void *)ds;
48 ds->slave_mii_bus->name = "dsa slave smi";
49 ds->slave_mii_bus->read = dsa_slave_phy_read;
50 ds->slave_mii_bus->write = dsa_slave_phy_write;
51 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
52 ds->index, ds->pd->sw_addr);
53 ds->slave_mii_bus->parent = ds->master_dev;
54 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
55}
56
57
58/* slave device handling ****************************************************/
59static int dsa_slave_get_iflink(const struct net_device *dev)
60{
61 struct dsa_slave_priv *p = netdev_priv(dev);
62
63 return p->parent->dst->master_netdev->ifindex;
64}
65
66static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
67{
68 return !!p->bridge_dev;
69}
70
71static int dsa_slave_open(struct net_device *dev)
72{
73 struct dsa_slave_priv *p = netdev_priv(dev);
74 struct net_device *master = p->parent->dst->master_netdev;
75 struct dsa_switch *ds = p->parent;
76 u8 stp_state = dsa_port_is_bridged(p) ?
77 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
78 int err;
79
80 if (!(master->flags & IFF_UP))
81 return -ENETDOWN;
82
83 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
84 err = dev_uc_add(master, dev->dev_addr);
85 if (err < 0)
86 goto out;
87 }
88
89 if (dev->flags & IFF_ALLMULTI) {
90 err = dev_set_allmulti(master, 1);
91 if (err < 0)
92 goto del_unicast;
93 }
94 if (dev->flags & IFF_PROMISC) {
95 err = dev_set_promiscuity(master, 1);
96 if (err < 0)
97 goto clear_allmulti;
98 }
99
100 if (ds->drv->port_enable) {
101 err = ds->drv->port_enable(ds, p->port, p->phy);
102 if (err)
103 goto clear_promisc;
104 }
105
106 if (ds->drv->port_stp_update)
107 ds->drv->port_stp_update(ds, p->port, stp_state);
108
109 if (p->phy)
110 phy_start(p->phy);
111
112 return 0;
113
114clear_promisc:
115 if (dev->flags & IFF_PROMISC)
116 dev_set_promiscuity(master, -1);
117clear_allmulti:
118 if (dev->flags & IFF_ALLMULTI)
119 dev_set_allmulti(master, -1);
120del_unicast:
121 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
122 dev_uc_del(master, dev->dev_addr);
123out:
124 return err;
125}
126
127static int dsa_slave_close(struct net_device *dev)
128{
129 struct dsa_slave_priv *p = netdev_priv(dev);
130 struct net_device *master = p->parent->dst->master_netdev;
131 struct dsa_switch *ds = p->parent;
132
133 if (p->phy)
134 phy_stop(p->phy);
135
136 dev_mc_unsync(master, dev);
137 dev_uc_unsync(master, dev);
138 if (dev->flags & IFF_ALLMULTI)
139 dev_set_allmulti(master, -1);
140 if (dev->flags & IFF_PROMISC)
141 dev_set_promiscuity(master, -1);
142
143 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
144 dev_uc_del(master, dev->dev_addr);
145
146 if (ds->drv->port_disable)
147 ds->drv->port_disable(ds, p->port, p->phy);
148
149 if (ds->drv->port_stp_update)
150 ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
151
152 return 0;
153}
154
155static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
156{
157 struct dsa_slave_priv *p = netdev_priv(dev);
158 struct net_device *master = p->parent->dst->master_netdev;
159
160 if (change & IFF_ALLMULTI)
161 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
162 if (change & IFF_PROMISC)
163 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
164}
165
166static void dsa_slave_set_rx_mode(struct net_device *dev)
167{
168 struct dsa_slave_priv *p = netdev_priv(dev);
169 struct net_device *master = p->parent->dst->master_netdev;
170
171 dev_mc_sync(master, dev);
172 dev_uc_sync(master, dev);
173}
174
175static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
176{
177 struct dsa_slave_priv *p = netdev_priv(dev);
178 struct net_device *master = p->parent->dst->master_netdev;
179 struct sockaddr *addr = a;
180 int err;
181
182 if (!is_valid_ether_addr(addr->sa_data))
183 return -EADDRNOTAVAIL;
184
185 if (!(dev->flags & IFF_UP))
186 goto out;
187
188 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
189 err = dev_uc_add(master, addr->sa_data);
190 if (err < 0)
191 return err;
192 }
193
194 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
195 dev_uc_del(master, dev->dev_addr);
196
197out:
198 ether_addr_copy(dev->dev_addr, addr->sa_data);
199
200 return 0;
201}
202
203static int dsa_bridge_check_vlan_range(struct dsa_switch *ds,
204 const struct net_device *bridge,
205 u16 vid_begin, u16 vid_end)
206{
207 struct dsa_slave_priv *p;
208 struct net_device *dev, *vlan_br;
209 DECLARE_BITMAP(members, DSA_MAX_PORTS);
210 DECLARE_BITMAP(untagged, DSA_MAX_PORTS);
211 u16 vid;
212 int member, err;
213
214 if (!ds->drv->vlan_getnext || !vid_begin)
215 return -EOPNOTSUPP;
216
217 vid = vid_begin - 1;
218
219 do {
220 err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
221 if (err)
222 break;
223
224 if (vid > vid_end)
225 break;
226
227 member = find_first_bit(members, DSA_MAX_PORTS);
228 if (member == DSA_MAX_PORTS)
229 continue;
230
231 dev = ds->ports[member];
232 p = netdev_priv(dev);
233 vlan_br = p->bridge_dev;
234 if (vlan_br == bridge)
235 continue;
236
237 netdev_dbg(vlan_br, "hardware VLAN %d already in use\n", vid);
238 return -EOPNOTSUPP;
239 } while (vid < vid_end);
240
241 return err == -ENOENT ? 0 : err;
242}
243
244static int dsa_slave_port_vlan_add(struct net_device *dev,
245 const struct switchdev_obj_port_vlan *vlan,
246 struct switchdev_trans *trans)
247{
248 struct dsa_slave_priv *p = netdev_priv(dev);
249 struct dsa_switch *ds = p->parent;
250 int err;
251
252 if (switchdev_trans_ph_prepare(trans)) {
253 if (!ds->drv->port_vlan_prepare || !ds->drv->port_vlan_add)
254 return -EOPNOTSUPP;
255
256 /* If the requested port doesn't belong to the same bridge as
257 * the VLAN members, fallback to software VLAN (hopefully).
258 */
259 err = dsa_bridge_check_vlan_range(ds, p->bridge_dev,
260 vlan->vid_begin,
261 vlan->vid_end);
262 if (err)
263 return err;
264
265 err = ds->drv->port_vlan_prepare(ds, p->port, vlan, trans);
266 if (err)
267 return err;
268 } else {
269 err = ds->drv->port_vlan_add(ds, p->port, vlan, trans);
270 if (err)
271 return err;
272 }
273
274 return 0;
275}
276
277static int dsa_slave_port_vlan_del(struct net_device *dev,
278 const struct switchdev_obj_port_vlan *vlan)
279{
280 struct dsa_slave_priv *p = netdev_priv(dev);
281 struct dsa_switch *ds = p->parent;
282
283 if (!ds->drv->port_vlan_del)
284 return -EOPNOTSUPP;
285
286 return ds->drv->port_vlan_del(ds, p->port, vlan);
287}
288
289static int dsa_slave_port_vlan_dump(struct net_device *dev,
290 struct switchdev_obj_port_vlan *vlan,
291 switchdev_obj_dump_cb_t *cb)
292{
293 struct dsa_slave_priv *p = netdev_priv(dev);
294 struct dsa_switch *ds = p->parent;
295 DECLARE_BITMAP(members, DSA_MAX_PORTS);
296 DECLARE_BITMAP(untagged, DSA_MAX_PORTS);
297 u16 pvid, vid = 0;
298 int err;
299
300 if (!ds->drv->vlan_getnext || !ds->drv->port_pvid_get)
301 return -EOPNOTSUPP;
302
303 err = ds->drv->port_pvid_get(ds, p->port, &pvid);
304 if (err)
305 return err;
306
307 for (;;) {
308 err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
309 if (err)
310 break;
311
312 if (!test_bit(p->port, members))
313 continue;
314
315 memset(vlan, 0, sizeof(*vlan));
316 vlan->vid_begin = vlan->vid_end = vid;
317
318 if (vid == pvid)
319 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
320
321 if (test_bit(p->port, untagged))
322 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
323
324 err = cb(&vlan->obj);
325 if (err)
326 break;
327 }
328
329 return err == -ENOENT ? 0 : err;
330}
331
332static int dsa_slave_port_fdb_add(struct net_device *dev,
333 const struct switchdev_obj_port_fdb *fdb,
334 struct switchdev_trans *trans)
335{
336 struct dsa_slave_priv *p = netdev_priv(dev);
337 struct dsa_switch *ds = p->parent;
338 int ret;
339
340 if (!ds->drv->port_fdb_prepare || !ds->drv->port_fdb_add)
341 return -EOPNOTSUPP;
342
343 if (switchdev_trans_ph_prepare(trans))
344 ret = ds->drv->port_fdb_prepare(ds, p->port, fdb, trans);
345 else
346 ret = ds->drv->port_fdb_add(ds, p->port, fdb, trans);
347
348 return ret;
349}
350
351static int dsa_slave_port_fdb_del(struct net_device *dev,
352 const struct switchdev_obj_port_fdb *fdb)
353{
354 struct dsa_slave_priv *p = netdev_priv(dev);
355 struct dsa_switch *ds = p->parent;
356 int ret = -EOPNOTSUPP;
357
358 if (ds->drv->port_fdb_del)
359 ret = ds->drv->port_fdb_del(ds, p->port, fdb);
360
361 return ret;
362}
363
364static int dsa_slave_port_fdb_dump(struct net_device *dev,
365 struct switchdev_obj_port_fdb *fdb,
366 switchdev_obj_dump_cb_t *cb)
367{
368 struct dsa_slave_priv *p = netdev_priv(dev);
369 struct dsa_switch *ds = p->parent;
370
371 if (ds->drv->port_fdb_dump)
372 return ds->drv->port_fdb_dump(ds, p->port, fdb, cb);
373
374 return -EOPNOTSUPP;
375}
376
377static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
378{
379 struct dsa_slave_priv *p = netdev_priv(dev);
380
381 if (p->phy != NULL)
382 return phy_mii_ioctl(p->phy, ifr, cmd);
383
384 return -EOPNOTSUPP;
385}
386
387/* Return a bitmask of all ports being currently bridged within a given bridge
388 * device. Note that on leave, the mask will still return the bitmask of ports
389 * currently bridged, prior to port removal, and this is exactly what we want.
390 */
391static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
392 struct net_device *bridge)
393{
394 struct dsa_slave_priv *p;
395 unsigned int port;
396 u32 mask = 0;
397
398 for (port = 0; port < DSA_MAX_PORTS; port++) {
399 if (!dsa_is_port_initialized(ds, port))
400 continue;
401
402 p = netdev_priv(ds->ports[port]);
403
404 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
405 p->bridge_dev == bridge)
406 mask |= 1 << port;
407 }
408
409 return mask;
410}
411
412static int dsa_slave_stp_update(struct net_device *dev, u8 state)
413{
414 struct dsa_slave_priv *p = netdev_priv(dev);
415 struct dsa_switch *ds = p->parent;
416 int ret = -EOPNOTSUPP;
417
418 if (ds->drv->port_stp_update)
419 ret = ds->drv->port_stp_update(ds, p->port, state);
420
421 return ret;
422}
423
424static int dsa_slave_port_attr_set(struct net_device *dev,
425 const struct switchdev_attr *attr,
426 struct switchdev_trans *trans)
427{
428 struct dsa_slave_priv *p = netdev_priv(dev);
429 struct dsa_switch *ds = p->parent;
430 int ret;
431
432 switch (attr->id) {
433 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
434 if (switchdev_trans_ph_prepare(trans))
435 ret = ds->drv->port_stp_update ? 0 : -EOPNOTSUPP;
436 else
437 ret = ds->drv->port_stp_update(ds, p->port,
438 attr->u.stp_state);
439 break;
440 default:
441 ret = -EOPNOTSUPP;
442 break;
443 }
444
445 return ret;
446}
447
448static int dsa_slave_port_obj_add(struct net_device *dev,
449 const struct switchdev_obj *obj,
450 struct switchdev_trans *trans)
451{
452 int err;
453
454 /* For the prepare phase, ensure the full set of changes is feasable in
455 * one go in order to signal a failure properly. If an operation is not
456 * supported, return -EOPNOTSUPP.
457 */
458
459 switch (obj->id) {
460 case SWITCHDEV_OBJ_ID_PORT_FDB:
461 err = dsa_slave_port_fdb_add(dev,
462 SWITCHDEV_OBJ_PORT_FDB(obj),
463 trans);
464 break;
465 case SWITCHDEV_OBJ_ID_PORT_VLAN:
466 err = dsa_slave_port_vlan_add(dev,
467 SWITCHDEV_OBJ_PORT_VLAN(obj),
468 trans);
469 break;
470 default:
471 err = -EOPNOTSUPP;
472 break;
473 }
474
475 return err;
476}
477
478static int dsa_slave_port_obj_del(struct net_device *dev,
479 const struct switchdev_obj *obj)
480{
481 int err;
482
483 switch (obj->id) {
484 case SWITCHDEV_OBJ_ID_PORT_FDB:
485 err = dsa_slave_port_fdb_del(dev,
486 SWITCHDEV_OBJ_PORT_FDB(obj));
487 break;
488 case SWITCHDEV_OBJ_ID_PORT_VLAN:
489 err = dsa_slave_port_vlan_del(dev,
490 SWITCHDEV_OBJ_PORT_VLAN(obj));
491 break;
492 default:
493 err = -EOPNOTSUPP;
494 break;
495 }
496
497 return err;
498}
499
500static int dsa_slave_port_obj_dump(struct net_device *dev,
501 struct switchdev_obj *obj,
502 switchdev_obj_dump_cb_t *cb)
503{
504 int err;
505
506 switch (obj->id) {
507 case SWITCHDEV_OBJ_ID_PORT_FDB:
508 err = dsa_slave_port_fdb_dump(dev,
509 SWITCHDEV_OBJ_PORT_FDB(obj),
510 cb);
511 break;
512 case SWITCHDEV_OBJ_ID_PORT_VLAN:
513 err = dsa_slave_port_vlan_dump(dev,
514 SWITCHDEV_OBJ_PORT_VLAN(obj),
515 cb);
516 break;
517 default:
518 err = -EOPNOTSUPP;
519 break;
520 }
521
522 return err;
523}
524
525static int dsa_slave_bridge_port_join(struct net_device *dev,
526 struct net_device *br)
527{
528 struct dsa_slave_priv *p = netdev_priv(dev);
529 struct dsa_switch *ds = p->parent;
530 int ret = -EOPNOTSUPP;
531
532 p->bridge_dev = br;
533
534 if (ds->drv->port_join_bridge)
535 ret = ds->drv->port_join_bridge(ds, p->port,
536 dsa_slave_br_port_mask(ds, br));
537
538 return ret;
539}
540
541static int dsa_slave_bridge_port_leave(struct net_device *dev)
542{
543 struct dsa_slave_priv *p = netdev_priv(dev);
544 struct dsa_switch *ds = p->parent;
545 int ret = -EOPNOTSUPP;
546
547
548 if (ds->drv->port_leave_bridge)
549 ret = ds->drv->port_leave_bridge(ds, p->port,
550 dsa_slave_br_port_mask(ds, p->bridge_dev));
551
552 p->bridge_dev = NULL;
553
554 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
555 * so allow it to be in BR_STATE_FORWARDING to be kept functional
556 */
557 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
558
559 return ret;
560}
561
562static int dsa_slave_port_attr_get(struct net_device *dev,
563 struct switchdev_attr *attr)
564{
565 struct dsa_slave_priv *p = netdev_priv(dev);
566 struct dsa_switch *ds = p->parent;
567
568 switch (attr->id) {
569 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
570 attr->u.ppid.id_len = sizeof(ds->index);
571 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
572 break;
573 default:
574 return -EOPNOTSUPP;
575 }
576
577 return 0;
578}
579
580static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
581 struct sk_buff *skb)
582{
583#ifdef CONFIG_NET_POLL_CONTROLLER
584 if (p->netpoll)
585 netpoll_send_skb(p->netpoll, skb);
586#else
587 BUG();
588#endif
589 return NETDEV_TX_OK;
590}
591
592static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
593{
594 struct dsa_slave_priv *p = netdev_priv(dev);
595 struct sk_buff *nskb;
596
597 dev->stats.tx_packets++;
598 dev->stats.tx_bytes += skb->len;
599
600 /* Transmit function may have to reallocate the original SKB */
601 nskb = p->xmit(skb, dev);
602 if (!nskb)
603 return NETDEV_TX_OK;
604
605 /* SKB for netpoll still need to be mangled with the protocol-specific
606 * tag to be successfully transmitted
607 */
608 if (unlikely(netpoll_tx_running(dev)))
609 return dsa_netpoll_send_skb(p, nskb);
610
611 /* Queue the SKB for transmission on the parent interface, but
612 * do not modify its EtherType
613 */
614 nskb->dev = p->parent->dst->master_netdev;
615 dev_queue_xmit(nskb);
616
617 return NETDEV_TX_OK;
618}
619
620static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
621 struct net_device *dev)
622{
623 /* Just return the original SKB */
624 return skb;
625}
626
627
628/* ethtool operations *******************************************************/
629static int
630dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
631{
632 struct dsa_slave_priv *p = netdev_priv(dev);
633 int err;
634
635 err = -EOPNOTSUPP;
636 if (p->phy != NULL) {
637 err = phy_read_status(p->phy);
638 if (err == 0)
639 err = phy_ethtool_gset(p->phy, cmd);
640 }
641
642 return err;
643}
644
645static int
646dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
647{
648 struct dsa_slave_priv *p = netdev_priv(dev);
649
650 if (p->phy != NULL)
651 return phy_ethtool_sset(p->phy, cmd);
652
653 return -EOPNOTSUPP;
654}
655
656static void dsa_slave_get_drvinfo(struct net_device *dev,
657 struct ethtool_drvinfo *drvinfo)
658{
659 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
660 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
661 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
662 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
663}
664
665static int dsa_slave_get_regs_len(struct net_device *dev)
666{
667 struct dsa_slave_priv *p = netdev_priv(dev);
668 struct dsa_switch *ds = p->parent;
669
670 if (ds->drv->get_regs_len)
671 return ds->drv->get_regs_len(ds, p->port);
672
673 return -EOPNOTSUPP;
674}
675
676static void
677dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
678{
679 struct dsa_slave_priv *p = netdev_priv(dev);
680 struct dsa_switch *ds = p->parent;
681
682 if (ds->drv->get_regs)
683 ds->drv->get_regs(ds, p->port, regs, _p);
684}
685
686static int dsa_slave_nway_reset(struct net_device *dev)
687{
688 struct dsa_slave_priv *p = netdev_priv(dev);
689
690 if (p->phy != NULL)
691 return genphy_restart_aneg(p->phy);
692
693 return -EOPNOTSUPP;
694}
695
696static u32 dsa_slave_get_link(struct net_device *dev)
697{
698 struct dsa_slave_priv *p = netdev_priv(dev);
699
700 if (p->phy != NULL) {
701 genphy_update_link(p->phy);
702 return p->phy->link;
703 }
704
705 return -EOPNOTSUPP;
706}
707
708static int dsa_slave_get_eeprom_len(struct net_device *dev)
709{
710 struct dsa_slave_priv *p = netdev_priv(dev);
711 struct dsa_switch *ds = p->parent;
712
713 if (ds->pd->eeprom_len)
714 return ds->pd->eeprom_len;
715
716 if (ds->drv->get_eeprom_len)
717 return ds->drv->get_eeprom_len(ds);
718
719 return 0;
720}
721
722static int dsa_slave_get_eeprom(struct net_device *dev,
723 struct ethtool_eeprom *eeprom, u8 *data)
724{
725 struct dsa_slave_priv *p = netdev_priv(dev);
726 struct dsa_switch *ds = p->parent;
727
728 if (ds->drv->get_eeprom)
729 return ds->drv->get_eeprom(ds, eeprom, data);
730
731 return -EOPNOTSUPP;
732}
733
734static int dsa_slave_set_eeprom(struct net_device *dev,
735 struct ethtool_eeprom *eeprom, u8 *data)
736{
737 struct dsa_slave_priv *p = netdev_priv(dev);
738 struct dsa_switch *ds = p->parent;
739
740 if (ds->drv->set_eeprom)
741 return ds->drv->set_eeprom(ds, eeprom, data);
742
743 return -EOPNOTSUPP;
744}
745
746static void dsa_slave_get_strings(struct net_device *dev,
747 uint32_t stringset, uint8_t *data)
748{
749 struct dsa_slave_priv *p = netdev_priv(dev);
750 struct dsa_switch *ds = p->parent;
751
752 if (stringset == ETH_SS_STATS) {
753 int len = ETH_GSTRING_LEN;
754
755 strncpy(data, "tx_packets", len);
756 strncpy(data + len, "tx_bytes", len);
757 strncpy(data + 2 * len, "rx_packets", len);
758 strncpy(data + 3 * len, "rx_bytes", len);
759 if (ds->drv->get_strings != NULL)
760 ds->drv->get_strings(ds, p->port, data + 4 * len);
761 }
762}
763
764static void dsa_slave_get_ethtool_stats(struct net_device *dev,
765 struct ethtool_stats *stats,
766 uint64_t *data)
767{
768 struct dsa_slave_priv *p = netdev_priv(dev);
769 struct dsa_switch *ds = p->parent;
770
771 data[0] = p->dev->stats.tx_packets;
772 data[1] = p->dev->stats.tx_bytes;
773 data[2] = p->dev->stats.rx_packets;
774 data[3] = p->dev->stats.rx_bytes;
775 if (ds->drv->get_ethtool_stats != NULL)
776 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
777}
778
779static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
780{
781 struct dsa_slave_priv *p = netdev_priv(dev);
782 struct dsa_switch *ds = p->parent;
783
784 if (sset == ETH_SS_STATS) {
785 int count;
786
787 count = 4;
788 if (ds->drv->get_sset_count != NULL)
789 count += ds->drv->get_sset_count(ds);
790
791 return count;
792 }
793
794 return -EOPNOTSUPP;
795}
796
797static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
798{
799 struct dsa_slave_priv *p = netdev_priv(dev);
800 struct dsa_switch *ds = p->parent;
801
802 if (ds->drv->get_wol)
803 ds->drv->get_wol(ds, p->port, w);
804}
805
806static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
807{
808 struct dsa_slave_priv *p = netdev_priv(dev);
809 struct dsa_switch *ds = p->parent;
810 int ret = -EOPNOTSUPP;
811
812 if (ds->drv->set_wol)
813 ret = ds->drv->set_wol(ds, p->port, w);
814
815 return ret;
816}
817
818static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
819{
820 struct dsa_slave_priv *p = netdev_priv(dev);
821 struct dsa_switch *ds = p->parent;
822 int ret;
823
824 if (!ds->drv->set_eee)
825 return -EOPNOTSUPP;
826
827 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
828 if (ret)
829 return ret;
830
831 if (p->phy)
832 ret = phy_ethtool_set_eee(p->phy, e);
833
834 return ret;
835}
836
837static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
838{
839 struct dsa_slave_priv *p = netdev_priv(dev);
840 struct dsa_switch *ds = p->parent;
841 int ret;
842
843 if (!ds->drv->get_eee)
844 return -EOPNOTSUPP;
845
846 ret = ds->drv->get_eee(ds, p->port, e);
847 if (ret)
848 return ret;
849
850 if (p->phy)
851 ret = phy_ethtool_get_eee(p->phy, e);
852
853 return ret;
854}
855
856#ifdef CONFIG_NET_POLL_CONTROLLER
857static int dsa_slave_netpoll_setup(struct net_device *dev,
858 struct netpoll_info *ni)
859{
860 struct dsa_slave_priv *p = netdev_priv(dev);
861 struct dsa_switch *ds = p->parent;
862 struct net_device *master = ds->dst->master_netdev;
863 struct netpoll *netpoll;
864 int err = 0;
865
866 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
867 if (!netpoll)
868 return -ENOMEM;
869
870 err = __netpoll_setup(netpoll, master);
871 if (err) {
872 kfree(netpoll);
873 goto out;
874 }
875
876 p->netpoll = netpoll;
877out:
878 return err;
879}
880
881static void dsa_slave_netpoll_cleanup(struct net_device *dev)
882{
883 struct dsa_slave_priv *p = netdev_priv(dev);
884 struct netpoll *netpoll = p->netpoll;
885
886 if (!netpoll)
887 return;
888
889 p->netpoll = NULL;
890
891 __netpoll_free_async(netpoll);
892}
893
894static void dsa_slave_poll_controller(struct net_device *dev)
895{
896}
897#endif
898
899static const struct ethtool_ops dsa_slave_ethtool_ops = {
900 .get_settings = dsa_slave_get_settings,
901 .set_settings = dsa_slave_set_settings,
902 .get_drvinfo = dsa_slave_get_drvinfo,
903 .get_regs_len = dsa_slave_get_regs_len,
904 .get_regs = dsa_slave_get_regs,
905 .nway_reset = dsa_slave_nway_reset,
906 .get_link = dsa_slave_get_link,
907 .get_eeprom_len = dsa_slave_get_eeprom_len,
908 .get_eeprom = dsa_slave_get_eeprom,
909 .set_eeprom = dsa_slave_set_eeprom,
910 .get_strings = dsa_slave_get_strings,
911 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
912 .get_sset_count = dsa_slave_get_sset_count,
913 .set_wol = dsa_slave_set_wol,
914 .get_wol = dsa_slave_get_wol,
915 .set_eee = dsa_slave_set_eee,
916 .get_eee = dsa_slave_get_eee,
917};
918
919static const struct net_device_ops dsa_slave_netdev_ops = {
920 .ndo_open = dsa_slave_open,
921 .ndo_stop = dsa_slave_close,
922 .ndo_start_xmit = dsa_slave_xmit,
923 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
924 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
925 .ndo_set_mac_address = dsa_slave_set_mac_address,
926 .ndo_fdb_add = switchdev_port_fdb_add,
927 .ndo_fdb_del = switchdev_port_fdb_del,
928 .ndo_fdb_dump = switchdev_port_fdb_dump,
929 .ndo_do_ioctl = dsa_slave_ioctl,
930 .ndo_get_iflink = dsa_slave_get_iflink,
931#ifdef CONFIG_NET_POLL_CONTROLLER
932 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
933 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
934 .ndo_poll_controller = dsa_slave_poll_controller,
935#endif
936 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
937 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
938 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
939};
940
941static const struct switchdev_ops dsa_slave_switchdev_ops = {
942 .switchdev_port_attr_get = dsa_slave_port_attr_get,
943 .switchdev_port_attr_set = dsa_slave_port_attr_set,
944 .switchdev_port_obj_add = dsa_slave_port_obj_add,
945 .switchdev_port_obj_del = dsa_slave_port_obj_del,
946 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
947};
948
949static struct device_type dsa_type = {
950 .name = "dsa",
951};
952
953static void dsa_slave_adjust_link(struct net_device *dev)
954{
955 struct dsa_slave_priv *p = netdev_priv(dev);
956 struct dsa_switch *ds = p->parent;
957 unsigned int status_changed = 0;
958
959 if (p->old_link != p->phy->link) {
960 status_changed = 1;
961 p->old_link = p->phy->link;
962 }
963
964 if (p->old_duplex != p->phy->duplex) {
965 status_changed = 1;
966 p->old_duplex = p->phy->duplex;
967 }
968
969 if (p->old_pause != p->phy->pause) {
970 status_changed = 1;
971 p->old_pause = p->phy->pause;
972 }
973
974 if (ds->drv->adjust_link && status_changed)
975 ds->drv->adjust_link(ds, p->port, p->phy);
976
977 if (status_changed)
978 phy_print_status(p->phy);
979}
980
981static int dsa_slave_fixed_link_update(struct net_device *dev,
982 struct fixed_phy_status *status)
983{
984 struct dsa_slave_priv *p = netdev_priv(dev);
985 struct dsa_switch *ds = p->parent;
986
987 if (ds->drv->fixed_link_update)
988 ds->drv->fixed_link_update(ds, p->port, status);
989
990 return 0;
991}
992
993/* slave device setup *******************************************************/
994static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
995 struct net_device *slave_dev,
996 int addr)
997{
998 struct dsa_switch *ds = p->parent;
999
1000 p->phy = ds->slave_mii_bus->phy_map[addr];
1001 if (!p->phy) {
1002 netdev_err(slave_dev, "no phy at %d\n", addr);
1003 return -ENODEV;
1004 }
1005
1006 /* Use already configured phy mode */
1007 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1008 p->phy_interface = p->phy->interface;
1009 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1010 p->phy_interface);
1011}
1012
1013static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
1014 struct net_device *slave_dev)
1015{
1016 struct dsa_switch *ds = p->parent;
1017 struct dsa_chip_data *cd = ds->pd;
1018 struct device_node *phy_dn, *port_dn;
1019 bool phy_is_fixed = false;
1020 u32 phy_flags = 0;
1021 int mode, ret;
1022
1023 port_dn = cd->port_dn[p->port];
1024 mode = of_get_phy_mode(port_dn);
1025 if (mode < 0)
1026 mode = PHY_INTERFACE_MODE_NA;
1027 p->phy_interface = mode;
1028
1029 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1030 if (of_phy_is_fixed_link(port_dn)) {
1031 /* In the case of a fixed PHY, the DT node associated
1032 * to the fixed PHY is the Port DT node
1033 */
1034 ret = of_phy_register_fixed_link(port_dn);
1035 if (ret) {
1036 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1037 return ret;
1038 }
1039 phy_is_fixed = true;
1040 phy_dn = port_dn;
1041 }
1042
1043 if (ds->drv->get_phy_flags)
1044 phy_flags = ds->drv->get_phy_flags(ds, p->port);
1045
1046 if (phy_dn) {
1047 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1048
1049 /* If this PHY address is part of phys_mii_mask, which means
1050 * that we need to divert reads and writes to/from it, then we
1051 * want to bind this device using the slave MII bus created by
1052 * DSA to make that happen.
1053 */
1054 if (!phy_is_fixed && phy_id >= 0 &&
1055 (ds->phys_mii_mask & (1 << phy_id))) {
1056 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1057 if (ret) {
1058 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1059 return ret;
1060 }
1061 } else {
1062 p->phy = of_phy_connect(slave_dev, phy_dn,
1063 dsa_slave_adjust_link,
1064 phy_flags,
1065 p->phy_interface);
1066 }
1067 }
1068
1069 if (p->phy && phy_is_fixed)
1070 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1071
1072 /* We could not connect to a designated PHY, so use the switch internal
1073 * MDIO bus instead
1074 */
1075 if (!p->phy) {
1076 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
1077 if (ret) {
1078 netdev_err(slave_dev, "failed to connect to port %d: %d\n", p->port, ret);
1079 return ret;
1080 }
1081 } else {
1082 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
1083 p->phy->addr, p->phy->drv->name);
1084 }
1085
1086 return 0;
1087}
1088
1089static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1090static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1091 struct netdev_queue *txq,
1092 void *_unused)
1093{
1094 lockdep_set_class(&txq->_xmit_lock,
1095 &dsa_slave_netdev_xmit_lock_key);
1096}
1097
1098int dsa_slave_suspend(struct net_device *slave_dev)
1099{
1100 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1101
1102 netif_device_detach(slave_dev);
1103
1104 if (p->phy) {
1105 phy_stop(p->phy);
1106 p->old_pause = -1;
1107 p->old_link = -1;
1108 p->old_duplex = -1;
1109 phy_suspend(p->phy);
1110 }
1111
1112 return 0;
1113}
1114
1115int dsa_slave_resume(struct net_device *slave_dev)
1116{
1117 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1118
1119 netif_device_attach(slave_dev);
1120
1121 if (p->phy) {
1122 phy_resume(p->phy);
1123 phy_start(p->phy);
1124 }
1125
1126 return 0;
1127}
1128
1129int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1130 int port, char *name)
1131{
1132 struct net_device *master = ds->dst->master_netdev;
1133 struct net_device *slave_dev;
1134 struct dsa_slave_priv *p;
1135 int ret;
1136
1137 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1138 NET_NAME_UNKNOWN, ether_setup);
1139 if (slave_dev == NULL)
1140 return -ENOMEM;
1141
1142 slave_dev->features = master->vlan_features;
1143 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1144 eth_hw_addr_inherit(slave_dev, master);
1145 slave_dev->priv_flags |= IFF_NO_QUEUE;
1146 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1147 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1148 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1149
1150 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1151 NULL);
1152
1153 SET_NETDEV_DEV(slave_dev, parent);
1154 slave_dev->dev.of_node = ds->pd->port_dn[port];
1155 slave_dev->vlan_features = master->vlan_features;
1156
1157 p = netdev_priv(slave_dev);
1158 p->dev = slave_dev;
1159 p->parent = ds;
1160 p->port = port;
1161
1162 switch (ds->dst->tag_protocol) {
1163#ifdef CONFIG_NET_DSA_TAG_DSA
1164 case DSA_TAG_PROTO_DSA:
1165 p->xmit = dsa_netdev_ops.xmit;
1166 break;
1167#endif
1168#ifdef CONFIG_NET_DSA_TAG_EDSA
1169 case DSA_TAG_PROTO_EDSA:
1170 p->xmit = edsa_netdev_ops.xmit;
1171 break;
1172#endif
1173#ifdef CONFIG_NET_DSA_TAG_TRAILER
1174 case DSA_TAG_PROTO_TRAILER:
1175 p->xmit = trailer_netdev_ops.xmit;
1176 break;
1177#endif
1178#ifdef CONFIG_NET_DSA_TAG_BRCM
1179 case DSA_TAG_PROTO_BRCM:
1180 p->xmit = brcm_netdev_ops.xmit;
1181 break;
1182#endif
1183 default:
1184 p->xmit = dsa_slave_notag_xmit;
1185 break;
1186 }
1187
1188 p->old_pause = -1;
1189 p->old_link = -1;
1190 p->old_duplex = -1;
1191
1192 ret = dsa_slave_phy_setup(p, slave_dev);
1193 if (ret) {
1194 netdev_err(master, "error %d setting up slave phy\n", ret);
1195 free_netdev(slave_dev);
1196 return ret;
1197 }
1198
1199 ds->ports[port] = slave_dev;
1200 ret = register_netdev(slave_dev);
1201 if (ret) {
1202 netdev_err(master, "error %d registering interface %s\n",
1203 ret, slave_dev->name);
1204 phy_disconnect(p->phy);
1205 ds->ports[port] = NULL;
1206 free_netdev(slave_dev);
1207 return ret;
1208 }
1209
1210 netif_carrier_off(slave_dev);
1211
1212 return 0;
1213}
1214
1215static bool dsa_slave_dev_check(struct net_device *dev)
1216{
1217 return dev->netdev_ops == &dsa_slave_netdev_ops;
1218}
1219
1220static int dsa_slave_master_changed(struct net_device *dev)
1221{
1222 struct net_device *master = netdev_master_upper_dev_get(dev);
1223 struct dsa_slave_priv *p = netdev_priv(dev);
1224 int err = 0;
1225
1226 if (master && master->rtnl_link_ops &&
1227 !strcmp(master->rtnl_link_ops->kind, "bridge"))
1228 err = dsa_slave_bridge_port_join(dev, master);
1229 else if (dsa_port_is_bridged(p))
1230 err = dsa_slave_bridge_port_leave(dev);
1231
1232 return err;
1233}
1234
1235int dsa_slave_netdevice_event(struct notifier_block *unused,
1236 unsigned long event, void *ptr)
1237{
1238 struct net_device *dev;
1239 int err = 0;
1240
1241 switch (event) {
1242 case NETDEV_CHANGEUPPER:
1243 dev = netdev_notifier_info_to_dev(ptr);
1244 if (!dsa_slave_dev_check(dev))
1245 goto out;
1246
1247 err = dsa_slave_master_changed(dev);
1248 if (err && err != -EOPNOTSUPP)
1249 netdev_warn(dev, "failed to reflect master change\n");
1250
1251 break;
1252 }
1253
1254out:
1255 return NOTIFY_DONE;
1256}