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 <linux/list.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>
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->tree, 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 struct dsa_slave_priv *p = netdev_priv(dev);
69 return dsa_master_netdev(p)->ifindex;
72 static int dsa_slave_open(struct net_device *dev)
74 struct dsa_slave_priv *p = netdev_priv(dev);
75 struct dsa_port *dp = p->dp;
76 struct net_device *master = dsa_master_netdev(p);
79 if (!(master->flags & IFF_UP))
82 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
83 err = dev_uc_add(master, dev->dev_addr);
88 if (dev->flags & IFF_ALLMULTI) {
89 err = dev_set_allmulti(master, 1);
93 if (dev->flags & IFF_PROMISC) {
94 err = dev_set_promiscuity(master, 1);
99 err = dsa_port_enable(dp, dev->phydev);
104 phy_start(dev->phydev);
109 if (dev->flags & IFF_PROMISC)
110 dev_set_promiscuity(master, -1);
112 if (dev->flags & IFF_ALLMULTI)
113 dev_set_allmulti(master, -1);
115 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
116 dev_uc_del(master, dev->dev_addr);
121 static int dsa_slave_close(struct net_device *dev)
123 struct dsa_slave_priv *p = netdev_priv(dev);
124 struct net_device *master = dsa_master_netdev(p);
125 struct dsa_port *dp = p->dp;
128 phy_stop(dev->phydev);
130 dsa_port_disable(dp, dev->phydev);
132 dev_mc_unsync(master, dev);
133 dev_uc_unsync(master, dev);
134 if (dev->flags & IFF_ALLMULTI)
135 dev_set_allmulti(master, -1);
136 if (dev->flags & IFF_PROMISC)
137 dev_set_promiscuity(master, -1);
139 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
140 dev_uc_del(master, dev->dev_addr);
145 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
147 struct dsa_slave_priv *p = netdev_priv(dev);
148 struct net_device *master = dsa_master_netdev(p);
150 if (change & IFF_ALLMULTI)
151 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
152 if (change & IFF_PROMISC)
153 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
156 static void dsa_slave_set_rx_mode(struct net_device *dev)
158 struct dsa_slave_priv *p = netdev_priv(dev);
159 struct net_device *master = dsa_master_netdev(p);
161 dev_mc_sync(master, dev);
162 dev_uc_sync(master, dev);
165 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
167 struct dsa_slave_priv *p = netdev_priv(dev);
168 struct net_device *master = dsa_master_netdev(p);
169 struct sockaddr *addr = a;
172 if (!is_valid_ether_addr(addr->sa_data))
173 return -EADDRNOTAVAIL;
175 if (!(dev->flags & IFF_UP))
178 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
179 err = dev_uc_add(master, addr->sa_data);
184 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
185 dev_uc_del(master, dev->dev_addr);
188 ether_addr_copy(dev->dev_addr, addr->sa_data);
193 struct dsa_slave_dump_ctx {
194 struct net_device *dev;
196 struct netlink_callback *cb;
201 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
202 bool is_static, void *data)
204 struct dsa_slave_dump_ctx *dump = data;
205 u32 portid = NETLINK_CB(dump->cb->skb).portid;
206 u32 seq = dump->cb->nlh->nlmsg_seq;
207 struct nlmsghdr *nlh;
210 if (dump->idx < dump->cb->args[2])
213 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
214 sizeof(*ndm), NLM_F_MULTI);
218 ndm = nlmsg_data(nlh);
219 ndm->ndm_family = AF_BRIDGE;
222 ndm->ndm_flags = NTF_SELF;
224 ndm->ndm_ifindex = dump->dev->ifindex;
225 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
227 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
228 goto nla_put_failure;
230 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
231 goto nla_put_failure;
233 nlmsg_end(dump->skb, nlh);
240 nlmsg_cancel(dump->skb, nlh);
245 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
246 struct net_device *dev, struct net_device *filter_dev,
249 struct dsa_slave_dump_ctx dump = {
255 struct dsa_slave_priv *p = netdev_priv(dev);
256 struct dsa_port *dp = p->dp;
259 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
265 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
270 return phy_mii_ioctl(dev->phydev, ifr, cmd);
273 static int dsa_slave_port_attr_set(struct net_device *dev,
274 const struct switchdev_attr *attr,
275 struct switchdev_trans *trans)
277 struct dsa_slave_priv *p = netdev_priv(dev);
278 struct dsa_port *dp = p->dp;
282 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
283 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
285 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
286 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
289 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
290 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
300 static int dsa_slave_port_obj_add(struct net_device *dev,
301 const struct switchdev_obj *obj,
302 struct switchdev_trans *trans)
304 struct dsa_slave_priv *p = netdev_priv(dev);
305 struct dsa_port *dp = p->dp;
308 /* For the prepare phase, ensure the full set of changes is feasable in
309 * one go in order to signal a failure properly. If an operation is not
310 * supported, return -EOPNOTSUPP.
314 case SWITCHDEV_OBJ_ID_PORT_MDB:
315 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
317 case SWITCHDEV_OBJ_ID_PORT_VLAN:
318 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
329 static int dsa_slave_port_obj_del(struct net_device *dev,
330 const struct switchdev_obj *obj)
332 struct dsa_slave_priv *p = netdev_priv(dev);
333 struct dsa_port *dp = p->dp;
337 case SWITCHDEV_OBJ_ID_PORT_MDB:
338 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
340 case SWITCHDEV_OBJ_ID_PORT_VLAN:
341 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
351 static int dsa_slave_port_attr_get(struct net_device *dev,
352 struct switchdev_attr *attr)
354 struct dsa_slave_priv *p = netdev_priv(dev);
355 struct dsa_switch *ds = p->dp->ds;
358 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
359 attr->u.ppid.id_len = sizeof(ds->index);
360 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
362 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
363 attr->u.brport_flags_support = 0;
372 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
375 #ifdef CONFIG_NET_POLL_CONTROLLER
376 struct dsa_slave_priv *p = netdev_priv(dev);
379 netpoll_send_skb(p->netpoll, skb);
386 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
388 struct dsa_slave_priv *p = netdev_priv(dev);
389 struct pcpu_sw_netstats *s;
390 struct sk_buff *nskb;
392 s = this_cpu_ptr(p->stats64);
393 u64_stats_update_begin(&s->syncp);
395 s->tx_bytes += skb->len;
396 u64_stats_update_end(&s->syncp);
398 /* Transmit function may have to reallocate the original SKB,
399 * in which case it must have freed it. Only free it here on error.
401 nskb = p->xmit(skb, dev);
407 /* SKB for netpoll still need to be mangled with the protocol-specific
408 * tag to be successfully transmitted
410 if (unlikely(netpoll_tx_running(dev)))
411 return dsa_slave_netpoll_send_skb(dev, nskb);
413 /* Queue the SKB for transmission on the parent interface, but
414 * do not modify its EtherType
416 nskb->dev = dsa_master_netdev(p);
417 dev_queue_xmit(nskb);
422 /* ethtool operations *******************************************************/
424 static void dsa_slave_get_drvinfo(struct net_device *dev,
425 struct ethtool_drvinfo *drvinfo)
427 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
428 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
429 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
432 static int dsa_slave_get_regs_len(struct net_device *dev)
434 struct dsa_slave_priv *p = netdev_priv(dev);
435 struct dsa_switch *ds = p->dp->ds;
437 if (ds->ops->get_regs_len)
438 return ds->ops->get_regs_len(ds, p->dp->index);
444 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
446 struct dsa_slave_priv *p = netdev_priv(dev);
447 struct dsa_switch *ds = p->dp->ds;
449 if (ds->ops->get_regs)
450 ds->ops->get_regs(ds, p->dp->index, regs, _p);
453 static u32 dsa_slave_get_link(struct net_device *dev)
458 genphy_update_link(dev->phydev);
460 return dev->phydev->link;
463 static int dsa_slave_get_eeprom_len(struct net_device *dev)
465 struct dsa_slave_priv *p = netdev_priv(dev);
466 struct dsa_switch *ds = p->dp->ds;
468 if (ds->cd && ds->cd->eeprom_len)
469 return ds->cd->eeprom_len;
471 if (ds->ops->get_eeprom_len)
472 return ds->ops->get_eeprom_len(ds);
477 static int dsa_slave_get_eeprom(struct net_device *dev,
478 struct ethtool_eeprom *eeprom, u8 *data)
480 struct dsa_slave_priv *p = netdev_priv(dev);
481 struct dsa_switch *ds = p->dp->ds;
483 if (ds->ops->get_eeprom)
484 return ds->ops->get_eeprom(ds, eeprom, data);
489 static int dsa_slave_set_eeprom(struct net_device *dev,
490 struct ethtool_eeprom *eeprom, u8 *data)
492 struct dsa_slave_priv *p = netdev_priv(dev);
493 struct dsa_switch *ds = p->dp->ds;
495 if (ds->ops->set_eeprom)
496 return ds->ops->set_eeprom(ds, eeprom, data);
501 static void dsa_slave_get_strings(struct net_device *dev,
502 uint32_t stringset, uint8_t *data)
504 struct dsa_slave_priv *p = netdev_priv(dev);
505 struct dsa_switch *ds = p->dp->ds;
507 if (stringset == ETH_SS_STATS) {
508 int len = ETH_GSTRING_LEN;
510 strncpy(data, "tx_packets", len);
511 strncpy(data + len, "tx_bytes", len);
512 strncpy(data + 2 * len, "rx_packets", len);
513 strncpy(data + 3 * len, "rx_bytes", len);
514 if (ds->ops->get_strings)
515 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
519 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
520 struct ethtool_stats *stats,
523 struct dsa_slave_priv *p = netdev_priv(dev);
524 struct dsa_switch *ds = p->dp->ds;
525 struct pcpu_sw_netstats *s;
529 for_each_possible_cpu(i) {
530 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
532 s = per_cpu_ptr(p->stats64, i);
534 start = u64_stats_fetch_begin_irq(&s->syncp);
535 tx_packets = s->tx_packets;
536 tx_bytes = s->tx_bytes;
537 rx_packets = s->rx_packets;
538 rx_bytes = s->rx_bytes;
539 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
540 data[0] += tx_packets;
542 data[2] += rx_packets;
545 if (ds->ops->get_ethtool_stats)
546 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
549 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
551 struct dsa_slave_priv *p = netdev_priv(dev);
552 struct dsa_switch *ds = p->dp->ds;
554 if (sset == ETH_SS_STATS) {
558 if (ds->ops->get_sset_count)
559 count += ds->ops->get_sset_count(ds);
567 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
569 struct dsa_slave_priv *p = netdev_priv(dev);
570 struct dsa_switch *ds = p->dp->ds;
572 if (ds->ops->get_wol)
573 ds->ops->get_wol(ds, p->dp->index, w);
576 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
578 struct dsa_slave_priv *p = netdev_priv(dev);
579 struct dsa_switch *ds = p->dp->ds;
580 int ret = -EOPNOTSUPP;
582 if (ds->ops->set_wol)
583 ret = ds->ops->set_wol(ds, p->dp->index, w);
588 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
590 struct dsa_slave_priv *p = netdev_priv(dev);
591 struct dsa_switch *ds = p->dp->ds;
594 /* Port's PHY and MAC both need to be EEE capable */
598 if (!ds->ops->set_mac_eee)
601 ret = ds->ops->set_mac_eee(ds, p->dp->index, e);
605 if (e->eee_enabled) {
606 ret = phy_init_eee(dev->phydev, 0);
611 return phy_ethtool_set_eee(dev->phydev, e);
614 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
616 struct dsa_slave_priv *p = netdev_priv(dev);
617 struct dsa_switch *ds = p->dp->ds;
620 /* Port's PHY and MAC both need to be EEE capable */
624 if (!ds->ops->get_mac_eee)
627 ret = ds->ops->get_mac_eee(ds, p->dp->index, e);
631 return phy_ethtool_get_eee(dev->phydev, e);
634 #ifdef CONFIG_NET_POLL_CONTROLLER
635 static int dsa_slave_netpoll_setup(struct net_device *dev,
636 struct netpoll_info *ni)
638 struct dsa_slave_priv *p = netdev_priv(dev);
639 struct net_device *master = dsa_master_netdev(p);
640 struct netpoll *netpoll;
643 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
647 err = __netpoll_setup(netpoll, master);
653 p->netpoll = netpoll;
658 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
660 struct dsa_slave_priv *p = netdev_priv(dev);
661 struct netpoll *netpoll = p->netpoll;
668 __netpoll_free_async(netpoll);
671 static void dsa_slave_poll_controller(struct net_device *dev)
676 static int dsa_slave_get_phys_port_name(struct net_device *dev,
677 char *name, size_t len)
679 struct dsa_slave_priv *p = netdev_priv(dev);
681 if (snprintf(name, len, "p%d", p->dp->index) >= len)
687 static struct dsa_mall_tc_entry *
688 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
690 struct dsa_slave_priv *p = netdev_priv(dev);
691 struct dsa_mall_tc_entry *mall_tc_entry;
693 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
694 if (mall_tc_entry->cookie == cookie)
695 return mall_tc_entry;
700 static int dsa_slave_add_cls_matchall(struct net_device *dev,
701 struct tc_cls_matchall_offload *cls,
704 struct dsa_slave_priv *p = netdev_priv(dev);
705 struct dsa_mall_tc_entry *mall_tc_entry;
706 __be16 protocol = cls->common.protocol;
707 struct dsa_switch *ds = p->dp->ds;
708 struct net *net = dev_net(dev);
709 struct dsa_slave_priv *to_p;
710 struct net_device *to_dev;
711 const struct tc_action *a;
712 int err = -EOPNOTSUPP;
716 if (!ds->ops->port_mirror_add)
719 if (!tcf_exts_has_one_action(cls->exts))
722 tcf_exts_to_list(cls->exts, &actions);
723 a = list_first_entry(&actions, struct tc_action, list);
725 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
726 struct dsa_mall_mirror_tc_entry *mirror;
728 ifindex = tcf_mirred_ifindex(a);
729 to_dev = __dev_get_by_index(net, ifindex);
733 if (!dsa_slave_dev_check(to_dev))
736 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
740 mall_tc_entry->cookie = cls->cookie;
741 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
742 mirror = &mall_tc_entry->mirror;
744 to_p = netdev_priv(to_dev);
746 mirror->to_local_port = to_p->dp->index;
747 mirror->ingress = ingress;
749 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
752 kfree(mall_tc_entry);
756 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
762 static void dsa_slave_del_cls_matchall(struct net_device *dev,
763 struct tc_cls_matchall_offload *cls)
765 struct dsa_slave_priv *p = netdev_priv(dev);
766 struct dsa_mall_tc_entry *mall_tc_entry;
767 struct dsa_switch *ds = p->dp->ds;
769 if (!ds->ops->port_mirror_del)
772 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
776 list_del(&mall_tc_entry->list);
778 switch (mall_tc_entry->type) {
779 case DSA_PORT_MALL_MIRROR:
780 ds->ops->port_mirror_del(ds, p->dp->index,
781 &mall_tc_entry->mirror);
787 kfree(mall_tc_entry);
790 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
791 struct tc_cls_matchall_offload *cls)
795 if (is_classid_clsact_ingress(cls->common.classid))
797 else if (is_classid_clsact_egress(cls->common.classid))
802 if (cls->common.chain_index)
805 switch (cls->command) {
806 case TC_CLSMATCHALL_REPLACE:
807 return dsa_slave_add_cls_matchall(dev, cls, ingress);
808 case TC_CLSMATCHALL_DESTROY:
809 dsa_slave_del_cls_matchall(dev, cls);
816 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
820 case TC_SETUP_CLSMATCHALL:
821 return dsa_slave_setup_tc_cls_matchall(dev, type_data);
827 static void dsa_slave_get_stats64(struct net_device *dev,
828 struct rtnl_link_stats64 *stats)
830 struct dsa_slave_priv *p = netdev_priv(dev);
831 struct pcpu_sw_netstats *s;
835 netdev_stats_to_stats64(stats, &dev->stats);
836 for_each_possible_cpu(i) {
837 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
839 s = per_cpu_ptr(p->stats64, i);
841 start = u64_stats_fetch_begin_irq(&s->syncp);
842 tx_packets = s->tx_packets;
843 tx_bytes = s->tx_bytes;
844 rx_packets = s->rx_packets;
845 rx_bytes = s->rx_bytes;
846 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
848 stats->tx_packets += tx_packets;
849 stats->tx_bytes += tx_bytes;
850 stats->rx_packets += rx_packets;
851 stats->rx_bytes += rx_bytes;
855 static int dsa_slave_get_rxnfc(struct net_device *dev,
856 struct ethtool_rxnfc *nfc, u32 *rule_locs)
858 struct dsa_slave_priv *p = netdev_priv(dev);
859 struct dsa_switch *ds = p->dp->ds;
861 if (!ds->ops->get_rxnfc)
864 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
867 static int dsa_slave_set_rxnfc(struct net_device *dev,
868 struct ethtool_rxnfc *nfc)
870 struct dsa_slave_priv *p = netdev_priv(dev);
871 struct dsa_switch *ds = p->dp->ds;
873 if (!ds->ops->set_rxnfc)
876 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
879 static const struct ethtool_ops dsa_slave_ethtool_ops = {
880 .get_drvinfo = dsa_slave_get_drvinfo,
881 .get_regs_len = dsa_slave_get_regs_len,
882 .get_regs = dsa_slave_get_regs,
883 .nway_reset = phy_ethtool_nway_reset,
884 .get_link = dsa_slave_get_link,
885 .get_eeprom_len = dsa_slave_get_eeprom_len,
886 .get_eeprom = dsa_slave_get_eeprom,
887 .set_eeprom = dsa_slave_set_eeprom,
888 .get_strings = dsa_slave_get_strings,
889 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
890 .get_sset_count = dsa_slave_get_sset_count,
891 .set_wol = dsa_slave_set_wol,
892 .get_wol = dsa_slave_get_wol,
893 .set_eee = dsa_slave_set_eee,
894 .get_eee = dsa_slave_get_eee,
895 .get_link_ksettings = phy_ethtool_get_link_ksettings,
896 .set_link_ksettings = phy_ethtool_set_link_ksettings,
897 .get_rxnfc = dsa_slave_get_rxnfc,
898 .set_rxnfc = dsa_slave_set_rxnfc,
901 static const struct net_device_ops dsa_slave_netdev_ops = {
902 .ndo_open = dsa_slave_open,
903 .ndo_stop = dsa_slave_close,
904 .ndo_start_xmit = dsa_slave_xmit,
905 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
906 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
907 .ndo_set_mac_address = dsa_slave_set_mac_address,
908 .ndo_fdb_add = dsa_legacy_fdb_add,
909 .ndo_fdb_del = dsa_legacy_fdb_del,
910 .ndo_fdb_dump = dsa_slave_fdb_dump,
911 .ndo_do_ioctl = dsa_slave_ioctl,
912 .ndo_get_iflink = dsa_slave_get_iflink,
913 #ifdef CONFIG_NET_POLL_CONTROLLER
914 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
915 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
916 .ndo_poll_controller = dsa_slave_poll_controller,
918 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
919 .ndo_setup_tc = dsa_slave_setup_tc,
920 .ndo_get_stats64 = dsa_slave_get_stats64,
923 static const struct switchdev_ops dsa_slave_switchdev_ops = {
924 .switchdev_port_attr_get = dsa_slave_port_attr_get,
925 .switchdev_port_attr_set = dsa_slave_port_attr_set,
926 .switchdev_port_obj_add = dsa_slave_port_obj_add,
927 .switchdev_port_obj_del = dsa_slave_port_obj_del,
930 static struct device_type dsa_type = {
934 static void dsa_slave_adjust_link(struct net_device *dev)
936 struct dsa_slave_priv *p = netdev_priv(dev);
937 struct dsa_switch *ds = p->dp->ds;
938 unsigned int status_changed = 0;
940 if (p->old_link != dev->phydev->link) {
942 p->old_link = dev->phydev->link;
945 if (p->old_duplex != dev->phydev->duplex) {
947 p->old_duplex = dev->phydev->duplex;
950 if (p->old_pause != dev->phydev->pause) {
952 p->old_pause = dev->phydev->pause;
955 if (ds->ops->adjust_link && status_changed)
956 ds->ops->adjust_link(ds, p->dp->index, dev->phydev);
959 phy_print_status(dev->phydev);
962 static int dsa_slave_fixed_link_update(struct net_device *dev,
963 struct fixed_phy_status *status)
965 struct dsa_slave_priv *p;
966 struct dsa_switch *ds;
969 p = netdev_priv(dev);
971 if (ds->ops->fixed_link_update)
972 ds->ops->fixed_link_update(ds, p->dp->index, status);
978 /* slave device setup *******************************************************/
979 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
981 struct dsa_slave_priv *p = netdev_priv(slave_dev);
982 struct dsa_switch *ds = p->dp->ds;
984 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
985 if (!slave_dev->phydev) {
986 netdev_err(slave_dev, "no phy at %d\n", addr);
990 /* Use already configured phy mode */
991 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
992 p->phy_interface = slave_dev->phydev->interface;
994 return phy_connect_direct(slave_dev, slave_dev->phydev,
995 dsa_slave_adjust_link, p->phy_interface);
998 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1000 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1001 struct dsa_switch *ds = p->dp->ds;
1002 struct device_node *phy_dn, *port_dn;
1003 bool phy_is_fixed = false;
1007 port_dn = p->dp->dn;
1008 mode = of_get_phy_mode(port_dn);
1010 mode = PHY_INTERFACE_MODE_NA;
1011 p->phy_interface = mode;
1013 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1014 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1015 /* In the case of a fixed PHY, the DT node associated
1016 * to the fixed PHY is the Port DT node
1018 ret = of_phy_register_fixed_link(port_dn);
1020 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1023 phy_is_fixed = true;
1024 phy_dn = of_node_get(port_dn);
1027 if (ds->ops->get_phy_flags)
1028 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
1031 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1033 /* If this PHY address is part of phys_mii_mask, which means
1034 * that we need to divert reads and writes to/from it, then we
1035 * want to bind this device using the slave MII bus created by
1036 * DSA to make that happen.
1038 if (!phy_is_fixed && phy_id >= 0 &&
1039 (ds->phys_mii_mask & (1 << phy_id))) {
1040 ret = dsa_slave_phy_connect(slave_dev, phy_id);
1042 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1043 of_node_put(phy_dn);
1047 slave_dev->phydev = of_phy_connect(slave_dev, phy_dn,
1048 dsa_slave_adjust_link,
1053 of_node_put(phy_dn);
1056 if (slave_dev->phydev && phy_is_fixed)
1057 fixed_phy_set_link_update(slave_dev->phydev,
1058 dsa_slave_fixed_link_update);
1060 /* We could not connect to a designated PHY, so use the switch internal
1063 if (!slave_dev->phydev) {
1064 ret = dsa_slave_phy_connect(slave_dev, p->dp->index);
1066 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1069 of_phy_deregister_fixed_link(port_dn);
1074 phy_attached_info(slave_dev->phydev);
1079 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1080 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1081 struct netdev_queue *txq,
1084 lockdep_set_class(&txq->_xmit_lock,
1085 &dsa_slave_netdev_xmit_lock_key);
1088 int dsa_slave_suspend(struct net_device *slave_dev)
1090 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1092 netif_device_detach(slave_dev);
1094 if (slave_dev->phydev) {
1095 phy_stop(slave_dev->phydev);
1099 phy_suspend(slave_dev->phydev);
1105 int dsa_slave_resume(struct net_device *slave_dev)
1107 netif_device_attach(slave_dev);
1109 if (slave_dev->phydev) {
1110 phy_resume(slave_dev->phydev);
1111 phy_start(slave_dev->phydev);
1117 int dsa_slave_create(struct dsa_port *port, const char *name)
1119 struct dsa_switch *ds = port->ds;
1120 struct dsa_switch_tree *dst = ds->dst;
1121 struct net_device *master;
1122 struct net_device *slave_dev;
1123 struct dsa_slave_priv *p;
1124 struct dsa_port *cpu_dp;
1127 cpu_dp = ds->dst->cpu_dp;
1128 master = cpu_dp->netdev;
1130 if (!ds->num_tx_queues)
1131 ds->num_tx_queues = 1;
1133 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1134 NET_NAME_UNKNOWN, ether_setup,
1135 ds->num_tx_queues, 1);
1136 if (slave_dev == NULL)
1139 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1140 slave_dev->hw_features |= NETIF_F_HW_TC;
1141 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1142 eth_hw_addr_inherit(slave_dev, master);
1143 slave_dev->priv_flags |= IFF_NO_QUEUE;
1144 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1145 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1146 slave_dev->min_mtu = 0;
1147 slave_dev->max_mtu = ETH_MAX_MTU;
1148 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1150 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1153 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1154 slave_dev->dev.of_node = port->dn;
1155 slave_dev->vlan_features = master->vlan_features;
1157 p = netdev_priv(slave_dev);
1158 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1160 free_netdev(slave_dev);
1164 INIT_LIST_HEAD(&p->mall_tc_list);
1165 p->xmit = dst->tag_ops->xmit;
1171 port->netdev = slave_dev;
1172 ret = register_netdev(slave_dev);
1174 netdev_err(master, "error %d registering interface %s\n",
1175 ret, slave_dev->name);
1176 port->netdev = NULL;
1177 free_percpu(p->stats64);
1178 free_netdev(slave_dev);
1182 netif_carrier_off(slave_dev);
1184 ret = dsa_slave_phy_setup(slave_dev);
1186 netdev_err(master, "error %d setting up slave phy\n", ret);
1187 unregister_netdev(slave_dev);
1188 free_percpu(p->stats64);
1189 free_netdev(slave_dev);
1196 void dsa_slave_destroy(struct net_device *slave_dev)
1198 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1199 struct device_node *port_dn;
1201 port_dn = p->dp->dn;
1203 netif_carrier_off(slave_dev);
1204 if (slave_dev->phydev) {
1205 phy_disconnect(slave_dev->phydev);
1207 if (of_phy_is_fixed_link(port_dn))
1208 of_phy_deregister_fixed_link(port_dn);
1210 unregister_netdev(slave_dev);
1211 free_percpu(p->stats64);
1212 free_netdev(slave_dev);
1215 static bool dsa_slave_dev_check(struct net_device *dev)
1217 return dev->netdev_ops == &dsa_slave_netdev_ops;
1220 static int dsa_slave_changeupper(struct net_device *dev,
1221 struct netdev_notifier_changeupper_info *info)
1223 struct dsa_slave_priv *p = netdev_priv(dev);
1224 struct dsa_port *dp = p->dp;
1225 int err = NOTIFY_DONE;
1227 if (netif_is_bridge_master(info->upper_dev)) {
1228 if (info->linking) {
1229 err = dsa_port_bridge_join(dp, info->upper_dev);
1230 err = notifier_from_errno(err);
1232 dsa_port_bridge_leave(dp, info->upper_dev);
1240 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1241 unsigned long event, void *ptr)
1243 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1245 if (!dsa_slave_dev_check(dev))
1248 if (event == NETDEV_CHANGEUPPER)
1249 return dsa_slave_changeupper(dev, ptr);
1254 struct dsa_switchdev_event_work {
1255 struct work_struct work;
1256 struct switchdev_notifier_fdb_info fdb_info;
1257 struct net_device *dev;
1258 unsigned long event;
1261 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1263 struct dsa_switchdev_event_work *switchdev_work =
1264 container_of(work, struct dsa_switchdev_event_work, work);
1265 struct net_device *dev = switchdev_work->dev;
1266 struct switchdev_notifier_fdb_info *fdb_info;
1267 struct dsa_slave_priv *p = netdev_priv(dev);
1271 switch (switchdev_work->event) {
1272 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1273 fdb_info = &switchdev_work->fdb_info;
1274 err = dsa_port_fdb_add(p->dp, fdb_info->addr, fdb_info->vid);
1276 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1279 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1283 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1284 fdb_info = &switchdev_work->fdb_info;
1285 err = dsa_port_fdb_del(p->dp, fdb_info->addr, fdb_info->vid);
1287 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1294 kfree(switchdev_work->fdb_info.addr);
1295 kfree(switchdev_work);
1300 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1302 const struct switchdev_notifier_fdb_info *
1305 memcpy(&switchdev_work->fdb_info, fdb_info,
1306 sizeof(switchdev_work->fdb_info));
1307 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1308 if (!switchdev_work->fdb_info.addr)
1310 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1315 /* Called under rcu_read_lock() */
1316 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1317 unsigned long event, void *ptr)
1319 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1320 struct dsa_switchdev_event_work *switchdev_work;
1322 if (!dsa_slave_dev_check(dev))
1325 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1326 if (!switchdev_work)
1329 INIT_WORK(&switchdev_work->work,
1330 dsa_slave_switchdev_event_work);
1331 switchdev_work->dev = dev;
1332 switchdev_work->event = event;
1335 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1336 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1337 if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1339 goto err_fdb_work_init;
1343 kfree(switchdev_work);
1347 dsa_schedule_work(&switchdev_work->work);
1351 kfree(switchdev_work);
1355 static struct notifier_block dsa_slave_nb __read_mostly = {
1356 .notifier_call = dsa_slave_netdevice_event,
1359 static struct notifier_block dsa_slave_switchdev_notifier = {
1360 .notifier_call = dsa_slave_switchdev_event,
1363 int dsa_slave_register_notifier(void)
1367 err = register_netdevice_notifier(&dsa_slave_nb);
1371 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1373 goto err_switchdev_nb;
1378 unregister_netdevice_notifier(&dsa_slave_nb);
1382 void dsa_slave_unregister_notifier(void)
1386 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1388 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1390 err = unregister_netdevice_notifier(&dsa_slave_nb);
1392 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);