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
->index
, ds
->index
);
59 ds
->slave_mii_bus
->parent
= ds
->dev
;
60 ds
->slave_mii_bus
->phy_mask
= ~ds
->phys_mii_mask
;
64 /* slave device handling ****************************************************/
65 static int dsa_slave_get_iflink(const struct net_device
*dev
)
67 return dsa_slave_to_master(dev
)->ifindex
;
70 static int dsa_slave_open(struct net_device
*dev
)
72 struct net_device
*master
= dsa_slave_to_master(dev
);
73 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
76 if (!(master
->flags
& IFF_UP
))
79 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
)) {
80 err
= dev_uc_add(master
, dev
->dev_addr
);
85 if (dev
->flags
& IFF_ALLMULTI
) {
86 err
= dev_set_allmulti(master
, 1);
90 if (dev
->flags
& IFF_PROMISC
) {
91 err
= dev_set_promiscuity(master
, 1);
96 err
= dsa_port_enable(dp
, dev
->phydev
);
101 phy_start(dev
->phydev
);
106 if (dev
->flags
& IFF_PROMISC
)
107 dev_set_promiscuity(master
, -1);
109 if (dev
->flags
& IFF_ALLMULTI
)
110 dev_set_allmulti(master
, -1);
112 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
113 dev_uc_del(master
, dev
->dev_addr
);
118 static int dsa_slave_close(struct net_device
*dev
)
120 struct net_device
*master
= dsa_slave_to_master(dev
);
121 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
124 phy_stop(dev
->phydev
);
126 dsa_port_disable(dp
, dev
->phydev
);
128 dev_mc_unsync(master
, dev
);
129 dev_uc_unsync(master
, dev
);
130 if (dev
->flags
& IFF_ALLMULTI
)
131 dev_set_allmulti(master
, -1);
132 if (dev
->flags
& IFF_PROMISC
)
133 dev_set_promiscuity(master
, -1);
135 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
136 dev_uc_del(master
, dev
->dev_addr
);
141 static void dsa_slave_change_rx_flags(struct net_device
*dev
, int change
)
143 struct net_device
*master
= dsa_slave_to_master(dev
);
145 if (change
& IFF_ALLMULTI
)
146 dev_set_allmulti(master
, dev
->flags
& IFF_ALLMULTI
? 1 : -1);
147 if (change
& IFF_PROMISC
)
148 dev_set_promiscuity(master
, dev
->flags
& IFF_PROMISC
? 1 : -1);
151 static void dsa_slave_set_rx_mode(struct net_device
*dev
)
153 struct net_device
*master
= dsa_slave_to_master(dev
);
155 dev_mc_sync(master
, dev
);
156 dev_uc_sync(master
, dev
);
159 static int dsa_slave_set_mac_address(struct net_device
*dev
, void *a
)
161 struct net_device
*master
= dsa_slave_to_master(dev
);
162 struct sockaddr
*addr
= a
;
165 if (!is_valid_ether_addr(addr
->sa_data
))
166 return -EADDRNOTAVAIL
;
168 if (!(dev
->flags
& IFF_UP
))
171 if (!ether_addr_equal(addr
->sa_data
, master
->dev_addr
)) {
172 err
= dev_uc_add(master
, addr
->sa_data
);
177 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
178 dev_uc_del(master
, dev
->dev_addr
);
181 ether_addr_copy(dev
->dev_addr
, addr
->sa_data
);
186 struct dsa_slave_dump_ctx
{
187 struct net_device
*dev
;
189 struct netlink_callback
*cb
;
194 dsa_slave_port_fdb_do_dump(const unsigned char *addr
, u16 vid
,
195 bool is_static
, void *data
)
197 struct dsa_slave_dump_ctx
*dump
= data
;
198 u32 portid
= NETLINK_CB(dump
->cb
->skb
).portid
;
199 u32 seq
= dump
->cb
->nlh
->nlmsg_seq
;
200 struct nlmsghdr
*nlh
;
203 if (dump
->idx
< dump
->cb
->args
[2])
206 nlh
= nlmsg_put(dump
->skb
, portid
, seq
, RTM_NEWNEIGH
,
207 sizeof(*ndm
), NLM_F_MULTI
);
211 ndm
= nlmsg_data(nlh
);
212 ndm
->ndm_family
= AF_BRIDGE
;
215 ndm
->ndm_flags
= NTF_SELF
;
217 ndm
->ndm_ifindex
= dump
->dev
->ifindex
;
218 ndm
->ndm_state
= is_static
? NUD_NOARP
: NUD_REACHABLE
;
220 if (nla_put(dump
->skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
221 goto nla_put_failure
;
223 if (vid
&& nla_put_u16(dump
->skb
, NDA_VLAN
, vid
))
224 goto nla_put_failure
;
226 nlmsg_end(dump
->skb
, nlh
);
233 nlmsg_cancel(dump
->skb
, nlh
);
238 dsa_slave_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
239 struct net_device
*dev
, struct net_device
*filter_dev
,
242 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
243 struct dsa_slave_dump_ctx dump
= {
251 err
= dsa_port_fdb_dump(dp
, dsa_slave_port_fdb_do_dump
, &dump
);
257 static int dsa_slave_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
262 return phy_mii_ioctl(dev
->phydev
, ifr
, cmd
);
265 static int dsa_slave_port_attr_set(struct net_device
*dev
,
266 const struct switchdev_attr
*attr
,
267 struct switchdev_trans
*trans
)
269 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
273 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
274 ret
= dsa_port_set_state(dp
, attr
->u
.stp_state
, trans
);
276 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
:
277 ret
= dsa_port_vlan_filtering(dp
, attr
->u
.vlan_filtering
,
280 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
:
281 ret
= dsa_port_ageing_time(dp
, attr
->u
.ageing_time
, trans
);
291 static int dsa_slave_port_obj_add(struct net_device
*dev
,
292 const struct switchdev_obj
*obj
,
293 struct switchdev_trans
*trans
)
295 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
298 /* For the prepare phase, ensure the full set of changes is feasable in
299 * one go in order to signal a failure properly. If an operation is not
300 * supported, return -EOPNOTSUPP.
304 case SWITCHDEV_OBJ_ID_PORT_MDB
:
305 err
= dsa_port_mdb_add(dp
, SWITCHDEV_OBJ_PORT_MDB(obj
), trans
);
307 case SWITCHDEV_OBJ_ID_HOST_MDB
:
308 /* DSA can directly translate this to a normal MDB add,
309 * but on the CPU port.
311 err
= dsa_port_mdb_add(dp
->cpu_dp
, SWITCHDEV_OBJ_PORT_MDB(obj
),
314 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
315 err
= dsa_port_vlan_add(dp
, SWITCHDEV_OBJ_PORT_VLAN(obj
),
326 static int dsa_slave_port_obj_del(struct net_device
*dev
,
327 const struct switchdev_obj
*obj
)
329 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
333 case SWITCHDEV_OBJ_ID_PORT_MDB
:
334 err
= dsa_port_mdb_del(dp
, SWITCHDEV_OBJ_PORT_MDB(obj
));
336 case SWITCHDEV_OBJ_ID_HOST_MDB
:
337 /* DSA can directly translate this to a normal MDB add,
338 * but on the CPU port.
340 err
= dsa_port_mdb_del(dp
->cpu_dp
, SWITCHDEV_OBJ_PORT_MDB(obj
));
342 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
343 err
= dsa_port_vlan_del(dp
, SWITCHDEV_OBJ_PORT_VLAN(obj
));
353 static int dsa_slave_port_attr_get(struct net_device
*dev
,
354 struct switchdev_attr
*attr
)
356 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
357 struct dsa_switch
*ds
= dp
->ds
;
358 struct dsa_switch_tree
*dst
= ds
->dst
;
361 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID
:
362 attr
->u
.ppid
.id_len
= sizeof(dst
->index
);
363 memcpy(&attr
->u
.ppid
.id
, &dst
->index
, attr
->u
.ppid
.id_len
);
365 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT
:
366 attr
->u
.brport_flags_support
= 0;
375 static inline netdev_tx_t
dsa_slave_netpoll_send_skb(struct net_device
*dev
,
378 #ifdef CONFIG_NET_POLL_CONTROLLER
379 struct dsa_slave_priv
*p
= netdev_priv(dev
);
382 netpoll_send_skb(p
->netpoll
, skb
);
389 static netdev_tx_t
dsa_slave_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
391 struct dsa_slave_priv
*p
= netdev_priv(dev
);
392 struct pcpu_sw_netstats
*s
;
393 struct sk_buff
*nskb
;
395 s
= this_cpu_ptr(p
->stats64
);
396 u64_stats_update_begin(&s
->syncp
);
398 s
->tx_bytes
+= skb
->len
;
399 u64_stats_update_end(&s
->syncp
);
401 /* Transmit function may have to reallocate the original SKB,
402 * in which case it must have freed it. Only free it here on error.
404 nskb
= p
->xmit(skb
, dev
);
410 /* SKB for netpoll still need to be mangled with the protocol-specific
411 * tag to be successfully transmitted
413 if (unlikely(netpoll_tx_running(dev
)))
414 return dsa_slave_netpoll_send_skb(dev
, nskb
);
416 /* Queue the SKB for transmission on the parent interface, but
417 * do not modify its EtherType
419 nskb
->dev
= dsa_slave_to_master(dev
);
420 dev_queue_xmit(nskb
);
425 /* ethtool operations *******************************************************/
427 static void dsa_slave_get_drvinfo(struct net_device
*dev
,
428 struct ethtool_drvinfo
*drvinfo
)
430 strlcpy(drvinfo
->driver
, "dsa", sizeof(drvinfo
->driver
));
431 strlcpy(drvinfo
->fw_version
, "N/A", sizeof(drvinfo
->fw_version
));
432 strlcpy(drvinfo
->bus_info
, "platform", sizeof(drvinfo
->bus_info
));
435 static int dsa_slave_get_regs_len(struct net_device
*dev
)
437 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
438 struct dsa_switch
*ds
= dp
->ds
;
440 if (ds
->ops
->get_regs_len
)
441 return ds
->ops
->get_regs_len(ds
, dp
->index
);
447 dsa_slave_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *_p
)
449 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
450 struct dsa_switch
*ds
= dp
->ds
;
452 if (ds
->ops
->get_regs
)
453 ds
->ops
->get_regs(ds
, dp
->index
, regs
, _p
);
456 static u32
dsa_slave_get_link(struct net_device
*dev
)
461 genphy_update_link(dev
->phydev
);
463 return dev
->phydev
->link
;
466 static int dsa_slave_get_eeprom_len(struct net_device
*dev
)
468 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
469 struct dsa_switch
*ds
= dp
->ds
;
471 if (ds
->cd
&& ds
->cd
->eeprom_len
)
472 return ds
->cd
->eeprom_len
;
474 if (ds
->ops
->get_eeprom_len
)
475 return ds
->ops
->get_eeprom_len(ds
);
480 static int dsa_slave_get_eeprom(struct net_device
*dev
,
481 struct ethtool_eeprom
*eeprom
, u8
*data
)
483 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
484 struct dsa_switch
*ds
= dp
->ds
;
486 if (ds
->ops
->get_eeprom
)
487 return ds
->ops
->get_eeprom(ds
, eeprom
, data
);
492 static int dsa_slave_set_eeprom(struct net_device
*dev
,
493 struct ethtool_eeprom
*eeprom
, u8
*data
)
495 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
496 struct dsa_switch
*ds
= dp
->ds
;
498 if (ds
->ops
->set_eeprom
)
499 return ds
->ops
->set_eeprom(ds
, eeprom
, data
);
504 static void dsa_slave_get_strings(struct net_device
*dev
,
505 uint32_t stringset
, uint8_t *data
)
507 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
508 struct dsa_switch
*ds
= dp
->ds
;
510 if (stringset
== ETH_SS_STATS
) {
511 int len
= ETH_GSTRING_LEN
;
513 strncpy(data
, "tx_packets", len
);
514 strncpy(data
+ len
, "tx_bytes", len
);
515 strncpy(data
+ 2 * len
, "rx_packets", len
);
516 strncpy(data
+ 3 * len
, "rx_bytes", len
);
517 if (ds
->ops
->get_strings
)
518 ds
->ops
->get_strings(ds
, dp
->index
, data
+ 4 * len
);
522 static void dsa_slave_get_ethtool_stats(struct net_device
*dev
,
523 struct ethtool_stats
*stats
,
526 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
527 struct dsa_slave_priv
*p
= netdev_priv(dev
);
528 struct dsa_switch
*ds
= dp
->ds
;
529 struct pcpu_sw_netstats
*s
;
533 for_each_possible_cpu(i
) {
534 u64 tx_packets
, tx_bytes
, rx_packets
, rx_bytes
;
536 s
= per_cpu_ptr(p
->stats64
, i
);
538 start
= u64_stats_fetch_begin_irq(&s
->syncp
);
539 tx_packets
= s
->tx_packets
;
540 tx_bytes
= s
->tx_bytes
;
541 rx_packets
= s
->rx_packets
;
542 rx_bytes
= s
->rx_bytes
;
543 } while (u64_stats_fetch_retry_irq(&s
->syncp
, start
));
544 data
[0] += tx_packets
;
546 data
[2] += rx_packets
;
549 if (ds
->ops
->get_ethtool_stats
)
550 ds
->ops
->get_ethtool_stats(ds
, dp
->index
, data
+ 4);
553 static int dsa_slave_get_sset_count(struct net_device
*dev
, int sset
)
555 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
556 struct dsa_switch
*ds
= dp
->ds
;
558 if (sset
== ETH_SS_STATS
) {
562 if (ds
->ops
->get_sset_count
)
563 count
+= ds
->ops
->get_sset_count(ds
);
571 static void dsa_slave_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
573 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
574 struct dsa_switch
*ds
= dp
->ds
;
576 if (ds
->ops
->get_wol
)
577 ds
->ops
->get_wol(ds
, dp
->index
, w
);
580 static int dsa_slave_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
582 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
583 struct dsa_switch
*ds
= dp
->ds
;
584 int ret
= -EOPNOTSUPP
;
586 if (ds
->ops
->set_wol
)
587 ret
= ds
->ops
->set_wol(ds
, dp
->index
, w
);
592 static int dsa_slave_set_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
594 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
595 struct dsa_switch
*ds
= dp
->ds
;
598 /* Port's PHY and MAC both need to be EEE capable */
602 if (!ds
->ops
->set_mac_eee
)
605 ret
= ds
->ops
->set_mac_eee(ds
, dp
->index
, e
);
609 if (e
->eee_enabled
) {
610 ret
= phy_init_eee(dev
->phydev
, 0);
615 return phy_ethtool_set_eee(dev
->phydev
, e
);
618 static int dsa_slave_get_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
620 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
621 struct dsa_switch
*ds
= dp
->ds
;
624 /* Port's PHY and MAC both need to be EEE capable */
628 if (!ds
->ops
->get_mac_eee
)
631 ret
= ds
->ops
->get_mac_eee(ds
, dp
->index
, e
);
635 return phy_ethtool_get_eee(dev
->phydev
, e
);
638 #ifdef CONFIG_NET_POLL_CONTROLLER
639 static int dsa_slave_netpoll_setup(struct net_device
*dev
,
640 struct netpoll_info
*ni
)
642 struct net_device
*master
= dsa_slave_to_master(dev
);
643 struct dsa_slave_priv
*p
= netdev_priv(dev
);
644 struct netpoll
*netpoll
;
647 netpoll
= kzalloc(sizeof(*netpoll
), GFP_KERNEL
);
651 err
= __netpoll_setup(netpoll
, master
);
657 p
->netpoll
= netpoll
;
662 static void dsa_slave_netpoll_cleanup(struct net_device
*dev
)
664 struct dsa_slave_priv
*p
= netdev_priv(dev
);
665 struct netpoll
*netpoll
= p
->netpoll
;
672 __netpoll_free_async(netpoll
);
675 static void dsa_slave_poll_controller(struct net_device
*dev
)
680 static int dsa_slave_get_phys_port_name(struct net_device
*dev
,
681 char *name
, size_t len
)
683 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
685 if (snprintf(name
, len
, "p%d", dp
->index
) >= len
)
691 static struct dsa_mall_tc_entry
*
692 dsa_slave_mall_tc_entry_find(struct net_device
*dev
, unsigned long cookie
)
694 struct dsa_slave_priv
*p
= netdev_priv(dev
);
695 struct dsa_mall_tc_entry
*mall_tc_entry
;
697 list_for_each_entry(mall_tc_entry
, &p
->mall_tc_list
, list
)
698 if (mall_tc_entry
->cookie
== cookie
)
699 return mall_tc_entry
;
704 static int dsa_slave_add_cls_matchall(struct net_device
*dev
,
705 struct tc_cls_matchall_offload
*cls
,
708 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
709 struct dsa_slave_priv
*p
= netdev_priv(dev
);
710 struct dsa_mall_tc_entry
*mall_tc_entry
;
711 __be16 protocol
= cls
->common
.protocol
;
712 struct net
*net
= dev_net(dev
);
713 struct dsa_switch
*ds
= dp
->ds
;
714 struct net_device
*to_dev
;
715 const struct tc_action
*a
;
716 struct dsa_port
*to_dp
;
717 int err
= -EOPNOTSUPP
;
721 if (!ds
->ops
->port_mirror_add
)
724 if (!tcf_exts_has_one_action(cls
->exts
))
727 tcf_exts_to_list(cls
->exts
, &actions
);
728 a
= list_first_entry(&actions
, struct tc_action
, list
);
730 if (is_tcf_mirred_egress_mirror(a
) && protocol
== htons(ETH_P_ALL
)) {
731 struct dsa_mall_mirror_tc_entry
*mirror
;
733 ifindex
= tcf_mirred_ifindex(a
);
734 to_dev
= __dev_get_by_index(net
, ifindex
);
738 if (!dsa_slave_dev_check(to_dev
))
741 mall_tc_entry
= kzalloc(sizeof(*mall_tc_entry
), GFP_KERNEL
);
745 mall_tc_entry
->cookie
= cls
->cookie
;
746 mall_tc_entry
->type
= DSA_PORT_MALL_MIRROR
;
747 mirror
= &mall_tc_entry
->mirror
;
749 to_dp
= dsa_slave_to_port(to_dev
);
751 mirror
->to_local_port
= to_dp
->index
;
752 mirror
->ingress
= ingress
;
754 err
= ds
->ops
->port_mirror_add(ds
, dp
->index
, mirror
, ingress
);
756 kfree(mall_tc_entry
);
760 list_add_tail(&mall_tc_entry
->list
, &p
->mall_tc_list
);
766 static void dsa_slave_del_cls_matchall(struct net_device
*dev
,
767 struct tc_cls_matchall_offload
*cls
)
769 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
770 struct dsa_mall_tc_entry
*mall_tc_entry
;
771 struct dsa_switch
*ds
= dp
->ds
;
773 if (!ds
->ops
->port_mirror_del
)
776 mall_tc_entry
= dsa_slave_mall_tc_entry_find(dev
, cls
->cookie
);
780 list_del(&mall_tc_entry
->list
);
782 switch (mall_tc_entry
->type
) {
783 case DSA_PORT_MALL_MIRROR
:
784 ds
->ops
->port_mirror_del(ds
, dp
->index
, &mall_tc_entry
->mirror
);
790 kfree(mall_tc_entry
);
793 static int dsa_slave_setup_tc_cls_matchall(struct net_device
*dev
,
794 struct tc_cls_matchall_offload
*cls
,
797 if (cls
->common
.chain_index
)
800 switch (cls
->command
) {
801 case TC_CLSMATCHALL_REPLACE
:
802 return dsa_slave_add_cls_matchall(dev
, cls
, ingress
);
803 case TC_CLSMATCHALL_DESTROY
:
804 dsa_slave_del_cls_matchall(dev
, cls
);
811 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type
, void *type_data
,
812 void *cb_priv
, bool ingress
)
814 struct net_device
*dev
= cb_priv
;
816 if (!tc_can_offload(dev
))
820 case TC_SETUP_CLSMATCHALL
:
821 return dsa_slave_setup_tc_cls_matchall(dev
, type_data
, ingress
);
827 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type
,
828 void *type_data
, void *cb_priv
)
830 return dsa_slave_setup_tc_block_cb(type
, type_data
, cb_priv
, true);
833 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type
,
834 void *type_data
, void *cb_priv
)
836 return dsa_slave_setup_tc_block_cb(type
, type_data
, cb_priv
, false);
839 static int dsa_slave_setup_tc_block(struct net_device
*dev
,
840 struct tc_block_offload
*f
)
844 if (f
->binder_type
== TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
)
845 cb
= dsa_slave_setup_tc_block_cb_ig
;
846 else if (f
->binder_type
== TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS
)
847 cb
= dsa_slave_setup_tc_block_cb_eg
;
851 switch (f
->command
) {
853 return tcf_block_cb_register(f
->block
, cb
, dev
, dev
);
854 case TC_BLOCK_UNBIND
:
855 tcf_block_cb_unregister(f
->block
, cb
, dev
);
862 static int dsa_slave_setup_tc(struct net_device
*dev
, enum tc_setup_type type
,
867 return dsa_slave_setup_tc_block(dev
, type_data
);
873 static void dsa_slave_get_stats64(struct net_device
*dev
,
874 struct rtnl_link_stats64
*stats
)
876 struct dsa_slave_priv
*p
= netdev_priv(dev
);
877 struct pcpu_sw_netstats
*s
;
881 netdev_stats_to_stats64(stats
, &dev
->stats
);
882 for_each_possible_cpu(i
) {
883 u64 tx_packets
, tx_bytes
, rx_packets
, rx_bytes
;
885 s
= per_cpu_ptr(p
->stats64
, i
);
887 start
= u64_stats_fetch_begin_irq(&s
->syncp
);
888 tx_packets
= s
->tx_packets
;
889 tx_bytes
= s
->tx_bytes
;
890 rx_packets
= s
->rx_packets
;
891 rx_bytes
= s
->rx_bytes
;
892 } while (u64_stats_fetch_retry_irq(&s
->syncp
, start
));
894 stats
->tx_packets
+= tx_packets
;
895 stats
->tx_bytes
+= tx_bytes
;
896 stats
->rx_packets
+= rx_packets
;
897 stats
->rx_bytes
+= rx_bytes
;
901 static int dsa_slave_get_rxnfc(struct net_device
*dev
,
902 struct ethtool_rxnfc
*nfc
, u32
*rule_locs
)
904 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
905 struct dsa_switch
*ds
= dp
->ds
;
907 if (!ds
->ops
->get_rxnfc
)
910 return ds
->ops
->get_rxnfc(ds
, dp
->index
, nfc
, rule_locs
);
913 static int dsa_slave_set_rxnfc(struct net_device
*dev
,
914 struct ethtool_rxnfc
*nfc
)
916 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
917 struct dsa_switch
*ds
= dp
->ds
;
919 if (!ds
->ops
->set_rxnfc
)
922 return ds
->ops
->set_rxnfc(ds
, dp
->index
, nfc
);
925 static const struct ethtool_ops dsa_slave_ethtool_ops
= {
926 .get_drvinfo
= dsa_slave_get_drvinfo
,
927 .get_regs_len
= dsa_slave_get_regs_len
,
928 .get_regs
= dsa_slave_get_regs
,
929 .nway_reset
= phy_ethtool_nway_reset
,
930 .get_link
= dsa_slave_get_link
,
931 .get_eeprom_len
= dsa_slave_get_eeprom_len
,
932 .get_eeprom
= dsa_slave_get_eeprom
,
933 .set_eeprom
= dsa_slave_set_eeprom
,
934 .get_strings
= dsa_slave_get_strings
,
935 .get_ethtool_stats
= dsa_slave_get_ethtool_stats
,
936 .get_sset_count
= dsa_slave_get_sset_count
,
937 .set_wol
= dsa_slave_set_wol
,
938 .get_wol
= dsa_slave_get_wol
,
939 .set_eee
= dsa_slave_set_eee
,
940 .get_eee
= dsa_slave_get_eee
,
941 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
942 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
943 .get_rxnfc
= dsa_slave_get_rxnfc
,
944 .set_rxnfc
= dsa_slave_set_rxnfc
,
947 static const struct net_device_ops dsa_slave_netdev_ops
= {
948 .ndo_open
= dsa_slave_open
,
949 .ndo_stop
= dsa_slave_close
,
950 .ndo_start_xmit
= dsa_slave_xmit
,
951 .ndo_change_rx_flags
= dsa_slave_change_rx_flags
,
952 .ndo_set_rx_mode
= dsa_slave_set_rx_mode
,
953 .ndo_set_mac_address
= dsa_slave_set_mac_address
,
954 .ndo_fdb_add
= dsa_legacy_fdb_add
,
955 .ndo_fdb_del
= dsa_legacy_fdb_del
,
956 .ndo_fdb_dump
= dsa_slave_fdb_dump
,
957 .ndo_do_ioctl
= dsa_slave_ioctl
,
958 .ndo_get_iflink
= dsa_slave_get_iflink
,
959 #ifdef CONFIG_NET_POLL_CONTROLLER
960 .ndo_netpoll_setup
= dsa_slave_netpoll_setup
,
961 .ndo_netpoll_cleanup
= dsa_slave_netpoll_cleanup
,
962 .ndo_poll_controller
= dsa_slave_poll_controller
,
964 .ndo_get_phys_port_name
= dsa_slave_get_phys_port_name
,
965 .ndo_setup_tc
= dsa_slave_setup_tc
,
966 .ndo_get_stats64
= dsa_slave_get_stats64
,
969 static const struct switchdev_ops dsa_slave_switchdev_ops
= {
970 .switchdev_port_attr_get
= dsa_slave_port_attr_get
,
971 .switchdev_port_attr_set
= dsa_slave_port_attr_set
,
972 .switchdev_port_obj_add
= dsa_slave_port_obj_add
,
973 .switchdev_port_obj_del
= dsa_slave_port_obj_del
,
976 static struct device_type dsa_type
= {
980 static void dsa_slave_adjust_link(struct net_device
*dev
)
982 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
983 struct dsa_slave_priv
*p
= netdev_priv(dev
);
984 struct dsa_switch
*ds
= dp
->ds
;
985 unsigned int status_changed
= 0;
987 if (p
->old_link
!= dev
->phydev
->link
) {
989 p
->old_link
= dev
->phydev
->link
;
992 if (p
->old_duplex
!= dev
->phydev
->duplex
) {
994 p
->old_duplex
= dev
->phydev
->duplex
;
997 if (p
->old_pause
!= dev
->phydev
->pause
) {
999 p
->old_pause
= dev
->phydev
->pause
;
1002 if (ds
->ops
->adjust_link
&& status_changed
)
1003 ds
->ops
->adjust_link(ds
, dp
->index
, dev
->phydev
);
1006 phy_print_status(dev
->phydev
);
1009 static int dsa_slave_fixed_link_update(struct net_device
*dev
,
1010 struct fixed_phy_status
*status
)
1012 struct dsa_switch
*ds
;
1013 struct dsa_port
*dp
;
1016 dp
= dsa_slave_to_port(dev
);
1018 if (ds
->ops
->fixed_link_update
)
1019 ds
->ops
->fixed_link_update(ds
, dp
->index
, status
);
1025 /* slave device setup *******************************************************/
1026 static int dsa_slave_phy_connect(struct net_device
*slave_dev
, int addr
)
1028 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1029 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1030 struct dsa_switch
*ds
= dp
->ds
;
1032 slave_dev
->phydev
= mdiobus_get_phy(ds
->slave_mii_bus
, addr
);
1033 if (!slave_dev
->phydev
) {
1034 netdev_err(slave_dev
, "no phy at %d\n", addr
);
1038 /* Use already configured phy mode */
1039 if (p
->phy_interface
== PHY_INTERFACE_MODE_NA
)
1040 p
->phy_interface
= slave_dev
->phydev
->interface
;
1042 return phy_connect_direct(slave_dev
, slave_dev
->phydev
,
1043 dsa_slave_adjust_link
, p
->phy_interface
);
1046 static int dsa_slave_phy_setup(struct net_device
*slave_dev
)
1048 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1049 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1050 struct device_node
*port_dn
= dp
->dn
;
1051 struct dsa_switch
*ds
= dp
->ds
;
1052 struct device_node
*phy_dn
;
1053 bool phy_is_fixed
= false;
1057 mode
= of_get_phy_mode(port_dn
);
1059 mode
= PHY_INTERFACE_MODE_NA
;
1060 p
->phy_interface
= mode
;
1062 phy_dn
= of_parse_phandle(port_dn
, "phy-handle", 0);
1063 if (!phy_dn
&& of_phy_is_fixed_link(port_dn
)) {
1064 /* In the case of a fixed PHY, the DT node associated
1065 * to the fixed PHY is the Port DT node
1067 ret
= of_phy_register_fixed_link(port_dn
);
1069 netdev_err(slave_dev
, "failed to register fixed PHY: %d\n", ret
);
1072 phy_is_fixed
= true;
1073 phy_dn
= of_node_get(port_dn
);
1076 if (ds
->ops
->get_phy_flags
)
1077 phy_flags
= ds
->ops
->get_phy_flags(ds
, dp
->index
);
1080 slave_dev
->phydev
= of_phy_connect(slave_dev
, phy_dn
,
1081 dsa_slave_adjust_link
,
1084 of_node_put(phy_dn
);
1087 if (slave_dev
->phydev
&& phy_is_fixed
)
1088 fixed_phy_set_link_update(slave_dev
->phydev
,
1089 dsa_slave_fixed_link_update
);
1091 /* We could not connect to a designated PHY, so use the switch internal
1094 if (!slave_dev
->phydev
) {
1095 ret
= dsa_slave_phy_connect(slave_dev
, dp
->index
);
1097 netdev_err(slave_dev
, "failed to connect to port %d: %d\n",
1100 of_phy_deregister_fixed_link(port_dn
);
1105 phy_attached_info(slave_dev
->phydev
);
1110 static struct lock_class_key dsa_slave_netdev_xmit_lock_key
;
1111 static void dsa_slave_set_lockdep_class_one(struct net_device
*dev
,
1112 struct netdev_queue
*txq
,
1115 lockdep_set_class(&txq
->_xmit_lock
,
1116 &dsa_slave_netdev_xmit_lock_key
);
1119 int dsa_slave_suspend(struct net_device
*slave_dev
)
1121 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1123 netif_device_detach(slave_dev
);
1125 if (slave_dev
->phydev
) {
1126 phy_stop(slave_dev
->phydev
);
1130 phy_suspend(slave_dev
->phydev
);
1136 int dsa_slave_resume(struct net_device
*slave_dev
)
1138 netif_device_attach(slave_dev
);
1140 if (slave_dev
->phydev
) {
1141 phy_resume(slave_dev
->phydev
);
1142 phy_start(slave_dev
->phydev
);
1148 static void dsa_slave_notify(struct net_device
*dev
, unsigned long val
)
1150 struct net_device
*master
= dsa_slave_to_master(dev
);
1151 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1152 struct dsa_notifier_register_info rinfo
= {
1153 .switch_number
= dp
->ds
->index
,
1154 .port_number
= dp
->index
,
1159 call_dsa_notifiers(val
, dev
, &rinfo
.info
);
1162 int dsa_slave_create(struct dsa_port
*port
)
1164 const struct dsa_port
*cpu_dp
= port
->cpu_dp
;
1165 struct net_device
*master
= cpu_dp
->master
;
1166 struct dsa_switch
*ds
= port
->ds
;
1167 const char *name
= port
->name
;
1168 struct net_device
*slave_dev
;
1169 struct dsa_slave_priv
*p
;
1172 if (!ds
->num_tx_queues
)
1173 ds
->num_tx_queues
= 1;
1175 slave_dev
= alloc_netdev_mqs(sizeof(struct dsa_slave_priv
), name
,
1176 NET_NAME_UNKNOWN
, ether_setup
,
1177 ds
->num_tx_queues
, 1);
1178 if (slave_dev
== NULL
)
1181 slave_dev
->features
= master
->vlan_features
| NETIF_F_HW_TC
;
1182 slave_dev
->hw_features
|= NETIF_F_HW_TC
;
1183 slave_dev
->ethtool_ops
= &dsa_slave_ethtool_ops
;
1184 eth_hw_addr_inherit(slave_dev
, master
);
1185 slave_dev
->priv_flags
|= IFF_NO_QUEUE
;
1186 slave_dev
->netdev_ops
= &dsa_slave_netdev_ops
;
1187 slave_dev
->switchdev_ops
= &dsa_slave_switchdev_ops
;
1188 slave_dev
->min_mtu
= 0;
1189 slave_dev
->max_mtu
= ETH_MAX_MTU
;
1190 SET_NETDEV_DEVTYPE(slave_dev
, &dsa_type
);
1192 netdev_for_each_tx_queue(slave_dev
, dsa_slave_set_lockdep_class_one
,
1195 SET_NETDEV_DEV(slave_dev
, port
->ds
->dev
);
1196 slave_dev
->dev
.of_node
= port
->dn
;
1197 slave_dev
->vlan_features
= master
->vlan_features
;
1199 p
= netdev_priv(slave_dev
);
1200 p
->stats64
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
1202 free_netdev(slave_dev
);
1206 INIT_LIST_HEAD(&p
->mall_tc_list
);
1207 p
->xmit
= cpu_dp
->tag_ops
->xmit
;
1213 port
->slave
= slave_dev
;
1215 netif_carrier_off(slave_dev
);
1217 ret
= dsa_slave_phy_setup(slave_dev
);
1219 netdev_err(master
, "error %d setting up slave phy\n", ret
);
1223 dsa_slave_notify(slave_dev
, DSA_PORT_REGISTER
);
1225 ret
= register_netdev(slave_dev
);
1227 netdev_err(master
, "error %d registering interface %s\n",
1228 ret
, slave_dev
->name
);
1235 phy_disconnect(slave_dev
->phydev
);
1236 if (of_phy_is_fixed_link(port
->dn
))
1237 of_phy_deregister_fixed_link(port
->dn
);
1239 free_percpu(p
->stats64
);
1240 free_netdev(slave_dev
);
1245 void dsa_slave_destroy(struct net_device
*slave_dev
)
1247 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1248 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1249 struct device_node
*port_dn
= dp
->dn
;
1251 netif_carrier_off(slave_dev
);
1252 if (slave_dev
->phydev
) {
1253 phy_disconnect(slave_dev
->phydev
);
1255 if (of_phy_is_fixed_link(port_dn
))
1256 of_phy_deregister_fixed_link(port_dn
);
1258 dsa_slave_notify(slave_dev
, DSA_PORT_UNREGISTER
);
1259 unregister_netdev(slave_dev
);
1260 free_percpu(p
->stats64
);
1261 free_netdev(slave_dev
);
1264 static bool dsa_slave_dev_check(struct net_device
*dev
)
1266 return dev
->netdev_ops
== &dsa_slave_netdev_ops
;
1269 static int dsa_slave_changeupper(struct net_device
*dev
,
1270 struct netdev_notifier_changeupper_info
*info
)
1272 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1273 int err
= NOTIFY_DONE
;
1275 if (netif_is_bridge_master(info
->upper_dev
)) {
1276 if (info
->linking
) {
1277 err
= dsa_port_bridge_join(dp
, info
->upper_dev
);
1278 err
= notifier_from_errno(err
);
1280 dsa_port_bridge_leave(dp
, info
->upper_dev
);
1288 static int dsa_slave_netdevice_event(struct notifier_block
*nb
,
1289 unsigned long event
, void *ptr
)
1291 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1293 if (!dsa_slave_dev_check(dev
))
1296 if (event
== NETDEV_CHANGEUPPER
)
1297 return dsa_slave_changeupper(dev
, ptr
);
1302 struct dsa_switchdev_event_work
{
1303 struct work_struct work
;
1304 struct switchdev_notifier_fdb_info fdb_info
;
1305 struct net_device
*dev
;
1306 unsigned long event
;
1309 static void dsa_slave_switchdev_event_work(struct work_struct
*work
)
1311 struct dsa_switchdev_event_work
*switchdev_work
=
1312 container_of(work
, struct dsa_switchdev_event_work
, work
);
1313 struct net_device
*dev
= switchdev_work
->dev
;
1314 struct switchdev_notifier_fdb_info
*fdb_info
;
1315 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1319 switch (switchdev_work
->event
) {
1320 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
1321 fdb_info
= &switchdev_work
->fdb_info
;
1322 err
= dsa_port_fdb_add(dp
, fdb_info
->addr
, fdb_info
->vid
);
1324 netdev_dbg(dev
, "fdb add failed err=%d\n", err
);
1327 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED
, dev
,
1331 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1332 fdb_info
= &switchdev_work
->fdb_info
;
1333 err
= dsa_port_fdb_del(dp
, fdb_info
->addr
, fdb_info
->vid
);
1335 netdev_dbg(dev
, "fdb del failed err=%d\n", err
);
1342 kfree(switchdev_work
->fdb_info
.addr
);
1343 kfree(switchdev_work
);
1348 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work
*
1350 const struct switchdev_notifier_fdb_info
*
1353 memcpy(&switchdev_work
->fdb_info
, fdb_info
,
1354 sizeof(switchdev_work
->fdb_info
));
1355 switchdev_work
->fdb_info
.addr
= kzalloc(ETH_ALEN
, GFP_ATOMIC
);
1356 if (!switchdev_work
->fdb_info
.addr
)
1358 ether_addr_copy((u8
*)switchdev_work
->fdb_info
.addr
,
1363 /* Called under rcu_read_lock() */
1364 static int dsa_slave_switchdev_event(struct notifier_block
*unused
,
1365 unsigned long event
, void *ptr
)
1367 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
1368 struct dsa_switchdev_event_work
*switchdev_work
;
1370 if (!dsa_slave_dev_check(dev
))
1373 switchdev_work
= kzalloc(sizeof(*switchdev_work
), GFP_ATOMIC
);
1374 if (!switchdev_work
)
1377 INIT_WORK(&switchdev_work
->work
,
1378 dsa_slave_switchdev_event_work
);
1379 switchdev_work
->dev
= dev
;
1380 switchdev_work
->event
= event
;
1383 case SWITCHDEV_FDB_ADD_TO_DEVICE
: /* fall through */
1384 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1385 if (dsa_slave_switchdev_fdb_work_init(switchdev_work
,
1387 goto err_fdb_work_init
;
1391 kfree(switchdev_work
);
1395 dsa_schedule_work(&switchdev_work
->work
);
1399 kfree(switchdev_work
);
1403 static struct notifier_block dsa_slave_nb __read_mostly
= {
1404 .notifier_call
= dsa_slave_netdevice_event
,
1407 static struct notifier_block dsa_slave_switchdev_notifier
= {
1408 .notifier_call
= dsa_slave_switchdev_event
,
1411 int dsa_slave_register_notifier(void)
1415 err
= register_netdevice_notifier(&dsa_slave_nb
);
1419 err
= register_switchdev_notifier(&dsa_slave_switchdev_notifier
);
1421 goto err_switchdev_nb
;
1426 unregister_netdevice_notifier(&dsa_slave_nb
);
1430 void dsa_slave_unregister_notifier(void)
1434 err
= unregister_switchdev_notifier(&dsa_slave_switchdev_notifier
);
1436 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err
);
1438 err
= unregister_netdevice_notifier(&dsa_slave_nb
);
1440 pr_err("DSA: failed to unregister slave notifier (%d)\n", err
);