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(dp, dev->phydev);
97 phylink_start(dp->pl);
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 cancel_work_sync(&dp->xmit_work);
120 skb_queue_purge(&dp->xmit_queue);
122 phylink_stop(dp->pl);
124 dsa_port_disable(dp);
126 dev_mc_unsync(master, dev);
127 dev_uc_unsync(master, dev);
128 if (dev->flags & IFF_ALLMULTI)
129 dev_set_allmulti(master, -1);
130 if (dev->flags & IFF_PROMISC)
131 dev_set_promiscuity(master, -1);
133 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
134 dev_uc_del(master, dev->dev_addr);
139 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
141 struct net_device *master = dsa_slave_to_master(dev);
142 if (dev->flags & IFF_UP) {
143 if (change & IFF_ALLMULTI)
144 dev_set_allmulti(master,
145 dev->flags & IFF_ALLMULTI ? 1 : -1);
146 if (change & IFF_PROMISC)
147 dev_set_promiscuity(master,
148 dev->flags & IFF_PROMISC ? 1 : -1);
152 static void dsa_slave_set_rx_mode(struct net_device *dev)
154 struct net_device *master = dsa_slave_to_master(dev);
156 dev_mc_sync(master, dev);
157 dev_uc_sync(master, dev);
160 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
162 struct net_device *master = dsa_slave_to_master(dev);
163 struct sockaddr *addr = a;
166 if (!is_valid_ether_addr(addr->sa_data))
167 return -EADDRNOTAVAIL;
169 if (!(dev->flags & IFF_UP))
172 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
173 err = dev_uc_add(master, addr->sa_data);
178 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
179 dev_uc_del(master, dev->dev_addr);
182 ether_addr_copy(dev->dev_addr, addr->sa_data);
187 struct dsa_slave_dump_ctx {
188 struct net_device *dev;
190 struct netlink_callback *cb;
195 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
196 bool is_static, void *data)
198 struct dsa_slave_dump_ctx *dump = data;
199 u32 portid = NETLINK_CB(dump->cb->skb).portid;
200 u32 seq = dump->cb->nlh->nlmsg_seq;
201 struct nlmsghdr *nlh;
204 if (dump->idx < dump->cb->args[2])
207 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
208 sizeof(*ndm), NLM_F_MULTI);
212 ndm = nlmsg_data(nlh);
213 ndm->ndm_family = AF_BRIDGE;
216 ndm->ndm_flags = NTF_SELF;
218 ndm->ndm_ifindex = dump->dev->ifindex;
219 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
221 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
222 goto nla_put_failure;
224 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
225 goto nla_put_failure;
227 nlmsg_end(dump->skb, nlh);
234 nlmsg_cancel(dump->skb, nlh);
239 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
240 struct net_device *dev, struct net_device *filter_dev,
243 struct dsa_port *dp = dsa_slave_to_port(dev);
244 struct dsa_slave_dump_ctx dump = {
252 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
258 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
260 struct dsa_slave_priv *p = netdev_priv(dev);
261 struct dsa_switch *ds = p->dp->ds;
262 int port = p->dp->index;
264 /* Pass through to switch driver if it supports timestamping */
267 if (ds->ops->port_hwtstamp_get)
268 return ds->ops->port_hwtstamp_get(ds, port, ifr);
271 if (ds->ops->port_hwtstamp_set)
272 return ds->ops->port_hwtstamp_set(ds, port, ifr);
276 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
279 static int dsa_slave_port_attr_set(struct net_device *dev,
280 const struct switchdev_attr *attr,
281 struct switchdev_trans *trans)
283 struct dsa_port *dp = dsa_slave_to_port(dev);
287 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
288 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
290 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
291 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
294 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
295 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
297 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
298 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
301 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
302 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
304 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
305 ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans);
315 static int dsa_slave_vlan_add(struct net_device *dev,
316 const struct switchdev_obj *obj,
317 struct switchdev_trans *trans)
319 struct dsa_port *dp = dsa_slave_to_port(dev);
320 struct switchdev_obj_port_vlan vlan;
323 if (obj->orig_dev != dev)
326 if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
329 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
331 err = dsa_port_vlan_add(dp, &vlan, trans);
338 static int dsa_slave_port_obj_add(struct net_device *dev,
339 const struct switchdev_obj *obj,
340 struct switchdev_trans *trans,
341 struct netlink_ext_ack *extack)
343 struct dsa_port *dp = dsa_slave_to_port(dev);
346 /* For the prepare phase, ensure the full set of changes is feasable in
347 * one go in order to signal a failure properly. If an operation is not
348 * supported, return -EOPNOTSUPP.
352 case SWITCHDEV_OBJ_ID_PORT_MDB:
353 if (obj->orig_dev != dev)
355 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
357 case SWITCHDEV_OBJ_ID_HOST_MDB:
358 /* DSA can directly translate this to a normal MDB add,
359 * but on the CPU port.
361 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
364 case SWITCHDEV_OBJ_ID_PORT_VLAN:
365 err = dsa_slave_vlan_add(dev, obj, trans);
375 static int dsa_slave_vlan_del(struct net_device *dev,
376 const struct switchdev_obj *obj)
378 struct dsa_port *dp = dsa_slave_to_port(dev);
380 if (obj->orig_dev != dev)
383 if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
386 return dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
389 static int dsa_slave_port_obj_del(struct net_device *dev,
390 const struct switchdev_obj *obj)
392 struct dsa_port *dp = dsa_slave_to_port(dev);
396 case SWITCHDEV_OBJ_ID_PORT_MDB:
397 if (obj->orig_dev != dev)
399 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
401 case SWITCHDEV_OBJ_ID_HOST_MDB:
402 /* DSA can directly translate this to a normal MDB add,
403 * but on the CPU port.
405 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
407 case SWITCHDEV_OBJ_ID_PORT_VLAN:
408 err = dsa_slave_vlan_del(dev, obj);
418 static int dsa_slave_get_port_parent_id(struct net_device *dev,
419 struct netdev_phys_item_id *ppid)
421 struct dsa_port *dp = dsa_slave_to_port(dev);
422 struct dsa_switch *ds = dp->ds;
423 struct dsa_switch_tree *dst = ds->dst;
425 /* For non-legacy ports, devlink is used and it takes
426 * care of the name generation. This ndo implementation
427 * should be removed with legacy support.
432 ppid->id_len = sizeof(dst->index);
433 memcpy(&ppid->id, &dst->index, ppid->id_len);
438 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
441 #ifdef CONFIG_NET_POLL_CONTROLLER
442 struct dsa_slave_priv *p = netdev_priv(dev);
445 netpoll_send_skb(p->netpoll, skb);
452 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
455 struct dsa_switch *ds = p->dp->ds;
456 struct sk_buff *clone;
459 type = ptp_classify_raw(skb);
460 if (type == PTP_CLASS_NONE)
463 if (!ds->ops->port_txtstamp)
466 clone = skb_clone_sk(skb);
470 DSA_SKB_CB(skb)->clone = clone;
472 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
478 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
480 /* SKB for netpoll still need to be mangled with the protocol-specific
481 * tag to be successfully transmitted
483 if (unlikely(netpoll_tx_running(dev)))
484 return dsa_slave_netpoll_send_skb(dev, skb);
486 /* Queue the SKB for transmission on the parent interface, but
487 * do not modify its EtherType
489 skb->dev = dsa_slave_to_master(dev);
494 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
496 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
498 struct dsa_slave_priv *p = netdev_priv(dev);
499 struct pcpu_sw_netstats *s;
500 struct sk_buff *nskb;
502 s = this_cpu_ptr(p->stats64);
503 u64_stats_update_begin(&s->syncp);
505 s->tx_bytes += skb->len;
506 u64_stats_update_end(&s->syncp);
508 DSA_SKB_CB(skb)->deferred_xmit = false;
509 DSA_SKB_CB(skb)->clone = NULL;
511 /* Identify PTP protocol packets, clone them, and pass them to the
514 dsa_skb_tx_timestamp(p, skb);
516 /* Transmit function may have to reallocate the original SKB,
517 * in which case it must have freed it. Only free it here on error.
519 nskb = p->xmit(skb, dev);
521 if (!DSA_SKB_CB(skb)->deferred_xmit)
526 return dsa_enqueue_skb(nskb, dev);
529 void *dsa_defer_xmit(struct sk_buff *skb, struct net_device *dev)
531 struct dsa_port *dp = dsa_slave_to_port(dev);
533 DSA_SKB_CB(skb)->deferred_xmit = true;
535 skb_queue_tail(&dp->xmit_queue, skb);
536 schedule_work(&dp->xmit_work);
539 EXPORT_SYMBOL_GPL(dsa_defer_xmit);
541 static void dsa_port_xmit_work(struct work_struct *work)
543 struct dsa_port *dp = container_of(work, struct dsa_port, xmit_work);
544 struct dsa_switch *ds = dp->ds;
547 if (unlikely(!ds->ops->port_deferred_xmit))
550 while ((skb = skb_dequeue(&dp->xmit_queue)) != NULL)
551 ds->ops->port_deferred_xmit(ds, dp->index, skb);
554 /* ethtool operations *******************************************************/
556 static void dsa_slave_get_drvinfo(struct net_device *dev,
557 struct ethtool_drvinfo *drvinfo)
559 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
560 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
561 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
564 static int dsa_slave_get_regs_len(struct net_device *dev)
566 struct dsa_port *dp = dsa_slave_to_port(dev);
567 struct dsa_switch *ds = dp->ds;
569 if (ds->ops->get_regs_len)
570 return ds->ops->get_regs_len(ds, dp->index);
576 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
578 struct dsa_port *dp = dsa_slave_to_port(dev);
579 struct dsa_switch *ds = dp->ds;
581 if (ds->ops->get_regs)
582 ds->ops->get_regs(ds, dp->index, regs, _p);
585 static int dsa_slave_nway_reset(struct net_device *dev)
587 struct dsa_port *dp = dsa_slave_to_port(dev);
589 return phylink_ethtool_nway_reset(dp->pl);
592 static int dsa_slave_get_eeprom_len(struct net_device *dev)
594 struct dsa_port *dp = dsa_slave_to_port(dev);
595 struct dsa_switch *ds = dp->ds;
597 if (ds->cd && ds->cd->eeprom_len)
598 return ds->cd->eeprom_len;
600 if (ds->ops->get_eeprom_len)
601 return ds->ops->get_eeprom_len(ds);
606 static int dsa_slave_get_eeprom(struct net_device *dev,
607 struct ethtool_eeprom *eeprom, u8 *data)
609 struct dsa_port *dp = dsa_slave_to_port(dev);
610 struct dsa_switch *ds = dp->ds;
612 if (ds->ops->get_eeprom)
613 return ds->ops->get_eeprom(ds, eeprom, data);
618 static int dsa_slave_set_eeprom(struct net_device *dev,
619 struct ethtool_eeprom *eeprom, u8 *data)
621 struct dsa_port *dp = dsa_slave_to_port(dev);
622 struct dsa_switch *ds = dp->ds;
624 if (ds->ops->set_eeprom)
625 return ds->ops->set_eeprom(ds, eeprom, data);
630 static void dsa_slave_get_strings(struct net_device *dev,
631 uint32_t stringset, uint8_t *data)
633 struct dsa_port *dp = dsa_slave_to_port(dev);
634 struct dsa_switch *ds = dp->ds;
636 if (stringset == ETH_SS_STATS) {
637 int len = ETH_GSTRING_LEN;
639 strncpy(data, "tx_packets", len);
640 strncpy(data + len, "tx_bytes", len);
641 strncpy(data + 2 * len, "rx_packets", len);
642 strncpy(data + 3 * len, "rx_bytes", len);
643 if (ds->ops->get_strings)
644 ds->ops->get_strings(ds, dp->index, stringset,
649 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
650 struct ethtool_stats *stats,
653 struct dsa_port *dp = dsa_slave_to_port(dev);
654 struct dsa_slave_priv *p = netdev_priv(dev);
655 struct dsa_switch *ds = dp->ds;
656 struct pcpu_sw_netstats *s;
660 for_each_possible_cpu(i) {
661 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
663 s = per_cpu_ptr(p->stats64, i);
665 start = u64_stats_fetch_begin_irq(&s->syncp);
666 tx_packets = s->tx_packets;
667 tx_bytes = s->tx_bytes;
668 rx_packets = s->rx_packets;
669 rx_bytes = s->rx_bytes;
670 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
671 data[0] += tx_packets;
673 data[2] += rx_packets;
676 if (ds->ops->get_ethtool_stats)
677 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
680 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
682 struct dsa_port *dp = dsa_slave_to_port(dev);
683 struct dsa_switch *ds = dp->ds;
685 if (sset == ETH_SS_STATS) {
689 if (ds->ops->get_sset_count)
690 count += ds->ops->get_sset_count(ds, dp->index, sset);
698 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
700 struct dsa_port *dp = dsa_slave_to_port(dev);
701 struct dsa_switch *ds = dp->ds;
703 phylink_ethtool_get_wol(dp->pl, w);
705 if (ds->ops->get_wol)
706 ds->ops->get_wol(ds, dp->index, w);
709 static int dsa_slave_set_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;
713 int ret = -EOPNOTSUPP;
715 phylink_ethtool_set_wol(dp->pl, w);
717 if (ds->ops->set_wol)
718 ret = ds->ops->set_wol(ds, dp->index, w);
723 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
725 struct dsa_port *dp = dsa_slave_to_port(dev);
726 struct dsa_switch *ds = dp->ds;
729 /* Port's PHY and MAC both need to be EEE capable */
730 if (!dev->phydev || !dp->pl)
733 if (!ds->ops->set_mac_eee)
736 ret = ds->ops->set_mac_eee(ds, dp->index, e);
740 return phylink_ethtool_set_eee(dp->pl, e);
743 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
745 struct dsa_port *dp = dsa_slave_to_port(dev);
746 struct dsa_switch *ds = dp->ds;
749 /* Port's PHY and MAC both need to be EEE capable */
750 if (!dev->phydev || !dp->pl)
753 if (!ds->ops->get_mac_eee)
756 ret = ds->ops->get_mac_eee(ds, dp->index, e);
760 return phylink_ethtool_get_eee(dp->pl, e);
763 static int dsa_slave_get_link_ksettings(struct net_device *dev,
764 struct ethtool_link_ksettings *cmd)
766 struct dsa_port *dp = dsa_slave_to_port(dev);
768 return phylink_ethtool_ksettings_get(dp->pl, cmd);
771 static int dsa_slave_set_link_ksettings(struct net_device *dev,
772 const struct ethtool_link_ksettings *cmd)
774 struct dsa_port *dp = dsa_slave_to_port(dev);
776 return phylink_ethtool_ksettings_set(dp->pl, cmd);
779 #ifdef CONFIG_NET_POLL_CONTROLLER
780 static int dsa_slave_netpoll_setup(struct net_device *dev,
781 struct netpoll_info *ni)
783 struct net_device *master = dsa_slave_to_master(dev);
784 struct dsa_slave_priv *p = netdev_priv(dev);
785 struct netpoll *netpoll;
788 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
792 err = __netpoll_setup(netpoll, master);
798 p->netpoll = netpoll;
803 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
805 struct dsa_slave_priv *p = netdev_priv(dev);
806 struct netpoll *netpoll = p->netpoll;
813 __netpoll_free(netpoll);
816 static void dsa_slave_poll_controller(struct net_device *dev)
821 static int dsa_slave_get_phys_port_name(struct net_device *dev,
822 char *name, size_t len)
824 struct dsa_port *dp = dsa_slave_to_port(dev);
826 /* For non-legacy ports, devlink is used and it takes
827 * care of the name generation. This ndo implementation
828 * should be removed with legacy support.
833 if (snprintf(name, len, "p%d", dp->index) >= len)
839 static struct dsa_mall_tc_entry *
840 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
842 struct dsa_slave_priv *p = netdev_priv(dev);
843 struct dsa_mall_tc_entry *mall_tc_entry;
845 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
846 if (mall_tc_entry->cookie == cookie)
847 return mall_tc_entry;
852 static int dsa_slave_add_cls_matchall(struct net_device *dev,
853 struct tc_cls_matchall_offload *cls,
856 struct dsa_port *dp = dsa_slave_to_port(dev);
857 struct dsa_slave_priv *p = netdev_priv(dev);
858 struct dsa_mall_tc_entry *mall_tc_entry;
859 __be16 protocol = cls->common.protocol;
860 struct dsa_switch *ds = dp->ds;
861 struct flow_action_entry *act;
862 struct dsa_port *to_dp;
863 int err = -EOPNOTSUPP;
865 if (!ds->ops->port_mirror_add)
868 if (!flow_offload_has_one_action(&cls->rule->action))
871 act = &cls->rule->action.entries[0];
873 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
874 struct dsa_mall_mirror_tc_entry *mirror;
879 if (!dsa_slave_dev_check(act->dev))
882 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
886 mall_tc_entry->cookie = cls->cookie;
887 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
888 mirror = &mall_tc_entry->mirror;
890 to_dp = dsa_slave_to_port(act->dev);
892 mirror->to_local_port = to_dp->index;
893 mirror->ingress = ingress;
895 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
897 kfree(mall_tc_entry);
901 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
907 static void dsa_slave_del_cls_matchall(struct net_device *dev,
908 struct tc_cls_matchall_offload *cls)
910 struct dsa_port *dp = dsa_slave_to_port(dev);
911 struct dsa_mall_tc_entry *mall_tc_entry;
912 struct dsa_switch *ds = dp->ds;
914 if (!ds->ops->port_mirror_del)
917 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
921 list_del(&mall_tc_entry->list);
923 switch (mall_tc_entry->type) {
924 case DSA_PORT_MALL_MIRROR:
925 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
931 kfree(mall_tc_entry);
934 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
935 struct tc_cls_matchall_offload *cls,
938 if (cls->common.chain_index)
941 switch (cls->command) {
942 case TC_CLSMATCHALL_REPLACE:
943 return dsa_slave_add_cls_matchall(dev, cls, ingress);
944 case TC_CLSMATCHALL_DESTROY:
945 dsa_slave_del_cls_matchall(dev, cls);
952 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
953 void *cb_priv, bool ingress)
955 struct net_device *dev = cb_priv;
957 if (!tc_can_offload(dev))
961 case TC_SETUP_CLSMATCHALL:
962 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
968 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
969 void *type_data, void *cb_priv)
971 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
974 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
975 void *type_data, void *cb_priv)
977 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
980 static LIST_HEAD(dsa_slave_block_cb_list);
982 static int dsa_slave_setup_tc_block(struct net_device *dev,
983 struct flow_block_offload *f)
985 struct flow_block_cb *block_cb;
988 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
989 cb = dsa_slave_setup_tc_block_cb_ig;
990 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
991 cb = dsa_slave_setup_tc_block_cb_eg;
995 f->driver_block_list = &dsa_slave_block_cb_list;
997 switch (f->command) {
998 case FLOW_BLOCK_BIND:
999 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1002 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1003 if (IS_ERR(block_cb))
1004 return PTR_ERR(block_cb);
1006 flow_block_cb_add(block_cb, f);
1007 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1009 case FLOW_BLOCK_UNBIND:
1010 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1014 flow_block_cb_remove(block_cb, f);
1015 list_del(&block_cb->driver_list);
1022 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1026 case TC_SETUP_BLOCK:
1027 return dsa_slave_setup_tc_block(dev, type_data);
1033 static void dsa_slave_get_stats64(struct net_device *dev,
1034 struct rtnl_link_stats64 *stats)
1036 struct dsa_slave_priv *p = netdev_priv(dev);
1037 struct pcpu_sw_netstats *s;
1041 netdev_stats_to_stats64(stats, &dev->stats);
1042 for_each_possible_cpu(i) {
1043 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
1045 s = per_cpu_ptr(p->stats64, i);
1047 start = u64_stats_fetch_begin_irq(&s->syncp);
1048 tx_packets = s->tx_packets;
1049 tx_bytes = s->tx_bytes;
1050 rx_packets = s->rx_packets;
1051 rx_bytes = s->rx_bytes;
1052 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
1054 stats->tx_packets += tx_packets;
1055 stats->tx_bytes += tx_bytes;
1056 stats->rx_packets += rx_packets;
1057 stats->rx_bytes += rx_bytes;
1061 static int dsa_slave_get_rxnfc(struct net_device *dev,
1062 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1064 struct dsa_port *dp = dsa_slave_to_port(dev);
1065 struct dsa_switch *ds = dp->ds;
1067 if (!ds->ops->get_rxnfc)
1070 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1073 static int dsa_slave_set_rxnfc(struct net_device *dev,
1074 struct ethtool_rxnfc *nfc)
1076 struct dsa_port *dp = dsa_slave_to_port(dev);
1077 struct dsa_switch *ds = dp->ds;
1079 if (!ds->ops->set_rxnfc)
1082 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1085 static int dsa_slave_get_ts_info(struct net_device *dev,
1086 struct ethtool_ts_info *ts)
1088 struct dsa_slave_priv *p = netdev_priv(dev);
1089 struct dsa_switch *ds = p->dp->ds;
1091 if (!ds->ops->get_ts_info)
1094 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1097 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1100 struct dsa_port *dp = dsa_slave_to_port(dev);
1101 struct bridge_vlan_info info;
1104 /* Check for a possible bridge VLAN entry now since there is no
1105 * need to emulate the switchdev prepare + commit phase.
1107 if (dp->bridge_dev) {
1108 if (!br_vlan_enabled(dp->bridge_dev))
1111 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1112 * device, respectively the VID is not found, returning
1113 * 0 means success, which is a failure for us here.
1115 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1120 ret = dsa_port_vid_add(dp, vid, 0);
1121 if (ret && ret != -EOPNOTSUPP)
1127 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1130 struct dsa_port *dp = dsa_slave_to_port(dev);
1131 struct bridge_vlan_info info;
1134 /* Check for a possible bridge VLAN entry now since there is no
1135 * need to emulate the switchdev prepare + commit phase.
1137 if (dp->bridge_dev) {
1138 if (!br_vlan_enabled(dp->bridge_dev))
1141 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1142 * device, respectively the VID is not found, returning
1143 * 0 means success, which is a failure for us here.
1145 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1150 ret = dsa_port_vid_del(dp, vid);
1151 if (ret == -EOPNOTSUPP)
1157 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1158 .get_drvinfo = dsa_slave_get_drvinfo,
1159 .get_regs_len = dsa_slave_get_regs_len,
1160 .get_regs = dsa_slave_get_regs,
1161 .nway_reset = dsa_slave_nway_reset,
1162 .get_link = ethtool_op_get_link,
1163 .get_eeprom_len = dsa_slave_get_eeprom_len,
1164 .get_eeprom = dsa_slave_get_eeprom,
1165 .set_eeprom = dsa_slave_set_eeprom,
1166 .get_strings = dsa_slave_get_strings,
1167 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1168 .get_sset_count = dsa_slave_get_sset_count,
1169 .set_wol = dsa_slave_set_wol,
1170 .get_wol = dsa_slave_get_wol,
1171 .set_eee = dsa_slave_set_eee,
1172 .get_eee = dsa_slave_get_eee,
1173 .get_link_ksettings = dsa_slave_get_link_ksettings,
1174 .set_link_ksettings = dsa_slave_set_link_ksettings,
1175 .get_rxnfc = dsa_slave_get_rxnfc,
1176 .set_rxnfc = dsa_slave_set_rxnfc,
1177 .get_ts_info = dsa_slave_get_ts_info,
1180 /* legacy way, bypassing the bridge *****************************************/
1181 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1182 struct net_device *dev,
1183 const unsigned char *addr, u16 vid,
1185 struct netlink_ext_ack *extack)
1187 struct dsa_port *dp = dsa_slave_to_port(dev);
1189 return dsa_port_fdb_add(dp, addr, vid);
1192 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1193 struct net_device *dev,
1194 const unsigned char *addr, u16 vid)
1196 struct dsa_port *dp = dsa_slave_to_port(dev);
1198 return dsa_port_fdb_del(dp, addr, vid);
1201 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1203 struct dsa_port *dp = dsa_slave_to_port(dev);
1205 return dp->ds->devlink ? &dp->devlink_port : NULL;
1208 static const struct net_device_ops dsa_slave_netdev_ops = {
1209 .ndo_open = dsa_slave_open,
1210 .ndo_stop = dsa_slave_close,
1211 .ndo_start_xmit = dsa_slave_xmit,
1212 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1213 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1214 .ndo_set_mac_address = dsa_slave_set_mac_address,
1215 .ndo_fdb_add = dsa_legacy_fdb_add,
1216 .ndo_fdb_del = dsa_legacy_fdb_del,
1217 .ndo_fdb_dump = dsa_slave_fdb_dump,
1218 .ndo_do_ioctl = dsa_slave_ioctl,
1219 .ndo_get_iflink = dsa_slave_get_iflink,
1220 #ifdef CONFIG_NET_POLL_CONTROLLER
1221 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1222 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1223 .ndo_poll_controller = dsa_slave_poll_controller,
1225 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1226 .ndo_setup_tc = dsa_slave_setup_tc,
1227 .ndo_get_stats64 = dsa_slave_get_stats64,
1228 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1229 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1230 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1231 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1234 static struct device_type dsa_type = {
1238 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1240 const struct dsa_port *dp = dsa_to_port(ds, port);
1242 phylink_mac_change(dp->pl, up);
1244 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1246 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1247 struct phylink_link_state *state)
1249 struct dsa_port *dp = dsa_slave_to_port(dev);
1250 struct dsa_switch *ds = dp->ds;
1252 /* No need to check that this operation is valid, the callback would
1253 * not be called if it was not.
1255 ds->ops->phylink_fixed_state(ds, dp->index, state);
1258 /* slave device setup *******************************************************/
1259 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1261 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1262 struct dsa_switch *ds = dp->ds;
1264 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1265 if (!slave_dev->phydev) {
1266 netdev_err(slave_dev, "no phy at %d\n", addr);
1270 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1273 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1275 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1276 struct device_node *port_dn = dp->dn;
1277 struct dsa_switch *ds = dp->ds;
1281 mode = of_get_phy_mode(port_dn);
1283 mode = PHY_INTERFACE_MODE_NA;
1285 dp->pl_config.dev = &slave_dev->dev;
1286 dp->pl_config.type = PHYLINK_NETDEV;
1288 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1289 &dsa_port_phylink_mac_ops);
1290 if (IS_ERR(dp->pl)) {
1291 netdev_err(slave_dev,
1292 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1293 return PTR_ERR(dp->pl);
1296 /* Register only if the switch provides such a callback, since this
1297 * callback takes precedence over polling the link GPIO in PHYLINK
1298 * (see phylink_get_fixed_state).
1300 if (ds->ops->phylink_fixed_state)
1301 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1303 if (ds->ops->get_phy_flags)
1304 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1306 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1307 if (ret == -ENODEV && ds->slave_mii_bus) {
1308 /* We could not connect to a designated PHY or SFP, so try to
1309 * use the switch internal MDIO bus instead
1311 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1313 netdev_err(slave_dev,
1314 "failed to connect to port %d: %d\n",
1316 phylink_destroy(dp->pl);
1324 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1325 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1326 struct netdev_queue *txq,
1329 lockdep_set_class(&txq->_xmit_lock,
1330 &dsa_slave_netdev_xmit_lock_key);
1333 int dsa_slave_suspend(struct net_device *slave_dev)
1335 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1337 if (!netif_running(slave_dev))
1340 cancel_work_sync(&dp->xmit_work);
1341 skb_queue_purge(&dp->xmit_queue);
1343 netif_device_detach(slave_dev);
1346 phylink_stop(dp->pl);
1352 int dsa_slave_resume(struct net_device *slave_dev)
1354 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1356 if (!netif_running(slave_dev))
1359 netif_device_attach(slave_dev);
1362 phylink_start(dp->pl);
1368 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1370 struct net_device *master = dsa_slave_to_master(dev);
1371 struct dsa_port *dp = dsa_slave_to_port(dev);
1372 struct dsa_notifier_register_info rinfo = {
1373 .switch_number = dp->ds->index,
1374 .port_number = dp->index,
1379 call_dsa_notifiers(val, dev, &rinfo.info);
1382 int dsa_slave_create(struct dsa_port *port)
1384 const struct dsa_port *cpu_dp = port->cpu_dp;
1385 struct net_device *master = cpu_dp->master;
1386 struct dsa_switch *ds = port->ds;
1387 const char *name = port->name;
1388 struct net_device *slave_dev;
1389 struct dsa_slave_priv *p;
1392 if (!ds->num_tx_queues)
1393 ds->num_tx_queues = 1;
1395 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1396 NET_NAME_UNKNOWN, ether_setup,
1397 ds->num_tx_queues, 1);
1398 if (slave_dev == NULL)
1401 slave_dev->features = master->vlan_features | NETIF_F_HW_TC |
1402 NETIF_F_HW_VLAN_CTAG_FILTER;
1403 slave_dev->hw_features |= NETIF_F_HW_TC;
1404 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1405 if (!IS_ERR_OR_NULL(port->mac))
1406 ether_addr_copy(slave_dev->dev_addr, port->mac);
1408 eth_hw_addr_inherit(slave_dev, master);
1409 slave_dev->priv_flags |= IFF_NO_QUEUE;
1410 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1411 slave_dev->min_mtu = 0;
1412 slave_dev->max_mtu = ETH_MAX_MTU;
1413 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1415 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1418 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1419 slave_dev->dev.of_node = port->dn;
1420 slave_dev->vlan_features = master->vlan_features;
1422 p = netdev_priv(slave_dev);
1423 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1425 free_netdev(slave_dev);
1429 INIT_LIST_HEAD(&p->mall_tc_list);
1430 INIT_WORK(&port->xmit_work, dsa_port_xmit_work);
1431 skb_queue_head_init(&port->xmit_queue);
1432 p->xmit = cpu_dp->tag_ops->xmit;
1433 port->slave = slave_dev;
1435 netif_carrier_off(slave_dev);
1437 ret = dsa_slave_phy_setup(slave_dev);
1439 netdev_err(master, "error %d setting up slave phy\n", ret);
1443 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1445 ret = register_netdev(slave_dev);
1447 netdev_err(master, "error %d registering interface %s\n",
1448 ret, slave_dev->name);
1456 phylink_disconnect_phy(p->dp->pl);
1458 phylink_destroy(p->dp->pl);
1460 free_percpu(p->stats64);
1461 free_netdev(slave_dev);
1466 void dsa_slave_destroy(struct net_device *slave_dev)
1468 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1469 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1471 netif_carrier_off(slave_dev);
1473 phylink_disconnect_phy(dp->pl);
1476 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1477 unregister_netdev(slave_dev);
1478 phylink_destroy(dp->pl);
1479 free_percpu(p->stats64);
1480 free_netdev(slave_dev);
1483 static bool dsa_slave_dev_check(const struct net_device *dev)
1485 return dev->netdev_ops == &dsa_slave_netdev_ops;
1488 static int dsa_slave_changeupper(struct net_device *dev,
1489 struct netdev_notifier_changeupper_info *info)
1491 struct dsa_port *dp = dsa_slave_to_port(dev);
1492 int err = NOTIFY_DONE;
1494 if (netif_is_bridge_master(info->upper_dev)) {
1495 if (info->linking) {
1496 err = dsa_port_bridge_join(dp, info->upper_dev);
1497 err = notifier_from_errno(err);
1499 dsa_port_bridge_leave(dp, info->upper_dev);
1507 static int dsa_slave_upper_vlan_check(struct net_device *dev,
1508 struct netdev_notifier_changeupper_info *
1511 struct netlink_ext_ack *ext_ack;
1512 struct net_device *slave;
1513 struct dsa_port *dp;
1515 ext_ack = netdev_notifier_info_to_extack(&info->info);
1517 if (!is_vlan_dev(dev))
1520 slave = vlan_dev_real_dev(dev);
1521 if (!dsa_slave_dev_check(slave))
1524 dp = dsa_slave_to_port(slave);
1525 if (!dp->bridge_dev)
1528 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1529 if (br_vlan_enabled(dp->bridge_dev) &&
1530 netif_is_bridge_master(info->upper_dev) && info->linking) {
1531 NL_SET_ERR_MSG_MOD(ext_ack,
1532 "Cannot enslave VLAN device into VLAN aware bridge");
1533 return notifier_from_errno(-EINVAL);
1539 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1540 unsigned long event, void *ptr)
1542 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1544 if (event == NETDEV_CHANGEUPPER) {
1545 if (!dsa_slave_dev_check(dev))
1546 return dsa_slave_upper_vlan_check(dev, ptr);
1548 return dsa_slave_changeupper(dev, ptr);
1554 struct dsa_switchdev_event_work {
1555 struct work_struct work;
1556 struct switchdev_notifier_fdb_info fdb_info;
1557 struct net_device *dev;
1558 unsigned long event;
1561 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1563 struct dsa_switchdev_event_work *switchdev_work =
1564 container_of(work, struct dsa_switchdev_event_work, work);
1565 struct net_device *dev = switchdev_work->dev;
1566 struct switchdev_notifier_fdb_info *fdb_info;
1567 struct dsa_port *dp = dsa_slave_to_port(dev);
1571 switch (switchdev_work->event) {
1572 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1573 fdb_info = &switchdev_work->fdb_info;
1574 if (!fdb_info->added_by_user)
1577 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1579 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1582 fdb_info->offloaded = true;
1583 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1584 &fdb_info->info, NULL);
1587 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1588 fdb_info = &switchdev_work->fdb_info;
1589 if (!fdb_info->added_by_user)
1592 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1594 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1601 kfree(switchdev_work->fdb_info.addr);
1602 kfree(switchdev_work);
1607 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1609 const struct switchdev_notifier_fdb_info *
1612 memcpy(&switchdev_work->fdb_info, fdb_info,
1613 sizeof(switchdev_work->fdb_info));
1614 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1615 if (!switchdev_work->fdb_info.addr)
1617 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1622 /* Called under rcu_read_lock() */
1623 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1624 unsigned long event, void *ptr)
1626 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1627 struct dsa_switchdev_event_work *switchdev_work;
1630 if (event == SWITCHDEV_PORT_ATTR_SET) {
1631 err = switchdev_handle_port_attr_set(dev, ptr,
1632 dsa_slave_dev_check,
1633 dsa_slave_port_attr_set);
1634 return notifier_from_errno(err);
1637 if (!dsa_slave_dev_check(dev))
1640 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1641 if (!switchdev_work)
1644 INIT_WORK(&switchdev_work->work,
1645 dsa_slave_switchdev_event_work);
1646 switchdev_work->dev = dev;
1647 switchdev_work->event = event;
1650 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1651 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1652 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1653 goto err_fdb_work_init;
1657 kfree(switchdev_work);
1661 dsa_schedule_work(&switchdev_work->work);
1665 kfree(switchdev_work);
1669 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1670 unsigned long event, void *ptr)
1672 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1676 case SWITCHDEV_PORT_OBJ_ADD:
1677 err = switchdev_handle_port_obj_add(dev, ptr,
1678 dsa_slave_dev_check,
1679 dsa_slave_port_obj_add);
1680 return notifier_from_errno(err);
1681 case SWITCHDEV_PORT_OBJ_DEL:
1682 err = switchdev_handle_port_obj_del(dev, ptr,
1683 dsa_slave_dev_check,
1684 dsa_slave_port_obj_del);
1685 return notifier_from_errno(err);
1686 case SWITCHDEV_PORT_ATTR_SET:
1687 err = switchdev_handle_port_attr_set(dev, ptr,
1688 dsa_slave_dev_check,
1689 dsa_slave_port_attr_set);
1690 return notifier_from_errno(err);
1696 static struct notifier_block dsa_slave_nb __read_mostly = {
1697 .notifier_call = dsa_slave_netdevice_event,
1700 static struct notifier_block dsa_slave_switchdev_notifier = {
1701 .notifier_call = dsa_slave_switchdev_event,
1704 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1705 .notifier_call = dsa_slave_switchdev_blocking_event,
1708 int dsa_slave_register_notifier(void)
1710 struct notifier_block *nb;
1713 err = register_netdevice_notifier(&dsa_slave_nb);
1717 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1719 goto err_switchdev_nb;
1721 nb = &dsa_slave_switchdev_blocking_notifier;
1722 err = register_switchdev_blocking_notifier(nb);
1724 goto err_switchdev_blocking_nb;
1728 err_switchdev_blocking_nb:
1729 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1731 unregister_netdevice_notifier(&dsa_slave_nb);
1735 void dsa_slave_unregister_notifier(void)
1737 struct notifier_block *nb;
1740 nb = &dsa_slave_switchdev_blocking_notifier;
1741 err = unregister_switchdev_blocking_notifier(nb);
1743 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1745 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1747 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1749 err = unregister_netdevice_notifier(&dsa_slave_nb);
1751 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);