Skip to contentSkip to conversation

net: ethernet: renesas: rcar_gen4_ptp: Hide private data

PATCH Applied Niklas Söderlund 3 participants 8 messages Feb 1 – Feb 4

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover net: ethernet: renesas: rcar_gen4_ptp: Hide private data

Hello,

The R-Car Gen4 PTP module started out as an exclusive feature of a single driver, but have since been extended to cover both R-Car Switch and TSN driver implementations on Gen4.

The feature have already been extended to be built as its own module with an interface exposed thru a local header file. The header file however also exposes the modules private data structure. The two existing users have already started to poke at members of the struct.

The exposed private data being manipulated by users makes refactoring and future rework hard as the interface for the module becomes to chaotic. This small series aims to create two helpers to hide the private data.

This is done as a small preparation before a third, new, users of the Gen4 PTP will be added in a follow up series.

Niklas Söderlund (4):
net: ethernet: renesas: rcar_gen4_ptp: Move address assignment
net: ethernet: renesas: rcar_gen4_ptp: Add helper to get clock index
net: ethernet: renesas: rcar_gen4_ptp: Add helper to read time
net: ethernet: renesas: rcar_gen4_ptp: Hide private data from users

 drivers/net/ethernet/renesas/rcar_gen4_ptp.c | 34 +++++++++++++++++++-
 drivers/net/ethernet/renesas/rcar_gen4_ptp.h | 18 ++++-------
 drivers/net/ethernet/renesas/rswitch_main.c  | 11 +++----
 drivers/net/ethernet/renesas/rtsn.c          | 30 +++++++----------
 4 files changed, 57 insertions(+), 36 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/4 net: ethernet: renesas: rcar_gen4_ptp: Move address assignment

Instead of accessing the Gen4 PTP specific structure directly in drivers move the device address assignment into the preparation call. This is done in preparation to completely hide the Gen4 PTP specific structure from users.

---
 drivers/net/ethernet/renesas/rcar_gen4_ptp.c |  5 ++++-
 drivers/net/ethernet/renesas/rcar_gen4_ptp.h |  3 ++-
 drivers/net/ethernet/renesas/rswitch_main.c  |  9 ++++-----
 drivers/net/ethernet/renesas/rtsn.c          | 19 ++++++++++---------
 4 files changed, 20 insertions(+), 16 deletions(-)
drivers/net/ethernet/renesas/rcar_gen4_ptp.c +4 -1
index d0979abd36de..3fd835128cc8 100644
@@ -168,7 +168,8 @@ int rcar_gen4_ptp_unregister(struct rcar_gen4_ptp_private *ptp_priv)
 }
 EXPORT_SYMBOL_GPL(rcar_gen4_ptp_unregister);
 
-struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev)
+struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev,
+						  void __iomem *addr)
 {
 	struct rcar_gen4_ptp_private *ptp;
 
@@ -178,6 +179,8 @@ struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev)
 
 	ptp->info = rcar_gen4_ptp_info;
 
+	ptp->addr = addr;
+
 	return ptp;
 }
 EXPORT_SYMBOL_GPL(rcar_gen4_ptp_alloc);
drivers/net/ethernet/renesas/rcar_gen4_ptp.h +2 -1
index 9a9c232c854e..b71aba873795 100644
@@ -20,6 +20,7 @@ struct rcar_gen4_ptp_private {
 
 int rcar_gen4_ptp_register(struct rcar_gen4_ptp_private *ptp_priv, u32 rate);
 int rcar_gen4_ptp_unregister(struct rcar_gen4_ptp_private *ptp_priv);
-struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev);
+struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev,
+						  void __iomem *addr);
 
 #endif	/* #ifndef __RCAR_GEN4_PTP_H__ */
drivers/net/ethernet/renesas/rswitch_main.c +4 -5
index e14b21148f27..ab0b40d4f4fb 100644
@@ -2150,17 +2150,16 @@ static int renesas_eth_sw_probe(struct platform_device *pdev)
 	if (attr)
 		priv->etha_no_runtime_change = true;
 
