2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/list.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/phylink.h>
17 #include <linux/of_net.h>
18 #include <linux/of_mdio.h>
19 #include <linux/mdio.h>
20 #include <net/rtnetlink.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_mirred.h>
23 #include <linux/if_bridge.h>
24 #include <linux/netpoll.h>
25 #include <linux/ptp_classify.h>
29 static bool dsa_slave_dev_check(struct net_device
*dev
);
31 /* slave mii_bus handling ***************************************************/
32 static int dsa_slave_phy_read(struct mii_bus
*bus
, int addr
, int reg
)
34 struct dsa_switch
*ds
= bus
->priv
;
36 if (ds
->phys_mii_mask
& (1 << addr
))
37 return ds
->ops
->phy_read(ds
, addr
, reg
);
42 static int dsa_slave_phy_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
44 struct dsa_switch
*ds
= bus
->priv
;
46 if (ds
->phys_mii_mask
& (1 << addr
))
47 return ds
->ops
->phy_write(ds
, addr
, reg
, val
);
52 void dsa_slave_mii_bus_init(struct dsa_switch
*ds
)
54 ds
->slave_mii_bus
->priv
= (void *)ds
;
55 ds
->slave_mii_bus
->name
= "dsa slave smi";
56 ds
->slave_mii_bus
->read
= dsa_slave_phy_read
;
57 ds
->slave_mii_bus
->write
= dsa_slave_phy_write
;
58 snprintf(ds
->slave_mii_bus
->id
, MII_BUS_ID_SIZE
, "dsa-%d.%d",
59 ds
->dst
->index
, ds
->index
);
60 ds
->slave_mii_bus
->parent
= ds
->dev
;
61 ds
->slave_mii_bus
->phy_mask
= ~ds
->phys_mii_mask
;
65 /* slave device handling ****************************************************/
66 static int dsa_slave_get_iflink(const struct net_device
*dev
)
68 return dsa_slave_to_master(dev
)->ifindex
;
71 static int dsa_slave_open(struct net_device
*dev
)
73 struct net_device
*master
= dsa_slave_to_master(dev
);
74 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
77 if (!(master
->flags
& IFF_UP
))
80 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
)) {
81 err
= dev_uc_add(master
, dev
->dev_addr
);
86 if (dev
->flags
& IFF_ALLMULTI
) {
87 err
= dev_set_allmulti(master
, 1);
91 if (dev
->flags
& IFF_PROMISC
) {
92 err
= dev_set_promiscuity(master
, 1);
97 err
= dsa_port_enable(dp
, dev
->phydev
);
101 phylink_start(dp
->pl
);
106 if (dev
->flags
& IFF_PROMISC
)
107 dev_set_promiscuity(master
, -1);
109 if (dev
->flags
& IFF_ALLMULTI
)
110 dev_set_allmulti(master
, -1);
112 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
113 dev_uc_del(master
, dev
->dev_addr
);
118 static int dsa_slave_close(struct net_device
*dev
)
120 struct net_device
*master
= dsa_slave_to_master(dev
);
121 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
123 cancel_work_sync(&dp
->xmit_work
);
124 skb_queue_purge(&dp
->xmit_queue
);
126 phylink_stop(dp
->pl
);
128 dsa_port_disable(dp
);
130 dev_mc_unsync(master
, dev
);
131 dev_uc_unsync(master
, dev
);
132 if (dev
->flags
& IFF_ALLMULTI
)
133 dev_set_allmulti(master
, -1);
134 if (dev
->flags
& IFF_PROMISC
)
135 dev_set_promiscuity(master
, -1);
137 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
138 dev_uc_del(master
, dev
->dev_addr
);
143 static void dsa_slave_change_rx_flags(struct net_device
*dev
, int change
)
145 struct net_device
*master
= dsa_slave_to_master(dev
);
146 if (dev
->flags
& IFF_UP
) {
147 if (change
& IFF_ALLMULTI
)
148 dev_set_allmulti(master
,
149 dev
->flags
& IFF_ALLMULTI
? 1 : -1);
150 if (change
& IFF_PROMISC
)
151 dev_set_promiscuity(master
,
152 dev
->flags
& IFF_PROMISC
? 1 : -1);
156 static void dsa_slave_set_rx_mode(struct net_device
*dev
)
158 struct net_device
*master
= dsa_slave_to_master(dev
);
160 dev_mc_sync(master
, dev
);
161 dev_uc_sync(master
, dev
);
164 static int dsa_slave_set_mac_address(struct net_device
*dev
, void *a
)
166 struct net_device
*master
= dsa_slave_to_master(dev
);
167 struct sockaddr
*addr
= a
;
170 if (!is_valid_ether_addr(addr
->sa_data
))
171 return -EADDRNOTAVAIL
;
173 if (!(dev
->flags
& IFF_UP
))
176 if (!ether_addr_equal(addr
->sa_data
, master
->dev_addr
)) {
177 err
= dev_uc_add(master
, addr
->sa_data
);
182 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
183 dev_uc_del(master
, dev
->dev_addr
);
186 ether_addr_copy(dev
->dev_addr
, addr
->sa_data
);
191 struct dsa_slave_dump_ctx
{
192 struct net_device
*dev
;
194 struct netlink_callback
*cb
;
199 dsa_slave_port_fdb_do_dump(const unsigned char *addr
, u16 vid
,
200 bool is_static
, void *data
)
202 struct dsa_slave_dump_ctx
*dump
= data
;
203 u32 portid
= NETLINK_CB(dump
->cb
->skb
).portid
;
204 u32 seq
= dump
->cb
->nlh
->nlmsg_seq
;
205 struct nlmsghdr
*nlh
;
208 if (dump
->idx
< dump
->cb
->args
[2])
211 nlh
= nlmsg_put(dump
->skb
, portid
, seq
, RTM_NEWNEIGH
,
212 sizeof(*ndm
), NLM_F_MULTI
);
216 ndm
= nlmsg_data(nlh
);
217 ndm
->ndm_family
= AF_BRIDGE
;
220 ndm
->ndm_flags
= NTF_SELF
;
222 ndm
->ndm_ifindex
= dump
->dev
->ifindex
;
223 ndm
->ndm_state
= is_static
? NUD_NOARP
: NUD_REACHABLE
;
225 if (nla_put(dump
->skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
226 goto nla_put_failure
;
228 if (vid
&& nla_put_u16(dump
->skb
, NDA_VLAN
, vid
))
229 goto nla_put_failure
;
231 nlmsg_end(dump
->skb
, nlh
);
238 nlmsg_cancel(dump
->skb
, nlh
);
243 dsa_slave_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
244 struct net_device
*dev
, struct net_device
*filter_dev
,
247 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
248 struct dsa_slave_dump_ctx dump
= {
256 err
= dsa_port_fdb_dump(dp
, dsa_slave_port_fdb_do_dump
, &dump
);
262 static int dsa_slave_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
264 struct dsa_slave_priv
*p
= netdev_priv(dev
);
265 struct dsa_switch
*ds
= p
->dp
->ds
;
266 int port
= p
->dp
->index
;
268 /* Pass through to switch driver if it supports timestamping */
271 if (ds
->ops
->port_hwtstamp_get
)
272 return ds
->ops
->port_hwtstamp_get(ds
, port
, ifr
);
275 if (ds
->ops
->port_hwtstamp_set
)
276 return ds
->ops
->port_hwtstamp_set(ds
, port
, ifr
);
280 return phylink_mii_ioctl(p
->dp
->pl
, ifr
, cmd
);
283 static int dsa_slave_port_attr_set(struct net_device
*dev
,
284 const struct switchdev_attr
*attr
,
285 struct switchdev_trans
*trans
)
287 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
291 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
292 ret
= dsa_port_set_state(dp
, attr
->u
.stp_state
, trans
);
294 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
:
295 ret
= dsa_port_vlan_filtering(dp
, attr
->u
.vlan_filtering
,
298 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
:
299 ret
= dsa_port_ageing_time(dp
, attr
->u
.ageing_time
, trans
);
301 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS
:
302 ret
= dsa_port_pre_bridge_flags(dp
, attr
->u
.brport_flags
,
305 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
:
306 ret
= dsa_port_bridge_flags(dp
, attr
->u
.brport_flags
, trans
);
316 static int dsa_slave_port_obj_add(struct net_device
*dev
,
317 const struct switchdev_obj
*obj
,
318 struct switchdev_trans
*trans
)
320 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
323 /* For the prepare phase, ensure the full set of changes is feasable in
324 * one go in order to signal a failure properly. If an operation is not
325 * supported, return -EOPNOTSUPP.
329 case SWITCHDEV_OBJ_ID_PORT_MDB
:
330 err
= dsa_port_mdb_add(dp
, SWITCHDEV_OBJ_PORT_MDB(obj
), trans
);
332 case SWITCHDEV_OBJ_ID_HOST_MDB
:
333 /* DSA can directly translate this to a normal MDB add,
334 * but on the CPU port.
336 err
= dsa_port_mdb_add(dp
->cpu_dp
, SWITCHDEV_OBJ_PORT_MDB(obj
),
339 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
340 err
= dsa_port_vlan_add(dp
, SWITCHDEV_OBJ_PORT_VLAN(obj
),
351 static int dsa_slave_port_obj_del(struct net_device
*dev
,
352 const struct switchdev_obj
*obj
)
354 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
358 case SWITCHDEV_OBJ_ID_PORT_MDB
:
359 err
= dsa_port_mdb_del(dp
, SWITCHDEV_OBJ_PORT_MDB(obj
));
361 case SWITCHDEV_OBJ_ID_HOST_MDB
:
362 /* DSA can directly translate this to a normal MDB add,
363 * but on the CPU port.
365 err
= dsa_port_mdb_del(dp
->cpu_dp
, SWITCHDEV_OBJ_PORT_MDB(obj
));
367 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
368 err
= dsa_port_vlan_del(dp
, SWITCHDEV_OBJ_PORT_VLAN(obj
));
378 static int dsa_slave_get_port_parent_id(struct net_device
*dev
,
379 struct netdev_phys_item_id
*ppid
)
381 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
382 struct dsa_switch
*ds
= dp
->ds
;
383 struct dsa_switch_tree
*dst
= ds
->dst
;
385 /* For non-legacy ports, devlink is used and it takes
386 * care of the name generation. This ndo implementation
387 * should be removed with legacy support.
392 ppid
->id_len
= sizeof(dst
->index
);
393 memcpy(&ppid
->id
, &dst
->index
, ppid
->id_len
);
398 static inline netdev_tx_t
dsa_slave_netpoll_send_skb(struct net_device
*dev
,
401 #ifdef CONFIG_NET_POLL_CONTROLLER
402 struct dsa_slave_priv
*p
= netdev_priv(dev
);
405 netpoll_send_skb(p
->netpoll
, skb
);
412 static void dsa_skb_tx_timestamp(struct dsa_slave_priv
*p
,
415 struct dsa_switch
*ds
= p
->dp
->ds
;
416 struct sk_buff
*clone
;
419 type
= ptp_classify_raw(skb
);
420 if (type
== PTP_CLASS_NONE
)
423 if (!ds
->ops
->port_txtstamp
)
426 clone
= skb_clone_sk(skb
);
430 if (ds
->ops
->port_txtstamp(ds
, p
->dp
->index
, clone
, type
))
436 netdev_tx_t
dsa_enqueue_skb(struct sk_buff
*skb
, struct net_device
*dev
)
438 /* SKB for netpoll still need to be mangled with the protocol-specific
439 * tag to be successfully transmitted
441 if (unlikely(netpoll_tx_running(dev
)))
442 return dsa_slave_netpoll_send_skb(dev
, skb
);
444 /* Queue the SKB for transmission on the parent interface, but
445 * do not modify its EtherType
447 skb
->dev
= dsa_slave_to_master(dev
);
452 EXPORT_SYMBOL_GPL(dsa_enqueue_skb
);
454 static netdev_tx_t
dsa_slave_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
456 struct dsa_slave_priv
*p
= netdev_priv(dev
);
457 struct pcpu_sw_netstats
*s
;
458 struct sk_buff
*nskb
;
460 s
= this_cpu_ptr(p
->stats64
);
461 u64_stats_update_begin(&s
->syncp
);
463 s
->tx_bytes
+= skb
->len
;
464 u64_stats_update_end(&s
->syncp
);
466 /* Identify PTP protocol packets, clone them, and pass them to the
469 dsa_skb_tx_timestamp(p
, skb
);
471 /* Transmit function may have to reallocate the original SKB,
472 * in which case it must have freed it. Only free it here on error.
474 nskb
= p
->xmit(skb
, dev
);
476 if (!DSA_SKB_CB(skb
)->deferred_xmit
)
481 return dsa_enqueue_skb(nskb
, dev
);
484 void *dsa_defer_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
486 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
488 DSA_SKB_CB(skb
)->deferred_xmit
= true;
490 skb_queue_tail(&dp
->xmit_queue
, skb
);
491 schedule_work(&dp
->xmit_work
);
494 EXPORT_SYMBOL_GPL(dsa_defer_xmit
);
496 static void dsa_port_xmit_work(struct work_struct
*work
)
498 struct dsa_port
*dp
= container_of(work
, struct dsa_port
, xmit_work
);
499 struct dsa_switch
*ds
= dp
->ds
;
502 if (unlikely(!ds
->ops
->port_deferred_xmit
))
505 while ((skb
= skb_dequeue(&dp
->xmit_queue
)) != NULL
)
506 ds
->ops
->port_deferred_xmit(ds
, dp
->index
, skb
);
509 /* ethtool operations *******************************************************/
511 static void dsa_slave_get_drvinfo(struct net_device
*dev
,
512 struct ethtool_drvinfo
*drvinfo
)
514 strlcpy(drvinfo
->driver
, "dsa", sizeof(drvinfo
->driver
));
515 strlcpy(drvinfo
->fw_version
, "N/A", sizeof(drvinfo
->fw_version
));
516 strlcpy(drvinfo
->bus_info
, "platform", sizeof(drvinfo
->bus_info
));
519 static int dsa_slave_get_regs_len(struct net_device
*dev
)
521 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
522 struct dsa_switch
*ds
= dp
->ds
;
524 if (ds
->ops
->get_regs_len
)
525 return ds
->ops
->get_regs_len(ds
, dp
->index
);
531 dsa_slave_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *_p
)
533 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
534 struct dsa_switch
*ds
= dp
->ds
;
536 if (ds
->ops
->get_regs
)
537 ds
->ops
->get_regs(ds
, dp
->index
, regs
, _p
);
540 static int dsa_slave_nway_reset(struct net_device
*dev
)
542 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
544 return phylink_ethtool_nway_reset(dp
->pl
);
547 static int dsa_slave_get_eeprom_len(struct net_device
*dev
)
549 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
550 struct dsa_switch
*ds
= dp
->ds
;
552 if (ds
->cd
&& ds
->cd
->eeprom_len
)
553 return ds
->cd
->eeprom_len
;
555 if (ds
->ops
->get_eeprom_len
)
556 return ds
->ops
->get_eeprom_len(ds
);
561 static int dsa_slave_get_eeprom(struct net_device
*dev
,
562 struct ethtool_eeprom
*eeprom
, u8
*data
)
564 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
565 struct dsa_switch
*ds
= dp
->ds
;
567 if (ds
->ops
->get_eeprom
)
568 return ds
->ops
->get_eeprom(ds
, eeprom
, data
);
573 static int dsa_slave_set_eeprom(struct net_device
*dev
,
574 struct ethtool_eeprom
*eeprom
, u8
*data
)
576 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
577 struct dsa_switch
*ds
= dp
->ds
;
579 if (ds
->ops
->set_eeprom
)
580 return ds
->ops
->set_eeprom(ds
, eeprom
, data
);
585 static void dsa_slave_get_strings(struct net_device
*dev
,
586 uint32_t stringset
, uint8_t *data
)
588 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
589 struct dsa_switch
*ds
= dp
->ds
;
591 if (stringset
== ETH_SS_STATS
) {
592 int len
= ETH_GSTRING_LEN
;
594 strncpy(data
, "tx_packets", len
);
595 strncpy(data
+ len
, "tx_bytes", len
);
596 strncpy(data
+ 2 * len
, "rx_packets", len
);
597 strncpy(data
+ 3 * len
, "rx_bytes", len
);
598 if (ds
->ops
->get_strings
)
599 ds
->ops
->get_strings(ds
, dp
->index
, stringset
,
604 static void dsa_slave_get_ethtool_stats(struct net_device
*dev
,
605 struct ethtool_stats
*stats
,
608 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
609 struct dsa_slave_priv
*p
= netdev_priv(dev
);
610 struct dsa_switch
*ds
= dp
->ds
;
611 struct pcpu_sw_netstats
*s
;
615 for_each_possible_cpu(i
) {
616 u64 tx_packets
, tx_bytes
, rx_packets
, rx_bytes
;
618 s
= per_cpu_ptr(p
->stats64
, i
);
620 start
= u64_stats_fetch_begin_irq(&s
->syncp
);
621 tx_packets
= s
->tx_packets
;
622 tx_bytes
= s
->tx_bytes
;
623 rx_packets
= s
->rx_packets
;
624 rx_bytes
= s
->rx_bytes
;
625 } while (u64_stats_fetch_retry_irq(&s
->syncp
, start
));
626 data
[0] += tx_packets
;
628 data
[2] += rx_packets
;
631 if (ds
->ops
->get_ethtool_stats
)
632 ds
->ops
->get_ethtool_stats(ds
, dp
->index
, data
+ 4);
635 static int dsa_slave_get_sset_count(struct net_device
*dev
, int sset
)
637 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
638 struct dsa_switch
*ds
= dp
->ds
;
640 if (sset
== ETH_SS_STATS
) {
644 if (ds
->ops
->get_sset_count
)
645 count
+= ds
->ops
->get_sset_count(ds
, dp
->index
, sset
);
653 static void dsa_slave_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
655 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
656 struct dsa_switch
*ds
= dp
->ds
;
658 phylink_ethtool_get_wol(dp
->pl
, w
);
660 if (ds
->ops
->get_wol
)
661 ds
->ops
->get_wol(ds
, dp
->index
, w
);
664 static int dsa_slave_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
666 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
667 struct dsa_switch
*ds
= dp
->ds
;
668 int ret
= -EOPNOTSUPP
;
670 phylink_ethtool_set_wol(dp
->pl
, w
);
672 if (ds
->ops
->set_wol
)
673 ret
= ds
->ops
->set_wol(ds
, dp
->index
, w
);
678 static int dsa_slave_set_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
680 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
681 struct dsa_switch
*ds
= dp
->ds
;
684 /* Port's PHY and MAC both need to be EEE capable */
685 if (!dev
->phydev
|| !dp
->pl
)
688 if (!ds
->ops
->set_mac_eee
)
691 ret
= ds
->ops
->set_mac_eee(ds
, dp
->index
, e
);
695 return phylink_ethtool_set_eee(dp
->pl
, e
);
698 static int dsa_slave_get_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
700 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
701 struct dsa_switch
*ds
= dp
->ds
;
704 /* Port's PHY and MAC both need to be EEE capable */
705 if (!dev
->phydev
|| !dp
->pl
)
708 if (!ds
->ops
->get_mac_eee
)
711 ret
= ds
->ops
->get_mac_eee(ds
, dp
->index
, e
);
715 return phylink_ethtool_get_eee(dp
->pl
, e
);
718 static int dsa_slave_get_link_ksettings(struct net_device
*dev
,
719 struct ethtool_link_ksettings
*cmd
)
721 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
723 return phylink_ethtool_ksettings_get(dp
->pl
, cmd
);
726 static int dsa_slave_set_link_ksettings(struct net_device
*dev
,
727 const struct ethtool_link_ksettings
*cmd
)
729 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
731 return phylink_ethtool_ksettings_set(dp
->pl
, cmd
);
734 #ifdef CONFIG_NET_POLL_CONTROLLER
735 static int dsa_slave_netpoll_setup(struct net_device
*dev
,
736 struct netpoll_info
*ni
)
738 struct net_device
*master
= dsa_slave_to_master(dev
);
739 struct dsa_slave_priv
*p
= netdev_priv(dev
);
740 struct netpoll
*netpoll
;
743 netpoll
= kzalloc(sizeof(*netpoll
), GFP_KERNEL
);
747 err
= __netpoll_setup(netpoll
, master
);
753 p
->netpoll
= netpoll
;
758 static void dsa_slave_netpoll_cleanup(struct net_device
*dev
)
760 struct dsa_slave_priv
*p
= netdev_priv(dev
);
761 struct netpoll
*netpoll
= p
->netpoll
;
768 __netpoll_free(netpoll
);
771 static void dsa_slave_poll_controller(struct net_device
*dev
)
776 static int dsa_slave_get_phys_port_name(struct net_device
*dev
,
777 char *name
, size_t len
)
779 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
781 /* For non-legacy ports, devlink is used and it takes
782 * care of the name generation. This ndo implementation
783 * should be removed with legacy support.
788 if (snprintf(name
, len
, "p%d", dp
->index
) >= len
)
794 static struct dsa_mall_tc_entry
*
795 dsa_slave_mall_tc_entry_find(struct net_device
*dev
, unsigned long cookie
)
797 struct dsa_slave_priv
*p
= netdev_priv(dev
);
798 struct dsa_mall_tc_entry
*mall_tc_entry
;
800 list_for_each_entry(mall_tc_entry
, &p
->mall_tc_list
, list
)
801 if (mall_tc_entry
->cookie
== cookie
)
802 return mall_tc_entry
;
807 static int dsa_slave_add_cls_matchall(struct net_device
*dev
,
808 struct tc_cls_matchall_offload
*cls
,
811 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
812 struct dsa_slave_priv
*p
= netdev_priv(dev
);
813 struct dsa_mall_tc_entry
*mall_tc_entry
;
814 __be16 protocol
= cls
->common
.protocol
;
815 struct dsa_switch
*ds
= dp
->ds
;
816 struct flow_action_entry
*act
;
817 struct dsa_port
*to_dp
;
818 int err
= -EOPNOTSUPP
;
820 if (!ds
->ops
->port_mirror_add
)
823 if (!flow_offload_has_one_action(&cls
->rule
->action
))
826 act
= &cls
->rule
->action
.entries
[0];
828 if (act
->id
== FLOW_ACTION_MIRRED
&& protocol
== htons(ETH_P_ALL
)) {
829 struct dsa_mall_mirror_tc_entry
*mirror
;
834 if (!dsa_slave_dev_check(act
->dev
))
837 mall_tc_entry
= kzalloc(sizeof(*mall_tc_entry
), GFP_KERNEL
);
841 mall_tc_entry
->cookie
= cls
->cookie
;
842 mall_tc_entry
->type
= DSA_PORT_MALL_MIRROR
;
843 mirror
= &mall_tc_entry
->mirror
;
845 to_dp
= dsa_slave_to_port(act
->dev
);
847 mirror
->to_local_port
= to_dp
->index
;
848 mirror
->ingress
= ingress
;
850 err
= ds
->ops
->port_mirror_add(ds
, dp
->index
, mirror
, ingress
);
852 kfree(mall_tc_entry
);
856 list_add_tail(&mall_tc_entry
->list
, &p
->mall_tc_list
);
862 static void dsa_slave_del_cls_matchall(struct net_device
*dev
,
863 struct tc_cls_matchall_offload
*cls
)
865 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
866 struct dsa_mall_tc_entry
*mall_tc_entry
;
867 struct dsa_switch
*ds
= dp
->ds
;
869 if (!ds
->ops
->port_mirror_del
)
872 mall_tc_entry
= dsa_slave_mall_tc_entry_find(dev
, cls
->cookie
);
876 list_del(&mall_tc_entry
->list
);
878 switch (mall_tc_entry
->type
) {
879 case DSA_PORT_MALL_MIRROR
:
880 ds
->ops
->port_mirror_del(ds
, dp
->index
, &mall_tc_entry
->mirror
);
886 kfree(mall_tc_entry
);
889 static int dsa_slave_setup_tc_cls_matchall(struct net_device
*dev
,
890 struct tc_cls_matchall_offload
*cls
,
893 if (cls
->common
.chain_index
)
896 switch (cls
->command
) {
897 case TC_CLSMATCHALL_REPLACE
:
898 return dsa_slave_add_cls_matchall(dev
, cls
, ingress
);
899 case TC_CLSMATCHALL_DESTROY
:
900 dsa_slave_del_cls_matchall(dev
, cls
);
907 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type
, void *type_data
,
908 void *cb_priv
, bool ingress
)
910 struct net_device
*dev
= cb_priv
;
912 if (!tc_can_offload(dev
))
916 case TC_SETUP_CLSMATCHALL
:
917 return dsa_slave_setup_tc_cls_matchall(dev
, type_data
, ingress
);
923 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type
,
924 void *type_data
, void *cb_priv
)
926 return dsa_slave_setup_tc_block_cb(type
, type_data
, cb_priv
, true);
929 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type
,
930 void *type_data
, void *cb_priv
)
932 return dsa_slave_setup_tc_block_cb(type
, type_data
, cb_priv
, false);
935 static int dsa_slave_setup_tc_block(struct net_device
*dev
,
936 struct tc_block_offload
*f
)
940 if (f
->binder_type
== TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
)
941 cb
= dsa_slave_setup_tc_block_cb_ig
;
942 else if (f
->binder_type
== TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS
)
943 cb
= dsa_slave_setup_tc_block_cb_eg
;
947 switch (f
->command
) {
949 return tcf_block_cb_register(f
->block
, cb
, dev
, dev
, f
->extack
);
950 case TC_BLOCK_UNBIND
:
951 tcf_block_cb_unregister(f
->block
, cb
, dev
);
958 static int dsa_slave_setup_tc(struct net_device
*dev
, enum tc_setup_type type
,
963 return dsa_slave_setup_tc_block(dev
, type_data
);
969 static void dsa_slave_get_stats64(struct net_device
*dev
,
970 struct rtnl_link_stats64
*stats
)
972 struct dsa_slave_priv
*p
= netdev_priv(dev
);
973 struct pcpu_sw_netstats
*s
;
977 netdev_stats_to_stats64(stats
, &dev
->stats
);
978 for_each_possible_cpu(i
) {
979 u64 tx_packets
, tx_bytes
, rx_packets
, rx_bytes
;
981 s
= per_cpu_ptr(p
->stats64
, i
);
983 start
= u64_stats_fetch_begin_irq(&s
->syncp
);
984 tx_packets
= s
->tx_packets
;
985 tx_bytes
= s
->tx_bytes
;
986 rx_packets
= s
->rx_packets
;
987 rx_bytes
= s
->rx_bytes
;
988 } while (u64_stats_fetch_retry_irq(&s
->syncp
, start
));
990 stats
->tx_packets
+= tx_packets
;
991 stats
->tx_bytes
+= tx_bytes
;
992 stats
->rx_packets
+= rx_packets
;
993 stats
->rx_bytes
+= rx_bytes
;
997 static int dsa_slave_get_rxnfc(struct net_device
*dev
,
998 struct ethtool_rxnfc
*nfc
, u32
*rule_locs
)
1000 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1001 struct dsa_switch
*ds
= dp
->ds
;
1003 if (!ds
->ops
->get_rxnfc
)
1006 return ds
->ops
->get_rxnfc(ds
, dp
->index
, nfc
, rule_locs
);
1009 static int dsa_slave_set_rxnfc(struct net_device
*dev
,
1010 struct ethtool_rxnfc
*nfc
)
1012 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1013 struct dsa_switch
*ds
= dp
->ds
;
1015 if (!ds
->ops
->set_rxnfc
)
1018 return ds
->ops
->set_rxnfc(ds
, dp
->index
, nfc
);
1021 static int dsa_slave_get_ts_info(struct net_device
*dev
,
1022 struct ethtool_ts_info
*ts
)
1024 struct dsa_slave_priv
*p
= netdev_priv(dev
);
1025 struct dsa_switch
*ds
= p
->dp
->ds
;
1027 if (!ds
->ops
->get_ts_info
)
1030 return ds
->ops
->get_ts_info(ds
, p
->dp
->index
, ts
);
1033 static int dsa_slave_vlan_rx_add_vid(struct net_device
*dev
, __be16 proto
,
1036 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1037 struct bridge_vlan_info info
;
1040 /* Check for a possible bridge VLAN entry now since there is no
1041 * need to emulate the switchdev prepare + commit phase.
1043 if (dp
->bridge_dev
) {
1044 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1045 * device, respectively the VID is not found, returning
1046 * 0 means success, which is a failure for us here.
1048 ret
= br_vlan_get_info(dp
->bridge_dev
, vid
, &info
);
1053 /* This API only allows programming tagged, non-PVID VIDs */
1054 return dsa_port_vid_add(dp
, vid
, 0);
1057 static int dsa_slave_vlan_rx_kill_vid(struct net_device
*dev
, __be16 proto
,
1060 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1061 struct bridge_vlan_info info
;
1064 /* Check for a possible bridge VLAN entry now since there is no
1065 * need to emulate the switchdev prepare + commit phase.
1067 if (dp
->bridge_dev
) {
1068 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1069 * device, respectively the VID is not found, returning
1070 * 0 means success, which is a failure for us here.
1072 ret
= br_vlan_get_info(dp
->bridge_dev
, vid
, &info
);
1077 ret
= dsa_port_vid_del(dp
, vid
);
1078 if (ret
== -EOPNOTSUPP
)
1084 static const struct ethtool_ops dsa_slave_ethtool_ops
= {
1085 .get_drvinfo
= dsa_slave_get_drvinfo
,
1086 .get_regs_len
= dsa_slave_get_regs_len
,
1087 .get_regs
= dsa_slave_get_regs
,
1088 .nway_reset
= dsa_slave_nway_reset
,
1089 .get_link
= ethtool_op_get_link
,
1090 .get_eeprom_len
= dsa_slave_get_eeprom_len
,
1091 .get_eeprom
= dsa_slave_get_eeprom
,
1092 .set_eeprom
= dsa_slave_set_eeprom
,
1093 .get_strings
= dsa_slave_get_strings
,
1094 .get_ethtool_stats
= dsa_slave_get_ethtool_stats
,
1095 .get_sset_count
= dsa_slave_get_sset_count
,
1096 .set_wol
= dsa_slave_set_wol
,
1097 .get_wol
= dsa_slave_get_wol
,
1098 .set_eee
= dsa_slave_set_eee
,
1099 .get_eee
= dsa_slave_get_eee
,
1100 .get_link_ksettings
= dsa_slave_get_link_ksettings
,
1101 .set_link_ksettings
= dsa_slave_set_link_ksettings
,
1102 .get_rxnfc
= dsa_slave_get_rxnfc
,
1103 .set_rxnfc
= dsa_slave_set_rxnfc
,
1104 .get_ts_info
= dsa_slave_get_ts_info
,
1107 /* legacy way, bypassing the bridge *****************************************/
1108 int dsa_legacy_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
1109 struct net_device
*dev
,
1110 const unsigned char *addr
, u16 vid
,
1112 struct netlink_ext_ack
*extack
)
1114 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1116 return dsa_port_fdb_add(dp
, addr
, vid
);
1119 int dsa_legacy_fdb_del(struct ndmsg
*ndm
, struct nlattr
*tb
[],
1120 struct net_device
*dev
,
1121 const unsigned char *addr
, u16 vid
)
1123 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1125 return dsa_port_fdb_del(dp
, addr
, vid
);
1128 static struct devlink_port
*dsa_slave_get_devlink_port(struct net_device
*dev
)
1130 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1132 return dp
->ds
->devlink
? &dp
->devlink_port
: NULL
;
1135 static const struct net_device_ops dsa_slave_netdev_ops
= {
1136 .ndo_open
= dsa_slave_open
,
1137 .ndo_stop
= dsa_slave_close
,
1138 .ndo_start_xmit
= dsa_slave_xmit
,
1139 .ndo_change_rx_flags
= dsa_slave_change_rx_flags
,
1140 .ndo_set_rx_mode
= dsa_slave_set_rx_mode
,
1141 .ndo_set_mac_address
= dsa_slave_set_mac_address
,
1142 .ndo_fdb_add
= dsa_legacy_fdb_add
,
1143 .ndo_fdb_del
= dsa_legacy_fdb_del
,
1144 .ndo_fdb_dump
= dsa_slave_fdb_dump
,
1145 .ndo_do_ioctl
= dsa_slave_ioctl
,
1146 .ndo_get_iflink
= dsa_slave_get_iflink
,
1147 #ifdef CONFIG_NET_POLL_CONTROLLER
1148 .ndo_netpoll_setup
= dsa_slave_netpoll_setup
,
1149 .ndo_netpoll_cleanup
= dsa_slave_netpoll_cleanup
,
1150 .ndo_poll_controller
= dsa_slave_poll_controller
,
1152 .ndo_get_phys_port_name
= dsa_slave_get_phys_port_name
,
1153 .ndo_setup_tc
= dsa_slave_setup_tc
,
1154 .ndo_get_stats64
= dsa_slave_get_stats64
,
1155 .ndo_get_port_parent_id
= dsa_slave_get_port_parent_id
,
1156 .ndo_vlan_rx_add_vid
= dsa_slave_vlan_rx_add_vid
,
1157 .ndo_vlan_rx_kill_vid
= dsa_slave_vlan_rx_kill_vid
,
1158 .ndo_get_devlink_port
= dsa_slave_get_devlink_port
,
1161 static struct device_type dsa_type
= {
1165 static void dsa_slave_phylink_validate(struct net_device
*dev
,
1166 unsigned long *supported
,
1167 struct phylink_link_state
*state
)
1169 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1170 struct dsa_switch
*ds
= dp
->ds
;
1172 if (!ds
->ops
->phylink_validate
)
1175 ds
->ops
->phylink_validate(ds
, dp
->index
, supported
, state
);
1178 static int dsa_slave_phylink_mac_link_state(struct net_device
*dev
,
1179 struct phylink_link_state
*state
)
1181 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1182 struct dsa_switch
*ds
= dp
->ds
;
1184 /* Only called for SGMII and 802.3z */
1185 if (!ds
->ops
->phylink_mac_link_state
)
1188 return ds
->ops
->phylink_mac_link_state(ds
, dp
->index
, state
);
1191 static void dsa_slave_phylink_mac_config(struct net_device
*dev
,
1193 const struct phylink_link_state
*state
)
1195 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1196 struct dsa_switch
*ds
= dp
->ds
;
1198 if (!ds
->ops
->phylink_mac_config
)
1201 ds
->ops
->phylink_mac_config(ds
, dp
->index
, mode
, state
);
1204 static void dsa_slave_phylink_mac_an_restart(struct net_device
*dev
)
1206 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1207 struct dsa_switch
*ds
= dp
->ds
;
1209 if (!ds
->ops
->phylink_mac_an_restart
)
1212 ds
->ops
->phylink_mac_an_restart(ds
, dp
->index
);
1215 static void dsa_slave_phylink_mac_link_down(struct net_device
*dev
,
1217 phy_interface_t interface
)
1219 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1220 struct dsa_switch
*ds
= dp
->ds
;
1222 if (!ds
->ops
->phylink_mac_link_down
) {
1223 if (ds
->ops
->adjust_link
&& dev
->phydev
)
1224 ds
->ops
->adjust_link(ds
, dp
->index
, dev
->phydev
);
1228 ds
->ops
->phylink_mac_link_down(ds
, dp
->index
, mode
, interface
);
1231 static void dsa_slave_phylink_mac_link_up(struct net_device
*dev
,
1233 phy_interface_t interface
,
1234 struct phy_device
*phydev
)
1236 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1237 struct dsa_switch
*ds
= dp
->ds
;
1239 if (!ds
->ops
->phylink_mac_link_up
) {
1240 if (ds
->ops
->adjust_link
&& dev
->phydev
)
1241 ds
->ops
->adjust_link(ds
, dp
->index
, dev
->phydev
);
1245 ds
->ops
->phylink_mac_link_up(ds
, dp
->index
, mode
, interface
, phydev
);
1248 static const struct phylink_mac_ops dsa_slave_phylink_mac_ops
= {
1249 .validate
= dsa_slave_phylink_validate
,
1250 .mac_link_state
= dsa_slave_phylink_mac_link_state
,
1251 .mac_config
= dsa_slave_phylink_mac_config
,
1252 .mac_an_restart
= dsa_slave_phylink_mac_an_restart
,
1253 .mac_link_down
= dsa_slave_phylink_mac_link_down
,
1254 .mac_link_up
= dsa_slave_phylink_mac_link_up
,
1257 void dsa_port_phylink_mac_change(struct dsa_switch
*ds
, int port
, bool up
)
1259 const struct dsa_port
*dp
= dsa_to_port(ds
, port
);
1261 phylink_mac_change(dp
->pl
, up
);
1263 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change
);
1265 static void dsa_slave_phylink_fixed_state(struct net_device
*dev
,
1266 struct phylink_link_state
*state
)
1268 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1269 struct dsa_switch
*ds
= dp
->ds
;
1271 /* No need to check that this operation is valid, the callback would
1272 * not be called if it was not.
1274 ds
->ops
->phylink_fixed_state(ds
, dp
->index
, state
);
1277 /* slave device setup *******************************************************/
1278 static int dsa_slave_phy_connect(struct net_device
*slave_dev
, int addr
)
1280 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1281 struct dsa_switch
*ds
= dp
->ds
;
1283 slave_dev
->phydev
= mdiobus_get_phy(ds
->slave_mii_bus
, addr
);
1284 if (!slave_dev
->phydev
) {
1285 netdev_err(slave_dev
, "no phy at %d\n", addr
);
1289 return phylink_connect_phy(dp
->pl
, slave_dev
->phydev
);
1292 static int dsa_slave_phy_setup(struct net_device
*slave_dev
)
1294 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1295 struct device_node
*port_dn
= dp
->dn
;
1296 struct dsa_switch
*ds
= dp
->ds
;
1300 mode
= of_get_phy_mode(port_dn
);
1302 mode
= PHY_INTERFACE_MODE_NA
;
1304 dp
->pl
= phylink_create(slave_dev
, of_fwnode_handle(port_dn
), mode
,
1305 &dsa_slave_phylink_mac_ops
);
1306 if (IS_ERR(dp
->pl
)) {
1307 netdev_err(slave_dev
,
1308 "error creating PHYLINK: %ld\n", PTR_ERR(dp
->pl
));
1309 return PTR_ERR(dp
->pl
);
1312 /* Register only if the switch provides such a callback, since this
1313 * callback takes precedence over polling the link GPIO in PHYLINK
1314 * (see phylink_get_fixed_state).
1316 if (ds
->ops
->phylink_fixed_state
)
1317 phylink_fixed_state_cb(dp
->pl
, dsa_slave_phylink_fixed_state
);
1319 if (ds
->ops
->get_phy_flags
)
1320 phy_flags
= ds
->ops
->get_phy_flags(ds
, dp
->index
);
1322 ret
= phylink_of_phy_connect(dp
->pl
, port_dn
, phy_flags
);
1323 if (ret
== -ENODEV
&& ds
->slave_mii_bus
) {
1324 /* We could not connect to a designated PHY or SFP, so try to
1325 * use the switch internal MDIO bus instead
1327 ret
= dsa_slave_phy_connect(slave_dev
, dp
->index
);
1329 netdev_err(slave_dev
,
1330 "failed to connect to port %d: %d\n",
1332 phylink_destroy(dp
->pl
);
1340 static struct lock_class_key dsa_slave_netdev_xmit_lock_key
;
1341 static void dsa_slave_set_lockdep_class_one(struct net_device
*dev
,
1342 struct netdev_queue
*txq
,
1345 lockdep_set_class(&txq
->_xmit_lock
,
1346 &dsa_slave_netdev_xmit_lock_key
);
1349 int dsa_slave_suspend(struct net_device
*slave_dev
)
1351 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1353 if (!netif_running(slave_dev
))
1356 cancel_work_sync(&dp
->xmit_work
);
1357 skb_queue_purge(&dp
->xmit_queue
);
1359 netif_device_detach(slave_dev
);
1362 phylink_stop(dp
->pl
);
1368 int dsa_slave_resume(struct net_device
*slave_dev
)
1370 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1372 if (!netif_running(slave_dev
))
1375 netif_device_attach(slave_dev
);
1378 phylink_start(dp
->pl
);
1384 static void dsa_slave_notify(struct net_device
*dev
, unsigned long val
)
1386 struct net_device
*master
= dsa_slave_to_master(dev
);
1387 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1388 struct dsa_notifier_register_info rinfo
= {
1389 .switch_number
= dp
->ds
->index
,
1390 .port_number
= dp
->index
,
1395 call_dsa_notifiers(val
, dev
, &rinfo
.info
);
1398 int dsa_slave_create(struct dsa_port
*port
)
1400 const struct dsa_port
*cpu_dp
= port
->cpu_dp
;
1401 struct net_device
*master
= cpu_dp
->master
;
1402 struct dsa_switch
*ds
= port
->ds
;
1403 const char *name
= port
->name
;
1404 struct net_device
*slave_dev
;
1405 struct dsa_slave_priv
*p
;
1408 if (!ds
->num_tx_queues
)
1409 ds
->num_tx_queues
= 1;
1411 slave_dev
= alloc_netdev_mqs(sizeof(struct dsa_slave_priv
), name
,
1412 NET_NAME_UNKNOWN
, ether_setup
,
1413 ds
->num_tx_queues
, 1);
1414 if (slave_dev
== NULL
)
1417 slave_dev
->features
= master
->vlan_features
| NETIF_F_HW_TC
|
1418 NETIF_F_HW_VLAN_CTAG_FILTER
;
1419 slave_dev
->hw_features
|= NETIF_F_HW_TC
;
1420 slave_dev
->ethtool_ops
= &dsa_slave_ethtool_ops
;
1421 if (!IS_ERR_OR_NULL(port
->mac
))
1422 ether_addr_copy(slave_dev
->dev_addr
, port
->mac
);
1424 eth_hw_addr_inherit(slave_dev
, master
);
1425 slave_dev
->priv_flags
|= IFF_NO_QUEUE
;
1426 slave_dev
->netdev_ops
= &dsa_slave_netdev_ops
;
1427 slave_dev
->min_mtu
= 0;
1428 slave_dev
->max_mtu
= ETH_MAX_MTU
;
1429 SET_NETDEV_DEVTYPE(slave_dev
, &dsa_type
);
1431 netdev_for_each_tx_queue(slave_dev
, dsa_slave_set_lockdep_class_one
,
1434 SET_NETDEV_DEV(slave_dev
, port
->ds
->dev
);
1435 slave_dev
->dev
.of_node
= port
->dn
;
1436 slave_dev
->vlan_features
= master
->vlan_features
;
1438 p
= netdev_priv(slave_dev
);
1439 p
->stats64
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
1441 free_netdev(slave_dev
);
1445 INIT_LIST_HEAD(&p
->mall_tc_list
);
1446 INIT_WORK(&port
->xmit_work
, dsa_port_xmit_work
);
1447 skb_queue_head_init(&port
->xmit_queue
);
1448 p
->xmit
= cpu_dp
->tag_ops
->xmit
;
1449 port
->slave
= slave_dev
;
1451 netif_carrier_off(slave_dev
);
1453 ret
= dsa_slave_phy_setup(slave_dev
);
1455 netdev_err(master
, "error %d setting up slave phy\n", ret
);
1459 dsa_slave_notify(slave_dev
, DSA_PORT_REGISTER
);
1461 ret
= register_netdev(slave_dev
);
1463 netdev_err(master
, "error %d registering interface %s\n",
1464 ret
, slave_dev
->name
);
1472 phylink_disconnect_phy(p
->dp
->pl
);
1474 phylink_destroy(p
->dp
->pl
);
1476 free_percpu(p
->stats64
);
1477 free_netdev(slave_dev
);
1482 void dsa_slave_destroy(struct net_device
*slave_dev
)
1484 struct dsa_port
*dp
= dsa_slave_to_port(slave_dev
);
1485 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1487 netif_carrier_off(slave_dev
);
1489 phylink_disconnect_phy(dp
->pl
);
1492 dsa_slave_notify(slave_dev
, DSA_PORT_UNREGISTER
);
1493 unregister_netdev(slave_dev
);
1494 phylink_destroy(dp
->pl
);
1495 free_percpu(p
->stats64
);
1496 free_netdev(slave_dev
);
1499 static bool dsa_slave_dev_check(struct net_device
*dev
)
1501 return dev
->netdev_ops
== &dsa_slave_netdev_ops
;
1504 static int dsa_slave_changeupper(struct net_device
*dev
,
1505 struct netdev_notifier_changeupper_info
*info
)
1507 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1508 int err
= NOTIFY_DONE
;
1510 if (netif_is_bridge_master(info
->upper_dev
)) {
1511 if (info
->linking
) {
1512 err
= dsa_port_bridge_join(dp
, info
->upper_dev
);
1513 err
= notifier_from_errno(err
);
1515 dsa_port_bridge_leave(dp
, info
->upper_dev
);
1523 static int dsa_slave_upper_vlan_check(struct net_device
*dev
,
1524 struct netdev_notifier_changeupper_info
*
1527 struct netlink_ext_ack
*ext_ack
;
1528 struct net_device
*slave
;
1529 struct dsa_port
*dp
;
1531 ext_ack
= netdev_notifier_info_to_extack(&info
->info
);
1533 if (!is_vlan_dev(dev
))
1536 slave
= vlan_dev_real_dev(dev
);
1537 if (!dsa_slave_dev_check(slave
))
1540 dp
= dsa_slave_to_port(slave
);
1541 if (!dp
->bridge_dev
)
1544 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1545 if (br_vlan_enabled(dp
->bridge_dev
) &&
1546 netif_is_bridge_master(info
->upper_dev
) && info
->linking
) {
1547 NL_SET_ERR_MSG_MOD(ext_ack
,
1548 "Cannot enslave VLAN device into VLAN aware bridge");
1549 return notifier_from_errno(-EINVAL
);
1555 static int dsa_slave_netdevice_event(struct notifier_block
*nb
,
1556 unsigned long event
, void *ptr
)
1558 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1560 if (event
== NETDEV_CHANGEUPPER
) {
1561 if (!dsa_slave_dev_check(dev
))
1562 return dsa_slave_upper_vlan_check(dev
, ptr
);
1564 return dsa_slave_changeupper(dev
, ptr
);
1571 dsa_slave_switchdev_port_attr_set_event(struct net_device
*netdev
,
1572 struct switchdev_notifier_port_attr_info
*port_attr_info
)
1576 err
= dsa_slave_port_attr_set(netdev
, port_attr_info
->attr
,
1577 port_attr_info
->trans
);
1579 port_attr_info
->handled
= true;
1580 return notifier_from_errno(err
);
1583 struct dsa_switchdev_event_work
{
1584 struct work_struct work
;
1585 struct switchdev_notifier_fdb_info fdb_info
;
1586 struct net_device
*dev
;
1587 unsigned long event
;
1590 static void dsa_slave_switchdev_event_work(struct work_struct
*work
)
1592 struct dsa_switchdev_event_work
*switchdev_work
=
1593 container_of(work
, struct dsa_switchdev_event_work
, work
);
1594 struct net_device
*dev
= switchdev_work
->dev
;
1595 struct switchdev_notifier_fdb_info
*fdb_info
;
1596 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
1600 switch (switchdev_work
->event
) {
1601 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
1602 fdb_info
= &switchdev_work
->fdb_info
;
1603 if (!fdb_info
->added_by_user
)
1606 err
= dsa_port_fdb_add(dp
, fdb_info
->addr
, fdb_info
->vid
);
1608 netdev_dbg(dev
, "fdb add failed err=%d\n", err
);
1611 fdb_info
->offloaded
= true;
1612 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED
, dev
,
1613 &fdb_info
->info
, NULL
);
1616 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1617 fdb_info
= &switchdev_work
->fdb_info
;
1618 if (!fdb_info
->added_by_user
)
1621 err
= dsa_port_fdb_del(dp
, fdb_info
->addr
, fdb_info
->vid
);
1623 netdev_dbg(dev
, "fdb del failed err=%d\n", err
);
1630 kfree(switchdev_work
->fdb_info
.addr
);
1631 kfree(switchdev_work
);
1636 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work
*
1638 const struct switchdev_notifier_fdb_info
*
1641 memcpy(&switchdev_work
->fdb_info
, fdb_info
,
1642 sizeof(switchdev_work
->fdb_info
));
1643 switchdev_work
->fdb_info
.addr
= kzalloc(ETH_ALEN
, GFP_ATOMIC
);
1644 if (!switchdev_work
->fdb_info
.addr
)
1646 ether_addr_copy((u8
*)switchdev_work
->fdb_info
.addr
,
1651 /* Called under rcu_read_lock() */
1652 static int dsa_slave_switchdev_event(struct notifier_block
*unused
,
1653 unsigned long event
, void *ptr
)
1655 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
1656 struct dsa_switchdev_event_work
*switchdev_work
;
1658 if (!dsa_slave_dev_check(dev
))
1661 if (event
== SWITCHDEV_PORT_ATTR_SET
)
1662 return dsa_slave_switchdev_port_attr_set_event(dev
, ptr
);
1664 switchdev_work
= kzalloc(sizeof(*switchdev_work
), GFP_ATOMIC
);
1665 if (!switchdev_work
)
1668 INIT_WORK(&switchdev_work
->work
,
1669 dsa_slave_switchdev_event_work
);
1670 switchdev_work
->dev
= dev
;
1671 switchdev_work
->event
= event
;
1674 case SWITCHDEV_FDB_ADD_TO_DEVICE
: /* fall through */
1675 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1676 if (dsa_slave_switchdev_fdb_work_init(switchdev_work
, ptr
))
1677 goto err_fdb_work_init
;
1681 kfree(switchdev_work
);
1685 dsa_schedule_work(&switchdev_work
->work
);
1689 kfree(switchdev_work
);
1694 dsa_slave_switchdev_port_obj_event(unsigned long event
,
1695 struct net_device
*netdev
,
1696 struct switchdev_notifier_port_obj_info
*port_obj_info
)
1698 int err
= -EOPNOTSUPP
;
1701 case SWITCHDEV_PORT_OBJ_ADD
:
1702 err
= dsa_slave_port_obj_add(netdev
, port_obj_info
->obj
,
1703 port_obj_info
->trans
);
1705 case SWITCHDEV_PORT_OBJ_DEL
:
1706 err
= dsa_slave_port_obj_del(netdev
, port_obj_info
->obj
);
1710 port_obj_info
->handled
= true;
1711 return notifier_from_errno(err
);
1714 static int dsa_slave_switchdev_blocking_event(struct notifier_block
*unused
,
1715 unsigned long event
, void *ptr
)
1717 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
1719 if (!dsa_slave_dev_check(dev
))
1723 case SWITCHDEV_PORT_OBJ_ADD
: /* fall through */
1724 case SWITCHDEV_PORT_OBJ_DEL
:
1725 return dsa_slave_switchdev_port_obj_event(event
, dev
, ptr
);
1726 case SWITCHDEV_PORT_ATTR_SET
:
1727 return dsa_slave_switchdev_port_attr_set_event(dev
, ptr
);
1733 static struct notifier_block dsa_slave_nb __read_mostly
= {
1734 .notifier_call
= dsa_slave_netdevice_event
,
1737 static struct notifier_block dsa_slave_switchdev_notifier
= {
1738 .notifier_call
= dsa_slave_switchdev_event
,
1741 static struct notifier_block dsa_slave_switchdev_blocking_notifier
= {
1742 .notifier_call
= dsa_slave_switchdev_blocking_event
,
1745 int dsa_slave_register_notifier(void)
1747 struct notifier_block
*nb
;
1750 err
= register_netdevice_notifier(&dsa_slave_nb
);
1754 err
= register_switchdev_notifier(&dsa_slave_switchdev_notifier
);
1756 goto err_switchdev_nb
;
1758 nb
= &dsa_slave_switchdev_blocking_notifier
;
1759 err
= register_switchdev_blocking_notifier(nb
);
1761 goto err_switchdev_blocking_nb
;
1765 err_switchdev_blocking_nb
:
1766 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier
);
1768 unregister_netdevice_notifier(&dsa_slave_nb
);
1772 void dsa_slave_unregister_notifier(void)
1774 struct notifier_block
*nb
;
1777 nb
= &dsa_slave_switchdev_blocking_notifier
;
1778 err
= unregister_switchdev_blocking_notifier(nb
);
1780 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err
);
1782 err
= unregister_switchdev_notifier(&dsa_slave_switchdev_notifier
);
1784 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err
);
1786 err
= unregister_netdevice_notifier(&dsa_slave_nb
);
1788 pr_err("DSA: failed to unregister slave notifier (%d)\n", err
);