1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/dsa/slave.c - Slave device handling
4 * Copyright (c) 2008-2009 Marvell Semiconductor
7 #include <linux/list.h>
8 #include <linux/etherdevice.h>
9 #include <linux/netdevice.h>
10 #include <linux/phy.h>
11 #include <linux/phy_fixed.h>
12 #include <linux/phylink.h>
13 #include <linux/of_net.h>
14 #include <linux/of_mdio.h>
15 #include <linux/mdio.h>
16 #include <net/rtnetlink.h>
17 #include <net/pkt_cls.h>
18 #include <net/tc_act/tc_mirred.h>
19 #include <linux/if_bridge.h>
20 #include <linux/netpoll.h>
21 #include <linux/ptp_classify.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->index, 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 return dsa_slave_to_master(dev)->ifindex;
65 static int dsa_slave_open(struct net_device *dev)
67 struct net_device *master = dsa_slave_to_master(dev);
68 struct dsa_port *dp = dsa_slave_to_port(dev);
71 if (!(master->flags & IFF_UP))
74 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
75 err = dev_uc_add(master, dev->dev_addr);
80 if (dev->flags & IFF_ALLMULTI) {
81 err = dev_set_allmulti(master, 1);
85 if (dev->flags & IFF_PROMISC) {
86 err = dev_set_promiscuity(master, 1);
91 err = dsa_port_enable_rt(dp, dev->phydev);
98 if (dev->flags & IFF_PROMISC)
99 dev_set_promiscuity(master, -1);
101 if (dev->flags & IFF_ALLMULTI)
102 dev_set_allmulti(master, -1);
104 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
105 dev_uc_del(master, dev->dev_addr);
110 static int dsa_slave_close(struct net_device *dev)
112 struct net_device *master = dsa_slave_to_master(dev);
113 struct dsa_port *dp = dsa_slave_to_port(dev);
115 dsa_port_disable_rt(dp);
117 dev_mc_unsync(master, dev);
118 dev_uc_unsync(master, dev);
119 if (dev->flags & IFF_ALLMULTI)
120 dev_set_allmulti(master, -1);
121 if (dev->flags & IFF_PROMISC)
122 dev_set_promiscuity(master, -1);
124 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
125 dev_uc_del(master, dev->dev_addr);
130 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
132 struct net_device *master = dsa_slave_to_master(dev);
133 if (dev->flags & IFF_UP) {
134 if (change & IFF_ALLMULTI)
135 dev_set_allmulti(master,
136 dev->flags & IFF_ALLMULTI ? 1 : -1);
137 if (change & IFF_PROMISC)
138 dev_set_promiscuity(master,
139 dev->flags & IFF_PROMISC ? 1 : -1);
143 static void dsa_slave_set_rx_mode(struct net_device *dev)
145 struct net_device *master = dsa_slave_to_master(dev);
147 dev_mc_sync(master, dev);
148 dev_uc_sync(master, dev);
151 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
153 struct net_device *master = dsa_slave_to_master(dev);
154 struct sockaddr *addr = a;
157 if (!is_valid_ether_addr(addr->sa_data))
158 return -EADDRNOTAVAIL;
160 if (!(dev->flags & IFF_UP))
163 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
164 err = dev_uc_add(master, addr->sa_data);
169 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
170 dev_uc_del(master, dev->dev_addr);
173 ether_addr_copy(dev->dev_addr, addr->sa_data);
178 struct dsa_slave_dump_ctx {
179 struct net_device *dev;
181 struct netlink_callback *cb;
186 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
187 bool is_static, void *data)
189 struct dsa_slave_dump_ctx *dump = data;
190 u32 portid = NETLINK_CB(dump->cb->skb).portid;
191 u32 seq = dump->cb->nlh->nlmsg_seq;
192 struct nlmsghdr *nlh;
195 if (dump->idx < dump->cb->args[2])
198 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
199 sizeof(*ndm), NLM_F_MULTI);
203 ndm = nlmsg_data(nlh);
204 ndm->ndm_family = AF_BRIDGE;
207 ndm->ndm_flags = NTF_SELF;
209 ndm->ndm_ifindex = dump->dev->ifindex;
210 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
212 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
213 goto nla_put_failure;
215 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
216 goto nla_put_failure;
218 nlmsg_end(dump->skb, nlh);
225 nlmsg_cancel(dump->skb, nlh);
230 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
231 struct net_device *dev, struct net_device *filter_dev,
234 struct dsa_port *dp = dsa_slave_to_port(dev);
235 struct dsa_slave_dump_ctx dump = {
243 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
249 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
251 struct dsa_slave_priv *p = netdev_priv(dev);
252 struct dsa_switch *ds = p->dp->ds;
253 int port = p->dp->index;
255 /* Pass through to switch driver if it supports timestamping */
258 if (ds->ops->port_hwtstamp_get)
259 return ds->ops->port_hwtstamp_get(ds, port, ifr);
262 if (ds->ops->port_hwtstamp_set)
263 return ds->ops->port_hwtstamp_set(ds, port, ifr);
267 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
270 static int dsa_slave_port_attr_set(struct net_device *dev,
271 const struct switchdev_attr *attr,
272 struct switchdev_trans *trans)
274 struct dsa_port *dp = dsa_slave_to_port(dev);
278 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
279 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
281 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
282 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
285 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
286 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
288 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
289 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
292 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
293 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
295 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
296 ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans);
306 /* Must be called under rcu_read_lock() */
308 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
309 const struct switchdev_obj_port_vlan *vlan)
311 struct net_device *upper_dev;
312 struct list_head *iter;
314 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
317 if (!is_vlan_dev(upper_dev))
320 vid = vlan_dev_vlan_id(upper_dev);
321 if (vid >= vlan->vid_begin && vid <= vlan->vid_end)
328 static int dsa_slave_vlan_add(struct net_device *dev,
329 const struct switchdev_obj *obj,
330 struct switchdev_trans *trans)
332 struct net_device *master = dsa_slave_to_master(dev);
333 struct dsa_port *dp = dsa_slave_to_port(dev);
334 struct switchdev_obj_port_vlan vlan;
337 if (obj->orig_dev != dev)
340 if (dsa_port_skip_vlan_configuration(dp))
343 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
345 /* Deny adding a bridge VLAN when there is already an 802.1Q upper with
348 if (trans->ph_prepare && br_vlan_enabled(dp->bridge_dev)) {
350 err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
356 err = dsa_port_vlan_add(dp, &vlan, trans);
360 /* We need the dedicated CPU port to be a member of the VLAN as well.
361 * Even though drivers often handle CPU membership in special ways,
362 * it doesn't make sense to program a PVID, so clear this flag.
364 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
366 err = dsa_port_vlan_add(dp->cpu_dp, &vlan, trans);
370 for (vid = vlan.vid_begin; vid <= vlan.vid_end; vid++) {
371 err = vlan_vid_add(master, htons(ETH_P_8021Q), vid);
379 static int dsa_slave_port_obj_add(struct net_device *dev,
380 const struct switchdev_obj *obj,
381 struct switchdev_trans *trans,
382 struct netlink_ext_ack *extack)
384 struct dsa_port *dp = dsa_slave_to_port(dev);
387 /* For the prepare phase, ensure the full set of changes is feasable in
388 * one go in order to signal a failure properly. If an operation is not
389 * supported, return -EOPNOTSUPP.
393 case SWITCHDEV_OBJ_ID_PORT_MDB:
394 if (obj->orig_dev != dev)
396 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
398 case SWITCHDEV_OBJ_ID_HOST_MDB:
399 /* DSA can directly translate this to a normal MDB add,
400 * but on the CPU port.
402 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
405 case SWITCHDEV_OBJ_ID_PORT_VLAN:
406 err = dsa_slave_vlan_add(dev, obj, trans);
416 static int dsa_slave_vlan_del(struct net_device *dev,
417 const struct switchdev_obj *obj)
419 struct net_device *master = dsa_slave_to_master(dev);
420 struct dsa_port *dp = dsa_slave_to_port(dev);
421 struct switchdev_obj_port_vlan *vlan;
424 if (obj->orig_dev != dev)
427 if (dsa_port_skip_vlan_configuration(dp))
430 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
432 /* Do not deprogram the CPU port as it may be shared with other user
433 * ports which can be members of this VLAN as well.
435 err = dsa_port_vlan_del(dp, vlan);
439 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
440 vlan_vid_del(master, htons(ETH_P_8021Q), vid);
445 static int dsa_slave_port_obj_del(struct net_device *dev,
446 const struct switchdev_obj *obj)
448 struct dsa_port *dp = dsa_slave_to_port(dev);
452 case SWITCHDEV_OBJ_ID_PORT_MDB:
453 if (obj->orig_dev != dev)
455 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
457 case SWITCHDEV_OBJ_ID_HOST_MDB:
458 /* DSA can directly translate this to a normal MDB add,
459 * but on the CPU port.
461 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
463 case SWITCHDEV_OBJ_ID_PORT_VLAN:
464 err = dsa_slave_vlan_del(dev, obj);
474 static int dsa_slave_get_port_parent_id(struct net_device *dev,
475 struct netdev_phys_item_id *ppid)
477 struct dsa_port *dp = dsa_slave_to_port(dev);
478 struct dsa_switch *ds = dp->ds;
479 struct dsa_switch_tree *dst = ds->dst;
481 /* For non-legacy ports, devlink is used and it takes
482 * care of the name generation. This ndo implementation
483 * should be removed with legacy support.
488 ppid->id_len = sizeof(dst->index);
489 memcpy(&ppid->id, &dst->index, ppid->id_len);
494 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
497 #ifdef CONFIG_NET_POLL_CONTROLLER
498 struct dsa_slave_priv *p = netdev_priv(dev);
500 return netpoll_send_skb(p->netpoll, skb);
507 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
510 struct dsa_switch *ds = p->dp->ds;
511 struct sk_buff *clone;
514 type = ptp_classify_raw(skb);
515 if (type == PTP_CLASS_NONE)
518 if (!ds->ops->port_txtstamp)
521 clone = skb_clone_sk(skb);
525 DSA_SKB_CB(skb)->clone = clone;
527 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
533 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
535 /* SKB for netpoll still need to be mangled with the protocol-specific
536 * tag to be successfully transmitted
538 if (unlikely(netpoll_tx_running(dev)))
539 return dsa_slave_netpoll_send_skb(dev, skb);
541 /* Queue the SKB for transmission on the parent interface, but
542 * do not modify its EtherType
544 skb->dev = dsa_slave_to_master(dev);
549 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
551 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
553 int needed_headroom = dev->needed_headroom;
554 int needed_tailroom = dev->needed_tailroom;
556 /* For tail taggers, we need to pad short frames ourselves, to ensure
557 * that the tail tag does not fail at its role of being at the end of
558 * the packet, once the master interface pads the frame. Account for
559 * that pad length here, and pad later.
561 if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
562 needed_tailroom += ETH_ZLEN - skb->len;
563 /* skb_headroom() returns unsigned int... */
564 needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
565 needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
567 if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
568 /* No reallocation needed, yay! */
571 return pskb_expand_head(skb, needed_headroom, needed_tailroom,
575 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
577 struct dsa_slave_priv *p = netdev_priv(dev);
578 struct pcpu_sw_netstats *s;
579 struct sk_buff *nskb;
581 s = this_cpu_ptr(p->stats64);
582 u64_stats_update_begin(&s->syncp);
584 s->tx_bytes += skb->len;
585 u64_stats_update_end(&s->syncp);
587 DSA_SKB_CB(skb)->clone = NULL;
589 /* Identify PTP protocol packets, clone them, and pass them to the
592 dsa_skb_tx_timestamp(p, skb);
594 if (dsa_realloc_skb(skb, dev)) {
595 dev_kfree_skb_any(skb);
599 /* needed_tailroom should still be 'warm' in the cache line from
600 * dsa_realloc_skb(), which has also ensured that padding is safe.
602 if (dev->needed_tailroom)
605 /* Transmit function may have to reallocate the original SKB,
606 * in which case it must have freed it. Only free it here on error.
608 nskb = p->xmit(skb, dev);
614 return dsa_enqueue_skb(nskb, dev);
617 /* ethtool operations *******************************************************/
619 static void dsa_slave_get_drvinfo(struct net_device *dev,
620 struct ethtool_drvinfo *drvinfo)
622 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
623 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
624 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
627 static int dsa_slave_get_regs_len(struct net_device *dev)
629 struct dsa_port *dp = dsa_slave_to_port(dev);
630 struct dsa_switch *ds = dp->ds;
632 if (ds->ops->get_regs_len)
633 return ds->ops->get_regs_len(ds, dp->index);
639 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
641 struct dsa_port *dp = dsa_slave_to_port(dev);
642 struct dsa_switch *ds = dp->ds;
644 if (ds->ops->get_regs)
645 ds->ops->get_regs(ds, dp->index, regs, _p);
648 static int dsa_slave_nway_reset(struct net_device *dev)
650 struct dsa_port *dp = dsa_slave_to_port(dev);
652 return phylink_ethtool_nway_reset(dp->pl);
655 static int dsa_slave_get_eeprom_len(struct net_device *dev)
657 struct dsa_port *dp = dsa_slave_to_port(dev);
658 struct dsa_switch *ds = dp->ds;
660 if (ds->cd && ds->cd->eeprom_len)
661 return ds->cd->eeprom_len;
663 if (ds->ops->get_eeprom_len)
664 return ds->ops->get_eeprom_len(ds);
669 static int dsa_slave_get_eeprom(struct net_device *dev,
670 struct ethtool_eeprom *eeprom, u8 *data)
672 struct dsa_port *dp = dsa_slave_to_port(dev);
673 struct dsa_switch *ds = dp->ds;
675 if (ds->ops->get_eeprom)
676 return ds->ops->get_eeprom(ds, eeprom, data);
681 static int dsa_slave_set_eeprom(struct net_device *dev,
682 struct ethtool_eeprom *eeprom, u8 *data)
684 struct dsa_port *dp = dsa_slave_to_port(dev);
685 struct dsa_switch *ds = dp->ds;
687 if (ds->ops->set_eeprom)
688 return ds->ops->set_eeprom(ds, eeprom, data);
693 static void dsa_slave_get_strings(struct net_device *dev,
694 uint32_t stringset, uint8_t *data)
696 struct dsa_port *dp = dsa_slave_to_port(dev);
697 struct dsa_switch *ds = dp->ds;
699 if (stringset == ETH_SS_STATS) {
700 int len = ETH_GSTRING_LEN;
702 strncpy(data, "tx_packets", len);
703 strncpy(data + len, "tx_bytes", len);
704 strncpy(data + 2 * len, "rx_packets", len);
705 strncpy(data + 3 * len, "rx_bytes", len);
706 if (ds->ops->get_strings)
707 ds->ops->get_strings(ds, dp->index, stringset,
712 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
713 struct ethtool_stats *stats,
716 struct dsa_port *dp = dsa_slave_to_port(dev);
717 struct dsa_slave_priv *p = netdev_priv(dev);
718 struct dsa_switch *ds = dp->ds;
719 struct pcpu_sw_netstats *s;
723 for_each_possible_cpu(i) {
724 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
726 s = per_cpu_ptr(p->stats64, i);
728 start = u64_stats_fetch_begin_irq(&s->syncp);
729 tx_packets = s->tx_packets;
730 tx_bytes = s->tx_bytes;
731 rx_packets = s->rx_packets;
732 rx_bytes = s->rx_bytes;
733 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
734 data[0] += tx_packets;
736 data[2] += rx_packets;
739 if (ds->ops->get_ethtool_stats)
740 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
743 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
745 struct dsa_port *dp = dsa_slave_to_port(dev);
746 struct dsa_switch *ds = dp->ds;
748 if (sset == ETH_SS_STATS) {
751 if (ds->ops->get_sset_count) {
752 count = ds->ops->get_sset_count(ds, dp->index, sset);
763 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
765 struct dsa_port *dp = dsa_slave_to_port(dev);
766 struct dsa_switch *ds = dp->ds;
768 phylink_ethtool_get_wol(dp->pl, w);
770 if (ds->ops->get_wol)
771 ds->ops->get_wol(ds, dp->index, w);
774 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
776 struct dsa_port *dp = dsa_slave_to_port(dev);
777 struct dsa_switch *ds = dp->ds;
778 int ret = -EOPNOTSUPP;
780 phylink_ethtool_set_wol(dp->pl, w);
782 if (ds->ops->set_wol)
783 ret = ds->ops->set_wol(ds, dp->index, w);
788 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
790 struct dsa_port *dp = dsa_slave_to_port(dev);
791 struct dsa_switch *ds = dp->ds;
794 /* Port's PHY and MAC both need to be EEE capable */
795 if (!dev->phydev || !dp->pl)
798 if (!ds->ops->set_mac_eee)
801 ret = ds->ops->set_mac_eee(ds, dp->index, e);
805 return phylink_ethtool_set_eee(dp->pl, e);
808 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
810 struct dsa_port *dp = dsa_slave_to_port(dev);
811 struct dsa_switch *ds = dp->ds;
814 /* Port's PHY and MAC both need to be EEE capable */
815 if (!dev->phydev || !dp->pl)
818 if (!ds->ops->get_mac_eee)
821 ret = ds->ops->get_mac_eee(ds, dp->index, e);
825 return phylink_ethtool_get_eee(dp->pl, e);
828 static int dsa_slave_get_link_ksettings(struct net_device *dev,
829 struct ethtool_link_ksettings *cmd)
831 struct dsa_port *dp = dsa_slave_to_port(dev);
833 return phylink_ethtool_ksettings_get(dp->pl, cmd);
836 static int dsa_slave_set_link_ksettings(struct net_device *dev,
837 const struct ethtool_link_ksettings *cmd)
839 struct dsa_port *dp = dsa_slave_to_port(dev);
841 return phylink_ethtool_ksettings_set(dp->pl, cmd);
844 static void dsa_slave_get_pauseparam(struct net_device *dev,
845 struct ethtool_pauseparam *pause)
847 struct dsa_port *dp = dsa_slave_to_port(dev);
849 phylink_ethtool_get_pauseparam(dp->pl, pause);
852 static int dsa_slave_set_pauseparam(struct net_device *dev,
853 struct ethtool_pauseparam *pause)
855 struct dsa_port *dp = dsa_slave_to_port(dev);
857 return phylink_ethtool_set_pauseparam(dp->pl, pause);
860 #ifdef CONFIG_NET_POLL_CONTROLLER
861 static int dsa_slave_netpoll_setup(struct net_device *dev,
862 struct netpoll_info *ni)
864 struct net_device *master = dsa_slave_to_master(dev);
865 struct dsa_slave_priv *p = netdev_priv(dev);
866 struct netpoll *netpoll;
869 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
873 err = __netpoll_setup(netpoll, master);
879 p->netpoll = netpoll;
884 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
886 struct dsa_slave_priv *p = netdev_priv(dev);
887 struct netpoll *netpoll = p->netpoll;
894 __netpoll_free(netpoll);
897 static void dsa_slave_poll_controller(struct net_device *dev)
902 static int dsa_slave_get_phys_port_name(struct net_device *dev,
903 char *name, size_t len)
905 struct dsa_port *dp = dsa_slave_to_port(dev);
907 /* For non-legacy ports, devlink is used and it takes
908 * care of the name generation. This ndo implementation
909 * should be removed with legacy support.
914 if (snprintf(name, len, "p%d", dp->index) >= len)
920 static struct dsa_mall_tc_entry *
921 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
923 struct dsa_slave_priv *p = netdev_priv(dev);
924 struct dsa_mall_tc_entry *mall_tc_entry;
926 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
927 if (mall_tc_entry->cookie == cookie)
928 return mall_tc_entry;
934 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
935 struct tc_cls_matchall_offload *cls,
938 struct dsa_port *dp = dsa_slave_to_port(dev);
939 struct dsa_slave_priv *p = netdev_priv(dev);
940 struct dsa_mall_mirror_tc_entry *mirror;
941 struct dsa_mall_tc_entry *mall_tc_entry;
942 struct dsa_switch *ds = dp->ds;
943 struct flow_action_entry *act;
944 struct dsa_port *to_dp;
947 if (!ds->ops->port_mirror_add)
950 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
954 act = &cls->rule->action.entries[0];
959 if (!dsa_slave_dev_check(act->dev))
962 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
966 mall_tc_entry->cookie = cls->cookie;
967 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
968 mirror = &mall_tc_entry->mirror;
970 to_dp = dsa_slave_to_port(act->dev);
972 mirror->to_local_port = to_dp->index;
973 mirror->ingress = ingress;
975 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
977 kfree(mall_tc_entry);
981 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
987 dsa_slave_add_cls_matchall_police(struct net_device *dev,
988 struct tc_cls_matchall_offload *cls,
991 struct netlink_ext_ack *extack = cls->common.extack;
992 struct dsa_port *dp = dsa_slave_to_port(dev);
993 struct dsa_slave_priv *p = netdev_priv(dev);
994 struct dsa_mall_policer_tc_entry *policer;
995 struct dsa_mall_tc_entry *mall_tc_entry;
996 struct dsa_switch *ds = dp->ds;
997 struct flow_action_entry *act;
1000 if (!ds->ops->port_policer_add) {
1001 NL_SET_ERR_MSG_MOD(extack,
1002 "Policing offload not implemented");
1007 NL_SET_ERR_MSG_MOD(extack,
1008 "Only supported on ingress qdisc");
1012 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1013 cls->common.extack))
1016 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1017 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1018 NL_SET_ERR_MSG_MOD(extack,
1019 "Only one port policer allowed");
1024 act = &cls->rule->action.entries[0];
1026 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1030 mall_tc_entry->cookie = cls->cookie;
1031 mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1032 policer = &mall_tc_entry->policer;
1033 policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1034 policer->burst = act->police.burst;
1036 err = ds->ops->port_policer_add(ds, dp->index, policer);
1038 kfree(mall_tc_entry);
1042 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1047 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1048 struct tc_cls_matchall_offload *cls,
1051 int err = -EOPNOTSUPP;
1053 if (cls->common.protocol == htons(ETH_P_ALL) &&
1054 flow_offload_has_one_action(&cls->rule->action) &&
1055 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1056 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1057 else if (flow_offload_has_one_action(&cls->rule->action) &&
1058 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1059 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1064 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1065 struct tc_cls_matchall_offload *cls)
1067 struct dsa_port *dp = dsa_slave_to_port(dev);
1068 struct dsa_mall_tc_entry *mall_tc_entry;
1069 struct dsa_switch *ds = dp->ds;
1071 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1075 list_del(&mall_tc_entry->list);
1077 switch (mall_tc_entry->type) {
1078 case DSA_PORT_MALL_MIRROR:
1079 if (ds->ops->port_mirror_del)
1080 ds->ops->port_mirror_del(ds, dp->index,
1081 &mall_tc_entry->mirror);
1083 case DSA_PORT_MALL_POLICER:
1084 if (ds->ops->port_policer_del)
1085 ds->ops->port_policer_del(ds, dp->index);
1091 kfree(mall_tc_entry);
1094 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1095 struct tc_cls_matchall_offload *cls,
1098 if (cls->common.chain_index)
1101 switch (cls->command) {
1102 case TC_CLSMATCHALL_REPLACE:
1103 return dsa_slave_add_cls_matchall(dev, cls, ingress);
1104 case TC_CLSMATCHALL_DESTROY:
1105 dsa_slave_del_cls_matchall(dev, cls);
1112 static int dsa_slave_add_cls_flower(struct net_device *dev,
1113 struct flow_cls_offload *cls,
1116 struct dsa_port *dp = dsa_slave_to_port(dev);
1117 struct dsa_switch *ds = dp->ds;
1118 int port = dp->index;
1120 if (!ds->ops->cls_flower_add)
1123 return ds->ops->cls_flower_add(ds, port, cls, ingress);
1126 static int dsa_slave_del_cls_flower(struct net_device *dev,
1127 struct flow_cls_offload *cls,
1130 struct dsa_port *dp = dsa_slave_to_port(dev);
1131 struct dsa_switch *ds = dp->ds;
1132 int port = dp->index;
1134 if (!ds->ops->cls_flower_del)
1137 return ds->ops->cls_flower_del(ds, port, cls, ingress);
1140 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1141 struct flow_cls_offload *cls,
1144 struct dsa_port *dp = dsa_slave_to_port(dev);
1145 struct dsa_switch *ds = dp->ds;
1146 int port = dp->index;
1148 if (!ds->ops->cls_flower_stats)
1151 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1154 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1155 struct flow_cls_offload *cls,
1158 switch (cls->command) {
1159 case FLOW_CLS_REPLACE:
1160 return dsa_slave_add_cls_flower(dev, cls, ingress);
1161 case FLOW_CLS_DESTROY:
1162 return dsa_slave_del_cls_flower(dev, cls, ingress);
1163 case FLOW_CLS_STATS:
1164 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1170 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1171 void *cb_priv, bool ingress)
1173 struct net_device *dev = cb_priv;
1175 if (!tc_can_offload(dev))
1179 case TC_SETUP_CLSMATCHALL:
1180 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1181 case TC_SETUP_CLSFLOWER:
1182 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1188 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1189 void *type_data, void *cb_priv)
1191 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1194 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1195 void *type_data, void *cb_priv)
1197 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1200 static LIST_HEAD(dsa_slave_block_cb_list);
1202 static int dsa_slave_setup_tc_block(struct net_device *dev,
1203 struct flow_block_offload *f)
1205 struct flow_block_cb *block_cb;
1206 flow_setup_cb_t *cb;
1208 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1209 cb = dsa_slave_setup_tc_block_cb_ig;
1210 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1211 cb = dsa_slave_setup_tc_block_cb_eg;
1215 f->driver_block_list = &dsa_slave_block_cb_list;
1217 switch (f->command) {
1218 case FLOW_BLOCK_BIND:
1219 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1222 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1223 if (IS_ERR(block_cb))
1224 return PTR_ERR(block_cb);
1226 flow_block_cb_add(block_cb, f);
1227 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1229 case FLOW_BLOCK_UNBIND:
1230 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1234 flow_block_cb_remove(block_cb, f);
1235 list_del(&block_cb->driver_list);
1242 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1245 struct dsa_port *dp = dsa_slave_to_port(dev);
1246 struct dsa_switch *ds = dp->ds;
1248 if (type == TC_SETUP_BLOCK)
1249 return dsa_slave_setup_tc_block(dev, type_data);
1251 if (!ds->ops->port_setup_tc)
1254 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1257 static void dsa_slave_get_stats64(struct net_device *dev,
1258 struct rtnl_link_stats64 *stats)
1260 struct dsa_slave_priv *p = netdev_priv(dev);
1262 netdev_stats_to_stats64(stats, &dev->stats);
1263 dev_fetch_sw_netstats(stats, p->stats64);
1266 static int dsa_slave_get_rxnfc(struct net_device *dev,
1267 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1269 struct dsa_port *dp = dsa_slave_to_port(dev);
1270 struct dsa_switch *ds = dp->ds;
1272 if (!ds->ops->get_rxnfc)
1275 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1278 static int dsa_slave_set_rxnfc(struct net_device *dev,
1279 struct ethtool_rxnfc *nfc)
1281 struct dsa_port *dp = dsa_slave_to_port(dev);
1282 struct dsa_switch *ds = dp->ds;
1284 if (!ds->ops->set_rxnfc)
1287 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1290 static int dsa_slave_get_ts_info(struct net_device *dev,
1291 struct ethtool_ts_info *ts)
1293 struct dsa_slave_priv *p = netdev_priv(dev);
1294 struct dsa_switch *ds = p->dp->ds;
1296 if (!ds->ops->get_ts_info)
1299 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1302 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1305 struct net_device *master = dsa_slave_to_master(dev);
1306 struct dsa_port *dp = dsa_slave_to_port(dev);
1307 struct switchdev_obj_port_vlan vlan = {
1308 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1311 /* This API only allows programming tagged, non-PVID VIDs */
1314 struct switchdev_trans trans;
1318 trans.ph_prepare = true;
1319 ret = dsa_port_vlan_add(dp, &vlan, &trans);
1323 trans.ph_prepare = false;
1324 ret = dsa_port_vlan_add(dp, &vlan, &trans);
1328 /* And CPU port... */
1329 trans.ph_prepare = true;
1330 ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &trans);
1334 trans.ph_prepare = false;
1335 ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &trans);
1339 return vlan_vid_add(master, proto, vid);
1342 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1345 struct net_device *master = dsa_slave_to_master(dev);
1346 struct dsa_port *dp = dsa_slave_to_port(dev);
1347 struct switchdev_obj_port_vlan vlan = {
1350 /* This API only allows programming tagged, non-PVID VIDs */
1355 /* Do not deprogram the CPU port as it may be shared with other user
1356 * ports which can be members of this VLAN as well.
1358 err = dsa_port_vlan_del(dp, &vlan);
1362 vlan_vid_del(master, proto, vid);
1367 struct dsa_hw_port {
1368 struct list_head list;
1369 struct net_device *dev;
1373 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1375 const struct dsa_hw_port *p;
1378 list_for_each_entry(p, hw_port_list, list) {
1379 if (p->dev->mtu == mtu)
1382 err = dev_set_mtu(p->dev, mtu);
1390 list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1391 if (p->dev->mtu == p->old_mtu)
1394 if (dev_set_mtu(p->dev, p->old_mtu))
1395 netdev_err(p->dev, "Failed to restore MTU\n");
1401 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1403 struct dsa_hw_port *p, *n;
1405 list_for_each_entry_safe(p, n, hw_port_list, list)
1409 /* Make the hardware datapath to/from @dev limited to a common MTU */
1410 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1412 struct list_head hw_port_list;
1413 struct dsa_switch_tree *dst;
1414 int min_mtu = ETH_MAX_MTU;
1415 struct dsa_port *other_dp;
1418 if (!dp->ds->mtu_enforcement_ingress)
1421 if (!dp->bridge_dev)
1424 INIT_LIST_HEAD(&hw_port_list);
1426 /* Populate the list of ports that are part of the same bridge
1427 * as the newly added/modified port
1429 list_for_each_entry(dst, &dsa_tree_list, list) {
1430 list_for_each_entry(other_dp, &dst->ports, list) {
1431 struct dsa_hw_port *hw_port;
1432 struct net_device *slave;
1434 if (other_dp->type != DSA_PORT_TYPE_USER)
1437 if (other_dp->bridge_dev != dp->bridge_dev)
1440 if (!other_dp->ds->mtu_enforcement_ingress)
1443 slave = other_dp->slave;
1445 if (min_mtu > slave->mtu)
1446 min_mtu = slave->mtu;
1448 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1452 hw_port->dev = slave;
1453 hw_port->old_mtu = slave->mtu;
1455 list_add(&hw_port->list, &hw_port_list);
1459 /* Attempt to configure the entire hardware bridge to the newly added
1460 * interface's MTU first, regardless of whether the intention of the
1461 * user was to raise or lower it.
1463 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1467 /* Clearly that didn't work out so well, so just set the minimum MTU on
1468 * all hardware bridge ports now. If this fails too, then all ports will
1469 * still have their old MTU rolled back anyway.
1471 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1474 dsa_hw_port_list_free(&hw_port_list);
1477 static int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1479 struct net_device *master = dsa_slave_to_master(dev);
1480 struct dsa_port *dp = dsa_slave_to_port(dev);
1481 struct dsa_slave_priv *p = netdev_priv(dev);
1482 struct dsa_switch *ds = p->dp->ds;
1483 struct dsa_port *cpu_dp;
1484 int port = p->dp->index;
1485 int largest_mtu = 0;
1492 if (!ds->ops->port_change_mtu)
1495 for (i = 0; i < ds->num_ports; i++) {
1498 if (!dsa_is_user_port(ds, i))
1501 /* During probe, this function will be called for each slave
1502 * device, while not all of them have been allocated. That's
1503 * ok, it doesn't change what the maximum is, so ignore it.
1505 if (!dsa_to_port(ds, i)->slave)
1508 /* Pretend that we already applied the setting, which we
1509 * actually haven't (still haven't done all integrity checks)
1512 slave_mtu = new_mtu;
1514 slave_mtu = dsa_to_port(ds, i)->slave->mtu;
1516 if (largest_mtu < slave_mtu)
1517 largest_mtu = slave_mtu;
1520 cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1522 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1523 old_master_mtu = master->mtu;
1524 new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead;
1525 if (new_master_mtu > mtu_limit)
1528 /* If the master MTU isn't over limit, there's no need to check the CPU
1529 * MTU, since that surely isn't either.
1531 cpu_mtu = largest_mtu;
1533 /* Start applying stuff */
1534 if (new_master_mtu != old_master_mtu) {
1535 err = dev_set_mtu(master, new_master_mtu);
1537 goto out_master_failed;
1539 /* We only need to propagate the MTU of the CPU port to
1540 * upstream switches.
1542 err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true);
1544 goto out_cpu_failed;
1547 err = dsa_port_mtu_change(dp, new_mtu, false);
1549 goto out_port_failed;
1553 dsa_bridge_mtu_normalization(dp);
1558 if (new_master_mtu != old_master_mtu)
1559 dsa_port_mtu_change(cpu_dp, old_master_mtu -
1560 cpu_dp->tag_ops->overhead,
1563 if (new_master_mtu != old_master_mtu)
1564 dev_set_mtu(master, old_master_mtu);
1569 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1570 .get_drvinfo = dsa_slave_get_drvinfo,
1571 .get_regs_len = dsa_slave_get_regs_len,
1572 .get_regs = dsa_slave_get_regs,
1573 .nway_reset = dsa_slave_nway_reset,
1574 .get_link = ethtool_op_get_link,
1575 .get_eeprom_len = dsa_slave_get_eeprom_len,
1576 .get_eeprom = dsa_slave_get_eeprom,
1577 .set_eeprom = dsa_slave_set_eeprom,
1578 .get_strings = dsa_slave_get_strings,
1579 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1580 .get_sset_count = dsa_slave_get_sset_count,
1581 .set_wol = dsa_slave_set_wol,
1582 .get_wol = dsa_slave_get_wol,
1583 .set_eee = dsa_slave_set_eee,
1584 .get_eee = dsa_slave_get_eee,
1585 .get_link_ksettings = dsa_slave_get_link_ksettings,
1586 .set_link_ksettings = dsa_slave_set_link_ksettings,
1587 .get_pauseparam = dsa_slave_get_pauseparam,
1588 .set_pauseparam = dsa_slave_set_pauseparam,
1589 .get_rxnfc = dsa_slave_get_rxnfc,
1590 .set_rxnfc = dsa_slave_set_rxnfc,
1591 .get_ts_info = dsa_slave_get_ts_info,
1594 /* legacy way, bypassing the bridge *****************************************/
1595 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1596 struct net_device *dev,
1597 const unsigned char *addr, u16 vid,
1599 struct netlink_ext_ack *extack)
1601 struct dsa_port *dp = dsa_slave_to_port(dev);
1603 return dsa_port_fdb_add(dp, addr, vid);
1606 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1607 struct net_device *dev,
1608 const unsigned char *addr, u16 vid)
1610 struct dsa_port *dp = dsa_slave_to_port(dev);
1612 return dsa_port_fdb_del(dp, addr, vid);
1615 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1617 struct dsa_port *dp = dsa_slave_to_port(dev);
1619 return dp->ds->devlink ? &dp->devlink_port : NULL;
1622 static const struct net_device_ops dsa_slave_netdev_ops = {
1623 .ndo_open = dsa_slave_open,
1624 .ndo_stop = dsa_slave_close,
1625 .ndo_start_xmit = dsa_slave_xmit,
1626 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1627 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1628 .ndo_set_mac_address = dsa_slave_set_mac_address,
1629 .ndo_fdb_add = dsa_legacy_fdb_add,
1630 .ndo_fdb_del = dsa_legacy_fdb_del,
1631 .ndo_fdb_dump = dsa_slave_fdb_dump,
1632 .ndo_do_ioctl = dsa_slave_ioctl,
1633 .ndo_get_iflink = dsa_slave_get_iflink,
1634 #ifdef CONFIG_NET_POLL_CONTROLLER
1635 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1636 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1637 .ndo_poll_controller = dsa_slave_poll_controller,
1639 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1640 .ndo_setup_tc = dsa_slave_setup_tc,
1641 .ndo_get_stats64 = dsa_slave_get_stats64,
1642 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1643 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1644 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1645 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1646 .ndo_change_mtu = dsa_slave_change_mtu,
1649 static struct device_type dsa_type = {
1653 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1655 const struct dsa_port *dp = dsa_to_port(ds, port);
1658 phylink_mac_change(dp->pl, up);
1660 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1662 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1663 struct phylink_link_state *state)
1665 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1666 struct dsa_switch *ds = dp->ds;
1668 /* No need to check that this operation is valid, the callback would
1669 * not be called if it was not.
1671 ds->ops->phylink_fixed_state(ds, dp->index, state);
1674 /* slave device setup *******************************************************/
1675 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1677 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1678 struct dsa_switch *ds = dp->ds;
1680 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1681 if (!slave_dev->phydev) {
1682 netdev_err(slave_dev, "no phy at %d\n", addr);
1686 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1689 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1691 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1692 struct device_node *port_dn = dp->dn;
1693 struct dsa_switch *ds = dp->ds;
1694 phy_interface_t mode;
1698 ret = of_get_phy_mode(port_dn, &mode);
1700 mode = PHY_INTERFACE_MODE_NA;
1702 dp->pl_config.dev = &slave_dev->dev;
1703 dp->pl_config.type = PHYLINK_NETDEV;
1705 /* The get_fixed_state callback takes precedence over polling the
1706 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set
1707 * this if the switch provides such a callback.
1709 if (ds->ops->phylink_fixed_state) {
1710 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
1711 dp->pl_config.poll_fixed_state = true;
1714 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1715 &dsa_port_phylink_mac_ops);
1716 if (IS_ERR(dp->pl)) {
1717 netdev_err(slave_dev,
1718 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1719 return PTR_ERR(dp->pl);
1722 if (ds->ops->get_phy_flags)
1723 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1725 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1726 if (ret == -ENODEV && ds->slave_mii_bus) {
1727 /* We could not connect to a designated PHY or SFP, so try to
1728 * use the switch internal MDIO bus instead
1730 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1733 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
1735 phylink_destroy(dp->pl);
1741 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1742 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1743 struct netdev_queue *txq,
1746 lockdep_set_class(&txq->_xmit_lock,
1747 &dsa_slave_netdev_xmit_lock_key);
1750 int dsa_slave_suspend(struct net_device *slave_dev)
1752 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1754 if (!netif_running(slave_dev))
1757 netif_device_detach(slave_dev);
1760 phylink_stop(dp->pl);
1766 int dsa_slave_resume(struct net_device *slave_dev)
1768 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1770 if (!netif_running(slave_dev))
1773 netif_device_attach(slave_dev);
1776 phylink_start(dp->pl);
1782 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1784 struct net_device *master = dsa_slave_to_master(dev);
1785 struct dsa_port *dp = dsa_slave_to_port(dev);
1786 struct dsa_notifier_register_info rinfo = {
1787 .switch_number = dp->ds->index,
1788 .port_number = dp->index,
1793 call_dsa_notifiers(val, dev, &rinfo.info);
1796 int dsa_slave_create(struct dsa_port *port)
1798 const struct dsa_port *cpu_dp = port->cpu_dp;
1799 struct net_device *master = cpu_dp->master;
1800 struct dsa_switch *ds = port->ds;
1801 const char *name = port->name;
1802 struct net_device *slave_dev;
1803 struct dsa_slave_priv *p;
1806 if (!ds->num_tx_queues)
1807 ds->num_tx_queues = 1;
1809 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1810 NET_NAME_UNKNOWN, ether_setup,
1811 ds->num_tx_queues, 1);
1812 if (slave_dev == NULL)
1815 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1816 if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
1817 slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1818 slave_dev->hw_features |= NETIF_F_HW_TC;
1819 slave_dev->features |= NETIF_F_LLTX;
1820 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1821 if (!IS_ERR_OR_NULL(port->mac))
1822 ether_addr_copy(slave_dev->dev_addr, port->mac);
1824 eth_hw_addr_inherit(slave_dev, master);
1825 slave_dev->priv_flags |= IFF_NO_QUEUE;
1826 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1827 if (ds->ops->port_max_mtu)
1828 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1829 if (cpu_dp->tag_ops->tail_tag)
1830 slave_dev->needed_tailroom = cpu_dp->tag_ops->overhead;
1832 slave_dev->needed_headroom = cpu_dp->tag_ops->overhead;
1833 /* Try to save one extra realloc later in the TX path (in the master)
1834 * by also inheriting the master's needed headroom and tailroom.
1835 * The 8021q driver also does this.
1837 slave_dev->needed_headroom += master->needed_headroom;
1838 slave_dev->needed_tailroom += master->needed_tailroom;
1839 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1841 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1844 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1845 slave_dev->dev.of_node = port->dn;
1846 slave_dev->vlan_features = master->vlan_features;
1848 p = netdev_priv(slave_dev);
1849 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1851 free_netdev(slave_dev);
1855 ret = gro_cells_init(&p->gcells, slave_dev);
1860 INIT_LIST_HEAD(&p->mall_tc_list);
1861 p->xmit = cpu_dp->tag_ops->xmit;
1862 port->slave = slave_dev;
1865 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1867 if (ret && ret != -EOPNOTSUPP)
1868 dev_warn(ds->dev, "nonfatal error %d setting MTU on port %d\n",
1871 netif_carrier_off(slave_dev);
1873 ret = dsa_slave_phy_setup(slave_dev);
1875 netdev_err(slave_dev,
1876 "error %d setting up PHY for tree %d, switch %d, port %d\n",
1877 ret, ds->dst->index, ds->index, port->index);
1881 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1885 ret = register_netdevice(slave_dev);
1887 netdev_err(master, "error %d registering interface %s\n",
1888 ret, slave_dev->name);
1893 ret = netdev_upper_dev_link(master, slave_dev, NULL);
1898 goto out_unregister;
1903 unregister_netdev(slave_dev);
1906 phylink_disconnect_phy(p->dp->pl);
1908 phylink_destroy(p->dp->pl);
1910 gro_cells_destroy(&p->gcells);
1912 free_percpu(p->stats64);
1913 free_netdev(slave_dev);
1918 void dsa_slave_destroy(struct net_device *slave_dev)
1920 struct net_device *master = dsa_slave_to_master(slave_dev);
1921 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1922 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1924 netif_carrier_off(slave_dev);
1926 netdev_upper_dev_unlink(master, slave_dev);
1927 unregister_netdevice(slave_dev);
1928 phylink_disconnect_phy(dp->pl);
1931 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1932 phylink_destroy(dp->pl);
1933 gro_cells_destroy(&p->gcells);
1934 free_percpu(p->stats64);
1935 free_netdev(slave_dev);
1938 bool dsa_slave_dev_check(const struct net_device *dev)
1940 return dev->netdev_ops == &dsa_slave_netdev_ops;
1943 static int dsa_slave_changeupper(struct net_device *dev,
1944 struct netdev_notifier_changeupper_info *info)
1946 struct dsa_port *dp = dsa_slave_to_port(dev);
1947 int err = NOTIFY_DONE;
1949 if (netif_is_bridge_master(info->upper_dev)) {
1950 if (info->linking) {
1951 err = dsa_port_bridge_join(dp, info->upper_dev);
1953 dsa_bridge_mtu_normalization(dp);
1954 err = notifier_from_errno(err);
1956 dsa_port_bridge_leave(dp, info->upper_dev);
1965 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
1966 struct netdev_notifier_changeupper_info *info)
1968 struct netlink_ext_ack *ext_ack;
1969 struct net_device *slave;
1970 struct dsa_port *dp;
1972 ext_ack = netdev_notifier_info_to_extack(&info->info);
1974 if (!is_vlan_dev(dev))
1977 slave = vlan_dev_real_dev(dev);
1978 if (!dsa_slave_dev_check(slave))
1981 dp = dsa_slave_to_port(slave);
1982 if (!dp->bridge_dev)
1985 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1986 if (br_vlan_enabled(dp->bridge_dev) &&
1987 netif_is_bridge_master(info->upper_dev) && info->linking) {
1988 NL_SET_ERR_MSG_MOD(ext_ack,
1989 "Cannot enslave VLAN device into VLAN aware bridge");
1990 return notifier_from_errno(-EINVAL);
1997 dsa_slave_check_8021q_upper(struct net_device *dev,
1998 struct netdev_notifier_changeupper_info *info)
2000 struct dsa_port *dp = dsa_slave_to_port(dev);
2001 struct net_device *br = dp->bridge_dev;
2002 struct bridge_vlan_info br_info;
2003 struct netlink_ext_ack *extack;
2004 int err = NOTIFY_DONE;
2007 if (!br || !br_vlan_enabled(br))
2010 extack = netdev_notifier_info_to_extack(&info->info);
2011 vid = vlan_dev_vlan_id(info->upper_dev);
2013 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2014 * device, respectively the VID is not found, returning
2015 * 0 means success, which is a failure for us here.
2017 err = br_vlan_get_info(br, vid, &br_info);
2019 NL_SET_ERR_MSG_MOD(extack,
2020 "This VLAN is already configured by the bridge");
2021 return notifier_from_errno(-EBUSY);
2027 static int dsa_slave_netdevice_event(struct notifier_block *nb,
2028 unsigned long event, void *ptr)
2030 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2033 case NETDEV_PRECHANGEUPPER: {
2034 struct netdev_notifier_changeupper_info *info = ptr;
2036 if (!dsa_slave_dev_check(dev))
2037 return dsa_prevent_bridging_8021q_upper(dev, ptr);
2039 if (is_vlan_dev(info->upper_dev))
2040 return dsa_slave_check_8021q_upper(dev, ptr);
2043 case NETDEV_CHANGEUPPER:
2044 if (!dsa_slave_dev_check(dev))
2047 return dsa_slave_changeupper(dev, ptr);
2053 struct dsa_switchdev_event_work {
2054 struct work_struct work;
2055 struct switchdev_notifier_fdb_info fdb_info;
2056 struct net_device *dev;
2057 unsigned long event;
2060 static void dsa_slave_switchdev_event_work(struct work_struct *work)
2062 struct dsa_switchdev_event_work *switchdev_work =
2063 container_of(work, struct dsa_switchdev_event_work, work);
2064 struct net_device *dev = switchdev_work->dev;
2065 struct switchdev_notifier_fdb_info *fdb_info;
2066 struct dsa_port *dp = dsa_slave_to_port(dev);
2070 switch (switchdev_work->event) {
2071 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2072 fdb_info = &switchdev_work->fdb_info;
2073 if (!fdb_info->added_by_user)
2076 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
2078 netdev_dbg(dev, "fdb add failed err=%d\n", err);
2081 fdb_info->offloaded = true;
2082 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
2083 &fdb_info->info, NULL);
2086 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2087 fdb_info = &switchdev_work->fdb_info;
2088 if (!fdb_info->added_by_user)
2091 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
2093 netdev_dbg(dev, "fdb del failed err=%d\n", err);
2100 kfree(switchdev_work->fdb_info.addr);
2101 kfree(switchdev_work);
2106 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
2108 const struct switchdev_notifier_fdb_info *
2111 memcpy(&switchdev_work->fdb_info, fdb_info,
2112 sizeof(switchdev_work->fdb_info));
2113 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2114 if (!switchdev_work->fdb_info.addr)
2116 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2121 /* Called under rcu_read_lock() */
2122 static int dsa_slave_switchdev_event(struct notifier_block *unused,
2123 unsigned long event, void *ptr)
2125 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2126 struct dsa_switchdev_event_work *switchdev_work;
2129 if (event == SWITCHDEV_PORT_ATTR_SET) {
2130 err = switchdev_handle_port_attr_set(dev, ptr,
2131 dsa_slave_dev_check,
2132 dsa_slave_port_attr_set);
2133 return notifier_from_errno(err);
2136 if (!dsa_slave_dev_check(dev))
2139 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2140 if (!switchdev_work)
2143 INIT_WORK(&switchdev_work->work,
2144 dsa_slave_switchdev_event_work);
2145 switchdev_work->dev = dev;
2146 switchdev_work->event = event;
2149 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2150 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2151 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
2152 goto err_fdb_work_init;
2156 kfree(switchdev_work);
2160 dsa_schedule_work(&switchdev_work->work);
2164 kfree(switchdev_work);
2168 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2169 unsigned long event, void *ptr)
2171 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2175 case SWITCHDEV_PORT_OBJ_ADD:
2176 err = switchdev_handle_port_obj_add(dev, ptr,
2177 dsa_slave_dev_check,
2178 dsa_slave_port_obj_add);
2179 return notifier_from_errno(err);
2180 case SWITCHDEV_PORT_OBJ_DEL:
2181 err = switchdev_handle_port_obj_del(dev, ptr,
2182 dsa_slave_dev_check,
2183 dsa_slave_port_obj_del);
2184 return notifier_from_errno(err);
2185 case SWITCHDEV_PORT_ATTR_SET:
2186 err = switchdev_handle_port_attr_set(dev, ptr,
2187 dsa_slave_dev_check,
2188 dsa_slave_port_attr_set);
2189 return notifier_from_errno(err);
2195 static struct notifier_block dsa_slave_nb __read_mostly = {
2196 .notifier_call = dsa_slave_netdevice_event,
2199 static struct notifier_block dsa_slave_switchdev_notifier = {
2200 .notifier_call = dsa_slave_switchdev_event,
2203 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
2204 .notifier_call = dsa_slave_switchdev_blocking_event,
2207 int dsa_slave_register_notifier(void)
2209 struct notifier_block *nb;
2212 err = register_netdevice_notifier(&dsa_slave_nb);
2216 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2218 goto err_switchdev_nb;
2220 nb = &dsa_slave_switchdev_blocking_notifier;
2221 err = register_switchdev_blocking_notifier(nb);
2223 goto err_switchdev_blocking_nb;
2227 err_switchdev_blocking_nb:
2228 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2230 unregister_netdevice_notifier(&dsa_slave_nb);
2234 void dsa_slave_unregister_notifier(void)
2236 struct notifier_block *nb;
2239 nb = &dsa_slave_switchdev_blocking_notifier;
2240 err = unregister_switchdev_blocking_notifier(nb);
2242 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
2244 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2246 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2248 err = unregister_netdevice_notifier(&dsa_slave_nb);
2250 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);