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 /* slave mii_bus handling ***************************************************/
168 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
170 struct dsa_switch *ds = bus->priv;
172 if (ds->phys_mii_mask & (1 << addr))
173 return ds->ops->phy_read(ds, addr, reg);
178 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
180 struct dsa_switch *ds = bus->priv;
182 if (ds->phys_mii_mask & (1 << addr))
183 return ds->ops->phy_write(ds, addr, reg, val);
188 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
190 ds->slave_mii_bus->priv = (void *)ds;
191 ds->slave_mii_bus->name = "dsa slave smi";
192 ds->slave_mii_bus->read = dsa_slave_phy_read;
193 ds->slave_mii_bus->write = dsa_slave_phy_write;
194 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
195 ds->dst->index, ds->index);
196 ds->slave_mii_bus->parent = ds->dev;
197 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
201 /* slave device handling ****************************************************/
202 static int dsa_slave_get_iflink(const struct net_device *dev)
204 return dsa_slave_to_master(dev)->ifindex;
207 static int dsa_slave_open(struct net_device *dev)
209 struct net_device *master = dsa_slave_to_master(dev);
210 struct dsa_port *dp = dsa_slave_to_port(dev);
211 struct dsa_switch *ds = dp->ds;
214 err = dev_open(master, NULL);
216 netdev_err(dev, "failed to open master %s\n", master->name);
220 if (dsa_switch_supports_uc_filtering(ds)) {
221 err = dsa_port_standalone_host_fdb_add(dp, dev->dev_addr, 0);
226 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
227 err = dev_uc_add(master, dev->dev_addr);
232 err = dsa_port_enable_rt(dp, dev->phydev);
239 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
240 dev_uc_del(master, dev->dev_addr);
242 if (dsa_switch_supports_uc_filtering(ds))
243 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
248 static int dsa_slave_close(struct net_device *dev)
250 struct net_device *master = dsa_slave_to_master(dev);
251 struct dsa_port *dp = dsa_slave_to_port(dev);
252 struct dsa_switch *ds = dp->ds;
254 dsa_port_disable_rt(dp);
256 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
257 dev_uc_del(master, dev->dev_addr);
259 if (dsa_switch_supports_uc_filtering(ds))
260 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
265 /* Keep flooding enabled towards this port's CPU port as long as it serves at
266 * least one port in the tree that requires it.
268 static void dsa_port_manage_cpu_flood(struct dsa_port *dp)
270 struct switchdev_brport_flags flags = {
271 .mask = BR_FLOOD | BR_MCAST_FLOOD,
273 struct dsa_switch_tree *dst = dp->ds->dst;
274 struct dsa_port *cpu_dp = dp->cpu_dp;
275 struct dsa_port *other_dp;
278 list_for_each_entry(other_dp, &dst->ports, list) {
279 if (!dsa_port_is_user(other_dp))
282 if (other_dp->cpu_dp != cpu_dp)
285 if (other_dp->slave->flags & IFF_ALLMULTI)
286 flags.val |= BR_MCAST_FLOOD;
287 if (other_dp->slave->flags & IFF_PROMISC)
288 flags.val |= BR_FLOOD;
291 err = dsa_port_pre_bridge_flags(dp, flags, NULL);
295 dsa_port_bridge_flags(cpu_dp, flags, NULL);
298 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
300 struct net_device *master = dsa_slave_to_master(dev);
301 struct dsa_port *dp = dsa_slave_to_port(dev);
302 struct dsa_switch *ds = dp->ds;
304 if (change & IFF_ALLMULTI)
305 dev_set_allmulti(master,
306 dev->flags & IFF_ALLMULTI ? 1 : -1);
307 if (change & IFF_PROMISC)
308 dev_set_promiscuity(master,
309 dev->flags & IFF_PROMISC ? 1 : -1);
311 if (dsa_switch_supports_uc_filtering(ds) &&
312 dsa_switch_supports_mc_filtering(ds))
313 dsa_port_manage_cpu_flood(dp);
316 static void dsa_slave_set_rx_mode(struct net_device *dev)
318 __dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc);
319 __dev_uc_sync(dev, dsa_slave_sync_uc, dsa_slave_unsync_uc);
322 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
324 struct net_device *master = dsa_slave_to_master(dev);
325 struct dsa_port *dp = dsa_slave_to_port(dev);
326 struct dsa_switch *ds = dp->ds;
327 struct sockaddr *addr = a;
330 if (!is_valid_ether_addr(addr->sa_data))
331 return -EADDRNOTAVAIL;
333 /* If the port is down, the address isn't synced yet to hardware or
334 * to the DSA master, so there is nothing to change.
336 if (!(dev->flags & IFF_UP))
337 goto out_change_dev_addr;
339 if (dsa_switch_supports_uc_filtering(ds)) {
340 err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0);
345 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
346 err = dev_uc_add(master, addr->sa_data);
351 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
352 dev_uc_del(master, dev->dev_addr);
354 if (dsa_switch_supports_uc_filtering(ds))
355 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
358 eth_hw_addr_set(dev, addr->sa_data);
363 if (dsa_switch_supports_uc_filtering(ds))
364 dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0);
369 struct dsa_slave_dump_ctx {
370 struct net_device *dev;
372 struct netlink_callback *cb;
377 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
378 bool is_static, void *data)
380 struct dsa_slave_dump_ctx *dump = data;
381 u32 portid = NETLINK_CB(dump->cb->skb).portid;
382 u32 seq = dump->cb->nlh->nlmsg_seq;
383 struct nlmsghdr *nlh;
386 if (dump->idx < dump->cb->args[2])
389 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
390 sizeof(*ndm), NLM_F_MULTI);
394 ndm = nlmsg_data(nlh);
395 ndm->ndm_family = AF_BRIDGE;
398 ndm->ndm_flags = NTF_SELF;
400 ndm->ndm_ifindex = dump->dev->ifindex;
401 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
403 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
404 goto nla_put_failure;
406 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
407 goto nla_put_failure;
409 nlmsg_end(dump->skb, nlh);
416 nlmsg_cancel(dump->skb, nlh);
421 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
422 struct net_device *dev, struct net_device *filter_dev,
425 struct dsa_port *dp = dsa_slave_to_port(dev);
426 struct dsa_slave_dump_ctx dump = {
434 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
440 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
442 struct dsa_slave_priv *p = netdev_priv(dev);
443 struct dsa_switch *ds = p->dp->ds;
444 int port = p->dp->index;
446 /* Pass through to switch driver if it supports timestamping */
449 if (ds->ops->port_hwtstamp_get)
450 return ds->ops->port_hwtstamp_get(ds, port, ifr);
453 if (ds->ops->port_hwtstamp_set)
454 return ds->ops->port_hwtstamp_set(ds, port, ifr);
458 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
461 static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
462 const struct switchdev_attr *attr,
463 struct netlink_ext_ack *extack)
465 struct dsa_port *dp = dsa_slave_to_port(dev);
468 if (ctx && ctx != dp)
472 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
473 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
476 ret = dsa_port_set_state(dp, attr->u.stp_state, true);
478 case SWITCHDEV_ATTR_ID_PORT_MST_STATE:
479 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
482 ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack);
484 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
485 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
488 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
491 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
492 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
495 ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
497 case SWITCHDEV_ATTR_ID_BRIDGE_MST:
498 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
501 ret = dsa_port_mst_enable(dp, attr->u.mst, extack);
503 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
504 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
507 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
510 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
511 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
514 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
516 case SWITCHDEV_ATTR_ID_VLAN_MSTI:
517 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
520 ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti);
530 /* Must be called under rcu_read_lock() */
532 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
533 const struct switchdev_obj_port_vlan *vlan)
535 struct net_device *upper_dev;
536 struct list_head *iter;
538 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
541 if (!is_vlan_dev(upper_dev))
544 vid = vlan_dev_vlan_id(upper_dev);
545 if (vid == vlan->vid)
552 static int dsa_slave_vlan_add(struct net_device *dev,
553 const struct switchdev_obj *obj,
554 struct netlink_ext_ack *extack)
556 struct dsa_port *dp = dsa_slave_to_port(dev);
557 struct switchdev_obj_port_vlan *vlan;
560 if (dsa_port_skip_vlan_configuration(dp)) {
561 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
565 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
567 /* Deny adding a bridge VLAN when there is already an 802.1Q upper with
570 if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
572 err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan);
575 NL_SET_ERR_MSG_MOD(extack,
576 "Port already has a VLAN upper with this VID");
581 return dsa_port_vlan_add(dp, vlan, extack);
584 /* Offload a VLAN installed on the bridge or on a foreign interface by
585 * installing it as a VLAN towards the CPU port.
587 static int dsa_slave_host_vlan_add(struct net_device *dev,
588 const struct switchdev_obj *obj,
589 struct netlink_ext_ack *extack)
591 struct dsa_port *dp = dsa_slave_to_port(dev);
592 struct switchdev_obj_port_vlan vlan;
594 /* Do nothing if this is a software bridge */
598 if (dsa_port_skip_vlan_configuration(dp)) {
599 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
603 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
605 /* Even though drivers often handle CPU membership in special ways,
606 * it doesn't make sense to program a PVID, so clear this flag.
608 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
610 return dsa_port_host_vlan_add(dp, &vlan, extack);
613 static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
614 const struct switchdev_obj *obj,
615 struct netlink_ext_ack *extack)
617 struct dsa_port *dp = dsa_slave_to_port(dev);
620 if (ctx && ctx != dp)
624 case SWITCHDEV_OBJ_ID_PORT_MDB:
625 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
628 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
630 case SWITCHDEV_OBJ_ID_HOST_MDB:
631 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
634 err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
636 case SWITCHDEV_OBJ_ID_PORT_VLAN:
637 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
638 err = dsa_slave_vlan_add(dev, obj, extack);
640 err = dsa_slave_host_vlan_add(dev, obj, extack);
642 case SWITCHDEV_OBJ_ID_MRP:
643 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
646 err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
648 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
649 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
652 err = dsa_port_mrp_add_ring_role(dp,
653 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
663 static int dsa_slave_vlan_del(struct net_device *dev,
664 const struct switchdev_obj *obj)
666 struct dsa_port *dp = dsa_slave_to_port(dev);
667 struct switchdev_obj_port_vlan *vlan;
669 if (dsa_port_skip_vlan_configuration(dp))
672 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
674 return dsa_port_vlan_del(dp, vlan);
677 static int dsa_slave_host_vlan_del(struct net_device *dev,
678 const struct switchdev_obj *obj)
680 struct dsa_port *dp = dsa_slave_to_port(dev);
681 struct switchdev_obj_port_vlan *vlan;
683 /* Do nothing if this is a software bridge */
687 if (dsa_port_skip_vlan_configuration(dp))
690 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
692 return dsa_port_host_vlan_del(dp, vlan);
695 static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
696 const struct switchdev_obj *obj)
698 struct dsa_port *dp = dsa_slave_to_port(dev);
701 if (ctx && ctx != dp)
705 case SWITCHDEV_OBJ_ID_PORT_MDB:
706 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
709 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
711 case SWITCHDEV_OBJ_ID_HOST_MDB:
712 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
715 err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
717 case SWITCHDEV_OBJ_ID_PORT_VLAN:
718 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
719 err = dsa_slave_vlan_del(dev, obj);
721 err = dsa_slave_host_vlan_del(dev, obj);
723 case SWITCHDEV_OBJ_ID_MRP:
724 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
727 err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
729 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
730 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
733 err = dsa_port_mrp_del_ring_role(dp,
734 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
744 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
747 #ifdef CONFIG_NET_POLL_CONTROLLER
748 struct dsa_slave_priv *p = netdev_priv(dev);
750 return netpoll_send_skb(p->netpoll, skb);
757 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
760 struct dsa_switch *ds = p->dp->ds;
762 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
765 if (!ds->ops->port_txtstamp)
768 ds->ops->port_txtstamp(ds, p->dp->index, skb);
771 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
773 /* SKB for netpoll still need to be mangled with the protocol-specific
774 * tag to be successfully transmitted
776 if (unlikely(netpoll_tx_running(dev)))
777 return dsa_slave_netpoll_send_skb(dev, skb);
779 /* Queue the SKB for transmission on the parent interface, but
780 * do not modify its EtherType
782 skb->dev = dsa_slave_to_master(dev);
787 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
789 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
791 int needed_headroom = dev->needed_headroom;
792 int needed_tailroom = dev->needed_tailroom;
794 /* For tail taggers, we need to pad short frames ourselves, to ensure
795 * that the tail tag does not fail at its role of being at the end of
796 * the packet, once the master interface pads the frame. Account for
797 * that pad length here, and pad later.
799 if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
800 needed_tailroom += ETH_ZLEN - skb->len;
801 /* skb_headroom() returns unsigned int... */
802 needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
803 needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
805 if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
806 /* No reallocation needed, yay! */
809 return pskb_expand_head(skb, needed_headroom, needed_tailroom,
813 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
815 struct dsa_slave_priv *p = netdev_priv(dev);
816 struct sk_buff *nskb;
818 dev_sw_netstats_tx_add(dev, 1, skb->len);
820 memset(skb->cb, 0, sizeof(skb->cb));
822 /* Handle tx timestamp if any */
823 dsa_skb_tx_timestamp(p, skb);
825 if (dsa_realloc_skb(skb, dev)) {
826 dev_kfree_skb_any(skb);
830 /* needed_tailroom should still be 'warm' in the cache line from
831 * dsa_realloc_skb(), which has also ensured that padding is safe.
833 if (dev->needed_tailroom)
836 /* Transmit function may have to reallocate the original SKB,
837 * in which case it must have freed it. Only free it here on error.
839 nskb = p->xmit(skb, dev);
845 return dsa_enqueue_skb(nskb, dev);
848 /* ethtool operations *******************************************************/
850 static void dsa_slave_get_drvinfo(struct net_device *dev,
851 struct ethtool_drvinfo *drvinfo)
853 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
854 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
855 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
858 static int dsa_slave_get_regs_len(struct net_device *dev)
860 struct dsa_port *dp = dsa_slave_to_port(dev);
861 struct dsa_switch *ds = dp->ds;
863 if (ds->ops->get_regs_len)
864 return ds->ops->get_regs_len(ds, dp->index);
870 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
872 struct dsa_port *dp = dsa_slave_to_port(dev);
873 struct dsa_switch *ds = dp->ds;
875 if (ds->ops->get_regs)
876 ds->ops->get_regs(ds, dp->index, regs, _p);
879 static int dsa_slave_nway_reset(struct net_device *dev)
881 struct dsa_port *dp = dsa_slave_to_port(dev);
883 return phylink_ethtool_nway_reset(dp->pl);
886 static int dsa_slave_get_eeprom_len(struct net_device *dev)
888 struct dsa_port *dp = dsa_slave_to_port(dev);
889 struct dsa_switch *ds = dp->ds;
891 if (ds->cd && ds->cd->eeprom_len)
892 return ds->cd->eeprom_len;
894 if (ds->ops->get_eeprom_len)
895 return ds->ops->get_eeprom_len(ds);
900 static int dsa_slave_get_eeprom(struct net_device *dev,
901 struct ethtool_eeprom *eeprom, u8 *data)
903 struct dsa_port *dp = dsa_slave_to_port(dev);
904 struct dsa_switch *ds = dp->ds;
906 if (ds->ops->get_eeprom)
907 return ds->ops->get_eeprom(ds, eeprom, data);
912 static int dsa_slave_set_eeprom(struct net_device *dev,
913 struct ethtool_eeprom *eeprom, u8 *data)
915 struct dsa_port *dp = dsa_slave_to_port(dev);
916 struct dsa_switch *ds = dp->ds;
918 if (ds->ops->set_eeprom)
919 return ds->ops->set_eeprom(ds, eeprom, data);
924 static void dsa_slave_get_strings(struct net_device *dev,
925 uint32_t stringset, uint8_t *data)
927 struct dsa_port *dp = dsa_slave_to_port(dev);
928 struct dsa_switch *ds = dp->ds;
930 if (stringset == ETH_SS_STATS) {
931 int len = ETH_GSTRING_LEN;
933 strncpy(data, "tx_packets", len);
934 strncpy(data + len, "tx_bytes", len);
935 strncpy(data + 2 * len, "rx_packets", len);
936 strncpy(data + 3 * len, "rx_bytes", len);
937 if (ds->ops->get_strings)
938 ds->ops->get_strings(ds, dp->index, stringset,
940 } else if (stringset == ETH_SS_TEST) {
941 net_selftest_get_strings(data);
946 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
947 struct ethtool_stats *stats,
950 struct dsa_port *dp = dsa_slave_to_port(dev);
951 struct dsa_switch *ds = dp->ds;
952 struct pcpu_sw_netstats *s;
956 for_each_possible_cpu(i) {
957 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
959 s = per_cpu_ptr(dev->tstats, i);
961 start = u64_stats_fetch_begin_irq(&s->syncp);
962 tx_packets = s->tx_packets;
963 tx_bytes = s->tx_bytes;
964 rx_packets = s->rx_packets;
965 rx_bytes = s->rx_bytes;
966 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
967 data[0] += tx_packets;
969 data[2] += rx_packets;
972 if (ds->ops->get_ethtool_stats)
973 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
976 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
978 struct dsa_port *dp = dsa_slave_to_port(dev);
979 struct dsa_switch *ds = dp->ds;
981 if (sset == ETH_SS_STATS) {
984 if (ds->ops->get_sset_count) {
985 count = ds->ops->get_sset_count(ds, dp->index, sset);
991 } else if (sset == ETH_SS_TEST) {
992 return net_selftest_get_count();
998 static void dsa_slave_get_eth_phy_stats(struct net_device *dev,
999 struct ethtool_eth_phy_stats *phy_stats)
1001 struct dsa_port *dp = dsa_slave_to_port(dev);
1002 struct dsa_switch *ds = dp->ds;
1004 if (ds->ops->get_eth_phy_stats)
1005 ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
1008 static void dsa_slave_get_eth_mac_stats(struct net_device *dev,
1009 struct ethtool_eth_mac_stats *mac_stats)
1011 struct dsa_port *dp = dsa_slave_to_port(dev);
1012 struct dsa_switch *ds = dp->ds;
1014 if (ds->ops->get_eth_mac_stats)
1015 ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
1019 dsa_slave_get_eth_ctrl_stats(struct net_device *dev,
1020 struct ethtool_eth_ctrl_stats *ctrl_stats)
1022 struct dsa_port *dp = dsa_slave_to_port(dev);
1023 struct dsa_switch *ds = dp->ds;
1025 if (ds->ops->get_eth_ctrl_stats)
1026 ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1029 static void dsa_slave_net_selftest(struct net_device *ndev,
1030 struct ethtool_test *etest, u64 *buf)
1032 struct dsa_port *dp = dsa_slave_to_port(ndev);
1033 struct dsa_switch *ds = dp->ds;
1035 if (ds->ops->self_test) {
1036 ds->ops->self_test(ds, dp->index, etest, buf);
1040 net_selftest(ndev, etest, buf);
1043 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1045 struct dsa_port *dp = dsa_slave_to_port(dev);
1046 struct dsa_switch *ds = dp->ds;
1048 phylink_ethtool_get_wol(dp->pl, w);
1050 if (ds->ops->get_wol)
1051 ds->ops->get_wol(ds, dp->index, w);
1054 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1056 struct dsa_port *dp = dsa_slave_to_port(dev);
1057 struct dsa_switch *ds = dp->ds;
1058 int ret = -EOPNOTSUPP;
1060 phylink_ethtool_set_wol(dp->pl, w);
1062 if (ds->ops->set_wol)
1063 ret = ds->ops->set_wol(ds, dp->index, w);
1068 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
1070 struct dsa_port *dp = dsa_slave_to_port(dev);
1071 struct dsa_switch *ds = dp->ds;
1074 /* Port's PHY and MAC both need to be EEE capable */
1075 if (!dev->phydev || !dp->pl)
1078 if (!ds->ops->set_mac_eee)
1081 ret = ds->ops->set_mac_eee(ds, dp->index, e);
1085 return phylink_ethtool_set_eee(dp->pl, e);
1088 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
1090 struct dsa_port *dp = dsa_slave_to_port(dev);
1091 struct dsa_switch *ds = dp->ds;
1094 /* Port's PHY and MAC both need to be EEE capable */
1095 if (!dev->phydev || !dp->pl)
1098 if (!ds->ops->get_mac_eee)
1101 ret = ds->ops->get_mac_eee(ds, dp->index, e);
1105 return phylink_ethtool_get_eee(dp->pl, e);
1108 static int dsa_slave_get_link_ksettings(struct net_device *dev,
1109 struct ethtool_link_ksettings *cmd)
1111 struct dsa_port *dp = dsa_slave_to_port(dev);
1113 return phylink_ethtool_ksettings_get(dp->pl, cmd);
1116 static int dsa_slave_set_link_ksettings(struct net_device *dev,
1117 const struct ethtool_link_ksettings *cmd)
1119 struct dsa_port *dp = dsa_slave_to_port(dev);
1121 return phylink_ethtool_ksettings_set(dp->pl, cmd);
1124 static void dsa_slave_get_pauseparam(struct net_device *dev,
1125 struct ethtool_pauseparam *pause)
1127 struct dsa_port *dp = dsa_slave_to_port(dev);
1129 phylink_ethtool_get_pauseparam(dp->pl, pause);
1132 static int dsa_slave_set_pauseparam(struct net_device *dev,
1133 struct ethtool_pauseparam *pause)
1135 struct dsa_port *dp = dsa_slave_to_port(dev);
1137 return phylink_ethtool_set_pauseparam(dp->pl, pause);
1140 #ifdef CONFIG_NET_POLL_CONTROLLER
1141 static int dsa_slave_netpoll_setup(struct net_device *dev,
1142 struct netpoll_info *ni)
1144 struct net_device *master = dsa_slave_to_master(dev);
1145 struct dsa_slave_priv *p = netdev_priv(dev);
1146 struct netpoll *netpoll;
1149 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1153 err = __netpoll_setup(netpoll, master);
1159 p->netpoll = netpoll;
1164 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1166 struct dsa_slave_priv *p = netdev_priv(dev);
1167 struct netpoll *netpoll = p->netpoll;
1174 __netpoll_free(netpoll);
1177 static void dsa_slave_poll_controller(struct net_device *dev)
1182 static struct dsa_mall_tc_entry *
1183 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1185 struct dsa_slave_priv *p = netdev_priv(dev);
1186 struct dsa_mall_tc_entry *mall_tc_entry;
1188 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1189 if (mall_tc_entry->cookie == cookie)
1190 return mall_tc_entry;
1196 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
1197 struct tc_cls_matchall_offload *cls,
1200 struct netlink_ext_ack *extack = cls->common.extack;
1201 struct dsa_port *dp = dsa_slave_to_port(dev);
1202 struct dsa_slave_priv *p = netdev_priv(dev);
1203 struct dsa_mall_mirror_tc_entry *mirror;
1204 struct dsa_mall_tc_entry *mall_tc_entry;
1205 struct dsa_switch *ds = dp->ds;
1206 struct flow_action_entry *act;
1207 struct dsa_port *to_dp;
1210 if (!ds->ops->port_mirror_add)
1213 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1214 cls->common.extack))
1217 act = &cls->rule->action.entries[0];
1222 if (!dsa_slave_dev_check(act->dev))
1225 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1229 mall_tc_entry->cookie = cls->cookie;
1230 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1231 mirror = &mall_tc_entry->mirror;
1233 to_dp = dsa_slave_to_port(act->dev);
1235 mirror->to_local_port = to_dp->index;
1236 mirror->ingress = ingress;
1238 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1240 kfree(mall_tc_entry);
1244 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1250 dsa_slave_add_cls_matchall_police(struct net_device *dev,
1251 struct tc_cls_matchall_offload *cls,
1254 struct netlink_ext_ack *extack = cls->common.extack;
1255 struct dsa_port *dp = dsa_slave_to_port(dev);
1256 struct dsa_slave_priv *p = netdev_priv(dev);
1257 struct dsa_mall_policer_tc_entry *policer;
1258 struct dsa_mall_tc_entry *mall_tc_entry;
1259 struct dsa_switch *ds = dp->ds;
1260 struct flow_action_entry *act;
1263 if (!ds->ops->port_policer_add) {
1264 NL_SET_ERR_MSG_MOD(extack,
1265 "Policing offload not implemented");
1270 NL_SET_ERR_MSG_MOD(extack,
1271 "Only supported on ingress qdisc");
1275 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1276 cls->common.extack))
1279 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1280 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1281 NL_SET_ERR_MSG_MOD(extack,
1282 "Only one port policer allowed");
1287 act = &cls->rule->action.entries[0];
1289 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1293 mall_tc_entry->cookie = cls->cookie;
1294 mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1295 policer = &mall_tc_entry->policer;
1296 policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1297 policer->burst = act->police.burst;
1299 err = ds->ops->port_policer_add(ds, dp->index, policer);
1301 kfree(mall_tc_entry);
1305 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1310 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1311 struct tc_cls_matchall_offload *cls,
1314 int err = -EOPNOTSUPP;
1316 if (cls->common.protocol == htons(ETH_P_ALL) &&
1317 flow_offload_has_one_action(&cls->rule->action) &&
1318 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1319 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1320 else if (flow_offload_has_one_action(&cls->rule->action) &&
1321 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1322 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1327 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1328 struct tc_cls_matchall_offload *cls)
1330 struct dsa_port *dp = dsa_slave_to_port(dev);
1331 struct dsa_mall_tc_entry *mall_tc_entry;
1332 struct dsa_switch *ds = dp->ds;
1334 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1338 list_del(&mall_tc_entry->list);
1340 switch (mall_tc_entry->type) {
1341 case DSA_PORT_MALL_MIRROR:
1342 if (ds->ops->port_mirror_del)
1343 ds->ops->port_mirror_del(ds, dp->index,
1344 &mall_tc_entry->mirror);
1346 case DSA_PORT_MALL_POLICER:
1347 if (ds->ops->port_policer_del)
1348 ds->ops->port_policer_del(ds, dp->index);
1354 kfree(mall_tc_entry);
1357 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1358 struct tc_cls_matchall_offload *cls,
1361 if (cls->common.chain_index)
1364 switch (cls->command) {
1365 case TC_CLSMATCHALL_REPLACE:
1366 return dsa_slave_add_cls_matchall(dev, cls, ingress);
1367 case TC_CLSMATCHALL_DESTROY:
1368 dsa_slave_del_cls_matchall(dev, cls);
1375 static int dsa_slave_add_cls_flower(struct net_device *dev,
1376 struct flow_cls_offload *cls,
1379 struct dsa_port *dp = dsa_slave_to_port(dev);
1380 struct dsa_switch *ds = dp->ds;
1381 int port = dp->index;
1383 if (!ds->ops->cls_flower_add)
1386 return ds->ops->cls_flower_add(ds, port, cls, ingress);
1389 static int dsa_slave_del_cls_flower(struct net_device *dev,
1390 struct flow_cls_offload *cls,
1393 struct dsa_port *dp = dsa_slave_to_port(dev);
1394 struct dsa_switch *ds = dp->ds;
1395 int port = dp->index;
1397 if (!ds->ops->cls_flower_del)
1400 return ds->ops->cls_flower_del(ds, port, cls, ingress);
1403 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1404 struct flow_cls_offload *cls,
1407 struct dsa_port *dp = dsa_slave_to_port(dev);
1408 struct dsa_switch *ds = dp->ds;
1409 int port = dp->index;
1411 if (!ds->ops->cls_flower_stats)
1414 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1417 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1418 struct flow_cls_offload *cls,
1421 switch (cls->command) {
1422 case FLOW_CLS_REPLACE:
1423 return dsa_slave_add_cls_flower(dev, cls, ingress);
1424 case FLOW_CLS_DESTROY:
1425 return dsa_slave_del_cls_flower(dev, cls, ingress);
1426 case FLOW_CLS_STATS:
1427 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1433 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1434 void *cb_priv, bool ingress)
1436 struct net_device *dev = cb_priv;
1438 if (!tc_can_offload(dev))
1442 case TC_SETUP_CLSMATCHALL:
1443 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1444 case TC_SETUP_CLSFLOWER:
1445 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1451 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1452 void *type_data, void *cb_priv)
1454 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1457 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1458 void *type_data, void *cb_priv)
1460 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1463 static LIST_HEAD(dsa_slave_block_cb_list);
1465 static int dsa_slave_setup_tc_block(struct net_device *dev,
1466 struct flow_block_offload *f)
1468 struct flow_block_cb *block_cb;
1469 flow_setup_cb_t *cb;
1471 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1472 cb = dsa_slave_setup_tc_block_cb_ig;
1473 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1474 cb = dsa_slave_setup_tc_block_cb_eg;
1478 f->driver_block_list = &dsa_slave_block_cb_list;
1480 switch (f->command) {
1481 case FLOW_BLOCK_BIND:
1482 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1485 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1486 if (IS_ERR(block_cb))
1487 return PTR_ERR(block_cb);
1489 flow_block_cb_add(block_cb, f);
1490 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1492 case FLOW_BLOCK_UNBIND:
1493 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1497 flow_block_cb_remove(block_cb, f);
1498 list_del(&block_cb->driver_list);
1505 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
1508 struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1509 struct net_device *master = cpu_dp->master;
1511 if (!master->netdev_ops->ndo_setup_tc)
1514 return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
1517 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1520 struct dsa_port *dp = dsa_slave_to_port(dev);
1521 struct dsa_switch *ds = dp->ds;
1524 case TC_SETUP_BLOCK:
1525 return dsa_slave_setup_tc_block(dev, type_data);
1527 return dsa_slave_setup_ft_block(ds, dp->index, type_data);
1532 if (!ds->ops->port_setup_tc)
1535 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1538 static int dsa_slave_get_rxnfc(struct net_device *dev,
1539 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1541 struct dsa_port *dp = dsa_slave_to_port(dev);
1542 struct dsa_switch *ds = dp->ds;
1544 if (!ds->ops->get_rxnfc)
1547 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1550 static int dsa_slave_set_rxnfc(struct net_device *dev,
1551 struct ethtool_rxnfc *nfc)
1553 struct dsa_port *dp = dsa_slave_to_port(dev);
1554 struct dsa_switch *ds = dp->ds;
1556 if (!ds->ops->set_rxnfc)
1559 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1562 static int dsa_slave_get_ts_info(struct net_device *dev,
1563 struct ethtool_ts_info *ts)
1565 struct dsa_slave_priv *p = netdev_priv(dev);
1566 struct dsa_switch *ds = p->dp->ds;
1568 if (!ds->ops->get_ts_info)
1571 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1574 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1577 struct dsa_port *dp = dsa_slave_to_port(dev);
1578 struct switchdev_obj_port_vlan vlan = {
1579 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1581 /* This API only allows programming tagged, non-PVID VIDs */
1584 struct netlink_ext_ack extack = {0};
1588 ret = dsa_port_vlan_add(dp, &vlan, &extack);
1591 netdev_err(dev, "%s\n", extack._msg);
1595 /* And CPU port... */
1596 ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
1599 netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1607 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1610 struct dsa_port *dp = dsa_slave_to_port(dev);
1611 struct switchdev_obj_port_vlan vlan = {
1613 /* This API only allows programming tagged, non-PVID VIDs */
1618 err = dsa_port_vlan_del(dp, &vlan);
1622 return dsa_port_host_vlan_del(dp, &vlan);
1625 static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
1627 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1629 return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
1632 static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
1634 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1636 return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
1639 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1640 * filtering is enabled. The baseline is that only ports that offload a
1641 * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1642 * but there are exceptions for quirky hardware.
1644 * If ds->vlan_filtering_is_global = true, then standalone ports which share
1645 * the same switch with other ports that offload a VLAN-aware bridge are also
1646 * inevitably VLAN-aware.
1648 * To summarize, a DSA switch port offloads:
1650 * - If standalone (this includes software bridge, software LAG):
1651 * - if ds->needs_standalone_vlan_filtering = true, OR if
1652 * (ds->vlan_filtering_is_global = true AND there are bridges spanning
1653 * this switch chip which have vlan_filtering=1)
1654 * - the 8021q upper VLANs
1655 * - else (standalone VLAN filtering is not needed, VLAN filtering is not
1656 * global, or it is, but no port is under a VLAN-aware bridge):
1657 * - no VLAN (any 8021q upper is a software VLAN)
1659 * - If under a vlan_filtering=0 bridge which it offload:
1660 * - if ds->configure_vlan_while_not_filtering = true (default):
1661 * - the bridge VLANs. These VLANs are committed to hardware but inactive.
1662 * - else (deprecated):
1663 * - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1664 * enabled, so this behavior is broken and discouraged.
1666 * - If under a vlan_filtering=1 bridge which it offload:
1667 * - the bridge VLANs
1668 * - the 8021q upper VLANs
1670 int dsa_slave_manage_vlan_filtering(struct net_device *slave,
1671 bool vlan_filtering)
1675 if (vlan_filtering) {
1676 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1678 err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
1680 vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1681 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1685 err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1689 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1695 struct dsa_hw_port {
1696 struct list_head list;
1697 struct net_device *dev;
1701 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1703 const struct dsa_hw_port *p;
1706 list_for_each_entry(p, hw_port_list, list) {
1707 if (p->dev->mtu == mtu)
1710 err = dev_set_mtu(p->dev, mtu);
1718 list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1719 if (p->dev->mtu == p->old_mtu)
1722 if (dev_set_mtu(p->dev, p->old_mtu))
1723 netdev_err(p->dev, "Failed to restore MTU\n");
1729 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1731 struct dsa_hw_port *p, *n;
1733 list_for_each_entry_safe(p, n, hw_port_list, list)
1737 /* Make the hardware datapath to/from @dev limited to a common MTU */
1738 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1740 struct list_head hw_port_list;
1741 struct dsa_switch_tree *dst;
1742 int min_mtu = ETH_MAX_MTU;
1743 struct dsa_port *other_dp;
1746 if (!dp->ds->mtu_enforcement_ingress)
1752 INIT_LIST_HEAD(&hw_port_list);
1754 /* Populate the list of ports that are part of the same bridge
1755 * as the newly added/modified port
1757 list_for_each_entry(dst, &dsa_tree_list, list) {
1758 list_for_each_entry(other_dp, &dst->ports, list) {
1759 struct dsa_hw_port *hw_port;
1760 struct net_device *slave;
1762 if (other_dp->type != DSA_PORT_TYPE_USER)
1765 if (!dsa_port_bridge_same(dp, other_dp))
1768 if (!other_dp->ds->mtu_enforcement_ingress)
1771 slave = other_dp->slave;
1773 if (min_mtu > slave->mtu)
1774 min_mtu = slave->mtu;
1776 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1780 hw_port->dev = slave;
1781 hw_port->old_mtu = slave->mtu;
1783 list_add(&hw_port->list, &hw_port_list);
1787 /* Attempt to configure the entire hardware bridge to the newly added
1788 * interface's MTU first, regardless of whether the intention of the
1789 * user was to raise or lower it.
1791 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1795 /* Clearly that didn't work out so well, so just set the minimum MTU on
1796 * all hardware bridge ports now. If this fails too, then all ports will
1797 * still have their old MTU rolled back anyway.
1799 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1802 dsa_hw_port_list_free(&hw_port_list);
1805 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1807 struct net_device *master = dsa_slave_to_master(dev);
1808 struct dsa_port *dp = dsa_slave_to_port(dev);
1809 struct dsa_slave_priv *p = netdev_priv(dev);
1810 struct dsa_switch *ds = p->dp->ds;
1811 struct dsa_port *dp_iter;
1812 struct dsa_port *cpu_dp;
1813 int port = p->dp->index;
1814 int largest_mtu = 0;
1821 if (!ds->ops->port_change_mtu)
1824 list_for_each_entry(dp_iter, &ds->dst->ports, list) {
1827 if (!dsa_port_is_user(dp_iter))
1830 /* During probe, this function will be called for each slave
1831 * device, while not all of them have been allocated. That's
1832 * ok, it doesn't change what the maximum is, so ignore it.
1834 if (!dp_iter->slave)
1837 /* Pretend that we already applied the setting, which we
1838 * actually haven't (still haven't done all integrity checks)
1841 slave_mtu = new_mtu;
1843 slave_mtu = dp_iter->slave->mtu;
1845 if (largest_mtu < slave_mtu)
1846 largest_mtu = slave_mtu;
1849 cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1851 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1852 old_master_mtu = master->mtu;
1853 new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1854 if (new_master_mtu > mtu_limit)
1857 /* If the master MTU isn't over limit, there's no need to check the CPU
1858 * MTU, since that surely isn't either.
1860 cpu_mtu = largest_mtu;
1862 /* Start applying stuff */
1863 if (new_master_mtu != old_master_mtu) {
1864 err = dev_set_mtu(master, new_master_mtu);
1866 goto out_master_failed;
1868 /* We only need to propagate the MTU of the CPU port to
1869 * upstream switches, so create a non-targeted notifier which
1870 * updates all switches.
1872 err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false);
1874 goto out_cpu_failed;
1877 err = dsa_port_mtu_change(dp, new_mtu, true);
1879 goto out_port_failed;
1883 dsa_bridge_mtu_normalization(dp);
1888 if (new_master_mtu != old_master_mtu)
1889 dsa_port_mtu_change(cpu_dp, old_master_mtu -
1890 dsa_tag_protocol_overhead(cpu_dp->tag_ops),
1893 if (new_master_mtu != old_master_mtu)
1894 dev_set_mtu(master, old_master_mtu);
1899 static int __maybe_unused
1900 dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
1902 struct dsa_port *dp = dsa_slave_to_port(dev);
1903 struct dsa_switch *ds = dp->ds;
1904 unsigned long mask, new_prio;
1905 int err, port = dp->index;
1907 if (!ds->ops->port_set_default_prio)
1910 err = dcb_ieee_setapp(dev, app);
1914 mask = dcb_ieee_getapp_mask(dev, app);
1915 new_prio = __fls(mask);
1917 err = ds->ops->port_set_default_prio(ds, port, new_prio);
1919 dcb_ieee_delapp(dev, app);
1926 static int __maybe_unused
1927 dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
1929 struct dsa_port *dp = dsa_slave_to_port(dev);
1930 struct dsa_switch *ds = dp->ds;
1931 unsigned long mask, new_prio;
1932 int err, port = dp->index;
1933 u8 dscp = app->protocol;
1935 if (!ds->ops->port_add_dscp_prio)
1939 netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
1944 err = dcb_ieee_setapp(dev, app);
1948 mask = dcb_ieee_getapp_mask(dev, app);
1949 new_prio = __fls(mask);
1951 err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
1953 dcb_ieee_delapp(dev, app);
1960 static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev,
1961 struct dcb_app *app)
1963 switch (app->selector) {
1964 case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
1965 switch (app->protocol) {
1967 return dsa_slave_dcbnl_set_default_prio(dev, app);
1972 case IEEE_8021QAZ_APP_SEL_DSCP:
1973 return dsa_slave_dcbnl_add_dscp_prio(dev, app);
1979 static int __maybe_unused
1980 dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
1982 struct dsa_port *dp = dsa_slave_to_port(dev);
1983 struct dsa_switch *ds = dp->ds;
1984 unsigned long mask, new_prio;
1985 int err, port = dp->index;
1987 if (!ds->ops->port_set_default_prio)
1990 err = dcb_ieee_delapp(dev, app);
1994 mask = dcb_ieee_getapp_mask(dev, app);
1995 new_prio = mask ? __fls(mask) : 0;
1997 err = ds->ops->port_set_default_prio(ds, port, new_prio);
1999 dcb_ieee_setapp(dev, app);
2006 static int __maybe_unused
2007 dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
2009 struct dsa_port *dp = dsa_slave_to_port(dev);
2010 struct dsa_switch *ds = dp->ds;
2011 int err, port = dp->index;
2012 u8 dscp = app->protocol;
2014 if (!ds->ops->port_del_dscp_prio)
2017 err = dcb_ieee_delapp(dev, app);
2021 err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
2023 dcb_ieee_setapp(dev, app);
2030 static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev,
2031 struct dcb_app *app)
2033 switch (app->selector) {
2034 case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2035 switch (app->protocol) {
2037 return dsa_slave_dcbnl_del_default_prio(dev, app);
2042 case IEEE_8021QAZ_APP_SEL_DSCP:
2043 return dsa_slave_dcbnl_del_dscp_prio(dev, app);
2049 /* Pre-populate the DCB application priority table with the priorities
2050 * configured during switch setup, which we read from hardware here.
2052 static int dsa_slave_dcbnl_init(struct net_device *dev)
2054 struct dsa_port *dp = dsa_slave_to_port(dev);
2055 struct dsa_switch *ds = dp->ds;
2056 int port = dp->index;
2059 if (ds->ops->port_get_default_prio) {
2060 int prio = ds->ops->port_get_default_prio(ds, port);
2061 struct dcb_app app = {
2062 .selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2070 err = dcb_ieee_setapp(dev, &app);
2075 if (ds->ops->port_get_dscp_prio) {
2078 for (protocol = 0; protocol < 64; protocol++) {
2079 struct dcb_app app = {
2080 .selector = IEEE_8021QAZ_APP_SEL_DSCP,
2081 .protocol = protocol,
2085 prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
2086 if (prio == -EOPNOTSUPP)
2091 app.priority = prio;
2093 err = dcb_ieee_setapp(dev, &app);
2102 static const struct ethtool_ops dsa_slave_ethtool_ops = {
2103 .get_drvinfo = dsa_slave_get_drvinfo,
2104 .get_regs_len = dsa_slave_get_regs_len,
2105 .get_regs = dsa_slave_get_regs,
2106 .nway_reset = dsa_slave_nway_reset,
2107 .get_link = ethtool_op_get_link,
2108 .get_eeprom_len = dsa_slave_get_eeprom_len,
2109 .get_eeprom = dsa_slave_get_eeprom,
2110 .set_eeprom = dsa_slave_set_eeprom,
2111 .get_strings = dsa_slave_get_strings,
2112 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
2113 .get_sset_count = dsa_slave_get_sset_count,
2114 .get_eth_phy_stats = dsa_slave_get_eth_phy_stats,
2115 .get_eth_mac_stats = dsa_slave_get_eth_mac_stats,
2116 .get_eth_ctrl_stats = dsa_slave_get_eth_ctrl_stats,
2117 .set_wol = dsa_slave_set_wol,
2118 .get_wol = dsa_slave_get_wol,
2119 .set_eee = dsa_slave_set_eee,
2120 .get_eee = dsa_slave_get_eee,
2121 .get_link_ksettings = dsa_slave_get_link_ksettings,
2122 .set_link_ksettings = dsa_slave_set_link_ksettings,
2123 .get_pauseparam = dsa_slave_get_pauseparam,
2124 .set_pauseparam = dsa_slave_set_pauseparam,
2125 .get_rxnfc = dsa_slave_get_rxnfc,
2126 .set_rxnfc = dsa_slave_set_rxnfc,
2127 .get_ts_info = dsa_slave_get_ts_info,
2128 .self_test = dsa_slave_net_selftest,
2131 static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = {
2132 .ieee_setapp = dsa_slave_dcbnl_ieee_setapp,
2133 .ieee_delapp = dsa_slave_dcbnl_ieee_delapp,
2136 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
2138 struct dsa_port *dp = dsa_slave_to_port(dev);
2140 return &dp->devlink_port;
2143 static void dsa_slave_get_stats64(struct net_device *dev,
2144 struct rtnl_link_stats64 *s)
2146 struct dsa_port *dp = dsa_slave_to_port(dev);
2147 struct dsa_switch *ds = dp->ds;
2149 if (ds->ops->get_stats64)
2150 ds->ops->get_stats64(ds, dp->index, s);
2152 dev_get_tstats64(dev, s);
2155 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
2156 struct net_device_path *path)
2158 struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
2159 struct dsa_port *cpu_dp = dp->cpu_dp;
2161 path->dev = ctx->dev;
2162 path->type = DEV_PATH_DSA;
2163 path->dsa.proto = cpu_dp->tag_ops->proto;
2164 path->dsa.port = dp->index;
2165 ctx->dev = cpu_dp->master;
2170 static const struct net_device_ops dsa_slave_netdev_ops = {
2171 .ndo_open = dsa_slave_open,
2172 .ndo_stop = dsa_slave_close,
2173 .ndo_start_xmit = dsa_slave_xmit,
2174 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
2175 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
2176 .ndo_set_mac_address = dsa_slave_set_mac_address,
2177 .ndo_fdb_dump = dsa_slave_fdb_dump,
2178 .ndo_eth_ioctl = dsa_slave_ioctl,
2179 .ndo_get_iflink = dsa_slave_get_iflink,
2180 #ifdef CONFIG_NET_POLL_CONTROLLER
2181 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
2182 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
2183 .ndo_poll_controller = dsa_slave_poll_controller,
2185 .ndo_setup_tc = dsa_slave_setup_tc,
2186 .ndo_get_stats64 = dsa_slave_get_stats64,
2187 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
2188 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
2189 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
2190 .ndo_change_mtu = dsa_slave_change_mtu,
2191 .ndo_fill_forward_path = dsa_slave_fill_forward_path,
2194 static struct device_type dsa_type = {
2198 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2200 const struct dsa_port *dp = dsa_to_port(ds, port);
2203 phylink_mac_change(dp->pl, up);
2205 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2207 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
2208 struct phylink_link_state *state)
2210 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
2211 struct dsa_switch *ds = dp->ds;
2213 /* No need to check that this operation is valid, the callback would
2214 * not be called if it was not.
2216 ds->ops->phylink_fixed_state(ds, dp->index, state);
2219 /* slave device setup *******************************************************/
2220 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
2223 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2224 struct dsa_switch *ds = dp->ds;
2226 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
2227 if (!slave_dev->phydev) {
2228 netdev_err(slave_dev, "no phy at %d\n", addr);
2232 slave_dev->phydev->dev_flags |= flags;
2234 return phylink_connect_phy(dp->pl, slave_dev->phydev);
2237 static int dsa_slave_phy_setup(struct net_device *slave_dev)
2239 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2240 struct device_node *port_dn = dp->dn;
2241 struct dsa_switch *ds = dp->ds;
2245 dp->pl_config.dev = &slave_dev->dev;
2246 dp->pl_config.type = PHYLINK_NETDEV;
2248 /* The get_fixed_state callback takes precedence over polling the
2249 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set
2250 * this if the switch provides such a callback.
2252 if (ds->ops->phylink_fixed_state) {
2253 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
2254 dp->pl_config.poll_fixed_state = true;
2257 ret = dsa_port_phylink_create(dp);
2261 if (ds->ops->get_phy_flags)
2262 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
2264 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
2265 if (ret == -ENODEV && ds->slave_mii_bus) {
2266 /* We could not connect to a designated PHY or SFP, so try to
2267 * use the switch internal MDIO bus instead
2269 ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
2272 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
2274 phylink_destroy(dp->pl);
2280 void dsa_slave_setup_tagger(struct net_device *slave)
2282 struct dsa_port *dp = dsa_slave_to_port(slave);
2283 struct dsa_slave_priv *p = netdev_priv(slave);
2284 const struct dsa_port *cpu_dp = dp->cpu_dp;
2285 struct net_device *master = cpu_dp->master;
2286 const struct dsa_switch *ds = dp->ds;
2288 slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
2289 slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
2290 /* Try to save one extra realloc later in the TX path (in the master)
2291 * by also inheriting the master's needed headroom and tailroom.
2292 * The 8021q driver also does this.
2294 slave->needed_headroom += master->needed_headroom;
2295 slave->needed_tailroom += master->needed_tailroom;
2297 p->xmit = cpu_dp->tag_ops->xmit;
2299 slave->features = master->vlan_features | NETIF_F_HW_TC;
2300 slave->hw_features |= NETIF_F_HW_TC;
2301 slave->features |= NETIF_F_LLTX;
2302 if (slave->needed_tailroom)
2303 slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
2304 if (ds->needs_standalone_vlan_filtering)
2305 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2308 int dsa_slave_suspend(struct net_device *slave_dev)
2310 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2312 if (!netif_running(slave_dev))
2315 netif_device_detach(slave_dev);
2318 phylink_stop(dp->pl);
2324 int dsa_slave_resume(struct net_device *slave_dev)
2326 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2328 if (!netif_running(slave_dev))
2331 netif_device_attach(slave_dev);
2334 phylink_start(dp->pl);
2340 int dsa_slave_create(struct dsa_port *port)
2342 const struct dsa_port *cpu_dp = port->cpu_dp;
2343 struct net_device *master = cpu_dp->master;
2344 struct dsa_switch *ds = port->ds;
2345 const char *name = port->name;
2346 struct net_device *slave_dev;
2347 struct dsa_slave_priv *p;
2350 if (!ds->num_tx_queues)
2351 ds->num_tx_queues = 1;
2353 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
2354 NET_NAME_UNKNOWN, ether_setup,
2355 ds->num_tx_queues, 1);
2356 if (slave_dev == NULL)
2359 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2360 #if IS_ENABLED(CONFIG_DCB)
2361 slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops;
2363 if (!is_zero_ether_addr(port->mac))
2364 eth_hw_addr_set(slave_dev, port->mac);
2366 eth_hw_addr_inherit(slave_dev, master);
2367 slave_dev->priv_flags |= IFF_NO_QUEUE;
2368 if (dsa_switch_supports_uc_filtering(ds))
2369 slave_dev->priv_flags |= IFF_UNICAST_FLT;
2370 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
2371 if (ds->ops->port_max_mtu)
2372 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2373 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
2375 SET_NETDEV_DEV(slave_dev, port->ds->dev);
2376 slave_dev->dev.of_node = port->dn;
2377 slave_dev->vlan_features = master->vlan_features;
2379 p = netdev_priv(slave_dev);
2380 slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2381 if (!slave_dev->tstats) {
2382 free_netdev(slave_dev);
2386 ret = gro_cells_init(&p->gcells, slave_dev);
2391 INIT_LIST_HEAD(&p->mall_tc_list);
2392 port->slave = slave_dev;
2393 dsa_slave_setup_tagger(slave_dev);
2395 netif_carrier_off(slave_dev);
2397 ret = dsa_slave_phy_setup(slave_dev);
2399 netdev_err(slave_dev,
2400 "error %d setting up PHY for tree %d, switch %d, port %d\n",
2401 ret, ds->dst->index, ds->index, port->index);
2407 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2408 if (ret && ret != -EOPNOTSUPP)
2409 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2410 ret, ETH_DATA_LEN, port->index);
2412 ret = register_netdevice(slave_dev);
2414 netdev_err(master, "error %d registering interface %s\n",
2415 ret, slave_dev->name);
2420 if (IS_ENABLED(CONFIG_DCB)) {
2421 ret = dsa_slave_dcbnl_init(slave_dev);
2423 netdev_err(slave_dev,
2424 "failed to initialize DCB: %pe\n",
2427 goto out_unregister;
2431 ret = netdev_upper_dev_link(master, slave_dev, NULL);
2436 goto out_unregister;
2441 unregister_netdev(slave_dev);
2444 phylink_disconnect_phy(p->dp->pl);
2446 phylink_destroy(p->dp->pl);
2448 gro_cells_destroy(&p->gcells);
2450 free_percpu(slave_dev->tstats);
2451 free_netdev(slave_dev);
2456 void dsa_slave_destroy(struct net_device *slave_dev)
2458 struct net_device *master = dsa_slave_to_master(slave_dev);
2459 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2460 struct dsa_slave_priv *p = netdev_priv(slave_dev);
2462 netif_carrier_off(slave_dev);
2464 netdev_upper_dev_unlink(master, slave_dev);
2465 unregister_netdevice(slave_dev);
2466 phylink_disconnect_phy(dp->pl);
2469 phylink_destroy(dp->pl);
2470 gro_cells_destroy(&p->gcells);
2471 free_percpu(slave_dev->tstats);
2472 free_netdev(slave_dev);
2475 bool dsa_slave_dev_check(const struct net_device *dev)
2477 return dev->netdev_ops == &dsa_slave_netdev_ops;
2479 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2481 static int dsa_slave_changeupper(struct net_device *dev,
2482 struct netdev_notifier_changeupper_info *info)
2484 struct dsa_port *dp = dsa_slave_to_port(dev);
2485 struct netlink_ext_ack *extack;
2486 int err = NOTIFY_DONE;
2488 extack = netdev_notifier_info_to_extack(&info->info);
2490 if (netif_is_bridge_master(info->upper_dev)) {
2491 if (info->linking) {
2492 err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2494 dsa_bridge_mtu_normalization(dp);
2495 if (err == -EOPNOTSUPP) {
2496 NL_SET_ERR_MSG_MOD(extack,
2497 "Offloading not supported");
2500 err = notifier_from_errno(err);
2502 dsa_port_bridge_leave(dp, info->upper_dev);
2505 } else if (netif_is_lag_master(info->upper_dev)) {
2506 if (info->linking) {
2507 err = dsa_port_lag_join(dp, info->upper_dev,
2508 info->upper_info, extack);
2509 if (err == -EOPNOTSUPP) {
2510 NL_SET_ERR_MSG_MOD(info->info.extack,
2511 "Offloading not supported");
2514 err = notifier_from_errno(err);
2516 dsa_port_lag_leave(dp, info->upper_dev);
2519 } else if (is_hsr_master(info->upper_dev)) {
2520 if (info->linking) {
2521 err = dsa_port_hsr_join(dp, info->upper_dev);
2522 if (err == -EOPNOTSUPP) {
2523 NL_SET_ERR_MSG_MOD(info->info.extack,
2524 "Offloading not supported");
2527 err = notifier_from_errno(err);
2529 dsa_port_hsr_leave(dp, info->upper_dev);
2537 static int dsa_slave_prechangeupper(struct net_device *dev,
2538 struct netdev_notifier_changeupper_info *info)
2540 struct dsa_port *dp = dsa_slave_to_port(dev);
2542 if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2543 dsa_port_pre_bridge_leave(dp, info->upper_dev);
2544 else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2545 dsa_port_pre_lag_leave(dp, info->upper_dev);
2546 /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2547 * meaningfully enslaved to a bridge yet
2554 dsa_slave_lag_changeupper(struct net_device *dev,
2555 struct netdev_notifier_changeupper_info *info)
2557 struct net_device *lower;
2558 struct list_head *iter;
2559 int err = NOTIFY_DONE;
2560 struct dsa_port *dp;
2562 netdev_for_each_lower_dev(dev, lower, iter) {
2563 if (!dsa_slave_dev_check(lower))
2566 dp = dsa_slave_to_port(lower);
2571 err = dsa_slave_changeupper(lower, info);
2572 if (notifier_to_errno(err))
2579 /* Same as dsa_slave_lag_changeupper() except that it calls
2580 * dsa_slave_prechangeupper()
2583 dsa_slave_lag_prechangeupper(struct net_device *dev,
2584 struct netdev_notifier_changeupper_info *info)
2586 struct net_device *lower;
2587 struct list_head *iter;
2588 int err = NOTIFY_DONE;
2589 struct dsa_port *dp;
2591 netdev_for_each_lower_dev(dev, lower, iter) {
2592 if (!dsa_slave_dev_check(lower))
2595 dp = dsa_slave_to_port(lower);
2600 err = dsa_slave_prechangeupper(lower, info);
2601 if (notifier_to_errno(err))
2609 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2610 struct netdev_notifier_changeupper_info *info)
2612 struct netlink_ext_ack *ext_ack;
2613 struct net_device *slave, *br;
2614 struct dsa_port *dp;
2616 ext_ack = netdev_notifier_info_to_extack(&info->info);
2618 if (!is_vlan_dev(dev))
2621 slave = vlan_dev_real_dev(dev);
2622 if (!dsa_slave_dev_check(slave))
2625 dp = dsa_slave_to_port(slave);
2626 br = dsa_port_bridge_dev_get(dp);
2630 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
2631 if (br_vlan_enabled(br) &&
2632 netif_is_bridge_master(info->upper_dev) && info->linking) {
2633 NL_SET_ERR_MSG_MOD(ext_ack,
2634 "Cannot enslave VLAN device into VLAN aware bridge");
2635 return notifier_from_errno(-EINVAL);
2642 dsa_slave_check_8021q_upper(struct net_device *dev,
2643 struct netdev_notifier_changeupper_info *info)
2645 struct dsa_port *dp = dsa_slave_to_port(dev);
2646 struct net_device *br = dsa_port_bridge_dev_get(dp);
2647 struct bridge_vlan_info br_info;
2648 struct netlink_ext_ack *extack;
2649 int err = NOTIFY_DONE;
2652 if (!br || !br_vlan_enabled(br))
2655 extack = netdev_notifier_info_to_extack(&info->info);
2656 vid = vlan_dev_vlan_id(info->upper_dev);
2658 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2659 * device, respectively the VID is not found, returning
2660 * 0 means success, which is a failure for us here.
2662 err = br_vlan_get_info(br, vid, &br_info);
2664 NL_SET_ERR_MSG_MOD(extack,
2665 "This VLAN is already configured by the bridge");
2666 return notifier_from_errno(-EBUSY);
2673 dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
2674 struct netdev_notifier_changeupper_info *info)
2676 struct dsa_switch *ds;
2677 struct dsa_port *dp;
2680 if (!dsa_slave_dev_check(dev))
2681 return dsa_prevent_bridging_8021q_upper(dev, info);
2683 dp = dsa_slave_to_port(dev);
2686 if (ds->ops->port_prechangeupper) {
2687 err = ds->ops->port_prechangeupper(ds, dp->index, info);
2689 return notifier_from_errno(err);
2692 if (is_vlan_dev(info->upper_dev))
2693 return dsa_slave_check_8021q_upper(dev, info);
2698 static int dsa_slave_netdevice_event(struct notifier_block *nb,
2699 unsigned long event, void *ptr)
2701 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2704 case NETDEV_PRECHANGEUPPER: {
2705 struct netdev_notifier_changeupper_info *info = ptr;
2708 err = dsa_slave_prechangeupper_sanity_check(dev, info);
2709 if (err != NOTIFY_DONE)
2712 if (dsa_slave_dev_check(dev))
2713 return dsa_slave_prechangeupper(dev, ptr);
2715 if (netif_is_lag_master(dev))
2716 return dsa_slave_lag_prechangeupper(dev, ptr);
2720 case NETDEV_CHANGEUPPER:
2721 if (dsa_slave_dev_check(dev))
2722 return dsa_slave_changeupper(dev, ptr);
2724 if (netif_is_lag_master(dev))
2725 return dsa_slave_lag_changeupper(dev, ptr);
2728 case NETDEV_CHANGELOWERSTATE: {
2729 struct netdev_notifier_changelowerstate_info *info = ptr;
2730 struct dsa_port *dp;
2733 if (!dsa_slave_dev_check(dev))
2736 dp = dsa_slave_to_port(dev);
2738 err = dsa_port_lag_change(dp, info->lower_state_info);
2739 return notifier_from_errno(err);
2743 /* Track state of master port.
2744 * DSA driver may require the master port (and indirectly
2745 * the tagger) to be available for some special operation.
2747 if (netdev_uses_dsa(dev)) {
2748 struct dsa_port *cpu_dp = dev->dsa_ptr;
2749 struct dsa_switch_tree *dst = cpu_dp->ds->dst;
2751 /* Track when the master port is UP */
2752 dsa_tree_master_oper_state_change(dst, dev,
2753 netif_oper_up(dev));
2755 /* Track when the master port is ready and can accept
2757 * NETDEV_UP event is not enough to flag a port as ready.
2758 * We also have to wait for linkwatch_do_dev to dev_activate
2759 * and emit a NETDEV_CHANGE event.
2760 * We check if a master port is ready by checking if the dev
2761 * have a qdisc assigned and is not noop.
2763 dsa_tree_master_admin_state_change(dst, dev,
2764 !qdisc_tx_is_noop(dev));
2771 case NETDEV_GOING_DOWN: {
2772 struct dsa_port *dp, *cpu_dp;
2773 struct dsa_switch_tree *dst;
2774 LIST_HEAD(close_list);
2776 if (!netdev_uses_dsa(dev))
2779 cpu_dp = dev->dsa_ptr;
2780 dst = cpu_dp->ds->dst;
2782 dsa_tree_master_admin_state_change(dst, dev, false);
2784 list_for_each_entry(dp, &dst->ports, list) {
2785 if (!dsa_port_is_user(dp))
2788 list_add(&dp->slave->close_list, &close_list);
2791 dev_close_many(&close_list, true);
2803 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2805 struct switchdev_notifier_fdb_info info = {};
2807 info.addr = switchdev_work->addr;
2808 info.vid = switchdev_work->vid;
2809 info.offloaded = true;
2810 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2811 switchdev_work->orig_dev, &info.info, NULL);
2814 static void dsa_slave_switchdev_event_work(struct work_struct *work)
2816 struct dsa_switchdev_event_work *switchdev_work =
2817 container_of(work, struct dsa_switchdev_event_work, work);
2818 const unsigned char *addr = switchdev_work->addr;
2819 struct net_device *dev = switchdev_work->dev;
2820 u16 vid = switchdev_work->vid;
2821 struct dsa_switch *ds;
2822 struct dsa_port *dp;
2825 dp = dsa_slave_to_port(dev);
2828 switch (switchdev_work->event) {
2829 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2830 if (switchdev_work->host_addr)
2831 err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
2833 err = dsa_port_lag_fdb_add(dp, addr, vid);
2835 err = dsa_port_fdb_add(dp, addr, vid);
2838 "port %d failed to add %pM vid %d to fdb: %d\n",
2839 dp->index, addr, vid, err);
2842 dsa_fdb_offload_notify(switchdev_work);
2845 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2846 if (switchdev_work->host_addr)
2847 err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
2849 err = dsa_port_lag_fdb_del(dp, addr, vid);
2851 err = dsa_port_fdb_del(dp, addr, vid);
2854 "port %d failed to delete %pM vid %d from fdb: %d\n",
2855 dp->index, addr, vid, err);
2861 kfree(switchdev_work);
2864 static bool dsa_foreign_dev_check(const struct net_device *dev,
2865 const struct net_device *foreign_dev)
2867 const struct dsa_port *dp = dsa_slave_to_port(dev);
2868 struct dsa_switch_tree *dst = dp->ds->dst;
2870 if (netif_is_bridge_master(foreign_dev))
2871 return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
2873 if (netif_is_bridge_port(foreign_dev))
2874 return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2876 /* Everything else is foreign */
2880 static int dsa_slave_fdb_event(struct net_device *dev,
2881 struct net_device *orig_dev,
2882 unsigned long event, const void *ctx,
2883 const struct switchdev_notifier_fdb_info *fdb_info)
2885 struct dsa_switchdev_event_work *switchdev_work;
2886 struct dsa_port *dp = dsa_slave_to_port(dev);
2887 bool host_addr = fdb_info->is_local;
2888 struct dsa_switch *ds = dp->ds;
2890 if (ctx && ctx != dp)
2896 if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
2897 if (dsa_port_offloads_bridge_port(dp, orig_dev))
2900 /* FDB entries learned by the software bridge or by foreign
2901 * bridge ports should be installed as host addresses only if
2902 * the driver requests assisted learning.
2904 if (!ds->assisted_learning_on_cpu_port)
2908 /* Also treat FDB entries on foreign interfaces bridged with us as host
2911 if (dsa_foreign_dev_check(dev, orig_dev))
2914 /* Check early that we're not doing work in vain.
2915 * Host addresses on LAG ports still require regular FDB ops,
2916 * since the CPU port isn't in a LAG.
2918 if (dp->lag && !host_addr) {
2919 if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
2922 if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2926 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2927 if (!switchdev_work)
2930 netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2931 event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2932 orig_dev->name, fdb_info->addr, fdb_info->vid,
2933 host_addr ? " as host address" : "");
2935 INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2936 switchdev_work->event = event;
2937 switchdev_work->dev = dev;
2938 switchdev_work->orig_dev = orig_dev;
2940 ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2941 switchdev_work->vid = fdb_info->vid;
2942 switchdev_work->host_addr = host_addr;
2944 dsa_schedule_work(&switchdev_work->work);
2949 /* Called under rcu_read_lock() */
2950 static int dsa_slave_switchdev_event(struct notifier_block *unused,
2951 unsigned long event, void *ptr)
2953 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2957 case SWITCHDEV_PORT_ATTR_SET:
2958 err = switchdev_handle_port_attr_set(dev, ptr,
2959 dsa_slave_dev_check,
2960 dsa_slave_port_attr_set);
2961 return notifier_from_errno(err);
2962 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2963 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2964 err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
2965 dsa_slave_dev_check,
2966 dsa_foreign_dev_check,
2967 dsa_slave_fdb_event);
2968 return notifier_from_errno(err);
2976 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2977 unsigned long event, void *ptr)
2979 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2983 case SWITCHDEV_PORT_OBJ_ADD:
2984 err = switchdev_handle_port_obj_add_foreign(dev, ptr,
2985 dsa_slave_dev_check,
2986 dsa_foreign_dev_check,
2987 dsa_slave_port_obj_add);
2988 return notifier_from_errno(err);
2989 case SWITCHDEV_PORT_OBJ_DEL:
2990 err = switchdev_handle_port_obj_del_foreign(dev, ptr,
2991 dsa_slave_dev_check,
2992 dsa_foreign_dev_check,
2993 dsa_slave_port_obj_del);
2994 return notifier_from_errno(err);
2995 case SWITCHDEV_PORT_ATTR_SET:
2996 err = switchdev_handle_port_attr_set(dev, ptr,
2997 dsa_slave_dev_check,
2998 dsa_slave_port_attr_set);
2999 return notifier_from_errno(err);
3005 static struct notifier_block dsa_slave_nb __read_mostly = {
3006 .notifier_call = dsa_slave_netdevice_event,
3009 struct notifier_block dsa_slave_switchdev_notifier = {
3010 .notifier_call = dsa_slave_switchdev_event,
3013 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
3014 .notifier_call = dsa_slave_switchdev_blocking_event,
3017 int dsa_slave_register_notifier(void)
3019 struct notifier_block *nb;
3022 err = register_netdevice_notifier(&dsa_slave_nb);
3026 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
3028 goto err_switchdev_nb;
3030 nb = &dsa_slave_switchdev_blocking_notifier;
3031 err = register_switchdev_blocking_notifier(nb);
3033 goto err_switchdev_blocking_nb;
3037 err_switchdev_blocking_nb:
3038 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3040 unregister_netdevice_notifier(&dsa_slave_nb);
3044 void dsa_slave_unregister_notifier(void)
3046 struct notifier_block *nb;
3049 nb = &dsa_slave_switchdev_blocking_notifier;
3050 err = unregister_switchdev_blocking_notifier(nb);
3052 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
3054 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3056 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3058 err = unregister_netdevice_notifier(&dsa_slave_nb);
3060 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);