Skip to contentSkip to conversation

net: add define to describe link speed modes

PATCH Superseded Christian Marangi 2 participants 6 messages Dec 15, 2023

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover net: add define to describe link speed modes

This is a simple series to add a way to describe link speed modes number.

An additional helper is added and the phy_speeds is better documented and expanded to return just the modes number.
Documentation on the additional helper is not added to the phy.h as suggested from another patch where in double documentation define .c is preferred.

This is also needed in the upcoming changes in the netdev trigger for LEDs where phy_speeds functions is used to declare a more compact array instead of using a "big enough" approach.

Changes v3:
- Fix various compilation error (wrong revision pushed) Changes v2:
- Drop stupid enum-define hack
- Introduce helper function
- Document phy_speeds function
- Extent phy_speeds function

Christian Marangi (3):
net: phy: refactor and better document phy_speeds function
net: phy: add simple helper to return count of supported speeds
net: phy: led: dynamically allocate speed modes array

 drivers/net/phy/phy-core.c         | 50 +++++++++++++++++++++++++++---
 drivers/net/phy/phy.c              | 12 +++++++
 drivers/net/phy/phy_led_triggers.c | 16 ++++++++--
 include/linux/phy.h                |  2 ++
 4 files changed, 72 insertions(+), 8 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH v3 1/3 net: phy: refactor and better document phy_speeds function

Refactor the phy_speeds function to be more readable and understandable and add some documentation on it.

While on it extend it to take NULL speeds values to make it return only the count of speed modes in the passed mask.

---
 drivers/net/phy/phy-core.c | 50 ++++++++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 5 deletions(-)
drivers/net/phy/phy-core.c +45 -5
index 966c93cbe616..9618d89458d1 100644
@@ -317,17 +317,57 @@ phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
 }
 EXPORT_SYMBOL_GPL(phy_lookup_setting);
 
+/**
+ * phy_speeds - return all speeds in mask
+ * @speeds: pointer to array where to put the speed modes
+ * @size: size of array where to put the speed modes
+ * @mask: mask of speed modes to compare with
+ *
+ * Take mask, test bit in mask with the settings table and compose the
+ * speeds array based on that as many as size permits.
+ *
+ * With speeds NULL, only the number of detected modes is returned and
+ * no array is composed. (size value is ignored)
+ *
+ * Return the number of detected modes in mask.
+ */
 size_t phy_speeds(unsigned int *speeds, size_t size,
 		  unsigned long *mask)
 {
+	unsigned int curr_speed;
 	size_t count;
 	int i;
 
-	for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
-		if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
-		    test_bit(settings[i].bit, mask) &&
-		    (count == 0 || speeds[count - 1] != settings[i].speed))
-			speeds[count++] = settings[i].speed;
+	for (i = 0, count = 0; i < ARRAY_SIZE(settings); i++) {
+		/* Inconsistent mapping with ethtool modes? */
+		if (unlikely(settings[i].bit >= __ETHTOOL_LINK_MODE_MASK_NBITS))
+			return count;
+
+		/* Skip. Speed not in provided mask */
+		if (!test_bit(settings[i].bit, mask))
+			continue;
+
+		/* settings struct is set in descending order with
+		 * ordered speed modes. Detect a new speed mode by
+		 * checking if it's different than the current one.
+		 */
+		if (count == 0 || curr_speed != settings[i].speed) {
+			curr_speed = settings[i].speed;
+
+			/* With speeds not declared, we return only
+			 * the number of detected speed mode in the mask.
+			 */
+			if (speeds) {
+				/* No more space to put new modes */
+				if (count > size)
+					return count;
+
+				speeds[count] = curr_speed;
+			}
+
+			count++;
+		}
+	}
 
 	return count;
 }
Signature · 1 line
2.40.1
#2 lore raw

Re: [net-next PATCH v3 1/3] net: phy: refactor and better document phy_speeds function

Hi--

Quoted 22 lines · Christian Marangi

Refactor the phy_speeds function to be more readable and understandable and add some documentation on it.

While on it extend it to take NULL speeds values to make it return only the count of speed modes in the passed mask.

---
 drivers/net/phy/phy-core.c | 50 ++++++++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 5 deletions(-)
drivers/net/phy/phy-core.c +3 -0
index 966c93cbe616..9618d89458d1 100644
@@ -317,17 +317,57 @@ phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
 }
 EXPORT_SYMBOL_GPL(phy_lookup_setting);
 
+/**
+ * phy_speeds - return all speeds in mask
+ * @speeds: pointer to array where to put the speed modes

drivers/net/phy/phy-core.c phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact) · line 332

Quoted 10 lines
drivers/net/phy/phy-core.c +10 -0
+ * @size: size of array where to put the speed modes
+ * @mask: mask of speed modes to compare with
+ *
+ * Take mask, test bit in mask with the settings table and compose the
+ * speeds array based on that as many as size permits.
+ *
+ * With speeds NULL, only the number of detected modes is returned and
+ * no array is composed. (size value is ignored)
+ *
+ * Return the number of detected modes in mask.

* Return: the number of detected modes in mask.

Quoted 1 line
drivers/net/phy/phy-core.c +1 -0
+ */
Signature · 3 lines
#Randy
https://people.kernel.org/tglx/notes-about-netiquette
https://subspace.kernel.org/etiquette.html
#3 lore raw

PATCH v3 2/3 net: phy: add simple helper to return count of supported speeds

In reply to Christian Marangi

Add simple helper to return count of supported speeds for the passed PHY device.

This can be useful to know the number of speed modes to dynamically allocate a speed array for it.

---
 drivers/net/phy/phy.c | 12 ++++++++++++
 include/linux/phy.h   |  2 ++
 2 files changed, 14 insertions(+)
