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 static void dsa_slave_manage_host_flood(struct net_device *dev)
267 bool mc = dev->flags & (IFF_PROMISC | IFF_ALLMULTI);
268 struct dsa_port *dp = dsa_slave_to_port(dev);
269 bool uc = dev->flags & IFF_PROMISC;
271 dsa_port_set_host_flood(dp, uc, mc);
274 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
276 struct net_device *master = dsa_slave_to_master(dev);
277 struct dsa_port *dp = dsa_slave_to_port(dev);
278 struct dsa_switch *ds = dp->ds;
280 if (change & IFF_ALLMULTI)
281 dev_set_allmulti(master,
282 dev->flags & IFF_ALLMULTI ? 1 : -1);
283 if (change & IFF_PROMISC)
284 dev_set_promiscuity(master,
285 dev->flags & IFF_PROMISC ? 1 : -1);
287 if (dsa_switch_supports_uc_filtering(ds) &&
288 dsa_switch_supports_mc_filtering(ds))
289 dsa_slave_manage_host_flood(dev);
292 static void dsa_slave_set_rx_mode(struct net_device *dev)
294 __dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc);
295 __dev_uc_sync(dev, dsa_slave_sync_uc, dsa_slave_unsync_uc);
298 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
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;
303 struct sockaddr *addr = a;
306 if (!is_valid_ether_addr(addr->sa_data))
307 return -EADDRNOTAVAIL;
309 /* If the port is down, the address isn't synced yet to hardware or
310 * to the DSA master, so there is nothing to change.
312 if (!(dev->flags & IFF_UP))
313 goto out_change_dev_addr;
315 if (dsa_switch_supports_uc_filtering(ds)) {
316 err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0);
321 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
322 err = dev_uc_add(master, addr->sa_data);
327 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
328 dev_uc_del(master, dev->dev_addr);
330 if (dsa_switch_supports_uc_filtering(ds))
331 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
334 eth_hw_addr_set(dev, addr->sa_data);
339 if (dsa_switch_supports_uc_filtering(ds))
340 dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0);
345 struct dsa_slave_dump_ctx {
346 struct net_device *dev;
348 struct netlink_callback *cb;
353 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
354 bool is_static, void *data)
356 struct dsa_slave_dump_ctx *dump = data;
357 u32 portid = NETLINK_CB(dump->cb->skb).portid;
358 u32 seq = dump->cb->nlh->nlmsg_seq;
359 struct nlmsghdr *nlh;
362 if (dump->idx < dump->cb->args[2])
365 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
366 sizeof(*ndm), NLM_F_MULTI);
370 ndm = nlmsg_data(nlh);
371 ndm->ndm_family = AF_BRIDGE;
374 ndm->ndm_flags = NTF_SELF;
376 ndm->ndm_ifindex = dump->dev->ifindex;
377 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
379 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
380 goto nla_put_failure;
382 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
383 goto nla_put_failure;
385 nlmsg_end(dump->skb, nlh);
392 nlmsg_cancel(dump->skb, nlh);
397 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
398 struct net_device *dev, struct net_device *filter_dev,
401 struct dsa_port *dp = dsa_slave_to_port(dev);
402 struct dsa_slave_dump_ctx dump = {
410 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
416 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
418 struct dsa_slave_priv *p = netdev_priv(dev);
419 struct dsa_switch *ds = p->dp->ds;
420 int port = p->dp->index;
422 /* Pass through to switch driver if it supports timestamping */
425 if (ds->ops->port_hwtstamp_get)
426 return ds->ops->port_hwtstamp_get(ds, port, ifr);
429 if (ds->ops->port_hwtstamp_set)
430 return ds->ops->port_hwtstamp_set(ds, port, ifr);
434 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
437 static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
438 const struct switchdev_attr *attr,
439 struct netlink_ext_ack *extack)
441 struct dsa_port *dp = dsa_slave_to_port(dev);
444 if (ctx && ctx != dp)
448 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
449 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
452 ret = dsa_port_set_state(dp, attr->u.stp_state, true);
454 case SWITCHDEV_ATTR_ID_PORT_MST_STATE:
455 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
458 ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack);
460 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
461 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
464 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
467 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
468 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
471 ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
473 case SWITCHDEV_ATTR_ID_BRIDGE_MST:
474 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
477 ret = dsa_port_mst_enable(dp, attr->u.mst, extack);
479 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
480 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
483 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
486 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
487 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
490 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
492 case SWITCHDEV_ATTR_ID_VLAN_MSTI:
493 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
496 ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti);
506 /* Must be called under rcu_read_lock() */
508 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
509 const struct switchdev_obj_port_vlan *vlan)
511 struct net_device *upper_dev;
512 struct list_head *iter;
514 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
517 if (!is_vlan_dev(upper_dev))
520 vid = vlan_dev_vlan_id(upper_dev);
521 if (vid == vlan->vid)
528 static int dsa_slave_vlan_add(struct net_device *dev,
529 const struct switchdev_obj *obj,
530 struct netlink_ext_ack *extack)
532 struct dsa_port *dp = dsa_slave_to_port(dev);
533 struct switchdev_obj_port_vlan *vlan;
536 if (dsa_port_skip_vlan_configuration(dp)) {
537 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
541 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
543 /* Deny adding a bridge VLAN when there is already an 802.1Q upper with
546 if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
548 err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan);
551 NL_SET_ERR_MSG_MOD(extack,
552 "Port already has a VLAN upper with this VID");
557 return dsa_port_vlan_add(dp, vlan, extack);
560 /* Offload a VLAN installed on the bridge or on a foreign interface by
561 * installing it as a VLAN towards the CPU port.
563 static int dsa_slave_host_vlan_add(struct net_device *dev,
564 const struct switchdev_obj *obj,
565 struct netlink_ext_ack *extack)
567 struct dsa_port *dp = dsa_slave_to_port(dev);
568 struct switchdev_obj_port_vlan vlan;
570 /* Do nothing if this is a software bridge */
574 if (dsa_port_skip_vlan_configuration(dp)) {
575 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
579 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
581 /* Even though drivers often handle CPU membership in special ways,
582 * it doesn't make sense to program a PVID, so clear this flag.
584 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
586 return dsa_port_host_vlan_add(dp, &vlan, extack);
589 static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
590 const struct switchdev_obj *obj,
591 struct netlink_ext_ack *extack)
593 struct dsa_port *dp = dsa_slave_to_port(dev);
596 if (ctx && ctx != dp)
600 case SWITCHDEV_OBJ_ID_PORT_MDB:
601 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
604 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
606 case SWITCHDEV_OBJ_ID_HOST_MDB:
607 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
610 err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
612 case SWITCHDEV_OBJ_ID_PORT_VLAN:
613 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
614 err = dsa_slave_vlan_add(dev, obj, extack);
616 err = dsa_slave_host_vlan_add(dev, obj, extack);
618 case SWITCHDEV_OBJ_ID_MRP:
619 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
622 err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
624 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
625 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
628 err = dsa_port_mrp_add_ring_role(dp,
629 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
639 static int dsa_slave_vlan_del(struct net_device *dev,
640 const struct switchdev_obj *obj)
642 struct dsa_port *dp = dsa_slave_to_port(dev);
643 struct switchdev_obj_port_vlan *vlan;
645 if (dsa_port_skip_vlan_configuration(dp))
648 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
650 return dsa_port_vlan_del(dp, vlan);
653 static int dsa_slave_host_vlan_del(struct net_device *dev,
654 const struct switchdev_obj *obj)
656 struct dsa_port *dp = dsa_slave_to_port(dev);
657 struct switchdev_obj_port_vlan *vlan;
659 /* Do nothing if this is a software bridge */
663 if (dsa_port_skip_vlan_configuration(dp))
666 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
668 return dsa_port_host_vlan_del(dp, vlan);
671 static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
672 const struct switchdev_obj *obj)
674 struct dsa_port *dp = dsa_slave_to_port(dev);
677 if (ctx && ctx != dp)
681 case SWITCHDEV_OBJ_ID_PORT_MDB:
682 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
685 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
687 case SWITCHDEV_OBJ_ID_HOST_MDB:
688 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
691 err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
693 case SWITCHDEV_OBJ_ID_PORT_VLAN:
694 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
695 err = dsa_slave_vlan_del(dev, obj);
697 err = dsa_slave_host_vlan_del(dev, obj);
699 case SWITCHDEV_OBJ_ID_MRP:
700 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
703 err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
705 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
706 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
709 err = dsa_port_mrp_del_ring_role(dp,
710 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
720 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
723 #ifdef CONFIG_NET_POLL_CONTROLLER
724 struct dsa_slave_priv *p = netdev_priv(dev);
726 return netpoll_send_skb(p->netpoll, skb);
733 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
736 struct dsa_switch *ds = p->dp->ds;
738 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
741 if (!ds->ops->port_txtstamp)
744 ds->ops->port_txtstamp(ds, p->dp->index, skb);
747 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
749 /* SKB for netpoll still need to be mangled with the protocol-specific
750 * tag to be successfully transmitted
752 if (unlikely(netpoll_tx_running(dev)))
753 return dsa_slave_netpoll_send_skb(dev, skb);
755 /* Queue the SKB for transmission on the parent interface, but
756 * do not modify its EtherType
758 skb->dev = dsa_slave_to_master(dev);
763 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
765 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
767 int needed_headroom = dev->needed_headroom;
768 int needed_tailroom = dev->needed_tailroom;
770 /* For tail taggers, we need to pad short frames ourselves, to ensure
771 * that the tail tag does not fail at its role of being at the end of
772 * the packet, once the master interface pads the frame. Account for
773 * that pad length here, and pad later.
775 if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
776 needed_tailroom += ETH_ZLEN - skb->len;
777 /* skb_headroom() returns unsigned int... */
778 needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
779 needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
781 if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
782 /* No reallocation needed, yay! */
785 return pskb_expand_head(skb, needed_headroom, needed_tailroom,
789 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
791 struct dsa_slave_priv *p = netdev_priv(dev);
792 struct sk_buff *nskb;
794 dev_sw_netstats_tx_add(dev, 1, skb->len);
796 memset(skb->cb, 0, sizeof(skb->cb));
798 /* Handle tx timestamp if any */
799 dsa_skb_tx_timestamp(p, skb);
801 if (dsa_realloc_skb(skb, dev)) {
802 dev_kfree_skb_any(skb);
806 /* needed_tailroom should still be 'warm' in the cache line from
807 * dsa_realloc_skb(), which has also ensured that padding is safe.
809 if (dev->needed_tailroom)
812 /* Transmit function may have to reallocate the original SKB,
813 * in which case it must have freed it. Only free it here on error.
815 nskb = p->xmit(skb, dev);
821 return dsa_enqueue_skb(nskb, dev);
824 /* ethtool operations *******************************************************/
826 static void dsa_slave_get_drvinfo(struct net_device *dev,
827 struct ethtool_drvinfo *drvinfo)
829 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
830 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
831 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
834 static int dsa_slave_get_regs_len(struct net_device *dev)
836 struct dsa_port *dp = dsa_slave_to_port(dev);
837 struct dsa_switch *ds = dp->ds;
839 if (ds->ops->get_regs_len)
840 return ds->ops->get_regs_len(ds, dp->index);
846 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
848 struct dsa_port *dp = dsa_slave_to_port(dev);
849 struct dsa_switch *ds = dp->ds;
851 if (ds->ops->get_regs)
852 ds->ops->get_regs(ds, dp->index, regs, _p);
855 static int dsa_slave_nway_reset(struct net_device *dev)
857 struct dsa_port *dp = dsa_slave_to_port(dev);
859 return phylink_ethtool_nway_reset(dp->pl);
862 static int dsa_slave_get_eeprom_len(struct net_device *dev)
864 struct dsa_port *dp = dsa_slave_to_port(dev);
865 struct dsa_switch *ds = dp->ds;
867 if (ds->cd && ds->cd->eeprom_len)
868 return ds->cd->eeprom_len;
870 if (ds->ops->get_eeprom_len)
871 return ds->ops->get_eeprom_len(ds);
876 static int dsa_slave_get_eeprom(struct net_device *dev,
877 struct ethtool_eeprom *eeprom, u8 *data)
879 struct dsa_port *dp = dsa_slave_to_port(dev);
880 struct dsa_switch *ds = dp->ds;
882 if (ds->ops->get_eeprom)
883 return ds->ops->get_eeprom(ds, eeprom, data);
888 static int dsa_slave_set_eeprom(struct net_device *dev,
889 struct ethtool_eeprom *eeprom, u8 *data)
891 struct dsa_port *dp = dsa_slave_to_port(dev);
892 struct dsa_switch *ds = dp->ds;
894 if (ds->ops->set_eeprom)
895 return ds->ops->set_eeprom(ds, eeprom, data);
900 static void dsa_slave_get_strings(struct net_device *dev,
901 uint32_t stringset, uint8_t *data)
903 struct dsa_port *dp = dsa_slave_to_port(dev);
904 struct dsa_switch *ds = dp->ds;
906 if (stringset == ETH_SS_STATS) {
907 int len = ETH_GSTRING_LEN;
909 strncpy(data, "tx_packets", len);
910 strncpy(data + len, "tx_bytes", len);
911 strncpy(data + 2 * len, "rx_packets", len);
912 strncpy(data + 3 * len, "rx_bytes", len);
913 if (ds->ops->get_strings)
914 ds->ops->get_strings(ds, dp->index, stringset,
916 } else if (stringset == ETH_SS_TEST) {
917 net_selftest_get_strings(data);
922 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
923 struct ethtool_stats *stats,
926 struct dsa_port *dp = dsa_slave_to_port(dev);
927 struct dsa_switch *ds = dp->ds;
928 struct pcpu_sw_netstats *s;
932 for_each_possible_cpu(i) {
933 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
935 s = per_cpu_ptr(dev->tstats, i);
937 start = u64_stats_fetch_begin_irq(&s->syncp);
938 tx_packets = u64_stats_read(&s->tx_packets);
939 tx_bytes = u64_stats_read(&s->tx_bytes);
940 rx_packets = u64_stats_read(&s->rx_packets);
941 rx_bytes = u64_stats_read(&s->rx_bytes);
942 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
943 data[0] += tx_packets;
945 data[2] += rx_packets;
948 if (ds->ops->get_ethtool_stats)
949 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
952 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
954 struct dsa_port *dp = dsa_slave_to_port(dev);
955 struct dsa_switch *ds = dp->ds;
957 if (sset == ETH_SS_STATS) {
960 if (ds->ops->get_sset_count) {
961 count = ds->ops->get_sset_count(ds, dp->index, sset);
967 } else if (sset == ETH_SS_TEST) {
968 return net_selftest_get_count();
974 static void dsa_slave_get_eth_phy_stats(struct net_device *dev,
975 struct ethtool_eth_phy_stats *phy_stats)
977 struct dsa_port *dp = dsa_slave_to_port(dev);
978 struct dsa_switch *ds = dp->ds;
980 if (ds->ops->get_eth_phy_stats)
981 ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
984 static void dsa_slave_get_eth_mac_stats(struct net_device *dev,
985 struct ethtool_eth_mac_stats *mac_stats)
987 struct dsa_port *dp = dsa_slave_to_port(dev);
988 struct dsa_switch *ds = dp->ds;
990 if (ds->ops->get_eth_mac_stats)
991 ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
995 dsa_slave_get_eth_ctrl_stats(struct net_device *dev,
996 struct ethtool_eth_ctrl_stats *ctrl_stats)
998 struct dsa_port *dp = dsa_slave_to_port(dev);
999 struct dsa_switch *ds = dp->ds;
1001 if (ds->ops->get_eth_ctrl_stats)
1002 ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1006 dsa_slave_get_rmon_stats(struct net_device *dev,
1007 struct ethtool_rmon_stats *rmon_stats,
1008 const struct ethtool_rmon_hist_range **ranges)
1010 struct dsa_port *dp = dsa_slave_to_port(dev);
1011 struct dsa_switch *ds = dp->ds;
1013 if (ds->ops->get_rmon_stats)
1014 ds->ops->get_rmon_stats(ds, dp->index, rmon_stats, ranges);
1017 static void dsa_slave_net_selftest(struct net_device *ndev,
1018 struct ethtool_test *etest, u64 *buf)
1020 struct dsa_port *dp = dsa_slave_to_port(ndev);
1021 struct dsa_switch *ds = dp->ds;
1023 if (ds->ops->self_test) {
1024 ds->ops->self_test(ds, dp->index, etest, buf);
1028 net_selftest(ndev, etest, buf);
1031 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1033 struct dsa_port *dp = dsa_slave_to_port(dev);
1034 struct dsa_switch *ds = dp->ds;
1036 phylink_ethtool_get_wol(dp->pl, w);
1038 if (ds->ops->get_wol)
1039 ds->ops->get_wol(ds, dp->index, w);
1042 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1044 struct dsa_port *dp = dsa_slave_to_port(dev);
1045 struct dsa_switch *ds = dp->ds;
1046 int ret = -EOPNOTSUPP;
1048 phylink_ethtool_set_wol(dp->pl, w);
1050 if (ds->ops->set_wol)
1051 ret = ds->ops->set_wol(ds, dp->index, w);
1056 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
1058 struct dsa_port *dp = dsa_slave_to_port(dev);
1059 struct dsa_switch *ds = dp->ds;
1062 /* Port's PHY and MAC both need to be EEE capable */
1063 if (!dev->phydev || !dp->pl)
1066 if (!ds->ops->set_mac_eee)
1069 ret = ds->ops->set_mac_eee(ds, dp->index, e);
1073 return phylink_ethtool_set_eee(dp->pl, e);
1076 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
1078 struct dsa_port *dp = dsa_slave_to_port(dev);
1079 struct dsa_switch *ds = dp->ds;
1082 /* Port's PHY and MAC both need to be EEE capable */
1083 if (!dev->phydev || !dp->pl)
1086 if (!ds->ops->get_mac_eee)
1089 ret = ds->ops->get_mac_eee(ds, dp->index, e);
1093 return phylink_ethtool_get_eee(dp->pl, e);
1096 static int dsa_slave_get_link_ksettings(struct net_device *dev,
1097 struct ethtool_link_ksettings *cmd)
1099 struct dsa_port *dp = dsa_slave_to_port(dev);
1101 return phylink_ethtool_ksettings_get(dp->pl, cmd);
1104 static int dsa_slave_set_link_ksettings(struct net_device *dev,
1105 const struct ethtool_link_ksettings *cmd)
1107 struct dsa_port *dp = dsa_slave_to_port(dev);
1109 return phylink_ethtool_ksettings_set(dp->pl, cmd);
1112 static void dsa_slave_get_pause_stats(struct net_device *dev,
1113 struct ethtool_pause_stats *pause_stats)
1115 struct dsa_port *dp = dsa_slave_to_port(dev);
1116 struct dsa_switch *ds = dp->ds;
1118 if (ds->ops->get_pause_stats)
1119 ds->ops->get_pause_stats(ds, dp->index, pause_stats);
1122 static void dsa_slave_get_pauseparam(struct net_device *dev,
1123 struct ethtool_pauseparam *pause)
1125 struct dsa_port *dp = dsa_slave_to_port(dev);
1127 phylink_ethtool_get_pauseparam(dp->pl, pause);
1130 static int dsa_slave_set_pauseparam(struct net_device *dev,
1131 struct ethtool_pauseparam *pause)
1133 struct dsa_port *dp = dsa_slave_to_port(dev);
1135 return phylink_ethtool_set_pauseparam(dp->pl, pause);
1138 #ifdef CONFIG_NET_POLL_CONTROLLER
1139 static int dsa_slave_netpoll_setup(struct net_device *dev,
1140 struct netpoll_info *ni)
1142 struct net_device *master = dsa_slave_to_master(dev);
1143 struct dsa_slave_priv *p = netdev_priv(dev);
1144 struct netpoll *netpoll;
1147 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1151 err = __netpoll_setup(netpoll, master);
1157 p->netpoll = netpoll;
1162 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1164 struct dsa_slave_priv *p = netdev_priv(dev);
1165 struct netpoll *netpoll = p->netpoll;
1172 __netpoll_free(netpoll);
1175 static void dsa_slave_poll_controller(struct net_device *dev)
1180 static struct dsa_mall_tc_entry *
1181 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1183 struct dsa_slave_priv *p = netdev_priv(dev);
1184 struct dsa_mall_tc_entry *mall_tc_entry;
1186 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1187 if (mall_tc_entry->cookie == cookie)
1188 return mall_tc_entry;
1194 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
1195 struct tc_cls_matchall_offload *cls,
1198 struct netlink_ext_ack *extack = cls->common.extack;
1199 struct dsa_port *dp = dsa_slave_to_port(dev);
1200 struct dsa_slave_priv *p = netdev_priv(dev);
1201 struct dsa_mall_mirror_tc_entry *mirror;
1202 struct dsa_mall_tc_entry *mall_tc_entry;
1203 struct dsa_switch *ds = dp->ds;
1204 struct flow_action_entry *act;
1205 struct dsa_port *to_dp;
1208 if (!ds->ops->port_mirror_add)
1211 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1212 cls->common.extack))
1215 act = &cls->rule->action.entries[0];
1220 if (!dsa_slave_dev_check(act->dev))
1223 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1227 mall_tc_entry->cookie = cls->cookie;
1228 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1229 mirror = &mall_tc_entry->mirror;
1231 to_dp = dsa_slave_to_port(act->dev);
1233 mirror->to_local_port = to_dp->index;
1234 mirror->ingress = ingress;
1236 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1238 kfree(mall_tc_entry);
1242 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1248 dsa_slave_add_cls_matchall_police(struct net_device *dev,
1249 struct tc_cls_matchall_offload *cls,
1252 struct netlink_ext_ack *extack = cls->common.extack;
1253 struct dsa_port *dp = dsa_slave_to_port(dev);
1254 struct dsa_slave_priv *p = netdev_priv(dev);
1255 struct dsa_mall_policer_tc_entry *policer;
1256 struct dsa_mall_tc_entry *mall_tc_entry;
1257 struct dsa_switch *ds = dp->ds;
1258 struct flow_action_entry *act;
1261 if (!ds->ops->port_policer_add) {
1262 NL_SET_ERR_MSG_MOD(extack,
1263 "Policing offload not implemented");
1268 NL_SET_ERR_MSG_MOD(extack,
1269 "Only supported on ingress qdisc");
1273 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1274 cls->common.extack))
1277 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1278 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1279 NL_SET_ERR_MSG_MOD(extack,
1280 "Only one port policer allowed");
1285 act = &cls->rule->action.entries[0];
1287 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1291 mall_tc_entry->cookie = cls->cookie;
1292 mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1293 policer = &mall_tc_entry->policer;
1294 policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1295 policer->burst = act->police.burst;
1297 err = ds->ops->port_policer_add(ds, dp->index, policer);
1299 kfree(mall_tc_entry);
1303 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1308 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1309 struct tc_cls_matchall_offload *cls,
1312 int err = -EOPNOTSUPP;
1314 if (cls->common.protocol == htons(ETH_P_ALL) &&
1315 flow_offload_has_one_action(&cls->rule->action) &&
1316 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1317 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1318 else if (flow_offload_has_one_action(&cls->rule->action) &&
1319 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1320 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1325 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1326 struct tc_cls_matchall_offload *cls)
1328 struct dsa_port *dp = dsa_slave_to_port(dev);
1329 struct dsa_mall_tc_entry *mall_tc_entry;
1330 struct dsa_switch *ds = dp->ds;
1332 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1336 list_del(&mall_tc_entry->list);
1338 switch (mall_tc_entry->type) {
1339 case DSA_PORT_MALL_MIRROR:
1340 if (ds->ops->port_mirror_del)
1341 ds->ops->port_mirror_del(ds, dp->index,
1342 &mall_tc_entry->mirror);
1344 case DSA_PORT_MALL_POLICER:
1345 if (ds->ops->port_policer_del)
1346 ds->ops->port_policer_del(ds, dp->index);
1352 kfree(mall_tc_entry);
1355 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1356 struct tc_cls_matchall_offload *cls,
1359 if (cls->common.chain_index)
1362 switch (cls->command) {
1363 case TC_CLSMATCHALL_REPLACE:
1364 return dsa_slave_add_cls_matchall(dev, cls, ingress);
1365 case TC_CLSMATCHALL_DESTROY:
1366 dsa_slave_del_cls_matchall(dev, cls);
1373 static int dsa_slave_add_cls_flower(struct net_device *dev,
1374 struct flow_cls_offload *cls,
1377 struct dsa_port *dp = dsa_slave_to_port(dev);
1378 struct dsa_switch *ds = dp->ds;
1379 int port = dp->index;
1381 if (!ds->ops->cls_flower_add)
1384 return ds->ops->cls_flower_add(ds, port, cls, ingress);
1387 static int dsa_slave_del_cls_flower(struct net_device *dev,
1388 struct flow_cls_offload *cls,
1391 struct dsa_port *dp = dsa_slave_to_port(dev);
1392 struct dsa_switch *ds = dp->ds;
1393 int port = dp->index;
1395 if (!ds->ops->cls_flower_del)
1398 return ds->ops->cls_flower_del(ds, port, cls, ingress);
1401 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1402 struct flow_cls_offload *cls,
1405 struct dsa_port *dp = dsa_slave_to_port(dev);
1406 struct dsa_switch *ds = dp->ds;
1407 int port = dp->index;
1409 if (!ds->ops->cls_flower_stats)
1412 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1415 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1416 struct flow_cls_offload *cls,
1419 switch (cls->command) {
1420 case FLOW_CLS_REPLACE:
1421 return dsa_slave_add_cls_flower(dev, cls, ingress);
1422 case FLOW_CLS_DESTROY:
1423 return dsa_slave_del_cls_flower(dev, cls, ingress);
1424 case FLOW_CLS_STATS:
1425 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1431 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1432 void *cb_priv, bool ingress)
1434 struct net_device *dev = cb_priv;
1436 if (!tc_can_offload(dev))
1440 case TC_SETUP_CLSMATCHALL:
1441 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1442 case TC_SETUP_CLSFLOWER:
1443 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1449 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1450 void *type_data, void *cb_priv)
1452 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1455 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1456 void *type_data, void *cb_priv)
1458 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1461 static LIST_HEAD(dsa_slave_block_cb_list);
1463 static int dsa_slave_setup_tc_block(struct net_device *dev,
1464 struct flow_block_offload *f)
1466 struct flow_block_cb *block_cb;
1467 flow_setup_cb_t *cb;
1469 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1470 cb = dsa_slave_setup_tc_block_cb_ig;
1471 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1472 cb = dsa_slave_setup_tc_block_cb_eg;
1476 f->driver_block_list = &dsa_slave_block_cb_list;
1478 switch (f->command) {
1479 case FLOW_BLOCK_BIND:
1480 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1483 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1484 if (IS_ERR(block_cb))
1485 return PTR_ERR(block_cb);
1487 flow_block_cb_add(block_cb, f);
1488 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1490 case FLOW_BLOCK_UNBIND:
1491 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1495 flow_block_cb_remove(block_cb, f);
1496 list_del(&block_cb->driver_list);
1503 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
1506 struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1507 struct net_device *master = cpu_dp->master;
1509 if (!master->netdev_ops->ndo_setup_tc)
1512 return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
1515 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1518 struct dsa_port *dp = dsa_slave_to_port(dev);
1519 struct dsa_switch *ds = dp->ds;
1522 case TC_SETUP_BLOCK:
1523 return dsa_slave_setup_tc_block(dev, type_data);
1525 return dsa_slave_setup_ft_block(ds, dp->index, type_data);
1530 if (!ds->ops->port_setup_tc)
1533 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1536 static int dsa_slave_get_rxnfc(struct net_device *dev,
1537 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1539 struct dsa_port *dp = dsa_slave_to_port(dev);
1540 struct dsa_switch *ds = dp->ds;
1542 if (!ds->ops->get_rxnfc)
1545 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1548 static int dsa_slave_set_rxnfc(struct net_device *dev,
1549 struct ethtool_rxnfc *nfc)
1551 struct dsa_port *dp = dsa_slave_to_port(dev);
1552 struct dsa_switch *ds = dp->ds;
1554 if (!ds->ops->set_rxnfc)
1557 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1560 static int dsa_slave_get_ts_info(struct net_device *dev,
1561 struct ethtool_ts_info *ts)
1563 struct dsa_slave_priv *p = netdev_priv(dev);
1564 struct dsa_switch *ds = p->dp->ds;
1566 if (!ds->ops->get_ts_info)
1569 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1572 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1575 struct dsa_port *dp = dsa_slave_to_port(dev);
1576 struct switchdev_obj_port_vlan vlan = {
1577 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1579 /* This API only allows programming tagged, non-PVID VIDs */
1582 struct netlink_ext_ack extack = {0};
1586 ret = dsa_port_vlan_add(dp, &vlan, &extack);
1589 netdev_err(dev, "%s\n", extack._msg);
1593 /* And CPU port... */
1594 ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
1597 netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1605 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1608 struct dsa_port *dp = dsa_slave_to_port(dev);
1609 struct switchdev_obj_port_vlan vlan = {
1611 /* This API only allows programming tagged, non-PVID VIDs */
1616 err = dsa_port_vlan_del(dp, &vlan);
1620 return dsa_port_host_vlan_del(dp, &vlan);
1623 static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
1625 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1627 return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
1630 static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
1632 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1634 return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
1637 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1638 * filtering is enabled. The baseline is that only ports that offload a
1639 * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1640 * but there are exceptions for quirky hardware.
1642 * If ds->vlan_filtering_is_global = true, then standalone ports which share
1643 * the same switch with other ports that offload a VLAN-aware bridge are also
1644 * inevitably VLAN-aware.
1646 * To summarize, a DSA switch port offloads:
1648 * - If standalone (this includes software bridge, software LAG):
1649 * - if ds->needs_standalone_vlan_filtering = true, OR if
1650 * (ds->vlan_filtering_is_global = true AND there are bridges spanning
1651 * this switch chip which have vlan_filtering=1)
1652 * - the 8021q upper VLANs
1653 * - else (standalone VLAN filtering is not needed, VLAN filtering is not
1654 * global, or it is, but no port is under a VLAN-aware bridge):
1655 * - no VLAN (any 8021q upper is a software VLAN)
1657 * - If under a vlan_filtering=0 bridge which it offload:
1658 * - if ds->configure_vlan_while_not_filtering = true (default):
1659 * - the bridge VLANs. These VLANs are committed to hardware but inactive.
1660 * - else (deprecated):
1661 * - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1662 * enabled, so this behavior is broken and discouraged.
1664 * - If under a vlan_filtering=1 bridge which it offload:
1665 * - the bridge VLANs
1666 * - the 8021q upper VLANs
1668 int dsa_slave_manage_vlan_filtering(struct net_device *slave,
1669 bool vlan_filtering)
1673 if (vlan_filtering) {
1674 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1676 err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
1678 vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1679 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1683 err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1687 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1693 struct dsa_hw_port {
1694 struct list_head list;
1695 struct net_device *dev;
1699 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1701 const struct dsa_hw_port *p;
1704 list_for_each_entry(p, hw_port_list, list) {
1705 if (p->dev->mtu == mtu)
1708 err = dev_set_mtu(p->dev, mtu);
1716 list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1717 if (p->dev->mtu == p->old_mtu)
1720 if (dev_set_mtu(p->dev, p->old_mtu))
1721 netdev_err(p->dev, "Failed to restore MTU\n");
1727 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1729 struct dsa_hw_port *p, *n;
1731 list_for_each_entry_safe(p, n, hw_port_list, list)
1735 /* Make the hardware datapath to/from @dev limited to a common MTU */
1736 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1738 struct list_head hw_port_list;
1739 struct dsa_switch_tree *dst;
1740 int min_mtu = ETH_MAX_MTU;
1741 struct dsa_port *other_dp;
1744 if (!dp->ds->mtu_enforcement_ingress)
1750 INIT_LIST_HEAD(&hw_port_list);
1752 /* Populate the list of ports that are part of the same bridge
1753 * as the newly added/modified port
1755 list_for_each_entry(dst, &dsa_tree_list, list) {
1756 list_for_each_entry(other_dp, &dst->ports, list) {
1757 struct dsa_hw_port *hw_port;
1758 struct net_device *slave;
1760 if (other_dp->type != DSA_PORT_TYPE_USER)
1763 if (!dsa_port_bridge_same(dp, other_dp))
1766 if (!other_dp->ds->mtu_enforcement_ingress)
1769 slave = other_dp->slave;
1771 if (min_mtu > slave->mtu)
1772 min_mtu = slave->mtu;
1774 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1778 hw_port->dev = slave;
1779 hw_port->old_mtu = slave->mtu;
1781 list_add(&hw_port->list, &hw_port_list);
1785 /* Attempt to configure the entire hardware bridge to the newly added
1786 * interface's MTU first, regardless of whether the intention of the
1787 * user was to raise or lower it.
1789 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1793 /* Clearly that didn't work out so well, so just set the minimum MTU on
1794 * all hardware bridge ports now. If this fails too, then all ports will
1795 * still have their old MTU rolled back anyway.
1797 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1800 dsa_hw_port_list_free(&hw_port_list);
1803 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1805 struct net_device *master = dsa_slave_to_master(dev);
1806 struct dsa_port *dp = dsa_slave_to_port(dev);
1807 struct dsa_port *cpu_dp = dp->cpu_dp;
1808 struct dsa_switch *ds = dp->ds;
1809 struct dsa_port *other_dp;
1810 int largest_mtu = 0;
1817 if (!ds->ops->port_change_mtu)
1820 dsa_tree_for_each_user_port(other_dp, ds->dst) {
1823 /* During probe, this function will be called for each slave
1824 * device, while not all of them have been allocated. That's
1825 * ok, it doesn't change what the maximum is, so ignore it.
1827 if (!other_dp->slave)
1830 /* Pretend that we already applied the setting, which we
1831 * actually haven't (still haven't done all integrity checks)
1834 slave_mtu = new_mtu;
1836 slave_mtu = other_dp->slave->mtu;
1838 if (largest_mtu < slave_mtu)
1839 largest_mtu = slave_mtu;
1842 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1843 old_master_mtu = master->mtu;
1844 new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1845 if (new_master_mtu > mtu_limit)
1848 /* If the master MTU isn't over limit, there's no need to check the CPU
1849 * MTU, since that surely isn't either.
1851 cpu_mtu = largest_mtu;
1853 /* Start applying stuff */
1854 if (new_master_mtu != old_master_mtu) {
1855 err = dev_set_mtu(master, new_master_mtu);
1857 goto out_master_failed;
1859 /* We only need to propagate the MTU of the CPU port to
1860 * upstream switches, so emit a notifier which updates them.
1862 err = dsa_port_mtu_change(cpu_dp, cpu_mtu);
1864 goto out_cpu_failed;
1867 err = ds->ops->port_change_mtu(ds, dp->index, new_mtu);
1869 goto out_port_failed;
1873 dsa_bridge_mtu_normalization(dp);
1878 if (new_master_mtu != old_master_mtu)
1879 dsa_port_mtu_change(cpu_dp, old_master_mtu -
1880 dsa_tag_protocol_overhead(cpu_dp->tag_ops));
1882 if (new_master_mtu != old_master_mtu)
1883 dev_set_mtu(master, old_master_mtu);
1888 static int __maybe_unused
1889 dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
1891 struct dsa_port *dp = dsa_slave_to_port(dev);
1892 struct dsa_switch *ds = dp->ds;
1893 unsigned long mask, new_prio;
1894 int err, port = dp->index;
1896 if (!ds->ops->port_set_default_prio)
1899 err = dcb_ieee_setapp(dev, app);
1903 mask = dcb_ieee_getapp_mask(dev, app);
1904 new_prio = __fls(mask);
1906 err = ds->ops->port_set_default_prio(ds, port, new_prio);
1908 dcb_ieee_delapp(dev, app);
1915 static int __maybe_unused
1916 dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
1918 struct dsa_port *dp = dsa_slave_to_port(dev);
1919 struct dsa_switch *ds = dp->ds;
1920 unsigned long mask, new_prio;
1921 int err, port = dp->index;
1922 u8 dscp = app->protocol;
1924 if (!ds->ops->port_add_dscp_prio)
1928 netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
1933 err = dcb_ieee_setapp(dev, app);
1937 mask = dcb_ieee_getapp_mask(dev, app);
1938 new_prio = __fls(mask);
1940 err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
1942 dcb_ieee_delapp(dev, app);
1949 static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev,
1950 struct dcb_app *app)
1952 switch (app->selector) {
1953 case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
1954 switch (app->protocol) {
1956 return dsa_slave_dcbnl_set_default_prio(dev, app);
1961 case IEEE_8021QAZ_APP_SEL_DSCP:
1962 return dsa_slave_dcbnl_add_dscp_prio(dev, app);
1968 static int __maybe_unused
1969 dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
1971 struct dsa_port *dp = dsa_slave_to_port(dev);
1972 struct dsa_switch *ds = dp->ds;
1973 unsigned long mask, new_prio;
1974 int err, port = dp->index;
1976 if (!ds->ops->port_set_default_prio)
1979 err = dcb_ieee_delapp(dev, app);
1983 mask = dcb_ieee_getapp_mask(dev, app);
1984 new_prio = mask ? __fls(mask) : 0;
1986 err = ds->ops->port_set_default_prio(ds, port, new_prio);
1988 dcb_ieee_setapp(dev, app);
1995 static int __maybe_unused
1996 dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
1998 struct dsa_port *dp = dsa_slave_to_port(dev);
1999 struct dsa_switch *ds = dp->ds;
2000 int err, port = dp->index;
2001 u8 dscp = app->protocol;
2003 if (!ds->ops->port_del_dscp_prio)
2006 err = dcb_ieee_delapp(dev, app);
2010 err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
2012 dcb_ieee_setapp(dev, app);
2019 static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev,
2020 struct dcb_app *app)
2022 switch (app->selector) {
2023 case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2024 switch (app->protocol) {
2026 return dsa_slave_dcbnl_del_default_prio(dev, app);
2031 case IEEE_8021QAZ_APP_SEL_DSCP:
2032 return dsa_slave_dcbnl_del_dscp_prio(dev, app);
2038 /* Pre-populate the DCB application priority table with the priorities
2039 * configured during switch setup, which we read from hardware here.
2041 static int dsa_slave_dcbnl_init(struct net_device *dev)
2043 struct dsa_port *dp = dsa_slave_to_port(dev);
2044 struct dsa_switch *ds = dp->ds;
2045 int port = dp->index;
2048 if (ds->ops->port_get_default_prio) {
2049 int prio = ds->ops->port_get_default_prio(ds, port);
2050 struct dcb_app app = {
2051 .selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2059 err = dcb_ieee_setapp(dev, &app);
2064 if (ds->ops->port_get_dscp_prio) {
2067 for (protocol = 0; protocol < 64; protocol++) {
2068 struct dcb_app app = {
2069 .selector = IEEE_8021QAZ_APP_SEL_DSCP,
2070 .protocol = protocol,
2074 prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
2075 if (prio == -EOPNOTSUPP)
2080 app.priority = prio;
2082 err = dcb_ieee_setapp(dev, &app);
2091 static const struct ethtool_ops dsa_slave_ethtool_ops = {
2092 .get_drvinfo = dsa_slave_get_drvinfo,
2093 .get_regs_len = dsa_slave_get_regs_len,
2094 .get_regs = dsa_slave_get_regs,
2095 .nway_reset = dsa_slave_nway_reset,
2096 .get_link = ethtool_op_get_link,
2097 .get_eeprom_len = dsa_slave_get_eeprom_len,
2098 .get_eeprom = dsa_slave_get_eeprom,
2099 .set_eeprom = dsa_slave_set_eeprom,
2100 .get_strings = dsa_slave_get_strings,
2101 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
2102 .get_sset_count = dsa_slave_get_sset_count,
2103 .get_eth_phy_stats = dsa_slave_get_eth_phy_stats,
2104 .get_eth_mac_stats = dsa_slave_get_eth_mac_stats,
2105 .get_eth_ctrl_stats = dsa_slave_get_eth_ctrl_stats,
2106 .get_rmon_stats = dsa_slave_get_rmon_stats,
2107 .set_wol = dsa_slave_set_wol,
2108 .get_wol = dsa_slave_get_wol,
2109 .set_eee = dsa_slave_set_eee,
2110 .get_eee = dsa_slave_get_eee,
2111 .get_link_ksettings = dsa_slave_get_link_ksettings,
2112 .set_link_ksettings = dsa_slave_set_link_ksettings,
2113 .get_pause_stats = dsa_slave_get_pause_stats,
2114 .get_pauseparam = dsa_slave_get_pauseparam,
2115 .set_pauseparam = dsa_slave_set_pauseparam,
2116 .get_rxnfc = dsa_slave_get_rxnfc,
2117 .set_rxnfc = dsa_slave_set_rxnfc,
2118 .get_ts_info = dsa_slave_get_ts_info,
2119 .self_test = dsa_slave_net_selftest,
2122 static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = {
2123 .ieee_setapp = dsa_slave_dcbnl_ieee_setapp,
2124 .ieee_delapp = dsa_slave_dcbnl_ieee_delapp,
2127 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
2129 struct dsa_port *dp = dsa_slave_to_port(dev);
2131 return &dp->devlink_port;
2134 static void dsa_slave_get_stats64(struct net_device *dev,
2135 struct rtnl_link_stats64 *s)
2137 struct dsa_port *dp = dsa_slave_to_port(dev);
2138 struct dsa_switch *ds = dp->ds;
2140 if (ds->ops->get_stats64)
2141 ds->ops->get_stats64(ds, dp->index, s);
2143 dev_get_tstats64(dev, s);
2146 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
2147 struct net_device_path *path)
2149 struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
2150 struct dsa_port *cpu_dp = dp->cpu_dp;
2152 path->dev = ctx->dev;
2153 path->type = DEV_PATH_DSA;
2154 path->dsa.proto = cpu_dp->tag_ops->proto;
2155 path->dsa.port = dp->index;
2156 ctx->dev = cpu_dp->master;
2161 static const struct net_device_ops dsa_slave_netdev_ops = {
2162 .ndo_open = dsa_slave_open,
2163 .ndo_stop = dsa_slave_close,
2164 .ndo_start_xmit = dsa_slave_xmit,
2165 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
2166 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
2167 .ndo_set_mac_address = dsa_slave_set_mac_address,
2168 .ndo_fdb_dump = dsa_slave_fdb_dump,
2169 .ndo_eth_ioctl = dsa_slave_ioctl,
2170 .ndo_get_iflink = dsa_slave_get_iflink,
2171 #ifdef CONFIG_NET_POLL_CONTROLLER
2172 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
2173 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
2174 .ndo_poll_controller = dsa_slave_poll_controller,
2176 .ndo_setup_tc = dsa_slave_setup_tc,
2177 .ndo_get_stats64 = dsa_slave_get_stats64,
2178 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
2179 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
2180 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
2181 .ndo_change_mtu = dsa_slave_change_mtu,
2182 .ndo_fill_forward_path = dsa_slave_fill_forward_path,
2185 static struct device_type dsa_type = {
2189 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2191 const struct dsa_port *dp = dsa_to_port(ds, port);
2194 phylink_mac_change(dp->pl, up);
2196 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2198 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
2199 struct phylink_link_state *state)
2201 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
2202 struct dsa_switch *ds = dp->ds;
2204 /* No need to check that this operation is valid, the callback would
2205 * not be called if it was not.
2207 ds->ops->phylink_fixed_state(ds, dp->index, state);
2210 /* slave device setup *******************************************************/
2211 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
2214 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2215 struct dsa_switch *ds = dp->ds;
2217 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
2218 if (!slave_dev->phydev) {
2219 netdev_err(slave_dev, "no phy at %d\n", addr);
2223 slave_dev->phydev->dev_flags |= flags;
2225 return phylink_connect_phy(dp->pl, slave_dev->phydev);
2228 static int dsa_slave_phy_setup(struct net_device *slave_dev)
2230 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2231 struct device_node *port_dn = dp->dn;
2232 struct dsa_switch *ds = dp->ds;
2236 dp->pl_config.dev = &slave_dev->dev;
2237 dp->pl_config.type = PHYLINK_NETDEV;
2239 /* The get_fixed_state callback takes precedence over polling the
2240 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set
2241 * this if the switch provides such a callback.
2243 if (ds->ops->phylink_fixed_state) {
2244 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
2245 dp->pl_config.poll_fixed_state = true;
2248 ret = dsa_port_phylink_create(dp);
2252 if (ds->ops->get_phy_flags)
2253 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
2255 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
2256 if (ret == -ENODEV && ds->slave_mii_bus) {
2257 /* We could not connect to a designated PHY or SFP, so try to
2258 * use the switch internal MDIO bus instead
2260 ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
2263 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
2265 phylink_destroy(dp->pl);
2271 void dsa_slave_setup_tagger(struct net_device *slave)
2273 struct dsa_port *dp = dsa_slave_to_port(slave);
2274 struct dsa_slave_priv *p = netdev_priv(slave);
2275 const struct dsa_port *cpu_dp = dp->cpu_dp;
2276 struct net_device *master = cpu_dp->master;
2277 const struct dsa_switch *ds = dp->ds;
2279 slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
2280 slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
2281 /* Try to save one extra realloc later in the TX path (in the master)
2282 * by also inheriting the master's needed headroom and tailroom.
2283 * The 8021q driver also does this.
2285 slave->needed_headroom += master->needed_headroom;
2286 slave->needed_tailroom += master->needed_tailroom;
2288 p->xmit = cpu_dp->tag_ops->xmit;
2290 slave->features = master->vlan_features | NETIF_F_HW_TC;
2291 slave->hw_features |= NETIF_F_HW_TC;
2292 slave->features |= NETIF_F_LLTX;
2293 if (slave->needed_tailroom)
2294 slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
2295 if (ds->needs_standalone_vlan_filtering)
2296 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2299 int dsa_slave_suspend(struct net_device *slave_dev)
2301 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2303 if (!netif_running(slave_dev))
2306 netif_device_detach(slave_dev);
2309 phylink_stop(dp->pl);
2315 int dsa_slave_resume(struct net_device *slave_dev)
2317 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2319 if (!netif_running(slave_dev))
2322 netif_device_attach(slave_dev);
2325 phylink_start(dp->pl);
2331 int dsa_slave_create(struct dsa_port *port)
2333 const struct dsa_port *cpu_dp = port->cpu_dp;
2334 struct net_device *master = cpu_dp->master;
2335 struct dsa_switch *ds = port->ds;
2336 const char *name = port->name;
2337 struct net_device *slave_dev;
2338 struct dsa_slave_priv *p;
2341 if (!ds->num_tx_queues)
2342 ds->num_tx_queues = 1;
2344 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
2345 NET_NAME_UNKNOWN, ether_setup,
2346 ds->num_tx_queues, 1);
2347 if (slave_dev == NULL)
2350 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2351 #if IS_ENABLED(CONFIG_DCB)
2352 slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops;
2354 if (!is_zero_ether_addr(port->mac))
2355 eth_hw_addr_set(slave_dev, port->mac);
2357 eth_hw_addr_inherit(slave_dev, master);
2358 slave_dev->priv_flags |= IFF_NO_QUEUE;
2359 if (dsa_switch_supports_uc_filtering(ds))
2360 slave_dev->priv_flags |= IFF_UNICAST_FLT;
2361 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
2362 if (ds->ops->port_max_mtu)
2363 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2364 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
2366 SET_NETDEV_DEV(slave_dev, port->ds->dev);
2367 slave_dev->dev.of_node = port->dn;
2368 slave_dev->vlan_features = master->vlan_features;
2370 p = netdev_priv(slave_dev);
2371 slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2372 if (!slave_dev->tstats) {
2373 free_netdev(slave_dev);
2377 ret = gro_cells_init(&p->gcells, slave_dev);
2382 INIT_LIST_HEAD(&p->mall_tc_list);
2383 port->slave = slave_dev;
2384 dsa_slave_setup_tagger(slave_dev);
2386 netif_carrier_off(slave_dev);
2388 ret = dsa_slave_phy_setup(slave_dev);
2390 netdev_err(slave_dev,
2391 "error %d setting up PHY for tree %d, switch %d, port %d\n",
2392 ret, ds->dst->index, ds->index, port->index);
2398 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2399 if (ret && ret != -EOPNOTSUPP)
2400 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2401 ret, ETH_DATA_LEN, port->index);
2403 ret = register_netdevice(slave_dev);
2405 netdev_err(master, "error %d registering interface %s\n",
2406 ret, slave_dev->name);
2411 if (IS_ENABLED(CONFIG_DCB)) {
2412 ret = dsa_slave_dcbnl_init(slave_dev);
2414 netdev_err(slave_dev,
2415 "failed to initialize DCB: %pe\n",
2418 goto out_unregister;
2422 ret = netdev_upper_dev_link(master, slave_dev, NULL);
2427 goto out_unregister;
2432 unregister_netdev(slave_dev);
2435 phylink_disconnect_phy(p->dp->pl);
2437 phylink_destroy(p->dp->pl);
2439 gro_cells_destroy(&p->gcells);
2441 free_percpu(slave_dev->tstats);
2442 free_netdev(slave_dev);
2447 void dsa_slave_destroy(struct net_device *slave_dev)
2449 struct net_device *master = dsa_slave_to_master(slave_dev);
2450 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2451 struct dsa_slave_priv *p = netdev_priv(slave_dev);
2453 netif_carrier_off(slave_dev);
2455 netdev_upper_dev_unlink(master, slave_dev);
2456 unregister_netdevice(slave_dev);
2457 phylink_disconnect_phy(dp->pl);
2460 phylink_destroy(dp->pl);
2461 gro_cells_destroy(&p->gcells);
2462 free_percpu(slave_dev->tstats);
2463 free_netdev(slave_dev);
2466 bool dsa_slave_dev_check(const struct net_device *dev)
2468 return dev->netdev_ops == &dsa_slave_netdev_ops;
2470 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2472 static int dsa_slave_changeupper(struct net_device *dev,
2473 struct netdev_notifier_changeupper_info *info)
2475 struct dsa_port *dp = dsa_slave_to_port(dev);
2476 struct netlink_ext_ack *extack;
2477 int err = NOTIFY_DONE;
2479 extack = netdev_notifier_info_to_extack(&info->info);
2481 if (netif_is_bridge_master(info->upper_dev)) {
2482 if (info->linking) {
2483 err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2485 dsa_bridge_mtu_normalization(dp);
2486 if (err == -EOPNOTSUPP) {
2487 if (extack && !extack->_msg)
2488 NL_SET_ERR_MSG_MOD(extack,
2489 "Offloading not supported");
2492 err = notifier_from_errno(err);
2494 dsa_port_bridge_leave(dp, info->upper_dev);
2497 } else if (netif_is_lag_master(info->upper_dev)) {
2498 if (info->linking) {
2499 err = dsa_port_lag_join(dp, info->upper_dev,
2500 info->upper_info, extack);
2501 if (err == -EOPNOTSUPP) {
2502 NL_SET_ERR_MSG_MOD(info->info.extack,
2503 "Offloading not supported");
2506 err = notifier_from_errno(err);
2508 dsa_port_lag_leave(dp, info->upper_dev);
2511 } else if (is_hsr_master(info->upper_dev)) {
2512 if (info->linking) {
2513 err = dsa_port_hsr_join(dp, info->upper_dev);
2514 if (err == -EOPNOTSUPP) {
2515 NL_SET_ERR_MSG_MOD(info->info.extack,
2516 "Offloading not supported");
2519 err = notifier_from_errno(err);
2521 dsa_port_hsr_leave(dp, info->upper_dev);
2529 static int dsa_slave_prechangeupper(struct net_device *dev,
2530 struct netdev_notifier_changeupper_info *info)
2532 struct dsa_port *dp = dsa_slave_to_port(dev);
2534 if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2535 dsa_port_pre_bridge_leave(dp, info->upper_dev);
2536 else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2537 dsa_port_pre_lag_leave(dp, info->upper_dev);
2538 /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2539 * meaningfully enslaved to a bridge yet
2546 dsa_slave_lag_changeupper(struct net_device *dev,
2547 struct netdev_notifier_changeupper_info *info)
2549 struct net_device *lower;
2550 struct list_head *iter;
2551 int err = NOTIFY_DONE;
2552 struct dsa_port *dp;
2554 netdev_for_each_lower_dev(dev, lower, iter) {
2555 if (!dsa_slave_dev_check(lower))
2558 dp = dsa_slave_to_port(lower);
2563 err = dsa_slave_changeupper(lower, info);
2564 if (notifier_to_errno(err))
2571 /* Same as dsa_slave_lag_changeupper() except that it calls
2572 * dsa_slave_prechangeupper()
2575 dsa_slave_lag_prechangeupper(struct net_device *dev,
2576 struct netdev_notifier_changeupper_info *info)
2578 struct net_device *lower;
2579 struct list_head *iter;
2580 int err = NOTIFY_DONE;
2581 struct dsa_port *dp;
2583 netdev_for_each_lower_dev(dev, lower, iter) {
2584 if (!dsa_slave_dev_check(lower))
2587 dp = dsa_slave_to_port(lower);
2592 err = dsa_slave_prechangeupper(lower, info);
2593 if (notifier_to_errno(err))
2601 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2602 struct netdev_notifier_changeupper_info *info)
2604 struct netlink_ext_ack *ext_ack;
2605 struct net_device *slave, *br;
2606 struct dsa_port *dp;
2608 ext_ack = netdev_notifier_info_to_extack(&info->info);
2610 if (!is_vlan_dev(dev))
2613 slave = vlan_dev_real_dev(dev);
2614 if (!dsa_slave_dev_check(slave))
2617 dp = dsa_slave_to_port(slave);
2618 br = dsa_port_bridge_dev_get(dp);
2622 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
2623 if (br_vlan_enabled(br) &&
2624 netif_is_bridge_master(info->upper_dev) && info->linking) {
2625 NL_SET_ERR_MSG_MOD(ext_ack,
2626 "Cannot enslave VLAN device into VLAN aware bridge");
2627 return notifier_from_errno(-EINVAL);
2634 dsa_slave_check_8021q_upper(struct net_device *dev,
2635 struct netdev_notifier_changeupper_info *info)
2637 struct dsa_port *dp = dsa_slave_to_port(dev);
2638 struct net_device *br = dsa_port_bridge_dev_get(dp);
2639 struct bridge_vlan_info br_info;
2640 struct netlink_ext_ack *extack;
2641 int err = NOTIFY_DONE;
2644 if (!br || !br_vlan_enabled(br))
2647 extack = netdev_notifier_info_to_extack(&info->info);
2648 vid = vlan_dev_vlan_id(info->upper_dev);
2650 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2651 * device, respectively the VID is not found, returning
2652 * 0 means success, which is a failure for us here.
2654 err = br_vlan_get_info(br, vid, &br_info);
2656 NL_SET_ERR_MSG_MOD(extack,
2657 "This VLAN is already configured by the bridge");
2658 return notifier_from_errno(-EBUSY);
2665 dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
2666 struct netdev_notifier_changeupper_info *info)
2668 struct dsa_switch *ds;
2669 struct dsa_port *dp;
2672 if (!dsa_slave_dev_check(dev))
2673 return dsa_prevent_bridging_8021q_upper(dev, info);
2675 dp = dsa_slave_to_port(dev);
2678 if (ds->ops->port_prechangeupper) {
2679 err = ds->ops->port_prechangeupper(ds, dp->index, info);
2681 return notifier_from_errno(err);
2684 if (is_vlan_dev(info->upper_dev))
2685 return dsa_slave_check_8021q_upper(dev, info);
2690 static int dsa_slave_netdevice_event(struct notifier_block *nb,
2691 unsigned long event, void *ptr)
2693 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2696 case NETDEV_PRECHANGEUPPER: {
2697 struct netdev_notifier_changeupper_info *info = ptr;
2700 err = dsa_slave_prechangeupper_sanity_check(dev, info);
2701 if (err != NOTIFY_DONE)
2704 if (dsa_slave_dev_check(dev))
2705 return dsa_slave_prechangeupper(dev, ptr);
2707 if (netif_is_lag_master(dev))
2708 return dsa_slave_lag_prechangeupper(dev, ptr);
2712 case NETDEV_CHANGEUPPER:
2713 if (dsa_slave_dev_check(dev))
2714 return dsa_slave_changeupper(dev, ptr);
2716 if (netif_is_lag_master(dev))
2717 return dsa_slave_lag_changeupper(dev, ptr);
2720 case NETDEV_CHANGELOWERSTATE: {
2721 struct netdev_notifier_changelowerstate_info *info = ptr;
2722 struct dsa_port *dp;
2725 if (!dsa_slave_dev_check(dev))
2728 dp = dsa_slave_to_port(dev);
2730 err = dsa_port_lag_change(dp, info->lower_state_info);
2731 return notifier_from_errno(err);
2735 /* Track state of master port.
2736 * DSA driver may require the master port (and indirectly
2737 * the tagger) to be available for some special operation.
2739 if (netdev_uses_dsa(dev)) {
2740 struct dsa_port *cpu_dp = dev->dsa_ptr;
2741 struct dsa_switch_tree *dst = cpu_dp->ds->dst;
2743 /* Track when the master port is UP */
2744 dsa_tree_master_oper_state_change(dst, dev,
2745 netif_oper_up(dev));
2747 /* Track when the master port is ready and can accept
2749 * NETDEV_UP event is not enough to flag a port as ready.
2750 * We also have to wait for linkwatch_do_dev to dev_activate
2751 * and emit a NETDEV_CHANGE event.
2752 * We check if a master port is ready by checking if the dev
2753 * have a qdisc assigned and is not noop.
2755 dsa_tree_master_admin_state_change(dst, dev,
2756 !qdisc_tx_is_noop(dev));
2763 case NETDEV_GOING_DOWN: {
2764 struct dsa_port *dp, *cpu_dp;
2765 struct dsa_switch_tree *dst;
2766 LIST_HEAD(close_list);
2768 if (!netdev_uses_dsa(dev))
2771 cpu_dp = dev->dsa_ptr;
2772 dst = cpu_dp->ds->dst;
2774 dsa_tree_master_admin_state_change(dst, dev, false);
2776 list_for_each_entry(dp, &dst->ports, list) {
2777 if (!dsa_port_is_user(dp))
2780 list_add(&dp->slave->close_list, &close_list);
2783 dev_close_many(&close_list, true);
2795 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2797 struct switchdev_notifier_fdb_info info = {};
2799 info.addr = switchdev_work->addr;
2800 info.vid = switchdev_work->vid;
2801 info.offloaded = true;
2802 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2803 switchdev_work->orig_dev, &info.info, NULL);
2806 static void dsa_slave_switchdev_event_work(struct work_struct *work)
2808 struct dsa_switchdev_event_work *switchdev_work =
2809 container_of(work, struct dsa_switchdev_event_work, work);
2810 const unsigned char *addr = switchdev_work->addr;
2811 struct net_device *dev = switchdev_work->dev;
2812 u16 vid = switchdev_work->vid;
2813 struct dsa_switch *ds;
2814 struct dsa_port *dp;
2817 dp = dsa_slave_to_port(dev);
2820 switch (switchdev_work->event) {
2821 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2822 if (switchdev_work->host_addr)
2823 err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
2825 err = dsa_port_lag_fdb_add(dp, addr, vid);
2827 err = dsa_port_fdb_add(dp, addr, vid);
2830 "port %d failed to add %pM vid %d to fdb: %d\n",
2831 dp->index, addr, vid, err);
2834 dsa_fdb_offload_notify(switchdev_work);
2837 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2838 if (switchdev_work->host_addr)
2839 err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
2841 err = dsa_port_lag_fdb_del(dp, addr, vid);
2843 err = dsa_port_fdb_del(dp, addr, vid);
2846 "port %d failed to delete %pM vid %d from fdb: %d\n",
2847 dp->index, addr, vid, err);
2853 kfree(switchdev_work);
2856 static bool dsa_foreign_dev_check(const struct net_device *dev,
2857 const struct net_device *foreign_dev)
2859 const struct dsa_port *dp = dsa_slave_to_port(dev);
2860 struct dsa_switch_tree *dst = dp->ds->dst;
2862 if (netif_is_bridge_master(foreign_dev))
2863 return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
2865 if (netif_is_bridge_port(foreign_dev))
2866 return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2868 /* Everything else is foreign */
2872 static int dsa_slave_fdb_event(struct net_device *dev,
2873 struct net_device *orig_dev,
2874 unsigned long event, const void *ctx,
2875 const struct switchdev_notifier_fdb_info *fdb_info)
2877 struct dsa_switchdev_event_work *switchdev_work;
2878 struct dsa_port *dp = dsa_slave_to_port(dev);
2879 bool host_addr = fdb_info->is_local;
2880 struct dsa_switch *ds = dp->ds;
2882 if (ctx && ctx != dp)
2888 if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
2889 if (dsa_port_offloads_bridge_port(dp, orig_dev))
2892 /* FDB entries learned by the software bridge or by foreign
2893 * bridge ports should be installed as host addresses only if
2894 * the driver requests assisted learning.
2896 if (!ds->assisted_learning_on_cpu_port)
2900 /* Also treat FDB entries on foreign interfaces bridged with us as host
2903 if (dsa_foreign_dev_check(dev, orig_dev))
2906 /* Check early that we're not doing work in vain.
2907 * Host addresses on LAG ports still require regular FDB ops,
2908 * since the CPU port isn't in a LAG.
2910 if (dp->lag && !host_addr) {
2911 if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
2914 if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2918 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2919 if (!switchdev_work)
2922 netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2923 event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2924 orig_dev->name, fdb_info->addr, fdb_info->vid,
2925 host_addr ? " as host address" : "");
2927 INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2928 switchdev_work->event = event;
2929 switchdev_work->dev = dev;
2930 switchdev_work->orig_dev = orig_dev;
2932 ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2933 switchdev_work->vid = fdb_info->vid;
2934 switchdev_work->host_addr = host_addr;
2936 dsa_schedule_work(&switchdev_work->work);
2941 /* Called under rcu_read_lock() */
2942 static int dsa_slave_switchdev_event(struct notifier_block *unused,
2943 unsigned long event, void *ptr)
2945 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2949 case SWITCHDEV_PORT_ATTR_SET:
2950 err = switchdev_handle_port_attr_set(dev, ptr,
2951 dsa_slave_dev_check,
2952 dsa_slave_port_attr_set);
2953 return notifier_from_errno(err);
2954 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2955 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2956 err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
2957 dsa_slave_dev_check,
2958 dsa_foreign_dev_check,
2959 dsa_slave_fdb_event);
2960 return notifier_from_errno(err);
2968 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2969 unsigned long event, void *ptr)
2971 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2975 case SWITCHDEV_PORT_OBJ_ADD:
2976 err = switchdev_handle_port_obj_add_foreign(dev, ptr,
2977 dsa_slave_dev_check,
2978 dsa_foreign_dev_check,
2979 dsa_slave_port_obj_add);
2980 return notifier_from_errno(err);
2981 case SWITCHDEV_PORT_OBJ_DEL:
2982 err = switchdev_handle_port_obj_del_foreign(dev, ptr,
2983 dsa_slave_dev_check,
2984 dsa_foreign_dev_check,
2985 dsa_slave_port_obj_del);
2986 return notifier_from_errno(err);
2987 case SWITCHDEV_PORT_ATTR_SET:
2988 err = switchdev_handle_port_attr_set(dev, ptr,
2989 dsa_slave_dev_check,
2990 dsa_slave_port_attr_set);
2991 return notifier_from_errno(err);
2997 static struct notifier_block dsa_slave_nb __read_mostly = {
2998 .notifier_call = dsa_slave_netdevice_event,
3001 struct notifier_block dsa_slave_switchdev_notifier = {
3002 .notifier_call = dsa_slave_switchdev_event,
3005 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
3006 .notifier_call = dsa_slave_switchdev_blocking_event,
3009 int dsa_slave_register_notifier(void)
3011 struct notifier_block *nb;
3014 err = register_netdevice_notifier(&dsa_slave_nb);
3018 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
3020 goto err_switchdev_nb;
3022 nb = &dsa_slave_switchdev_blocking_notifier;
3023 err = register_switchdev_blocking_notifier(nb);
3025 goto err_switchdev_blocking_nb;
3029 err_switchdev_blocking_nb:
3030 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3032 unregister_netdevice_notifier(&dsa_slave_nb);
3036 void dsa_slave_unregister_notifier(void)
3038 struct notifier_block *nb;
3041 nb = &dsa_slave_switchdev_blocking_notifier;
3042 err = unregister_switchdev_blocking_notifier(nb);
3044 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
3046 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3048 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3050 err = unregister_netdevice_notifier(&dsa_slave_nb);
3052 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);