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 <net/rtnetlink.h>
19 #include <net/switchdev.h>
20 #include <linux/if_bridge.h>
21 #include <linux/netpoll.h>
24 /* slave mii_bus handling ***************************************************/
25 static int dsa_slave_phy_read(struct mii_bus
*bus
, int addr
, int reg
)
27 struct dsa_switch
*ds
= bus
->priv
;
29 if (ds
->phys_mii_mask
& (1 << addr
))
30 return ds
->drv
->phy_read(ds
, addr
, reg
);
35 static int dsa_slave_phy_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
37 struct dsa_switch
*ds
= bus
->priv
;
39 if (ds
->phys_mii_mask
& (1 << addr
))
40 return ds
->drv
->phy_write(ds
, addr
, reg
, val
);
45 void dsa_slave_mii_bus_init(struct dsa_switch
*ds
)
47 ds
->slave_mii_bus
->priv
= (void *)ds
;
48 ds
->slave_mii_bus
->name
= "dsa slave smi";
49 ds
->slave_mii_bus
->read
= dsa_slave_phy_read
;
50 ds
->slave_mii_bus
->write
= dsa_slave_phy_write
;
51 snprintf(ds
->slave_mii_bus
->id
, MII_BUS_ID_SIZE
, "dsa-%d:%.2x",
52 ds
->index
, ds
->pd
->sw_addr
);
53 ds
->slave_mii_bus
->parent
= ds
->master_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_update
)
107 ds
->drv
->port_stp_update(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_update
)
150 ds
->drv
->port_stp_update(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_bridge_check_vlan_range(struct dsa_switch
*ds
,
204 const struct net_device
*bridge
,
205 u16 vid_begin
, u16 vid_end
)
207 struct dsa_slave_priv
*p
;
208 struct net_device
*dev
, *vlan_br
;
209 DECLARE_BITMAP(members
, DSA_MAX_PORTS
);
210 DECLARE_BITMAP(untagged
, DSA_MAX_PORTS
);
214 if (!ds
->drv
->vlan_getnext
|| !vid_begin
)
220 err
= ds
->drv
->vlan_getnext(ds
, &vid
, members
, untagged
);
227 member
= find_first_bit(members
, DSA_MAX_PORTS
);
228 if (member
== DSA_MAX_PORTS
)
231 dev
= ds
->ports
[member
];
232 p
= netdev_priv(dev
);
233 vlan_br
= p
->bridge_dev
;
234 if (vlan_br
== bridge
)
237 netdev_dbg(vlan_br
, "hardware VLAN %d already in use\n", vid
);
239 } while (vid
< vid_end
);
241 return err
== -ENOENT
? 0 : err
;
244 static int dsa_slave_port_vlan_add(struct net_device
*dev
,
245 const struct switchdev_obj_port_vlan
*vlan
,
246 struct switchdev_trans
*trans
)
248 struct dsa_slave_priv
*p
= netdev_priv(dev
);
249 struct dsa_switch
*ds
= p
->parent
;
252 if (switchdev_trans_ph_prepare(trans
)) {
253 if (!ds
->drv
->port_vlan_prepare
|| !ds
->drv
->port_vlan_add
)
256 /* If the requested port doesn't belong to the same bridge as
257 * the VLAN members, fallback to software VLAN (hopefully).
259 err
= dsa_bridge_check_vlan_range(ds
, p
->bridge_dev
,
265 err
= ds
->drv
->port_vlan_prepare(ds
, p
->port
, vlan
, trans
);
269 err
= ds
->drv
->port_vlan_add(ds
, p
->port
, vlan
, trans
);
277 static int dsa_slave_port_vlan_del(struct net_device
*dev
,
278 const struct switchdev_obj_port_vlan
*vlan
)
280 struct dsa_slave_priv
*p
= netdev_priv(dev
);
281 struct dsa_switch
*ds
= p
->parent
;
283 if (!ds
->drv
->port_vlan_del
)
286 return ds
->drv
->port_vlan_del(ds
, p
->port
, vlan
);
289 static int dsa_slave_port_vlan_dump(struct net_device
*dev
,
290 struct switchdev_obj_port_vlan
*vlan
,
291 switchdev_obj_dump_cb_t
*cb
)
293 struct dsa_slave_priv
*p
= netdev_priv(dev
);
294 struct dsa_switch
*ds
= p
->parent
;
295 DECLARE_BITMAP(members
, DSA_MAX_PORTS
);
296 DECLARE_BITMAP(untagged
, DSA_MAX_PORTS
);
300 if (!ds
->drv
->vlan_getnext
|| !ds
->drv
->port_pvid_get
)
303 err
= ds
->drv
->port_pvid_get(ds
, p
->port
, &pvid
);
308 err
= ds
->drv
->vlan_getnext(ds
, &vid
, members
, untagged
);
312 if (!test_bit(p
->port
, members
))
315 memset(vlan
, 0, sizeof(*vlan
));
316 vlan
->vid_begin
= vlan
->vid_end
= vid
;
319 vlan
->flags
|= BRIDGE_VLAN_INFO_PVID
;
321 if (test_bit(p
->port
, untagged
))
322 vlan
->flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
324 err
= cb(&vlan
->obj
);
329 return err
== -ENOENT
? 0 : err
;
332 static int dsa_slave_port_fdb_add(struct net_device
*dev
,
333 const struct switchdev_obj_port_fdb
*fdb
,
334 struct switchdev_trans
*trans
)
336 struct dsa_slave_priv
*p
= netdev_priv(dev
);
337 struct dsa_switch
*ds
= p
->parent
;
340 if (!ds
->drv
->port_fdb_prepare
|| !ds
->drv
->port_fdb_add
)
343 if (switchdev_trans_ph_prepare(trans
))
344 ret
= ds
->drv
->port_fdb_prepare(ds
, p
->port
, fdb
, trans
);
346 ret
= ds
->drv
->port_fdb_add(ds
, p
->port
, fdb
, trans
);
351 static int dsa_slave_port_fdb_del(struct net_device
*dev
,
352 const struct switchdev_obj_port_fdb
*fdb
)
354 struct dsa_slave_priv
*p
= netdev_priv(dev
);
355 struct dsa_switch
*ds
= p
->parent
;
356 int ret
= -EOPNOTSUPP
;
358 if (ds
->drv
->port_fdb_del
)
359 ret
= ds
->drv
->port_fdb_del(ds
, p
->port
, fdb
);
364 static int dsa_slave_port_fdb_dump(struct net_device
*dev
,
365 struct switchdev_obj_port_fdb
*fdb
,
366 switchdev_obj_dump_cb_t
*cb
)
368 struct dsa_slave_priv
*p
= netdev_priv(dev
);
369 struct dsa_switch
*ds
= p
->parent
;
371 if (ds
->drv
->port_fdb_dump
)
372 return ds
->drv
->port_fdb_dump(ds
, p
->port
, fdb
, cb
);
377 static int dsa_slave_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
379 struct dsa_slave_priv
*p
= netdev_priv(dev
);
382 return phy_mii_ioctl(p
->phy
, ifr
, cmd
);
387 /* Return a bitmask of all ports being currently bridged within a given bridge
388 * device. Note that on leave, the mask will still return the bitmask of ports
389 * currently bridged, prior to port removal, and this is exactly what we want.
391 static u32
dsa_slave_br_port_mask(struct dsa_switch
*ds
,
392 struct net_device
*bridge
)
394 struct dsa_slave_priv
*p
;
398 for (port
= 0; port
< DSA_MAX_PORTS
; port
++) {
399 if (!dsa_is_port_initialized(ds
, port
))
402 p
= netdev_priv(ds
->ports
[port
]);
404 if (ds
->ports
[port
]->priv_flags
& IFF_BRIDGE_PORT
&&
405 p
->bridge_dev
== bridge
)
412 static int dsa_slave_stp_update(struct net_device
*dev
, u8 state
)
414 struct dsa_slave_priv
*p
= netdev_priv(dev
);
415 struct dsa_switch
*ds
= p
->parent
;
416 int ret
= -EOPNOTSUPP
;
418 if (ds
->drv
->port_stp_update
)
419 ret
= ds
->drv
->port_stp_update(ds
, p
->port
, state
);
424 static int dsa_slave_port_attr_set(struct net_device
*dev
,
425 const struct switchdev_attr
*attr
,
426 struct switchdev_trans
*trans
)
428 struct dsa_slave_priv
*p
= netdev_priv(dev
);
429 struct dsa_switch
*ds
= p
->parent
;
433 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
434 if (switchdev_trans_ph_prepare(trans
))
435 ret
= ds
->drv
->port_stp_update
? 0 : -EOPNOTSUPP
;
437 ret
= ds
->drv
->port_stp_update(ds
, p
->port
,
448 static int dsa_slave_port_obj_add(struct net_device
*dev
,
449 const struct switchdev_obj
*obj
,
450 struct switchdev_trans
*trans
)
454 /* For the prepare phase, ensure the full set of changes is feasable in
455 * one go in order to signal a failure properly. If an operation is not
456 * supported, return -EOPNOTSUPP.
460 case SWITCHDEV_OBJ_ID_PORT_FDB
:
461 err
= dsa_slave_port_fdb_add(dev
,
462 SWITCHDEV_OBJ_PORT_FDB(obj
),
465 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
466 err
= dsa_slave_port_vlan_add(dev
,
467 SWITCHDEV_OBJ_PORT_VLAN(obj
),
478 static int dsa_slave_port_obj_del(struct net_device
*dev
,
479 const struct switchdev_obj
*obj
)
484 case SWITCHDEV_OBJ_ID_PORT_FDB
:
485 err
= dsa_slave_port_fdb_del(dev
,
486 SWITCHDEV_OBJ_PORT_FDB(obj
));
488 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
489 err
= dsa_slave_port_vlan_del(dev
,
490 SWITCHDEV_OBJ_PORT_VLAN(obj
));
500 static int dsa_slave_port_obj_dump(struct net_device
*dev
,
501 struct switchdev_obj
*obj
,
502 switchdev_obj_dump_cb_t
*cb
)
507 case SWITCHDEV_OBJ_ID_PORT_FDB
:
508 err
= dsa_slave_port_fdb_dump(dev
,
509 SWITCHDEV_OBJ_PORT_FDB(obj
),
512 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
513 err
= dsa_slave_port_vlan_dump(dev
,
514 SWITCHDEV_OBJ_PORT_VLAN(obj
),
525 static int dsa_slave_bridge_port_join(struct net_device
*dev
,
526 struct net_device
*br
)
528 struct dsa_slave_priv
*p
= netdev_priv(dev
);
529 struct dsa_switch
*ds
= p
->parent
;
530 int ret
= -EOPNOTSUPP
;
534 if (ds
->drv
->port_join_bridge
)
535 ret
= ds
->drv
->port_join_bridge(ds
, p
->port
,
536 dsa_slave_br_port_mask(ds
, br
));
541 static int dsa_slave_bridge_port_leave(struct net_device
*dev
)
543 struct dsa_slave_priv
*p
= netdev_priv(dev
);
544 struct dsa_switch
*ds
= p
->parent
;
545 int ret
= -EOPNOTSUPP
;
548 if (ds
->drv
->port_leave_bridge
)
549 ret
= ds
->drv
->port_leave_bridge(ds
, p
->port
,
550 dsa_slave_br_port_mask(ds
, p
->bridge_dev
));
552 p
->bridge_dev
= NULL
;
554 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
555 * so allow it to be in BR_STATE_FORWARDING to be kept functional
557 dsa_slave_stp_update(dev
, BR_STATE_FORWARDING
);
562 static int dsa_slave_port_attr_get(struct net_device
*dev
,
563 struct switchdev_attr
*attr
)
565 struct dsa_slave_priv
*p
= netdev_priv(dev
);
566 struct dsa_switch
*ds
= p
->parent
;
569 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID
:
570 attr
->u
.ppid
.id_len
= sizeof(ds
->index
);
571 memcpy(&attr
->u
.ppid
.id
, &ds
->index
, attr
->u
.ppid
.id_len
);
580 static inline netdev_tx_t
dsa_netpoll_send_skb(struct dsa_slave_priv
*p
,
583 #ifdef CONFIG_NET_POLL_CONTROLLER
585 netpoll_send_skb(p
->netpoll
, skb
);
592 static netdev_tx_t
dsa_slave_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
594 struct dsa_slave_priv
*p
= netdev_priv(dev
);
595 struct sk_buff
*nskb
;
597 dev
->stats
.tx_packets
++;
598 dev
->stats
.tx_bytes
+= skb
->len
;
600 /* Transmit function may have to reallocate the original SKB */
601 nskb
= p
->xmit(skb
, dev
);
605 /* SKB for netpoll still need to be mangled with the protocol-specific
606 * tag to be successfully transmitted
608 if (unlikely(netpoll_tx_running(dev
)))
609 return dsa_netpoll_send_skb(p
, nskb
);
611 /* Queue the SKB for transmission on the parent interface, but
612 * do not modify its EtherType
614 nskb
->dev
= p
->parent
->dst
->master_netdev
;
615 dev_queue_xmit(nskb
);
620 static struct sk_buff
*dsa_slave_notag_xmit(struct sk_buff
*skb
,
621 struct net_device
*dev
)
623 /* Just return the original SKB */
628 /* ethtool operations *******************************************************/
630 dsa_slave_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
632 struct dsa_slave_priv
*p
= netdev_priv(dev
);
636 if (p
->phy
!= NULL
) {
637 err
= phy_read_status(p
->phy
);
639 err
= phy_ethtool_gset(p
->phy
, cmd
);
646 dsa_slave_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
648 struct dsa_slave_priv
*p
= netdev_priv(dev
);
651 return phy_ethtool_sset(p
->phy
, cmd
);
656 static void dsa_slave_get_drvinfo(struct net_device
*dev
,
657 struct ethtool_drvinfo
*drvinfo
)
659 strlcpy(drvinfo
->driver
, "dsa", sizeof(drvinfo
->driver
));
660 strlcpy(drvinfo
->version
, dsa_driver_version
, sizeof(drvinfo
->version
));
661 strlcpy(drvinfo
->fw_version
, "N/A", sizeof(drvinfo
->fw_version
));
662 strlcpy(drvinfo
->bus_info
, "platform", sizeof(drvinfo
->bus_info
));
665 static int dsa_slave_get_regs_len(struct net_device
*dev
)
667 struct dsa_slave_priv
*p
= netdev_priv(dev
);
668 struct dsa_switch
*ds
= p
->parent
;
670 if (ds
->drv
->get_regs_len
)
671 return ds
->drv
->get_regs_len(ds
, p
->port
);
677 dsa_slave_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *_p
)
679 struct dsa_slave_priv
*p
= netdev_priv(dev
);
680 struct dsa_switch
*ds
= p
->parent
;
682 if (ds
->drv
->get_regs
)
683 ds
->drv
->get_regs(ds
, p
->port
, regs
, _p
);
686 static int dsa_slave_nway_reset(struct net_device
*dev
)
688 struct dsa_slave_priv
*p
= netdev_priv(dev
);
691 return genphy_restart_aneg(p
->phy
);
696 static u32
dsa_slave_get_link(struct net_device
*dev
)
698 struct dsa_slave_priv
*p
= netdev_priv(dev
);
700 if (p
->phy
!= NULL
) {
701 genphy_update_link(p
->phy
);
708 static int dsa_slave_get_eeprom_len(struct net_device
*dev
)
710 struct dsa_slave_priv
*p
= netdev_priv(dev
);
711 struct dsa_switch
*ds
= p
->parent
;
713 if (ds
->pd
->eeprom_len
)
714 return ds
->pd
->eeprom_len
;
716 if (ds
->drv
->get_eeprom_len
)
717 return ds
->drv
->get_eeprom_len(ds
);
722 static int dsa_slave_get_eeprom(struct net_device
*dev
,
723 struct ethtool_eeprom
*eeprom
, u8
*data
)
725 struct dsa_slave_priv
*p
= netdev_priv(dev
);
726 struct dsa_switch
*ds
= p
->parent
;
728 if (ds
->drv
->get_eeprom
)
729 return ds
->drv
->get_eeprom(ds
, eeprom
, data
);
734 static int dsa_slave_set_eeprom(struct net_device
*dev
,
735 struct ethtool_eeprom
*eeprom
, u8
*data
)
737 struct dsa_slave_priv
*p
= netdev_priv(dev
);
738 struct dsa_switch
*ds
= p
->parent
;
740 if (ds
->drv
->set_eeprom
)
741 return ds
->drv
->set_eeprom(ds
, eeprom
, data
);
746 static void dsa_slave_get_strings(struct net_device
*dev
,
747 uint32_t stringset
, uint8_t *data
)
749 struct dsa_slave_priv
*p
= netdev_priv(dev
);
750 struct dsa_switch
*ds
= p
->parent
;
752 if (stringset
== ETH_SS_STATS
) {
753 int len
= ETH_GSTRING_LEN
;
755 strncpy(data
, "tx_packets", len
);
756 strncpy(data
+ len
, "tx_bytes", len
);
757 strncpy(data
+ 2 * len
, "rx_packets", len
);
758 strncpy(data
+ 3 * len
, "rx_bytes", len
);
759 if (ds
->drv
->get_strings
!= NULL
)
760 ds
->drv
->get_strings(ds
, p
->port
, data
+ 4 * len
);
764 static void dsa_slave_get_ethtool_stats(struct net_device
*dev
,
765 struct ethtool_stats
*stats
,
768 struct dsa_slave_priv
*p
= netdev_priv(dev
);
769 struct dsa_switch
*ds
= p
->parent
;
771 data
[0] = p
->dev
->stats
.tx_packets
;
772 data
[1] = p
->dev
->stats
.tx_bytes
;
773 data
[2] = p
->dev
->stats
.rx_packets
;
774 data
[3] = p
->dev
->stats
.rx_bytes
;
775 if (ds
->drv
->get_ethtool_stats
!= NULL
)
776 ds
->drv
->get_ethtool_stats(ds
, p
->port
, data
+ 4);
779 static int dsa_slave_get_sset_count(struct net_device
*dev
, int sset
)
781 struct dsa_slave_priv
*p
= netdev_priv(dev
);
782 struct dsa_switch
*ds
= p
->parent
;
784 if (sset
== ETH_SS_STATS
) {
788 if (ds
->drv
->get_sset_count
!= NULL
)
789 count
+= ds
->drv
->get_sset_count(ds
);
797 static void dsa_slave_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
799 struct dsa_slave_priv
*p
= netdev_priv(dev
);
800 struct dsa_switch
*ds
= p
->parent
;
802 if (ds
->drv
->get_wol
)
803 ds
->drv
->get_wol(ds
, p
->port
, w
);
806 static int dsa_slave_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
808 struct dsa_slave_priv
*p
= netdev_priv(dev
);
809 struct dsa_switch
*ds
= p
->parent
;
810 int ret
= -EOPNOTSUPP
;
812 if (ds
->drv
->set_wol
)
813 ret
= ds
->drv
->set_wol(ds
, p
->port
, w
);
818 static int dsa_slave_set_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
820 struct dsa_slave_priv
*p
= netdev_priv(dev
);
821 struct dsa_switch
*ds
= p
->parent
;
824 if (!ds
->drv
->set_eee
)
827 ret
= ds
->drv
->set_eee(ds
, p
->port
, p
->phy
, e
);
832 ret
= phy_ethtool_set_eee(p
->phy
, e
);
837 static int dsa_slave_get_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
839 struct dsa_slave_priv
*p
= netdev_priv(dev
);
840 struct dsa_switch
*ds
= p
->parent
;
843 if (!ds
->drv
->get_eee
)
846 ret
= ds
->drv
->get_eee(ds
, p
->port
, e
);
851 ret
= phy_ethtool_get_eee(p
->phy
, e
);
856 #ifdef CONFIG_NET_POLL_CONTROLLER
857 static int dsa_slave_netpoll_setup(struct net_device
*dev
,
858 struct netpoll_info
*ni
)
860 struct dsa_slave_priv
*p
= netdev_priv(dev
);
861 struct dsa_switch
*ds
= p
->parent
;
862 struct net_device
*master
= ds
->dst
->master_netdev
;
863 struct netpoll
*netpoll
;
866 netpoll
= kzalloc(sizeof(*netpoll
), GFP_KERNEL
);
870 err
= __netpoll_setup(netpoll
, master
);
876 p
->netpoll
= netpoll
;
881 static void dsa_slave_netpoll_cleanup(struct net_device
*dev
)
883 struct dsa_slave_priv
*p
= netdev_priv(dev
);
884 struct netpoll
*netpoll
= p
->netpoll
;
891 __netpoll_free_async(netpoll
);
894 static void dsa_slave_poll_controller(struct net_device
*dev
)
899 static const struct ethtool_ops dsa_slave_ethtool_ops
= {
900 .get_settings
= dsa_slave_get_settings
,
901 .set_settings
= dsa_slave_set_settings
,
902 .get_drvinfo
= dsa_slave_get_drvinfo
,
903 .get_regs_len
= dsa_slave_get_regs_len
,
904 .get_regs
= dsa_slave_get_regs
,
905 .nway_reset
= dsa_slave_nway_reset
,
906 .get_link
= dsa_slave_get_link
,
907 .get_eeprom_len
= dsa_slave_get_eeprom_len
,
908 .get_eeprom
= dsa_slave_get_eeprom
,
909 .set_eeprom
= dsa_slave_set_eeprom
,
910 .get_strings
= dsa_slave_get_strings
,
911 .get_ethtool_stats
= dsa_slave_get_ethtool_stats
,
912 .get_sset_count
= dsa_slave_get_sset_count
,
913 .set_wol
= dsa_slave_set_wol
,
914 .get_wol
= dsa_slave_get_wol
,
915 .set_eee
= dsa_slave_set_eee
,
916 .get_eee
= dsa_slave_get_eee
,
919 static const struct net_device_ops dsa_slave_netdev_ops
= {
920 .ndo_open
= dsa_slave_open
,
921 .ndo_stop
= dsa_slave_close
,
922 .ndo_start_xmit
= dsa_slave_xmit
,
923 .ndo_change_rx_flags
= dsa_slave_change_rx_flags
,
924 .ndo_set_rx_mode
= dsa_slave_set_rx_mode
,
925 .ndo_set_mac_address
= dsa_slave_set_mac_address
,
926 .ndo_fdb_add
= switchdev_port_fdb_add
,
927 .ndo_fdb_del
= switchdev_port_fdb_del
,
928 .ndo_fdb_dump
= switchdev_port_fdb_dump
,
929 .ndo_do_ioctl
= dsa_slave_ioctl
,
930 .ndo_get_iflink
= dsa_slave_get_iflink
,
931 #ifdef CONFIG_NET_POLL_CONTROLLER
932 .ndo_netpoll_setup
= dsa_slave_netpoll_setup
,
933 .ndo_netpoll_cleanup
= dsa_slave_netpoll_cleanup
,
934 .ndo_poll_controller
= dsa_slave_poll_controller
,
936 .ndo_bridge_getlink
= switchdev_port_bridge_getlink
,
937 .ndo_bridge_setlink
= switchdev_port_bridge_setlink
,
938 .ndo_bridge_dellink
= switchdev_port_bridge_dellink
,
941 static const struct switchdev_ops dsa_slave_switchdev_ops
= {
942 .switchdev_port_attr_get
= dsa_slave_port_attr_get
,
943 .switchdev_port_attr_set
= dsa_slave_port_attr_set
,
944 .switchdev_port_obj_add
= dsa_slave_port_obj_add
,
945 .switchdev_port_obj_del
= dsa_slave_port_obj_del
,
946 .switchdev_port_obj_dump
= dsa_slave_port_obj_dump
,
949 static struct device_type dsa_type
= {
953 static void dsa_slave_adjust_link(struct net_device
*dev
)
955 struct dsa_slave_priv
*p
= netdev_priv(dev
);
956 struct dsa_switch
*ds
= p
->parent
;
957 unsigned int status_changed
= 0;
959 if (p
->old_link
!= p
->phy
->link
) {
961 p
->old_link
= p
->phy
->link
;
964 if (p
->old_duplex
!= p
->phy
->duplex
) {
966 p
->old_duplex
= p
->phy
->duplex
;
969 if (p
->old_pause
!= p
->phy
->pause
) {
971 p
->old_pause
= p
->phy
->pause
;
974 if (ds
->drv
->adjust_link
&& status_changed
)
975 ds
->drv
->adjust_link(ds
, p
->port
, p
->phy
);
978 phy_print_status(p
->phy
);
981 static int dsa_slave_fixed_link_update(struct net_device
*dev
,
982 struct fixed_phy_status
*status
)
984 struct dsa_slave_priv
*p
= netdev_priv(dev
);
985 struct dsa_switch
*ds
= p
->parent
;
987 if (ds
->drv
->fixed_link_update
)
988 ds
->drv
->fixed_link_update(ds
, p
->port
, status
);
993 /* slave device setup *******************************************************/
994 static int dsa_slave_phy_connect(struct dsa_slave_priv
*p
,
995 struct net_device
*slave_dev
,
998 struct dsa_switch
*ds
= p
->parent
;
1000 p
->phy
= ds
->slave_mii_bus
->phy_map
[addr
];
1002 netdev_err(slave_dev
, "no phy at %d\n", addr
);
1006 /* Use already configured phy mode */
1007 if (p
->phy_interface
== PHY_INTERFACE_MODE_NA
)
1008 p
->phy_interface
= p
->phy
->interface
;
1009 phy_connect_direct(slave_dev
, p
->phy
, dsa_slave_adjust_link
,
1015 static int dsa_slave_phy_setup(struct dsa_slave_priv
*p
,
1016 struct net_device
*slave_dev
)
1018 struct dsa_switch
*ds
= p
->parent
;
1019 struct dsa_chip_data
*cd
= ds
->pd
;
1020 struct device_node
*phy_dn
, *port_dn
;
1021 bool phy_is_fixed
= false;
1025 port_dn
= cd
->port_dn
[p
->port
];
1026 mode
= of_get_phy_mode(port_dn
);
1028 mode
= PHY_INTERFACE_MODE_NA
;
1029 p
->phy_interface
= mode
;
1031 phy_dn
= of_parse_phandle(port_dn
, "phy-handle", 0);
1032 if (of_phy_is_fixed_link(port_dn
)) {
1033 /* In the case of a fixed PHY, the DT node associated
1034 * to the fixed PHY is the Port DT node
1036 ret
= of_phy_register_fixed_link(port_dn
);
1038 netdev_err(slave_dev
, "failed to register fixed PHY: %d\n", ret
);
1041 phy_is_fixed
= true;
1045 if (ds
->drv
->get_phy_flags
)
1046 phy_flags
= ds
->drv
->get_phy_flags(ds
, p
->port
);
1049 int phy_id
= of_mdio_parse_addr(&slave_dev
->dev
, phy_dn
);
1051 /* If this PHY address is part of phys_mii_mask, which means
1052 * that we need to divert reads and writes to/from it, then we
1053 * want to bind this device using the slave MII bus created by
1054 * DSA to make that happen.
1056 if (!phy_is_fixed
&& phy_id
>= 0 &&
1057 (ds
->phys_mii_mask
& (1 << phy_id
))) {
1058 ret
= dsa_slave_phy_connect(p
, slave_dev
, phy_id
);
1060 netdev_err(slave_dev
, "failed to connect to phy%d: %d\n", phy_id
, ret
);
1064 p
->phy
= of_phy_connect(slave_dev
, phy_dn
,
1065 dsa_slave_adjust_link
,
1071 if (p
->phy
&& phy_is_fixed
)
1072 fixed_phy_set_link_update(p
->phy
, dsa_slave_fixed_link_update
);
1074 /* We could not connect to a designated PHY, so use the switch internal
1078 ret
= dsa_slave_phy_connect(p
, slave_dev
, p
->port
);
1080 netdev_err(slave_dev
, "failed to connect to port %d: %d\n", p
->port
, ret
);
1084 netdev_info(slave_dev
, "attached PHY at address %d [%s]\n",
1085 p
->phy
->addr
, p
->phy
->drv
->name
);
1091 static struct lock_class_key dsa_slave_netdev_xmit_lock_key
;
1092 static void dsa_slave_set_lockdep_class_one(struct net_device
*dev
,
1093 struct netdev_queue
*txq
,
1096 lockdep_set_class(&txq
->_xmit_lock
,
1097 &dsa_slave_netdev_xmit_lock_key
);
1100 int dsa_slave_suspend(struct net_device
*slave_dev
)
1102 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1109 phy_suspend(p
->phy
);
1115 int dsa_slave_resume(struct net_device
*slave_dev
)
1117 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
1119 netif_device_attach(slave_dev
);
1129 int dsa_slave_create(struct dsa_switch
*ds
, struct device
*parent
,
1130 int port
, char *name
)
1132 struct net_device
*master
= ds
->dst
->master_netdev
;
1133 struct net_device
*slave_dev
;
1134 struct dsa_slave_priv
*p
;
1137 slave_dev
= alloc_netdev(sizeof(struct dsa_slave_priv
), name
,
1138 NET_NAME_UNKNOWN
, ether_setup
);
1139 if (slave_dev
== NULL
)
1142 slave_dev
->features
= master
->vlan_features
;
1143 slave_dev
->ethtool_ops
= &dsa_slave_ethtool_ops
;
1144 eth_hw_addr_inherit(slave_dev
, master
);
1145 slave_dev
->priv_flags
|= IFF_NO_QUEUE
;
1146 slave_dev
->netdev_ops
= &dsa_slave_netdev_ops
;
1147 slave_dev
->switchdev_ops
= &dsa_slave_switchdev_ops
;
1148 SET_NETDEV_DEVTYPE(slave_dev
, &dsa_type
);
1150 netdev_for_each_tx_queue(slave_dev
, dsa_slave_set_lockdep_class_one
,
1153 SET_NETDEV_DEV(slave_dev
, parent
);
1154 slave_dev
->dev
.of_node
= ds
->pd
->port_dn
[port
];
1155 slave_dev
->vlan_features
= master
->vlan_features
;
1157 p
= netdev_priv(slave_dev
);
1162 switch (ds
->dst
->tag_protocol
) {
1163 #ifdef CONFIG_NET_DSA_TAG_DSA
1164 case DSA_TAG_PROTO_DSA
:
1165 p
->xmit
= dsa_netdev_ops
.xmit
;
1168 #ifdef CONFIG_NET_DSA_TAG_EDSA
1169 case DSA_TAG_PROTO_EDSA
:
1170 p
->xmit
= edsa_netdev_ops
.xmit
;
1173 #ifdef CONFIG_NET_DSA_TAG_TRAILER
1174 case DSA_TAG_PROTO_TRAILER
:
1175 p
->xmit
= trailer_netdev_ops
.xmit
;
1178 #ifdef CONFIG_NET_DSA_TAG_BRCM
1179 case DSA_TAG_PROTO_BRCM
:
1180 p
->xmit
= brcm_netdev_ops
.xmit
;
1184 p
->xmit
= dsa_slave_notag_xmit
;
1192 ret
= dsa_slave_phy_setup(p
, slave_dev
);
1194 netdev_err(master
, "error %d setting up slave phy\n", ret
);
1195 free_netdev(slave_dev
);
1199 ds
->ports
[port
] = slave_dev
;
1200 ret
= register_netdev(slave_dev
);
1202 netdev_err(master
, "error %d registering interface %s\n",
1203 ret
, slave_dev
->name
);
1204 phy_disconnect(p
->phy
);
1205 ds
->ports
[port
] = NULL
;
1206 free_netdev(slave_dev
);
1210 netif_carrier_off(slave_dev
);
1215 static bool dsa_slave_dev_check(struct net_device
*dev
)
1217 return dev
->netdev_ops
== &dsa_slave_netdev_ops
;
1220 static int dsa_slave_master_changed(struct net_device
*dev
)
1222 struct net_device
*master
= netdev_master_upper_dev_get(dev
);
1223 struct dsa_slave_priv
*p
= netdev_priv(dev
);
1226 if (master
&& master
->rtnl_link_ops
&&
1227 !strcmp(master
->rtnl_link_ops
->kind
, "bridge"))
1228 err
= dsa_slave_bridge_port_join(dev
, master
);
1229 else if (dsa_port_is_bridged(p
))
1230 err
= dsa_slave_bridge_port_leave(dev
);
1235 int dsa_slave_netdevice_event(struct notifier_block
*unused
,
1236 unsigned long event
, void *ptr
)
1238 struct net_device
*dev
;
1242 case NETDEV_CHANGEUPPER
:
1243 dev
= netdev_notifier_info_to_dev(ptr
);
1244 if (!dsa_slave_dev_check(dev
))
1247 err
= dsa_slave_master_changed(dev
);
1248 if (err
&& err
!= -EOPNOTSUPP
)
1249 netdev_warn(dev
, "failed to reflect master change\n");