2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/list.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/phylink.h>
17 #include <linux/of_net.h>
18 #include <linux/of_mdio.h>
19 #include <linux/mdio.h>
20 #include <net/rtnetlink.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_mirred.h>
23 #include <linux/if_bridge.h>
24 #include <linux/netpoll.h>
25 #include <linux/ptp_classify.h>
29 static bool dsa_slave_dev_check(struct net_device *dev);
31 /* slave mii_bus handling ***************************************************/
32 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
34 struct dsa_switch *ds = bus->priv;
36 if (ds->phys_mii_mask & (1 << addr))
37 return ds->ops->phy_read(ds, addr, reg);
42 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
44 struct dsa_switch *ds = bus->priv;
46 if (ds->phys_mii_mask & (1 << addr))
47 return ds->ops->phy_write(ds, addr, reg, val);
52 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
54 ds->slave_mii_bus->priv = (void *)ds;
55 ds->slave_mii_bus->name = "dsa slave smi";
56 ds->slave_mii_bus->read = dsa_slave_phy_read;
57 ds->slave_mii_bus->write = dsa_slave_phy_write;
58 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
59 ds->dst->index, ds->index);
60 ds->slave_mii_bus->parent = ds->dev;
61 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
65 /* slave device handling ****************************************************/
66 static int dsa_slave_get_iflink(const struct net_device *dev)
68 return dsa_slave_to_master(dev)->ifindex;
71 static int dsa_slave_open(struct net_device *dev)
73 struct net_device *master = dsa_slave_to_master(dev);
74 struct dsa_port *dp = dsa_slave_to_port(dev);
77 if (!(master->flags & IFF_UP))
80 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
81 err = dev_uc_add(master, dev->dev_addr);
86 if (dev->flags & IFF_ALLMULTI) {
87 err = dev_set_allmulti(master, 1);
91 if (dev->flags & IFF_PROMISC) {
92 err = dev_set_promiscuity(master, 1);
97 err = dsa_port_enable(dp, dev->phydev);
101 phylink_start(dp->pl);
106 if (dev->flags & IFF_PROMISC)
107 dev_set_promiscuity(master, -1);
109 if (dev->flags & IFF_ALLMULTI)
110 dev_set_allmulti(master, -1);
112 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
113 dev_uc_del(master, dev->dev_addr);
118 static int dsa_slave_close(struct net_device *dev)
120 struct net_device *master = dsa_slave_to_master(dev);
121 struct dsa_port *dp = dsa_slave_to_port(dev);
123 cancel_work_sync(&dp->xmit_work);
124 skb_queue_purge(&dp->xmit_queue);
126 phylink_stop(dp->pl);
128 dsa_port_disable(dp);
130 dev_mc_unsync(master, dev);
131 dev_uc_unsync(master, dev);
132 if (dev->flags & IFF_ALLMULTI)
133 dev_set_allmulti(master, -1);
134 if (dev->flags & IFF_PROMISC)
135 dev_set_promiscuity(master, -1);
137 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
138 dev_uc_del(master, dev->dev_addr);
143 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
145 struct net_device *master = dsa_slave_to_master(dev);
146 if (dev->flags & IFF_UP) {
147 if (change & IFF_ALLMULTI)
148 dev_set_allmulti(master,
149 dev->flags & IFF_ALLMULTI ? 1 : -1);
150 if (change & IFF_PROMISC)
151 dev_set_promiscuity(master,
152 dev->flags & IFF_PROMISC ? 1 : -1);
156 static void dsa_slave_set_rx_mode(struct net_device *dev)
158 struct net_device *master = dsa_slave_to_master(dev);
160 dev_mc_sync(master, dev);
161 dev_uc_sync(master, dev);
164 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
166 struct net_device *master = dsa_slave_to_master(dev);
167 struct sockaddr *addr = a;
170 if (!is_valid_ether_addr(addr->sa_data))
171 return -EADDRNOTAVAIL;
173 if (!(dev->flags & IFF_UP))
176 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
177 err = dev_uc_add(master, addr->sa_data);
182 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
183 dev_uc_del(master, dev->dev_addr);
186 ether_addr_copy(dev->dev_addr, addr->sa_data);
191 struct dsa_slave_dump_ctx {
192 struct net_device *dev;
194 struct netlink_callback *cb;
199 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
200 bool is_static, void *data)
202 struct dsa_slave_dump_ctx *dump = data;
203 u32 portid = NETLINK_CB(dump->cb->skb).portid;
204 u32 seq = dump->cb->nlh->nlmsg_seq;
205 struct nlmsghdr *nlh;
208 if (dump->idx < dump->cb->args[2])
211 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
212 sizeof(*ndm), NLM_F_MULTI);
216 ndm = nlmsg_data(nlh);
217 ndm->ndm_family = AF_BRIDGE;
220 ndm->ndm_flags = NTF_SELF;
222 ndm->ndm_ifindex = dump->dev->ifindex;
223 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
225 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
226 goto nla_put_failure;
228 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
229 goto nla_put_failure;
231 nlmsg_end(dump->skb, nlh);
238 nlmsg_cancel(dump->skb, nlh);
243 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
244 struct net_device *dev, struct net_device *filter_dev,
247 struct dsa_port *dp = dsa_slave_to_port(dev);
248 struct dsa_slave_dump_ctx dump = {
256 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
262 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
264 struct dsa_slave_priv *p = netdev_priv(dev);
265 struct dsa_switch *ds = p->dp->ds;
266 int port = p->dp->index;
268 /* Pass through to switch driver if it supports timestamping */
271 if (ds->ops->port_hwtstamp_get)
272 return ds->ops->port_hwtstamp_get(ds, port, ifr);
275 if (ds->ops->port_hwtstamp_set)
276 return ds->ops->port_hwtstamp_set(ds, port, ifr);
280 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
283 static int dsa_slave_port_attr_set(struct net_device *dev,
284 const struct switchdev_attr *attr,
285 struct switchdev_trans *trans)
287 struct dsa_port *dp = dsa_slave_to_port(dev);
291 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
292 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
294 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
295 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
298 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
299 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
301 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
302 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
305 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
306 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
316 static int dsa_slave_port_obj_add(struct net_device *dev,
317 const struct switchdev_obj *obj,
318 struct switchdev_trans *trans)
320 struct dsa_port *dp = dsa_slave_to_port(dev);
323 /* For the prepare phase, ensure the full set of changes is feasable in
324 * one go in order to signal a failure properly. If an operation is not
325 * supported, return -EOPNOTSUPP.
329 case SWITCHDEV_OBJ_ID_PORT_MDB:
330 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
332 case SWITCHDEV_OBJ_ID_HOST_MDB:
333 /* DSA can directly translate this to a normal MDB add,
334 * but on the CPU port.
336 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
339 case SWITCHDEV_OBJ_ID_PORT_VLAN:
340 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
351 static int dsa_slave_port_obj_del(struct net_device *dev,
352 const struct switchdev_obj *obj)
354 struct dsa_port *dp = dsa_slave_to_port(dev);
358 case SWITCHDEV_OBJ_ID_PORT_MDB:
359 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
361 case SWITCHDEV_OBJ_ID_HOST_MDB:
362 /* DSA can directly translate this to a normal MDB add,
363 * but on the CPU port.
365 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
367 case SWITCHDEV_OBJ_ID_PORT_VLAN:
368 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
378 static int dsa_slave_get_port_parent_id(struct net_device *dev,
379 struct netdev_phys_item_id *ppid)
381 struct dsa_port *dp = dsa_slave_to_port(dev);
382 struct dsa_switch *ds = dp->ds;
383 struct dsa_switch_tree *dst = ds->dst;
385 /* For non-legacy ports, devlink is used and it takes
386 * care of the name generation. This ndo implementation
387 * should be removed with legacy support.
392 ppid->id_len = sizeof(dst->index);
393 memcpy(&ppid->id, &dst->index, ppid->id_len);
398 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
401 #ifdef CONFIG_NET_POLL_CONTROLLER
402 struct dsa_slave_priv *p = netdev_priv(dev);
405 netpoll_send_skb(p->netpoll, skb);
412 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
415 struct dsa_switch *ds = p->dp->ds;
416 struct sk_buff *clone;
419 type = ptp_classify_raw(skb);
420 if (type == PTP_CLASS_NONE)
423 if (!ds->ops->port_txtstamp)
426 clone = skb_clone_sk(skb);
430 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
436 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
438 /* SKB for netpoll still need to be mangled with the protocol-specific
439 * tag to be successfully transmitted
441 if (unlikely(netpoll_tx_running(dev)))
442 return dsa_slave_netpoll_send_skb(dev, skb);
444 /* Queue the SKB for transmission on the parent interface, but
445 * do not modify its EtherType
447 skb->dev = dsa_slave_to_master(dev);
452 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
454 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
456 struct dsa_slave_priv *p = netdev_priv(dev);
457 struct pcpu_sw_netstats *s;
458 struct sk_buff *nskb;
460 s = this_cpu_ptr(p->stats64);
461 u64_stats_update_begin(&s->syncp);
463 s->tx_bytes += skb->len;
464 u64_stats_update_end(&s->syncp);
466 DSA_SKB_CB(skb)->deferred_xmit = false;
468 /* Identify PTP protocol packets, clone them, and pass them to the
471 dsa_skb_tx_timestamp(p, skb);
473 /* Transmit function may have to reallocate the original SKB,
474 * in which case it must have freed it. Only free it here on error.
476 nskb = p->xmit(skb, dev);
478 if (!DSA_SKB_CB(skb)->deferred_xmit)
483 return dsa_enqueue_skb(nskb, dev);
486 void *dsa_defer_xmit(struct sk_buff *skb, struct net_device *dev)
488 struct dsa_port *dp = dsa_slave_to_port(dev);
490 DSA_SKB_CB(skb)->deferred_xmit = true;
492 skb_queue_tail(&dp->xmit_queue, skb);
493 schedule_work(&dp->xmit_work);
496 EXPORT_SYMBOL_GPL(dsa_defer_xmit);
498 static void dsa_port_xmit_work(struct work_struct *work)
500 struct dsa_port *dp = container_of(work, struct dsa_port, xmit_work);
501 struct dsa_switch *ds = dp->ds;
504 if (unlikely(!ds->ops->port_deferred_xmit))
507 while ((skb = skb_dequeue(&dp->xmit_queue)) != NULL)
508 ds->ops->port_deferred_xmit(ds, dp->index, skb);
511 /* ethtool operations *******************************************************/
513 static void dsa_slave_get_drvinfo(struct net_device *dev,
514 struct ethtool_drvinfo *drvinfo)
516 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
517 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
518 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
521 static int dsa_slave_get_regs_len(struct net_device *dev)
523 struct dsa_port *dp = dsa_slave_to_port(dev);
524 struct dsa_switch *ds = dp->ds;
526 if (ds->ops->get_regs_len)
527 return ds->ops->get_regs_len(ds, dp->index);
533 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
535 struct dsa_port *dp = dsa_slave_to_port(dev);
536 struct dsa_switch *ds = dp->ds;
538 if (ds->ops->get_regs)
539 ds->ops->get_regs(ds, dp->index, regs, _p);
542 static int dsa_slave_nway_reset(struct net_device *dev)
544 struct dsa_port *dp = dsa_slave_to_port(dev);
546 return phylink_ethtool_nway_reset(dp->pl);
549 static int dsa_slave_get_eeprom_len(struct net_device *dev)
551 struct dsa_port *dp = dsa_slave_to_port(dev);
552 struct dsa_switch *ds = dp->ds;
554 if (ds->cd && ds->cd->eeprom_len)
555 return ds->cd->eeprom_len;
557 if (ds->ops->get_eeprom_len)
558 return ds->ops->get_eeprom_len(ds);
563 static int dsa_slave_get_eeprom(struct net_device *dev,
564 struct ethtool_eeprom *eeprom, u8 *data)
566 struct dsa_port *dp = dsa_slave_to_port(dev);
567 struct dsa_switch *ds = dp->ds;
569 if (ds->ops->get_eeprom)
570 return ds->ops->get_eeprom(ds, eeprom, data);
575 static int dsa_slave_set_eeprom(struct net_device *dev,
576 struct ethtool_eeprom *eeprom, u8 *data)
578 struct dsa_port *dp = dsa_slave_to_port(dev);
579 struct dsa_switch *ds = dp->ds;
581 if (ds->ops->set_eeprom)
582 return ds->ops->set_eeprom(ds, eeprom, data);
587 static void dsa_slave_get_strings(struct net_device *dev,
588 uint32_t stringset, uint8_t *data)
590 struct dsa_port *dp = dsa_slave_to_port(dev);
591 struct dsa_switch *ds = dp->ds;
593 if (stringset == ETH_SS_STATS) {
594 int len = ETH_GSTRING_LEN;
596 strncpy(data, "tx_packets", len);
597 strncpy(data + len, "tx_bytes", len);
598 strncpy(data + 2 * len, "rx_packets", len);
599 strncpy(data + 3 * len, "rx_bytes", len);
600 if (ds->ops->get_strings)
601 ds->ops->get_strings(ds, dp->index, stringset,
606 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
607 struct ethtool_stats *stats,
610 struct dsa_port *dp = dsa_slave_to_port(dev);
611 struct dsa_slave_priv *p = netdev_priv(dev);
612 struct dsa_switch *ds = dp->ds;
613 struct pcpu_sw_netstats *s;
617 for_each_possible_cpu(i) {
618 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
620 s = per_cpu_ptr(p->stats64, i);
622 start = u64_stats_fetch_begin_irq(&s->syncp);
623 tx_packets = s->tx_packets;
624 tx_bytes = s->tx_bytes;
625 rx_packets = s->rx_packets;
626 rx_bytes = s->rx_bytes;
627 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
628 data[0] += tx_packets;
630 data[2] += rx_packets;
633 if (ds->ops->get_ethtool_stats)
634 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
637 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
639 struct dsa_port *dp = dsa_slave_to_port(dev);
640 struct dsa_switch *ds = dp->ds;
642 if (sset == ETH_SS_STATS) {
646 if (ds->ops->get_sset_count)
647 count += ds->ops->get_sset_count(ds, dp->index, sset);
655 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
657 struct dsa_port *dp = dsa_slave_to_port(dev);
658 struct dsa_switch *ds = dp->ds;
660 phylink_ethtool_get_wol(dp->pl, w);
662 if (ds->ops->get_wol)
663 ds->ops->get_wol(ds, dp->index, w);
666 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
668 struct dsa_port *dp = dsa_slave_to_port(dev);
669 struct dsa_switch *ds = dp->ds;
670 int ret = -EOPNOTSUPP;
672 phylink_ethtool_set_wol(dp->pl, w);
674 if (ds->ops->set_wol)
675 ret = ds->ops->set_wol(ds, dp->index, w);
680 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
682 struct dsa_port *dp = dsa_slave_to_port(dev);
683 struct dsa_switch *ds = dp->ds;
686 /* Port's PHY and MAC both need to be EEE capable */
687 if (!dev->phydev || !dp->pl)
690 if (!ds->ops->set_mac_eee)
693 ret = ds->ops->set_mac_eee(ds, dp->index, e);
697 return phylink_ethtool_set_eee(dp->pl, e);
700 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
702 struct dsa_port *dp = dsa_slave_to_port(dev);
703 struct dsa_switch *ds = dp->ds;
706 /* Port's PHY and MAC both need to be EEE capable */
707 if (!dev->phydev || !dp->pl)
710 if (!ds->ops->get_mac_eee)
713 ret = ds->ops->get_mac_eee(ds, dp->index, e);
717 return phylink_ethtool_get_eee(dp->pl, e);
720 static int dsa_slave_get_link_ksettings(struct net_device *dev,
721 struct ethtool_link_ksettings *cmd)
723 struct dsa_port *dp = dsa_slave_to_port(dev);
725 return phylink_ethtool_ksettings_get(dp->pl, cmd);
728 static int dsa_slave_set_link_ksettings(struct net_device *dev,
729 const struct ethtool_link_ksettings *cmd)
731 struct dsa_port *dp = dsa_slave_to_port(dev);
733 return phylink_ethtool_ksettings_set(dp->pl, cmd);
736 #ifdef CONFIG_NET_POLL_CONTROLLER
737 static int dsa_slave_netpoll_setup(struct net_device *dev,
738 struct netpoll_info *ni)
740 struct net_device *master = dsa_slave_to_master(dev);
741 struct dsa_slave_priv *p = netdev_priv(dev);
742 struct netpoll *netpoll;
745 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
749 err = __netpoll_setup(netpoll, master);
755 p->netpoll = netpoll;
760 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
762 struct dsa_slave_priv *p = netdev_priv(dev);
763 struct netpoll *netpoll = p->netpoll;
770 __netpoll_free(netpoll);
773 static void dsa_slave_poll_controller(struct net_device *dev)
778 static int dsa_slave_get_phys_port_name(struct net_device *dev,
779 char *name, size_t len)
781 struct dsa_port *dp = dsa_slave_to_port(dev);
783 /* For non-legacy ports, devlink is used and it takes
784 * care of the name generation. This ndo implementation
785 * should be removed with legacy support.
790 if (snprintf(name, len, "p%d", dp->index) >= len)
796 static struct dsa_mall_tc_entry *
797 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
799 struct dsa_slave_priv *p = netdev_priv(dev);
800 struct dsa_mall_tc_entry *mall_tc_entry;
802 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
803 if (mall_tc_entry->cookie == cookie)
804 return mall_tc_entry;
809 static int dsa_slave_add_cls_matchall(struct net_device *dev,
810 struct tc_cls_matchall_offload *cls,
813 struct dsa_port *dp = dsa_slave_to_port(dev);
814 struct dsa_slave_priv *p = netdev_priv(dev);
815 struct dsa_mall_tc_entry *mall_tc_entry;
816 __be16 protocol = cls->common.protocol;
817 struct dsa_switch *ds = dp->ds;
818 struct flow_action_entry *act;
819 struct dsa_port *to_dp;
820 int err = -EOPNOTSUPP;
822 if (!ds->ops->port_mirror_add)
825 if (!flow_offload_has_one_action(&cls->rule->action))
828 act = &cls->rule->action.entries[0];
830 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
831 struct dsa_mall_mirror_tc_entry *mirror;
836 if (!dsa_slave_dev_check(act->dev))
839 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
843 mall_tc_entry->cookie = cls->cookie;
844 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
845 mirror = &mall_tc_entry->mirror;
847 to_dp = dsa_slave_to_port(act->dev);
849 mirror->to_local_port = to_dp->index;
850 mirror->ingress = ingress;
852 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
854 kfree(mall_tc_entry);
858 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
864 static void dsa_slave_del_cls_matchall(struct net_device *dev,
865 struct tc_cls_matchall_offload *cls)
867 struct dsa_port *dp = dsa_slave_to_port(dev);
868 struct dsa_mall_tc_entry *mall_tc_entry;
869 struct dsa_switch *ds = dp->ds;
871 if (!ds->ops->port_mirror_del)
874 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
878 list_del(&mall_tc_entry->list);
880 switch (mall_tc_entry->type) {
881 case DSA_PORT_MALL_MIRROR:
882 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
888 kfree(mall_tc_entry);
891 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
892 struct tc_cls_matchall_offload *cls,
895 if (cls->common.chain_index)
898 switch (cls->command) {
899 case TC_CLSMATCHALL_REPLACE:
900 return dsa_slave_add_cls_matchall(dev, cls, ingress);
901 case TC_CLSMATCHALL_DESTROY:
902 dsa_slave_del_cls_matchall(dev, cls);
909 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
910 void *cb_priv, bool ingress)
912 struct net_device *dev = cb_priv;
914 if (!tc_can_offload(dev))
918 case TC_SETUP_CLSMATCHALL:
919 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
925 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
926 void *type_data, void *cb_priv)
928 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
931 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
932 void *type_data, void *cb_priv)
934 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
937 static int dsa_slave_setup_tc_block(struct net_device *dev,
938 struct tc_block_offload *f)
942 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
943 cb = dsa_slave_setup_tc_block_cb_ig;
944 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
945 cb = dsa_slave_setup_tc_block_cb_eg;
949 switch (f->command) {
951 return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
952 case TC_BLOCK_UNBIND:
953 tcf_block_cb_unregister(f->block, cb, dev);
960 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
965 return dsa_slave_setup_tc_block(dev, type_data);
971 static void dsa_slave_get_stats64(struct net_device *dev,
972 struct rtnl_link_stats64 *stats)
974 struct dsa_slave_priv *p = netdev_priv(dev);
975 struct pcpu_sw_netstats *s;
979 netdev_stats_to_stats64(stats, &dev->stats);
980 for_each_possible_cpu(i) {
981 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
983 s = per_cpu_ptr(p->stats64, i);
985 start = u64_stats_fetch_begin_irq(&s->syncp);
986 tx_packets = s->tx_packets;
987 tx_bytes = s->tx_bytes;
988 rx_packets = s->rx_packets;
989 rx_bytes = s->rx_bytes;
990 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
992 stats->tx_packets += tx_packets;
993 stats->tx_bytes += tx_bytes;
994 stats->rx_packets += rx_packets;
995 stats->rx_bytes += rx_bytes;
999 static int dsa_slave_get_rxnfc(struct net_device *dev,
1000 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1002 struct dsa_port *dp = dsa_slave_to_port(dev);
1003 struct dsa_switch *ds = dp->ds;
1005 if (!ds->ops->get_rxnfc)
1008 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1011 static int dsa_slave_set_rxnfc(struct net_device *dev,
1012 struct ethtool_rxnfc *nfc)
1014 struct dsa_port *dp = dsa_slave_to_port(dev);
1015 struct dsa_switch *ds = dp->ds;
1017 if (!ds->ops->set_rxnfc)
1020 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1023 static int dsa_slave_get_ts_info(struct net_device *dev,
1024 struct ethtool_ts_info *ts)
1026 struct dsa_slave_priv *p = netdev_priv(dev);
1027 struct dsa_switch *ds = p->dp->ds;
1029 if (!ds->ops->get_ts_info)
1032 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1035 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1038 struct dsa_port *dp = dsa_slave_to_port(dev);
1039 struct bridge_vlan_info info;
1042 /* Check for a possible bridge VLAN entry now since there is no
1043 * need to emulate the switchdev prepare + commit phase.
1045 if (dp->bridge_dev) {
1046 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1047 * device, respectively the VID is not found, returning
1048 * 0 means success, which is a failure for us here.
1050 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1055 /* This API only allows programming tagged, non-PVID VIDs */
1056 return dsa_port_vid_add(dp, vid, 0);
1059 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1062 struct dsa_port *dp = dsa_slave_to_port(dev);
1063 struct bridge_vlan_info info;
1066 /* Check for a possible bridge VLAN entry now since there is no
1067 * need to emulate the switchdev prepare + commit phase.
1069 if (dp->bridge_dev) {
1070 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1071 * device, respectively the VID is not found, returning
1072 * 0 means success, which is a failure for us here.
1074 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1079 ret = dsa_port_vid_del(dp, vid);
1080 if (ret == -EOPNOTSUPP)
1086 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1087 .get_drvinfo = dsa_slave_get_drvinfo,
1088 .get_regs_len = dsa_slave_get_regs_len,
1089 .get_regs = dsa_slave_get_regs,
1090 .nway_reset = dsa_slave_nway_reset,
1091 .get_link = ethtool_op_get_link,
1092 .get_eeprom_len = dsa_slave_get_eeprom_len,
1093 .get_eeprom = dsa_slave_get_eeprom,
1094 .set_eeprom = dsa_slave_set_eeprom,
1095 .get_strings = dsa_slave_get_strings,
1096 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1097 .get_sset_count = dsa_slave_get_sset_count,
1098 .set_wol = dsa_slave_set_wol,
1099 .get_wol = dsa_slave_get_wol,
1100 .set_eee = dsa_slave_set_eee,
1101 .get_eee = dsa_slave_get_eee,
1102 .get_link_ksettings = dsa_slave_get_link_ksettings,
1103 .set_link_ksettings = dsa_slave_set_link_ksettings,
1104 .get_rxnfc = dsa_slave_get_rxnfc,
1105 .set_rxnfc = dsa_slave_set_rxnfc,
1106 .get_ts_info = dsa_slave_get_ts_info,
1109 /* legacy way, bypassing the bridge *****************************************/
1110 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1111 struct net_device *dev,
1112 const unsigned char *addr, u16 vid,
1114 struct netlink_ext_ack *extack)
1116 struct dsa_port *dp = dsa_slave_to_port(dev);
1118 return dsa_port_fdb_add(dp, addr, vid);
1121 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1122 struct net_device *dev,
1123 const unsigned char *addr, u16 vid)
1125 struct dsa_port *dp = dsa_slave_to_port(dev);
1127 return dsa_port_fdb_del(dp, addr, vid);
1130 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1132 struct dsa_port *dp = dsa_slave_to_port(dev);
1134 return dp->ds->devlink ? &dp->devlink_port : NULL;
1137 static const struct net_device_ops dsa_slave_netdev_ops = {
1138 .ndo_open = dsa_slave_open,
1139 .ndo_stop = dsa_slave_close,
1140 .ndo_start_xmit = dsa_slave_xmit,
1141 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1142 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1143 .ndo_set_mac_address = dsa_slave_set_mac_address,
1144 .ndo_fdb_add = dsa_legacy_fdb_add,
1145 .ndo_fdb_del = dsa_legacy_fdb_del,
1146 .ndo_fdb_dump = dsa_slave_fdb_dump,
1147 .ndo_do_ioctl = dsa_slave_ioctl,
1148 .ndo_get_iflink = dsa_slave_get_iflink,
1149 #ifdef CONFIG_NET_POLL_CONTROLLER
1150 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1151 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1152 .ndo_poll_controller = dsa_slave_poll_controller,
1154 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1155 .ndo_setup_tc = dsa_slave_setup_tc,
1156 .ndo_get_stats64 = dsa_slave_get_stats64,
1157 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1158 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1159 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1160 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1163 static struct device_type dsa_type = {
1167 static void dsa_slave_phylink_validate(struct net_device *dev,
1168 unsigned long *supported,
1169 struct phylink_link_state *state)
1171 struct dsa_port *dp = dsa_slave_to_port(dev);
1172 struct dsa_switch *ds = dp->ds;
1174 if (!ds->ops->phylink_validate)
1177 ds->ops->phylink_validate(ds, dp->index, supported, state);
1180 static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1181 struct phylink_link_state *state)
1183 struct dsa_port *dp = dsa_slave_to_port(dev);
1184 struct dsa_switch *ds = dp->ds;
1186 /* Only called for SGMII and 802.3z */
1187 if (!ds->ops->phylink_mac_link_state)
1190 return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1193 static void dsa_slave_phylink_mac_config(struct net_device *dev,
1195 const struct phylink_link_state *state)
1197 struct dsa_port *dp = dsa_slave_to_port(dev);
1198 struct dsa_switch *ds = dp->ds;
1200 if (!ds->ops->phylink_mac_config)
1203 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1206 static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1208 struct dsa_port *dp = dsa_slave_to_port(dev);
1209 struct dsa_switch *ds = dp->ds;
1211 if (!ds->ops->phylink_mac_an_restart)
1214 ds->ops->phylink_mac_an_restart(ds, dp->index);
1217 static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1219 phy_interface_t interface)
1221 struct dsa_port *dp = dsa_slave_to_port(dev);
1222 struct dsa_switch *ds = dp->ds;
1224 if (!ds->ops->phylink_mac_link_down) {
1225 if (ds->ops->adjust_link && dev->phydev)
1226 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1230 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1233 static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1235 phy_interface_t interface,
1236 struct phy_device *phydev)
1238 struct dsa_port *dp = dsa_slave_to_port(dev);
1239 struct dsa_switch *ds = dp->ds;
1241 if (!ds->ops->phylink_mac_link_up) {
1242 if (ds->ops->adjust_link && dev->phydev)
1243 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1247 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1250 static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1251 .validate = dsa_slave_phylink_validate,
1252 .mac_link_state = dsa_slave_phylink_mac_link_state,
1253 .mac_config = dsa_slave_phylink_mac_config,
1254 .mac_an_restart = dsa_slave_phylink_mac_an_restart,
1255 .mac_link_down = dsa_slave_phylink_mac_link_down,
1256 .mac_link_up = dsa_slave_phylink_mac_link_up,
1259 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1261 const struct dsa_port *dp = dsa_to_port(ds, port);
1263 phylink_mac_change(dp->pl, up);
1265 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1267 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1268 struct phylink_link_state *state)
1270 struct dsa_port *dp = dsa_slave_to_port(dev);
1271 struct dsa_switch *ds = dp->ds;
1273 /* No need to check that this operation is valid, the callback would
1274 * not be called if it was not.
1276 ds->ops->phylink_fixed_state(ds, dp->index, state);
1279 /* slave device setup *******************************************************/
1280 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1282 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1283 struct dsa_switch *ds = dp->ds;
1285 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1286 if (!slave_dev->phydev) {
1287 netdev_err(slave_dev, "no phy at %d\n", addr);
1291 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1294 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1296 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1297 struct device_node *port_dn = dp->dn;
1298 struct dsa_switch *ds = dp->ds;
1302 mode = of_get_phy_mode(port_dn);
1304 mode = PHY_INTERFACE_MODE_NA;
1306 dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1307 &dsa_slave_phylink_mac_ops);
1308 if (IS_ERR(dp->pl)) {
1309 netdev_err(slave_dev,
1310 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1311 return PTR_ERR(dp->pl);
1314 /* Register only if the switch provides such a callback, since this
1315 * callback takes precedence over polling the link GPIO in PHYLINK
1316 * (see phylink_get_fixed_state).
1318 if (ds->ops->phylink_fixed_state)
1319 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1321 if (ds->ops->get_phy_flags)
1322 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1324 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1325 if (ret == -ENODEV && ds->slave_mii_bus) {
1326 /* We could not connect to a designated PHY or SFP, so try to
1327 * use the switch internal MDIO bus instead
1329 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1331 netdev_err(slave_dev,
1332 "failed to connect to port %d: %d\n",
1334 phylink_destroy(dp->pl);
1342 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1343 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1344 struct netdev_queue *txq,
1347 lockdep_set_class(&txq->_xmit_lock,
1348 &dsa_slave_netdev_xmit_lock_key);
1351 int dsa_slave_suspend(struct net_device *slave_dev)
1353 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1355 if (!netif_running(slave_dev))
1358 cancel_work_sync(&dp->xmit_work);
1359 skb_queue_purge(&dp->xmit_queue);
1361 netif_device_detach(slave_dev);
1364 phylink_stop(dp->pl);
1370 int dsa_slave_resume(struct net_device *slave_dev)
1372 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1374 if (!netif_running(slave_dev))
1377 netif_device_attach(slave_dev);
1380 phylink_start(dp->pl);
1386 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1388 struct net_device *master = dsa_slave_to_master(dev);
1389 struct dsa_port *dp = dsa_slave_to_port(dev);
1390 struct dsa_notifier_register_info rinfo = {
1391 .switch_number = dp->ds->index,
1392 .port_number = dp->index,
1397 call_dsa_notifiers(val, dev, &rinfo.info);
1400 int dsa_slave_create(struct dsa_port *port)
1402 const struct dsa_port *cpu_dp = port->cpu_dp;
1403 struct net_device *master = cpu_dp->master;
1404 struct dsa_switch *ds = port->ds;
1405 const char *name = port->name;
1406 struct net_device *slave_dev;
1407 struct dsa_slave_priv *p;
1410 if (!ds->num_tx_queues)
1411 ds->num_tx_queues = 1;
1413 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1414 NET_NAME_UNKNOWN, ether_setup,
1415 ds->num_tx_queues, 1);
1416 if (slave_dev == NULL)
1419 slave_dev->features = master->vlan_features | NETIF_F_HW_TC |
1420 NETIF_F_HW_VLAN_CTAG_FILTER;
1421 slave_dev->hw_features |= NETIF_F_HW_TC;
1422 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1423 if (!IS_ERR_OR_NULL(port->mac))
1424 ether_addr_copy(slave_dev->dev_addr, port->mac);
1426 eth_hw_addr_inherit(slave_dev, master);
1427 slave_dev->priv_flags |= IFF_NO_QUEUE;
1428 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1429 slave_dev->min_mtu = 0;
1430 slave_dev->max_mtu = ETH_MAX_MTU;
1431 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1433 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1436 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1437 slave_dev->dev.of_node = port->dn;
1438 slave_dev->vlan_features = master->vlan_features;
1440 p = netdev_priv(slave_dev);
1441 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1443 free_netdev(slave_dev);
1447 INIT_LIST_HEAD(&p->mall_tc_list);
1448 INIT_WORK(&port->xmit_work, dsa_port_xmit_work);
1449 skb_queue_head_init(&port->xmit_queue);
1450 p->xmit = cpu_dp->tag_ops->xmit;
1451 port->slave = slave_dev;
1453 netif_carrier_off(slave_dev);
1455 ret = dsa_slave_phy_setup(slave_dev);
1457 netdev_err(master, "error %d setting up slave phy\n", ret);
1461 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1463 ret = register_netdev(slave_dev);
1465 netdev_err(master, "error %d registering interface %s\n",
1466 ret, slave_dev->name);
1474 phylink_disconnect_phy(p->dp->pl);
1476 phylink_destroy(p->dp->pl);
1478 free_percpu(p->stats64);
1479 free_netdev(slave_dev);
1484 void dsa_slave_destroy(struct net_device *slave_dev)
1486 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1487 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1489 netif_carrier_off(slave_dev);
1491 phylink_disconnect_phy(dp->pl);
1494 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1495 unregister_netdev(slave_dev);
1496 phylink_destroy(dp->pl);
1497 free_percpu(p->stats64);
1498 free_netdev(slave_dev);
1501 static bool dsa_slave_dev_check(struct net_device *dev)
1503 return dev->netdev_ops == &dsa_slave_netdev_ops;
1506 static int dsa_slave_changeupper(struct net_device *dev,
1507 struct netdev_notifier_changeupper_info *info)
1509 struct dsa_port *dp = dsa_slave_to_port(dev);
1510 int err = NOTIFY_DONE;
1512 if (netif_is_bridge_master(info->upper_dev)) {
1513 if (info->linking) {
1514 err = dsa_port_bridge_join(dp, info->upper_dev);
1515 err = notifier_from_errno(err);
1517 dsa_port_bridge_leave(dp, info->upper_dev);
1525 static int dsa_slave_upper_vlan_check(struct net_device *dev,
1526 struct netdev_notifier_changeupper_info *
1529 struct netlink_ext_ack *ext_ack;
1530 struct net_device *slave;
1531 struct dsa_port *dp;
1533 ext_ack = netdev_notifier_info_to_extack(&info->info);
1535 if (!is_vlan_dev(dev))
1538 slave = vlan_dev_real_dev(dev);
1539 if (!dsa_slave_dev_check(slave))
1542 dp = dsa_slave_to_port(slave);
1543 if (!dp->bridge_dev)
1546 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1547 if (br_vlan_enabled(dp->bridge_dev) &&
1548 netif_is_bridge_master(info->upper_dev) && info->linking) {
1549 NL_SET_ERR_MSG_MOD(ext_ack,
1550 "Cannot enslave VLAN device into VLAN aware bridge");
1551 return notifier_from_errno(-EINVAL);
1557 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1558 unsigned long event, void *ptr)
1560 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1562 if (event == NETDEV_CHANGEUPPER) {
1563 if (!dsa_slave_dev_check(dev))
1564 return dsa_slave_upper_vlan_check(dev, ptr);
1566 return dsa_slave_changeupper(dev, ptr);
1573 dsa_slave_switchdev_port_attr_set_event(struct net_device *netdev,
1574 struct switchdev_notifier_port_attr_info *port_attr_info)
1578 err = dsa_slave_port_attr_set(netdev, port_attr_info->attr,
1579 port_attr_info->trans);
1581 port_attr_info->handled = true;
1582 return notifier_from_errno(err);
1585 struct dsa_switchdev_event_work {
1586 struct work_struct work;
1587 struct switchdev_notifier_fdb_info fdb_info;
1588 struct net_device *dev;
1589 unsigned long event;
1592 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1594 struct dsa_switchdev_event_work *switchdev_work =
1595 container_of(work, struct dsa_switchdev_event_work, work);
1596 struct net_device *dev = switchdev_work->dev;
1597 struct switchdev_notifier_fdb_info *fdb_info;
1598 struct dsa_port *dp = dsa_slave_to_port(dev);
1602 switch (switchdev_work->event) {
1603 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1604 fdb_info = &switchdev_work->fdb_info;
1605 if (!fdb_info->added_by_user)
1608 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1610 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1613 fdb_info->offloaded = true;
1614 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1615 &fdb_info->info, NULL);
1618 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1619 fdb_info = &switchdev_work->fdb_info;
1620 if (!fdb_info->added_by_user)
1623 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1625 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1632 kfree(switchdev_work->fdb_info.addr);
1633 kfree(switchdev_work);
1638 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1640 const struct switchdev_notifier_fdb_info *
1643 memcpy(&switchdev_work->fdb_info, fdb_info,
1644 sizeof(switchdev_work->fdb_info));
1645 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1646 if (!switchdev_work->fdb_info.addr)
1648 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1653 /* Called under rcu_read_lock() */
1654 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1655 unsigned long event, void *ptr)
1657 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1658 struct dsa_switchdev_event_work *switchdev_work;
1660 if (!dsa_slave_dev_check(dev))
1663 if (event == SWITCHDEV_PORT_ATTR_SET)
1664 return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
1666 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1667 if (!switchdev_work)
1670 INIT_WORK(&switchdev_work->work,
1671 dsa_slave_switchdev_event_work);
1672 switchdev_work->dev = dev;
1673 switchdev_work->event = event;
1676 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1677 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1678 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1679 goto err_fdb_work_init;
1683 kfree(switchdev_work);
1687 dsa_schedule_work(&switchdev_work->work);
1691 kfree(switchdev_work);
1696 dsa_slave_switchdev_port_obj_event(unsigned long event,
1697 struct net_device *netdev,
1698 struct switchdev_notifier_port_obj_info *port_obj_info)
1700 int err = -EOPNOTSUPP;
1703 case SWITCHDEV_PORT_OBJ_ADD:
1704 err = dsa_slave_port_obj_add(netdev, port_obj_info->obj,
1705 port_obj_info->trans);
1707 case SWITCHDEV_PORT_OBJ_DEL:
1708 err = dsa_slave_port_obj_del(netdev, port_obj_info->obj);
1712 port_obj_info->handled = true;
1713 return notifier_from_errno(err);
1716 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1717 unsigned long event, void *ptr)
1719 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1721 if (!dsa_slave_dev_check(dev))
1725 case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
1726 case SWITCHDEV_PORT_OBJ_DEL:
1727 return dsa_slave_switchdev_port_obj_event(event, dev, ptr);
1728 case SWITCHDEV_PORT_ATTR_SET:
1729 return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
1735 static struct notifier_block dsa_slave_nb __read_mostly = {
1736 .notifier_call = dsa_slave_netdevice_event,
1739 static struct notifier_block dsa_slave_switchdev_notifier = {
1740 .notifier_call = dsa_slave_switchdev_event,
1743 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1744 .notifier_call = dsa_slave_switchdev_blocking_event,
1747 int dsa_slave_register_notifier(void)
1749 struct notifier_block *nb;
1752 err = register_netdevice_notifier(&dsa_slave_nb);
1756 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1758 goto err_switchdev_nb;
1760 nb = &dsa_slave_switchdev_blocking_notifier;
1761 err = register_switchdev_blocking_notifier(nb);
1763 goto err_switchdev_blocking_nb;
1767 err_switchdev_blocking_nb:
1768 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1770 unregister_netdevice_notifier(&dsa_slave_nb);
1774 void dsa_slave_unregister_notifier(void)
1776 struct notifier_block *nb;
1779 nb = &dsa_slave_switchdev_blocking_notifier;
1780 err = unregister_switchdev_blocking_notifier(nb);
1782 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1784 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1786 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1788 err = unregister_netdevice_notifier(&dsa_slave_nb);
1790 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);