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 <net/rtnetlink.h>
20 #include <net/pkt_cls.h>
21 #include <net/tc_act/tc_mirred.h>
22 #include <linux/if_bridge.h>
23 #include <linux/netpoll.h>
27 static bool dsa_slave_dev_check(struct net_device
*dev
);
29 /* slave mii_bus handling ***************************************************/
30 static int dsa_slave_phy_read(struct mii_bus
*bus
, int addr
, int reg
)
32 struct dsa_switch
*ds
= bus
->priv
;
34 if (ds
->phys_mii_mask
& (1 << addr
))
35 return ds
->ops
->phy_read(ds
, addr
, reg
);
40 static int dsa_slave_phy_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
42 struct dsa_switch
*ds
= bus
->priv
;
44 if (ds
->phys_mii_mask
& (1 << addr
))
45 return ds
->ops
->phy_write(ds
, addr
, reg
, val
);
50 void dsa_slave_mii_bus_init(struct dsa_switch
*ds
)
52 ds
->slave_mii_bus
->priv
= (void *)ds
;
53 ds
->slave_mii_bus
->name
= "dsa slave smi";
54 ds
->slave_mii_bus
->read
= dsa_slave_phy_read
;
55 ds
->slave_mii_bus
->write
= dsa_slave_phy_write
;
56 snprintf(ds
->slave_mii_bus
->id
, MII_BUS_ID_SIZE
, "dsa-%d.%d",
57 ds
->dst
->index
, ds
->index
);
58 ds
->slave_mii_bus
->parent
= ds
->dev
;
59 ds
->slave_mii_bus
->phy_mask
= ~ds
->phys_mii_mask
;
63 /* slave device handling ****************************************************/
64 static int dsa_slave_get_iflink(const struct net_device
*dev
)
66 return dsa_slave_to_master(dev
)->ifindex
;
69 static int dsa_slave_open(struct net_device
*dev
)
71 struct net_device
*master
= dsa_slave_to_master(dev
);
72 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
75 if (!(master
->flags
& IFF_UP
))
78 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
)) {
79 err
= dev_uc_add(master
, dev
->dev_addr
);
84 if (dev
->flags
& IFF_ALLMULTI
) {
85 err
= dev_set_allmulti(master
, 1);
89 if (dev
->flags
& IFF_PROMISC
) {
90 err
= dev_set_promiscuity(master
, 1);
95 err
= dsa_port_enable(dp
, dev
->phydev
);
100 phy_start(dev
->phydev
);
105 if (dev
->flags
& IFF_PROMISC
)
106 dev_set_promiscuity(master
, -1);
108 if (dev
->flags
& IFF_ALLMULTI
)
109 dev_set_allmulti(master
, -1);
111 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
112 dev_uc_del(master
, dev
->dev_addr
);
117 static int dsa_slave_close(struct net_device
*dev
)
119 struct net_device
*master
= dsa_slave_to_master(dev
);
120 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
123 phy_stop(dev
->phydev
);
125 dsa_port_disable(dp
, dev
->phydev
);
127 dev_mc_unsync(master
, dev
);
128 dev_uc_unsync(master
, dev
);
129 if (dev
->flags
& IFF_ALLMULTI
)
130 dev_set_allmulti(master
, -1);
131 if (dev
->flags
& IFF_PROMISC
)
132 dev_set_promiscuity(master
, -1);
134 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
135 dev_uc_del(master
, dev
->dev_addr
);
140 static void dsa_slave_change_rx_flags(struct net_device
*dev
, int change
)
142 struct net_device
*master
= dsa_slave_to_master(dev
);
144 if (change
& IFF_ALLMULTI
)
145 dev_set_allmulti(master
, dev
->flags
& IFF_ALLMULTI
? 1 : -1);
146 if (change
& IFF_PROMISC
)
147 dev_set_promiscuity(master
, dev
->flags
& IFF_PROMISC
? 1 : -1);
150 static void dsa_slave_set_rx_mode(struct net_device
*dev
)
152 struct net_device
*master
= dsa_slave_to_master(dev
);
154 dev_mc_sync(master
, dev
);
155 dev_uc_sync(master
, dev
);
158 static int dsa_slave_set_mac_address(struct net_device
*dev
, void *a
)
160 struct net_device
*master
= dsa_slave_to_master(dev
);
161 struct sockaddr
*addr
= a
;
164 if (!is_valid_ether_addr(addr
->sa_data
))
165 return -EADDRNOTAVAIL
;
167 if (!(dev
->flags
& IFF_UP
))
170 if (!ether_addr_equal(addr
->sa_data
, master
->dev_addr
)) {
171 err
= dev_uc_add(master
, addr
->sa_data
);
176 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
177 dev_uc_del(master
, dev
->dev_addr
);
180 ether_addr_copy(dev
->dev_addr
, addr
->sa_data
);
185 struct dsa_slave_dump_ctx
{
186 struct net_device
*dev
;
188 struct netlink_callback
*cb
;
193 dsa_slave_port_fdb_do_dump(const unsigned char *addr
, u16 vid
,
194 bool is_static
, void *data
)
196 struct dsa_slave_dump_ctx
*dump
= data
;
197 u32 portid
= NETLINK_CB(dump
->cb
->skb
).portid
;
198 u32 seq
= dump
->cb
->nlh
->nlmsg_seq
;
199 struct nlmsghdr
*nlh
;
202 if (dump
->idx
< dump
->cb
->args
[2])
205 nlh
= nlmsg_put(dump
->skb
, portid
, seq
, RTM_NEWNEIGH
,
206 sizeof(*ndm
), NLM_F_MULTI
);
210 ndm
= nlmsg_data(nlh
);
211 ndm
->ndm_family
= AF_BRIDGE
;
214 ndm
->ndm_flags
= NTF_SELF
;
216 ndm
->ndm_ifindex
= dump
->dev
->ifindex
;
217 ndm
->ndm_state
= is_static
? NUD_NOARP
: NUD_REACHABLE
;
219 if (nla_put(dump
->skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
220 goto nla_put_failure
;
222 if (vid
&& nla_put_u16(dump
->skb
, NDA_VLAN
, vid
))
223 goto nla_put_failure
;
225 nlmsg_end(dump
->skb
, nlh
);
232 nlmsg_cancel(dump
->skb
, nlh
);
237 dsa_slave_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
238 struct net_device
*dev
, struct net_device
*filter_dev
,
241 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
242 struct dsa_slave_dump_ctx dump
= {
250 err
= dsa_port_fdb_dump(dp
, dsa_slave_port_fdb_do_dump
, &dump
);
256 static int dsa_slave_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
261 return phy_mii_ioctl(dev
->phydev
, ifr
, cmd
);
264 static int dsa_slave_port_attr_set(struct net_device
*dev
,
265 const struct switchdev_attr
*attr
,
266 struct switchdev_trans
*trans
)
268 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
272 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
273 ret
= dsa_port_set_state(dp
, attr
->u
.stp_state
, trans
);
275 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
:
276 ret
= dsa_port_vlan_filtering(dp
, attr
->u
.vlan_filtering
,
279 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
:
280 ret
= dsa_port_ageing_time(dp
, attr
->u
.ageing_time
, trans
);
290 static int dsa_slave_port_obj_add(struct net_device
*dev
,
291 const struct switchdev_obj
*obj
,
292 struct switchdev_trans
*trans
)
294 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
297 /* For the prepare phase, ensure the full set of changes is feasable in
298 * one go in order to signal a failure properly. If an operation is not
299 * supported, return -EOPNOTSUPP.
303 case SWITCHDEV_OBJ_ID_PORT_MDB
:
304 err
= dsa_port_mdb_add(dp
, SWITCHDEV_OBJ_PORT_MDB(obj
), trans
);
306 case SWITCHDEV_OBJ_ID_HOST_MDB
:
307 /* DSA can directly translate this to a normal MDB add,
308 * but on the CPU port.
310 err
= dsa_port_mdb_add(dp
->cpu_dp
, SWITCHDEV_OBJ_PORT_MDB(obj
),
313 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
314 err
= dsa_port_vlan_add(dp
, SWITCHDEV_OBJ_PORT_VLAN(obj
),
325 static int dsa_slave_port_obj_del(struct net_device
*dev
,
326 const struct switchdev_obj
*obj
)
328 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
332 case SWITCHDEV_OBJ_ID_PORT_MDB
:
333 err
= dsa_port_mdb_del(dp
, SWITCHDEV_OBJ_PORT_MDB(obj
));
335 case SWITCHDEV_OBJ_ID_HOST_MDB
:
336 /* DSA can directly translate this to a normal MDB add,
337 * but on the CPU port.
339 err
= dsa_port_mdb_del(dp
->cpu_dp
, SWITCHDEV_OBJ_PORT_MDB(obj
));
341 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
342 err
= dsa_port_vlan_del(dp
, SWITCHDEV_OBJ_PORT_VLAN(obj
));
352 static int dsa_slave_port_attr_get(struct net_device
*dev
,
353 struct switchdev_attr
*attr
)
355 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
356 struct dsa_switch
*ds
= dp
->ds
;
357 struct dsa_switch_tree
*dst
= ds
->dst
;
360 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID
:
361 attr
->u
.ppid
.id_len
= sizeof(dst
->index
);
362 memcpy(&attr
->u
.ppid
.id
, &dst
->index
, attr
->u
.ppid
.id_len
);
364 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT
:
365 attr
->u
.brport_flags_support
= 0;
374 static inline netdev_tx_t
dsa_slave_netpoll_send_skb(struct net_device
*dev
,
377 #ifdef CONFIG_NET_POLL_CONTROLLER
378 struct dsa_slave_priv
*p
= netdev_priv(dev
);
381 netpoll_send_skb(p
->netpoll
, skb
);
388 static netdev_tx_t
dsa_slave_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
390 struct dsa_slave_priv
*p
= netdev_priv(dev
);
391 struct pcpu_sw_netstats
*s
;
392 struct sk_buff
*nskb
;
394 s
= this_cpu_ptr(p
->stats64
);
395 u64_stats_update_begin(&s
->syncp
);
397 s
->tx_bytes
+= skb
->len
;
398 u64_stats_update_end(&s
->syncp
);
400 /* Transmit function may have to reallocate the original SKB,
401 * in which case it must have freed it. Only free it here on error.
403 nskb
= p
->xmit(skb
, dev
);
409 /* SKB for netpoll still need to be mangled with the protocol-specific
410 * tag to be successfully transmitted
412 if (unlikely(netpoll_tx_running(dev
)))
413 return dsa_slave_netpoll_send_skb(dev
, nskb
);
415 /* Queue the SKB for transmission on the parent interface, but
416 * do not modify its EtherType
418 nskb
->dev
= dsa_slave_to_master(dev
);
419 dev_queue_xmit(nskb
);
424 /* ethtool operations *******************************************************/
426 static void dsa_slave_get_drvinfo(struct net_device
*dev
,
427 struct ethtool_drvinfo
*drvinfo
)
429 strlcpy(drvinfo
->driver
, "dsa", sizeof(drvinfo
->driver
));
430 strlcpy(drvinfo
->fw_version
, "N/A", sizeof(drvinfo
->fw_version
));
431 strlcpy(drvinfo
->bus_info
, "platform", sizeof(drvinfo
->bus_info
));
434 static int dsa_slave_get_regs_len(struct net_device
*dev
)
436 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
437 struct dsa_switch
*ds
= dp
->ds
;
439 if (ds
->ops
->get_regs_len
)
440 return ds
->ops
->get_regs_len(ds
, dp
->index
);
446 dsa_slave_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *_p
)
448 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
449 struct dsa_switch
*ds
= dp
->ds
;
451 if (ds
->ops
->get_regs
)
452 ds
->ops
->get_regs(ds
, dp
->index
, regs
, _p
);
455 static u32
dsa_slave_get_link(struct net_device
*dev
)
460 genphy_update_link(dev
->phydev
);
462 return dev
->phydev
->link
;
465 static int dsa_slave_get_eeprom_len(struct net_device
*dev
)
467 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
468 struct dsa_switch
*ds
= dp
->ds
;
470 if (ds
->cd
&& ds
->cd
->eeprom_len
)
471 return ds
->cd
->eeprom_len
;
473 if (ds
->ops
->get_eeprom_len
)
474 return ds
->ops
->get_eeprom_len(ds
);
479 static int dsa_slave_get_eeprom(struct net_device
*dev
,
480 struct ethtool_eeprom
*eeprom
, u8
*data
)
482 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
483 struct dsa_switch
*ds
= dp
->ds
;
485 if (ds
->ops
->get_eeprom
)
486 return ds
->ops
->get_eeprom(ds
, eeprom
, data
);
491 static int dsa_slave_set_eeprom(struct net_device
*dev
,
492 struct ethtool_eeprom
*eeprom
, u8
*data
)
494 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
495 struct dsa_switch
*ds
= dp
->ds
;
497 if (ds
->ops
->set_eeprom
)
498 return ds
->ops
->set_eeprom(ds
, eeprom
, data
);
503 static void dsa_slave_get_strings(struct net_device
*dev
,
504 uint32_t stringset
, uint8_t *data
)
506 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
507 struct dsa_switch
*ds
= dp
->ds
;
509 if (stringset
== ETH_SS_STATS
) {
510 int len
= ETH_GSTRING_LEN
;
512 strncpy(data
, "tx_packets", len
);
513 strncpy(data
+ len
, "tx_bytes", len
);
514 strncpy(data
+ 2 * len
, "rx_packets", len
);
515 strncpy(data
+ 3 * len
, "rx_bytes", len
);
516 if (ds
->ops
->get_strings
)
517 ds
->ops
->get_strings(ds
, dp
->index
, data
+ 4 * len
);
521 static void dsa_slave_get_ethtool_stats(struct net_device
*dev
,
522 struct ethtool_stats
*stats
,
525 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
526 struct dsa_slave_priv
*p
= netdev_priv(dev
);
527 struct dsa_switch
*ds
= dp
->ds
;
528 struct pcpu_sw_netstats
*s
;
532 for_each_possible_cpu(i
) {
533 u64 tx_packets
, tx_bytes
, rx_packets
, rx_bytes
;
535 s
= per_cpu_ptr(p
->stats64
, i
);
537 start
= u64_stats_fetch_begin_irq(&s
->syncp
);
538 tx_packets
= s
->tx_packets
;
539 tx_bytes
= s
->tx_bytes
;
540 rx_packets
= s
->rx_packets
;
541 rx_bytes
= s
->rx_bytes
;
542 } while (u64_stats_fetch_retry_irq(&s
->syncp
, start
));
543 data
[0] += tx_packets
;
545 data
[2] += rx_packets
;
548 if (ds
->ops
->get_ethtool_stats
)
549 ds
->ops
->get_ethtool_stats(ds
, dp
->index
, data
+ 4);
552 static int dsa_slave_get_sset_count(struct net_device
*dev
, int sset
)
554 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
555 struct dsa_switch
*ds
= dp
->ds
;
557 if (sset
== ETH_SS_STATS
) {
561 if (ds
->ops
->get_sset_count
)
562 count
+= ds
->ops
->get_sset_count(ds
);
570 static void dsa_slave_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
572 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
573 struct dsa_switch
*ds
= dp
->ds
;
575 if (ds
->ops
->get_wol
)
576 ds
->ops
->get_wol(ds
, dp
->index
, w
);
579 static int dsa_slave_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
581 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
582 struct dsa_switch
*ds
= dp
->ds
;
583 int ret
= -EOPNOTSUPP
;
585 if (ds
->ops
->set_wol
)
586 ret
= ds
->ops
->set_wol(ds
, dp
->index
, w
);
591 static int dsa_slave_set_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
593 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
594 struct dsa_switch
*ds
= dp
->ds
;
597 /* Port's PHY and MAC both need to be EEE capable */
601 if (!ds
->ops
->set_mac_eee
)
604 ret
= ds
->ops
->set_mac_eee(ds
, dp
->index
, e
);
608 if (e
->eee_enabled
) {
609 ret
= phy_init_eee(dev
->phydev
, 0);
614 return phy_ethtool_set_eee(dev
->phydev
, e
);
617 static int dsa_slave_get_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
619 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
620 struct dsa_switch
*ds
= dp
->ds
;
623 /* Port's PHY and MAC both need to be EEE capable */
627 if (!ds
->ops
->get_mac_eee
)
630 ret
= ds
->ops
->get_mac_eee(ds
, dp
->index
, e
);
634 return phy_ethtool_get_eee(dev
->phydev
, e
);
637 #ifdef CONFIG_NET_POLL_CONTROLLER
638 static int dsa_slave_netpoll_setup(struct net_device
*dev
,
639 struct netpoll_info
*ni
)
641 struct net_device
*master
= dsa_slave_to_master(dev
);
642 struct dsa_slave_priv
*p
= netdev_priv(dev
);
643 struct netpoll
*netpoll
;
646 netpoll
= kzalloc(sizeof(*netpoll
), GFP_KERNEL
);
650 err
= __netpoll_setup(netpoll
, master
);
656 p
->netpoll
= netpoll
;
661 static void dsa_slave_netpoll_cleanup(struct net_device
*dev
)
663 struct dsa_slave_priv
*p
= netdev_priv(dev
);
664 struct netpoll
*netpoll
= p
->netpoll
;
671 __netpoll_free_async(netpoll
);
674 static void dsa_slave_poll_controller(struct net_device
*dev
)
679 static int dsa_slave_get_phys_port_name(struct net_device
*dev
,
680 char *name
, size_t len
)
682 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
684 if (snprintf(name
, len
, "p%d", dp
->index
) >= len
)
690 static struct dsa_mall_tc_entry
*
691 dsa_slave_mall_tc_entry_find(struct net_device
*dev
, unsigned long cookie
)
693 struct dsa_slave_priv
*p
= netdev_priv(dev
);
694 struct dsa_mall_tc_entry
*mall_tc_entry
;
696 list_for_each_entry(mall_tc_entry
, &p
->mall_tc_list
, list
)
697 if (mall_tc_entry
->cookie
== cookie
)
698 return mall_tc_entry
;
703 static int dsa_slave_add_cls_matchall(struct net_device
*dev
,
704 struct tc_cls_matchall_offload
*cls
,
707 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
708 struct dsa_slave_priv
*p
= netdev_priv(dev
);
709 struct dsa_mall_tc_entry
*mall_tc_entry
;
710 __be16 protocol
= cls
->common
.protocol
;
711 struct net
*net
= dev_net(dev
);
712 struct dsa_switch
*ds
= dp
->ds
;
713 struct net_device
*to_dev
;
714 const struct tc_action
*a
;
715 struct dsa_port
*to_dp
;
716 int err
= -EOPNOTSUPP
;
720 if (!ds
->ops
->port_mirror_add
)
723 if (!tcf_exts_has_one_action(cls
->exts
))
726 tcf_exts_to_list(cls
->exts
, &actions
);
727 a
= list_first_entry(&actions
, struct tc_action
, list
);
729 if (is_tcf_mirred_egress_mirror(a
) && protocol
== htons(ETH_P_ALL
)) {
730 struct dsa_mall_mirror_tc_entry
*mirror
;
732 ifindex
= tcf_mirred_ifindex(a
);
733 to_dev
= __dev_get_by_index(net
, ifindex
);
737 if (!dsa_slave_dev_check(to_dev
))
740 mall_tc_entry
= kzalloc(sizeof(*mall_tc_entry
), GFP_KERNEL
);
744 mall_tc_entry
->cookie
= cls
->cookie
;
745 mall_tc_entry
->type
= DSA_PORT_MALL_MIRROR
;
746 mirror
= &mall_tc_entry
->mirror
;
748 to_dp
= dsa_slave_to_port(to_dev
);
750 mirror
->to_local_port
= to_dp
->index
;
751 mirror
->ingress
= ingress
;
753 err
= ds
->ops
->port_mirror_add(ds
, dp
->index
, mirror
, ingress
);
755 kfree(mall_tc_entry
);
759 list_add_tail(&mall_tc_entry
->list
, &p
->mall_tc_list
);
765 static void dsa_slave_del_cls_matchall(struct net_device
*dev
,
766 struct tc_cls_matchall_offload
*cls
)
768 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
769 struct dsa_mall_tc_entry
*mall_tc_entry
;
770 struct dsa_switch
*ds
= dp
->ds
;
772 if (!ds
->ops
->port_mirror_del
)
775 mall_tc_entry
= dsa_slave_mall_tc_entry_find(dev
, cls
->cookie
);
779 list_del(&mall_tc_entry
->list
);
781 switch (mall_tc_entry
->type
) {
782 case DSA_PORT_MALL_MIRROR
:
783 ds
->ops
->port_mirror_del(ds
, dp
->index
, &mall_tc_entry
->mirror
);
789 kfree(mall_tc_entry
);
792 static int dsa_slave_setup_tc_cls_matchall(struct net_device
*dev
,
793 struct tc_cls_matchall_offload
*cls
,
796 if (cls
->common
.chain_index
)
799 switch (cls
->command
) {
800 case TC_CLSMATCHALL_REPLACE
:
801 return dsa_slave_add_cls_matchall(dev
, cls
, ingress
);
802 case TC_CLSMATCHALL_DESTROY
:
803 dsa_slave_del_cls_matchall(dev
, cls
);
810 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type
, void *type_data
,
811 void *cb_priv
, bool ingress
)
813 struct net_device
*dev
= cb_priv
;
815 if (!tc_can_offload(dev
))
819 case TC_SETUP_CLSMATCHALL
:
820 return dsa_slave_setup_tc_cls_matchall(dev
, type_data
, ingress
);
826 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type
,
827 void *type_data
, void *cb_priv
)
829 return dsa_slave_setup_tc_block_cb(type
, type_data
, cb_priv
, true);
832 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type
,
833 void *type_data
, void *cb_priv
)
835 return dsa_slave_setup_tc_block_cb(type
, type_data
, cb_priv
, false);
838 static int dsa_slave_setup_tc_block(struct net_device
*dev
,
839 struct tc_block_offload
*f
)
843 if (f
->binder_type
== TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
)
844 cb
= dsa_slave_setup_tc_block_cb_ig
;
845 else if (f
->binder_type
== TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS
)
846 cb
= dsa_slave_setup_tc_block_cb_eg
;
850 switch (f
->command
) {
852 return tcf_block_cb_register(f
->block
, cb
, dev
, dev
);
853 case TC_BLOCK_UNBIND
:
854 tcf_block_cb_unregister(f
->block
, cb
, dev
);
861 static int dsa_slave_setup_tc(struct net_device
*dev
, enum tc_setup_type type
,
866 return dsa_slave_setup_tc_block(dev
, type_data
);
872 static void dsa_slave_get_stats64(struct net_device
*dev
,
873 struct rtnl_link_stats64
*stats
)
875 struct dsa_slave_priv
*p
= netdev_priv(dev
);
876 struct pcpu_sw_netstats
*s
;
880 netdev_stats_to_stats64(stats
, &dev
->stats
);
881 for_each_possible_cpu(i
) {
882 u64 tx_packets
, tx_bytes
, rx_packets
, rx_bytes
;
884 s
= per_cpu_ptr(p
->stats64
, i
);
886 start
= u64_stats_fetch_begin_irq(&s
->syncp
);
887 tx_packets
= s
->tx_packets
;
888 tx_bytes
= s
->tx_bytes
;
889 rx_packets
= s
->rx_packets
;
890 rx_bytes
= s
->rx_bytes
;
891 } while (u64_stats_fetch_retry_irq(&s
->syncp
, start
));
893 stats
->tx_packets
+= tx_packets
;
894 stats
->tx_bytes
+= tx_bytes
;
895 stats
->rx_packets
+= rx_packets
;
896 stats
->rx_bytes
+= rx_bytes
;
900 static int dsa_slave_get_rxnfc(struct net_device
*dev
,
901 struct ethtool_rxnfc
*nfc
, u32
*rule_locs
)
903 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
904 struct dsa_switch
*ds
= dp
->ds
;
906 if (!ds
->ops
->get_rxnfc
)
909 return ds
->ops
->get_rxnfc(ds
, dp
->index
, nfc
, rule_locs
);
912 static int dsa_slave_set_rxnfc(struct net_device
*dev
,
913 struct ethtool_rxnfc
*nfc
)
915 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
916 struct dsa_switch
*ds
= dp
->ds
;
918 if (!ds
->ops
->set_rxnfc
)
921 return ds
->ops
->set_rxnfc(ds
, dp
->index
, nfc
);
924 static const struct ethtool_ops dsa_slave_ethtool_ops
= {
925 .get_drvinfo
= dsa_slave_get_drvinfo
,
926 .get_regs_len
= dsa_slave_get_regs_len
,
927 .get_regs
= dsa_slave_get_regs
,
928 .nway_reset
= phy_ethtool_nway_reset
,
929 .get_link
= dsa_slave_get_link
,
930 .get_eeprom_len
= dsa_slave_get_eeprom_len
,
931 .get_eeprom
= dsa_slave_get_eeprom
,
932 .set_eeprom
= dsa_slave_set_eeprom
,
933 .get_strings
= dsa_slave_get_strings
,
934 .get_ethtool_stats
= dsa_slave_get_ethtool_stats
,
935 .get_sset_count
= dsa_slave_get_sset_count
,
936 .set_wol
= dsa_slave_set_wol
,
937 .get_wol
= dsa_slave_get_wol
,
938 .set_eee
= dsa_slave_set_eee
,
939 .get_eee
= dsa_slave_get_eee
,
940 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
941 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
942 .get_rxnfc
= dsa_slave_get_rxnfc
,
943 .set_rxnfc
= dsa_slave_set_rxnfc
,
946 static const struct net_device_ops dsa_slave_netdev_ops
= {
947 .ndo_open
= dsa_slave_open
,
948 .ndo_stop
= dsa_slave_close
,
949 .ndo_start_xmit
= dsa_slave_xmit
,
950 .ndo_change_rx_flags
= dsa_slave_change_rx_flags
,
951 .ndo_set_rx_mode
= dsa_slave_set_rx_mode
,
952 .ndo_set_mac_address
= dsa_slave_set_mac_address
,
953 .ndo_fdb_add
= dsa_legacy_fdb_add
,
954 .ndo_fdb_del
= dsa_legacy_fdb_del
,
955 .ndo_fdb_dump
= dsa_slave_fdb_dump
,
956 .ndo_do_ioctl
= dsa_slave_ioctl
,
957 .ndo_get_iflink
= dsa_slave_get_iflink
,
958 #ifdef CONFIG_NET_POLL_CONTROLLER
959 .ndo_netpoll_setup
= dsa_slave_netpoll_setup
,
960 .ndo_netpoll_cleanup
= dsa_slave_netpoll_cleanup
,
961 .ndo_poll_controller
= dsa_slave_poll_controller
,
963 .ndo_get_phys_port_name
= dsa_slave_get_phys_port_name
,
964 .ndo_setup_tc
= dsa_slave_setup_tc
,
965 .ndo_get_stats64
= dsa_slave_get_stats64
,
968 static const struct switchdev_ops dsa_slave_switchdev_ops
= {
969 .switchdev_port_attr_get
= dsa_slave_port_attr_get
,
970 .switchdev_port_attr_set
= dsa_slave_port_attr_set
,
971 .switchdev_port_obj_add
= dsa_slave_port_obj_add
,
972 .switchdev_port_obj_del
= dsa_slave_port_obj_del
,
975 static struct device_type dsa_type
= {
979 static void dsa_slave_adjust_link(struct net_device
*dev
)
981 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
982 struct dsa_slave_priv
*p
= netdev_priv(dev
);
983 struct dsa_switch
*ds
= dp
->ds
;
984 unsigned int status_changed
= 0;
986 if (p
->old_link
!= dev
->phydev
->link
) {
988 p
->old_link
= dev
->phydev
->link
;
991 if (p
->old_duplex
!= dev
->phydev
->duplex
) {
993 p
->old_duplex
= dev
->phydev
->duplex
;
996 if (p
->old_pause
!= dev
->phydev
->pause
) {
998 p
->old_pause
= dev
->phydev
->pause
;
1001 if (ds
->ops
->adjust_link
&& status_changed
)
1002 ds
->ops
->adjust_link(ds
, dp
->index
, dev
->phydev
);
1005 phy_print_status(dev
->phydev
);
1008 static int dsa_slave_fixed_link_update(struct net_device
*dev
,
1009 struct fixed_phy_status
*status
)
1011 struct dsa_switch
*ds
;
1012 struct dsa_port
*dp
;
1015 dp
= dsa_slave_to_port(dev
);
1017 if (ds
->ops
->fixed_link_update
)
1018 ds
->ops
->fixed_link_update(ds
, dp
->index
, status
);
1024 /* slave device setup *******************************************************/
1025 static int dsa_slave_phy_connect(struct net_device
*slave_dev
, int addr
)
1027 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1028 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1029 struct dsa_switch
*ds
= dp
->ds
;
1031 slave_dev
->phydev
= mdiobus_get_phy(ds
->slave_mii_bus
, addr
);
1032 if (!slave_dev
->phydev
) {
1033 netdev_err(slave_dev
, "no phy at %d\n", addr
);
1037 /* Use already configured phy mode */
1038 if (p
->phy_interface
== PHY_INTERFACE_MODE_NA
)
1039 p
->phy_interface
= slave_dev
->phydev
->interface
;
1041 return phy_connect_direct(slave_dev
, slave_dev
->phydev
,
1042 dsa_slave_adjust_link
, p
->phy_interface
);
1045 static int dsa_slave_phy_setup(struct net_device
*slave_dev
)
1047 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1048 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1049 struct device_node
*port_dn
= dp
->dn
;
1050 struct dsa_switch
*ds
= dp
->ds
;
1051 struct device_node
*phy_dn
;
1052 bool phy_is_fixed
= false;
1056 mode
= of_get_phy_mode(port_dn
);
1058 mode
= PHY_INTERFACE_MODE_NA
;
1059 p
->phy_interface
= mode
;
1061 phy_dn
= of_parse_phandle(port_dn
, "phy-handle", 0);
1062 if (!phy_dn
&& of_phy_is_fixed_link(port_dn
)) {
1063 /* In the case of a fixed PHY, the DT node associated
1064 * to the fixed PHY is the Port DT node
1066 ret
= of_phy_register_fixed_link(port_dn
);
1068 netdev_err(slave_dev
, "failed to register fixed PHY: %d\n", ret
);
1071 phy_is_fixed
= true;
1072 phy_dn
= of_node_get(port_dn
);
1075 if (ds
->ops
->get_phy_flags
)
1076 phy_flags
= ds
->ops
->get_phy_flags(ds
, dp
->index
);
1079 slave_dev
->phydev
= of_phy_connect(slave_dev
, phy_dn
,
1080 dsa_slave_adjust_link
,
1083 of_node_put(phy_dn
);
1086 if (slave_dev
->phydev
&& phy_is_fixed
)
1087 fixed_phy_set_link_update(slave_dev
->phydev
,
1088 dsa_slave_fixed_link_update
);
1090 /* We could not connect to a designated PHY, so use the switch internal
1093 if (!slave_dev
->phydev
) {
1094 ret
= dsa_slave_phy_connect(slave_dev
, dp
->index
);
1096 netdev_err(slave_dev
, "failed to connect to port %d: %d\n",
1099 of_phy_deregister_fixed_link(port_dn
);
1104 phy_attached_info(slave_dev
->phydev
);
1109 static struct lock_class_key dsa_slave_netdev_xmit_lock_key
;
1110 static void dsa_slave_set_lockdep_class_one(struct net_device
*dev
,
1111 struct netdev_queue
*txq
,
1114 lockdep_set_class(&txq
->_xmit_lock
,
1115 &dsa_slave_netdev_xmit_lock_key
);
1118 int dsa_slave_suspend(struct net_device
*slave_dev
)
1120 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1122 if (!netif_running(slave_dev
))
1125 netif_device_detach(slave_dev
);
1127 if (slave_dev
->phydev
) {
1128 phy_stop(slave_dev
->phydev
);
1132 phy_suspend(slave_dev
->phydev
);
1138 int dsa_slave_resume(struct net_device
*slave_dev
)
1140 if (!netif_running(slave_dev
))
1143 netif_device_attach(slave_dev
);
1145 if (slave_dev
->phydev
) {
1146 phy_resume(slave_dev
->phydev
);
1147 phy_start(slave_dev
->phydev
);
1153 static void dsa_slave_notify(struct net_device
*dev
, unsigned long val
)
1155 struct net_device
*master
= dsa_slave_to_master(dev
);
1156 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1157 struct dsa_notifier_register_info rinfo
= {
1158 .switch_number
= dp
->ds
->index
,
1159 .port_number
= dp
->index
,
1164 call_dsa_notifiers(val
, dev
, &rinfo
.info
);
1167 int dsa_slave_create(struct dsa_port
*port
)
1169 const struct dsa_port
*cpu_dp
= port
->cpu_dp
;
1170 struct net_device
*master
= cpu_dp
->master
;
1171 struct dsa_switch
*ds
= port
->ds
;
1172 const char *name
= port
->name
;
1173 struct net_device
*slave_dev
;
1174 struct dsa_slave_priv
*p
;
1177 if (!ds
->num_tx_queues
)
1178 ds
->num_tx_queues
= 1;
1180 slave_dev
= alloc_netdev_mqs(sizeof(struct dsa_slave_priv
), name
,
1181 NET_NAME_UNKNOWN
, ether_setup
,
1182 ds
->num_tx_queues
, 1);
1183 if (slave_dev
== NULL
)
1186 slave_dev
->features
= master
->vlan_features
| NETIF_F_HW_TC
;
1187 slave_dev
->hw_features
|= NETIF_F_HW_TC
;
1188 slave_dev
->ethtool_ops
= &dsa_slave_ethtool_ops
;
1189 eth_hw_addr_inherit(slave_dev
, master
);
1190 slave_dev
->priv_flags
|= IFF_NO_QUEUE
;
1191 slave_dev
->netdev_ops
= &dsa_slave_netdev_ops
;
1192 slave_dev
->switchdev_ops
= &dsa_slave_switchdev_ops
;
1193 slave_dev
->min_mtu
= 0;
1194 slave_dev
->max_mtu
= ETH_MAX_MTU
;
1195 SET_NETDEV_DEVTYPE(slave_dev
, &dsa_type
);
1197 netdev_for_each_tx_queue(slave_dev
, dsa_slave_set_lockdep_class_one
,
1200 SET_NETDEV_DEV(slave_dev
, port
->ds
->dev
);
1201 slave_dev
->dev
.of_node
= port
->dn
;
1202 slave_dev
->vlan_features
= master
->vlan_features
;
1204 p
= netdev_priv(slave_dev
);
1205 p
->stats64
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
1207 free_netdev(slave_dev
);
1211 INIT_LIST_HEAD(&p
->mall_tc_list
);
1212 p
->xmit
= cpu_dp
->tag_ops
->xmit
;
1218 port
->slave
= slave_dev
;
1220 netif_carrier_off(slave_dev
);
1222 ret
= dsa_slave_phy_setup(slave_dev
);
1224 netdev_err(master
, "error %d setting up slave phy\n", ret
);
1228 dsa_slave_notify(slave_dev
, DSA_PORT_REGISTER
);
1230 ret
= register_netdev(slave_dev
);
1232 netdev_err(master
, "error %d registering interface %s\n",
1233 ret
, slave_dev
->name
);
1240 phy_disconnect(slave_dev
->phydev
);
1241 if (of_phy_is_fixed_link(port
->dn
))
1242 of_phy_deregister_fixed_link(port
->dn
);
1244 free_percpu(p
->stats64
);
1245 free_netdev(slave_dev
);
1250 void dsa_slave_destroy(struct net_device
*slave_dev
)
1252 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1253 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1254 struct device_node
*port_dn
= dp
->dn
;
1256 netif_carrier_off(slave_dev
);
1257 if (slave_dev
->phydev
) {
1258 phy_disconnect(slave_dev
->phydev
);
1260 if (of_phy_is_fixed_link(port_dn
))
1261 of_phy_deregister_fixed_link(port_dn
);
1263 dsa_slave_notify(slave_dev
, DSA_PORT_UNREGISTER
);
1264 unregister_netdev(slave_dev
);
1265 free_percpu(p
->stats64
);
1266 free_netdev(slave_dev
);
1269 static bool dsa_slave_dev_check(struct net_device
*dev
)
1271 return dev
->netdev_ops
== &dsa_slave_netdev_ops
;
1274 static int dsa_slave_changeupper(struct net_device
*dev
,
1275 struct netdev_notifier_changeupper_info
*info
)
1277 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1278 int err
= NOTIFY_DONE
;
1280 if (netif_is_bridge_master(info
->upper_dev
)) {
1281 if (info
->linking
) {
1282 err
= dsa_port_bridge_join(dp
, info
->upper_dev
);
1283 err
= notifier_from_errno(err
);
1285 dsa_port_bridge_leave(dp
, info
->upper_dev
);
1293 static int dsa_slave_netdevice_event(struct notifier_block
*nb
,
1294 unsigned long event
, void *ptr
)
1296 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1298 if (!dsa_slave_dev_check(dev
))
1301 if (event
== NETDEV_CHANGEUPPER
)
1302 return dsa_slave_changeupper(dev
, ptr
);
1307 struct dsa_switchdev_event_work
{
1308 struct work_struct work
;
1309 struct switchdev_notifier_fdb_info fdb_info
;
1310 struct net_device
*dev
;
1311 unsigned long event
;
1314 static void dsa_slave_switchdev_event_work(struct work_struct
*work
)
1316 struct dsa_switchdev_event_work
*switchdev_work
=
1317 container_of(work
, struct dsa_switchdev_event_work
, work
);
1318 struct net_device
*dev
= switchdev_work
->dev
;
1319 struct switchdev_notifier_fdb_info
*fdb_info
;
1320 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1324 switch (switchdev_work
->event
) {
1325 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
1326 fdb_info
= &switchdev_work
->fdb_info
;
1327 err
= dsa_port_fdb_add(dp
, fdb_info
->addr
, fdb_info
->vid
);
1329 netdev_dbg(dev
, "fdb add failed err=%d\n", err
);
1332 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED
, dev
,
1336 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1337 fdb_info
= &switchdev_work
->fdb_info
;
1338 err
= dsa_port_fdb_del(dp
, fdb_info
->addr
, fdb_info
->vid
);
1340 netdev_dbg(dev
, "fdb del failed err=%d\n", err
);
1347 kfree(switchdev_work
->fdb_info
.addr
);
1348 kfree(switchdev_work
);
1353 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work
*
1355 const struct switchdev_notifier_fdb_info
*
1358 memcpy(&switchdev_work
->fdb_info
, fdb_info
,
1359 sizeof(switchdev_work
->fdb_info
));
1360 switchdev_work
->fdb_info
.addr
= kzalloc(ETH_ALEN
, GFP_ATOMIC
);
1361 if (!switchdev_work
->fdb_info
.addr
)
1363 ether_addr_copy((u8
*)switchdev_work
->fdb_info
.addr
,
1368 /* Called under rcu_read_lock() */
1369 static int dsa_slave_switchdev_event(struct notifier_block
*unused
,
1370 unsigned long event
, void *ptr
)
1372 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
1373 struct dsa_switchdev_event_work
*switchdev_work
;
1375 if (!dsa_slave_dev_check(dev
))
1378 switchdev_work
= kzalloc(sizeof(*switchdev_work
), GFP_ATOMIC
);
1379 if (!switchdev_work
)
1382 INIT_WORK(&switchdev_work
->work
,
1383 dsa_slave_switchdev_event_work
);
1384 switchdev_work
->dev
= dev
;
1385 switchdev_work
->event
= event
;
1388 case SWITCHDEV_FDB_ADD_TO_DEVICE
: /* fall through */
1389 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1390 if (dsa_slave_switchdev_fdb_work_init(switchdev_work
,
1392 goto err_fdb_work_init
;
1396 kfree(switchdev_work
);
1400 dsa_schedule_work(&switchdev_work
->work
);
1404 kfree(switchdev_work
);
1408 static struct notifier_block dsa_slave_nb __read_mostly
= {
1409 .notifier_call
= dsa_slave_netdevice_event
,
1412 static struct notifier_block dsa_slave_switchdev_notifier
= {
1413 .notifier_call
= dsa_slave_switchdev_event
,
1416 int dsa_slave_register_notifier(void)
1420 err
= register_netdevice_notifier(&dsa_slave_nb
);
1424 err
= register_switchdev_notifier(&dsa_slave_switchdev_notifier
);
1426 goto err_switchdev_nb
;
1431 unregister_netdevice_notifier(&dsa_slave_nb
);
1435 void dsa_slave_unregister_notifier(void)
1439 err
= unregister_switchdev_notifier(&dsa_slave_switchdev_notifier
);
1441 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err
);
1443 err
= unregister_netdevice_notifier(&dsa_slave_nb
);
1445 pr_err("DSA: failed to unregister slave notifier (%d)\n", err
);