2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/list.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/of_net.h>
17 #include <linux/of_mdio.h>
18 #include <linux/mdio.h>
19 #include <linux/list.h>
20 #include <net/rtnetlink.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_mirred.h>
23 #include <linux/if_bridge.h>
24 #include <linux/netpoll.h>
28 static bool dsa_slave_dev_check(struct net_device
*dev
);
30 /* slave mii_bus handling ***************************************************/
31 static int dsa_slave_phy_read(struct mii_bus
*bus
, int addr
, int reg
)
33 struct dsa_switch
*ds
= bus
->priv
;
35 if (ds
->phys_mii_mask
& (1 << addr
))
36 return ds
->ops
->phy_read(ds
, addr
, reg
);
41 static int dsa_slave_phy_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
43 struct dsa_switch
*ds
= bus
->priv
;
45 if (ds
->phys_mii_mask
& (1 << addr
))
46 return ds
->ops
->phy_write(ds
, addr
, reg
, val
);
51 void dsa_slave_mii_bus_init(struct dsa_switch
*ds
)
53 ds
->slave_mii_bus
->priv
= (void *)ds
;
54 ds
->slave_mii_bus
->name
= "dsa slave smi";
55 ds
->slave_mii_bus
->read
= dsa_slave_phy_read
;
56 ds
->slave_mii_bus
->write
= dsa_slave_phy_write
;
57 snprintf(ds
->slave_mii_bus
->id
, MII_BUS_ID_SIZE
, "dsa-%d.%d",
58 ds
->dst
->tree
, ds
->index
);
59 ds
->slave_mii_bus
->parent
= ds
->dev
;
60 ds
->slave_mii_bus
->phy_mask
= ~ds
->phys_mii_mask
;
64 /* slave device handling ****************************************************/
65 static int dsa_slave_get_iflink(const struct net_device
*dev
)
67 struct dsa_slave_priv
*p
= netdev_priv(dev
);
69 return dsa_master_netdev(p
)->ifindex
;
72 static int dsa_slave_open(struct net_device
*dev
)
74 struct dsa_slave_priv
*p
= netdev_priv(dev
);
75 struct net_device
*master
= dsa_master_netdev(p
);
76 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
79 if (!(master
->flags
& IFF_UP
))
82 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
)) {
83 err
= dev_uc_add(master
, dev
->dev_addr
);
88 if (dev
->flags
& IFF_ALLMULTI
) {
89 err
= dev_set_allmulti(master
, 1);
93 if (dev
->flags
& IFF_PROMISC
) {
94 err
= dev_set_promiscuity(master
, 1);
99 err
= dsa_port_enable(dp
, dev
->phydev
);
104 phy_start(dev
->phydev
);
109 if (dev
->flags
& IFF_PROMISC
)
110 dev_set_promiscuity(master
, -1);
112 if (dev
->flags
& IFF_ALLMULTI
)
113 dev_set_allmulti(master
, -1);
115 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
116 dev_uc_del(master
, dev
->dev_addr
);
121 static int dsa_slave_close(struct net_device
*dev
)
123 struct dsa_slave_priv
*p
= netdev_priv(dev
);
124 struct net_device
*master
= dsa_master_netdev(p
);
125 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
128 phy_stop(dev
->phydev
);
130 dsa_port_disable(dp
, dev
->phydev
);
132 dev_mc_unsync(master
, dev
);
133 dev_uc_unsync(master
, dev
);
134 if (dev
->flags
& IFF_ALLMULTI
)
135 dev_set_allmulti(master
, -1);
136 if (dev
->flags
& IFF_PROMISC
)
137 dev_set_promiscuity(master
, -1);
139 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
140 dev_uc_del(master
, dev
->dev_addr
);
145 static void dsa_slave_change_rx_flags(struct net_device
*dev
, int change
)
147 struct dsa_slave_priv
*p
= netdev_priv(dev
);
148 struct net_device
*master
= dsa_master_netdev(p
);
150 if (change
& IFF_ALLMULTI
)
151 dev_set_allmulti(master
, dev
->flags
& IFF_ALLMULTI
? 1 : -1);
152 if (change
& IFF_PROMISC
)
153 dev_set_promiscuity(master
, dev
->flags
& IFF_PROMISC
? 1 : -1);
156 static void dsa_slave_set_rx_mode(struct net_device
*dev
)
158 struct dsa_slave_priv
*p
= netdev_priv(dev
);
159 struct net_device
*master
= dsa_master_netdev(p
);
161 dev_mc_sync(master
, dev
);
162 dev_uc_sync(master
, dev
);
165 static int dsa_slave_set_mac_address(struct net_device
*dev
, void *a
)
167 struct dsa_slave_priv
*p
= netdev_priv(dev
);
168 struct net_device
*master
= dsa_master_netdev(p
);
169 struct sockaddr
*addr
= a
;
172 if (!is_valid_ether_addr(addr
->sa_data
))
173 return -EADDRNOTAVAIL
;
175 if (!(dev
->flags
& IFF_UP
))
178 if (!ether_addr_equal(addr
->sa_data
, master
->dev_addr
)) {
179 err
= dev_uc_add(master
, addr
->sa_data
);
184 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
185 dev_uc_del(master
, dev
->dev_addr
);
188 ether_addr_copy(dev
->dev_addr
, addr
->sa_data
);
193 struct dsa_slave_dump_ctx
{
194 struct net_device
*dev
;
196 struct netlink_callback
*cb
;
201 dsa_slave_port_fdb_do_dump(const unsigned char *addr
, u16 vid
,
202 bool is_static
, void *data
)
204 struct dsa_slave_dump_ctx
*dump
= data
;
205 u32 portid
= NETLINK_CB(dump
->cb
->skb
).portid
;
206 u32 seq
= dump
->cb
->nlh
->nlmsg_seq
;
207 struct nlmsghdr
*nlh
;
210 if (dump
->idx
< dump
->cb
->args
[2])
213 nlh
= nlmsg_put(dump
->skb
, portid
, seq
, RTM_NEWNEIGH
,
214 sizeof(*ndm
), NLM_F_MULTI
);
218 ndm
= nlmsg_data(nlh
);
219 ndm
->ndm_family
= AF_BRIDGE
;
222 ndm
->ndm_flags
= NTF_SELF
;
224 ndm
->ndm_ifindex
= dump
->dev
->ifindex
;
225 ndm
->ndm_state
= is_static
? NUD_NOARP
: NUD_REACHABLE
;
227 if (nla_put(dump
->skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
228 goto nla_put_failure
;
230 if (vid
&& nla_put_u16(dump
->skb
, NDA_VLAN
, vid
))
231 goto nla_put_failure
;
233 nlmsg_end(dump
->skb
, nlh
);
240 nlmsg_cancel(dump
->skb
, nlh
);
245 dsa_slave_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
246 struct net_device
*dev
, struct net_device
*filter_dev
,
249 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
250 struct dsa_slave_dump_ctx dump
= {
258 err
= dsa_port_fdb_dump(dp
, dsa_slave_port_fdb_do_dump
, &dump
);
264 static int dsa_slave_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
269 return phy_mii_ioctl(dev
->phydev
, ifr
, cmd
);
272 static int dsa_slave_port_attr_set(struct net_device
*dev
,
273 const struct switchdev_attr
*attr
,
274 struct switchdev_trans
*trans
)
276 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
280 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
281 ret
= dsa_port_set_state(dp
, attr
->u
.stp_state
, trans
);
283 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
:
284 ret
= dsa_port_vlan_filtering(dp
, attr
->u
.vlan_filtering
,
287 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
:
288 ret
= dsa_port_ageing_time(dp
, attr
->u
.ageing_time
, trans
);
298 static int dsa_slave_port_obj_add(struct net_device
*dev
,
299 const struct switchdev_obj
*obj
,
300 struct switchdev_trans
*trans
)
302 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
305 /* For the prepare phase, ensure the full set of changes is feasable in
306 * one go in order to signal a failure properly. If an operation is not
307 * supported, return -EOPNOTSUPP.
311 case SWITCHDEV_OBJ_ID_PORT_MDB
:
312 err
= dsa_port_mdb_add(dp
, SWITCHDEV_OBJ_PORT_MDB(obj
), trans
);
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_PORT_VLAN
:
337 err
= dsa_port_vlan_del(dp
, SWITCHDEV_OBJ_PORT_VLAN(obj
));
347 static int dsa_slave_port_attr_get(struct net_device
*dev
,
348 struct switchdev_attr
*attr
)
350 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
351 struct dsa_switch
*ds
= dp
->ds
;
354 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID
:
355 attr
->u
.ppid
.id_len
= sizeof(ds
->index
);
356 memcpy(&attr
->u
.ppid
.id
, &ds
->index
, attr
->u
.ppid
.id_len
);
358 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT
:
359 attr
->u
.brport_flags_support
= 0;
368 static inline netdev_tx_t
dsa_slave_netpoll_send_skb(struct net_device
*dev
,
371 #ifdef CONFIG_NET_POLL_CONTROLLER
372 struct dsa_slave_priv
*p
= netdev_priv(dev
);
375 netpoll_send_skb(p
->netpoll
, skb
);
382 static netdev_tx_t
dsa_slave_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
384 struct dsa_slave_priv
*p
= netdev_priv(dev
);
385 struct pcpu_sw_netstats
*s
;
386 struct sk_buff
*nskb
;
388 s
= this_cpu_ptr(p
->stats64
);
389 u64_stats_update_begin(&s
->syncp
);
391 s
->tx_bytes
+= skb
->len
;
392 u64_stats_update_end(&s
->syncp
);
394 /* Transmit function may have to reallocate the original SKB,
395 * in which case it must have freed it. Only free it here on error.
397 nskb
= p
->xmit(skb
, dev
);
403 /* SKB for netpoll still need to be mangled with the protocol-specific
404 * tag to be successfully transmitted
406 if (unlikely(netpoll_tx_running(dev
)))
407 return dsa_slave_netpoll_send_skb(dev
, nskb
);
409 /* Queue the SKB for transmission on the parent interface, but
410 * do not modify its EtherType
412 nskb
->dev
= dsa_master_netdev(p
);
413 dev_queue_xmit(nskb
);
418 /* ethtool operations *******************************************************/
420 static void dsa_slave_get_drvinfo(struct net_device
*dev
,
421 struct ethtool_drvinfo
*drvinfo
)
423 strlcpy(drvinfo
->driver
, "dsa", sizeof(drvinfo
->driver
));
424 strlcpy(drvinfo
->fw_version
, "N/A", sizeof(drvinfo
->fw_version
));
425 strlcpy(drvinfo
->bus_info
, "platform", sizeof(drvinfo
->bus_info
));
428 static int dsa_slave_get_regs_len(struct net_device
*dev
)
430 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
431 struct dsa_switch
*ds
= dp
->ds
;
433 if (ds
->ops
->get_regs_len
)
434 return ds
->ops
->get_regs_len(ds
, dp
->index
);
440 dsa_slave_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *_p
)
442 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
443 struct dsa_switch
*ds
= dp
->ds
;
445 if (ds
->ops
->get_regs
)
446 ds
->ops
->get_regs(ds
, dp
->index
, regs
, _p
);
449 static u32
dsa_slave_get_link(struct net_device
*dev
)
454 genphy_update_link(dev
->phydev
);
456 return dev
->phydev
->link
;
459 static int dsa_slave_get_eeprom_len(struct net_device
*dev
)
461 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
462 struct dsa_switch
*ds
= dp
->ds
;
464 if (ds
->cd
&& ds
->cd
->eeprom_len
)
465 return ds
->cd
->eeprom_len
;
467 if (ds
->ops
->get_eeprom_len
)
468 return ds
->ops
->get_eeprom_len(ds
);
473 static int dsa_slave_get_eeprom(struct net_device
*dev
,
474 struct ethtool_eeprom
*eeprom
, u8
*data
)
476 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
477 struct dsa_switch
*ds
= dp
->ds
;
479 if (ds
->ops
->get_eeprom
)
480 return ds
->ops
->get_eeprom(ds
, eeprom
, data
);
485 static int dsa_slave_set_eeprom(struct net_device
*dev
,
486 struct ethtool_eeprom
*eeprom
, u8
*data
)
488 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
489 struct dsa_switch
*ds
= dp
->ds
;
491 if (ds
->ops
->set_eeprom
)
492 return ds
->ops
->set_eeprom(ds
, eeprom
, data
);
497 static void dsa_slave_get_strings(struct net_device
*dev
,
498 uint32_t stringset
, uint8_t *data
)
500 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
501 struct dsa_switch
*ds
= dp
->ds
;
503 if (stringset
== ETH_SS_STATS
) {
504 int len
= ETH_GSTRING_LEN
;
506 strncpy(data
, "tx_packets", len
);
507 strncpy(data
+ len
, "tx_bytes", len
);
508 strncpy(data
+ 2 * len
, "rx_packets", len
);
509 strncpy(data
+ 3 * len
, "rx_bytes", len
);
510 if (ds
->ops
->get_strings
)
511 ds
->ops
->get_strings(ds
, dp
->index
, data
+ 4 * len
);
515 static void dsa_slave_get_ethtool_stats(struct net_device
*dev
,
516 struct ethtool_stats
*stats
,
519 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
520 struct dsa_slave_priv
*p
= netdev_priv(dev
);
521 struct dsa_switch
*ds
= dp
->ds
;
522 struct pcpu_sw_netstats
*s
;
526 for_each_possible_cpu(i
) {
527 u64 tx_packets
, tx_bytes
, rx_packets
, rx_bytes
;
529 s
= per_cpu_ptr(p
->stats64
, i
);
531 start
= u64_stats_fetch_begin_irq(&s
->syncp
);
532 tx_packets
= s
->tx_packets
;
533 tx_bytes
= s
->tx_bytes
;
534 rx_packets
= s
->rx_packets
;
535 rx_bytes
= s
->rx_bytes
;
536 } while (u64_stats_fetch_retry_irq(&s
->syncp
, start
));
537 data
[0] += tx_packets
;
539 data
[2] += rx_packets
;
542 if (ds
->ops
->get_ethtool_stats
)
543 ds
->ops
->get_ethtool_stats(ds
, dp
->index
, data
+ 4);
546 static int dsa_slave_get_sset_count(struct net_device
*dev
, int sset
)
548 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
549 struct dsa_switch
*ds
= dp
->ds
;
551 if (sset
== ETH_SS_STATS
) {
555 if (ds
->ops
->get_sset_count
)
556 count
+= ds
->ops
->get_sset_count(ds
);
564 static void dsa_slave_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
566 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
567 struct dsa_switch
*ds
= dp
->ds
;
569 if (ds
->ops
->get_wol
)
570 ds
->ops
->get_wol(ds
, dp
->index
, w
);
573 static int dsa_slave_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
575 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
576 struct dsa_switch
*ds
= dp
->ds
;
577 int ret
= -EOPNOTSUPP
;
579 if (ds
->ops
->set_wol
)
580 ret
= ds
->ops
->set_wol(ds
, dp
->index
, w
);
585 static int dsa_slave_set_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
587 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
588 struct dsa_switch
*ds
= dp
->ds
;
591 /* Port's PHY and MAC both need to be EEE capable */
595 if (!ds
->ops
->set_mac_eee
)
598 ret
= ds
->ops
->set_mac_eee(ds
, dp
->index
, e
);
602 if (e
->eee_enabled
) {
603 ret
= phy_init_eee(dev
->phydev
, 0);
608 return phy_ethtool_set_eee(dev
->phydev
, e
);
611 static int dsa_slave_get_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
613 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
614 struct dsa_switch
*ds
= dp
->ds
;
617 /* Port's PHY and MAC both need to be EEE capable */
621 if (!ds
->ops
->get_mac_eee
)
624 ret
= ds
->ops
->get_mac_eee(ds
, dp
->index
, e
);
628 return phy_ethtool_get_eee(dev
->phydev
, e
);
631 #ifdef CONFIG_NET_POLL_CONTROLLER
632 static int dsa_slave_netpoll_setup(struct net_device
*dev
,
633 struct netpoll_info
*ni
)
635 struct dsa_slave_priv
*p
= netdev_priv(dev
);
636 struct net_device
*master
= dsa_master_netdev(p
);
637 struct netpoll
*netpoll
;
640 netpoll
= kzalloc(sizeof(*netpoll
), GFP_KERNEL
);
644 err
= __netpoll_setup(netpoll
, master
);
650 p
->netpoll
= netpoll
;
655 static void dsa_slave_netpoll_cleanup(struct net_device
*dev
)
657 struct dsa_slave_priv
*p
= netdev_priv(dev
);
658 struct netpoll
*netpoll
= p
->netpoll
;
665 __netpoll_free_async(netpoll
);
668 static void dsa_slave_poll_controller(struct net_device
*dev
)
673 static int dsa_slave_get_phys_port_name(struct net_device
*dev
,
674 char *name
, size_t len
)
676 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
678 if (snprintf(name
, len
, "p%d", dp
->index
) >= len
)
684 static struct dsa_mall_tc_entry
*
685 dsa_slave_mall_tc_entry_find(struct net_device
*dev
, unsigned long cookie
)
687 struct dsa_slave_priv
*p
= netdev_priv(dev
);
688 struct dsa_mall_tc_entry
*mall_tc_entry
;
690 list_for_each_entry(mall_tc_entry
, &p
->mall_tc_list
, list
)
691 if (mall_tc_entry
->cookie
== cookie
)
692 return mall_tc_entry
;
697 static int dsa_slave_add_cls_matchall(struct net_device
*dev
,
698 struct tc_cls_matchall_offload
*cls
,
701 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
702 struct dsa_slave_priv
*p
= netdev_priv(dev
);
703 struct dsa_mall_tc_entry
*mall_tc_entry
;
704 __be16 protocol
= cls
->common
.protocol
;
705 struct net
*net
= dev_net(dev
);
706 struct dsa_switch
*ds
= dp
->ds
;
707 struct net_device
*to_dev
;
708 const struct tc_action
*a
;
709 struct dsa_port
*to_dp
;
710 int err
= -EOPNOTSUPP
;
714 if (!ds
->ops
->port_mirror_add
)
717 if (!tcf_exts_has_one_action(cls
->exts
))
720 tcf_exts_to_list(cls
->exts
, &actions
);
721 a
= list_first_entry(&actions
, struct tc_action
, list
);
723 if (is_tcf_mirred_egress_mirror(a
) && protocol
== htons(ETH_P_ALL
)) {
724 struct dsa_mall_mirror_tc_entry
*mirror
;
726 ifindex
= tcf_mirred_ifindex(a
);
727 to_dev
= __dev_get_by_index(net
, ifindex
);
731 if (!dsa_slave_dev_check(to_dev
))
734 mall_tc_entry
= kzalloc(sizeof(*mall_tc_entry
), GFP_KERNEL
);
738 mall_tc_entry
->cookie
= cls
->cookie
;
739 mall_tc_entry
->type
= DSA_PORT_MALL_MIRROR
;
740 mirror
= &mall_tc_entry
->mirror
;
742 to_dp
= dsa_slave_to_port(to_dev
);
744 mirror
->to_local_port
= to_dp
->index
;
745 mirror
->ingress
= ingress
;
747 err
= ds
->ops
->port_mirror_add(ds
, dp
->index
, mirror
, ingress
);
749 kfree(mall_tc_entry
);
753 list_add_tail(&mall_tc_entry
->list
, &p
->mall_tc_list
);
759 static void dsa_slave_del_cls_matchall(struct net_device
*dev
,
760 struct tc_cls_matchall_offload
*cls
)
762 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
763 struct dsa_mall_tc_entry
*mall_tc_entry
;
764 struct dsa_switch
*ds
= dp
->ds
;
766 if (!ds
->ops
->port_mirror_del
)
769 mall_tc_entry
= dsa_slave_mall_tc_entry_find(dev
, cls
->cookie
);
773 list_del(&mall_tc_entry
->list
);
775 switch (mall_tc_entry
->type
) {
776 case DSA_PORT_MALL_MIRROR
:
777 ds
->ops
->port_mirror_del(ds
, dp
->index
, &mall_tc_entry
->mirror
);
783 kfree(mall_tc_entry
);
786 static int dsa_slave_setup_tc_cls_matchall(struct net_device
*dev
,
787 struct tc_cls_matchall_offload
*cls
)
791 if (is_classid_clsact_ingress(cls
->common
.classid
))
793 else if (is_classid_clsact_egress(cls
->common
.classid
))
798 if (cls
->common
.chain_index
)
801 switch (cls
->command
) {
802 case TC_CLSMATCHALL_REPLACE
:
803 return dsa_slave_add_cls_matchall(dev
, cls
, ingress
);
804 case TC_CLSMATCHALL_DESTROY
:
805 dsa_slave_del_cls_matchall(dev
, cls
);
812 static int dsa_slave_setup_tc(struct net_device
*dev
, enum tc_setup_type type
,
816 case TC_SETUP_CLSMATCHALL
:
817 return dsa_slave_setup_tc_cls_matchall(dev
, type_data
);
823 static void dsa_slave_get_stats64(struct net_device
*dev
,
824 struct rtnl_link_stats64
*stats
)
826 struct dsa_slave_priv
*p
= netdev_priv(dev
);
827 struct pcpu_sw_netstats
*s
;
831 netdev_stats_to_stats64(stats
, &dev
->stats
);
832 for_each_possible_cpu(i
) {
833 u64 tx_packets
, tx_bytes
, rx_packets
, rx_bytes
;
835 s
= per_cpu_ptr(p
->stats64
, i
);
837 start
= u64_stats_fetch_begin_irq(&s
->syncp
);
838 tx_packets
= s
->tx_packets
;
839 tx_bytes
= s
->tx_bytes
;
840 rx_packets
= s
->rx_packets
;
841 rx_bytes
= s
->rx_bytes
;
842 } while (u64_stats_fetch_retry_irq(&s
->syncp
, start
));
844 stats
->tx_packets
+= tx_packets
;
845 stats
->tx_bytes
+= tx_bytes
;
846 stats
->rx_packets
+= rx_packets
;
847 stats
->rx_bytes
+= rx_bytes
;
851 static int dsa_slave_get_rxnfc(struct net_device
*dev
,
852 struct ethtool_rxnfc
*nfc
, u32
*rule_locs
)
854 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
855 struct dsa_switch
*ds
= dp
->ds
;
857 if (!ds
->ops
->get_rxnfc
)
860 return ds
->ops
->get_rxnfc(ds
, dp
->index
, nfc
, rule_locs
);
863 static int dsa_slave_set_rxnfc(struct net_device
*dev
,
864 struct ethtool_rxnfc
*nfc
)
866 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
867 struct dsa_switch
*ds
= dp
->ds
;
869 if (!ds
->ops
->set_rxnfc
)
872 return ds
->ops
->set_rxnfc(ds
, dp
->index
, nfc
);
875 static const struct ethtool_ops dsa_slave_ethtool_ops
= {
876 .get_drvinfo
= dsa_slave_get_drvinfo
,
877 .get_regs_len
= dsa_slave_get_regs_len
,
878 .get_regs
= dsa_slave_get_regs
,
879 .nway_reset
= phy_ethtool_nway_reset
,
880 .get_link
= dsa_slave_get_link
,
881 .get_eeprom_len
= dsa_slave_get_eeprom_len
,
882 .get_eeprom
= dsa_slave_get_eeprom
,
883 .set_eeprom
= dsa_slave_set_eeprom
,
884 .get_strings
= dsa_slave_get_strings
,
885 .get_ethtool_stats
= dsa_slave_get_ethtool_stats
,
886 .get_sset_count
= dsa_slave_get_sset_count
,
887 .set_wol
= dsa_slave_set_wol
,
888 .get_wol
= dsa_slave_get_wol
,
889 .set_eee
= dsa_slave_set_eee
,
890 .get_eee
= dsa_slave_get_eee
,
891 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
892 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
893 .get_rxnfc
= dsa_slave_get_rxnfc
,
894 .set_rxnfc
= dsa_slave_set_rxnfc
,
897 static const struct net_device_ops dsa_slave_netdev_ops
= {
898 .ndo_open
= dsa_slave_open
,
899 .ndo_stop
= dsa_slave_close
,
900 .ndo_start_xmit
= dsa_slave_xmit
,
901 .ndo_change_rx_flags
= dsa_slave_change_rx_flags
,
902 .ndo_set_rx_mode
= dsa_slave_set_rx_mode
,
903 .ndo_set_mac_address
= dsa_slave_set_mac_address
,
904 .ndo_fdb_add
= dsa_legacy_fdb_add
,
905 .ndo_fdb_del
= dsa_legacy_fdb_del
,
906 .ndo_fdb_dump
= dsa_slave_fdb_dump
,
907 .ndo_do_ioctl
= dsa_slave_ioctl
,
908 .ndo_get_iflink
= dsa_slave_get_iflink
,
909 #ifdef CONFIG_NET_POLL_CONTROLLER
910 .ndo_netpoll_setup
= dsa_slave_netpoll_setup
,
911 .ndo_netpoll_cleanup
= dsa_slave_netpoll_cleanup
,
912 .ndo_poll_controller
= dsa_slave_poll_controller
,
914 .ndo_get_phys_port_name
= dsa_slave_get_phys_port_name
,
915 .ndo_setup_tc
= dsa_slave_setup_tc
,
916 .ndo_get_stats64
= dsa_slave_get_stats64
,
919 static const struct switchdev_ops dsa_slave_switchdev_ops
= {
920 .switchdev_port_attr_get
= dsa_slave_port_attr_get
,
921 .switchdev_port_attr_set
= dsa_slave_port_attr_set
,
922 .switchdev_port_obj_add
= dsa_slave_port_obj_add
,
923 .switchdev_port_obj_del
= dsa_slave_port_obj_del
,
926 static struct device_type dsa_type
= {
930 static void dsa_slave_adjust_link(struct net_device
*dev
)
932 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
933 struct dsa_slave_priv
*p
= netdev_priv(dev
);
934 struct dsa_switch
*ds
= dp
->ds
;
935 unsigned int status_changed
= 0;
937 if (p
->old_link
!= dev
->phydev
->link
) {
939 p
->old_link
= dev
->phydev
->link
;
942 if (p
->old_duplex
!= dev
->phydev
->duplex
) {
944 p
->old_duplex
= dev
->phydev
->duplex
;
947 if (p
->old_pause
!= dev
->phydev
->pause
) {
949 p
->old_pause
= dev
->phydev
->pause
;
952 if (ds
->ops
->adjust_link
&& status_changed
)
953 ds
->ops
->adjust_link(ds
, dp
->index
, dev
->phydev
);
956 phy_print_status(dev
->phydev
);
959 static int dsa_slave_fixed_link_update(struct net_device
*dev
,
960 struct fixed_phy_status
*status
)
962 struct dsa_switch
*ds
;
966 dp
= dsa_slave_to_port(dev
);
968 if (ds
->ops
->fixed_link_update
)
969 ds
->ops
->fixed_link_update(ds
, dp
->index
, status
);
975 /* slave device setup *******************************************************/
976 static int dsa_slave_phy_connect(struct net_device
*slave_dev
, int addr
)
978 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
979 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
980 struct dsa_switch
*ds
= dp
->ds
;
982 slave_dev
->phydev
= mdiobus_get_phy(ds
->slave_mii_bus
, addr
);
983 if (!slave_dev
->phydev
) {
984 netdev_err(slave_dev
, "no phy at %d\n", addr
);
988 /* Use already configured phy mode */
989 if (p
->phy_interface
== PHY_INTERFACE_MODE_NA
)
990 p
->phy_interface
= slave_dev
->phydev
->interface
;
992 return phy_connect_direct(slave_dev
, slave_dev
->phydev
,
993 dsa_slave_adjust_link
, p
->phy_interface
);
996 static int dsa_slave_phy_setup(struct net_device
*slave_dev
)
998 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
999 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1000 struct device_node
*port_dn
= dp
->dn
;
1001 struct dsa_switch
*ds
= dp
->ds
;
1002 struct device_node
*phy_dn
;
1003 bool phy_is_fixed
= false;
1007 mode
= of_get_phy_mode(port_dn
);
1009 mode
= PHY_INTERFACE_MODE_NA
;
1010 p
->phy_interface
= mode
;
1012 phy_dn
= of_parse_phandle(port_dn
, "phy-handle", 0);
1013 if (!phy_dn
&& of_phy_is_fixed_link(port_dn
)) {
1014 /* In the case of a fixed PHY, the DT node associated
1015 * to the fixed PHY is the Port DT node
1017 ret
= of_phy_register_fixed_link(port_dn
);
1019 netdev_err(slave_dev
, "failed to register fixed PHY: %d\n", ret
);
1022 phy_is_fixed
= true;
1023 phy_dn
= of_node_get(port_dn
);
1026 if (ds
->ops
->get_phy_flags
)
1027 phy_flags
= ds
->ops
->get_phy_flags(ds
, dp
->index
);
1030 int phy_id
= of_mdio_parse_addr(&slave_dev
->dev
, phy_dn
);
1032 /* If this PHY address is part of phys_mii_mask, which means
1033 * that we need to divert reads and writes to/from it, then we
1034 * want to bind this device using the slave MII bus created by
1035 * DSA to make that happen.
1037 if (!phy_is_fixed
&& phy_id
>= 0 &&
1038 (ds
->phys_mii_mask
& (1 << phy_id
))) {
1039 ret
= dsa_slave_phy_connect(slave_dev
, phy_id
);
1041 netdev_err(slave_dev
, "failed to connect to phy%d: %d\n", phy_id
, ret
);
1042 of_node_put(phy_dn
);
1046 slave_dev
->phydev
= of_phy_connect(slave_dev
, phy_dn
,
1047 dsa_slave_adjust_link
,
1052 of_node_put(phy_dn
);
1055 if (slave_dev
->phydev
&& phy_is_fixed
)
1056 fixed_phy_set_link_update(slave_dev
->phydev
,
1057 dsa_slave_fixed_link_update
);
1059 /* We could not connect to a designated PHY, so use the switch internal
1062 if (!slave_dev
->phydev
) {
1063 ret
= dsa_slave_phy_connect(slave_dev
, dp
->index
);
1065 netdev_err(slave_dev
, "failed to connect to port %d: %d\n",
1068 of_phy_deregister_fixed_link(port_dn
);
1073 phy_attached_info(slave_dev
->phydev
);
1078 static struct lock_class_key dsa_slave_netdev_xmit_lock_key
;
1079 static void dsa_slave_set_lockdep_class_one(struct net_device
*dev
,
1080 struct netdev_queue
*txq
,
1083 lockdep_set_class(&txq
->_xmit_lock
,
1084 &dsa_slave_netdev_xmit_lock_key
);
1087 int dsa_slave_suspend(struct net_device
*slave_dev
)
1089 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1091 netif_device_detach(slave_dev
);
1093 if (slave_dev
->phydev
) {
1094 phy_stop(slave_dev
->phydev
);
1098 phy_suspend(slave_dev
->phydev
);
1104 int dsa_slave_resume(struct net_device
*slave_dev
)
1106 netif_device_attach(slave_dev
);
1108 if (slave_dev
->phydev
) {
1109 phy_resume(slave_dev
->phydev
);
1110 phy_start(slave_dev
->phydev
);
1116 static void dsa_slave_notify(struct net_device
*dev
, unsigned long val
)
1118 struct dsa_slave_priv
*p
= netdev_priv(dev
);
1119 struct net_device
*master
= dsa_master_netdev(p
);
1120 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1121 struct dsa_notifier_register_info rinfo
= {
1122 .switch_number
= dp
->ds
->index
,
1123 .port_number
= dp
->index
,
1128 call_dsa_notifiers(val
, dev
, &rinfo
.info
);
1131 int dsa_slave_create(struct dsa_port
*port
, const char *name
)
1133 struct dsa_port
*cpu_dp
= port
->cpu_dp
;
1134 struct net_device
*master
= cpu_dp
->netdev
;
1135 struct dsa_switch
*ds
= port
->ds
;
1136 struct net_device
*slave_dev
;
1137 struct dsa_slave_priv
*p
;
1140 if (!ds
->num_tx_queues
)
1141 ds
->num_tx_queues
= 1;
1143 slave_dev
= alloc_netdev_mqs(sizeof(struct dsa_slave_priv
), name
,
1144 NET_NAME_UNKNOWN
, ether_setup
,
1145 ds
->num_tx_queues
, 1);
1146 if (slave_dev
== NULL
)
1149 slave_dev
->features
= master
->vlan_features
| NETIF_F_HW_TC
;
1150 slave_dev
->hw_features
|= NETIF_F_HW_TC
;
1151 slave_dev
->ethtool_ops
= &dsa_slave_ethtool_ops
;
1152 eth_hw_addr_inherit(slave_dev
, master
);
1153 slave_dev
->priv_flags
|= IFF_NO_QUEUE
;
1154 slave_dev
->netdev_ops
= &dsa_slave_netdev_ops
;
1155 slave_dev
->switchdev_ops
= &dsa_slave_switchdev_ops
;
1156 slave_dev
->min_mtu
= 0;
1157 slave_dev
->max_mtu
= ETH_MAX_MTU
;
1158 SET_NETDEV_DEVTYPE(slave_dev
, &dsa_type
);
1160 netdev_for_each_tx_queue(slave_dev
, dsa_slave_set_lockdep_class_one
,
1163 SET_NETDEV_DEV(slave_dev
, port
->ds
->dev
);
1164 slave_dev
->dev
.of_node
= port
->dn
;
1165 slave_dev
->vlan_features
= master
->vlan_features
;
1167 p
= netdev_priv(slave_dev
);
1168 p
->stats64
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
1170 free_netdev(slave_dev
);
1174 INIT_LIST_HEAD(&p
->mall_tc_list
);
1175 p
->xmit
= cpu_dp
->tag_ops
->xmit
;
1181 port
->netdev
= slave_dev
;
1183 netif_carrier_off(slave_dev
);
1185 ret
= dsa_slave_phy_setup(slave_dev
);
1187 netdev_err(master
, "error %d setting up slave phy\n", ret
);
1191 dsa_slave_notify(slave_dev
, DSA_PORT_REGISTER
);
1193 ret
= register_netdev(slave_dev
);
1195 netdev_err(master
, "error %d registering interface %s\n",
1196 ret
, slave_dev
->name
);
1203 phy_disconnect(slave_dev
->phydev
);
1204 if (of_phy_is_fixed_link(port
->dn
))
1205 of_phy_deregister_fixed_link(port
->dn
);
1207 free_percpu(p
->stats64
);
1208 free_netdev(slave_dev
);
1209 port
->netdev
= NULL
;
1213 void dsa_slave_destroy(struct net_device
*slave_dev
)
1215 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1216 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1217 struct device_node
*port_dn
= dp
->dn
;
1219 netif_carrier_off(slave_dev
);
1220 if (slave_dev
->phydev
) {
1221 phy_disconnect(slave_dev
->phydev
);
1223 if (of_phy_is_fixed_link(port_dn
))
1224 of_phy_deregister_fixed_link(port_dn
);
1226 dsa_slave_notify(slave_dev
, DSA_PORT_UNREGISTER
);
1227 unregister_netdev(slave_dev
);
1228 free_percpu(p
->stats64
);
1229 free_netdev(slave_dev
);
1232 static bool dsa_slave_dev_check(struct net_device
*dev
)
1234 return dev
->netdev_ops
== &dsa_slave_netdev_ops
;
1237 static int dsa_slave_changeupper(struct net_device
*dev
,
1238 struct netdev_notifier_changeupper_info
*info
)
1240 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1241 int err
= NOTIFY_DONE
;
1243 if (netif_is_bridge_master(info
->upper_dev
)) {
1244 if (info
->linking
) {
1245 err
= dsa_port_bridge_join(dp
, info
->upper_dev
);
1246 err
= notifier_from_errno(err
);
1248 dsa_port_bridge_leave(dp
, info
->upper_dev
);
1256 static int dsa_slave_netdevice_event(struct notifier_block
*nb
,
1257 unsigned long event
, void *ptr
)
1259 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1261 if (!dsa_slave_dev_check(dev
))
1264 if (event
== NETDEV_CHANGEUPPER
)
1265 return dsa_slave_changeupper(dev
, ptr
);
1270 struct dsa_switchdev_event_work
{
1271 struct work_struct work
;
1272 struct switchdev_notifier_fdb_info fdb_info
;
1273 struct net_device
*dev
;
1274 unsigned long event
;
1277 static void dsa_slave_switchdev_event_work(struct work_struct
*work
)
1279 struct dsa_switchdev_event_work
*switchdev_work
=
1280 container_of(work
, struct dsa_switchdev_event_work
, work
);
1281 struct net_device
*dev
= switchdev_work
->dev
;
1282 struct switchdev_notifier_fdb_info
*fdb_info
;
1283 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1287 switch (switchdev_work
->event
) {
1288 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
1289 fdb_info
= &switchdev_work
->fdb_info
;
1290 err
= dsa_port_fdb_add(dp
, fdb_info
->addr
, fdb_info
->vid
);
1292 netdev_dbg(dev
, "fdb add failed err=%d\n", err
);
1295 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED
, dev
,
1299 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1300 fdb_info
= &switchdev_work
->fdb_info
;
1301 err
= dsa_port_fdb_del(dp
, fdb_info
->addr
, fdb_info
->vid
);
1303 netdev_dbg(dev
, "fdb del failed err=%d\n", err
);
1310 kfree(switchdev_work
->fdb_info
.addr
);
1311 kfree(switchdev_work
);
1316 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work
*
1318 const struct switchdev_notifier_fdb_info
*
1321 memcpy(&switchdev_work
->fdb_info
, fdb_info
,
1322 sizeof(switchdev_work
->fdb_info
));
1323 switchdev_work
->fdb_info
.addr
= kzalloc(ETH_ALEN
, GFP_ATOMIC
);
1324 if (!switchdev_work
->fdb_info
.addr
)
1326 ether_addr_copy((u8
*)switchdev_work
->fdb_info
.addr
,
1331 /* Called under rcu_read_lock() */
1332 static int dsa_slave_switchdev_event(struct notifier_block
*unused
,
1333 unsigned long event
, void *ptr
)
1335 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
1336 struct dsa_switchdev_event_work
*switchdev_work
;
1338 if (!dsa_slave_dev_check(dev
))
1341 switchdev_work
= kzalloc(sizeof(*switchdev_work
), GFP_ATOMIC
);
1342 if (!switchdev_work
)
1345 INIT_WORK(&switchdev_work
->work
,
1346 dsa_slave_switchdev_event_work
);
1347 switchdev_work
->dev
= dev
;
1348 switchdev_work
->event
= event
;
1351 case SWITCHDEV_FDB_ADD_TO_DEVICE
: /* fall through */
1352 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1353 if (dsa_slave_switchdev_fdb_work_init(switchdev_work
,
1355 goto err_fdb_work_init
;
1359 kfree(switchdev_work
);
1363 dsa_schedule_work(&switchdev_work
->work
);
1367 kfree(switchdev_work
);
1371 static struct notifier_block dsa_slave_nb __read_mostly
= {
1372 .notifier_call
= dsa_slave_netdevice_event
,
1375 static struct notifier_block dsa_slave_switchdev_notifier
= {
1376 .notifier_call
= dsa_slave_switchdev_event
,
1379 int dsa_slave_register_notifier(void)
1383 err
= register_netdevice_notifier(&dsa_slave_nb
);
1387 err
= register_switchdev_notifier(&dsa_slave_switchdev_notifier
);
1389 goto err_switchdev_nb
;
1394 unregister_netdevice_notifier(&dsa_slave_nb
);
1398 void dsa_slave_unregister_notifier(void)
1402 err
= unregister_switchdev_notifier(&dsa_slave_switchdev_notifier
);
1404 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err
);
1406 err
= unregister_netdevice_notifier(&dsa_slave_nb
);
1408 pr_err("DSA: failed to unregister slave notifier (%d)\n", err
);