1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/dsa/user.c - user 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>
24 #include <linux/string.h>
34 struct dsa_switchdev_event_work {
35 struct net_device *dev;
36 struct net_device *orig_dev;
37 struct work_struct work;
39 /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
40 * SWITCHDEV_FDB_DEL_TO_DEVICE
42 unsigned char addr[ETH_ALEN];
47 enum dsa_standalone_event {
54 struct dsa_standalone_event_work {
55 struct work_struct work;
56 struct net_device *dev;
57 enum dsa_standalone_event event;
58 unsigned char addr[ETH_ALEN];
62 struct dsa_host_vlan_rx_filtering_ctx {
63 struct net_device *dev;
64 const unsigned char *addr;
65 enum dsa_standalone_event event;
68 static bool dsa_switch_supports_uc_filtering(struct dsa_switch *ds)
70 return ds->ops->port_fdb_add && ds->ops->port_fdb_del &&
71 ds->fdb_isolation && !ds->vlan_filtering_is_global &&
72 !ds->needs_standalone_vlan_filtering;
75 static bool dsa_switch_supports_mc_filtering(struct dsa_switch *ds)
77 return ds->ops->port_mdb_add && ds->ops->port_mdb_del &&
78 ds->fdb_isolation && !ds->vlan_filtering_is_global &&
79 !ds->needs_standalone_vlan_filtering;
82 static void dsa_user_standalone_event_work(struct work_struct *work)
84 struct dsa_standalone_event_work *standalone_work =
85 container_of(work, struct dsa_standalone_event_work, work);
86 const unsigned char *addr = standalone_work->addr;
87 struct net_device *dev = standalone_work->dev;
88 struct dsa_port *dp = dsa_user_to_port(dev);
89 struct switchdev_obj_port_mdb mdb;
90 struct dsa_switch *ds = dp->ds;
91 u16 vid = standalone_work->vid;
94 switch (standalone_work->event) {
96 err = dsa_port_standalone_host_fdb_add(dp, addr, vid);
99 "port %d failed to add %pM vid %d to fdb: %d\n",
100 dp->index, addr, vid, err);
106 err = dsa_port_standalone_host_fdb_del(dp, addr, vid);
109 "port %d failed to delete %pM vid %d from fdb: %d\n",
110 dp->index, addr, vid, err);
115 ether_addr_copy(mdb.addr, addr);
118 err = dsa_port_standalone_host_mdb_add(dp, &mdb);
121 "port %d failed to add %pM vid %d to mdb: %d\n",
122 dp->index, addr, vid, err);
127 ether_addr_copy(mdb.addr, addr);
130 err = dsa_port_standalone_host_mdb_del(dp, &mdb);
133 "port %d failed to delete %pM vid %d from mdb: %d\n",
134 dp->index, addr, vid, err);
140 kfree(standalone_work);
143 static int dsa_user_schedule_standalone_work(struct net_device *dev,
144 enum dsa_standalone_event event,
145 const unsigned char *addr,
148 struct dsa_standalone_event_work *standalone_work;
150 standalone_work = kzalloc(sizeof(*standalone_work), GFP_ATOMIC);
151 if (!standalone_work)
154 INIT_WORK(&standalone_work->work, dsa_user_standalone_event_work);
155 standalone_work->event = event;
156 standalone_work->dev = dev;
158 ether_addr_copy(standalone_work->addr, addr);
159 standalone_work->vid = vid;
161 dsa_schedule_work(&standalone_work->work);
166 static int dsa_user_host_vlan_rx_filtering(void *arg, int vid)
168 struct dsa_host_vlan_rx_filtering_ctx *ctx = arg;
170 return dsa_user_schedule_standalone_work(ctx->dev, ctx->event,
174 static int dsa_user_vlan_for_each(struct net_device *dev,
175 int (*cb)(void *arg, int vid), void *arg)
177 struct dsa_port *dp = dsa_user_to_port(dev);
181 lockdep_assert_held(&dev->addr_list_lock);
187 list_for_each_entry(v, &dp->user_vlans, list) {
188 err = cb(arg, v->vid);
196 static int dsa_user_sync_uc(struct net_device *dev,
197 const unsigned char *addr)
199 struct net_device *conduit = dsa_user_to_conduit(dev);
200 struct dsa_port *dp = dsa_user_to_port(dev);
201 struct dsa_host_vlan_rx_filtering_ctx ctx = {
207 dev_uc_add(conduit, addr);
209 if (!dsa_switch_supports_uc_filtering(dp->ds))
212 return dsa_user_vlan_for_each(dev, dsa_user_host_vlan_rx_filtering,
216 static int dsa_user_unsync_uc(struct net_device *dev,
217 const unsigned char *addr)
219 struct net_device *conduit = dsa_user_to_conduit(dev);
220 struct dsa_port *dp = dsa_user_to_port(dev);
221 struct dsa_host_vlan_rx_filtering_ctx ctx = {
227 dev_uc_del(conduit, addr);
229 if (!dsa_switch_supports_uc_filtering(dp->ds))
232 return dsa_user_vlan_for_each(dev, dsa_user_host_vlan_rx_filtering,
236 static int dsa_user_sync_mc(struct net_device *dev,
237 const unsigned char *addr)
239 struct net_device *conduit = dsa_user_to_conduit(dev);
240 struct dsa_port *dp = dsa_user_to_port(dev);
241 struct dsa_host_vlan_rx_filtering_ctx ctx = {
247 dev_mc_add(conduit, addr);
249 if (!dsa_switch_supports_mc_filtering(dp->ds))
252 return dsa_user_vlan_for_each(dev, dsa_user_host_vlan_rx_filtering,
256 static int dsa_user_unsync_mc(struct net_device *dev,
257 const unsigned char *addr)
259 struct net_device *conduit = dsa_user_to_conduit(dev);
260 struct dsa_port *dp = dsa_user_to_port(dev);
261 struct dsa_host_vlan_rx_filtering_ctx ctx = {
267 dev_mc_del(conduit, addr);
269 if (!dsa_switch_supports_mc_filtering(dp->ds))
272 return dsa_user_vlan_for_each(dev, dsa_user_host_vlan_rx_filtering,
276 void dsa_user_sync_ha(struct net_device *dev)
278 struct dsa_port *dp = dsa_user_to_port(dev);
279 struct dsa_switch *ds = dp->ds;
280 struct netdev_hw_addr *ha;
282 netif_addr_lock_bh(dev);
284 netdev_for_each_synced_mc_addr(ha, dev)
285 dsa_user_sync_mc(dev, ha->addr);
287 netdev_for_each_synced_uc_addr(ha, dev)
288 dsa_user_sync_uc(dev, ha->addr);
290 netif_addr_unlock_bh(dev);
292 if (dsa_switch_supports_uc_filtering(ds) ||
293 dsa_switch_supports_mc_filtering(ds))
294 dsa_flush_workqueue();
297 void dsa_user_unsync_ha(struct net_device *dev)
299 struct dsa_port *dp = dsa_user_to_port(dev);
300 struct dsa_switch *ds = dp->ds;
301 struct netdev_hw_addr *ha;
303 netif_addr_lock_bh(dev);
305 netdev_for_each_synced_uc_addr(ha, dev)
306 dsa_user_unsync_uc(dev, ha->addr);
308 netdev_for_each_synced_mc_addr(ha, dev)
309 dsa_user_unsync_mc(dev, ha->addr);
311 netif_addr_unlock_bh(dev);
313 if (dsa_switch_supports_uc_filtering(ds) ||
314 dsa_switch_supports_mc_filtering(ds))
315 dsa_flush_workqueue();
318 /* user mii_bus handling ***************************************************/
319 static int dsa_user_phy_read(struct mii_bus *bus, int addr, int reg)
321 struct dsa_switch *ds = bus->priv;
323 if (ds->phys_mii_mask & (1 << addr))
324 return ds->ops->phy_read(ds, addr, reg);
329 static int dsa_user_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
331 struct dsa_switch *ds = bus->priv;
333 if (ds->phys_mii_mask & (1 << addr))
334 return ds->ops->phy_write(ds, addr, reg, val);
339 void dsa_user_mii_bus_init(struct dsa_switch *ds)
341 ds->user_mii_bus->priv = (void *)ds;
342 ds->user_mii_bus->name = "dsa user smi";
343 ds->user_mii_bus->read = dsa_user_phy_read;
344 ds->user_mii_bus->write = dsa_user_phy_write;
345 snprintf(ds->user_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
346 ds->dst->index, ds->index);
347 ds->user_mii_bus->parent = ds->dev;
348 ds->user_mii_bus->phy_mask = ~ds->phys_mii_mask;
352 /* user device handling ****************************************************/
353 static int dsa_user_get_iflink(const struct net_device *dev)
355 return dsa_user_to_conduit(dev)->ifindex;
358 static int dsa_user_open(struct net_device *dev)
360 struct net_device *conduit = dsa_user_to_conduit(dev);
361 struct dsa_port *dp = dsa_user_to_port(dev);
362 struct dsa_switch *ds = dp->ds;
365 err = dev_open(conduit, NULL);
367 netdev_err(dev, "failed to open conduit %s\n", conduit->name);
371 if (dsa_switch_supports_uc_filtering(ds)) {
372 err = dsa_port_standalone_host_fdb_add(dp, dev->dev_addr, 0);
377 if (!ether_addr_equal(dev->dev_addr, conduit->dev_addr)) {
378 err = dev_uc_add(conduit, dev->dev_addr);
383 err = dsa_port_enable_rt(dp, dev->phydev);
390 if (!ether_addr_equal(dev->dev_addr, conduit->dev_addr))
391 dev_uc_del(conduit, dev->dev_addr);
393 if (dsa_switch_supports_uc_filtering(ds))
394 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
399 static int dsa_user_close(struct net_device *dev)
401 struct net_device *conduit = dsa_user_to_conduit(dev);
402 struct dsa_port *dp = dsa_user_to_port(dev);
403 struct dsa_switch *ds = dp->ds;
405 dsa_port_disable_rt(dp);
407 if (!ether_addr_equal(dev->dev_addr, conduit->dev_addr))
408 dev_uc_del(conduit, dev->dev_addr);
410 if (dsa_switch_supports_uc_filtering(ds))
411 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
416 static void dsa_user_manage_host_flood(struct net_device *dev)
418 bool mc = dev->flags & (IFF_PROMISC | IFF_ALLMULTI);
419 struct dsa_port *dp = dsa_user_to_port(dev);
420 bool uc = dev->flags & IFF_PROMISC;
422 dsa_port_set_host_flood(dp, uc, mc);
425 static void dsa_user_change_rx_flags(struct net_device *dev, int change)
427 struct net_device *conduit = dsa_user_to_conduit(dev);
428 struct dsa_port *dp = dsa_user_to_port(dev);
429 struct dsa_switch *ds = dp->ds;
431 if (change & IFF_ALLMULTI)
432 dev_set_allmulti(conduit,
433 dev->flags & IFF_ALLMULTI ? 1 : -1);
434 if (change & IFF_PROMISC)
435 dev_set_promiscuity(conduit,
436 dev->flags & IFF_PROMISC ? 1 : -1);
438 if (dsa_switch_supports_uc_filtering(ds) &&
439 dsa_switch_supports_mc_filtering(ds))
440 dsa_user_manage_host_flood(dev);
443 static void dsa_user_set_rx_mode(struct net_device *dev)
445 __dev_mc_sync(dev, dsa_user_sync_mc, dsa_user_unsync_mc);
446 __dev_uc_sync(dev, dsa_user_sync_uc, dsa_user_unsync_uc);
449 static int dsa_user_set_mac_address(struct net_device *dev, void *a)
451 struct net_device *conduit = dsa_user_to_conduit(dev);
452 struct dsa_port *dp = dsa_user_to_port(dev);
453 struct dsa_switch *ds = dp->ds;
454 struct sockaddr *addr = a;
457 if (!is_valid_ether_addr(addr->sa_data))
458 return -EADDRNOTAVAIL;
460 if (ds->ops->port_set_mac_address) {
461 err = ds->ops->port_set_mac_address(ds, dp->index,
467 /* If the port is down, the address isn't synced yet to hardware or
468 * to the DSA conduit, so there is nothing to change.
470 if (!(dev->flags & IFF_UP))
471 goto out_change_dev_addr;
473 if (dsa_switch_supports_uc_filtering(ds)) {
474 err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0);
479 if (!ether_addr_equal(addr->sa_data, conduit->dev_addr)) {
480 err = dev_uc_add(conduit, addr->sa_data);
485 if (!ether_addr_equal(dev->dev_addr, conduit->dev_addr))
486 dev_uc_del(conduit, dev->dev_addr);
488 if (dsa_switch_supports_uc_filtering(ds))
489 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
492 eth_hw_addr_set(dev, addr->sa_data);
497 if (dsa_switch_supports_uc_filtering(ds))
498 dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0);
503 struct dsa_user_dump_ctx {
504 struct net_device *dev;
506 struct netlink_callback *cb;
511 dsa_user_port_fdb_do_dump(const unsigned char *addr, u16 vid,
512 bool is_static, void *data)
514 struct dsa_user_dump_ctx *dump = data;
515 u32 portid = NETLINK_CB(dump->cb->skb).portid;
516 u32 seq = dump->cb->nlh->nlmsg_seq;
517 struct nlmsghdr *nlh;
520 if (dump->idx < dump->cb->args[2])
523 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
524 sizeof(*ndm), NLM_F_MULTI);
528 ndm = nlmsg_data(nlh);
529 ndm->ndm_family = AF_BRIDGE;
532 ndm->ndm_flags = NTF_SELF;
534 ndm->ndm_ifindex = dump->dev->ifindex;
535 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
537 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
538 goto nla_put_failure;
540 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
541 goto nla_put_failure;
543 nlmsg_end(dump->skb, nlh);
550 nlmsg_cancel(dump->skb, nlh);
555 dsa_user_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
556 struct net_device *dev, struct net_device *filter_dev,
559 struct dsa_port *dp = dsa_user_to_port(dev);
560 struct dsa_user_dump_ctx dump = {
568 err = dsa_port_fdb_dump(dp, dsa_user_port_fdb_do_dump, &dump);
574 static int dsa_user_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
576 struct dsa_user_priv *p = netdev_priv(dev);
577 struct dsa_switch *ds = p->dp->ds;
578 int port = p->dp->index;
580 /* Pass through to switch driver if it supports timestamping */
583 if (ds->ops->port_hwtstamp_get)
584 return ds->ops->port_hwtstamp_get(ds, port, ifr);
587 if (ds->ops->port_hwtstamp_set)
588 return ds->ops->port_hwtstamp_set(ds, port, ifr);
592 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
595 static int dsa_user_port_attr_set(struct net_device *dev, const void *ctx,
596 const struct switchdev_attr *attr,
597 struct netlink_ext_ack *extack)
599 struct dsa_port *dp = dsa_user_to_port(dev);
602 if (ctx && ctx != dp)
606 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
607 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
610 ret = dsa_port_set_state(dp, attr->u.stp_state, true);
612 case SWITCHDEV_ATTR_ID_PORT_MST_STATE:
613 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
616 ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack);
618 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
619 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
622 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
625 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
626 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
629 ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
631 case SWITCHDEV_ATTR_ID_BRIDGE_MST:
632 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
635 ret = dsa_port_mst_enable(dp, attr->u.mst, extack);
637 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
638 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
641 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
644 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
645 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
648 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
650 case SWITCHDEV_ATTR_ID_VLAN_MSTI:
651 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
654 ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti);
664 /* Must be called under rcu_read_lock() */
666 dsa_user_vlan_check_for_8021q_uppers(struct net_device *user,
667 const struct switchdev_obj_port_vlan *vlan)
669 struct net_device *upper_dev;
670 struct list_head *iter;
672 netdev_for_each_upper_dev_rcu(user, upper_dev, iter) {
675 if (!is_vlan_dev(upper_dev))
678 vid = vlan_dev_vlan_id(upper_dev);
679 if (vid == vlan->vid)
686 static int dsa_user_vlan_add(struct net_device *dev,
687 const struct switchdev_obj *obj,
688 struct netlink_ext_ack *extack)
690 struct dsa_port *dp = dsa_user_to_port(dev);
691 struct switchdev_obj_port_vlan *vlan;
694 if (dsa_port_skip_vlan_configuration(dp)) {
695 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
699 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
701 /* Deny adding a bridge VLAN when there is already an 802.1Q upper with
704 if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
706 err = dsa_user_vlan_check_for_8021q_uppers(dev, vlan);
709 NL_SET_ERR_MSG_MOD(extack,
710 "Port already has a VLAN upper with this VID");
715 return dsa_port_vlan_add(dp, vlan, extack);
718 /* Offload a VLAN installed on the bridge or on a foreign interface by
719 * installing it as a VLAN towards the CPU port.
721 static int dsa_user_host_vlan_add(struct net_device *dev,
722 const struct switchdev_obj *obj,
723 struct netlink_ext_ack *extack)
725 struct dsa_port *dp = dsa_user_to_port(dev);
726 struct switchdev_obj_port_vlan vlan;
728 /* Do nothing if this is a software bridge */
732 if (dsa_port_skip_vlan_configuration(dp)) {
733 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
737 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
739 /* Even though drivers often handle CPU membership in special ways,
740 * it doesn't make sense to program a PVID, so clear this flag.
742 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
744 return dsa_port_host_vlan_add(dp, &vlan, extack);
747 static int dsa_user_port_obj_add(struct net_device *dev, const void *ctx,
748 const struct switchdev_obj *obj,
749 struct netlink_ext_ack *extack)
751 struct dsa_port *dp = dsa_user_to_port(dev);
754 if (ctx && ctx != dp)
758 case SWITCHDEV_OBJ_ID_PORT_MDB:
759 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
762 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
764 case SWITCHDEV_OBJ_ID_HOST_MDB:
765 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
768 err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
770 case SWITCHDEV_OBJ_ID_PORT_VLAN:
771 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
772 err = dsa_user_vlan_add(dev, obj, extack);
774 err = dsa_user_host_vlan_add(dev, obj, extack);
776 case SWITCHDEV_OBJ_ID_MRP:
777 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
780 err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
782 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
783 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
786 err = dsa_port_mrp_add_ring_role(dp,
787 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
797 static int dsa_user_vlan_del(struct net_device *dev,
798 const struct switchdev_obj *obj)
800 struct dsa_port *dp = dsa_user_to_port(dev);
801 struct switchdev_obj_port_vlan *vlan;
803 if (dsa_port_skip_vlan_configuration(dp))
806 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
808 return dsa_port_vlan_del(dp, vlan);
811 static int dsa_user_host_vlan_del(struct net_device *dev,
812 const struct switchdev_obj *obj)
814 struct dsa_port *dp = dsa_user_to_port(dev);
815 struct switchdev_obj_port_vlan *vlan;
817 /* Do nothing if this is a software bridge */
821 if (dsa_port_skip_vlan_configuration(dp))
824 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
826 return dsa_port_host_vlan_del(dp, vlan);
829 static int dsa_user_port_obj_del(struct net_device *dev, const void *ctx,
830 const struct switchdev_obj *obj)
832 struct dsa_port *dp = dsa_user_to_port(dev);
835 if (ctx && ctx != dp)
839 case SWITCHDEV_OBJ_ID_PORT_MDB:
840 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
843 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
845 case SWITCHDEV_OBJ_ID_HOST_MDB:
846 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
849 err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
851 case SWITCHDEV_OBJ_ID_PORT_VLAN:
852 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
853 err = dsa_user_vlan_del(dev, obj);
855 err = dsa_user_host_vlan_del(dev, obj);
857 case SWITCHDEV_OBJ_ID_MRP:
858 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
861 err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
863 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
864 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
867 err = dsa_port_mrp_del_ring_role(dp,
868 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
878 static inline netdev_tx_t dsa_user_netpoll_send_skb(struct net_device *dev,
881 #ifdef CONFIG_NET_POLL_CONTROLLER
882 struct dsa_user_priv *p = netdev_priv(dev);
884 return netpoll_send_skb(p->netpoll, skb);
891 static void dsa_skb_tx_timestamp(struct dsa_user_priv *p,
894 struct dsa_switch *ds = p->dp->ds;
896 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
899 if (!ds->ops->port_txtstamp)
902 ds->ops->port_txtstamp(ds, p->dp->index, skb);
905 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
907 /* SKB for netpoll still need to be mangled with the protocol-specific
908 * tag to be successfully transmitted
910 if (unlikely(netpoll_tx_running(dev)))
911 return dsa_user_netpoll_send_skb(dev, skb);
913 /* Queue the SKB for transmission on the parent interface, but
914 * do not modify its EtherType
916 skb->dev = dsa_user_to_conduit(dev);
921 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
923 static netdev_tx_t dsa_user_xmit(struct sk_buff *skb, struct net_device *dev)
925 struct dsa_user_priv *p = netdev_priv(dev);
926 struct sk_buff *nskb;
928 dev_sw_netstats_tx_add(dev, 1, skb->len);
930 memset(skb->cb, 0, sizeof(skb->cb));
932 /* Handle tx timestamp if any */
933 dsa_skb_tx_timestamp(p, skb);
935 if (skb_ensure_writable_head_tail(skb, dev)) {
936 dev_kfree_skb_any(skb);
940 /* needed_tailroom should still be 'warm' in the cache line from
941 * skb_ensure_writable_head_tail(), which has also ensured that
944 if (dev->needed_tailroom)
947 /* Transmit function may have to reallocate the original SKB,
948 * in which case it must have freed it. Only free it here on error.
950 nskb = p->xmit(skb, dev);
956 return dsa_enqueue_skb(nskb, dev);
959 /* ethtool operations *******************************************************/
961 static void dsa_user_get_drvinfo(struct net_device *dev,
962 struct ethtool_drvinfo *drvinfo)
964 strscpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
965 strscpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
966 strscpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
969 static int dsa_user_get_regs_len(struct net_device *dev)
971 struct dsa_port *dp = dsa_user_to_port(dev);
972 struct dsa_switch *ds = dp->ds;
974 if (ds->ops->get_regs_len)
975 return ds->ops->get_regs_len(ds, dp->index);
981 dsa_user_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
983 struct dsa_port *dp = dsa_user_to_port(dev);
984 struct dsa_switch *ds = dp->ds;
986 if (ds->ops->get_regs)
987 ds->ops->get_regs(ds, dp->index, regs, _p);
990 static int dsa_user_nway_reset(struct net_device *dev)
992 struct dsa_port *dp = dsa_user_to_port(dev);
994 return phylink_ethtool_nway_reset(dp->pl);
997 static int dsa_user_get_eeprom_len(struct net_device *dev)
999 struct dsa_port *dp = dsa_user_to_port(dev);
1000 struct dsa_switch *ds = dp->ds;
1002 if (ds->cd && ds->cd->eeprom_len)
1003 return ds->cd->eeprom_len;
1005 if (ds->ops->get_eeprom_len)
1006 return ds->ops->get_eeprom_len(ds);
1011 static int dsa_user_get_eeprom(struct net_device *dev,
1012 struct ethtool_eeprom *eeprom, u8 *data)
1014 struct dsa_port *dp = dsa_user_to_port(dev);
1015 struct dsa_switch *ds = dp->ds;
1017 if (ds->ops->get_eeprom)
1018 return ds->ops->get_eeprom(ds, eeprom, data);
1023 static int dsa_user_set_eeprom(struct net_device *dev,
1024 struct ethtool_eeprom *eeprom, u8 *data)
1026 struct dsa_port *dp = dsa_user_to_port(dev);
1027 struct dsa_switch *ds = dp->ds;
1029 if (ds->ops->set_eeprom)
1030 return ds->ops->set_eeprom(ds, eeprom, data);
1035 static void dsa_user_get_strings(struct net_device *dev,
1036 uint32_t stringset, uint8_t *data)
1038 struct dsa_port *dp = dsa_user_to_port(dev);
1039 struct dsa_switch *ds = dp->ds;
1041 if (stringset == ETH_SS_STATS) {
1042 int len = ETH_GSTRING_LEN;
1044 strscpy_pad(data, "tx_packets", len);
1045 strscpy_pad(data + len, "tx_bytes", len);
1046 strscpy_pad(data + 2 * len, "rx_packets", len);
1047 strscpy_pad(data + 3 * len, "rx_bytes", len);
1048 if (ds->ops->get_strings)
1049 ds->ops->get_strings(ds, dp->index, stringset,
1051 } else if (stringset == ETH_SS_TEST) {
1052 net_selftest_get_strings(data);
1057 static void dsa_user_get_ethtool_stats(struct net_device *dev,
1058 struct ethtool_stats *stats,
1061 struct dsa_port *dp = dsa_user_to_port(dev);
1062 struct dsa_switch *ds = dp->ds;
1063 struct pcpu_sw_netstats *s;
1067 for_each_possible_cpu(i) {
1068 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
1070 s = per_cpu_ptr(dev->tstats, i);
1072 start = u64_stats_fetch_begin(&s->syncp);
1073 tx_packets = u64_stats_read(&s->tx_packets);
1074 tx_bytes = u64_stats_read(&s->tx_bytes);
1075 rx_packets = u64_stats_read(&s->rx_packets);
1076 rx_bytes = u64_stats_read(&s->rx_bytes);
1077 } while (u64_stats_fetch_retry(&s->syncp, start));
1078 data[0] += tx_packets;
1079 data[1] += tx_bytes;
1080 data[2] += rx_packets;
1081 data[3] += rx_bytes;
1083 if (ds->ops->get_ethtool_stats)
1084 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
1087 static int dsa_user_get_sset_count(struct net_device *dev, int sset)
1089 struct dsa_port *dp = dsa_user_to_port(dev);
1090 struct dsa_switch *ds = dp->ds;
1092 if (sset == ETH_SS_STATS) {
1095 if (ds->ops->get_sset_count) {
1096 count = ds->ops->get_sset_count(ds, dp->index, sset);
1102 } else if (sset == ETH_SS_TEST) {
1103 return net_selftest_get_count();
1109 static void dsa_user_get_eth_phy_stats(struct net_device *dev,
1110 struct ethtool_eth_phy_stats *phy_stats)
1112 struct dsa_port *dp = dsa_user_to_port(dev);
1113 struct dsa_switch *ds = dp->ds;
1115 if (ds->ops->get_eth_phy_stats)
1116 ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
1119 static void dsa_user_get_eth_mac_stats(struct net_device *dev,
1120 struct ethtool_eth_mac_stats *mac_stats)
1122 struct dsa_port *dp = dsa_user_to_port(dev);
1123 struct dsa_switch *ds = dp->ds;
1125 if (ds->ops->get_eth_mac_stats)
1126 ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
1130 dsa_user_get_eth_ctrl_stats(struct net_device *dev,
1131 struct ethtool_eth_ctrl_stats *ctrl_stats)
1133 struct dsa_port *dp = dsa_user_to_port(dev);
1134 struct dsa_switch *ds = dp->ds;
1136 if (ds->ops->get_eth_ctrl_stats)
1137 ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1141 dsa_user_get_rmon_stats(struct net_device *dev,
1142 struct ethtool_rmon_stats *rmon_stats,
1143 const struct ethtool_rmon_hist_range **ranges)
1145 struct dsa_port *dp = dsa_user_to_port(dev);
1146 struct dsa_switch *ds = dp->ds;
1148 if (ds->ops->get_rmon_stats)
1149 ds->ops->get_rmon_stats(ds, dp->index, rmon_stats, ranges);
1152 static void dsa_user_net_selftest(struct net_device *ndev,
1153 struct ethtool_test *etest, u64 *buf)
1155 struct dsa_port *dp = dsa_user_to_port(ndev);
1156 struct dsa_switch *ds = dp->ds;
1158 if (ds->ops->self_test) {
1159 ds->ops->self_test(ds, dp->index, etest, buf);
1163 net_selftest(ndev, etest, buf);
1166 static int dsa_user_get_mm(struct net_device *dev,
1167 struct ethtool_mm_state *state)
1169 struct dsa_port *dp = dsa_user_to_port(dev);
1170 struct dsa_switch *ds = dp->ds;
1172 if (!ds->ops->get_mm)
1175 return ds->ops->get_mm(ds, dp->index, state);
1178 static int dsa_user_set_mm(struct net_device *dev, struct ethtool_mm_cfg *cfg,
1179 struct netlink_ext_ack *extack)
1181 struct dsa_port *dp = dsa_user_to_port(dev);
1182 struct dsa_switch *ds = dp->ds;
1184 if (!ds->ops->set_mm)
1187 return ds->ops->set_mm(ds, dp->index, cfg, extack);
1190 static void dsa_user_get_mm_stats(struct net_device *dev,
1191 struct ethtool_mm_stats *stats)
1193 struct dsa_port *dp = dsa_user_to_port(dev);
1194 struct dsa_switch *ds = dp->ds;
1196 if (ds->ops->get_mm_stats)
1197 ds->ops->get_mm_stats(ds, dp->index, stats);
1200 static void dsa_user_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1202 struct dsa_port *dp = dsa_user_to_port(dev);
1203 struct dsa_switch *ds = dp->ds;
1205 phylink_ethtool_get_wol(dp->pl, w);
1207 if (ds->ops->get_wol)
1208 ds->ops->get_wol(ds, dp->index, w);
1211 static int dsa_user_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1213 struct dsa_port *dp = dsa_user_to_port(dev);
1214 struct dsa_switch *ds = dp->ds;
1215 int ret = -EOPNOTSUPP;
1217 phylink_ethtool_set_wol(dp->pl, w);
1219 if (ds->ops->set_wol)
1220 ret = ds->ops->set_wol(ds, dp->index, w);
1225 static int dsa_user_set_eee(struct net_device *dev, struct ethtool_eee *e)
1227 struct dsa_port *dp = dsa_user_to_port(dev);
1228 struct dsa_switch *ds = dp->ds;
1231 /* Port's PHY and MAC both need to be EEE capable */
1232 if (!dev->phydev || !dp->pl)
1235 if (!ds->ops->set_mac_eee)
1238 ret = ds->ops->set_mac_eee(ds, dp->index, e);
1242 return phylink_ethtool_set_eee(dp->pl, e);
1245 static int dsa_user_get_eee(struct net_device *dev, struct ethtool_eee *e)
1247 struct dsa_port *dp = dsa_user_to_port(dev);
1248 struct dsa_switch *ds = dp->ds;
1251 /* Port's PHY and MAC both need to be EEE capable */
1252 if (!dev->phydev || !dp->pl)
1255 if (!ds->ops->get_mac_eee)
1258 ret = ds->ops->get_mac_eee(ds, dp->index, e);
1262 return phylink_ethtool_get_eee(dp->pl, e);
1265 static int dsa_user_get_link_ksettings(struct net_device *dev,
1266 struct ethtool_link_ksettings *cmd)
1268 struct dsa_port *dp = dsa_user_to_port(dev);
1270 return phylink_ethtool_ksettings_get(dp->pl, cmd);
1273 static int dsa_user_set_link_ksettings(struct net_device *dev,
1274 const struct ethtool_link_ksettings *cmd)
1276 struct dsa_port *dp = dsa_user_to_port(dev);
1278 return phylink_ethtool_ksettings_set(dp->pl, cmd);
1281 static void dsa_user_get_pause_stats(struct net_device *dev,
1282 struct ethtool_pause_stats *pause_stats)
1284 struct dsa_port *dp = dsa_user_to_port(dev);
1285 struct dsa_switch *ds = dp->ds;
1287 if (ds->ops->get_pause_stats)
1288 ds->ops->get_pause_stats(ds, dp->index, pause_stats);
1291 static void dsa_user_get_pauseparam(struct net_device *dev,
1292 struct ethtool_pauseparam *pause)
1294 struct dsa_port *dp = dsa_user_to_port(dev);
1296 phylink_ethtool_get_pauseparam(dp->pl, pause);
1299 static int dsa_user_set_pauseparam(struct net_device *dev,
1300 struct ethtool_pauseparam *pause)
1302 struct dsa_port *dp = dsa_user_to_port(dev);
1304 return phylink_ethtool_set_pauseparam(dp->pl, pause);
1307 #ifdef CONFIG_NET_POLL_CONTROLLER
1308 static int dsa_user_netpoll_setup(struct net_device *dev,
1309 struct netpoll_info *ni)
1311 struct net_device *conduit = dsa_user_to_conduit(dev);
1312 struct dsa_user_priv *p = netdev_priv(dev);
1313 struct netpoll *netpoll;
1316 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1320 err = __netpoll_setup(netpoll, conduit);
1326 p->netpoll = netpoll;
1331 static void dsa_user_netpoll_cleanup(struct net_device *dev)
1333 struct dsa_user_priv *p = netdev_priv(dev);
1334 struct netpoll *netpoll = p->netpoll;
1341 __netpoll_free(netpoll);
1344 static void dsa_user_poll_controller(struct net_device *dev)
1349 static struct dsa_mall_tc_entry *
1350 dsa_user_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1352 struct dsa_user_priv *p = netdev_priv(dev);
1353 struct dsa_mall_tc_entry *mall_tc_entry;
1355 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1356 if (mall_tc_entry->cookie == cookie)
1357 return mall_tc_entry;
1363 dsa_user_add_cls_matchall_mirred(struct net_device *dev,
1364 struct tc_cls_matchall_offload *cls,
1367 struct netlink_ext_ack *extack = cls->common.extack;
1368 struct dsa_port *dp = dsa_user_to_port(dev);
1369 struct dsa_user_priv *p = netdev_priv(dev);
1370 struct dsa_mall_mirror_tc_entry *mirror;
1371 struct dsa_mall_tc_entry *mall_tc_entry;
1372 struct dsa_switch *ds = dp->ds;
1373 struct flow_action_entry *act;
1374 struct dsa_port *to_dp;
1377 if (!ds->ops->port_mirror_add)
1380 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1381 cls->common.extack))
1384 act = &cls->rule->action.entries[0];
1389 if (!dsa_user_dev_check(act->dev))
1392 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1396 mall_tc_entry->cookie = cls->cookie;
1397 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1398 mirror = &mall_tc_entry->mirror;
1400 to_dp = dsa_user_to_port(act->dev);
1402 mirror->to_local_port = to_dp->index;
1403 mirror->ingress = ingress;
1405 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1407 kfree(mall_tc_entry);
1411 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1417 dsa_user_add_cls_matchall_police(struct net_device *dev,
1418 struct tc_cls_matchall_offload *cls,
1421 struct netlink_ext_ack *extack = cls->common.extack;
1422 struct dsa_port *dp = dsa_user_to_port(dev);
1423 struct dsa_user_priv *p = netdev_priv(dev);
1424 struct dsa_mall_policer_tc_entry *policer;
1425 struct dsa_mall_tc_entry *mall_tc_entry;
1426 struct dsa_switch *ds = dp->ds;
1427 struct flow_action_entry *act;
1430 if (!ds->ops->port_policer_add) {
1431 NL_SET_ERR_MSG_MOD(extack,
1432 "Policing offload not implemented");
1437 NL_SET_ERR_MSG_MOD(extack,
1438 "Only supported on ingress qdisc");
1442 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1443 cls->common.extack))
1446 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1447 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1448 NL_SET_ERR_MSG_MOD(extack,
1449 "Only one port policer allowed");
1454 act = &cls->rule->action.entries[0];
1456 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1460 mall_tc_entry->cookie = cls->cookie;
1461 mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1462 policer = &mall_tc_entry->policer;
1463 policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1464 policer->burst = act->police.burst;
1466 err = ds->ops->port_policer_add(ds, dp->index, policer);
1468 kfree(mall_tc_entry);
1472 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1477 static int dsa_user_add_cls_matchall(struct net_device *dev,
1478 struct tc_cls_matchall_offload *cls,
1481 int err = -EOPNOTSUPP;
1483 if (cls->common.protocol == htons(ETH_P_ALL) &&
1484 flow_offload_has_one_action(&cls->rule->action) &&
1485 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1486 err = dsa_user_add_cls_matchall_mirred(dev, cls, ingress);
1487 else if (flow_offload_has_one_action(&cls->rule->action) &&
1488 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1489 err = dsa_user_add_cls_matchall_police(dev, cls, ingress);
1494 static void dsa_user_del_cls_matchall(struct net_device *dev,
1495 struct tc_cls_matchall_offload *cls)
1497 struct dsa_port *dp = dsa_user_to_port(dev);
1498 struct dsa_mall_tc_entry *mall_tc_entry;
1499 struct dsa_switch *ds = dp->ds;
1501 mall_tc_entry = dsa_user_mall_tc_entry_find(dev, cls->cookie);
1505 list_del(&mall_tc_entry->list);
1507 switch (mall_tc_entry->type) {
1508 case DSA_PORT_MALL_MIRROR:
1509 if (ds->ops->port_mirror_del)
1510 ds->ops->port_mirror_del(ds, dp->index,
1511 &mall_tc_entry->mirror);
1513 case DSA_PORT_MALL_POLICER:
1514 if (ds->ops->port_policer_del)
1515 ds->ops->port_policer_del(ds, dp->index);
1521 kfree(mall_tc_entry);
1524 static int dsa_user_setup_tc_cls_matchall(struct net_device *dev,
1525 struct tc_cls_matchall_offload *cls,
1528 if (cls->common.chain_index)
1531 switch (cls->command) {
1532 case TC_CLSMATCHALL_REPLACE:
1533 return dsa_user_add_cls_matchall(dev, cls, ingress);
1534 case TC_CLSMATCHALL_DESTROY:
1535 dsa_user_del_cls_matchall(dev, cls);
1542 static int dsa_user_add_cls_flower(struct net_device *dev,
1543 struct flow_cls_offload *cls,
1546 struct dsa_port *dp = dsa_user_to_port(dev);
1547 struct dsa_switch *ds = dp->ds;
1548 int port = dp->index;
1550 if (!ds->ops->cls_flower_add)
1553 return ds->ops->cls_flower_add(ds, port, cls, ingress);
1556 static int dsa_user_del_cls_flower(struct net_device *dev,
1557 struct flow_cls_offload *cls,
1560 struct dsa_port *dp = dsa_user_to_port(dev);
1561 struct dsa_switch *ds = dp->ds;
1562 int port = dp->index;
1564 if (!ds->ops->cls_flower_del)
1567 return ds->ops->cls_flower_del(ds, port, cls, ingress);
1570 static int dsa_user_stats_cls_flower(struct net_device *dev,
1571 struct flow_cls_offload *cls,
1574 struct dsa_port *dp = dsa_user_to_port(dev);
1575 struct dsa_switch *ds = dp->ds;
1576 int port = dp->index;
1578 if (!ds->ops->cls_flower_stats)
1581 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1584 static int dsa_user_setup_tc_cls_flower(struct net_device *dev,
1585 struct flow_cls_offload *cls,
1588 switch (cls->command) {
1589 case FLOW_CLS_REPLACE:
1590 return dsa_user_add_cls_flower(dev, cls, ingress);
1591 case FLOW_CLS_DESTROY:
1592 return dsa_user_del_cls_flower(dev, cls, ingress);
1593 case FLOW_CLS_STATS:
1594 return dsa_user_stats_cls_flower(dev, cls, ingress);
1600 static int dsa_user_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1601 void *cb_priv, bool ingress)
1603 struct net_device *dev = cb_priv;
1605 if (!tc_can_offload(dev))
1609 case TC_SETUP_CLSMATCHALL:
1610 return dsa_user_setup_tc_cls_matchall(dev, type_data, ingress);
1611 case TC_SETUP_CLSFLOWER:
1612 return dsa_user_setup_tc_cls_flower(dev, type_data, ingress);
1618 static int dsa_user_setup_tc_block_cb_ig(enum tc_setup_type type,
1619 void *type_data, void *cb_priv)
1621 return dsa_user_setup_tc_block_cb(type, type_data, cb_priv, true);
1624 static int dsa_user_setup_tc_block_cb_eg(enum tc_setup_type type,
1625 void *type_data, void *cb_priv)
1627 return dsa_user_setup_tc_block_cb(type, type_data, cb_priv, false);
1630 static LIST_HEAD(dsa_user_block_cb_list);
1632 static int dsa_user_setup_tc_block(struct net_device *dev,
1633 struct flow_block_offload *f)
1635 struct flow_block_cb *block_cb;
1636 flow_setup_cb_t *cb;
1638 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1639 cb = dsa_user_setup_tc_block_cb_ig;
1640 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1641 cb = dsa_user_setup_tc_block_cb_eg;
1645 f->driver_block_list = &dsa_user_block_cb_list;
1647 switch (f->command) {
1648 case FLOW_BLOCK_BIND:
1649 if (flow_block_cb_is_busy(cb, dev, &dsa_user_block_cb_list))
1652 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1653 if (IS_ERR(block_cb))
1654 return PTR_ERR(block_cb);
1656 flow_block_cb_add(block_cb, f);
1657 list_add_tail(&block_cb->driver_list, &dsa_user_block_cb_list);
1659 case FLOW_BLOCK_UNBIND:
1660 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1664 flow_block_cb_remove(block_cb, f);
1665 list_del(&block_cb->driver_list);
1672 static int dsa_user_setup_ft_block(struct dsa_switch *ds, int port,
1675 struct net_device *conduit = dsa_port_to_conduit(dsa_to_port(ds, port));
1677 if (!conduit->netdev_ops->ndo_setup_tc)
1680 return conduit->netdev_ops->ndo_setup_tc(conduit, TC_SETUP_FT, type_data);
1683 static int dsa_user_setup_tc(struct net_device *dev, enum tc_setup_type type,
1686 struct dsa_port *dp = dsa_user_to_port(dev);
1687 struct dsa_switch *ds = dp->ds;
1690 case TC_SETUP_BLOCK:
1691 return dsa_user_setup_tc_block(dev, type_data);
1693 return dsa_user_setup_ft_block(ds, dp->index, type_data);
1698 if (!ds->ops->port_setup_tc)
1701 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1704 static int dsa_user_get_rxnfc(struct net_device *dev,
1705 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1707 struct dsa_port *dp = dsa_user_to_port(dev);
1708 struct dsa_switch *ds = dp->ds;
1710 if (!ds->ops->get_rxnfc)
1713 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1716 static int dsa_user_set_rxnfc(struct net_device *dev,
1717 struct ethtool_rxnfc *nfc)
1719 struct dsa_port *dp = dsa_user_to_port(dev);
1720 struct dsa_switch *ds = dp->ds;
1722 if (!ds->ops->set_rxnfc)
1725 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1728 static int dsa_user_get_ts_info(struct net_device *dev,
1729 struct ethtool_ts_info *ts)
1731 struct dsa_user_priv *p = netdev_priv(dev);
1732 struct dsa_switch *ds = p->dp->ds;
1734 if (!ds->ops->get_ts_info)
1737 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1740 static int dsa_user_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1743 struct dsa_port *dp = dsa_user_to_port(dev);
1744 struct switchdev_obj_port_vlan vlan = {
1745 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1747 /* This API only allows programming tagged, non-PVID VIDs */
1750 struct netlink_ext_ack extack = {0};
1751 struct dsa_switch *ds = dp->ds;
1752 struct netdev_hw_addr *ha;
1757 ret = dsa_port_vlan_add(dp, &vlan, &extack);
1760 netdev_err(dev, "%s\n", extack._msg);
1764 /* And CPU port... */
1765 ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
1768 netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1773 if (!dsa_switch_supports_uc_filtering(ds) &&
1774 !dsa_switch_supports_mc_filtering(ds))
1777 v = kzalloc(sizeof(*v), GFP_KERNEL);
1783 netif_addr_lock_bh(dev);
1786 list_add_tail(&v->list, &dp->user_vlans);
1788 if (dsa_switch_supports_mc_filtering(ds)) {
1789 netdev_for_each_synced_mc_addr(ha, dev) {
1790 dsa_user_schedule_standalone_work(dev, DSA_MC_ADD,
1795 if (dsa_switch_supports_uc_filtering(ds)) {
1796 netdev_for_each_synced_uc_addr(ha, dev) {
1797 dsa_user_schedule_standalone_work(dev, DSA_UC_ADD,
1802 netif_addr_unlock_bh(dev);
1804 dsa_flush_workqueue();
1809 dsa_port_host_vlan_del(dp, &vlan);
1810 dsa_port_vlan_del(dp, &vlan);
1815 static int dsa_user_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1818 struct dsa_port *dp = dsa_user_to_port(dev);
1819 struct switchdev_obj_port_vlan vlan = {
1821 /* This API only allows programming tagged, non-PVID VIDs */
1824 struct dsa_switch *ds = dp->ds;
1825 struct netdev_hw_addr *ha;
1829 err = dsa_port_vlan_del(dp, &vlan);
1833 err = dsa_port_host_vlan_del(dp, &vlan);
1837 if (!dsa_switch_supports_uc_filtering(ds) &&
1838 !dsa_switch_supports_mc_filtering(ds))
1841 netif_addr_lock_bh(dev);
1843 v = dsa_vlan_find(&dp->user_vlans, &vlan);
1845 netif_addr_unlock_bh(dev);
1852 if (dsa_switch_supports_mc_filtering(ds)) {
1853 netdev_for_each_synced_mc_addr(ha, dev) {
1854 dsa_user_schedule_standalone_work(dev, DSA_MC_DEL,
1859 if (dsa_switch_supports_uc_filtering(ds)) {
1860 netdev_for_each_synced_uc_addr(ha, dev) {
1861 dsa_user_schedule_standalone_work(dev, DSA_UC_DEL,
1866 netif_addr_unlock_bh(dev);
1868 dsa_flush_workqueue();
1873 static int dsa_user_restore_vlan(struct net_device *vdev, int vid, void *arg)
1875 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1877 return dsa_user_vlan_rx_add_vid(arg, proto, vid);
1880 static int dsa_user_clear_vlan(struct net_device *vdev, int vid, void *arg)
1882 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1884 return dsa_user_vlan_rx_kill_vid(arg, proto, vid);
1887 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1888 * filtering is enabled. The baseline is that only ports that offload a
1889 * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1890 * but there are exceptions for quirky hardware.
1892 * If ds->vlan_filtering_is_global = true, then standalone ports which share
1893 * the same switch with other ports that offload a VLAN-aware bridge are also
1894 * inevitably VLAN-aware.
1896 * To summarize, a DSA switch port offloads:
1898 * - If standalone (this includes software bridge, software LAG):
1899 * - if ds->needs_standalone_vlan_filtering = true, OR if
1900 * (ds->vlan_filtering_is_global = true AND there are bridges spanning
1901 * this switch chip which have vlan_filtering=1)
1902 * - the 8021q upper VLANs
1903 * - else (standalone VLAN filtering is not needed, VLAN filtering is not
1904 * global, or it is, but no port is under a VLAN-aware bridge):
1905 * - no VLAN (any 8021q upper is a software VLAN)
1907 * - If under a vlan_filtering=0 bridge which it offload:
1908 * - if ds->configure_vlan_while_not_filtering = true (default):
1909 * - the bridge VLANs. These VLANs are committed to hardware but inactive.
1910 * - else (deprecated):
1911 * - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1912 * enabled, so this behavior is broken and discouraged.
1914 * - If under a vlan_filtering=1 bridge which it offload:
1915 * - the bridge VLANs
1916 * - the 8021q upper VLANs
1918 int dsa_user_manage_vlan_filtering(struct net_device *user,
1919 bool vlan_filtering)
1923 if (vlan_filtering) {
1924 user->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1926 err = vlan_for_each(user, dsa_user_restore_vlan, user);
1928 vlan_for_each(user, dsa_user_clear_vlan, user);
1929 user->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1933 err = vlan_for_each(user, dsa_user_clear_vlan, user);
1937 user->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1943 struct dsa_hw_port {
1944 struct list_head list;
1945 struct net_device *dev;
1949 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1951 const struct dsa_hw_port *p;
1954 list_for_each_entry(p, hw_port_list, list) {
1955 if (p->dev->mtu == mtu)
1958 err = dev_set_mtu(p->dev, mtu);
1966 list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1967 if (p->dev->mtu == p->old_mtu)
1970 if (dev_set_mtu(p->dev, p->old_mtu))
1971 netdev_err(p->dev, "Failed to restore MTU\n");
1977 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1979 struct dsa_hw_port *p, *n;
1981 list_for_each_entry_safe(p, n, hw_port_list, list)
1985 /* Make the hardware datapath to/from @dev limited to a common MTU */
1986 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1988 struct list_head hw_port_list;
1989 struct dsa_switch_tree *dst;
1990 int min_mtu = ETH_MAX_MTU;
1991 struct dsa_port *other_dp;
1994 if (!dp->ds->mtu_enforcement_ingress)
2000 INIT_LIST_HEAD(&hw_port_list);
2002 /* Populate the list of ports that are part of the same bridge
2003 * as the newly added/modified port
2005 list_for_each_entry(dst, &dsa_tree_list, list) {
2006 list_for_each_entry(other_dp, &dst->ports, list) {
2007 struct dsa_hw_port *hw_port;
2008 struct net_device *user;
2010 if (other_dp->type != DSA_PORT_TYPE_USER)
2013 if (!dsa_port_bridge_same(dp, other_dp))
2016 if (!other_dp->ds->mtu_enforcement_ingress)
2019 user = other_dp->user;
2021 if (min_mtu > user->mtu)
2022 min_mtu = user->mtu;
2024 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
2028 hw_port->dev = user;
2029 hw_port->old_mtu = user->mtu;
2031 list_add(&hw_port->list, &hw_port_list);
2035 /* Attempt to configure the entire hardware bridge to the newly added
2036 * interface's MTU first, regardless of whether the intention of the
2037 * user was to raise or lower it.
2039 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->user->mtu);
2043 /* Clearly that didn't work out so well, so just set the minimum MTU on
2044 * all hardware bridge ports now. If this fails too, then all ports will
2045 * still have their old MTU rolled back anyway.
2047 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
2050 dsa_hw_port_list_free(&hw_port_list);
2053 int dsa_user_change_mtu(struct net_device *dev, int new_mtu)
2055 struct net_device *conduit = dsa_user_to_conduit(dev);
2056 struct dsa_port *dp = dsa_user_to_port(dev);
2057 struct dsa_port *cpu_dp = dp->cpu_dp;
2058 struct dsa_switch *ds = dp->ds;
2059 struct dsa_port *other_dp;
2060 int largest_mtu = 0;
2061 int new_conduit_mtu;
2062 int old_conduit_mtu;
2068 if (!ds->ops->port_change_mtu)
2071 dsa_tree_for_each_user_port(other_dp, ds->dst) {
2074 /* During probe, this function will be called for each user
2075 * device, while not all of them have been allocated. That's
2076 * ok, it doesn't change what the maximum is, so ignore it.
2078 if (!other_dp->user)
2081 /* Pretend that we already applied the setting, which we
2082 * actually haven't (still haven't done all integrity checks)
2087 user_mtu = other_dp->user->mtu;
2089 if (largest_mtu < user_mtu)
2090 largest_mtu = user_mtu;
2093 overhead = dsa_tag_protocol_overhead(cpu_dp->tag_ops);
2094 mtu_limit = min_t(int, conduit->max_mtu, dev->max_mtu + overhead);
2095 old_conduit_mtu = conduit->mtu;
2096 new_conduit_mtu = largest_mtu + overhead;
2097 if (new_conduit_mtu > mtu_limit)
2100 /* If the conduit MTU isn't over limit, there's no need to check the CPU
2101 * MTU, since that surely isn't either.
2103 cpu_mtu = largest_mtu;
2105 /* Start applying stuff */
2106 if (new_conduit_mtu != old_conduit_mtu) {
2107 err = dev_set_mtu(conduit, new_conduit_mtu);
2109 goto out_conduit_failed;
2111 /* We only need to propagate the MTU of the CPU port to
2112 * upstream switches, so emit a notifier which updates them.
2114 err = dsa_port_mtu_change(cpu_dp, cpu_mtu);
2116 goto out_cpu_failed;
2119 err = ds->ops->port_change_mtu(ds, dp->index, new_mtu);
2121 goto out_port_failed;
2125 dsa_bridge_mtu_normalization(dp);
2130 if (new_conduit_mtu != old_conduit_mtu)
2131 dsa_port_mtu_change(cpu_dp, old_conduit_mtu - overhead);
2133 if (new_conduit_mtu != old_conduit_mtu)
2134 dev_set_mtu(conduit, old_conduit_mtu);
2139 static int __maybe_unused
2140 dsa_user_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
2142 struct dsa_port *dp = dsa_user_to_port(dev);
2143 struct dsa_switch *ds = dp->ds;
2144 unsigned long mask, new_prio;
2145 int err, port = dp->index;
2147 if (!ds->ops->port_set_default_prio)
2150 err = dcb_ieee_setapp(dev, app);
2154 mask = dcb_ieee_getapp_mask(dev, app);
2155 new_prio = __fls(mask);
2157 err = ds->ops->port_set_default_prio(ds, port, new_prio);
2159 dcb_ieee_delapp(dev, app);
2166 static int __maybe_unused
2167 dsa_user_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
2169 struct dsa_port *dp = dsa_user_to_port(dev);
2170 struct dsa_switch *ds = dp->ds;
2171 unsigned long mask, new_prio;
2172 int err, port = dp->index;
2173 u8 dscp = app->protocol;
2175 if (!ds->ops->port_add_dscp_prio)
2179 netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
2184 err = dcb_ieee_setapp(dev, app);
2188 mask = dcb_ieee_getapp_mask(dev, app);
2189 new_prio = __fls(mask);
2191 err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
2193 dcb_ieee_delapp(dev, app);
2200 static int __maybe_unused dsa_user_dcbnl_ieee_setapp(struct net_device *dev,
2201 struct dcb_app *app)
2203 switch (app->selector) {
2204 case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2205 switch (app->protocol) {
2207 return dsa_user_dcbnl_set_default_prio(dev, app);
2212 case IEEE_8021QAZ_APP_SEL_DSCP:
2213 return dsa_user_dcbnl_add_dscp_prio(dev, app);
2219 static int __maybe_unused
2220 dsa_user_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
2222 struct dsa_port *dp = dsa_user_to_port(dev);
2223 struct dsa_switch *ds = dp->ds;
2224 unsigned long mask, new_prio;
2225 int err, port = dp->index;
2227 if (!ds->ops->port_set_default_prio)
2230 err = dcb_ieee_delapp(dev, app);
2234 mask = dcb_ieee_getapp_mask(dev, app);
2235 new_prio = mask ? __fls(mask) : 0;
2237 err = ds->ops->port_set_default_prio(ds, port, new_prio);
2239 dcb_ieee_setapp(dev, app);
2246 static int __maybe_unused
2247 dsa_user_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
2249 struct dsa_port *dp = dsa_user_to_port(dev);
2250 struct dsa_switch *ds = dp->ds;
2251 int err, port = dp->index;
2252 u8 dscp = app->protocol;
2254 if (!ds->ops->port_del_dscp_prio)
2257 err = dcb_ieee_delapp(dev, app);
2261 err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
2263 dcb_ieee_setapp(dev, app);
2270 static int __maybe_unused dsa_user_dcbnl_ieee_delapp(struct net_device *dev,
2271 struct dcb_app *app)
2273 switch (app->selector) {
2274 case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2275 switch (app->protocol) {
2277 return dsa_user_dcbnl_del_default_prio(dev, app);
2282 case IEEE_8021QAZ_APP_SEL_DSCP:
2283 return dsa_user_dcbnl_del_dscp_prio(dev, app);
2289 /* Pre-populate the DCB application priority table with the priorities
2290 * configured during switch setup, which we read from hardware here.
2292 static int dsa_user_dcbnl_init(struct net_device *dev)
2294 struct dsa_port *dp = dsa_user_to_port(dev);
2295 struct dsa_switch *ds = dp->ds;
2296 int port = dp->index;
2299 if (ds->ops->port_get_default_prio) {
2300 int prio = ds->ops->port_get_default_prio(ds, port);
2301 struct dcb_app app = {
2302 .selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2310 err = dcb_ieee_setapp(dev, &app);
2315 if (ds->ops->port_get_dscp_prio) {
2318 for (protocol = 0; protocol < 64; protocol++) {
2319 struct dcb_app app = {
2320 .selector = IEEE_8021QAZ_APP_SEL_DSCP,
2321 .protocol = protocol,
2325 prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
2326 if (prio == -EOPNOTSUPP)
2331 app.priority = prio;
2333 err = dcb_ieee_setapp(dev, &app);
2342 static const struct ethtool_ops dsa_user_ethtool_ops = {
2343 .get_drvinfo = dsa_user_get_drvinfo,
2344 .get_regs_len = dsa_user_get_regs_len,
2345 .get_regs = dsa_user_get_regs,
2346 .nway_reset = dsa_user_nway_reset,
2347 .get_link = ethtool_op_get_link,
2348 .get_eeprom_len = dsa_user_get_eeprom_len,
2349 .get_eeprom = dsa_user_get_eeprom,
2350 .set_eeprom = dsa_user_set_eeprom,
2351 .get_strings = dsa_user_get_strings,
2352 .get_ethtool_stats = dsa_user_get_ethtool_stats,
2353 .get_sset_count = dsa_user_get_sset_count,
2354 .get_eth_phy_stats = dsa_user_get_eth_phy_stats,
2355 .get_eth_mac_stats = dsa_user_get_eth_mac_stats,
2356 .get_eth_ctrl_stats = dsa_user_get_eth_ctrl_stats,
2357 .get_rmon_stats = dsa_user_get_rmon_stats,
2358 .set_wol = dsa_user_set_wol,
2359 .get_wol = dsa_user_get_wol,
2360 .set_eee = dsa_user_set_eee,
2361 .get_eee = dsa_user_get_eee,
2362 .get_link_ksettings = dsa_user_get_link_ksettings,
2363 .set_link_ksettings = dsa_user_set_link_ksettings,
2364 .get_pause_stats = dsa_user_get_pause_stats,
2365 .get_pauseparam = dsa_user_get_pauseparam,
2366 .set_pauseparam = dsa_user_set_pauseparam,
2367 .get_rxnfc = dsa_user_get_rxnfc,
2368 .set_rxnfc = dsa_user_set_rxnfc,
2369 .get_ts_info = dsa_user_get_ts_info,
2370 .self_test = dsa_user_net_selftest,
2371 .get_mm = dsa_user_get_mm,
2372 .set_mm = dsa_user_set_mm,
2373 .get_mm_stats = dsa_user_get_mm_stats,
2376 static const struct dcbnl_rtnl_ops __maybe_unused dsa_user_dcbnl_ops = {
2377 .ieee_setapp = dsa_user_dcbnl_ieee_setapp,
2378 .ieee_delapp = dsa_user_dcbnl_ieee_delapp,
2381 static void dsa_user_get_stats64(struct net_device *dev,
2382 struct rtnl_link_stats64 *s)
2384 struct dsa_port *dp = dsa_user_to_port(dev);
2385 struct dsa_switch *ds = dp->ds;
2387 if (ds->ops->get_stats64)
2388 ds->ops->get_stats64(ds, dp->index, s);
2390 dev_get_tstats64(dev, s);
2393 static int dsa_user_fill_forward_path(struct net_device_path_ctx *ctx,
2394 struct net_device_path *path)
2396 struct dsa_port *dp = dsa_user_to_port(ctx->dev);
2397 struct net_device *conduit = dsa_port_to_conduit(dp);
2398 struct dsa_port *cpu_dp = dp->cpu_dp;
2400 path->dev = ctx->dev;
2401 path->type = DEV_PATH_DSA;
2402 path->dsa.proto = cpu_dp->tag_ops->proto;
2403 path->dsa.port = dp->index;
2409 static const struct net_device_ops dsa_user_netdev_ops = {
2410 .ndo_open = dsa_user_open,
2411 .ndo_stop = dsa_user_close,
2412 .ndo_start_xmit = dsa_user_xmit,
2413 .ndo_change_rx_flags = dsa_user_change_rx_flags,
2414 .ndo_set_rx_mode = dsa_user_set_rx_mode,
2415 .ndo_set_mac_address = dsa_user_set_mac_address,
2416 .ndo_fdb_dump = dsa_user_fdb_dump,
2417 .ndo_eth_ioctl = dsa_user_ioctl,
2418 .ndo_get_iflink = dsa_user_get_iflink,
2419 #ifdef CONFIG_NET_POLL_CONTROLLER
2420 .ndo_netpoll_setup = dsa_user_netpoll_setup,
2421 .ndo_netpoll_cleanup = dsa_user_netpoll_cleanup,
2422 .ndo_poll_controller = dsa_user_poll_controller,
2424 .ndo_setup_tc = dsa_user_setup_tc,
2425 .ndo_get_stats64 = dsa_user_get_stats64,
2426 .ndo_vlan_rx_add_vid = dsa_user_vlan_rx_add_vid,
2427 .ndo_vlan_rx_kill_vid = dsa_user_vlan_rx_kill_vid,
2428 .ndo_change_mtu = dsa_user_change_mtu,
2429 .ndo_fill_forward_path = dsa_user_fill_forward_path,
2432 static struct device_type dsa_type = {
2436 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2438 const struct dsa_port *dp = dsa_to_port(ds, port);
2441 phylink_mac_change(dp->pl, up);
2443 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2445 static void dsa_user_phylink_fixed_state(struct phylink_config *config,
2446 struct phylink_link_state *state)
2448 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
2449 struct dsa_switch *ds = dp->ds;
2451 /* No need to check that this operation is valid, the callback would
2452 * not be called if it was not.
2454 ds->ops->phylink_fixed_state(ds, dp->index, state);
2457 /* user device setup *******************************************************/
2458 static int dsa_user_phy_connect(struct net_device *user_dev, int addr,
2461 struct dsa_port *dp = dsa_user_to_port(user_dev);
2462 struct dsa_switch *ds = dp->ds;
2464 user_dev->phydev = mdiobus_get_phy(ds->user_mii_bus, addr);
2465 if (!user_dev->phydev) {
2466 netdev_err(user_dev, "no phy at %d\n", addr);
2470 user_dev->phydev->dev_flags |= flags;
2472 return phylink_connect_phy(dp->pl, user_dev->phydev);
2475 static int dsa_user_phy_setup(struct net_device *user_dev)
2477 struct dsa_port *dp = dsa_user_to_port(user_dev);
2478 struct device_node *port_dn = dp->dn;
2479 struct dsa_switch *ds = dp->ds;
2483 dp->pl_config.dev = &user_dev->dev;
2484 dp->pl_config.type = PHYLINK_NETDEV;
2486 /* The get_fixed_state callback takes precedence over polling the
2487 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set
2488 * this if the switch provides such a callback.
2490 if (ds->ops->phylink_fixed_state) {
2491 dp->pl_config.get_fixed_state = dsa_user_phylink_fixed_state;
2492 dp->pl_config.poll_fixed_state = true;
2495 ret = dsa_port_phylink_create(dp);
2499 if (ds->ops->get_phy_flags)
2500 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
2502 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
2503 if (ret == -ENODEV && ds->user_mii_bus) {
2504 /* We could not connect to a designated PHY or SFP, so try to
2505 * use the switch internal MDIO bus instead
2507 ret = dsa_user_phy_connect(user_dev, dp->index, phy_flags);
2510 netdev_err(user_dev, "failed to connect to PHY: %pe\n",
2512 dsa_port_phylink_destroy(dp);
2518 void dsa_user_setup_tagger(struct net_device *user)
2520 struct dsa_port *dp = dsa_user_to_port(user);
2521 struct net_device *conduit = dsa_port_to_conduit(dp);
2522 struct dsa_user_priv *p = netdev_priv(user);
2523 const struct dsa_port *cpu_dp = dp->cpu_dp;
2524 const struct dsa_switch *ds = dp->ds;
2526 user->needed_headroom = cpu_dp->tag_ops->needed_headroom;
2527 user->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
2528 /* Try to save one extra realloc later in the TX path (in the conduit)
2529 * by also inheriting the conduit's needed headroom and tailroom.
2530 * The 8021q driver also does this.
2532 user->needed_headroom += conduit->needed_headroom;
2533 user->needed_tailroom += conduit->needed_tailroom;
2535 p->xmit = cpu_dp->tag_ops->xmit;
2537 user->features = conduit->vlan_features | NETIF_F_HW_TC;
2538 user->hw_features |= NETIF_F_HW_TC;
2539 user->features |= NETIF_F_LLTX;
2540 if (user->needed_tailroom)
2541 user->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
2542 if (ds->needs_standalone_vlan_filtering)
2543 user->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2546 int dsa_user_suspend(struct net_device *user_dev)
2548 struct dsa_port *dp = dsa_user_to_port(user_dev);
2550 if (!netif_running(user_dev))
2553 netif_device_detach(user_dev);
2556 phylink_stop(dp->pl);
2562 int dsa_user_resume(struct net_device *user_dev)
2564 struct dsa_port *dp = dsa_user_to_port(user_dev);
2566 if (!netif_running(user_dev))
2569 netif_device_attach(user_dev);
2572 phylink_start(dp->pl);
2578 int dsa_user_create(struct dsa_port *port)
2580 struct net_device *conduit = dsa_port_to_conduit(port);
2581 struct dsa_switch *ds = port->ds;
2582 struct net_device *user_dev;
2583 struct dsa_user_priv *p;
2588 if (!ds->num_tx_queues)
2589 ds->num_tx_queues = 1;
2593 assign_type = NET_NAME_PREDICTABLE;
2596 assign_type = NET_NAME_ENUM;
2599 user_dev = alloc_netdev_mqs(sizeof(struct dsa_user_priv), name,
2600 assign_type, ether_setup,
2601 ds->num_tx_queues, 1);
2602 if (user_dev == NULL)
2605 user_dev->rtnl_link_ops = &dsa_link_ops;
2606 user_dev->ethtool_ops = &dsa_user_ethtool_ops;
2607 #if IS_ENABLED(CONFIG_DCB)
2608 user_dev->dcbnl_ops = &dsa_user_dcbnl_ops;
2610 if (!is_zero_ether_addr(port->mac))
2611 eth_hw_addr_set(user_dev, port->mac);
2613 eth_hw_addr_inherit(user_dev, conduit);
2614 user_dev->priv_flags |= IFF_NO_QUEUE;
2615 if (dsa_switch_supports_uc_filtering(ds))
2616 user_dev->priv_flags |= IFF_UNICAST_FLT;
2617 user_dev->netdev_ops = &dsa_user_netdev_ops;
2618 if (ds->ops->port_max_mtu)
2619 user_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2620 SET_NETDEV_DEVTYPE(user_dev, &dsa_type);
2622 SET_NETDEV_DEV(user_dev, port->ds->dev);
2623 SET_NETDEV_DEVLINK_PORT(user_dev, &port->devlink_port);
2624 user_dev->dev.of_node = port->dn;
2625 user_dev->vlan_features = conduit->vlan_features;
2627 p = netdev_priv(user_dev);
2628 user_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2629 if (!user_dev->tstats) {
2630 free_netdev(user_dev);
2634 ret = gro_cells_init(&p->gcells, user_dev);
2639 INIT_LIST_HEAD(&p->mall_tc_list);
2640 port->user = user_dev;
2641 dsa_user_setup_tagger(user_dev);
2643 netif_carrier_off(user_dev);
2645 ret = dsa_user_phy_setup(user_dev);
2647 netdev_err(user_dev,
2648 "error %d setting up PHY for tree %d, switch %d, port %d\n",
2649 ret, ds->dst->index, ds->index, port->index);
2655 ret = dsa_user_change_mtu(user_dev, ETH_DATA_LEN);
2656 if (ret && ret != -EOPNOTSUPP)
2657 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2658 ret, ETH_DATA_LEN, port->index);
2660 ret = register_netdevice(user_dev);
2662 netdev_err(conduit, "error %d registering interface %s\n",
2663 ret, user_dev->name);
2668 if (IS_ENABLED(CONFIG_DCB)) {
2669 ret = dsa_user_dcbnl_init(user_dev);
2671 netdev_err(user_dev,
2672 "failed to initialize DCB: %pe\n",
2675 goto out_unregister;
2679 ret = netdev_upper_dev_link(conduit, user_dev, NULL);
2684 goto out_unregister;
2689 unregister_netdev(user_dev);
2692 phylink_disconnect_phy(p->dp->pl);
2694 dsa_port_phylink_destroy(p->dp);
2696 gro_cells_destroy(&p->gcells);
2698 free_percpu(user_dev->tstats);
2699 free_netdev(user_dev);
2704 void dsa_user_destroy(struct net_device *user_dev)
2706 struct net_device *conduit = dsa_user_to_conduit(user_dev);
2707 struct dsa_port *dp = dsa_user_to_port(user_dev);
2708 struct dsa_user_priv *p = netdev_priv(user_dev);
2710 netif_carrier_off(user_dev);
2712 netdev_upper_dev_unlink(conduit, user_dev);
2713 unregister_netdevice(user_dev);
2714 phylink_disconnect_phy(dp->pl);
2717 dsa_port_phylink_destroy(dp);
2718 gro_cells_destroy(&p->gcells);
2719 free_percpu(user_dev->tstats);
2720 free_netdev(user_dev);
2723 int dsa_user_change_conduit(struct net_device *dev, struct net_device *conduit,
2724 struct netlink_ext_ack *extack)
2726 struct net_device *old_conduit = dsa_user_to_conduit(dev);
2727 struct dsa_port *dp = dsa_user_to_port(dev);
2728 struct dsa_switch *ds = dp->ds;
2729 struct net_device *upper;
2730 struct list_head *iter;
2733 if (conduit == old_conduit)
2736 if (!ds->ops->port_change_conduit) {
2737 NL_SET_ERR_MSG_MOD(extack,
2738 "Driver does not support changing DSA conduit");
2742 if (!netdev_uses_dsa(conduit)) {
2743 NL_SET_ERR_MSG_MOD(extack,
2744 "Interface not eligible as DSA conduit");
2748 netdev_for_each_upper_dev_rcu(conduit, upper, iter) {
2749 if (dsa_user_dev_check(upper))
2751 if (netif_is_bridge_master(upper))
2753 NL_SET_ERR_MSG_MOD(extack, "Cannot join conduit with unknown uppers");
2757 /* Since we allow live-changing the DSA conduit, plus we auto-open the
2758 * DSA conduit when the user port opens => we need to ensure that the
2759 * new DSA conduit is open too.
2761 if (dev->flags & IFF_UP) {
2762 err = dev_open(conduit, extack);
2767 netdev_upper_dev_unlink(old_conduit, dev);
2769 err = netdev_upper_dev_link(conduit, dev, extack);
2771 goto out_revert_old_conduit_unlink;
2773 err = dsa_port_change_conduit(dp, conduit, extack);
2775 goto out_revert_conduit_link;
2777 /* Update the MTU of the new CPU port through cross-chip notifiers */
2778 err = dsa_user_change_mtu(dev, dev->mtu);
2779 if (err && err != -EOPNOTSUPP) {
2781 "nonfatal error updating MTU with new conduit: %pe\n",
2785 /* If the port doesn't have its own MAC address and relies on the DSA
2786 * conduit's one, inherit it again from the new DSA conduit.
2788 if (is_zero_ether_addr(dp->mac))
2789 eth_hw_addr_inherit(dev, conduit);
2793 out_revert_conduit_link:
2794 netdev_upper_dev_unlink(conduit, dev);
2795 out_revert_old_conduit_unlink:
2796 netdev_upper_dev_link(old_conduit, dev, NULL);
2800 bool dsa_user_dev_check(const struct net_device *dev)
2802 return dev->netdev_ops == &dsa_user_netdev_ops;
2804 EXPORT_SYMBOL_GPL(dsa_user_dev_check);
2806 static int dsa_user_changeupper(struct net_device *dev,
2807 struct netdev_notifier_changeupper_info *info)
2809 struct netlink_ext_ack *extack;
2810 int err = NOTIFY_DONE;
2811 struct dsa_port *dp;
2813 if (!dsa_user_dev_check(dev))
2816 dp = dsa_user_to_port(dev);
2817 extack = netdev_notifier_info_to_extack(&info->info);
2819 if (netif_is_bridge_master(info->upper_dev)) {
2820 if (info->linking) {
2821 err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2823 dsa_bridge_mtu_normalization(dp);
2824 if (err == -EOPNOTSUPP) {
2825 NL_SET_ERR_MSG_WEAK_MOD(extack,
2826 "Offloading not supported");
2829 err = notifier_from_errno(err);
2831 dsa_port_bridge_leave(dp, info->upper_dev);
2834 } else if (netif_is_lag_master(info->upper_dev)) {
2835 if (info->linking) {
2836 err = dsa_port_lag_join(dp, info->upper_dev,
2837 info->upper_info, extack);
2838 if (err == -EOPNOTSUPP) {
2839 NL_SET_ERR_MSG_WEAK_MOD(extack,
2840 "Offloading not supported");
2843 err = notifier_from_errno(err);
2845 dsa_port_lag_leave(dp, info->upper_dev);
2848 } else if (is_hsr_master(info->upper_dev)) {
2849 if (info->linking) {
2850 err = dsa_port_hsr_join(dp, info->upper_dev, extack);
2851 if (err == -EOPNOTSUPP) {
2852 NL_SET_ERR_MSG_WEAK_MOD(extack,
2853 "Offloading not supported");
2856 err = notifier_from_errno(err);
2858 dsa_port_hsr_leave(dp, info->upper_dev);
2866 static int dsa_user_prechangeupper(struct net_device *dev,
2867 struct netdev_notifier_changeupper_info *info)
2869 struct dsa_port *dp;
2871 if (!dsa_user_dev_check(dev))
2874 dp = dsa_user_to_port(dev);
2876 if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2877 dsa_port_pre_bridge_leave(dp, info->upper_dev);
2878 else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2879 dsa_port_pre_lag_leave(dp, info->upper_dev);
2880 /* dsa_port_pre_hsr_leave is not yet necessary since hsr devices cannot
2881 * meaningfully placed under a bridge yet
2888 dsa_user_lag_changeupper(struct net_device *dev,
2889 struct netdev_notifier_changeupper_info *info)
2891 struct net_device *lower;
2892 struct list_head *iter;
2893 int err = NOTIFY_DONE;
2894 struct dsa_port *dp;
2896 if (!netif_is_lag_master(dev))
2899 netdev_for_each_lower_dev(dev, lower, iter) {
2900 if (!dsa_user_dev_check(lower))
2903 dp = dsa_user_to_port(lower);
2908 err = dsa_user_changeupper(lower, info);
2909 if (notifier_to_errno(err))
2916 /* Same as dsa_user_lag_changeupper() except that it calls
2917 * dsa_user_prechangeupper()
2920 dsa_user_lag_prechangeupper(struct net_device *dev,
2921 struct netdev_notifier_changeupper_info *info)
2923 struct net_device *lower;
2924 struct list_head *iter;
2925 int err = NOTIFY_DONE;
2926 struct dsa_port *dp;
2928 if (!netif_is_lag_master(dev))
2931 netdev_for_each_lower_dev(dev, lower, iter) {
2932 if (!dsa_user_dev_check(lower))
2935 dp = dsa_user_to_port(lower);
2940 err = dsa_user_prechangeupper(lower, info);
2941 if (notifier_to_errno(err))
2949 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2950 struct netdev_notifier_changeupper_info *info)
2952 struct netlink_ext_ack *ext_ack;
2953 struct net_device *user, *br;
2954 struct dsa_port *dp;
2956 ext_ack = netdev_notifier_info_to_extack(&info->info);
2958 if (!is_vlan_dev(dev))
2961 user = vlan_dev_real_dev(dev);
2962 if (!dsa_user_dev_check(user))
2965 dp = dsa_user_to_port(user);
2966 br = dsa_port_bridge_dev_get(dp);
2970 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
2971 if (br_vlan_enabled(br) &&
2972 netif_is_bridge_master(info->upper_dev) && info->linking) {
2973 NL_SET_ERR_MSG_MOD(ext_ack,
2974 "Cannot make VLAN device join VLAN-aware bridge");
2975 return notifier_from_errno(-EINVAL);
2982 dsa_user_check_8021q_upper(struct net_device *dev,
2983 struct netdev_notifier_changeupper_info *info)
2985 struct dsa_port *dp = dsa_user_to_port(dev);
2986 struct net_device *br = dsa_port_bridge_dev_get(dp);
2987 struct bridge_vlan_info br_info;
2988 struct netlink_ext_ack *extack;
2989 int err = NOTIFY_DONE;
2992 if (!br || !br_vlan_enabled(br))
2995 extack = netdev_notifier_info_to_extack(&info->info);
2996 vid = vlan_dev_vlan_id(info->upper_dev);
2998 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2999 * device, respectively the VID is not found, returning
3000 * 0 means success, which is a failure for us here.
3002 err = br_vlan_get_info(br, vid, &br_info);
3004 NL_SET_ERR_MSG_MOD(extack,
3005 "This VLAN is already configured by the bridge");
3006 return notifier_from_errno(-EBUSY);
3013 dsa_user_prechangeupper_sanity_check(struct net_device *dev,
3014 struct netdev_notifier_changeupper_info *info)
3016 struct dsa_switch *ds;
3017 struct dsa_port *dp;
3020 if (!dsa_user_dev_check(dev))
3021 return dsa_prevent_bridging_8021q_upper(dev, info);
3023 dp = dsa_user_to_port(dev);
3026 if (ds->ops->port_prechangeupper) {
3027 err = ds->ops->port_prechangeupper(ds, dp->index, info);
3029 return notifier_from_errno(err);
3032 if (is_vlan_dev(info->upper_dev))
3033 return dsa_user_check_8021q_upper(dev, info);
3038 /* To be eligible as a DSA conduit, a LAG must have all lower interfaces be
3039 * eligible DSA conduits. Additionally, all LAG slaves must be DSA conduits of
3040 * switches in the same switch tree.
3042 static int dsa_lag_conduit_validate(struct net_device *lag_dev,
3043 struct netlink_ext_ack *extack)
3045 struct net_device *lower1, *lower2;
3046 struct list_head *iter1, *iter2;
3048 netdev_for_each_lower_dev(lag_dev, lower1, iter1) {
3049 netdev_for_each_lower_dev(lag_dev, lower2, iter2) {
3050 if (!netdev_uses_dsa(lower1) ||
3051 !netdev_uses_dsa(lower2)) {
3052 NL_SET_ERR_MSG_MOD(extack,
3053 "All LAG ports must be eligible as DSA conduits");
3054 return notifier_from_errno(-EINVAL);
3057 if (lower1 == lower2)
3060 if (!dsa_port_tree_same(lower1->dsa_ptr,
3062 NL_SET_ERR_MSG_MOD(extack,
3063 "LAG contains DSA conduits of disjoint switch trees");
3064 return notifier_from_errno(-EINVAL);
3073 dsa_conduit_prechangeupper_sanity_check(struct net_device *conduit,
3074 struct netdev_notifier_changeupper_info *info)
3076 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
3078 if (!netdev_uses_dsa(conduit))
3084 /* Allow DSA switch uppers */
3085 if (dsa_user_dev_check(info->upper_dev))
3088 /* Allow bridge uppers of DSA conduits, subject to further
3089 * restrictions in dsa_bridge_prechangelower_sanity_check()
3091 if (netif_is_bridge_master(info->upper_dev))
3094 /* Allow LAG uppers, subject to further restrictions in
3095 * dsa_lag_conduit_prechangelower_sanity_check()
3097 if (netif_is_lag_master(info->upper_dev))
3098 return dsa_lag_conduit_validate(info->upper_dev, extack);
3100 NL_SET_ERR_MSG_MOD(extack,
3101 "DSA conduit cannot join unknown upper interfaces");
3102 return notifier_from_errno(-EBUSY);
3106 dsa_lag_conduit_prechangelower_sanity_check(struct net_device *dev,
3107 struct netdev_notifier_changeupper_info *info)
3109 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
3110 struct net_device *lag_dev = info->upper_dev;
3111 struct net_device *lower;
3112 struct list_head *iter;
3114 if (!netdev_uses_dsa(lag_dev) || !netif_is_lag_master(lag_dev))
3120 if (!netdev_uses_dsa(dev)) {
3121 NL_SET_ERR_MSG(extack,
3122 "Only DSA conduits can join a LAG DSA conduit");
3123 return notifier_from_errno(-EINVAL);
3126 netdev_for_each_lower_dev(lag_dev, lower, iter) {
3127 if (!dsa_port_tree_same(dev->dsa_ptr, lower->dsa_ptr)) {
3128 NL_SET_ERR_MSG(extack,
3129 "Interface is DSA conduit for a different switch tree than this LAG");
3130 return notifier_from_errno(-EINVAL);
3139 /* Don't allow bridging of DSA conduits, since the bridge layer rx_handler
3140 * prevents the DSA fake ethertype handler to be invoked, so we don't get the
3141 * chance to strip off and parse the DSA switch tag protocol header (the bridge
3142 * layer just returns RX_HANDLER_CONSUMED, stopping RX processing for these
3144 * The only case where that would not be an issue is when bridging can already
3145 * be offloaded, such as when the DSA conduit is itself a DSA or plain switchdev
3146 * port, and is bridged only with other ports from the same hardware device.
3149 dsa_bridge_prechangelower_sanity_check(struct net_device *new_lower,
3150 struct netdev_notifier_changeupper_info *info)
3152 struct net_device *br = info->upper_dev;
3153 struct netlink_ext_ack *extack;
3154 struct net_device *lower;
3155 struct list_head *iter;
3157 if (!netif_is_bridge_master(br))
3163 extack = netdev_notifier_info_to_extack(&info->info);
3165 netdev_for_each_lower_dev(br, lower, iter) {
3166 if (!netdev_uses_dsa(new_lower) && !netdev_uses_dsa(lower))
3169 if (!netdev_port_same_parent_id(lower, new_lower)) {
3170 NL_SET_ERR_MSG(extack,
3171 "Cannot do software bridging with a DSA conduit");
3172 return notifier_from_errno(-EINVAL);
3179 static void dsa_tree_migrate_ports_from_lag_conduit(struct dsa_switch_tree *dst,
3180 struct net_device *lag_dev)
3182 struct net_device *new_conduit = dsa_tree_find_first_conduit(dst);
3183 struct dsa_port *dp;
3186 dsa_tree_for_each_user_port(dp, dst) {
3187 if (dsa_port_to_conduit(dp) != lag_dev)
3190 err = dsa_user_change_conduit(dp->user, new_conduit, NULL);
3192 netdev_err(dp->user,
3193 "failed to restore conduit to %s: %pe\n",
3194 new_conduit->name, ERR_PTR(err));
3199 static int dsa_conduit_lag_join(struct net_device *conduit,
3200 struct net_device *lag_dev,
3201 struct netdev_lag_upper_info *uinfo,
3202 struct netlink_ext_ack *extack)
3204 struct dsa_port *cpu_dp = conduit->dsa_ptr;
3205 struct dsa_switch_tree *dst = cpu_dp->dst;
3206 struct dsa_port *dp;
3209 err = dsa_conduit_lag_setup(lag_dev, cpu_dp, uinfo, extack);
3213 dsa_tree_for_each_user_port(dp, dst) {
3214 if (dsa_port_to_conduit(dp) != conduit)
3217 err = dsa_user_change_conduit(dp->user, lag_dev, extack);
3225 dsa_tree_for_each_user_port_continue_reverse(dp, dst) {
3226 if (dsa_port_to_conduit(dp) != lag_dev)
3229 err = dsa_user_change_conduit(dp->user, conduit, NULL);
3231 netdev_err(dp->user,
3232 "failed to restore conduit to %s: %pe\n",
3233 conduit->name, ERR_PTR(err));
3237 dsa_conduit_lag_teardown(lag_dev, conduit->dsa_ptr);
3242 static void dsa_conduit_lag_leave(struct net_device *conduit,
3243 struct net_device *lag_dev)
3245 struct dsa_port *dp, *cpu_dp = lag_dev->dsa_ptr;
3246 struct dsa_switch_tree *dst = cpu_dp->dst;
3247 struct dsa_port *new_cpu_dp = NULL;
3248 struct net_device *lower;
3249 struct list_head *iter;
3251 netdev_for_each_lower_dev(lag_dev, lower, iter) {
3252 if (netdev_uses_dsa(lower)) {
3253 new_cpu_dp = lower->dsa_ptr;
3259 /* Update the CPU port of the user ports still under the LAG
3260 * so that dsa_port_to_conduit() continues to work properly
3262 dsa_tree_for_each_user_port(dp, dst)
3263 if (dsa_port_to_conduit(dp) == lag_dev)
3264 dp->cpu_dp = new_cpu_dp;
3266 /* Update the index of the virtual CPU port to match the lowest
3269 lag_dev->dsa_ptr = new_cpu_dp;
3272 /* If the LAG DSA conduit has no ports left, migrate back all
3273 * user ports to the first physical CPU port
3275 dsa_tree_migrate_ports_from_lag_conduit(dst, lag_dev);
3278 /* This DSA conduit has left its LAG in any case, so let
3279 * the CPU port leave the hardware LAG as well
3281 dsa_conduit_lag_teardown(lag_dev, conduit->dsa_ptr);
3284 static int dsa_conduit_changeupper(struct net_device *dev,
3285 struct netdev_notifier_changeupper_info *info)
3287 struct netlink_ext_ack *extack;
3288 int err = NOTIFY_DONE;
3290 if (!netdev_uses_dsa(dev))
3293 extack = netdev_notifier_info_to_extack(&info->info);
3295 if (netif_is_lag_master(info->upper_dev)) {
3296 if (info->linking) {
3297 err = dsa_conduit_lag_join(dev, info->upper_dev,
3298 info->upper_info, extack);
3299 err = notifier_from_errno(err);
3301 dsa_conduit_lag_leave(dev, info->upper_dev);
3309 static int dsa_user_netdevice_event(struct notifier_block *nb,
3310 unsigned long event, void *ptr)
3312 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3315 case NETDEV_PRECHANGEUPPER: {
3316 struct netdev_notifier_changeupper_info *info = ptr;
3319 err = dsa_user_prechangeupper_sanity_check(dev, info);
3320 if (notifier_to_errno(err))
3323 err = dsa_conduit_prechangeupper_sanity_check(dev, info);
3324 if (notifier_to_errno(err))
3327 err = dsa_lag_conduit_prechangelower_sanity_check(dev, info);
3328 if (notifier_to_errno(err))
3331 err = dsa_bridge_prechangelower_sanity_check(dev, info);
3332 if (notifier_to_errno(err))
3335 err = dsa_user_prechangeupper(dev, ptr);
3336 if (notifier_to_errno(err))
3339 err = dsa_user_lag_prechangeupper(dev, ptr);
3340 if (notifier_to_errno(err))
3345 case NETDEV_CHANGEUPPER: {
3348 err = dsa_user_changeupper(dev, ptr);
3349 if (notifier_to_errno(err))
3352 err = dsa_user_lag_changeupper(dev, ptr);
3353 if (notifier_to_errno(err))
3356 err = dsa_conduit_changeupper(dev, ptr);
3357 if (notifier_to_errno(err))
3362 case NETDEV_CHANGELOWERSTATE: {
3363 struct netdev_notifier_changelowerstate_info *info = ptr;
3364 struct dsa_port *dp;
3367 if (dsa_user_dev_check(dev)) {
3368 dp = dsa_user_to_port(dev);
3370 err = dsa_port_lag_change(dp, info->lower_state_info);
3373 /* Mirror LAG port events on DSA conduits that are in
3374 * a LAG towards their respective switch CPU ports
3376 if (netdev_uses_dsa(dev)) {
3379 err = dsa_port_lag_change(dp, info->lower_state_info);
3382 return notifier_from_errno(err);
3386 /* Track state of conduit port.
3387 * DSA driver may require the conduit port (and indirectly
3388 * the tagger) to be available for some special operation.
3390 if (netdev_uses_dsa(dev)) {
3391 struct dsa_port *cpu_dp = dev->dsa_ptr;
3392 struct dsa_switch_tree *dst = cpu_dp->ds->dst;
3394 /* Track when the conduit port is UP */
3395 dsa_tree_conduit_oper_state_change(dst, dev,
3396 netif_oper_up(dev));
3398 /* Track when the conduit port is ready and can accept
3400 * NETDEV_UP event is not enough to flag a port as ready.
3401 * We also have to wait for linkwatch_do_dev to dev_activate
3402 * and emit a NETDEV_CHANGE event.
3403 * We check if a conduit port is ready by checking if the dev
3404 * have a qdisc assigned and is not noop.
3406 dsa_tree_conduit_admin_state_change(dst, dev,
3407 !qdisc_tx_is_noop(dev));
3414 case NETDEV_GOING_DOWN: {
3415 struct dsa_port *dp, *cpu_dp;
3416 struct dsa_switch_tree *dst;
3417 LIST_HEAD(close_list);
3419 if (!netdev_uses_dsa(dev))
3422 cpu_dp = dev->dsa_ptr;
3423 dst = cpu_dp->ds->dst;
3425 dsa_tree_conduit_admin_state_change(dst, dev, false);
3427 list_for_each_entry(dp, &dst->ports, list) {
3428 if (!dsa_port_is_user(dp))
3431 if (dp->cpu_dp != cpu_dp)
3434 list_add(&dp->user->close_list, &close_list);
3437 dev_close_many(&close_list, true);
3449 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
3451 struct switchdev_notifier_fdb_info info = {};
3453 info.addr = switchdev_work->addr;
3454 info.vid = switchdev_work->vid;
3455 info.offloaded = true;
3456 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
3457 switchdev_work->orig_dev, &info.info, NULL);
3460 static void dsa_user_switchdev_event_work(struct work_struct *work)
3462 struct dsa_switchdev_event_work *switchdev_work =
3463 container_of(work, struct dsa_switchdev_event_work, work);
3464 const unsigned char *addr = switchdev_work->addr;
3465 struct net_device *dev = switchdev_work->dev;
3466 u16 vid = switchdev_work->vid;
3467 struct dsa_switch *ds;
3468 struct dsa_port *dp;
3471 dp = dsa_user_to_port(dev);
3474 switch (switchdev_work->event) {
3475 case SWITCHDEV_FDB_ADD_TO_DEVICE:
3476 if (switchdev_work->host_addr)
3477 err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
3479 err = dsa_port_lag_fdb_add(dp, addr, vid);
3481 err = dsa_port_fdb_add(dp, addr, vid);
3484 "port %d failed to add %pM vid %d to fdb: %d\n",
3485 dp->index, addr, vid, err);
3488 dsa_fdb_offload_notify(switchdev_work);
3491 case SWITCHDEV_FDB_DEL_TO_DEVICE:
3492 if (switchdev_work->host_addr)
3493 err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
3495 err = dsa_port_lag_fdb_del(dp, addr, vid);
3497 err = dsa_port_fdb_del(dp, addr, vid);
3500 "port %d failed to delete %pM vid %d from fdb: %d\n",
3501 dp->index, addr, vid, err);
3507 kfree(switchdev_work);
3510 static bool dsa_foreign_dev_check(const struct net_device *dev,
3511 const struct net_device *foreign_dev)
3513 const struct dsa_port *dp = dsa_user_to_port(dev);
3514 struct dsa_switch_tree *dst = dp->ds->dst;
3516 if (netif_is_bridge_master(foreign_dev))
3517 return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
3519 if (netif_is_bridge_port(foreign_dev))
3520 return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
3522 /* Everything else is foreign */
3526 static int dsa_user_fdb_event(struct net_device *dev,
3527 struct net_device *orig_dev,
3528 unsigned long event, const void *ctx,
3529 const struct switchdev_notifier_fdb_info *fdb_info)
3531 struct dsa_switchdev_event_work *switchdev_work;
3532 struct dsa_port *dp = dsa_user_to_port(dev);
3533 bool host_addr = fdb_info->is_local;
3534 struct dsa_switch *ds = dp->ds;
3536 if (ctx && ctx != dp)
3542 if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
3543 if (dsa_port_offloads_bridge_port(dp, orig_dev))
3546 /* FDB entries learned by the software bridge or by foreign
3547 * bridge ports should be installed as host addresses only if
3548 * the driver requests assisted learning.
3550 if (!ds->assisted_learning_on_cpu_port)
3554 /* Also treat FDB entries on foreign interfaces bridged with us as host
3557 if (dsa_foreign_dev_check(dev, orig_dev))
3560 /* Check early that we're not doing work in vain.
3561 * Host addresses on LAG ports still require regular FDB ops,
3562 * since the CPU port isn't in a LAG.
3564 if (dp->lag && !host_addr) {
3565 if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
3568 if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
3572 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3573 if (!switchdev_work)
3576 netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
3577 event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
3578 orig_dev->name, fdb_info->addr, fdb_info->vid,
3579 host_addr ? " as host address" : "");
3581 INIT_WORK(&switchdev_work->work, dsa_user_switchdev_event_work);
3582 switchdev_work->event = event;
3583 switchdev_work->dev = dev;
3584 switchdev_work->orig_dev = orig_dev;
3586 ether_addr_copy(switchdev_work->addr, fdb_info->addr);
3587 switchdev_work->vid = fdb_info->vid;
3588 switchdev_work->host_addr = host_addr;
3590 dsa_schedule_work(&switchdev_work->work);
3595 /* Called under rcu_read_lock() */
3596 static int dsa_user_switchdev_event(struct notifier_block *unused,
3597 unsigned long event, void *ptr)
3599 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3603 case SWITCHDEV_PORT_ATTR_SET:
3604 err = switchdev_handle_port_attr_set(dev, ptr,
3606 dsa_user_port_attr_set);
3607 return notifier_from_errno(err);
3608 case SWITCHDEV_FDB_ADD_TO_DEVICE:
3609 case SWITCHDEV_FDB_DEL_TO_DEVICE:
3610 err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
3612 dsa_foreign_dev_check,
3613 dsa_user_fdb_event);
3614 return notifier_from_errno(err);
3622 static int dsa_user_switchdev_blocking_event(struct notifier_block *unused,
3623 unsigned long event, void *ptr)
3625 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3629 case SWITCHDEV_PORT_OBJ_ADD:
3630 err = switchdev_handle_port_obj_add_foreign(dev, ptr,
3632 dsa_foreign_dev_check,
3633 dsa_user_port_obj_add);
3634 return notifier_from_errno(err);
3635 case SWITCHDEV_PORT_OBJ_DEL:
3636 err = switchdev_handle_port_obj_del_foreign(dev, ptr,
3638 dsa_foreign_dev_check,
3639 dsa_user_port_obj_del);
3640 return notifier_from_errno(err);
3641 case SWITCHDEV_PORT_ATTR_SET:
3642 err = switchdev_handle_port_attr_set(dev, ptr,
3644 dsa_user_port_attr_set);
3645 return notifier_from_errno(err);
3651 static struct notifier_block dsa_user_nb __read_mostly = {
3652 .notifier_call = dsa_user_netdevice_event,
3655 struct notifier_block dsa_user_switchdev_notifier = {
3656 .notifier_call = dsa_user_switchdev_event,
3659 struct notifier_block dsa_user_switchdev_blocking_notifier = {
3660 .notifier_call = dsa_user_switchdev_blocking_event,
3663 int dsa_user_register_notifier(void)
3665 struct notifier_block *nb;
3668 err = register_netdevice_notifier(&dsa_user_nb);
3672 err = register_switchdev_notifier(&dsa_user_switchdev_notifier);
3674 goto err_switchdev_nb;
3676 nb = &dsa_user_switchdev_blocking_notifier;
3677 err = register_switchdev_blocking_notifier(nb);
3679 goto err_switchdev_blocking_nb;
3683 err_switchdev_blocking_nb:
3684 unregister_switchdev_notifier(&dsa_user_switchdev_notifier);
3686 unregister_netdevice_notifier(&dsa_user_nb);
3690 void dsa_user_unregister_notifier(void)
3692 struct notifier_block *nb;
3695 nb = &dsa_user_switchdev_blocking_notifier;
3696 err = unregister_switchdev_blocking_notifier(nb);
3698 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
3700 err = unregister_switchdev_notifier(&dsa_user_switchdev_notifier);
3702 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3704 err = unregister_netdevice_notifier(&dsa_user_nb);
3706 pr_err("DSA: failed to unregister user notifier (%d)\n", err);