From patchwork Thu Dec 14 12:20:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 754173 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZyYLXhTC" Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61D9B112; Thu, 14 Dec 2023 04:20:59 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-33621d443a7so4323087f8f.3; Thu, 14 Dec 2023 04:20:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702556458; x=1703161258; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=hTkiSP+MxKrMId9/OvL1ZupidnFboSQTL72KpVM+M+Q=; b=ZyYLXhTCZJ20za5v8OZUf+gyif6hW9ysh1EVGUeRDIxnTYKgIxayk8ifMxc6DktRCq ofquPuFMl4uER6Js6HGPfVF8xJTDfqvVGyOWp5S3W5taCoKFeLu3NQyzvPvYym5oVhgM wpyX3dlhIx+i4llShr9NkIHMxeB2nuMWEn4n5I9pfiHO76DzJdBHJETHV9ymQH5md5hP PODLMVi45Lg4zio3GMQEK2bCEhrnmO4TOtXva3FNTKHbPTuVYZj3IWFfvY52XZsRIRX7 H8kj4UHdk/7Dh+CHgb9R17LAtpxgP7lWuWU5wfl5Gfw7UgidBSPLzF8o5H+3Minp0Pn+ 01wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702556458; x=1703161258; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hTkiSP+MxKrMId9/OvL1ZupidnFboSQTL72KpVM+M+Q=; b=Pqg/XtvGlVKC73Yn9Cr2wTS9K4W52VqpjgoAT31uJYXIfQnHwhMkRVz94Z/raG0mmt gyGnsMfFpmw9xC/swf1oJirAGFXPIVXZn84GA3I8+3RGON5qvSo2JR1Vt2/TQqz37iWw BMW7Tb7V/mf5GiGfUNCXM7UU8hfPmA28rFUJ6l1TJ1RhEMzhqtJHzY2jGs6cYCUMpFvz 3XmImcJoE8+V8jeX4lsxR2fhbf30jzHhBlWC2ppgs4OV8mXPucRSm53TgfMPrX+k/g/r Eqm8xCQjbQ3Kl4XgplQ8On6LDp6eUMUteysoa441BZYAatBkmlrJx5z1wzgZ8Cvq5sOx Kjrw== X-Gm-Message-State: AOJu0Yxvk6RDGQYp0KOXfxpaoDPVm59NX2XAQJBGlE2Ji8KVtEWhw23N 4iiQlZmqVgl1RAffXwwUrnQ= X-Google-Smtp-Source: AGHT+IHUx8uByEjYu0UiXRRWXssAP3UeLQtNDVDRl0YkNW+5n7cjkUTQ7hurZ5sk2Rc6YFxeYebErA== X-Received: by 2002:adf:ea44:0:b0:336:4704:d288 with SMTP id j4-20020adfea44000000b003364704d288mr457691wrn.83.1702556457498; Thu, 14 Dec 2023 04:20:57 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id s18-20020a5d5112000000b0033330ace23asm15988494wrt.73.2023.12.14.04.20.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 04:20:56 -0800 (PST) From: Christian Marangi To: Pavel Machek , Lee Jones , Andrew Lunn , Jakub Kicinski , Christian Marangi , Daniel Golle , =?utf-8?q?Marek_Beh=C3=BAn?= , "David S. Miller" , Li Zetao , linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org Subject: [PATCH v3 1/2] leds: trigger: netdev: display only supported link speed attribute Date: Thu, 14 Dec 2023 13:20:40 +0100 Message-Id: <20231214122041.17439-1-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: linux-leds@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 With the addition of more link speed mode to the netdev trigger, it was pointed out that there may be a problem with bloating the attribute list with modes that won't ever be supported by the trigger as the attached device name doesn't support them. To clear and address this problem, change the logic where these additional trigger modes are listed. Since the netdev trigger REQUIRE a device name to be set, attach to the device name change function additional logic to parse the supported link speed modes using ethtool APIs and show only the supported link speed modes attribute. Link speed attribute are refreshed on device_name set and on NETDEV_CHANGE events. This only apply to the link speed modes and every other mode is still provided by default. Signed-off-by: Christian Marangi --- Changes v3: - Use phy_speeds API to parse the ethtool mask Changes v2: - Use is_visibile instead of removing/adding attr drivers/leds/trigger/ledtrig-netdev.c | 104 ++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/drivers/leds/trigger/ledtrig-netdev.c b/drivers/leds/trigger/ledtrig-netdev.c index bd68da15c723..345e522bc44a 100644 --- a/drivers/leds/trigger/ledtrig-netdev.c +++ b/drivers/leds/trigger/ledtrig-netdev.c @@ -18,10 +18,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include "../leds.h" @@ -55,12 +57,16 @@ struct led_netdev_data { unsigned long mode; int link_speed; + unsigned int supported_link_speeds[20]; + int supported_link_speeds_num; u8 duplex; bool carrier_link_up; bool hw_control; }; +static const struct attribute_group netdev_trig_link_speed_attrs_group; + static void set_baseline_state(struct led_netdev_data *trigger_data) { int current_brightness; @@ -206,15 +212,25 @@ static bool can_hw_control(struct led_netdev_data *trigger_data) static void get_device_state(struct led_netdev_data *trigger_data) { struct ethtool_link_ksettings cmd; + int speeds_num; trigger_data->carrier_link_up = netif_carrier_ok(trigger_data->net_dev); - if (!trigger_data->carrier_link_up) + + if (__ethtool_get_link_ksettings(trigger_data->net_dev, &cmd)) return; - if (!__ethtool_get_link_ksettings(trigger_data->net_dev, &cmd)) { + if (trigger_data->carrier_link_up) { trigger_data->link_speed = cmd.base.speed; trigger_data->duplex = cmd.base.duplex; } + + /* Have a local copy of the link speed supported to not rtnl lock every time + * Modes are refreshed on any change event to handle mode changes + */ + speeds_num = phy_speeds(trigger_data->supported_link_speeds, + ARRAY_SIZE(trigger_data->supported_link_speeds), + cmd.link_modes.supported); + trigger_data->supported_link_speeds_num = speeds_num; } static ssize_t device_name_show(struct device *dev, @@ -257,6 +273,7 @@ static int set_device_name(struct led_netdev_data *trigger_data, trigger_data->carrier_link_up = false; trigger_data->link_speed = SPEED_UNKNOWN; trigger_data->duplex = DUPLEX_UNKNOWN; + trigger_data->supported_link_speeds_num = 0; if (trigger_data->net_dev != NULL) { rtnl_lock(); get_device_state(trigger_data); @@ -282,6 +299,10 @@ static ssize_t device_name_store(struct device *dev, if (ret < 0) return ret; + + /* Refresh link_speed visibility */ + sysfs_update_group(&dev->kobj, &netdev_trig_link_speed_attrs_group); + return size; } @@ -440,15 +461,72 @@ static ssize_t offloaded_show(struct device *dev, static DEVICE_ATTR_RO(offloaded); -static struct attribute *netdev_trig_attrs[] = { - &dev_attr_device_name.attr, - &dev_attr_link.attr, +static umode_t netdev_trig_link_speed_visible(struct kobject *kobj, + struct attribute *attr, int n) +{ + struct device *dev = kobj_to_dev(kobj); + struct led_netdev_data *trigger_data; + unsigned int *supported_link_speeds; + int speeds_num, i; + + trigger_data = led_trigger_get_drvdata(dev); + supported_link_speeds = trigger_data->supported_link_speeds; + speeds_num = trigger_data->supported_link_speeds_num; + + for (i = 0; i < speeds_num; i++) { + int speed = supported_link_speeds[i]; + + switch (speed) { + case SPEED_10: + if (attr == &dev_attr_link_10.attr) + return attr->mode; + break; + case SPEED_100: + if (attr == &dev_attr_link_100.attr) + return attr->mode; + break; + case SPEED_1000: + if (attr == &dev_attr_link_1000.attr) + return attr->mode; + break; + case SPEED_2500: + if (attr == &dev_attr_link_2500.attr) + return attr->mode; + break; + case SPEED_5000: + if (attr == &dev_attr_link_5000.attr) + return attr->mode; + break; + case SPEED_10000: + if (attr == &dev_attr_link_10000.attr) + return attr->mode; + break; + default: + return 0; + } + } + + return 0; +} + +static struct attribute *netdev_trig_link_speed_attrs[] = { &dev_attr_link_10.attr, &dev_attr_link_100.attr, &dev_attr_link_1000.attr, &dev_attr_link_2500.attr, &dev_attr_link_5000.attr, &dev_attr_link_10000.attr, + NULL +}; + +static const struct attribute_group netdev_trig_link_speed_attrs_group = { + .attrs = netdev_trig_link_speed_attrs, + .is_visible = netdev_trig_link_speed_visible, +}; + +static struct attribute *netdev_trig_attrs[] = { + &dev_attr_device_name.attr, + &dev_attr_link.attr, &dev_attr_full_duplex.attr, &dev_attr_half_duplex.attr, &dev_attr_rx.attr, @@ -457,7 +535,16 @@ static struct attribute *netdev_trig_attrs[] = { &dev_attr_offloaded.attr, NULL }; -ATTRIBUTE_GROUPS(netdev_trig); + +static const struct attribute_group netdev_trig_attrs_group = { + .attrs = netdev_trig_attrs, +}; + +static const struct attribute_group *netdev_trig_groups[] = { + &netdev_trig_attrs_group, + &netdev_trig_link_speed_attrs_group, + NULL, +}; static int netdev_trig_notify(struct notifier_block *nb, unsigned long evt, void *dv) @@ -466,6 +553,7 @@ static int netdev_trig_notify(struct notifier_block *nb, netdev_notifier_info_to_dev((struct netdev_notifier_info *)dv); struct led_netdev_data *trigger_data = container_of(nb, struct led_netdev_data, notifier); + struct led_classdev *led_cdev = trigger_data->led_cdev; if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER @@ -500,6 +588,10 @@ static int netdev_trig_notify(struct notifier_block *nb, case NETDEV_UP: case NETDEV_CHANGE: get_device_state(trigger_data); + /* Refresh link_speed visibility */ + if (evt == NETDEV_CHANGE) + sysfs_update_group(&led_cdev->dev->kobj, + &netdev_trig_link_speed_attrs_group); break; } From patchwork Thu Dec 14 12:20:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 755084 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lyKCowcq" Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88891113; Thu, 14 Dec 2023 04:21:00 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-3363e9240b4so329119f8f.0; Thu, 14 Dec 2023 04:21:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702556459; x=1703161259; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=XsKFunyIEF//ykMeSfV/a0SwdfxlBLJSsRhTiMN79G0=; b=lyKCowcqaNdBWnuIDXwY7XdtXVW2h/ll7v1U9VHpqysE8HAXIf333XVu+7UfnQpWFO DbzJudLlCtGlJubI9iVGKOhhx9gFNnQdK1vmnqKgtpvLQ1mPI3cXFmVkL7EHRliue+ei Ij3ytX9ffZQ0RE5MME1Ix24MAyS/zFLgXnco/sGC55DT+t3iqyRo9uLt4AeoFDuArjb0 4d3V2gxPiduGn/XS4o8Spr4HfG1PuRuWdgZhfqx1WURyhj+kPdyyIQXfl/9VCsYETvlK /EffLAH9wvRtNyzy9S8m80qLu5g4gwt0TTPco138d03R4ZUdSTHXz70jKRxo5VzAWCwn NDOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702556459; x=1703161259; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XsKFunyIEF//ykMeSfV/a0SwdfxlBLJSsRhTiMN79G0=; b=a5hGBSC924y+MW9a1+BFKq1amMMCdB8ZEfpONeUJiOTD1kpNR2LcYYAyUmULoS2F3i GEiPsU2d2/ZP70BsjBFC0boIHCb5I2JQEIjjA+2p2UZsY/ZEbI8ELUXxy7ogsub4vfJN fndxtYpNLZpbJ/5j0etz7DV8uj0pjK0DwjmkyYgFvhePrD7GCh1bHLOJSLjktrGM0IjD 2OUQBj/OBUrTdv89V4p0XDDsZithaL1NyALwyQ0hIn89rqiwAEuZsv9J55JMAj7h+Tnh iIP3evAQiQYqZ8JvIOd3zqE9xTYrhwAel+yO/reQ2xNT3G1ERCDXvoYngHIHoOVZAEan Rlgg== X-Gm-Message-State: AOJu0YxvYYNSXdV0skPLD2DEN6ZfIXm0r2J0VvuTSuDwqEuHEq/TlnDE rvvHfI/KM59W/W70tzt5vKU= X-Google-Smtp-Source: AGHT+IFd9GnOEo3uXLIFH2XNQ00FOp1Fd8656uT7gBZ3hma8d0rM0P9MECPNrOecViHOjk0xSlAzeg== X-Received: by 2002:adf:f609:0:b0:336:45aa:689a with SMTP id t9-20020adff609000000b0033645aa689amr676600wrp.33.1702556458826; Thu, 14 Dec 2023 04:20:58 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id s18-20020a5d5112000000b0033330ace23asm15988494wrt.73.2023.12.14.04.20.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 04:20:58 -0800 (PST) From: Christian Marangi To: Pavel Machek , Lee Jones , Andrew Lunn , Jakub Kicinski , Christian Marangi , Daniel Golle , =?utf-8?q?Marek_Beh=C3=BAn?= , "David S. Miller" , Li Zetao , linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org Subject: [PATCH v3 2/2] docs: ABI: sysfs-class-led-trigger-netdev: Document now hidable link_* Date: Thu, 14 Dec 2023 13:20:41 +0100 Message-Id: <20231214122041.17439-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231214122041.17439-1-ansuelsmth@gmail.com> References: <20231214122041.17439-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: linux-leds@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Document now hidable link speed modes for the LED netdev trigger. Link speed modes are now showed only if the named network device supports them and are hidden if not. Signed-off-by: Christian Marangi --- Changes v2: - Add this patch .../ABI/testing/sysfs-class-led-trigger-netdev | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-class-led-trigger-netdev b/Documentation/ABI/testing/sysfs-class-led-trigger-netdev index a6c307c4befa..ed46b37ab8a2 100644 --- a/Documentation/ABI/testing/sysfs-class-led-trigger-netdev +++ b/Documentation/ABI/testing/sysfs-class-led-trigger-netdev @@ -88,6 +88,8 @@ Description: speed of 10MBps of the named network device. Setting this value also immediately changes the LED state. + Present only if the named network device supports 10Mbps link speed. + What: /sys/class/leds//link_100 Date: Jun 2023 KernelVersion: 6.5 @@ -101,6 +103,8 @@ Description: speed of 100Mbps of the named network device. Setting this value also immediately changes the LED state. + Present only if the named network device supports 100Mbps link speed. + What: /sys/class/leds//link_1000 Date: Jun 2023 KernelVersion: 6.5 @@ -114,6 +118,8 @@ Description: speed of 1000Mbps of the named network device. Setting this value also immediately changes the LED state. + Present only if the named network device supports 1000Mbps link speed. + What: /sys/class/leds//link_2500 Date: Nov 2023 KernelVersion: 6.8 @@ -127,6 +133,8 @@ Description: speed of 2500Mbps of the named network device. Setting this value also immediately changes the LED state. + Present only if the named network device supports 2500Mbps link speed. + What: /sys/class/leds//link_5000 Date: Nov 2023 KernelVersion: 6.8 @@ -140,6 +148,8 @@ Description: speed of 5000Mbps of the named network device. Setting this value also immediately changes the LED state. + Present only if the named network device supports 5000Mbps link speed. + What: /sys/class/leds//link_10000 Date: Nov 2023 KernelVersion: 6.8 @@ -153,6 +163,8 @@ Description: speed of 10000Mbps of the named network device. Setting this value also immediately changes the LED state. + Present only if the named network device supports 10000Mbps link speed. + What: /sys/class/leds//half_duplex Date: Jun 2023 KernelVersion: 6.5