1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/dsa/slave.c - Slave device handling
4 * Copyright (c) 2008-2009 Marvell Semiconductor
7 #include <linux/list.h>
8 #include <linux/etherdevice.h>
9 #include <linux/netdevice.h>
10 #include <linux/phy.h>
11 #include <linux/phy_fixed.h>
12 #include <linux/phylink.h>
13 #include <linux/of_net.h>
14 #include <linux/of_mdio.h>
15 #include <linux/mdio.h>
16 #include <net/rtnetlink.h>
17 #include <net/pkt_cls.h>
18 #include <net/tc_act/tc_mirred.h>
19 #include <linux/if_bridge.h>
20 #include <linux/netpoll.h>
21 #include <linux/ptp_classify.h>
25 /* slave mii_bus handling ***************************************************/
26 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
28 struct dsa_switch *ds = bus->priv;
30 if (ds->phys_mii_mask & (1 << addr))
31 return ds->ops->phy_read(ds, addr, reg);
36 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
38 struct dsa_switch *ds = bus->priv;
40 if (ds->phys_mii_mask & (1 << addr))
41 return ds->ops->phy_write(ds, addr, reg, val);
46 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
48 ds->slave_mii_bus->priv = (void *)ds;
49 ds->slave_mii_bus->name = "dsa slave smi";
50 ds->slave_mii_bus->read = dsa_slave_phy_read;
51 ds->slave_mii_bus->write = dsa_slave_phy_write;
52 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
53 ds->dst->index, ds->index);
54 ds->slave_mii_bus->parent = ds->dev;
55 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
59 /* slave device handling ****************************************************/
60 static int dsa_slave_get_iflink(const struct net_device *dev)
62 return dsa_slave_to_master(dev)->ifindex;
65 static int dsa_slave_open(struct net_device *dev)
67 struct net_device *master = dsa_slave_to_master(dev);
68 struct dsa_port *dp = dsa_slave_to_port(dev);
71 if (!(master->flags & IFF_UP))
74 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
75 err = dev_uc_add(master, dev->dev_addr);
80 if (dev->flags & IFF_ALLMULTI) {
81 err = dev_set_allmulti(master, 1);
85 if (dev->flags & IFF_PROMISC) {
86 err = dev_set_promiscuity(master, 1);
91 err = dsa_port_enable(dp, dev->phydev);
95 phylink_start(dp->pl);
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 phylink_stop(dp->pl);
119 dsa_port_disable(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 ether_addr_copy(dev->dev_addr, 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,
275 const struct switchdev_attr *attr,
276 struct switchdev_trans *trans)
278 struct dsa_port *dp = dsa_slave_to_port(dev);
282 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
283 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
285 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
286 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
289 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
290 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
292 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
293 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
296 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
297 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
299 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
300 ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans);
310 static int dsa_slave_vlan_add(struct net_device *dev,
311 const struct switchdev_obj *obj,
312 struct switchdev_trans *trans)
314 struct dsa_port *dp = dsa_slave_to_port(dev);
315 struct switchdev_obj_port_vlan vlan;
318 if (obj->orig_dev != dev)
321 if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
324 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
326 err = dsa_port_vlan_add(dp, &vlan, trans);
330 /* We need the dedicated CPU port to be a member of the VLAN as well.
331 * Even though drivers often handle CPU membership in special ways,
332 * it doesn't make sense to program a PVID, so clear this flag.
334 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
336 err = dsa_port_vlan_add(dp->cpu_dp, &vlan, trans);
343 static int dsa_slave_port_obj_add(struct net_device *dev,
344 const struct switchdev_obj *obj,
345 struct switchdev_trans *trans,
346 struct netlink_ext_ack *extack)
348 struct dsa_port *dp = dsa_slave_to_port(dev);
351 /* For the prepare phase, ensure the full set of changes is feasable in
352 * one go in order to signal a failure properly. If an operation is not
353 * supported, return -EOPNOTSUPP.
357 case SWITCHDEV_OBJ_ID_PORT_MDB:
358 if (obj->orig_dev != dev)
360 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
362 case SWITCHDEV_OBJ_ID_HOST_MDB:
363 /* DSA can directly translate this to a normal MDB add,
364 * but on the CPU port.
366 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
369 case SWITCHDEV_OBJ_ID_PORT_VLAN:
370 err = dsa_slave_vlan_add(dev, obj, trans);
380 static int dsa_slave_vlan_del(struct net_device *dev,
381 const struct switchdev_obj *obj)
383 struct dsa_port *dp = dsa_slave_to_port(dev);
385 if (obj->orig_dev != dev)
388 if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
391 /* Do not deprogram the CPU port as it may be shared with other user
392 * ports which can be members of this VLAN as well.
394 return dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
397 static int dsa_slave_port_obj_del(struct net_device *dev,
398 const struct switchdev_obj *obj)
400 struct dsa_port *dp = dsa_slave_to_port(dev);
404 case SWITCHDEV_OBJ_ID_PORT_MDB:
405 if (obj->orig_dev != dev)
407 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
409 case SWITCHDEV_OBJ_ID_HOST_MDB:
410 /* DSA can directly translate this to a normal MDB add,
411 * but on the CPU port.
413 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
415 case SWITCHDEV_OBJ_ID_PORT_VLAN:
416 err = dsa_slave_vlan_del(dev, obj);
426 static int dsa_slave_get_port_parent_id(struct net_device *dev,
427 struct netdev_phys_item_id *ppid)
429 struct dsa_port *dp = dsa_slave_to_port(dev);
430 struct dsa_switch *ds = dp->ds;
431 struct dsa_switch_tree *dst = ds->dst;
433 /* For non-legacy ports, devlink is used and it takes
434 * care of the name generation. This ndo implementation
435 * should be removed with legacy support.
440 ppid->id_len = sizeof(dst->index);
441 memcpy(&ppid->id, &dst->index, ppid->id_len);
446 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
449 #ifdef CONFIG_NET_POLL_CONTROLLER
450 struct dsa_slave_priv *p = netdev_priv(dev);
453 netpoll_send_skb(p->netpoll, skb);
460 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
463 struct dsa_switch *ds = p->dp->ds;
464 struct sk_buff *clone;
467 type = ptp_classify_raw(skb);
468 if (type == PTP_CLASS_NONE)
471 if (!ds->ops->port_txtstamp)
474 clone = skb_clone_sk(skb);
478 DSA_SKB_CB(skb)->clone = clone;
480 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
486 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
488 /* SKB for netpoll still need to be mangled with the protocol-specific
489 * tag to be successfully transmitted
491 if (unlikely(netpoll_tx_running(dev)))
492 return dsa_slave_netpoll_send_skb(dev, skb);
494 /* Queue the SKB for transmission on the parent interface, but
495 * do not modify its EtherType
497 skb->dev = dsa_slave_to_master(dev);
502 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
504 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
506 struct dsa_slave_priv *p = netdev_priv(dev);
507 struct pcpu_sw_netstats *s;
508 struct sk_buff *nskb;
510 s = this_cpu_ptr(p->stats64);
511 u64_stats_update_begin(&s->syncp);
513 s->tx_bytes += skb->len;
514 u64_stats_update_end(&s->syncp);
516 DSA_SKB_CB(skb)->clone = NULL;
518 /* Identify PTP protocol packets, clone them, and pass them to the
521 dsa_skb_tx_timestamp(p, skb);
523 /* Transmit function may have to reallocate the original SKB,
524 * in which case it must have freed it. Only free it here on error.
526 nskb = p->xmit(skb, dev);
532 return dsa_enqueue_skb(nskb, dev);
535 /* ethtool operations *******************************************************/
537 static void dsa_slave_get_drvinfo(struct net_device *dev,
538 struct ethtool_drvinfo *drvinfo)
540 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
541 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
542 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
545 static int dsa_slave_get_regs_len(struct net_device *dev)
547 struct dsa_port *dp = dsa_slave_to_port(dev);
548 struct dsa_switch *ds = dp->ds;
550 if (ds->ops->get_regs_len)
551 return ds->ops->get_regs_len(ds, dp->index);
557 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
559 struct dsa_port *dp = dsa_slave_to_port(dev);
560 struct dsa_switch *ds = dp->ds;
562 if (ds->ops->get_regs)
563 ds->ops->get_regs(ds, dp->index, regs, _p);
566 static int dsa_slave_nway_reset(struct net_device *dev)
568 struct dsa_port *dp = dsa_slave_to_port(dev);
570 return phylink_ethtool_nway_reset(dp->pl);
573 static int dsa_slave_get_eeprom_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->cd && ds->cd->eeprom_len)
579 return ds->cd->eeprom_len;
581 if (ds->ops->get_eeprom_len)
582 return ds->ops->get_eeprom_len(ds);
587 static int dsa_slave_get_eeprom(struct net_device *dev,
588 struct ethtool_eeprom *eeprom, u8 *data)
590 struct dsa_port *dp = dsa_slave_to_port(dev);
591 struct dsa_switch *ds = dp->ds;
593 if (ds->ops->get_eeprom)
594 return ds->ops->get_eeprom(ds, eeprom, data);
599 static int dsa_slave_set_eeprom(struct net_device *dev,
600 struct ethtool_eeprom *eeprom, u8 *data)
602 struct dsa_port *dp = dsa_slave_to_port(dev);
603 struct dsa_switch *ds = dp->ds;
605 if (ds->ops->set_eeprom)
606 return ds->ops->set_eeprom(ds, eeprom, data);
611 static void dsa_slave_get_strings(struct net_device *dev,
612 uint32_t stringset, uint8_t *data)
614 struct dsa_port *dp = dsa_slave_to_port(dev);
615 struct dsa_switch *ds = dp->ds;
617 if (stringset == ETH_SS_STATS) {
618 int len = ETH_GSTRING_LEN;
620 strncpy(data, "tx_packets", len);
621 strncpy(data + len, "tx_bytes", len);
622 strncpy(data + 2 * len, "rx_packets", len);
623 strncpy(data + 3 * len, "rx_bytes", len);
624 if (ds->ops->get_strings)
625 ds->ops->get_strings(ds, dp->index, stringset,
630 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
631 struct ethtool_stats *stats,
634 struct dsa_port *dp = dsa_slave_to_port(dev);
635 struct dsa_slave_priv *p = netdev_priv(dev);
636 struct dsa_switch *ds = dp->ds;
637 struct pcpu_sw_netstats *s;
641 for_each_possible_cpu(i) {
642 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
644 s = per_cpu_ptr(p->stats64, i);
646 start = u64_stats_fetch_begin_irq(&s->syncp);
647 tx_packets = s->tx_packets;
648 tx_bytes = s->tx_bytes;
649 rx_packets = s->rx_packets;
650 rx_bytes = s->rx_bytes;
651 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
652 data[0] += tx_packets;
654 data[2] += rx_packets;
657 if (ds->ops->get_ethtool_stats)
658 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
661 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
663 struct dsa_port *dp = dsa_slave_to_port(dev);
664 struct dsa_switch *ds = dp->ds;
666 if (sset == ETH_SS_STATS) {
670 if (ds->ops->get_sset_count)
671 count += ds->ops->get_sset_count(ds, dp->index, sset);
679 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
681 struct dsa_port *dp = dsa_slave_to_port(dev);
682 struct dsa_switch *ds = dp->ds;
684 phylink_ethtool_get_wol(dp->pl, w);
686 if (ds->ops->get_wol)
687 ds->ops->get_wol(ds, dp->index, w);
690 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
692 struct dsa_port *dp = dsa_slave_to_port(dev);
693 struct dsa_switch *ds = dp->ds;
694 int ret = -EOPNOTSUPP;
696 phylink_ethtool_set_wol(dp->pl, w);
698 if (ds->ops->set_wol)
699 ret = ds->ops->set_wol(ds, dp->index, w);
704 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
706 struct dsa_port *dp = dsa_slave_to_port(dev);
707 struct dsa_switch *ds = dp->ds;
710 /* Port's PHY and MAC both need to be EEE capable */
711 if (!dev->phydev || !dp->pl)
714 if (!ds->ops->set_mac_eee)
717 ret = ds->ops->set_mac_eee(ds, dp->index, e);
721 return phylink_ethtool_set_eee(dp->pl, e);
724 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
726 struct dsa_port *dp = dsa_slave_to_port(dev);
727 struct dsa_switch *ds = dp->ds;
730 /* Port's PHY and MAC both need to be EEE capable */
731 if (!dev->phydev || !dp->pl)
734 if (!ds->ops->get_mac_eee)
737 ret = ds->ops->get_mac_eee(ds, dp->index, e);
741 return phylink_ethtool_get_eee(dp->pl, e);
744 static int dsa_slave_get_link_ksettings(struct net_device *dev,
745 struct ethtool_link_ksettings *cmd)
747 struct dsa_port *dp = dsa_slave_to_port(dev);
749 return phylink_ethtool_ksettings_get(dp->pl, cmd);
752 static int dsa_slave_set_link_ksettings(struct net_device *dev,
753 const struct ethtool_link_ksettings *cmd)
755 struct dsa_port *dp = dsa_slave_to_port(dev);
757 return phylink_ethtool_ksettings_set(dp->pl, cmd);
760 static void dsa_slave_get_pauseparam(struct net_device *dev,
761 struct ethtool_pauseparam *pause)
763 struct dsa_port *dp = dsa_slave_to_port(dev);
765 phylink_ethtool_get_pauseparam(dp->pl, pause);
768 static int dsa_slave_set_pauseparam(struct net_device *dev,
769 struct ethtool_pauseparam *pause)
771 struct dsa_port *dp = dsa_slave_to_port(dev);
773 return phylink_ethtool_set_pauseparam(dp->pl, pause);
776 #ifdef CONFIG_NET_POLL_CONTROLLER
777 static int dsa_slave_netpoll_setup(struct net_device *dev,
778 struct netpoll_info *ni)
780 struct net_device *master = dsa_slave_to_master(dev);
781 struct dsa_slave_priv *p = netdev_priv(dev);
782 struct netpoll *netpoll;
785 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
789 err = __netpoll_setup(netpoll, master);
795 p->netpoll = netpoll;
800 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
802 struct dsa_slave_priv *p = netdev_priv(dev);
803 struct netpoll *netpoll = p->netpoll;
810 __netpoll_free(netpoll);
813 static void dsa_slave_poll_controller(struct net_device *dev)
818 static int dsa_slave_get_phys_port_name(struct net_device *dev,
819 char *name, size_t len)
821 struct dsa_port *dp = dsa_slave_to_port(dev);
823 /* For non-legacy ports, devlink is used and it takes
824 * care of the name generation. This ndo implementation
825 * should be removed with legacy support.
830 if (snprintf(name, len, "p%d", dp->index) >= len)
836 static struct dsa_mall_tc_entry *
837 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
839 struct dsa_slave_priv *p = netdev_priv(dev);
840 struct dsa_mall_tc_entry *mall_tc_entry;
842 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
843 if (mall_tc_entry->cookie == cookie)
844 return mall_tc_entry;
849 static int dsa_slave_add_cls_matchall(struct net_device *dev,
850 struct tc_cls_matchall_offload *cls,
853 struct dsa_port *dp = dsa_slave_to_port(dev);
854 struct dsa_slave_priv *p = netdev_priv(dev);
855 struct dsa_mall_tc_entry *mall_tc_entry;
856 __be16 protocol = cls->common.protocol;
857 struct dsa_switch *ds = dp->ds;
858 struct flow_action_entry *act;
859 struct dsa_port *to_dp;
860 int err = -EOPNOTSUPP;
862 if (!ds->ops->port_mirror_add)
865 if (!flow_offload_has_one_action(&cls->rule->action))
868 if (!flow_action_basic_hw_stats_types_check(&cls->rule->action,
872 act = &cls->rule->action.entries[0];
874 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
875 struct dsa_mall_mirror_tc_entry *mirror;
880 if (!dsa_slave_dev_check(act->dev))
883 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
887 mall_tc_entry->cookie = cls->cookie;
888 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
889 mirror = &mall_tc_entry->mirror;
891 to_dp = dsa_slave_to_port(act->dev);
893 mirror->to_local_port = to_dp->index;
894 mirror->ingress = ingress;
896 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
898 kfree(mall_tc_entry);
902 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
908 static void dsa_slave_del_cls_matchall(struct net_device *dev,
909 struct tc_cls_matchall_offload *cls)
911 struct dsa_port *dp = dsa_slave_to_port(dev);
912 struct dsa_mall_tc_entry *mall_tc_entry;
913 struct dsa_switch *ds = dp->ds;
915 if (!ds->ops->port_mirror_del)
918 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
922 list_del(&mall_tc_entry->list);
924 switch (mall_tc_entry->type) {
925 case DSA_PORT_MALL_MIRROR:
926 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
932 kfree(mall_tc_entry);
935 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
936 struct tc_cls_matchall_offload *cls,
939 if (cls->common.chain_index)
942 switch (cls->command) {
943 case TC_CLSMATCHALL_REPLACE:
944 return dsa_slave_add_cls_matchall(dev, cls, ingress);
945 case TC_CLSMATCHALL_DESTROY:
946 dsa_slave_del_cls_matchall(dev, cls);
953 static int dsa_slave_add_cls_flower(struct net_device *dev,
954 struct flow_cls_offload *cls,
957 struct dsa_port *dp = dsa_slave_to_port(dev);
958 struct dsa_switch *ds = dp->ds;
959 int port = dp->index;
961 if (!ds->ops->cls_flower_add)
964 return ds->ops->cls_flower_add(ds, port, cls, ingress);
967 static int dsa_slave_del_cls_flower(struct net_device *dev,
968 struct flow_cls_offload *cls,
971 struct dsa_port *dp = dsa_slave_to_port(dev);
972 struct dsa_switch *ds = dp->ds;
973 int port = dp->index;
975 if (!ds->ops->cls_flower_del)
978 return ds->ops->cls_flower_del(ds, port, cls, ingress);
981 static int dsa_slave_stats_cls_flower(struct net_device *dev,
982 struct flow_cls_offload *cls,
985 struct dsa_port *dp = dsa_slave_to_port(dev);
986 struct dsa_switch *ds = dp->ds;
987 int port = dp->index;
989 if (!ds->ops->cls_flower_stats)
992 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
995 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
996 struct flow_cls_offload *cls,
999 switch (cls->command) {
1000 case FLOW_CLS_REPLACE:
1001 return dsa_slave_add_cls_flower(dev, cls, ingress);
1002 case FLOW_CLS_DESTROY:
1003 return dsa_slave_del_cls_flower(dev, cls, ingress);
1004 case FLOW_CLS_STATS:
1005 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1011 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1012 void *cb_priv, bool ingress)
1014 struct net_device *dev = cb_priv;
1016 if (!tc_can_offload(dev))
1020 case TC_SETUP_CLSMATCHALL:
1021 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1022 case TC_SETUP_CLSFLOWER:
1023 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1029 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1030 void *type_data, void *cb_priv)
1032 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1035 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1036 void *type_data, void *cb_priv)
1038 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1041 static LIST_HEAD(dsa_slave_block_cb_list);
1043 static int dsa_slave_setup_tc_block(struct net_device *dev,
1044 struct flow_block_offload *f)
1046 struct flow_block_cb *block_cb;
1047 flow_setup_cb_t *cb;
1049 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1050 cb = dsa_slave_setup_tc_block_cb_ig;
1051 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1052 cb = dsa_slave_setup_tc_block_cb_eg;
1056 f->driver_block_list = &dsa_slave_block_cb_list;
1058 switch (f->command) {
1059 case FLOW_BLOCK_BIND:
1060 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1063 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1064 if (IS_ERR(block_cb))
1065 return PTR_ERR(block_cb);
1067 flow_block_cb_add(block_cb, f);
1068 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1070 case FLOW_BLOCK_UNBIND:
1071 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1075 flow_block_cb_remove(block_cb, f);
1076 list_del(&block_cb->driver_list);
1083 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1086 struct dsa_port *dp = dsa_slave_to_port(dev);
1087 struct dsa_switch *ds = dp->ds;
1089 if (type == TC_SETUP_BLOCK)
1090 return dsa_slave_setup_tc_block(dev, type_data);
1092 if (!ds->ops->port_setup_tc)
1095 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1098 static void dsa_slave_get_stats64(struct net_device *dev,
1099 struct rtnl_link_stats64 *stats)
1101 struct dsa_slave_priv *p = netdev_priv(dev);
1102 struct pcpu_sw_netstats *s;
1106 netdev_stats_to_stats64(stats, &dev->stats);
1107 for_each_possible_cpu(i) {
1108 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
1110 s = per_cpu_ptr(p->stats64, i);
1112 start = u64_stats_fetch_begin_irq(&s->syncp);
1113 tx_packets = s->tx_packets;
1114 tx_bytes = s->tx_bytes;
1115 rx_packets = s->rx_packets;
1116 rx_bytes = s->rx_bytes;
1117 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
1119 stats->tx_packets += tx_packets;
1120 stats->tx_bytes += tx_bytes;
1121 stats->rx_packets += rx_packets;
1122 stats->rx_bytes += rx_bytes;
1126 static int dsa_slave_get_rxnfc(struct net_device *dev,
1127 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1129 struct dsa_port *dp = dsa_slave_to_port(dev);
1130 struct dsa_switch *ds = dp->ds;
1132 if (!ds->ops->get_rxnfc)
1135 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1138 static int dsa_slave_set_rxnfc(struct net_device *dev,
1139 struct ethtool_rxnfc *nfc)
1141 struct dsa_port *dp = dsa_slave_to_port(dev);
1142 struct dsa_switch *ds = dp->ds;
1144 if (!ds->ops->set_rxnfc)
1147 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1150 static int dsa_slave_get_ts_info(struct net_device *dev,
1151 struct ethtool_ts_info *ts)
1153 struct dsa_slave_priv *p = netdev_priv(dev);
1154 struct dsa_switch *ds = p->dp->ds;
1156 if (!ds->ops->get_ts_info)
1159 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1162 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1165 struct dsa_port *dp = dsa_slave_to_port(dev);
1166 struct bridge_vlan_info info;
1169 /* Check for a possible bridge VLAN entry now since there is no
1170 * need to emulate the switchdev prepare + commit phase.
1172 if (dp->bridge_dev) {
1173 if (!br_vlan_enabled(dp->bridge_dev))
1176 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1177 * device, respectively the VID is not found, returning
1178 * 0 means success, which is a failure for us here.
1180 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1185 ret = dsa_port_vid_add(dp, vid, 0);
1189 ret = dsa_port_vid_add(dp->cpu_dp, vid, 0);
1196 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1199 struct dsa_port *dp = dsa_slave_to_port(dev);
1200 struct bridge_vlan_info info;
1203 /* Check for a possible bridge VLAN entry now since there is no
1204 * need to emulate the switchdev prepare + commit phase.
1206 if (dp->bridge_dev) {
1207 if (!br_vlan_enabled(dp->bridge_dev))
1210 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1211 * device, respectively the VID is not found, returning
1212 * 0 means success, which is a failure for us here.
1214 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1219 /* Do not deprogram the CPU port as it may be shared with other user
1220 * ports which can be members of this VLAN as well.
1222 return dsa_port_vid_del(dp, vid);
1225 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1226 .get_drvinfo = dsa_slave_get_drvinfo,
1227 .get_regs_len = dsa_slave_get_regs_len,
1228 .get_regs = dsa_slave_get_regs,
1229 .nway_reset = dsa_slave_nway_reset,
1230 .get_link = ethtool_op_get_link,
1231 .get_eeprom_len = dsa_slave_get_eeprom_len,
1232 .get_eeprom = dsa_slave_get_eeprom,
1233 .set_eeprom = dsa_slave_set_eeprom,
1234 .get_strings = dsa_slave_get_strings,
1235 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1236 .get_sset_count = dsa_slave_get_sset_count,
1237 .set_wol = dsa_slave_set_wol,
1238 .get_wol = dsa_slave_get_wol,
1239 .set_eee = dsa_slave_set_eee,
1240 .get_eee = dsa_slave_get_eee,
1241 .get_link_ksettings = dsa_slave_get_link_ksettings,
1242 .set_link_ksettings = dsa_slave_set_link_ksettings,
1243 .get_pauseparam = dsa_slave_get_pauseparam,
1244 .set_pauseparam = dsa_slave_set_pauseparam,
1245 .get_rxnfc = dsa_slave_get_rxnfc,
1246 .set_rxnfc = dsa_slave_set_rxnfc,
1247 .get_ts_info = dsa_slave_get_ts_info,
1250 /* legacy way, bypassing the bridge *****************************************/
1251 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1252 struct net_device *dev,
1253 const unsigned char *addr, u16 vid,
1255 struct netlink_ext_ack *extack)
1257 struct dsa_port *dp = dsa_slave_to_port(dev);
1259 return dsa_port_fdb_add(dp, addr, vid);
1262 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1263 struct net_device *dev,
1264 const unsigned char *addr, u16 vid)
1266 struct dsa_port *dp = dsa_slave_to_port(dev);
1268 return dsa_port_fdb_del(dp, addr, vid);
1271 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1273 struct dsa_port *dp = dsa_slave_to_port(dev);
1275 return dp->ds->devlink ? &dp->devlink_port : NULL;
1278 static const struct net_device_ops dsa_slave_netdev_ops = {
1279 .ndo_open = dsa_slave_open,
1280 .ndo_stop = dsa_slave_close,
1281 .ndo_start_xmit = dsa_slave_xmit,
1282 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1283 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1284 .ndo_set_mac_address = dsa_slave_set_mac_address,
1285 .ndo_fdb_add = dsa_legacy_fdb_add,
1286 .ndo_fdb_del = dsa_legacy_fdb_del,
1287 .ndo_fdb_dump = dsa_slave_fdb_dump,
1288 .ndo_do_ioctl = dsa_slave_ioctl,
1289 .ndo_get_iflink = dsa_slave_get_iflink,
1290 #ifdef CONFIG_NET_POLL_CONTROLLER
1291 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1292 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1293 .ndo_poll_controller = dsa_slave_poll_controller,
1295 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1296 .ndo_setup_tc = dsa_slave_setup_tc,
1297 .ndo_get_stats64 = dsa_slave_get_stats64,
1298 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1299 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1300 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1301 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1304 static struct device_type dsa_type = {
1308 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1310 const struct dsa_port *dp = dsa_to_port(ds, port);
1312 phylink_mac_change(dp->pl, up);
1314 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1316 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1317 struct phylink_link_state *state)
1319 struct dsa_port *dp = dsa_slave_to_port(dev);
1320 struct dsa_switch *ds = dp->ds;
1322 /* No need to check that this operation is valid, the callback would
1323 * not be called if it was not.
1325 ds->ops->phylink_fixed_state(ds, dp->index, state);
1328 /* slave device setup *******************************************************/
1329 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1331 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1332 struct dsa_switch *ds = dp->ds;
1334 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1335 if (!slave_dev->phydev) {
1336 netdev_err(slave_dev, "no phy at %d\n", addr);
1340 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1343 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1345 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1346 struct device_node *port_dn = dp->dn;
1347 struct dsa_switch *ds = dp->ds;
1348 phy_interface_t mode;
1352 ret = of_get_phy_mode(port_dn, &mode);
1354 mode = PHY_INTERFACE_MODE_NA;
1356 dp->pl_config.dev = &slave_dev->dev;
1357 dp->pl_config.type = PHYLINK_NETDEV;
1359 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1360 &dsa_port_phylink_mac_ops);
1361 if (IS_ERR(dp->pl)) {
1362 netdev_err(slave_dev,
1363 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1364 return PTR_ERR(dp->pl);
1367 /* Register only if the switch provides such a callback, since this
1368 * callback takes precedence over polling the link GPIO in PHYLINK
1369 * (see phylink_get_fixed_state).
1371 if (ds->ops->phylink_fixed_state)
1372 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1374 if (ds->ops->get_phy_flags)
1375 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1377 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1378 if (ret == -ENODEV && ds->slave_mii_bus) {
1379 /* We could not connect to a designated PHY or SFP, so try to
1380 * use the switch internal MDIO bus instead
1382 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1384 netdev_err(slave_dev,
1385 "failed to connect to port %d: %d\n",
1387 phylink_destroy(dp->pl);
1395 int dsa_slave_suspend(struct net_device *slave_dev)
1397 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1399 if (!netif_running(slave_dev))
1402 netif_device_detach(slave_dev);
1405 phylink_stop(dp->pl);
1411 int dsa_slave_resume(struct net_device *slave_dev)
1413 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1415 if (!netif_running(slave_dev))
1418 netif_device_attach(slave_dev);
1421 phylink_start(dp->pl);
1427 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1429 struct net_device *master = dsa_slave_to_master(dev);
1430 struct dsa_port *dp = dsa_slave_to_port(dev);
1431 struct dsa_notifier_register_info rinfo = {
1432 .switch_number = dp->ds->index,
1433 .port_number = dp->index,
1438 call_dsa_notifiers(val, dev, &rinfo.info);
1441 int dsa_slave_create(struct dsa_port *port)
1443 const struct dsa_port *cpu_dp = port->cpu_dp;
1444 struct net_device *master = cpu_dp->master;
1445 struct dsa_switch *ds = port->ds;
1446 const char *name = port->name;
1447 struct net_device *slave_dev;
1448 struct dsa_slave_priv *p;
1451 if (!ds->num_tx_queues)
1452 ds->num_tx_queues = 1;
1454 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1455 NET_NAME_UNKNOWN, ether_setup,
1456 ds->num_tx_queues, 1);
1457 if (slave_dev == NULL)
1460 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1461 if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
1462 slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1463 slave_dev->hw_features |= NETIF_F_HW_TC;
1464 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1465 if (!IS_ERR_OR_NULL(port->mac))
1466 ether_addr_copy(slave_dev->dev_addr, port->mac);
1468 eth_hw_addr_inherit(slave_dev, master);
1469 slave_dev->priv_flags |= IFF_NO_QUEUE;
1470 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1471 slave_dev->min_mtu = 0;
1472 slave_dev->max_mtu = ETH_MAX_MTU;
1473 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1475 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1476 slave_dev->dev.of_node = port->dn;
1477 slave_dev->vlan_features = master->vlan_features;
1479 p = netdev_priv(slave_dev);
1480 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1482 free_netdev(slave_dev);
1486 INIT_LIST_HEAD(&p->mall_tc_list);
1487 p->xmit = cpu_dp->tag_ops->xmit;
1488 port->slave = slave_dev;
1490 netif_carrier_off(slave_dev);
1492 ret = dsa_slave_phy_setup(slave_dev);
1494 netdev_err(master, "error %d setting up slave phy\n", ret);
1498 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1500 ret = register_netdev(slave_dev);
1502 netdev_err(master, "error %d registering interface %s\n",
1503 ret, slave_dev->name);
1511 phylink_disconnect_phy(p->dp->pl);
1513 phylink_destroy(p->dp->pl);
1515 free_percpu(p->stats64);
1516 free_netdev(slave_dev);
1521 void dsa_slave_destroy(struct net_device *slave_dev)
1523 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1524 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1526 netif_carrier_off(slave_dev);
1528 phylink_disconnect_phy(dp->pl);
1531 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1532 unregister_netdev(slave_dev);
1533 phylink_destroy(dp->pl);
1534 free_percpu(p->stats64);
1535 free_netdev(slave_dev);
1538 bool dsa_slave_dev_check(const struct net_device *dev)
1540 return dev->netdev_ops == &dsa_slave_netdev_ops;
1543 static int dsa_slave_changeupper(struct net_device *dev,
1544 struct netdev_notifier_changeupper_info *info)
1546 struct dsa_port *dp = dsa_slave_to_port(dev);
1547 int err = NOTIFY_DONE;
1549 if (netif_is_bridge_master(info->upper_dev)) {
1550 if (info->linking) {
1551 err = dsa_port_bridge_join(dp, info->upper_dev);
1552 err = notifier_from_errno(err);
1554 dsa_port_bridge_leave(dp, info->upper_dev);
1562 static int dsa_slave_upper_vlan_check(struct net_device *dev,
1563 struct netdev_notifier_changeupper_info *
1566 struct netlink_ext_ack *ext_ack;
1567 struct net_device *slave;
1568 struct dsa_port *dp;
1570 ext_ack = netdev_notifier_info_to_extack(&info->info);
1572 if (!is_vlan_dev(dev))
1575 slave = vlan_dev_real_dev(dev);
1576 if (!dsa_slave_dev_check(slave))
1579 dp = dsa_slave_to_port(slave);
1580 if (!dp->bridge_dev)
1583 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1584 if (br_vlan_enabled(dp->bridge_dev) &&
1585 netif_is_bridge_master(info->upper_dev) && info->linking) {
1586 NL_SET_ERR_MSG_MOD(ext_ack,
1587 "Cannot enslave VLAN device into VLAN aware bridge");
1588 return notifier_from_errno(-EINVAL);
1594 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1595 unsigned long event, void *ptr)
1597 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1599 if (event == NETDEV_CHANGEUPPER) {
1600 if (!dsa_slave_dev_check(dev))
1601 return dsa_slave_upper_vlan_check(dev, ptr);
1603 return dsa_slave_changeupper(dev, ptr);
1609 struct dsa_switchdev_event_work {
1610 struct work_struct work;
1611 struct switchdev_notifier_fdb_info fdb_info;
1612 struct net_device *dev;
1613 unsigned long event;
1616 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1618 struct dsa_switchdev_event_work *switchdev_work =
1619 container_of(work, struct dsa_switchdev_event_work, work);
1620 struct net_device *dev = switchdev_work->dev;
1621 struct switchdev_notifier_fdb_info *fdb_info;
1622 struct dsa_port *dp = dsa_slave_to_port(dev);
1626 switch (switchdev_work->event) {
1627 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1628 fdb_info = &switchdev_work->fdb_info;
1629 if (!fdb_info->added_by_user)
1632 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1634 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1637 fdb_info->offloaded = true;
1638 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1639 &fdb_info->info, NULL);
1642 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1643 fdb_info = &switchdev_work->fdb_info;
1644 if (!fdb_info->added_by_user)
1647 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1649 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1656 kfree(switchdev_work->fdb_info.addr);
1657 kfree(switchdev_work);
1662 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1664 const struct switchdev_notifier_fdb_info *
1667 memcpy(&switchdev_work->fdb_info, fdb_info,
1668 sizeof(switchdev_work->fdb_info));
1669 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1670 if (!switchdev_work->fdb_info.addr)
1672 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1677 /* Called under rcu_read_lock() */
1678 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1679 unsigned long event, void *ptr)
1681 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1682 struct dsa_switchdev_event_work *switchdev_work;
1685 if (event == SWITCHDEV_PORT_ATTR_SET) {
1686 err = switchdev_handle_port_attr_set(dev, ptr,
1687 dsa_slave_dev_check,
1688 dsa_slave_port_attr_set);
1689 return notifier_from_errno(err);
1692 if (!dsa_slave_dev_check(dev))
1695 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1696 if (!switchdev_work)
1699 INIT_WORK(&switchdev_work->work,
1700 dsa_slave_switchdev_event_work);
1701 switchdev_work->dev = dev;
1702 switchdev_work->event = event;
1705 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1706 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1707 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1708 goto err_fdb_work_init;
1712 kfree(switchdev_work);
1716 dsa_schedule_work(&switchdev_work->work);
1720 kfree(switchdev_work);
1724 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1725 unsigned long event, void *ptr)
1727 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1731 case SWITCHDEV_PORT_OBJ_ADD:
1732 err = switchdev_handle_port_obj_add(dev, ptr,
1733 dsa_slave_dev_check,
1734 dsa_slave_port_obj_add);
1735 return notifier_from_errno(err);
1736 case SWITCHDEV_PORT_OBJ_DEL:
1737 err = switchdev_handle_port_obj_del(dev, ptr,
1738 dsa_slave_dev_check,
1739 dsa_slave_port_obj_del);
1740 return notifier_from_errno(err);
1741 case SWITCHDEV_PORT_ATTR_SET:
1742 err = switchdev_handle_port_attr_set(dev, ptr,
1743 dsa_slave_dev_check,
1744 dsa_slave_port_attr_set);
1745 return notifier_from_errno(err);
1751 static struct notifier_block dsa_slave_nb __read_mostly = {
1752 .notifier_call = dsa_slave_netdevice_event,
1755 static struct notifier_block dsa_slave_switchdev_notifier = {
1756 .notifier_call = dsa_slave_switchdev_event,
1759 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1760 .notifier_call = dsa_slave_switchdev_blocking_event,
1763 int dsa_slave_register_notifier(void)
1765 struct notifier_block *nb;
1768 err = register_netdevice_notifier(&dsa_slave_nb);
1772 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1774 goto err_switchdev_nb;
1776 nb = &dsa_slave_switchdev_blocking_notifier;
1777 err = register_switchdev_blocking_notifier(nb);
1779 goto err_switchdev_blocking_nb;
1783 err_switchdev_blocking_nb:
1784 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1786 unregister_netdevice_notifier(&dsa_slave_nb);
1790 void dsa_slave_unregister_notifier(void)
1792 struct notifier_block *nb;
1795 nb = &dsa_slave_switchdev_blocking_notifier;
1796 err = unregister_switchdev_blocking_notifier(nb);
1798 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1800 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1802 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1804 err = unregister_netdevice_notifier(&dsa_slave_nb);
1806 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);