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/of_net.h>
17 #include <linux/of_mdio.h>
18 #include <linux/mdio.h>
19 #include <net/rtnetlink.h>
20 #include <net/pkt_cls.h>
21 #include <net/tc_act/tc_mirred.h>
22 #include <linux/if_bridge.h>
23 #include <linux/netpoll.h>
24 #include <linux/ptp_classify.h>
28 static bool dsa_slave_dev_check(struct net_device *dev);
30 /* slave mii_bus handling ***************************************************/
31 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
33 struct dsa_switch *ds = bus->priv;
35 if (ds->phys_mii_mask & (1 << addr))
36 return ds->ops->phy_read(ds, addr, reg);
41 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
43 struct dsa_switch *ds = bus->priv;
45 if (ds->phys_mii_mask & (1 << addr))
46 return ds->ops->phy_write(ds, addr, reg, val);
51 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
53 ds->slave_mii_bus->priv = (void *)ds;
54 ds->slave_mii_bus->name = "dsa slave smi";
55 ds->slave_mii_bus->read = dsa_slave_phy_read;
56 ds->slave_mii_bus->write = dsa_slave_phy_write;
57 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
58 ds->dst->index, ds->index);
59 ds->slave_mii_bus->parent = ds->dev;
60 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
64 /* slave device handling ****************************************************/
65 static int dsa_slave_get_iflink(const struct net_device *dev)
67 return dsa_slave_to_master(dev)->ifindex;
70 static int dsa_slave_open(struct net_device *dev)
72 struct net_device *master = dsa_slave_to_master(dev);
73 struct dsa_port *dp = dsa_slave_to_port(dev);
76 if (!(master->flags & IFF_UP))
79 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
80 err = dev_uc_add(master, dev->dev_addr);
85 if (dev->flags & IFF_ALLMULTI) {
86 err = dev_set_allmulti(master, 1);
90 if (dev->flags & IFF_PROMISC) {
91 err = dev_set_promiscuity(master, 1);
96 err = dsa_port_enable(dp, dev->phydev);
101 phy_start(dev->phydev);
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);
124 phy_stop(dev->phydev);
126 dsa_port_disable(dp, dev->phydev);
128 dev_mc_unsync(master, dev);
129 dev_uc_unsync(master, dev);
130 if (dev->flags & IFF_ALLMULTI)
131 dev_set_allmulti(master, -1);
132 if (dev->flags & IFF_PROMISC)
133 dev_set_promiscuity(master, -1);
135 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
136 dev_uc_del(master, dev->dev_addr);
141 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
143 struct net_device *master = dsa_slave_to_master(dev);
145 if (change & IFF_ALLMULTI)
146 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
147 if (change & IFF_PROMISC)
148 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
151 static void dsa_slave_set_rx_mode(struct net_device *dev)
153 struct net_device *master = dsa_slave_to_master(dev);
155 dev_mc_sync(master, dev);
156 dev_uc_sync(master, dev);
159 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
161 struct net_device *master = dsa_slave_to_master(dev);
162 struct sockaddr *addr = a;
165 if (!is_valid_ether_addr(addr->sa_data))
166 return -EADDRNOTAVAIL;
168 if (!(dev->flags & IFF_UP))
171 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
172 err = dev_uc_add(master, addr->sa_data);
177 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
178 dev_uc_del(master, dev->dev_addr);
181 ether_addr_copy(dev->dev_addr, addr->sa_data);
186 struct dsa_slave_dump_ctx {
187 struct net_device *dev;
189 struct netlink_callback *cb;
194 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
195 bool is_static, void *data)
197 struct dsa_slave_dump_ctx *dump = data;
198 u32 portid = NETLINK_CB(dump->cb->skb).portid;
199 u32 seq = dump->cb->nlh->nlmsg_seq;
200 struct nlmsghdr *nlh;
203 if (dump->idx < dump->cb->args[2])
206 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
207 sizeof(*ndm), NLM_F_MULTI);
211 ndm = nlmsg_data(nlh);
212 ndm->ndm_family = AF_BRIDGE;
215 ndm->ndm_flags = NTF_SELF;
217 ndm->ndm_ifindex = dump->dev->ifindex;
218 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
220 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
221 goto nla_put_failure;
223 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
224 goto nla_put_failure;
226 nlmsg_end(dump->skb, nlh);
233 nlmsg_cancel(dump->skb, nlh);
238 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
239 struct net_device *dev, struct net_device *filter_dev,
242 struct dsa_port *dp = dsa_slave_to_port(dev);
243 struct dsa_slave_dump_ctx dump = {
251 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
257 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
259 struct dsa_slave_priv *p = netdev_priv(dev);
260 struct dsa_switch *ds = p->dp->ds;
261 int port = p->dp->index;
263 /* Pass through to switch driver if it supports timestamping */
266 if (ds->ops->port_hwtstamp_get)
267 return ds->ops->port_hwtstamp_get(ds, port, ifr);
270 if (ds->ops->port_hwtstamp_set)
271 return ds->ops->port_hwtstamp_set(ds, port, ifr);
278 return phy_mii_ioctl(dev->phydev, ifr, cmd);
281 static int dsa_slave_port_attr_set(struct net_device *dev,
282 const struct switchdev_attr *attr,
283 struct switchdev_trans *trans)
285 struct dsa_port *dp = dsa_slave_to_port(dev);
289 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
290 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
292 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
293 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
296 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
297 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
307 static int dsa_slave_port_obj_add(struct net_device *dev,
308 const struct switchdev_obj *obj,
309 struct switchdev_trans *trans)
311 struct dsa_port *dp = dsa_slave_to_port(dev);
314 /* For the prepare phase, ensure the full set of changes is feasable in
315 * one go in order to signal a failure properly. If an operation is not
316 * supported, return -EOPNOTSUPP.
320 case SWITCHDEV_OBJ_ID_PORT_MDB:
321 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
323 case SWITCHDEV_OBJ_ID_HOST_MDB:
324 /* DSA can directly translate this to a normal MDB add,
325 * but on the CPU port.
327 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
330 case SWITCHDEV_OBJ_ID_PORT_VLAN:
331 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
342 static int dsa_slave_port_obj_del(struct net_device *dev,
343 const struct switchdev_obj *obj)
345 struct dsa_port *dp = dsa_slave_to_port(dev);
349 case SWITCHDEV_OBJ_ID_PORT_MDB:
350 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
352 case SWITCHDEV_OBJ_ID_HOST_MDB:
353 /* DSA can directly translate this to a normal MDB add,
354 * but on the CPU port.
356 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
358 case SWITCHDEV_OBJ_ID_PORT_VLAN:
359 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
369 static int dsa_slave_port_attr_get(struct net_device *dev,
370 struct switchdev_attr *attr)
372 struct dsa_port *dp = dsa_slave_to_port(dev);
373 struct dsa_switch *ds = dp->ds;
374 struct dsa_switch_tree *dst = ds->dst;
377 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
378 attr->u.ppid.id_len = sizeof(dst->index);
379 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
381 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
382 attr->u.brport_flags_support = 0;
391 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
394 #ifdef CONFIG_NET_POLL_CONTROLLER
395 struct dsa_slave_priv *p = netdev_priv(dev);
398 netpoll_send_skb(p->netpoll, skb);
405 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
408 struct dsa_switch *ds = p->dp->ds;
409 struct sk_buff *clone;
412 type = ptp_classify_raw(skb);
413 if (type == PTP_CLASS_NONE)
416 if (!ds->ops->port_txtstamp)
419 clone = skb_clone_sk(skb);
423 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
429 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
431 struct dsa_slave_priv *p = netdev_priv(dev);
432 struct pcpu_sw_netstats *s;
433 struct sk_buff *nskb;
435 s = this_cpu_ptr(p->stats64);
436 u64_stats_update_begin(&s->syncp);
438 s->tx_bytes += skb->len;
439 u64_stats_update_end(&s->syncp);
441 /* Identify PTP protocol packets, clone them, and pass them to the
444 dsa_skb_tx_timestamp(p, skb);
446 /* Transmit function may have to reallocate the original SKB,
447 * in which case it must have freed it. Only free it here on error.
449 nskb = p->xmit(skb, dev);
455 /* SKB for netpoll still need to be mangled with the protocol-specific
456 * tag to be successfully transmitted
458 if (unlikely(netpoll_tx_running(dev)))
459 return dsa_slave_netpoll_send_skb(dev, nskb);
461 /* Queue the SKB for transmission on the parent interface, but
462 * do not modify its EtherType
464 nskb->dev = dsa_slave_to_master(dev);
465 dev_queue_xmit(nskb);
470 /* ethtool operations *******************************************************/
472 static void dsa_slave_get_drvinfo(struct net_device *dev,
473 struct ethtool_drvinfo *drvinfo)
475 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
476 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
477 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
480 static int dsa_slave_get_regs_len(struct net_device *dev)
482 struct dsa_port *dp = dsa_slave_to_port(dev);
483 struct dsa_switch *ds = dp->ds;
485 if (ds->ops->get_regs_len)
486 return ds->ops->get_regs_len(ds, dp->index);
492 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
494 struct dsa_port *dp = dsa_slave_to_port(dev);
495 struct dsa_switch *ds = dp->ds;
497 if (ds->ops->get_regs)
498 ds->ops->get_regs(ds, dp->index, regs, _p);
501 static u32 dsa_slave_get_link(struct net_device *dev)
506 genphy_update_link(dev->phydev);
508 return dev->phydev->link;
511 static int dsa_slave_get_eeprom_len(struct net_device *dev)
513 struct dsa_port *dp = dsa_slave_to_port(dev);
514 struct dsa_switch *ds = dp->ds;
516 if (ds->cd && ds->cd->eeprom_len)
517 return ds->cd->eeprom_len;
519 if (ds->ops->get_eeprom_len)
520 return ds->ops->get_eeprom_len(ds);
525 static int dsa_slave_get_eeprom(struct net_device *dev,
526 struct ethtool_eeprom *eeprom, u8 *data)
528 struct dsa_port *dp = dsa_slave_to_port(dev);
529 struct dsa_switch *ds = dp->ds;
531 if (ds->ops->get_eeprom)
532 return ds->ops->get_eeprom(ds, eeprom, data);
537 static int dsa_slave_set_eeprom(struct net_device *dev,
538 struct ethtool_eeprom *eeprom, u8 *data)
540 struct dsa_port *dp = dsa_slave_to_port(dev);
541 struct dsa_switch *ds = dp->ds;
543 if (ds->ops->set_eeprom)
544 return ds->ops->set_eeprom(ds, eeprom, data);
549 static void dsa_slave_get_strings(struct net_device *dev,
550 uint32_t stringset, uint8_t *data)
552 struct dsa_port *dp = dsa_slave_to_port(dev);
553 struct dsa_switch *ds = dp->ds;
555 if (stringset == ETH_SS_STATS) {
556 int len = ETH_GSTRING_LEN;
558 strncpy(data, "tx_packets", len);
559 strncpy(data + len, "tx_bytes", len);
560 strncpy(data + 2 * len, "rx_packets", len);
561 strncpy(data + 3 * len, "rx_bytes", len);
562 if (ds->ops->get_strings)
563 ds->ops->get_strings(ds, dp->index, stringset,
568 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
569 struct ethtool_stats *stats,
572 struct dsa_port *dp = dsa_slave_to_port(dev);
573 struct dsa_slave_priv *p = netdev_priv(dev);
574 struct dsa_switch *ds = dp->ds;
575 struct pcpu_sw_netstats *s;
579 for_each_possible_cpu(i) {
580 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
582 s = per_cpu_ptr(p->stats64, i);
584 start = u64_stats_fetch_begin_irq(&s->syncp);
585 tx_packets = s->tx_packets;
586 tx_bytes = s->tx_bytes;
587 rx_packets = s->rx_packets;
588 rx_bytes = s->rx_bytes;
589 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
590 data[0] += tx_packets;
592 data[2] += rx_packets;
595 if (ds->ops->get_ethtool_stats)
596 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
599 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
601 struct dsa_port *dp = dsa_slave_to_port(dev);
602 struct dsa_switch *ds = dp->ds;
604 if (sset == ETH_SS_STATS) {
608 if (ds->ops->get_sset_count)
609 count += ds->ops->get_sset_count(ds, dp->index, sset);
617 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
619 struct dsa_port *dp = dsa_slave_to_port(dev);
620 struct dsa_switch *ds = dp->ds;
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 if (ds->ops->set_wol)
633 ret = ds->ops->set_wol(ds, dp->index, w);
638 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
640 struct dsa_port *dp = dsa_slave_to_port(dev);
641 struct dsa_switch *ds = dp->ds;
644 /* Port's PHY and MAC both need to be EEE capable */
648 if (!ds->ops->set_mac_eee)
651 ret = ds->ops->set_mac_eee(ds, dp->index, e);
655 if (e->eee_enabled) {
656 ret = phy_init_eee(dev->phydev, 0);
661 return phy_ethtool_set_eee(dev->phydev, e);
664 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
666 struct dsa_port *dp = dsa_slave_to_port(dev);
667 struct dsa_switch *ds = dp->ds;
670 /* Port's PHY and MAC both need to be EEE capable */
674 if (!ds->ops->get_mac_eee)
677 ret = ds->ops->get_mac_eee(ds, dp->index, e);
681 return phy_ethtool_get_eee(dev->phydev, e);
684 #ifdef CONFIG_NET_POLL_CONTROLLER
685 static int dsa_slave_netpoll_setup(struct net_device *dev,
686 struct netpoll_info *ni)
688 struct net_device *master = dsa_slave_to_master(dev);
689 struct dsa_slave_priv *p = netdev_priv(dev);
690 struct netpoll *netpoll;
693 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
697 err = __netpoll_setup(netpoll, master);
703 p->netpoll = netpoll;
708 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
710 struct dsa_slave_priv *p = netdev_priv(dev);
711 struct netpoll *netpoll = p->netpoll;
718 __netpoll_free_async(netpoll);
721 static void dsa_slave_poll_controller(struct net_device *dev)
726 static int dsa_slave_get_phys_port_name(struct net_device *dev,
727 char *name, size_t len)
729 struct dsa_port *dp = dsa_slave_to_port(dev);
731 if (snprintf(name, len, "p%d", dp->index) >= len)
737 static struct dsa_mall_tc_entry *
738 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
740 struct dsa_slave_priv *p = netdev_priv(dev);
741 struct dsa_mall_tc_entry *mall_tc_entry;
743 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
744 if (mall_tc_entry->cookie == cookie)
745 return mall_tc_entry;
750 static int dsa_slave_add_cls_matchall(struct net_device *dev,
751 struct tc_cls_matchall_offload *cls,
754 struct dsa_port *dp = dsa_slave_to_port(dev);
755 struct dsa_slave_priv *p = netdev_priv(dev);
756 struct dsa_mall_tc_entry *mall_tc_entry;
757 __be16 protocol = cls->common.protocol;
758 struct dsa_switch *ds = dp->ds;
759 struct net_device *to_dev;
760 const struct tc_action *a;
761 struct dsa_port *to_dp;
762 int err = -EOPNOTSUPP;
765 if (!ds->ops->port_mirror_add)
768 if (!tcf_exts_has_one_action(cls->exts))
771 tcf_exts_to_list(cls->exts, &actions);
772 a = list_first_entry(&actions, struct tc_action, list);
774 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
775 struct dsa_mall_mirror_tc_entry *mirror;
777 to_dev = tcf_mirred_dev(a);
781 if (!dsa_slave_dev_check(to_dev))
784 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
788 mall_tc_entry->cookie = cls->cookie;
789 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
790 mirror = &mall_tc_entry->mirror;
792 to_dp = dsa_slave_to_port(to_dev);
794 mirror->to_local_port = to_dp->index;
795 mirror->ingress = ingress;
797 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
799 kfree(mall_tc_entry);
803 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
809 static void dsa_slave_del_cls_matchall(struct net_device *dev,
810 struct tc_cls_matchall_offload *cls)
812 struct dsa_port *dp = dsa_slave_to_port(dev);
813 struct dsa_mall_tc_entry *mall_tc_entry;
814 struct dsa_switch *ds = dp->ds;
816 if (!ds->ops->port_mirror_del)
819 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
823 list_del(&mall_tc_entry->list);
825 switch (mall_tc_entry->type) {
826 case DSA_PORT_MALL_MIRROR:
827 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
833 kfree(mall_tc_entry);
836 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
837 struct tc_cls_matchall_offload *cls,
840 if (cls->common.chain_index)
843 switch (cls->command) {
844 case TC_CLSMATCHALL_REPLACE:
845 return dsa_slave_add_cls_matchall(dev, cls, ingress);
846 case TC_CLSMATCHALL_DESTROY:
847 dsa_slave_del_cls_matchall(dev, cls);
854 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
855 void *cb_priv, bool ingress)
857 struct net_device *dev = cb_priv;
859 if (!tc_can_offload(dev))
863 case TC_SETUP_CLSMATCHALL:
864 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
870 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
871 void *type_data, void *cb_priv)
873 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
876 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
877 void *type_data, void *cb_priv)
879 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
882 static int dsa_slave_setup_tc_block(struct net_device *dev,
883 struct tc_block_offload *f)
887 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
888 cb = dsa_slave_setup_tc_block_cb_ig;
889 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
890 cb = dsa_slave_setup_tc_block_cb_eg;
894 switch (f->command) {
896 return tcf_block_cb_register(f->block, cb, dev, dev);
897 case TC_BLOCK_UNBIND:
898 tcf_block_cb_unregister(f->block, cb, dev);
905 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
910 return dsa_slave_setup_tc_block(dev, type_data);
916 static void dsa_slave_get_stats64(struct net_device *dev,
917 struct rtnl_link_stats64 *stats)
919 struct dsa_slave_priv *p = netdev_priv(dev);
920 struct pcpu_sw_netstats *s;
924 netdev_stats_to_stats64(stats, &dev->stats);
925 for_each_possible_cpu(i) {
926 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
928 s = per_cpu_ptr(p->stats64, i);
930 start = u64_stats_fetch_begin_irq(&s->syncp);
931 tx_packets = s->tx_packets;
932 tx_bytes = s->tx_bytes;
933 rx_packets = s->rx_packets;
934 rx_bytes = s->rx_bytes;
935 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
937 stats->tx_packets += tx_packets;
938 stats->tx_bytes += tx_bytes;
939 stats->rx_packets += rx_packets;
940 stats->rx_bytes += rx_bytes;
944 static int dsa_slave_get_rxnfc(struct net_device *dev,
945 struct ethtool_rxnfc *nfc, u32 *rule_locs)
947 struct dsa_port *dp = dsa_slave_to_port(dev);
948 struct dsa_switch *ds = dp->ds;
950 if (!ds->ops->get_rxnfc)
953 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
956 static int dsa_slave_set_rxnfc(struct net_device *dev,
957 struct ethtool_rxnfc *nfc)
959 struct dsa_port *dp = dsa_slave_to_port(dev);
960 struct dsa_switch *ds = dp->ds;
962 if (!ds->ops->set_rxnfc)
965 return ds->ops->set_rxnfc(ds, dp->index, nfc);
968 static int dsa_slave_get_ts_info(struct net_device *dev,
969 struct ethtool_ts_info *ts)
971 struct dsa_slave_priv *p = netdev_priv(dev);
972 struct dsa_switch *ds = p->dp->ds;
974 if (!ds->ops->get_ts_info)
977 return ds->ops->get_ts_info(ds, p->dp->index, ts);
980 static const struct ethtool_ops dsa_slave_ethtool_ops = {
981 .get_drvinfo = dsa_slave_get_drvinfo,
982 .get_regs_len = dsa_slave_get_regs_len,
983 .get_regs = dsa_slave_get_regs,
984 .nway_reset = phy_ethtool_nway_reset,
985 .get_link = dsa_slave_get_link,
986 .get_eeprom_len = dsa_slave_get_eeprom_len,
987 .get_eeprom = dsa_slave_get_eeprom,
988 .set_eeprom = dsa_slave_set_eeprom,
989 .get_strings = dsa_slave_get_strings,
990 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
991 .get_sset_count = dsa_slave_get_sset_count,
992 .set_wol = dsa_slave_set_wol,
993 .get_wol = dsa_slave_get_wol,
994 .set_eee = dsa_slave_set_eee,
995 .get_eee = dsa_slave_get_eee,
996 .get_link_ksettings = phy_ethtool_get_link_ksettings,
997 .set_link_ksettings = phy_ethtool_set_link_ksettings,
998 .get_rxnfc = dsa_slave_get_rxnfc,
999 .set_rxnfc = dsa_slave_set_rxnfc,
1000 .get_ts_info = dsa_slave_get_ts_info,
1003 /* legacy way, bypassing the bridge *****************************************/
1004 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1005 struct net_device *dev,
1006 const unsigned char *addr, u16 vid,
1009 struct dsa_port *dp = dsa_slave_to_port(dev);
1011 return dsa_port_fdb_add(dp, addr, vid);
1014 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1015 struct net_device *dev,
1016 const unsigned char *addr, u16 vid)
1018 struct dsa_port *dp = dsa_slave_to_port(dev);
1020 return dsa_port_fdb_del(dp, addr, vid);
1023 static const struct net_device_ops dsa_slave_netdev_ops = {
1024 .ndo_open = dsa_slave_open,
1025 .ndo_stop = dsa_slave_close,
1026 .ndo_start_xmit = dsa_slave_xmit,
1027 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1028 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1029 .ndo_set_mac_address = dsa_slave_set_mac_address,
1030 .ndo_fdb_add = dsa_legacy_fdb_add,
1031 .ndo_fdb_del = dsa_legacy_fdb_del,
1032 .ndo_fdb_dump = dsa_slave_fdb_dump,
1033 .ndo_do_ioctl = dsa_slave_ioctl,
1034 .ndo_get_iflink = dsa_slave_get_iflink,
1035 #ifdef CONFIG_NET_POLL_CONTROLLER
1036 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1037 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1038 .ndo_poll_controller = dsa_slave_poll_controller,
1040 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1041 .ndo_setup_tc = dsa_slave_setup_tc,
1042 .ndo_get_stats64 = dsa_slave_get_stats64,
1045 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1046 .switchdev_port_attr_get = dsa_slave_port_attr_get,
1047 .switchdev_port_attr_set = dsa_slave_port_attr_set,
1048 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1049 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1052 static struct device_type dsa_type = {
1056 static void dsa_slave_adjust_link(struct net_device *dev)
1058 struct dsa_port *dp = dsa_slave_to_port(dev);
1059 struct dsa_slave_priv *p = netdev_priv(dev);
1060 struct dsa_switch *ds = dp->ds;
1061 unsigned int status_changed = 0;
1063 if (p->old_link != dev->phydev->link) {
1065 p->old_link = dev->phydev->link;
1068 if (p->old_duplex != dev->phydev->duplex) {
1070 p->old_duplex = dev->phydev->duplex;
1073 if (p->old_pause != dev->phydev->pause) {
1075 p->old_pause = dev->phydev->pause;
1078 if (ds->ops->adjust_link && status_changed)
1079 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1082 phy_print_status(dev->phydev);
1085 static int dsa_slave_fixed_link_update(struct net_device *dev,
1086 struct fixed_phy_status *status)
1088 struct dsa_switch *ds;
1089 struct dsa_port *dp;
1092 dp = dsa_slave_to_port(dev);
1094 if (ds->ops->fixed_link_update)
1095 ds->ops->fixed_link_update(ds, dp->index, status);
1101 /* slave device setup *******************************************************/
1102 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1104 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1105 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1106 struct dsa_switch *ds = dp->ds;
1108 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1109 if (!slave_dev->phydev) {
1110 netdev_err(slave_dev, "no phy at %d\n", addr);
1114 /* Use already configured phy mode */
1115 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1116 p->phy_interface = slave_dev->phydev->interface;
1118 return phy_connect_direct(slave_dev, slave_dev->phydev,
1119 dsa_slave_adjust_link, p->phy_interface);
1122 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1124 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1125 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1126 struct device_node *port_dn = dp->dn;
1127 struct dsa_switch *ds = dp->ds;
1128 struct device_node *phy_dn;
1129 bool phy_is_fixed = false;
1133 mode = of_get_phy_mode(port_dn);
1135 mode = PHY_INTERFACE_MODE_NA;
1136 p->phy_interface = mode;
1138 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1139 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1140 /* In the case of a fixed PHY, the DT node associated
1141 * to the fixed PHY is the Port DT node
1143 ret = of_phy_register_fixed_link(port_dn);
1145 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1148 phy_is_fixed = true;
1149 phy_dn = of_node_get(port_dn);
1152 if (ds->ops->get_phy_flags)
1153 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1156 slave_dev->phydev = of_phy_connect(slave_dev, phy_dn,
1157 dsa_slave_adjust_link,
1160 of_node_put(phy_dn);
1163 if (slave_dev->phydev && phy_is_fixed)
1164 fixed_phy_set_link_update(slave_dev->phydev,
1165 dsa_slave_fixed_link_update);
1167 /* We could not connect to a designated PHY, so use the switch internal
1170 if (!slave_dev->phydev) {
1171 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1173 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1176 of_phy_deregister_fixed_link(port_dn);
1181 phy_attached_info(slave_dev->phydev);
1186 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1187 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1188 struct netdev_queue *txq,
1191 lockdep_set_class(&txq->_xmit_lock,
1192 &dsa_slave_netdev_xmit_lock_key);
1195 int dsa_slave_suspend(struct net_device *slave_dev)
1197 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1199 netif_device_detach(slave_dev);
1201 if (slave_dev->phydev) {
1202 phy_stop(slave_dev->phydev);
1206 phy_suspend(slave_dev->phydev);
1212 int dsa_slave_resume(struct net_device *slave_dev)
1214 netif_device_attach(slave_dev);
1216 if (slave_dev->phydev) {
1217 phy_resume(slave_dev->phydev);
1218 phy_start(slave_dev->phydev);
1224 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1226 struct net_device *master = dsa_slave_to_master(dev);
1227 struct dsa_port *dp = dsa_slave_to_port(dev);
1228 struct dsa_notifier_register_info rinfo = {
1229 .switch_number = dp->ds->index,
1230 .port_number = dp->index,
1235 call_dsa_notifiers(val, dev, &rinfo.info);
1238 int dsa_slave_create(struct dsa_port *port)
1240 const struct dsa_port *cpu_dp = port->cpu_dp;
1241 struct net_device *master = cpu_dp->master;
1242 struct dsa_switch *ds = port->ds;
1243 const char *name = port->name;
1244 struct net_device *slave_dev;
1245 struct dsa_slave_priv *p;
1248 if (!ds->num_tx_queues)
1249 ds->num_tx_queues = 1;
1251 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1252 NET_NAME_UNKNOWN, ether_setup,
1253 ds->num_tx_queues, 1);
1254 if (slave_dev == NULL)
1257 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1258 slave_dev->hw_features |= NETIF_F_HW_TC;
1259 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1260 eth_hw_addr_inherit(slave_dev, master);
1261 slave_dev->priv_flags |= IFF_NO_QUEUE;
1262 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1263 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1264 slave_dev->min_mtu = 0;
1265 slave_dev->max_mtu = ETH_MAX_MTU;
1266 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1268 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1271 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1272 slave_dev->dev.of_node = port->dn;
1273 slave_dev->vlan_features = master->vlan_features;
1275 p = netdev_priv(slave_dev);
1276 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1278 free_netdev(slave_dev);
1282 INIT_LIST_HEAD(&p->mall_tc_list);
1283 p->xmit = cpu_dp->tag_ops->xmit;
1289 port->slave = slave_dev;
1291 netif_carrier_off(slave_dev);
1293 ret = dsa_slave_phy_setup(slave_dev);
1295 netdev_err(master, "error %d setting up slave phy\n", ret);
1299 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1301 ret = register_netdev(slave_dev);
1303 netdev_err(master, "error %d registering interface %s\n",
1304 ret, slave_dev->name);
1311 phy_disconnect(slave_dev->phydev);
1312 if (of_phy_is_fixed_link(port->dn))
1313 of_phy_deregister_fixed_link(port->dn);
1315 free_percpu(p->stats64);
1316 free_netdev(slave_dev);
1321 void dsa_slave_destroy(struct net_device *slave_dev)
1323 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1324 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1325 struct device_node *port_dn = dp->dn;
1327 netif_carrier_off(slave_dev);
1328 if (slave_dev->phydev) {
1329 phy_disconnect(slave_dev->phydev);
1331 if (of_phy_is_fixed_link(port_dn))
1332 of_phy_deregister_fixed_link(port_dn);
1334 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1335 unregister_netdev(slave_dev);
1336 free_percpu(p->stats64);
1337 free_netdev(slave_dev);
1340 static bool dsa_slave_dev_check(struct net_device *dev)
1342 return dev->netdev_ops == &dsa_slave_netdev_ops;
1345 static int dsa_slave_changeupper(struct net_device *dev,
1346 struct netdev_notifier_changeupper_info *info)
1348 struct dsa_port *dp = dsa_slave_to_port(dev);
1349 int err = NOTIFY_DONE;
1351 if (netif_is_bridge_master(info->upper_dev)) {
1352 if (info->linking) {
1353 err = dsa_port_bridge_join(dp, info->upper_dev);
1354 err = notifier_from_errno(err);
1356 dsa_port_bridge_leave(dp, info->upper_dev);
1364 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1365 unsigned long event, void *ptr)
1367 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1369 if (!dsa_slave_dev_check(dev))
1372 if (event == NETDEV_CHANGEUPPER)
1373 return dsa_slave_changeupper(dev, ptr);
1378 struct dsa_switchdev_event_work {
1379 struct work_struct work;
1380 struct switchdev_notifier_fdb_info fdb_info;
1381 struct net_device *dev;
1382 unsigned long event;
1385 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1387 struct dsa_switchdev_event_work *switchdev_work =
1388 container_of(work, struct dsa_switchdev_event_work, work);
1389 struct net_device *dev = switchdev_work->dev;
1390 struct switchdev_notifier_fdb_info *fdb_info;
1391 struct dsa_port *dp = dsa_slave_to_port(dev);
1395 switch (switchdev_work->event) {
1396 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1397 fdb_info = &switchdev_work->fdb_info;
1398 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1400 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1403 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1407 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1408 fdb_info = &switchdev_work->fdb_info;
1409 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1411 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1418 kfree(switchdev_work->fdb_info.addr);
1419 kfree(switchdev_work);
1424 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1426 const struct switchdev_notifier_fdb_info *
1429 memcpy(&switchdev_work->fdb_info, fdb_info,
1430 sizeof(switchdev_work->fdb_info));
1431 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1432 if (!switchdev_work->fdb_info.addr)
1434 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1439 /* Called under rcu_read_lock() */
1440 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1441 unsigned long event, void *ptr)
1443 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1444 struct switchdev_notifier_fdb_info *fdb_info = ptr;
1445 struct dsa_switchdev_event_work *switchdev_work;
1447 if (!dsa_slave_dev_check(dev))
1450 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1451 if (!switchdev_work)
1454 INIT_WORK(&switchdev_work->work,
1455 dsa_slave_switchdev_event_work);
1456 switchdev_work->dev = dev;
1457 switchdev_work->event = event;
1460 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1461 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1462 if (!fdb_info->added_by_user)
1464 if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1466 goto err_fdb_work_init;
1470 kfree(switchdev_work);
1474 dsa_schedule_work(&switchdev_work->work);
1478 kfree(switchdev_work);
1482 static struct notifier_block dsa_slave_nb __read_mostly = {
1483 .notifier_call = dsa_slave_netdevice_event,
1486 static struct notifier_block dsa_slave_switchdev_notifier = {
1487 .notifier_call = dsa_slave_switchdev_event,
1490 int dsa_slave_register_notifier(void)
1494 err = register_netdevice_notifier(&dsa_slave_nb);
1498 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1500 goto err_switchdev_nb;
1505 unregister_netdevice_notifier(&dsa_slave_nb);
1509 void dsa_slave_unregister_notifier(void)
1513 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1515 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1517 err = unregister_netdevice_notifier(&dsa_slave_nb);
1519 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);