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(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);
312 static int dsa_slave_port_obj_add(struct net_device *dev,
313 const struct switchdev_obj *obj,
314 struct switchdev_trans *trans)
316 struct dsa_port *dp = dsa_slave_to_port(dev);
319 /* For the prepare phase, ensure the full set of changes is feasable in
320 * one go in order to signal a failure properly. If an operation is not
321 * supported, return -EOPNOTSUPP.
325 case SWITCHDEV_OBJ_ID_PORT_MDB:
326 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
328 case SWITCHDEV_OBJ_ID_HOST_MDB:
329 /* DSA can directly translate this to a normal MDB add,
330 * but on the CPU port.
332 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
335 case SWITCHDEV_OBJ_ID_PORT_VLAN:
336 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
347 static int dsa_slave_port_obj_del(struct net_device *dev,
348 const struct switchdev_obj *obj)
350 struct dsa_port *dp = dsa_slave_to_port(dev);
354 case SWITCHDEV_OBJ_ID_PORT_MDB:
355 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
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_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
363 case SWITCHDEV_OBJ_ID_PORT_VLAN:
364 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
374 static int dsa_slave_get_port_parent_id(struct net_device *dev,
375 struct netdev_phys_item_id *ppid)
377 struct dsa_port *dp = dsa_slave_to_port(dev);
378 struct dsa_switch *ds = dp->ds;
379 struct dsa_switch_tree *dst = ds->dst;
381 /* For non-legacy ports, devlink is used and it takes
382 * care of the name generation. This ndo implementation
383 * should be removed with legacy support.
388 ppid->id_len = sizeof(dst->index);
389 memcpy(&ppid->id, &dst->index, ppid->id_len);
394 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
397 #ifdef CONFIG_NET_POLL_CONTROLLER
398 struct dsa_slave_priv *p = netdev_priv(dev);
401 netpoll_send_skb(p->netpoll, skb);
408 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
411 struct dsa_switch *ds = p->dp->ds;
412 struct sk_buff *clone;
415 type = ptp_classify_raw(skb);
416 if (type == PTP_CLASS_NONE)
419 if (!ds->ops->port_txtstamp)
422 clone = skb_clone_sk(skb);
426 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
432 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
434 /* SKB for netpoll still need to be mangled with the protocol-specific
435 * tag to be successfully transmitted
437 if (unlikely(netpoll_tx_running(dev)))
438 return dsa_slave_netpoll_send_skb(dev, skb);
440 /* Queue the SKB for transmission on the parent interface, but
441 * do not modify its EtherType
443 skb->dev = dsa_slave_to_master(dev);
448 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
450 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
452 struct dsa_slave_priv *p = netdev_priv(dev);
453 struct pcpu_sw_netstats *s;
454 struct sk_buff *nskb;
456 s = this_cpu_ptr(p->stats64);
457 u64_stats_update_begin(&s->syncp);
459 s->tx_bytes += skb->len;
460 u64_stats_update_end(&s->syncp);
462 DSA_SKB_CB(skb)->deferred_xmit = false;
464 /* Identify PTP protocol packets, clone them, and pass them to the
467 dsa_skb_tx_timestamp(p, skb);
469 /* Transmit function may have to reallocate the original SKB,
470 * in which case it must have freed it. Only free it here on error.
472 nskb = p->xmit(skb, dev);
474 if (!DSA_SKB_CB(skb)->deferred_xmit)
479 return dsa_enqueue_skb(nskb, dev);
482 void *dsa_defer_xmit(struct sk_buff *skb, struct net_device *dev)
484 struct dsa_port *dp = dsa_slave_to_port(dev);
486 DSA_SKB_CB(skb)->deferred_xmit = true;
488 skb_queue_tail(&dp->xmit_queue, skb);
489 schedule_work(&dp->xmit_work);
492 EXPORT_SYMBOL_GPL(dsa_defer_xmit);
494 static void dsa_port_xmit_work(struct work_struct *work)
496 struct dsa_port *dp = container_of(work, struct dsa_port, xmit_work);
497 struct dsa_switch *ds = dp->ds;
500 if (unlikely(!ds->ops->port_deferred_xmit))
503 while ((skb = skb_dequeue(&dp->xmit_queue)) != NULL)
504 ds->ops->port_deferred_xmit(ds, dp->index, skb);
507 /* ethtool operations *******************************************************/
509 static void dsa_slave_get_drvinfo(struct net_device *dev,
510 struct ethtool_drvinfo *drvinfo)
512 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
513 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
514 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
517 static int dsa_slave_get_regs_len(struct net_device *dev)
519 struct dsa_port *dp = dsa_slave_to_port(dev);
520 struct dsa_switch *ds = dp->ds;
522 if (ds->ops->get_regs_len)
523 return ds->ops->get_regs_len(ds, dp->index);
529 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
531 struct dsa_port *dp = dsa_slave_to_port(dev);
532 struct dsa_switch *ds = dp->ds;
534 if (ds->ops->get_regs)
535 ds->ops->get_regs(ds, dp->index, regs, _p);
538 static int dsa_slave_nway_reset(struct net_device *dev)
540 struct dsa_port *dp = dsa_slave_to_port(dev);
542 return phylink_ethtool_nway_reset(dp->pl);
545 static int dsa_slave_get_eeprom_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->cd && ds->cd->eeprom_len)
551 return ds->cd->eeprom_len;
553 if (ds->ops->get_eeprom_len)
554 return ds->ops->get_eeprom_len(ds);
559 static int dsa_slave_get_eeprom(struct net_device *dev,
560 struct ethtool_eeprom *eeprom, u8 *data)
562 struct dsa_port *dp = dsa_slave_to_port(dev);
563 struct dsa_switch *ds = dp->ds;
565 if (ds->ops->get_eeprom)
566 return ds->ops->get_eeprom(ds, eeprom, data);
571 static int dsa_slave_set_eeprom(struct net_device *dev,
572 struct ethtool_eeprom *eeprom, u8 *data)
574 struct dsa_port *dp = dsa_slave_to_port(dev);
575 struct dsa_switch *ds = dp->ds;
577 if (ds->ops->set_eeprom)
578 return ds->ops->set_eeprom(ds, eeprom, data);
583 static void dsa_slave_get_strings(struct net_device *dev,
584 uint32_t stringset, uint8_t *data)
586 struct dsa_port *dp = dsa_slave_to_port(dev);
587 struct dsa_switch *ds = dp->ds;
589 if (stringset == ETH_SS_STATS) {
590 int len = ETH_GSTRING_LEN;
592 strncpy(data, "tx_packets", len);
593 strncpy(data + len, "tx_bytes", len);
594 strncpy(data + 2 * len, "rx_packets", len);
595 strncpy(data + 3 * len, "rx_bytes", len);
596 if (ds->ops->get_strings)
597 ds->ops->get_strings(ds, dp->index, stringset,
602 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
603 struct ethtool_stats *stats,
606 struct dsa_port *dp = dsa_slave_to_port(dev);
607 struct dsa_slave_priv *p = netdev_priv(dev);
608 struct dsa_switch *ds = dp->ds;
609 struct pcpu_sw_netstats *s;
613 for_each_possible_cpu(i) {
614 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
616 s = per_cpu_ptr(p->stats64, i);
618 start = u64_stats_fetch_begin_irq(&s->syncp);
619 tx_packets = s->tx_packets;
620 tx_bytes = s->tx_bytes;
621 rx_packets = s->rx_packets;
622 rx_bytes = s->rx_bytes;
623 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
624 data[0] += tx_packets;
626 data[2] += rx_packets;
629 if (ds->ops->get_ethtool_stats)
630 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
633 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
635 struct dsa_port *dp = dsa_slave_to_port(dev);
636 struct dsa_switch *ds = dp->ds;
638 if (sset == ETH_SS_STATS) {
642 if (ds->ops->get_sset_count)
643 count += ds->ops->get_sset_count(ds, dp->index, sset);
651 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
653 struct dsa_port *dp = dsa_slave_to_port(dev);
654 struct dsa_switch *ds = dp->ds;
656 phylink_ethtool_get_wol(dp->pl, w);
658 if (ds->ops->get_wol)
659 ds->ops->get_wol(ds, dp->index, w);
662 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
664 struct dsa_port *dp = dsa_slave_to_port(dev);
665 struct dsa_switch *ds = dp->ds;
666 int ret = -EOPNOTSUPP;
668 phylink_ethtool_set_wol(dp->pl, w);
670 if (ds->ops->set_wol)
671 ret = ds->ops->set_wol(ds, dp->index, w);
676 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
678 struct dsa_port *dp = dsa_slave_to_port(dev);
679 struct dsa_switch *ds = dp->ds;
682 /* Port's PHY and MAC both need to be EEE capable */
683 if (!dev->phydev || !dp->pl)
686 if (!ds->ops->set_mac_eee)
689 ret = ds->ops->set_mac_eee(ds, dp->index, e);
693 return phylink_ethtool_set_eee(dp->pl, e);
696 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
698 struct dsa_port *dp = dsa_slave_to_port(dev);
699 struct dsa_switch *ds = dp->ds;
702 /* Port's PHY and MAC both need to be EEE capable */
703 if (!dev->phydev || !dp->pl)
706 if (!ds->ops->get_mac_eee)
709 ret = ds->ops->get_mac_eee(ds, dp->index, e);
713 return phylink_ethtool_get_eee(dp->pl, e);
716 static int dsa_slave_get_link_ksettings(struct net_device *dev,
717 struct ethtool_link_ksettings *cmd)
719 struct dsa_port *dp = dsa_slave_to_port(dev);
721 return phylink_ethtool_ksettings_get(dp->pl, cmd);
724 static int dsa_slave_set_link_ksettings(struct net_device *dev,
725 const struct ethtool_link_ksettings *cmd)
727 struct dsa_port *dp = dsa_slave_to_port(dev);
729 return phylink_ethtool_ksettings_set(dp->pl, cmd);
732 #ifdef CONFIG_NET_POLL_CONTROLLER
733 static int dsa_slave_netpoll_setup(struct net_device *dev,
734 struct netpoll_info *ni)
736 struct net_device *master = dsa_slave_to_master(dev);
737 struct dsa_slave_priv *p = netdev_priv(dev);
738 struct netpoll *netpoll;
741 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
745 err = __netpoll_setup(netpoll, master);
751 p->netpoll = netpoll;
756 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
758 struct dsa_slave_priv *p = netdev_priv(dev);
759 struct netpoll *netpoll = p->netpoll;
766 __netpoll_free(netpoll);
769 static void dsa_slave_poll_controller(struct net_device *dev)
774 static int dsa_slave_get_phys_port_name(struct net_device *dev,
775 char *name, size_t len)
777 struct dsa_port *dp = dsa_slave_to_port(dev);
779 /* For non-legacy ports, devlink is used and it takes
780 * care of the name generation. This ndo implementation
781 * should be removed with legacy support.
786 if (snprintf(name, len, "p%d", dp->index) >= len)
792 static struct dsa_mall_tc_entry *
793 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
795 struct dsa_slave_priv *p = netdev_priv(dev);
796 struct dsa_mall_tc_entry *mall_tc_entry;
798 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
799 if (mall_tc_entry->cookie == cookie)
800 return mall_tc_entry;
805 static int dsa_slave_add_cls_matchall(struct net_device *dev,
806 struct tc_cls_matchall_offload *cls,
809 struct dsa_port *dp = dsa_slave_to_port(dev);
810 struct dsa_slave_priv *p = netdev_priv(dev);
811 struct dsa_mall_tc_entry *mall_tc_entry;
812 __be16 protocol = cls->common.protocol;
813 struct dsa_switch *ds = dp->ds;
814 struct flow_action_entry *act;
815 struct dsa_port *to_dp;
816 int err = -EOPNOTSUPP;
818 if (!ds->ops->port_mirror_add)
821 if (!flow_offload_has_one_action(&cls->rule->action))
824 act = &cls->rule->action.entries[0];
826 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
827 struct dsa_mall_mirror_tc_entry *mirror;
832 if (!dsa_slave_dev_check(act->dev))
835 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
839 mall_tc_entry->cookie = cls->cookie;
840 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
841 mirror = &mall_tc_entry->mirror;
843 to_dp = dsa_slave_to_port(act->dev);
845 mirror->to_local_port = to_dp->index;
846 mirror->ingress = ingress;
848 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
850 kfree(mall_tc_entry);
854 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
860 static void dsa_slave_del_cls_matchall(struct net_device *dev,
861 struct tc_cls_matchall_offload *cls)
863 struct dsa_port *dp = dsa_slave_to_port(dev);
864 struct dsa_mall_tc_entry *mall_tc_entry;
865 struct dsa_switch *ds = dp->ds;
867 if (!ds->ops->port_mirror_del)
870 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
874 list_del(&mall_tc_entry->list);
876 switch (mall_tc_entry->type) {
877 case DSA_PORT_MALL_MIRROR:
878 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
884 kfree(mall_tc_entry);
887 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
888 struct tc_cls_matchall_offload *cls,
891 if (cls->common.chain_index)
894 switch (cls->command) {
895 case TC_CLSMATCHALL_REPLACE:
896 return dsa_slave_add_cls_matchall(dev, cls, ingress);
897 case TC_CLSMATCHALL_DESTROY:
898 dsa_slave_del_cls_matchall(dev, cls);
905 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
906 void *cb_priv, bool ingress)
908 struct net_device *dev = cb_priv;
910 if (!tc_can_offload(dev))
914 case TC_SETUP_CLSMATCHALL:
915 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
921 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
922 void *type_data, void *cb_priv)
924 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
927 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
928 void *type_data, void *cb_priv)
930 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
933 static int dsa_slave_setup_tc_block(struct net_device *dev,
934 struct tc_block_offload *f)
938 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
939 cb = dsa_slave_setup_tc_block_cb_ig;
940 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
941 cb = dsa_slave_setup_tc_block_cb_eg;
945 switch (f->command) {
947 return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
948 case TC_BLOCK_UNBIND:
949 tcf_block_cb_unregister(f->block, cb, dev);
956 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
961 return dsa_slave_setup_tc_block(dev, type_data);
967 static void dsa_slave_get_stats64(struct net_device *dev,
968 struct rtnl_link_stats64 *stats)
970 struct dsa_slave_priv *p = netdev_priv(dev);
971 struct pcpu_sw_netstats *s;
975 netdev_stats_to_stats64(stats, &dev->stats);
976 for_each_possible_cpu(i) {
977 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
979 s = per_cpu_ptr(p->stats64, i);
981 start = u64_stats_fetch_begin_irq(&s->syncp);
982 tx_packets = s->tx_packets;
983 tx_bytes = s->tx_bytes;
984 rx_packets = s->rx_packets;
985 rx_bytes = s->rx_bytes;
986 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
988 stats->tx_packets += tx_packets;
989 stats->tx_bytes += tx_bytes;
990 stats->rx_packets += rx_packets;
991 stats->rx_bytes += rx_bytes;
995 static int dsa_slave_get_rxnfc(struct net_device *dev,
996 struct ethtool_rxnfc *nfc, u32 *rule_locs)
998 struct dsa_port *dp = dsa_slave_to_port(dev);
999 struct dsa_switch *ds = dp->ds;
1001 if (!ds->ops->get_rxnfc)
1004 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1007 static int dsa_slave_set_rxnfc(struct net_device *dev,
1008 struct ethtool_rxnfc *nfc)
1010 struct dsa_port *dp = dsa_slave_to_port(dev);
1011 struct dsa_switch *ds = dp->ds;
1013 if (!ds->ops->set_rxnfc)
1016 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1019 static int dsa_slave_get_ts_info(struct net_device *dev,
1020 struct ethtool_ts_info *ts)
1022 struct dsa_slave_priv *p = netdev_priv(dev);
1023 struct dsa_switch *ds = p->dp->ds;
1025 if (!ds->ops->get_ts_info)
1028 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1031 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1034 struct dsa_port *dp = dsa_slave_to_port(dev);
1035 struct bridge_vlan_info info;
1038 /* Check for a possible bridge VLAN entry now since there is no
1039 * need to emulate the switchdev prepare + commit phase.
1041 if (dp->bridge_dev) {
1042 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1043 * device, respectively the VID is not found, returning
1044 * 0 means success, which is a failure for us here.
1046 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1051 /* This API only allows programming tagged, non-PVID VIDs */
1052 return dsa_port_vid_add(dp, vid, 0);
1055 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1058 struct dsa_port *dp = dsa_slave_to_port(dev);
1059 struct bridge_vlan_info info;
1062 /* Check for a possible bridge VLAN entry now since there is no
1063 * need to emulate the switchdev prepare + commit phase.
1065 if (dp->bridge_dev) {
1066 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1067 * device, respectively the VID is not found, returning
1068 * 0 means success, which is a failure for us here.
1070 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1075 ret = dsa_port_vid_del(dp, vid);
1076 if (ret == -EOPNOTSUPP)
1082 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1083 .get_drvinfo = dsa_slave_get_drvinfo,
1084 .get_regs_len = dsa_slave_get_regs_len,
1085 .get_regs = dsa_slave_get_regs,
1086 .nway_reset = dsa_slave_nway_reset,
1087 .get_link = ethtool_op_get_link,
1088 .get_eeprom_len = dsa_slave_get_eeprom_len,
1089 .get_eeprom = dsa_slave_get_eeprom,
1090 .set_eeprom = dsa_slave_set_eeprom,
1091 .get_strings = dsa_slave_get_strings,
1092 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1093 .get_sset_count = dsa_slave_get_sset_count,
1094 .set_wol = dsa_slave_set_wol,
1095 .get_wol = dsa_slave_get_wol,
1096 .set_eee = dsa_slave_set_eee,
1097 .get_eee = dsa_slave_get_eee,
1098 .get_link_ksettings = dsa_slave_get_link_ksettings,
1099 .set_link_ksettings = dsa_slave_set_link_ksettings,
1100 .get_rxnfc = dsa_slave_get_rxnfc,
1101 .set_rxnfc = dsa_slave_set_rxnfc,
1102 .get_ts_info = dsa_slave_get_ts_info,
1105 /* legacy way, bypassing the bridge *****************************************/
1106 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1107 struct net_device *dev,
1108 const unsigned char *addr, u16 vid,
1110 struct netlink_ext_ack *extack)
1112 struct dsa_port *dp = dsa_slave_to_port(dev);
1114 return dsa_port_fdb_add(dp, addr, vid);
1117 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1118 struct net_device *dev,
1119 const unsigned char *addr, u16 vid)
1121 struct dsa_port *dp = dsa_slave_to_port(dev);
1123 return dsa_port_fdb_del(dp, addr, vid);
1126 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1128 struct dsa_port *dp = dsa_slave_to_port(dev);
1130 return dp->ds->devlink ? &dp->devlink_port : NULL;
1133 static const struct net_device_ops dsa_slave_netdev_ops = {
1134 .ndo_open = dsa_slave_open,
1135 .ndo_stop = dsa_slave_close,
1136 .ndo_start_xmit = dsa_slave_xmit,
1137 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1138 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1139 .ndo_set_mac_address = dsa_slave_set_mac_address,
1140 .ndo_fdb_add = dsa_legacy_fdb_add,
1141 .ndo_fdb_del = dsa_legacy_fdb_del,
1142 .ndo_fdb_dump = dsa_slave_fdb_dump,
1143 .ndo_do_ioctl = dsa_slave_ioctl,
1144 .ndo_get_iflink = dsa_slave_get_iflink,
1145 #ifdef CONFIG_NET_POLL_CONTROLLER
1146 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1147 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1148 .ndo_poll_controller = dsa_slave_poll_controller,
1150 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1151 .ndo_setup_tc = dsa_slave_setup_tc,
1152 .ndo_get_stats64 = dsa_slave_get_stats64,
1153 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1154 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1155 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1156 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1159 static struct device_type dsa_type = {
1163 static void dsa_slave_phylink_validate(struct net_device *dev,
1164 unsigned long *supported,
1165 struct phylink_link_state *state)
1167 struct dsa_port *dp = dsa_slave_to_port(dev);
1168 struct dsa_switch *ds = dp->ds;
1170 if (!ds->ops->phylink_validate)
1173 ds->ops->phylink_validate(ds, dp->index, supported, state);
1176 static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1177 struct phylink_link_state *state)
1179 struct dsa_port *dp = dsa_slave_to_port(dev);
1180 struct dsa_switch *ds = dp->ds;
1182 /* Only called for SGMII and 802.3z */
1183 if (!ds->ops->phylink_mac_link_state)
1186 return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1189 static void dsa_slave_phylink_mac_config(struct net_device *dev,
1191 const struct phylink_link_state *state)
1193 struct dsa_port *dp = dsa_slave_to_port(dev);
1194 struct dsa_switch *ds = dp->ds;
1196 if (!ds->ops->phylink_mac_config)
1199 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1202 static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1204 struct dsa_port *dp = dsa_slave_to_port(dev);
1205 struct dsa_switch *ds = dp->ds;
1207 if (!ds->ops->phylink_mac_an_restart)
1210 ds->ops->phylink_mac_an_restart(ds, dp->index);
1213 static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1215 phy_interface_t interface)
1217 struct dsa_port *dp = dsa_slave_to_port(dev);
1218 struct dsa_switch *ds = dp->ds;
1220 if (!ds->ops->phylink_mac_link_down) {
1221 if (ds->ops->adjust_link && dev->phydev)
1222 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1226 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1229 static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1231 phy_interface_t interface,
1232 struct phy_device *phydev)
1234 struct dsa_port *dp = dsa_slave_to_port(dev);
1235 struct dsa_switch *ds = dp->ds;
1237 if (!ds->ops->phylink_mac_link_up) {
1238 if (ds->ops->adjust_link && dev->phydev)
1239 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1243 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1246 static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1247 .validate = dsa_slave_phylink_validate,
1248 .mac_link_state = dsa_slave_phylink_mac_link_state,
1249 .mac_config = dsa_slave_phylink_mac_config,
1250 .mac_an_restart = dsa_slave_phylink_mac_an_restart,
1251 .mac_link_down = dsa_slave_phylink_mac_link_down,
1252 .mac_link_up = dsa_slave_phylink_mac_link_up,
1255 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1257 const struct dsa_port *dp = dsa_to_port(ds, port);
1259 phylink_mac_change(dp->pl, up);
1261 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1263 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1264 struct phylink_link_state *state)
1266 struct dsa_port *dp = dsa_slave_to_port(dev);
1267 struct dsa_switch *ds = dp->ds;
1269 /* No need to check that this operation is valid, the callback would
1270 * not be called if it was not.
1272 ds->ops->phylink_fixed_state(ds, dp->index, state);
1275 /* slave device setup *******************************************************/
1276 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1278 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1279 struct dsa_switch *ds = dp->ds;
1281 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1282 if (!slave_dev->phydev) {
1283 netdev_err(slave_dev, "no phy at %d\n", addr);
1287 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1290 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1292 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1293 struct device_node *port_dn = dp->dn;
1294 struct dsa_switch *ds = dp->ds;
1298 mode = of_get_phy_mode(port_dn);
1300 mode = PHY_INTERFACE_MODE_NA;
1302 dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1303 &dsa_slave_phylink_mac_ops);
1304 if (IS_ERR(dp->pl)) {
1305 netdev_err(slave_dev,
1306 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1307 return PTR_ERR(dp->pl);
1310 /* Register only if the switch provides such a callback, since this
1311 * callback takes precedence over polling the link GPIO in PHYLINK
1312 * (see phylink_get_fixed_state).
1314 if (ds->ops->phylink_fixed_state)
1315 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1317 if (ds->ops->get_phy_flags)
1318 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1320 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1321 if (ret == -ENODEV && ds->slave_mii_bus) {
1322 /* We could not connect to a designated PHY or SFP, so try to
1323 * use the switch internal MDIO bus instead
1325 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1327 netdev_err(slave_dev,
1328 "failed to connect to port %d: %d\n",
1330 phylink_destroy(dp->pl);
1338 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1339 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1340 struct netdev_queue *txq,
1343 lockdep_set_class(&txq->_xmit_lock,
1344 &dsa_slave_netdev_xmit_lock_key);
1347 int dsa_slave_suspend(struct net_device *slave_dev)
1349 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1351 if (!netif_running(slave_dev))
1354 cancel_work_sync(&dp->xmit_work);
1355 skb_queue_purge(&dp->xmit_queue);
1357 netif_device_detach(slave_dev);
1360 phylink_stop(dp->pl);
1366 int dsa_slave_resume(struct net_device *slave_dev)
1368 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1370 if (!netif_running(slave_dev))
1373 netif_device_attach(slave_dev);
1376 phylink_start(dp->pl);
1382 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1384 struct net_device *master = dsa_slave_to_master(dev);
1385 struct dsa_port *dp = dsa_slave_to_port(dev);
1386 struct dsa_notifier_register_info rinfo = {
1387 .switch_number = dp->ds->index,
1388 .port_number = dp->index,
1393 call_dsa_notifiers(val, dev, &rinfo.info);
1396 int dsa_slave_create(struct dsa_port *port)
1398 const struct dsa_port *cpu_dp = port->cpu_dp;
1399 struct net_device *master = cpu_dp->master;
1400 struct dsa_switch *ds = port->ds;
1401 const char *name = port->name;
1402 struct net_device *slave_dev;
1403 struct dsa_slave_priv *p;
1406 if (!ds->num_tx_queues)
1407 ds->num_tx_queues = 1;
1409 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1410 NET_NAME_UNKNOWN, ether_setup,
1411 ds->num_tx_queues, 1);
1412 if (slave_dev == NULL)
1415 slave_dev->features = master->vlan_features | NETIF_F_HW_TC |
1416 NETIF_F_HW_VLAN_CTAG_FILTER;
1417 slave_dev->hw_features |= NETIF_F_HW_TC;
1418 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1419 if (!IS_ERR_OR_NULL(port->mac))
1420 ether_addr_copy(slave_dev->dev_addr, port->mac);
1422 eth_hw_addr_inherit(slave_dev, master);
1423 slave_dev->priv_flags |= IFF_NO_QUEUE;
1424 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1425 slave_dev->min_mtu = 0;
1426 slave_dev->max_mtu = ETH_MAX_MTU;
1427 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1429 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1432 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1433 slave_dev->dev.of_node = port->dn;
1434 slave_dev->vlan_features = master->vlan_features;
1436 p = netdev_priv(slave_dev);
1437 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1439 free_netdev(slave_dev);
1443 INIT_LIST_HEAD(&p->mall_tc_list);
1444 INIT_WORK(&port->xmit_work, dsa_port_xmit_work);
1445 skb_queue_head_init(&port->xmit_queue);
1446 p->xmit = cpu_dp->tag_ops->xmit;
1447 port->slave = slave_dev;
1449 netif_carrier_off(slave_dev);
1451 ret = dsa_slave_phy_setup(slave_dev);
1453 netdev_err(master, "error %d setting up slave phy\n", ret);
1457 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1459 ret = register_netdev(slave_dev);
1461 netdev_err(master, "error %d registering interface %s\n",
1462 ret, slave_dev->name);
1470 phylink_disconnect_phy(p->dp->pl);
1472 phylink_destroy(p->dp->pl);
1474 free_percpu(p->stats64);
1475 free_netdev(slave_dev);
1480 void dsa_slave_destroy(struct net_device *slave_dev)
1482 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1483 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1485 netif_carrier_off(slave_dev);
1487 phylink_disconnect_phy(dp->pl);
1490 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1491 unregister_netdev(slave_dev);
1492 phylink_destroy(dp->pl);
1493 free_percpu(p->stats64);
1494 free_netdev(slave_dev);
1497 static bool dsa_slave_dev_check(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);
1569 dsa_slave_switchdev_port_attr_set_event(struct net_device *netdev,
1570 struct switchdev_notifier_port_attr_info *port_attr_info)
1574 err = dsa_slave_port_attr_set(netdev, port_attr_info->attr,
1575 port_attr_info->trans);
1577 port_attr_info->handled = true;
1578 return notifier_from_errno(err);
1581 struct dsa_switchdev_event_work {
1582 struct work_struct work;
1583 struct switchdev_notifier_fdb_info fdb_info;
1584 struct net_device *dev;
1585 unsigned long event;
1588 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1590 struct dsa_switchdev_event_work *switchdev_work =
1591 container_of(work, struct dsa_switchdev_event_work, work);
1592 struct net_device *dev = switchdev_work->dev;
1593 struct switchdev_notifier_fdb_info *fdb_info;
1594 struct dsa_port *dp = dsa_slave_to_port(dev);
1598 switch (switchdev_work->event) {
1599 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1600 fdb_info = &switchdev_work->fdb_info;
1601 if (!fdb_info->added_by_user)
1604 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1606 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1609 fdb_info->offloaded = true;
1610 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1611 &fdb_info->info, NULL);
1614 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1615 fdb_info = &switchdev_work->fdb_info;
1616 if (!fdb_info->added_by_user)
1619 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1621 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1628 kfree(switchdev_work->fdb_info.addr);
1629 kfree(switchdev_work);
1634 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1636 const struct switchdev_notifier_fdb_info *
1639 memcpy(&switchdev_work->fdb_info, fdb_info,
1640 sizeof(switchdev_work->fdb_info));
1641 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1642 if (!switchdev_work->fdb_info.addr)
1644 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1649 /* Called under rcu_read_lock() */
1650 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1651 unsigned long event, void *ptr)
1653 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1654 struct dsa_switchdev_event_work *switchdev_work;
1656 if (!dsa_slave_dev_check(dev))
1659 if (event == SWITCHDEV_PORT_ATTR_SET)
1660 return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
1662 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1663 if (!switchdev_work)
1666 INIT_WORK(&switchdev_work->work,
1667 dsa_slave_switchdev_event_work);
1668 switchdev_work->dev = dev;
1669 switchdev_work->event = event;
1672 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1673 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1674 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1675 goto err_fdb_work_init;
1679 kfree(switchdev_work);
1683 dsa_schedule_work(&switchdev_work->work);
1687 kfree(switchdev_work);
1692 dsa_slave_switchdev_port_obj_event(unsigned long event,
1693 struct net_device *netdev,
1694 struct switchdev_notifier_port_obj_info *port_obj_info)
1696 int err = -EOPNOTSUPP;
1699 case SWITCHDEV_PORT_OBJ_ADD:
1700 err = dsa_slave_port_obj_add(netdev, port_obj_info->obj,
1701 port_obj_info->trans);
1703 case SWITCHDEV_PORT_OBJ_DEL:
1704 err = dsa_slave_port_obj_del(netdev, port_obj_info->obj);
1708 port_obj_info->handled = true;
1709 return notifier_from_errno(err);
1712 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1713 unsigned long event, void *ptr)
1715 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1717 if (!dsa_slave_dev_check(dev))
1721 case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
1722 case SWITCHDEV_PORT_OBJ_DEL:
1723 return dsa_slave_switchdev_port_obj_event(event, dev, ptr);
1724 case SWITCHDEV_PORT_ATTR_SET:
1725 return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
1731 static struct notifier_block dsa_slave_nb __read_mostly = {
1732 .notifier_call = dsa_slave_netdevice_event,
1735 static struct notifier_block dsa_slave_switchdev_notifier = {
1736 .notifier_call = dsa_slave_switchdev_event,
1739 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1740 .notifier_call = dsa_slave_switchdev_blocking_event,
1743 int dsa_slave_register_notifier(void)
1745 struct notifier_block *nb;
1748 err = register_netdevice_notifier(&dsa_slave_nb);
1752 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1754 goto err_switchdev_nb;
1756 nb = &dsa_slave_switchdev_blocking_notifier;
1757 err = register_switchdev_blocking_notifier(nb);
1759 goto err_switchdev_blocking_nb;
1763 err_switchdev_blocking_nb:
1764 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1766 unregister_netdevice_notifier(&dsa_slave_nb);
1770 void dsa_slave_unregister_notifier(void)
1772 struct notifier_block *nb;
1775 nb = &dsa_slave_switchdev_blocking_notifier;
1776 err = unregister_switchdev_blocking_notifier(nb);
1778 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1780 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1782 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1784 err = unregister_netdevice_notifier(&dsa_slave_nb);
1786 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);