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/selftests.h>
19 #include <net/tc_act/tc_mirred.h>
20 #include <linux/if_bridge.h>
21 #include <linux/if_hsr.h>
22 #include <linux/netpoll.h>
26 /* slave mii_bus handling ***************************************************/
27 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
29 struct dsa_switch *ds = bus->priv;
31 if (ds->phys_mii_mask & (1 << addr))
32 return ds->ops->phy_read(ds, addr, reg);
37 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
39 struct dsa_switch *ds = bus->priv;
41 if (ds->phys_mii_mask & (1 << addr))
42 return ds->ops->phy_write(ds, addr, reg, val);
47 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
49 ds->slave_mii_bus->priv = (void *)ds;
50 ds->slave_mii_bus->name = "dsa slave smi";
51 ds->slave_mii_bus->read = dsa_slave_phy_read;
52 ds->slave_mii_bus->write = dsa_slave_phy_write;
53 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
54 ds->dst->index, ds->index);
55 ds->slave_mii_bus->parent = ds->dev;
56 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
60 /* slave device handling ****************************************************/
61 static int dsa_slave_get_iflink(const struct net_device *dev)
63 return dsa_slave_to_master(dev)->ifindex;
66 static int dsa_slave_open(struct net_device *dev)
68 struct net_device *master = dsa_slave_to_master(dev);
69 struct dsa_port *dp = dsa_slave_to_port(dev);
72 err = dev_open(master, NULL);
74 netdev_err(dev, "failed to open master %s\n", master->name);
78 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
79 err = dev_uc_add(master, dev->dev_addr);
84 if (dev->flags & IFF_ALLMULTI) {
85 err = dev_set_allmulti(master, 1);
89 if (dev->flags & IFF_PROMISC) {
90 err = dev_set_promiscuity(master, 1);
95 err = dsa_port_enable_rt(dp, dev->phydev);
102 if (dev->flags & IFF_PROMISC)
103 dev_set_promiscuity(master, -1);
105 if (dev->flags & IFF_ALLMULTI)
106 dev_set_allmulti(master, -1);
108 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
109 dev_uc_del(master, dev->dev_addr);
114 static int dsa_slave_close(struct net_device *dev)
116 struct net_device *master = dsa_slave_to_master(dev);
117 struct dsa_port *dp = dsa_slave_to_port(dev);
119 dsa_port_disable_rt(dp);
121 dev_mc_unsync(master, dev);
122 dev_uc_unsync(master, dev);
123 if (dev->flags & IFF_ALLMULTI)
124 dev_set_allmulti(master, -1);
125 if (dev->flags & IFF_PROMISC)
126 dev_set_promiscuity(master, -1);
128 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
129 dev_uc_del(master, dev->dev_addr);
134 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
136 struct net_device *master = dsa_slave_to_master(dev);
137 if (dev->flags & IFF_UP) {
138 if (change & IFF_ALLMULTI)
139 dev_set_allmulti(master,
140 dev->flags & IFF_ALLMULTI ? 1 : -1);
141 if (change & IFF_PROMISC)
142 dev_set_promiscuity(master,
143 dev->flags & IFF_PROMISC ? 1 : -1);
147 static void dsa_slave_set_rx_mode(struct net_device *dev)
149 struct net_device *master = dsa_slave_to_master(dev);
151 dev_mc_sync(master, dev);
152 dev_uc_sync(master, dev);
155 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
157 struct net_device *master = dsa_slave_to_master(dev);
158 struct sockaddr *addr = a;
161 if (!is_valid_ether_addr(addr->sa_data))
162 return -EADDRNOTAVAIL;
164 if (!(dev->flags & IFF_UP))
167 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
168 err = dev_uc_add(master, addr->sa_data);
173 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
174 dev_uc_del(master, dev->dev_addr);
177 eth_hw_addr_set(dev, addr->sa_data);
182 struct dsa_slave_dump_ctx {
183 struct net_device *dev;
185 struct netlink_callback *cb;
190 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
191 bool is_static, void *data)
193 struct dsa_slave_dump_ctx *dump = data;
194 u32 portid = NETLINK_CB(dump->cb->skb).portid;
195 u32 seq = dump->cb->nlh->nlmsg_seq;
196 struct nlmsghdr *nlh;
199 if (dump->idx < dump->cb->args[2])
202 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
203 sizeof(*ndm), NLM_F_MULTI);
207 ndm = nlmsg_data(nlh);
208 ndm->ndm_family = AF_BRIDGE;
211 ndm->ndm_flags = NTF_SELF;
213 ndm->ndm_ifindex = dump->dev->ifindex;
214 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
216 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
217 goto nla_put_failure;
219 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
220 goto nla_put_failure;
222 nlmsg_end(dump->skb, nlh);
229 nlmsg_cancel(dump->skb, nlh);
234 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
235 struct net_device *dev, struct net_device *filter_dev,
238 struct dsa_port *dp = dsa_slave_to_port(dev);
239 struct dsa_slave_dump_ctx dump = {
247 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
253 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
255 struct dsa_slave_priv *p = netdev_priv(dev);
256 struct dsa_switch *ds = p->dp->ds;
257 int port = p->dp->index;
259 /* Pass through to switch driver if it supports timestamping */
262 if (ds->ops->port_hwtstamp_get)
263 return ds->ops->port_hwtstamp_get(ds, port, ifr);
266 if (ds->ops->port_hwtstamp_set)
267 return ds->ops->port_hwtstamp_set(ds, port, ifr);
271 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
274 static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
275 const struct switchdev_attr *attr,
276 struct netlink_ext_ack *extack)
278 struct dsa_port *dp = dsa_slave_to_port(dev);
281 if (ctx && ctx != dp)
285 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
286 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
289 ret = dsa_port_set_state(dp, attr->u.stp_state, true);
291 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
292 if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
295 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
298 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
299 if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
302 ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
304 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
305 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
308 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
311 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
312 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
315 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
325 /* Must be called under rcu_read_lock() */
327 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
328 const struct switchdev_obj_port_vlan *vlan)
330 struct net_device *upper_dev;
331 struct list_head *iter;
333 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
336 if (!is_vlan_dev(upper_dev))
339 vid = vlan_dev_vlan_id(upper_dev);
340 if (vid == vlan->vid)
347 static int dsa_slave_vlan_add(struct net_device *dev,
348 const struct switchdev_obj *obj,
349 struct netlink_ext_ack *extack)
351 struct net_device *master = dsa_slave_to_master(dev);
352 struct dsa_port *dp = dsa_slave_to_port(dev);
353 struct switchdev_obj_port_vlan vlan;
356 if (dsa_port_skip_vlan_configuration(dp)) {
357 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
361 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
363 /* Deny adding a bridge VLAN when there is already an 802.1Q upper with
366 if (br_vlan_enabled(dp->bridge_dev)) {
368 err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
371 NL_SET_ERR_MSG_MOD(extack,
372 "Port already has a VLAN upper with this VID");
377 err = dsa_port_vlan_add(dp, &vlan, extack);
381 /* We need the dedicated CPU port to be a member of the VLAN as well.
382 * Even though drivers often handle CPU membership in special ways,
383 * it doesn't make sense to program a PVID, so clear this flag.
385 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
387 err = dsa_port_vlan_add(dp->cpu_dp, &vlan, extack);
391 return vlan_vid_add(master, htons(ETH_P_8021Q), vlan.vid);
394 static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
395 const struct switchdev_obj *obj,
396 struct netlink_ext_ack *extack)
398 struct dsa_port *dp = dsa_slave_to_port(dev);
401 if (ctx && ctx != dp)
405 case SWITCHDEV_OBJ_ID_PORT_MDB:
406 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
409 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
411 case SWITCHDEV_OBJ_ID_HOST_MDB:
412 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
415 err = dsa_port_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
417 case SWITCHDEV_OBJ_ID_PORT_VLAN:
418 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
421 err = dsa_slave_vlan_add(dev, obj, extack);
423 case SWITCHDEV_OBJ_ID_MRP:
424 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
427 err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
429 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
430 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
433 err = dsa_port_mrp_add_ring_role(dp,
434 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
444 static int dsa_slave_vlan_del(struct net_device *dev,
445 const struct switchdev_obj *obj)
447 struct net_device *master = dsa_slave_to_master(dev);
448 struct dsa_port *dp = dsa_slave_to_port(dev);
449 struct switchdev_obj_port_vlan *vlan;
452 if (dsa_port_skip_vlan_configuration(dp))
455 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
457 /* Do not deprogram the CPU port as it may be shared with other user
458 * ports which can be members of this VLAN as well.
460 err = dsa_port_vlan_del(dp, vlan);
464 vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid);
469 static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
470 const struct switchdev_obj *obj)
472 struct dsa_port *dp = dsa_slave_to_port(dev);
475 if (ctx && ctx != dp)
479 case SWITCHDEV_OBJ_ID_PORT_MDB:
480 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
483 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
485 case SWITCHDEV_OBJ_ID_HOST_MDB:
486 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
489 err = dsa_port_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
491 case SWITCHDEV_OBJ_ID_PORT_VLAN:
492 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
495 err = dsa_slave_vlan_del(dev, obj);
497 case SWITCHDEV_OBJ_ID_MRP:
498 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
501 err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
503 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
504 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
507 err = dsa_port_mrp_del_ring_role(dp,
508 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
518 static int dsa_slave_get_port_parent_id(struct net_device *dev,
519 struct netdev_phys_item_id *ppid)
521 struct dsa_port *dp = dsa_slave_to_port(dev);
522 struct dsa_switch *ds = dp->ds;
523 struct dsa_switch_tree *dst = ds->dst;
525 /* For non-legacy ports, devlink is used and it takes
526 * care of the name generation. This ndo implementation
527 * should be removed with legacy support.
532 ppid->id_len = sizeof(dst->index);
533 memcpy(&ppid->id, &dst->index, ppid->id_len);
538 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
541 #ifdef CONFIG_NET_POLL_CONTROLLER
542 struct dsa_slave_priv *p = netdev_priv(dev);
544 return netpoll_send_skb(p->netpoll, skb);
551 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
554 struct dsa_switch *ds = p->dp->ds;
556 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
559 if (!ds->ops->port_txtstamp)
562 ds->ops->port_txtstamp(ds, p->dp->index, skb);
565 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
567 /* SKB for netpoll still need to be mangled with the protocol-specific
568 * tag to be successfully transmitted
570 if (unlikely(netpoll_tx_running(dev)))
571 return dsa_slave_netpoll_send_skb(dev, skb);
573 /* Queue the SKB for transmission on the parent interface, but
574 * do not modify its EtherType
576 skb->dev = dsa_slave_to_master(dev);
581 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
583 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
585 int needed_headroom = dev->needed_headroom;
586 int needed_tailroom = dev->needed_tailroom;
588 /* For tail taggers, we need to pad short frames ourselves, to ensure
589 * that the tail tag does not fail at its role of being at the end of
590 * the packet, once the master interface pads the frame. Account for
591 * that pad length here, and pad later.
593 if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
594 needed_tailroom += ETH_ZLEN - skb->len;
595 /* skb_headroom() returns unsigned int... */
596 needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
597 needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
599 if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
600 /* No reallocation needed, yay! */
603 return pskb_expand_head(skb, needed_headroom, needed_tailroom,
607 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
609 struct dsa_slave_priv *p = netdev_priv(dev);
610 struct sk_buff *nskb;
612 dev_sw_netstats_tx_add(dev, 1, skb->len);
614 memset(skb->cb, 0, sizeof(skb->cb));
616 /* Handle tx timestamp if any */
617 dsa_skb_tx_timestamp(p, skb);
619 if (dsa_realloc_skb(skb, dev)) {
620 dev_kfree_skb_any(skb);
624 /* needed_tailroom should still be 'warm' in the cache line from
625 * dsa_realloc_skb(), which has also ensured that padding is safe.
627 if (dev->needed_tailroom)
630 /* Transmit function may have to reallocate the original SKB,
631 * in which case it must have freed it. Only free it here on error.
633 nskb = p->xmit(skb, dev);
639 return dsa_enqueue_skb(nskb, dev);
642 /* ethtool operations *******************************************************/
644 static void dsa_slave_get_drvinfo(struct net_device *dev,
645 struct ethtool_drvinfo *drvinfo)
647 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
648 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
649 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
652 static int dsa_slave_get_regs_len(struct net_device *dev)
654 struct dsa_port *dp = dsa_slave_to_port(dev);
655 struct dsa_switch *ds = dp->ds;
657 if (ds->ops->get_regs_len)
658 return ds->ops->get_regs_len(ds, dp->index);
664 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
666 struct dsa_port *dp = dsa_slave_to_port(dev);
667 struct dsa_switch *ds = dp->ds;
669 if (ds->ops->get_regs)
670 ds->ops->get_regs(ds, dp->index, regs, _p);
673 static int dsa_slave_nway_reset(struct net_device *dev)
675 struct dsa_port *dp = dsa_slave_to_port(dev);
677 return phylink_ethtool_nway_reset(dp->pl);
680 static int dsa_slave_get_eeprom_len(struct net_device *dev)
682 struct dsa_port *dp = dsa_slave_to_port(dev);
683 struct dsa_switch *ds = dp->ds;
685 if (ds->cd && ds->cd->eeprom_len)
686 return ds->cd->eeprom_len;
688 if (ds->ops->get_eeprom_len)
689 return ds->ops->get_eeprom_len(ds);
694 static int dsa_slave_get_eeprom(struct net_device *dev,
695 struct ethtool_eeprom *eeprom, u8 *data)
697 struct dsa_port *dp = dsa_slave_to_port(dev);
698 struct dsa_switch *ds = dp->ds;
700 if (ds->ops->get_eeprom)
701 return ds->ops->get_eeprom(ds, eeprom, data);
706 static int dsa_slave_set_eeprom(struct net_device *dev,
707 struct ethtool_eeprom *eeprom, u8 *data)
709 struct dsa_port *dp = dsa_slave_to_port(dev);
710 struct dsa_switch *ds = dp->ds;
712 if (ds->ops->set_eeprom)
713 return ds->ops->set_eeprom(ds, eeprom, data);
718 static void dsa_slave_get_strings(struct net_device *dev,
719 uint32_t stringset, uint8_t *data)
721 struct dsa_port *dp = dsa_slave_to_port(dev);
722 struct dsa_switch *ds = dp->ds;
724 if (stringset == ETH_SS_STATS) {
725 int len = ETH_GSTRING_LEN;
727 strncpy(data, "tx_packets", len);
728 strncpy(data + len, "tx_bytes", len);
729 strncpy(data + 2 * len, "rx_packets", len);
730 strncpy(data + 3 * len, "rx_bytes", len);
731 if (ds->ops->get_strings)
732 ds->ops->get_strings(ds, dp->index, stringset,
734 } else if (stringset == ETH_SS_TEST) {
735 net_selftest_get_strings(data);
740 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
741 struct ethtool_stats *stats,
744 struct dsa_port *dp = dsa_slave_to_port(dev);
745 struct dsa_switch *ds = dp->ds;
746 struct pcpu_sw_netstats *s;
750 for_each_possible_cpu(i) {
751 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
753 s = per_cpu_ptr(dev->tstats, i);
755 start = u64_stats_fetch_begin_irq(&s->syncp);
756 tx_packets = s->tx_packets;
757 tx_bytes = s->tx_bytes;
758 rx_packets = s->rx_packets;
759 rx_bytes = s->rx_bytes;
760 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
761 data[0] += tx_packets;
763 data[2] += rx_packets;
766 if (ds->ops->get_ethtool_stats)
767 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
770 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
772 struct dsa_port *dp = dsa_slave_to_port(dev);
773 struct dsa_switch *ds = dp->ds;
775 if (sset == ETH_SS_STATS) {
778 if (ds->ops->get_sset_count) {
779 count = ds->ops->get_sset_count(ds, dp->index, sset);
785 } else if (sset == ETH_SS_TEST) {
786 return net_selftest_get_count();
792 static void dsa_slave_get_eth_phy_stats(struct net_device *dev,
793 struct ethtool_eth_phy_stats *phy_stats)
795 struct dsa_port *dp = dsa_slave_to_port(dev);
796 struct dsa_switch *ds = dp->ds;
798 if (ds->ops->get_eth_phy_stats)
799 ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
802 static void dsa_slave_get_eth_mac_stats(struct net_device *dev,
803 struct ethtool_eth_mac_stats *mac_stats)
805 struct dsa_port *dp = dsa_slave_to_port(dev);
806 struct dsa_switch *ds = dp->ds;
808 if (ds->ops->get_eth_mac_stats)
809 ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
813 dsa_slave_get_eth_ctrl_stats(struct net_device *dev,
814 struct ethtool_eth_ctrl_stats *ctrl_stats)
816 struct dsa_port *dp = dsa_slave_to_port(dev);
817 struct dsa_switch *ds = dp->ds;
819 if (ds->ops->get_eth_ctrl_stats)
820 ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
823 static void dsa_slave_net_selftest(struct net_device *ndev,
824 struct ethtool_test *etest, u64 *buf)
826 struct dsa_port *dp = dsa_slave_to_port(ndev);
827 struct dsa_switch *ds = dp->ds;
829 if (ds->ops->self_test) {
830 ds->ops->self_test(ds, dp->index, etest, buf);
834 net_selftest(ndev, etest, buf);
837 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
839 struct dsa_port *dp = dsa_slave_to_port(dev);
840 struct dsa_switch *ds = dp->ds;
842 phylink_ethtool_get_wol(dp->pl, w);
844 if (ds->ops->get_wol)
845 ds->ops->get_wol(ds, dp->index, w);
848 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
850 struct dsa_port *dp = dsa_slave_to_port(dev);
851 struct dsa_switch *ds = dp->ds;
852 int ret = -EOPNOTSUPP;
854 phylink_ethtool_set_wol(dp->pl, w);
856 if (ds->ops->set_wol)
857 ret = ds->ops->set_wol(ds, dp->index, w);
862 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
864 struct dsa_port *dp = dsa_slave_to_port(dev);
865 struct dsa_switch *ds = dp->ds;
868 /* Port's PHY and MAC both need to be EEE capable */
869 if (!dev->phydev || !dp->pl)
872 if (!ds->ops->set_mac_eee)
875 ret = ds->ops->set_mac_eee(ds, dp->index, e);
879 return phylink_ethtool_set_eee(dp->pl, e);
882 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
884 struct dsa_port *dp = dsa_slave_to_port(dev);
885 struct dsa_switch *ds = dp->ds;
888 /* Port's PHY and MAC both need to be EEE capable */
889 if (!dev->phydev || !dp->pl)
892 if (!ds->ops->get_mac_eee)
895 ret = ds->ops->get_mac_eee(ds, dp->index, e);
899 return phylink_ethtool_get_eee(dp->pl, e);
902 static int dsa_slave_get_link_ksettings(struct net_device *dev,
903 struct ethtool_link_ksettings *cmd)
905 struct dsa_port *dp = dsa_slave_to_port(dev);
907 return phylink_ethtool_ksettings_get(dp->pl, cmd);
910 static int dsa_slave_set_link_ksettings(struct net_device *dev,
911 const struct ethtool_link_ksettings *cmd)
913 struct dsa_port *dp = dsa_slave_to_port(dev);
915 return phylink_ethtool_ksettings_set(dp->pl, cmd);
918 static void dsa_slave_get_pauseparam(struct net_device *dev,
919 struct ethtool_pauseparam *pause)
921 struct dsa_port *dp = dsa_slave_to_port(dev);
923 phylink_ethtool_get_pauseparam(dp->pl, pause);
926 static int dsa_slave_set_pauseparam(struct net_device *dev,
927 struct ethtool_pauseparam *pause)
929 struct dsa_port *dp = dsa_slave_to_port(dev);
931 return phylink_ethtool_set_pauseparam(dp->pl, pause);
934 #ifdef CONFIG_NET_POLL_CONTROLLER
935 static int dsa_slave_netpoll_setup(struct net_device *dev,
936 struct netpoll_info *ni)
938 struct net_device *master = dsa_slave_to_master(dev);
939 struct dsa_slave_priv *p = netdev_priv(dev);
940 struct netpoll *netpoll;
943 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
947 err = __netpoll_setup(netpoll, master);
953 p->netpoll = netpoll;
958 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
960 struct dsa_slave_priv *p = netdev_priv(dev);
961 struct netpoll *netpoll = p->netpoll;
968 __netpoll_free(netpoll);
971 static void dsa_slave_poll_controller(struct net_device *dev)
976 static int dsa_slave_get_phys_port_name(struct net_device *dev,
977 char *name, size_t len)
979 struct dsa_port *dp = dsa_slave_to_port(dev);
981 /* For non-legacy ports, devlink is used and it takes
982 * care of the name generation. This ndo implementation
983 * should be removed with legacy support.
988 if (snprintf(name, len, "p%d", dp->index) >= len)
994 static struct dsa_mall_tc_entry *
995 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
997 struct dsa_slave_priv *p = netdev_priv(dev);
998 struct dsa_mall_tc_entry *mall_tc_entry;
1000 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1001 if (mall_tc_entry->cookie == cookie)
1002 return mall_tc_entry;
1008 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
1009 struct tc_cls_matchall_offload *cls,
1012 struct dsa_port *dp = dsa_slave_to_port(dev);
1013 struct dsa_slave_priv *p = netdev_priv(dev);
1014 struct dsa_mall_mirror_tc_entry *mirror;
1015 struct dsa_mall_tc_entry *mall_tc_entry;
1016 struct dsa_switch *ds = dp->ds;
1017 struct flow_action_entry *act;
1018 struct dsa_port *to_dp;
1021 if (!ds->ops->port_mirror_add)
1024 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1025 cls->common.extack))
1028 act = &cls->rule->action.entries[0];
1033 if (!dsa_slave_dev_check(act->dev))
1036 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1040 mall_tc_entry->cookie = cls->cookie;
1041 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1042 mirror = &mall_tc_entry->mirror;
1044 to_dp = dsa_slave_to_port(act->dev);
1046 mirror->to_local_port = to_dp->index;
1047 mirror->ingress = ingress;
1049 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
1051 kfree(mall_tc_entry);
1055 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1061 dsa_slave_add_cls_matchall_police(struct net_device *dev,
1062 struct tc_cls_matchall_offload *cls,
1065 struct netlink_ext_ack *extack = cls->common.extack;
1066 struct dsa_port *dp = dsa_slave_to_port(dev);
1067 struct dsa_slave_priv *p = netdev_priv(dev);
1068 struct dsa_mall_policer_tc_entry *policer;
1069 struct dsa_mall_tc_entry *mall_tc_entry;
1070 struct dsa_switch *ds = dp->ds;
1071 struct flow_action_entry *act;
1074 if (!ds->ops->port_policer_add) {
1075 NL_SET_ERR_MSG_MOD(extack,
1076 "Policing offload not implemented");
1081 NL_SET_ERR_MSG_MOD(extack,
1082 "Only supported on ingress qdisc");
1086 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1087 cls->common.extack))
1090 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1091 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1092 NL_SET_ERR_MSG_MOD(extack,
1093 "Only one port policer allowed");
1098 act = &cls->rule->action.entries[0];
1100 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1104 mall_tc_entry->cookie = cls->cookie;
1105 mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1106 policer = &mall_tc_entry->policer;
1107 policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1108 policer->burst = act->police.burst;
1110 err = ds->ops->port_policer_add(ds, dp->index, policer);
1112 kfree(mall_tc_entry);
1116 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1121 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1122 struct tc_cls_matchall_offload *cls,
1125 int err = -EOPNOTSUPP;
1127 if (cls->common.protocol == htons(ETH_P_ALL) &&
1128 flow_offload_has_one_action(&cls->rule->action) &&
1129 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1130 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1131 else if (flow_offload_has_one_action(&cls->rule->action) &&
1132 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1133 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1138 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1139 struct tc_cls_matchall_offload *cls)
1141 struct dsa_port *dp = dsa_slave_to_port(dev);
1142 struct dsa_mall_tc_entry *mall_tc_entry;
1143 struct dsa_switch *ds = dp->ds;
1145 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1149 list_del(&mall_tc_entry->list);
1151 switch (mall_tc_entry->type) {
1152 case DSA_PORT_MALL_MIRROR:
1153 if (ds->ops->port_mirror_del)
1154 ds->ops->port_mirror_del(ds, dp->index,
1155 &mall_tc_entry->mirror);
1157 case DSA_PORT_MALL_POLICER:
1158 if (ds->ops->port_policer_del)
1159 ds->ops->port_policer_del(ds, dp->index);
1165 kfree(mall_tc_entry);
1168 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1169 struct tc_cls_matchall_offload *cls,
1172 if (cls->common.chain_index)
1175 switch (cls->command) {
1176 case TC_CLSMATCHALL_REPLACE:
1177 return dsa_slave_add_cls_matchall(dev, cls, ingress);
1178 case TC_CLSMATCHALL_DESTROY:
1179 dsa_slave_del_cls_matchall(dev, cls);
1186 static int dsa_slave_add_cls_flower(struct net_device *dev,
1187 struct flow_cls_offload *cls,
1190 struct dsa_port *dp = dsa_slave_to_port(dev);
1191 struct dsa_switch *ds = dp->ds;
1192 int port = dp->index;
1194 if (!ds->ops->cls_flower_add)
1197 return ds->ops->cls_flower_add(ds, port, cls, ingress);
1200 static int dsa_slave_del_cls_flower(struct net_device *dev,
1201 struct flow_cls_offload *cls,
1204 struct dsa_port *dp = dsa_slave_to_port(dev);
1205 struct dsa_switch *ds = dp->ds;
1206 int port = dp->index;
1208 if (!ds->ops->cls_flower_del)
1211 return ds->ops->cls_flower_del(ds, port, cls, ingress);
1214 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1215 struct flow_cls_offload *cls,
1218 struct dsa_port *dp = dsa_slave_to_port(dev);
1219 struct dsa_switch *ds = dp->ds;
1220 int port = dp->index;
1222 if (!ds->ops->cls_flower_stats)
1225 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1228 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1229 struct flow_cls_offload *cls,
1232 switch (cls->command) {
1233 case FLOW_CLS_REPLACE:
1234 return dsa_slave_add_cls_flower(dev, cls, ingress);
1235 case FLOW_CLS_DESTROY:
1236 return dsa_slave_del_cls_flower(dev, cls, ingress);
1237 case FLOW_CLS_STATS:
1238 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1244 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1245 void *cb_priv, bool ingress)
1247 struct net_device *dev = cb_priv;
1249 if (!tc_can_offload(dev))
1253 case TC_SETUP_CLSMATCHALL:
1254 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1255 case TC_SETUP_CLSFLOWER:
1256 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1262 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1263 void *type_data, void *cb_priv)
1265 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1268 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1269 void *type_data, void *cb_priv)
1271 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1274 static LIST_HEAD(dsa_slave_block_cb_list);
1276 static int dsa_slave_setup_tc_block(struct net_device *dev,
1277 struct flow_block_offload *f)
1279 struct flow_block_cb *block_cb;
1280 flow_setup_cb_t *cb;
1282 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1283 cb = dsa_slave_setup_tc_block_cb_ig;
1284 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1285 cb = dsa_slave_setup_tc_block_cb_eg;
1289 f->driver_block_list = &dsa_slave_block_cb_list;
1291 switch (f->command) {
1292 case FLOW_BLOCK_BIND:
1293 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1296 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1297 if (IS_ERR(block_cb))
1298 return PTR_ERR(block_cb);
1300 flow_block_cb_add(block_cb, f);
1301 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1303 case FLOW_BLOCK_UNBIND:
1304 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1308 flow_block_cb_remove(block_cb, f);
1309 list_del(&block_cb->driver_list);
1316 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
1319 struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1320 struct net_device *master = cpu_dp->master;
1322 if (!master->netdev_ops->ndo_setup_tc)
1325 return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
1328 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1331 struct dsa_port *dp = dsa_slave_to_port(dev);
1332 struct dsa_switch *ds = dp->ds;
1335 case TC_SETUP_BLOCK:
1336 return dsa_slave_setup_tc_block(dev, type_data);
1338 return dsa_slave_setup_ft_block(ds, dp->index, type_data);
1343 if (!ds->ops->port_setup_tc)
1346 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1349 static int dsa_slave_get_rxnfc(struct net_device *dev,
1350 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1352 struct dsa_port *dp = dsa_slave_to_port(dev);
1353 struct dsa_switch *ds = dp->ds;
1355 if (!ds->ops->get_rxnfc)
1358 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1361 static int dsa_slave_set_rxnfc(struct net_device *dev,
1362 struct ethtool_rxnfc *nfc)
1364 struct dsa_port *dp = dsa_slave_to_port(dev);
1365 struct dsa_switch *ds = dp->ds;
1367 if (!ds->ops->set_rxnfc)
1370 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1373 static int dsa_slave_get_ts_info(struct net_device *dev,
1374 struct ethtool_ts_info *ts)
1376 struct dsa_slave_priv *p = netdev_priv(dev);
1377 struct dsa_switch *ds = p->dp->ds;
1379 if (!ds->ops->get_ts_info)
1382 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1385 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1388 struct net_device *master = dsa_slave_to_master(dev);
1389 struct dsa_port *dp = dsa_slave_to_port(dev);
1390 struct switchdev_obj_port_vlan vlan = {
1391 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1393 /* This API only allows programming tagged, non-PVID VIDs */
1396 struct netlink_ext_ack extack = {0};
1400 ret = dsa_port_vlan_add(dp, &vlan, &extack);
1403 netdev_err(dev, "%s\n", extack._msg);
1407 /* And CPU port... */
1408 ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
1411 netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1416 return vlan_vid_add(master, proto, vid);
1419 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1422 struct net_device *master = dsa_slave_to_master(dev);
1423 struct dsa_port *dp = dsa_slave_to_port(dev);
1424 struct switchdev_obj_port_vlan vlan = {
1426 /* This API only allows programming tagged, non-PVID VIDs */
1431 /* Do not deprogram the CPU port as it may be shared with other user
1432 * ports which can be members of this VLAN as well.
1434 err = dsa_port_vlan_del(dp, &vlan);
1438 vlan_vid_del(master, proto, vid);
1443 static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
1445 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1447 return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
1450 static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
1452 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1454 return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
1457 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1458 * filtering is enabled. The baseline is that only ports that offload a
1459 * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1460 * but there are exceptions for quirky hardware.
1462 * If ds->vlan_filtering_is_global = true, then standalone ports which share
1463 * the same switch with other ports that offload a VLAN-aware bridge are also
1464 * inevitably VLAN-aware.
1466 * To summarize, a DSA switch port offloads:
1468 * - If standalone (this includes software bridge, software LAG):
1469 * - if ds->needs_standalone_vlan_filtering = true, OR if
1470 * (ds->vlan_filtering_is_global = true AND there are bridges spanning
1471 * this switch chip which have vlan_filtering=1)
1472 * - the 8021q upper VLANs
1473 * - else (standalone VLAN filtering is not needed, VLAN filtering is not
1474 * global, or it is, but no port is under a VLAN-aware bridge):
1475 * - no VLAN (any 8021q upper is a software VLAN)
1477 * - If under a vlan_filtering=0 bridge which it offload:
1478 * - if ds->configure_vlan_while_not_filtering = true (default):
1479 * - the bridge VLANs. These VLANs are committed to hardware but inactive.
1480 * - else (deprecated):
1481 * - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1482 * enabled, so this behavior is broken and discouraged.
1484 * - If under a vlan_filtering=1 bridge which it offload:
1485 * - the bridge VLANs
1486 * - the 8021q upper VLANs
1488 int dsa_slave_manage_vlan_filtering(struct net_device *slave,
1489 bool vlan_filtering)
1493 if (vlan_filtering) {
1494 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1496 err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
1498 vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1499 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1503 err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1507 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1513 struct dsa_hw_port {
1514 struct list_head list;
1515 struct net_device *dev;
1519 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1521 const struct dsa_hw_port *p;
1524 list_for_each_entry(p, hw_port_list, list) {
1525 if (p->dev->mtu == mtu)
1528 err = dev_set_mtu(p->dev, mtu);
1536 list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1537 if (p->dev->mtu == p->old_mtu)
1540 if (dev_set_mtu(p->dev, p->old_mtu))
1541 netdev_err(p->dev, "Failed to restore MTU\n");
1547 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1549 struct dsa_hw_port *p, *n;
1551 list_for_each_entry_safe(p, n, hw_port_list, list)
1555 /* Make the hardware datapath to/from @dev limited to a common MTU */
1556 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1558 struct list_head hw_port_list;
1559 struct dsa_switch_tree *dst;
1560 int min_mtu = ETH_MAX_MTU;
1561 struct dsa_port *other_dp;
1564 if (!dp->ds->mtu_enforcement_ingress)
1567 if (!dp->bridge_dev)
1570 INIT_LIST_HEAD(&hw_port_list);
1572 /* Populate the list of ports that are part of the same bridge
1573 * as the newly added/modified port
1575 list_for_each_entry(dst, &dsa_tree_list, list) {
1576 list_for_each_entry(other_dp, &dst->ports, list) {
1577 struct dsa_hw_port *hw_port;
1578 struct net_device *slave;
1580 if (other_dp->type != DSA_PORT_TYPE_USER)
1583 if (other_dp->bridge_dev != dp->bridge_dev)
1586 if (!other_dp->ds->mtu_enforcement_ingress)
1589 slave = other_dp->slave;
1591 if (min_mtu > slave->mtu)
1592 min_mtu = slave->mtu;
1594 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1598 hw_port->dev = slave;
1599 hw_port->old_mtu = slave->mtu;
1601 list_add(&hw_port->list, &hw_port_list);
1605 /* Attempt to configure the entire hardware bridge to the newly added
1606 * interface's MTU first, regardless of whether the intention of the
1607 * user was to raise or lower it.
1609 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1613 /* Clearly that didn't work out so well, so just set the minimum MTU on
1614 * all hardware bridge ports now. If this fails too, then all ports will
1615 * still have their old MTU rolled back anyway.
1617 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1620 dsa_hw_port_list_free(&hw_port_list);
1623 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1625 struct net_device *master = dsa_slave_to_master(dev);
1626 struct dsa_port *dp = dsa_slave_to_port(dev);
1627 struct dsa_slave_priv *p = netdev_priv(dev);
1628 struct dsa_switch *ds = p->dp->ds;
1629 struct dsa_port *dp_iter;
1630 struct dsa_port *cpu_dp;
1631 int port = p->dp->index;
1632 int largest_mtu = 0;
1639 if (!ds->ops->port_change_mtu)
1642 list_for_each_entry(dp_iter, &ds->dst->ports, list) {
1645 if (!dsa_port_is_user(dp_iter))
1648 /* During probe, this function will be called for each slave
1649 * device, while not all of them have been allocated. That's
1650 * ok, it doesn't change what the maximum is, so ignore it.
1652 if (!dp_iter->slave)
1655 /* Pretend that we already applied the setting, which we
1656 * actually haven't (still haven't done all integrity checks)
1659 slave_mtu = new_mtu;
1661 slave_mtu = dp_iter->slave->mtu;
1663 if (largest_mtu < slave_mtu)
1664 largest_mtu = slave_mtu;
1667 cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1669 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1670 old_master_mtu = master->mtu;
1671 new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1672 if (new_master_mtu > mtu_limit)
1675 /* If the master MTU isn't over limit, there's no need to check the CPU
1676 * MTU, since that surely isn't either.
1678 cpu_mtu = largest_mtu;
1680 /* Start applying stuff */
1681 if (new_master_mtu != old_master_mtu) {
1682 err = dev_set_mtu(master, new_master_mtu);
1684 goto out_master_failed;
1686 /* We only need to propagate the MTU of the CPU port to
1687 * upstream switches, so create a non-targeted notifier which
1688 * updates all switches.
1690 err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false);
1692 goto out_cpu_failed;
1695 err = dsa_port_mtu_change(dp, new_mtu, true);
1697 goto out_port_failed;
1701 dsa_bridge_mtu_normalization(dp);
1706 if (new_master_mtu != old_master_mtu)
1707 dsa_port_mtu_change(cpu_dp, old_master_mtu -
1708 dsa_tag_protocol_overhead(cpu_dp->tag_ops),
1711 if (new_master_mtu != old_master_mtu)
1712 dev_set_mtu(master, old_master_mtu);
1717 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1718 .get_drvinfo = dsa_slave_get_drvinfo,
1719 .get_regs_len = dsa_slave_get_regs_len,
1720 .get_regs = dsa_slave_get_regs,
1721 .nway_reset = dsa_slave_nway_reset,
1722 .get_link = ethtool_op_get_link,
1723 .get_eeprom_len = dsa_slave_get_eeprom_len,
1724 .get_eeprom = dsa_slave_get_eeprom,
1725 .set_eeprom = dsa_slave_set_eeprom,
1726 .get_strings = dsa_slave_get_strings,
1727 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1728 .get_sset_count = dsa_slave_get_sset_count,
1729 .get_eth_phy_stats = dsa_slave_get_eth_phy_stats,
1730 .get_eth_mac_stats = dsa_slave_get_eth_mac_stats,
1731 .get_eth_ctrl_stats = dsa_slave_get_eth_ctrl_stats,
1732 .set_wol = dsa_slave_set_wol,
1733 .get_wol = dsa_slave_get_wol,
1734 .set_eee = dsa_slave_set_eee,
1735 .get_eee = dsa_slave_get_eee,
1736 .get_link_ksettings = dsa_slave_get_link_ksettings,
1737 .set_link_ksettings = dsa_slave_set_link_ksettings,
1738 .get_pauseparam = dsa_slave_get_pauseparam,
1739 .set_pauseparam = dsa_slave_set_pauseparam,
1740 .get_rxnfc = dsa_slave_get_rxnfc,
1741 .set_rxnfc = dsa_slave_set_rxnfc,
1742 .get_ts_info = dsa_slave_get_ts_info,
1743 .self_test = dsa_slave_net_selftest,
1746 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1748 struct dsa_port *dp = dsa_slave_to_port(dev);
1750 return dp->ds->devlink ? &dp->devlink_port : NULL;
1753 static void dsa_slave_get_stats64(struct net_device *dev,
1754 struct rtnl_link_stats64 *s)
1756 struct dsa_port *dp = dsa_slave_to_port(dev);
1757 struct dsa_switch *ds = dp->ds;
1759 if (ds->ops->get_stats64)
1760 ds->ops->get_stats64(ds, dp->index, s);
1762 dev_get_tstats64(dev, s);
1765 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
1766 struct net_device_path *path)
1768 struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
1769 struct dsa_port *cpu_dp = dp->cpu_dp;
1771 path->dev = ctx->dev;
1772 path->type = DEV_PATH_DSA;
1773 path->dsa.proto = cpu_dp->tag_ops->proto;
1774 path->dsa.port = dp->index;
1775 ctx->dev = cpu_dp->master;
1780 static const struct net_device_ops dsa_slave_netdev_ops = {
1781 .ndo_open = dsa_slave_open,
1782 .ndo_stop = dsa_slave_close,
1783 .ndo_start_xmit = dsa_slave_xmit,
1784 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1785 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1786 .ndo_set_mac_address = dsa_slave_set_mac_address,
1787 .ndo_fdb_dump = dsa_slave_fdb_dump,
1788 .ndo_eth_ioctl = dsa_slave_ioctl,
1789 .ndo_get_iflink = dsa_slave_get_iflink,
1790 #ifdef CONFIG_NET_POLL_CONTROLLER
1791 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1792 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1793 .ndo_poll_controller = dsa_slave_poll_controller,
1795 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1796 .ndo_setup_tc = dsa_slave_setup_tc,
1797 .ndo_get_stats64 = dsa_slave_get_stats64,
1798 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1799 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1800 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1801 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1802 .ndo_change_mtu = dsa_slave_change_mtu,
1803 .ndo_fill_forward_path = dsa_slave_fill_forward_path,
1806 static struct device_type dsa_type = {
1810 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1812 const struct dsa_port *dp = dsa_to_port(ds, port);
1815 phylink_mac_change(dp->pl, up);
1817 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1819 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1820 struct phylink_link_state *state)
1822 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1823 struct dsa_switch *ds = dp->ds;
1825 /* No need to check that this operation is valid, the callback would
1826 * not be called if it was not.
1828 ds->ops->phylink_fixed_state(ds, dp->index, state);
1831 /* slave device setup *******************************************************/
1832 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
1835 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1836 struct dsa_switch *ds = dp->ds;
1838 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1839 if (!slave_dev->phydev) {
1840 netdev_err(slave_dev, "no phy at %d\n", addr);
1844 slave_dev->phydev->dev_flags |= flags;
1846 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1849 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1851 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1852 struct device_node *port_dn = dp->dn;
1853 struct dsa_switch *ds = dp->ds;
1854 phy_interface_t mode;
1858 ret = of_get_phy_mode(port_dn, &mode);
1860 mode = PHY_INTERFACE_MODE_NA;
1862 dp->pl_config.dev = &slave_dev->dev;
1863 dp->pl_config.type = PHYLINK_NETDEV;
1865 /* The get_fixed_state callback takes precedence over polling the
1866 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set
1867 * this if the switch provides such a callback.
1869 if (ds->ops->phylink_fixed_state) {
1870 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
1871 dp->pl_config.poll_fixed_state = true;
1874 if (ds->ops->phylink_get_interfaces)
1875 ds->ops->phylink_get_interfaces(ds, dp->index,
1876 dp->pl_config.supported_interfaces);
1878 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1879 &dsa_port_phylink_mac_ops);
1880 if (IS_ERR(dp->pl)) {
1881 netdev_err(slave_dev,
1882 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1883 return PTR_ERR(dp->pl);
1886 if (ds->ops->get_phy_flags)
1887 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1889 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1890 if (ret == -ENODEV && ds->slave_mii_bus) {
1891 /* We could not connect to a designated PHY or SFP, so try to
1892 * use the switch internal MDIO bus instead
1894 ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
1897 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
1899 phylink_destroy(dp->pl);
1905 void dsa_slave_setup_tagger(struct net_device *slave)
1907 struct dsa_port *dp = dsa_slave_to_port(slave);
1908 struct dsa_slave_priv *p = netdev_priv(slave);
1909 const struct dsa_port *cpu_dp = dp->cpu_dp;
1910 struct net_device *master = cpu_dp->master;
1911 const struct dsa_switch *ds = dp->ds;
1913 slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
1914 slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
1915 /* Try to save one extra realloc later in the TX path (in the master)
1916 * by also inheriting the master's needed headroom and tailroom.
1917 * The 8021q driver also does this.
1919 slave->needed_headroom += master->needed_headroom;
1920 slave->needed_tailroom += master->needed_tailroom;
1922 p->xmit = cpu_dp->tag_ops->xmit;
1924 slave->features = master->vlan_features | NETIF_F_HW_TC;
1925 slave->hw_features |= NETIF_F_HW_TC;
1926 slave->features |= NETIF_F_LLTX;
1927 if (slave->needed_tailroom)
1928 slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
1929 if (ds->needs_standalone_vlan_filtering)
1930 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1933 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1934 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1935 struct netdev_queue *txq,
1938 lockdep_set_class(&txq->_xmit_lock,
1939 &dsa_slave_netdev_xmit_lock_key);
1942 int dsa_slave_suspend(struct net_device *slave_dev)
1944 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1946 if (!netif_running(slave_dev))
1949 netif_device_detach(slave_dev);
1952 phylink_stop(dp->pl);
1958 int dsa_slave_resume(struct net_device *slave_dev)
1960 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1962 if (!netif_running(slave_dev))
1965 netif_device_attach(slave_dev);
1968 phylink_start(dp->pl);
1974 int dsa_slave_create(struct dsa_port *port)
1976 const struct dsa_port *cpu_dp = port->cpu_dp;
1977 struct net_device *master = cpu_dp->master;
1978 struct dsa_switch *ds = port->ds;
1979 const char *name = port->name;
1980 struct net_device *slave_dev;
1981 struct dsa_slave_priv *p;
1984 if (!ds->num_tx_queues)
1985 ds->num_tx_queues = 1;
1987 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1988 NET_NAME_UNKNOWN, ether_setup,
1989 ds->num_tx_queues, 1);
1990 if (slave_dev == NULL)
1993 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1994 if (!is_zero_ether_addr(port->mac))
1995 eth_hw_addr_set(slave_dev, port->mac);
1997 eth_hw_addr_inherit(slave_dev, master);
1998 slave_dev->priv_flags |= IFF_NO_QUEUE;
1999 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
2000 if (ds->ops->port_max_mtu)
2001 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2002 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
2004 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
2007 SET_NETDEV_DEV(slave_dev, port->ds->dev);
2008 slave_dev->dev.of_node = port->dn;
2009 slave_dev->vlan_features = master->vlan_features;
2011 p = netdev_priv(slave_dev);
2012 slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2013 if (!slave_dev->tstats) {
2014 free_netdev(slave_dev);
2018 ret = gro_cells_init(&p->gcells, slave_dev);
2023 INIT_LIST_HEAD(&p->mall_tc_list);
2024 port->slave = slave_dev;
2025 dsa_slave_setup_tagger(slave_dev);
2028 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2030 if (ret && ret != -EOPNOTSUPP)
2031 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2032 ret, ETH_DATA_LEN, port->index);
2034 netif_carrier_off(slave_dev);
2036 ret = dsa_slave_phy_setup(slave_dev);
2038 netdev_err(slave_dev,
2039 "error %d setting up PHY for tree %d, switch %d, port %d\n",
2040 ret, ds->dst->index, ds->index, port->index);
2046 ret = register_netdevice(slave_dev);
2048 netdev_err(master, "error %d registering interface %s\n",
2049 ret, slave_dev->name);
2054 ret = netdev_upper_dev_link(master, slave_dev, NULL);
2059 goto out_unregister;
2064 unregister_netdev(slave_dev);
2067 phylink_disconnect_phy(p->dp->pl);
2069 phylink_destroy(p->dp->pl);
2071 gro_cells_destroy(&p->gcells);
2073 free_percpu(slave_dev->tstats);
2074 free_netdev(slave_dev);
2079 void dsa_slave_destroy(struct net_device *slave_dev)
2081 struct net_device *master = dsa_slave_to_master(slave_dev);
2082 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2083 struct dsa_slave_priv *p = netdev_priv(slave_dev);
2085 netif_carrier_off(slave_dev);
2087 netdev_upper_dev_unlink(master, slave_dev);
2088 unregister_netdevice(slave_dev);
2089 phylink_disconnect_phy(dp->pl);
2092 phylink_destroy(dp->pl);
2093 gro_cells_destroy(&p->gcells);
2094 free_percpu(slave_dev->tstats);
2095 free_netdev(slave_dev);
2098 bool dsa_slave_dev_check(const struct net_device *dev)
2100 return dev->netdev_ops == &dsa_slave_netdev_ops;
2102 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2104 static int dsa_slave_changeupper(struct net_device *dev,
2105 struct netdev_notifier_changeupper_info *info)
2107 struct dsa_port *dp = dsa_slave_to_port(dev);
2108 struct netlink_ext_ack *extack;
2109 int err = NOTIFY_DONE;
2111 extack = netdev_notifier_info_to_extack(&info->info);
2113 if (netif_is_bridge_master(info->upper_dev)) {
2114 if (info->linking) {
2115 err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2117 dsa_bridge_mtu_normalization(dp);
2118 if (err == -EOPNOTSUPP) {
2119 NL_SET_ERR_MSG_MOD(extack,
2120 "Offloading not supported");
2123 err = notifier_from_errno(err);
2125 dsa_port_bridge_leave(dp, info->upper_dev);
2128 } else if (netif_is_lag_master(info->upper_dev)) {
2129 if (info->linking) {
2130 err = dsa_port_lag_join(dp, info->upper_dev,
2131 info->upper_info, extack);
2132 if (err == -EOPNOTSUPP) {
2133 NL_SET_ERR_MSG_MOD(info->info.extack,
2134 "Offloading not supported");
2137 err = notifier_from_errno(err);
2139 dsa_port_lag_leave(dp, info->upper_dev);
2142 } else if (is_hsr_master(info->upper_dev)) {
2143 if (info->linking) {
2144 err = dsa_port_hsr_join(dp, info->upper_dev);
2145 if (err == -EOPNOTSUPP) {
2146 NL_SET_ERR_MSG_MOD(info->info.extack,
2147 "Offloading not supported");
2150 err = notifier_from_errno(err);
2152 dsa_port_hsr_leave(dp, info->upper_dev);
2160 static int dsa_slave_prechangeupper(struct net_device *dev,
2161 struct netdev_notifier_changeupper_info *info)
2163 struct dsa_port *dp = dsa_slave_to_port(dev);
2165 if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2166 dsa_port_pre_bridge_leave(dp, info->upper_dev);
2167 else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2168 dsa_port_pre_lag_leave(dp, info->upper_dev);
2169 /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2170 * meaningfully enslaved to a bridge yet
2177 dsa_slave_lag_changeupper(struct net_device *dev,
2178 struct netdev_notifier_changeupper_info *info)
2180 struct net_device *lower;
2181 struct list_head *iter;
2182 int err = NOTIFY_DONE;
2183 struct dsa_port *dp;
2185 netdev_for_each_lower_dev(dev, lower, iter) {
2186 if (!dsa_slave_dev_check(lower))
2189 dp = dsa_slave_to_port(lower);
2194 err = dsa_slave_changeupper(lower, info);
2195 if (notifier_to_errno(err))
2202 /* Same as dsa_slave_lag_changeupper() except that it calls
2203 * dsa_slave_prechangeupper()
2206 dsa_slave_lag_prechangeupper(struct net_device *dev,
2207 struct netdev_notifier_changeupper_info *info)
2209 struct net_device *lower;
2210 struct list_head *iter;
2211 int err = NOTIFY_DONE;
2212 struct dsa_port *dp;
2214 netdev_for_each_lower_dev(dev, lower, iter) {
2215 if (!dsa_slave_dev_check(lower))
2218 dp = dsa_slave_to_port(lower);
2223 err = dsa_slave_prechangeupper(lower, info);
2224 if (notifier_to_errno(err))
2232 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2233 struct netdev_notifier_changeupper_info *info)
2235 struct netlink_ext_ack *ext_ack;
2236 struct net_device *slave;
2237 struct dsa_port *dp;
2239 ext_ack = netdev_notifier_info_to_extack(&info->info);
2241 if (!is_vlan_dev(dev))
2244 slave = vlan_dev_real_dev(dev);
2245 if (!dsa_slave_dev_check(slave))
2248 dp = dsa_slave_to_port(slave);
2249 if (!dp->bridge_dev)
2252 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
2253 if (br_vlan_enabled(dp->bridge_dev) &&
2254 netif_is_bridge_master(info->upper_dev) && info->linking) {
2255 NL_SET_ERR_MSG_MOD(ext_ack,
2256 "Cannot enslave VLAN device into VLAN aware bridge");
2257 return notifier_from_errno(-EINVAL);
2264 dsa_slave_check_8021q_upper(struct net_device *dev,
2265 struct netdev_notifier_changeupper_info *info)
2267 struct dsa_port *dp = dsa_slave_to_port(dev);
2268 struct net_device *br = dp->bridge_dev;
2269 struct bridge_vlan_info br_info;
2270 struct netlink_ext_ack *extack;
2271 int err = NOTIFY_DONE;
2274 if (!br || !br_vlan_enabled(br))
2277 extack = netdev_notifier_info_to_extack(&info->info);
2278 vid = vlan_dev_vlan_id(info->upper_dev);
2280 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2281 * device, respectively the VID is not found, returning
2282 * 0 means success, which is a failure for us here.
2284 err = br_vlan_get_info(br, vid, &br_info);
2286 NL_SET_ERR_MSG_MOD(extack,
2287 "This VLAN is already configured by the bridge");
2288 return notifier_from_errno(-EBUSY);
2295 dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
2296 struct netdev_notifier_changeupper_info *info)
2298 struct dsa_switch *ds;
2299 struct dsa_port *dp;
2302 if (!dsa_slave_dev_check(dev))
2303 return dsa_prevent_bridging_8021q_upper(dev, info);
2305 dp = dsa_slave_to_port(dev);
2308 if (ds->ops->port_prechangeupper) {
2309 err = ds->ops->port_prechangeupper(ds, dp->index, info);
2311 return notifier_from_errno(err);
2314 if (is_vlan_dev(info->upper_dev))
2315 return dsa_slave_check_8021q_upper(dev, info);
2320 static int dsa_slave_netdevice_event(struct notifier_block *nb,
2321 unsigned long event, void *ptr)
2323 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2326 case NETDEV_PRECHANGEUPPER: {
2327 struct netdev_notifier_changeupper_info *info = ptr;
2330 err = dsa_slave_prechangeupper_sanity_check(dev, info);
2331 if (err != NOTIFY_DONE)
2334 if (dsa_slave_dev_check(dev))
2335 return dsa_slave_prechangeupper(dev, ptr);
2337 if (netif_is_lag_master(dev))
2338 return dsa_slave_lag_prechangeupper(dev, ptr);
2342 case NETDEV_CHANGEUPPER:
2343 if (dsa_slave_dev_check(dev))
2344 return dsa_slave_changeupper(dev, ptr);
2346 if (netif_is_lag_master(dev))
2347 return dsa_slave_lag_changeupper(dev, ptr);
2350 case NETDEV_CHANGELOWERSTATE: {
2351 struct netdev_notifier_changelowerstate_info *info = ptr;
2352 struct dsa_port *dp;
2355 if (!dsa_slave_dev_check(dev))
2358 dp = dsa_slave_to_port(dev);
2360 err = dsa_port_lag_change(dp, info->lower_state_info);
2361 return notifier_from_errno(err);
2363 case NETDEV_GOING_DOWN: {
2364 struct dsa_port *dp, *cpu_dp;
2365 struct dsa_switch_tree *dst;
2366 LIST_HEAD(close_list);
2368 if (!netdev_uses_dsa(dev))
2371 cpu_dp = dev->dsa_ptr;
2372 dst = cpu_dp->ds->dst;
2374 list_for_each_entry(dp, &dst->ports, list) {
2375 if (!dsa_port_is_user(dp))
2378 list_add(&dp->slave->close_list, &close_list);
2381 dev_close_many(&close_list, true);
2393 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2395 struct switchdev_notifier_fdb_info info = {};
2396 struct dsa_switch *ds = switchdev_work->ds;
2397 struct dsa_port *dp;
2399 if (!dsa_is_user_port(ds, switchdev_work->port))
2402 info.addr = switchdev_work->addr;
2403 info.vid = switchdev_work->vid;
2404 info.offloaded = true;
2405 dp = dsa_to_port(ds, switchdev_work->port);
2406 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2407 dp->slave, &info.info, NULL);
2410 static void dsa_slave_switchdev_event_work(struct work_struct *work)
2412 struct dsa_switchdev_event_work *switchdev_work =
2413 container_of(work, struct dsa_switchdev_event_work, work);
2414 struct dsa_switch *ds = switchdev_work->ds;
2415 struct dsa_port *dp;
2418 dp = dsa_to_port(ds, switchdev_work->port);
2420 switch (switchdev_work->event) {
2421 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2422 if (switchdev_work->host_addr)
2423 err = dsa_port_host_fdb_add(dp, switchdev_work->addr,
2424 switchdev_work->vid);
2426 err = dsa_port_fdb_add(dp, switchdev_work->addr,
2427 switchdev_work->vid);
2430 "port %d failed to add %pM vid %d to fdb: %d\n",
2431 dp->index, switchdev_work->addr,
2432 switchdev_work->vid, err);
2435 dsa_fdb_offload_notify(switchdev_work);
2438 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2439 if (switchdev_work->host_addr)
2440 err = dsa_port_host_fdb_del(dp, switchdev_work->addr,
2441 switchdev_work->vid);
2443 err = dsa_port_fdb_del(dp, switchdev_work->addr,
2444 switchdev_work->vid);
2447 "port %d failed to delete %pM vid %d from fdb: %d\n",
2448 dp->index, switchdev_work->addr,
2449 switchdev_work->vid, err);
2455 kfree(switchdev_work);
2458 static bool dsa_foreign_dev_check(const struct net_device *dev,
2459 const struct net_device *foreign_dev)
2461 const struct dsa_port *dp = dsa_slave_to_port(dev);
2462 struct dsa_switch_tree *dst = dp->ds->dst;
2464 if (netif_is_bridge_master(foreign_dev))
2465 return !dsa_tree_offloads_bridge(dst, foreign_dev);
2467 if (netif_is_bridge_port(foreign_dev))
2468 return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2470 /* Everything else is foreign */
2474 static int dsa_slave_fdb_event(struct net_device *dev,
2475 struct net_device *orig_dev,
2476 unsigned long event, const void *ctx,
2477 const struct switchdev_notifier_fdb_info *fdb_info)
2479 struct dsa_switchdev_event_work *switchdev_work;
2480 struct dsa_port *dp = dsa_slave_to_port(dev);
2481 bool host_addr = fdb_info->is_local;
2482 struct dsa_switch *ds = dp->ds;
2484 if (ctx && ctx != dp)
2487 if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2490 if (dsa_slave_dev_check(orig_dev) &&
2491 switchdev_fdb_is_dynamically_learned(fdb_info))
2494 /* FDB entries learned by the software bridge should be installed as
2495 * host addresses only if the driver requests assisted learning.
2497 if (switchdev_fdb_is_dynamically_learned(fdb_info) &&
2498 !ds->assisted_learning_on_cpu_port)
2501 /* Also treat FDB entries on foreign interfaces bridged with us as host
2504 if (dsa_foreign_dev_check(dev, orig_dev))
2507 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2508 if (!switchdev_work)
2511 netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2512 event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2513 orig_dev->name, fdb_info->addr, fdb_info->vid,
2514 host_addr ? " as host address" : "");
2516 INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2517 switchdev_work->ds = ds;
2518 switchdev_work->port = dp->index;
2519 switchdev_work->event = event;
2520 switchdev_work->dev = dev;
2522 ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2523 switchdev_work->vid = fdb_info->vid;
2524 switchdev_work->host_addr = host_addr;
2526 dsa_schedule_work(&switchdev_work->work);
2531 /* Called under rcu_read_lock() */
2532 static int dsa_slave_switchdev_event(struct notifier_block *unused,
2533 unsigned long event, void *ptr)
2535 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2539 case SWITCHDEV_PORT_ATTR_SET:
2540 err = switchdev_handle_port_attr_set(dev, ptr,
2541 dsa_slave_dev_check,
2542 dsa_slave_port_attr_set);
2543 return notifier_from_errno(err);
2544 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2545 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2546 err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
2547 dsa_slave_dev_check,
2548 dsa_foreign_dev_check,
2549 dsa_slave_fdb_event,
2551 return notifier_from_errno(err);
2559 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2560 unsigned long event, void *ptr)
2562 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2566 case SWITCHDEV_PORT_OBJ_ADD:
2567 err = switchdev_handle_port_obj_add(dev, ptr,
2568 dsa_slave_dev_check,
2569 dsa_slave_port_obj_add);
2570 return notifier_from_errno(err);
2571 case SWITCHDEV_PORT_OBJ_DEL:
2572 err = switchdev_handle_port_obj_del(dev, ptr,
2573 dsa_slave_dev_check,
2574 dsa_slave_port_obj_del);
2575 return notifier_from_errno(err);
2576 case SWITCHDEV_PORT_ATTR_SET:
2577 err = switchdev_handle_port_attr_set(dev, ptr,
2578 dsa_slave_dev_check,
2579 dsa_slave_port_attr_set);
2580 return notifier_from_errno(err);
2586 static struct notifier_block dsa_slave_nb __read_mostly = {
2587 .notifier_call = dsa_slave_netdevice_event,
2590 struct notifier_block dsa_slave_switchdev_notifier = {
2591 .notifier_call = dsa_slave_switchdev_event,
2594 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
2595 .notifier_call = dsa_slave_switchdev_blocking_event,
2598 int dsa_slave_register_notifier(void)
2600 struct notifier_block *nb;
2603 err = register_netdevice_notifier(&dsa_slave_nb);
2607 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2609 goto err_switchdev_nb;
2611 nb = &dsa_slave_switchdev_blocking_notifier;
2612 err = register_switchdev_blocking_notifier(nb);
2614 goto err_switchdev_blocking_nb;
2618 err_switchdev_blocking_nb:
2619 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2621 unregister_netdevice_notifier(&dsa_slave_nb);
2625 void dsa_slave_unregister_notifier(void)
2627 struct notifier_block *nb;
2630 nb = &dsa_slave_switchdev_blocking_notifier;
2631 err = unregister_switchdev_blocking_notifier(nb);
2633 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
2635 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2637 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2639 err = unregister_netdevice_notifier(&dsa_slave_nb);
2641 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);