Netdev List
 help / color / mirror / Atom feed
From: Potnuri Bharat Teja <bharat@chelsio.com>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, edumazet@google.com,
	pabeni@redhat.com, andrew+netdev@lunn.ch, bharat@chelsio.com,
	harshitha.vr@chelsio.com
Subject: [PATCH net-next v6 06/12] cxgb4: update PTP register access for T7
Date: Tue,  1 Dec 2026 17:15:50 -0500	[thread overview]
Message-ID: <20261201221556.251018-7-bharat@chelsio.com> (raw)
In-Reply-To: <20261201221556.251018-1-bharat@chelsio.com>

The MAC_PORT_TX_TS_VAL_LO/HI registers moved within the port register
block on T7, and the PTP clock sum is now read from the chip global
MAC_PTP_SUM_LO/HI instead of a per port register.  Select the right
registers by chip version.  The clock sum is read with
t4_read_reg64(); the Tx timestamp is read as two 32-bit registers,
as T7 MAC_PORT_TX_TS_VAL_LO is not 8-byte aligned.

PTP port register accesses and the port ID of the PTP firmware
commands (Rx redirect, Rx timestamping mode and Tx type) now use the
logical port number (pi->lport), as does the channel of the Rx
redirect command.

On transmit, ptp_classify_raw() replaces the open coded IPv4/UDP
test.  Only untagged IPv4/UDP event messages are timestamped, as
those are the only ones whose looped back copy cxgb4_ptp_is_ptp_rx()
recognises.  A PTP packet is sent on the PTP Tx queue and timestamped
only if it fits in an immediate work request (is_eth_imm()); the
timestamp is read back through adap->ptp_tx_skb, which the SGL path
would orphan and leave DMA mapped.  The PTP Tx slot is claimed only
after the queue credit and DMA mapping checks, so a failed transmit no
longer leaves adap->ptp_tx_skb set and blocks later PTP packets.

On receive, cxgb4_ptp_is_ptp_rx() checks the IPv4 header length and
the packet length before reading the looped back packet, and
t4_systim_to_hwstamp() checks that a received PTP event packet holds a
MAC header and finds the PTP header of an IEEE 802.3 (ETH_P_1588)
packet right after it.

Signed-off-by: Potnuri Bharat Teja <bharat@chelsio.com>
---
 .../net/ethernet/chelsio/cxgb4/cxgb4_main.c   |  6 +-
 .../net/ethernet/chelsio/cxgb4/cxgb4_ptp.c    | 65 ++++++++++++-------
 drivers/net/ethernet/chelsio/cxgb4/sge.c      | 32 +++++++--
 drivers/net/ethernet/chelsio/cxgb4/t4_regs.h  |  1 +
 4 files changed, 71 insertions(+), 33 deletions(-)

