Skip to content

Commit 3b454b6

Browse files
oleremkuba-moo
authored andcommitted
net: dsa: microchip: ksz9477: Add Wake on Magic Packet support
Introduce Wake on Magic Packet (WoL) functionality to the ksz9477 driver. Major changes include: 1. Extending the `ksz9477_handle_wake_reason` function to identify Magic Packet wake events alongside existing wake reasons. 2. Updating the `ksz9477_get_wol` and `ksz9477_set_wol` functions to handle WAKE_MAGIC alongside the existing WAKE_PHY option, and to program the switch's MAC address register accordingly when Magic Packet wake-up is enabled. This change will prevent WAKE_MAGIC activation if the related port has a different MAC address compared to a MAC address already used by HSR or an already active WAKE_MAGIC on another port. 3. Adding a restriction in `ksz_port_set_mac_address` to prevent MAC address changes on ports with active Wake on Magic Packet, as the switch's MAC address register is utilized for this feature. Signed-off-by: Oleksij Rempel <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Reviewed-by: Vladimir Oltean <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 3a04927 commit 3b454b6

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

drivers/net/dsa/microchip/ksz9477.c

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ static int ksz9477_handle_wake_reason(struct ksz_device *dev, int port)
8181
if (!pme_status)
8282
return 0;
8383

