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_net_selftest(struct net_device *ndev,
793 struct ethtool_test *etest, u64 *buf)
795 struct dsa_port *dp = dsa_slave_to_port(ndev);
796 struct dsa_switch *ds = dp->ds;
798 if (ds->ops->self_test) {
799 ds->ops->self_test(ds, dp->index, etest, buf);
803 net_selftest(ndev, etest, buf);
806 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
808 struct dsa_port *dp = dsa_slave_to_port(dev);
809 struct dsa_switch *ds = dp->ds;
811 phylink_ethtool_get_wol(dp->pl, w);
813 if (ds->ops->get_wol)
814 ds->ops->get_wol(ds, dp->index, w);
817 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
819 struct dsa_port *dp = dsa_slave_to_port(dev);
820 struct dsa_switch *ds = dp->ds;
821 int ret = -EOPNOTSUPP;
823 phylink_ethtool_set_wol(dp->pl, w);
825 if (ds->ops->set_wol)
826 ret = ds->ops->set_wol(ds, dp->index, w);
831 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
833 struct dsa_port *dp = dsa_slave_to_port(dev);
834 struct dsa_switch *ds = dp->ds;
837 /* Port's PHY and MAC both need to be EEE capable */
838 if (!dev->phydev || !dp->pl)
841 if (!ds->ops->set_mac_eee)
844 ret = ds->ops->set_mac_eee(ds, dp->index, e);
848 return phylink_ethtool_set_eee(dp->pl, e);
851 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
853 struct dsa_port *dp = dsa_slave_to_port(dev);
854 struct dsa_switch *ds = dp->ds;
857 /* Port's PHY and MAC both need to be EEE capable */
858 if (!dev->phydev || !dp->pl)
861 if (!ds->ops->get_mac_eee)
864 ret = ds->ops->get_mac_eee(ds, dp->index, e);
868 return phylink_ethtool_get_eee(dp->pl, e);
871 static int dsa_slave_get_link_ksettings(struct net_device *dev,
872 struct ethtool_link_ksettings *cmd)
874 struct dsa_port *dp = dsa_slave_to_port(dev);
876 return phylink_ethtool_ksettings_get(dp->pl, cmd);
879 static int dsa_slave_set_link_ksettings(struct net_device *dev,
880 const struct ethtool_link_ksettings *cmd)
882 struct dsa_port *dp = dsa_slave_to_port(dev);
884 return phylink_ethtool_ksettings_set(dp->pl, cmd);
887 static void dsa_slave_get_pauseparam(struct net_device *dev,
888 struct ethtool_pauseparam *pause)
890 struct dsa_port *dp = dsa_slave_to_port(dev);
892 phylink_ethtool_get_pauseparam(dp->pl, pause);
895 static int dsa_slave_set_pauseparam(struct net_device *dev,
896 struct ethtool_pauseparam *pause)
898 struct dsa_port *dp = dsa_slave_to_port(dev);
900 return phylink_ethtool_set_pauseparam(dp->pl, pause);
903 #ifdef CONFIG_NET_POLL_CONTROLLER
904 static int dsa_slave_netpoll_setup(struct net_device *dev,
905 struct netpoll_info *ni)
907 struct net_device *master = dsa_slave_to_master(dev);
908 struct dsa_slave_priv *p = netdev_priv(dev);
909 struct netpoll *netpoll;
912 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
916 err = __netpoll_setup(netpoll, master);
922 p->netpoll = netpoll;
927 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
929 struct dsa_slave_priv *p = netdev_priv(dev);
930 struct netpoll *netpoll = p->netpoll;
937 __netpoll_free(netpoll);
940 static void dsa_slave_poll_controller(struct net_device *dev)
945 static int dsa_slave_get_phys_port_name(struct net_device *dev,
946 char *name, size_t len)
948 struct dsa_port *dp = dsa_slave_to_port(dev);
950 /* For non-legacy ports, devlink is used and it takes
951 * care of the name generation. This ndo implementation
952 * should be removed with legacy support.
957 if (snprintf(name, len, "p%d", dp->index) >= len)
963 static struct dsa_mall_tc_entry *
964 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
966 struct dsa_slave_priv *p = netdev_priv(dev);
967 struct dsa_mall_tc_entry *mall_tc_entry;
969 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
970 if (mall_tc_entry->cookie == cookie)
971 return mall_tc_entry;
977 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
978 struct tc_cls_matchall_offload *cls,
981 struct dsa_port *dp = dsa_slave_to_port(dev);
982 struct dsa_slave_priv *p = netdev_priv(dev);
983 struct dsa_mall_mirror_tc_entry *mirror;
984 struct dsa_mall_tc_entry *mall_tc_entry;
985 struct dsa_switch *ds = dp->ds;
986 struct flow_action_entry *act;
987 struct dsa_port *to_dp;
990 if (!ds->ops->port_mirror_add)
993 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
997 act = &cls->rule->action.entries[0];
1002 if (!dsa_slave_dev_check(act->dev))
1005 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1009 mall_tc_entry->cookie = cls->cookie;
1010 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1011 mirror = &mall_tc_entry->mirror;
1013 to_dp = dsa_slave_to_port(act->dev);
1015 mirror->to_local_port = to_dp->index;
1016 mirror->ingress = ingress;
1018 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
1020 kfree(mall_tc_entry);
1024 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1030 dsa_slave_add_cls_matchall_police(struct net_device *dev,
1031 struct tc_cls_matchall_offload *cls,
1034 struct netlink_ext_ack *extack = cls->common.extack;
1035 struct dsa_port *dp = dsa_slave_to_port(dev);
1036 struct dsa_slave_priv *p = netdev_priv(dev);
1037 struct dsa_mall_policer_tc_entry *policer;
1038 struct dsa_mall_tc_entry *mall_tc_entry;
1039 struct dsa_switch *ds = dp->ds;
1040 struct flow_action_entry *act;
1043 if (!ds->ops->port_policer_add) {
1044 NL_SET_ERR_MSG_MOD(extack,
1045 "Policing offload not implemented");
1050 NL_SET_ERR_MSG_MOD(extack,
1051 "Only supported on ingress qdisc");
1055 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1056 cls->common.extack))
1059 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1060 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1061 NL_SET_ERR_MSG_MOD(extack,
1062 "Only one port policer allowed");
1067 act = &cls->rule->action.entries[0];
1069 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1073 mall_tc_entry->cookie = cls->cookie;
1074 mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1075 policer = &mall_tc_entry->policer;
1076 policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1077 policer->burst = act->police.burst;
1079 err = ds->ops->port_policer_add(ds, dp->index, policer);
1081 kfree(mall_tc_entry);
1085 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1090 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1091 struct tc_cls_matchall_offload *cls,
1094 int err = -EOPNOTSUPP;
1096 if (cls->common.protocol == htons(ETH_P_ALL) &&
1097 flow_offload_has_one_action(&cls->rule->action) &&
1098 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1099 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1100 else if (flow_offload_has_one_action(&cls->rule->action) &&
1101 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1102 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1107 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1108 struct tc_cls_matchall_offload *cls)
1110 struct dsa_port *dp = dsa_slave_to_port(dev);
1111 struct dsa_mall_tc_entry *mall_tc_entry;
1112 struct dsa_switch *ds = dp->ds;
1114 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1118 list_del(&mall_tc_entry->list);
1120 switch (mall_tc_entry->type) {
1121 case DSA_PORT_MALL_MIRROR:
1122 if (ds->ops->port_mirror_del)
1123 ds->ops->port_mirror_del(ds, dp->index,
1124 &mall_tc_entry->mirror);
1126 case DSA_PORT_MALL_POLICER:
1127 if (ds->ops->port_policer_del)
1128 ds->ops->port_policer_del(ds, dp->index);
1134 kfree(mall_tc_entry);
1137 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1138 struct tc_cls_matchall_offload *cls,
1141 if (cls->common.chain_index)
1144 switch (cls->command) {
1145 case TC_CLSMATCHALL_REPLACE:
1146 return dsa_slave_add_cls_matchall(dev, cls, ingress);
1147 case TC_CLSMATCHALL_DESTROY:
1148 dsa_slave_del_cls_matchall(dev, cls);
1155 static int dsa_slave_add_cls_flower(struct net_device *dev,
1156 struct flow_cls_offload *cls,
1159 struct dsa_port *dp = dsa_slave_to_port(dev);
1160 struct dsa_switch *ds = dp->ds;
1161 int port = dp->index;
1163 if (!ds->ops->cls_flower_add)
1166 return ds->ops->cls_flower_add(ds, port, cls, ingress);
1169 static int dsa_slave_del_cls_flower(struct net_device *dev,
1170 struct flow_cls_offload *cls,
1173 struct dsa_port *dp = dsa_slave_to_port(dev);
1174 struct dsa_switch *ds = dp->ds;
1175 int port = dp->index;
1177 if (!ds->ops->cls_flower_del)
1180 return ds->ops->cls_flower_del(ds, port, cls, ingress);
1183 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1184 struct flow_cls_offload *cls,
1187 struct dsa_port *dp = dsa_slave_to_port(dev);
1188 struct dsa_switch *ds = dp->ds;
1189 int port = dp->index;
1191 if (!ds->ops->cls_flower_stats)
1194 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1197 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1198 struct flow_cls_offload *cls,
1201 switch (cls->command) {
1202 case FLOW_CLS_REPLACE:
1203 return dsa_slave_add_cls_flower(dev, cls, ingress);
1204 case FLOW_CLS_DESTROY:
1205 return dsa_slave_del_cls_flower(dev, cls, ingress);
1206 case FLOW_CLS_STATS:
1207 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1213 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1214 void *cb_priv, bool ingress)
1216 struct net_device *dev = cb_priv;
1218 if (!tc_can_offload(dev))
1222 case TC_SETUP_CLSMATCHALL:
1223 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1224 case TC_SETUP_CLSFLOWER:
1225 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1231 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1232 void *type_data, void *cb_priv)
1234 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1237 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1238 void *type_data, void *cb_priv)
1240 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1243 static LIST_HEAD(dsa_slave_block_cb_list);
1245 static int dsa_slave_setup_tc_block(struct net_device *dev,
1246 struct flow_block_offload *f)
1248 struct flow_block_cb *block_cb;
1249 flow_setup_cb_t *cb;
1251 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1252 cb = dsa_slave_setup_tc_block_cb_ig;
1253 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1254 cb = dsa_slave_setup_tc_block_cb_eg;
1258 f->driver_block_list = &dsa_slave_block_cb_list;
1260 switch (f->command) {
1261 case FLOW_BLOCK_BIND:
1262 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1265 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1266 if (IS_ERR(block_cb))
1267 return PTR_ERR(block_cb);
1269 flow_block_cb_add(block_cb, f);
1270 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1272 case FLOW_BLOCK_UNBIND:
1273 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1277 flow_block_cb_remove(block_cb, f);
1278 list_del(&block_cb->driver_list);
1285 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
1288 struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1289 struct net_device *master = cpu_dp->master;
1291 if (!master->netdev_ops->ndo_setup_tc)
1294 return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
1297 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1300 struct dsa_port *dp = dsa_slave_to_port(dev);
1301 struct dsa_switch *ds = dp->ds;
1304 case TC_SETUP_BLOCK:
1305 return dsa_slave_setup_tc_block(dev, type_data);
1307 return dsa_slave_setup_ft_block(ds, dp->index, type_data);
1312 if (!ds->ops->port_setup_tc)
1315 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1318 static int dsa_slave_get_rxnfc(struct net_device *dev,
1319 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1321 struct dsa_port *dp = dsa_slave_to_port(dev);
1322 struct dsa_switch *ds = dp->ds;
1324 if (!ds->ops->get_rxnfc)
1327 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1330 static int dsa_slave_set_rxnfc(struct net_device *dev,
1331 struct ethtool_rxnfc *nfc)
1333 struct dsa_port *dp = dsa_slave_to_port(dev);
1334 struct dsa_switch *ds = dp->ds;
1336 if (!ds->ops->set_rxnfc)
1339 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1342 static int dsa_slave_get_ts_info(struct net_device *dev,
1343 struct ethtool_ts_info *ts)
1345 struct dsa_slave_priv *p = netdev_priv(dev);
1346 struct dsa_switch *ds = p->dp->ds;
1348 if (!ds->ops->get_ts_info)
1351 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1354 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1357 struct net_device *master = dsa_slave_to_master(dev);
1358 struct dsa_port *dp = dsa_slave_to_port(dev);
1359 struct switchdev_obj_port_vlan vlan = {
1360 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1362 /* This API only allows programming tagged, non-PVID VIDs */
1365 struct netlink_ext_ack extack = {0};
1369 ret = dsa_port_vlan_add(dp, &vlan, &extack);
1372 netdev_err(dev, "%s\n", extack._msg);
1376 /* And CPU port... */
1377 ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
1380 netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1385 return vlan_vid_add(master, proto, vid);
1388 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1391 struct net_device *master = dsa_slave_to_master(dev);
1392 struct dsa_port *dp = dsa_slave_to_port(dev);
1393 struct switchdev_obj_port_vlan vlan = {
1395 /* This API only allows programming tagged, non-PVID VIDs */
1400 /* Do not deprogram the CPU port as it may be shared with other user
1401 * ports which can be members of this VLAN as well.
1403 err = dsa_port_vlan_del(dp, &vlan);
1407 vlan_vid_del(master, proto, vid);
1412 static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
1414 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1416 return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
1419 static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
1421 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1423 return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
1426 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1427 * filtering is enabled. The baseline is that only ports that offload a
1428 * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1429 * but there are exceptions for quirky hardware.
1431 * If ds->vlan_filtering_is_global = true, then standalone ports which share
1432 * the same switch with other ports that offload a VLAN-aware bridge are also
1433 * inevitably VLAN-aware.
1435 * To summarize, a DSA switch port offloads:
1437 * - If standalone (this includes software bridge, software LAG):
1438 * - if ds->needs_standalone_vlan_filtering = true, OR if
1439 * (ds->vlan_filtering_is_global = true AND there are bridges spanning
1440 * this switch chip which have vlan_filtering=1)
1441 * - the 8021q upper VLANs
1442 * - else (standalone VLAN filtering is not needed, VLAN filtering is not
1443 * global, or it is, but no port is under a VLAN-aware bridge):
1444 * - no VLAN (any 8021q upper is a software VLAN)
1446 * - If under a vlan_filtering=0 bridge which it offload:
1447 * - if ds->configure_vlan_while_not_filtering = true (default):
1448 * - the bridge VLANs. These VLANs are committed to hardware but inactive.
1449 * - else (deprecated):
1450 * - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1451 * enabled, so this behavior is broken and discouraged.
1453 * - If under a vlan_filtering=1 bridge which it offload:
1454 * - the bridge VLANs
1455 * - the 8021q upper VLANs
1457 int dsa_slave_manage_vlan_filtering(struct net_device *slave,
1458 bool vlan_filtering)
1462 if (vlan_filtering) {
1463 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1465 err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
1467 vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1468 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1472 err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1476 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1482 struct dsa_hw_port {
1483 struct list_head list;
1484 struct net_device *dev;
1488 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1490 const struct dsa_hw_port *p;
1493 list_for_each_entry(p, hw_port_list, list) {
1494 if (p->dev->mtu == mtu)
1497 err = dev_set_mtu(p->dev, mtu);
1505 list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1506 if (p->dev->mtu == p->old_mtu)
1509 if (dev_set_mtu(p->dev, p->old_mtu))
1510 netdev_err(p->dev, "Failed to restore MTU\n");
1516 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1518 struct dsa_hw_port *p, *n;
1520 list_for_each_entry_safe(p, n, hw_port_list, list)
1524 /* Make the hardware datapath to/from @dev limited to a common MTU */
1525 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1527 struct list_head hw_port_list;
1528 struct dsa_switch_tree *dst;
1529 int min_mtu = ETH_MAX_MTU;
1530 struct dsa_port *other_dp;
1533 if (!dp->ds->mtu_enforcement_ingress)
1536 if (!dp->bridge_dev)
1539 INIT_LIST_HEAD(&hw_port_list);
1541 /* Populate the list of ports that are part of the same bridge
1542 * as the newly added/modified port
1544 list_for_each_entry(dst, &dsa_tree_list, list) {
1545 list_for_each_entry(other_dp, &dst->ports, list) {
1546 struct dsa_hw_port *hw_port;
1547 struct net_device *slave;
1549 if (other_dp->type != DSA_PORT_TYPE_USER)
1552 if (other_dp->bridge_dev != dp->bridge_dev)
1555 if (!other_dp->ds->mtu_enforcement_ingress)
1558 slave = other_dp->slave;
1560 if (min_mtu > slave->mtu)
1561 min_mtu = slave->mtu;
1563 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1567 hw_port->dev = slave;
1568 hw_port->old_mtu = slave->mtu;
1570 list_add(&hw_port->list, &hw_port_list);
1574 /* Attempt to configure the entire hardware bridge to the newly added
1575 * interface's MTU first, regardless of whether the intention of the
1576 * user was to raise or lower it.
1578 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1582 /* Clearly that didn't work out so well, so just set the minimum MTU on
1583 * all hardware bridge ports now. If this fails too, then all ports will
1584 * still have their old MTU rolled back anyway.
1586 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1589 dsa_hw_port_list_free(&hw_port_list);
1592 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1594 struct net_device *master = dsa_slave_to_master(dev);
1595 struct dsa_port *dp = dsa_slave_to_port(dev);
1596 struct dsa_slave_priv *p = netdev_priv(dev);
1597 struct dsa_switch *ds = p->dp->ds;
1598 struct dsa_port *dp_iter;
1599 struct dsa_port *cpu_dp;
1600 int port = p->dp->index;
1601 int largest_mtu = 0;
1608 if (!ds->ops->port_change_mtu)
1611 list_for_each_entry(dp_iter, &ds->dst->ports, list) {
1614 if (!dsa_port_is_user(dp_iter))
1617 /* During probe, this function will be called for each slave
1618 * device, while not all of them have been allocated. That's
1619 * ok, it doesn't change what the maximum is, so ignore it.
1621 if (!dp_iter->slave)
1624 /* Pretend that we already applied the setting, which we
1625 * actually haven't (still haven't done all integrity checks)
1628 slave_mtu = new_mtu;
1630 slave_mtu = dp_iter->slave->mtu;
1632 if (largest_mtu < slave_mtu)
1633 largest_mtu = slave_mtu;
1636 cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1638 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1639 old_master_mtu = master->mtu;
1640 new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1641 if (new_master_mtu > mtu_limit)
1644 /* If the master MTU isn't over limit, there's no need to check the CPU
1645 * MTU, since that surely isn't either.
1647 cpu_mtu = largest_mtu;
1649 /* Start applying stuff */
1650 if (new_master_mtu != old_master_mtu) {
1651 err = dev_set_mtu(master, new_master_mtu);
1653 goto out_master_failed;
1655 /* We only need to propagate the MTU of the CPU port to
1656 * upstream switches, so create a non-targeted notifier which
1657 * updates all switches.
1659 err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false);
1661 goto out_cpu_failed;
1664 err = dsa_port_mtu_change(dp, new_mtu, true);
1666 goto out_port_failed;
1670 dsa_bridge_mtu_normalization(dp);
1675 if (new_master_mtu != old_master_mtu)
1676 dsa_port_mtu_change(cpu_dp, old_master_mtu -
1677 dsa_tag_protocol_overhead(cpu_dp->tag_ops),
1680 if (new_master_mtu != old_master_mtu)
1681 dev_set_mtu(master, old_master_mtu);
1686 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1687 .get_drvinfo = dsa_slave_get_drvinfo,
1688 .get_regs_len = dsa_slave_get_regs_len,
1689 .get_regs = dsa_slave_get_regs,
1690 .nway_reset = dsa_slave_nway_reset,
1691 .get_link = ethtool_op_get_link,
1692 .get_eeprom_len = dsa_slave_get_eeprom_len,
1693 .get_eeprom = dsa_slave_get_eeprom,
1694 .set_eeprom = dsa_slave_set_eeprom,
1695 .get_strings = dsa_slave_get_strings,
1696 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1697 .get_sset_count = dsa_slave_get_sset_count,
1698 .set_wol = dsa_slave_set_wol,
1699 .get_wol = dsa_slave_get_wol,
1700 .set_eee = dsa_slave_set_eee,
1701 .get_eee = dsa_slave_get_eee,
1702 .get_link_ksettings = dsa_slave_get_link_ksettings,
1703 .set_link_ksettings = dsa_slave_set_link_ksettings,
1704 .get_pauseparam = dsa_slave_get_pauseparam,
1705 .set_pauseparam = dsa_slave_set_pauseparam,
1706 .get_rxnfc = dsa_slave_get_rxnfc,
1707 .set_rxnfc = dsa_slave_set_rxnfc,
1708 .get_ts_info = dsa_slave_get_ts_info,
1709 .self_test = dsa_slave_net_selftest,
1712 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1714 struct dsa_port *dp = dsa_slave_to_port(dev);
1716 return dp->ds->devlink ? &dp->devlink_port : NULL;
1719 static void dsa_slave_get_stats64(struct net_device *dev,
1720 struct rtnl_link_stats64 *s)
1722 struct dsa_port *dp = dsa_slave_to_port(dev);
1723 struct dsa_switch *ds = dp->ds;
1725 if (ds->ops->get_stats64)
1726 ds->ops->get_stats64(ds, dp->index, s);
1728 dev_get_tstats64(dev, s);
1731 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
1732 struct net_device_path *path)
1734 struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
1735 struct dsa_port *cpu_dp = dp->cpu_dp;
1737 path->dev = ctx->dev;
1738 path->type = DEV_PATH_DSA;
1739 path->dsa.proto = cpu_dp->tag_ops->proto;
1740 path->dsa.port = dp->index;
1741 ctx->dev = cpu_dp->master;
1746 static const struct net_device_ops dsa_slave_netdev_ops = {
1747 .ndo_open = dsa_slave_open,
1748 .ndo_stop = dsa_slave_close,
1749 .ndo_start_xmit = dsa_slave_xmit,
1750 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1751 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1752 .ndo_set_mac_address = dsa_slave_set_mac_address,
1753 .ndo_fdb_dump = dsa_slave_fdb_dump,
1754 .ndo_eth_ioctl = dsa_slave_ioctl,
1755 .ndo_get_iflink = dsa_slave_get_iflink,
1756 #ifdef CONFIG_NET_POLL_CONTROLLER
1757 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1758 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1759 .ndo_poll_controller = dsa_slave_poll_controller,
1761 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1762 .ndo_setup_tc = dsa_slave_setup_tc,
1763 .ndo_get_stats64 = dsa_slave_get_stats64,
1764 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1765 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1766 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1767 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1768 .ndo_change_mtu = dsa_slave_change_mtu,
1769 .ndo_fill_forward_path = dsa_slave_fill_forward_path,
1772 static struct device_type dsa_type = {
1776 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1778 const struct dsa_port *dp = dsa_to_port(ds, port);
1781 phylink_mac_change(dp->pl, up);
1783 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1785 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1786 struct phylink_link_state *state)
1788 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1789 struct dsa_switch *ds = dp->ds;
1791 /* No need to check that this operation is valid, the callback would
1792 * not be called if it was not.
1794 ds->ops->phylink_fixed_state(ds, dp->index, state);
1797 /* slave device setup *******************************************************/
1798 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
1801 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1802 struct dsa_switch *ds = dp->ds;
1804 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1805 if (!slave_dev->phydev) {
1806 netdev_err(slave_dev, "no phy at %d\n", addr);
1810 slave_dev->phydev->dev_flags |= flags;
1812 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1815 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1817 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1818 struct device_node *port_dn = dp->dn;
1819 struct dsa_switch *ds = dp->ds;
1820 phy_interface_t mode;
1824 ret = of_get_phy_mode(port_dn, &mode);
1826 mode = PHY_INTERFACE_MODE_NA;
1828 dp->pl_config.dev = &slave_dev->dev;
1829 dp->pl_config.type = PHYLINK_NETDEV;
1831 /* The get_fixed_state callback takes precedence over polling the
1832 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set
1833 * this if the switch provides such a callback.
1835 if (ds->ops->phylink_fixed_state) {
1836 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
1837 dp->pl_config.poll_fixed_state = true;
1840 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1841 &dsa_port_phylink_mac_ops);
1842 if (IS_ERR(dp->pl)) {
1843 netdev_err(slave_dev,
1844 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1845 return PTR_ERR(dp->pl);
1848 if (ds->ops->get_phy_flags)
1849 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1851 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1852 if (ret == -ENODEV && ds->slave_mii_bus) {
1853 /* We could not connect to a designated PHY or SFP, so try to
1854 * use the switch internal MDIO bus instead
1856 ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
1859 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
1861 phylink_destroy(dp->pl);
1867 void dsa_slave_setup_tagger(struct net_device *slave)
1869 struct dsa_port *dp = dsa_slave_to_port(slave);
1870 struct dsa_slave_priv *p = netdev_priv(slave);
1871 const struct dsa_port *cpu_dp = dp->cpu_dp;
1872 struct net_device *master = cpu_dp->master;
1873 const struct dsa_switch *ds = dp->ds;
1875 slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
1876 slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
1877 /* Try to save one extra realloc later in the TX path (in the master)
1878 * by also inheriting the master's needed headroom and tailroom.
1879 * The 8021q driver also does this.
1881 slave->needed_headroom += master->needed_headroom;
1882 slave->needed_tailroom += master->needed_tailroom;
1884 p->xmit = cpu_dp->tag_ops->xmit;
1886 slave->features = master->vlan_features | NETIF_F_HW_TC;
1887 slave->hw_features |= NETIF_F_HW_TC;
1888 slave->features |= NETIF_F_LLTX;
1889 if (slave->needed_tailroom)
1890 slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
1891 if (ds->needs_standalone_vlan_filtering)
1892 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1895 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1896 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1897 struct netdev_queue *txq,
1900 lockdep_set_class(&txq->_xmit_lock,
1901 &dsa_slave_netdev_xmit_lock_key);
1904 int dsa_slave_suspend(struct net_device *slave_dev)
1906 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1908 if (!netif_running(slave_dev))
1911 netif_device_detach(slave_dev);
1914 phylink_stop(dp->pl);
1920 int dsa_slave_resume(struct net_device *slave_dev)
1922 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1924 if (!netif_running(slave_dev))
1927 netif_device_attach(slave_dev);
1930 phylink_start(dp->pl);
1936 int dsa_slave_create(struct dsa_port *port)
1938 const struct dsa_port *cpu_dp = port->cpu_dp;
1939 struct net_device *master = cpu_dp->master;
1940 struct dsa_switch *ds = port->ds;
1941 const char *name = port->name;
1942 struct net_device *slave_dev;
1943 struct dsa_slave_priv *p;
1946 if (!ds->num_tx_queues)
1947 ds->num_tx_queues = 1;
1949 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1950 NET_NAME_UNKNOWN, ether_setup,
1951 ds->num_tx_queues, 1);
1952 if (slave_dev == NULL)
1955 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1956 if (!is_zero_ether_addr(port->mac))
1957 eth_hw_addr_set(slave_dev, port->mac);
1959 eth_hw_addr_inherit(slave_dev, master);
1960 slave_dev->priv_flags |= IFF_NO_QUEUE;
1961 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1962 if (ds->ops->port_max_mtu)
1963 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1964 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1966 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1969 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1970 slave_dev->dev.of_node = port->dn;
1971 slave_dev->vlan_features = master->vlan_features;
1973 p = netdev_priv(slave_dev);
1974 slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1975 if (!slave_dev->tstats) {
1976 free_netdev(slave_dev);
1980 ret = gro_cells_init(&p->gcells, slave_dev);
1985 INIT_LIST_HEAD(&p->mall_tc_list);
1986 port->slave = slave_dev;
1987 dsa_slave_setup_tagger(slave_dev);
1990 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1992 if (ret && ret != -EOPNOTSUPP)
1993 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
1994 ret, ETH_DATA_LEN, port->index);
1996 netif_carrier_off(slave_dev);
1998 ret = dsa_slave_phy_setup(slave_dev);
2000 netdev_err(slave_dev,
2001 "error %d setting up PHY for tree %d, switch %d, port %d\n",
2002 ret, ds->dst->index, ds->index, port->index);
2008 ret = register_netdevice(slave_dev);
2010 netdev_err(master, "error %d registering interface %s\n",
2011 ret, slave_dev->name);
2016 ret = netdev_upper_dev_link(master, slave_dev, NULL);
2021 goto out_unregister;
2026 unregister_netdev(slave_dev);
2029 phylink_disconnect_phy(p->dp->pl);
2031 phylink_destroy(p->dp->pl);
2033 gro_cells_destroy(&p->gcells);
2035 free_percpu(slave_dev->tstats);
2036 free_netdev(slave_dev);
2041 void dsa_slave_destroy(struct net_device *slave_dev)
2043 struct net_device *master = dsa_slave_to_master(slave_dev);
2044 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2045 struct dsa_slave_priv *p = netdev_priv(slave_dev);
2047 netif_carrier_off(slave_dev);
2049 netdev_upper_dev_unlink(master, slave_dev);
2050 unregister_netdevice(slave_dev);
2051 phylink_disconnect_phy(dp->pl);
2054 phylink_destroy(dp->pl);
2055 gro_cells_destroy(&p->gcells);
2056 free_percpu(slave_dev->tstats);
2057 free_netdev(slave_dev);
2060 bool dsa_slave_dev_check(const struct net_device *dev)
2062 return dev->netdev_ops == &dsa_slave_netdev_ops;
2064 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2066 static int dsa_slave_changeupper(struct net_device *dev,
2067 struct netdev_notifier_changeupper_info *info)
2069 struct dsa_port *dp = dsa_slave_to_port(dev);
2070 struct netlink_ext_ack *extack;
2071 int err = NOTIFY_DONE;
2073 extack = netdev_notifier_info_to_extack(&info->info);
2075 if (netif_is_bridge_master(info->upper_dev)) {
2076 if (info->linking) {
2077 err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2079 dsa_bridge_mtu_normalization(dp);
2080 if (err == -EOPNOTSUPP) {
2081 NL_SET_ERR_MSG_MOD(extack,
2082 "Offloading not supported");
2085 err = notifier_from_errno(err);
2087 dsa_port_bridge_leave(dp, info->upper_dev);
2090 } else if (netif_is_lag_master(info->upper_dev)) {
2091 if (info->linking) {
2092 err = dsa_port_lag_join(dp, info->upper_dev,
2093 info->upper_info, extack);
2094 if (err == -EOPNOTSUPP) {
2095 NL_SET_ERR_MSG_MOD(info->info.extack,
2096 "Offloading not supported");
2099 err = notifier_from_errno(err);
2101 dsa_port_lag_leave(dp, info->upper_dev);
2104 } else if (is_hsr_master(info->upper_dev)) {
2105 if (info->linking) {
2106 err = dsa_port_hsr_join(dp, info->upper_dev);
2107 if (err == -EOPNOTSUPP) {
2108 NL_SET_ERR_MSG_MOD(info->info.extack,
2109 "Offloading not supported");
2112 err = notifier_from_errno(err);
2114 dsa_port_hsr_leave(dp, info->upper_dev);
2122 static int dsa_slave_prechangeupper(struct net_device *dev,
2123 struct netdev_notifier_changeupper_info *info)
2125 struct dsa_port *dp = dsa_slave_to_port(dev);
2127 if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2128 dsa_port_pre_bridge_leave(dp, info->upper_dev);
2129 else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2130 dsa_port_pre_lag_leave(dp, info->upper_dev);
2131 /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2132 * meaningfully enslaved to a bridge yet
2139 dsa_slave_lag_changeupper(struct net_device *dev,
2140 struct netdev_notifier_changeupper_info *info)
2142 struct net_device *lower;
2143 struct list_head *iter;
2144 int err = NOTIFY_DONE;
2145 struct dsa_port *dp;
2147 netdev_for_each_lower_dev(dev, lower, iter) {
2148 if (!dsa_slave_dev_check(lower))
2151 dp = dsa_slave_to_port(lower);
2156 err = dsa_slave_changeupper(lower, info);
2157 if (notifier_to_errno(err))
2164 /* Same as dsa_slave_lag_changeupper() except that it calls
2165 * dsa_slave_prechangeupper()
2168 dsa_slave_lag_prechangeupper(struct net_device *dev,
2169 struct netdev_notifier_changeupper_info *info)
2171 struct net_device *lower;
2172 struct list_head *iter;
2173 int err = NOTIFY_DONE;
2174 struct dsa_port *dp;
2176 netdev_for_each_lower_dev(dev, lower, iter) {
2177 if (!dsa_slave_dev_check(lower))
2180 dp = dsa_slave_to_port(lower);
2185 err = dsa_slave_prechangeupper(lower, info);
2186 if (notifier_to_errno(err))
2194 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2195 struct netdev_notifier_changeupper_info *info)
2197 struct netlink_ext_ack *ext_ack;
2198 struct net_device *slave;
2199 struct dsa_port *dp;
2201 ext_ack = netdev_notifier_info_to_extack(&info->info);
2203 if (!is_vlan_dev(dev))
2206 slave = vlan_dev_real_dev(dev);
2207 if (!dsa_slave_dev_check(slave))
2210 dp = dsa_slave_to_port(slave);
2211 if (!dp->bridge_dev)
2214 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
2215 if (br_vlan_enabled(dp->bridge_dev) &&
2216 netif_is_bridge_master(info->upper_dev) && info->linking) {
2217 NL_SET_ERR_MSG_MOD(ext_ack,
2218 "Cannot enslave VLAN device into VLAN aware bridge");
2219 return notifier_from_errno(-EINVAL);
2226 dsa_slave_check_8021q_upper(struct net_device *dev,
2227 struct netdev_notifier_changeupper_info *info)
2229 struct dsa_port *dp = dsa_slave_to_port(dev);
2230 struct net_device *br = dp->bridge_dev;
2231 struct bridge_vlan_info br_info;
2232 struct netlink_ext_ack *extack;
2233 int err = NOTIFY_DONE;
2236 if (!br || !br_vlan_enabled(br))
2239 extack = netdev_notifier_info_to_extack(&info->info);
2240 vid = vlan_dev_vlan_id(info->upper_dev);
2242 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2243 * device, respectively the VID is not found, returning
2244 * 0 means success, which is a failure for us here.
2246 err = br_vlan_get_info(br, vid, &br_info);
2248 NL_SET_ERR_MSG_MOD(extack,
2249 "This VLAN is already configured by the bridge");
2250 return notifier_from_errno(-EBUSY);
2257 dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
2258 struct netdev_notifier_changeupper_info *info)
2260 struct dsa_switch *ds;
2261 struct dsa_port *dp;
2264 if (!dsa_slave_dev_check(dev))
2265 return dsa_prevent_bridging_8021q_upper(dev, info);
2267 dp = dsa_slave_to_port(dev);
2270 if (ds->ops->port_prechangeupper) {
2271 err = ds->ops->port_prechangeupper(ds, dp->index, info);
2273 return notifier_from_errno(err);
2276 if (is_vlan_dev(info->upper_dev))
2277 return dsa_slave_check_8021q_upper(dev, info);
2282 static int dsa_slave_netdevice_event(struct notifier_block *nb,
2283 unsigned long event, void *ptr)
2285 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2288 case NETDEV_PRECHANGEUPPER: {
2289 struct netdev_notifier_changeupper_info *info = ptr;
2292 err = dsa_slave_prechangeupper_sanity_check(dev, info);
2293 if (err != NOTIFY_DONE)
2296 if (dsa_slave_dev_check(dev))
2297 return dsa_slave_prechangeupper(dev, ptr);
2299 if (netif_is_lag_master(dev))
2300 return dsa_slave_lag_prechangeupper(dev, ptr);
2304 case NETDEV_CHANGEUPPER:
2305 if (dsa_slave_dev_check(dev))
2306 return dsa_slave_changeupper(dev, ptr);
2308 if (netif_is_lag_master(dev))
2309 return dsa_slave_lag_changeupper(dev, ptr);
2312 case NETDEV_CHANGELOWERSTATE: {
2313 struct netdev_notifier_changelowerstate_info *info = ptr;
2314 struct dsa_port *dp;
2317 if (!dsa_slave_dev_check(dev))
2320 dp = dsa_slave_to_port(dev);
2322 err = dsa_port_lag_change(dp, info->lower_state_info);
2323 return notifier_from_errno(err);
2325 case NETDEV_GOING_DOWN: {
2326 struct dsa_port *dp, *cpu_dp;
2327 struct dsa_switch_tree *dst;
2328 LIST_HEAD(close_list);
2330 if (!netdev_uses_dsa(dev))
2333 cpu_dp = dev->dsa_ptr;
2334 dst = cpu_dp->ds->dst;
2336 list_for_each_entry(dp, &dst->ports, list) {
2337 if (!dsa_is_user_port(dp->ds, dp->index))
2340 list_add(&dp->slave->close_list, &close_list);
2343 dev_close_many(&close_list, true);
2355 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2357 struct switchdev_notifier_fdb_info info = {};
2358 struct dsa_switch *ds = switchdev_work->ds;
2359 struct dsa_port *dp;
2361 if (!dsa_is_user_port(ds, switchdev_work->port))
2364 info.addr = switchdev_work->addr;
2365 info.vid = switchdev_work->vid;
2366 info.offloaded = true;
2367 dp = dsa_to_port(ds, switchdev_work->port);
2368 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2369 dp->slave, &info.info, NULL);
2372 static void dsa_slave_switchdev_event_work(struct work_struct *work)
2374 struct dsa_switchdev_event_work *switchdev_work =
2375 container_of(work, struct dsa_switchdev_event_work, work);
2376 struct dsa_switch *ds = switchdev_work->ds;
2377 struct dsa_port *dp;
2380 dp = dsa_to_port(ds, switchdev_work->port);
2383 switch (switchdev_work->event) {
2384 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2385 if (switchdev_work->host_addr)
2386 err = dsa_port_host_fdb_add(dp, switchdev_work->addr,
2387 switchdev_work->vid);
2389 err = dsa_port_fdb_add(dp, switchdev_work->addr,
2390 switchdev_work->vid);
2393 "port %d failed to add %pM vid %d to fdb: %d\n",
2394 dp->index, switchdev_work->addr,
2395 switchdev_work->vid, err);
2398 dsa_fdb_offload_notify(switchdev_work);
2401 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2402 if (switchdev_work->host_addr)
2403 err = dsa_port_host_fdb_del(dp, switchdev_work->addr,
2404 switchdev_work->vid);
2406 err = dsa_port_fdb_del(dp, switchdev_work->addr,
2407 switchdev_work->vid);
2410 "port %d failed to delete %pM vid %d from fdb: %d\n",
2411 dp->index, switchdev_work->addr,
2412 switchdev_work->vid, err);
2419 dev_put(switchdev_work->dev);
2420 kfree(switchdev_work);
2423 static bool dsa_foreign_dev_check(const struct net_device *dev,
2424 const struct net_device *foreign_dev)
2426 const struct dsa_port *dp = dsa_slave_to_port(dev);
2427 struct dsa_switch_tree *dst = dp->ds->dst;
2429 if (netif_is_bridge_master(foreign_dev))
2430 return !dsa_tree_offloads_bridge(dst, foreign_dev);
2432 if (netif_is_bridge_port(foreign_dev))
2433 return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2435 /* Everything else is foreign */
2439 static int dsa_slave_fdb_event(struct net_device *dev,
2440 const struct net_device *orig_dev,
2442 const struct switchdev_notifier_fdb_info *fdb_info,
2443 unsigned long event)
2445 struct dsa_switchdev_event_work *switchdev_work;
2446 struct dsa_port *dp = dsa_slave_to_port(dev);
2447 bool host_addr = fdb_info->is_local;
2448 struct dsa_switch *ds = dp->ds;
2450 if (ctx && ctx != dp)
2453 if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2456 if (dsa_slave_dev_check(orig_dev) &&
2457 switchdev_fdb_is_dynamically_learned(fdb_info))
2460 /* FDB entries learned by the software bridge should be installed as
2461 * host addresses only if the driver requests assisted learning.
2463 if (switchdev_fdb_is_dynamically_learned(fdb_info) &&
2464 !ds->assisted_learning_on_cpu_port)
2467 /* Also treat FDB entries on foreign interfaces bridged with us as host
2470 if (dsa_foreign_dev_check(dev, orig_dev))
2473 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2474 if (!switchdev_work)
2477 netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2478 event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2479 orig_dev->name, fdb_info->addr, fdb_info->vid,
2480 host_addr ? " as host address" : "");
2482 INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2483 switchdev_work->ds = ds;
2484 switchdev_work->port = dp->index;
2485 switchdev_work->event = event;
2486 switchdev_work->dev = dev;
2488 ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2489 switchdev_work->vid = fdb_info->vid;
2490 switchdev_work->host_addr = host_addr;
2492 /* Hold a reference for dsa_fdb_offload_notify */
2494 dsa_schedule_work(&switchdev_work->work);
2500 dsa_slave_fdb_add_to_device(struct net_device *dev,
2501 const struct net_device *orig_dev, const void *ctx,
2502 const struct switchdev_notifier_fdb_info *fdb_info)
2504 return dsa_slave_fdb_event(dev, orig_dev, ctx, fdb_info,
2505 SWITCHDEV_FDB_ADD_TO_DEVICE);
2509 dsa_slave_fdb_del_to_device(struct net_device *dev,
2510 const struct net_device *orig_dev, const void *ctx,
2511 const struct switchdev_notifier_fdb_info *fdb_info)
2513 return dsa_slave_fdb_event(dev, orig_dev, ctx, fdb_info,
2514 SWITCHDEV_FDB_DEL_TO_DEVICE);
2517 /* Called under rcu_read_lock() */
2518 static int dsa_slave_switchdev_event(struct notifier_block *unused,
2519 unsigned long event, void *ptr)
2521 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2525 case SWITCHDEV_PORT_ATTR_SET:
2526 err = switchdev_handle_port_attr_set(dev, ptr,
2527 dsa_slave_dev_check,
2528 dsa_slave_port_attr_set);
2529 return notifier_from_errno(err);
2530 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2531 err = switchdev_handle_fdb_add_to_device(dev, ptr,
2532 dsa_slave_dev_check,
2533 dsa_foreign_dev_check,
2534 dsa_slave_fdb_add_to_device,
2536 return notifier_from_errno(err);
2537 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2538 err = switchdev_handle_fdb_del_to_device(dev, ptr,
2539 dsa_slave_dev_check,
2540 dsa_foreign_dev_check,
2541 dsa_slave_fdb_del_to_device,
2543 return notifier_from_errno(err);
2551 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2552 unsigned long event, void *ptr)
2554 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2558 case SWITCHDEV_PORT_OBJ_ADD:
2559 err = switchdev_handle_port_obj_add(dev, ptr,
2560 dsa_slave_dev_check,
2561 dsa_slave_port_obj_add);
2562 return notifier_from_errno(err);
2563 case SWITCHDEV_PORT_OBJ_DEL:
2564 err = switchdev_handle_port_obj_del(dev, ptr,
2565 dsa_slave_dev_check,
2566 dsa_slave_port_obj_del);
2567 return notifier_from_errno(err);
2568 case SWITCHDEV_PORT_ATTR_SET:
2569 err = switchdev_handle_port_attr_set(dev, ptr,
2570 dsa_slave_dev_check,
2571 dsa_slave_port_attr_set);
2572 return notifier_from_errno(err);
2578 static struct notifier_block dsa_slave_nb __read_mostly = {
2579 .notifier_call = dsa_slave_netdevice_event,
2582 struct notifier_block dsa_slave_switchdev_notifier = {
2583 .notifier_call = dsa_slave_switchdev_event,
2586 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
2587 .notifier_call = dsa_slave_switchdev_blocking_event,
2590 int dsa_slave_register_notifier(void)
2592 struct notifier_block *nb;
2595 err = register_netdevice_notifier(&dsa_slave_nb);
2599 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2601 goto err_switchdev_nb;
2603 nb = &dsa_slave_switchdev_blocking_notifier;
2604 err = register_switchdev_blocking_notifier(nb);
2606 goto err_switchdev_blocking_nb;
2610 err_switchdev_blocking_nb:
2611 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2613 unregister_netdevice_notifier(&dsa_slave_nb);
2617 void dsa_slave_unregister_notifier(void)
2619 struct notifier_block *nb;
2622 nb = &dsa_slave_switchdev_blocking_notifier;
2623 err = unregister_switchdev_blocking_notifier(nb);
2625 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
2627 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2629 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2631 err = unregister_netdevice_notifier(&dsa_slave_nb);
2633 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);