diff --git a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c
index 11e0479db7b4..db018947d021 100644
--- a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c
+++ b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c
@@ -3071,10 +3071,10 @@ static int cxgb_hwtstamp_set(struct net_device *dev,
 		break;
 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
-		cxgb4_ptprx_timestamping(pi, pi->port_id, PTP_TS_L4);
+		cxgb4_ptprx_timestamping(pi, pi->lport, PTP_TS_L4);
 		break;
 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
-		cxgb4_ptprx_timestamping(pi, pi->port_id, PTP_TS_L2_L4);
+		cxgb4_ptprx_timestamping(pi, pi->lport, PTP_TS_L2_L4);
 		break;
 	case HWTSTAMP_FILTER_ALL:
 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
@@ -3089,7 +3089,7 @@ static int cxgb_hwtstamp_set(struct net_device *dev,
 
 	if (config->tx_type == HWTSTAMP_TX_OFF &&
 	    config->rx_filter == HWTSTAMP_FILTER_NONE) {
-		if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
+		if (cxgb4_ptp_txtype(adapter, pi->lport) >= 0)
 			pi->ptp_enable = false;
 	}
 
diff --git a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ptp.c b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ptp.c
index cbd06d9b95d4..abc817525f0c 100644
--- a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ptp.c
+++ b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ptp.c
@@ -54,17 +54,15 @@
  * cxgb4_ptp_is_ptp_tx - determine whether TX packet is PTP or not
  * @skb: skb of outgoing ptp request
  *
+ * Return: true if @skb is an untagged IPv4/UDP PTP event message, the only
+ * kind whose looped back copy cxgb4_ptp_is_ptp_rx() recognises.
  */
 bool cxgb4_ptp_is_ptp_tx(struct sk_buff *skb)
 {
-	struct udphdr *uh;
+	unsigned int ptp_class = ptp_classify_raw(skb);
 
-	uh = udp_hdr(skb);
-	return skb->len >= PTP_MIN_LENGTH &&
-		skb->len <= PTP_IN_TRANSMIT_PACKET_MAXNUM &&
-		likely(skb->protocol == htons(ETH_P_IP)) &&
-		ip_hdr(skb)->protocol == IPPROTO_UDP &&
-		uh->dest == htons(PTP_EVENT_PORT);
+	return (ptp_class & (PTP_CLASS_PMASK | PTP_CLASS_VLAN)) ==
+	       PTP_CLASS_IPV4;
 }
 
 bool is_ptp_enabled(struct sk_buff *skb, struct net_device *dev)
@@ -83,11 +81,24 @@ bool is_ptp_enabled(struct sk_buff *skb, struct net_device *dev)
  */
 bool cxgb4_ptp_is_ptp_rx(struct sk_buff *skb)
 {
-	struct udphdr *uh = (struct udphdr *)(skb->data + ETH_HLEN +
-					      IPV4_HLEN(skb->data));
-
-	return  uh->dest == htons(PTP_EVENT_PORT) &&
-		uh->source == htons(PTP_EVENT_PORT);
+	struct ethhdr *eth = (struct ethhdr *)skb->data;
+	struct udphdr *uh;
+	struct iphdr *iph;
+	unsigned int ihl;
+
+	if (skb_headlen(skb) < ETH_HLEN + sizeof(*iph) ||
+	    eth->h_proto != htons(ETH_P_IP))
+		return false;
+
+	iph = (struct iphdr *)(skb->data + ETH_HLEN);
+	ihl = iph->ihl * 4;
+	if (ihl < sizeof(*iph) || iph->protocol != IPPROTO_UDP ||
+	    skb_headlen(skb) < ETH_HLEN + ihl + sizeof(*uh))
+		return false;
+
+	uh = (struct udphdr *)(skb->data + ETH_HLEN + ihl);
+	return uh->dest == htons(PTP_EVENT_PORT) &&
+	       uh->source == htons(PTP_EVENT_PORT);
 }
 
 /**
@@ -99,16 +110,23 @@ bool cxgb4_ptp_is_ptp_rx(struct sk_buff *skb)
 void cxgb4_ptp_read_hwstamp(struct adapter *adapter, struct port_info *pi)
 {
 	struct skb_shared_hwtstamps *skb_ts = NULL;
+	u32 lo_reg, hi_reg;
 	u64 tx_ts;
 
 	skb_ts = skb_hwtstamps(adapter->ptp_tx_skb);
 
-	tx_ts = t4_read_reg(adapter,
-			    T5_PORT_REG(pi->port_id, MAC_PORT_TX_TS_VAL_LO));
+	if (CHELSIO_CHIP_VERSION(adapter->params.chip) >= CHELSIO_T7) {
+		lo_reg = T7_PORT_REG(pi->lport, T7_MAC_PORT_TX_TS_VAL_LO);
+		hi_reg = T7_PORT_REG(pi->lport, T7_MAC_PORT_TX_TS_VAL_HI);
+	} else {
+		lo_reg = T5_PORT_REG(pi->lport, MAC_PORT_TX_TS_VAL_LO);
+		hi_reg = T5_PORT_REG(pi->lport, MAC_PORT_TX_TS_VAL_HI);
+	}
+
+	/* T7 TX_TS_VAL_LO is not 8-byte aligned, use two 32-bit reads */
+	tx_ts = t4_read_reg(adapter, lo_reg);
+	tx_ts |= (u64)t4_read_reg(adapter, hi_reg) << 32;
 
-	tx_ts |= (u64)t4_read_reg(adapter,
-				  T5_PORT_REG(pi->port_id,
-					      MAC_PORT_TX_TS_VAL_HI)) << 32;
 	skb_ts->hwtstamp = ns_to_ktime(tx_ts);
 	skb_tstamp_tx(adapter->ptp_tx_skb, skb_ts);
 	dev_kfree_skb_any(adapter->ptp_tx_skb);
@@ -179,11 +197,11 @@ int cxgb4_ptp_redirect_rx_packet(struct adapter *adapter, struct port_info *pi)
 	c.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PTP_CMD) |
 				     FW_CMD_REQUEST_F |
 				     FW_CMD_WRITE_F |
-				     FW_PTP_CMD_PORTID_V(pi->port_id));
+				     FW_PTP_CMD_PORTID_V(pi->lport));
 
 	c.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(sizeof(c) / 16));
 	c.u.init.sc = FW_PTP_SC_RDRX_TYPE;
