Skip to contentSkip to conversation

net: lan743x: ptp: Fix simultaneous PEROUT on PCI11x1x

PATCH In review Thangaraj Samynathan 2 participants 5 messages May 25 – May 29

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

cover net: lan743x: ptp: Fix simultaneous PEROUT on PCI11x1x

The PCI11x1x hardware supports up to 12 PTP-IO pins as simultaneous periodic outputs, but the driver was limited to 2 due to two bugs:

1. ptp_io_perout[] was sized by LAN743X_PTP_N_PEROUT (2) and n_per_out was hardcoded to LAN743X_PTP_N_EVENT_CHAN (2), so any perout index above 1 was rejected immediately by the PTP core.

2. lan743x_ptp_reserve_event_ch() refused to assign a channel already in use, and lan743x_ptp_release_event_ch() unconditionally tore down the hardware channel on first release — both preventing multiple pins from sharing the same event channel.

Patch 1 introduces PCI11X1X_PTP_IO_MAX_OUT_PINS (12), resizes the arrays, and sets n_per_out conditionally for PCI11x1x devices.

Patch 2 fixes the channel assignment logic: maps perout indices to channels via index % 2, adds refcount-based sharing, and guards hardware teardown until the last user releases.

Thangaraj Samynathan (2):
net: lan743x: ptp: Add PCI11X1X_PTP_N_PEROUT and extend ptp_io_perout
array
net: lan743x: ptp: Fix PEROUT event channel assignment on PCI11x1x

 drivers/net/ethernet/microchip/lan743x_ptp.c | 87 ++++++++++++--------
 drivers/net/ethernet/microchip/lan743x_ptp.h |  5 +-
 2 files changed, 55 insertions(+), 37 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 net: lan743x: ptp: Add PCI11X1X_PTP_N_PEROUT and extend ptp_io_perout array

The PCI11x1x hardware supports up to 12 PTP-IO pins as simultaneous periodic outputs, each independently selecting Timer A or B via its own PTP_IO_CH_SEL bit in PTP_IO_EVENT_OUTPUT_CFG. However, ptp_io_perout[] was sized by LAN743X_PTP_N_PEROUT (2), matching only the number of hardware event channels rather than the number of IO output pins.

Introduce PCI11X1X_PTP_IO_MAX_OUT_PINS (12) for the output pin count and PCI11X1X_PTP_N_PEROUT as its alias, keeping PCI11X1X_PTP_IO_MAX_CHANNELS (8) for extts/capture which is limited to 8 channels by the hardware capture logic.

Resize ptp_io_perout[] to PCI11X1X_PTP_N_PEROUT (12) and set n_per_out conditionally based on is_pci11x1x, so all 12 PTP-IO output pins can be used simultaneously as periodic outputs.

Fixes e432dd3bee2c ("net: lan743x: Add support for PTP-IO Event Output (Periodic Output)")
---
 drivers/net/ethernet/microchip/lan743x_ptp.c | 4 +++-
 drivers/net/ethernet/microchip/lan743x_ptp.h | 5 ++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/net/ethernet/microchip/lan743x_ptp.c +3 -1
index a3b48388b3fd..8453b9d954cf 100644
@@ -1530,7 +1530,9 @@ int lan743x_ptp_open(struct lan743x_adapter *adapter)
 	ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
 	ptp->ptp_clock_info.n_alarm = 0;
 	ptp->ptp_clock_info.n_ext_ts = LAN743X_PTP_N_EXTTS;
-	ptp->ptp_clock_info.n_per_out = LAN743X_PTP_N_EVENT_CHAN;
+	ptp->ptp_clock_info.n_per_out = adapter->is_pci11x1x ?
+					PCI11X1X_PTP_N_PEROUT :
+					LAN743X_PTP_N_EVENT_CHAN;
 	ptp->ptp_clock_info.n_pins = n_pins;
 	ptp->ptp_clock_info.pps = LAN743X_PTP_N_PPS;
 	ptp->ptp_clock_info.supported_extts_flags = PTP_RISING_EDGE |
