netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oleksij Rempel <o.rempel@pengutronix.de>
To: vtpieter@gmail.com
Cc: devicetree@vger.kernel.org, woojung.huh@microchip.com,
	UNGLinuxDriver@microchip.com, netdev@vger.kernel.org,
	Pieter Van Trappen <pieter.van.trappen@cern.ch>
Subject: Re: [PATCH 2/4] net: dsa: microchip: ksz8795: add Wake on LAN support
Date: Fri, 19 Jul 2024 15:46:42 +0200	[thread overview]
Message-ID: <ZppuQo9sGdYJWgBQ@pengutronix.de> (raw)
In-Reply-To: <20240717193725.469192-3-vtpieter@gmail.com>

Hi Pieter,

If I see it correctly, the only difference between KSZ9477 and KSZ8795
code is the register access. Even bit offsets are identical. I do not
think indirect register access is good justification for duplication
this amount of code.

On Wed, Jul 17, 2024 at 09:37:23PM +0200, vtpieter@gmail.com wrote:
>  
> +static int ksz8_ind_read8(struct ksz_device *dev, u8 table, u16 addr, u8 *val)

Will be good to add comment for this function.

....
> +/**
> + * ksz8_handle_wake_reason - Handle wake reason on a specified port.
> + * @dev: The device structure.
> + * @port: The port number.
> + *
> + * This function reads the PME (Power Management Event) status register of a
> + * specified port to determine the wake reason. If there is no wake event, it
> + * returns early. Otherwise, it logs the wake reason which could be due to a
> + * "Magic Packet", "Link Up", or "Energy Detect" event. The PME status register
> + * is then cleared to acknowledge the handling of the wake event; followed by
> + * clearing the global Interrupt Status Register.
> + *
> + * Return: 0 on success, or an error code on failure.
> + */
> +static int ksz8_handle_wake_reason(struct ksz_device *dev, int port)
> +{
> +	u8 pme_status;
> +	int ret;
> +
> +	ret = ksz8_ind_read8(dev, TABLE_PME_PORT(port), REG_IND_PORT_PME_STATUS, &pme_status);
> +	if (ret)
> +		return ret;
> +
> +	if (!pme_status)
> +		return 0;
> +
> +	dev_dbg(dev->dev, "Wake event on port %d due to:%s%s%s\n", port,
> +		pme_status & PME_WOL_MAGICPKT ? " \"Magic Packet\"" : "",
> +		pme_status & PME_WOL_LINKUP ? " \"Link Up\"" : "",
> +		pme_status & PME_WOL_ENERGY ? " \"Energy detect\"" : "");
> +
> +	ret = ksz8_ind_write8(dev, TABLE_PME_PORT(port), REG_IND_PORT_PME_STATUS, pme_status);
> +	if (ret)
> +		return ret;
> +
> +	ksz_read8(dev, REG_INT_STATUS, &pme_status);

Recycling a variable with use case specific name, make the code more
confusing. Use "var" far all or "int_status" for this case.

> +	return ksz_write8(dev, REG_INT_STATUS, pme_status && INT_PME);

"pme_status && INT_PME" will write BIT(0) instead of BIT(4) which should
be written in this case - So, it should be "pme_status & INT_PME".

Instead of ksz_read8(dev, REG_INT_STATUS + ksz_write8, you can use one
ksz_rmw8()

> +}
> +
> +/**
> + * ksz8_get_wol - Get Wake-on-LAN settings for a specified port.
> + * @dev: The device structure.
> + * @port: The port number.
> + * @wol: Pointer to ethtool Wake-on-LAN settings structure.
> + *
> + * This function checks the PME 'wakeup-source' property from the
> + * device tree. If enabled, it sets the supported and active WoL
> + * flags.

This is a bit confusing - this function do not checks devicetree properly.
It only checks if the wakeup_source flag is set. In current code state, it is
set from devicetree properly.

> + */
> +void ksz8_get_wol(struct ksz_device *dev, int port,
> +		  struct ethtool_wolinfo *wol)
> +{
> +	u8 pme_ctrl;
> +	int ret;
> +
> +	if (!dev->wakeup_source)
> +		return;
> +
> +	wol->supported = WAKE_PHY;
> +
> +	/* Check if the current MAC address on this port can be set
> +	 * as global for WAKE_MAGIC support. The result may vary
> +	 * dynamically based on other ports configurations.
> +	 */
> +	if (ksz_is_port_mac_global_usable(dev->ds, port))
> +		wol->supported |= WAKE_MAGIC;
> +
> +	ret = ksz8_ind_read8(dev, TABLE_PME_PORT(port), REG_IND_PORT_PME_CTRL, &pme_ctrl);
> +	if (ret)
> +		return;
> +
> +	if (pme_ctrl & PME_WOL_MAGICPKT)
> +		wol->wolopts |= WAKE_MAGIC;
> +	if (pme_ctrl & (PME_WOL_LINKUP | PME_WOL_ENERGY))
> +		wol->wolopts |= WAKE_PHY;
> +}
> +
> +/**
> + * ksz8_set_wol - Set Wake-on-LAN settings for a specified port.
> + * @dev: The device structure.
> + * @port: The port number.
> + * @wol: Pointer to ethtool Wake-on-LAN settings structure.
> + *
> + * This function configures Wake-on-LAN (WoL) settings for a specified port.
> + * It validates the provided WoL options, checks if PME is enabled via the
> + * switch's device tree property, clears any previous wake reasons,

Same here, the "device tree" related part of comment can bit rot with
time.

> + * and sets the Magic Packet flag in the port's PME control register if
> + * specified.
> + *
> + * Return: 0 on success, or other error codes on failure.
> + */
> +int ksz8_set_wol(struct ksz_device *dev, int port,
> +/**
> + * ksz9477_wol_pre_shutdown - Prepares the switch device for shutdown while

s/ksz9477_wol_pre_shutdown/ksz8_wol_pre_shutdown

> + *                            considering Wake-on-LAN (WoL) settings.
> + * @dev: The switch device structure.
> + * @wol_enabled: Pointer to a boolean which will be set to true if WoL is
> + *               enabled on any port.
> + *
> + * This function prepares the switch device for a safe shutdown while taking
> + * into account the Wake-on-LAN (WoL) settings on the user ports. It updates
> + * the wol_enabled flag accordingly to reflect whether WoL is active on any
> + * port. It also sets the PME output pin enable with the polarity specified
> + * through the device-tree.
> + */
> +void ksz8_wol_pre_shutdown(struct ksz_device *dev, bool *wol_enabled)
> +{

> diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c
> index b074b4bb0629..61403898c1f4 100644
> --- a/drivers/net/dsa/microchip/ksz_common.c
> +++ b/drivers/net/dsa/microchip/ksz_common.c
> @@ -307,6 +307,9 @@ static const struct ksz_dev_ops ksz8_dev_ops = {
>  	.init = ksz8_switch_init,
>  	.exit = ksz8_switch_exit,
>  	.change_mtu = ksz8_change_mtu,
> +	.get_wol = ksz8_get_wol,
> +	.set_wol = ksz8_set_wol,
> +	.wol_pre_shutdown = ksz8_wol_pre_shutdown,

This part will break on KSZ8830 variants. There is no WoL support.

Regards,
Oleksij
-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

  parent reply	other threads:[~2024-07-19 13:46 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-17 19:37 [PATCH 0/4] net: dsa: microchip: ksz8795: add Wake on LAN support vtpieter
2024-07-17 19:37 ` [PATCH 1/4] dt-bindings: net: dsa: microchip: add microchip,pme-active-high flag vtpieter
2024-07-17 19:37   ` [PATCH 2/4] net: dsa: microchip: ksz8795: add Wake on LAN support vtpieter
2024-07-17 19:37     ` [PATCH 3/4] net: dsa: microchip: check erratum workaround through indirect register read vtpieter
2024-07-17 19:37       ` [PATCH 4/4] net: dsa: microchip: ksz9477: correct description of WoL functions vtpieter
2024-07-19 13:52         ` Oleksij Rempel
2024-07-18  2:24       ` [PATCH 3/4] net: dsa: microchip: check erratum workaround through indirect register read Andrew Lunn
2024-07-18 13:45         ` Pieter
2024-07-19 13:46     ` Oleksij Rempel [this message]
2024-07-20 12:44       ` [PATCH 2/4] net: dsa: microchip: ksz8795: add Wake on LAN support Pieter
2024-07-19 12:36   ` [PATCH 1/4] dt-bindings: net: dsa: microchip: add microchip,pme-active-high flag Oleksij Rempel
2024-07-23  2:42   ` Rob Herring (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZppuQo9sGdYJWgBQ@pengutronix.de \
    --to=o.rempel@pengutronix.de \
    --cc=UNGLinuxDriver@microchip.com \
    --cc=devicetree@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pieter.van.trappen@cern.ch \
    --cc=vtpieter@gmail.com \
    --cc=woojung.huh@microchip.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).