1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/dsa/slave.c - Slave device handling
4 * Copyright (c) 2008-2009 Marvell Semiconductor
7 #include <linux/list.h>
8 #include <linux/etherdevice.h>
9 #include <linux/netdevice.h>
10 #include <linux/phy.h>
11 #include <linux/phy_fixed.h>
12 #include <linux/phylink.h>
13 #include <linux/of_net.h>
14 #include <linux/of_mdio.h>
15 #include <linux/mdio.h>
16 #include <net/rtnetlink.h>
17 #include <net/pkt_cls.h>
18 #include <net/tc_act/tc_mirred.h>
19 #include <linux/if_bridge.h>
20 #include <linux/netpoll.h>
21 #include <linux/ptp_classify.h>
25 static bool dsa_slave_dev_check(const struct net_device *dev);
27 /* slave mii_bus handling ***************************************************/
28 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
30 struct dsa_switch *ds = bus->priv;
32 if (ds->phys_mii_mask & (1 << addr))
33 return ds->ops->phy_read(ds, addr, reg);
38 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
40 struct dsa_switch *ds = bus->priv;
42 if (ds->phys_mii_mask & (1 << addr))
43 return ds->ops->phy_write(ds, addr, reg, val);
48 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
50 ds->slave_mii_bus->priv = (void *)ds;
51 ds->slave_mii_bus->name = "dsa slave smi";
52 ds->slave_mii_bus->read = dsa_slave_phy_read;
53 ds->slave_mii_bus->write = dsa_slave_phy_write;
54 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
55 ds->dst->index, ds->index);
56 ds->slave_mii_bus->parent = ds->dev;
57 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
61 /* slave device handling ****************************************************/
62 static int dsa_slave_get_iflink(const struct net_device *dev)
64 return dsa_slave_to_master(dev)->ifindex;
67 static int dsa_slave_open(struct net_device *dev)
69 struct net_device *master = dsa_slave_to_master(dev);
70 struct dsa_port *dp = dsa_slave_to_port(dev);
73 if (!(master->flags & IFF_UP))
76 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
77 err = dev_uc_add(master, dev->dev_addr);
82 if (dev->flags & IFF_ALLMULTI) {
83 err = dev_set_allmulti(master, 1);
87 if (dev->flags & IFF_PROMISC) {
88 err = dev_set_promiscuity(master, 1);
93 err = dsa_port_enable_rt(dp, dev->phydev);
100 if (dev->flags & IFF_PROMISC)
101 dev_set_promiscuity(master, -1);
103 if (dev->flags & IFF_ALLMULTI)
104 dev_set_allmulti(master, -1);
106 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
107 dev_uc_del(master, dev->dev_addr);
112 static int dsa_slave_close(struct net_device *dev)
114 struct net_device *master = dsa_slave_to_master(dev);
115 struct dsa_port *dp = dsa_slave_to_port(dev);
117 cancel_work_sync(&dp->xmit_work);
118 skb_queue_purge(&dp->xmit_queue);
120 dsa_port_disable_rt(dp);
122 dev_mc_unsync(master, dev);
123 dev_uc_unsync(master, dev);
124 if (dev->flags & IFF_ALLMULTI)
125 dev_set_allmulti(master, -1);
126 if (dev->flags & IFF_PROMISC)
127 dev_set_promiscuity(master, -1);
129 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
130 dev_uc_del(master, dev->dev_addr);
135 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
137 struct net_device *master = dsa_slave_to_master(dev);
138 if (dev->flags & IFF_UP) {
139 if (change & IFF_ALLMULTI)
140 dev_set_allmulti(master,
141 dev->flags & IFF_ALLMULTI ? 1 : -1);
142 if (change & IFF_PROMISC)
143 dev_set_promiscuity(master,
144 dev->flags & IFF_PROMISC ? 1 : -1);
148 static void dsa_slave_set_rx_mode(struct net_device *dev)
150 struct net_device *master = dsa_slave_to_master(dev);
152 dev_mc_sync(master, dev);
153 dev_uc_sync(master, dev);
156 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
158 struct net_device *master = dsa_slave_to_master(dev);
159 struct sockaddr *addr = a;
162 if (!is_valid_ether_addr(addr->sa_data))
163 return -EADDRNOTAVAIL;
165 if (!(dev->flags & IFF_UP))
168 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
169 err = dev_uc_add(master, addr->sa_data);
174 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
175 dev_uc_del(master, dev->dev_addr);
178 ether_addr_copy(dev->dev_addr, addr->sa_data);
183 struct dsa_slave_dump_ctx {
184 struct net_device *dev;
186 struct netlink_callback *cb;
191 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
192 bool is_static, void *data)
194 struct dsa_slave_dump_ctx *dump = data;
195 u32 portid = NETLINK_CB(dump->cb->skb).portid;
196 u32 seq = dump->cb->nlh->nlmsg_seq;
197 struct nlmsghdr *nlh;
200 if (dump->idx < dump->cb->args[2])
203 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
204 sizeof(*ndm), NLM_F_MULTI);
208 ndm = nlmsg_data(nlh);
209 ndm->ndm_family = AF_BRIDGE;
212 ndm->ndm_flags = NTF_SELF;
214 ndm->ndm_ifindex = dump->dev->ifindex;
215 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
217 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
218 goto nla_put_failure;
220 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
221 goto nla_put_failure;
223 nlmsg_end(dump->skb, nlh);
230 nlmsg_cancel(dump->skb, nlh);
235 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
236 struct net_device *dev, struct net_device *filter_dev,
239 struct dsa_port *dp = dsa_slave_to_port(dev);
240 struct dsa_slave_dump_ctx dump = {
248 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
254 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
256 struct dsa_slave_priv *p = netdev_priv(dev);
257 struct dsa_switch *ds = p->dp->ds;
258 int port = p->dp->index;
260 /* Pass through to switch driver if it supports timestamping */
263 if (ds->ops->port_hwtstamp_get)
264 return ds->ops->port_hwtstamp_get(ds, port, ifr);
267 if (ds->ops->port_hwtstamp_set)
268 return ds->ops->port_hwtstamp_set(ds, port, ifr);
272 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
275 static int dsa_slave_port_attr_set(struct net_device *dev,
276 const struct switchdev_attr *attr,
277 struct switchdev_trans *trans)
279 struct dsa_port *dp = dsa_slave_to_port(dev);
283 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
284 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
286 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
287 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
290 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
291 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
293 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
294 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
297 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
298 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
300 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
301 ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans);
311 static int dsa_slave_vlan_add(struct net_device *dev,
312 const struct switchdev_obj *obj,
313 struct switchdev_trans *trans)
315 struct dsa_port *dp = dsa_slave_to_port(dev);
316 struct switchdev_obj_port_vlan vlan;
319 if (obj->orig_dev != dev)
322 if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
325 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
327 err = dsa_port_vlan_add(dp, &vlan, trans);
331 /* We need the dedicated CPU port to be a member of the VLAN as well.
332 * Even though drivers often handle CPU membership in special ways,
333 * it doesn't make sense to program a PVID, so clear this flag.
335 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
337 err = dsa_port_vlan_add(dp->cpu_dp, &vlan, trans);
344 static int dsa_slave_port_obj_add(struct net_device *dev,
345 const struct switchdev_obj *obj,
346 struct switchdev_trans *trans,
347 struct netlink_ext_ack *extack)
349 struct dsa_port *dp = dsa_slave_to_port(dev);
352 /* For the prepare phase, ensure the full set of changes is feasable in
353 * one go in order to signal a failure properly. If an operation is not
354 * supported, return -EOPNOTSUPP.
358 case SWITCHDEV_OBJ_ID_PORT_MDB:
359 if (obj->orig_dev != dev)
361 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
363 case SWITCHDEV_OBJ_ID_HOST_MDB:
364 /* DSA can directly translate this to a normal MDB add,
365 * but on the CPU port.
367 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
370 case SWITCHDEV_OBJ_ID_PORT_VLAN:
371 err = dsa_slave_vlan_add(dev, obj, trans);
381 static int dsa_slave_vlan_del(struct net_device *dev,
382 const struct switchdev_obj *obj)
384 struct dsa_port *dp = dsa_slave_to_port(dev);
386 if (obj->orig_dev != dev)
389 if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
392 /* Do not deprogram the CPU port as it may be shared with other user
393 * ports which can be members of this VLAN as well.
395 return dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
398 static int dsa_slave_port_obj_del(struct net_device *dev,
399 const struct switchdev_obj *obj)
401 struct dsa_port *dp = dsa_slave_to_port(dev);
405 case SWITCHDEV_OBJ_ID_PORT_MDB:
406 if (obj->orig_dev != dev)
408 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
410 case SWITCHDEV_OBJ_ID_HOST_MDB:
411 /* DSA can directly translate this to a normal MDB add,
412 * but on the CPU port.
414 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
416 case SWITCHDEV_OBJ_ID_PORT_VLAN:
417 err = dsa_slave_vlan_del(dev, obj);
427 static int dsa_slave_get_port_parent_id(struct net_device *dev,
428 struct netdev_phys_item_id *ppid)
430 struct dsa_port *dp = dsa_slave_to_port(dev);
431 struct dsa_switch *ds = dp->ds;
432 struct dsa_switch_tree *dst = ds->dst;
434 /* For non-legacy ports, devlink is used and it takes
435 * care of the name generation. This ndo implementation
436 * should be removed with legacy support.
441 ppid->id_len = sizeof(dst->index);
442 memcpy(&ppid->id, &dst->index, ppid->id_len);
447 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
450 #ifdef CONFIG_NET_POLL_CONTROLLER
451 struct dsa_slave_priv *p = netdev_priv(dev);
454 netpoll_send_skb(p->netpoll, skb);
461 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
464 struct dsa_switch *ds = p->dp->ds;
465 struct sk_buff *clone;
468 type = ptp_classify_raw(skb);
469 if (type == PTP_CLASS_NONE)
472 if (!ds->ops->port_txtstamp)
475 clone = skb_clone_sk(skb);
479 DSA_SKB_CB(skb)->clone = clone;
481 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
487 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
489 /* SKB for netpoll still need to be mangled with the protocol-specific
490 * tag to be successfully transmitted
492 if (unlikely(netpoll_tx_running(dev)))
493 return dsa_slave_netpoll_send_skb(dev, skb);
495 /* Queue the SKB for transmission on the parent interface, but
496 * do not modify its EtherType
498 skb->dev = dsa_slave_to_master(dev);
503 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
505 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
507 struct dsa_slave_priv *p = netdev_priv(dev);
508 struct pcpu_sw_netstats *s;
509 struct sk_buff *nskb;
511 s = this_cpu_ptr(p->stats64);
512 u64_stats_update_begin(&s->syncp);
514 s->tx_bytes += skb->len;
515 u64_stats_update_end(&s->syncp);
517 DSA_SKB_CB(skb)->deferred_xmit = false;
518 DSA_SKB_CB(skb)->clone = NULL;
520 /* Identify PTP protocol packets, clone them, and pass them to the
523 dsa_skb_tx_timestamp(p, skb);
525 /* Transmit function may have to reallocate the original SKB,
526 * in which case it must have freed it. Only free it here on error.
528 nskb = p->xmit(skb, dev);
530 if (!DSA_SKB_CB(skb)->deferred_xmit)
535 return dsa_enqueue_skb(nskb, dev);
538 void *dsa_defer_xmit(struct sk_buff *skb, struct net_device *dev)
540 struct dsa_port *dp = dsa_slave_to_port(dev);
542 DSA_SKB_CB(skb)->deferred_xmit = true;
544 skb_queue_tail(&dp->xmit_queue, skb);
545 schedule_work(&dp->xmit_work);
548 EXPORT_SYMBOL_GPL(dsa_defer_xmit);
550 static void dsa_port_xmit_work(struct work_struct *work)
552 struct dsa_port *dp = container_of(work, struct dsa_port, xmit_work);
553 struct dsa_switch *ds = dp->ds;
556 if (unlikely(!ds->ops->port_deferred_xmit))
559 while ((skb = skb_dequeue(&dp->xmit_queue)) != NULL)
560 ds->ops->port_deferred_xmit(ds, dp->index, skb);
563 /* ethtool operations *******************************************************/
565 static void dsa_slave_get_drvinfo(struct net_device *dev,
566 struct ethtool_drvinfo *drvinfo)
568 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
569 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
570 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
573 static int dsa_slave_get_regs_len(struct net_device *dev)
575 struct dsa_port *dp = dsa_slave_to_port(dev);
576 struct dsa_switch *ds = dp->ds;
578 if (ds->ops->get_regs_len)
579 return ds->ops->get_regs_len(ds, dp->index);
585 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
587 struct dsa_port *dp = dsa_slave_to_port(dev);
588 struct dsa_switch *ds = dp->ds;
590 if (ds->ops->get_regs)
591 ds->ops->get_regs(ds, dp->index, regs, _p);
594 static int dsa_slave_nway_reset(struct net_device *dev)
596 struct dsa_port *dp = dsa_slave_to_port(dev);
598 return phylink_ethtool_nway_reset(dp->pl);
601 static int dsa_slave_get_eeprom_len(struct net_device *dev)
603 struct dsa_port *dp = dsa_slave_to_port(dev);
604 struct dsa_switch *ds = dp->ds;
606 if (ds->cd && ds->cd->eeprom_len)
607 return ds->cd->eeprom_len;
609 if (ds->ops->get_eeprom_len)
610 return ds->ops->get_eeprom_len(ds);
615 static int dsa_slave_get_eeprom(struct net_device *dev,
616 struct ethtool_eeprom *eeprom, u8 *data)
618 struct dsa_port *dp = dsa_slave_to_port(dev);
619 struct dsa_switch *ds = dp->ds;
621 if (ds->ops->get_eeprom)
622 return ds->ops->get_eeprom(ds, eeprom, data);
627 static int dsa_slave_set_eeprom(struct net_device *dev,
628 struct ethtool_eeprom *eeprom, u8 *data)
630 struct dsa_port *dp = dsa_slave_to_port(dev);
631 struct dsa_switch *ds = dp->ds;
633 if (ds->ops->set_eeprom)
634 return ds->ops->set_eeprom(ds, eeprom, data);
639 static void dsa_slave_get_strings(struct net_device *dev,
640 uint32_t stringset, uint8_t *data)
642 struct dsa_port *dp = dsa_slave_to_port(dev);
643 struct dsa_switch *ds = dp->ds;
645 if (stringset == ETH_SS_STATS) {
646 int len = ETH_GSTRING_LEN;
648 strncpy(data, "tx_packets", len);
649 strncpy(data + len, "tx_bytes", len);
650 strncpy(data + 2 * len, "rx_packets", len);
651 strncpy(data + 3 * len, "rx_bytes", len);
652 if (ds->ops->get_strings)
653 ds->ops->get_strings(ds, dp->index, stringset,
658 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
659 struct ethtool_stats *stats,
662 struct dsa_port *dp = dsa_slave_to_port(dev);
663 struct dsa_slave_priv *p = netdev_priv(dev);
664 struct dsa_switch *ds = dp->ds;
665 struct pcpu_sw_netstats *s;
669 for_each_possible_cpu(i) {
670 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
672 s = per_cpu_ptr(p->stats64, i);
674 start = u64_stats_fetch_begin_irq(&s->syncp);
675 tx_packets = s->tx_packets;
676 tx_bytes = s->tx_bytes;
677 rx_packets = s->rx_packets;
678 rx_bytes = s->rx_bytes;
679 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
680 data[0] += tx_packets;
682 data[2] += rx_packets;
685 if (ds->ops->get_ethtool_stats)
686 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
689 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
691 struct dsa_port *dp = dsa_slave_to_port(dev);
692 struct dsa_switch *ds = dp->ds;
694 if (sset == ETH_SS_STATS) {
697 if (ds->ops->get_sset_count) {
698 count = ds->ops->get_sset_count(ds, dp->index, sset);
709 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
711 struct dsa_port *dp = dsa_slave_to_port(dev);
712 struct dsa_switch *ds = dp->ds;
714 phylink_ethtool_get_wol(dp->pl, w);
716 if (ds->ops->get_wol)
717 ds->ops->get_wol(ds, dp->index, w);
720 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
722 struct dsa_port *dp = dsa_slave_to_port(dev);
723 struct dsa_switch *ds = dp->ds;
724 int ret = -EOPNOTSUPP;
726 phylink_ethtool_set_wol(dp->pl, w);
728 if (ds->ops->set_wol)
729 ret = ds->ops->set_wol(ds, dp->index, w);
734 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
736 struct dsa_port *dp = dsa_slave_to_port(dev);
737 struct dsa_switch *ds = dp->ds;
740 /* Port's PHY and MAC both need to be EEE capable */
741 if (!dev->phydev || !dp->pl)
744 if (!ds->ops->set_mac_eee)
747 ret = ds->ops->set_mac_eee(ds, dp->index, e);
751 return phylink_ethtool_set_eee(dp->pl, e);
754 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
756 struct dsa_port *dp = dsa_slave_to_port(dev);
757 struct dsa_switch *ds = dp->ds;
760 /* Port's PHY and MAC both need to be EEE capable */
761 if (!dev->phydev || !dp->pl)
764 if (!ds->ops->get_mac_eee)
767 ret = ds->ops->get_mac_eee(ds, dp->index, e);
771 return phylink_ethtool_get_eee(dp->pl, e);
774 static int dsa_slave_get_link_ksettings(struct net_device *dev,
775 struct ethtool_link_ksettings *cmd)
777 struct dsa_port *dp = dsa_slave_to_port(dev);
779 return phylink_ethtool_ksettings_get(dp->pl, cmd);
782 static int dsa_slave_set_link_ksettings(struct net_device *dev,
783 const struct ethtool_link_ksettings *cmd)
785 struct dsa_port *dp = dsa_slave_to_port(dev);
787 return phylink_ethtool_ksettings_set(dp->pl, cmd);
790 #ifdef CONFIG_NET_POLL_CONTROLLER
791 static int dsa_slave_netpoll_setup(struct net_device *dev,
792 struct netpoll_info *ni)
794 struct net_device *master = dsa_slave_to_master(dev);
795 struct dsa_slave_priv *p = netdev_priv(dev);
796 struct netpoll *netpoll;
799 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
803 err = __netpoll_setup(netpoll, master);
809 p->netpoll = netpoll;
814 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
816 struct dsa_slave_priv *p = netdev_priv(dev);
817 struct netpoll *netpoll = p->netpoll;
824 __netpoll_free(netpoll);
827 static void dsa_slave_poll_controller(struct net_device *dev)
832 static int dsa_slave_get_phys_port_name(struct net_device *dev,
833 char *name, size_t len)
835 struct dsa_port *dp = dsa_slave_to_port(dev);
837 /* For non-legacy ports, devlink is used and it takes
838 * care of the name generation. This ndo implementation
839 * should be removed with legacy support.
844 if (snprintf(name, len, "p%d", dp->index) >= len)
850 static struct dsa_mall_tc_entry *
851 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
853 struct dsa_slave_priv *p = netdev_priv(dev);
854 struct dsa_mall_tc_entry *mall_tc_entry;
856 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
857 if (mall_tc_entry->cookie == cookie)
858 return mall_tc_entry;
863 static int dsa_slave_add_cls_matchall(struct net_device *dev,
864 struct tc_cls_matchall_offload *cls,
867 struct dsa_port *dp = dsa_slave_to_port(dev);
868 struct dsa_slave_priv *p = netdev_priv(dev);
869 struct dsa_mall_tc_entry *mall_tc_entry;
870 __be16 protocol = cls->common.protocol;
871 struct dsa_switch *ds = dp->ds;
872 struct flow_action_entry *act;
873 struct dsa_port *to_dp;
874 int err = -EOPNOTSUPP;
876 if (!ds->ops->port_mirror_add)
879 if (!flow_offload_has_one_action(&cls->rule->action))
882 act = &cls->rule->action.entries[0];
884 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
885 struct dsa_mall_mirror_tc_entry *mirror;
890 if (!dsa_slave_dev_check(act->dev))
893 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
897 mall_tc_entry->cookie = cls->cookie;
898 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
899 mirror = &mall_tc_entry->mirror;
901 to_dp = dsa_slave_to_port(act->dev);
903 mirror->to_local_port = to_dp->index;
904 mirror->ingress = ingress;
906 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
908 kfree(mall_tc_entry);
912 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
918 static void dsa_slave_del_cls_matchall(struct net_device *dev,
919 struct tc_cls_matchall_offload *cls)
921 struct dsa_port *dp = dsa_slave_to_port(dev);
922 struct dsa_mall_tc_entry *mall_tc_entry;
923 struct dsa_switch *ds = dp->ds;
925 if (!ds->ops->port_mirror_del)
928 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
932 list_del(&mall_tc_entry->list);
934 switch (mall_tc_entry->type) {
935 case DSA_PORT_MALL_MIRROR:
936 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
942 kfree(mall_tc_entry);
945 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
946 struct tc_cls_matchall_offload *cls,
949 if (cls->common.chain_index)
952 switch (cls->command) {
953 case TC_CLSMATCHALL_REPLACE:
954 return dsa_slave_add_cls_matchall(dev, cls, ingress);
955 case TC_CLSMATCHALL_DESTROY:
956 dsa_slave_del_cls_matchall(dev, cls);
963 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
964 void *cb_priv, bool ingress)
966 struct net_device *dev = cb_priv;
968 if (!tc_can_offload(dev))
972 case TC_SETUP_CLSMATCHALL:
973 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
979 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
980 void *type_data, void *cb_priv)
982 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
985 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
986 void *type_data, void *cb_priv)
988 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
991 static LIST_HEAD(dsa_slave_block_cb_list);
993 static int dsa_slave_setup_tc_block(struct net_device *dev,
994 struct flow_block_offload *f)
996 struct flow_block_cb *block_cb;
999 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1000 cb = dsa_slave_setup_tc_block_cb_ig;
1001 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1002 cb = dsa_slave_setup_tc_block_cb_eg;
1006 f->driver_block_list = &dsa_slave_block_cb_list;
1008 switch (f->command) {
1009 case FLOW_BLOCK_BIND:
1010 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1013 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1014 if (IS_ERR(block_cb))
1015 return PTR_ERR(block_cb);
1017 flow_block_cb_add(block_cb, f);
1018 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1020 case FLOW_BLOCK_UNBIND:
1021 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1025 flow_block_cb_remove(block_cb, f);
1026 list_del(&block_cb->driver_list);
1033 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1036 struct dsa_port *dp = dsa_slave_to_port(dev);
1037 struct dsa_switch *ds = dp->ds;
1039 if (type == TC_SETUP_BLOCK)
1040 return dsa_slave_setup_tc_block(dev, type_data);
1042 if (!ds->ops->port_setup_tc)
1045 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1048 static void dsa_slave_get_stats64(struct net_device *dev,
1049 struct rtnl_link_stats64 *stats)
1051 struct dsa_slave_priv *p = netdev_priv(dev);
1052 struct pcpu_sw_netstats *s;
1056 netdev_stats_to_stats64(stats, &dev->stats);
1057 for_each_possible_cpu(i) {
1058 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
1060 s = per_cpu_ptr(p->stats64, i);
1062 start = u64_stats_fetch_begin_irq(&s->syncp);
1063 tx_packets = s->tx_packets;
1064 tx_bytes = s->tx_bytes;
1065 rx_packets = s->rx_packets;
1066 rx_bytes = s->rx_bytes;
1067 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
1069 stats->tx_packets += tx_packets;
1070 stats->tx_bytes += tx_bytes;
1071 stats->rx_packets += rx_packets;
1072 stats->rx_bytes += rx_bytes;
1076 static int dsa_slave_get_rxnfc(struct net_device *dev,
1077 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1079 struct dsa_port *dp = dsa_slave_to_port(dev);
1080 struct dsa_switch *ds = dp->ds;
1082 if (!ds->ops->get_rxnfc)
1085 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1088 static int dsa_slave_set_rxnfc(struct net_device *dev,
1089 struct ethtool_rxnfc *nfc)
1091 struct dsa_port *dp = dsa_slave_to_port(dev);
1092 struct dsa_switch *ds = dp->ds;
1094 if (!ds->ops->set_rxnfc)
1097 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1100 static int dsa_slave_get_ts_info(struct net_device *dev,
1101 struct ethtool_ts_info *ts)
1103 struct dsa_slave_priv *p = netdev_priv(dev);
1104 struct dsa_switch *ds = p->dp->ds;
1106 if (!ds->ops->get_ts_info)
1109 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1112 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1115 struct dsa_port *dp = dsa_slave_to_port(dev);
1116 struct bridge_vlan_info info;
1119 /* Check for a possible bridge VLAN entry now since there is no
1120 * need to emulate the switchdev prepare + commit phase.
1122 if (dp->bridge_dev) {
1123 if (!br_vlan_enabled(dp->bridge_dev))
1126 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1127 * device, respectively the VID is not found, returning
1128 * 0 means success, which is a failure for us here.
1130 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1135 ret = dsa_port_vid_add(dp, vid, 0);
1139 ret = dsa_port_vid_add(dp->cpu_dp, vid, 0);
1146 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1149 struct dsa_port *dp = dsa_slave_to_port(dev);
1150 struct bridge_vlan_info info;
1153 /* Check for a possible bridge VLAN entry now since there is no
1154 * need to emulate the switchdev prepare + commit phase.
1156 if (dp->bridge_dev) {
1157 if (!br_vlan_enabled(dp->bridge_dev))
1160 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1161 * device, respectively the VID is not found, returning
1162 * 0 means success, which is a failure for us here.
1164 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1169 /* Do not deprogram the CPU port as it may be shared with other user
1170 * ports which can be members of this VLAN as well.
1172 return dsa_port_vid_del(dp, vid);
1175 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1176 .get_drvinfo = dsa_slave_get_drvinfo,
1177 .get_regs_len = dsa_slave_get_regs_len,
1178 .get_regs = dsa_slave_get_regs,
1179 .nway_reset = dsa_slave_nway_reset,
1180 .get_link = ethtool_op_get_link,
1181 .get_eeprom_len = dsa_slave_get_eeprom_len,
1182 .get_eeprom = dsa_slave_get_eeprom,
1183 .set_eeprom = dsa_slave_set_eeprom,
1184 .get_strings = dsa_slave_get_strings,
1185 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1186 .get_sset_count = dsa_slave_get_sset_count,
1187 .set_wol = dsa_slave_set_wol,
1188 .get_wol = dsa_slave_get_wol,
1189 .set_eee = dsa_slave_set_eee,
1190 .get_eee = dsa_slave_get_eee,
1191 .get_link_ksettings = dsa_slave_get_link_ksettings,
1192 .set_link_ksettings = dsa_slave_set_link_ksettings,
1193 .get_rxnfc = dsa_slave_get_rxnfc,
1194 .set_rxnfc = dsa_slave_set_rxnfc,
1195 .get_ts_info = dsa_slave_get_ts_info,
1198 /* legacy way, bypassing the bridge *****************************************/
1199 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1200 struct net_device *dev,
1201 const unsigned char *addr, u16 vid,
1203 struct netlink_ext_ack *extack)
1205 struct dsa_port *dp = dsa_slave_to_port(dev);
1207 return dsa_port_fdb_add(dp, addr, vid);
1210 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1211 struct net_device *dev,
1212 const unsigned char *addr, u16 vid)
1214 struct dsa_port *dp = dsa_slave_to_port(dev);
1216 return dsa_port_fdb_del(dp, addr, vid);
1219 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1221 struct dsa_port *dp = dsa_slave_to_port(dev);
1223 return dp->ds->devlink ? &dp->devlink_port : NULL;
1226 static const struct net_device_ops dsa_slave_netdev_ops = {
1227 .ndo_open = dsa_slave_open,
1228 .ndo_stop = dsa_slave_close,
1229 .ndo_start_xmit = dsa_slave_xmit,
1230 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1231 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1232 .ndo_set_mac_address = dsa_slave_set_mac_address,
1233 .ndo_fdb_add = dsa_legacy_fdb_add,
1234 .ndo_fdb_del = dsa_legacy_fdb_del,
1235 .ndo_fdb_dump = dsa_slave_fdb_dump,
1236 .ndo_do_ioctl = dsa_slave_ioctl,
1237 .ndo_get_iflink = dsa_slave_get_iflink,
1238 #ifdef CONFIG_NET_POLL_CONTROLLER
1239 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1240 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1241 .ndo_poll_controller = dsa_slave_poll_controller,
1243 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1244 .ndo_setup_tc = dsa_slave_setup_tc,
1245 .ndo_get_stats64 = dsa_slave_get_stats64,
1246 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1247 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1248 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1249 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1252 static struct device_type dsa_type = {
1256 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1258 const struct dsa_port *dp = dsa_to_port(ds, port);
1260 phylink_mac_change(dp->pl, up);
1262 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1264 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1265 struct phylink_link_state *state)
1267 struct dsa_port *dp = dsa_slave_to_port(dev);
1268 struct dsa_switch *ds = dp->ds;
1270 /* No need to check that this operation is valid, the callback would
1271 * not be called if it was not.
1273 ds->ops->phylink_fixed_state(ds, dp->index, state);
1276 /* slave device setup *******************************************************/
1277 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1279 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1280 struct dsa_switch *ds = dp->ds;
1282 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1283 if (!slave_dev->phydev) {
1284 netdev_err(slave_dev, "no phy at %d\n", addr);
1288 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1291 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1293 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1294 struct device_node *port_dn = dp->dn;
1295 struct dsa_switch *ds = dp->ds;
1299 mode = of_get_phy_mode(port_dn);
1301 mode = PHY_INTERFACE_MODE_NA;
1303 dp->pl_config.dev = &slave_dev->dev;
1304 dp->pl_config.type = PHYLINK_NETDEV;
1306 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1307 &dsa_port_phylink_mac_ops);
1308 if (IS_ERR(dp->pl)) {
1309 netdev_err(slave_dev,
1310 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1311 return PTR_ERR(dp->pl);
1314 /* Register only if the switch provides such a callback, since this
1315 * callback takes precedence over polling the link GPIO in PHYLINK
1316 * (see phylink_get_fixed_state).
1318 if (ds->ops->phylink_fixed_state)
1319 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1321 if (ds->ops->get_phy_flags)
1322 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1324 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1325 if (ret == -ENODEV && ds->slave_mii_bus) {
1326 /* We could not connect to a designated PHY or SFP, so try to
1327 * use the switch internal MDIO bus instead
1329 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1332 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
1334 phylink_destroy(dp->pl);
1340 int dsa_slave_suspend(struct net_device *slave_dev)
1342 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1344 if (!netif_running(slave_dev))
1347 cancel_work_sync(&dp->xmit_work);
1348 skb_queue_purge(&dp->xmit_queue);
1350 netif_device_detach(slave_dev);
1353 phylink_stop(dp->pl);
1359 int dsa_slave_resume(struct net_device *slave_dev)
1361 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1363 if (!netif_running(slave_dev))
1366 netif_device_attach(slave_dev);
1369 phylink_start(dp->pl);
1375 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1377 struct net_device *master = dsa_slave_to_master(dev);
1378 struct dsa_port *dp = dsa_slave_to_port(dev);
1379 struct dsa_notifier_register_info rinfo = {
1380 .switch_number = dp->ds->index,
1381 .port_number = dp->index,
1386 call_dsa_notifiers(val, dev, &rinfo.info);
1389 int dsa_slave_create(struct dsa_port *port)
1391 const struct dsa_port *cpu_dp = port->cpu_dp;
1392 struct net_device *master = cpu_dp->master;
1393 struct dsa_switch *ds = port->ds;
1394 const char *name = port->name;
1395 struct net_device *slave_dev;
1396 struct dsa_slave_priv *p;
1399 if (!ds->num_tx_queues)
1400 ds->num_tx_queues = 1;
1402 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1403 NET_NAME_UNKNOWN, ether_setup,
1404 ds->num_tx_queues, 1);
1405 if (slave_dev == NULL)
1408 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1409 if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
1410 slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1411 slave_dev->hw_features |= NETIF_F_HW_TC;
1412 slave_dev->features |= NETIF_F_LLTX;
1413 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1414 if (!IS_ERR_OR_NULL(port->mac))
1415 ether_addr_copy(slave_dev->dev_addr, port->mac);
1417 eth_hw_addr_inherit(slave_dev, master);
1418 slave_dev->priv_flags |= IFF_NO_QUEUE;
1419 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1420 slave_dev->min_mtu = 0;
1421 slave_dev->max_mtu = ETH_MAX_MTU;
1422 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1424 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1425 slave_dev->dev.of_node = port->dn;
1426 slave_dev->vlan_features = master->vlan_features;
1428 p = netdev_priv(slave_dev);
1429 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1431 free_netdev(slave_dev);
1435 ret = gro_cells_init(&p->gcells, slave_dev);
1440 INIT_LIST_HEAD(&p->mall_tc_list);
1441 INIT_WORK(&port->xmit_work, dsa_port_xmit_work);
1442 skb_queue_head_init(&port->xmit_queue);
1443 p->xmit = cpu_dp->tag_ops->xmit;
1444 port->slave = slave_dev;
1446 netif_carrier_off(slave_dev);
1448 ret = dsa_slave_phy_setup(slave_dev);
1450 netdev_err(master, "error %d setting up slave phy\n", ret);
1454 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1456 ret = register_netdev(slave_dev);
1458 netdev_err(master, "error %d registering interface %s\n",
1459 ret, slave_dev->name);
1467 phylink_disconnect_phy(p->dp->pl);
1469 phylink_destroy(p->dp->pl);
1471 gro_cells_destroy(&p->gcells);
1473 free_percpu(p->stats64);
1474 free_netdev(slave_dev);
1479 void dsa_slave_destroy(struct net_device *slave_dev)
1481 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1482 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1484 netif_carrier_off(slave_dev);
1486 phylink_disconnect_phy(dp->pl);
1489 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1490 unregister_netdev(slave_dev);
1491 phylink_destroy(dp->pl);
1492 gro_cells_destroy(&p->gcells);
1493 free_percpu(p->stats64);
1494 free_netdev(slave_dev);
1497 static bool dsa_slave_dev_check(const struct net_device *dev)
1499 return dev->netdev_ops == &dsa_slave_netdev_ops;
1502 static int dsa_slave_changeupper(struct net_device *dev,
1503 struct netdev_notifier_changeupper_info *info)
1505 struct dsa_port *dp = dsa_slave_to_port(dev);
1506 int err = NOTIFY_DONE;
1508 if (netif_is_bridge_master(info->upper_dev)) {
1509 if (info->linking) {
1510 err = dsa_port_bridge_join(dp, info->upper_dev);
1511 err = notifier_from_errno(err);
1513 dsa_port_bridge_leave(dp, info->upper_dev);
1521 static int dsa_slave_upper_vlan_check(struct net_device *dev,
1522 struct netdev_notifier_changeupper_info *
1525 struct netlink_ext_ack *ext_ack;
1526 struct net_device *slave;
1527 struct dsa_port *dp;
1529 ext_ack = netdev_notifier_info_to_extack(&info->info);
1531 if (!is_vlan_dev(dev))
1534 slave = vlan_dev_real_dev(dev);
1535 if (!dsa_slave_dev_check(slave))
1538 dp = dsa_slave_to_port(slave);
1539 if (!dp->bridge_dev)
1542 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1543 if (br_vlan_enabled(dp->bridge_dev) &&
1544 netif_is_bridge_master(info->upper_dev) && info->linking) {
1545 NL_SET_ERR_MSG_MOD(ext_ack,
1546 "Cannot enslave VLAN device into VLAN aware bridge");
1547 return notifier_from_errno(-EINVAL);
1553 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1554 unsigned long event, void *ptr)
1556 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1558 if (event == NETDEV_CHANGEUPPER) {
1559 if (!dsa_slave_dev_check(dev))
1560 return dsa_slave_upper_vlan_check(dev, ptr);
1562 return dsa_slave_changeupper(dev, ptr);
1568 struct dsa_switchdev_event_work {
1569 struct work_struct work;
1570 struct switchdev_notifier_fdb_info fdb_info;
1571 struct net_device *dev;
1572 unsigned long event;
1575 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1577 struct dsa_switchdev_event_work *switchdev_work =
1578 container_of(work, struct dsa_switchdev_event_work, work);
1579 struct net_device *dev = switchdev_work->dev;
1580 struct switchdev_notifier_fdb_info *fdb_info;
1581 struct dsa_port *dp = dsa_slave_to_port(dev);
1585 switch (switchdev_work->event) {
1586 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1587 fdb_info = &switchdev_work->fdb_info;
1588 if (!fdb_info->added_by_user)
1591 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1593 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1596 fdb_info->offloaded = true;
1597 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1598 &fdb_info->info, NULL);
1601 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1602 fdb_info = &switchdev_work->fdb_info;
1603 if (!fdb_info->added_by_user)
1606 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1608 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1615 kfree(switchdev_work->fdb_info.addr);
1616 kfree(switchdev_work);
1621 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1623 const struct switchdev_notifier_fdb_info *
1626 memcpy(&switchdev_work->fdb_info, fdb_info,
1627 sizeof(switchdev_work->fdb_info));
1628 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1629 if (!switchdev_work->fdb_info.addr)
1631 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1636 /* Called under rcu_read_lock() */
1637 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1638 unsigned long event, void *ptr)
1640 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1641 struct dsa_switchdev_event_work *switchdev_work;
1644 if (event == SWITCHDEV_PORT_ATTR_SET) {
1645 err = switchdev_handle_port_attr_set(dev, ptr,
1646 dsa_slave_dev_check,
1647 dsa_slave_port_attr_set);
1648 return notifier_from_errno(err);
1651 if (!dsa_slave_dev_check(dev))
1654 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1655 if (!switchdev_work)
1658 INIT_WORK(&switchdev_work->work,
1659 dsa_slave_switchdev_event_work);
1660 switchdev_work->dev = dev;
1661 switchdev_work->event = event;
1664 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1665 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1666 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1667 goto err_fdb_work_init;
1671 kfree(switchdev_work);
1675 dsa_schedule_work(&switchdev_work->work);
1679 kfree(switchdev_work);
1683 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1684 unsigned long event, void *ptr)
1686 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1690 case SWITCHDEV_PORT_OBJ_ADD:
1691 err = switchdev_handle_port_obj_add(dev, ptr,
1692 dsa_slave_dev_check,
1693 dsa_slave_port_obj_add);
1694 return notifier_from_errno(err);
1695 case SWITCHDEV_PORT_OBJ_DEL:
1696 err = switchdev_handle_port_obj_del(dev, ptr,
1697 dsa_slave_dev_check,
1698 dsa_slave_port_obj_del);
1699 return notifier_from_errno(err);
1700 case SWITCHDEV_PORT_ATTR_SET:
1701 err = switchdev_handle_port_attr_set(dev, ptr,
1702 dsa_slave_dev_check,
1703 dsa_slave_port_attr_set);
1704 return notifier_from_errno(err);
1710 static struct notifier_block dsa_slave_nb __read_mostly = {
1711 .notifier_call = dsa_slave_netdevice_event,
1714 static struct notifier_block dsa_slave_switchdev_notifier = {
1715 .notifier_call = dsa_slave_switchdev_event,
1718 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1719 .notifier_call = dsa_slave_switchdev_blocking_event,
1722 int dsa_slave_register_notifier(void)
1724 struct notifier_block *nb;
1727 err = register_netdevice_notifier(&dsa_slave_nb);
1731 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1733 goto err_switchdev_nb;
1735 nb = &dsa_slave_switchdev_blocking_notifier;
1736 err = register_switchdev_blocking_notifier(nb);
1738 goto err_switchdev_blocking_nb;
1742 err_switchdev_blocking_nb:
1743 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1745 unregister_netdevice_notifier(&dsa_slave_nb);
1749 void dsa_slave_unregister_notifier(void)
1751 struct notifier_block *nb;
1754 nb = &dsa_slave_switchdev_blocking_notifier;
1755 err = unregister_switchdev_blocking_notifier(nb);
1757 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1759 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1761 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1763 err = unregister_netdevice_notifier(&dsa_slave_nb);
1765 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);