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/switchdev.h>
22 #include <net/pkt_cls.h>
23 #include <net/tc_act/tc_mirred.h>
24 #include <linux/if_bridge.h>
25 #include <linux/netpoll.h>
28 static bool dsa_slave_dev_check(struct net_device *dev);
30 static int dsa_slave_notify(struct net_device *dev, unsigned long e, void *v)
32 struct dsa_slave_priv *p = netdev_priv(dev);
33 struct raw_notifier_head *nh = &p->dp->ds->dst->nh;
36 err = raw_notifier_call_chain(nh, e, v);
38 return notifier_to_errno(err);
41 /* slave mii_bus handling ***************************************************/
42 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
44 struct dsa_switch *ds = bus->priv;
46 if (ds->phys_mii_mask & (1 << addr))
47 return ds->ops->phy_read(ds, addr, reg);
52 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
54 struct dsa_switch *ds = bus->priv;
56 if (ds->phys_mii_mask & (1 << addr))
57 return ds->ops->phy_write(ds, addr, reg, val);
62 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
64 ds->slave_mii_bus->priv = (void *)ds;
65 ds->slave_mii_bus->name = "dsa slave smi";
66 ds->slave_mii_bus->read = dsa_slave_phy_read;
67 ds->slave_mii_bus->write = dsa_slave_phy_write;
68 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
69 ds->dst->tree, ds->index);
70 ds->slave_mii_bus->parent = ds->dev;
71 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
75 /* slave device handling ****************************************************/
76 static int dsa_slave_get_iflink(const struct net_device *dev)
78 struct dsa_slave_priv *p = netdev_priv(dev);
80 return p->dp->ds->dst->master_netdev->ifindex;
83 static inline bool dsa_port_is_bridged(struct dsa_port *dp)
85 return !!dp->bridge_dev;
88 static void dsa_slave_set_state(struct net_device *dev, u8 state)
90 struct dsa_slave_priv *p = netdev_priv(dev);
91 struct dsa_port *dp = p->dp;
92 struct dsa_switch *ds = dp->ds;
95 if (ds->ops->port_stp_state_set)
96 ds->ops->port_stp_state_set(ds, port, state);
98 if (ds->ops->port_fast_age) {
99 /* Fast age FDB entries or flush appropriate forwarding database
100 * for the given port, if we are moving it from Learning or
101 * Forwarding state, to Disabled or Blocking or Listening state.
104 if ((dp->stp_state == BR_STATE_LEARNING ||
105 dp->stp_state == BR_STATE_FORWARDING) &&
106 (state == BR_STATE_DISABLED ||
107 state == BR_STATE_BLOCKING ||
108 state == BR_STATE_LISTENING))
109 ds->ops->port_fast_age(ds, port);
112 dp->stp_state = state;
115 static int dsa_slave_open(struct net_device *dev)
117 struct dsa_slave_priv *p = netdev_priv(dev);
118 struct net_device *master = p->dp->ds->dst->master_netdev;
119 struct dsa_switch *ds = p->dp->ds;
120 u8 stp_state = dsa_port_is_bridged(p->dp) ?
121 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
124 if (!(master->flags & IFF_UP))
127 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
128 err = dev_uc_add(master, dev->dev_addr);
133 if (dev->flags & IFF_ALLMULTI) {
134 err = dev_set_allmulti(master, 1);
138 if (dev->flags & IFF_PROMISC) {
139 err = dev_set_promiscuity(master, 1);
144 if (ds->ops->port_enable) {
145 err = ds->ops->port_enable(ds, p->dp->index, p->phy);
150 dsa_slave_set_state(dev, stp_state);
158 if (dev->flags & IFF_PROMISC)
159 dev_set_promiscuity(master, -1);
161 if (dev->flags & IFF_ALLMULTI)
162 dev_set_allmulti(master, -1);
164 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
165 dev_uc_del(master, dev->dev_addr);
170 static int dsa_slave_close(struct net_device *dev)
172 struct dsa_slave_priv *p = netdev_priv(dev);
173 struct net_device *master = p->dp->ds->dst->master_netdev;
174 struct dsa_switch *ds = p->dp->ds;
179 dev_mc_unsync(master, dev);
180 dev_uc_unsync(master, dev);
181 if (dev->flags & IFF_ALLMULTI)
182 dev_set_allmulti(master, -1);
183 if (dev->flags & IFF_PROMISC)
184 dev_set_promiscuity(master, -1);
186 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
187 dev_uc_del(master, dev->dev_addr);
189 if (ds->ops->port_disable)
190 ds->ops->port_disable(ds, p->dp->index, p->phy);
192 dsa_slave_set_state(dev, BR_STATE_DISABLED);
197 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
199 struct dsa_slave_priv *p = netdev_priv(dev);
200 struct net_device *master = p->dp->ds->dst->master_netdev;
202 if (change & IFF_ALLMULTI)
203 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
204 if (change & IFF_PROMISC)
205 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
208 static void dsa_slave_set_rx_mode(struct net_device *dev)
210 struct dsa_slave_priv *p = netdev_priv(dev);
211 struct net_device *master = p->dp->ds->dst->master_netdev;
213 dev_mc_sync(master, dev);
214 dev_uc_sync(master, dev);
217 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
219 struct dsa_slave_priv *p = netdev_priv(dev);
220 struct net_device *master = p->dp->ds->dst->master_netdev;
221 struct sockaddr *addr = a;
224 if (!is_valid_ether_addr(addr->sa_data))
225 return -EADDRNOTAVAIL;
227 if (!(dev->flags & IFF_UP))
230 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
231 err = dev_uc_add(master, addr->sa_data);
236 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
237 dev_uc_del(master, dev->dev_addr);
240 ether_addr_copy(dev->dev_addr, addr->sa_data);
245 static int dsa_slave_port_vlan_add(struct net_device *dev,
246 const struct switchdev_obj_port_vlan *vlan,
247 struct switchdev_trans *trans)
249 struct dsa_slave_priv *p = netdev_priv(dev);
250 struct dsa_port *dp = p->dp;
251 struct dsa_switch *ds = dp->ds;
253 if (switchdev_trans_ph_prepare(trans)) {
254 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
257 return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
260 ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
265 static int dsa_slave_port_vlan_del(struct net_device *dev,
266 const struct switchdev_obj_port_vlan *vlan)
268 struct dsa_slave_priv *p = netdev_priv(dev);
269 struct dsa_switch *ds = p->dp->ds;
271 if (!ds->ops->port_vlan_del)
274 return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
277 static int dsa_slave_port_vlan_dump(struct net_device *dev,
278 struct switchdev_obj_port_vlan *vlan,
279 switchdev_obj_dump_cb_t *cb)
281 struct dsa_slave_priv *p = netdev_priv(dev);
282 struct dsa_switch *ds = p->dp->ds;
284 if (ds->ops->port_vlan_dump)
285 return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
290 static int dsa_slave_port_fdb_add(struct net_device *dev,
291 const struct switchdev_obj_port_fdb *fdb,
292 struct switchdev_trans *trans)
294 struct dsa_slave_priv *p = netdev_priv(dev);
295 struct dsa_switch *ds = p->dp->ds;
297 if (switchdev_trans_ph_prepare(trans)) {
298 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
301 return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans);
304 ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans);
309 static int dsa_slave_port_fdb_del(struct net_device *dev,
310 const struct switchdev_obj_port_fdb *fdb)
312 struct dsa_slave_priv *p = netdev_priv(dev);
313 struct dsa_switch *ds = p->dp->ds;
314 int ret = -EOPNOTSUPP;
316 if (ds->ops->port_fdb_del)
317 ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb);
322 static int dsa_slave_port_fdb_dump(struct net_device *dev,
323 struct switchdev_obj_port_fdb *fdb,
324 switchdev_obj_dump_cb_t *cb)
326 struct dsa_slave_priv *p = netdev_priv(dev);
327 struct dsa_switch *ds = p->dp->ds;
329 if (ds->ops->port_fdb_dump)
330 return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb);
335 static int dsa_slave_port_mdb_add(struct net_device *dev,
336 const struct switchdev_obj_port_mdb *mdb,
337 struct switchdev_trans *trans)
339 struct dsa_slave_priv *p = netdev_priv(dev);
340 struct dsa_switch *ds = p->dp->ds;
342 if (switchdev_trans_ph_prepare(trans)) {
343 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
346 return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
349 ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
354 static int dsa_slave_port_mdb_del(struct net_device *dev,
355 const struct switchdev_obj_port_mdb *mdb)
357 struct dsa_slave_priv *p = netdev_priv(dev);
358 struct dsa_switch *ds = p->dp->ds;
360 if (ds->ops->port_mdb_del)
361 return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
366 static int dsa_slave_port_mdb_dump(struct net_device *dev,
367 struct switchdev_obj_port_mdb *mdb,
368 switchdev_obj_dump_cb_t *cb)
370 struct dsa_slave_priv *p = netdev_priv(dev);
371 struct dsa_switch *ds = p->dp->ds;
373 if (ds->ops->port_mdb_dump)
374 return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
379 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
381 struct dsa_slave_priv *p = netdev_priv(dev);
384 return phy_mii_ioctl(p->phy, ifr, cmd);
389 static int dsa_slave_stp_state_set(struct net_device *dev,
390 const struct switchdev_attr *attr,
391 struct switchdev_trans *trans)
393 struct dsa_slave_priv *p = netdev_priv(dev);
394 struct dsa_switch *ds = p->dp->ds;
396 if (switchdev_trans_ph_prepare(trans))
397 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
399 dsa_slave_set_state(dev, attr->u.stp_state);
404 static int dsa_slave_vlan_filtering(struct net_device *dev,
405 const struct switchdev_attr *attr,
406 struct switchdev_trans *trans)
408 struct dsa_slave_priv *p = netdev_priv(dev);
409 struct dsa_switch *ds = p->dp->ds;
411 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
412 if (switchdev_trans_ph_prepare(trans))
415 if (ds->ops->port_vlan_filtering)
416 return ds->ops->port_vlan_filtering(ds, p->dp->index,
417 attr->u.vlan_filtering);
422 static int dsa_fastest_ageing_time(struct dsa_switch *ds,
423 unsigned int ageing_time)
427 for (i = 0; i < ds->num_ports; ++i) {
428 struct dsa_port *dp = &ds->ports[i];
430 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
431 ageing_time = dp->ageing_time;
437 static int dsa_slave_ageing_time(struct net_device *dev,
438 const struct switchdev_attr *attr,
439 struct switchdev_trans *trans)
441 struct dsa_slave_priv *p = netdev_priv(dev);
442 struct dsa_switch *ds = p->dp->ds;
443 unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
444 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
446 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
447 if (switchdev_trans_ph_prepare(trans))
450 /* Keep the fastest ageing time in case of multiple bridges */
451 p->dp->ageing_time = ageing_time;
452 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
454 if (ds->ops->set_ageing_time)
455 return ds->ops->set_ageing_time(ds, ageing_time);
460 static int dsa_slave_port_attr_set(struct net_device *dev,
461 const struct switchdev_attr *attr,
462 struct switchdev_trans *trans)
467 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
468 ret = dsa_slave_stp_state_set(dev, attr, trans);
470 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
471 ret = dsa_slave_vlan_filtering(dev, attr, trans);
473 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
474 ret = dsa_slave_ageing_time(dev, attr, trans);
484 static int dsa_slave_port_obj_add(struct net_device *dev,
485 const struct switchdev_obj *obj,
486 struct switchdev_trans *trans)
490 /* For the prepare phase, ensure the full set of changes is feasable in
491 * one go in order to signal a failure properly. If an operation is not
492 * supported, return -EOPNOTSUPP.
496 case SWITCHDEV_OBJ_ID_PORT_FDB:
497 err = dsa_slave_port_fdb_add(dev,
498 SWITCHDEV_OBJ_PORT_FDB(obj),
501 case SWITCHDEV_OBJ_ID_PORT_MDB:
502 err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
505 case SWITCHDEV_OBJ_ID_PORT_VLAN:
506 err = dsa_slave_port_vlan_add(dev,
507 SWITCHDEV_OBJ_PORT_VLAN(obj),
518 static int dsa_slave_port_obj_del(struct net_device *dev,
519 const struct switchdev_obj *obj)
524 case SWITCHDEV_OBJ_ID_PORT_FDB:
525 err = dsa_slave_port_fdb_del(dev,
526 SWITCHDEV_OBJ_PORT_FDB(obj));
528 case SWITCHDEV_OBJ_ID_PORT_MDB:
529 err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
531 case SWITCHDEV_OBJ_ID_PORT_VLAN:
532 err = dsa_slave_port_vlan_del(dev,
533 SWITCHDEV_OBJ_PORT_VLAN(obj));
543 static int dsa_slave_port_obj_dump(struct net_device *dev,
544 struct switchdev_obj *obj,
545 switchdev_obj_dump_cb_t *cb)
550 case SWITCHDEV_OBJ_ID_PORT_FDB:
551 err = dsa_slave_port_fdb_dump(dev,
552 SWITCHDEV_OBJ_PORT_FDB(obj),
555 case SWITCHDEV_OBJ_ID_PORT_MDB:
556 err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
559 case SWITCHDEV_OBJ_ID_PORT_VLAN:
560 err = dsa_slave_port_vlan_dump(dev,
561 SWITCHDEV_OBJ_PORT_VLAN(obj),
572 static int dsa_slave_bridge_port_join(struct net_device *dev,
573 struct net_device *br)
575 struct dsa_slave_priv *p = netdev_priv(dev);
576 struct dsa_notifier_bridge_info info = {
577 .sw_index = p->dp->ds->index,
578 .port = p->dp->index,
583 /* Here the port is already bridged. Reflect the current configuration
584 * so that drivers can program their chips accordingly.
586 p->dp->bridge_dev = br;
588 err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_JOIN, &info);
590 /* The bridging is rolled back on error */
592 p->dp->bridge_dev = NULL;
597 static void dsa_slave_bridge_port_leave(struct net_device *dev,
598 struct net_device *br)
600 struct dsa_slave_priv *p = netdev_priv(dev);
601 struct dsa_notifier_bridge_info info = {
602 .sw_index = p->dp->ds->index,
603 .port = p->dp->index,
608 /* Here the port is already unbridged. Reflect the current configuration
609 * so that drivers can program their chips accordingly.
611 p->dp->bridge_dev = NULL;
613 err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
615 netdev_err(dev, "failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
617 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
618 * so allow it to be in BR_STATE_FORWARDING to be kept functional
620 dsa_slave_set_state(dev, BR_STATE_FORWARDING);
623 static int dsa_slave_port_attr_get(struct net_device *dev,
624 struct switchdev_attr *attr)
626 struct dsa_slave_priv *p = netdev_priv(dev);
627 struct dsa_switch *ds = p->dp->ds;
630 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
631 attr->u.ppid.id_len = sizeof(ds->index);
632 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
641 static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
644 #ifdef CONFIG_NET_POLL_CONTROLLER
646 netpoll_send_skb(p->netpoll, skb);
653 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
655 struct dsa_slave_priv *p = netdev_priv(dev);
656 struct sk_buff *nskb;
658 dev->stats.tx_packets++;
659 dev->stats.tx_bytes += skb->len;
661 /* Transmit function may have to reallocate the original SKB */
662 nskb = p->xmit(skb, dev);
666 /* SKB for netpoll still need to be mangled with the protocol-specific
667 * tag to be successfully transmitted
669 if (unlikely(netpoll_tx_running(dev)))
670 return dsa_netpoll_send_skb(p, nskb);
672 /* Queue the SKB for transmission on the parent interface, but
673 * do not modify its EtherType
675 nskb->dev = p->dp->ds->dst->master_netdev;
676 dev_queue_xmit(nskb);
681 /* ethtool operations *******************************************************/
683 dsa_slave_get_link_ksettings(struct net_device *dev,
684 struct ethtool_link_ksettings *cmd)
686 struct dsa_slave_priv *p = netdev_priv(dev);
687 int err = -EOPNOTSUPP;
690 err = phy_ethtool_ksettings_get(p->phy, cmd);
696 dsa_slave_set_link_ksettings(struct net_device *dev,
697 const struct ethtool_link_ksettings *cmd)
699 struct dsa_slave_priv *p = netdev_priv(dev);
702 return phy_ethtool_ksettings_set(p->phy, cmd);
707 static void dsa_slave_get_drvinfo(struct net_device *dev,
708 struct ethtool_drvinfo *drvinfo)
710 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
711 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
712 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
715 static int dsa_slave_get_regs_len(struct net_device *dev)
717 struct dsa_slave_priv *p = netdev_priv(dev);
718 struct dsa_switch *ds = p->dp->ds;
720 if (ds->ops->get_regs_len)
721 return ds->ops->get_regs_len(ds, p->dp->index);
727 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
729 struct dsa_slave_priv *p = netdev_priv(dev);
730 struct dsa_switch *ds = p->dp->ds;
732 if (ds->ops->get_regs)
733 ds->ops->get_regs(ds, p->dp->index, regs, _p);
736 static int dsa_slave_nway_reset(struct net_device *dev)
738 struct dsa_slave_priv *p = netdev_priv(dev);
741 return genphy_restart_aneg(p->phy);
746 static u32 dsa_slave_get_link(struct net_device *dev)
748 struct dsa_slave_priv *p = netdev_priv(dev);
750 if (p->phy != NULL) {
751 genphy_update_link(p->phy);
758 static int dsa_slave_get_eeprom_len(struct net_device *dev)
760 struct dsa_slave_priv *p = netdev_priv(dev);
761 struct dsa_switch *ds = p->dp->ds;
763 if (ds->cd && ds->cd->eeprom_len)
764 return ds->cd->eeprom_len;
766 if (ds->ops->get_eeprom_len)
767 return ds->ops->get_eeprom_len(ds);
772 static int dsa_slave_get_eeprom(struct net_device *dev,
773 struct ethtool_eeprom *eeprom, u8 *data)
775 struct dsa_slave_priv *p = netdev_priv(dev);
776 struct dsa_switch *ds = p->dp->ds;
778 if (ds->ops->get_eeprom)
779 return ds->ops->get_eeprom(ds, eeprom, data);
784 static int dsa_slave_set_eeprom(struct net_device *dev,
785 struct ethtool_eeprom *eeprom, u8 *data)
787 struct dsa_slave_priv *p = netdev_priv(dev);
788 struct dsa_switch *ds = p->dp->ds;
790 if (ds->ops->set_eeprom)
791 return ds->ops->set_eeprom(ds, eeprom, data);
796 static void dsa_slave_get_strings(struct net_device *dev,
797 uint32_t stringset, uint8_t *data)
799 struct dsa_slave_priv *p = netdev_priv(dev);
800 struct dsa_switch *ds = p->dp->ds;
802 if (stringset == ETH_SS_STATS) {
803 int len = ETH_GSTRING_LEN;
805 strncpy(data, "tx_packets", len);
806 strncpy(data + len, "tx_bytes", len);
807 strncpy(data + 2 * len, "rx_packets", len);
808 strncpy(data + 3 * len, "rx_bytes", len);
809 if (ds->ops->get_strings)
810 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
814 static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
815 struct ethtool_stats *stats,
818 struct dsa_switch_tree *dst = dev->dsa_ptr;
819 struct dsa_switch *ds = dst->cpu_switch;
820 s8 cpu_port = dst->cpu_port;
823 if (dst->master_ethtool_ops.get_sset_count) {
824 count = dst->master_ethtool_ops.get_sset_count(dev,
826 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
829 if (ds->ops->get_ethtool_stats)
830 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
833 static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
835 struct dsa_switch_tree *dst = dev->dsa_ptr;
836 struct dsa_switch *ds = dst->cpu_switch;
839 if (dst->master_ethtool_ops.get_sset_count)
840 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
842 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
843 count += ds->ops->get_sset_count(ds);
848 static void dsa_cpu_port_get_strings(struct net_device *dev,
849 uint32_t stringset, uint8_t *data)
851 struct dsa_switch_tree *dst = dev->dsa_ptr;
852 struct dsa_switch *ds = dst->cpu_switch;
853 s8 cpu_port = dst->cpu_port;
854 int len = ETH_GSTRING_LEN;
855 int mcount = 0, count;
860 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
861 /* We do not want to be NULL-terminated, since this is a prefix */
862 pfx[sizeof(pfx) - 1] = '_';
864 if (dst->master_ethtool_ops.get_sset_count) {
865 mcount = dst->master_ethtool_ops.get_sset_count(dev,
867 dst->master_ethtool_ops.get_strings(dev, stringset, data);
870 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
871 ndata = data + mcount * len;
872 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
873 * the output after to prepend our CPU port prefix we
874 * constructed earlier
876 ds->ops->get_strings(ds, cpu_port, ndata);
877 count = ds->ops->get_sset_count(ds);
878 for (i = 0; i < count; i++) {
879 memmove(ndata + (i * len + sizeof(pfx)),
880 ndata + i * len, len - sizeof(pfx));
881 memcpy(ndata + i * len, pfx, sizeof(pfx));
886 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
887 struct ethtool_stats *stats,
890 struct dsa_slave_priv *p = netdev_priv(dev);
891 struct dsa_switch *ds = p->dp->ds;
893 data[0] = dev->stats.tx_packets;
894 data[1] = dev->stats.tx_bytes;
895 data[2] = dev->stats.rx_packets;
896 data[3] = dev->stats.rx_bytes;
897 if (ds->ops->get_ethtool_stats)
898 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
901 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
903 struct dsa_slave_priv *p = netdev_priv(dev);
904 struct dsa_switch *ds = p->dp->ds;
906 if (sset == ETH_SS_STATS) {
910 if (ds->ops->get_sset_count)
911 count += ds->ops->get_sset_count(ds);
919 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
921 struct dsa_slave_priv *p = netdev_priv(dev);
922 struct dsa_switch *ds = p->dp->ds;
924 if (ds->ops->get_wol)
925 ds->ops->get_wol(ds, p->dp->index, w);
928 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
930 struct dsa_slave_priv *p = netdev_priv(dev);
931 struct dsa_switch *ds = p->dp->ds;
932 int ret = -EOPNOTSUPP;
934 if (ds->ops->set_wol)
935 ret = ds->ops->set_wol(ds, p->dp->index, w);
940 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
942 struct dsa_slave_priv *p = netdev_priv(dev);
943 struct dsa_switch *ds = p->dp->ds;
946 if (!ds->ops->set_eee)
949 ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
954 ret = phy_ethtool_set_eee(p->phy, e);
959 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
961 struct dsa_slave_priv *p = netdev_priv(dev);
962 struct dsa_switch *ds = p->dp->ds;
965 if (!ds->ops->get_eee)
968 ret = ds->ops->get_eee(ds, p->dp->index, e);
973 ret = phy_ethtool_get_eee(p->phy, e);
978 #ifdef CONFIG_NET_POLL_CONTROLLER
979 static int dsa_slave_netpoll_setup(struct net_device *dev,
980 struct netpoll_info *ni)
982 struct dsa_slave_priv *p = netdev_priv(dev);
983 struct dsa_switch *ds = p->dp->ds;
984 struct net_device *master = ds->dst->master_netdev;
985 struct netpoll *netpoll;
988 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
992 err = __netpoll_setup(netpoll, master);
998 p->netpoll = netpoll;
1003 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1005 struct dsa_slave_priv *p = netdev_priv(dev);
1006 struct netpoll *netpoll = p->netpoll;
1013 __netpoll_free_async(netpoll);
1016 static void dsa_slave_poll_controller(struct net_device *dev)
1021 static int dsa_slave_get_phys_port_name(struct net_device *dev,
1022 char *name, size_t len)
1024 struct dsa_slave_priv *p = netdev_priv(dev);
1026 if (snprintf(name, len, "p%d", p->dp->index) >= len)
1032 static struct dsa_mall_tc_entry *
1033 dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1034 unsigned long cookie)
1036 struct dsa_mall_tc_entry *mall_tc_entry;
1038 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1039 if (mall_tc_entry->cookie == cookie)
1040 return mall_tc_entry;
1045 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1047 struct tc_cls_matchall_offload *cls,
1050 struct dsa_slave_priv *p = netdev_priv(dev);
1051 struct dsa_mall_tc_entry *mall_tc_entry;
1052 struct dsa_switch *ds = p->dp->ds;
1053 struct net *net = dev_net(dev);
1054 struct dsa_slave_priv *to_p;
1055 struct net_device *to_dev;
1056 const struct tc_action *a;
1057 int err = -EOPNOTSUPP;
1061 if (!ds->ops->port_mirror_add)
1064 if (!tc_single_action(cls->exts))
1067 tcf_exts_to_list(cls->exts, &actions);
1068 a = list_first_entry(&actions, struct tc_action, list);
1070 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1071 struct dsa_mall_mirror_tc_entry *mirror;
1073 ifindex = tcf_mirred_ifindex(a);
1074 to_dev = __dev_get_by_index(net, ifindex);
1078 if (!dsa_slave_dev_check(to_dev))
1081 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1085 mall_tc_entry->cookie = cls->cookie;
1086 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1087 mirror = &mall_tc_entry->mirror;
1089 to_p = netdev_priv(to_dev);
1091 mirror->to_local_port = to_p->dp->index;
1092 mirror->ingress = ingress;
1094 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1097 kfree(mall_tc_entry);
1101 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1107 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1108 struct tc_cls_matchall_offload *cls)
1110 struct dsa_slave_priv *p = netdev_priv(dev);
1111 struct dsa_mall_tc_entry *mall_tc_entry;
1112 struct dsa_switch *ds = p->dp->ds;
1114 if (!ds->ops->port_mirror_del)
1117 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1121 list_del(&mall_tc_entry->list);
1123 switch (mall_tc_entry->type) {
1124 case DSA_PORT_MALL_MIRROR:
1125 ds->ops->port_mirror_del(ds, p->dp->index,
1126 &mall_tc_entry->mirror);
1132 kfree(mall_tc_entry);
1135 static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1136 __be16 protocol, struct tc_to_netdev *tc)
1138 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1139 int ret = -EOPNOTSUPP;
1142 case TC_SETUP_MATCHALL:
1143 switch (tc->cls_mall->command) {
1144 case TC_CLSMATCHALL_REPLACE:
1145 return dsa_slave_add_cls_matchall(dev, protocol,
1148 case TC_CLSMATCHALL_DESTROY:
1149 dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1159 void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1161 ops->get_sset_count = dsa_cpu_port_get_sset_count;
1162 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1163 ops->get_strings = dsa_cpu_port_get_strings;
1166 static int dsa_slave_get_rxnfc(struct net_device *dev,
1167 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1169 struct dsa_slave_priv *p = netdev_priv(dev);
1170 struct dsa_switch *ds = p->dp->ds;
1172 if (!ds->ops->get_rxnfc)
1175 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1178 static int dsa_slave_set_rxnfc(struct net_device *dev,
1179 struct ethtool_rxnfc *nfc)
1181 struct dsa_slave_priv *p = netdev_priv(dev);
1182 struct dsa_switch *ds = p->dp->ds;
1184 if (!ds->ops->set_rxnfc)
1187 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1190 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1191 .get_drvinfo = dsa_slave_get_drvinfo,
1192 .get_regs_len = dsa_slave_get_regs_len,
1193 .get_regs = dsa_slave_get_regs,
1194 .nway_reset = dsa_slave_nway_reset,
1195 .get_link = dsa_slave_get_link,
1196 .get_eeprom_len = dsa_slave_get_eeprom_len,
1197 .get_eeprom = dsa_slave_get_eeprom,
1198 .set_eeprom = dsa_slave_set_eeprom,
1199 .get_strings = dsa_slave_get_strings,
1200 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1201 .get_sset_count = dsa_slave_get_sset_count,
1202 .set_wol = dsa_slave_set_wol,
1203 .get_wol = dsa_slave_get_wol,
1204 .set_eee = dsa_slave_set_eee,
1205 .get_eee = dsa_slave_get_eee,
1206 .get_link_ksettings = dsa_slave_get_link_ksettings,
1207 .set_link_ksettings = dsa_slave_set_link_ksettings,
1208 .get_rxnfc = dsa_slave_get_rxnfc,
1209 .set_rxnfc = dsa_slave_set_rxnfc,
1212 static const struct net_device_ops dsa_slave_netdev_ops = {
1213 .ndo_open = dsa_slave_open,
1214 .ndo_stop = dsa_slave_close,
1215 .ndo_start_xmit = dsa_slave_xmit,
1216 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1217 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1218 .ndo_set_mac_address = dsa_slave_set_mac_address,
1219 .ndo_fdb_add = switchdev_port_fdb_add,
1220 .ndo_fdb_del = switchdev_port_fdb_del,
1221 .ndo_fdb_dump = switchdev_port_fdb_dump,
1222 .ndo_do_ioctl = dsa_slave_ioctl,
1223 .ndo_get_iflink = dsa_slave_get_iflink,
1224 #ifdef CONFIG_NET_POLL_CONTROLLER
1225 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1226 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1227 .ndo_poll_controller = dsa_slave_poll_controller,
1229 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1230 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1231 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
1232 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1233 .ndo_setup_tc = dsa_slave_setup_tc,
1236 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1237 .switchdev_port_attr_get = dsa_slave_port_attr_get,
1238 .switchdev_port_attr_set = dsa_slave_port_attr_set,
1239 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1240 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1241 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
1244 static struct device_type dsa_type = {
1248 static void dsa_slave_adjust_link(struct net_device *dev)
1250 struct dsa_slave_priv *p = netdev_priv(dev);
1251 struct dsa_switch *ds = p->dp->ds;
1252 unsigned int status_changed = 0;
1254 if (p->old_link != p->phy->link) {
1256 p->old_link = p->phy->link;
1259 if (p->old_duplex != p->phy->duplex) {
1261 p->old_duplex = p->phy->duplex;
1264 if (p->old_pause != p->phy->pause) {
1266 p->old_pause = p->phy->pause;
1269 if (ds->ops->adjust_link && status_changed)
1270 ds->ops->adjust_link(ds, p->dp->index, p->phy);
1273 phy_print_status(p->phy);
1276 static int dsa_slave_fixed_link_update(struct net_device *dev,
1277 struct fixed_phy_status *status)
1279 struct dsa_slave_priv *p;
1280 struct dsa_switch *ds;
1283 p = netdev_priv(dev);
1285 if (ds->ops->fixed_link_update)
1286 ds->ops->fixed_link_update(ds, p->dp->index, status);
1292 /* slave device setup *******************************************************/
1293 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1294 struct net_device *slave_dev,
1297 struct dsa_switch *ds = p->dp->ds;
1299 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1301 netdev_err(slave_dev, "no phy at %d\n", addr);
1305 /* Use already configured phy mode */
1306 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1307 p->phy_interface = p->phy->interface;
1308 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1312 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
1313 struct net_device *slave_dev)
1315 struct dsa_switch *ds = p->dp->ds;
1316 struct device_node *phy_dn, *port_dn;
1317 bool phy_is_fixed = false;
1321 port_dn = p->dp->dn;
1322 mode = of_get_phy_mode(port_dn);
1324 mode = PHY_INTERFACE_MODE_NA;
1325 p->phy_interface = mode;
1327 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1328 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1329 /* In the case of a fixed PHY, the DT node associated
1330 * to the fixed PHY is the Port DT node
1332 ret = of_phy_register_fixed_link(port_dn);
1334 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1337 phy_is_fixed = true;
1338 phy_dn = of_node_get(port_dn);
1341 if (ds->ops->get_phy_flags)
1342 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
1345 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1347 /* If this PHY address is part of phys_mii_mask, which means
1348 * that we need to divert reads and writes to/from it, then we
1349 * want to bind this device using the slave MII bus created by
1350 * DSA to make that happen.
1352 if (!phy_is_fixed && phy_id >= 0 &&
1353 (ds->phys_mii_mask & (1 << phy_id))) {
1354 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1356 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1357 of_node_put(phy_dn);
1361 p->phy = of_phy_connect(slave_dev, phy_dn,
1362 dsa_slave_adjust_link,
1367 of_node_put(phy_dn);
1370 if (p->phy && phy_is_fixed)
1371 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1373 /* We could not connect to a designated PHY, so use the switch internal
1377 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1379 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1382 of_phy_deregister_fixed_link(port_dn);
1387 phy_attached_info(p->phy);
1392 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1393 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1394 struct netdev_queue *txq,
1397 lockdep_set_class(&txq->_xmit_lock,
1398 &dsa_slave_netdev_xmit_lock_key);
1401 int dsa_slave_suspend(struct net_device *slave_dev)
1403 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1405 netif_device_detach(slave_dev);
1412 phy_suspend(p->phy);
1418 int dsa_slave_resume(struct net_device *slave_dev)
1420 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1422 netif_device_attach(slave_dev);
1432 int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1433 int port, const char *name)
1435 struct dsa_switch_tree *dst = ds->dst;
1436 struct net_device *master;
1437 struct net_device *slave_dev;
1438 struct dsa_slave_priv *p;
1441 master = ds->dst->master_netdev;
1442 if (ds->master_netdev)
1443 master = ds->master_netdev;
1445 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1446 NET_NAME_UNKNOWN, ether_setup);
1447 if (slave_dev == NULL)
1450 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1451 slave_dev->hw_features |= NETIF_F_HW_TC;
1452 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1453 eth_hw_addr_inherit(slave_dev, master);
1454 slave_dev->priv_flags |= IFF_NO_QUEUE;
1455 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1456 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1457 slave_dev->min_mtu = 0;
1458 slave_dev->max_mtu = ETH_MAX_MTU;
1459 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1461 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1464 SET_NETDEV_DEV(slave_dev, parent);
1465 slave_dev->dev.of_node = ds->ports[port].dn;
1466 slave_dev->vlan_features = master->vlan_features;
1468 p = netdev_priv(slave_dev);
1469 p->dp = &ds->ports[port];
1470 INIT_LIST_HEAD(&p->mall_tc_list);
1471 p->xmit = dst->tag_ops->xmit;
1477 ds->ports[port].netdev = slave_dev;
1478 ret = register_netdev(slave_dev);
1480 netdev_err(master, "error %d registering interface %s\n",
1481 ret, slave_dev->name);
1482 ds->ports[port].netdev = NULL;
1483 free_netdev(slave_dev);
1487 netif_carrier_off(slave_dev);
1489 ret = dsa_slave_phy_setup(p, slave_dev);
1491 netdev_err(master, "error %d setting up slave phy\n", ret);
1492 unregister_netdev(slave_dev);
1493 free_netdev(slave_dev);
1500 void dsa_slave_destroy(struct net_device *slave_dev)
1502 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1503 struct device_node *port_dn;
1505 port_dn = p->dp->dn;
1507 netif_carrier_off(slave_dev);
1509 phy_disconnect(p->phy);
1511 if (of_phy_is_fixed_link(port_dn))
1512 of_phy_deregister_fixed_link(port_dn);
1514 unregister_netdev(slave_dev);
1515 free_netdev(slave_dev);
1518 static bool dsa_slave_dev_check(struct net_device *dev)
1520 return dev->netdev_ops == &dsa_slave_netdev_ops;
1523 static int dsa_slave_changeupper(struct net_device *dev,
1524 struct netdev_notifier_changeupper_info *info)
1526 int err = NOTIFY_DONE;
1528 if (netif_is_bridge_master(info->upper_dev)) {
1529 if (info->linking) {
1530 err = dsa_slave_bridge_port_join(dev, info->upper_dev);
1531 err = notifier_from_errno(err);
1533 dsa_slave_bridge_port_leave(dev, info->upper_dev);
1541 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1542 unsigned long event, void *ptr)
1544 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1546 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1549 if (event == NETDEV_CHANGEUPPER)
1550 return dsa_slave_changeupper(dev, ptr);
1555 static struct notifier_block dsa_slave_nb __read_mostly = {
1556 .notifier_call = dsa_slave_netdevice_event,
1559 int dsa_slave_register_notifier(void)
1561 return register_netdevice_notifier(&dsa_slave_nb);
1564 void dsa_slave_unregister_notifier(void)
1568 err = unregister_netdevice_notifier(&dsa_slave_nb);
1570 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);