-	priv->ptp_priv = rcar_gen4_ptp_alloc(pdev);
-	if (!priv->ptp_priv)
-		return -ENOMEM;
-
 	platform_set_drvdata(pdev, priv);
 	priv->pdev = pdev;
 	priv->addr = devm_ioremap_resource(&pdev->dev, res);
 	if (IS_ERR(priv->addr))
 		return PTR_ERR(priv->addr);
 
-	priv->ptp_priv->addr = priv->addr + RSWITCH_GPTP_OFFSET_S4;
+	priv->ptp_priv =
+		rcar_gen4_ptp_alloc(pdev, priv->addr + RSWITCH_GPTP_OFFSET_S4);
+	if (!priv->ptp_priv)
+		return -ENOMEM;
 
 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
 	if (ret < 0) {
drivers/net/ethernet/renesas/rtsn.c +10 -9
index fdb1e7b7fb06..d4b955c87f1b 100644
@@ -1227,6 +1227,7 @@ static int rtsn_probe(struct platform_device *pdev)
 {
 	struct rtsn_private *priv;
 	struct net_device *ndev;
+	void __iomem *ptpaddr;
 	struct resource *res;
 	int ret;
 
@@ -1239,12 +1240,6 @@ static int rtsn_probe(struct platform_device *pdev)
 	priv->pdev = pdev;
 	priv->ndev = ndev;
 
-	priv->ptp_priv = rcar_gen4_ptp_alloc(pdev);
-	if (!priv->ptp_priv) {
-		ret = -ENOMEM;
-		goto error_free;
-	}
-
 	spin_lock_init(&priv->lock);
 	platform_set_drvdata(pdev, priv);
 
@@ -1288,9 +1283,15 @@ static int rtsn_probe(struct platform_device *pdev)
 		goto error_free;
 	}
 
-	priv->ptp_priv->addr = devm_ioremap_resource(&pdev->dev, res);
-	if (IS_ERR(priv->ptp_priv->addr)) {
-		ret = PTR_ERR(priv->ptp_priv->addr);
+	ptpaddr = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(ptpaddr)) {
+		ret = PTR_ERR(ptpaddr);
+		goto error_free;
+	}
+
+	priv->ptp_priv = rcar_gen4_ptp_alloc(pdev, ptpaddr);
+	if (!priv->ptp_priv) {
+		ret = -ENOMEM;
 		goto error_free;
 	}
 
Signature · 1 line
2.52.0
#2 lore raw

PATCH 2/4 net: ethernet: renesas: rcar_gen4_ptp: Add helper to get clock index

In reply to Niklas Söderlund

Instead of accessing the Gen4 PTP specific structure directly in drivers add a helper to read the clock index. This is done in preparation to completely hide the Gen4 PTP specific structure from users.

---
 drivers/net/ethernet/renesas/rcar_gen4_ptp.c | 9 +++++++++
 drivers/net/ethernet/renesas/rcar_gen4_ptp.h | 2 ++
 drivers/net/ethernet/renesas/rswitch_main.c  | 2 +-
 drivers/net/ethernet/renesas/rtsn.c          | 2 +-
 4 files changed, 13 insertions(+), 2 deletions(-)
drivers/net/ethernet/renesas/rcar_gen4_ptp.c +9 -0
index 3fd835128cc8..75bf7aac089b 100644
@@ -185,6 +185,15 @@ struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev,
 }
 EXPORT_SYMBOL_GPL(rcar_gen4_ptp_alloc);
 
+int rcar_gen4_ptp_clock_index(struct rcar_gen4_ptp_private *priv)
+{
+	if (!priv->initialized)
+		return -1;
+
+	return ptp_clock_index(priv->clock);
+}
+EXPORT_SYMBOL_GPL(rcar_gen4_ptp_clock_index);
+
 MODULE_AUTHOR("Yoshihiro Shimoda");
 MODULE_DESCRIPTION("Renesas R-Car Gen4 gPTP driver");
 MODULE_LICENSE("GPL");