-	c.u.init.txchan = pi->tx_chan;
+	c.u.init.txchan = pi->lport;
 	c.u.init.absid = cpu_to_be16(receive_q->rspq.abs_id);
 
 	err = t4_wr_mbox(adapter, adapter->mbox, &c, sizeof(c), NULL);
@@ -319,9 +337,10 @@ static int cxgb4_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
 					       ptp_clock_info);
 	u64 ns;
 
-	ns = t4_read_reg(adapter, T5_PORT_REG(0, MAC_PORT_PTP_SUM_LO_A));
-	ns |= (u64)t4_read_reg(adapter,
-			       T5_PORT_REG(0, MAC_PORT_PTP_SUM_HI_A)) << 32;
+	if (CHELSIO_CHIP_VERSION(adapter->params.chip) >= CHELSIO_T7)
+		ns = t4_read_reg64(adapter, MAC_PTP_SUM_LO_A);
+	else
+		ns = t4_read_reg64(adapter, T5_PORT_REG(0, MAC_PORT_PTP_SUM_LO_A));
 
 	/* convert to timespec*/
 	*ts = ns_to_timespec64(ns);
@@ -432,7 +451,7 @@ void cxgb4_ptp_init(struct adapter *adapter)
 	spin_lock_init(&adapter->ptp_lock);
 
 	adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
