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/switchdev.h>
21 #include <linux/if_bridge.h>
22 #include <linux/netpoll.h>
25 /* slave mii_bus handling ***************************************************/
26 static int dsa_slave_phy_read(struct mii_bus
*bus
, int addr
, int reg
)
28 struct dsa_switch
*ds
= bus
->priv
;
30 if (ds
->phys_mii_mask
& (1 << addr
))
31 return ds
->drv
->phy_read(ds
, addr
, reg
);
36 static int dsa_slave_phy_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
38 struct dsa_switch
*ds
= bus
->priv
;
40 if (ds
->phys_mii_mask
& (1 << addr
))
41 return ds
->drv
->phy_write(ds
, addr
, reg
, val
);
46 void dsa_slave_mii_bus_init(struct dsa_switch
*ds
)
48 ds
->slave_mii_bus
->priv
= (void *)ds
;
49 ds
->slave_mii_bus
->name
= "dsa slave smi";
50 ds
->slave_mii_bus
->read
= dsa_slave_phy_read
;
51 ds
->slave_mii_bus
->write
= dsa_slave_phy_write
;
52 snprintf(ds
->slave_mii_bus
->id
, MII_BUS_ID_SIZE
, "dsa-%d", ds
->index
);
53 ds
->slave_mii_bus
->parent
= ds
->dev
;
54 ds
->slave_mii_bus
->phy_mask
= ~ds
->phys_mii_mask
;
58 /* slave device handling ****************************************************/
59 static int dsa_slave_get_iflink(const struct net_device
*dev
)
61 struct dsa_slave_priv
*p
= netdev_priv(dev
);
63 return p
->parent
->dst
->master_netdev
->ifindex
;
66 static inline bool dsa_port_is_bridged(struct dsa_slave_priv
*p
)
68 return !!p
->bridge_dev
;
71 static int dsa_slave_open(struct net_device
*dev
)
73 struct dsa_slave_priv
*p
= netdev_priv(dev
);
74 struct net_device
*master
= p
->parent
->dst
->master_netdev
;
75 struct dsa_switch
*ds
= p
->parent
;
76 u8 stp_state
= dsa_port_is_bridged(p
) ?
77 BR_STATE_BLOCKING
: BR_STATE_FORWARDING
;
80 if (!(master
->flags
& IFF_UP
))
83 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
)) {
84 err
= dev_uc_add(master
, dev
->dev_addr
);
89 if (dev
->flags
& IFF_ALLMULTI
) {
90 err
= dev_set_allmulti(master
, 1);
94 if (dev
->flags
& IFF_PROMISC
) {
95 err
= dev_set_promiscuity(master
, 1);
100 if (ds
->drv
->port_enable
) {
101 err
= ds
->drv
->port_enable(ds
, p
->port
, p
->phy
);
106 if (ds
->drv
->port_stp_state_set
)
107 ds
->drv
->port_stp_state_set(ds
, p
->port
, stp_state
);
115 if (dev
->flags
& IFF_PROMISC
)
116 dev_set_promiscuity(master
, -1);
118 if (dev
->flags
& IFF_ALLMULTI
)
119 dev_set_allmulti(master
, -1);
121 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
122 dev_uc_del(master
, dev
->dev_addr
);
127 static int dsa_slave_close(struct net_device
*dev
)
129 struct dsa_slave_priv
*p
= netdev_priv(dev
);
130 struct net_device
*master
= p
->parent
->dst
->master_netdev
;
131 struct dsa_switch
*ds
= p
->parent
;
136 dev_mc_unsync(master
, dev
);
137 dev_uc_unsync(master
, dev
);
138 if (dev
->flags
& IFF_ALLMULTI
)
139 dev_set_allmulti(master
, -1);
140 if (dev
->flags
& IFF_PROMISC
)
141 dev_set_promiscuity(master
, -1);
143 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
144 dev_uc_del(master
, dev
->dev_addr
);
146 if (ds
->drv
->port_disable
)
147 ds
->drv
->port_disable(ds
, p
->port
, p
->phy
);
149 if (ds
->drv
->port_stp_state_set
)
150 ds
->drv
->port_stp_state_set(ds
, p
->port
, BR_STATE_DISABLED
);
155 static void dsa_slave_change_rx_flags(struct net_device
*dev
, int change
)
157 struct dsa_slave_priv
*p
= netdev_priv(dev
);
158 struct net_device
*master
= p
->parent
->dst
->master_netdev
;
160 if (change
& IFF_ALLMULTI
)
161 dev_set_allmulti(master
, dev
->flags
& IFF_ALLMULTI
? 1 : -1);
162 if (change
& IFF_PROMISC
)
163 dev_set_promiscuity(master
, dev
->flags
& IFF_PROMISC
? 1 : -1);
166 static void dsa_slave_set_rx_mode(struct net_device
*dev
)
168 struct dsa_slave_priv
*p
= netdev_priv(dev
);
169 struct net_device
*master
= p
->parent
->dst
->master_netdev
;
171 dev_mc_sync(master
, dev
);
172 dev_uc_sync(master
, dev
);
175 static int dsa_slave_set_mac_address(struct net_device
*dev
, void *a
)
177 struct dsa_slave_priv
*p
= netdev_priv(dev
);
178 struct net_device
*master
= p
->parent
->dst
->master_netdev
;
179 struct sockaddr
*addr
= a
;
182 if (!is_valid_ether_addr(addr
->sa_data
))
183 return -EADDRNOTAVAIL
;
185 if (!(dev
->flags
& IFF_UP
))
188 if (!ether_addr_equal(addr
->sa_data
, master
->dev_addr
)) {
189 err
= dev_uc_add(master
, addr
->sa_data
);
194 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
195 dev_uc_del(master
, dev
->dev_addr
);
198 ether_addr_copy(dev
->dev_addr
, addr
->sa_data
);
203 static int dsa_slave_port_vlan_add(struct net_device
*dev
,
204 const struct switchdev_obj_port_vlan
*vlan
,
205 struct switchdev_trans
*trans
)
207 struct dsa_slave_priv
*p
= netdev_priv(dev
);
208 struct dsa_switch
*ds
= p
->parent
;
210 if (switchdev_trans_ph_prepare(trans
)) {
211 if (!ds
->drv
->port_vlan_prepare
|| !ds
->drv
->port_vlan_add
)
214 return ds
->drv
->port_vlan_prepare(ds
, p
->port
, vlan
, trans
);
217 ds
->drv
->port_vlan_add(ds
, p
->port
, vlan
, trans
);
222 static int dsa_slave_port_vlan_del(struct net_device
*dev
,
223 const struct switchdev_obj_port_vlan
*vlan
)
225 struct dsa_slave_priv
*p
= netdev_priv(dev
);
226 struct dsa_switch
*ds
= p
->parent
;
228 if (!ds
->drv
->port_vlan_del
)
231 return ds
->drv
->port_vlan_del(ds
, p
->port
, vlan
);
234 static int dsa_slave_port_vlan_dump(struct net_device
*dev
,
235 struct switchdev_obj_port_vlan
*vlan
,
236 switchdev_obj_dump_cb_t
*cb
)
238 struct dsa_slave_priv
*p
= netdev_priv(dev
);
239 struct dsa_switch
*ds
= p
->parent
;
241 if (ds
->drv
->port_vlan_dump
)
242 return ds
->drv
->port_vlan_dump(ds
, p
->port
, vlan
, cb
);
247 static int dsa_slave_port_fdb_add(struct net_device
*dev
,
248 const struct switchdev_obj_port_fdb
*fdb
,
249 struct switchdev_trans
*trans
)
251 struct dsa_slave_priv
*p
= netdev_priv(dev
);
252 struct dsa_switch
*ds
= p
->parent
;
254 if (switchdev_trans_ph_prepare(trans
)) {
255 if (!ds
->drv
->port_fdb_prepare
|| !ds
->drv
->port_fdb_add
)
258 return ds
->drv
->port_fdb_prepare(ds
, p
->port
, fdb
, trans
);
261 ds
->drv
->port_fdb_add(ds
, p
->port
, fdb
, trans
);
266 static int dsa_slave_port_fdb_del(struct net_device
*dev
,
267 const struct switchdev_obj_port_fdb
*fdb
)
269 struct dsa_slave_priv
*p
= netdev_priv(dev
);
270 struct dsa_switch
*ds
= p
->parent
;
271 int ret
= -EOPNOTSUPP
;
273 if (ds
->drv
->port_fdb_del
)
274 ret
= ds
->drv
->port_fdb_del(ds
, p
->port
, fdb
);
279 static int dsa_slave_port_fdb_dump(struct net_device
*dev
,
280 struct switchdev_obj_port_fdb
*fdb
,
281 switchdev_obj_dump_cb_t
*cb
)
283 struct dsa_slave_priv
*p
= netdev_priv(dev
);
284 struct dsa_switch
*ds
= p
->parent
;
286 if (ds
->drv
->port_fdb_dump
)
287 return ds
->drv
->port_fdb_dump(ds
, p
->port
, fdb
, cb
);
292 static int dsa_slave_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
294 struct dsa_slave_priv
*p
= netdev_priv(dev
);
297 return phy_mii_ioctl(p
->phy
, ifr
, cmd
);
302 static int dsa_slave_stp_state_set(struct net_device
*dev
,
303 const struct switchdev_attr
*attr
,
304 struct switchdev_trans
*trans
)
306 struct dsa_slave_priv
*p
= netdev_priv(dev
);
307 struct dsa_switch
*ds
= p
->parent
;
309 if (switchdev_trans_ph_prepare(trans
))
310 return ds
->drv
->port_stp_state_set
? 0 : -EOPNOTSUPP
;
312 ds
->drv
->port_stp_state_set(ds
, p
->port
, attr
->u
.stp_state
);
317 static int dsa_slave_vlan_filtering(struct net_device
*dev
,
318 const struct switchdev_attr
*attr
,
319 struct switchdev_trans
*trans
)
321 struct dsa_slave_priv
*p
= netdev_priv(dev
);
322 struct dsa_switch
*ds
= p
->parent
;
324 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
325 if (switchdev_trans_ph_prepare(trans
))
328 if (ds
->drv
->port_vlan_filtering
)
329 return ds
->drv
->port_vlan_filtering(ds
, p
->port
,
330 attr
->u
.vlan_filtering
);
335 static int dsa_slave_port_attr_set(struct net_device
*dev
,
336 const struct switchdev_attr
*attr
,
337 struct switchdev_trans
*trans
)
342 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
343 ret
= dsa_slave_stp_state_set(dev
, attr
, trans
);
345 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
:
346 ret
= dsa_slave_vlan_filtering(dev
, attr
, trans
);
356 static int dsa_slave_port_obj_add(struct net_device
*dev
,
357 const struct switchdev_obj
*obj
,
358 struct switchdev_trans
*trans
)
362 /* For the prepare phase, ensure the full set of changes is feasable in
363 * one go in order to signal a failure properly. If an operation is not
364 * supported, return -EOPNOTSUPP.
368 case SWITCHDEV_OBJ_ID_PORT_FDB
:
369 err
= dsa_slave_port_fdb_add(dev
,
370 SWITCHDEV_OBJ_PORT_FDB(obj
),
373 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
374 err
= dsa_slave_port_vlan_add(dev
,
375 SWITCHDEV_OBJ_PORT_VLAN(obj
),
386 static int dsa_slave_port_obj_del(struct net_device
*dev
,
387 const struct switchdev_obj
*obj
)
392 case SWITCHDEV_OBJ_ID_PORT_FDB
:
393 err
= dsa_slave_port_fdb_del(dev
,
394 SWITCHDEV_OBJ_PORT_FDB(obj
));
396 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
397 err
= dsa_slave_port_vlan_del(dev
,
398 SWITCHDEV_OBJ_PORT_VLAN(obj
));
408 static int dsa_slave_port_obj_dump(struct net_device
*dev
,
409 struct switchdev_obj
*obj
,
410 switchdev_obj_dump_cb_t
*cb
)
415 case SWITCHDEV_OBJ_ID_PORT_FDB
:
416 err
= dsa_slave_port_fdb_dump(dev
,
417 SWITCHDEV_OBJ_PORT_FDB(obj
),
420 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
421 err
= dsa_slave_port_vlan_dump(dev
,
422 SWITCHDEV_OBJ_PORT_VLAN(obj
),
433 static int dsa_slave_bridge_port_join(struct net_device
*dev
,
434 struct net_device
*br
)
436 struct dsa_slave_priv
*p
= netdev_priv(dev
);
437 struct dsa_switch
*ds
= p
->parent
;
438 int ret
= -EOPNOTSUPP
;
442 if (ds
->drv
->port_bridge_join
)
443 ret
= ds
->drv
->port_bridge_join(ds
, p
->port
, br
);
445 return ret
== -EOPNOTSUPP
? 0 : ret
;
448 static void dsa_slave_bridge_port_leave(struct net_device
*dev
)
450 struct dsa_slave_priv
*p
= netdev_priv(dev
);
451 struct dsa_switch
*ds
= p
->parent
;
454 if (ds
->drv
->port_bridge_leave
)
455 ds
->drv
->port_bridge_leave(ds
, p
->port
);
457 p
->bridge_dev
= NULL
;
459 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
460 * so allow it to be in BR_STATE_FORWARDING to be kept functional
462 if (ds
->drv
->port_stp_state_set
)
463 ds
->drv
->port_stp_state_set(ds
, p
->port
, BR_STATE_FORWARDING
);
466 static int dsa_slave_port_attr_get(struct net_device
*dev
,
467 struct switchdev_attr
*attr
)
469 struct dsa_slave_priv
*p
= netdev_priv(dev
);
470 struct dsa_switch
*ds
= p
->parent
;
473 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID
:
474 attr
->u
.ppid
.id_len
= sizeof(ds
->index
);
475 memcpy(&attr
->u
.ppid
.id
, &ds
->index
, attr
->u
.ppid
.id_len
);
484 static inline netdev_tx_t
dsa_netpoll_send_skb(struct dsa_slave_priv
*p
,
487 #ifdef CONFIG_NET_POLL_CONTROLLER
489 netpoll_send_skb(p
->netpoll
, skb
);
496 static netdev_tx_t
dsa_slave_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
498 struct dsa_slave_priv
*p
= netdev_priv(dev
);
499 struct sk_buff
*nskb
;
501 dev
->stats
.tx_packets
++;
502 dev
->stats
.tx_bytes
+= skb
->len
;
504 /* Transmit function may have to reallocate the original SKB */
505 nskb
= p
->xmit(skb
, dev
);
509 /* SKB for netpoll still need to be mangled with the protocol-specific
510 * tag to be successfully transmitted
512 if (unlikely(netpoll_tx_running(dev
)))
513 return dsa_netpoll_send_skb(p
, nskb
);
515 /* Queue the SKB for transmission on the parent interface, but
516 * do not modify its EtherType
518 nskb
->dev
= p
->parent
->dst
->master_netdev
;
519 dev_queue_xmit(nskb
);
524 static struct sk_buff
*dsa_slave_notag_xmit(struct sk_buff
*skb
,
525 struct net_device
*dev
)
527 /* Just return the original SKB */
532 /* ethtool operations *******************************************************/
534 dsa_slave_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
536 struct dsa_slave_priv
*p
= netdev_priv(dev
);
540 if (p
->phy
!= NULL
) {
541 err
= phy_read_status(p
->phy
);
543 err
= phy_ethtool_gset(p
->phy
, cmd
);
550 dsa_slave_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
552 struct dsa_slave_priv
*p
= netdev_priv(dev
);
555 return phy_ethtool_sset(p
->phy
, cmd
);
560 static void dsa_slave_get_drvinfo(struct net_device
*dev
,
561 struct ethtool_drvinfo
*drvinfo
)
563 strlcpy(drvinfo
->driver
, "dsa", sizeof(drvinfo
->driver
));
564 strlcpy(drvinfo
->version
, dsa_driver_version
, sizeof(drvinfo
->version
));
565 strlcpy(drvinfo
->fw_version
, "N/A", sizeof(drvinfo
->fw_version
));
566 strlcpy(drvinfo
->bus_info
, "platform", sizeof(drvinfo
->bus_info
));
569 static int dsa_slave_get_regs_len(struct net_device
*dev
)
571 struct dsa_slave_priv
*p
= netdev_priv(dev
);
572 struct dsa_switch
*ds
= p
->parent
;
574 if (ds
->drv
->get_regs_len
)
575 return ds
->drv
->get_regs_len(ds
, p
->port
);
581 dsa_slave_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *_p
)
583 struct dsa_slave_priv
*p
= netdev_priv(dev
);
584 struct dsa_switch
*ds
= p
->parent
;
586 if (ds
->drv
->get_regs
)
587 ds
->drv
->get_regs(ds
, p
->port
, regs
, _p
);
590 static int dsa_slave_nway_reset(struct net_device
*dev
)
592 struct dsa_slave_priv
*p
= netdev_priv(dev
);
595 return genphy_restart_aneg(p
->phy
);
600 static u32
dsa_slave_get_link(struct net_device
*dev
)
602 struct dsa_slave_priv
*p
= netdev_priv(dev
);
604 if (p
->phy
!= NULL
) {
605 genphy_update_link(p
->phy
);
612 static int dsa_slave_get_eeprom_len(struct net_device
*dev
)
614 struct dsa_slave_priv
*p
= netdev_priv(dev
);
615 struct dsa_switch
*ds
= p
->parent
;
617 if (ds
->cd
&& ds
->cd
->eeprom_len
)
618 return ds
->cd
->eeprom_len
;
620 if (ds
->drv
->get_eeprom_len
)
621 return ds
->drv
->get_eeprom_len(ds
);
626 static int dsa_slave_get_eeprom(struct net_device
*dev
,
627 struct ethtool_eeprom
*eeprom
, u8
*data
)
629 struct dsa_slave_priv
*p
= netdev_priv(dev
);
630 struct dsa_switch
*ds
= p
->parent
;
632 if (ds
->drv
->get_eeprom
)
633 return ds
->drv
->get_eeprom(ds
, eeprom
, data
);
638 static int dsa_slave_set_eeprom(struct net_device
*dev
,
639 struct ethtool_eeprom
*eeprom
, u8
*data
)
641 struct dsa_slave_priv
*p
= netdev_priv(dev
);
642 struct dsa_switch
*ds
= p
->parent
;
644 if (ds
->drv
->set_eeprom
)
645 return ds
->drv
->set_eeprom(ds
, eeprom
, data
);
650 static void dsa_slave_get_strings(struct net_device
*dev
,
651 uint32_t stringset
, uint8_t *data
)
653 struct dsa_slave_priv
*p
= netdev_priv(dev
);
654 struct dsa_switch
*ds
= p
->parent
;
656 if (stringset
== ETH_SS_STATS
) {
657 int len
= ETH_GSTRING_LEN
;
659 strncpy(data
, "tx_packets", len
);
660 strncpy(data
+ len
, "tx_bytes", len
);
661 strncpy(data
+ 2 * len
, "rx_packets", len
);
662 strncpy(data
+ 3 * len
, "rx_bytes", len
);
663 if (ds
->drv
->get_strings
!= NULL
)
664 ds
->drv
->get_strings(ds
, p
->port
, data
+ 4 * len
);
668 static void dsa_cpu_port_get_ethtool_stats(struct net_device
*dev
,
669 struct ethtool_stats
*stats
,
672 struct dsa_switch_tree
*dst
= dev
->dsa_ptr
;
673 struct dsa_switch
*ds
= dst
->ds
[0];
674 s8 cpu_port
= dst
->cpu_port
;
677 if (dst
->master_ethtool_ops
.get_sset_count
) {
678 count
= dst
->master_ethtool_ops
.get_sset_count(dev
,
680 dst
->master_ethtool_ops
.get_ethtool_stats(dev
, stats
, data
);
683 if (ds
->drv
->get_ethtool_stats
)
684 ds
->drv
->get_ethtool_stats(ds
, cpu_port
, data
+ count
);
687 static int dsa_cpu_port_get_sset_count(struct net_device
*dev
, int sset
)
689 struct dsa_switch_tree
*dst
= dev
->dsa_ptr
;
690 struct dsa_switch
*ds
= dst
->ds
[0];
693 if (dst
->master_ethtool_ops
.get_sset_count
)
694 count
+= dst
->master_ethtool_ops
.get_sset_count(dev
, sset
);
696 if (sset
== ETH_SS_STATS
&& ds
->drv
->get_sset_count
)
697 count
+= ds
->drv
->get_sset_count(ds
);
702 static void dsa_cpu_port_get_strings(struct net_device
*dev
,
703 uint32_t stringset
, uint8_t *data
)
705 struct dsa_switch_tree
*dst
= dev
->dsa_ptr
;
706 struct dsa_switch
*ds
= dst
->ds
[0];
707 s8 cpu_port
= dst
->cpu_port
;
708 int len
= ETH_GSTRING_LEN
;
709 int mcount
= 0, count
;
714 snprintf(pfx
, sizeof(pfx
), "p%.2d", cpu_port
);
715 /* We do not want to be NULL-terminated, since this is a prefix */
716 pfx
[sizeof(pfx
) - 1] = '_';
718 if (dst
->master_ethtool_ops
.get_sset_count
) {
719 mcount
= dst
->master_ethtool_ops
.get_sset_count(dev
,
721 dst
->master_ethtool_ops
.get_strings(dev
, stringset
, data
);
724 if (stringset
== ETH_SS_STATS
&& ds
->drv
->get_strings
) {
725 ndata
= data
+ mcount
* len
;
726 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
727 * the output after to prepend our CPU port prefix we
728 * constructed earlier
730 ds
->drv
->get_strings(ds
, cpu_port
, ndata
);
731 count
= ds
->drv
->get_sset_count(ds
);
732 for (i
= 0; i
< count
; i
++) {
733 memmove(ndata
+ (i
* len
+ sizeof(pfx
)),
734 ndata
+ i
* len
, len
- sizeof(pfx
));
735 memcpy(ndata
+ i
* len
, pfx
, sizeof(pfx
));
740 static void dsa_slave_get_ethtool_stats(struct net_device
*dev
,
741 struct ethtool_stats
*stats
,
744 struct dsa_slave_priv
*p
= netdev_priv(dev
);
745 struct dsa_switch
*ds
= p
->parent
;
747 data
[0] = dev
->stats
.tx_packets
;
748 data
[1] = dev
->stats
.tx_bytes
;
749 data
[2] = dev
->stats
.rx_packets
;
750 data
[3] = dev
->stats
.rx_bytes
;
751 if (ds
->drv
->get_ethtool_stats
!= NULL
)
752 ds
->drv
->get_ethtool_stats(ds
, p
->port
, data
+ 4);
755 static int dsa_slave_get_sset_count(struct net_device
*dev
, int sset
)
757 struct dsa_slave_priv
*p
= netdev_priv(dev
);
758 struct dsa_switch
*ds
= p
->parent
;
760 if (sset
== ETH_SS_STATS
) {
764 if (ds
->drv
->get_sset_count
!= NULL
)
765 count
+= ds
->drv
->get_sset_count(ds
);
773 static void dsa_slave_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
775 struct dsa_slave_priv
*p
= netdev_priv(dev
);
776 struct dsa_switch
*ds
= p
->parent
;
778 if (ds
->drv
->get_wol
)
779 ds
->drv
->get_wol(ds
, p
->port
, w
);
782 static int dsa_slave_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
784 struct dsa_slave_priv
*p
= netdev_priv(dev
);
785 struct dsa_switch
*ds
= p
->parent
;
786 int ret
= -EOPNOTSUPP
;
788 if (ds
->drv
->set_wol
)
789 ret
= ds
->drv
->set_wol(ds
, p
->port
, w
);
794 static int dsa_slave_set_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
796 struct dsa_slave_priv
*p
= netdev_priv(dev
);
797 struct dsa_switch
*ds
= p
->parent
;
800 if (!ds
->drv
->set_eee
)
803 ret
= ds
->drv
->set_eee(ds
, p
->port
, p
->phy
, e
);
808 ret
= phy_ethtool_set_eee(p
->phy
, e
);
813 static int dsa_slave_get_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
815 struct dsa_slave_priv
*p
= netdev_priv(dev
);
816 struct dsa_switch
*ds
= p
->parent
;
819 if (!ds
->drv
->get_eee
)
822 ret
= ds
->drv
->get_eee(ds
, p
->port
, e
);
827 ret
= phy_ethtool_get_eee(p
->phy
, e
);
832 #ifdef CONFIG_NET_POLL_CONTROLLER
833 static int dsa_slave_netpoll_setup(struct net_device
*dev
,
834 struct netpoll_info
*ni
)
836 struct dsa_slave_priv
*p
= netdev_priv(dev
);
837 struct dsa_switch
*ds
= p
->parent
;
838 struct net_device
*master
= ds
->dst
->master_netdev
;
839 struct netpoll
*netpoll
;
842 netpoll
= kzalloc(sizeof(*netpoll
), GFP_KERNEL
);
846 err
= __netpoll_setup(netpoll
, master
);
852 p
->netpoll
= netpoll
;
857 static void dsa_slave_netpoll_cleanup(struct net_device
*dev
)
859 struct dsa_slave_priv
*p
= netdev_priv(dev
);
860 struct netpoll
*netpoll
= p
->netpoll
;
867 __netpoll_free_async(netpoll
);
870 static void dsa_slave_poll_controller(struct net_device
*dev
)
875 static const struct ethtool_ops dsa_slave_ethtool_ops
= {
876 .get_settings
= dsa_slave_get_settings
,
877 .set_settings
= dsa_slave_set_settings
,
878 .get_drvinfo
= dsa_slave_get_drvinfo
,
879 .get_regs_len
= dsa_slave_get_regs_len
,
880 .get_regs
= dsa_slave_get_regs
,
881 .nway_reset
= dsa_slave_nway_reset
,
882 .get_link
= dsa_slave_get_link
,
883 .get_eeprom_len
= dsa_slave_get_eeprom_len
,
884 .get_eeprom
= dsa_slave_get_eeprom
,
885 .set_eeprom
= dsa_slave_set_eeprom
,
886 .get_strings
= dsa_slave_get_strings
,
887 .get_ethtool_stats
= dsa_slave_get_ethtool_stats
,
888 .get_sset_count
= dsa_slave_get_sset_count
,
889 .set_wol
= dsa_slave_set_wol
,
890 .get_wol
= dsa_slave_get_wol
,
891 .set_eee
= dsa_slave_set_eee
,
892 .get_eee
= dsa_slave_get_eee
,
895 static struct ethtool_ops dsa_cpu_port_ethtool_ops
;
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
= switchdev_port_fdb_add
,
905 .ndo_fdb_del
= switchdev_port_fdb_del
,
906 .ndo_fdb_dump
= switchdev_port_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_bridge_getlink
= switchdev_port_bridge_getlink
,
915 .ndo_bridge_setlink
= switchdev_port_bridge_setlink
,
916 .ndo_bridge_dellink
= switchdev_port_bridge_dellink
,
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
,
924 .switchdev_port_obj_dump
= dsa_slave_port_obj_dump
,
927 static struct device_type dsa_type
= {
931 static void dsa_slave_adjust_link(struct net_device
*dev
)
933 struct dsa_slave_priv
*p
= netdev_priv(dev
);
934 struct dsa_switch
*ds
= p
->parent
;
935 unsigned int status_changed
= 0;
937 if (p
->old_link
!= p
->phy
->link
) {
939 p
->old_link
= p
->phy
->link
;
942 if (p
->old_duplex
!= p
->phy
->duplex
) {
944 p
->old_duplex
= p
->phy
->duplex
;
947 if (p
->old_pause
!= p
->phy
->pause
) {
949 p
->old_pause
= p
->phy
->pause
;
952 if (ds
->drv
->adjust_link
&& status_changed
)
953 ds
->drv
->adjust_link(ds
, p
->port
, p
->phy
);
956 phy_print_status(p
->phy
);
959 static int dsa_slave_fixed_link_update(struct net_device
*dev
,
960 struct fixed_phy_status
*status
)
962 struct dsa_slave_priv
*p
;
963 struct dsa_switch
*ds
;
966 p
= netdev_priv(dev
);
968 if (ds
->drv
->fixed_link_update
)
969 ds
->drv
->fixed_link_update(ds
, p
->port
, status
);
975 /* slave device setup *******************************************************/
976 static int dsa_slave_phy_connect(struct dsa_slave_priv
*p
,
977 struct net_device
*slave_dev
,
980 struct dsa_switch
*ds
= p
->parent
;
982 p
->phy
= mdiobus_get_phy(ds
->slave_mii_bus
, addr
);
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
= p
->phy
->interface
;
991 phy_connect_direct(slave_dev
, p
->phy
, dsa_slave_adjust_link
,
997 static int dsa_slave_phy_setup(struct dsa_slave_priv
*p
,
998 struct net_device
*slave_dev
)
1000 struct dsa_switch
*ds
= p
->parent
;
1001 struct device_node
*phy_dn
, *port_dn
;
1002 bool phy_is_fixed
= false;
1006 port_dn
= ds
->ports
[p
->port
].dn
;
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 (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;
1026 if (ds
->drv
->get_phy_flags
)
1027 phy_flags
= ds
->drv
->get_phy_flags(ds
, p
->port
);
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(p
, slave_dev
, phy_id
);
1041 netdev_err(slave_dev
, "failed to connect to phy%d: %d\n", phy_id
, ret
);
1045 p
->phy
= of_phy_connect(slave_dev
, phy_dn
,
1046 dsa_slave_adjust_link
,
1052 if (p
->phy
&& phy_is_fixed
)
1053 fixed_phy_set_link_update(p
->phy
, dsa_slave_fixed_link_update
);
1055 /* We could not connect to a designated PHY, so use the switch internal
1059 ret
= dsa_slave_phy_connect(p
, slave_dev
, p
->port
);
1061 netdev_err(slave_dev
, "failed to connect to port %d: %d\n", p
->port
, ret
);
1066 phy_attached_info(p
->phy
);
1071 static struct lock_class_key dsa_slave_netdev_xmit_lock_key
;
1072 static void dsa_slave_set_lockdep_class_one(struct net_device
*dev
,
1073 struct netdev_queue
*txq
,
1076 lockdep_set_class(&txq
->_xmit_lock
,
1077 &dsa_slave_netdev_xmit_lock_key
);
1080 int dsa_slave_suspend(struct net_device
*slave_dev
)
1082 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1089 phy_suspend(p
->phy
);
1095 int dsa_slave_resume(struct net_device
*slave_dev
)
1097 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1099 netif_device_attach(slave_dev
);
1109 int dsa_slave_create(struct dsa_switch
*ds
, struct device
*parent
,
1110 int port
, char *name
)
1112 struct net_device
*master
= ds
->dst
->master_netdev
;
1113 struct dsa_switch_tree
*dst
= ds
->dst
;
1114 struct net_device
*slave_dev
;
1115 struct dsa_slave_priv
*p
;
1118 slave_dev
= alloc_netdev(sizeof(struct dsa_slave_priv
), name
,
1119 NET_NAME_UNKNOWN
, ether_setup
);
1120 if (slave_dev
== NULL
)
1123 slave_dev
->features
= master
->vlan_features
;
1124 slave_dev
->ethtool_ops
= &dsa_slave_ethtool_ops
;
1125 if (master
->ethtool_ops
!= &dsa_cpu_port_ethtool_ops
) {
1126 memcpy(&dst
->master_ethtool_ops
, master
->ethtool_ops
,
1127 sizeof(struct ethtool_ops
));
1128 memcpy(&dsa_cpu_port_ethtool_ops
, &dst
->master_ethtool_ops
,
1129 sizeof(struct ethtool_ops
));
1130 dsa_cpu_port_ethtool_ops
.get_sset_count
=
1131 dsa_cpu_port_get_sset_count
;
1132 dsa_cpu_port_ethtool_ops
.get_ethtool_stats
=
1133 dsa_cpu_port_get_ethtool_stats
;
1134 dsa_cpu_port_ethtool_ops
.get_strings
=
1135 dsa_cpu_port_get_strings
;
1136 master
->ethtool_ops
= &dsa_cpu_port_ethtool_ops
;
1138 eth_hw_addr_inherit(slave_dev
, master
);
1139 slave_dev
->priv_flags
|= IFF_NO_QUEUE
;
1140 slave_dev
->netdev_ops
= &dsa_slave_netdev_ops
;
1141 slave_dev
->switchdev_ops
= &dsa_slave_switchdev_ops
;
1142 SET_NETDEV_DEVTYPE(slave_dev
, &dsa_type
);
1144 netdev_for_each_tx_queue(slave_dev
, dsa_slave_set_lockdep_class_one
,
1147 SET_NETDEV_DEV(slave_dev
, parent
);
1148 slave_dev
->dev
.of_node
= ds
->ports
[port
].dn
;
1149 slave_dev
->vlan_features
= master
->vlan_features
;
1151 p
= netdev_priv(slave_dev
);
1155 switch (ds
->dst
->tag_protocol
) {
1156 #ifdef CONFIG_NET_DSA_TAG_DSA
1157 case DSA_TAG_PROTO_DSA
:
1158 p
->xmit
= dsa_netdev_ops
.xmit
;
1161 #ifdef CONFIG_NET_DSA_TAG_EDSA
1162 case DSA_TAG_PROTO_EDSA
:
1163 p
->xmit
= edsa_netdev_ops
.xmit
;
1166 #ifdef CONFIG_NET_DSA_TAG_TRAILER
1167 case DSA_TAG_PROTO_TRAILER
:
1168 p
->xmit
= trailer_netdev_ops
.xmit
;
1171 #ifdef CONFIG_NET_DSA_TAG_BRCM
1172 case DSA_TAG_PROTO_BRCM
:
1173 p
->xmit
= brcm_netdev_ops
.xmit
;
1177 p
->xmit
= dsa_slave_notag_xmit
;
1185 ds
->ports
[port
].netdev
= slave_dev
;
1186 ret
= register_netdev(slave_dev
);
1188 netdev_err(master
, "error %d registering interface %s\n",
1189 ret
, slave_dev
->name
);
1190 ds
->ports
[port
].netdev
= NULL
;
1191 free_netdev(slave_dev
);
1195 netif_carrier_off(slave_dev
);
1197 ret
= dsa_slave_phy_setup(p
, slave_dev
);
1199 netdev_err(master
, "error %d setting up slave phy\n", ret
);
1200 unregister_netdev(slave_dev
);
1201 free_netdev(slave_dev
);
1208 void dsa_slave_destroy(struct net_device
*slave_dev
)
1210 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1212 netif_carrier_off(slave_dev
);
1214 phy_disconnect(p
->phy
);
1215 unregister_netdev(slave_dev
);
1216 free_netdev(slave_dev
);
1219 static bool dsa_slave_dev_check(struct net_device
*dev
)
1221 return dev
->netdev_ops
== &dsa_slave_netdev_ops
;
1224 static int dsa_slave_port_upper_event(struct net_device
*dev
,
1225 unsigned long event
, void *ptr
)
1227 struct netdev_notifier_changeupper_info
*info
= ptr
;
1228 struct net_device
*upper
= info
->upper_dev
;
1232 case NETDEV_CHANGEUPPER
:
1233 if (netif_is_bridge_master(upper
)) {
1235 err
= dsa_slave_bridge_port_join(dev
, upper
);
1237 dsa_slave_bridge_port_leave(dev
);
1243 return notifier_from_errno(err
);
1246 static int dsa_slave_port_event(struct net_device
*dev
, unsigned long event
,
1250 case NETDEV_CHANGEUPPER
:
1251 return dsa_slave_port_upper_event(dev
, event
, ptr
);
1257 int dsa_slave_netdevice_event(struct notifier_block
*unused
,
1258 unsigned long event
, void *ptr
)
1260 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1262 if (dsa_slave_dev_check(dev
))
1263 return dsa_slave_port_event(dev
, event
, ptr
);