of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / net / dsa / slave.c
blob7bc787b095c897922751c3eaa5c2b32d5bca95fa
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/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"
24 /* slave mii_bus handling ***************************************************/
25 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
27 struct dsa_switch *ds = bus->priv;
29 if (ds->phys_mii_mask & (1 << addr))
30 return ds->drv->phy_read(ds, addr, reg);
32 return 0xffff;
35 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
37 struct dsa_switch *ds = bus->priv;
39 if (ds->phys_mii_mask & (1 << addr))
40 return ds->drv->phy_write(ds, addr, reg, val);
42 return 0;
45 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
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;
58 /* slave device handling ****************************************************/
59 static int dsa_slave_get_iflink(const struct net_device *dev)
61 struct dsa_slave_priv *p = netdev_priv(dev);
63 return p->parent->dst->master_netdev->ifindex;
66 static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
68 return !!p->bridge_dev;
71 static int dsa_slave_open(struct net_device *dev)
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;
80 if (!(master->flags & IFF_UP))
81 return -ENETDOWN;
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;
89 if (dev->flags & IFF_ALLMULTI) {
90 err = dev_set_allmulti(master, 1);
91 if (err < 0)
92 goto del_unicast;
94 if (dev->flags & IFF_PROMISC) {
95 err = dev_set_promiscuity(master, 1);
96 if (err < 0)
97 goto clear_allmulti;
100 if (ds->drv->port_enable) {
101 err = ds->drv->port_enable(ds, p->port, p->phy);
102 if (err)
103 goto clear_promisc;
106 if (ds->drv->port_stp_update)
107 ds->drv->port_stp_update(ds, p->port, stp_state);
109 if (p->phy)
110 phy_start(p->phy);
112 return 0;
114 clear_promisc:
115 if (dev->flags & IFF_PROMISC)
116 dev_set_promiscuity(master, -1);
117 clear_allmulti:
118 if (dev->flags & IFF_ALLMULTI)
119 dev_set_allmulti(master, -1);
120 del_unicast:
121 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
122 dev_uc_del(master, dev->dev_addr);
123 out:
124 return err;
127 static int dsa_slave_close(struct net_device *dev)
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;
133 if (p->phy)
134 phy_stop(p->phy);
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);
143 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
144 dev_uc_del(master, dev->dev_addr);
146 if (ds->drv->port_disable)
147 ds->drv->port_disable(ds, p->port, p->phy);
149 if (ds->drv->port_stp_update)
150 ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
152 return 0;
155 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
157 struct dsa_slave_priv *p = netdev_priv(dev);
158 struct net_device *master = p->parent->dst->master_netdev;
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);
166 static void dsa_slave_set_rx_mode(struct net_device *dev)
168 struct dsa_slave_priv *p = netdev_priv(dev);
169 struct net_device *master = p->parent->dst->master_netdev;
171 dev_mc_sync(master, dev);
172 dev_uc_sync(master, dev);
175 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
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;
182 if (!is_valid_ether_addr(addr->sa_data))
183 return -EADDRNOTAVAIL;
185 if (!(dev->flags & IFF_UP))
186 goto out;
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;
194 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
195 dev_uc_del(master, dev->dev_addr);
197 out:
198 ether_addr_copy(dev->dev_addr, addr->sa_data);
200 return 0;
203 static int dsa_bridge_check_vlan_range(struct dsa_switch *ds,
204 const struct net_device *bridge,
205 u16 vid_begin, u16 vid_end)
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;
214 if (!ds->drv->vlan_getnext || !vid_begin)
215 return -EOPNOTSUPP;
217 vid = vid_begin - 1;
219 do {
220 err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
221 if (err)
222 break;
224 if (vid > vid_end)
225 break;
227 member = find_first_bit(members, DSA_MAX_PORTS);
228 if (member == DSA_MAX_PORTS)
229 continue;
231 dev = ds->ports[member];
232 p = netdev_priv(dev);
233 vlan_br = p->bridge_dev;
234 if (vlan_br == bridge)
235 continue;
237 netdev_dbg(vlan_br, "hardware VLAN %d already in use\n", vid);
238 return -EOPNOTSUPP;
239 } while (vid < vid_end);
241 return err == -ENOENT ? 0 : err;
244 static int dsa_slave_port_vlan_add(struct net_device *dev,
245 const struct switchdev_obj_port_vlan *vlan,
246 struct switchdev_trans *trans)
248 struct dsa_slave_priv *p = netdev_priv(dev);
249 struct dsa_switch *ds = p->parent;
250 int err;
252 if (switchdev_trans_ph_prepare(trans)) {
253 if (!ds->drv->port_vlan_prepare || !ds->drv->port_vlan_add)
254 return -EOPNOTSUPP;
256 /* If the requested port doesn't belong to the same bridge as
257 * the VLAN members, fallback to software VLAN (hopefully).
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;
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;
274 return 0;
277 static int dsa_slave_port_vlan_del(struct net_device *dev,
278 const struct switchdev_obj_port_vlan *vlan)
280 struct dsa_slave_priv *p = netdev_priv(dev);
281 struct dsa_switch *ds = p->parent;
283 if (!ds->drv->port_vlan_del)
284 return -EOPNOTSUPP;
286 return ds->drv->port_vlan_del(ds, p->port, vlan);
289 static int dsa_slave_port_vlan_dump(struct net_device *dev,
290 struct switchdev_obj_port_vlan *vlan,
291 switchdev_obj_dump_cb_t *cb)
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;
300 if (!ds->drv->vlan_getnext || !ds->drv->port_pvid_get)
301 return -EOPNOTSUPP;
303 err = ds->drv->port_pvid_get(ds, p->port, &pvid);
304 if (err)
305 return err;
307 for (;;) {
308 err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
309 if (err)
310 break;
312 if (!test_bit(p->port, members))
313 continue;
315 memset(vlan, 0, sizeof(*vlan));
316 vlan->vid_begin = vlan->vid_end = vid;
318 if (vid == pvid)
319 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
321 if (test_bit(p->port, untagged))
322 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
324 err = cb(&vlan->obj);
325 if (err)
326 break;
329 return err == -ENOENT ? 0 : err;
332 static int dsa_slave_port_fdb_add(struct net_device *dev,
333 const struct switchdev_obj_port_fdb *fdb,
334 struct switchdev_trans *trans)
336 struct dsa_slave_priv *p = netdev_priv(dev);
337 struct dsa_switch *ds = p->parent;
338 int ret;
340 if (!ds->drv->port_fdb_prepare || !ds->drv->port_fdb_add)
341 return -EOPNOTSUPP;
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);
348 return ret;
351 static int dsa_slave_port_fdb_del(struct net_device *dev,
352 const struct switchdev_obj_port_fdb *fdb)
354 struct dsa_slave_priv *p = netdev_priv(dev);
355 struct dsa_switch *ds = p->parent;
356 int ret = -EOPNOTSUPP;
358 if (ds->drv->port_fdb_del)
359 ret = ds->drv->port_fdb_del(ds, p->port, fdb);
361 return ret;
364 static int dsa_slave_port_fdb_dump(struct net_device *dev,
365 struct switchdev_obj_port_fdb *fdb,
366 switchdev_obj_dump_cb_t *cb)
368 struct dsa_slave_priv *p = netdev_priv(dev);
369 struct dsa_switch *ds = p->parent;
371 if (ds->drv->port_fdb_dump)
372 return ds->drv->port_fdb_dump(ds, p->port, fdb, cb);
374 return -EOPNOTSUPP;
377 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
379 struct dsa_slave_priv *p = netdev_priv(dev);
381 if (p->phy != NULL)
382 return phy_mii_ioctl(p->phy, ifr, cmd);
384 return -EOPNOTSUPP;
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.
391 static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
392 struct net_device *bridge)
394 struct dsa_slave_priv *p;
395 unsigned int port;
396 u32 mask = 0;
398 for (port = 0; port < DSA_MAX_PORTS; port++) {
399 if (!dsa_is_port_initialized(ds, port))
400 continue;
402 p = netdev_priv(ds->ports[port]);
404 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
405 p->bridge_dev == bridge)
406 mask |= 1 << port;
409 return mask;
412 static int dsa_slave_stp_update(struct net_device *dev, u8 state)
414 struct dsa_slave_priv *p = netdev_priv(dev);
415 struct dsa_switch *ds = p->parent;
416 int ret = -EOPNOTSUPP;
418 if (ds->drv->port_stp_update)
419 ret = ds->drv->port_stp_update(ds, p->port, state);
421 return ret;
424 static int dsa_slave_port_attr_set(struct net_device *dev,
425 const struct switchdev_attr *attr,
426 struct switchdev_trans *trans)
428 struct dsa_slave_priv *p = netdev_priv(dev);
429 struct dsa_switch *ds = p->parent;
430 int ret;
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;
445 return ret;
448 static int dsa_slave_port_obj_add(struct net_device *dev,
449 const struct switchdev_obj *obj,
450 struct switchdev_trans *trans)
452 int err;
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.
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;
475 return err;
478 static int dsa_slave_port_obj_del(struct net_device *dev,
479 const struct switchdev_obj *obj)
481 int err;
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;
497 return err;
500 static int dsa_slave_port_obj_dump(struct net_device *dev,
501 struct switchdev_obj *obj,
502 switchdev_obj_dump_cb_t *cb)
504 int err;
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;
522 return err;
525 static int dsa_slave_bridge_port_join(struct net_device *dev,
526 struct net_device *br)
528 struct dsa_slave_priv *p = netdev_priv(dev);
529 struct dsa_switch *ds = p->parent;
530 int ret = -EOPNOTSUPP;
532 p->bridge_dev = br;
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));
538 return ret;
541 static int dsa_slave_bridge_port_leave(struct net_device *dev)
543 struct dsa_slave_priv *p = netdev_priv(dev);
544 struct dsa_switch *ds = p->parent;
545 int ret = -EOPNOTSUPP;
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));
552 p->bridge_dev = NULL;
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
557 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
559 return ret;
562 static int dsa_slave_port_attr_get(struct net_device *dev,
563 struct switchdev_attr *attr)
565 struct dsa_slave_priv *p = netdev_priv(dev);
566 struct dsa_switch *ds = p->parent;
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;
577 return 0;
580 static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
581 struct sk_buff *skb)
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;
592 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
594 struct dsa_slave_priv *p = netdev_priv(dev);
595 struct sk_buff *nskb;
597 dev->stats.tx_packets++;
598 dev->stats.tx_bytes += skb->len;
600 /* Transmit function may have to reallocate the original SKB */
601 nskb = p->xmit(skb, dev);
602 if (!nskb)
603 return NETDEV_TX_OK;
605 /* SKB for netpoll still need to be mangled with the protocol-specific
606 * tag to be successfully transmitted
608 if (unlikely(netpoll_tx_running(dev)))
609 return dsa_netpoll_send_skb(p, nskb);
611 /* Queue the SKB for transmission on the parent interface, but
612 * do not modify its EtherType
614 nskb->dev = p->parent->dst->master_netdev;
615 dev_queue_xmit(nskb);
617 return NETDEV_TX_OK;
620 static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
621 struct net_device *dev)
623 /* Just return the original SKB */
624 return skb;
628 /* ethtool operations *******************************************************/
629 static int
630 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
632 struct dsa_slave_priv *p = netdev_priv(dev);
633 int err;
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);
642 return err;
645 static int
646 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
648 struct dsa_slave_priv *p = netdev_priv(dev);
650 if (p->phy != NULL)
651 return phy_ethtool_sset(p->phy, cmd);
653 return -EOPNOTSUPP;
656 static void dsa_slave_get_drvinfo(struct net_device *dev,
657 struct ethtool_drvinfo *drvinfo)
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));
665 static int dsa_slave_get_regs_len(struct net_device *dev)
667 struct dsa_slave_priv *p = netdev_priv(dev);
668 struct dsa_switch *ds = p->parent;
670 if (ds->drv->get_regs_len)
671 return ds->drv->get_regs_len(ds, p->port);
673 return -EOPNOTSUPP;
676 static void
677 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
679 struct dsa_slave_priv *p = netdev_priv(dev);
680 struct dsa_switch *ds = p->parent;
682 if (ds->drv->get_regs)
683 ds->drv->get_regs(ds, p->port, regs, _p);
686 static int dsa_slave_nway_reset(struct net_device *dev)
688 struct dsa_slave_priv *p = netdev_priv(dev);
690 if (p->phy != NULL)
691 return genphy_restart_aneg(p->phy);
693 return -EOPNOTSUPP;
696 static u32 dsa_slave_get_link(struct net_device *dev)
698 struct dsa_slave_priv *p = netdev_priv(dev);
700 if (p->phy != NULL) {
701 genphy_update_link(p->phy);
702 return p->phy->link;
705 return -EOPNOTSUPP;
708 static int dsa_slave_get_eeprom_len(struct net_device *dev)
710 struct dsa_slave_priv *p = netdev_priv(dev);
711 struct dsa_switch *ds = p->parent;
713 if (ds->pd->eeprom_len)
714 return ds->pd->eeprom_len;
716 if (ds->drv->get_eeprom_len)
717 return ds->drv->get_eeprom_len(ds);
719 return 0;
722 static int dsa_slave_get_eeprom(struct net_device *dev,
723 struct ethtool_eeprom *eeprom, u8 *data)
725 struct dsa_slave_priv *p = netdev_priv(dev);
726 struct dsa_switch *ds = p->parent;
728 if (ds->drv->get_eeprom)
729 return ds->drv->get_eeprom(ds, eeprom, data);
731 return -EOPNOTSUPP;
734 static int dsa_slave_set_eeprom(struct net_device *dev,
735 struct ethtool_eeprom *eeprom, u8 *data)
737 struct dsa_slave_priv *p = netdev_priv(dev);
738 struct dsa_switch *ds = p->parent;
740 if (ds->drv->set_eeprom)
741 return ds->drv->set_eeprom(ds, eeprom, data);
743 return -EOPNOTSUPP;
746 static void dsa_slave_get_strings(struct net_device *dev,
747 uint32_t stringset, uint8_t *data)
749 struct dsa_slave_priv *p = netdev_priv(dev);
750 struct dsa_switch *ds = p->parent;
752 if (stringset == ETH_SS_STATS) {
753 int len = ETH_GSTRING_LEN;
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);
764 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
765 struct ethtool_stats *stats,
766 uint64_t *data)
768 struct dsa_slave_priv *p = netdev_priv(dev);
769 struct dsa_switch *ds = p->parent;
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);
779 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
781 struct dsa_slave_priv *p = netdev_priv(dev);
782 struct dsa_switch *ds = p->parent;
784 if (sset == ETH_SS_STATS) {
785 int count;
787 count = 4;
788 if (ds->drv->get_sset_count != NULL)
789 count += ds->drv->get_sset_count(ds);
791 return count;
794 return -EOPNOTSUPP;
797 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
799 struct dsa_slave_priv *p = netdev_priv(dev);
800 struct dsa_switch *ds = p->parent;
802 if (ds->drv->get_wol)
803 ds->drv->get_wol(ds, p->port, w);
806 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
808 struct dsa_slave_priv *p = netdev_priv(dev);
809 struct dsa_switch *ds = p->parent;
810 int ret = -EOPNOTSUPP;
812 if (ds->drv->set_wol)
813 ret = ds->drv->set_wol(ds, p->port, w);
815 return ret;
818 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
820 struct dsa_slave_priv *p = netdev_priv(dev);
821 struct dsa_switch *ds = p->parent;
822 int ret;
824 if (!ds->drv->set_eee)
825 return -EOPNOTSUPP;
827 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
828 if (ret)
829 return ret;
831 if (p->phy)
832 ret = phy_ethtool_set_eee(p->phy, e);
834 return ret;
837 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
839 struct dsa_slave_priv *p = netdev_priv(dev);
840 struct dsa_switch *ds = p->parent;
841 int ret;
843 if (!ds->drv->get_eee)
844 return -EOPNOTSUPP;
846 ret = ds->drv->get_eee(ds, p->port, e);
847 if (ret)
848 return ret;
850 if (p->phy)
851 ret = phy_ethtool_get_eee(p->phy, e);
853 return ret;
856 #ifdef CONFIG_NET_POLL_CONTROLLER
857 static int dsa_slave_netpoll_setup(struct net_device *dev,
858 struct netpoll_info *ni)
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;
866 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
867 if (!netpoll)
868 return -ENOMEM;
870 err = __netpoll_setup(netpoll, master);
871 if (err) {
872 kfree(netpoll);
873 goto out;
876 p->netpoll = netpoll;
877 out:
878 return err;
881 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
883 struct dsa_slave_priv *p = netdev_priv(dev);
884 struct netpoll *netpoll = p->netpoll;
886 if (!netpoll)
887 return;
889 p->netpoll = NULL;
891 __netpoll_free_async(netpoll);
894 static void dsa_slave_poll_controller(struct net_device *dev)
897 #endif
899 static 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,
919 static 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,
941 static 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,
949 static struct device_type dsa_type = {
950 .name = "dsa",
953 static void dsa_slave_adjust_link(struct net_device *dev)
955 struct dsa_slave_priv *p = netdev_priv(dev);
956 struct dsa_switch *ds = p->parent;
957 unsigned int status_changed = 0;
959 if (p->old_link != p->phy->link) {
960 status_changed = 1;
961 p->old_link = p->phy->link;
964 if (p->old_duplex != p->phy->duplex) {
965 status_changed = 1;
966 p->old_duplex = p->phy->duplex;
969 if (p->old_pause != p->phy->pause) {
970 status_changed = 1;
971 p->old_pause = p->phy->pause;
974 if (ds->drv->adjust_link && status_changed)
975 ds->drv->adjust_link(ds, p->port, p->phy);
977 if (status_changed)
978 phy_print_status(p->phy);
981 static int dsa_slave_fixed_link_update(struct net_device *dev,
982 struct fixed_phy_status *status)
984 struct dsa_slave_priv *p = netdev_priv(dev);
985 struct dsa_switch *ds = p->parent;
987 if (ds->drv->fixed_link_update)
988 ds->drv->fixed_link_update(ds, p->port, status);
990 return 0;
993 /* slave device setup *******************************************************/
994 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
995 struct net_device *slave_dev,
996 int addr)
998 struct dsa_switch *ds = p->parent;
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;
1006 /* Use already configured phy mode */
1007 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1008 p->phy_interface = p->phy->interface;
1009 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1010 p->phy_interface);
1012 return 0;
1015 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
1016 struct net_device *slave_dev)
1018 struct dsa_switch *ds = p->parent;
1019 struct dsa_chip_data *cd = ds->pd;
1020 struct device_node *phy_dn, *port_dn;
1021 bool phy_is_fixed = false;
1022 u32 phy_flags = 0;
1023 int mode, ret;
1025 port_dn = cd->port_dn[p->port];
1026 mode = of_get_phy_mode(port_dn);
1027 if (mode < 0)
1028 mode = PHY_INTERFACE_MODE_NA;
1029 p->phy_interface = mode;
1031 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1032 if (of_phy_is_fixed_link(port_dn)) {
1033 /* In the case of a fixed PHY, the DT node associated
1034 * to the fixed PHY is the Port DT node
1036 ret = of_phy_register_fixed_link(port_dn);
1037 if (ret) {
1038 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1039 return ret;
1041 phy_is_fixed = true;
1042 phy_dn = port_dn;
1045 if (ds->drv->get_phy_flags)
1046 phy_flags = ds->drv->get_phy_flags(ds, p->port);
1048 if (phy_dn) {
1049 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1051 /* If this PHY address is part of phys_mii_mask, which means
1052 * that we need to divert reads and writes to/from it, then we
1053 * want to bind this device using the slave MII bus created by
1054 * DSA to make that happen.
1056 if (!phy_is_fixed && phy_id >= 0 &&
1057 (ds->phys_mii_mask & (1 << phy_id))) {
1058 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1059 if (ret) {
1060 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1061 return ret;
1063 } else {
1064 p->phy = of_phy_connect(slave_dev, phy_dn,
1065 dsa_slave_adjust_link,
1066 phy_flags,
1067 p->phy_interface);
1071 if (p->phy && phy_is_fixed)
1072 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1074 /* We could not connect to a designated PHY, so use the switch internal
1075 * MDIO bus instead
1077 if (!p->phy) {
1078 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
1079 if (ret) {
1080 netdev_err(slave_dev, "failed to connect to port %d: %d\n", p->port, ret);
1081 return ret;
1083 } else {
1084 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
1085 p->phy->addr, p->phy->drv->name);
1088 return 0;
1091 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1092 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1093 struct netdev_queue *txq,
1094 void *_unused)
1096 lockdep_set_class(&txq->_xmit_lock,
1097 &dsa_slave_netdev_xmit_lock_key);
1100 int dsa_slave_suspend(struct net_device *slave_dev)
1102 struct dsa_slave_priv *p = netdev_priv(slave_dev);
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);
1112 return 0;
1115 int dsa_slave_resume(struct net_device *slave_dev)
1117 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1119 netif_device_attach(slave_dev);
1121 if (p->phy) {
1122 phy_resume(p->phy);
1123 phy_start(p->phy);
1126 return 0;
1129 int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1130 int port, char *name)
1132 struct net_device *master = ds->dst->master_netdev;
1133 struct net_device *slave_dev;
1134 struct dsa_slave_priv *p;
1135 int ret;
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;
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);
1150 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1151 NULL);
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;
1157 p = netdev_priv(slave_dev);
1158 p->dev = slave_dev;
1159 p->parent = ds;
1160 p->port = port;
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;
1188 p->old_pause = -1;
1189 p->old_link = -1;
1190 p->old_duplex = -1;
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;
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;
1210 netif_carrier_off(slave_dev);
1212 return 0;
1215 static bool dsa_slave_dev_check(struct net_device *dev)
1217 return dev->netdev_ops == &dsa_slave_netdev_ops;
1220 static int dsa_slave_master_changed(struct net_device *dev)
1222 struct net_device *master = netdev_master_upper_dev_get(dev);
1223 struct dsa_slave_priv *p = netdev_priv(dev);
1224 int err = 0;
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);
1232 return err;
1235 int dsa_slave_netdevice_event(struct notifier_block *unused,
1236 unsigned long event, void *ptr)
1238 struct net_device *dev;
1239 int err = 0;
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;
1247 err = dsa_slave_master_changed(dev);
1248 if (err && err != -EOPNOTSUPP)
1249 netdev_warn(dev, "failed to reflect master change\n");
1251 break;
1254 out:
1255 return NOTIFY_DONE;