drivers/net/ethernet/renesas/rcar_gen4_ptp.h +2 -0
index b71aba873795..c141cd38a40d 100644
@@ -23,4 +23,6 @@ int rcar_gen4_ptp_unregister(struct rcar_gen4_ptp_private *ptp_priv);
 struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev,
 						  void __iomem *addr);
 
+int rcar_gen4_ptp_clock_index(struct rcar_gen4_ptp_private *priv);
+
 #endif	/* #ifndef __RCAR_GEN4_PTP_H__ */
drivers/net/ethernet/renesas/rswitch_main.c +1 -1
index ab0b40d4f4fb..433eb2b00d10 100644
@@ -1891,7 +1891,7 @@ static int rswitch_get_ts_info(struct net_device *ndev, struct kernel_ethtool_ts
 {
 	struct rswitch_device *rdev = netdev_priv(ndev);
 
-	info->phc_index = ptp_clock_index(rdev->priv->ptp_priv->clock);
+	info->phc_index = rcar_gen4_ptp_clock_index(rdev->priv->ptp_priv);
 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
 				SOF_TIMESTAMPING_TX_HARDWARE |
 				SOF_TIMESTAMPING_RX_HARDWARE |
drivers/net/ethernet/renesas/rtsn.c +1 -1
index d4b955c87f1b..ce1047743970 100644
@@ -1197,7 +1197,7 @@ static int rtsn_get_ts_info(struct net_device *ndev,
 {
 	struct rtsn_private *priv = netdev_priv(ndev);
 
-	info->phc_index = ptp_clock_index(priv->ptp_priv->clock);
+	info->phc_index = rcar_gen4_ptp_clock_index(priv->ptp_priv);
 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
 		SOF_TIMESTAMPING_TX_HARDWARE |
 		SOF_TIMESTAMPING_RX_HARDWARE |
Signature · 1 line
2.52.0
#3 lore raw

PATCH 3/4 net: ethernet: renesas: rcar_gen4_ptp: Add helper to read time

In reply to Niklas Söderlund

Instead of accessing the Gen4 PTP specific structure directly in drivers add a helper to read the time. This is done in preparation to completely hide the Gen4 PTP specific structure from users.

---
 drivers/net/ethernet/renesas/rcar_gen4_ptp.c | 10 ++++++++++
 drivers/net/ethernet/renesas/rcar_gen4_ptp.h |  2 ++
 drivers/net/ethernet/renesas/rtsn.c          |  9 +--------
 3 files changed, 13 insertions(+), 8 deletions(-)
drivers/net/ethernet/renesas/rcar_gen4_ptp.c +10 -0
index 75bf7aac089b..a14a16cf5fe6 100644
@@ -194,6 +194,16 @@ int rcar_gen4_ptp_clock_index(struct rcar_gen4_ptp_private *priv)
 }
 EXPORT_SYMBOL_GPL(rcar_gen4_ptp_clock_index);
 
+void rcar_gen4_ptp_gettime64(struct rcar_gen4_ptp_private *priv,
+			     struct timespec64 *ts)
+{
+	if (!priv->initialized)
+		return;
+
+	priv->info.gettime64(&priv->info, ts);
+}
+EXPORT_SYMBOL_GPL(rcar_gen4_ptp_gettime64);
+
 MODULE_AUTHOR("Yoshihiro Shimoda");
 MODULE_DESCRIPTION("Renesas R-Car Gen4 gPTP driver");
 MODULE_LICENSE("GPL");
drivers/net/ethernet/renesas/rcar_gen4_ptp.h +2 -0
index c141cd38a40d..fffccda401f5 100644
@@ -24,5 +24,7 @@ struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev,
 						  void __iomem *addr);
 
 int rcar_gen4_ptp_clock_index(struct rcar_gen4_ptp_private *priv);
+void rcar_gen4_ptp_gettime64(struct rcar_gen4_ptp_private *priv,
+			     struct timespec64 *ts);
 
 #endif	/* #ifndef __RCAR_GEN4_PTP_H__ */
drivers/net/ethernet/renesas/rtsn.c +1 -8
index ce1047743970..85052b47afb9 100644
@@ -104,13 +104,6 @@ static void rtsn_ctrl_data_irq(struct rtsn_private *priv, bool enable)
 	}
 }
 
