devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Russell King (Oracle)" <linux@armlinux.org.uk>
To: Christian Marangi <ansuelsmth@gmail.com>
Cc: Andrew Lunn <andrew@lunn.ch>,
	Florian Fainelli <f.fainelli@gmail.com>,
	Vladimir Oltean <olteanv@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Jonathan Corbet <corbet@lwn.net>, Pavel Machek <pavel@ucw.cz>,
	John Crispin <john@phrozen.org>,
	netdev@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-leds@vger.kernel.org, Tim Harvey <tharvey@gateworks.com>,
	Alexander Stein <alexander.stein@ew.tq-group.com>,
	Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Subject: Re: [PATCH v7 06/11] leds: trigger: netdev: add hardware control support
Date: Thu, 15 Dec 2022 17:07:31 +0000	[thread overview]
Message-ID: <Y5tUU5zA/lkYJza+@shell.armlinux.org.uk> (raw)
In-Reply-To: <20221214235438.30271-7-ansuelsmth@gmail.com>

On Thu, Dec 15, 2022 at 12:54:33AM +0100, Christian Marangi wrote:
> Add hardware control support for the Netdev trigger.
> The trigger on config change will check if the requested trigger can set
> to blink mode using LED hardware mode and if every blink mode is supported,
> the trigger will enable hardware mode with the requested configuration.
> If there is at least one trigger that is not supported and can't run in
> hardware mode, then software mode will be used instead.
> A validation is done on every value change and on fail the old value is
> restored and -EINVAL is returned.
> 
> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
> ---
>  drivers/leds/trigger/ledtrig-netdev.c | 155 +++++++++++++++++++++++++-
>  1 file changed, 149 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/leds/trigger/ledtrig-netdev.c b/drivers/leds/trigger/ledtrig-netdev.c
> index dd63cadb896e..ed019cb5867c 100644
> --- a/drivers/leds/trigger/ledtrig-netdev.c
> +++ b/drivers/leds/trigger/ledtrig-netdev.c
> @@ -37,6 +37,7 @@
>   */
>  
>  struct led_netdev_data {
> +	enum led_blink_modes blink_mode;
>  	spinlock_t lock;
>  
>  	struct delayed_work work;
> @@ -53,11 +54,105 @@ struct led_netdev_data {
>  	bool carrier_link_up;
>  };
>  
> +struct netdev_led_attr_detail {
> +	char *name;
> +	bool hardware_only;
> +	enum led_trigger_netdev_modes bit;
> +};
> +
> +static struct netdev_led_attr_detail attr_details[] = {
> +	{ .name = "link", .bit = TRIGGER_NETDEV_LINK},
> +	{ .name = "tx", .bit = TRIGGER_NETDEV_TX},
> +	{ .name = "rx", .bit = TRIGGER_NETDEV_RX},
> +};
> +
> +static bool validate_baseline_state(struct led_netdev_data *trigger_data)
> +{
> +	struct led_classdev *led_cdev = trigger_data->led_cdev;
> +	struct netdev_led_attr_detail *detail;
> +	u32 hw_blink_mode_supported = 0;
> +	bool force_sw = false;
> +	int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(attr_details); i++) {
> +		detail = &attr_details[i];
> +
> +		/* Mode not active, skip */
> +		if (!test_bit(detail->bit, &trigger_data->mode))
> +			continue;
> +
> +		/* Hardware only mode enabled on software controlled led */
> +		if (led_cdev->blink_mode == SOFTWARE_CONTROLLED &&
> +		    detail->hardware_only)
> +			return false;
> +
> +		/* Check if the mode supports hardware mode */
> +		if (led_cdev->blink_mode != SOFTWARE_CONTROLLED) {
> +			/* With a net dev set, force software mode.
> +			 * With modes are handled by hardware, led will blink
> +			 * based on his own events and will ignore any event
> +			 * from the provided dev.
> +			 */
> +			if (trigger_data->net_dev) {
> +				force_sw = true;
> +				continue;
> +			}
> +
> +			/* With empty dev, check if the mode is supported */
> +			if (led_trigger_blink_mode_is_supported(led_cdev, detail->bit))
> +				hw_blink_mode_supported |= BIT(detail->bit);
> +		}
> +	}
> +
> +	/* We can't run modes handled by both software and hardware.
> +	 * Check if we run hardware modes and check if all the modes
> +	 * can be handled by hardware.
> +	 */
> +	if (hw_blink_mode_supported && hw_blink_mode_supported != trigger_data->mode)
> +		return false;
> +
> +	/* Modes are valid. Decide now the running mode to later
> +	 * set the baseline.
> +	 * Software mode is enforced with net_dev set. With an empty
> +	 * one hardware mode is selected by default (if supported).
> +	 */
> +	if (force_sw || led_cdev->blink_mode == SOFTWARE_CONTROLLED)
> +		trigger_data->blink_mode = SOFTWARE_CONTROLLED;
> +	else
> +		trigger_data->blink_mode = HARDWARE_CONTROLLED;
> +
> +	return true;
> +}
> +
>  static void set_baseline_state(struct led_netdev_data *trigger_data)
>  {
> +	int i;
>  	int current_brightness;
> +	struct netdev_led_attr_detail *detail;
>  	struct led_classdev *led_cdev = trigger_data->led_cdev;
>  
> +	/* Modes already validated. Directly apply hw trigger modes */
> +	if (trigger_data->blink_mode == HARDWARE_CONTROLLED) {
> +		/* We are refreshing the blink modes. Reset them */
> +		led_cdev->hw_control_configure(led_cdev, BIT(TRIGGER_NETDEV_LINK),
> +					       BLINK_MODE_ZERO);
> +
> +		for (i = 0; i < ARRAY_SIZE(attr_details); i++) {
> +			detail = &attr_details[i];
> +
> +			if (!test_bit(detail->bit, &trigger_data->mode))
> +				continue;
> +
> +			led_cdev->hw_control_configure(led_cdev, BIT(detail->bit),
> +						       BLINK_MODE_ENABLE);
> +		}
> +
> +		led_cdev->hw_control_start(led_cdev);
> +
> +		return;
> +	}
> +
> +	/* Handle trigger modes by software */
>  	current_brightness = led_cdev->brightness;
>  	if (current_brightness)
>  		led_cdev->blink_brightness = current_brightness;
> @@ -100,10 +195,15 @@ static ssize_t device_name_store(struct device *dev,
>  				 size_t size)
>  {
>  	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
> +	struct net_device *old_net = trigger_data->net_dev;
> +	char old_device_name[IFNAMSIZ];
>  
>  	if (size >= IFNAMSIZ)
>  		return -EINVAL;
>  
> +	/* Backup old device name */
> +	memcpy(old_device_name, trigger_data->device_name, IFNAMSIZ);
> +
>  	cancel_delayed_work_sync(&trigger_data->work);
>  
>  	spin_lock_bh(&trigger_data->lock);
> @@ -122,6 +222,19 @@ static ssize_t device_name_store(struct device *dev,
>  		trigger_data->net_dev =
>  		    dev_get_by_name(&init_net, trigger_data->device_name);
>  
> +	if (!validate_baseline_state(trigger_data)) {
> +		/* Restore old net_dev and device_name */
> +		if (trigger_data->net_dev)
> +			dev_put(trigger_data->net_dev);
> +
> +		dev_hold(old_net);
> +		trigger_data->net_dev = old_net;
> +		memcpy(trigger_data->device_name, old_device_name, IFNAMSIZ);
> +
> +		spin_unlock_bh(&trigger_data->lock);
> +		return -EINVAL;

I'm not sure this is the best way... putting the net_dev but holding a
reference, to leter regain the reference via dev_hold() just feels
wrong. Also, I wonder what happens if two threads try to change the
netdev together - will the read of the old device name be potentially
corrupted (since we're not holding the trigger's lock?)

Maybe instead:

+	struct net_device *old_net;
...
-	if (trigger_data->net_dev) {
-		dev_put(trigger_data->net_dev);
-		trigger_data->net_dev = NULL;
-	}
+	old_net = trigger_data->net_dev;
+	trigger_data->net_dev = NULL;
+	memcpy(old_device_name, trigger_data->device_name, IFNAMSIZ);
...
	... extract out the setup of trigger_data->device_name
...
+	if (!validate_baseline_state(trigger_data)) {
+		if (trigger_data->net_dev)
+			dev_put(trigger_data->net_dev);
+
+		/* Restore device settings */
+		trigger_data->net_dev = old_dev;
+		memcpy(trigger_data->device_name, old_device_name, IFNAMSIZ);
+		spin_unlock_bh(&trigger_data->lock);
+		return -EINVAL;
+	} else {
+		dev_put(old_net);
+	}

would be safer all round?

One thought on this approach though - if one has a PHY that supports
"activity" but not independent "rx" and "tx" activity indications
and it doesn't support software control, how would one enable activity
mode? There isn't a way to simultaneously enable both at the same
time... However, I need to check whether there are any PHYs that fall
into this category.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

  parent reply	other threads:[~2022-12-15 17:09 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-14 23:54 [PATCH v7 00/11] Adds support for PHY LEDs with offload triggers Christian Marangi
2022-12-14 23:54 ` [PATCH v7 01/11] leds: add support for hardware driven LEDs Christian Marangi
2022-12-15  4:40   ` kernel test robot
2022-12-15  5:10   ` kernel test robot
2022-12-15 16:13   ` Russell King (Oracle)
2022-12-16 16:45     ` Christian Marangi
2022-12-16 16:51       ` Russell King (Oracle)
2022-12-20 23:35       ` Andrew Lunn
2023-01-03  5:40   ` Bagas Sanjaya
2022-12-14 23:54 ` [PATCH v7 02/11] leds: add function to configure hardware controlled LED Christian Marangi
2022-12-15 16:30   ` Russell King (Oracle)
2022-12-16 16:58     ` Christian Marangi
2022-12-14 23:54 ` [PATCH v7 03/11] leds: trigger: netdev: drop NETDEV_LED_MODE_LINKUP from mode Christian Marangi
2022-12-14 23:54 ` [PATCH v7 04/11] leds: trigger: netdev: rename and expose NETDEV trigger enum modes Christian Marangi
2022-12-20 23:48   ` Andrew Lunn
2022-12-14 23:54 ` [PATCH v7 05/11] leds: trigger: netdev: convert device attr to macro Christian Marangi
2022-12-14 23:54 ` [PATCH v7 06/11] leds: trigger: netdev: add hardware control support Christian Marangi
2022-12-15  5:31   ` kernel test robot
2022-12-15 15:27   ` Alexander Stein
2022-12-16 17:00     ` Christian Marangi
2023-01-02 12:44       ` Alexander Stein
2022-12-15 17:07   ` Russell King (Oracle) [this message]
2022-12-16 17:09     ` Christian Marangi
2022-12-20 23:59       ` Andrew Lunn
2022-12-21  9:54         ` Russell King (Oracle)
2022-12-21 13:00           ` Christian Marangi
2022-12-21 13:10             ` Andrew Lunn
2022-12-14 23:54 ` [PATCH v7 07/11] leds: trigger: netdev: use mutex instead of spinlocks Christian Marangi
2022-12-14 23:54 ` [PATCH v7 08/11] leds: trigger: netdev: add available mode sysfs attr Christian Marangi
2022-12-14 23:54 ` [PATCH v7 09/11] leds: trigger: netdev: add additional hardware only triggers Christian Marangi
2022-12-15 17:35   ` Russell King (Oracle)
2022-12-16 17:17     ` Christian Marangi
2022-12-21  0:12     ` Andrew Lunn
2022-12-21 12:56       ` Christian Marangi
2022-12-14 23:54 ` [PATCH v7 10/11] net: dsa: qca8k: add LEDs support Christian Marangi
2022-12-15  3:50   ` kernel test robot
2022-12-15  3:54   ` Arun.Ramadoss
2022-12-15 17:49   ` Russell King (Oracle)
2022-12-16 17:48     ` Christian Marangi
2022-12-20 23:11     ` Andrew Lunn
2022-12-14 23:54 ` [PATCH v7 11/11] dt-bindings: net: dsa: qca8k: add LEDs definition example Christian Marangi
2022-12-20 17:39   ` Rob Herring
2022-12-20 23:20     ` Andrew Lunn
2022-12-21  1:41       ` Andrew Lunn
2022-12-21 12:54         ` Christian Marangi
2022-12-21 12:59           ` Andrew Lunn
2022-12-21 15:29           ` Rob Herring
2023-01-29 20:43           ` Sander Vanheule
2023-01-29 22:02             ` Andrew Lunn
2023-01-30 10:59               ` Sander Vanheule
2023-01-30 13:48                 ` Andrew Lunn
2022-12-20 23:23   ` Andrew Lunn
2022-12-15 15:34 ` [PATCH v7 00/11] Adds support for PHY LEDs with offload triggers Alexander Stein

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=Y5tUU5zA/lkYJza+@shell.armlinux.org.uk \
    --to=linux@armlinux.org.uk \
    --cc=alexander.stein@ew.tq-group.com \
    --cc=andrew@lunn.ch \
    --cc=ansuelsmth@gmail.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=edumazet@google.com \
    --cc=f.fainelli@gmail.com \
    --cc=john@phrozen.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=pabeni@redhat.com \
    --cc=pavel@ucw.cz \
    --cc=rasmus.villemoes@prevas.dk \
    --cc=robh+dt@kernel.org \
    --cc=tharvey@gateworks.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).