drivers/net/ethernet/microchip/lan743x_ptp.h +4 -1
index f33dc83c5700..94373c65d5c8 100644
@@ -19,6 +19,8 @@
 #define LAN743X_PTP_N_EVENT_CHAN	2
 #define LAN743X_PTP_N_PEROUT		LAN743X_PTP_N_EVENT_CHAN
 #define PCI11X1X_PTP_IO_MAX_CHANNELS	8
+#define PCI11X1X_PTP_IO_MAX_OUT_PINS	12
+#define PCI11X1X_PTP_N_PEROUT		PCI11X1X_PTP_IO_MAX_OUT_PINS
 #define LAN743X_PTP_N_EXTTS		PCI11X1X_PTP_IO_MAX_CHANNELS
 #define LAN743X_PTP_N_PPS		0
 #define PTP_CMD_CTL_TIMEOUT_CNT		50
@@ -83,8 +85,9 @@ struct lan743x_ptp {
 	struct ptp_pin_desc pin_config[LAN743X_PTP_N_GPIO];
 
 	unsigned long used_event_ch;
+	int event_ch_refcnt[LAN743X_PTP_N_EVENT_CHAN]; /* ref count for shared channel usage */
 	struct lan743x_ptp_perout perout[LAN743X_PTP_N_PEROUT];
-	int ptp_io_perout[LAN743X_PTP_N_PEROUT]; /* PTP event channel (0=channel A, 1=channel B) */
+	int ptp_io_perout[PCI11X1X_PTP_N_PEROUT]; /* PTP event channel per perout index */
 	struct lan743x_extts extts[LAN743X_PTP_N_EXTTS];
 
 	bool leds_multiplexed;
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/2 net: lan743x: ptp: Fix PEROUT event channel assignment on PCI11x1x

In reply to Thangaraj Samynathan

The PCI11x1x device supports up to 12 PTP-IO pins as periodic outputs, but only has 2 hardware event channels (A and B). The previous code called lan743x_ptp_reserve_event_ch(adapter, index) where index is the perout request index (0..11), which fails for any index >= 2 since only channels 0 and 1 exist. As a result only two simultaneous periodic outputs were possible instead of twelve.

Additionally, reserve_event_ch refused to assign a channel already in use, preventing multiple pins from sharing the same event channel. release_event_ch unconditionally tore down the hardware channel even when other outputs were still using it.

Fix by:
- Adding bounds checking to lan743x_ptp_reserve_event_ch() and using event_ch_refcnt[] (introduced in the preceding commit), allowing multiple perout outputs to share one channel.
- Changing lan743x_ptp_release_event_ch() to return bool and decrement the reference count, only clearing the used_event_ch bit and returning true when the last user releases.
- Using index % LAN743X_PTP_N_EVENT_CHAN in lan743x_ptp_io_perout() so even-indexed outputs map to channel A and odd-indexed to channel B, enabling all 12 PTP-IO pins to be driven simultaneously.
- Guarding lan743x_ptp_io_perout_off() with ch_free so the hardware event channel is only disabled when the last user releases it.

Fixes e432dd3bee2c ("net: lan743x: Add support for PTP-IO Event Output (Periodic Output)")
---
 drivers/net/ethernet/microchip/lan743x_ptp.c | 83 +++++++++++---------
 1 file changed, 48 insertions(+), 35 deletions(-)
drivers/net/ethernet/microchip/lan743x_ptp.c +48 -35
index 8453b9d954cf..ffccc4dd06d0 100644
@@ -159,33 +159,38 @@ static int lan743x_ptp_reserve_event_ch(struct lan743x_adapter *adapter,
 	struct lan743x_ptp *ptp = &adapter->ptp;
 	int result = -ENODEV;
 
+	if (event_channel < 0 || event_channel >= LAN743X_PTP_N_EVENT_CHAN)
+		return -EINVAL;
+
 	mutex_lock(&ptp->command_lock);
-	if (!(test_bit(event_channel, &ptp->used_event_ch))) {
-		ptp->used_event_ch |= BIT(event_channel);
-		result = event_channel;
-	} else {
-		netif_warn(adapter, drv, adapter->netdev,
-			   "attempted to reserved a used event_channel = %d\n",
-			   event_channel);
-	}
+	ptp->used_event_ch |= BIT(event_channel);
+	ptp->event_ch_refcnt[event_channel]++;
+	result = event_channel;
 	mutex_unlock(&ptp->command_lock);
 	return result;
 }
 
-static void lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter,
+static bool lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter,
 					 int event_channel)
 {
 	struct lan743x_ptp *ptp = &adapter->ptp;
+	bool fully_released = false;
 
 	mutex_lock(&ptp->command_lock);
 	if (test_bit(event_channel, &ptp->used_event_ch)) {
-		ptp->used_event_ch &= ~BIT(event_channel);
+		ptp->event_ch_refcnt[event_channel]--;
+		if (ptp->event_ch_refcnt[event_channel] <= 0) {
+			ptp->event_ch_refcnt[event_channel] = 0;
+			ptp->used_event_ch &= ~BIT(event_channel);
+			fully_released = true;
+		}
 	} else {
 		netif_warn(adapter, drv, adapter->netdev,
 			   "attempted release on a not used event_channel = %d\n",
 			   event_channel);
 	}
 	mutex_unlock(&ptp->command_lock);
+	return fully_released;
 }
 
 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
