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.
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 <linux/mdio.h>
19 #include <net/rtnetlink.h>
20 #include <net/switchdev.h>
21 #include <linux/if_bridge.h>
22 #include <linux/netpoll.h>
25 /* slave mii_bus handling ***************************************************/
26 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
28 struct dsa_switch *ds = bus->priv;
30 if (ds->phys_mii_mask & (1 << addr))
31 return ds->ops->phy_read(ds, addr, reg);
36 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
38 struct dsa_switch *ds = bus->priv;
40 if (ds->phys_mii_mask & (1 << addr))
41 return ds->ops->phy_write(ds, addr, reg, val);
46 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
48 ds->slave_mii_bus->priv = (void *)ds;
49 ds->slave_mii_bus->name = "dsa slave smi";
50 ds->slave_mii_bus->read = dsa_slave_phy_read;
51 ds->slave_mii_bus->write = dsa_slave_phy_write;
52 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
53 ds->dst->tree, ds->index);
54 ds->slave_mii_bus->parent = ds->dev;
55 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
59 /* slave device handling ****************************************************/
60 static int dsa_slave_get_iflink(const struct net_device *dev)
62 struct dsa_slave_priv *p = netdev_priv(dev);
64 return p->parent->dst->master_netdev->ifindex;
67 static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
69 return !!p->bridge_dev;
72 static void dsa_port_set_stp_state(struct dsa_switch *ds, int port, u8 state)
74 struct dsa_port *dp = &ds->ports[port];
76 if (ds->ops->port_stp_state_set)
77 ds->ops->port_stp_state_set(ds, port, state);
79 if (ds->ops->port_fast_age) {
80 /* Fast age FDB entries or flush appropriate forwarding database
81 * for the given port, if we are moving it from Learning or
82 * Forwarding state, to Disabled or Blocking or Listening state.
85 if ((dp->stp_state == BR_STATE_LEARNING ||
86 dp->stp_state == BR_STATE_FORWARDING) &&
87 (state == BR_STATE_DISABLED ||
88 state == BR_STATE_BLOCKING ||
89 state == BR_STATE_LISTENING))
90 ds->ops->port_fast_age(ds, port);
93 dp->stp_state = state;
96 static int dsa_slave_open(struct net_device *dev)
98 struct dsa_slave_priv *p = netdev_priv(dev);
99 struct net_device *master = p->parent->dst->master_netdev;
100 struct dsa_switch *ds = p->parent;
101 u8 stp_state = dsa_port_is_bridged(p) ?
102 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
105 if (!(master->flags & IFF_UP))
108 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
109 err = dev_uc_add(master, dev->dev_addr);
114 if (dev->flags & IFF_ALLMULTI) {
115 err = dev_set_allmulti(master, 1);
119 if (dev->flags & IFF_PROMISC) {
120 err = dev_set_promiscuity(master, 1);
125 if (ds->ops->port_enable) {
126 err = ds->ops->port_enable(ds, p->port, p->phy);
131 dsa_port_set_stp_state(ds, p->port, stp_state);
139 if (dev->flags & IFF_PROMISC)
140 dev_set_promiscuity(master, -1);
142 if (dev->flags & IFF_ALLMULTI)
143 dev_set_allmulti(master, -1);
145 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
146 dev_uc_del(master, dev->dev_addr);
151 static int dsa_slave_close(struct net_device *dev)
153 struct dsa_slave_priv *p = netdev_priv(dev);
154 struct net_device *master = p->parent->dst->master_netdev;
155 struct dsa_switch *ds = p->parent;
160 dev_mc_unsync(master, dev);
161 dev_uc_unsync(master, dev);
162 if (dev->flags & IFF_ALLMULTI)
163 dev_set_allmulti(master, -1);
164 if (dev->flags & IFF_PROMISC)
165 dev_set_promiscuity(master, -1);
167 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
168 dev_uc_del(master, dev->dev_addr);
170 if (ds->ops->port_disable)
171 ds->ops->port_disable(ds, p->port, p->phy);
173 dsa_port_set_stp_state(ds, p->port, BR_STATE_DISABLED);
178 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
180 struct dsa_slave_priv *p = netdev_priv(dev);
181 struct net_device *master = p->parent->dst->master_netdev;
183 if (dev->flags & IFF_UP) {
184 if (change & IFF_ALLMULTI)
185 dev_set_allmulti(master,
186 dev->flags & IFF_ALLMULTI ? 1 : -1);
187 if (change & IFF_PROMISC)
188 dev_set_promiscuity(master,
189 dev->flags & IFF_PROMISC ? 1 : -1);
193 static void dsa_slave_set_rx_mode(struct net_device *dev)
195 struct dsa_slave_priv *p = netdev_priv(dev);
196 struct net_device *master = p->parent->dst->master_netdev;
198 dev_mc_sync(master, dev);
199 dev_uc_sync(master, dev);
202 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
204 struct dsa_slave_priv *p = netdev_priv(dev);
205 struct net_device *master = p->parent->dst->master_netdev;
206 struct sockaddr *addr = a;
209 if (!is_valid_ether_addr(addr->sa_data))
210 return -EADDRNOTAVAIL;
212 if (!(dev->flags & IFF_UP))
215 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
216 err = dev_uc_add(master, addr->sa_data);
221 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
222 dev_uc_del(master, dev->dev_addr);
225 ether_addr_copy(dev->dev_addr, addr->sa_data);
230 static int dsa_slave_port_vlan_add(struct net_device *dev,
231 const struct switchdev_obj_port_vlan *vlan,
232 struct switchdev_trans *trans)
234 struct dsa_slave_priv *p = netdev_priv(dev);
235 struct dsa_switch *ds = p->parent;
237 if (switchdev_trans_ph_prepare(trans)) {
238 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
241 return ds->ops->port_vlan_prepare(ds, p->port, vlan, trans);
244 ds->ops->port_vlan_add(ds, p->port, vlan, trans);
249 static int dsa_slave_port_vlan_del(struct net_device *dev,
250 const struct switchdev_obj_port_vlan *vlan)
252 struct dsa_slave_priv *p = netdev_priv(dev);
253 struct dsa_switch *ds = p->parent;
255 if (!ds->ops->port_vlan_del)
258 return ds->ops->port_vlan_del(ds, p->port, vlan);
261 static int dsa_slave_port_vlan_dump(struct net_device *dev,
262 struct switchdev_obj_port_vlan *vlan,
263 switchdev_obj_dump_cb_t *cb)
265 struct dsa_slave_priv *p = netdev_priv(dev);
266 struct dsa_switch *ds = p->parent;
268 if (ds->ops->port_vlan_dump)
269 return ds->ops->port_vlan_dump(ds, p->port, vlan, cb);
274 static int dsa_slave_port_fdb_add(struct net_device *dev,
275 const struct switchdev_obj_port_fdb *fdb,
276 struct switchdev_trans *trans)
278 struct dsa_slave_priv *p = netdev_priv(dev);
279 struct dsa_switch *ds = p->parent;
281 if (switchdev_trans_ph_prepare(trans)) {
282 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
285 return ds->ops->port_fdb_prepare(ds, p->port, fdb, trans);
288 ds->ops->port_fdb_add(ds, p->port, fdb, trans);
293 static int dsa_slave_port_fdb_del(struct net_device *dev,
294 const struct switchdev_obj_port_fdb *fdb)
296 struct dsa_slave_priv *p = netdev_priv(dev);
297 struct dsa_switch *ds = p->parent;
298 int ret = -EOPNOTSUPP;
300 if (ds->ops->port_fdb_del)
301 ret = ds->ops->port_fdb_del(ds, p->port, fdb);
306 static int dsa_slave_port_fdb_dump(struct net_device *dev,
307 struct switchdev_obj_port_fdb *fdb,
308 switchdev_obj_dump_cb_t *cb)
310 struct dsa_slave_priv *p = netdev_priv(dev);
311 struct dsa_switch *ds = p->parent;
313 if (ds->ops->port_fdb_dump)
314 return ds->ops->port_fdb_dump(ds, p->port, fdb, cb);
319 static int dsa_slave_port_mdb_add(struct net_device *dev,
320 const struct switchdev_obj_port_mdb *mdb,
321 struct switchdev_trans *trans)
323 struct dsa_slave_priv *p = netdev_priv(dev);
324 struct dsa_switch *ds = p->parent;
326 if (switchdev_trans_ph_prepare(trans)) {
327 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
330 return ds->ops->port_mdb_prepare(ds, p->port, mdb, trans);
333 ds->ops->port_mdb_add(ds, p->port, mdb, trans);
338 static int dsa_slave_port_mdb_del(struct net_device *dev,
339 const struct switchdev_obj_port_mdb *mdb)
341 struct dsa_slave_priv *p = netdev_priv(dev);
342 struct dsa_switch *ds = p->parent;
344 if (ds->ops->port_mdb_del)
345 return ds->ops->port_mdb_del(ds, p->port, mdb);
350 static int dsa_slave_port_mdb_dump(struct net_device *dev,
351 struct switchdev_obj_port_mdb *mdb,
352 switchdev_obj_dump_cb_t *cb)
354 struct dsa_slave_priv *p = netdev_priv(dev);
355 struct dsa_switch *ds = p->parent;
357 if (ds->ops->port_mdb_dump)
358 return ds->ops->port_mdb_dump(ds, p->port, mdb, cb);
363 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
365 struct dsa_slave_priv *p = netdev_priv(dev);
368 return phy_mii_ioctl(p->phy, ifr, cmd);
373 static int dsa_slave_stp_state_set(struct net_device *dev,
374 const struct switchdev_attr *attr,
375 struct switchdev_trans *trans)
377 struct dsa_slave_priv *p = netdev_priv(dev);
378 struct dsa_switch *ds = p->parent;
380 if (switchdev_trans_ph_prepare(trans))
381 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
383 dsa_port_set_stp_state(ds, p->port, attr->u.stp_state);
388 static int dsa_slave_vlan_filtering(struct net_device *dev,
389 const struct switchdev_attr *attr,
390 struct switchdev_trans *trans)
392 struct dsa_slave_priv *p = netdev_priv(dev);
393 struct dsa_switch *ds = p->parent;
395 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
396 if (switchdev_trans_ph_prepare(trans))
399 if (ds->ops->port_vlan_filtering)
400 return ds->ops->port_vlan_filtering(ds, p->port,
401 attr->u.vlan_filtering);
406 static int dsa_fastest_ageing_time(struct dsa_switch *ds,
407 unsigned int ageing_time)
411 for (i = 0; i < DSA_MAX_PORTS; ++i) {
412 struct dsa_port *dp = &ds->ports[i];
414 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
415 ageing_time = dp->ageing_time;
421 static int dsa_slave_ageing_time(struct net_device *dev,
422 const struct switchdev_attr *attr,
423 struct switchdev_trans *trans)
425 struct dsa_slave_priv *p = netdev_priv(dev);
426 struct dsa_switch *ds = p->parent;
427 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
428 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
430 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
431 if (switchdev_trans_ph_prepare(trans))
434 /* Keep the fastest ageing time in case of multiple bridges */
435 ds->ports[p->port].ageing_time = ageing_time;
436 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
438 if (ds->ops->set_ageing_time)
439 return ds->ops->set_ageing_time(ds, ageing_time);
444 static int dsa_slave_port_attr_set(struct net_device *dev,
445 const struct switchdev_attr *attr,
446 struct switchdev_trans *trans)
451 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
452 ret = dsa_slave_stp_state_set(dev, attr, trans);
454 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
455 ret = dsa_slave_vlan_filtering(dev, attr, trans);
457 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
458 ret = dsa_slave_ageing_time(dev, attr, trans);
468 static int dsa_slave_port_obj_add(struct net_device *dev,
469 const struct switchdev_obj *obj,
470 struct switchdev_trans *trans)
474 /* For the prepare phase, ensure the full set of changes is feasable in
475 * one go in order to signal a failure properly. If an operation is not
476 * supported, return -EOPNOTSUPP.
480 case SWITCHDEV_OBJ_ID_PORT_FDB:
481 err = dsa_slave_port_fdb_add(dev,
482 SWITCHDEV_OBJ_PORT_FDB(obj),
485 case SWITCHDEV_OBJ_ID_PORT_MDB:
486 err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
489 case SWITCHDEV_OBJ_ID_PORT_VLAN:
490 err = dsa_slave_port_vlan_add(dev,
491 SWITCHDEV_OBJ_PORT_VLAN(obj),
502 static int dsa_slave_port_obj_del(struct net_device *dev,
503 const struct switchdev_obj *obj)
508 case SWITCHDEV_OBJ_ID_PORT_FDB:
509 err = dsa_slave_port_fdb_del(dev,
510 SWITCHDEV_OBJ_PORT_FDB(obj));
512 case SWITCHDEV_OBJ_ID_PORT_MDB:
513 err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
515 case SWITCHDEV_OBJ_ID_PORT_VLAN:
516 err = dsa_slave_port_vlan_del(dev,
517 SWITCHDEV_OBJ_PORT_VLAN(obj));
527 static int dsa_slave_port_obj_dump(struct net_device *dev,
528 struct switchdev_obj *obj,
529 switchdev_obj_dump_cb_t *cb)
534 case SWITCHDEV_OBJ_ID_PORT_FDB:
535 err = dsa_slave_port_fdb_dump(dev,
536 SWITCHDEV_OBJ_PORT_FDB(obj),
539 case SWITCHDEV_OBJ_ID_PORT_MDB:
540 err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
543 case SWITCHDEV_OBJ_ID_PORT_VLAN:
544 err = dsa_slave_port_vlan_dump(dev,
545 SWITCHDEV_OBJ_PORT_VLAN(obj),
556 static int dsa_slave_bridge_port_join(struct net_device *dev,
557 struct net_device *br)
559 struct dsa_slave_priv *p = netdev_priv(dev);
560 struct dsa_switch *ds = p->parent;
561 int ret = -EOPNOTSUPP;
565 if (ds->ops->port_bridge_join)
566 ret = ds->ops->port_bridge_join(ds, p->port, br);
568 return ret == -EOPNOTSUPP ? 0 : ret;
571 static void dsa_slave_bridge_port_leave(struct net_device *dev)
573 struct dsa_slave_priv *p = netdev_priv(dev);
574 struct dsa_switch *ds = p->parent;
577 if (ds->ops->port_bridge_leave)
578 ds->ops->port_bridge_leave(ds, p->port);
580 p->bridge_dev = NULL;
582 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
583 * so allow it to be in BR_STATE_FORWARDING to be kept functional
585 dsa_port_set_stp_state(ds, p->port, BR_STATE_FORWARDING);
588 static int dsa_slave_port_attr_get(struct net_device *dev,
589 struct switchdev_attr *attr)
591 struct dsa_slave_priv *p = netdev_priv(dev);
592 struct dsa_switch *ds = p->parent;
595 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
596 attr->u.ppid.id_len = sizeof(ds->index);
597 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
606 static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
609 #ifdef CONFIG_NET_POLL_CONTROLLER
611 netpoll_send_skb(p->netpoll, skb);
618 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
620 struct dsa_slave_priv *p = netdev_priv(dev);
621 struct sk_buff *nskb;
623 dev->stats.tx_packets++;
624 dev->stats.tx_bytes += skb->len;
626 /* Transmit function may have to reallocate the original SKB */
627 nskb = p->xmit(skb, dev);
631 /* SKB for netpoll still need to be mangled with the protocol-specific
632 * tag to be successfully transmitted
634 if (unlikely(netpoll_tx_running(dev)))
635 return dsa_netpoll_send_skb(p, nskb);
637 /* Queue the SKB for transmission on the parent interface, but
638 * do not modify its EtherType
640 nskb->dev = p->parent->dst->master_netdev;
641 dev_queue_xmit(nskb);
646 /* ethtool operations *******************************************************/
648 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
650 struct dsa_slave_priv *p = netdev_priv(dev);
654 if (p->phy != NULL) {
655 err = phy_read_status(p->phy);
657 err = phy_ethtool_gset(p->phy, cmd);
664 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
666 struct dsa_slave_priv *p = netdev_priv(dev);
669 return phy_ethtool_sset(p->phy, cmd);
674 static void dsa_slave_get_drvinfo(struct net_device *dev,
675 struct ethtool_drvinfo *drvinfo)
677 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
678 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
679 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
680 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
683 static int dsa_slave_get_regs_len(struct net_device *dev)
685 struct dsa_slave_priv *p = netdev_priv(dev);
686 struct dsa_switch *ds = p->parent;
688 if (ds->ops->get_regs_len)
689 return ds->ops->get_regs_len(ds, p->port);
695 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
697 struct dsa_slave_priv *p = netdev_priv(dev);
698 struct dsa_switch *ds = p->parent;
700 if (ds->ops->get_regs)
701 ds->ops->get_regs(ds, p->port, regs, _p);
704 static int dsa_slave_nway_reset(struct net_device *dev)
706 struct dsa_slave_priv *p = netdev_priv(dev);
709 return genphy_restart_aneg(p->phy);
714 static u32 dsa_slave_get_link(struct net_device *dev)
716 struct dsa_slave_priv *p = netdev_priv(dev);
718 if (p->phy != NULL) {
719 genphy_update_link(p->phy);
726 static int dsa_slave_get_eeprom_len(struct net_device *dev)
728 struct dsa_slave_priv *p = netdev_priv(dev);
729 struct dsa_switch *ds = p->parent;
731 if (ds->cd && ds->cd->eeprom_len)
732 return ds->cd->eeprom_len;
734 if (ds->ops->get_eeprom_len)
735 return ds->ops->get_eeprom_len(ds);
740 static int dsa_slave_get_eeprom(struct net_device *dev,
741 struct ethtool_eeprom *eeprom, u8 *data)
743 struct dsa_slave_priv *p = netdev_priv(dev);
744 struct dsa_switch *ds = p->parent;
746 if (ds->ops->get_eeprom)
747 return ds->ops->get_eeprom(ds, eeprom, data);
752 static int dsa_slave_set_eeprom(struct net_device *dev,
753 struct ethtool_eeprom *eeprom, u8 *data)
755 struct dsa_slave_priv *p = netdev_priv(dev);
756 struct dsa_switch *ds = p->parent;
758 if (ds->ops->set_eeprom)
759 return ds->ops->set_eeprom(ds, eeprom, data);
764 static void dsa_slave_get_strings(struct net_device *dev,
765 uint32_t stringset, uint8_t *data)
767 struct dsa_slave_priv *p = netdev_priv(dev);
768 struct dsa_switch *ds = p->parent;
770 if (stringset == ETH_SS_STATS) {
771 int len = ETH_GSTRING_LEN;
773 strncpy(data, "tx_packets", len);
774 strncpy(data + len, "tx_bytes", len);
775 strncpy(data + 2 * len, "rx_packets", len);
776 strncpy(data + 3 * len, "rx_bytes", len);
777 if (ds->ops->get_strings)
778 ds->ops->get_strings(ds, p->port, data + 4 * len);
782 static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
783 struct ethtool_stats *stats,
786 struct dsa_switch_tree *dst = dev->dsa_ptr;
787 struct dsa_switch *ds = dst->ds[0];
788 s8 cpu_port = dst->cpu_port;
791 if (dst->master_ethtool_ops.get_sset_count) {
792 count = dst->master_ethtool_ops.get_sset_count(dev,
794 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
797 if (ds->ops->get_ethtool_stats)
798 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
801 static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
803 struct dsa_switch_tree *dst = dev->dsa_ptr;
804 struct dsa_switch *ds = dst->ds[0];
807 if (dst->master_ethtool_ops.get_sset_count)
808 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
810 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
811 count += ds->ops->get_sset_count(ds);
816 static void dsa_cpu_port_get_strings(struct net_device *dev,
817 uint32_t stringset, uint8_t *data)
819 struct dsa_switch_tree *dst = dev->dsa_ptr;
820 struct dsa_switch *ds = dst->ds[0];
821 s8 cpu_port = dst->cpu_port;
822 int len = ETH_GSTRING_LEN;
823 int mcount = 0, count;
828 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
829 /* We do not want to be NULL-terminated, since this is a prefix */
830 pfx[sizeof(pfx) - 1] = '_';
832 if (dst->master_ethtool_ops.get_sset_count) {
833 mcount = dst->master_ethtool_ops.get_sset_count(dev,
835 dst->master_ethtool_ops.get_strings(dev, stringset, data);
838 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
839 ndata = data + mcount * len;
840 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
841 * the output after to prepend our CPU port prefix we
842 * constructed earlier
844 ds->ops->get_strings(ds, cpu_port, ndata);
845 count = ds->ops->get_sset_count(ds);
846 for (i = 0; i < count; i++) {
847 memmove(ndata + (i * len + sizeof(pfx)),
848 ndata + i * len, len - sizeof(pfx));
849 memcpy(ndata + i * len, pfx, sizeof(pfx));
854 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
855 struct ethtool_stats *stats,
858 struct dsa_slave_priv *p = netdev_priv(dev);
859 struct dsa_switch *ds = p->parent;
861 data[0] = dev->stats.tx_packets;
862 data[1] = dev->stats.tx_bytes;
863 data[2] = dev->stats.rx_packets;
864 data[3] = dev->stats.rx_bytes;
865 if (ds->ops->get_ethtool_stats)
866 ds->ops->get_ethtool_stats(ds, p->port, data + 4);
869 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
871 struct dsa_slave_priv *p = netdev_priv(dev);
872 struct dsa_switch *ds = p->parent;
874 if (sset == ETH_SS_STATS) {
878 if (ds->ops->get_sset_count)
879 count += ds->ops->get_sset_count(ds);
887 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
889 struct dsa_slave_priv *p = netdev_priv(dev);
890 struct dsa_switch *ds = p->parent;
892 if (ds->ops->get_wol)
893 ds->ops->get_wol(ds, p->port, w);
896 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
898 struct dsa_slave_priv *p = netdev_priv(dev);
899 struct dsa_switch *ds = p->parent;
900 int ret = -EOPNOTSUPP;
902 if (ds->ops->set_wol)
903 ret = ds->ops->set_wol(ds, p->port, w);
908 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
910 struct dsa_slave_priv *p = netdev_priv(dev);
911 struct dsa_switch *ds = p->parent;
914 if (!ds->ops->set_eee)
917 ret = ds->ops->set_eee(ds, p->port, p->phy, e);
922 ret = phy_ethtool_set_eee(p->phy, e);
927 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
929 struct dsa_slave_priv *p = netdev_priv(dev);
930 struct dsa_switch *ds = p->parent;
933 if (!ds->ops->get_eee)
936 ret = ds->ops->get_eee(ds, p->port, e);
941 ret = phy_ethtool_get_eee(p->phy, e);
946 #ifdef CONFIG_NET_POLL_CONTROLLER
947 static int dsa_slave_netpoll_setup(struct net_device *dev,
948 struct netpoll_info *ni)
950 struct dsa_slave_priv *p = netdev_priv(dev);
951 struct dsa_switch *ds = p->parent;
952 struct net_device *master = ds->dst->master_netdev;
953 struct netpoll *netpoll;
956 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
960 err = __netpoll_setup(netpoll, master);
966 p->netpoll = netpoll;
971 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
973 struct dsa_slave_priv *p = netdev_priv(dev);
974 struct netpoll *netpoll = p->netpoll;
981 __netpoll_free_async(netpoll);
984 static void dsa_slave_poll_controller(struct net_device *dev)
989 void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
991 ops->get_sset_count = dsa_cpu_port_get_sset_count;
992 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
993 ops->get_strings = dsa_cpu_port_get_strings;
996 static const struct ethtool_ops dsa_slave_ethtool_ops = {
997 .get_settings = dsa_slave_get_settings,
998 .set_settings = dsa_slave_set_settings,
999 .get_drvinfo = dsa_slave_get_drvinfo,
1000 .get_regs_len = dsa_slave_get_regs_len,
1001 .get_regs = dsa_slave_get_regs,
1002 .nway_reset = dsa_slave_nway_reset,
1003 .get_link = dsa_slave_get_link,
1004 .get_eeprom_len = dsa_slave_get_eeprom_len,
1005 .get_eeprom = dsa_slave_get_eeprom,
1006 .set_eeprom = dsa_slave_set_eeprom,
1007 .get_strings = dsa_slave_get_strings,
1008 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1009 .get_sset_count = dsa_slave_get_sset_count,
1010 .set_wol = dsa_slave_set_wol,
1011 .get_wol = dsa_slave_get_wol,
1012 .set_eee = dsa_slave_set_eee,
1013 .get_eee = dsa_slave_get_eee,
1016 static const struct net_device_ops dsa_slave_netdev_ops = {
1017 .ndo_open = dsa_slave_open,
1018 .ndo_stop = dsa_slave_close,
1019 .ndo_start_xmit = dsa_slave_xmit,
1020 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1021 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1022 .ndo_set_mac_address = dsa_slave_set_mac_address,
1023 .ndo_fdb_add = switchdev_port_fdb_add,
1024 .ndo_fdb_del = switchdev_port_fdb_del,
1025 .ndo_fdb_dump = switchdev_port_fdb_dump,
1026 .ndo_do_ioctl = dsa_slave_ioctl,
1027 .ndo_get_iflink = dsa_slave_get_iflink,
1028 #ifdef CONFIG_NET_POLL_CONTROLLER
1029 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1030 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1031 .ndo_poll_controller = dsa_slave_poll_controller,
1033 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1034 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1035 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
1038 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1039 .switchdev_port_attr_get = dsa_slave_port_attr_get,
1040 .switchdev_port_attr_set = dsa_slave_port_attr_set,
1041 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1042 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1043 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
1046 static struct device_type dsa_type = {
1050 static void dsa_slave_adjust_link(struct net_device *dev)
1052 struct dsa_slave_priv *p = netdev_priv(dev);
1053 struct dsa_switch *ds = p->parent;
1054 unsigned int status_changed = 0;
1056 if (p->old_link != p->phy->link) {
1058 p->old_link = p->phy->link;
1061 if (p->old_duplex != p->phy->duplex) {
1063 p->old_duplex = p->phy->duplex;
1066 if (p->old_pause != p->phy->pause) {
1068 p->old_pause = p->phy->pause;
1071 if (ds->ops->adjust_link && status_changed)
1072 ds->ops->adjust_link(ds, p->port, p->phy);
1075 phy_print_status(p->phy);
1078 static int dsa_slave_fixed_link_update(struct net_device *dev,
1079 struct fixed_phy_status *status)
1081 struct dsa_slave_priv *p;
1082 struct dsa_switch *ds;
1085 p = netdev_priv(dev);
1087 if (ds->ops->fixed_link_update)
1088 ds->ops->fixed_link_update(ds, p->port, status);
1094 /* slave device setup *******************************************************/
1095 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1096 struct net_device *slave_dev,
1099 struct dsa_switch *ds = p->parent;
1101 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1103 netdev_err(slave_dev, "no phy at %d\n", addr);
1107 /* Use already configured phy mode */
1108 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1109 p->phy_interface = p->phy->interface;
1110 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1114 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
1115 struct net_device *slave_dev)
1117 struct dsa_switch *ds = p->parent;
1118 struct device_node *phy_dn, *port_dn;
1119 bool phy_is_fixed = false;
1123 port_dn = ds->ports[p->port].dn;
1124 mode = of_get_phy_mode(port_dn);
1126 mode = PHY_INTERFACE_MODE_NA;
1127 p->phy_interface = mode;
1129 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1130 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1131 /* In the case of a fixed PHY, the DT node associated
1132 * to the fixed PHY is the Port DT node
1134 ret = of_phy_register_fixed_link(port_dn);
1136 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1139 phy_is_fixed = true;
1140 phy_dn = of_node_get(port_dn);
1143 if (ds->ops->get_phy_flags)
1144 phy_flags = ds->ops->get_phy_flags(ds, p->port);
1147 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1149 /* If this PHY address is part of phys_mii_mask, which means
1150 * that we need to divert reads and writes to/from it, then we
1151 * want to bind this device using the slave MII bus created by
1152 * DSA to make that happen.
1154 if (!phy_is_fixed && phy_id >= 0 &&
1155 (ds->phys_mii_mask & (1 << phy_id))) {
1156 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1158 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1159 of_node_put(phy_dn);
1163 p->phy = of_phy_connect(slave_dev, phy_dn,
1164 dsa_slave_adjust_link,
1169 of_node_put(phy_dn);
1172 if (p->phy && phy_is_fixed)
1173 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1175 /* We could not connect to a designated PHY, so use the switch internal
1179 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
1181 netdev_err(slave_dev, "failed to connect to port %d: %d\n", p->port, ret);
1183 of_phy_deregister_fixed_link(port_dn);
1188 phy_attached_info(p->phy);
1193 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1194 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1195 struct netdev_queue *txq,
1198 lockdep_set_class(&txq->_xmit_lock,
1199 &dsa_slave_netdev_xmit_lock_key);
1202 int dsa_slave_suspend(struct net_device *slave_dev)
1204 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1206 if (!netif_running(slave_dev))
1209 netif_device_detach(slave_dev);
1216 phy_suspend(p->phy);
1222 int dsa_slave_resume(struct net_device *slave_dev)
1224 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1226 if (!netif_running(slave_dev))
1229 netif_device_attach(slave_dev);
1239 int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1240 int port, const char *name)
1242 struct dsa_switch_tree *dst = ds->dst;
1243 struct net_device *master;
1244 struct net_device *slave_dev;
1245 struct dsa_slave_priv *p;
1248 master = ds->dst->master_netdev;
1249 if (ds->master_netdev)
1250 master = ds->master_netdev;
1252 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1253 NET_NAME_UNKNOWN, ether_setup);
1254 if (slave_dev == NULL)
1257 slave_dev->features = master->vlan_features;
1258 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1259 eth_hw_addr_inherit(slave_dev, master);
1260 slave_dev->priv_flags |= IFF_NO_QUEUE;
1261 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1262 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1263 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1265 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1268 SET_NETDEV_DEV(slave_dev, parent);
1269 slave_dev->dev.of_node = ds->ports[port].dn;
1270 slave_dev->vlan_features = master->vlan_features;
1272 p = netdev_priv(slave_dev);
1275 p->xmit = dst->tag_ops->xmit;
1281 ds->ports[port].netdev = slave_dev;
1283 netif_carrier_off(slave_dev);
1285 ret = dsa_slave_phy_setup(p, slave_dev);
1287 netdev_err(master, "error %d setting up slave phy\n", ret);
1291 ret = register_netdev(slave_dev);
1293 netdev_err(master, "error %d registering interface %s\n",
1294 ret, slave_dev->name);
1301 phy_disconnect(p->phy);
1302 if (of_phy_is_fixed_link(ds->ports[port].dn))
1303 of_phy_deregister_fixed_link(ds->ports[port].dn);
1305 free_netdev(slave_dev);
1306 ds->ports[port].netdev = NULL;
1310 void dsa_slave_destroy(struct net_device *slave_dev)
1312 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1313 struct dsa_switch *ds = p->parent;
1314 struct device_node *port_dn;
1316 port_dn = ds->ports[p->port].dn;
1318 netif_carrier_off(slave_dev);
1320 phy_disconnect(p->phy);
1322 if (of_phy_is_fixed_link(port_dn))
1323 of_phy_deregister_fixed_link(port_dn);
1325 unregister_netdev(slave_dev);
1326 free_netdev(slave_dev);
1329 static bool dsa_slave_dev_check(struct net_device *dev)
1331 return dev->netdev_ops == &dsa_slave_netdev_ops;
1334 static int dsa_slave_port_upper_event(struct net_device *dev,
1335 unsigned long event, void *ptr)
1337 struct netdev_notifier_changeupper_info *info = ptr;
1338 struct net_device *upper = info->upper_dev;
1342 case NETDEV_CHANGEUPPER:
1343 if (netif_is_bridge_master(upper)) {
1345 err = dsa_slave_bridge_port_join(dev, upper);
1347 dsa_slave_bridge_port_leave(dev);
1353 return notifier_from_errno(err);
1356 static int dsa_slave_port_event(struct net_device *dev, unsigned long event,
1360 case NETDEV_CHANGEUPPER:
1361 return dsa_slave_port_upper_event(dev, event, ptr);
1367 int dsa_slave_netdevice_event(struct notifier_block *unused,
1368 unsigned long event, void *ptr)
1370 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1372 if (dsa_slave_dev_check(dev))
1373 return dsa_slave_port_event(dev, event, ptr);