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);
143 if (dev->flags & IFF_UP) {
144 if (change & IFF_ALLMULTI)
145 dev_set_allmulti(master,
146 dev->flags & IFF_ALLMULTI ? 1 : -1);
147 if (change & IFF_PROMISC)
148 dev_set_promiscuity(master,
149 dev->flags & IFF_PROMISC ? 1 : -1);
153 static void dsa_slave_set_rx_mode(struct net_device *dev)
155 struct net_device *master = dsa_slave_to_master(dev);
157 dev_mc_sync(master, dev);
158 dev_uc_sync(master, dev);
161 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
163 struct net_device *master = dsa_slave_to_master(dev);
164 struct sockaddr *addr = a;
167 if (!is_valid_ether_addr(addr->sa_data))
168 return -EADDRNOTAVAIL;
170 if (!(dev->flags & IFF_UP))
173 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
174 err = dev_uc_add(master, addr->sa_data);
179 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
180 dev_uc_del(master, dev->dev_addr);
183 ether_addr_copy(dev->dev_addr, addr->sa_data);
188 struct dsa_slave_dump_ctx {
189 struct net_device *dev;
191 struct netlink_callback *cb;
196 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
197 bool is_static, void *data)
199 struct dsa_slave_dump_ctx *dump = data;
200 u32 portid = NETLINK_CB(dump->cb->skb).portid;
201 u32 seq = dump->cb->nlh->nlmsg_seq;
202 struct nlmsghdr *nlh;
205 if (dump->idx < dump->cb->args[2])
208 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
209 sizeof(*ndm), NLM_F_MULTI);
213 ndm = nlmsg_data(nlh);
214 ndm->ndm_family = AF_BRIDGE;
217 ndm->ndm_flags = NTF_SELF;
219 ndm->ndm_ifindex = dump->dev->ifindex;
220 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
222 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
223 goto nla_put_failure;
225 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
226 goto nla_put_failure;
228 nlmsg_end(dump->skb, nlh);
235 nlmsg_cancel(dump->skb, nlh);
240 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
241 struct net_device *dev, struct net_device *filter_dev,
244 struct dsa_port *dp = dsa_slave_to_port(dev);
245 struct dsa_slave_dump_ctx dump = {
253 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
259 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
261 struct dsa_slave_priv *p = netdev_priv(dev);
262 struct dsa_switch *ds = p->dp->ds;
263 int port = p->dp->index;
265 /* Pass through to switch driver if it supports timestamping */
268 if (ds->ops->port_hwtstamp_get)
269 return ds->ops->port_hwtstamp_get(ds, port, ifr);
272 if (ds->ops->port_hwtstamp_set)
273 return ds->ops->port_hwtstamp_set(ds, port, ifr);
277 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
280 static int dsa_slave_port_attr_set(struct net_device *dev,
281 const struct switchdev_attr *attr,
282 struct switchdev_trans *trans)
284 struct dsa_port *dp = dsa_slave_to_port(dev);
288 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
289 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
291 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
292 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
295 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
296 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
306 static int dsa_slave_port_obj_add(struct net_device *dev,
307 const struct switchdev_obj *obj,
308 struct switchdev_trans *trans)
310 struct dsa_port *dp = dsa_slave_to_port(dev);
313 /* For the prepare phase, ensure the full set of changes is feasable in
314 * one go in order to signal a failure properly. If an operation is not
315 * supported, return -EOPNOTSUPP.
319 case SWITCHDEV_OBJ_ID_PORT_MDB:
320 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
322 case SWITCHDEV_OBJ_ID_HOST_MDB:
323 /* DSA can directly translate this to a normal MDB add,
324 * but on the CPU port.
326 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
329 case SWITCHDEV_OBJ_ID_PORT_VLAN:
330 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
341 static int dsa_slave_port_obj_del(struct net_device *dev,
342 const struct switchdev_obj *obj)
344 struct dsa_port *dp = dsa_slave_to_port(dev);
348 case SWITCHDEV_OBJ_ID_PORT_MDB:
349 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
351 case SWITCHDEV_OBJ_ID_HOST_MDB:
352 /* DSA can directly translate this to a normal MDB add,
353 * but on the CPU port.
355 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
357 case SWITCHDEV_OBJ_ID_PORT_VLAN:
358 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
368 static int dsa_slave_port_attr_get(struct net_device *dev,
369 struct switchdev_attr *attr)
371 struct dsa_port *dp = dsa_slave_to_port(dev);
372 struct dsa_switch *ds = dp->ds;
373 struct dsa_switch_tree *dst = ds->dst;
376 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
377 attr->u.ppid.id_len = sizeof(dst->index);
378 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
380 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
381 attr->u.brport_flags_support = 0;
390 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
393 #ifdef CONFIG_NET_POLL_CONTROLLER
394 struct dsa_slave_priv *p = netdev_priv(dev);
397 netpoll_send_skb(p->netpoll, skb);
404 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
407 struct dsa_switch *ds = p->dp->ds;
408 struct sk_buff *clone;
411 type = ptp_classify_raw(skb);
412 if (type == PTP_CLASS_NONE)
415 if (!ds->ops->port_txtstamp)
418 clone = skb_clone_sk(skb);
422 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
428 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
430 struct dsa_slave_priv *p = netdev_priv(dev);
431 struct pcpu_sw_netstats *s;
432 struct sk_buff *nskb;
434 s = this_cpu_ptr(p->stats64);
435 u64_stats_update_begin(&s->syncp);
437 s->tx_bytes += skb->len;
438 u64_stats_update_end(&s->syncp);
440 /* Identify PTP protocol packets, clone them, and pass them to the
443 dsa_skb_tx_timestamp(p, skb);
445 /* Transmit function may have to reallocate the original SKB,
446 * in which case it must have freed it. Only free it here on error.
448 nskb = p->xmit(skb, dev);
454 /* SKB for netpoll still need to be mangled with the protocol-specific
455 * tag to be successfully transmitted
457 if (unlikely(netpoll_tx_running(dev)))
458 return dsa_slave_netpoll_send_skb(dev, nskb);
460 /* Queue the SKB for transmission on the parent interface, but
461 * do not modify its EtherType
463 nskb->dev = dsa_slave_to_master(dev);
464 dev_queue_xmit(nskb);
469 /* ethtool operations *******************************************************/
471 static void dsa_slave_get_drvinfo(struct net_device *dev,
472 struct ethtool_drvinfo *drvinfo)
474 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
475 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
476 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
479 static int dsa_slave_get_regs_len(struct net_device *dev)
481 struct dsa_port *dp = dsa_slave_to_port(dev);
482 struct dsa_switch *ds = dp->ds;
484 if (ds->ops->get_regs_len)
485 return ds->ops->get_regs_len(ds, dp->index);
491 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
493 struct dsa_port *dp = dsa_slave_to_port(dev);
494 struct dsa_switch *ds = dp->ds;
496 if (ds->ops->get_regs)
497 ds->ops->get_regs(ds, dp->index, regs, _p);
500 static int dsa_slave_nway_reset(struct net_device *dev)
502 struct dsa_port *dp = dsa_slave_to_port(dev);
504 return phylink_ethtool_nway_reset(dp->pl);
507 static int dsa_slave_get_eeprom_len(struct net_device *dev)
509 struct dsa_port *dp = dsa_slave_to_port(dev);
510 struct dsa_switch *ds = dp->ds;
512 if (ds->cd && ds->cd->eeprom_len)
513 return ds->cd->eeprom_len;
515 if (ds->ops->get_eeprom_len)
516 return ds->ops->get_eeprom_len(ds);
521 static int dsa_slave_get_eeprom(struct net_device *dev,
522 struct ethtool_eeprom *eeprom, u8 *data)
524 struct dsa_port *dp = dsa_slave_to_port(dev);
525 struct dsa_switch *ds = dp->ds;
527 if (ds->ops->get_eeprom)
528 return ds->ops->get_eeprom(ds, eeprom, data);
533 static int dsa_slave_set_eeprom(struct net_device *dev,
534 struct ethtool_eeprom *eeprom, u8 *data)
536 struct dsa_port *dp = dsa_slave_to_port(dev);
537 struct dsa_switch *ds = dp->ds;
539 if (ds->ops->set_eeprom)
540 return ds->ops->set_eeprom(ds, eeprom, data);
545 static void dsa_slave_get_strings(struct net_device *dev,
546 uint32_t stringset, uint8_t *data)
548 struct dsa_port *dp = dsa_slave_to_port(dev);
549 struct dsa_switch *ds = dp->ds;
551 if (stringset == ETH_SS_STATS) {
552 int len = ETH_GSTRING_LEN;
554 strncpy(data, "tx_packets", len);
555 strncpy(data + len, "tx_bytes", len);
556 strncpy(data + 2 * len, "rx_packets", len);
557 strncpy(data + 3 * len, "rx_bytes", len);
558 if (ds->ops->get_strings)
559 ds->ops->get_strings(ds, dp->index, stringset,
564 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
565 struct ethtool_stats *stats,
568 struct dsa_port *dp = dsa_slave_to_port(dev);
569 struct dsa_slave_priv *p = netdev_priv(dev);
570 struct dsa_switch *ds = dp->ds;
571 struct pcpu_sw_netstats *s;
575 for_each_possible_cpu(i) {
576 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
578 s = per_cpu_ptr(p->stats64, i);
580 start = u64_stats_fetch_begin_irq(&s->syncp);
581 tx_packets = s->tx_packets;
582 tx_bytes = s->tx_bytes;
583 rx_packets = s->rx_packets;
584 rx_bytes = s->rx_bytes;
585 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
586 data[0] += tx_packets;
588 data[2] += rx_packets;
591 if (ds->ops->get_ethtool_stats)
592 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
595 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
597 struct dsa_port *dp = dsa_slave_to_port(dev);
598 struct dsa_switch *ds = dp->ds;
600 if (sset == ETH_SS_STATS) {
603 if (ds->ops->get_sset_count) {
604 count = ds->ops->get_sset_count(ds, dp->index, sset);
615 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
617 struct dsa_port *dp = dsa_slave_to_port(dev);
618 struct dsa_switch *ds = dp->ds;
620 phylink_ethtool_get_wol(dp->pl, w);
622 if (ds->ops->get_wol)
623 ds->ops->get_wol(ds, dp->index, w);
626 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
628 struct dsa_port *dp = dsa_slave_to_port(dev);
629 struct dsa_switch *ds = dp->ds;
630 int ret = -EOPNOTSUPP;
632 phylink_ethtool_set_wol(dp->pl, w);
634 if (ds->ops->set_wol)
635 ret = ds->ops->set_wol(ds, dp->index, w);
640 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
642 struct dsa_port *dp = dsa_slave_to_port(dev);
643 struct dsa_switch *ds = dp->ds;
646 /* Port's PHY and MAC both need to be EEE capable */
647 if (!dev->phydev || !dp->pl)
650 if (!ds->ops->set_mac_eee)
653 ret = ds->ops->set_mac_eee(ds, dp->index, e);
657 return phylink_ethtool_set_eee(dp->pl, e);
660 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
662 struct dsa_port *dp = dsa_slave_to_port(dev);
663 struct dsa_switch *ds = dp->ds;
666 /* Port's PHY and MAC both need to be EEE capable */
667 if (!dev->phydev || !dp->pl)
670 if (!ds->ops->get_mac_eee)
673 ret = ds->ops->get_mac_eee(ds, dp->index, e);
677 return phylink_ethtool_get_eee(dp->pl, e);
680 static int dsa_slave_get_link_ksettings(struct net_device *dev,
681 struct ethtool_link_ksettings *cmd)
683 struct dsa_port *dp = dsa_slave_to_port(dev);
685 return phylink_ethtool_ksettings_get(dp->pl, cmd);
688 static int dsa_slave_set_link_ksettings(struct net_device *dev,
689 const struct ethtool_link_ksettings *cmd)
691 struct dsa_port *dp = dsa_slave_to_port(dev);
693 return phylink_ethtool_ksettings_set(dp->pl, cmd);
696 #ifdef CONFIG_NET_POLL_CONTROLLER
697 static int dsa_slave_netpoll_setup(struct net_device *dev,
698 struct netpoll_info *ni)
700 struct net_device *master = dsa_slave_to_master(dev);
701 struct dsa_slave_priv *p = netdev_priv(dev);
702 struct netpoll *netpoll;
705 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
709 err = __netpoll_setup(netpoll, master);
715 p->netpoll = netpoll;
720 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
722 struct dsa_slave_priv *p = netdev_priv(dev);
723 struct netpoll *netpoll = p->netpoll;
730 __netpoll_free_async(netpoll);
733 static void dsa_slave_poll_controller(struct net_device *dev)
738 static int dsa_slave_get_phys_port_name(struct net_device *dev,
739 char *name, size_t len)
741 struct dsa_port *dp = dsa_slave_to_port(dev);
743 if (snprintf(name, len, "p%d", dp->index) >= len)
749 static struct dsa_mall_tc_entry *
750 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
752 struct dsa_slave_priv *p = netdev_priv(dev);
753 struct dsa_mall_tc_entry *mall_tc_entry;
755 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
756 if (mall_tc_entry->cookie == cookie)
757 return mall_tc_entry;
762 static int dsa_slave_add_cls_matchall(struct net_device *dev,
763 struct tc_cls_matchall_offload *cls,
766 struct dsa_port *dp = dsa_slave_to_port(dev);
767 struct dsa_slave_priv *p = netdev_priv(dev);
768 struct dsa_mall_tc_entry *mall_tc_entry;
769 __be16 protocol = cls->common.protocol;
770 struct dsa_switch *ds = dp->ds;
771 struct net_device *to_dev;
772 const struct tc_action *a;
773 struct dsa_port *to_dp;
774 int err = -EOPNOTSUPP;
776 if (!ds->ops->port_mirror_add)
779 if (!tcf_exts_has_one_action(cls->exts))
782 a = tcf_exts_first_action(cls->exts);
784 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
785 struct dsa_mall_mirror_tc_entry *mirror;
787 to_dev = tcf_mirred_dev(a);
791 if (!dsa_slave_dev_check(to_dev))
794 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
798 mall_tc_entry->cookie = cls->cookie;
799 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
800 mirror = &mall_tc_entry->mirror;
802 to_dp = dsa_slave_to_port(to_dev);
804 mirror->to_local_port = to_dp->index;
805 mirror->ingress = ingress;
807 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
809 kfree(mall_tc_entry);
813 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
819 static void dsa_slave_del_cls_matchall(struct net_device *dev,
820 struct tc_cls_matchall_offload *cls)
822 struct dsa_port *dp = dsa_slave_to_port(dev);
823 struct dsa_mall_tc_entry *mall_tc_entry;
824 struct dsa_switch *ds = dp->ds;
826 if (!ds->ops->port_mirror_del)
829 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
833 list_del(&mall_tc_entry->list);
835 switch (mall_tc_entry->type) {
836 case DSA_PORT_MALL_MIRROR:
837 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
843 kfree(mall_tc_entry);
846 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
847 struct tc_cls_matchall_offload *cls,
850 if (cls->common.chain_index)
853 switch (cls->command) {
854 case TC_CLSMATCHALL_REPLACE:
855 return dsa_slave_add_cls_matchall(dev, cls, ingress);
856 case TC_CLSMATCHALL_DESTROY:
857 dsa_slave_del_cls_matchall(dev, cls);
864 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
865 void *cb_priv, bool ingress)
867 struct net_device *dev = cb_priv;
869 if (!tc_can_offload(dev))
873 case TC_SETUP_CLSMATCHALL:
874 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
880 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
881 void *type_data, void *cb_priv)
883 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
886 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
887 void *type_data, void *cb_priv)
889 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
892 static int dsa_slave_setup_tc_block(struct net_device *dev,
893 struct tc_block_offload *f)
897 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
898 cb = dsa_slave_setup_tc_block_cb_ig;
899 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
900 cb = dsa_slave_setup_tc_block_cb_eg;
904 switch (f->command) {
906 return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
907 case TC_BLOCK_UNBIND:
908 tcf_block_cb_unregister(f->block, cb, dev);
915 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
920 return dsa_slave_setup_tc_block(dev, type_data);
926 static void dsa_slave_get_stats64(struct net_device *dev,
927 struct rtnl_link_stats64 *stats)
929 struct dsa_slave_priv *p = netdev_priv(dev);
930 struct pcpu_sw_netstats *s;
934 netdev_stats_to_stats64(stats, &dev->stats);
935 for_each_possible_cpu(i) {
936 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
938 s = per_cpu_ptr(p->stats64, i);
940 start = u64_stats_fetch_begin_irq(&s->syncp);
941 tx_packets = s->tx_packets;
942 tx_bytes = s->tx_bytes;
943 rx_packets = s->rx_packets;
944 rx_bytes = s->rx_bytes;
945 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
947 stats->tx_packets += tx_packets;
948 stats->tx_bytes += tx_bytes;
949 stats->rx_packets += rx_packets;
950 stats->rx_bytes += rx_bytes;
954 static int dsa_slave_get_rxnfc(struct net_device *dev,
955 struct ethtool_rxnfc *nfc, u32 *rule_locs)
957 struct dsa_port *dp = dsa_slave_to_port(dev);
958 struct dsa_switch *ds = dp->ds;
960 if (!ds->ops->get_rxnfc)
963 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
966 static int dsa_slave_set_rxnfc(struct net_device *dev,
967 struct ethtool_rxnfc *nfc)
969 struct dsa_port *dp = dsa_slave_to_port(dev);
970 struct dsa_switch *ds = dp->ds;
972 if (!ds->ops->set_rxnfc)
975 return ds->ops->set_rxnfc(ds, dp->index, nfc);
978 static int dsa_slave_get_ts_info(struct net_device *dev,
979 struct ethtool_ts_info *ts)
981 struct dsa_slave_priv *p = netdev_priv(dev);
982 struct dsa_switch *ds = p->dp->ds;
984 if (!ds->ops->get_ts_info)
987 return ds->ops->get_ts_info(ds, p->dp->index, ts);
990 static const struct ethtool_ops dsa_slave_ethtool_ops = {
991 .get_drvinfo = dsa_slave_get_drvinfo,
992 .get_regs_len = dsa_slave_get_regs_len,
993 .get_regs = dsa_slave_get_regs,
994 .nway_reset = dsa_slave_nway_reset,
995 .get_link = ethtool_op_get_link,
996 .get_eeprom_len = dsa_slave_get_eeprom_len,
997 .get_eeprom = dsa_slave_get_eeprom,
998 .set_eeprom = dsa_slave_set_eeprom,
999 .get_strings = dsa_slave_get_strings,
1000 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1001 .get_sset_count = dsa_slave_get_sset_count,
1002 .set_wol = dsa_slave_set_wol,
1003 .get_wol = dsa_slave_get_wol,
1004 .set_eee = dsa_slave_set_eee,
1005 .get_eee = dsa_slave_get_eee,
1006 .get_link_ksettings = dsa_slave_get_link_ksettings,
1007 .set_link_ksettings = dsa_slave_set_link_ksettings,
1008 .get_rxnfc = dsa_slave_get_rxnfc,
1009 .set_rxnfc = dsa_slave_set_rxnfc,
1010 .get_ts_info = dsa_slave_get_ts_info,
1013 /* legacy way, bypassing the bridge *****************************************/
1014 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1015 struct net_device *dev,
1016 const unsigned char *addr, u16 vid,
1019 struct dsa_port *dp = dsa_slave_to_port(dev);
1021 return dsa_port_fdb_add(dp, addr, vid);
1024 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1025 struct net_device *dev,
1026 const unsigned char *addr, u16 vid)
1028 struct dsa_port *dp = dsa_slave_to_port(dev);
1030 return dsa_port_fdb_del(dp, addr, vid);
1033 static const struct net_device_ops dsa_slave_netdev_ops = {
1034 .ndo_open = dsa_slave_open,
1035 .ndo_stop = dsa_slave_close,
1036 .ndo_start_xmit = dsa_slave_xmit,
1037 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1038 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1039 .ndo_set_mac_address = dsa_slave_set_mac_address,
1040 .ndo_fdb_add = dsa_legacy_fdb_add,
1041 .ndo_fdb_del = dsa_legacy_fdb_del,
1042 .ndo_fdb_dump = dsa_slave_fdb_dump,
1043 .ndo_do_ioctl = dsa_slave_ioctl,
1044 .ndo_get_iflink = dsa_slave_get_iflink,
1045 #ifdef CONFIG_NET_POLL_CONTROLLER
1046 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1047 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1048 .ndo_poll_controller = dsa_slave_poll_controller,
1050 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1051 .ndo_setup_tc = dsa_slave_setup_tc,
1052 .ndo_get_stats64 = dsa_slave_get_stats64,
1055 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1056 .switchdev_port_attr_get = dsa_slave_port_attr_get,
1057 .switchdev_port_attr_set = dsa_slave_port_attr_set,
1058 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1059 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1062 static struct device_type dsa_type = {
1066 static void dsa_slave_phylink_validate(struct net_device *dev,
1067 unsigned long *supported,
1068 struct phylink_link_state *state)
1070 struct dsa_port *dp = dsa_slave_to_port(dev);
1071 struct dsa_switch *ds = dp->ds;
1073 if (!ds->ops->phylink_validate)
1076 ds->ops->phylink_validate(ds, dp->index, supported, state);
1079 static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1080 struct phylink_link_state *state)
1082 struct dsa_port *dp = dsa_slave_to_port(dev);
1083 struct dsa_switch *ds = dp->ds;
1085 /* Only called for SGMII and 802.3z */
1086 if (!ds->ops->phylink_mac_link_state)
1089 return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1092 static void dsa_slave_phylink_mac_config(struct net_device *dev,
1094 const struct phylink_link_state *state)
1096 struct dsa_port *dp = dsa_slave_to_port(dev);
1097 struct dsa_switch *ds = dp->ds;
1099 if (!ds->ops->phylink_mac_config)
1102 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1105 static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1107 struct dsa_port *dp = dsa_slave_to_port(dev);
1108 struct dsa_switch *ds = dp->ds;
1110 if (!ds->ops->phylink_mac_an_restart)
1113 ds->ops->phylink_mac_an_restart(ds, dp->index);
1116 static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1118 phy_interface_t interface)
1120 struct dsa_port *dp = dsa_slave_to_port(dev);
1121 struct dsa_switch *ds = dp->ds;
1123 if (!ds->ops->phylink_mac_link_down) {
1124 if (ds->ops->adjust_link && dev->phydev)
1125 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1129 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1132 static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1134 phy_interface_t interface,
1135 struct phy_device *phydev)
1137 struct dsa_port *dp = dsa_slave_to_port(dev);
1138 struct dsa_switch *ds = dp->ds;
1140 if (!ds->ops->phylink_mac_link_up) {
1141 if (ds->ops->adjust_link && dev->phydev)
1142 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1146 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1149 static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1150 .validate = dsa_slave_phylink_validate,
1151 .mac_link_state = dsa_slave_phylink_mac_link_state,
1152 .mac_config = dsa_slave_phylink_mac_config,
1153 .mac_an_restart = dsa_slave_phylink_mac_an_restart,
1154 .mac_link_down = dsa_slave_phylink_mac_link_down,
1155 .mac_link_up = dsa_slave_phylink_mac_link_up,
1158 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1160 const struct dsa_port *dp = dsa_to_port(ds, port);
1162 phylink_mac_change(dp->pl, up);
1164 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1166 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1167 struct phylink_link_state *state)
1169 struct dsa_port *dp = dsa_slave_to_port(dev);
1170 struct dsa_switch *ds = dp->ds;
1172 /* No need to check that this operation is valid, the callback would
1173 * not be called if it was not.
1175 ds->ops->phylink_fixed_state(ds, dp->index, state);
1178 /* slave device setup *******************************************************/
1179 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1181 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1182 struct dsa_switch *ds = dp->ds;
1184 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1185 if (!slave_dev->phydev) {
1186 netdev_err(slave_dev, "no phy at %d\n", addr);
1190 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1193 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1195 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1196 struct device_node *port_dn = dp->dn;
1197 struct dsa_switch *ds = dp->ds;
1201 mode = of_get_phy_mode(port_dn);
1203 mode = PHY_INTERFACE_MODE_NA;
1205 dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1206 &dsa_slave_phylink_mac_ops);
1207 if (IS_ERR(dp->pl)) {
1208 netdev_err(slave_dev,
1209 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1210 return PTR_ERR(dp->pl);
1213 /* Register only if the switch provides such a callback, since this
1214 * callback takes precedence over polling the link GPIO in PHYLINK
1215 * (see phylink_get_fixed_state).
1217 if (ds->ops->phylink_fixed_state)
1218 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1220 if (ds->ops->get_phy_flags)
1221 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1223 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1224 if (ret == -ENODEV && ds->slave_mii_bus) {
1225 /* We could not connect to a designated PHY or SFP, so try to
1226 * use the switch internal MDIO bus instead
1228 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1231 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
1233 phylink_destroy(dp->pl);
1239 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1240 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1241 struct netdev_queue *txq,
1244 lockdep_set_class(&txq->_xmit_lock,
1245 &dsa_slave_netdev_xmit_lock_key);
1248 int dsa_slave_suspend(struct net_device *slave_dev)
1250 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1252 if (!netif_running(slave_dev))
1255 netif_device_detach(slave_dev);
1258 phylink_stop(dp->pl);
1264 int dsa_slave_resume(struct net_device *slave_dev)
1266 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1268 if (!netif_running(slave_dev))
1271 netif_device_attach(slave_dev);
1274 phylink_start(dp->pl);
1280 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1282 struct net_device *master = dsa_slave_to_master(dev);
1283 struct dsa_port *dp = dsa_slave_to_port(dev);
1284 struct dsa_notifier_register_info rinfo = {
1285 .switch_number = dp->ds->index,
1286 .port_number = dp->index,
1291 call_dsa_notifiers(val, dev, &rinfo.info);
1294 int dsa_slave_create(struct dsa_port *port)
1296 const struct dsa_port *cpu_dp = port->cpu_dp;
1297 struct net_device *master = cpu_dp->master;
1298 struct dsa_switch *ds = port->ds;
1299 const char *name = port->name;
1300 struct net_device *slave_dev;
1301 struct dsa_slave_priv *p;
1304 if (!ds->num_tx_queues)
1305 ds->num_tx_queues = 1;
1307 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1308 NET_NAME_UNKNOWN, ether_setup,
1309 ds->num_tx_queues, 1);
1310 if (slave_dev == NULL)
1313 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1314 slave_dev->hw_features |= NETIF_F_HW_TC;
1315 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1316 if (port->mac && is_valid_ether_addr(port->mac))
1317 ether_addr_copy(slave_dev->dev_addr, port->mac);
1319 eth_hw_addr_inherit(slave_dev, master);
1320 slave_dev->priv_flags |= IFF_NO_QUEUE;
1321 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1322 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1323 slave_dev->min_mtu = 0;
1324 slave_dev->max_mtu = ETH_MAX_MTU;
1325 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1327 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1330 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1331 slave_dev->dev.of_node = port->dn;
1332 slave_dev->vlan_features = master->vlan_features;
1334 p = netdev_priv(slave_dev);
1335 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1337 free_netdev(slave_dev);
1341 ret = gro_cells_init(&p->gcells, slave_dev);
1346 INIT_LIST_HEAD(&p->mall_tc_list);
1347 p->xmit = cpu_dp->tag_ops->xmit;
1348 port->slave = slave_dev;
1350 netif_carrier_off(slave_dev);
1352 ret = dsa_slave_phy_setup(slave_dev);
1354 netdev_err(master, "error %d setting up slave phy\n", ret);
1358 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1360 ret = register_netdev(slave_dev);
1362 netdev_err(master, "error %d registering interface %s\n",
1363 ret, slave_dev->name);
1371 phylink_disconnect_phy(p->dp->pl);
1373 phylink_destroy(p->dp->pl);
1375 gro_cells_destroy(&p->gcells);
1377 free_percpu(p->stats64);
1378 free_netdev(slave_dev);
1383 void dsa_slave_destroy(struct net_device *slave_dev)
1385 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1386 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1388 netif_carrier_off(slave_dev);
1390 phylink_disconnect_phy(dp->pl);
1393 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1394 unregister_netdev(slave_dev);
1395 phylink_destroy(dp->pl);
1396 gro_cells_destroy(&p->gcells);
1397 free_percpu(p->stats64);
1398 free_netdev(slave_dev);
1401 static bool dsa_slave_dev_check(struct net_device *dev)
1403 return dev->netdev_ops == &dsa_slave_netdev_ops;
1406 static int dsa_slave_changeupper(struct net_device *dev,
1407 struct netdev_notifier_changeupper_info *info)
1409 struct dsa_port *dp = dsa_slave_to_port(dev);
1410 int err = NOTIFY_DONE;
1412 if (netif_is_bridge_master(info->upper_dev)) {
1413 if (info->linking) {
1414 err = dsa_port_bridge_join(dp, info->upper_dev);
1415 err = notifier_from_errno(err);
1417 dsa_port_bridge_leave(dp, info->upper_dev);
1425 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1426 unsigned long event, void *ptr)
1428 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1430 if (!dsa_slave_dev_check(dev))
1433 if (event == NETDEV_CHANGEUPPER)
1434 return dsa_slave_changeupper(dev, ptr);
1439 struct dsa_switchdev_event_work {
1440 struct work_struct work;
1441 struct switchdev_notifier_fdb_info fdb_info;
1442 struct net_device *dev;
1443 unsigned long event;
1446 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1448 struct dsa_switchdev_event_work *switchdev_work =
1449 container_of(work, struct dsa_switchdev_event_work, work);
1450 struct net_device *dev = switchdev_work->dev;
1451 struct switchdev_notifier_fdb_info *fdb_info;
1452 struct dsa_port *dp = dsa_slave_to_port(dev);
1456 switch (switchdev_work->event) {
1457 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1458 fdb_info = &switchdev_work->fdb_info;
1459 if (!fdb_info->added_by_user)
1462 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1464 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1467 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1471 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1472 fdb_info = &switchdev_work->fdb_info;
1473 if (!fdb_info->added_by_user)
1476 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1478 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1485 kfree(switchdev_work->fdb_info.addr);
1486 kfree(switchdev_work);
1491 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1493 const struct switchdev_notifier_fdb_info *
1496 memcpy(&switchdev_work->fdb_info, fdb_info,
1497 sizeof(switchdev_work->fdb_info));
1498 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1499 if (!switchdev_work->fdb_info.addr)
1501 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1506 /* Called under rcu_read_lock() */
1507 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1508 unsigned long event, void *ptr)
1510 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1511 struct dsa_switchdev_event_work *switchdev_work;
1513 if (!dsa_slave_dev_check(dev))
1516 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1517 if (!switchdev_work)
1520 INIT_WORK(&switchdev_work->work,
1521 dsa_slave_switchdev_event_work);
1522 switchdev_work->dev = dev;
1523 switchdev_work->event = event;
1526 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1527 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1528 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1529 goto err_fdb_work_init;
1533 kfree(switchdev_work);
1537 dsa_schedule_work(&switchdev_work->work);
1541 kfree(switchdev_work);
1545 static struct notifier_block dsa_slave_nb __read_mostly = {
1546 .notifier_call = dsa_slave_netdevice_event,
1549 static struct notifier_block dsa_slave_switchdev_notifier = {
1550 .notifier_call = dsa_slave_switchdev_event,
1553 int dsa_slave_register_notifier(void)
1557 err = register_netdevice_notifier(&dsa_slave_nb);
1561 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1563 goto err_switchdev_nb;
1568 unregister_netdevice_notifier(&dsa_slave_nb);
1572 void dsa_slave_unregister_notifier(void)
1576 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1578 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1580 err = unregister_netdevice_notifier(&dsa_slave_nb);
1582 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);