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 <linux/list.h>
20 #include <net/rtnetlink.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_mirred.h>
23 #include <linux/if_bridge.h>
24 #include <linux/netpoll.h>
28 static bool dsa_slave_dev_check(struct net_device *dev);
30 static int dsa_port_notify(struct dsa_port *dp, unsigned long e, void *v)
32 struct raw_notifier_head *nh = &dp->ds->dst->nh;
35 err = raw_notifier_call_chain(nh, e, v);
37 return notifier_to_errno(err);
40 /* slave mii_bus handling ***************************************************/
41 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
43 struct dsa_switch *ds = bus->priv;
45 if (ds->phys_mii_mask & (1 << addr))
46 return ds->ops->phy_read(ds, addr, reg);
51 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
53 struct dsa_switch *ds = bus->priv;
55 if (ds->phys_mii_mask & (1 << addr))
56 return ds->ops->phy_write(ds, addr, reg, val);
61 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
63 ds->slave_mii_bus->priv = (void *)ds;
64 ds->slave_mii_bus->name = "dsa slave smi";
65 ds->slave_mii_bus->read = dsa_slave_phy_read;
66 ds->slave_mii_bus->write = dsa_slave_phy_write;
67 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
68 ds->dst->tree, ds->index);
69 ds->slave_mii_bus->parent = ds->dev;
70 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
74 /* slave device handling ****************************************************/
75 static int dsa_slave_get_iflink(const struct net_device *dev)
77 struct dsa_slave_priv *p = netdev_priv(dev);
79 return p->dp->ds->dst->master_netdev->ifindex;
82 static inline bool dsa_port_is_bridged(struct dsa_port *dp)
84 return !!dp->bridge_dev;
87 static int dsa_port_set_state(struct dsa_port *dp, u8 state,
88 struct switchdev_trans *trans)
90 struct dsa_switch *ds = dp->ds;
93 if (switchdev_trans_ph_prepare(trans))
94 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
96 if (ds->ops->port_stp_state_set)
97 ds->ops->port_stp_state_set(ds, port, state);
99 if (ds->ops->port_fast_age) {
100 /* Fast age FDB entries or flush appropriate forwarding database
101 * for the given port, if we are moving it from Learning or
102 * Forwarding state, to Disabled or Blocking or Listening state.
105 if ((dp->stp_state == BR_STATE_LEARNING ||
106 dp->stp_state == BR_STATE_FORWARDING) &&
107 (state == BR_STATE_DISABLED ||
108 state == BR_STATE_BLOCKING ||
109 state == BR_STATE_LISTENING))
110 ds->ops->port_fast_age(ds, port);
113 dp->stp_state = state;
118 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
122 err = dsa_port_set_state(dp, state, NULL);
124 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
127 static int dsa_slave_open(struct net_device *dev)
129 struct dsa_slave_priv *p = netdev_priv(dev);
130 struct net_device *master = p->dp->ds->dst->master_netdev;
131 struct dsa_switch *ds = p->dp->ds;
132 u8 stp_state = dsa_port_is_bridged(p->dp) ?
133 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
136 if (!(master->flags & IFF_UP))
139 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
140 err = dev_uc_add(master, dev->dev_addr);
145 if (dev->flags & IFF_ALLMULTI) {
146 err = dev_set_allmulti(master, 1);
150 if (dev->flags & IFF_PROMISC) {
151 err = dev_set_promiscuity(master, 1);
156 if (ds->ops->port_enable) {
157 err = ds->ops->port_enable(ds, p->dp->index, p->phy);
162 dsa_port_set_state_now(p->dp, stp_state);
170 if (dev->flags & IFF_PROMISC)
171 dev_set_promiscuity(master, -1);
173 if (dev->flags & IFF_ALLMULTI)
174 dev_set_allmulti(master, -1);
176 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
177 dev_uc_del(master, dev->dev_addr);
182 static int dsa_slave_close(struct net_device *dev)
184 struct dsa_slave_priv *p = netdev_priv(dev);
185 struct net_device *master = p->dp->ds->dst->master_netdev;
186 struct dsa_switch *ds = p->dp->ds;
191 dev_mc_unsync(master, dev);
192 dev_uc_unsync(master, dev);
193 if (dev->flags & IFF_ALLMULTI)
194 dev_set_allmulti(master, -1);
195 if (dev->flags & IFF_PROMISC)
196 dev_set_promiscuity(master, -1);
198 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
199 dev_uc_del(master, dev->dev_addr);
201 if (ds->ops->port_disable)
202 ds->ops->port_disable(ds, p->dp->index, p->phy);
204 dsa_port_set_state_now(p->dp, BR_STATE_DISABLED);
209 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
211 struct dsa_slave_priv *p = netdev_priv(dev);
212 struct net_device *master = p->dp->ds->dst->master_netdev;
214 if (change & IFF_ALLMULTI)
215 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
216 if (change & IFF_PROMISC)
217 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
220 static void dsa_slave_set_rx_mode(struct net_device *dev)
222 struct dsa_slave_priv *p = netdev_priv(dev);
223 struct net_device *master = p->dp->ds->dst->master_netdev;
225 dev_mc_sync(master, dev);
226 dev_uc_sync(master, dev);
229 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
231 struct dsa_slave_priv *p = netdev_priv(dev);
232 struct net_device *master = p->dp->ds->dst->master_netdev;
233 struct sockaddr *addr = a;
236 if (!is_valid_ether_addr(addr->sa_data))
237 return -EADDRNOTAVAIL;
239 if (!(dev->flags & IFF_UP))
242 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
243 err = dev_uc_add(master, addr->sa_data);
248 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
249 dev_uc_del(master, dev->dev_addr);
252 ether_addr_copy(dev->dev_addr, addr->sa_data);
257 static int dsa_slave_port_vlan_add(struct net_device *dev,
258 const struct switchdev_obj_port_vlan *vlan,
259 struct switchdev_trans *trans)
261 struct dsa_slave_priv *p = netdev_priv(dev);
262 struct dsa_port *dp = p->dp;
263 struct dsa_switch *ds = dp->ds;
265 if (switchdev_trans_ph_prepare(trans)) {
266 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
269 return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
272 ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
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->dp->ds;
283 if (!ds->ops->port_vlan_del)
286 return ds->ops->port_vlan_del(ds, p->dp->index, 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->dp->ds;
296 if (ds->ops->port_vlan_dump)
297 return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
302 static int dsa_port_fdb_add(struct dsa_port *dp,
303 const struct switchdev_obj_port_fdb *fdb,
304 struct switchdev_trans *trans)
306 struct dsa_switch *ds = dp->ds;
308 if (switchdev_trans_ph_prepare(trans)) {
309 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
312 return ds->ops->port_fdb_prepare(ds, dp->index, fdb, trans);
315 ds->ops->port_fdb_add(ds, dp->index, fdb, trans);
320 static int dsa_port_fdb_del(struct dsa_port *dp,
321 const struct switchdev_obj_port_fdb *fdb)
323 struct dsa_switch *ds = dp->ds;
324 int ret = -EOPNOTSUPP;
326 if (ds->ops->port_fdb_del)
327 ret = ds->ops->port_fdb_del(ds, dp->index, fdb);
332 static int dsa_port_fdb_dump(struct dsa_port *dp,
333 struct switchdev_obj_port_fdb *fdb,
334 switchdev_obj_dump_cb_t *cb)
336 struct dsa_switch *ds = dp->ds;
338 if (ds->ops->port_fdb_dump)
339 return ds->ops->port_fdb_dump(ds, dp->index, fdb, cb);
344 static int dsa_port_mdb_add(struct dsa_port *dp,
345 const struct switchdev_obj_port_mdb *mdb,
346 struct switchdev_trans *trans)
348 struct dsa_switch *ds = dp->ds;
350 if (switchdev_trans_ph_prepare(trans)) {
351 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
354 return ds->ops->port_mdb_prepare(ds, dp->index, mdb, trans);
357 ds->ops->port_mdb_add(ds, dp->index, mdb, trans);
362 static int dsa_port_mdb_del(struct dsa_port *dp,
363 const struct switchdev_obj_port_mdb *mdb)
365 struct dsa_switch *ds = dp->ds;
367 if (ds->ops->port_mdb_del)
368 return ds->ops->port_mdb_del(ds, dp->index, mdb);
373 static int dsa_port_mdb_dump(struct dsa_port *dp,
374 struct switchdev_obj_port_mdb *mdb,
375 switchdev_obj_dump_cb_t *cb)
377 struct dsa_switch *ds = dp->ds;
379 if (ds->ops->port_mdb_dump)
380 return ds->ops->port_mdb_dump(ds, dp->index, mdb, cb);
385 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
387 struct dsa_slave_priv *p = netdev_priv(dev);
390 return phy_mii_ioctl(p->phy, ifr, cmd);
395 static int dsa_slave_vlan_filtering(struct net_device *dev,
396 const struct switchdev_attr *attr,
397 struct switchdev_trans *trans)
399 struct dsa_slave_priv *p = netdev_priv(dev);
400 struct dsa_switch *ds = p->dp->ds;
402 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
403 if (switchdev_trans_ph_prepare(trans))
406 if (ds->ops->port_vlan_filtering)
407 return ds->ops->port_vlan_filtering(ds, p->dp->index,
408 attr->u.vlan_filtering);
413 static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
414 unsigned int ageing_time)
418 for (i = 0; i < ds->num_ports; ++i) {
419 struct dsa_port *dp = &ds->ports[i];
421 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
422 ageing_time = dp->ageing_time;
428 static int dsa_slave_ageing_time(struct net_device *dev,
429 const struct switchdev_attr *attr,
430 struct switchdev_trans *trans)
432 struct dsa_slave_priv *p = netdev_priv(dev);
433 struct dsa_switch *ds = p->dp->ds;
434 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
435 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
437 if (switchdev_trans_ph_prepare(trans)) {
438 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
440 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
445 /* Keep the fastest ageing time in case of multiple bridges */
446 p->dp->ageing_time = ageing_time;
447 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
449 if (ds->ops->set_ageing_time)
450 return ds->ops->set_ageing_time(ds, ageing_time);
455 static int dsa_slave_port_attr_set(struct net_device *dev,
456 const struct switchdev_attr *attr,
457 struct switchdev_trans *trans)
459 struct dsa_slave_priv *p = netdev_priv(dev);
460 struct dsa_port *dp = p->dp;
464 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
465 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
467 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
468 ret = dsa_slave_vlan_filtering(dev, attr, trans);
470 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
471 ret = dsa_slave_ageing_time(dev, attr, trans);
481 static int dsa_slave_port_obj_add(struct net_device *dev,
482 const struct switchdev_obj *obj,
483 struct switchdev_trans *trans)
485 struct dsa_slave_priv *p = netdev_priv(dev);
486 struct dsa_port *dp = p->dp;
489 /* For the prepare phase, ensure the full set of changes is feasable in
490 * one go in order to signal a failure properly. If an operation is not
491 * supported, return -EOPNOTSUPP.
495 case SWITCHDEV_OBJ_ID_PORT_FDB:
496 err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans);
498 case SWITCHDEV_OBJ_ID_PORT_MDB:
499 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
501 case SWITCHDEV_OBJ_ID_PORT_VLAN:
502 err = dsa_slave_port_vlan_add(dev,
503 SWITCHDEV_OBJ_PORT_VLAN(obj),
514 static int dsa_slave_port_obj_del(struct net_device *dev,
515 const struct switchdev_obj *obj)
517 struct dsa_slave_priv *p = netdev_priv(dev);
518 struct dsa_port *dp = p->dp;
522 case SWITCHDEV_OBJ_ID_PORT_FDB:
523 err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj));
525 case SWITCHDEV_OBJ_ID_PORT_MDB:
526 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
528 case SWITCHDEV_OBJ_ID_PORT_VLAN:
529 err = dsa_slave_port_vlan_del(dev,
530 SWITCHDEV_OBJ_PORT_VLAN(obj));
540 static int dsa_slave_port_obj_dump(struct net_device *dev,
541 struct switchdev_obj *obj,
542 switchdev_obj_dump_cb_t *cb)
544 struct dsa_slave_priv *p = netdev_priv(dev);
545 struct dsa_port *dp = p->dp;
549 case SWITCHDEV_OBJ_ID_PORT_FDB:
550 err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb);
552 case SWITCHDEV_OBJ_ID_PORT_MDB:
553 err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb);
555 case SWITCHDEV_OBJ_ID_PORT_VLAN:
556 err = dsa_slave_port_vlan_dump(dev,
557 SWITCHDEV_OBJ_PORT_VLAN(obj),
568 static int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
570 struct dsa_notifier_bridge_info info = {
571 .sw_index = dp->ds->index,
577 /* Here the port is already bridged. Reflect the current configuration
578 * so that drivers can program their chips accordingly.
582 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
584 /* The bridging is rolled back on error */
586 dp->bridge_dev = NULL;
591 static void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
593 struct dsa_notifier_bridge_info info = {
594 .sw_index = dp->ds->index,
600 /* Here the port is already unbridged. Reflect the current configuration
601 * so that drivers can program their chips accordingly.
603 dp->bridge_dev = NULL;
605 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
607 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
609 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
610 * so allow it to be in BR_STATE_FORWARDING to be kept functional
612 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
615 static int dsa_slave_port_attr_get(struct net_device *dev,
616 struct switchdev_attr *attr)
618 struct dsa_slave_priv *p = netdev_priv(dev);
619 struct dsa_switch *ds = p->dp->ds;
622 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
623 attr->u.ppid.id_len = sizeof(ds->index);
624 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
633 static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
636 #ifdef CONFIG_NET_POLL_CONTROLLER
638 netpoll_send_skb(p->netpoll, skb);
645 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
647 struct dsa_slave_priv *p = netdev_priv(dev);
648 struct sk_buff *nskb;
650 dev->stats.tx_packets++;
651 dev->stats.tx_bytes += skb->len;
653 /* Transmit function may have to reallocate the original SKB */
654 nskb = p->xmit(skb, dev);
658 /* SKB for netpoll still need to be mangled with the protocol-specific
659 * tag to be successfully transmitted
661 if (unlikely(netpoll_tx_running(dev)))
662 return dsa_netpoll_send_skb(p, nskb);
664 /* Queue the SKB for transmission on the parent interface, but
665 * do not modify its EtherType
667 nskb->dev = p->dp->ds->dst->master_netdev;
668 dev_queue_xmit(nskb);
673 /* ethtool operations *******************************************************/
675 dsa_slave_get_link_ksettings(struct net_device *dev,
676 struct ethtool_link_ksettings *cmd)
678 struct dsa_slave_priv *p = netdev_priv(dev);
679 int err = -EOPNOTSUPP;
682 err = phy_ethtool_ksettings_get(p->phy, cmd);
688 dsa_slave_set_link_ksettings(struct net_device *dev,
689 const struct ethtool_link_ksettings *cmd)
691 struct dsa_slave_priv *p = netdev_priv(dev);
694 return phy_ethtool_ksettings_set(p->phy, cmd);
699 static void dsa_slave_get_drvinfo(struct net_device *dev,
700 struct ethtool_drvinfo *drvinfo)
702 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
703 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
704 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
707 static int dsa_slave_get_regs_len(struct net_device *dev)
709 struct dsa_slave_priv *p = netdev_priv(dev);
710 struct dsa_switch *ds = p->dp->ds;
712 if (ds->ops->get_regs_len)
713 return ds->ops->get_regs_len(ds, p->dp->index);
719 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
721 struct dsa_slave_priv *p = netdev_priv(dev);
722 struct dsa_switch *ds = p->dp->ds;
724 if (ds->ops->get_regs)
725 ds->ops->get_regs(ds, p->dp->index, regs, _p);
728 static int dsa_slave_nway_reset(struct net_device *dev)
730 struct dsa_slave_priv *p = netdev_priv(dev);
733 return genphy_restart_aneg(p->phy);
738 static u32 dsa_slave_get_link(struct net_device *dev)
740 struct dsa_slave_priv *p = netdev_priv(dev);
742 if (p->phy != NULL) {
743 genphy_update_link(p->phy);
750 static int dsa_slave_get_eeprom_len(struct net_device *dev)
752 struct dsa_slave_priv *p = netdev_priv(dev);
753 struct dsa_switch *ds = p->dp->ds;
755 if (ds->cd && ds->cd->eeprom_len)
756 return ds->cd->eeprom_len;
758 if (ds->ops->get_eeprom_len)
759 return ds->ops->get_eeprom_len(ds);
764 static int dsa_slave_get_eeprom(struct net_device *dev,
765 struct ethtool_eeprom *eeprom, u8 *data)
767 struct dsa_slave_priv *p = netdev_priv(dev);
768 struct dsa_switch *ds = p->dp->ds;
770 if (ds->ops->get_eeprom)
771 return ds->ops->get_eeprom(ds, eeprom, data);
776 static int dsa_slave_set_eeprom(struct net_device *dev,
777 struct ethtool_eeprom *eeprom, u8 *data)
779 struct dsa_slave_priv *p = netdev_priv(dev);
780 struct dsa_switch *ds = p->dp->ds;
782 if (ds->ops->set_eeprom)
783 return ds->ops->set_eeprom(ds, eeprom, data);
788 static void dsa_slave_get_strings(struct net_device *dev,
789 uint32_t stringset, uint8_t *data)
791 struct dsa_slave_priv *p = netdev_priv(dev);
792 struct dsa_switch *ds = p->dp->ds;
794 if (stringset == ETH_SS_STATS) {
795 int len = ETH_GSTRING_LEN;
797 strncpy(data, "tx_packets", len);
798 strncpy(data + len, "tx_bytes", len);
799 strncpy(data + 2 * len, "rx_packets", len);
800 strncpy(data + 3 * len, "rx_bytes", len);
801 if (ds->ops->get_strings)
802 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
806 static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
807 struct ethtool_stats *stats,
810 struct dsa_switch_tree *dst = dev->dsa_ptr;
811 struct dsa_switch *ds = dst->cpu_dp->ds;
812 s8 cpu_port = dst->cpu_dp->index;
815 if (dst->master_ethtool_ops.get_sset_count) {
816 count = dst->master_ethtool_ops.get_sset_count(dev,
818 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
821 if (ds->ops->get_ethtool_stats)
822 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
825 static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
827 struct dsa_switch_tree *dst = dev->dsa_ptr;
828 struct dsa_switch *ds = dst->cpu_dp->ds;
831 if (dst->master_ethtool_ops.get_sset_count)
832 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
834 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
835 count += ds->ops->get_sset_count(ds);
840 static void dsa_cpu_port_get_strings(struct net_device *dev,
841 uint32_t stringset, uint8_t *data)
843 struct dsa_switch_tree *dst = dev->dsa_ptr;
844 struct dsa_switch *ds = dst->cpu_dp->ds;
845 s8 cpu_port = dst->cpu_dp->index;
846 int len = ETH_GSTRING_LEN;
847 int mcount = 0, count;
852 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
853 /* We do not want to be NULL-terminated, since this is a prefix */
854 pfx[sizeof(pfx) - 1] = '_';
856 if (dst->master_ethtool_ops.get_sset_count) {
857 mcount = dst->master_ethtool_ops.get_sset_count(dev,
859 dst->master_ethtool_ops.get_strings(dev, stringset, data);
862 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
863 ndata = data + mcount * len;
864 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
865 * the output after to prepend our CPU port prefix we
866 * constructed earlier
868 ds->ops->get_strings(ds, cpu_port, ndata);
869 count = ds->ops->get_sset_count(ds);
870 for (i = 0; i < count; i++) {
871 memmove(ndata + (i * len + sizeof(pfx)),
872 ndata + i * len, len - sizeof(pfx));
873 memcpy(ndata + i * len, pfx, sizeof(pfx));
878 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
879 struct ethtool_stats *stats,
882 struct dsa_slave_priv *p = netdev_priv(dev);
883 struct dsa_switch *ds = p->dp->ds;
885 data[0] = dev->stats.tx_packets;
886 data[1] = dev->stats.tx_bytes;
887 data[2] = dev->stats.rx_packets;
888 data[3] = dev->stats.rx_bytes;
889 if (ds->ops->get_ethtool_stats)
890 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
893 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
895 struct dsa_slave_priv *p = netdev_priv(dev);
896 struct dsa_switch *ds = p->dp->ds;
898 if (sset == ETH_SS_STATS) {
902 if (ds->ops->get_sset_count)
903 count += ds->ops->get_sset_count(ds);
911 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
913 struct dsa_slave_priv *p = netdev_priv(dev);
914 struct dsa_switch *ds = p->dp->ds;
916 if (ds->ops->get_wol)
917 ds->ops->get_wol(ds, p->dp->index, w);
920 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
922 struct dsa_slave_priv *p = netdev_priv(dev);
923 struct dsa_switch *ds = p->dp->ds;
924 int ret = -EOPNOTSUPP;
926 if (ds->ops->set_wol)
927 ret = ds->ops->set_wol(ds, p->dp->index, w);
932 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
934 struct dsa_slave_priv *p = netdev_priv(dev);
935 struct dsa_switch *ds = p->dp->ds;
938 if (!ds->ops->set_eee)
941 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
946 ret = phy_ethtool_set_eee(p->phy, e);
951 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
953 struct dsa_slave_priv *p = netdev_priv(dev);
954 struct dsa_switch *ds = p->dp->ds;
957 if (!ds->ops->get_eee)
960 ret = ds->ops->get_eee(ds, p->dp->index, e);
965 ret = phy_ethtool_get_eee(p->phy, e);
970 #ifdef CONFIG_NET_POLL_CONTROLLER
971 static int dsa_slave_netpoll_setup(struct net_device *dev,
972 struct netpoll_info *ni)
974 struct dsa_slave_priv *p = netdev_priv(dev);
975 struct dsa_switch *ds = p->dp->ds;
976 struct net_device *master = ds->dst->master_netdev;
977 struct netpoll *netpoll;
980 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
984 err = __netpoll_setup(netpoll, master);
990 p->netpoll = netpoll;
995 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
997 struct dsa_slave_priv *p = netdev_priv(dev);
998 struct netpoll *netpoll = p->netpoll;
1005 __netpoll_free_async(netpoll);
1008 static void dsa_slave_poll_controller(struct net_device *dev)
1013 static int dsa_slave_get_phys_port_name(struct net_device *dev,
1014 char *name, size_t len)
1016 struct dsa_slave_priv *p = netdev_priv(dev);
1018 if (snprintf(name, len, "p%d", p->dp->index) >= len)
1024 static struct dsa_mall_tc_entry *
1025 dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1026 unsigned long cookie)
1028 struct dsa_mall_tc_entry *mall_tc_entry;
1030 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1031 if (mall_tc_entry->cookie == cookie)
1032 return mall_tc_entry;
1037 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1039 struct tc_cls_matchall_offload *cls,
1042 struct dsa_slave_priv *p = netdev_priv(dev);
1043 struct dsa_mall_tc_entry *mall_tc_entry;
1044 struct dsa_switch *ds = p->dp->ds;
1045 struct net *net = dev_net(dev);
1046 struct dsa_slave_priv *to_p;
1047 struct net_device *to_dev;
1048 const struct tc_action *a;
1049 int err = -EOPNOTSUPP;
1053 if (!ds->ops->port_mirror_add)
1056 if (!tc_single_action(cls->exts))
1059 tcf_exts_to_list(cls->exts, &actions);
1060 a = list_first_entry(&actions, struct tc_action, list);
1062 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1063 struct dsa_mall_mirror_tc_entry *mirror;
1065 ifindex = tcf_mirred_ifindex(a);
1066 to_dev = __dev_get_by_index(net, ifindex);
1070 if (!dsa_slave_dev_check(to_dev))
1073 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1077 mall_tc_entry->cookie = cls->cookie;
1078 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1079 mirror = &mall_tc_entry->mirror;
1081 to_p = netdev_priv(to_dev);
1083 mirror->to_local_port = to_p->dp->index;
1084 mirror->ingress = ingress;
1086 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1089 kfree(mall_tc_entry);
1093 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1099 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1100 struct tc_cls_matchall_offload *cls)
1102 struct dsa_slave_priv *p = netdev_priv(dev);
1103 struct dsa_mall_tc_entry *mall_tc_entry;
1104 struct dsa_switch *ds = p->dp->ds;
1106 if (!ds->ops->port_mirror_del)
1109 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1113 list_del(&mall_tc_entry->list);
1115 switch (mall_tc_entry->type) {
1116 case DSA_PORT_MALL_MIRROR:
1117 ds->ops->port_mirror_del(ds, p->dp->index,
1118 &mall_tc_entry->mirror);
1124 kfree(mall_tc_entry);
1127 static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1128 __be16 protocol, struct tc_to_netdev *tc)
1130 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1131 int ret = -EOPNOTSUPP;
1134 case TC_SETUP_MATCHALL:
1135 switch (tc->cls_mall->command) {
1136 case TC_CLSMATCHALL_REPLACE:
1137 return dsa_slave_add_cls_matchall(dev, protocol,
1140 case TC_CLSMATCHALL_DESTROY:
1141 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1151 void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1153 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1154 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1155 ops->get_strings = dsa_cpu_port_get_strings;
1158 static int dsa_slave_get_rxnfc(struct net_device *dev,
1159 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1161 struct dsa_slave_priv *p = netdev_priv(dev);
1162 struct dsa_switch *ds = p->dp->ds;
1164 if (!ds->ops->get_rxnfc)
1167 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1170 static int dsa_slave_set_rxnfc(struct net_device *dev,
1171 struct ethtool_rxnfc *nfc)
1173 struct dsa_slave_priv *p = netdev_priv(dev);
1174 struct dsa_switch *ds = p->dp->ds;
1176 if (!ds->ops->set_rxnfc)
1179 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1182 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1183 .get_drvinfo = dsa_slave_get_drvinfo,
1184 .get_regs_len = dsa_slave_get_regs_len,
1185 .get_regs = dsa_slave_get_regs,
1186 .nway_reset = dsa_slave_nway_reset,
1187 .get_link = dsa_slave_get_link,
1188 .get_eeprom_len = dsa_slave_get_eeprom_len,
1189 .get_eeprom = dsa_slave_get_eeprom,
1190 .set_eeprom = dsa_slave_set_eeprom,
1191 .get_strings = dsa_slave_get_strings,
1192 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1193 .get_sset_count = dsa_slave_get_sset_count,
1194 .set_wol = dsa_slave_set_wol,
1195 .get_wol = dsa_slave_get_wol,
1196 .set_eee = dsa_slave_set_eee,
1197 .get_eee = dsa_slave_get_eee,
1198 .get_link_ksettings = dsa_slave_get_link_ksettings,
1199 .set_link_ksettings = dsa_slave_set_link_ksettings,
1200 .get_rxnfc = dsa_slave_get_rxnfc,
1201 .set_rxnfc = dsa_slave_set_rxnfc,
1204 static const struct net_device_ops dsa_slave_netdev_ops = {
1205 .ndo_open = dsa_slave_open,
1206 .ndo_stop = dsa_slave_close,
1207 .ndo_start_xmit = dsa_slave_xmit,
1208 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1209 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1210 .ndo_set_mac_address = dsa_slave_set_mac_address,
1211 .ndo_fdb_add = switchdev_port_fdb_add,
1212 .ndo_fdb_del = switchdev_port_fdb_del,
1213 .ndo_fdb_dump = switchdev_port_fdb_dump,
1214 .ndo_do_ioctl = dsa_slave_ioctl,
1215 .ndo_get_iflink = dsa_slave_get_iflink,
1216 #ifdef CONFIG_NET_POLL_CONTROLLER
1217 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1218 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1219 .ndo_poll_controller = dsa_slave_poll_controller,
1221 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1222 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1223 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
1224 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1225 .ndo_setup_tc = dsa_slave_setup_tc,
1228 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1229 .switchdev_port_attr_get = dsa_slave_port_attr_get,
1230 .switchdev_port_attr_set = dsa_slave_port_attr_set,
1231 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1232 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1233 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
1236 static struct device_type dsa_type = {
1240 static void dsa_slave_adjust_link(struct net_device *dev)
1242 struct dsa_slave_priv *p = netdev_priv(dev);
1243 struct dsa_switch *ds = p->dp->ds;
1244 unsigned int status_changed = 0;
1246 if (p->old_link != p->phy->link) {
1248 p->old_link = p->phy->link;
1251 if (p->old_duplex != p->phy->duplex) {
1253 p->old_duplex = p->phy->duplex;
1256 if (p->old_pause != p->phy->pause) {
1258 p->old_pause = p->phy->pause;
1261 if (ds->ops->adjust_link && status_changed)
1262 ds->ops->adjust_link(ds, p->dp->index, p->phy);
1265 phy_print_status(p->phy);
1268 static int dsa_slave_fixed_link_update(struct net_device *dev,
1269 struct fixed_phy_status *status)
1271 struct dsa_slave_priv *p;
1272 struct dsa_switch *ds;
1275 p = netdev_priv(dev);
1277 if (ds->ops->fixed_link_update)
1278 ds->ops->fixed_link_update(ds, p->dp->index, status);
1284 /* slave device setup *******************************************************/
1285 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1286 struct net_device *slave_dev,
1289 struct dsa_switch *ds = p->dp->ds;
1291 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1293 netdev_err(slave_dev, "no phy at %d\n", addr);
1297 /* Use already configured phy mode */
1298 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1299 p->phy_interface = p->phy->interface;
1300 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1304 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
1305 struct net_device *slave_dev)
1307 struct dsa_switch *ds = p->dp->ds;
1308 struct device_node *phy_dn, *port_dn;
1309 bool phy_is_fixed = false;
1313 port_dn = p->dp->dn;
1314 mode = of_get_phy_mode(port_dn);
1316 mode = PHY_INTERFACE_MODE_NA;
1317 p->phy_interface = mode;
1319 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1320 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1321 /* In the case of a fixed PHY, the DT node associated
1322 * to the fixed PHY is the Port DT node
1324 ret = of_phy_register_fixed_link(port_dn);
1326 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1329 phy_is_fixed = true;
1330 phy_dn = of_node_get(port_dn);
1333 if (ds->ops->get_phy_flags)
1334 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
1337 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1339 /* If this PHY address is part of phys_mii_mask, which means
1340 * that we need to divert reads and writes to/from it, then we
1341 * want to bind this device using the slave MII bus created by
1342 * DSA to make that happen.
1344 if (!phy_is_fixed && phy_id >= 0 &&
1345 (ds->phys_mii_mask & (1 << phy_id))) {
1346 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1348 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1349 of_node_put(phy_dn);
1353 p->phy = of_phy_connect(slave_dev, phy_dn,
1354 dsa_slave_adjust_link,
1359 of_node_put(phy_dn);
1362 if (p->phy && phy_is_fixed)
1363 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1365 /* We could not connect to a designated PHY, so use the switch internal
1369 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1371 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1374 of_phy_deregister_fixed_link(port_dn);
1379 phy_attached_info(p->phy);
1384 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1385 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1386 struct netdev_queue *txq,
1389 lockdep_set_class(&txq->_xmit_lock,
1390 &dsa_slave_netdev_xmit_lock_key);
1393 int dsa_slave_suspend(struct net_device *slave_dev)
1395 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1397 netif_device_detach(slave_dev);
1404 phy_suspend(p->phy);
1410 int dsa_slave_resume(struct net_device *slave_dev)
1412 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1414 netif_device_attach(slave_dev);
1424 int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1425 int port, const char *name)
1427 struct dsa_switch_tree *dst = ds->dst;
1428 struct net_device *master;
1429 struct net_device *slave_dev;
1430 struct dsa_slave_priv *p;
1433 master = ds->dst->master_netdev;
1434 if (ds->master_netdev)
1435 master = ds->master_netdev;
1437 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1438 NET_NAME_UNKNOWN, ether_setup);
1439 if (slave_dev == NULL)
1442 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1443 slave_dev->hw_features |= NETIF_F_HW_TC;
1444 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1445 eth_hw_addr_inherit(slave_dev, master);
1446 slave_dev->priv_flags |= IFF_NO_QUEUE;
1447 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1448 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1449 slave_dev->min_mtu = 0;
1450 slave_dev->max_mtu = ETH_MAX_MTU;
1451 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1453 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1456 SET_NETDEV_DEV(slave_dev, parent);
1457 slave_dev->dev.of_node = ds->ports[port].dn;
1458 slave_dev->vlan_features = master->vlan_features;
1460 p = netdev_priv(slave_dev);
1461 p->dp = &ds->ports[port];
1462 INIT_LIST_HEAD(&p->mall_tc_list);
1463 p->xmit = dst->tag_ops->xmit;
1469 ds->ports[port].netdev = slave_dev;
1470 ret = register_netdev(slave_dev);
1472 netdev_err(master, "error %d registering interface %s\n",
1473 ret, slave_dev->name);
1474 ds->ports[port].netdev = NULL;
1475 free_netdev(slave_dev);
1479 netif_carrier_off(slave_dev);
1481 ret = dsa_slave_phy_setup(p, slave_dev);
1483 netdev_err(master, "error %d setting up slave phy\n", ret);
1484 unregister_netdev(slave_dev);
1485 free_netdev(slave_dev);
1492 void dsa_slave_destroy(struct net_device *slave_dev)
1494 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1495 struct device_node *port_dn;
1497 port_dn = p->dp->dn;
1499 netif_carrier_off(slave_dev);
1501 phy_disconnect(p->phy);
1503 if (of_phy_is_fixed_link(port_dn))
1504 of_phy_deregister_fixed_link(port_dn);
1506 unregister_netdev(slave_dev);
1507 free_netdev(slave_dev);
1510 static bool dsa_slave_dev_check(struct net_device *dev)
1512 return dev->netdev_ops == &dsa_slave_netdev_ops;
1515 static int dsa_slave_changeupper(struct net_device *dev,
1516 struct netdev_notifier_changeupper_info *info)
1518 struct dsa_slave_priv *p = netdev_priv(dev);
1519 struct dsa_port *dp = p->dp;
1520 int err = NOTIFY_DONE;
1522 if (netif_is_bridge_master(info->upper_dev)) {
1523 if (info->linking) {
1524 err = dsa_port_bridge_join(dp, info->upper_dev);
1525 err = notifier_from_errno(err);
1527 dsa_port_bridge_leave(dp, info->upper_dev);
1535 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1536 unsigned long event, void *ptr)
1538 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1540 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1543 if (event == NETDEV_CHANGEUPPER)
1544 return dsa_slave_changeupper(dev, ptr);
1549 static struct notifier_block dsa_slave_nb __read_mostly = {
1550 .notifier_call = dsa_slave_netdevice_event,
1553 int dsa_slave_register_notifier(void)
1555 return register_netdevice_notifier(&dsa_slave_nb);
1558 void dsa_slave_unregister_notifier(void)
1562 err = unregister_netdevice_notifier(&dsa_slave_nb);
1564 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);