-						&adapter->pdev->dev);
+						adapter->pdev_dev);
 	if (IS_ERR_OR_NULL(adapter->ptp_clock)) {
 		adapter->ptp_clock = NULL;
 		dev_err(adapter->pdev_dev,
diff --git a/drivers/net/ethernet/chelsio/cxgb4/sge.c b/drivers/net/ethernet/chelsio/cxgb4/sge.c
index 71d936810696..b33ded33a9f2 100644
--- a/drivers/net/ethernet/chelsio/cxgb4/sge.c
+++ b/drivers/net/ethernet/chelsio/cxgb4/sge.c
@@ -1529,13 +1529,18 @@ static netdev_tx_t cxgb4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
 #endif /* CHELSIO_TLS_DEVICE */
 
 	qidx = skb_get_queue_mapping(skb);
+	chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
+
+	/* A PTP packet is timestamped only if it fits in an immediate work
+	 * request: the timestamp is read back through adap->ptp_tx_skb, which
+	 * the SGL path would orphan and keep mapped.
+	 */
+	if (ptp_enabled && (skb_is_gso(skb) || !is_eth_imm(skb, chip_ver)))
+		ptp_enabled = false;
+
 	if (ptp_enabled) {
-		if (!(adap->ptp_tx_skb)) {
-			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
-			adap->ptp_tx_skb = skb_get(skb);
-		} else {
+		if (adap->ptp_tx_skb)
 			goto out_free;
-		}
 		q = &adap->sge.ptptxq;
 	} else {
 		q = &adap->sge.ethtxq[qidx + pi->first_qset];
@@ -1550,7 +1555,6 @@ static netdev_tx_t cxgb4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
 		goto out_free;
 #endif /* CONFIG_CHELSIO_T4_FCOE */
 
-	chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
 	flits = calc_tx_flits(skb, chip_ver);
 	ndesc = flits_to_desc(flits);
 	credits = txq_avail(&q->q) - ndesc;
@@ -1581,6 +1585,12 @@ static netdev_tx_t cxgb4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
 		goto out_free;
 	}
 
+	/* Claim the PTP Tx slot only once nothing can fail any more */
+	if (ptp_enabled) {
+		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
+		adap->ptp_tx_skb = skb_get(skb);
+	}
+
 	wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
 	if (unlikely(credits < ETHTXQ_STOP_THRES)) {
 		/* After we're done injecting the Work Request for this
@@ -3509,7 +3519,15 @@ static noinline int t4_systim_to_hwstamp(struct adapter *adapter,
 
 	data = skb->data + sizeof(*cpl);
 	skb_pull(skb, 2 * sizeof(u64) + sizeof(struct cpl_rx_mps_pkt));
-	offset = ETH_HLEN + IPV4_HLEN(skb->data) + UDP_HLEN;
+
+	if (skb->len < ETH_HLEN)
+		return RX_PTP_PKT_ERR;
+
+	/* IEEE 802.3 PTP carries the PTP message right after the MAC header */
+	if (((struct ethhdr *)skb->data)->h_proto == htons(ETH_P_1588))
+		offset = ETH_HLEN;
+	else
+		offset = ETH_HLEN + IPV4_HLEN(skb->data) + UDP_HLEN;
 	if (skb->len < offset + OFF_PTP_SEQUENCE_ID + sizeof(short))
 		return RX_PTP_PKT_ERR;
 
diff --git a/drivers/net/ethernet/chelsio/cxgb4/t4_regs.h b/drivers/net/ethernet/chelsio/cxgb4/t4_regs.h
index 4957045b6afe..83fde22afe17 100644
--- a/drivers/net/ethernet/chelsio/cxgb4/t4_regs.h
+++ b/drivers/net/ethernet/chelsio/cxgb4/t4_regs.h
@@ -4574,6 +4574,7 @@
 #define MAC_PORT_EPIO_OP_A 0x8d0
 
 #define MAC_PORT_CFG2_A 0x818
+#define MAC_PTP_SUM_LO_A 0x38108
 
 #define MAC_PORT_PTP_SUM_LO_A 0x990
 #define MAC_PORT_PTP_SUM_HI_A 0x994
-- 
2.39.1


  parent reply	other threads:[~2026-12-01 12:45 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 22:15 [PATCH net-next v6 00/12] cxgb4: Add T7 adapter support Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 01/12] cxgb4: add T7 hardware definitions and firmware interfaces Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 02/12] cxgb4: rework the interrupt handling framework Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 03/12] cxgb4: add T7 hardware management support Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 04/12] cxgb4: move PCI device management into cxgb4_pci.c and rework driver helpers Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 05/12] cxgb4: add T7 support to the main driver Potnuri Bharat Teja
2026-12-01 22:15 ` Potnuri Bharat Teja [this message]
2026-12-01 22:15 ` [PATCH net-next v6 07/12] cxgb4: extend ethtool support for T7 adapters Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 08/12] cxgb4: Add T7 support to the filter infrastructure Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 09/12] cxgb4: Add indirect register definitions for T7 Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 10/12] cxgb4: extend CUDBG support for T7 adapters Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 11/12] cxgb4: extend debugfs " Potnuri Bharat Teja
2026-12-01 22:15 ` [PATCH net-next v6 12/12] cxgb4: select ULD Tx and control queues by TID on T7 Potnuri Bharat Teja

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=20261201221556.251018-7-bharat@chelsio.com \
    --to=bharat@chelsio.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=harshitha.vr@chelsio.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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