2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/list.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/phylink.h>
17 #include <linux/of_net.h>
18 #include <linux/of_mdio.h>
19 #include <linux/mdio.h>
20 #include <net/rtnetlink.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_mirred.h>
23 #include <linux/if_bridge.h>
24 #include <linux/netpoll.h>
25 #include <linux/ptp_classify.h>
29 static bool dsa_slave_dev_check(struct net_device *dev);
31 /* slave mii_bus handling ***************************************************/
32 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
34 struct dsa_switch *ds = bus->priv;
36 if (ds->phys_mii_mask & (1 << addr))
37 return ds->ops->phy_read(ds, addr, reg);
42 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
44 struct dsa_switch *ds = bus->priv;
46 if (ds->phys_mii_mask & (1 << addr))
47 return ds->ops->phy_write(ds, addr, reg, val);
52 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
54 ds->slave_mii_bus->priv = (void *)ds;
55 ds->slave_mii_bus->name = "dsa slave smi";
56 ds->slave_mii_bus->read = dsa_slave_phy_read;
57 ds->slave_mii_bus->write = dsa_slave_phy_write;
58 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
59 ds->dst->index, ds->index);
60 ds->slave_mii_bus->parent = ds->dev;
61 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
65 /* slave device handling ****************************************************/
66 static int dsa_slave_get_iflink(const struct net_device *dev)
68 return dsa_slave_to_master(dev)->ifindex;
71 static int dsa_slave_open(struct net_device *dev)
73 struct net_device *master = dsa_slave_to_master(dev);
74 struct dsa_port *dp = dsa_slave_to_port(dev);
77 if (!(master->flags & IFF_UP))
80 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
81 err = dev_uc_add(master, dev->dev_addr);
86 if (dev->flags & IFF_ALLMULTI) {
87 err = dev_set_allmulti(master, 1);
91 if (dev->flags & IFF_PROMISC) {
92 err = dev_set_promiscuity(master, 1);
97 err = dsa_port_enable(dp, dev->phydev);
101 phylink_start(dp->pl);
106 if (dev->flags & IFF_PROMISC)
107 dev_set_promiscuity(master, -1);
109 if (dev->flags & IFF_ALLMULTI)
110 dev_set_allmulti(master, -1);
112 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
113 dev_uc_del(master, dev->dev_addr);
118 static int dsa_slave_close(struct net_device *dev)
120 struct net_device *master = dsa_slave_to_master(dev);
121 struct dsa_port *dp = dsa_slave_to_port(dev);
123 phylink_stop(dp->pl);
125 dsa_port_disable(dp, dev->phydev);
127 dev_mc_unsync(master, dev);
128 dev_uc_unsync(master, dev);
129 if (dev->flags & IFF_ALLMULTI)
130 dev_set_allmulti(master, -1);
131 if (dev->flags & IFF_PROMISC)
132 dev_set_promiscuity(master, -1);
134 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
135 dev_uc_del(master, dev->dev_addr);
140 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
142 struct net_device *master = dsa_slave_to_master(dev);
143 if (dev->flags & IFF_UP) {
144 if (change & IFF_ALLMULTI)
145 dev_set_allmulti(master,
146 dev->flags & IFF_ALLMULTI ? 1 : -1);
147 if (change & IFF_PROMISC)
148 dev_set_promiscuity(master,
149 dev->flags & IFF_PROMISC ? 1 : -1);
153 static void dsa_slave_set_rx_mode(struct net_device *dev)
155 struct net_device *master = dsa_slave_to_master(dev);
157 dev_mc_sync(master, dev);
158 dev_uc_sync(master, dev);
161 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
163 struct net_device *master = dsa_slave_to_master(dev);
164 struct sockaddr *addr = a;
167 if (!is_valid_ether_addr(addr->sa_data))
168 return -EADDRNOTAVAIL;
170 if (!(dev->flags & IFF_UP))
173 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
174 err = dev_uc_add(master, addr->sa_data);
179 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
180 dev_uc_del(master, dev->dev_addr);
183 ether_addr_copy(dev->dev_addr, addr->sa_data);
188 struct dsa_slave_dump_ctx {
189 struct net_device *dev;
191 struct netlink_callback *cb;
196 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
197 bool is_static, void *data)
199 struct dsa_slave_dump_ctx *dump = data;
200 u32 portid = NETLINK_CB(dump->cb->skb).portid;
201 u32 seq = dump->cb->nlh->nlmsg_seq;
202 struct nlmsghdr *nlh;
205 if (dump->idx < dump->cb->args[2])
208 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
209 sizeof(*ndm), NLM_F_MULTI);
213 ndm = nlmsg_data(nlh);
214 ndm->ndm_family = AF_BRIDGE;
217 ndm->ndm_flags = NTF_SELF;
219 ndm->ndm_ifindex = dump->dev->ifindex;
220 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
222 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
223 goto nla_put_failure;
225 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
226 goto nla_put_failure;
228 nlmsg_end(dump->skb, nlh);
235 nlmsg_cancel(dump->skb, nlh);
240 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
241 struct net_device *dev, struct net_device *filter_dev,
244 struct dsa_port *dp = dsa_slave_to_port(dev);
245 struct dsa_slave_dump_ctx dump = {
253 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
259 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
261 struct dsa_slave_priv *p = netdev_priv(dev);
262 struct dsa_switch *ds = p->dp->ds;
263 int port = p->dp->index;
265 /* Pass through to switch driver if it supports timestamping */
268 if (ds->ops->port_hwtstamp_get)
269 return ds->ops->port_hwtstamp_get(ds, port, ifr);
272 if (ds->ops->port_hwtstamp_set)
273 return ds->ops->port_hwtstamp_set(ds, port, ifr);
277 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
280 static int dsa_slave_port_attr_set(struct net_device *dev,
281 const struct switchdev_attr *attr,
282 struct switchdev_trans *trans)
284 struct dsa_port *dp = dsa_slave_to_port(dev);
288 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
289 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
291 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
292 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
295 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
296 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
306 static int dsa_slave_port_obj_add(struct net_device *dev,
307 const struct switchdev_obj *obj,
308 struct switchdev_trans *trans)
310 struct dsa_port *dp = dsa_slave_to_port(dev);
313 /* For the prepare phase, ensure the full set of changes is feasable in
314 * one go in order to signal a failure properly. If an operation is not
315 * supported, return -EOPNOTSUPP.
319 case SWITCHDEV_OBJ_ID_PORT_MDB:
320 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
322 case SWITCHDEV_OBJ_ID_HOST_MDB:
323 /* DSA can directly translate this to a normal MDB add,
324 * but on the CPU port.
326 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
329 case SWITCHDEV_OBJ_ID_PORT_VLAN:
330 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
341 static int dsa_slave_port_obj_del(struct net_device *dev,
342 const struct switchdev_obj *obj)
344 struct dsa_port *dp = dsa_slave_to_port(dev);
348 case SWITCHDEV_OBJ_ID_PORT_MDB:
349 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
351 case SWITCHDEV_OBJ_ID_HOST_MDB:
352 /* DSA can directly translate this to a normal MDB add,
353 * but on the CPU port.
355 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
357 case SWITCHDEV_OBJ_ID_PORT_VLAN:
358 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
368 static int dsa_slave_port_attr_get(struct net_device *dev,
369 struct switchdev_attr *attr)
371 struct dsa_port *dp = dsa_slave_to_port(dev);
372 struct dsa_switch *ds = dp->ds;
373 struct dsa_switch_tree *dst = ds->dst;
376 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
377 attr->u.ppid.id_len = sizeof(dst->index);
378 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
380 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
381 attr->u.brport_flags_support = 0;
390 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
393 #ifdef CONFIG_NET_POLL_CONTROLLER
394 struct dsa_slave_priv *p = netdev_priv(dev);
397 netpoll_send_skb(p->netpoll, skb);
404 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
407 struct dsa_switch *ds = p->dp->ds;
408 struct sk_buff *clone;
411 type = ptp_classify_raw(skb);
412 if (type == PTP_CLASS_NONE)
415 if (!ds->ops->port_txtstamp)
418 clone = skb_clone_sk(skb);
422 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
428 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
430 struct dsa_slave_priv *p = netdev_priv(dev);
431 struct pcpu_sw_netstats *s;
432 struct sk_buff *nskb;
434 s = this_cpu_ptr(p->stats64);
435 u64_stats_update_begin(&s->syncp);
437 s->tx_bytes += skb->len;
438 u64_stats_update_end(&s->syncp);
440 /* Identify PTP protocol packets, clone them, and pass them to the
443 dsa_skb_tx_timestamp(p, skb);
445 /* Transmit function may have to reallocate the original SKB,
446 * in which case it must have freed it. Only free it here on error.
448 nskb = p->xmit(skb, dev);
454 /* SKB for netpoll still need to be mangled with the protocol-specific
455 * tag to be successfully transmitted
457 if (unlikely(netpoll_tx_running(dev)))
458 return dsa_slave_netpoll_send_skb(dev, nskb);
460 /* Queue the SKB for transmission on the parent interface, but
461 * do not modify its EtherType
463 nskb->dev = dsa_slave_to_master(dev);
464 dev_queue_xmit(nskb);
469 /* ethtool operations *******************************************************/
471 static void dsa_slave_get_drvinfo(struct net_device *dev,
472 struct ethtool_drvinfo *drvinfo)
474 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
475 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
476 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
479 static int dsa_slave_get_regs_len(struct net_device *dev)
481 struct dsa_port *dp = dsa_slave_to_port(dev);
482 struct dsa_switch *ds = dp->ds;
484 if (ds->ops->get_regs_len)
485 return ds->ops->get_regs_len(ds, dp->index);
491 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
493 struct dsa_port *dp = dsa_slave_to_port(dev);
494 struct dsa_switch *ds = dp->ds;
496 if (ds->ops->get_regs)
497 ds->ops->get_regs(ds, dp->index, regs, _p);
500 static int dsa_slave_nway_reset(struct net_device *dev)
502 struct dsa_port *dp = dsa_slave_to_port(dev);
504 return phylink_ethtool_nway_reset(dp->pl);
507 static int dsa_slave_get_eeprom_len(struct net_device *dev)
509 struct dsa_port *dp = dsa_slave_to_port(dev);
510 struct dsa_switch *ds = dp->ds;
512 if (ds->cd && ds->cd->eeprom_len)
513 return ds->cd->eeprom_len;
515 if (ds->ops->get_eeprom_len)
516 return ds->ops->get_eeprom_len(ds);
521 static int dsa_slave_get_eeprom(struct net_device *dev,
522 struct ethtool_eeprom *eeprom, u8 *data)
524 struct dsa_port *dp = dsa_slave_to_port(dev);
525 struct dsa_switch *ds = dp->ds;
527 if (ds->ops->get_eeprom)
528 return ds->ops->get_eeprom(ds, eeprom, data);
533 static int dsa_slave_set_eeprom(struct net_device *dev,
534 struct ethtool_eeprom *eeprom, u8 *data)
536 struct dsa_port *dp = dsa_slave_to_port(dev);
537 struct dsa_switch *ds = dp->ds;
539 if (ds->ops->set_eeprom)
540 return ds->ops->set_eeprom(ds, eeprom, data);
545 static void dsa_slave_get_strings(struct net_device *dev,
546 uint32_t stringset, uint8_t *data)
548 struct dsa_port *dp = dsa_slave_to_port(dev);
549 struct dsa_switch *ds = dp->ds;
551 if (stringset == ETH_SS_STATS) {
552 int len = ETH_GSTRING_LEN;
554 strncpy(data, "tx_packets", len);
555 strncpy(data + len, "tx_bytes", len);
556 strncpy(data + 2 * len, "rx_packets", len);
557 strncpy(data + 3 * len, "rx_bytes", len);
558 if (ds->ops->get_strings)
559 ds->ops->get_strings(ds, dp->index, stringset,
564 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
565 struct ethtool_stats *stats,
568 struct dsa_port *dp = dsa_slave_to_port(dev);
569 struct dsa_slave_priv *p = netdev_priv(dev);
570 struct dsa_switch *ds = dp->ds;
571 struct pcpu_sw_netstats *s;
575 for_each_possible_cpu(i) {
576 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
578 s = per_cpu_ptr(p->stats64, i);
580 start = u64_stats_fetch_begin_irq(&s->syncp);
581 tx_packets = s->tx_packets;
582 tx_bytes = s->tx_bytes;
583 rx_packets = s->rx_packets;
584 rx_bytes = s->rx_bytes;
585 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
586 data[0] += tx_packets;
588 data[2] += rx_packets;
591 if (ds->ops->get_ethtool_stats)
592 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
595 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
597 struct dsa_port *dp = dsa_slave_to_port(dev);
598 struct dsa_switch *ds = dp->ds;
600 if (sset == ETH_SS_STATS) {
604 if (ds->ops->get_sset_count)
605 count += ds->ops->get_sset_count(ds, dp->index, sset);
613 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
615 struct dsa_port *dp = dsa_slave_to_port(dev);
616 struct dsa_switch *ds = dp->ds;
618 phylink_ethtool_get_wol(dp->pl, w);
620 if (ds->ops->get_wol)
621 ds->ops->get_wol(ds, dp->index, w);
624 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
626 struct dsa_port *dp = dsa_slave_to_port(dev);
627 struct dsa_switch *ds = dp->ds;
628 int ret = -EOPNOTSUPP;
630 phylink_ethtool_set_wol(dp->pl, w);
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 */
645 if (!dev->phydev || !dp->pl)
648 if (!ds->ops->set_mac_eee)
651 ret = ds->ops->set_mac_eee(ds, dp->index, e);
655 return phylink_ethtool_set_eee(dp->pl, e);
658 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
660 struct dsa_port *dp = dsa_slave_to_port(dev);
661 struct dsa_switch *ds = dp->ds;
664 /* Port's PHY and MAC both need to be EEE capable */
665 if (!dev->phydev || !dp->pl)
668 if (!ds->ops->get_mac_eee)
671 ret = ds->ops->get_mac_eee(ds, dp->index, e);
675 return phylink_ethtool_get_eee(dp->pl, e);
678 static int dsa_slave_get_link_ksettings(struct net_device *dev,
679 struct ethtool_link_ksettings *cmd)
681 struct dsa_port *dp = dsa_slave_to_port(dev);
683 return phylink_ethtool_ksettings_get(dp->pl, cmd);
686 static int dsa_slave_set_link_ksettings(struct net_device *dev,
687 const struct ethtool_link_ksettings *cmd)
689 struct dsa_port *dp = dsa_slave_to_port(dev);
691 return phylink_ethtool_ksettings_set(dp->pl, cmd);
694 #ifdef CONFIG_NET_POLL_CONTROLLER
695 static int dsa_slave_netpoll_setup(struct net_device *dev,
696 struct netpoll_info *ni)
698 struct net_device *master = dsa_slave_to_master(dev);
699 struct dsa_slave_priv *p = netdev_priv(dev);
700 struct netpoll *netpoll;
703 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
707 err = __netpoll_setup(netpoll, master);
713 p->netpoll = netpoll;
718 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
720 struct dsa_slave_priv *p = netdev_priv(dev);
721 struct netpoll *netpoll = p->netpoll;
728 __netpoll_free(netpoll);
731 static void dsa_slave_poll_controller(struct net_device *dev)
736 static int dsa_slave_get_phys_port_name(struct net_device *dev,
737 char *name, size_t len)
739 struct dsa_port *dp = dsa_slave_to_port(dev);
741 if (snprintf(name, len, "p%d", dp->index) >= len)
747 static struct dsa_mall_tc_entry *
748 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
750 struct dsa_slave_priv *p = netdev_priv(dev);
751 struct dsa_mall_tc_entry *mall_tc_entry;
753 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
754 if (mall_tc_entry->cookie == cookie)
755 return mall_tc_entry;
760 static int dsa_slave_add_cls_matchall(struct net_device *dev,
761 struct tc_cls_matchall_offload *cls,
764 struct dsa_port *dp = dsa_slave_to_port(dev);
765 struct dsa_slave_priv *p = netdev_priv(dev);
766 struct dsa_mall_tc_entry *mall_tc_entry;
767 __be16 protocol = cls->common.protocol;
768 struct dsa_switch *ds = dp->ds;
769 struct net_device *to_dev;
770 const struct tc_action *a;
771 struct dsa_port *to_dp;
772 int err = -EOPNOTSUPP;
774 if (!ds->ops->port_mirror_add)
777 if (!tcf_exts_has_one_action(cls->exts))
780 a = tcf_exts_first_action(cls->exts);
782 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
783 struct dsa_mall_mirror_tc_entry *mirror;
785 to_dev = tcf_mirred_dev(a);
789 if (!dsa_slave_dev_check(to_dev))
792 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
796 mall_tc_entry->cookie = cls->cookie;
797 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
798 mirror = &mall_tc_entry->mirror;
800 to_dp = dsa_slave_to_port(to_dev);
802 mirror->to_local_port = to_dp->index;
803 mirror->ingress = ingress;
805 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
807 kfree(mall_tc_entry);
811 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
817 static void dsa_slave_del_cls_matchall(struct net_device *dev,
818 struct tc_cls_matchall_offload *cls)
820 struct dsa_port *dp = dsa_slave_to_port(dev);
821 struct dsa_mall_tc_entry *mall_tc_entry;
822 struct dsa_switch *ds = dp->ds;
824 if (!ds->ops->port_mirror_del)
827 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
831 list_del(&mall_tc_entry->list);
833 switch (mall_tc_entry->type) {
834 case DSA_PORT_MALL_MIRROR:
835 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
841 kfree(mall_tc_entry);
844 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
845 struct tc_cls_matchall_offload *cls,
848 if (cls->common.chain_index)
851 switch (cls->command) {
852 case TC_CLSMATCHALL_REPLACE:
853 return dsa_slave_add_cls_matchall(dev, cls, ingress);
854 case TC_CLSMATCHALL_DESTROY:
855 dsa_slave_del_cls_matchall(dev, cls);
862 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
863 void *cb_priv, bool ingress)
865 struct net_device *dev = cb_priv;
867 if (!tc_can_offload(dev))
871 case TC_SETUP_CLSMATCHALL:
872 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
878 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
879 void *type_data, void *cb_priv)
881 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
884 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
885 void *type_data, void *cb_priv)
887 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
890 static int dsa_slave_setup_tc_block(struct net_device *dev,
891 struct tc_block_offload *f)
895 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
896 cb = dsa_slave_setup_tc_block_cb_ig;
897 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
898 cb = dsa_slave_setup_tc_block_cb_eg;
902 switch (f->command) {
904 return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
905 case TC_BLOCK_UNBIND:
906 tcf_block_cb_unregister(f->block, cb, dev);
913 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
918 return dsa_slave_setup_tc_block(dev, type_data);
924 static void dsa_slave_get_stats64(struct net_device *dev,
925 struct rtnl_link_stats64 *stats)
927 struct dsa_slave_priv *p = netdev_priv(dev);
928 struct pcpu_sw_netstats *s;
932 netdev_stats_to_stats64(stats, &dev->stats);
933 for_each_possible_cpu(i) {
934 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
936 s = per_cpu_ptr(p->stats64, i);
938 start = u64_stats_fetch_begin_irq(&s->syncp);
939 tx_packets = s->tx_packets;
940 tx_bytes = s->tx_bytes;
941 rx_packets = s->rx_packets;
942 rx_bytes = s->rx_bytes;
943 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
945 stats->tx_packets += tx_packets;
946 stats->tx_bytes += tx_bytes;
947 stats->rx_packets += rx_packets;
948 stats->rx_bytes += rx_bytes;
952 static int dsa_slave_get_rxnfc(struct net_device *dev,
953 struct ethtool_rxnfc *nfc, u32 *rule_locs)
955 struct dsa_port *dp = dsa_slave_to_port(dev);
956 struct dsa_switch *ds = dp->ds;
958 if (!ds->ops->get_rxnfc)
961 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
964 static int dsa_slave_set_rxnfc(struct net_device *dev,
965 struct ethtool_rxnfc *nfc)
967 struct dsa_port *dp = dsa_slave_to_port(dev);
968 struct dsa_switch *ds = dp->ds;
970 if (!ds->ops->set_rxnfc)
973 return ds->ops->set_rxnfc(ds, dp->index, nfc);
976 static int dsa_slave_get_ts_info(struct net_device *dev,
977 struct ethtool_ts_info *ts)
979 struct dsa_slave_priv *p = netdev_priv(dev);
980 struct dsa_switch *ds = p->dp->ds;
982 if (!ds->ops->get_ts_info)
985 return ds->ops->get_ts_info(ds, p->dp->index, ts);
988 static const struct ethtool_ops dsa_slave_ethtool_ops = {
989 .get_drvinfo = dsa_slave_get_drvinfo,
990 .get_regs_len = dsa_slave_get_regs_len,
991 .get_regs = dsa_slave_get_regs,
992 .nway_reset = dsa_slave_nway_reset,
993 .get_link = ethtool_op_get_link,
994 .get_eeprom_len = dsa_slave_get_eeprom_len,
995 .get_eeprom = dsa_slave_get_eeprom,
996 .set_eeprom = dsa_slave_set_eeprom,
997 .get_strings = dsa_slave_get_strings,
998 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
999 .get_sset_count = dsa_slave_get_sset_count,
1000 .set_wol = dsa_slave_set_wol,
1001 .get_wol = dsa_slave_get_wol,
1002 .set_eee = dsa_slave_set_eee,
1003 .get_eee = dsa_slave_get_eee,
1004 .get_link_ksettings = dsa_slave_get_link_ksettings,
1005 .set_link_ksettings = dsa_slave_set_link_ksettings,
1006 .get_rxnfc = dsa_slave_get_rxnfc,
1007 .set_rxnfc = dsa_slave_set_rxnfc,
1008 .get_ts_info = dsa_slave_get_ts_info,
1011 /* legacy way, bypassing the bridge *****************************************/
1012 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1013 struct net_device *dev,
1014 const unsigned char *addr, u16 vid,
1017 struct dsa_port *dp = dsa_slave_to_port(dev);
1019 return dsa_port_fdb_add(dp, addr, vid);
1022 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1023 struct net_device *dev,
1024 const unsigned char *addr, u16 vid)
1026 struct dsa_port *dp = dsa_slave_to_port(dev);
1028 return dsa_port_fdb_del(dp, addr, vid);
1031 static const struct net_device_ops dsa_slave_netdev_ops = {
1032 .ndo_open = dsa_slave_open,
1033 .ndo_stop = dsa_slave_close,
1034 .ndo_start_xmit = dsa_slave_xmit,
1035 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1036 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1037 .ndo_set_mac_address = dsa_slave_set_mac_address,
1038 .ndo_fdb_add = dsa_legacy_fdb_add,
1039 .ndo_fdb_del = dsa_legacy_fdb_del,
1040 .ndo_fdb_dump = dsa_slave_fdb_dump,
1041 .ndo_do_ioctl = dsa_slave_ioctl,
1042 .ndo_get_iflink = dsa_slave_get_iflink,
1043 #ifdef CONFIG_NET_POLL_CONTROLLER
1044 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1045 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1046 .ndo_poll_controller = dsa_slave_poll_controller,
1048 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1049 .ndo_setup_tc = dsa_slave_setup_tc,
1050 .ndo_get_stats64 = dsa_slave_get_stats64,
1053 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1054 .switchdev_port_attr_get = dsa_slave_port_attr_get,
1055 .switchdev_port_attr_set = dsa_slave_port_attr_set,
1058 static struct device_type dsa_type = {
1062 static void dsa_slave_phylink_validate(struct net_device *dev,
1063 unsigned long *supported,
1064 struct phylink_link_state *state)
1066 struct dsa_port *dp = dsa_slave_to_port(dev);
1067 struct dsa_switch *ds = dp->ds;
1069 if (!ds->ops->phylink_validate)
1072 ds->ops->phylink_validate(ds, dp->index, supported, state);
1075 static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1076 struct phylink_link_state *state)
1078 struct dsa_port *dp = dsa_slave_to_port(dev);
1079 struct dsa_switch *ds = dp->ds;
1081 /* Only called for SGMII and 802.3z */
1082 if (!ds->ops->phylink_mac_link_state)
1085 return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1088 static void dsa_slave_phylink_mac_config(struct net_device *dev,
1090 const struct phylink_link_state *state)
1092 struct dsa_port *dp = dsa_slave_to_port(dev);
1093 struct dsa_switch *ds = dp->ds;
1095 if (!ds->ops->phylink_mac_config)
1098 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1101 static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1103 struct dsa_port *dp = dsa_slave_to_port(dev);
1104 struct dsa_switch *ds = dp->ds;
1106 if (!ds->ops->phylink_mac_an_restart)
1109 ds->ops->phylink_mac_an_restart(ds, dp->index);
1112 static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1114 phy_interface_t interface)
1116 struct dsa_port *dp = dsa_slave_to_port(dev);
1117 struct dsa_switch *ds = dp->ds;
1119 if (!ds->ops->phylink_mac_link_down) {
1120 if (ds->ops->adjust_link && dev->phydev)
1121 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1125 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1128 static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1130 phy_interface_t interface,
1131 struct phy_device *phydev)
1133 struct dsa_port *dp = dsa_slave_to_port(dev);
1134 struct dsa_switch *ds = dp->ds;
1136 if (!ds->ops->phylink_mac_link_up) {
1137 if (ds->ops->adjust_link && dev->phydev)
1138 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1142 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1145 static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1146 .validate = dsa_slave_phylink_validate,
1147 .mac_link_state = dsa_slave_phylink_mac_link_state,
1148 .mac_config = dsa_slave_phylink_mac_config,
1149 .mac_an_restart = dsa_slave_phylink_mac_an_restart,
1150 .mac_link_down = dsa_slave_phylink_mac_link_down,
1151 .mac_link_up = dsa_slave_phylink_mac_link_up,
1154 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1156 const struct dsa_port *dp = dsa_to_port(ds, port);
1158 phylink_mac_change(dp->pl, up);
1160 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1162 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1163 struct phylink_link_state *state)
1165 struct dsa_port *dp = dsa_slave_to_port(dev);
1166 struct dsa_switch *ds = dp->ds;
1168 /* No need to check that this operation is valid, the callback would
1169 * not be called if it was not.
1171 ds->ops->phylink_fixed_state(ds, dp->index, state);
1174 /* slave device setup *******************************************************/
1175 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1177 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1178 struct dsa_switch *ds = dp->ds;
1180 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1181 if (!slave_dev->phydev) {
1182 netdev_err(slave_dev, "no phy at %d\n", addr);
1186 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1189 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1191 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1192 struct device_node *port_dn = dp->dn;
1193 struct dsa_switch *ds = dp->ds;
1197 mode = of_get_phy_mode(port_dn);
1199 mode = PHY_INTERFACE_MODE_NA;
1201 dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1202 &dsa_slave_phylink_mac_ops);
1203 if (IS_ERR(dp->pl)) {
1204 netdev_err(slave_dev,
1205 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1206 return PTR_ERR(dp->pl);
1209 /* Register only if the switch provides such a callback, since this
1210 * callback takes precedence over polling the link GPIO in PHYLINK
1211 * (see phylink_get_fixed_state).
1213 if (ds->ops->phylink_fixed_state)
1214 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1216 if (ds->ops->get_phy_flags)
1217 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1219 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1220 if (ret == -ENODEV) {
1221 /* We could not connect to a designated PHY or SFP, so use the
1222 * switch internal MDIO bus instead
1224 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1226 netdev_err(slave_dev,
1227 "failed to connect to port %d: %d\n",
1229 phylink_destroy(dp->pl);
1237 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1238 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1239 struct netdev_queue *txq,
1242 lockdep_set_class(&txq->_xmit_lock,
1243 &dsa_slave_netdev_xmit_lock_key);
1246 int dsa_slave_suspend(struct net_device *slave_dev)
1248 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1250 if (!netif_running(slave_dev))
1253 netif_device_detach(slave_dev);
1256 phylink_stop(dp->pl);
1262 int dsa_slave_resume(struct net_device *slave_dev)
1264 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1266 if (!netif_running(slave_dev))
1269 netif_device_attach(slave_dev);
1272 phylink_start(dp->pl);
1278 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1280 struct net_device *master = dsa_slave_to_master(dev);
1281 struct dsa_port *dp = dsa_slave_to_port(dev);
1282 struct dsa_notifier_register_info rinfo = {
1283 .switch_number = dp->ds->index,
1284 .port_number = dp->index,
1289 call_dsa_notifiers(val, dev, &rinfo.info);
1292 int dsa_slave_create(struct dsa_port *port)
1294 const struct dsa_port *cpu_dp = port->cpu_dp;
1295 struct net_device *master = cpu_dp->master;
1296 struct dsa_switch *ds = port->ds;
1297 const char *name = port->name;
1298 struct net_device *slave_dev;
1299 struct dsa_slave_priv *p;
1302 if (!ds->num_tx_queues)
1303 ds->num_tx_queues = 1;
1305 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1306 NET_NAME_UNKNOWN, ether_setup,
1307 ds->num_tx_queues, 1);
1308 if (slave_dev == NULL)
1311 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1312 slave_dev->hw_features |= NETIF_F_HW_TC;
1313 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1314 eth_hw_addr_inherit(slave_dev, master);
1315 slave_dev->priv_flags |= IFF_NO_QUEUE;
1316 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1317 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1318 slave_dev->min_mtu = 0;
1319 slave_dev->max_mtu = ETH_MAX_MTU;
1320 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1322 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1325 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1326 slave_dev->dev.of_node = port->dn;
1327 slave_dev->vlan_features = master->vlan_features;
1329 p = netdev_priv(slave_dev);
1330 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1332 free_netdev(slave_dev);
1336 INIT_LIST_HEAD(&p->mall_tc_list);
1337 p->xmit = cpu_dp->tag_ops->xmit;
1338 port->slave = slave_dev;
1340 netif_carrier_off(slave_dev);
1342 ret = dsa_slave_phy_setup(slave_dev);
1344 netdev_err(master, "error %d setting up slave phy\n", ret);
1348 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1350 ret = register_netdev(slave_dev);
1352 netdev_err(master, "error %d registering interface %s\n",
1353 ret, slave_dev->name);
1361 phylink_disconnect_phy(p->dp->pl);
1363 phylink_destroy(p->dp->pl);
1365 free_percpu(p->stats64);
1366 free_netdev(slave_dev);
1371 void dsa_slave_destroy(struct net_device *slave_dev)
1373 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1374 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1376 netif_carrier_off(slave_dev);
1378 phylink_disconnect_phy(dp->pl);
1381 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1382 unregister_netdev(slave_dev);
1383 phylink_destroy(dp->pl);
1384 free_percpu(p->stats64);
1385 free_netdev(slave_dev);
1388 static bool dsa_slave_dev_check(struct net_device *dev)
1390 return dev->netdev_ops == &dsa_slave_netdev_ops;
1393 static int dsa_slave_changeupper(struct net_device *dev,
1394 struct netdev_notifier_changeupper_info *info)
1396 struct dsa_port *dp = dsa_slave_to_port(dev);
1397 int err = NOTIFY_DONE;
1399 if (netif_is_bridge_master(info->upper_dev)) {
1400 if (info->linking) {
1401 err = dsa_port_bridge_join(dp, info->upper_dev);
1402 err = notifier_from_errno(err);
1404 dsa_port_bridge_leave(dp, info->upper_dev);
1412 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1413 unsigned long event, void *ptr)
1415 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1417 if (!dsa_slave_dev_check(dev))
1420 if (event == NETDEV_CHANGEUPPER)
1421 return dsa_slave_changeupper(dev, ptr);
1426 struct dsa_switchdev_event_work {
1427 struct work_struct work;
1428 struct switchdev_notifier_fdb_info fdb_info;
1429 struct net_device *dev;
1430 unsigned long event;
1433 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1435 struct dsa_switchdev_event_work *switchdev_work =
1436 container_of(work, struct dsa_switchdev_event_work, work);
1437 struct net_device *dev = switchdev_work->dev;
1438 struct switchdev_notifier_fdb_info *fdb_info;
1439 struct dsa_port *dp = dsa_slave_to_port(dev);
1443 switch (switchdev_work->event) {
1444 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1445 fdb_info = &switchdev_work->fdb_info;
1446 if (!fdb_info->added_by_user)
1449 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1451 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1454 fdb_info->offloaded = true;
1455 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1459 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1460 fdb_info = &switchdev_work->fdb_info;
1461 if (!fdb_info->added_by_user)
1464 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1466 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1473 kfree(switchdev_work->fdb_info.addr);
1474 kfree(switchdev_work);
1479 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1481 const struct switchdev_notifier_fdb_info *
1484 memcpy(&switchdev_work->fdb_info, fdb_info,
1485 sizeof(switchdev_work->fdb_info));
1486 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1487 if (!switchdev_work->fdb_info.addr)
1489 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1494 /* Called under rcu_read_lock() */
1495 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1496 unsigned long event, void *ptr)
1498 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1499 struct dsa_switchdev_event_work *switchdev_work;
1501 if (!dsa_slave_dev_check(dev))
1504 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1505 if (!switchdev_work)
1508 INIT_WORK(&switchdev_work->work,
1509 dsa_slave_switchdev_event_work);
1510 switchdev_work->dev = dev;
1511 switchdev_work->event = event;
1514 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1515 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1516 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1517 goto err_fdb_work_init;
1521 kfree(switchdev_work);
1525 dsa_schedule_work(&switchdev_work->work);
1529 kfree(switchdev_work);
1534 dsa_slave_switchdev_port_obj_event(unsigned long event,
1535 struct net_device *netdev,
1536 struct switchdev_notifier_port_obj_info *port_obj_info)
1538 int err = -EOPNOTSUPP;
1541 case SWITCHDEV_PORT_OBJ_ADD:
1542 err = dsa_slave_port_obj_add(netdev, port_obj_info->obj,
1543 port_obj_info->trans);
1545 case SWITCHDEV_PORT_OBJ_DEL:
1546 err = dsa_slave_port_obj_del(netdev, port_obj_info->obj);
1550 port_obj_info->handled = true;
1551 return notifier_from_errno(err);
1554 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1555 unsigned long event, void *ptr)
1557 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1559 if (!dsa_slave_dev_check(dev))
1563 case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
1564 case SWITCHDEV_PORT_OBJ_DEL:
1565 return dsa_slave_switchdev_port_obj_event(event, dev, ptr);
1571 static struct notifier_block dsa_slave_nb __read_mostly = {
1572 .notifier_call = dsa_slave_netdevice_event,
1575 static struct notifier_block dsa_slave_switchdev_notifier = {
1576 .notifier_call = dsa_slave_switchdev_event,
1579 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1580 .notifier_call = dsa_slave_switchdev_blocking_event,
1583 int dsa_slave_register_notifier(void)
1585 struct notifier_block *nb;
1588 err = register_netdevice_notifier(&dsa_slave_nb);
1592 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1594 goto err_switchdev_nb;
1596 nb = &dsa_slave_switchdev_blocking_notifier;
1597 err = register_switchdev_blocking_notifier(nb);
1599 goto err_switchdev_blocking_nb;
1603 err_switchdev_blocking_nb:
1604 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1606 unregister_netdevice_notifier(&dsa_slave_nb);
1610 void dsa_slave_unregister_notifier(void)
1612 struct notifier_block *nb;
1615 nb = &dsa_slave_switchdev_blocking_notifier;
1616 err = unregister_switchdev_blocking_notifier(nb);
1618 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1620 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1622 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1624 err = unregister_netdevice_notifier(&dsa_slave_nb);
1626 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);