@@ -648,27 +653,30 @@ static void lan743x_ptp_io_perout_off(struct lan743x_adapter *adapter,
 
 	event_ch = ptp->ptp_io_perout[index];
 	if (event_ch >= 0) {
-		/* set target to far in the future, effectively disabling it */
-		lan743x_csr_write(adapter,
-				  PTP_CLOCK_TARGET_SEC_X(event_ch),
-				  0xFFFF0000);
-		lan743x_csr_write(adapter,
-				  PTP_CLOCK_TARGET_NS_X(event_ch),
-				  0);
-
-		gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
-		gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
-				    (event_ch));
-		gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch));
-		gen_cfg |= HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch);
-		lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);
-		if (event_ch)
-			lan743x_csr_write(adapter, PTP_INT_STS,
-					  PTP_INT_TIMER_INT_B_);
-		else
-			lan743x_csr_write(adapter, PTP_INT_STS,
-					  PTP_INT_TIMER_INT_A_);
-		lan743x_ptp_release_event_ch(adapter, event_ch);
+		bool ch_free = lan743x_ptp_release_event_ch(adapter, event_ch);
+
+		if (ch_free) {
+			/* Last user of this channel - disable HW channel */
+			lan743x_csr_write(adapter,
+					  PTP_CLOCK_TARGET_SEC_X(event_ch),
+					  0xFFFF0000);
+			lan743x_csr_write(adapter,
+					  PTP_CLOCK_TARGET_NS_X(event_ch),
+					  0);
+
+			gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
+			gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
+					    (event_ch));
+			gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch));
+			gen_cfg |= HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch);
+			lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);
+			if (event_ch)
+				lan743x_csr_write(adapter, PTP_INT_STS,
+						  PTP_INT_TIMER_INT_B_);
+			else
+				lan743x_csr_write(adapter, PTP_INT_STS,
+						  PTP_INT_TIMER_INT_A_);
+		}
 		ptp->ptp_io_perout[index] = -1;
 	}
 
@@ -715,16 +723,21 @@ static int lan743x_ptp_io_perout(struct lan743x_adapter *adapter, int on,
 		return 0;
 	}
 
-	if (event_ch >= LAN743X_PTP_N_EVENT_CHAN) {
-		/* already on, turn off first */
+	if (event_ch >= 0) {
+		/* already on for this index, turn off this pin first */
 		lan743x_ptp_io_perout_off(adapter, index);
 	}
 
-	event_ch = lan743x_ptp_reserve_event_ch(adapter, index);
+	/* Map perout index to one of the available event channels. Multiple
+	 * perout indices sharing the same channel (even/odd split) allows all
+	 * PTP-IO pins to be driven simultaneously from a single channel.
+	 */
+	event_ch = lan743x_ptp_reserve_event_ch(adapter,
+						index % LAN743X_PTP_N_EVENT_CHAN);
 	if (event_ch < 0) {
 		netif_warn(adapter, drv, adapter->netdev,
 			   "Failed to reserve event channel %d for PEROUT\n",
-			   index);
+			   index % LAN743X_PTP_N_EVENT_CHAN);
 		goto failed;
 	}
 	ptp->ptp_io_perout[index] = event_ch;
