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/selftests.h>
19 #include <net/tc_act/tc_mirred.h>
20 #include <linux/if_bridge.h>
21 #include <linux/if_hsr.h>
22 #include <net/dcbnl.h>
23 #include <linux/netpoll.h>
27 static void dsa_slave_standalone_event_work(struct work_struct *work)
29 struct dsa_standalone_event_work *standalone_work =
30 container_of(work, struct dsa_standalone_event_work, work);
31 const unsigned char *addr = standalone_work->addr;
32 struct net_device *dev = standalone_work->dev;
33 struct dsa_port *dp = dsa_slave_to_port(dev);
34 struct switchdev_obj_port_mdb mdb;
35 struct dsa_switch *ds = dp->ds;
36 u16 vid = standalone_work->vid;
39 switch (standalone_work->event) {
41 err = dsa_port_standalone_host_fdb_add(dp, addr, vid);
44 "port %d failed to add %pM vid %d to fdb: %d\n",
45 dp->index, addr, vid, err);
51 err = dsa_port_standalone_host_fdb_del(dp, addr, vid);
54 "port %d failed to delete %pM vid %d from fdb: %d\n",
55 dp->index, addr, vid, err);
60 ether_addr_copy(mdb.addr, addr);
63 err = dsa_port_standalone_host_mdb_add(dp, &mdb);
66 "port %d failed to add %pM vid %d to mdb: %d\n",
67 dp->index, addr, vid, err);
72 ether_addr_copy(mdb.addr, addr);
75 err = dsa_port_standalone_host_mdb_del(dp, &mdb);
78 "port %d failed to delete %pM vid %d from mdb: %d\n",
79 dp->index, addr, vid, err);
85 kfree(standalone_work);
88 static int dsa_slave_schedule_standalone_work(struct net_device *dev,
89 enum dsa_standalone_event event,
90 const unsigned char *addr,
93 struct dsa_standalone_event_work *standalone_work;
95 standalone_work = kzalloc(sizeof(*standalone_work), GFP_ATOMIC);
99 INIT_WORK(&standalone_work->work, dsa_slave_standalone_event_work);
100 standalone_work->event = event;
101 standalone_work->dev = dev;
103 ether_addr_copy(standalone_work->addr, addr);
104 standalone_work->vid = vid;
106 dsa_schedule_work(&standalone_work->work);
111 static int dsa_slave_sync_uc(struct net_device *dev,
112 const unsigned char *addr)
114 struct net_device *master = dsa_slave_to_master(dev);
115 struct dsa_port *dp = dsa_slave_to_port(dev);
117 dev_uc_add(master, addr);
119 if (!dsa_switch_supports_uc_filtering(dp->ds))
122 return dsa_slave_schedule_standalone_work(dev, DSA_UC_ADD, addr, 0);
125 static int dsa_slave_unsync_uc(struct net_device *dev,
126 const unsigned char *addr)
128 struct net_device *master = dsa_slave_to_master(dev);
129 struct dsa_port *dp = dsa_slave_to_port(dev);
131 dev_uc_del(master, addr);
133 if (!dsa_switch_supports_uc_filtering(dp->ds))
136 return dsa_slave_schedule_standalone_work(dev, DSA_UC_DEL, addr, 0);
139 static int dsa_slave_sync_mc(struct net_device *dev,
140 const unsigned char *addr)
142 struct net_device *master = dsa_slave_to_master(dev);
143 struct dsa_port *dp = dsa_slave_to_port(dev);
145 dev_mc_add(master, addr);
147 if (!dsa_switch_supports_mc_filtering(dp->ds))
150 return dsa_slave_schedule_standalone_work(dev, DSA_MC_ADD, addr, 0);
153 static int dsa_slave_unsync_mc(struct net_device *dev,
154 const unsigned char *addr)
156 struct net_device *master = dsa_slave_to_master(dev);
157 struct dsa_port *dp = dsa_slave_to_port(dev);
159 dev_mc_del(master, addr);
161 if (!dsa_switch_supports_mc_filtering(dp->ds))
164 return dsa_slave_schedule_standalone_work(dev, DSA_MC_DEL, addr, 0);
167 void dsa_slave_sync_ha(struct net_device *dev)
169 struct dsa_port *dp = dsa_slave_to_port(dev);
170 struct dsa_switch *ds = dp->ds;
171 struct netdev_hw_addr *ha;
173 netif_addr_lock_bh(dev);
175 netdev_for_each_synced_mc_addr(ha, dev)
176 dsa_slave_sync_mc(dev, ha->addr);
178 netdev_for_each_synced_uc_addr(ha, dev)
179 dsa_slave_sync_uc(dev, ha->addr);
181 netif_addr_unlock_bh(dev);
183 if (dsa_switch_supports_uc_filtering(ds) ||
184 dsa_switch_supports_mc_filtering(ds))
185 dsa_flush_workqueue();
188 void dsa_slave_unsync_ha(struct net_device *dev)
190 struct dsa_port *dp = dsa_slave_to_port(dev);
191 struct dsa_switch *ds = dp->ds;
192 struct netdev_hw_addr *ha;
194 netif_addr_lock_bh(dev);
196 netdev_for_each_synced_uc_addr(ha, dev)
197 dsa_slave_unsync_uc(dev, ha->addr);
199 netdev_for_each_synced_mc_addr(ha, dev)
200 dsa_slave_unsync_mc(dev, ha->addr);
202 netif_addr_unlock_bh(dev);
204 if (dsa_switch_supports_uc_filtering(ds) ||
205 dsa_switch_supports_mc_filtering(ds))
206 dsa_flush_workqueue();
209 /* slave mii_bus handling ***************************************************/
210 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
212 struct dsa_switch *ds = bus->priv;
214 if (ds->phys_mii_mask & (1 << addr))
215 return ds->ops->phy_read(ds, addr, reg);
220 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
222 struct dsa_switch *ds = bus->priv;
224 if (ds->phys_mii_mask & (1 << addr))
225 return ds->ops->phy_write(ds, addr, reg, val);
230 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
232 ds->slave_mii_bus->priv = (void *)ds;
233 ds->slave_mii_bus->name = "dsa slave smi";
234 ds->slave_mii_bus->read = dsa_slave_phy_read;
235 ds->slave_mii_bus->write = dsa_slave_phy_write;
236 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
237 ds->dst->index, ds->index);
238 ds->slave_mii_bus->parent = ds->dev;
239 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
243 /* slave device handling ****************************************************/
244 static int dsa_slave_get_iflink(const struct net_device *dev)
246 return dsa_slave_to_master(dev)->ifindex;
249 static int dsa_slave_open(struct net_device *dev)
251 struct net_device *master = dsa_slave_to_master(dev);
252 struct dsa_port *dp = dsa_slave_to_port(dev);
253 struct dsa_switch *ds = dp->ds;
256 err = dev_open(master, NULL);
258 netdev_err(dev, "failed to open master %s\n", master->name);
262 if (dsa_switch_supports_uc_filtering(ds)) {
263 err = dsa_port_standalone_host_fdb_add(dp, dev->dev_addr, 0);
268 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
269 err = dev_uc_add(master, dev->dev_addr);
274 err = dsa_port_enable_rt(dp, dev->phydev);
281 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
282 dev_uc_del(master, dev->dev_addr);
284 if (dsa_switch_supports_uc_filtering(ds))
285 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
290 static int dsa_slave_close(struct net_device *dev)
292 struct net_device *master = dsa_slave_to_master(dev);
293 struct dsa_port *dp = dsa_slave_to_port(dev);
294 struct dsa_switch *ds = dp->ds;
296 dsa_port_disable_rt(dp);
298 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
299 dev_uc_del(master, dev->dev_addr);
301 if (dsa_switch_supports_uc_filtering(ds))
302 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
307 static void dsa_slave_manage_host_flood(struct net_device *dev)
309 bool mc = dev->flags & (IFF_PROMISC | IFF_ALLMULTI);
310 struct dsa_port *dp = dsa_slave_to_port(dev);
311 bool uc = dev->flags & IFF_PROMISC;
313 dsa_port_set_host_flood(dp, uc, mc);
316 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
318 struct net_device *master = dsa_slave_to_master(dev);
319 struct dsa_port *dp = dsa_slave_to_port(dev);
320 struct dsa_switch *ds = dp->ds;
322 if (change & IFF_ALLMULTI)
323 dev_set_allmulti(master,
324 dev->flags & IFF_ALLMULTI ? 1 : -1);
325 if (change & IFF_PROMISC)
326 dev_set_promiscuity(master,
327 dev->flags & IFF_PROMISC ? 1 : -1);
329 if (dsa_switch_supports_uc_filtering(ds) &&
330 dsa_switch_supports_mc_filtering(ds))
331 dsa_slave_manage_host_flood(dev);
334 static void dsa_slave_set_rx_mode(struct net_device *dev)
336 __dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc);
337 __dev_uc_sync(dev, dsa_slave_sync_uc, dsa_slave_unsync_uc);
340 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
342 struct net_device *master = dsa_slave_to_master(dev);
343 struct dsa_port *dp = dsa_slave_to_port(dev);
344 struct dsa_switch *ds = dp->ds;
345 struct sockaddr *addr = a;
348 if (!is_valid_ether_addr(addr->sa_data))
349 return -EADDRNOTAVAIL;
351 /* If the port is down, the address isn't synced yet to hardware or
352 * to the DSA master, so there is nothing to change.
354 if (!(dev->flags & IFF_UP))
355 goto out_change_dev_addr;
357 if (dsa_switch_supports_uc_filtering(ds)) {
358 err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0);
363 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
364 err = dev_uc_add(master, addr->sa_data);
369 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
370 dev_uc_del(master, dev->dev_addr);
372 if (dsa_switch_supports_uc_filtering(ds))
373 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
376 eth_hw_addr_set(dev, addr->sa_data);
381 if (dsa_switch_supports_uc_filtering(ds))
382 dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0);
387 struct dsa_slave_dump_ctx {
388 struct net_device *dev;
390 struct netlink_callback *cb;
395 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
396 bool is_static, void *data)
398 struct dsa_slave_dump_ctx *dump = data;
399 u32 portid = NETLINK_CB(dump->cb->skb).portid;
400 u32 seq = dump->cb->nlh->nlmsg_seq;
401 struct nlmsghdr *nlh;
404 if (dump->idx < dump->cb->args[2])
407 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
408 sizeof(*ndm), NLM_F_MULTI);
412 ndm = nlmsg_data(nlh);
413 ndm->ndm_family = AF_BRIDGE;
416 ndm->ndm_flags = NTF_SELF;
418 ndm->ndm_ifindex = dump->dev->ifindex;
419 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
421 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
422 goto nla_put_failure;
424 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
425 goto nla_put_failure;
427 nlmsg_end(dump->skb, nlh);
434 nlmsg_cancel(dump->skb, nlh);
439 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
440 struct net_device *dev, struct net_device *filter_dev,
443 struct dsa_port *dp = dsa_slave_to_port(dev);
444 struct dsa_slave_dump_ctx dump = {
452 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
458 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
460 struct dsa_slave_priv *p = netdev_priv(dev);
461 struct dsa_switch *ds = p->dp->ds;
462 int port = p->dp->index;
464 /* Pass through to switch driver if it supports timestamping */
467 if (ds->ops->port_hwtstamp_get)
468 return ds->ops->port_hwtstamp_get(ds, port, ifr);
471 if (ds->ops->port_hwtstamp_set)
472 return ds->ops->port_hwtstamp_set(ds, port, ifr);
476 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
479 static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
480 const struct switchdev_attr *attr,
481 struct netlink_ext_ack *extack)
483 struct dsa_port *dp = dsa_slave_to_port(dev);
486 if (ctx && ctx != dp)
490 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
491 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
494 ret = dsa_port_set_state(dp, attr->u.stp_state, true);
496 case SWITCHDEV_ATTR_ID_PORT_MST_STATE:
497 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
500 ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack);
502 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
503 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
506 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
509 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
510 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
513 ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
515 case SWITCHDEV_ATTR_ID_BRIDGE_MST:
516 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
519 ret = dsa_port_mst_enable(dp, attr->u.mst, extack);
521 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
522 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
525 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
528 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
529 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
532 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
534 case SWITCHDEV_ATTR_ID_VLAN_MSTI:
535 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
538 ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti);
548 /* Must be called under rcu_read_lock() */
550 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
551 const struct switchdev_obj_port_vlan *vlan)
553 struct net_device *upper_dev;
554 struct list_head *iter;
556 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
559 if (!is_vlan_dev(upper_dev))
562 vid = vlan_dev_vlan_id(upper_dev);
563 if (vid == vlan->vid)
570 static int dsa_slave_vlan_add(struct net_device *dev,
571 const struct switchdev_obj *obj,
572 struct netlink_ext_ack *extack)
574 struct dsa_port *dp = dsa_slave_to_port(dev);
575 struct switchdev_obj_port_vlan *vlan;
578 if (dsa_port_skip_vlan_configuration(dp)) {
579 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
583 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
585 /* Deny adding a bridge VLAN when there is already an 802.1Q upper with
588 if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
590 err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan);
593 NL_SET_ERR_MSG_MOD(extack,
594 "Port already has a VLAN upper with this VID");
599 return dsa_port_vlan_add(dp, vlan, extack);
602 /* Offload a VLAN installed on the bridge or on a foreign interface by
603 * installing it as a VLAN towards the CPU port.
605 static int dsa_slave_host_vlan_add(struct net_device *dev,
606 const struct switchdev_obj *obj,
607 struct netlink_ext_ack *extack)
609 struct dsa_port *dp = dsa_slave_to_port(dev);
610 struct switchdev_obj_port_vlan vlan;
612 /* Do nothing if this is a software bridge */
616 if (dsa_port_skip_vlan_configuration(dp)) {
617 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
621 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
623 /* Even though drivers often handle CPU membership in special ways,
624 * it doesn't make sense to program a PVID, so clear this flag.
626 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
628 return dsa_port_host_vlan_add(dp, &vlan, extack);
631 static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
632 const struct switchdev_obj *obj,
633 struct netlink_ext_ack *extack)
635 struct dsa_port *dp = dsa_slave_to_port(dev);
638 if (ctx && ctx != dp)
642 case SWITCHDEV_OBJ_ID_PORT_MDB:
643 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
646 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
648 case SWITCHDEV_OBJ_ID_HOST_MDB:
649 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
652 err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
654 case SWITCHDEV_OBJ_ID_PORT_VLAN:
655 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
656 err = dsa_slave_vlan_add(dev, obj, extack);
658 err = dsa_slave_host_vlan_add(dev, obj, extack);
660 case SWITCHDEV_OBJ_ID_MRP:
661 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
664 err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
666 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
667 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
670 err = dsa_port_mrp_add_ring_role(dp,
671 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
681 static int dsa_slave_vlan_del(struct net_device *dev,
682 const struct switchdev_obj *obj)
684 struct dsa_port *dp = dsa_slave_to_port(dev);
685 struct switchdev_obj_port_vlan *vlan;
687 if (dsa_port_skip_vlan_configuration(dp))
690 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
692 return dsa_port_vlan_del(dp, vlan);
695 static int dsa_slave_host_vlan_del(struct net_device *dev,
696 const struct switchdev_obj *obj)
698 struct dsa_port *dp = dsa_slave_to_port(dev);
699 struct switchdev_obj_port_vlan *vlan;
701 /* Do nothing if this is a software bridge */
705 if (dsa_port_skip_vlan_configuration(dp))
708 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
710 return dsa_port_host_vlan_del(dp, vlan);
713 static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
714 const struct switchdev_obj *obj)
716 struct dsa_port *dp = dsa_slave_to_port(dev);
719 if (ctx && ctx != dp)
723 case SWITCHDEV_OBJ_ID_PORT_MDB:
724 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
727 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
729 case SWITCHDEV_OBJ_ID_HOST_MDB:
730 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
733 err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
735 case SWITCHDEV_OBJ_ID_PORT_VLAN:
736 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
737 err = dsa_slave_vlan_del(dev, obj);
739 err = dsa_slave_host_vlan_del(dev, obj);
741 case SWITCHDEV_OBJ_ID_MRP:
742 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
745 err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
747 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
748 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
751 err = dsa_port_mrp_del_ring_role(dp,
752 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
762 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
765 #ifdef CONFIG_NET_POLL_CONTROLLER
766 struct dsa_slave_priv *p = netdev_priv(dev);
768 return netpoll_send_skb(p->netpoll, skb);
775 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
778 struct dsa_switch *ds = p->dp->ds;
780 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
783 if (!ds->ops->port_txtstamp)
786 ds->ops->port_txtstamp(ds, p->dp->index, skb);
789 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
791 /* SKB for netpoll still need to be mangled with the protocol-specific
792 * tag to be successfully transmitted
794 if (unlikely(netpoll_tx_running(dev)))
795 return dsa_slave_netpoll_send_skb(dev, skb);
797 /* Queue the SKB for transmission on the parent interface, but
798 * do not modify its EtherType
800 skb->dev = dsa_slave_to_master(dev);
805 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
807 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
809 int needed_headroom = dev->needed_headroom;
810 int needed_tailroom = dev->needed_tailroom;
812 /* For tail taggers, we need to pad short frames ourselves, to ensure
813 * that the tail tag does not fail at its role of being at the end of
814 * the packet, once the master interface pads the frame. Account for
815 * that pad length here, and pad later.
817 if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
818 needed_tailroom += ETH_ZLEN - skb->len;
819 /* skb_headroom() returns unsigned int... */
820 needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
821 needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
823 if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
824 /* No reallocation needed, yay! */
827 return pskb_expand_head(skb, needed_headroom, needed_tailroom,
831 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
833 struct dsa_slave_priv *p = netdev_priv(dev);
834 struct sk_buff *nskb;
836 dev_sw_netstats_tx_add(dev, 1, skb->len);
838 memset(skb->cb, 0, sizeof(skb->cb));
840 /* Handle tx timestamp if any */
841 dsa_skb_tx_timestamp(p, skb);
843 if (dsa_realloc_skb(skb, dev)) {
844 dev_kfree_skb_any(skb);
848 /* needed_tailroom should still be 'warm' in the cache line from
849 * dsa_realloc_skb(), which has also ensured that padding is safe.
851 if (dev->needed_tailroom)
854 /* Transmit function may have to reallocate the original SKB,
855 * in which case it must have freed it. Only free it here on error.
857 nskb = p->xmit(skb, dev);
863 return dsa_enqueue_skb(nskb, dev);
866 /* ethtool operations *******************************************************/
868 static void dsa_slave_get_drvinfo(struct net_device *dev,
869 struct ethtool_drvinfo *drvinfo)
871 strscpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
872 strscpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
873 strscpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
876 static int dsa_slave_get_regs_len(struct net_device *dev)
878 struct dsa_port *dp = dsa_slave_to_port(dev);
879 struct dsa_switch *ds = dp->ds;
881 if (ds->ops->get_regs_len)
882 return ds->ops->get_regs_len(ds, dp->index);
888 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
890 struct dsa_port *dp = dsa_slave_to_port(dev);
891 struct dsa_switch *ds = dp->ds;
893 if (ds->ops->get_regs)
894 ds->ops->get_regs(ds, dp->index, regs, _p);
897 static int dsa_slave_nway_reset(struct net_device *dev)
899 struct dsa_port *dp = dsa_slave_to_port(dev);
901 return phylink_ethtool_nway_reset(dp->pl);
904 static int dsa_slave_get_eeprom_len(struct net_device *dev)
906 struct dsa_port *dp = dsa_slave_to_port(dev);
907 struct dsa_switch *ds = dp->ds;
909 if (ds->cd && ds->cd->eeprom_len)
910 return ds->cd->eeprom_len;
912 if (ds->ops->get_eeprom_len)
913 return ds->ops->get_eeprom_len(ds);
918 static int dsa_slave_get_eeprom(struct net_device *dev,
919 struct ethtool_eeprom *eeprom, u8 *data)
921 struct dsa_port *dp = dsa_slave_to_port(dev);
922 struct dsa_switch *ds = dp->ds;
924 if (ds->ops->get_eeprom)
925 return ds->ops->get_eeprom(ds, eeprom, data);
930 static int dsa_slave_set_eeprom(struct net_device *dev,
931 struct ethtool_eeprom *eeprom, u8 *data)
933 struct dsa_port *dp = dsa_slave_to_port(dev);
934 struct dsa_switch *ds = dp->ds;
936 if (ds->ops->set_eeprom)
937 return ds->ops->set_eeprom(ds, eeprom, data);
942 static void dsa_slave_get_strings(struct net_device *dev,
943 uint32_t stringset, uint8_t *data)
945 struct dsa_port *dp = dsa_slave_to_port(dev);
946 struct dsa_switch *ds = dp->ds;
948 if (stringset == ETH_SS_STATS) {
949 int len = ETH_GSTRING_LEN;
951 strncpy(data, "tx_packets", len);
952 strncpy(data + len, "tx_bytes", len);
953 strncpy(data + 2 * len, "rx_packets", len);
954 strncpy(data + 3 * len, "rx_bytes", len);
955 if (ds->ops->get_strings)
956 ds->ops->get_strings(ds, dp->index, stringset,
958 } else if (stringset == ETH_SS_TEST) {
959 net_selftest_get_strings(data);
964 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
965 struct ethtool_stats *stats,
968 struct dsa_port *dp = dsa_slave_to_port(dev);
969 struct dsa_switch *ds = dp->ds;
970 struct pcpu_sw_netstats *s;
974 for_each_possible_cpu(i) {
975 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
977 s = per_cpu_ptr(dev->tstats, i);
979 start = u64_stats_fetch_begin_irq(&s->syncp);
980 tx_packets = u64_stats_read(&s->tx_packets);
981 tx_bytes = u64_stats_read(&s->tx_bytes);
982 rx_packets = u64_stats_read(&s->rx_packets);
983 rx_bytes = u64_stats_read(&s->rx_bytes);
984 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
985 data[0] += tx_packets;
987 data[2] += rx_packets;
990 if (ds->ops->get_ethtool_stats)
991 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
994 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
996 struct dsa_port *dp = dsa_slave_to_port(dev);
997 struct dsa_switch *ds = dp->ds;
999 if (sset == ETH_SS_STATS) {
1002 if (ds->ops->get_sset_count) {
1003 count = ds->ops->get_sset_count(ds, dp->index, sset);
1009 } else if (sset == ETH_SS_TEST) {
1010 return net_selftest_get_count();
1016 static void dsa_slave_get_eth_phy_stats(struct net_device *dev,
1017 struct ethtool_eth_phy_stats *phy_stats)
1019 struct dsa_port *dp = dsa_slave_to_port(dev);
1020 struct dsa_switch *ds = dp->ds;
1022 if (ds->ops->get_eth_phy_stats)
1023 ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
1026 static void dsa_slave_get_eth_mac_stats(struct net_device *dev,
1027 struct ethtool_eth_mac_stats *mac_stats)
1029 struct dsa_port *dp = dsa_slave_to_port(dev);
1030 struct dsa_switch *ds = dp->ds;
1032 if (ds->ops->get_eth_mac_stats)
1033 ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
1037 dsa_slave_get_eth_ctrl_stats(struct net_device *dev,
1038 struct ethtool_eth_ctrl_stats *ctrl_stats)
1040 struct dsa_port *dp = dsa_slave_to_port(dev);
1041 struct dsa_switch *ds = dp->ds;
1043 if (ds->ops->get_eth_ctrl_stats)
1044 ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1048 dsa_slave_get_rmon_stats(struct net_device *dev,
1049 struct ethtool_rmon_stats *rmon_stats,
1050 const struct ethtool_rmon_hist_range **ranges)
1052 struct dsa_port *dp = dsa_slave_to_port(dev);
1053 struct dsa_switch *ds = dp->ds;
1055 if (ds->ops->get_rmon_stats)
1056 ds->ops->get_rmon_stats(ds, dp->index, rmon_stats, ranges);
1059 static void dsa_slave_net_selftest(struct net_device *ndev,
1060 struct ethtool_test *etest, u64 *buf)
1062 struct dsa_port *dp = dsa_slave_to_port(ndev);
1063 struct dsa_switch *ds = dp->ds;
1065 if (ds->ops->self_test) {
1066 ds->ops->self_test(ds, dp->index, etest, buf);
1070 net_selftest(ndev, etest, buf);
1073 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1075 struct dsa_port *dp = dsa_slave_to_port(dev);
1076 struct dsa_switch *ds = dp->ds;
1078 phylink_ethtool_get_wol(dp->pl, w);
1080 if (ds->ops->get_wol)
1081 ds->ops->get_wol(ds, dp->index, w);
1084 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1086 struct dsa_port *dp = dsa_slave_to_port(dev);
1087 struct dsa_switch *ds = dp->ds;
1088 int ret = -EOPNOTSUPP;
1090 phylink_ethtool_set_wol(dp->pl, w);
1092 if (ds->ops->set_wol)
1093 ret = ds->ops->set_wol(ds, dp->index, w);
1098 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
1100 struct dsa_port *dp = dsa_slave_to_port(dev);
1101 struct dsa_switch *ds = dp->ds;
1104 /* Port's PHY and MAC both need to be EEE capable */
1105 if (!dev->phydev || !dp->pl)
1108 if (!ds->ops->set_mac_eee)
1111 ret = ds->ops->set_mac_eee(ds, dp->index, e);
1115 return phylink_ethtool_set_eee(dp->pl, e);
1118 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
1120 struct dsa_port *dp = dsa_slave_to_port(dev);
1121 struct dsa_switch *ds = dp->ds;
1124 /* Port's PHY and MAC both need to be EEE capable */
1125 if (!dev->phydev || !dp->pl)
1128 if (!ds->ops->get_mac_eee)
1131 ret = ds->ops->get_mac_eee(ds, dp->index, e);
1135 return phylink_ethtool_get_eee(dp->pl, e);
1138 static int dsa_slave_get_link_ksettings(struct net_device *dev,
1139 struct ethtool_link_ksettings *cmd)
1141 struct dsa_port *dp = dsa_slave_to_port(dev);
1143 return phylink_ethtool_ksettings_get(dp->pl, cmd);
1146 static int dsa_slave_set_link_ksettings(struct net_device *dev,
1147 const struct ethtool_link_ksettings *cmd)
1149 struct dsa_port *dp = dsa_slave_to_port(dev);
1151 return phylink_ethtool_ksettings_set(dp->pl, cmd);
1154 static void dsa_slave_get_pause_stats(struct net_device *dev,
1155 struct ethtool_pause_stats *pause_stats)
1157 struct dsa_port *dp = dsa_slave_to_port(dev);
1158 struct dsa_switch *ds = dp->ds;
1160 if (ds->ops->get_pause_stats)
1161 ds->ops->get_pause_stats(ds, dp->index, pause_stats);
1164 static void dsa_slave_get_pauseparam(struct net_device *dev,
1165 struct ethtool_pauseparam *pause)
1167 struct dsa_port *dp = dsa_slave_to_port(dev);
1169 phylink_ethtool_get_pauseparam(dp->pl, pause);
1172 static int dsa_slave_set_pauseparam(struct net_device *dev,
1173 struct ethtool_pauseparam *pause)
1175 struct dsa_port *dp = dsa_slave_to_port(dev);
1177 return phylink_ethtool_set_pauseparam(dp->pl, pause);
1180 #ifdef CONFIG_NET_POLL_CONTROLLER
1181 static int dsa_slave_netpoll_setup(struct net_device *dev,
1182 struct netpoll_info *ni)
1184 struct net_device *master = dsa_slave_to_master(dev);
1185 struct dsa_slave_priv *p = netdev_priv(dev);
1186 struct netpoll *netpoll;
1189 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1193 err = __netpoll_setup(netpoll, master);
1199 p->netpoll = netpoll;
1204 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1206 struct dsa_slave_priv *p = netdev_priv(dev);
1207 struct netpoll *netpoll = p->netpoll;
1214 __netpoll_free(netpoll);
1217 static void dsa_slave_poll_controller(struct net_device *dev)
1222 static struct dsa_mall_tc_entry *
1223 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1225 struct dsa_slave_priv *p = netdev_priv(dev);
1226 struct dsa_mall_tc_entry *mall_tc_entry;
1228 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1229 if (mall_tc_entry->cookie == cookie)
1230 return mall_tc_entry;
1236 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
1237 struct tc_cls_matchall_offload *cls,
1240 struct netlink_ext_ack *extack = cls->common.extack;
1241 struct dsa_port *dp = dsa_slave_to_port(dev);
1242 struct dsa_slave_priv *p = netdev_priv(dev);
1243 struct dsa_mall_mirror_tc_entry *mirror;
1244 struct dsa_mall_tc_entry *mall_tc_entry;
1245 struct dsa_switch *ds = dp->ds;
1246 struct flow_action_entry *act;
1247 struct dsa_port *to_dp;
1250 if (!ds->ops->port_mirror_add)
1253 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1254 cls->common.extack))
1257 act = &cls->rule->action.entries[0];
1262 if (!dsa_slave_dev_check(act->dev))
1265 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1269 mall_tc_entry->cookie = cls->cookie;
1270 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1271 mirror = &mall_tc_entry->mirror;
1273 to_dp = dsa_slave_to_port(act->dev);
1275 mirror->to_local_port = to_dp->index;
1276 mirror->ingress = ingress;
1278 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1280 kfree(mall_tc_entry);
1284 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1290 dsa_slave_add_cls_matchall_police(struct net_device *dev,
1291 struct tc_cls_matchall_offload *cls,
1294 struct netlink_ext_ack *extack = cls->common.extack;
1295 struct dsa_port *dp = dsa_slave_to_port(dev);
1296 struct dsa_slave_priv *p = netdev_priv(dev);
1297 struct dsa_mall_policer_tc_entry *policer;
1298 struct dsa_mall_tc_entry *mall_tc_entry;
1299 struct dsa_switch *ds = dp->ds;
1300 struct flow_action_entry *act;
1303 if (!ds->ops->port_policer_add) {
1304 NL_SET_ERR_MSG_MOD(extack,
1305 "Policing offload not implemented");
1310 NL_SET_ERR_MSG_MOD(extack,
1311 "Only supported on ingress qdisc");
1315 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1316 cls->common.extack))
1319 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1320 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1321 NL_SET_ERR_MSG_MOD(extack,
1322 "Only one port policer allowed");
1327 act = &cls->rule->action.entries[0];
1329 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1333 mall_tc_entry->cookie = cls->cookie;
1334 mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1335 policer = &mall_tc_entry->policer;
1336 policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1337 policer->burst = act->police.burst;
1339 err = ds->ops->port_policer_add(ds, dp->index, policer);
1341 kfree(mall_tc_entry);
1345 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1350 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1351 struct tc_cls_matchall_offload *cls,
1354 int err = -EOPNOTSUPP;
1356 if (cls->common.protocol == htons(ETH_P_ALL) &&
1357 flow_offload_has_one_action(&cls->rule->action) &&
1358 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1359 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1360 else if (flow_offload_has_one_action(&cls->rule->action) &&
1361 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1362 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1367 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1368 struct tc_cls_matchall_offload *cls)
1370 struct dsa_port *dp = dsa_slave_to_port(dev);
1371 struct dsa_mall_tc_entry *mall_tc_entry;
1372 struct dsa_switch *ds = dp->ds;
1374 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1378 list_del(&mall_tc_entry->list);
1380 switch (mall_tc_entry->type) {
1381 case DSA_PORT_MALL_MIRROR:
1382 if (ds->ops->port_mirror_del)
1383 ds->ops->port_mirror_del(ds, dp->index,
1384 &mall_tc_entry->mirror);
1386 case DSA_PORT_MALL_POLICER:
1387 if (ds->ops->port_policer_del)
1388 ds->ops->port_policer_del(ds, dp->index);
1394 kfree(mall_tc_entry);
1397 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1398 struct tc_cls_matchall_offload *cls,
1401 if (cls->common.chain_index)
1404 switch (cls->command) {
1405 case TC_CLSMATCHALL_REPLACE:
1406 return dsa_slave_add_cls_matchall(dev, cls, ingress);
1407 case TC_CLSMATCHALL_DESTROY:
1408 dsa_slave_del_cls_matchall(dev, cls);
1415 static int dsa_slave_add_cls_flower(struct net_device *dev,
1416 struct flow_cls_offload *cls,
1419 struct dsa_port *dp = dsa_slave_to_port(dev);
1420 struct dsa_switch *ds = dp->ds;
1421 int port = dp->index;
1423 if (!ds->ops->cls_flower_add)
1426 return ds->ops->cls_flower_add(ds, port, cls, ingress);
1429 static int dsa_slave_del_cls_flower(struct net_device *dev,
1430 struct flow_cls_offload *cls,
1433 struct dsa_port *dp = dsa_slave_to_port(dev);
1434 struct dsa_switch *ds = dp->ds;
1435 int port = dp->index;
1437 if (!ds->ops->cls_flower_del)
1440 return ds->ops->cls_flower_del(ds, port, cls, ingress);
1443 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1444 struct flow_cls_offload *cls,
1447 struct dsa_port *dp = dsa_slave_to_port(dev);
1448 struct dsa_switch *ds = dp->ds;
1449 int port = dp->index;
1451 if (!ds->ops->cls_flower_stats)
1454 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1457 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1458 struct flow_cls_offload *cls,
1461 switch (cls->command) {
1462 case FLOW_CLS_REPLACE:
1463 return dsa_slave_add_cls_flower(dev, cls, ingress);
1464 case FLOW_CLS_DESTROY:
1465 return dsa_slave_del_cls_flower(dev, cls, ingress);
1466 case FLOW_CLS_STATS:
1467 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1473 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1474 void *cb_priv, bool ingress)
1476 struct net_device *dev = cb_priv;
1478 if (!tc_can_offload(dev))
1482 case TC_SETUP_CLSMATCHALL:
1483 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1484 case TC_SETUP_CLSFLOWER:
1485 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1491 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1492 void *type_data, void *cb_priv)
1494 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1497 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1498 void *type_data, void *cb_priv)
1500 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1503 static LIST_HEAD(dsa_slave_block_cb_list);
1505 static int dsa_slave_setup_tc_block(struct net_device *dev,
1506 struct flow_block_offload *f)
1508 struct flow_block_cb *block_cb;
1509 flow_setup_cb_t *cb;
1511 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1512 cb = dsa_slave_setup_tc_block_cb_ig;
1513 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1514 cb = dsa_slave_setup_tc_block_cb_eg;
1518 f->driver_block_list = &dsa_slave_block_cb_list;
1520 switch (f->command) {
1521 case FLOW_BLOCK_BIND:
1522 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1525 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1526 if (IS_ERR(block_cb))
1527 return PTR_ERR(block_cb);
1529 flow_block_cb_add(block_cb, f);
1530 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1532 case FLOW_BLOCK_UNBIND:
1533 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1537 flow_block_cb_remove(block_cb, f);
1538 list_del(&block_cb->driver_list);
1545 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
1548 struct net_device *master = dsa_port_to_master(dsa_to_port(ds, port));
1550 if (!master->netdev_ops->ndo_setup_tc)
1553 return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
1556 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1559 struct dsa_port *dp = dsa_slave_to_port(dev);
1560 struct dsa_switch *ds = dp->ds;
1563 case TC_SETUP_BLOCK:
1564 return dsa_slave_setup_tc_block(dev, type_data);
1566 return dsa_slave_setup_ft_block(ds, dp->index, type_data);
1571 if (!ds->ops->port_setup_tc)
1574 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1577 static int dsa_slave_get_rxnfc(struct net_device *dev,
1578 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1580 struct dsa_port *dp = dsa_slave_to_port(dev);
1581 struct dsa_switch *ds = dp->ds;
1583 if (!ds->ops->get_rxnfc)
1586 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1589 static int dsa_slave_set_rxnfc(struct net_device *dev,
1590 struct ethtool_rxnfc *nfc)
1592 struct dsa_port *dp = dsa_slave_to_port(dev);
1593 struct dsa_switch *ds = dp->ds;
1595 if (!ds->ops->set_rxnfc)
1598 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1601 static int dsa_slave_get_ts_info(struct net_device *dev,
1602 struct ethtool_ts_info *ts)
1604 struct dsa_slave_priv *p = netdev_priv(dev);
1605 struct dsa_switch *ds = p->dp->ds;
1607 if (!ds->ops->get_ts_info)
1610 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1613 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1616 struct dsa_port *dp = dsa_slave_to_port(dev);
1617 struct switchdev_obj_port_vlan vlan = {
1618 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1620 /* This API only allows programming tagged, non-PVID VIDs */
1623 struct netlink_ext_ack extack = {0};
1627 ret = dsa_port_vlan_add(dp, &vlan, &extack);
1630 netdev_err(dev, "%s\n", extack._msg);
1634 /* And CPU port... */
1635 ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
1638 netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1646 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1649 struct dsa_port *dp = dsa_slave_to_port(dev);
1650 struct switchdev_obj_port_vlan vlan = {
1652 /* This API only allows programming tagged, non-PVID VIDs */
1657 err = dsa_port_vlan_del(dp, &vlan);
1661 return dsa_port_host_vlan_del(dp, &vlan);
1664 static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
1666 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1668 return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
1671 static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
1673 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1675 return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
1678 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1679 * filtering is enabled. The baseline is that only ports that offload a
1680 * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1681 * but there are exceptions for quirky hardware.
1683 * If ds->vlan_filtering_is_global = true, then standalone ports which share
1684 * the same switch with other ports that offload a VLAN-aware bridge are also
1685 * inevitably VLAN-aware.
1687 * To summarize, a DSA switch port offloads:
1689 * - If standalone (this includes software bridge, software LAG):
1690 * - if ds->needs_standalone_vlan_filtering = true, OR if
1691 * (ds->vlan_filtering_is_global = true AND there are bridges spanning
1692 * this switch chip which have vlan_filtering=1)
1693 * - the 8021q upper VLANs
1694 * - else (standalone VLAN filtering is not needed, VLAN filtering is not
1695 * global, or it is, but no port is under a VLAN-aware bridge):
1696 * - no VLAN (any 8021q upper is a software VLAN)
1698 * - If under a vlan_filtering=0 bridge which it offload:
1699 * - if ds->configure_vlan_while_not_filtering = true (default):
1700 * - the bridge VLANs. These VLANs are committed to hardware but inactive.
1701 * - else (deprecated):
1702 * - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1703 * enabled, so this behavior is broken and discouraged.
1705 * - If under a vlan_filtering=1 bridge which it offload:
1706 * - the bridge VLANs
1707 * - the 8021q upper VLANs
1709 int dsa_slave_manage_vlan_filtering(struct net_device *slave,
1710 bool vlan_filtering)
1714 if (vlan_filtering) {
1715 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1717 err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
1719 vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1720 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1724 err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1728 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1734 struct dsa_hw_port {
1735 struct list_head list;
1736 struct net_device *dev;
1740 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1742 const struct dsa_hw_port *p;
1745 list_for_each_entry(p, hw_port_list, list) {
1746 if (p->dev->mtu == mtu)
1749 err = dev_set_mtu(p->dev, mtu);
1757 list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1758 if (p->dev->mtu == p->old_mtu)
1761 if (dev_set_mtu(p->dev, p->old_mtu))
1762 netdev_err(p->dev, "Failed to restore MTU\n");
1768 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1770 struct dsa_hw_port *p, *n;
1772 list_for_each_entry_safe(p, n, hw_port_list, list)
1776 /* Make the hardware datapath to/from @dev limited to a common MTU */
1777 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1779 struct list_head hw_port_list;
1780 struct dsa_switch_tree *dst;
1781 int min_mtu = ETH_MAX_MTU;
1782 struct dsa_port *other_dp;
1785 if (!dp->ds->mtu_enforcement_ingress)
1791 INIT_LIST_HEAD(&hw_port_list);
1793 /* Populate the list of ports that are part of the same bridge
1794 * as the newly added/modified port
1796 list_for_each_entry(dst, &dsa_tree_list, list) {
1797 list_for_each_entry(other_dp, &dst->ports, list) {
1798 struct dsa_hw_port *hw_port;
1799 struct net_device *slave;
1801 if (other_dp->type != DSA_PORT_TYPE_USER)
1804 if (!dsa_port_bridge_same(dp, other_dp))
1807 if (!other_dp->ds->mtu_enforcement_ingress)
1810 slave = other_dp->slave;
1812 if (min_mtu > slave->mtu)
1813 min_mtu = slave->mtu;
1815 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1819 hw_port->dev = slave;
1820 hw_port->old_mtu = slave->mtu;
1822 list_add(&hw_port->list, &hw_port_list);
1826 /* Attempt to configure the entire hardware bridge to the newly added
1827 * interface's MTU first, regardless of whether the intention of the
1828 * user was to raise or lower it.
1830 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1834 /* Clearly that didn't work out so well, so just set the minimum MTU on
1835 * all hardware bridge ports now. If this fails too, then all ports will
1836 * still have their old MTU rolled back anyway.
1838 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1841 dsa_hw_port_list_free(&hw_port_list);
1844 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1846 struct net_device *master = dsa_slave_to_master(dev);
1847 struct dsa_port *dp = dsa_slave_to_port(dev);
1848 struct dsa_port *cpu_dp = dp->cpu_dp;
1849 struct dsa_switch *ds = dp->ds;
1850 struct dsa_port *other_dp;
1851 int largest_mtu = 0;
1858 if (!ds->ops->port_change_mtu)
1861 dsa_tree_for_each_user_port(other_dp, ds->dst) {
1864 /* During probe, this function will be called for each slave
1865 * device, while not all of them have been allocated. That's
1866 * ok, it doesn't change what the maximum is, so ignore it.
1868 if (!other_dp->slave)
1871 /* Pretend that we already applied the setting, which we
1872 * actually haven't (still haven't done all integrity checks)
1875 slave_mtu = new_mtu;
1877 slave_mtu = other_dp->slave->mtu;
1879 if (largest_mtu < slave_mtu)
1880 largest_mtu = slave_mtu;
1883 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1884 old_master_mtu = master->mtu;
1885 new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1886 if (new_master_mtu > mtu_limit)
1889 /* If the master MTU isn't over limit, there's no need to check the CPU
1890 * MTU, since that surely isn't either.
1892 cpu_mtu = largest_mtu;
1894 /* Start applying stuff */
1895 if (new_master_mtu != old_master_mtu) {
1896 err = dev_set_mtu(master, new_master_mtu);
1898 goto out_master_failed;
1900 /* We only need to propagate the MTU of the CPU port to
1901 * upstream switches, so emit a notifier which updates them.
1903 err = dsa_port_mtu_change(cpu_dp, cpu_mtu);
1905 goto out_cpu_failed;
1908 err = ds->ops->port_change_mtu(ds, dp->index, new_mtu);
1910 goto out_port_failed;
1914 dsa_bridge_mtu_normalization(dp);
1919 if (new_master_mtu != old_master_mtu)
1920 dsa_port_mtu_change(cpu_dp, old_master_mtu -
1921 dsa_tag_protocol_overhead(cpu_dp->tag_ops));
1923 if (new_master_mtu != old_master_mtu)
1924 dev_set_mtu(master, old_master_mtu);
1929 static int __maybe_unused
1930 dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
1932 struct dsa_port *dp = dsa_slave_to_port(dev);
1933 struct dsa_switch *ds = dp->ds;
1934 unsigned long mask, new_prio;
1935 int err, port = dp->index;
1937 if (!ds->ops->port_set_default_prio)
1940 err = dcb_ieee_setapp(dev, app);
1944 mask = dcb_ieee_getapp_mask(dev, app);
1945 new_prio = __fls(mask);
1947 err = ds->ops->port_set_default_prio(ds, port, new_prio);
1949 dcb_ieee_delapp(dev, app);
1956 static int __maybe_unused
1957 dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
1959 struct dsa_port *dp = dsa_slave_to_port(dev);
1960 struct dsa_switch *ds = dp->ds;
1961 unsigned long mask, new_prio;
1962 int err, port = dp->index;
1963 u8 dscp = app->protocol;
1965 if (!ds->ops->port_add_dscp_prio)
1969 netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
1974 err = dcb_ieee_setapp(dev, app);
1978 mask = dcb_ieee_getapp_mask(dev, app);
1979 new_prio = __fls(mask);
1981 err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
1983 dcb_ieee_delapp(dev, app);
1990 static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev,
1991 struct dcb_app *app)
1993 switch (app->selector) {
1994 case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
1995 switch (app->protocol) {
1997 return dsa_slave_dcbnl_set_default_prio(dev, app);
2002 case IEEE_8021QAZ_APP_SEL_DSCP:
2003 return dsa_slave_dcbnl_add_dscp_prio(dev, app);
2009 static int __maybe_unused
2010 dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
2012 struct dsa_port *dp = dsa_slave_to_port(dev);
2013 struct dsa_switch *ds = dp->ds;
2014 unsigned long mask, new_prio;
2015 int err, port = dp->index;
2017 if (!ds->ops->port_set_default_prio)
2020 err = dcb_ieee_delapp(dev, app);
2024 mask = dcb_ieee_getapp_mask(dev, app);
2025 new_prio = mask ? __fls(mask) : 0;
2027 err = ds->ops->port_set_default_prio(ds, port, new_prio);
2029 dcb_ieee_setapp(dev, app);
2036 static int __maybe_unused
2037 dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
2039 struct dsa_port *dp = dsa_slave_to_port(dev);
2040 struct dsa_switch *ds = dp->ds;
2041 int err, port = dp->index;
2042 u8 dscp = app->protocol;
2044 if (!ds->ops->port_del_dscp_prio)
2047 err = dcb_ieee_delapp(dev, app);
2051 err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
2053 dcb_ieee_setapp(dev, app);
2060 static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev,
2061 struct dcb_app *app)
2063 switch (app->selector) {
2064 case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2065 switch (app->protocol) {
2067 return dsa_slave_dcbnl_del_default_prio(dev, app);
2072 case IEEE_8021QAZ_APP_SEL_DSCP:
2073 return dsa_slave_dcbnl_del_dscp_prio(dev, app);
2079 /* Pre-populate the DCB application priority table with the priorities
2080 * configured during switch setup, which we read from hardware here.
2082 static int dsa_slave_dcbnl_init(struct net_device *dev)
2084 struct dsa_port *dp = dsa_slave_to_port(dev);
2085 struct dsa_switch *ds = dp->ds;
2086 int port = dp->index;
2089 if (ds->ops->port_get_default_prio) {
2090 int prio = ds->ops->port_get_default_prio(ds, port);
2091 struct dcb_app app = {
2092 .selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2100 err = dcb_ieee_setapp(dev, &app);
2105 if (ds->ops->port_get_dscp_prio) {
2108 for (protocol = 0; protocol < 64; protocol++) {
2109 struct dcb_app app = {
2110 .selector = IEEE_8021QAZ_APP_SEL_DSCP,
2111 .protocol = protocol,
2115 prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
2116 if (prio == -EOPNOTSUPP)
2121 app.priority = prio;
2123 err = dcb_ieee_setapp(dev, &app);
2132 static const struct ethtool_ops dsa_slave_ethtool_ops = {
2133 .get_drvinfo = dsa_slave_get_drvinfo,
2134 .get_regs_len = dsa_slave_get_regs_len,
2135 .get_regs = dsa_slave_get_regs,
2136 .nway_reset = dsa_slave_nway_reset,
2137 .get_link = ethtool_op_get_link,
2138 .get_eeprom_len = dsa_slave_get_eeprom_len,
2139 .get_eeprom = dsa_slave_get_eeprom,
2140 .set_eeprom = dsa_slave_set_eeprom,
2141 .get_strings = dsa_slave_get_strings,
2142 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
2143 .get_sset_count = dsa_slave_get_sset_count,
2144 .get_eth_phy_stats = dsa_slave_get_eth_phy_stats,
2145 .get_eth_mac_stats = dsa_slave_get_eth_mac_stats,
2146 .get_eth_ctrl_stats = dsa_slave_get_eth_ctrl_stats,
2147 .get_rmon_stats = dsa_slave_get_rmon_stats,
2148 .set_wol = dsa_slave_set_wol,
2149 .get_wol = dsa_slave_get_wol,
2150 .set_eee = dsa_slave_set_eee,
2151 .get_eee = dsa_slave_get_eee,
2152 .get_link_ksettings = dsa_slave_get_link_ksettings,
2153 .set_link_ksettings = dsa_slave_set_link_ksettings,
2154 .get_pause_stats = dsa_slave_get_pause_stats,
2155 .get_pauseparam = dsa_slave_get_pauseparam,
2156 .set_pauseparam = dsa_slave_set_pauseparam,
2157 .get_rxnfc = dsa_slave_get_rxnfc,
2158 .set_rxnfc = dsa_slave_set_rxnfc,
2159 .get_ts_info = dsa_slave_get_ts_info,
2160 .self_test = dsa_slave_net_selftest,
2163 static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = {
2164 .ieee_setapp = dsa_slave_dcbnl_ieee_setapp,
2165 .ieee_delapp = dsa_slave_dcbnl_ieee_delapp,
2168 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
2170 struct dsa_port *dp = dsa_slave_to_port(dev);
2172 return &dp->devlink_port;
2175 static void dsa_slave_get_stats64(struct net_device *dev,
2176 struct rtnl_link_stats64 *s)
2178 struct dsa_port *dp = dsa_slave_to_port(dev);
2179 struct dsa_switch *ds = dp->ds;
2181 if (ds->ops->get_stats64)
2182 ds->ops->get_stats64(ds, dp->index, s);
2184 dev_get_tstats64(dev, s);
2187 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
2188 struct net_device_path *path)
2190 struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
2191 struct net_device *master = dsa_port_to_master(dp);
2192 struct dsa_port *cpu_dp = dp->cpu_dp;
2194 path->dev = ctx->dev;
2195 path->type = DEV_PATH_DSA;
2196 path->dsa.proto = cpu_dp->tag_ops->proto;
2197 path->dsa.port = dp->index;
2203 static const struct net_device_ops dsa_slave_netdev_ops = {
2204 .ndo_open = dsa_slave_open,
2205 .ndo_stop = dsa_slave_close,
2206 .ndo_start_xmit = dsa_slave_xmit,
2207 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
2208 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
2209 .ndo_set_mac_address = dsa_slave_set_mac_address,
2210 .ndo_fdb_dump = dsa_slave_fdb_dump,
2211 .ndo_eth_ioctl = dsa_slave_ioctl,
2212 .ndo_get_iflink = dsa_slave_get_iflink,
2213 #ifdef CONFIG_NET_POLL_CONTROLLER
2214 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
2215 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
2216 .ndo_poll_controller = dsa_slave_poll_controller,
2218 .ndo_setup_tc = dsa_slave_setup_tc,
2219 .ndo_get_stats64 = dsa_slave_get_stats64,
2220 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
2221 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
2222 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
2223 .ndo_change_mtu = dsa_slave_change_mtu,
2224 .ndo_fill_forward_path = dsa_slave_fill_forward_path,
2227 static struct device_type dsa_type = {
2231 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2233 const struct dsa_port *dp = dsa_to_port(ds, port);
2236 phylink_mac_change(dp->pl, up);
2238 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2240 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
2241 struct phylink_link_state *state)
2243 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
2244 struct dsa_switch *ds = dp->ds;
2246 /* No need to check that this operation is valid, the callback would
2247 * not be called if it was not.
2249 ds->ops->phylink_fixed_state(ds, dp->index, state);
2252 /* slave device setup *******************************************************/
2253 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
2256 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2257 struct dsa_switch *ds = dp->ds;
2259 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
2260 if (!slave_dev->phydev) {
2261 netdev_err(slave_dev, "no phy at %d\n", addr);
2265 slave_dev->phydev->dev_flags |= flags;
2267 return phylink_connect_phy(dp->pl, slave_dev->phydev);
2270 static int dsa_slave_phy_setup(struct net_device *slave_dev)
2272 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2273 struct device_node *port_dn = dp->dn;
2274 struct dsa_switch *ds = dp->ds;
2278 dp->pl_config.dev = &slave_dev->dev;
2279 dp->pl_config.type = PHYLINK_NETDEV;
2281 /* The get_fixed_state callback takes precedence over polling the
2282 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set
2283 * this if the switch provides such a callback.
2285 if (ds->ops->phylink_fixed_state) {
2286 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
2287 dp->pl_config.poll_fixed_state = true;
2290 ret = dsa_port_phylink_create(dp);
2294 if (ds->ops->get_phy_flags)
2295 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
2297 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
2298 if (ret == -ENODEV && ds->slave_mii_bus) {
2299 /* We could not connect to a designated PHY or SFP, so try to
2300 * use the switch internal MDIO bus instead
2302 ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
2305 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
2307 dsa_port_phylink_destroy(dp);
2313 void dsa_slave_setup_tagger(struct net_device *slave)
2315 struct dsa_port *dp = dsa_slave_to_port(slave);
2316 struct net_device *master = dsa_port_to_master(dp);
2317 struct dsa_slave_priv *p = netdev_priv(slave);
2318 const struct dsa_port *cpu_dp = dp->cpu_dp;
2319 const struct dsa_switch *ds = dp->ds;
2321 slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
2322 slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
2323 /* Try to save one extra realloc later in the TX path (in the master)
2324 * by also inheriting the master's needed headroom and tailroom.
2325 * The 8021q driver also does this.
2327 slave->needed_headroom += master->needed_headroom;
2328 slave->needed_tailroom += master->needed_tailroom;
2330 p->xmit = cpu_dp->tag_ops->xmit;
2332 slave->features = master->vlan_features | NETIF_F_HW_TC;
2333 slave->hw_features |= NETIF_F_HW_TC;
2334 slave->features |= NETIF_F_LLTX;
2335 if (slave->needed_tailroom)
2336 slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
2337 if (ds->needs_standalone_vlan_filtering)
2338 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2341 int dsa_slave_suspend(struct net_device *slave_dev)
2343 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2345 if (!netif_running(slave_dev))
2348 netif_device_detach(slave_dev);
2351 phylink_stop(dp->pl);
2357 int dsa_slave_resume(struct net_device *slave_dev)
2359 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2361 if (!netif_running(slave_dev))
2364 netif_device_attach(slave_dev);
2367 phylink_start(dp->pl);
2373 int dsa_slave_create(struct dsa_port *port)
2375 struct net_device *master = dsa_port_to_master(port);
2376 struct dsa_switch *ds = port->ds;
2377 const char *name = port->name;
2378 struct net_device *slave_dev;
2379 struct dsa_slave_priv *p;
2382 if (!ds->num_tx_queues)
2383 ds->num_tx_queues = 1;
2385 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
2386 NET_NAME_UNKNOWN, ether_setup,
2387 ds->num_tx_queues, 1);
2388 if (slave_dev == NULL)
2391 slave_dev->rtnl_link_ops = &dsa_link_ops;
2392 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2393 #if IS_ENABLED(CONFIG_DCB)
2394 slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops;
2396 if (!is_zero_ether_addr(port->mac))
2397 eth_hw_addr_set(slave_dev, port->mac);
2399 eth_hw_addr_inherit(slave_dev, master);
2400 slave_dev->priv_flags |= IFF_NO_QUEUE;
2401 if (dsa_switch_supports_uc_filtering(ds))
2402 slave_dev->priv_flags |= IFF_UNICAST_FLT;
2403 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
2404 if (ds->ops->port_max_mtu)
2405 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2406 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
2408 SET_NETDEV_DEV(slave_dev, port->ds->dev);
2409 slave_dev->dev.of_node = port->dn;
2410 slave_dev->vlan_features = master->vlan_features;
2412 p = netdev_priv(slave_dev);
2413 slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2414 if (!slave_dev->tstats) {
2415 free_netdev(slave_dev);
2419 ret = gro_cells_init(&p->gcells, slave_dev);
2424 INIT_LIST_HEAD(&p->mall_tc_list);
2425 port->slave = slave_dev;
2426 dsa_slave_setup_tagger(slave_dev);
2428 netif_carrier_off(slave_dev);
2430 ret = dsa_slave_phy_setup(slave_dev);
2432 netdev_err(slave_dev,
2433 "error %d setting up PHY for tree %d, switch %d, port %d\n",
2434 ret, ds->dst->index, ds->index, port->index);
2440 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2441 if (ret && ret != -EOPNOTSUPP)
2442 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2443 ret, ETH_DATA_LEN, port->index);
2445 ret = register_netdevice(slave_dev);
2447 netdev_err(master, "error %d registering interface %s\n",
2448 ret, slave_dev->name);
2453 if (IS_ENABLED(CONFIG_DCB)) {
2454 ret = dsa_slave_dcbnl_init(slave_dev);
2456 netdev_err(slave_dev,
2457 "failed to initialize DCB: %pe\n",
2460 goto out_unregister;
2464 ret = netdev_upper_dev_link(master, slave_dev, NULL);
2469 goto out_unregister;
2474 unregister_netdev(slave_dev);
2477 phylink_disconnect_phy(p->dp->pl);
2479 dsa_port_phylink_destroy(p->dp);
2481 gro_cells_destroy(&p->gcells);
2483 free_percpu(slave_dev->tstats);
2484 free_netdev(slave_dev);
2489 void dsa_slave_destroy(struct net_device *slave_dev)
2491 struct net_device *master = dsa_slave_to_master(slave_dev);
2492 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2493 struct dsa_slave_priv *p = netdev_priv(slave_dev);
2495 netif_carrier_off(slave_dev);
2497 netdev_upper_dev_unlink(master, slave_dev);
2498 unregister_netdevice(slave_dev);
2499 phylink_disconnect_phy(dp->pl);
2502 dsa_port_phylink_destroy(dp);
2503 gro_cells_destroy(&p->gcells);
2504 free_percpu(slave_dev->tstats);
2505 free_netdev(slave_dev);
2508 int dsa_slave_change_master(struct net_device *dev, struct net_device *master,
2509 struct netlink_ext_ack *extack)
2511 struct net_device *old_master = dsa_slave_to_master(dev);
2512 struct dsa_port *dp = dsa_slave_to_port(dev);
2513 struct dsa_switch *ds = dp->ds;
2514 struct net_device *upper;
2515 struct list_head *iter;
2518 if (master == old_master)
2521 if (!ds->ops->port_change_master) {
2522 NL_SET_ERR_MSG_MOD(extack,
2523 "Driver does not support changing DSA master");
2527 if (!netdev_uses_dsa(master)) {
2528 NL_SET_ERR_MSG_MOD(extack,
2529 "Interface not eligible as DSA master");
2533 netdev_for_each_upper_dev_rcu(master, upper, iter) {
2534 if (dsa_slave_dev_check(upper))
2536 if (netif_is_bridge_master(upper))
2538 NL_SET_ERR_MSG_MOD(extack, "Cannot join master with unknown uppers");
2542 /* Since we allow live-changing the DSA master, plus we auto-open the
2543 * DSA master when the user port opens => we need to ensure that the
2544 * new DSA master is open too.
2546 if (dev->flags & IFF_UP) {
2547 err = dev_open(master, extack);
2552 netdev_upper_dev_unlink(old_master, dev);
2554 err = netdev_upper_dev_link(master, dev, extack);
2556 goto out_revert_old_master_unlink;
2558 err = dsa_port_change_master(dp, master, extack);
2560 goto out_revert_master_link;
2562 /* Update the MTU of the new CPU port through cross-chip notifiers */
2563 err = dsa_slave_change_mtu(dev, dev->mtu);
2564 if (err && err != -EOPNOTSUPP) {
2566 "nonfatal error updating MTU with new master: %pe\n",
2570 /* If the port doesn't have its own MAC address and relies on the DSA
2571 * master's one, inherit it again from the new DSA master.
2573 if (is_zero_ether_addr(dp->mac))
2574 eth_hw_addr_inherit(dev, master);
2578 out_revert_master_link:
2579 netdev_upper_dev_unlink(master, dev);
2580 out_revert_old_master_unlink:
2581 netdev_upper_dev_link(old_master, dev, NULL);
2585 bool dsa_slave_dev_check(const struct net_device *dev)
2587 return dev->netdev_ops == &dsa_slave_netdev_ops;
2589 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2591 static int dsa_slave_changeupper(struct net_device *dev,
2592 struct netdev_notifier_changeupper_info *info)
2594 struct dsa_port *dp = dsa_slave_to_port(dev);
2595 struct netlink_ext_ack *extack;
2596 int err = NOTIFY_DONE;
2598 if (!dsa_slave_dev_check(dev))
2601 extack = netdev_notifier_info_to_extack(&info->info);
2603 if (netif_is_bridge_master(info->upper_dev)) {
2604 if (info->linking) {
2605 err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2607 dsa_bridge_mtu_normalization(dp);
2608 if (err == -EOPNOTSUPP) {
2609 if (extack && !extack->_msg)
2610 NL_SET_ERR_MSG_MOD(extack,
2611 "Offloading not supported");
2614 err = notifier_from_errno(err);
2616 dsa_port_bridge_leave(dp, info->upper_dev);
2619 } else if (netif_is_lag_master(info->upper_dev)) {
2620 if (info->linking) {
2621 err = dsa_port_lag_join(dp, info->upper_dev,
2622 info->upper_info, extack);
2623 if (err == -EOPNOTSUPP) {
2624 NL_SET_ERR_MSG_MOD(info->info.extack,
2625 "Offloading not supported");
2628 err = notifier_from_errno(err);
2630 dsa_port_lag_leave(dp, info->upper_dev);
2633 } else if (is_hsr_master(info->upper_dev)) {
2634 if (info->linking) {
2635 err = dsa_port_hsr_join(dp, info->upper_dev);
2636 if (err == -EOPNOTSUPP) {
2637 NL_SET_ERR_MSG_MOD(info->info.extack,
2638 "Offloading not supported");
2641 err = notifier_from_errno(err);
2643 dsa_port_hsr_leave(dp, info->upper_dev);
2651 static int dsa_slave_prechangeupper(struct net_device *dev,
2652 struct netdev_notifier_changeupper_info *info)
2654 struct dsa_port *dp = dsa_slave_to_port(dev);
2656 if (!dsa_slave_dev_check(dev))
2659 if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2660 dsa_port_pre_bridge_leave(dp, info->upper_dev);
2661 else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2662 dsa_port_pre_lag_leave(dp, info->upper_dev);
2663 /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2664 * meaningfully enslaved to a bridge yet
2671 dsa_slave_lag_changeupper(struct net_device *dev,
2672 struct netdev_notifier_changeupper_info *info)
2674 struct net_device *lower;
2675 struct list_head *iter;
2676 int err = NOTIFY_DONE;
2677 struct dsa_port *dp;
2679 if (!netif_is_lag_master(dev))
2682 netdev_for_each_lower_dev(dev, lower, iter) {
2683 if (!dsa_slave_dev_check(lower))
2686 dp = dsa_slave_to_port(lower);
2691 err = dsa_slave_changeupper(lower, info);
2692 if (notifier_to_errno(err))
2699 /* Same as dsa_slave_lag_changeupper() except that it calls
2700 * dsa_slave_prechangeupper()
2703 dsa_slave_lag_prechangeupper(struct net_device *dev,
2704 struct netdev_notifier_changeupper_info *info)
2706 struct net_device *lower;
2707 struct list_head *iter;
2708 int err = NOTIFY_DONE;
2709 struct dsa_port *dp;
2711 if (!netif_is_lag_master(dev))
2714 netdev_for_each_lower_dev(dev, lower, iter) {
2715 if (!dsa_slave_dev_check(lower))
2718 dp = dsa_slave_to_port(lower);
2723 err = dsa_slave_prechangeupper(lower, info);
2724 if (notifier_to_errno(err))
2732 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2733 struct netdev_notifier_changeupper_info *info)
2735 struct netlink_ext_ack *ext_ack;
2736 struct net_device *slave, *br;
2737 struct dsa_port *dp;
2739 ext_ack = netdev_notifier_info_to_extack(&info->info);
2741 if (!is_vlan_dev(dev))
2744 slave = vlan_dev_real_dev(dev);
2745 if (!dsa_slave_dev_check(slave))
2748 dp = dsa_slave_to_port(slave);
2749 br = dsa_port_bridge_dev_get(dp);
2753 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
2754 if (br_vlan_enabled(br) &&
2755 netif_is_bridge_master(info->upper_dev) && info->linking) {
2756 NL_SET_ERR_MSG_MOD(ext_ack,
2757 "Cannot enslave VLAN device into VLAN aware bridge");
2758 return notifier_from_errno(-EINVAL);
2765 dsa_slave_check_8021q_upper(struct net_device *dev,
2766 struct netdev_notifier_changeupper_info *info)
2768 struct dsa_port *dp = dsa_slave_to_port(dev);
2769 struct net_device *br = dsa_port_bridge_dev_get(dp);
2770 struct bridge_vlan_info br_info;
2771 struct netlink_ext_ack *extack;
2772 int err = NOTIFY_DONE;
2775 if (!br || !br_vlan_enabled(br))
2778 extack = netdev_notifier_info_to_extack(&info->info);
2779 vid = vlan_dev_vlan_id(info->upper_dev);
2781 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2782 * device, respectively the VID is not found, returning
2783 * 0 means success, which is a failure for us here.
2785 err = br_vlan_get_info(br, vid, &br_info);
2787 NL_SET_ERR_MSG_MOD(extack,
2788 "This VLAN is already configured by the bridge");
2789 return notifier_from_errno(-EBUSY);
2796 dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
2797 struct netdev_notifier_changeupper_info *info)
2799 struct dsa_switch *ds;
2800 struct dsa_port *dp;
2803 if (!dsa_slave_dev_check(dev))
2804 return dsa_prevent_bridging_8021q_upper(dev, info);
2806 dp = dsa_slave_to_port(dev);
2809 if (ds->ops->port_prechangeupper) {
2810 err = ds->ops->port_prechangeupper(ds, dp->index, info);
2812 return notifier_from_errno(err);
2815 if (is_vlan_dev(info->upper_dev))
2816 return dsa_slave_check_8021q_upper(dev, info);
2821 /* To be eligible as a DSA master, a LAG must have all lower interfaces be
2822 * eligible DSA masters. Additionally, all LAG slaves must be DSA masters of
2823 * switches in the same switch tree.
2825 static int dsa_lag_master_validate(struct net_device *lag_dev,
2826 struct netlink_ext_ack *extack)
2828 struct net_device *lower1, *lower2;
2829 struct list_head *iter1, *iter2;
2831 netdev_for_each_lower_dev(lag_dev, lower1, iter1) {
2832 netdev_for_each_lower_dev(lag_dev, lower2, iter2) {
2833 if (!netdev_uses_dsa(lower1) ||
2834 !netdev_uses_dsa(lower2)) {
2835 NL_SET_ERR_MSG_MOD(extack,
2836 "All LAG ports must be eligible as DSA masters");
2837 return notifier_from_errno(-EINVAL);
2840 if (lower1 == lower2)
2843 if (!dsa_port_tree_same(lower1->dsa_ptr,
2845 NL_SET_ERR_MSG_MOD(extack,
2846 "LAG contains DSA masters of disjoint switch trees");
2847 return notifier_from_errno(-EINVAL);
2856 dsa_master_prechangeupper_sanity_check(struct net_device *master,
2857 struct netdev_notifier_changeupper_info *info)
2859 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
2861 if (!netdev_uses_dsa(master))
2867 /* Allow DSA switch uppers */
2868 if (dsa_slave_dev_check(info->upper_dev))
2871 /* Allow bridge uppers of DSA masters, subject to further
2872 * restrictions in dsa_bridge_prechangelower_sanity_check()
2874 if (netif_is_bridge_master(info->upper_dev))
2877 /* Allow LAG uppers, subject to further restrictions in
2878 * dsa_lag_master_prechangelower_sanity_check()
2880 if (netif_is_lag_master(info->upper_dev))
2881 return dsa_lag_master_validate(info->upper_dev, extack);
2883 NL_SET_ERR_MSG_MOD(extack,
2884 "DSA master cannot join unknown upper interfaces");
2885 return notifier_from_errno(-EBUSY);
2889 dsa_lag_master_prechangelower_sanity_check(struct net_device *dev,
2890 struct netdev_notifier_changeupper_info *info)
2892 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
2893 struct net_device *lag_dev = info->upper_dev;
2894 struct net_device *lower;
2895 struct list_head *iter;
2897 if (!netdev_uses_dsa(lag_dev) || !netif_is_lag_master(lag_dev))
2903 if (!netdev_uses_dsa(dev)) {
2904 NL_SET_ERR_MSG(extack,
2905 "Only DSA masters can join a LAG DSA master");
2906 return notifier_from_errno(-EINVAL);
2909 netdev_for_each_lower_dev(lag_dev, lower, iter) {
2910 if (!dsa_port_tree_same(dev->dsa_ptr, lower->dsa_ptr)) {
2911 NL_SET_ERR_MSG(extack,
2912 "Interface is DSA master for a different switch tree than this LAG");
2913 return notifier_from_errno(-EINVAL);
2922 /* Don't allow bridging of DSA masters, since the bridge layer rx_handler
2923 * prevents the DSA fake ethertype handler to be invoked, so we don't get the
2924 * chance to strip off and parse the DSA switch tag protocol header (the bridge
2925 * layer just returns RX_HANDLER_CONSUMED, stopping RX processing for these
2927 * The only case where that would not be an issue is when bridging can already
2928 * be offloaded, such as when the DSA master is itself a DSA or plain switchdev
2929 * port, and is bridged only with other ports from the same hardware device.
2932 dsa_bridge_prechangelower_sanity_check(struct net_device *new_lower,
2933 struct netdev_notifier_changeupper_info *info)
2935 struct net_device *br = info->upper_dev;
2936 struct netlink_ext_ack *extack;
2937 struct net_device *lower;
2938 struct list_head *iter;
2940 if (!netif_is_bridge_master(br))
2946 extack = netdev_notifier_info_to_extack(&info->info);
2948 netdev_for_each_lower_dev(br, lower, iter) {
2949 if (!netdev_uses_dsa(new_lower) && !netdev_uses_dsa(lower))
2952 if (!netdev_port_same_parent_id(lower, new_lower)) {
2953 NL_SET_ERR_MSG(extack,
2954 "Cannot do software bridging with a DSA master");
2955 return notifier_from_errno(-EINVAL);
2962 static void dsa_tree_migrate_ports_from_lag_master(struct dsa_switch_tree *dst,
2963 struct net_device *lag_dev)
2965 struct net_device *new_master = dsa_tree_find_first_master(dst);
2966 struct dsa_port *dp;
2969 dsa_tree_for_each_user_port(dp, dst) {
2970 if (dsa_port_to_master(dp) != lag_dev)
2973 err = dsa_slave_change_master(dp->slave, new_master, NULL);
2975 netdev_err(dp->slave,
2976 "failed to restore master to %s: %pe\n",
2977 new_master->name, ERR_PTR(err));
2982 static int dsa_master_lag_join(struct net_device *master,
2983 struct net_device *lag_dev,
2984 struct netdev_lag_upper_info *uinfo,
2985 struct netlink_ext_ack *extack)
2987 struct dsa_port *cpu_dp = master->dsa_ptr;
2988 struct dsa_switch_tree *dst = cpu_dp->dst;
2989 struct dsa_port *dp;
2992 err = dsa_master_lag_setup(lag_dev, cpu_dp, uinfo, extack);
2996 dsa_tree_for_each_user_port(dp, dst) {
2997 if (dsa_port_to_master(dp) != master)
3000 err = dsa_slave_change_master(dp->slave, lag_dev, extack);
3008 dsa_tree_for_each_user_port_continue_reverse(dp, dst) {
3009 if (dsa_port_to_master(dp) != lag_dev)
3012 err = dsa_slave_change_master(dp->slave, master, NULL);
3014 netdev_err(dp->slave,
3015 "failed to restore master to %s: %pe\n",
3016 master->name, ERR_PTR(err));
3020 dsa_master_lag_teardown(lag_dev, master->dsa_ptr);
3025 static void dsa_master_lag_leave(struct net_device *master,
3026 struct net_device *lag_dev)
3028 struct dsa_port *dp, *cpu_dp = lag_dev->dsa_ptr;
3029 struct dsa_switch_tree *dst = cpu_dp->dst;
3030 struct dsa_port *new_cpu_dp = NULL;
3031 struct net_device *lower;
3032 struct list_head *iter;
3034 netdev_for_each_lower_dev(lag_dev, lower, iter) {
3035 if (netdev_uses_dsa(lower)) {
3036 new_cpu_dp = lower->dsa_ptr;
3042 /* Update the CPU port of the user ports still under the LAG
3043 * so that dsa_port_to_master() continues to work properly
3045 dsa_tree_for_each_user_port(dp, dst)
3046 if (dsa_port_to_master(dp) == lag_dev)
3047 dp->cpu_dp = new_cpu_dp;
3049 /* Update the index of the virtual CPU port to match the lowest
3052 lag_dev->dsa_ptr = new_cpu_dp;
3055 /* If the LAG DSA master has no ports left, migrate back all
3056 * user ports to the first physical CPU port
3058 dsa_tree_migrate_ports_from_lag_master(dst, lag_dev);
3061 /* This DSA master has left its LAG in any case, so let
3062 * the CPU port leave the hardware LAG as well
3064 dsa_master_lag_teardown(lag_dev, master->dsa_ptr);
3067 static int dsa_master_changeupper(struct net_device *dev,
3068 struct netdev_notifier_changeupper_info *info)
3070 struct netlink_ext_ack *extack;
3071 int err = NOTIFY_DONE;
3073 if (!netdev_uses_dsa(dev))
3076 extack = netdev_notifier_info_to_extack(&info->info);
3078 if (netif_is_lag_master(info->upper_dev)) {
3079 if (info->linking) {
3080 err = dsa_master_lag_join(dev, info->upper_dev,
3081 info->upper_info, extack);
3082 err = notifier_from_errno(err);
3084 dsa_master_lag_leave(dev, info->upper_dev);
3092 static int dsa_slave_netdevice_event(struct notifier_block *nb,
3093 unsigned long event, void *ptr)
3095 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3098 case NETDEV_PRECHANGEUPPER: {
3099 struct netdev_notifier_changeupper_info *info = ptr;
3102 err = dsa_slave_prechangeupper_sanity_check(dev, info);
3103 if (notifier_to_errno(err))
3106 err = dsa_master_prechangeupper_sanity_check(dev, info);
3107 if (notifier_to_errno(err))
3110 err = dsa_lag_master_prechangelower_sanity_check(dev, info);
3111 if (notifier_to_errno(err))
3114 err = dsa_bridge_prechangelower_sanity_check(dev, info);
3115 if (notifier_to_errno(err))
3118 err = dsa_slave_prechangeupper(dev, ptr);
3119 if (notifier_to_errno(err))
3122 err = dsa_slave_lag_prechangeupper(dev, ptr);
3123 if (notifier_to_errno(err))
3128 case NETDEV_CHANGEUPPER: {
3131 err = dsa_slave_changeupper(dev, ptr);
3132 if (notifier_to_errno(err))
3135 err = dsa_slave_lag_changeupper(dev, ptr);
3136 if (notifier_to_errno(err))
3139 err = dsa_master_changeupper(dev, ptr);
3140 if (notifier_to_errno(err))
3145 case NETDEV_CHANGELOWERSTATE: {
3146 struct netdev_notifier_changelowerstate_info *info = ptr;
3147 struct dsa_port *dp;
3150 if (dsa_slave_dev_check(dev)) {
3151 dp = dsa_slave_to_port(dev);
3153 err = dsa_port_lag_change(dp, info->lower_state_info);
3156 /* Mirror LAG port events on DSA masters that are in
3157 * a LAG towards their respective switch CPU ports
3159 if (netdev_uses_dsa(dev)) {
3162 err = dsa_port_lag_change(dp, info->lower_state_info);
3165 return notifier_from_errno(err);
3169 /* Track state of master port.
3170 * DSA driver may require the master port (and indirectly
3171 * the tagger) to be available for some special operation.
3173 if (netdev_uses_dsa(dev)) {
3174 struct dsa_port *cpu_dp = dev->dsa_ptr;
3175 struct dsa_switch_tree *dst = cpu_dp->ds->dst;
3177 /* Track when the master port is UP */
3178 dsa_tree_master_oper_state_change(dst, dev,
3179 netif_oper_up(dev));
3181 /* Track when the master port is ready and can accept
3183 * NETDEV_UP event is not enough to flag a port as ready.
3184 * We also have to wait for linkwatch_do_dev to dev_activate
3185 * and emit a NETDEV_CHANGE event.
3186 * We check if a master port is ready by checking if the dev
3187 * have a qdisc assigned and is not noop.
3189 dsa_tree_master_admin_state_change(dst, dev,
3190 !qdisc_tx_is_noop(dev));
3197 case NETDEV_GOING_DOWN: {
3198 struct dsa_port *dp, *cpu_dp;
3199 struct dsa_switch_tree *dst;
3200 LIST_HEAD(close_list);
3202 if (!netdev_uses_dsa(dev))
3205 cpu_dp = dev->dsa_ptr;
3206 dst = cpu_dp->ds->dst;
3208 dsa_tree_master_admin_state_change(dst, dev, false);
3210 list_for_each_entry(dp, &dst->ports, list) {
3211 if (!dsa_port_is_user(dp))
3214 if (dp->cpu_dp != cpu_dp)
3217 list_add(&dp->slave->close_list, &close_list);
3220 dev_close_many(&close_list, true);
3232 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
3234 struct switchdev_notifier_fdb_info info = {};
3236 info.addr = switchdev_work->addr;
3237 info.vid = switchdev_work->vid;
3238 info.offloaded = true;
3239 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
3240 switchdev_work->orig_dev, &info.info, NULL);
3243 static void dsa_slave_switchdev_event_work(struct work_struct *work)
3245 struct dsa_switchdev_event_work *switchdev_work =
3246 container_of(work, struct dsa_switchdev_event_work, work);
3247 const unsigned char *addr = switchdev_work->addr;
3248 struct net_device *dev = switchdev_work->dev;
3249 u16 vid = switchdev_work->vid;
3250 struct dsa_switch *ds;
3251 struct dsa_port *dp;
3254 dp = dsa_slave_to_port(dev);
3257 switch (switchdev_work->event) {
3258 case SWITCHDEV_FDB_ADD_TO_DEVICE:
3259 if (switchdev_work->host_addr)
3260 err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
3262 err = dsa_port_lag_fdb_add(dp, addr, vid);
3264 err = dsa_port_fdb_add(dp, addr, vid);
3267 "port %d failed to add %pM vid %d to fdb: %d\n",
3268 dp->index, addr, vid, err);
3271 dsa_fdb_offload_notify(switchdev_work);
3274 case SWITCHDEV_FDB_DEL_TO_DEVICE:
3275 if (switchdev_work->host_addr)
3276 err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
3278 err = dsa_port_lag_fdb_del(dp, addr, vid);
3280 err = dsa_port_fdb_del(dp, addr, vid);
3283 "port %d failed to delete %pM vid %d from fdb: %d\n",
3284 dp->index, addr, vid, err);
3290 kfree(switchdev_work);
3293 static bool dsa_foreign_dev_check(const struct net_device *dev,
3294 const struct net_device *foreign_dev)
3296 const struct dsa_port *dp = dsa_slave_to_port(dev);
3297 struct dsa_switch_tree *dst = dp->ds->dst;
3299 if (netif_is_bridge_master(foreign_dev))
3300 return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
3302 if (netif_is_bridge_port(foreign_dev))
3303 return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
3305 /* Everything else is foreign */
3309 static int dsa_slave_fdb_event(struct net_device *dev,
3310 struct net_device *orig_dev,
3311 unsigned long event, const void *ctx,
3312 const struct switchdev_notifier_fdb_info *fdb_info)
3314 struct dsa_switchdev_event_work *switchdev_work;
3315 struct dsa_port *dp = dsa_slave_to_port(dev);
3316 bool host_addr = fdb_info->is_local;
3317 struct dsa_switch *ds = dp->ds;
3319 if (ctx && ctx != dp)
3325 if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
3326 if (dsa_port_offloads_bridge_port(dp, orig_dev))
3329 /* FDB entries learned by the software bridge or by foreign
3330 * bridge ports should be installed as host addresses only if
3331 * the driver requests assisted learning.
3333 if (!ds->assisted_learning_on_cpu_port)
3337 /* Also treat FDB entries on foreign interfaces bridged with us as host
3340 if (dsa_foreign_dev_check(dev, orig_dev))
3343 /* Check early that we're not doing work in vain.
3344 * Host addresses on LAG ports still require regular FDB ops,
3345 * since the CPU port isn't in a LAG.
3347 if (dp->lag && !host_addr) {
3348 if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
3351 if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
3355 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3356 if (!switchdev_work)
3359 netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
3360 event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
3361 orig_dev->name, fdb_info->addr, fdb_info->vid,
3362 host_addr ? " as host address" : "");
3364 INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
3365 switchdev_work->event = event;
3366 switchdev_work->dev = dev;
3367 switchdev_work->orig_dev = orig_dev;
3369 ether_addr_copy(switchdev_work->addr, fdb_info->addr);
3370 switchdev_work->vid = fdb_info->vid;
3371 switchdev_work->host_addr = host_addr;
3373 dsa_schedule_work(&switchdev_work->work);
3378 /* Called under rcu_read_lock() */
3379 static int dsa_slave_switchdev_event(struct notifier_block *unused,
3380 unsigned long event, void *ptr)
3382 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3386 case SWITCHDEV_PORT_ATTR_SET:
3387 err = switchdev_handle_port_attr_set(dev, ptr,
3388 dsa_slave_dev_check,
3389 dsa_slave_port_attr_set);
3390 return notifier_from_errno(err);
3391 case SWITCHDEV_FDB_ADD_TO_DEVICE:
3392 case SWITCHDEV_FDB_DEL_TO_DEVICE:
3393 err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
3394 dsa_slave_dev_check,
3395 dsa_foreign_dev_check,
3396 dsa_slave_fdb_event);
3397 return notifier_from_errno(err);
3405 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
3406 unsigned long event, void *ptr)
3408 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3412 case SWITCHDEV_PORT_OBJ_ADD:
3413 err = switchdev_handle_port_obj_add_foreign(dev, ptr,
3414 dsa_slave_dev_check,
3415 dsa_foreign_dev_check,
3416 dsa_slave_port_obj_add);
3417 return notifier_from_errno(err);
3418 case SWITCHDEV_PORT_OBJ_DEL:
3419 err = switchdev_handle_port_obj_del_foreign(dev, ptr,
3420 dsa_slave_dev_check,
3421 dsa_foreign_dev_check,
3422 dsa_slave_port_obj_del);
3423 return notifier_from_errno(err);
3424 case SWITCHDEV_PORT_ATTR_SET:
3425 err = switchdev_handle_port_attr_set(dev, ptr,
3426 dsa_slave_dev_check,
3427 dsa_slave_port_attr_set);
3428 return notifier_from_errno(err);
3434 static struct notifier_block dsa_slave_nb __read_mostly = {
3435 .notifier_call = dsa_slave_netdevice_event,
3438 struct notifier_block dsa_slave_switchdev_notifier = {
3439 .notifier_call = dsa_slave_switchdev_event,
3442 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
3443 .notifier_call = dsa_slave_switchdev_blocking_event,
3446 int dsa_slave_register_notifier(void)
3448 struct notifier_block *nb;
3451 err = register_netdevice_notifier(&dsa_slave_nb);
3455 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
3457 goto err_switchdev_nb;
3459 nb = &dsa_slave_switchdev_blocking_notifier;
3460 err = register_switchdev_blocking_notifier(nb);
3462 goto err_switchdev_blocking_nb;
3466 err_switchdev_blocking_nb:
3467 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3469 unregister_netdevice_notifier(&dsa_slave_nb);
3473 void dsa_slave_unregister_notifier(void)
3475 struct notifier_block *nb;
3478 nb = &dsa_slave_switchdev_blocking_notifier;
3479 err = unregister_switchdev_blocking_notifier(nb);
3481 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
3483 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3485 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3487 err = unregister_netdevice_notifier(&dsa_slave_nb);
3489 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);