drivers/net/phy/phy.c +12 -0
index a5fa077650e8..311560e72126 100644
@@ -229,6 +229,18 @@ phy_find_valid(int speed, int duplex, unsigned long *supported)
 	return phy_lookup_setting(speed, duplex, supported, false);
 }
 
+/**
+ * phy_supported_speeds_num - return the number of all speeds currently
+ *			      supported by a phy device
+ * @phy: The phy device to return supported speeds of.
+ *
+ * Description: Returns the number of supported speeds.
+ */
+unsigned int phy_supported_speeds_num(struct phy_device *phy)
+{
+	return phy_speeds(NULL, 0, phy->supported);
+}
+
 /**
  * phy_supported_speeds - return all speeds currently supported by a phy device
  * @phy: The phy device to return supported speeds of.
include/linux/phy.h +2 -0
index 3cc52826f18e..52aa415fab0f 100644
@@ -202,6 +202,8 @@ static inline void phy_interface_set_rgmii(unsigned long *intf)
 	__set_bit(PHY_INTERFACE_MODE_RGMII_TXID, intf);
 }
 
+unsigned int phy_supported_speeds_num(struct phy_device *phy);
+
 /*
  * phy_supported_speeds - return all speeds currently supported by a PHY device
  */
Signature · 1 line
2.40.1
#4 lore raw

Re: [net-next PATCH v3 2/3] net: phy: add simple helper to return count of supported speeds

Quoted 17 lines · Christian Marangi

Add simple helper to return count of supported speeds for the passed PHY device.

This can be useful to know the number of speed modes to dynamically allocate a speed array for it.

---
 drivers/net/phy/phy.c | 12 ++++++++++++
 include/linux/phy.h   |  2 ++
 2 files changed, 14 insertions(+)
drivers/net/phy/phy.c +0 -0
index a5fa077650e8..311560e72126 100644

drivers/net/phy/phy.c phy_find_valid(int speed, int duplex, unsigned long *supported) · line 237

Quoted 9 lines
drivers/net/phy/phy.c +6 -0
@@ -229,6 +229,18 @@ phy_find_valid(int speed, int duplex, unsigned long *supported)
 	return phy_lookup_setting(speed, duplex, supported, false);
 }
 
+/**
+ * phy_supported_speeds_num - return the number of all speeds currently
+ *			      supported by a phy device
+ * @phy: The phy device to return supported speeds of.
+ *
+ * Description: Returns the number of supported speeds.

For kernel-doc, better to have that line as:

* Returns: the number of supported speeds.

Quoted 22 lines
drivers/net/phy/phy.c +6 -0
+ */
+unsigned int phy_supported_speeds_num(struct phy_device *phy)
+{
+	return phy_speeds(NULL, 0, phy->supported);
+}
+
 /**
  * phy_supported_speeds - return all speeds currently supported by a phy device
  * @phy: The phy device to return supported speeds of.
include/linux/phy.h +2 -0
index 3cc52826f18e..52aa415fab0f 100644
@@ -202,6 +202,8 @@ static inline void phy_interface_set_rgmii(unsigned long *intf)
 	__set_bit(PHY_INTERFACE_MODE_RGMII_TXID, intf);
 }
 
+unsigned int phy_supported_speeds_num(struct phy_device *phy);
+
 /*
  * phy_supported_speeds - return all speeds currently supported by a PHY device
  */
Signature · 3 lines
#Randy
https://people.kernel.org/tglx/notes-about-netiquette
https://subspace.kernel.org/etiquette.html
#5 lore raw

PATCH v3 3/3 net: phy: led: dynamically allocate speed modes array

In reply to Christian Marangi

Instead of defining a big enough array for speed modes use the newly introduced API to get the actual number of supported speed modes and dynamically allocate them.

Allocated space is freed at the end of the LED register loop.

---
 drivers/net/phy/phy_led_triggers.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/net/phy/phy_led_triggers.c +13 -3
index f550576eb9da..3b962ec13d90 100644
@@ -83,14 +83,21 @@ static void phy_led_trigger_unregister(struct phy_led_trigger *plt)
 
 int phy_led_triggers_register(struct phy_device *phy)
 {
+	unsigned int *speeds;
 	int i, err;
-	unsigned int speeds[50];
 
-	phy->phy_num_led_triggers = phy_supported_speeds(phy, speeds,
-							 ARRAY_SIZE(speeds));
+	phy->phy_num_led_triggers = phy_supported_speeds_num(phy);
 	if (!phy->phy_num_led_triggers)
 		return 0;
 
+	speeds = kmalloc_array(phy->phy_num_led_triggers, sizeof(*speeds),
+			       GFP_KERNEL);
+	if (!speeds)
+		return -ENOMEM;
+
+	/* Presence of speed modes already checked up */
+	phy_supported_speeds(phy, speeds, phy->phy_num_led_triggers);
+
 	phy->led_link_trigger = devm_kzalloc(&phy->mdio.dev,
 					     sizeof(*phy->led_link_trigger),
 					     GFP_KERNEL);
@@ -123,6 +130,8 @@ int phy_led_triggers_register(struct phy_device *phy)
 	phy->last_triggered = NULL;
 	phy_led_trigger_change_speed(phy);
 
+	kfree(speeds);
+
 	return 0;
 out_unreg:
 	while (i--)
@@ -134,6 +143,7 @@ int phy_led_triggers_register(struct phy_device *phy)
 	devm_kfree(&phy->mdio.dev, phy->led_link_trigger);
 	phy->led_link_trigger = NULL;
 out_clear:
+	kfree(speeds);
 	phy->phy_num_led_triggers = 0;
 	return err;
 }
Signature · 1 line
2.40.1