2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/list.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/phylink.h>
17 #include <linux/of_net.h>
18 #include <linux/of_mdio.h>
19 #include <linux/mdio.h>
20 #include <net/rtnetlink.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_mirred.h>
23 #include <linux/if_bridge.h>
24 #include <linux/netpoll.h>
25 #include <linux/ptp_classify.h>
29 static bool dsa_slave_dev_check(struct net_device *dev);
31 /* slave mii_bus handling ***************************************************/
32 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
34 struct dsa_switch *ds = bus->priv;
36 if (ds->phys_mii_mask & (1 << addr))
37 return ds->ops->phy_read(ds, addr, reg);
42 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
44 struct dsa_switch *ds = bus->priv;
46 if (ds->phys_mii_mask & (1 << addr))
47 return ds->ops->phy_write(ds, addr, reg, val);
52 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
54 ds->slave_mii_bus->priv = (void *)ds;
55 ds->slave_mii_bus->name = "dsa slave smi";
56 ds->slave_mii_bus->read = dsa_slave_phy_read;
57 ds->slave_mii_bus->write = dsa_slave_phy_write;
58 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
59 ds->dst->index, ds->index);
60 ds->slave_mii_bus->parent = ds->dev;
61 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
65 /* slave device handling ****************************************************/
66 static int dsa_slave_get_iflink(const struct net_device *dev)
68 return dsa_slave_to_master(dev)->ifindex;
71 static int dsa_slave_open(struct net_device *dev)
73 struct net_device *master = dsa_slave_to_master(dev);
74 struct dsa_port *dp = dsa_slave_to_port(dev);
77 if (!(master->flags & IFF_UP))
80 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
81 err = dev_uc_add(master, dev->dev_addr);
86 if (dev->flags & IFF_ALLMULTI) {
87 err = dev_set_allmulti(master, 1);
91 if (dev->flags & IFF_PROMISC) {
92 err = dev_set_promiscuity(master, 1);
97 err = dsa_port_enable(dp, dev->phydev);
101 phylink_start(dp->pl);
106 if (dev->flags & IFF_PROMISC)
107 dev_set_promiscuity(master, -1);
109 if (dev->flags & IFF_ALLMULTI)
110 dev_set_allmulti(master, -1);
112 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
113 dev_uc_del(master, dev->dev_addr);
118 static int dsa_slave_close(struct net_device *dev)
120 struct net_device *master = dsa_slave_to_master(dev);
121 struct dsa_port *dp = dsa_slave_to_port(dev);
123 phylink_stop(dp->pl);
125 dsa_port_disable(dp, dev->phydev);
127 dev_mc_unsync(master, dev);
128 dev_uc_unsync(master, dev);
129 if (dev->flags & IFF_ALLMULTI)
130 dev_set_allmulti(master, -1);
131 if (dev->flags & IFF_PROMISC)
132 dev_set_promiscuity(master, -1);
134 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
135 dev_uc_del(master, dev->dev_addr);
140 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
142 struct net_device *master = dsa_slave_to_master(dev);
144 if (change & IFF_ALLMULTI)
145 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
146 if (change & IFF_PROMISC)
147 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
150 static void dsa_slave_set_rx_mode(struct net_device *dev)
152 struct net_device *master = dsa_slave_to_master(dev);
154 dev_mc_sync(master, dev);
155 dev_uc_sync(master, dev);
158 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
160 struct net_device *master = dsa_slave_to_master(dev);
161 struct sockaddr *addr = a;
164 if (!is_valid_ether_addr(addr->sa_data))
165 return -EADDRNOTAVAIL;
167 if (!(dev->flags & IFF_UP))
170 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
171 err = dev_uc_add(master, addr->sa_data);
176 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
177 dev_uc_del(master, dev->dev_addr);
180 ether_addr_copy(dev->dev_addr, addr->sa_data);
185 struct dsa_slave_dump_ctx {
186 struct net_device *dev;
188 struct netlink_callback *cb;
193 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
194 bool is_static, void *data)
196 struct dsa_slave_dump_ctx *dump = data;
197 u32 portid = NETLINK_CB(dump->cb->skb).portid;
198 u32 seq = dump->cb->nlh->nlmsg_seq;
199 struct nlmsghdr *nlh;
202 if (dump->idx < dump->cb->args[2])
205 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
206 sizeof(*ndm), NLM_F_MULTI);
210 ndm = nlmsg_data(nlh);
211 ndm->ndm_family = AF_BRIDGE;
214 ndm->ndm_flags = NTF_SELF;
216 ndm->ndm_ifindex = dump->dev->ifindex;
217 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
219 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
220 goto nla_put_failure;
222 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
223 goto nla_put_failure;
225 nlmsg_end(dump->skb, nlh);
232 nlmsg_cancel(dump->skb, nlh);
237 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
238 struct net_device *dev, struct net_device *filter_dev,
241 struct dsa_port *dp = dsa_slave_to_port(dev);
242 struct dsa_slave_dump_ctx dump = {
250 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
256 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
258 struct dsa_slave_priv *p = netdev_priv(dev);
259 struct dsa_switch *ds = p->dp->ds;
260 int port = p->dp->index;
262 /* Pass through to switch driver if it supports timestamping */
265 if (ds->ops->port_hwtstamp_get)
266 return ds->ops->port_hwtstamp_get(ds, port, ifr);
269 if (ds->ops->port_hwtstamp_set)
270 return ds->ops->port_hwtstamp_set(ds, port, ifr);
274 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
277 static int dsa_slave_port_attr_set(struct net_device *dev,
278 const struct switchdev_attr *attr,
279 struct switchdev_trans *trans)
281 struct dsa_port *dp = dsa_slave_to_port(dev);
285 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
286 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
288 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
289 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
292 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
293 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
303 static int dsa_slave_port_obj_add(struct net_device *dev,
304 const struct switchdev_obj *obj,
305 struct switchdev_trans *trans)
307 struct dsa_port *dp = dsa_slave_to_port(dev);
310 /* For the prepare phase, ensure the full set of changes is feasable in
311 * one go in order to signal a failure properly. If an operation is not
312 * supported, return -EOPNOTSUPP.
316 case SWITCHDEV_OBJ_ID_PORT_MDB:
317 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
319 case SWITCHDEV_OBJ_ID_HOST_MDB:
320 /* DSA can directly translate this to a normal MDB add,
321 * but on the CPU port.
323 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
326 case SWITCHDEV_OBJ_ID_PORT_VLAN:
327 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
338 static int dsa_slave_port_obj_del(struct net_device *dev,
339 const struct switchdev_obj *obj)
341 struct dsa_port *dp = dsa_slave_to_port(dev);
345 case SWITCHDEV_OBJ_ID_PORT_MDB:
346 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
348 case SWITCHDEV_OBJ_ID_HOST_MDB:
349 /* DSA can directly translate this to a normal MDB add,
350 * but on the CPU port.
352 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
354 case SWITCHDEV_OBJ_ID_PORT_VLAN:
355 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
365 static int dsa_slave_port_attr_get(struct net_device *dev,
366 struct switchdev_attr *attr)
368 struct dsa_port *dp = dsa_slave_to_port(dev);
369 struct dsa_switch *ds = dp->ds;
370 struct dsa_switch_tree *dst = ds->dst;
373 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
374 attr->u.ppid.id_len = sizeof(dst->index);
375 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
377 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
378 attr->u.brport_flags_support = 0;
387 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
390 #ifdef CONFIG_NET_POLL_CONTROLLER
391 struct dsa_slave_priv *p = netdev_priv(dev);
394 netpoll_send_skb(p->netpoll, skb);
401 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
404 struct dsa_switch *ds = p->dp->ds;
405 struct sk_buff *clone;
408 type = ptp_classify_raw(skb);
409 if (type == PTP_CLASS_NONE)
412 if (!ds->ops->port_txtstamp)
415 clone = skb_clone_sk(skb);
419 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
425 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
427 struct dsa_slave_priv *p = netdev_priv(dev);
428 struct pcpu_sw_netstats *s;
429 struct sk_buff *nskb;
431 s = this_cpu_ptr(p->stats64);
432 u64_stats_update_begin(&s->syncp);
434 s->tx_bytes += skb->len;
435 u64_stats_update_end(&s->syncp);
437 /* Identify PTP protocol packets, clone them, and pass them to the
440 dsa_skb_tx_timestamp(p, skb);
442 /* Transmit function may have to reallocate the original SKB,
443 * in which case it must have freed it. Only free it here on error.
445 nskb = p->xmit(skb, dev);
451 /* SKB for netpoll still need to be mangled with the protocol-specific
452 * tag to be successfully transmitted
454 if (unlikely(netpoll_tx_running(dev)))
455 return dsa_slave_netpoll_send_skb(dev, nskb);
457 /* Queue the SKB for transmission on the parent interface, but
458 * do not modify its EtherType
460 nskb->dev = dsa_slave_to_master(dev);
461 dev_queue_xmit(nskb);
466 /* ethtool operations *******************************************************/
468 static void dsa_slave_get_drvinfo(struct net_device *dev,
469 struct ethtool_drvinfo *drvinfo)
471 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
472 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
473 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
476 static int dsa_slave_get_regs_len(struct net_device *dev)
478 struct dsa_port *dp = dsa_slave_to_port(dev);
479 struct dsa_switch *ds = dp->ds;
481 if (ds->ops->get_regs_len)
482 return ds->ops->get_regs_len(ds, dp->index);
488 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
490 struct dsa_port *dp = dsa_slave_to_port(dev);
491 struct dsa_switch *ds = dp->ds;
493 if (ds->ops->get_regs)
494 ds->ops->get_regs(ds, dp->index, regs, _p);
497 static int dsa_slave_nway_reset(struct net_device *dev)
499 struct dsa_port *dp = dsa_slave_to_port(dev);
501 return phylink_ethtool_nway_reset(dp->pl);
504 static int dsa_slave_get_eeprom_len(struct net_device *dev)
506 struct dsa_port *dp = dsa_slave_to_port(dev);
507 struct dsa_switch *ds = dp->ds;
509 if (ds->cd && ds->cd->eeprom_len)
510 return ds->cd->eeprom_len;
512 if (ds->ops->get_eeprom_len)
513 return ds->ops->get_eeprom_len(ds);
518 static int dsa_slave_get_eeprom(struct net_device *dev,
519 struct ethtool_eeprom *eeprom, u8 *data)
521 struct dsa_port *dp = dsa_slave_to_port(dev);
522 struct dsa_switch *ds = dp->ds;
524 if (ds->ops->get_eeprom)
525 return ds->ops->get_eeprom(ds, eeprom, data);
530 static int dsa_slave_set_eeprom(struct net_device *dev,
531 struct ethtool_eeprom *eeprom, u8 *data)
533 struct dsa_port *dp = dsa_slave_to_port(dev);
534 struct dsa_switch *ds = dp->ds;
536 if (ds->ops->set_eeprom)
537 return ds->ops->set_eeprom(ds, eeprom, data);
542 static void dsa_slave_get_strings(struct net_device *dev,
543 uint32_t stringset, uint8_t *data)
545 struct dsa_port *dp = dsa_slave_to_port(dev);
546 struct dsa_switch *ds = dp->ds;
548 if (stringset == ETH_SS_STATS) {
549 int len = ETH_GSTRING_LEN;
551 strncpy(data, "tx_packets", len);
552 strncpy(data + len, "tx_bytes", len);
553 strncpy(data + 2 * len, "rx_packets", len);
554 strncpy(data + 3 * len, "rx_bytes", len);
555 if (ds->ops->get_strings)
556 ds->ops->get_strings(ds, dp->index, stringset,
561 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
562 struct ethtool_stats *stats,
565 struct dsa_port *dp = dsa_slave_to_port(dev);
566 struct dsa_slave_priv *p = netdev_priv(dev);
567 struct dsa_switch *ds = dp->ds;
568 struct pcpu_sw_netstats *s;
572 for_each_possible_cpu(i) {
573 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
575 s = per_cpu_ptr(p->stats64, i);
577 start = u64_stats_fetch_begin_irq(&s->syncp);
578 tx_packets = s->tx_packets;
579 tx_bytes = s->tx_bytes;
580 rx_packets = s->rx_packets;
581 rx_bytes = s->rx_bytes;
582 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
583 data[0] += tx_packets;
585 data[2] += rx_packets;
588 if (ds->ops->get_ethtool_stats)
589 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
592 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
594 struct dsa_port *dp = dsa_slave_to_port(dev);
595 struct dsa_switch *ds = dp->ds;
597 if (sset == ETH_SS_STATS) {
601 if (ds->ops->get_sset_count)
602 count += ds->ops->get_sset_count(ds, dp->index, sset);
610 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
612 struct dsa_port *dp = dsa_slave_to_port(dev);
613 struct dsa_switch *ds = dp->ds;
615 phylink_ethtool_get_wol(dp->pl, w);
617 if (ds->ops->get_wol)
618 ds->ops->get_wol(ds, dp->index, w);
621 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
623 struct dsa_port *dp = dsa_slave_to_port(dev);
624 struct dsa_switch *ds = dp->ds;
625 int ret = -EOPNOTSUPP;
627 phylink_ethtool_set_wol(dp->pl, w);
629 if (ds->ops->set_wol)
630 ret = ds->ops->set_wol(ds, dp->index, w);
635 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
637 struct dsa_port *dp = dsa_slave_to_port(dev);
638 struct dsa_switch *ds = dp->ds;
641 /* Port's PHY and MAC both need to be EEE capable */
642 if (!dev->phydev && !dp->pl)
645 if (!ds->ops->set_mac_eee)
648 ret = ds->ops->set_mac_eee(ds, dp->index, e);
652 return phylink_ethtool_set_eee(dp->pl, e);
655 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
657 struct dsa_port *dp = dsa_slave_to_port(dev);
658 struct dsa_switch *ds = dp->ds;
661 /* Port's PHY and MAC both need to be EEE capable */
662 if (!dev->phydev && !dp->pl)
665 if (!ds->ops->get_mac_eee)
668 ret = ds->ops->get_mac_eee(ds, dp->index, e);
672 return phylink_ethtool_get_eee(dp->pl, e);
675 static int dsa_slave_get_link_ksettings(struct net_device *dev,
676 struct ethtool_link_ksettings *cmd)
678 struct dsa_port *dp = dsa_slave_to_port(dev);
680 return phylink_ethtool_ksettings_get(dp->pl, cmd);
683 static int dsa_slave_set_link_ksettings(struct net_device *dev,
684 const struct ethtool_link_ksettings *cmd)
686 struct dsa_port *dp = dsa_slave_to_port(dev);
688 return phylink_ethtool_ksettings_set(dp->pl, cmd);
691 #ifdef CONFIG_NET_POLL_CONTROLLER
692 static int dsa_slave_netpoll_setup(struct net_device *dev,
693 struct netpoll_info *ni)
695 struct net_device *master = dsa_slave_to_master(dev);
696 struct dsa_slave_priv *p = netdev_priv(dev);
697 struct netpoll *netpoll;
700 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
704 err = __netpoll_setup(netpoll, master);
710 p->netpoll = netpoll;
715 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
717 struct dsa_slave_priv *p = netdev_priv(dev);
718 struct netpoll *netpoll = p->netpoll;
725 __netpoll_free_async(netpoll);
728 static void dsa_slave_poll_controller(struct net_device *dev)
733 static int dsa_slave_get_phys_port_name(struct net_device *dev,
734 char *name, size_t len)
736 struct dsa_port *dp = dsa_slave_to_port(dev);
738 if (snprintf(name, len, "p%d", dp->index) >= len)
744 static struct dsa_mall_tc_entry *
745 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
747 struct dsa_slave_priv *p = netdev_priv(dev);
748 struct dsa_mall_tc_entry *mall_tc_entry;
750 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
751 if (mall_tc_entry->cookie == cookie)
752 return mall_tc_entry;
757 static int dsa_slave_add_cls_matchall(struct net_device *dev,
758 struct tc_cls_matchall_offload *cls,
761 struct dsa_port *dp = dsa_slave_to_port(dev);
762 struct dsa_slave_priv *p = netdev_priv(dev);
763 struct dsa_mall_tc_entry *mall_tc_entry;
764 __be16 protocol = cls->common.protocol;
765 struct dsa_switch *ds = dp->ds;
766 struct net_device *to_dev;
767 const struct tc_action *a;
768 struct dsa_port *to_dp;
769 int err = -EOPNOTSUPP;
771 if (!ds->ops->port_mirror_add)
774 if (!tcf_exts_has_one_action(cls->exts))
777 a = tcf_exts_first_action(cls->exts);
779 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
780 struct dsa_mall_mirror_tc_entry *mirror;
782 to_dev = tcf_mirred_dev(a);
786 if (!dsa_slave_dev_check(to_dev))
789 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
793 mall_tc_entry->cookie = cls->cookie;
794 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
795 mirror = &mall_tc_entry->mirror;
797 to_dp = dsa_slave_to_port(to_dev);
799 mirror->to_local_port = to_dp->index;
800 mirror->ingress = ingress;
802 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
804 kfree(mall_tc_entry);
808 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
814 static void dsa_slave_del_cls_matchall(struct net_device *dev,
815 struct tc_cls_matchall_offload *cls)
817 struct dsa_port *dp = dsa_slave_to_port(dev);
818 struct dsa_mall_tc_entry *mall_tc_entry;
819 struct dsa_switch *ds = dp->ds;
821 if (!ds->ops->port_mirror_del)
824 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
828 list_del(&mall_tc_entry->list);
830 switch (mall_tc_entry->type) {
831 case DSA_PORT_MALL_MIRROR:
832 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
838 kfree(mall_tc_entry);
841 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
842 struct tc_cls_matchall_offload *cls,
845 if (cls->common.chain_index)
848 switch (cls->command) {
849 case TC_CLSMATCHALL_REPLACE:
850 return dsa_slave_add_cls_matchall(dev, cls, ingress);
851 case TC_CLSMATCHALL_DESTROY:
852 dsa_slave_del_cls_matchall(dev, cls);
859 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
860 void *cb_priv, bool ingress)
862 struct net_device *dev = cb_priv;
864 if (!tc_can_offload(dev))
868 case TC_SETUP_CLSMATCHALL:
869 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
875 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
876 void *type_data, void *cb_priv)
878 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
881 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
882 void *type_data, void *cb_priv)
884 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
887 static int dsa_slave_setup_tc_block(struct net_device *dev,
888 struct tc_block_offload *f)
892 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
893 cb = dsa_slave_setup_tc_block_cb_ig;
894 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
895 cb = dsa_slave_setup_tc_block_cb_eg;
899 switch (f->command) {
901 return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
902 case TC_BLOCK_UNBIND:
903 tcf_block_cb_unregister(f->block, cb, dev);
910 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
915 return dsa_slave_setup_tc_block(dev, type_data);
921 static void dsa_slave_get_stats64(struct net_device *dev,
922 struct rtnl_link_stats64 *stats)
924 struct dsa_slave_priv *p = netdev_priv(dev);
925 struct pcpu_sw_netstats *s;
929 netdev_stats_to_stats64(stats, &dev->stats);
930 for_each_possible_cpu(i) {
931 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
933 s = per_cpu_ptr(p->stats64, i);
935 start = u64_stats_fetch_begin_irq(&s->syncp);
936 tx_packets = s->tx_packets;
937 tx_bytes = s->tx_bytes;
938 rx_packets = s->rx_packets;
939 rx_bytes = s->rx_bytes;
940 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
942 stats->tx_packets += tx_packets;
943 stats->tx_bytes += tx_bytes;
944 stats->rx_packets += rx_packets;
945 stats->rx_bytes += rx_bytes;
949 static int dsa_slave_get_rxnfc(struct net_device *dev,
950 struct ethtool_rxnfc *nfc, u32 *rule_locs)
952 struct dsa_port *dp = dsa_slave_to_port(dev);
953 struct dsa_switch *ds = dp->ds;
955 if (!ds->ops->get_rxnfc)
958 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
961 static int dsa_slave_set_rxnfc(struct net_device *dev,
962 struct ethtool_rxnfc *nfc)
964 struct dsa_port *dp = dsa_slave_to_port(dev);
965 struct dsa_switch *ds = dp->ds;
967 if (!ds->ops->set_rxnfc)
970 return ds->ops->set_rxnfc(ds, dp->index, nfc);
973 static int dsa_slave_get_ts_info(struct net_device *dev,
974 struct ethtool_ts_info *ts)
976 struct dsa_slave_priv *p = netdev_priv(dev);
977 struct dsa_switch *ds = p->dp->ds;
979 if (!ds->ops->get_ts_info)
982 return ds->ops->get_ts_info(ds, p->dp->index, ts);
985 static const struct ethtool_ops dsa_slave_ethtool_ops = {
986 .get_drvinfo = dsa_slave_get_drvinfo,
987 .get_regs_len = dsa_slave_get_regs_len,
988 .get_regs = dsa_slave_get_regs,
989 .nway_reset = dsa_slave_nway_reset,
990 .get_link = ethtool_op_get_link,
991 .get_eeprom_len = dsa_slave_get_eeprom_len,
992 .get_eeprom = dsa_slave_get_eeprom,
993 .set_eeprom = dsa_slave_set_eeprom,
994 .get_strings = dsa_slave_get_strings,
995 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
996 .get_sset_count = dsa_slave_get_sset_count,
997 .set_wol = dsa_slave_set_wol,
998 .get_wol = dsa_slave_get_wol,
999 .set_eee = dsa_slave_set_eee,
1000 .get_eee = dsa_slave_get_eee,
1001 .get_link_ksettings = dsa_slave_get_link_ksettings,
1002 .set_link_ksettings = dsa_slave_set_link_ksettings,
1003 .get_rxnfc = dsa_slave_get_rxnfc,
1004 .set_rxnfc = dsa_slave_set_rxnfc,
1005 .get_ts_info = dsa_slave_get_ts_info,
1008 /* legacy way, bypassing the bridge *****************************************/
1009 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1010 struct net_device *dev,
1011 const unsigned char *addr, u16 vid,
1014 struct dsa_port *dp = dsa_slave_to_port(dev);
1016 return dsa_port_fdb_add(dp, addr, vid);
1019 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1020 struct net_device *dev,
1021 const unsigned char *addr, u16 vid)
1023 struct dsa_port *dp = dsa_slave_to_port(dev);
1025 return dsa_port_fdb_del(dp, addr, vid);
1028 static const struct net_device_ops dsa_slave_netdev_ops = {
1029 .ndo_open = dsa_slave_open,
1030 .ndo_stop = dsa_slave_close,
1031 .ndo_start_xmit = dsa_slave_xmit,
1032 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1033 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1034 .ndo_set_mac_address = dsa_slave_set_mac_address,
1035 .ndo_fdb_add = dsa_legacy_fdb_add,
1036 .ndo_fdb_del = dsa_legacy_fdb_del,
1037 .ndo_fdb_dump = dsa_slave_fdb_dump,
1038 .ndo_do_ioctl = dsa_slave_ioctl,
1039 .ndo_get_iflink = dsa_slave_get_iflink,
1040 #ifdef CONFIG_NET_POLL_CONTROLLER
1041 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1042 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1043 .ndo_poll_controller = dsa_slave_poll_controller,
1045 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1046 .ndo_setup_tc = dsa_slave_setup_tc,
1047 .ndo_get_stats64 = dsa_slave_get_stats64,
1050 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1051 .switchdev_port_attr_get = dsa_slave_port_attr_get,
1052 .switchdev_port_attr_set = dsa_slave_port_attr_set,
1053 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1054 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1057 static struct device_type dsa_type = {
1061 static void dsa_slave_phylink_validate(struct net_device *dev,
1062 unsigned long *supported,
1063 struct phylink_link_state *state)
1065 struct dsa_port *dp = dsa_slave_to_port(dev);
1066 struct dsa_switch *ds = dp->ds;
1068 if (!ds->ops->phylink_validate)
1071 ds->ops->phylink_validate(ds, dp->index, supported, state);
1074 static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1075 struct phylink_link_state *state)
1077 struct dsa_port *dp = dsa_slave_to_port(dev);
1078 struct dsa_switch *ds = dp->ds;
1080 /* Only called for SGMII and 802.3z */
1081 if (!ds->ops->phylink_mac_link_state)
1084 return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1087 static void dsa_slave_phylink_mac_config(struct net_device *dev,
1089 const struct phylink_link_state *state)
1091 struct dsa_port *dp = dsa_slave_to_port(dev);
1092 struct dsa_switch *ds = dp->ds;
1094 if (!ds->ops->phylink_mac_config)
1097 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1100 static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1102 struct dsa_port *dp = dsa_slave_to_port(dev);
1103 struct dsa_switch *ds = dp->ds;
1105 if (!ds->ops->phylink_mac_an_restart)
1108 ds->ops->phylink_mac_an_restart(ds, dp->index);
1111 static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1113 phy_interface_t interface)
1115 struct dsa_port *dp = dsa_slave_to_port(dev);
1116 struct dsa_switch *ds = dp->ds;
1118 if (!ds->ops->phylink_mac_link_down) {
1119 if (ds->ops->adjust_link && dev->phydev)
1120 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1124 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1127 static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1129 phy_interface_t interface,
1130 struct phy_device *phydev)
1132 struct dsa_port *dp = dsa_slave_to_port(dev);
1133 struct dsa_switch *ds = dp->ds;
1135 if (!ds->ops->phylink_mac_link_up) {
1136 if (ds->ops->adjust_link && dev->phydev)
1137 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1141 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1144 static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1145 .validate = dsa_slave_phylink_validate,
1146 .mac_link_state = dsa_slave_phylink_mac_link_state,
1147 .mac_config = dsa_slave_phylink_mac_config,
1148 .mac_an_restart = dsa_slave_phylink_mac_an_restart,
1149 .mac_link_down = dsa_slave_phylink_mac_link_down,
1150 .mac_link_up = dsa_slave_phylink_mac_link_up,
1153 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1155 const struct dsa_port *dp = dsa_to_port(ds, port);
1157 phylink_mac_change(dp->pl, up);
1159 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1161 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1162 struct phylink_link_state *state)
1164 struct dsa_port *dp = dsa_slave_to_port(dev);
1165 struct dsa_switch *ds = dp->ds;
1167 /* No need to check that this operation is valid, the callback would
1168 * not be called if it was not.
1170 ds->ops->phylink_fixed_state(ds, dp->index, state);
1173 /* slave device setup *******************************************************/
1174 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1176 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1177 struct dsa_switch *ds = dp->ds;
1179 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1180 if (!slave_dev->phydev) {
1181 netdev_err(slave_dev, "no phy at %d\n", addr);
1185 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1188 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1190 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1191 struct device_node *port_dn = dp->dn;
1192 struct dsa_switch *ds = dp->ds;
1196 mode = of_get_phy_mode(port_dn);
1198 mode = PHY_INTERFACE_MODE_NA;
1200 dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1201 &dsa_slave_phylink_mac_ops);
1202 if (IS_ERR(dp->pl)) {
1203 netdev_err(slave_dev,
1204 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1205 return PTR_ERR(dp->pl);
1208 /* Register only if the switch provides such a callback, since this
1209 * callback takes precedence over polling the link GPIO in PHYLINK
1210 * (see phylink_get_fixed_state).
1212 if (ds->ops->phylink_fixed_state)
1213 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1215 if (ds->ops->get_phy_flags)
1216 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1218 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1219 if (ret == -ENODEV) {
1220 /* We could not connect to a designated PHY or SFP, so use the
1221 * switch internal MDIO bus instead
1223 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1225 netdev_err(slave_dev,
1226 "failed to connect to port %d: %d\n",
1228 phylink_destroy(dp->pl);
1236 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1237 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1238 struct netdev_queue *txq,
1241 lockdep_set_class(&txq->_xmit_lock,
1242 &dsa_slave_netdev_xmit_lock_key);
1245 int dsa_slave_suspend(struct net_device *slave_dev)
1247 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1249 if (!netif_running(slave_dev))
1252 netif_device_detach(slave_dev);
1255 phylink_stop(dp->pl);
1261 int dsa_slave_resume(struct net_device *slave_dev)
1263 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1265 if (!netif_running(slave_dev))
1268 netif_device_attach(slave_dev);
1271 phylink_start(dp->pl);
1277 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1279 struct net_device *master = dsa_slave_to_master(dev);
1280 struct dsa_port *dp = dsa_slave_to_port(dev);
1281 struct dsa_notifier_register_info rinfo = {
1282 .switch_number = dp->ds->index,
1283 .port_number = dp->index,
1288 call_dsa_notifiers(val, dev, &rinfo.info);
1291 int dsa_slave_create(struct dsa_port *port)
1293 const struct dsa_port *cpu_dp = port->cpu_dp;
1294 struct net_device *master = cpu_dp->master;
1295 struct dsa_switch *ds = port->ds;
1296 const char *name = port->name;
1297 struct net_device *slave_dev;
1298 struct dsa_slave_priv *p;
1301 if (!ds->num_tx_queues)
1302 ds->num_tx_queues = 1;
1304 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1305 NET_NAME_UNKNOWN, ether_setup,
1306 ds->num_tx_queues, 1);
1307 if (slave_dev == NULL)
1310 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1311 slave_dev->hw_features |= NETIF_F_HW_TC;
1312 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1313 eth_hw_addr_inherit(slave_dev, master);
1314 slave_dev->priv_flags |= IFF_NO_QUEUE;
1315 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1316 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1317 slave_dev->min_mtu = 0;
1318 slave_dev->max_mtu = ETH_MAX_MTU;
1319 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1321 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1324 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1325 slave_dev->dev.of_node = port->dn;
1326 slave_dev->vlan_features = master->vlan_features;
1328 p = netdev_priv(slave_dev);
1329 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1331 free_netdev(slave_dev);
1335 INIT_LIST_HEAD(&p->mall_tc_list);
1336 p->xmit = cpu_dp->tag_ops->xmit;
1337 port->slave = slave_dev;
1339 netif_carrier_off(slave_dev);
1341 ret = dsa_slave_phy_setup(slave_dev);
1343 netdev_err(master, "error %d setting up slave phy\n", ret);
1347 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1349 ret = register_netdev(slave_dev);
1351 netdev_err(master, "error %d registering interface %s\n",
1352 ret, slave_dev->name);
1360 phylink_disconnect_phy(p->dp->pl);
1362 phylink_destroy(p->dp->pl);
1364 free_percpu(p->stats64);
1365 free_netdev(slave_dev);
1370 void dsa_slave_destroy(struct net_device *slave_dev)
1372 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1373 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1375 netif_carrier_off(slave_dev);
1377 phylink_disconnect_phy(dp->pl);
1380 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1381 unregister_netdev(slave_dev);
1382 phylink_destroy(dp->pl);
1383 free_percpu(p->stats64);
1384 free_netdev(slave_dev);
1387 static bool dsa_slave_dev_check(struct net_device *dev)
1389 return dev->netdev_ops == &dsa_slave_netdev_ops;
1392 static int dsa_slave_changeupper(struct net_device *dev,
1393 struct netdev_notifier_changeupper_info *info)
1395 struct dsa_port *dp = dsa_slave_to_port(dev);
1396 int err = NOTIFY_DONE;
1398 if (netif_is_bridge_master(info->upper_dev)) {
1399 if (info->linking) {
1400 err = dsa_port_bridge_join(dp, info->upper_dev);
1401 err = notifier_from_errno(err);
1403 dsa_port_bridge_leave(dp, info->upper_dev);
1411 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1412 unsigned long event, void *ptr)
1414 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1416 if (!dsa_slave_dev_check(dev))
1419 if (event == NETDEV_CHANGEUPPER)
1420 return dsa_slave_changeupper(dev, ptr);
1425 struct dsa_switchdev_event_work {
1426 struct work_struct work;
1427 struct switchdev_notifier_fdb_info fdb_info;
1428 struct net_device *dev;
1429 unsigned long event;
1432 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1434 struct dsa_switchdev_event_work *switchdev_work =
1435 container_of(work, struct dsa_switchdev_event_work, work);
1436 struct net_device *dev = switchdev_work->dev;
1437 struct switchdev_notifier_fdb_info *fdb_info;
1438 struct dsa_port *dp = dsa_slave_to_port(dev);
1442 switch (switchdev_work->event) {
1443 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1444 fdb_info = &switchdev_work->fdb_info;
1445 if (!fdb_info->added_by_user)
1448 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1450 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1453 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1457 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1458 fdb_info = &switchdev_work->fdb_info;
1459 if (!fdb_info->added_by_user)
1462 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1464 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1471 kfree(switchdev_work->fdb_info.addr);
1472 kfree(switchdev_work);
1477 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1479 const struct switchdev_notifier_fdb_info *
1482 memcpy(&switchdev_work->fdb_info, fdb_info,
1483 sizeof(switchdev_work->fdb_info));
1484 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1485 if (!switchdev_work->fdb_info.addr)
1487 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1492 /* Called under rcu_read_lock() */
1493 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1494 unsigned long event, void *ptr)
1496 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1497 struct dsa_switchdev_event_work *switchdev_work;
1499 if (!dsa_slave_dev_check(dev))
1502 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1503 if (!switchdev_work)
1506 INIT_WORK(&switchdev_work->work,
1507 dsa_slave_switchdev_event_work);
1508 switchdev_work->dev = dev;
1509 switchdev_work->event = event;
1512 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1513 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1514 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1515 goto err_fdb_work_init;
1519 kfree(switchdev_work);
1523 dsa_schedule_work(&switchdev_work->work);
1527 kfree(switchdev_work);
1531 static struct notifier_block dsa_slave_nb __read_mostly = {
1532 .notifier_call = dsa_slave_netdevice_event,
1535 static struct notifier_block dsa_slave_switchdev_notifier = {
1536 .notifier_call = dsa_slave_switchdev_event,
1539 int dsa_slave_register_notifier(void)
1543 err = register_netdevice_notifier(&dsa_slave_nb);
1547 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1549 goto err_switchdev_nb;
1554 unregister_netdevice_notifier(&dsa_slave_nb);
1558 void dsa_slave_unregister_notifier(void)
1562 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1564 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1566 err = unregister_netdevice_notifier(&dsa_slave_nb);
1568 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);