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, data + 4 * len);
567 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
568 struct ethtool_stats *stats,
571 struct dsa_port *dp = dsa_slave_to_port(dev);
572 struct dsa_slave_priv *p = netdev_priv(dev);
573 struct dsa_switch *ds = dp->ds;
574 struct pcpu_sw_netstats *s;
578 for_each_possible_cpu(i) {
579 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
581 s = per_cpu_ptr(p->stats64, i);
583 start = u64_stats_fetch_begin_irq(&s->syncp);
584 tx_packets = s->tx_packets;
585 tx_bytes = s->tx_bytes;
586 rx_packets = s->rx_packets;
587 rx_bytes = s->rx_bytes;
588 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
589 data[0] += tx_packets;
591 data[2] += rx_packets;
594 if (ds->ops->get_ethtool_stats)
595 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
598 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
600 struct dsa_port *dp = dsa_slave_to_port(dev);
601 struct dsa_switch *ds = dp->ds;
603 if (sset == ETH_SS_STATS) {
607 if (ds->ops->get_sset_count)
608 count += ds->ops->get_sset_count(ds, dp->index);
616 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
618 struct dsa_port *dp = dsa_slave_to_port(dev);
619 struct dsa_switch *ds = dp->ds;
621 if (ds->ops->get_wol)
622 ds->ops->get_wol(ds, dp->index, w);
625 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
627 struct dsa_port *dp = dsa_slave_to_port(dev);
628 struct dsa_switch *ds = dp->ds;
629 int ret = -EOPNOTSUPP;
631 if (ds->ops->set_wol)
632 ret = ds->ops->set_wol(ds, dp->index, w);
637 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
639 struct dsa_port *dp = dsa_slave_to_port(dev);
640 struct dsa_switch *ds = dp->ds;
643 /* Port's PHY and MAC both need to be EEE capable */
647 if (!ds->ops->set_mac_eee)
650 ret = ds->ops->set_mac_eee(ds, dp->index, e);
654 if (e->eee_enabled) {
655 ret = phy_init_eee(dev->phydev, 0);
660 return phy_ethtool_set_eee(dev->phydev, e);
663 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
665 struct dsa_port *dp = dsa_slave_to_port(dev);
666 struct dsa_switch *ds = dp->ds;
669 /* Port's PHY and MAC both need to be EEE capable */
673 if (!ds->ops->get_mac_eee)
676 ret = ds->ops->get_mac_eee(ds, dp->index, e);
680 return phy_ethtool_get_eee(dev->phydev, e);
683 #ifdef CONFIG_NET_POLL_CONTROLLER
684 static int dsa_slave_netpoll_setup(struct net_device *dev,
685 struct netpoll_info *ni)
687 struct net_device *master = dsa_slave_to_master(dev);
688 struct dsa_slave_priv *p = netdev_priv(dev);
689 struct netpoll *netpoll;
692 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
696 err = __netpoll_setup(netpoll, master);
702 p->netpoll = netpoll;
707 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
709 struct dsa_slave_priv *p = netdev_priv(dev);
710 struct netpoll *netpoll = p->netpoll;
717 __netpoll_free_async(netpoll);
720 static void dsa_slave_poll_controller(struct net_device *dev)
725 static int dsa_slave_get_phys_port_name(struct net_device *dev,
726 char *name, size_t len)
728 struct dsa_port *dp = dsa_slave_to_port(dev);
730 if (snprintf(name, len, "p%d", dp->index) >= len)
736 static struct dsa_mall_tc_entry *
737 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
739 struct dsa_slave_priv *p = netdev_priv(dev);
740 struct dsa_mall_tc_entry *mall_tc_entry;
742 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
743 if (mall_tc_entry->cookie == cookie)
744 return mall_tc_entry;
749 static int dsa_slave_add_cls_matchall(struct net_device *dev,
750 struct tc_cls_matchall_offload *cls,
753 struct dsa_port *dp = dsa_slave_to_port(dev);
754 struct dsa_slave_priv *p = netdev_priv(dev);
755 struct dsa_mall_tc_entry *mall_tc_entry;
756 __be16 protocol = cls->common.protocol;
757 struct dsa_switch *ds = dp->ds;
758 struct net_device *to_dev;
759 const struct tc_action *a;
760 struct dsa_port *to_dp;
761 int err = -EOPNOTSUPP;
764 if (!ds->ops->port_mirror_add)
767 if (!tcf_exts_has_one_action(cls->exts))
770 tcf_exts_to_list(cls->exts, &actions);
771 a = list_first_entry(&actions, struct tc_action, list);
773 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
774 struct dsa_mall_mirror_tc_entry *mirror;
776 to_dev = tcf_mirred_dev(a);
780 if (!dsa_slave_dev_check(to_dev))
783 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
787 mall_tc_entry->cookie = cls->cookie;
788 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
789 mirror = &mall_tc_entry->mirror;
791 to_dp = dsa_slave_to_port(to_dev);
793 mirror->to_local_port = to_dp->index;
794 mirror->ingress = ingress;
796 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
798 kfree(mall_tc_entry);
802 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
808 static void dsa_slave_del_cls_matchall(struct net_device *dev,
809 struct tc_cls_matchall_offload *cls)
811 struct dsa_port *dp = dsa_slave_to_port(dev);
812 struct dsa_mall_tc_entry *mall_tc_entry;
813 struct dsa_switch *ds = dp->ds;
815 if (!ds->ops->port_mirror_del)
818 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
822 list_del(&mall_tc_entry->list);
824 switch (mall_tc_entry->type) {
825 case DSA_PORT_MALL_MIRROR:
826 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
832 kfree(mall_tc_entry);
835 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
836 struct tc_cls_matchall_offload *cls,
839 if (cls->common.chain_index)
842 switch (cls->command) {
843 case TC_CLSMATCHALL_REPLACE:
844 return dsa_slave_add_cls_matchall(dev, cls, ingress);
845 case TC_CLSMATCHALL_DESTROY:
846 dsa_slave_del_cls_matchall(dev, cls);
853 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
854 void *cb_priv, bool ingress)
856 struct net_device *dev = cb_priv;
858 if (!tc_can_offload(dev))
862 case TC_SETUP_CLSMATCHALL:
863 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
869 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
870 void *type_data, void *cb_priv)
872 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
875 static int dsa_slave_setup_tc_block_cb_eg(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, false);
881 static int dsa_slave_setup_tc_block(struct net_device *dev,
882 struct tc_block_offload *f)
886 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
887 cb = dsa_slave_setup_tc_block_cb_ig;
888 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
889 cb = dsa_slave_setup_tc_block_cb_eg;
893 switch (f->command) {
895 return tcf_block_cb_register(f->block, cb, dev, dev);
896 case TC_BLOCK_UNBIND:
897 tcf_block_cb_unregister(f->block, cb, dev);
904 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
909 return dsa_slave_setup_tc_block(dev, type_data);
915 static void dsa_slave_get_stats64(struct net_device *dev,
916 struct rtnl_link_stats64 *stats)
918 struct dsa_slave_priv *p = netdev_priv(dev);
919 struct pcpu_sw_netstats *s;
923 netdev_stats_to_stats64(stats, &dev->stats);
924 for_each_possible_cpu(i) {
925 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
927 s = per_cpu_ptr(p->stats64, i);
929 start = u64_stats_fetch_begin_irq(&s->syncp);
930 tx_packets = s->tx_packets;
931 tx_bytes = s->tx_bytes;
932 rx_packets = s->rx_packets;
933 rx_bytes = s->rx_bytes;
934 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
936 stats->tx_packets += tx_packets;
937 stats->tx_bytes += tx_bytes;
938 stats->rx_packets += rx_packets;
939 stats->rx_bytes += rx_bytes;
943 static int dsa_slave_get_rxnfc(struct net_device *dev,
944 struct ethtool_rxnfc *nfc, u32 *rule_locs)
946 struct dsa_port *dp = dsa_slave_to_port(dev);
947 struct dsa_switch *ds = dp->ds;
949 if (!ds->ops->get_rxnfc)
952 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
955 static int dsa_slave_set_rxnfc(struct net_device *dev,
956 struct ethtool_rxnfc *nfc)
958 struct dsa_port *dp = dsa_slave_to_port(dev);
959 struct dsa_switch *ds = dp->ds;
961 if (!ds->ops->set_rxnfc)
964 return ds->ops->set_rxnfc(ds, dp->index, nfc);
967 static int dsa_slave_get_ts_info(struct net_device *dev,
968 struct ethtool_ts_info *ts)
970 struct dsa_slave_priv *p = netdev_priv(dev);
971 struct dsa_switch *ds = p->dp->ds;
973 if (!ds->ops->get_ts_info)
976 return ds->ops->get_ts_info(ds, p->dp->index, ts);
979 static const struct ethtool_ops dsa_slave_ethtool_ops = {
980 .get_drvinfo = dsa_slave_get_drvinfo,
981 .get_regs_len = dsa_slave_get_regs_len,
982 .get_regs = dsa_slave_get_regs,
983 .nway_reset = phy_ethtool_nway_reset,
984 .get_link = dsa_slave_get_link,
985 .get_eeprom_len = dsa_slave_get_eeprom_len,
986 .get_eeprom = dsa_slave_get_eeprom,
987 .set_eeprom = dsa_slave_set_eeprom,
988 .get_strings = dsa_slave_get_strings,
989 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
990 .get_sset_count = dsa_slave_get_sset_count,
991 .set_wol = dsa_slave_set_wol,
992 .get_wol = dsa_slave_get_wol,
993 .set_eee = dsa_slave_set_eee,
994 .get_eee = dsa_slave_get_eee,
995 .get_link_ksettings = phy_ethtool_get_link_ksettings,
996 .set_link_ksettings = phy_ethtool_set_link_ksettings,
997 .get_rxnfc = dsa_slave_get_rxnfc,
998 .set_rxnfc = dsa_slave_set_rxnfc,
999 .get_ts_info = dsa_slave_get_ts_info,
1002 /* legacy way, bypassing the bridge *****************************************/
1003 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1004 struct net_device *dev,
1005 const unsigned char *addr, u16 vid,
1008 struct dsa_port *dp = dsa_slave_to_port(dev);
1010 return dsa_port_fdb_add(dp, addr, vid);
1013 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1014 struct net_device *dev,
1015 const unsigned char *addr, u16 vid)
1017 struct dsa_port *dp = dsa_slave_to_port(dev);
1019 return dsa_port_fdb_del(dp, addr, vid);
1022 static const struct net_device_ops dsa_slave_netdev_ops = {
1023 .ndo_open = dsa_slave_open,
1024 .ndo_stop = dsa_slave_close,
1025 .ndo_start_xmit = dsa_slave_xmit,
1026 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1027 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1028 .ndo_set_mac_address = dsa_slave_set_mac_address,
1029 .ndo_fdb_add = dsa_legacy_fdb_add,
1030 .ndo_fdb_del = dsa_legacy_fdb_del,
1031 .ndo_fdb_dump = dsa_slave_fdb_dump,
1032 .ndo_do_ioctl = dsa_slave_ioctl,
1033 .ndo_get_iflink = dsa_slave_get_iflink,
1034 #ifdef CONFIG_NET_POLL_CONTROLLER
1035 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1036 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1037 .ndo_poll_controller = dsa_slave_poll_controller,
1039 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1040 .ndo_setup_tc = dsa_slave_setup_tc,
1041 .ndo_get_stats64 = dsa_slave_get_stats64,
1044 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1045 .switchdev_port_attr_get = dsa_slave_port_attr_get,
1046 .switchdev_port_attr_set = dsa_slave_port_attr_set,
1047 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1048 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1051 static struct device_type dsa_type = {
1055 static void dsa_slave_adjust_link(struct net_device *dev)
1057 struct dsa_port *dp = dsa_slave_to_port(dev);
1058 struct dsa_slave_priv *p = netdev_priv(dev);
1059 struct dsa_switch *ds = dp->ds;
1060 unsigned int status_changed = 0;
1062 if (p->old_link != dev->phydev->link) {
1064 p->old_link = dev->phydev->link;
1067 if (p->old_duplex != dev->phydev->duplex) {
1069 p->old_duplex = dev->phydev->duplex;
1072 if (p->old_pause != dev->phydev->pause) {
1074 p->old_pause = dev->phydev->pause;
1077 if (ds->ops->adjust_link && status_changed)
1078 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1081 phy_print_status(dev->phydev);
1084 static int dsa_slave_fixed_link_update(struct net_device *dev,
1085 struct fixed_phy_status *status)
1087 struct dsa_switch *ds;
1088 struct dsa_port *dp;
1091 dp = dsa_slave_to_port(dev);
1093 if (ds->ops->fixed_link_update)
1094 ds->ops->fixed_link_update(ds, dp->index, status);
1100 /* slave device setup *******************************************************/
1101 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1103 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1104 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1105 struct dsa_switch *ds = dp->ds;
1107 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1108 if (!slave_dev->phydev) {
1109 netdev_err(slave_dev, "no phy at %d\n", addr);
1113 /* Use already configured phy mode */
1114 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1115 p->phy_interface = slave_dev->phydev->interface;
1117 return phy_connect_direct(slave_dev, slave_dev->phydev,
1118 dsa_slave_adjust_link, p->phy_interface);
1121 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1123 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1124 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1125 struct device_node *port_dn = dp->dn;
1126 struct dsa_switch *ds = dp->ds;
1127 struct device_node *phy_dn;
1128 bool phy_is_fixed = false;
1132 mode = of_get_phy_mode(port_dn);
1134 mode = PHY_INTERFACE_MODE_NA;
1135 p->phy_interface = mode;
1137 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1138 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1139 /* In the case of a fixed PHY, the DT node associated
1140 * to the fixed PHY is the Port DT node
1142 ret = of_phy_register_fixed_link(port_dn);
1144 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1147 phy_is_fixed = true;
1148 phy_dn = of_node_get(port_dn);
1151 if (ds->ops->get_phy_flags)
1152 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1155 slave_dev->phydev = of_phy_connect(slave_dev, phy_dn,
1156 dsa_slave_adjust_link,
1159 of_node_put(phy_dn);
1162 if (slave_dev->phydev && phy_is_fixed)
1163 fixed_phy_set_link_update(slave_dev->phydev,
1164 dsa_slave_fixed_link_update);
1166 /* We could not connect to a designated PHY, so use the switch internal
1169 if (!slave_dev->phydev) {
1170 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1172 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1175 of_phy_deregister_fixed_link(port_dn);
1180 phy_attached_info(slave_dev->phydev);
1185 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1186 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1187 struct netdev_queue *txq,
1190 lockdep_set_class(&txq->_xmit_lock,
1191 &dsa_slave_netdev_xmit_lock_key);
1194 int dsa_slave_suspend(struct net_device *slave_dev)
1196 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1198 netif_device_detach(slave_dev);
1200 if (slave_dev->phydev) {
1201 phy_stop(slave_dev->phydev);
1205 phy_suspend(slave_dev->phydev);
1211 int dsa_slave_resume(struct net_device *slave_dev)
1213 netif_device_attach(slave_dev);
1215 if (slave_dev->phydev) {
1216 phy_resume(slave_dev->phydev);
1217 phy_start(slave_dev->phydev);
1223 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1225 struct net_device *master = dsa_slave_to_master(dev);
1226 struct dsa_port *dp = dsa_slave_to_port(dev);
1227 struct dsa_notifier_register_info rinfo = {
1228 .switch_number = dp->ds->index,
1229 .port_number = dp->index,
1234 call_dsa_notifiers(val, dev, &rinfo.info);
1237 int dsa_slave_create(struct dsa_port *port)
1239 const struct dsa_port *cpu_dp = port->cpu_dp;
1240 struct net_device *master = cpu_dp->master;
1241 struct dsa_switch *ds = port->ds;
1242 const char *name = port->name;
1243 struct net_device *slave_dev;
1244 struct dsa_slave_priv *p;
1247 if (!ds->num_tx_queues)
1248 ds->num_tx_queues = 1;
1250 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1251 NET_NAME_UNKNOWN, ether_setup,
1252 ds->num_tx_queues, 1);
1253 if (slave_dev == NULL)
1256 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1257 slave_dev->hw_features |= NETIF_F_HW_TC;
1258 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1259 eth_hw_addr_inherit(slave_dev, master);
1260 slave_dev->priv_flags |= IFF_NO_QUEUE;
1261 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1262 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1263 slave_dev->min_mtu = 0;
1264 slave_dev->max_mtu = ETH_MAX_MTU;
1265 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1267 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1270 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1271 slave_dev->dev.of_node = port->dn;
1272 slave_dev->vlan_features = master->vlan_features;
1274 p = netdev_priv(slave_dev);
1275 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1277 free_netdev(slave_dev);
1281 INIT_LIST_HEAD(&p->mall_tc_list);
1282 p->xmit = cpu_dp->tag_ops->xmit;
1288 port->slave = slave_dev;
1290 netif_carrier_off(slave_dev);
1292 ret = dsa_slave_phy_setup(slave_dev);
1294 netdev_err(master, "error %d setting up slave phy\n", ret);
1298 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1300 ret = register_netdev(slave_dev);
1302 netdev_err(master, "error %d registering interface %s\n",
1303 ret, slave_dev->name);
1310 phy_disconnect(slave_dev->phydev);
1311 if (of_phy_is_fixed_link(port->dn))
1312 of_phy_deregister_fixed_link(port->dn);
1314 free_percpu(p->stats64);
1315 free_netdev(slave_dev);
1320 void dsa_slave_destroy(struct net_device *slave_dev)
1322 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1323 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1324 struct device_node *port_dn = dp->dn;
1326 netif_carrier_off(slave_dev);
1327 if (slave_dev->phydev) {
1328 phy_disconnect(slave_dev->phydev);
1330 if (of_phy_is_fixed_link(port_dn))
1331 of_phy_deregister_fixed_link(port_dn);
1333 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1334 unregister_netdev(slave_dev);
1335 free_percpu(p->stats64);
1336 free_netdev(slave_dev);
1339 static bool dsa_slave_dev_check(struct net_device *dev)
1341 return dev->netdev_ops == &dsa_slave_netdev_ops;
1344 static int dsa_slave_changeupper(struct net_device *dev,
1345 struct netdev_notifier_changeupper_info *info)
1347 struct dsa_port *dp = dsa_slave_to_port(dev);
1348 int err = NOTIFY_DONE;
1350 if (netif_is_bridge_master(info->upper_dev)) {
1351 if (info->linking) {
1352 err = dsa_port_bridge_join(dp, info->upper_dev);
1353 err = notifier_from_errno(err);
1355 dsa_port_bridge_leave(dp, info->upper_dev);
1363 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1364 unsigned long event, void *ptr)
1366 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1368 if (!dsa_slave_dev_check(dev))
1371 if (event == NETDEV_CHANGEUPPER)
1372 return dsa_slave_changeupper(dev, ptr);
1377 struct dsa_switchdev_event_work {
1378 struct work_struct work;
1379 struct switchdev_notifier_fdb_info fdb_info;
1380 struct net_device *dev;
1381 unsigned long event;
1384 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1386 struct dsa_switchdev_event_work *switchdev_work =
1387 container_of(work, struct dsa_switchdev_event_work, work);
1388 struct net_device *dev = switchdev_work->dev;
1389 struct switchdev_notifier_fdb_info *fdb_info;
1390 struct dsa_port *dp = dsa_slave_to_port(dev);
1394 switch (switchdev_work->event) {
1395 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1396 fdb_info = &switchdev_work->fdb_info;
1397 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1399 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1402 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1406 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1407 fdb_info = &switchdev_work->fdb_info;
1408 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1410 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1417 kfree(switchdev_work->fdb_info.addr);
1418 kfree(switchdev_work);
1423 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1425 const struct switchdev_notifier_fdb_info *
1428 memcpy(&switchdev_work->fdb_info, fdb_info,
1429 sizeof(switchdev_work->fdb_info));
1430 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1431 if (!switchdev_work->fdb_info.addr)
1433 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1438 /* Called under rcu_read_lock() */
1439 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1440 unsigned long event, void *ptr)
1442 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1443 struct dsa_switchdev_event_work *switchdev_work;
1445 if (!dsa_slave_dev_check(dev))
1448 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1449 if (!switchdev_work)
1452 INIT_WORK(&switchdev_work->work,
1453 dsa_slave_switchdev_event_work);
1454 switchdev_work->dev = dev;
1455 switchdev_work->event = event;
1458 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1459 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1460 if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1462 goto err_fdb_work_init;
1466 kfree(switchdev_work);
1470 dsa_schedule_work(&switchdev_work->work);
1474 kfree(switchdev_work);
1478 static struct notifier_block dsa_slave_nb __read_mostly = {
1479 .notifier_call = dsa_slave_netdevice_event,
1482 static struct notifier_block dsa_slave_switchdev_notifier = {
1483 .notifier_call = dsa_slave_switchdev_event,
1486 int dsa_slave_register_notifier(void)
1490 err = register_netdevice_notifier(&dsa_slave_nb);
1494 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1496 goto err_switchdev_nb;
1501 unregister_netdevice_notifier(&dsa_slave_nb);
1505 void dsa_slave_unregister_notifier(void)
1509 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1511 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1513 err = unregister_netdevice_notifier(&dsa_slave_nb);
1515 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);