84-
dev_dbg(dev->dev, "Wake event on port %d due to:%s%s\n", port,
84+
dev_dbg(dev->dev, "Wake event on port %d due to:%s%s%s\n", port,
85+
pme_status & PME_WOL_MAGICPKT ? " \"Magic Packet\"" : "",
8586
pme_status & PME_WOL_LINKUP ? " \"Link Up\"" : "",
8687
pme_status & PME_WOL_ENERGY ? " \"Enery detect\"" : "");
8788

@@ -109,10 +110,19 @@ void ksz9477_get_wol(struct ksz_device *dev, int port,
109110

110111
wol->supported = WAKE_PHY;
111112

113+
/* Check if the current MAC address on this port can be set
114+
* as global for WAKE_MAGIC support. The result may vary
115+
* dynamically based on other ports configurations.
116+
*/
117+
if (ksz_is_port_mac_global_usable(dev->ds, port))
118+
wol->supported |= WAKE_MAGIC;
119+
112120
ret = ksz_pread8(dev, port, REG_PORT_PME_CTRL, &pme_ctrl);
113121
if (ret)
114122
return;
115123

124+
if (pme_ctrl & PME_WOL_MAGICPKT)
125+
wol->wolopts |= WAKE_MAGIC;
116126
if (pme_ctrl & (PME_WOL_LINKUP | PME_WOL_ENERGY))
117127
wol->wolopts |= WAKE_PHY;
118128
}
@@ -134,10 +144,12 @@ void ksz9477_get_wol(struct ksz_device *dev, int port,
134144
int ksz9477_set_wol(struct ksz_device *dev, int port,
135145
struct ethtool_wolinfo *wol)
136146
{
137-
u8 pme_ctrl = 0;
147+
u8 pme_ctrl = 0, pme_ctrl_old = 0;
148+
bool magic_switched_off;
149+
bool magic_switched_on;
138150
int ret;
139151

140-
if (wol->wolopts & ~WAKE_PHY)
152+
if (wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
141153
return -EINVAL;
142154

143155
if (!dev->wakeup_source)
@@ -147,10 +159,42 @@ int ksz9477_set_wol(struct ksz_device *dev, int port,
147159
if (ret)
148160
return ret;
149161

162+
if (wol->wolopts & WAKE_MAGIC)
163+
pme_ctrl |= PME_WOL_MAGICPKT;
150164
if (wol->wolopts & WAKE_PHY)
151165
pme_ctrl |= PME_WOL_LINKUP | PME_WOL_ENERGY;
152166

153-
return ksz_pwrite8(dev, port, REG_PORT_PME_CTRL, pme_ctrl);
167+
ret = ksz_pread8(dev, port, REG_PORT_PME_CTRL, &pme_ctrl_old);
168+
if (ret)
169+
return ret;
170+
171+
if (pme_ctrl_old == pme_ctrl)
172+
return 0;
173+
174+
magic_switched_off = (pme_ctrl_old & PME_WOL_MAGICPKT) &&
175+
!(pme_ctrl & PME_WOL_MAGICPKT);
176+
magic_switched_on = !(pme_ctrl_old & PME_WOL_MAGICPKT) &&
177+
(pme_ctrl & PME_WOL_MAGICPKT);
178+
179+
/* To keep reference count of MAC address, we should do this
180+
* operation only on change of WOL settings.
181+
*/
182+
if (magic_switched_on) {
183+
ret = ksz_switch_macaddr_get(dev->ds, port, NULL);
184+
if (ret)
185+
return ret;
186+
} else if (magic_switched_off) {
187+
ksz_switch_macaddr_put(dev->ds);
188+
}
189+
190+
ret = ksz_pwrite8(dev, port, REG_PORT_PME_CTRL, pme_ctrl);
191+
if (ret) {
192+
if (magic_switched_on)
193+
ksz_switch_macaddr_put(dev->ds);
194+
return ret;
195+
}
196+
197+
return 0;
154198
}
155199

156200
static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev)
@@ -1106,6 +1150,11 @@ void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port)
11061150

11071151
/* clear pending wake flags */
11081152
ksz9477_handle_wake_reason(dev, port);
1153+
1154+
/* Disable all WoL options by default. Otherwise
1155+
* ksz_switch_macaddr_get/put logic will not work properly.
1156+
*/
1157+
ksz_pwrite8(dev, port, REG_PORT_PME_CTRL, 0);
11091158
}
11101159

11111160
void ksz9477_config_cpu_port(struct dsa_switch *ds)

drivers/net/dsa/microchip/ksz_common.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,6 +3569,7 @@ static int ksz_port_set_mac_address(struct dsa_switch *ds, int port,
35693569
const unsigned char *addr)
35703570
{
35713571
struct dsa_port *dp = dsa_to_port(ds, port);
3572+
struct ethtool_wolinfo wol;
35723573

35733574
if (dp->hsr_dev) {
35743575
dev_err(ds->dev,
@@ -3577,18 +3578,54 @@ static int ksz_port_set_mac_address(struct dsa_switch *ds, int port,
35773578
return -EBUSY;
35783579
}
35793580

3581+
ksz_get_wol(ds, dp->index, &wol);
3582+
if (wol.wolopts & WAKE_MAGIC) {
3583+
dev_err(ds->dev,
3584+
"Cannot change MAC address on port %d with active Wake on Magic Packet\n",
3585+
port);
3586+
return -EBUSY;
3587+
}
3588+
35803589
return 0;
35813590
}
35823591

3592+
/**
3593+
* ksz_is_port_mac_global_usable - Check if the MAC address on a given port
3594+
* can be used as a global address.
3595+
* @ds: Pointer to the DSA switch structure.
3596+
* @port: The port number on which the MAC address is to be checked.
3597+
*
3598+
* This function examines the MAC address set on the specified port and
3599+
* determines if it can be used as a global address for the switch.
3600+
*
3601+
* Return: true if the port's MAC address can be used as a global address, false
3602+
* otherwise.
3603+
*/
3604+
bool ksz_is_port_mac_global_usable(struct dsa_switch *ds, int port)
3605+
{
3606+
struct net_device *user = dsa_to_port(ds, port)->user;
3607+
const unsigned char *addr = user->dev_addr;
3608+
struct ksz_switch_macaddr *switch_macaddr;
3609+
struct ksz_device *dev = ds->priv;
3610+
3611+
ASSERT_RTNL();
3612+
3613+
switch_macaddr = dev->switch_macaddr;
3614+
if (switch_macaddr && !ether_addr_equal(switch_macaddr->addr, addr))
3615+
return false;
3616+
3617+
return true;
3618+
}
3619+
35833620
/* Program the switch's MAC address register with the MAC address of the
35843621
* requesting user port. This single address is used by the switch for multiple
35853622
* features, like HSR self-address filtering and WoL. Other user ports are
35863623
* allowed to share ownership of this address as long as their MAC address is
35873624
* the same. The user ports' MAC addresses must not change while they have
35883625
* ownership of the switch MAC address.
35893626
*/
3590-
static int ksz_switch_macaddr_get(struct dsa_switch *ds, int port,
3591-
struct netlink_ext_ack *extack)
3627+
int ksz_switch_macaddr_get(struct dsa_switch *ds, int port,
3628+
struct netlink_ext_ack *extack)
35923629
{
35933630
struct net_device *user = dsa_to_port(ds, port)->user;
35943631
const unsigned char *addr = user->dev_addr;
@@ -3628,7 +3665,7 @@ static int ksz_switch_macaddr_get(struct dsa_switch *ds, int port,
36283665
return 0;
36293666
}
36303667

3631-
static void ksz_switch_macaddr_put(struct dsa_switch *ds)
3668+
void ksz_switch_macaddr_put(struct dsa_switch *ds)
36323669
{
36333670
struct ksz_switch_macaddr *switch_macaddr;
36343671
struct ksz_device *dev = ds->priv;

drivers/net/dsa/microchip/ksz_common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,16 @@ int ksz_switch_register(struct ksz_device *dev);
390390
void ksz_switch_remove(struct ksz_device *dev);
391391

392392
void ksz_init_mib_timer(struct ksz_device *dev);
393+
bool ksz_is_port_mac_global_usable(struct dsa_switch *ds, int port);
393394
void ksz_r_mib_stats64(struct ksz_device *dev, int port);
394395
void ksz88xx_r_mib_stats64(struct ksz_device *dev, int port);
395396
void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
396397
bool ksz_get_gbit(struct ksz_device *dev, int port);
397398
phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit);
398399
extern const struct ksz_chip_data ksz_switch_chips[];
400+
int ksz_switch_macaddr_get(struct dsa_switch *ds, int port,
401+
struct netlink_ext_ack *extack);
402+
void ksz_switch_macaddr_put(struct dsa_switch *ds);
399403

400404
/* Common register access functions */
401405
static inline struct regmap *ksz_regmap_8(struct ksz_device *dev)

0 commit comments

Comments
 (0)