-static void rtsn_get_timestamp(struct rtsn_private *priv, struct timespec64 *ts)
-{
-	struct rcar_gen4_ptp_private *ptp_priv = priv->ptp_priv;
-
-	ptp_priv->info.gettime64(&ptp_priv->info, ts);
-}
-
 static int rtsn_tx_free(struct net_device *ndev, bool free_txed_only)
 {
 	struct rtsn_private *priv = netdev_priv(ndev);
@@ -133,7 +126,7 @@ static int rtsn_tx_free(struct net_device *ndev, bool free_txed_only)
 				struct skb_shared_hwtstamps shhwtstamps;
 				struct timespec64 ts;
 
-				rtsn_get_timestamp(priv, &ts);
+				rcar_gen4_ptp_gettime64(priv->ptp_priv, &ts);
 				memset(&shhwtstamps, 0, sizeof(shhwtstamps));
 				shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
 				skb_tstamp_tx(skb, &shhwtstamps);
Signature · 1 line
2.52.0
#4 lore raw

PATCH 4/4 net: ethernet: renesas: rcar_gen4_ptp: Hide private data from users

In reply to Niklas Söderlund

The Gen4 PTP helper module is already used by RTSN and RSWITCH to support PTP clocks and will be used by RAVB too. Hide the Gen4 PTP private data structure to make sure none of the users poke at it.

This will be more important for RAVB use-cases as more then one RAVB device will need to cooperate using one PTP clock source.

---
 drivers/net/ethernet/renesas/rcar_gen4_ptp.c | 10 ++++++++++
 drivers/net/ethernet/renesas/rcar_gen4_ptp.h | 11 +----------
 2 files changed, 11 insertions(+), 10 deletions(-)
drivers/net/ethernet/renesas/rcar_gen4_ptp.c +10 -0
index a14a16cf5fe6..27a6f0492097 100644
@@ -9,6 +9,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/ptp_clock_kernel.h>
 #include <linux/slab.h>
 
 #include "rcar_gen4_ptp.h"
@@ -23,6 +24,15 @@
 #define PTPGPTPTM10_REG		0x0054
 #define PTPGPTPTM20_REG		0x0058
 
+struct rcar_gen4_ptp_private {
+	void __iomem *addr;
+	struct ptp_clock *clock;
+	struct ptp_clock_info info;
+	spinlock_t lock;	/* For multiple registers access */
+	s64 default_addend;
+	bool initialized;
+};
+
 #define ptp_to_priv(ptp)	container_of(ptp, struct rcar_gen4_ptp_private, info)
 
 static int rcar_gen4_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
drivers/net/ethernet/renesas/rcar_gen4_ptp.h +1 -10
index fffccda401f5..6abaa7cc6b77 100644
@@ -7,16 +7,7 @@
 #ifndef __RCAR_GEN4_PTP_H__
 #define __RCAR_GEN4_PTP_H__
 
-#include <linux/ptp_clock_kernel.h>
-
-struct rcar_gen4_ptp_private {
-	void __iomem *addr;
-	struct ptp_clock *clock;
-	struct ptp_clock_info info;
-	spinlock_t lock;	/* For multiple registers access */
-	s64 default_addend;
-	bool initialized;
-};
+struct rcar_gen4_ptp_private;
 
 int rcar_gen4_ptp_register(struct rcar_gen4_ptp_private *ptp_priv, u32 rate);
 int rcar_gen4_ptp_unregister(struct rcar_gen4_ptp_private *ptp_priv);
Signature · 1 line
2.52.0
maintainer
#5 lore raw

Re: [net-next 4/4] net: ethernet: renesas: rcar_gen4_ptp: Hide private data from users

Quoted 6 lines · On Sun, 1 Feb 2026 19:37:45 +0100 Niklas Söderlund wrote:

The Gen4 PTP helper module is already used by RTSN and RSWITCH to support PTP clocks and will be used by RAVB too. Hide the Gen4 PTP private data structure to make sure none of the users poke at it.

This will be more important for RAVB use-cases as more then one RAVB device will need to cooperate using one PTP clock source.

IMO hiding type definitions in C is an anti-pattern. Sooner or later you'll need a sizeof or a static inline helper and you'll have to bend over backwards to access the type info.

We can take this if you have a strong preference, but please think again if you really need this or it's a code cleanliness instinct carried over from objective programming, hence foreign..

#6 lore raw

Re: [net-next 4/4] net: ethernet: renesas: rcar_gen4_ptp: Hide private data from users

Hello Jakub,

Thanks for your feedback.

Quoted 8 lines · Jakub Kicinski
Quoted 6 lines · On Sun, 1 Feb 2026 19:37:45 +0100 Niklas Söderlund wrote:

The Gen4 PTP helper module is already used by RTSN and RSWITCH to support PTP clocks and will be used by RAVB too. Hide the Gen4 PTP private data structure to make sure none of the users poke at it.

This will be more important for RAVB use-cases as more then one RAVB device will need to cooperate using one PTP clock source.

Quoted 7 lines

IMO hiding type definitions in C is an anti-pattern. Sooner or later you'll need a sizeof or a static inline helper and you'll have to bend over backwards to access the type info.

We can take this if you have a strong preference, but please think again if you really need this or it's a code cleanliness instinct carried over from objective programming, hence foreign..

On principle I agree with you, hiding type definitions is usually not a good idea long term, but here I would still like to do it.

My reason is that the goal is to completely remove the type from the header file and only expose the struct ptp_clock to users.

The private data will need to be reworked as the next device where support for the Gen4 PTP clock need to support the same PTP clock device to be used by multiple network devices. This will make the private data more fragile and I don't want to expose that to the users as the helpers will then be mandatory to use.

I'm sure there are multiple paths to achieve my goal, but unless you strongly object against this intermediate step I would like to move forward with it.

Signature · 2 lines
Kind Regards,
Niklas Söderlund
#7 lore raw
In reply to Niklas Söderlund
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 6 lines · On Sun, 1 Feb 2026 19:37:41 +0100 you wrote:

Hello,

The R-Car Gen4 PTP module started out as an exclusive feature of a single driver, but have since been extended to cover both R-Car Switch and TSN driver implementations on Gen4.

Quoted 6 lines

The feature have already been extended to be built as its own module with an interface exposed thru a local header file. The header file however also exposes the modules private data structure. The two existing users have already started to poke at members of the struct.

[...]

Here is the summary with links:
- [net-next,1/4] net: ethernet: renesas: rcar_gen4_ptp: Move address assignment

    https://git.kernel.org/netdev/net-next/c/642377346a4e
  - [net-next,2/4] net: ethernet: renesas: rcar_gen4_ptp: Add helper to get clock index
    https://git.kernel.org/netdev/net-next/c/9afe65e7e7d7
  - [net-next,3/4] net: ethernet: renesas: rcar_gen4_ptp: Add helper to read time
    https://git.kernel.org/netdev/net-next/c/5640afa0583d
  - [net-next,4/4] net: ethernet: renesas: rcar_gen4_ptp: Hide private data from users
    https://git.kernel.org/netdev/net-next/c/9c2f568eb236

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html