Signature · 1 line
2.34.1
maintainer
#3 lore raw
In reply to Thangaraj Samynathan
Quoted 12 lines · Thangaraj Samynathan

The PCI11x1x hardware supports up to 12 PTP-IO pins as simultaneous periodic outputs, but the driver was limited to 2 due to two bugs:

1. ptp_io_perout[] was sized by LAN743X_PTP_N_PEROUT (2) and n_per_out was hardcoded to LAN743X_PTP_N_EVENT_CHAN (2), so any perout index above 1 was rejected immediately by the PTP core.

2. lan743x_ptp_reserve_event_ch() refused to assign a channel already in use, and lan743x_ptp_release_event_ch() unconditionally tore down the hardware channel on first release — both preventing multiple pins from sharing the same event channel.

Quoted 6 lines

Patch 1 introduces PCI11X1X_PTP_IO_MAX_OUT_PINS (12), resizes the arrays, and sets n_per_out conditionally for PCI11x1x devices.

Patch 2 fixes the channel assignment logic: maps perout indices to channels via index % 2, adds refcount-based sharing, and guards hardware teardown until the last user releases.

IMHO the above are not fixes, but features and should target net-next. Also the split is incorrect, as introduces OoB after patch 1. You have to split the series differently, i.e. introducing support for multiple pin per channel (without raising the limits) in patch 1 and raising the pins limit in patch 2, together will all the required buffers adjustment.

Also note that sashiko gemini pointed to a few pre-existing issues that should be addresses in a separate net series.

/P

#4 lore raw

Hi Paolo,
Thanks for the comments.

Quoted 37 lines

-----Original Message-----
From: Paolo Abeni <[email protected]>
Sent: Thursday, May 28, 2026 3:10 PM
To: Thangaraj Samynathan - I53494 <[email protected]>; [email protected]
Cc: Bryan Whitehead - C21958 <[email protected]>; UNGLinuxDriver <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH net 0/2] net: lan743x: ptp: Fix simultaneous PEROUT on PCI11x1x

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 4 lines · Thangaraj Samynathan

The PCI11x1x hardware supports up to 12 PTP-IO pins as simultaneous periodic outputs, but the driver was limited to 2 due to two bugs:

1. ptp_io_perout[] was sized by LAN743X_PTP_N_PEROUT (2) and

n_per_out

Quoted 8 lines

was hardcoded to LAN743X_PTP_N_EVENT_CHAN (2), so any perout index above 1 was rejected immediately by the PTP core.

2. lan743x_ptp_reserve_event_ch() refused to assign a channel already in use, and lan743x_ptp_release_event_ch() unconditionally tore down the hardware channel on first release — both preventing multiple pins from sharing the same event channel.

Quoted 6 lines

Patch 1 introduces PCI11X1X_PTP_IO_MAX_OUT_PINS (12), resizes the arrays, and sets n_per_out conditionally for PCI11x1x devices.

Patch 2 fixes the channel assignment logic: maps perout indices to channels via index % 2, adds refcount-based sharing, and guards hardware teardown until the last user releases.

Quoted 8 lines

IMHO the above are not fixes, but features and should target net-next. Also the split is incorrect, as introduces OoB after patch 1. You have to split the series differently, i.e. introducing support for multiple pin per channel (without raising the limits) in patch 1 and raising the pins limit in patch 2, together will all the required buffers adjustment.

Also note that sashiko gemini pointed to a few pre-existing issues that should be addresses in a separate net series.

[Thangaraj Samynathan] Sure, Will submit it via net-next. And submit issues reported in follow-up patches.

Quoted 4 lines

/P