* [PATCH net-next 0/3] net: usb: ax88179: Add support for PTP
@ 2026-11-29 9:29 Birger Koblitz
2026-11-29 9:29 ` [PATCH net-next 1/3] net: usb: ax88179: Add PTP data structures and definitions Birger Koblitz
` (2 more replies)
0 siblings, 3 replies; 13+ messages in thread
From: Birger Koblitz @ 2026-11-29 9:29 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Russell King
Cc: netdev, linux-usb, linux-kernel, Birger Koblitz
The latest versions of the AX88179A (AX88179A_HW_EC_VERSION >= 3),
the AX88772D, which shares the same MAC as the AX88179A and merely
has a different 100MBit PHY and all versions of the AX88279 support PTP.
This adds a complete implementation of PTP for these devices. It was
tested on the AX88179A/B, the AX88279 and the AX88772E as a latest
version of the AX88772D. It was cross-verified with an older version
of the AX88179A.
Signed-off-by: Birger Koblitz <mail@birger-koblitz.de>
---
Birger Koblitz (3):
net: usb: ax88179: Add PTP data structures and definitions
net: usb: ax88179: Add PTP Implementation
net: usb: ax88179: Enable PTP
drivers/net/usb/Kconfig | 1 +
drivers/net/usb/Makefile | 2 +-
drivers/net/usb/ax88179_lib.c | 20 +-
drivers/net/usb/ax88179_lib.h | 10 +-
drivers/net/usb/ax88179a_devices.c | 149 ++++++-
drivers/net/usb/ax88179a_ptp.c | 845 +++++++++++++++++++++++++++++++++++++
drivers/net/usb/ax88179a_ptp.h | 249 +++++++++++
7 files changed, 1263 insertions(+), 13 deletions(-)
---
base-commit: eb6d6c80d35449c27471b1874f13066c09a8964d
change-id: 20261129-ptp-5cc2670802f5
Best regards,
--
Birger Koblitz <mail@birger-koblitz.de>
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH net-next 1/3] net: usb: ax88179: Add PTP data structures and definitions 2026-11-29 9:29 [PATCH net-next 0/3] net: usb: ax88179: Add support for PTP Birger Koblitz @ 2026-11-29 9:29 ` Birger Koblitz 2026-11-30 9:30 ` sashiko-bot 2026-11-29 9:29 ` [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation Birger Koblitz 2026-11-29 9:29 ` [PATCH net-next 3/3] net: usb: ax88179: Enable PTP Birger Koblitz 2 siblings, 1 reply; 13+ messages in thread From: Birger Koblitz @ 2026-11-29 9:29 UTC (permalink / raw) To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran, Russell King Cc: netdev, linux-usb, linux-kernel, Birger Koblitz Add a PTP data structure to ax88179_data. Define the registers for enabling PTP and verifying support for PTP on the chip. The registers relevant for PTP on the chips have a width of 32bits. They are transferred over USB as Litte Endian. Add ax88179_write_u32() for the register writes. Signed-off-by: Birger Koblitz <mail@birger-koblitz.de> --- drivers/net/usb/ax88179_lib.c | 20 +++++++++++++++++--- drivers/net/usb/ax88179_lib.h | 10 +++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/drivers/net/usb/ax88179_lib.c b/drivers/net/usb/ax88179_lib.c index 821ee9cd8ded1b8f1e425bcef493fb175ed30376..5f2b76010e30c3bfad8ac767b683701bc59c8d07 100644 --- a/drivers/net/usb/ax88179_lib.c +++ b/drivers/net/usb/ax88179_lib.c @@ -70,14 +70,20 @@ static int __ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, void ax88179_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index, u16 size, void *data) { - u16 buf; - if (size == 2) { - buf = *((u16 *)data); + u16 buf = *((u16 *)data); + cpu_to_le16s(&buf); usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value, index, &buf, size); + } else if (size == 4) { + u32 buf = *((u32 *)data); + + cpu_to_le32s(&buf); + usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | + USB_RECIP_DEVICE, value, index, &buf, + size); } else { usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value, index, data, @@ -129,6 +135,14 @@ int ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, return ret; } +int ax88179_write_u32(struct usbnet *dev, u8 cmd, u16 value, u16 index, u32 data) +{ + u32 buf = data; + + cpu_to_le32s(&buf); + return __ax88179_write_cmd(dev, cmd, value, index, 4, &buf); +} + int ax88179_write_u16(struct usbnet *dev, u8 cmd, u16 value, u16 index, u16 data) { u16 buf = data; diff --git a/drivers/net/usb/ax88179_lib.h b/drivers/net/usb/ax88179_lib.h index 401c0bed3d28b4b94f0a0cad7509cd7087ef0639..0a73111c0211baa4dbcaff9c14b83fe15c6afd8e 100644 --- a/drivers/net/usb/ax88179_lib.h +++ b/drivers/net/usb/ax88179_lib.h @@ -43,6 +43,7 @@ #define AX_PAUSE_WATERLVL_HIGH 0x55 #define AX_FW_MODE_179A 0x01 +#define AX_USB_EP4_EN 0x02 #define PHYSICAL_LINK_STATUS 0x02 #define AX_USB_SS 0x04 #define AX_USB_HS 0x02 @@ -121,8 +122,8 @@ #define AX88179A_VLAN_ID_DATA0 0x2C #define AX88179A_VLAN_ID_DATA1 0x2D +#define AX_RX_BULKIN_QCTRL 0x2E -#define AX_RX_BULKIN_QCTRL 0x2e #define AX_GPHY_EEE_CTRL 0x01 #define AX_CLK_SELECT 0x33 @@ -241,6 +242,7 @@ #define GMII_PHY_PGSEL_PAGE3 0x0003 #define GMII_PHY_PGSEL_PAGE5 0x0005 +#define AX88179A_HW_EC_VERSION 0xFB #define AX88179A_SW_REVISION 0xFC #define AX88179A_SW_VERSION 0xFD @@ -261,11 +263,13 @@ #define AX179A_RX_PD_L3_IP 0x20 #define AX179A_RX_PD_L3_IP6 0x40 +#define AX179A_RX_PD_HDR_CRC_ERR BIT(7) #define AX179A_RX_PD_VLAN BIT(10) #define AX179A_RX_PD_RX_OK BIT(11) #define AX179A_RX_PD_DROP BIT(31) #define AX179A_RX_PD_LEN GENMASK(30, 16) #define AX179A_RX_PD_VLAN_SHIFT 0x20 +#define AX179A_RX_PD_PTP BIT(56) /* RX Descriptor header */ #define AX179A_RX_DH_PKT_CNT_MASK 0x1FFF @@ -290,6 +294,8 @@ enum ax_chip_version { AX_VERSION_AX88279 = 0x7, }; +struct ax88179a_ptp; + struct ax88179_data { struct mutex medium_status_mutex; /* protect AX_MEDIUM_STATUS_MODE */ u8 eee_enabled; @@ -317,6 +323,7 @@ struct ax88179_data { struct phylink_config phylink_config; int (*resume)(struct usb_interface *intf); int (*suspend)(struct usb_interface *intf, pm_message_t message); + struct ax88179a_ptp *ptp; }; struct ax88179_int_data { @@ -335,6 +342,7 @@ int ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, u16 size const void *data); void ax88179_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index, u16 size, void *data); +int ax88179_write_u32(struct usbnet *dev, u8 cmd, u16 value, u16 index, u32 data); int ax88179_write_u16(struct usbnet *dev, u8 cmd, u16 value, u16 index, u16 data); int ax88179_write_u8(struct usbnet *dev, u8 cmd, u16 value, u16 index, u8 data); int ax88179_mdio_read(struct net_device *netdev, int phy_id, int loc); -- 2.47.3 ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH net-next 1/3] net: usb: ax88179: Add PTP data structures and definitions 2026-11-29 9:29 ` [PATCH net-next 1/3] net: usb: ax88179: Add PTP data structures and definitions Birger Koblitz @ 2026-11-30 9:30 ` sashiko-bot 0 siblings, 0 replies; 13+ messages in thread From: sashiko-bot @ 2026-11-30 9:30 UTC (permalink / raw) To: Birger Koblitz; +Cc: linux-usb Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net: usb: ax88179: 32-bit shift overflow in AX179A_RX_PD_PTP BIT(56) -- commit b639231242b9a5584ff7eca0b050d1ec24065223 Author: Birger Koblitz <mail@birger-koblitz.de> net: usb: ax88179: Add PTP data structures and definitions This commit adds a PTP pointer to ax88179_data, defines PTP-related registers and RX descriptor flags, and introduces 32-bit USB register write support. > diff --git a/drivers/net/usb/ax88179_lib.h b/drivers/net/usb/ax88179_lib.h > index 401c0bed3d28b..0a73111c0211b 100644 > --- a/drivers/net/usb/ax88179_lib.h > +++ b/drivers/net/usb/ax88179_lib.h [ ... ] > @@ -261,11 +263,13 @@ > #define AX179A_RX_PD_L3_IP 0x20 > #define AX179A_RX_PD_L3_IP6 0x40 > > +#define AX179A_RX_PD_HDR_CRC_ERR BIT(7) > #define AX179A_RX_PD_VLAN BIT(10) > #define AX179A_RX_PD_RX_OK BIT(11) > #define AX179A_RX_PD_DROP BIT(31) > #define AX179A_RX_PD_LEN GENMASK(30, 16) > #define AX179A_RX_PD_VLAN_SHIFT 0x20 > +#define AX179A_RX_PD_PTP BIT(56) [Severity: Medium] Should AX179A_RX_PD_PTP use BIT_ULL(56) or BIT_U64(56) instead of BIT(56)? BIT() shifts an unsigned long, so on 32-bit architectures BIT(56) overflows a 32-bit shift count, triggering a -Wshift-count-overflow compiler warning (or build failure with CONFIG_WERROR) and failing to match bit 56 of the 64-bit RX packet descriptor. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261129-ptp-v1-0-8148c402beaf@birger-koblitz.de?part=1 ^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation 2026-11-29 9:29 [PATCH net-next 0/3] net: usb: ax88179: Add support for PTP Birger Koblitz 2026-11-29 9:29 ` [PATCH net-next 1/3] net: usb: ax88179: Add PTP data structures and definitions Birger Koblitz @ 2026-11-29 9:29 ` Birger Koblitz 2026-11-29 18:31 ` Manuel Ebner 2026-11-30 9:30 ` sashiko-bot 2026-11-29 9:29 ` [PATCH net-next 3/3] net: usb: ax88179: Enable PTP Birger Koblitz 2 siblings, 2 replies; 13+ messages in thread From: Birger Koblitz @ 2026-11-29 9:29 UTC (permalink / raw) To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran, Russell King Cc: netdev, linux-usb, linux-kernel, Birger Koblitz Adds support to match TX timestamps for packets for which TX timestamps are requested against TX timestamps received separately by the host and processing of the hardware timestamps inserted in received frames. For both AX88179A and AX88279 support for a PTP clock with adjfine(), adjtime(), gettime64() and settime64() is added. AX88179A and AX88279 implement reception of the TX timestamp information differently: the AX279A uses USB bulk requests, whereas the AX88179A uses a control transfer read. Since the control transfer reads via usbnet are synchronous (in contrast to the AX88279A bulk reads), they are handled as work items. TX frames for which a timestamp is requested are copied to a separate ptp->tx_skbs queue and matched in a delayed work against received TX timestamps. Signed-off-by: Birger Koblitz <mail@birger-koblitz.de> --- drivers/net/usb/Kconfig | 1 + drivers/net/usb/Makefile | 2 +- drivers/net/usb/ax88179a_ptp.c | 845 +++++++++++++++++++++++++++++++++++++++++ drivers/net/usb/ax88179a_ptp.h | 249 ++++++++++++ 4 files changed, 1096 insertions(+), 1 deletion(-) diff --git a/drivers/net/usb/Kconfig b/drivers/net/usb/Kconfig index cc8f2e150339915908fe1c28dea82e1103bbf17d..2a811116d34dc1a5d2225d947662b983dac7ddfe 100644 --- a/drivers/net/usb/Kconfig +++ b/drivers/net/usb/Kconfig @@ -166,6 +166,7 @@ config USB_USBNET config USB_NET_AX8817X tristate "ASIX AX88xxx Based USB 2.0 Ethernet Adapters" depends on USB_USBNET + depends on PTP_1588_CLOCK_OPTIONAL select CRC32 select PHYLINK select AX88796B_PHY diff --git a/drivers/net/usb/Makefile b/drivers/net/usb/Makefile index 4026f19ecb826a6465de38f1d9bb0e4c0af80421..812adcf839dc4061d4d6ef27025a26723bd51927 100644 --- a/drivers/net/usb/Makefile +++ b/drivers/net/usb/Makefile @@ -13,7 +13,7 @@ obj-$(CONFIG_USB_LAN78XX) += lan78xx.o obj-$(CONFIG_USB_NET_AX8817X) += asix.o asix-y := asix_devices.o asix_common.o ax88172a.o obj-$(CONFIG_USB_NET_AX88179) += ax88179.o -ax88179-y := ax88179_178a.o ax88179a_devices.o ax88179_lib.o +ax88179-y := ax88179_178a.o ax88179a_devices.o ax88179_lib.o ax88179a_ptp.o obj-$(CONFIG_USB_NET_CDCETHER) += cdc_ether.o obj-$(CONFIG_USB_NET_CDC_EEM) += cdc_eem.o obj-$(CONFIG_USB_NET_DM9601) += dm9601.o diff --git a/drivers/net/usb/ax88179a_ptp.c b/drivers/net/usb/ax88179a_ptp.c new file mode 100644 index 0000000000000000000000000000000000000000..9a04a93e509523436ccd19895b2a6cb6a819aea0 --- /dev/null +++ b/drivers/net/usb/ax88179a_ptp.c @@ -0,0 +1,845 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <linux/ptp_clock_kernel.h> +#include <linux/net_tstamp.h> +#include <linux/ptp_classify.h> +#include "ax88179_lib.h" +#include "ax88179a_ptp.h" + +#define ptp_to_dev(ptp) container_of(ptp, struct ax88179a_ptp, ptp_ci) + +static int ax88279_ptp_pbus_write(struct usbnet *dev, u16 offset, + u16 len, void *data) +{ + int ret; + + ret = ax88179_write_cmd(dev, AX88279_PBUS_A32, offset, + AX88279_PTP_REG_BASE_ADDR_HI, len, data); + if (ret < 0) + return ret; + + return 0; +} + +static int ax88279_ptp_pbus_write_u32(struct usbnet *dev, u16 offset, u32 data) +{ + u32 buf = data; + + return ax88279_ptp_pbus_write(dev, offset, 4, &buf); +} + +static int ax88279_ptp_clk_read(struct usbnet *dev, u16 offset, u16 len, void *data) +{ + int ret; + + ret = ax88179_read_cmd(dev, AX_PTP_CLK, 0x0002, offset, len, data); + if (ret < 0) + return ret; + + return 0; +} + +static int ax88279_ptp_clk_write(struct usbnet *dev, u16 offset, u16 len, void *data) +{ + int ret; + + ret = ax88179_write_cmd(dev, AX88279_PBUS_A32, offset, + AX88279_PTP_REG_BASE_ADDR_HI, len, data); + if (ret < 0) + return ret; + + return 0; +} + +void ax88179a_process_rx_ts(struct sk_buff *skb, u64 timestamp_l, u64 timestamp_h) +{ + struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); + u64 time64; + + timestamp_h <<= 32; + timestamp_h |= (timestamp_l >> 32) & 0xFFFFFFFF; + time64 = (timestamp_h * NSEC_PER_SEC); + time64 += (timestamp_l & 0xFFFFFFFF); + + memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); + + shhwtstamps->hwtstamp = ktime_sub_ns(ns_to_ktime(time64), 0); +} + +static int ax88179a_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) +{ + long ppb = scaled_ppm_to_ppb(scaled_ppm); + struct ax88179a_ptp *ax88179a_ptp; + struct ax88179_data *ax179_data; + bool neg_adj = false; + u32 new_addend_val; + u64 adjust_val; + int ret = 0; + + ax88179a_ptp = ptp_to_dev(ptp); + ax179_data = ax88179a_ptp->ax179_data; + if (ppb < 0) { + neg_adj = true; + ppb = -ppb; + } + + adjust_val = AX88179A_BASE_ADDEND; + adjust_val *= ppb; + adjust_val = div_u64(adjust_val, NSEC_PER_SEC); + + if (neg_adj) + new_addend_val = (u32)(AX88179A_BASE_ADDEND - adjust_val); + else + new_addend_val = (u32)(AX88179A_BASE_ADDEND + adjust_val); + + if (ax179_data->chip_version == AX_VERSION_AX88279) + ret = ax88279_ptp_pbus_write_u32(ax88179a_ptp->dev, AX88179A_PTP_TIMER_ADDEND_VAL, + new_addend_val); + else + ret = ax88179_write_u32(ax88179a_ptp->dev, AX_PTP_OP, AX_SET_ADDEND, 0, + new_addend_val); + if (ret < 0) + return ret; + + return 0; +} + +static int ax88179a_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) +{ + struct ax88179a_ptp *ax88179a_ptp = ptp_to_dev(ptp); + u8 timestamp[AX_PTP_DATA_LEN] = {0}; + struct ax88179_data *ax179_data; + bool delta_is_neg = false; + u64 sec, d, d_sec; + u32 d_nsec; + u32 nsec; + int ret; + + ax179_data = ax88179a_ptp->ax179_data; + if (ax179_data->chip_version == AX_VERSION_AX88279) + ret = ax88279_ptp_clk_read(ax88179a_ptp->dev, AX88179A_PTP_GET_80B_LCK, + AX_PTP_DATA_LEN, timestamp); + else + ret = ax88179_read_cmd(ax88179a_ptp->dev, AX_PTP_OP, AX_GET_LOCAL_CLOCK, 0, + AX_GET_LOCAL_CLOCK_SIZE, timestamp); + + if (ret < 0) + return ret; + + nsec = get_unaligned_le32(timestamp); + sec = get_unaligned_le64(×tamp[4]); + if (delta < 0) + delta_is_neg = true; + d = abs(delta); + + d_sec = div_u64_rem(d, NSEC_PER_SEC, &d_nsec); + if (delta_is_neg) { + if (d_nsec > nsec) { + nsec += NSEC_PER_SEC; + sec -= 1; + } + nsec -= d_nsec; + sec -= d_sec; + } else { + nsec += d_nsec; + if (nsec > NSEC_PER_SEC) { + sec += 1; + nsec -= NSEC_PER_SEC; + } + sec += d_sec; + } + put_unaligned_le32(nsec, timestamp); + put_unaligned_le64(sec, ×tamp[4]); + /* Use only 48 bits for seconds */ + timestamp[AX_PTP_DATA_LEN - 2] = 0; + timestamp[AX_PTP_DATA_LEN - 1] = 0; + + if (ax179_data->chip_version == AX_VERSION_AX88279) + ret = ax88279_ptp_clk_write(ax88179a_ptp->dev, AX88179A_PTP_SET_80B_LCK, + AX_PTP_DATA_LEN, timestamp); + else + ret = ax88179_write_cmd(ax88179a_ptp->dev, AX_PTP_OP, AX_SET_LOCAL_CLOCK, 0, + AX_SET_LOCAL_CLOCK_SIZE, ×tamp); + if (ret < 0) + return ret; + + return 0; +} + +static int ax88179a_ptp_gettime64(struct ptp_clock_info *ptp, + struct timespec64 *ts) +{ + struct ax88179a_ptp *ax88179a_ptp = ptp_to_dev(ptp); + u8 timestamp[AX_PTP_DATA_LEN] = {0}; + struct ax88179_data *ax179_data; + u32 nsec; + u64 sec; + int ret; + + ax179_data = ax88179a_ptp->ax179_data; + if (ax179_data->chip_version == AX_VERSION_AX88279) + ret = ax88279_ptp_clk_read(ax88179a_ptp->dev, AX88179A_PTP_GET_80B_LCK, + AX_PTP_DATA_LEN, timestamp); + else + ret = ax88179_read_cmd(ax88179a_ptp->dev, AX_PTP_OP, AX_GET_LOCAL_CLOCK, 0, + AX_GET_LOCAL_CLOCK_SIZE, timestamp); + if (ret < 0) + return ret; + + nsec = get_unaligned_le32(timestamp); + sec = get_unaligned_le64(×tamp[4]); + ts->tv_nsec = nsec; + ts->tv_sec = sec; + + return 0; +} + +static int ax88179a_ptp_settime64(struct ptp_clock_info *ptp, + const struct timespec64 *ts) +{ + struct ax88179a_ptp *ax88179a_ptp = ptp_to_dev(ptp); + u8 timestamp[AX_PTP_DATA_LEN] = {0}; + struct ax88179_data *ax179_data; + int ret; + + ax179_data = ax88179a_ptp->ax179_data; + put_unaligned_le32((u32)ts->tv_nsec, timestamp); + put_unaligned_le64((u64)ts->tv_sec, ×tamp[4]); + /* Use only 48 bits for seconds */ + timestamp[AX_PTP_DATA_LEN - 2] = 0; + timestamp[AX_PTP_DATA_LEN - 1] = 0; + + if (ax179_data->chip_version == AX_VERSION_AX88279) + ret = ax88279_ptp_clk_write(ax88179a_ptp->dev, AX88179A_PTP_SET_80B_LCK, + AX_PTP_DATA_LEN, timestamp); + else + ret = ax88179_write_cmd(ax88179a_ptp->dev, AX_PTP_OP, AX_SET_LOCAL_CLOCK, 0, + AX_SET_LOCAL_CLOCK_SIZE, ×tamp); + if (ret < 0) + return ret; + + return 0; +} + +static int match_ptp_ts(struct ax88179a_ptp *ptp, struct ptp_header *ptp_hdr, + struct sk_buff *skb, int type) +{ + u16 sequence_id; + + sequence_id = ntohs(ptp_hdr->sequence_id); + if (ptp->ax179_data->chip_version == AX_VERSION_AX88179A) + sequence_id &= 0xff; + + while (ptp->ptp_info_buf_tail != ptp->ptp_info_buf_head) { + struct ptp_info *ptp_info = &ptp->ptp_info_buf[ptp->ptp_info_buf_tail]; + + if (ptp_info->sequence_id == sequence_id && + (ptp_hdr->tsmt & 0xf) == ptp_info->msg_type) { + struct skb_shared_hwtstamps shhwtstamps; + u64 timestamp_h, timestamp_l; + u8 msgtype; + u64 time64; + + timestamp_l = ptp_info->nsec; + timestamp_h = ptp_info->sec; + time64 = timestamp_h * NSEC_PER_SEC; + time64 += timestamp_l & 0xFFFFFFFF; + memset(&shhwtstamps, 0, sizeof(shhwtstamps)); + shhwtstamps.hwtstamp = ns_to_ktime(time64); + + msgtype = ptp_get_msgtype(ptp_hdr, type); + if (ptp_hdr->flag_field[0] & 0x2 || (msgtype != 0 && msgtype != 3)) + skb_tstamp_tx(skb, &shhwtstamps); + } + ptp->ptp_info_buf_tail++; + ptp->ptp_info_buf_tail %= AX88179A_PTP_QUEUE_SIZE; + } + + return 0; +} + +static void ax88279_process_tx_ts(struct work_struct *work) +{ + struct ptp_header *ptp_hdr; + struct sk_buff *skb, *tmp; + struct ax88179a_ptp *ptp; + int type; + + ptp = container_of(work, struct ax88179a_ptp, process_ptp_info_buf); + + skb_queue_walk_safe(&ptp->tx_skbs, skb, tmp) { + type = ptp_classify_raw(skb); + if (type != PTP_CLASS_NONE) { + ptp_hdr = ptp_parse_header(skb, type); + if (!ptp_hdr) + goto frame_err; + } + match_ptp_ts(ptp, ptp_hdr, skb, type); +frame_err: + __skb_unlink(skb, &ptp->tx_skbs); + dev_kfree_skb_any(skb); + } +} + +static void ax88279_read_ts_complete(struct urb *urb) +{ + struct ax88179_data *ax179_data; + struct ptp_info_279 *ptp_info; + struct ax88179a_ptp *ptp; + struct usbnet *dev; + int offset; + + dev = urb->context; + ax179_data = dev->driver_priv; + if (!ax179_data) + return; + + ptp = ax179_data->ptp; + if (!ptp) + return; + + if (!netif_carrier_ok(dev->net)) + return; + + if (urb->status < 0) { + netdev_err(dev->net, "Timestamp URB status error: %d", urb->status); + goto out; + } + + usb_mark_last_busy(dev->udev); + + // There can only be 2 packets in this buffer + offset = (ptp->ep4_buf[AX88279_PTP_EP4_SIZE - 1] & AX_TS_SEG_1) ? + 0 : (sizeof(struct ptp_info_279) * AX88179A_PTP_QUEUE_SIZE); + + ptp_info = (struct ptp_info_279 *)&ptp->ep4_buf[offset]; + for (int i = 0; i < AX88179A_PTP_QUEUE_SIZE; i++) { + int j; + + j = ptp->ptp_info_buf_head; + if (ptp_info[i].status) { + ptp->ptp_info_buf[j].msg_type = ptp_info->msg_type; + ptp->ptp_info_buf[j].sequence_id = le16_to_cpu(ptp_info->sequence_id); + ptp->ptp_info_buf[j].nsec = le32_to_cpu(ptp_info->nsec); + ptp->ptp_info_buf[j].sec = le16_to_cpu(ptp_info->sec_h); + ptp->ptp_info_buf[j].sec <<= 32; + ptp->ptp_info_buf[j].sec |= le32_to_cpu(ptp_info->sec_l); + ptp->ptp_info_buf_head++; + ptp->ptp_info_buf_head %= AX88179A_PTP_QUEUE_SIZE; + } + } + schedule_work(&ptp->process_ptp_info_buf); +out: + ptp->ptp_read_ts(dev); +} + +void ax88179a_ptp_kill_urbs(struct usbnet *dev) +{ + struct ax88179_data *ax179_data = dev->driver_priv; + + if (ax179_data->chip_version == AX_VERSION_AX88279 && ax179_data->ptp && + ax179_data->ptp->urb) { + netdev_info(dev->net, "PTP: killing URBs\n"); + usb_kill_urb(ax179_data->ptp->urb); + } +} + +static int ax88279_ptp_read_ts(struct usbnet *dev) +{ + struct ax88179_data *ax179_data = dev->driver_priv; + struct ax88179a_ptp *ptp = ax179_data->ptp; + struct urb *urb = ptp->urb; + int ret; + + memset(ptp->ep4_buf, 0, AX88279_PTP_EP4_SIZE); + usb_fill_bulk_urb(urb, dev->udev, + usb_rcvbulkpipe(dev->udev, 4), + (void *)ptp->ep4_buf, AX88279_PTP_EP4_SIZE, + (usb_complete_t)ax88279_read_ts_complete, + dev); + + ret = usb_submit_urb(urb, GFP_KERNEL); + if (ret == -ENODEV) { + netdev_err(dev->net, "Error submitting PTP bulk message: status=%d", ret); + netif_device_detach(dev->net); + } + + urb->actual_length = 0; + + return ret; +} + +static int ax88179a_ptp_read_ts(struct usbnet *dev) +{ + struct ax88179_data *ax179_data = dev->driver_priv; + struct ax88179a_ptp *ptp = ax179_data->ptp; + struct ptp_info_179a *ptp_info; + int count = 0; + int ret; + + ret = ax88179_read_cmd(dev, AX_PTP_TIMESTAMP_REQ, 0, 0, AX88279_PTP_EP4_SIZE, ptp->ep4_buf); + if (ret < 0) { + netdev_err(dev->net, "Error submitting PTP control message: status=%d", ret); + netif_device_detach(dev->net); + return ret; + } + + ptp_info = (struct ptp_info_179a *)&ptp->ep4_buf; + for (int i = 0; i < AX88179A_PTP_QUEUE_SIZE; i++) { + if (ptp_info[i].status) { + int j; + + j = ptp->ptp_info_buf_head; + ptp->ptp_info_buf[j].msg_type = ptp_info->msg_type; + ptp->ptp_info_buf[j].sequence_id = ptp_info->sequence_id; + ptp->ptp_info_buf[j].nsec = le32_to_cpu(ptp_info->nsec); + ptp->ptp_info_buf[j].sec = le16_to_cpu(ptp_info->sec_h); + ptp->ptp_info_buf[j].sec <<= 32; + ptp->ptp_info_buf[j].sec |= le32_to_cpu(ptp_info->sec_l); + ptp->ptp_info_buf_head++; + ptp->ptp_info_buf_head %= AX88179A_PTP_QUEUE_SIZE; + count++; + } + } + + if (count) + schedule_work(&ptp->process_ptp_info_buf); + + return 0; +} + +static void ax88179a_ptp_read_ts_work(struct work_struct *work) +{ + struct ax88179a_ptp *ptp; + struct usbnet *dev; + + ptp = container_of(work, struct ax88179a_ptp, req_tx_timestamp); + dev = ptp->dev; + + ax88179a_ptp_read_ts(dev); +} + +static int ax88179a_ptp_enable(struct ptp_clock_info *ptp, + struct ptp_clock_request *rq, int on) +{ + return -EOPNOTSUPP; +} + +static int ax88179a_ptp_pps_ctrl(struct usbnet *dev, bool enable) +{ + struct ax88179_data *ax179_data = dev->driver_priv; + u32 reg32 = 0; + int ret = 0; + + if (!ax179_data->ptp) + return 0; + + ret = ax88179_read_cmd(dev, AX88179A_PBUS_REG, 0x1894, 0x000F, 4, ®32); + if (ret < 0) + return ret; + + reg32 &= ~0x01000000; + if (enable) + reg32 |= 0x01000000; + + ret = ax88179_write_cmd(dev, AX88179A_PBUS_REG, 0x1894, 0x000F, 4, ®32); + + return ret; +} + +static int ax88279_ptp_pps_ctrl(struct usbnet *dev, bool enable) +{ + struct ax88179_data *ax179_data = dev->driver_priv; + u32 reg32 = 0; + int ret = 0; + + if (!ax179_data->ptp) + return 0; + + ret = ax88179_read_cmd(dev, AX88279_PBUS_A32, 0xF8C8, 0x000C, 4, ®32); + if (ret < 0) + return ret; + + reg32 &= ~0x00004000; + if (enable) + reg32 |= 0x00004000; + + ret = ax88179_write_cmd(dev, AX88279_PBUS_A32, 0xF8C8, 0x000C, 4, ®32); + + return ret; +} + +static int ax88179a_ptp_config(struct usbnet *dev, int speed) +{ + struct ax88179_data *ax179_data = dev->driver_priv; + u32 new_addend_val = AX88179A_BASE_ADDEND; + struct ax88179a_ptp *ptp_data; + u8 ptp_tx_delay[5] = { 0 }; + u8 ptp_rx_delay[5] = { 0 }; + u32 timeout = 0; + u8 ptpset = 0; + u8 reg8 = 0; + int ret; + + if (!ax179_data->ptp) + return -EOPNOTSUPP; + ptp_data = ax179_data->ptp; + + ret = ax88179a_ptp_pps_ctrl(dev, true); + if (ret < 0) + return ret; + + ax88179_write_cmd(dev, AX_PTP_OP, AX_SET_ADDEND, 0, AX_SET_ADDEND_SIZE, &new_addend_val); + + ret = ax88179_write_u8(dev, AX_PTP_CMD, AX88179A_PTP_TIMER_PERIOD, 0, AX_PTP_PERIOD); + if (ret < 0) + return ret; + + ret = ax88179_read_cmd(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_2, 0, 1, ®8); + if (ret < 0) + return ret; + reg8 |= AX_PTP_CTRL_SET_PERIOD; + ret = ax88179_write_cmd(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_2, 0, 1, ®8); + if (ret < 0) + return ret; + + ret = ax88179_read_cmd(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, 1, &ptpset); + if (ret < 0) + return ret; + ptpset |= AX_PTP_CTRL_L3_EN | AX_PTP_CTRL_EN | AX_PTP_TX_PLUS_DELAY | + AX_PTP_TX_FILTER_GENERAL_MSG | AX_PTP_RX_FILTER_GENERAL_MSG; + ax88179_write_u8(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, ptpset); + ax88179_write_u8(dev, AX_PTP_CMD, AX88179A_PTP_MEM_SEG_SIZE, 0, + sizeof(struct ptp_info_179a) * AX88179A_PTP_QUEUE_SIZE); + + do { + ax88179_write_u8(dev, AX_PTP_CMD, AX88179A_PTP_MEM_SEG_SET, 0, 0); + ret = ax88179_read_cmd(dev, AX_PTP_CMD, AX88179A_PTP_MEM_SEG_STATUS, + 0, 1, ®8); + if (ret < 0) + return ret; + reg8 &= AX_PTP_MEM_SEG_MASK; + + if (timeout++ > 5) + break; + } while (reg8 != AX_PTP_MEM_SEG_0); + + ax88179_write_u32(dev, AX_PTP_OP, AX_SET_ACTIVE_TIME, 0, AX_PPS_ACTIVE_DEFAULT_TIME); + + switch (speed) { + case ETHER_LINK_10: + ptp_tx_delay[0] = 0xB7; + ptp_tx_delay[1] = 0x0F; + ptp_tx_delay[4] = ptpset; + ptp_rx_delay[0] = 0xAC; + ptp_rx_delay[1] = 0x0A; + ptp_rx_delay[4] = ptpset; + break; + case ETHER_LINK_100: + ptp_tx_delay[0] = 0x64; + ptp_tx_delay[4] = ptpset; + ptp_rx_delay[0] = 0x22; + ptp_rx_delay[1] = 0x01; + ptp_rx_delay[4] = ptpset; + break; + case ETHER_LINK_1000: + default: + ptp_tx_delay[0] = 0x6E; + ptp_tx_delay[4] = ptpset; + ptp_rx_delay[0] = 0xE4; + ptp_rx_delay[4] = ptpset; + break; + } + + ax88179_write_cmd(dev, AX_PTP_OP, AX_SET_TX_PHY_DELAY, 0, + AX_SET_TX_PHY_DELAY_SIZE, ptp_tx_delay); + ax88179_write_cmd(dev, AX_PTP_OP, AX_SET_RX_PHY_DELAY, 0, + AX_SET_RX_PHY_DELAY_SIZE, ptp_rx_delay); + + ptp_data->tx_tstamp = true; + ptp_data->rx_tstamp = true; + + return 0; +} + +static int ax88279_ptp_config(struct usbnet *dev, int speed) +{ + u8 reg8 = 0; + int ret; + + ax88279_ptp_pps_ctrl(dev, true); + + ax88279_ptp_pbus_write_u32(dev, AX_PTP_TX_MEM_SETTING, + (AX_PTP_MEM_SEG_SIZE_279 << 24) | + (AX_PTP_MEM_START_ADDR << 8) | AX_PTP_PTP_CPU_EN); + + ax88279_ptp_pbus_write_u32(dev, AX_PTP_PPS_ACTIVE_TIME, AX_PPS_ACTIVE_DEFAULT_TIME); + + ax88279_ptp_pbus_write_u32(dev, AX_PTP_LCK_CTRL0, + AX_PTP_LCK_CTRL0_EN | AX_PTP_LCK_CTRL0_80B_NS_EN | + AX_PTP_LCK_CTRL0_80B_S_EN | AX_PTP_LCK_CTRL0_48B_EN | + AX_PTP_LCK_CTRL0_PPS_EN | AX_PTP_LCK_CTRL0_TX_DEL_VEC); + + ax88279_ptp_pbus_write_u32(dev, AX88179A_PTP_TIMER_ADDEND_VAL, AX88179A_BASE_ADDEND); + ax88279_ptp_pbus_write_u32(dev, AX88179A_PTP_TIMER_PERIOD_VAL, AX_PTP_PERIOD); + + ret = ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MAC_BFM_CTRL, 1, 1, ®8); + if (ret < 0) + goto out; + reg8 |= AX_CS_TRAIL_UDPV4_EN | AX_CS_TRAIL_UDPV6_EN; + ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MAC_BFM_CTRL, 1, 1, ®8); + + switch (speed) { + case SPEED_100: + ax88179_write_u32(dev, AX88279_PBUS_A32, AX_TX_READY_CTRL, + AX88279_PBUS_REG_BASE_ADDR_HI, + (AX_IPG_COUNTER_100M) | + (AX_SOF_DELAY_COUNTER_100M << 8) | + (AX_VAILD_DELAY_COUNTER_100M << 16)); + + ax88279_ptp_pbus_write_u32(dev, AX_PTP_RX_CTRL0, AX_PTP_RX_CTRL0_DEFAULT); + ax88279_ptp_pbus_write_u32(dev, AX_PTP_TX_CTRL0, + AX_PTP_TX_CTRL0_DEFAULT | + (0x10 << AX_TXC0_VAL_DELAY_CNT_SHIFT)); + break; + + case SPEED_1000: + ax88179_write_u32(dev, AX88279_PBUS_A32, AX_TX_READY_CTRL, + AX88279_PBUS_REG_BASE_ADDR_HI, (AX_IPG_COUNTER_1G) | + (AX_SOF_DELAY_COUNTER_1G << 8) | + (AX_VAILD_DELAY_COUNTER_1G << 16)); + + ax88279_ptp_pbus_write_u32(dev, AX_PTP_RX_CTRL0, AX_PTP_RX_CTRL0_DEFAULT); + ax88279_ptp_pbus_write_u32(dev, AX_PTP_TX_CTRL0, + AX_PTP_TX_CTRL0_DEFAULT | + (0x8 << AX_TXC0_VAL_DELAY_CNT_SHIFT)); + break; + + case SPEED_2500: + ax88279_ptp_pbus_write_u32(dev, AX_PTP_RX_CTRL0, + AX_PTP_RX_CTRL0_DEFAULT | AX_PTP_RXC0_XGMII_EN); + ax88279_ptp_pbus_write_u32(dev, AX_PTP_TX_CTRL0, + AX_PTP_TX_CTRL0_DEFAULT | + AX_PTP_TXC0_XGMII_EN | + (0x8 << AX_TXC0_VAL_DELAY_CNT_SHIFT)); + break; + + default: + break; + } + + ax88279_ptp_pbus_write_u32(dev, AX88179A_PTP_TX_DELAY, 0); + ax88279_ptp_pbus_write_u32(dev, AX88179A_PTP_RX_DELAY, 0); + + ax88179_write_u8(dev, AX_PTP_TOD_CTRL, (AX_PTP_TS_INT | AX_EXT_INT), 0, + AX_EXT_INT_ON | AX_PTP_TX_TX_INT_EN); + ax88179_write_u32(dev, AX88279_PBUS_A32, AX_MAC_CLK_CTRL, AX88279_PBUS_REG_BASE_ADDR_HI, 0); + + ax88179_write_u32(dev, AX88279_PBUS_A32, AX_MAC_CLK_CTRL, AX88279_PBUS_REG_BASE_ADDR_HI, + (0 << AX_DIVIDE_PTP_CLK_SHIFT) | + (1 << AX_DIVIDE_AES_CLK_SHIFT) | + AX_PTP_CLK_EN | AX_AES_CLK_EN | + AX_PTP_CLK_SELECT_DIVIDE | + AX_AES_CLK_SELECT_DIVIDE | + AX_XGMAC_TX_CLK_EN | + AX_XGMAC_RX_CLK_EN); + +out: + return ret; +} + +int ax88179a_hwtstamp_set(struct net_device *net, + struct kernel_hwtstamp_config *config, + struct netlink_ext_ack *extack) +{ + struct ax88179_data *ax179_data = netdev2data(net); + struct ax88179a_ptp *ptp_data = ax179_data->ptp; + + if (!ax179_data->ptp) + return -EOPNOTSUPP; + + switch (config->tx_type) { + case HWTSTAMP_TX_OFF: + /* Cannot disable rx/tx timestamping separately. + * Do nothing here. + */ + ptp_data->tx_tstamp = false; + break; + + case HWTSTAMP_TX_ON: + ptp_data->tx_tstamp = true; + break; + default: + return -ERANGE; + } + + if (config->rx_filter == HWTSTAMP_FILTER_NONE) { + /* Couldn't disable rx/tx timestamping separately. + * Do nothing here. + */ + ptp_data->rx_tstamp = false; + } else { + ptp_data->rx_tstamp = true; + /* TS is set for all frame types, not only those requested */ + config->rx_filter = HWTSTAMP_FILTER_ALL; + } + + return 0; +} + +int ax88179a_hwtstamp_get(struct net_device *net, + struct kernel_hwtstamp_config *config) +{ + struct ax88179_data *ax179_data = netdev2data(net); + struct ax88179a_ptp *ptp_data = ax179_data->ptp; + + if (!ax179_data->ptp) + return -EOPNOTSUPP; + + config->tx_type = ptp_data->tx_tstamp ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; + config->rx_filter = ptp_data->rx_tstamp ? HWTSTAMP_FILTER_ALL : + HWTSTAMP_FILTER_NONE; + + return 0; +} + +static struct ptp_clock_info ax88179a_ptp_clock = { + .owner = THIS_MODULE, + .name = "ASIX88179A PTP", + .max_adj = 100000000, + .n_ext_ts = 0, + .n_per_out = 0, + .n_pins = 0, + .pps = 0, + .adjfine = ax88179a_ptp_adjfine, + .adjtime = ax88179a_ptp_adjtime, + .gettime64 = ax88179a_ptp_gettime64, + .settime64 = ax88179a_ptp_settime64, + .enable = ax88179a_ptp_enable, + .verify = NULL, + .pin_config = NULL, +}; + +int ax88179a_ptp_register(struct usbnet *dev) +{ + struct ax88179a_ptp *ax88179a_ptp; + struct ax88179_data *ax179_data; + u8 ptp_ver; + int ret = 0; + + ax179_data = dev->driver_priv; + ax88179a_ptp = kzalloc_obj(*ax88179a_ptp); + if (!ax88179a_ptp) + return -ENOMEM; + + ax179_data->ptp = ax88179a_ptp; + ax88179a_ptp->dev = dev; + ax88179a_ptp->ax179_data = ax179_data; + + ax88179a_ptp->ptp_info_buf_head = 0; + ax88179a_ptp->ptp_info_buf_tail = 0; + INIT_WORK(&ax88179a_ptp->process_ptp_info_buf, ax88279_process_tx_ts); + INIT_WORK(&ax88179a_ptp->req_tx_timestamp, ax88179a_ptp_read_ts_work); + skb_queue_head_init(&ax88179a_ptp->tx_skbs); + + switch (ax179_data->chip_version) { + case AX_VERSION_AX88279: + ax88179a_ptp->urb = usb_alloc_urb(0, GFP_KERNEL); + if (!ax88179a_ptp->urb) + goto err; + ax88179a_ptp->pps_ctrl = ax88279_ptp_pps_ctrl; + ax88179a_ptp->ptp_config = ax88279_ptp_config; + ax88179a_ptp->ptp_read_ts = ax88279_ptp_read_ts; + netdev_info(dev->net, "PTP enabled on AX88279\n"); + break; + + case AX_VERSION_AX88179A: + ax88179a_ptp->urb = usb_alloc_urb(0, GFP_KERNEL); + if (!ax88179a_ptp->urb) + goto err; + ax88179a_ptp->ctrlreq = kzalloc_obj(*ax179_data->ptp->ctrlreq); + if (!ax88179a_ptp->ctrlreq) + goto no_ctrlreq; + if (ax88179_read_cmd(dev, AX88179A_ACCESS_BL, + AX88179A_HW_EC_VERSION, 1, 1, &ptp_ver) < 0) + return 0; + + if (ptp_ver < 3) { + netdev_warn(dev->net, "PTP on AX88179A version %d < 3 unsupported\n", + ptp_ver); + return 0; + } + netdev_info(dev->net, "PTP on AX88179A version %d enabled\n", ptp_ver); + + ax88179a_ptp->pps_ctrl = ax88179a_ptp_pps_ctrl; + ax88179a_ptp->ptp_config = ax88179a_ptp_config; + ax88179a_ptp->ptp_read_ts = ax88179a_ptp_read_ts; + + // PTP excludes TSO and SG on AX88179A + dev->net->features &= ~(NETIF_F_SG | NETIF_F_TSO); + dev->net->hw_features &= ~(NETIF_F_SG | NETIF_F_TSO); + dev->net->vlan_features &= ~(NETIF_F_SG | NETIF_F_TSO); + break; + + default: + netdev_warn(dev->net, "PTP not supported\n"); + ret = -EOPNOTSUPP; + goto no_ctrlreq; + }; + + ax88179a_ptp->ptp_ci = ax88179a_ptp_clock; + ax88179a_ptp->ptp_clock = ptp_clock_register(&ax88179a_ptp->ptp_ci, &dev->udev->dev); + + if (IS_ERR(ax88179a_ptp->ptp_clock)) { + dev_err(&dev->udev->dev, "ptp_clock_register failed\n"); + ret = PTR_ERR(ax88179a_ptp->ptp_clock); + goto err; + } + + ax88179a_ptp->phc_index = ptp_clock_index(ax88179a_ptp->ptp_clock); + + ax88179a_ptp->ax179_data = ax179_data; + + return 0; + +no_ctrlreq: + kfree(ax88179a_ptp->urb); +err: + kfree(ax88179a_ptp); + return ret; +} + +void ax88179a_ptp_unregister(struct usbnet *dev) +{ + struct ax88179a_ptp *ax88179a_ptp; + struct ax88179_data *ax179_data; + + ax179_data = dev->driver_priv; + ax88179a_ptp = ax179_data->ptp; + + if (!ax179_data->ptp) + return; + + ax88179a_ptp->pps_ctrl(dev, false); + cancel_work_sync(&ax88179a_ptp->process_ptp_info_buf); + + if (ax179_data->chip_version == AX_VERSION_AX88279) { + ax88279_ptp_pbus_write_u32(dev, AX88279_PTP_LCK_CTRL0, 0); + ax88279_ptp_pbus_write_u32(dev, AX88279_PTP_RX_CTRL0, 0); + ax88279_ptp_pbus_write_u32(dev, AX88279_PTP_TX_CTRL0, 0); + usb_free_urb(ax88179a_ptp->urb); + } else { + u8 reg8 = 0; + + ax88179_read_cmd(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, 1, ®8); + reg8 &= ~(AX_PTP_CTRL_L3_EN | AX_PTP_CTRL_EN); + ax88179_write_cmd(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, 1, ®8); + kfree(ax88179a_ptp->ctrlreq); + } + skb_queue_purge(&ax88179a_ptp->tx_skbs); + + if (ax88179a_ptp && ax88179a_ptp->ptp_clock) + ptp_clock_unregister(ax88179a_ptp->ptp_clock); + + kfree(ax179_data->ptp); +} diff --git a/drivers/net/usb/ax88179a_ptp.h b/drivers/net/usb/ax88179a_ptp.h new file mode 100644 index 0000000000000000000000000000000000000000..534e73cdc951ebdac57caf9109e9b5552fd46961 --- /dev/null +++ b/drivers/net/usb/ax88179a_ptp.h @@ -0,0 +1,249 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <linux/ptp_clock_kernel.h> + +#ifndef __LINUX_USBNET_AX88179A_PTP_H +#define __LINUX_USBNET_AX88179A_PTP_H + +#define AX88179A_BASE_ADDEND 0xCCCCCCCC +#define AX88179A_PTP_TIMER_ADDEND_VAL 0x1030 + +#define AX_PTP_CMD 0x09 +#define AX_PTP_OP 0x0E +#define AX_SET_LOCAL_CLOCK 0x01 +#define AX_SET_LOCAL_CLOCK_SIZE 0x0A +#define AX_GET_LOCAL_CLOCK 0x02 +#define AX_GET_LOCAL_CLOCK_SIZE 0x0A +#define AX_SET_ADDEND 0x03 +#define AX_SET_ADDEND_SIZE 0x04 + +#define AX88179A_PTP_TIMER_PERIOD 0x0A +#define AX_PTP_PERIOD 0x0A +#define AX88179A_PTP_TIMER_PERIOD_VAL 0x1034 + +#define AX88179A_PTP_GET_80B_LCK 0x1014 +#define AX88179A_PTP_SET_80B_LCK 0x1008 +#define AX_PTP_DATA_LEN 0x000C + +#define AX_PTP_CLK_EN 0x010000 +#define AX_PTP_CLK_SELECT_DIVIDE 0x040000 + +#define AX88179A_PBUS_REG 0x10 +#define AX_PTP_CLK 0x13 + +#define AX88279_PTP_LCK_CTRL0 0x1000 +#define AX_PTP_LCK_CTRL0_EN 0x0001 +#define AX_PTP_LCK_CTRL0_80B_NS_EN 0x0002 +#define AX_PTP_LCK_CTRL0_80B_S_E 0x0004 +#define AX_PTP_LCK_CTRL0_48B_EN 0x0008 +#define AX_PTP_LCK_CTRL0_PPS_EN 0x0010 +#define AX_PTP_LCK_CTRL0_TX_DEL_VEC 0x0040 + +#define AX88279_PTP_RX_CTRL0 0x2000 +#define AX88279_PTP_TX_CTRL0 0x3000 + +#define AX88179A_PTP_CTRL_1 0x00 +#define AX_PTP_CTRL_EN 0x01 +#define AX_PTP_CTRL_L3_EN 0x02 +#define AX_PTP_TX_FILTER_GENERAL_MSG 0x08 +#define AX_PTP_RX_FILTER_GENERAL_MSG 0x10 +#define AX_PTP_TX_PLUS_DELAY 0x20 +#define AX88179A_PTP_MEM_SEG_SIZE 0x21 + +#define AX88179A_PTP_CTRL_2 0x01 +#define AX_PTP_CTRL_SET_PERIOD 0x08 + +#define AX88179A_PTP_MEM_SEG_SIZE 0x21 +#define AX88179A_PTP_MEM_SEG_SET 0x22 +#define AX88179A_PTP_MEM_SEG_STATUS 0x23 +#define AX_PTP_MEM_SEG_MASK 0x01 +#define AX_PTP_MEM_SEG_0 0 + +#define AX_SET_ACTIVE_TIME 0x06 +#define AX_SET_ACTIVE_TIME_SIZE 0x04 + +#define AX_SET_TX_PHY_DELAY 0x07 +#define AX_SET_TX_PHY_DELAY_SIZE 0x05 +#define AX_SET_RX_PHY_DELAY 0x08 +#define AX_SET_RX_PHY_DELAY_SIZE AX_SET_TX_PHY_DELAY_SIZE + +#define AX_MAC_BFM_CTRL 0xC1 +#define AX_CS_TRAIL_UDPV4_EN 0x40 +#define AX_CS_TRAIL_UDPV6_EN 0x80 + +#define AX_PTP_TIMESTAMP_REQ 0x0A + +/* PBUS registers/settings for AX88279 */ + +#define AX88279_PBUS_A32 0x11 +#define AX88279_PBUS_REG_BASE_ADDR_HI 0x0013 +#define AX88279_PTP_REG_BASE_ADDR_HI 0x0012 + +#define AX_PTP_TX_MEM_SETTING 0x0000 +#define AX_PTP_PTP_CPU_EN 0x0001 +#define AX_PTP_MEM_SEG_SIZE_279 0x41 +#define AX_PTP_MEM_START_ADDR 0x800 + +#define AX_PTP_LCK_CTRL0 0x1000 +#define AX_PTP_LCK_CTRL0_EN 0x0001 +#define AX_PTP_LCK_CTRL0_80B_NS_EN 0x0002 +#define AX_PTP_LCK_CTRL0_80B_S_EN 0x0004 +#define AX_PTP_LCK_CTRL0_48B_EN 0x0008 +#define AX_PTP_LCK_CTRL0_PPS_EN 0x0010 +#define AX_PTP_LCK_CTRL0_TX_DEL_VEC 0x0040 + +#define AX_PTP_PPS_ACTIVE_TIME 0x1040 +#define AX_PPS_ACTIVE_DEFAULT_TIME 0x1DCD6500 +#define AX88179A_PTP_RX_DELAY 0x103C +#define AX88179A_PTP_TX_DELAY 0x1038 + +#define AX_TX_READY_CTRL 0x1008 + +#define AX_MAC_CLK_CTRL 0x3004 + +#define AX_PTP_CLK_EN 0x010000 +#define AX_AES_CLK_EN 0x020000 +#define AX_AES_CLK_SELECT_DIVIDE 0x080000 +#define AX_XGMAC_TX_CLK_EN 0x100000 +#define AX_XGMAC_RX_CLK_EN 0x200000 + +#define AX_PTP_TOD_CTRL 0x15 +#define AX_PTP_TS_INT 0x02 +#define AX_EXT_INT 0x04 +#define AX_EXT_INT_ON 0x10 +#define AX_PTP_TX_TX_INT_EN 0x40 + +#define AX_IPG_COUNTER_100M 0x28 +#define AX_SOF_DELAY_COUNTER_100M 0x1C +#define AX_VAILD_DELAY_COUNTER_100M 0x0F + +#define AX_IPG_COUNTER_1G 0x22 +#define AX_SOF_DELAY_COUNTER_1G 0x06 +#define AX_VAILD_DELAY_COUNTER_1G 0x07 + +#define AX_PTP_RX_CTRL0 0x2000 + #define AX_PTP_RXC0_EN 0x0001 + #define AX_PTP_RXC0_XGMII_EN 0x0002 + #define AX_RXC0_PARSER_L2_EN 0x0010 + #define AX_RXC0_PARSER_L2_PTP_EN 0x0020 + #define AX_RXC0_PARSER_IPV4_EN 0x0040 + #define AX_RXC0_PARSER_IPV6_EN 0x0080 + #define AX_RXC0_PARSER_VLAN_EN 0x0100 + #define AX_RXC0_PARSER_PPPOE_EN 0x0200 + #define AX_RXC0_PARSER_SNAP_EN 0x0400 + #define AX_RXC0_PARSER_UDP_EN 0x0800 + #define AX_RXC0_PARSER_UDPV6_EN 0x1000 + #define AX_RXC0_PARSER_UDP_PTP_EN 0x2000 + #define AX_RXC0_PARSER_UDPV6_PTP_EN 0x4000 + #define AX_RXC0_PARSER_EVENT_PORT_EN 0x00010000 + #define AX_RXC0_VAL_DELAY_CNT_SHIFT 24 + +#define AX_PTP_TX_CTRL0 0x3000 + #define AX_PTP_TXC0_EN 0x0001 + #define AX_PTP_TXC0_XGMII_EN 0x0002 + #define AX_TXC0_PARSER_L2_EN 0x0010 + #define AX_TXC0_PARSER_L2_PTP_EN 0x0020 + #define AX_TXC0_PARSER_IPV4_EN 0x0040 + #define AX_TXC0_PARSER_IPV6_EN 0x0080 + #define AX_TXC0_PARSER_VLAN_EN 0x0100 + #define AX_TXC0_PARSER_PPPOE_EN 0x0200 + #define AX_TXC0_PARSER_SNAP_EN 0x0400 + #define AX_TXC0_PARSER_UDP_EN 0x0800 + #define AX_TXC0_PARSER_UDPV6_EN 0x1000 + #define AX_TXC0_PARSER_UDP_PTP_EN 0x2000 + #define AX_TXC0_PARSER_UDPV6_PTP_EN 0x4000 + #define AX_TXC0_PARSER_EVENT_PORT_EN 0x00010000 + #define AX_TXC0_VAL_DELAY_CNT_SHIFT 24 + +#define AX_PTP_RX_CTRL0_DEFAULT \ + (AX_PTP_RXC0_EN | AX_RXC0_PARSER_L2_EN | AX_RXC0_PARSER_L2_PTP_EN | \ + AX_RXC0_PARSER_IPV4_EN | AX_RXC0_PARSER_IPV6_EN | \ + AX_RXC0_PARSER_VLAN_EN | AX_RXC0_PARSER_PPPOE_EN | \ + AX_RXC0_PARSER_SNAP_EN | AX_RXC0_PARSER_UDP_EN | \ + AX_RXC0_PARSER_UDPV6_EN | AX_RXC0_PARSER_UDP_PTP_EN | \ + AX_RXC0_PARSER_UDPV6_PTP_EN | AX_RXC0_PARSER_EVENT_PORT_EN) + +#define AX_PTP_TX_CTRL0_DEFAULT \ + (AX_PTP_TXC0_EN | AX_TXC0_PARSER_L2_EN | AX_TXC0_PARSER_L2_PTP_EN | \ + AX_TXC0_PARSER_IPV4_EN | AX_TXC0_PARSER_IPV6_EN | \ + AX_TXC0_PARSER_VLAN_EN | AX_TXC0_PARSER_PPPOE_EN | \ + AX_TXC0_PARSER_SNAP_EN | AX_TXC0_PARSER_UDP_EN | \ + AX_TXC0_PARSER_UDPV6_EN | AX_TXC0_PARSER_UDP_PTP_EN | \ + AX_TXC0_PARSER_UDPV6_PTP_EN | AX_TXC0_PARSER_EVENT_PORT_EN) + +#define AX_DIVIDE_PTP_CLK_SHIFT 0 +#define AX_DIVIDE_AES_CLK_SHIFT 8 + +#define AX_TS_SEG_1 1 + +#define AX_TX_TIMESTAMPS BIT(1) + +// PTP TX information returned by AX87279-NIC in proprietary URB +struct ptp_info_279 { + u8 reserved :3, + status :1, + msg_type :4; + __le16 sequence_id; + __le32 nsec; + __le32 sec_l; + __le16 sec_h; +} __packed; + +// PTP TX information returned by AX88179A-NIC in proprietary URB +struct ptp_info_179a { + u8 reserved :3, + status :1, + msg_type :4; + u8 sequence_id; + __le32 nsec; + __le32 sec_l; + __le16 sec_h; +} __packed; + +// Generic PTP TX information for matching with PTP frame +struct ptp_info { + u8 msg_type; + u16 sequence_id; + u32 nsec; + u64 sec; +}; + +// Size of the TS request structure +#define AX88179A_PTP_QUEUE_SIZE 5 +#define AX88279_PTP_EP4_SIZE ((2 * sizeof(struct ptp_info_279) * AX88179A_PTP_QUEUE_SIZE) + 1) + +struct ax88179a_ptp { + struct usbnet *dev; + struct ax88179_data *ax179_data; + struct ax_ptp_func *ptp_func; + struct ptp_clock_info ptp_ci; + struct ptp_clock *ptp_clock; + unsigned int phc_index; + bool tx_tstamp; + bool rx_tstamp; + struct urb *urb; + struct usb_ctrlrequest *ctrlreq; + unsigned char ep4_buf[AX88279_PTP_EP4_SIZE]; + struct ptp_info ptp_info_buf[AX88179A_PTP_QUEUE_SIZE]; + struct sk_buff_head tx_skbs; + int ptp_info_buf_head; + int ptp_info_buf_tail; + int (*pps_ctrl)(struct usbnet *dev, bool enable); + int (*ptp_config)(struct usbnet *dev, int speed); + int (*ptp_read_ts)(struct usbnet *dev); + struct work_struct process_ptp_info_buf; + struct work_struct req_tx_timestamp; +}; + +void ax88179a_ptp_kill_urbs(struct usbnet *dev); +int ax88179a_ptp_register(struct usbnet *dev); +void ax88179a_ptp_unregister(struct usbnet *dev); +void ax88179a_process_rx_ts(struct sk_buff *skb, u64 timestamp_l, u64 timestamp_h); +int ax88179a_hwtstamp_set(struct net_device *netdev, + struct kernel_hwtstamp_config *config, + struct netlink_ext_ack *extack); + +int ax88179a_hwtstamp_get(struct net_device *net, + struct kernel_hwtstamp_config *config); + +#endif -- 2.47.3 ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation 2026-11-29 9:29 ` [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation Birger Koblitz @ 2026-11-29 18:31 ` Manuel Ebner 2026-11-30 5:18 ` Birger Koblitz 2026-11-30 9:30 ` sashiko-bot 1 sibling, 1 reply; 13+ messages in thread From: Manuel Ebner @ 2026-11-29 18:31 UTC (permalink / raw) To: Birger Koblitz, Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran, Russell King Cc: netdev, linux-usb, linux-kernel On Sun, 2026-11-29 at 10:29 +0100, Birger Koblitz wrote: > Adds support to match TX timestamps for packets for which TX timestamps > are requested against TX timestamps received separately by the host and > processing of the hardware timestamps inserted in received frames. > > For both AX88179A and AX88279 support for a PTP clock with adjfine(), > adjtime(), gettime64() and settime64() is added. > > AX88179A and AX88279 implement reception of the TX timestamp information > differently: the AX279A uses USB bulk requests, whereas the AX88179A uses > a control transfer read. Since the control transfer reads via usbnet are > synchronous (in contrast to the AX88279A bulk reads), they are handled as > work items. > > TX frames for which a timestamp is requested are copied to a separate > ptp->tx_skbs queue and matched in a delayed work against received TX > timestamps. > > Signed-off-by: Birger Koblitz <mail@birger-koblitz.de> > --- > drivers/net/usb/Kconfig | 1 + > drivers/net/usb/Makefile | 2 +- > drivers/net/usb/ax88179a_ptp.c | 845 +++++++++++++++++++++++++++++++++++++++++ > drivers/net/usb/ax88179a_ptp.h | 249 ++++++++++++ > 4 files changed, 1096 insertions(+), 1 deletion(-) > > diff --git a/drivers/net/usb/Kconfig b/drivers/net/usb/Kconfig > index cc8f2e150339915908fe1c28dea82e1103bbf17d..2a811116d34dc1a5d2225d947662b983dac7ddfe 100644 > --- a/drivers/net/usb/Kconfig > +++ b/drivers/net/usb/Kconfig > @@ -166,6 +166,7 @@ config USB_USBNET > config USB_NET_AX8817X > tristate "ASIX AX88xxx Based USB 2.0 Ethernet Adapters" > depends on USB_USBNET > + depends on PTP_1588_CLOCK_OPTIONAL > select CRC32 > select PHYLINK > select AX88796B_PHY > diff --git a/drivers/net/usb/Makefile b/drivers/net/usb/Makefile > index 4026f19ecb826a6465de38f1d9bb0e4c0af80421..812adcf839dc4061d4d6ef27025a26723bd51927 100644 > --- a/drivers/net/usb/Makefile > +++ b/drivers/net/usb/Makefile > @@ -13,7 +13,7 @@ obj-$(CONFIG_USB_LAN78XX) += lan78xx.o > obj-$(CONFIG_USB_NET_AX8817X) += asix.o > asix-y := asix_devices.o asix_common.o ax88172a.o > obj-$(CONFIG_USB_NET_AX88179) += ax88179.o > -ax88179-y := ax88179_178a.o ax88179a_devices.o ax88179_lib.o > +ax88179-y := ax88179_178a.o ax88179a_devices.o ax88179_lib.o ax88179a_ptp.o > obj-$(CONFIG_USB_NET_CDCETHER) += cdc_ether.o > obj-$(CONFIG_USB_NET_CDC_EEM) += cdc_eem.o > obj-$(CONFIG_USB_NET_DM9601) += dm9601.o > diff --git a/drivers/net/usb/ax88179a_ptp.c b/drivers/net/usb/ax88179a_ptp.c > new file mode 100644 > index 0000000000000000000000000000000000000000..9a04a93e509523436ccd19895b2a6cb6a819aea0 > --- /dev/null > +++ b/drivers/net/usb/ax88179a_ptp.c > @@ -0,0 +1,845 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > + > +#include <linux/ptp_clock_kernel.h> > +#include <linux/net_tstamp.h> Sort headers alphabetically. > +#include <linux/ptp_classify.h> > +#include "ax88179_lib.h" > +#include "ax88179a_ptp.h" > + > +#define ptp_to_dev(ptp) container_of(ptp, struct ax88179a_ptp, ptp_ci) > + > +static int ax88279_ptp_pbus_write(struct usbnet *dev, u16 offset, > + u16 len, void *data) > +{ > + int ret; > + > + ret = ax88179_write_cmd(dev, AX88279_PBUS_A32, offset, > + AX88279_PTP_REG_BASE_ADDR_HI, len, data); > + if (ret < 0) > + return ret; > + > + return 0; > +} > + > +static int ax88279_ptp_pbus_write_u32(struct usbnet *dev, u16 offset, u32 data) > +{ > + u32 buf = data; > + > + return ax88279_ptp_pbus_write(dev, offset, 4, &buf); > +} > + > +static int ax88279_ptp_clk_read(struct usbnet *dev, u16 offset, u16 len, void *data) > +{ > + int ret; > + > + ret = ax88179_read_cmd(dev, AX_PTP_CLK, 0x0002, offset, len, data); > + if (ret < 0) > + return ret; > + > + return 0; > +} > + > +static int ax88279_ptp_clk_write(struct usbnet *dev, u16 offset, u16 len, void *data) > +{ > + int ret; > + > + ret = ax88179_write_cmd(dev, AX88279_PBUS_A32, offset, > + AX88279_PTP_REG_BASE_ADDR_HI, len, data); > + if (ret < 0) > + return ret; > + > + return 0; > +} > + > +void ax88179a_process_rx_ts(struct sk_buff *skb, u64 timestamp_l, u64 timestamp_h) > +{ > + struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); > + u64 time64; > + > + timestamp_h <<= 32; > + timestamp_h |= (timestamp_l >> 32) & 0xFFFFFFFF; > + time64 = (timestamp_h * NSEC_PER_SEC); > + time64 += (timestamp_l & 0xFFFFFFFF); > + > + memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); > + > + shhwtstamps->hwtstamp = ktime_sub_ns(ns_to_ktime(time64), 0); > +} > + > +static int ax88179a_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) > +{ > + long ppb = scaled_ppm_to_ppb(scaled_ppm); > + struct ax88179a_ptp *ax88179a_ptp; > + struct ax88179_data *ax179_data; > + bool neg_adj = false; > + u32 new_addend_val; > + u64 adjust_val; > + int ret = 0; > + > + ax88179a_ptp = ptp_to_dev(ptp); > + ax179_data = ax88179a_ptp->ax179_data; Here's a double" "^^. Reviewed-by: Manuel Ebner <manuelebnerli@mailbox.org> Thanks Manuel > [...] ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation 2026-11-29 18:31 ` Manuel Ebner @ 2026-11-30 5:18 ` Birger Koblitz 0 siblings, 0 replies; 13+ messages in thread From: Birger Koblitz @ 2026-11-30 5:18 UTC (permalink / raw) To: Manuel Ebner, Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran, Russell King Cc: netdev, linux-usb, linux-kernel Hi Manuel, thanks for reviewing! On 29/11/2026 7:31 pm, Manuel Ebner wrote: >> +++ b/drivers/net/usb/ax88179a_ptp.c >> @@ -0,0 +1,845 @@ >> +// SPDX-License-Identifier: GPL-2.0-or-later >> + >> +#include <linux/ptp_clock_kernel.h> >> +#include <linux/net_tstamp.h> > > Sort headers alphabetically. Will do. >> +static int ax88179a_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) >> +{ >> + long ppb = scaled_ppm_to_ppb(scaled_ppm); >> + struct ax88179a_ptp *ax88179a_ptp; >> + struct ax88179_data *ax179_data; >> + bool neg_adj = false; >> + u32 new_addend_val; >> + u64 adjust_val; >> + int ret = 0; >> + >> + ax88179a_ptp = ptp_to_dev(ptp); >> + ax179_data = ax88179a_ptp->ax179_data; > Here's a double" "^^. Will fix. > > Reviewed-by: Manuel Ebner <manuelebnerli@mailbox.org> > > Thanks > Manuel Thanks a lot! Birger ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation 2026-11-29 9:29 ` [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation Birger Koblitz 2026-11-29 18:31 ` Manuel Ebner @ 2026-11-30 9:30 ` sashiko-bot 1 sibling, 0 replies; 13+ messages in thread From: sashiko-bot @ 2026-11-30 9:30 UTC (permalink / raw) To: Birger Koblitz; +Cc: linux-usb Thank you for your contribution! Sashiko AI review found 14 potential issues to consider: - [High] net: usb: ax88179: PTP_1588_CLOCK_OPTIONAL added to USB_NET_AX8817X in Kconfig - [High] net: usb: ax88179: uninitialized or dangling ptp_hdr in ax88279_process_tx_ts() - [High] net: usb: ax88179: ptp_info[0] read instead of ptp_info[i] in timestamp readers - [High] net: usb: ax88179: broken error handling and leaks in ax88179a_ptp_register() - [High] net: usb: ax88179: incomplete teardown and leaks in ax88179a_ptp_unregister() - [High] net: usb: ax88179: GFP_KERNEL and error resubmit in ax88279_read_ts_complete() - [High] net: usb: ax88179: ring drain and full-queue wraparound on ptp_info_buf - [High] net: usb: ax88179: unlocked __skb_unlink() on tx_skbs and ptp_info_buf race - [High] net: usb: ax88179: unlocked tx_skbs unlink in ax88279_process_tx_ts() - [Medium] net: usb: ax88179: speed enum mismatch in ax88179a_ptp_config() - [Medium] net: usb: ax88179: missing endian conversion in 32-bit PTP writes - [Medium] net: usb: ax88179: wrong transfer size in ax88179a_ptp_read_ts() - [Medium] net: usb: ax88179: off-by-one nanosecond carry in ax88179a_ptp_adjtime() - [Low] net: usb: ax88179: duplicate macro definitions in ax88179a_ptp.h -- commit fe13df4a07a9671fd74415abcea9a55992d30d82 Author: Birger Koblitz <mail@birger-koblitz.de> net: usb: ax88179: Add PTP Implementation This commit adds PTP hardware clock and TX/RX hardware timestamping support for the AX88179A and AX88279 USB Ethernet controllers. > diff --git a/drivers/net/usb/Kconfig b/drivers/net/usb/Kconfig > index cc8f2e1503399..2a811116d34dc 100644 > --- a/drivers/net/usb/Kconfig > +++ b/drivers/net/usb/Kconfig > @@ -166,6 +166,7 @@ config USB_USBNET > config USB_NET_AX8817X > tristate "ASIX AX88xxx Based USB 2.0 Ethernet Adapters" > depends on USB_USBNET > + depends on PTP_1588_CLOCK_OPTIONAL > select CRC32 > select PHYLINK > select AX88796B_PHY > diff --git a/drivers/net/usb/Makefile b/drivers/net/usb/Makefile > index 4026f19ecb826..812adcf839dc4 100644 > --- a/drivers/net/usb/Makefile > +++ b/drivers/net/usb/Makefile > @@ -13,7 +13,7 @@ obj-$(CONFIG_USB_LAN78XX) += lan78xx.o > obj-$(CONFIG_USB_NET_AX8817X) += asix.o > asix-y := asix_devices.o asix_common.o ax88172a.o > obj-$(CONFIG_USB_NET_AX88179) += ax88179.o > -ax88179-y := ax88179_178a.o ax88179a_devices.o ax88179_lib.o > +ax88179-y := ax88179_178a.o ax88179a_devices.o ax88179_lib.o ax88179a_ptp.o [Severity: High] Should the PTP_1588_CLOCK_OPTIONAL dependency be on USB_NET_AX88179 instead of USB_NET_AX8817X? ax88179a_ptp.o is linked into ax88179.o under CONFIG_USB_NET_AX88179, so building with CONFIG_USB_NET_AX88179=y and CONFIG_PTP_1588_CLOCK=m fails at link time with undefined references to ptp_clock_register(), ptp_clock_index() and ptp_clock_unregister(). > diff --git a/drivers/net/usb/ax88179a_ptp.c b/drivers/net/usb/ax88179a_ptp.c > new file mode 100644 > index 0000000000000..9a04a93e50952 > --- /dev/null > +++ b/drivers/net/usb/ax88179a_ptp.c > @@ -0,0 +1,845 @@ [ ... ] > +static int ax88279_ptp_pbus_write(struct usbnet *dev, u16 offset, > + u16 len, void *data) > +{ > + int ret; > + > + ret = ax88179_write_cmd(dev, AX88279_PBUS_A32, offset, > + AX88279_PTP_REG_BASE_ADDR_HI, len, data); > + if (ret < 0) > + return ret; > + > + return 0; > +} > + > +static int ax88279_ptp_pbus_write_u32(struct usbnet *dev, u16 offset, u32 data) > +{ > + u32 buf = data; > + > + return ax88279_ptp_pbus_write(dev, offset, 4, &buf); > +} [Severity: Medium] Does ax88279_ptp_pbus_write_u32() write byte-reversed 32-bit values on big-endian hosts? ax88179_write_cmd() only byte-swaps 2-byte buffers and passes 4-byte buffers through raw, unlike ax88179_write_u32() which calls cpu_to_le32s(). The same endianness mismatch affects the 4-byte ax88179_write_cmd() calls in ax88179a_ptp_pps_ctrl(), ax88279_ptp_pps_ctrl() (after ax88179_read_cmd() converts reg32 to CPU byte order) and ax88179a_ptp_config(), as well as the unguarded C bitfields in struct ptp_info_279 and struct ptp_info_179a. [ ... ] > +static int ax88179a_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) > +{ > + struct ax88179a_ptp *ax88179a_ptp = ptp_to_dev(ptp); > + u8 timestamp[AX_PTP_DATA_LEN] = {0}; > + struct ax88179_data *ax179_data; > + bool delta_is_neg = false; > + u64 sec, d, d_sec; > + u32 d_nsec; > + u32 nsec; > + int ret; > + > + ax179_data = ax88179a_ptp->ax179_data; > + if (ax179_data->chip_version == AX_VERSION_AX88279) > + ret = ax88279_ptp_clk_read(ax88179a_ptp->dev, AX88179A_PTP_GET_80B_LCK, > + AX_PTP_DATA_LEN, timestamp); > + else > + ret = ax88179_read_cmd(ax88179a_ptp->dev, AX_PTP_OP, AX_GET_LOCAL_CLOCK, 0, > + AX_GET_LOCAL_CLOCK_SIZE, timestamp); > + > + if (ret < 0) > + return ret; > + > + nsec = get_unaligned_le32(timestamp); > + sec = get_unaligned_le64(×tamp[4]); > + if (delta < 0) > + delta_is_neg = true; > + d = abs(delta); > + > + d_sec = div_u64_rem(d, NSEC_PER_SEC, &d_nsec); > + if (delta_is_neg) { > + if (d_nsec > nsec) { > + nsec += NSEC_PER_SEC; > + sec -= 1; > + } > + nsec -= d_nsec; > + sec -= d_sec; > + } else { > + nsec += d_nsec; > + if (nsec > NSEC_PER_SEC) { > + sec += 1; > + nsec -= NSEC_PER_SEC; > + } > + sec += d_sec; > + } [Severity: Medium] Should the nanosecond carry check in ax88179a_ptp_adjtime() be "nsec >= NSEC_PER_SEC" instead of "nsec > NSEC_PER_SEC"? When nsec + d_nsec equals NSEC_PER_SEC, this writes 1000000000 into the nanosecond field without incrementing sec. In addition, on AX88279 both ax88179a_ptp_adjtime() and ax88179a_ptp_gettime64() read AX_PTP_DATA_LEN (12) bytes from the 80-bit clock register without masking the upper 16 bits of sec, and the read-modify-write sequence in ax88179a_ptp_adjtime() is not serialized against concurrent clock adjustments. [ ... ] > +static int match_ptp_ts(struct ax88179a_ptp *ptp, struct ptp_header *ptp_hdr, > + struct sk_buff *skb, int type) > +{ > + u16 sequence_id; > + > + sequence_id = ntohs(ptp_hdr->sequence_id); > + if (ptp->ax179_data->chip_version == AX_VERSION_AX88179A) > + sequence_id &= 0xff; > + > + while (ptp->ptp_info_buf_tail != ptp->ptp_info_buf_head) { > + struct ptp_info *ptp_info = &ptp->ptp_info_buf[ptp->ptp_info_buf_tail]; > + > + if (ptp_info->sequence_id == sequence_id && > + (ptp_hdr->tsmt & 0xf) == ptp_info->msg_type) { > + struct skb_shared_hwtstamps shhwtstamps; > + u64 timestamp_h, timestamp_l; > + u8 msgtype; > + u64 time64; > + > + timestamp_l = ptp_info->nsec; > + timestamp_h = ptp_info->sec; > + time64 = timestamp_h * NSEC_PER_SEC; > + time64 += timestamp_l & 0xFFFFFFFF; > + memset(&shhwtstamps, 0, sizeof(shhwtstamps)); > + shhwtstamps.hwtstamp = ns_to_ktime(time64); > + > + msgtype = ptp_get_msgtype(ptp_hdr, type); > + if (ptp_hdr->flag_field[0] & 0x2 || (msgtype != 0 && msgtype != 3)) > + skb_tstamp_tx(skb, &shhwtstamps); > + } > + ptp->ptp_info_buf_tail++; > + ptp->ptp_info_buf_tail %= AX88179A_PTP_QUEUE_SIZE; > + } > + > + return 0; > +} [Severity: High] Doesn't match_ptp_ts() drain the entire ptp_info_buf ring on the first skb in ptp->tx_skbs? Because the loop never breaks after a match and advances ptp_info_buf_tail to ptp_info_buf_head unconditionally, all remaining queued timestamps are discarded and any subsequent skbs in ptp->tx_skbs see an empty ring. Furthermore, ptp_info_buf has AX88179A_PTP_QUEUE_SIZE (5) slots, so when all 5 slots are populated in a batch, advancing ptp_info_buf_head 5 times modulo 5 wraps it back to ptp_info_buf_tail and makes the full ring appear empty. > + > +static void ax88279_process_tx_ts(struct work_struct *work) > +{ > + struct ptp_header *ptp_hdr; > + struct sk_buff *skb, *tmp; > + struct ax88179a_ptp *ptp; > + int type; > + > + ptp = container_of(work, struct ax88179a_ptp, process_ptp_info_buf); > + > + skb_queue_walk_safe(&ptp->tx_skbs, skb, tmp) { > + type = ptp_classify_raw(skb); > + if (type != PTP_CLASS_NONE) { > + ptp_hdr = ptp_parse_header(skb, type); > + if (!ptp_hdr) > + goto frame_err; > + } > + match_ptp_ts(ptp, ptp_hdr, skb, type); > +frame_err: > + __skb_unlink(skb, &ptp->tx_skbs); > + dev_kfree_skb_any(skb); > + } > +} [Severity: High] When ptp_classify_raw(skb) returns PTP_CLASS_NONE, doesn't ax88279_process_tx_ts() pass an uninitialized or dangling ptp_hdr to match_ptp_ts()? If a non-PTP packet is queued in ptp->tx_skbs (or CONFIG_NET_PTP_CLASSIFY is disabled), ptp_hdr is not set and execution still falls through to match_ptp_ts(), which dereferences ptp_hdr->sequence_id. On the first iteration ptp_hdr is uninitialized stack data; on later iterations it points into the skb freed by dev_kfree_skb_any() on a previous iteration. [Severity: High] Can walking ptp->tx_skbs and calling __skb_unlink() without holding ptp->tx_skbs.lock corrupt the queue? CPU0 (TX path) CPU1 (kworker) ax88179a_tx_fixup() ax88279_process_tx_ts() skb_queue_tail(&ptp->tx_skbs) skb_queue_walk_safe(&ptp->tx_skbs, ...) spin_lock_irqsave(&list->lock) __skb_queue_tail(...) __skb_unlink(skb, &ptp->tx_skbs) <-- unlocked list mutation __skb_unlink() is a non-atomic helper that requires ptp->tx_skbs.lock; using skb_dequeue(&ptp->tx_skbs) in a while loop avoids racing with skb_queue_tail() and skb_queue_purge(). Likewise, match_ptp_ts() reads ptp->ptp_info_buf and updates ptp_info_buf_tail without synchronization while ax88279_read_ts_complete() (in URB completion context) or ax88179a_ptp_read_ts() writes ptp_info_buf and updates ptp_info_buf_head. > + > +static void ax88279_read_ts_complete(struct urb *urb) > +{ > + struct ax88179_data *ax179_data; > + struct ptp_info_279 *ptp_info; > + struct ax88179a_ptp *ptp; > + struct usbnet *dev; > + int offset; > + > + dev = urb->context; > + ax179_data = dev->driver_priv; > + if (!ax179_data) > + return; > + > + ptp = ax179_data->ptp; > + if (!ptp) > + return; > + > + if (!netif_carrier_ok(dev->net)) > + return; > + > + if (urb->status < 0) { > + netdev_err(dev->net, "Timestamp URB status error: %d", urb->status); > + goto out; > + } > + > + usb_mark_last_busy(dev->udev); > + > + // There can only be 2 packets in this buffer > + offset = (ptp->ep4_buf[AX88279_PTP_EP4_SIZE - 1] & AX_TS_SEG_1) ? > + 0 : (sizeof(struct ptp_info_279) * AX88179A_PTP_QUEUE_SIZE); > + > + ptp_info = (struct ptp_info_279 *)&ptp->ep4_buf[offset]; > + for (int i = 0; i < AX88179A_PTP_QUEUE_SIZE; i++) { > + int j; > + > + j = ptp->ptp_info_buf_head; > + if (ptp_info[i].status) { > + ptp->ptp_info_buf[j].msg_type = ptp_info->msg_type; > + ptp->ptp_info_buf[j].sequence_id = le16_to_cpu(ptp_info->sequence_id); > + ptp->ptp_info_buf[j].nsec = le32_to_cpu(ptp_info->nsec); > + ptp->ptp_info_buf[j].sec = le16_to_cpu(ptp_info->sec_h); > + ptp->ptp_info_buf[j].sec <<= 32; > + ptp->ptp_info_buf[j].sec |= le32_to_cpu(ptp_info->sec_l); > + ptp->ptp_info_buf_head++; > + ptp->ptp_info_buf_head %= AX88179A_PTP_QUEUE_SIZE; > + } > + } > + schedule_work(&ptp->process_ptp_info_buf); > +out: > + ptp->ptp_read_ts(dev); > +} [Severity: High] Doesn't this loop copy from ptp_info (element 0) instead of ptp_info[i] when ptp_info[i].status is set? Any timestamp reported in slots 1..4 is overwritten with the contents of slot 0. The same bug is present in ax88179a_ptp_read_ts() below. > + > +void ax88179a_ptp_kill_urbs(struct usbnet *dev) > +{ > + struct ax88179_data *ax179_data = dev->driver_priv; > + > + if (ax179_data->chip_version == AX_VERSION_AX88279 && ax179_data->ptp && > + ax179_data->ptp->urb) { > + netdev_info(dev->net, "PTP: killing URBs\n"); > + usb_kill_urb(ax179_data->ptp->urb); > + } > +} > + > +static int ax88279_ptp_read_ts(struct usbnet *dev) > +{ > + struct ax88179_data *ax179_data = dev->driver_priv; > + struct ax88179a_ptp *ptp = ax179_data->ptp; > + struct urb *urb = ptp->urb; > + int ret; > + > + memset(ptp->ep4_buf, 0, AX88279_PTP_EP4_SIZE); > + usb_fill_bulk_urb(urb, dev->udev, > + usb_rcvbulkpipe(dev->udev, 4), > + (void *)ptp->ep4_buf, AX88279_PTP_EP4_SIZE, > + (usb_complete_t)ax88279_read_ts_complete, > + dev); > + > + ret = usb_submit_urb(urb, GFP_KERNEL); > + if (ret == -ENODEV) { > + netdev_err(dev->net, "Error submitting PTP bulk message: status=%d", ret); > + netif_device_detach(dev->net); > + } > + > + urb->actual_length = 0; > + > + return ret; > +} [Severity: High] ax88279_read_ts_complete() runs in atomic URB completion context and calls ptp->ptp_read_ts(dev) -> ax88279_ptp_read_ts(), which calls usb_submit_urb(urb, GFP_KERNEL). Doesn't that sleep in atomic context? In addition, when urb->status < 0 in ax88279_read_ts_complete(), jumping to "out:" resubmits the URB even on -ENOENT, -ECONNRESET or -ESHUTDOWN (such as when usb_kill_urb() is called while carrier is up). ax88279_ptp_read_ts() also writes urb->actual_length = 0 after usb_submit_urb() has already submitted the URB, and DMA-maps ptp->ep4_buf, which is embedded in struct ax88179a_ptp without cacheline alignment. > + > +static int ax88179a_ptp_read_ts(struct usbnet *dev) > +{ > + struct ax88179_data *ax179_data = dev->driver_priv; > + struct ax88179a_ptp *ptp = ax179_data->ptp; > + struct ptp_info_179a *ptp_info; > + int count = 0; > + int ret; > + > + ret = ax88179_read_cmd(dev, AX_PTP_TIMESTAMP_REQ, 0, 0, AX88279_PTP_EP4_SIZE, ptp->ep4_buf); > + if (ret < 0) { > + netdev_err(dev->net, "Error submitting PTP control message: status=%d", ret); > + netif_device_detach(dev->net); > + return ret; > + } [Severity: Medium] Should ax88179a_ptp_read_ts() request sizeof(struct ptp_info_179a) * AX88179A_PTP_QUEUE_SIZE (60 bytes) instead of AX88279_PTP_EP4_SIZE (131 bytes)? ax88179a_ptp_config() programs AX88179A_PTP_MEM_SEG_SIZE to sizeof(struct ptp_info_179a) * AX88179A_PTP_QUEUE_SIZE. Also, ptp->ep4_buf is not zeroed before the read and short reads are not checked, so a short transfer can leave stale status bits in ptp->ep4_buf, and calling netif_device_detach() on any negative return value detaches the netdev even on transient control transfer errors (unlike ax88279_ptp_read_ts(), which checks for -ENODEV). [ ... ] > +static int ax88179a_ptp_config(struct usbnet *dev, int speed) > +{ [ ... ] > + switch (speed) { > + case ETHER_LINK_10: > + ptp_tx_delay[0] = 0xB7; > + ptp_tx_delay[1] = 0x0F; > + ptp_tx_delay[4] = ptpset; > + ptp_rx_delay[0] = 0xAC; > + ptp_rx_delay[1] = 0x0A; > + ptp_rx_delay[4] = ptpset; > + break; > + case ETHER_LINK_100: > + ptp_tx_delay[0] = 0x64; > + ptp_tx_delay[4] = ptpset; > + ptp_rx_delay[0] = 0x22; > + ptp_rx_delay[1] = 0x01; > + ptp_rx_delay[4] = ptpset; > + break; > + case ETHER_LINK_1000: > + default: > + ptp_tx_delay[0] = 0x6E; > + ptp_tx_delay[4] = ptpset; > + ptp_rx_delay[0] = 0xE4; > + ptp_rx_delay[4] = ptpset; > + break; > + } [ ... ] > + ptp_data->tx_tstamp = true; > + ptp_data->rx_tstamp = true; > + > + return 0; > +} > + > +static int ax88279_ptp_config(struct usbnet *dev, int speed) > +{ [ ... ] > + ret = ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MAC_BFM_CTRL, 1, 1, ®8); > + if (ret < 0) > + goto out; > + reg8 |= AX_CS_TRAIL_UDPV4_EN | AX_CS_TRAIL_UDPV6_EN; > + ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MAC_BFM_CTRL, 1, 1, ®8); > + > + switch (speed) { > + case SPEED_100: [ ... ] > +out: > + return ret; > +} [Severity: Medium] Why does ax88179a_ptp_config() switch on ETHER_LINK_10 / ETHER_LINK_100 / ETHER_LINK_1000 (1, 2, 3) while ax88279_ptp_config() switches on SPEED_100 / SPEED_1000 / SPEED_2500 for the same ptp_config() callback? When phylink passes SPEED_10 or SPEED_100 to ptp_config(), ax88179a_ptp_config() always falls through to the 1000 Mbps default case. Also, unlike ax88179a_ptp_config(), ax88279_ptp_config() never sets ptp_data->tx_tstamp and ptp_data->rx_tstamp to true and returns ret (1 on success from ax88179_read_cmd()) instead of 0. [ ... ] > +int ax88179a_ptp_register(struct usbnet *dev) > +{ > + struct ax88179a_ptp *ax88179a_ptp; > + struct ax88179_data *ax179_data; > + u8 ptp_ver; > + int ret = 0; > + > + ax179_data = dev->driver_priv; > + ax88179a_ptp = kzalloc_obj(*ax88179a_ptp); > + if (!ax88179a_ptp) > + return -ENOMEM; > + > + ax179_data->ptp = ax88179a_ptp; > + ax88179a_ptp->dev = dev; > + ax88179a_ptp->ax179_data = ax179_data; > + > + ax88179a_ptp->ptp_info_buf_head = 0; > + ax88179a_ptp->ptp_info_buf_tail = 0; > + INIT_WORK(&ax88179a_ptp->process_ptp_info_buf, ax88279_process_tx_ts); > + INIT_WORK(&ax88179a_ptp->req_tx_timestamp, ax88179a_ptp_read_ts_work); > + skb_queue_head_init(&ax88179a_ptp->tx_skbs); > + > + switch (ax179_data->chip_version) { > + case AX_VERSION_AX88279: > + ax88179a_ptp->urb = usb_alloc_urb(0, GFP_KERNEL); > + if (!ax88179a_ptp->urb) > + goto err; > + ax88179a_ptp->pps_ctrl = ax88279_ptp_pps_ctrl; > + ax88179a_ptp->ptp_config = ax88279_ptp_config; > + ax88179a_ptp->ptp_read_ts = ax88279_ptp_read_ts; > + netdev_info(dev->net, "PTP enabled on AX88279\n"); > + break; > + > + case AX_VERSION_AX88179A: > + ax88179a_ptp->urb = usb_alloc_urb(0, GFP_KERNEL); > + if (!ax88179a_ptp->urb) > + goto err; > + ax88179a_ptp->ctrlreq = kzalloc_obj(*ax179_data->ptp->ctrlreq); > + if (!ax88179a_ptp->ctrlreq) > + goto no_ctrlreq; > + if (ax88179_read_cmd(dev, AX88179A_ACCESS_BL, > + AX88179A_HW_EC_VERSION, 1, 1, &ptp_ver) < 0) > + return 0; > + > + if (ptp_ver < 3) { > + netdev_warn(dev->net, "PTP on AX88179A version %d < 3 unsupported\n", > + ptp_ver); > + return 0; > + } > + netdev_info(dev->net, "PTP on AX88179A version %d enabled\n", ptp_ver); > + > + ax88179a_ptp->pps_ctrl = ax88179a_ptp_pps_ctrl; > + ax88179a_ptp->ptp_config = ax88179a_ptp_config; > + ax88179a_ptp->ptp_read_ts = ax88179a_ptp_read_ts; > + > + // PTP excludes TSO and SG on AX88179A > + dev->net->features &= ~(NETIF_F_SG | NETIF_F_TSO); > + dev->net->hw_features &= ~(NETIF_F_SG | NETIF_F_TSO); > + dev->net->vlan_features &= ~(NETIF_F_SG | NETIF_F_TSO); > + break; > + > + default: > + netdev_warn(dev->net, "PTP not supported\n"); > + ret = -EOPNOTSUPP; > + goto no_ctrlreq; > + }; > + > + ax88179a_ptp->ptp_ci = ax88179a_ptp_clock; > + ax88179a_ptp->ptp_clock = ptp_clock_register(&ax88179a_ptp->ptp_ci, &dev->udev->dev); > + > + if (IS_ERR(ax88179a_ptp->ptp_clock)) { > + dev_err(&dev->udev->dev, "ptp_clock_register failed\n"); > + ret = PTR_ERR(ax88179a_ptp->ptp_clock); > + goto err; > + } > + > + ax88179a_ptp->phc_index = ptp_clock_index(ax88179a_ptp->ptp_clock); > + > + ax88179a_ptp->ax179_data = ax179_data; > + > + return 0; > + > +no_ctrlreq: > + kfree(ax88179a_ptp->urb); > +err: > + kfree(ax88179a_ptp); > + return ret; > +} [Severity: High] What happens on the early returns and error paths in ax88179a_ptp_register()? If reading AX88179A_HW_EC_VERSION fails or ptp_ver < 3, returning 0 leaks urb, ctrlreq and ax88179a_ptp while leaving ax179_data->ptp non-NULL with NULL pps_ctrl, ptp_config and ptp_read_ts callbacks, causing a NULL function pointer dereference when callers check ax179_data->ptp and invoke those callbacks (for example in ax88179a_ptp_unregister()). On the goto error paths: - ax179_data->ptp is never reset to NULL when ax88179a_ptp is freed, leaving a dangling pointer; - ret is still 0 when usb_alloc_urb() or kzalloc_obj() fails; - no_ctrlreq frees ax88179a_ptp->urb with kfree() instead of usb_free_urb(); - ptp_clock_register() failure jumps to err, leaking urb and ctrlreq. > + > +void ax88179a_ptp_unregister(struct usbnet *dev) > +{ > + struct ax88179a_ptp *ax88179a_ptp; > + struct ax88179_data *ax179_data; > + > + ax179_data = dev->driver_priv; > + ax88179a_ptp = ax179_data->ptp; > + > + if (!ax179_data->ptp) > + return; > + > + ax88179a_ptp->pps_ctrl(dev, false); > + cancel_work_sync(&ax88179a_ptp->process_ptp_info_buf); > + > + if (ax179_data->chip_version == AX_VERSION_AX88279) { > + ax88279_ptp_pbus_write_u32(dev, AX88279_PTP_LCK_CTRL0, 0); > + ax88279_ptp_pbus_write_u32(dev, AX88279_PTP_RX_CTRL0, 0); > + ax88279_ptp_pbus_write_u32(dev, AX88279_PTP_TX_CTRL0, 0); > + usb_free_urb(ax88179a_ptp->urb); > + } else { > + u8 reg8 = 0; > + > + ax88179_read_cmd(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, 1, ®8); > + reg8 &= ~(AX_PTP_CTRL_L3_EN | AX_PTP_CTRL_EN); > + ax88179_write_cmd(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, 1, ®8); > + kfree(ax88179a_ptp->ctrlreq); > + } > + skb_queue_purge(&ax88179a_ptp->tx_skbs); > + > + if (ax88179a_ptp && ax88179a_ptp->ptp_clock) > + ptp_clock_unregister(ax88179a_ptp->ptp_clock); > + > + kfree(ax179_data->ptp); > +} [Severity: High] Does ax88179a_ptp_unregister() leave req_tx_timestamp work and the URB active, and leak ax88179a_ptp->urb on AX88179A? req_tx_timestamp is never cancelled with cancel_work_sync(), and the URB is not killed before cancelling process_ptp_info_buf, so an in-flight req_tx_timestamp work item or URB completion can reschedule process_ptp_info_buf and access ax88179a_ptp after it is freed. Also, ax88179a_ptp->urb is allocated for AX_VERSION_AX88179A in ax88179a_ptp_register() but only freed in the AX_VERSION_AX88279 branch here, and ax179_data->ptp is not cleared to NULL after kfree(). > diff --git a/drivers/net/usb/ax88179a_ptp.h b/drivers/net/usb/ax88179a_ptp.h > new file mode 100644 > index 0000000000000..534e73cdc951e > --- /dev/null > +++ b/drivers/net/usb/ax88179a_ptp.h > @@ -0,0 +1,249 @@ [ ... ] > +#define AX_PTP_CLK_EN 0x010000 > +#define AX_PTP_CLK_SELECT_DIVIDE 0x040000 > + > +#define AX88179A_PBUS_REG 0x10 > +#define AX_PTP_CLK 0x13 > + > +#define AX88279_PTP_LCK_CTRL0 0x1000 > +#define AX_PTP_LCK_CTRL0_EN 0x0001 > +#define AX_PTP_LCK_CTRL0_80B_NS_EN 0x0002 > +#define AX_PTP_LCK_CTRL0_80B_S_E 0x0004 > +#define AX_PTP_LCK_CTRL0_48B_EN 0x0008 > +#define AX_PTP_LCK_CTRL0_PPS_EN 0x0010 > +#define AX_PTP_LCK_CTRL0_TX_DEL_VEC 0x0040 > + > +#define AX88279_PTP_RX_CTRL0 0x2000 > +#define AX88279_PTP_TX_CTRL0 0x3000 > + > +#define AX88179A_PTP_CTRL_1 0x00 > +#define AX_PTP_CTRL_EN 0x01 > +#define AX_PTP_CTRL_L3_EN 0x02 > +#define AX_PTP_TX_FILTER_GENERAL_MSG 0x08 > +#define AX_PTP_RX_FILTER_GENERAL_MSG 0x10 > +#define AX_PTP_TX_PLUS_DELAY 0x20 > +#define AX88179A_PTP_MEM_SEG_SIZE 0x21 > + > +#define AX88179A_PTP_CTRL_2 0x01 > +#define AX_PTP_CTRL_SET_PERIOD 0x08 > + > +#define AX88179A_PTP_MEM_SEG_SIZE 0x21 [Severity: Low] This isn't a bug, but several macros in ax88179a_ptp.h are defined twice: AX88179A_PTP_MEM_SEG_SIZE here, AX_PTP_CLK_EN, and the AX_PTP_LCK_CTRL0_* group (including the unused typo AX_PTP_LCK_CTRL0_80B_S_E alongside AX_PTP_LCK_CTRL0_80B_S_EN further down). -- Sashiko AI review · https://sashiko.dev/#/patchset/20261129-ptp-v1-0-8148c402beaf@birger-koblitz.de?part=2 ^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH net-next 3/3] net: usb: ax88179: Enable PTP 2026-11-29 9:29 [PATCH net-next 0/3] net: usb: ax88179: Add support for PTP Birger Koblitz 2026-11-29 9:29 ` [PATCH net-next 1/3] net: usb: ax88179: Add PTP data structures and definitions Birger Koblitz 2026-11-29 9:29 ` [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation Birger Koblitz @ 2026-11-29 9:29 ` Birger Koblitz 2026-11-29 15:48 ` Andrew Lunn ` (2 more replies) 2 siblings, 3 replies; 13+ messages in thread From: Birger Koblitz @ 2026-11-29 9:29 UTC (permalink / raw) To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran, Russell King Cc: netdev, linux-usb, linux-kernel, Birger Koblitz Adds a module parameter ptp_enable which allows to enable PTP support in the module. The default is not to enable PTP as PTP is not compatible with segmentation offload on the AX88179A. Add PTP configuration when the module binds and when the link comes up. Add ax88179a_get_ts_info() to show PTP support in ethtool. Add hooks into the TX and RX path to handle timestamps. Signed-off-by: Birger Koblitz <mail@birger-koblitz.de> --- drivers/net/usb/ax88179a_devices.c | 149 +++++++++++++++++++++++++++++++++++-- 1 file changed, 141 insertions(+), 8 deletions(-) diff --git a/drivers/net/usb/ax88179a_devices.c b/drivers/net/usb/ax88179a_devices.c index 69095110e70435806a99274c50d79e86016ec9a0..994e749ffaa2b5e2d7c1e1ec362bd55b9c882a86 100644 --- a/drivers/net/usb/ax88179a_devices.c +++ b/drivers/net/usb/ax88179a_devices.c @@ -4,16 +4,21 @@ #include <linux/phylink.h> #include <linux/if_vlan.h> #include "ax88179_lib.h" +#include "ax88179a_ptp.h" static int ax88179a_reset(struct usbnet *dev); #define AX88279_EEPROM_LEN 0x4000 #define AX88179A_EEPROM_LEN (32 * 20) +static bool ptp_enable; +module_param(ptp_enable, bool, 0); +MODULE_PARM_DESC(ptp_enable, "Enable PTP hardware timestamping"); + enum ax_bulk_in_speeds { BULK_IN_SPEED_2G5 = 0, BULK_IN_SPEED_1G_SS = 1, - BULK_IN_SPEED_1G_HS = 2, + BULK_IN_SPEED_1G_HS = 2, BULK_IN_SPEED_100_FULL_SS = 3, BULK_IN_SPEED_100_HALF_SS = 4, BULK_IN_SPEED_100_FULL_HS = 5, @@ -137,6 +142,9 @@ static int ax88179a_suspend(struct usb_interface *intf, pm_message_t message) int ret; priv = dev->driver_priv; + if (netif_running(dev->net)) + ax88179a_ptp_kill_urbs(dev); + ax88179_set_pm_mode(dev, true); if (netif_running(dev->net)) { @@ -182,6 +190,7 @@ static int ax88179a_suspend(struct usb_interface *intf, pm_message_t message) } ax88179_set_pm_mode(dev, false); + return 0; } @@ -213,7 +222,8 @@ static int ax88179a_resume(struct usb_interface *intf) ax88179_write_cmd(dev, AX88179A_PHY_POWER, 0, 0, 1, ®8); msleep(250); } - ax88179_write_cmd(dev, AX_FW_MODE, AX_FW_MODE_179A, 0, 0, NULL); + ax88179_write_cmd(dev, AX_FW_MODE, AX_FW_MODE_179A, + ax179_data->ptp ? AX_USB_EP4_EN : 0, 0, NULL); /* Now, that AX_FW_MODE_179A is enabled, the PHY needs a power-cycle. * PHY-power is re-enabled in ax88179a_reset() @@ -232,6 +242,11 @@ static int ax88179a_resume(struct usb_interface *intf) ax88179_set_pm_mode(dev, false); + if (netif_running(dev->net) && + ax179_data->chip_version == AX_VERSION_AX88279 && ax179_data->ptp) { + ax179_data->ptp->ptp_read_ts(dev); + } + return usbnet_resume(intf); } @@ -353,6 +368,42 @@ static int ax88179a_set_eee(struct net_device *net, struct ethtool_keee *edata) return phylink_ethtool_set_eee(ax179_data->phylink, edata); } +static int ax88179a_get_ts_info(struct net_device *net, + struct kernel_ethtool_ts_info *info) +{ + struct ax88179_data *ax179_data = netdev2data(net); + + if (!ax179_data->ptp) + return ethtool_op_get_ts_info(net, info); + + info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | + SOF_TIMESTAMPING_RX_SOFTWARE | + SOF_TIMESTAMPING_SOFTWARE | + SOF_TIMESTAMPING_TX_HARDWARE | + SOF_TIMESTAMPING_RX_HARDWARE | + SOF_TIMESTAMPING_RAW_HARDWARE; + + info->phc_index = ax179_data->ptp->phc_index; + + info->tx_types = BIT(HWTSTAMP_TX_OFF) | + BIT(HWTSTAMP_TX_ON) | + BIT(HWTSTAMP_TX_ONESTEP_SYNC) | + BIT(HWTSTAMP_TX_ONESTEP_P2P); + + info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | + BIT(HWTSTAMP_FILTER_PTP_V1_L4_EVENT) | + BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) | + BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) | + BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | + BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) | + BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) | + BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | + BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) | + BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ); + + return 0; +} + static const struct ethtool_ops ax88179a_ethtool_ops = { .get_drvinfo = ax88179a_get_drvinfo, .get_link = ethtool_op_get_link, @@ -370,7 +421,7 @@ static const struct ethtool_ops ax88179a_ethtool_ops = { .set_link_ksettings = ax88179a_set_link_ksettings, .get_pauseparam = ax88179a_get_pauseparam, .set_pauseparam = ax88179a_set_pauseparam, - .get_ts_info = ethtool_op_get_ts_info, + .get_ts_info = ax88179a_get_ts_info, }; static int ax88179a_vlan_rx_kill_vid(struct net_device *net, __be16 proto, u16 vid) @@ -496,7 +547,19 @@ static void ax88179a_mac_config(struct phylink_config *config, unsigned int mode static void ax88179a_mac_link_down(struct phylink_config *config, unsigned int mode, phy_interface_t interface) { - /* Nothing to do */ + struct usbnet *dev = netdev_priv(to_net_dev(config->dev)); + struct ax88179_data *ax179_data = dev->driver_priv; + + if (ax179_data->ptp) { + if (ax179_data->chip_version == AX_VERSION_AX88279) { + if (ax179_data->ptp->urb) + usb_kill_urb(ax179_data->ptp->urb); + } + if (ax179_data->ptp && ax179_data->chip_version == AX_VERSION_AX88179A) + cancel_work_sync(&ax179_data->ptp->req_tx_timestamp); + + ax179_data->ptp->pps_ctrl(dev, false); + } } static void ax88179a_mac_link_up(struct phylink_config *config, @@ -511,6 +574,9 @@ static void ax88179a_mac_link_up(struct phylink_config *config, u8 bulk_config_speed = 0; u16 mode; + if (ax179_data->ptp) + ax179_data->ptp->pps_ctrl(dev, true); + /* Stop RX/TX for link configuration */ ax88179_write_u16(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, AX_RX_CTL_STOP); ax88179_write_u8(dev, AX_ACCESS_MAC, AX88179A_MAC_PATH, 1, 0); @@ -606,6 +672,11 @@ static void ax88179a_mac_link_up(struct phylink_config *config, ax88179_write_u8(dev, AX_ACCESS_MAC, AX88179A_MAC_PATH, 1, AX_MAC_RX_PATH_READY | AX_MAC_TX_PATH_READY); + + if (ax179_data->ptp) { + ax179_data->ptp->ptp_config(dev, speed); + ax179_data->ptp->ptp_read_ts(dev); + } } static void ax88179a_mac_disable_tx_lpi(struct phylink_config *config) @@ -797,6 +868,8 @@ static const struct net_device_ops ax88179a_netdev_ops = { .ndo_set_features = ax88179a_set_features, .ndo_vlan_rx_add_vid = ax88179a_vlan_rx_add_vid, .ndo_vlan_rx_kill_vid = ax88179a_vlan_rx_kill_vid, + .ndo_hwtstamp_get = ax88179a_hwtstamp_get, + .ndo_hwtstamp_set = ax88179a_hwtstamp_set, }; static int ax88179a_bind(struct usbnet *dev, struct usb_interface *intf) @@ -896,8 +969,11 @@ static int ax88179a_bind(struct usbnet *dev, struct usb_interface *intf) netif_set_tso_max_size(dev->net, 16384); - /* Enable Transmission of Link Speed byte in interrupt URB */ - ax88179_write_cmd(dev, AX_FW_MODE, AX_FW_MODE_179A, 0, 0, NULL); + /* Enable Transmission of Link Speed byte in interrupt URB + * and optionally PTP functionality + */ + ax88179_write_cmd(dev, AX_FW_MODE, AX_FW_MODE_179A, + ptp_enable ? AX_USB_EP4_EN : 0, 0, NULL); ax88179_write_cmd(dev, AX_RELOAD_EEPROM_EFUSE, 0, 0, 0, NULL); /* Read MAC address from DTB or ASIX chip */ @@ -927,6 +1003,30 @@ static int ax88179a_bind(struct usbnet *dev, struct usb_interface *intf) */ pm_runtime_get_noresume(&intf->dev); + if (!ptp_enable) + return 0; + + ret = ax88179a_ptp_register(dev); + if (ret < 0) { + netdev_warn(dev->net, "Failed to register PHC device. (%d)\n", ret); + netdev_warn(dev->net, "PTP disabled\n"); + /* Disable PTP functionality again and cycle PHY to take effect */ + ax88179_write_cmd(dev, AX_FW_MODE, AX_FW_MODE_179A, 0, 0, NULL); + ax88179_write_u8(dev, AX88179A_PHY_POWER, 0, 0, 0); + msleep(50); + return 0; + } + + if (ax179_data->ptp && ax179_data->chip_version == AX_VERSION_AX88179A) { + ax179_data->ptp->ptp_config(dev, SPEED_UNKNOWN); + + /* Power cycle PHY to make PTP settings take effect, + * reset() will power on the PHY again + */ + ax88179_write_u8(dev, AX88179A_PHY_POWER, 0, 0, 0); + msleep(50); + } + return 0; initphy_err: @@ -946,6 +1046,8 @@ static void ax88179a_unbind(struct usbnet *dev, struct usb_interface *intf) /* Configure RX control register => stop operation */ ax88179_write_u16(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, AX_RX_CTL_STOP); + ax88179a_ptp_kill_urbs(dev); + ax88179a_ptp_unregister(dev); rtnl_lock(); phylink_disconnect_phy(ax179_data->phylink); @@ -1017,8 +1119,10 @@ static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) hdr_off = FIELD_GET(AX179A_RX_DH_DESC_OFFSET, rx_hdr); pkt_cnt = rx_hdr & AX179A_RX_DH_PKT_CNT_MASK; - /* Consistency check header position */ - if (hdr_off != skb->len - (pkt_cnt * sizeof(rx_hdr))) + /* Consistency check header position, frames with PTP timestamps + * have longer headers than normal frames, so check for minimum size + */ + if (hdr_off > skb->len - (pkt_cnt * sizeof(rx_hdr))) goto err; /* Make sure that the bounds of the metadata array are inside the SKB @@ -1075,6 +1179,13 @@ static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) __vlan_hwaccel_put_tag(ax_skb, htons(ETH_P_8021Q), vlan_tag); } + if (pkt_desc & AX179A_RX_PD_PTP) { + u64 ts_l = get_unaligned_le64(++pkt_desc_ptr); + u64 ts_h = get_unaligned_le64(++pkt_desc_ptr); + + ax88179a_process_rx_ts(ax_skb, ts_l, ts_h); + } + usbnet_skb_return(dev, ax_skb); skb_pull(skb, pkt_len_plus_padd); @@ -1090,6 +1201,7 @@ static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) static struct sk_buff *ax88179a_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) { + struct ax88179_data *ax179_data = dev->driver_priv; u64 tx_desc = skb->len & AX179A_TX_DESC_LEN_MASK; int frame_size = dev->maxpacket; struct sk_buff *ax_skb; @@ -1132,6 +1244,24 @@ static struct sk_buff *ax88179a_tx_fixup(struct usbnet *dev, struct sk_buff *skb } if (padding_size != 0) skb_put_zero(skb, padding_size); + + if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && ax179_data->ptp) { + struct sk_buff *ptp_skb; + + /* Keep the original SKB owned by the socket in our + * queue with PTP SKBs, while continuing TX with a + * clone, so that skb_tstamp_tx() can later return + * the TX timestamp via the socket's error channel. + */ + skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; + ptp_skb = skb_clone(skb, GFP_ATOMIC); + skb_queue_tail(&ax179_data->ptp->tx_skbs, skb); + skb = ptp_skb; + // Get the TX timestamp + if (ax179_data->chip_version == AX_VERSION_AX88179A) + schedule_work(&ax179_data->ptp->req_tx_timestamp); + } + /* Copy TX header */ tx_desc_ptr = skb_push(skb, sizeof(tx_desc)); put_unaligned_le64(tx_desc, tx_desc_ptr); @@ -1261,6 +1391,9 @@ static int ax88179a_stop(struct usbnet *dev) struct ax88179_data *ax179_data = dev->driver_priv; u16 reg16 = 0; + if (ax179_data->ptp && ax179_data->chip_version == AX_VERSION_AX88179A) + cancel_work_sync(&ax179_data->ptp->req_tx_timestamp); + phylink_stop(ax179_data->phylink); ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE, 2, 2, ®16); -- 2.47.3 ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH net-next 3/3] net: usb: ax88179: Enable PTP 2026-11-29 9:29 ` [PATCH net-next 3/3] net: usb: ax88179: Enable PTP Birger Koblitz @ 2026-11-29 15:48 ` Andrew Lunn 2026-11-29 17:24 ` Birger Koblitz 2026-11-29 21:18 ` kernel test robot 2026-11-30 9:30 ` sashiko-bot 2 siblings, 1 reply; 13+ messages in thread From: Andrew Lunn @ 2026-11-29 15:48 UTC (permalink / raw) To: Birger Koblitz Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran, Russell King, netdev, linux-usb, linux-kernel On Sun, Nov 29, 2026 at 10:29:52AM +0100, Birger Koblitz wrote: > Adds a module parameter ptp_enable which allows to enable PTP > support in the module. The default is not to enable PTP as PTP > is not compatible with segmentation offload on the AX88179A. > > Add PTP configuration when the module binds and when the link > comes up. Add ax88179a_get_ts_info() to show PTP support > in ethtool. > > Add hooks into the TX and RX path to handle timestamps. > > Signed-off-by: Birger Koblitz <mail@birger-koblitz.de> > --- > drivers/net/usb/ax88179a_devices.c | 149 +++++++++++++++++++++++++++++++++++-- > 1 file changed, 141 insertions(+), 8 deletions(-) > > diff --git a/drivers/net/usb/ax88179a_devices.c b/drivers/net/usb/ax88179a_devices.c > index 69095110e70435806a99274c50d79e86016ec9a0..994e749ffaa2b5e2d7c1e1ec362bd55b9c882a86 100644 > --- a/drivers/net/usb/ax88179a_devices.c > +++ b/drivers/net/usb/ax88179a_devices.c > @@ -4,16 +4,21 @@ > #include <linux/phylink.h> > #include <linux/if_vlan.h> > #include "ax88179_lib.h" > +#include "ax88179a_ptp.h" > > static int ax88179a_reset(struct usbnet *dev); > > #define AX88279_EEPROM_LEN 0x4000 > #define AX88179A_EEPROM_LEN (32 * 20) > > +static bool ptp_enable; > +module_param(ptp_enable, bool, 0); > +MODULE_PARM_DESC(ptp_enable, "Enable PTP hardware timestamping"); > + Module parameters are not liked. Is this needed? Why not always enable it? > @@ -182,6 +190,7 @@ static int ax88179a_suspend(struct usb_interface *intf, pm_message_t message) > } > > ax88179_set_pm_mode(dev, false); > + > return 0; Stray white space change. > } > Andrew ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH net-next 3/3] net: usb: ax88179: Enable PTP 2026-11-29 15:48 ` Andrew Lunn @ 2026-11-29 17:24 ` Birger Koblitz 2026-11-29 21:09 ` Andrew Lunn 0 siblings, 1 reply; 13+ messages in thread From: Birger Koblitz @ 2026-11-29 17:24 UTC (permalink / raw) To: Andrew Lunn Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran, Russell King, netdev, linux-usb, linux-kernel Thanks Andrew for reviewing this! On 29/11/2026 4:48 pm, Andrew Lunn wrote: >> >> +static bool ptp_enable; >> +module_param(ptp_enable, bool, 0); >> +MODULE_PARM_DESC(ptp_enable, "Enable PTP hardware timestamping"); >> + > > Module parameters are not liked. Is this needed? Why not always enable > it? I was not aware of that. I will enable PTP by default. That it disables NETIF_F_SG and NETIF_F_TSO is probably a small price for users of the AX88179A for the extra PTP functionality, and on the AX88279 no impact on the rest of the features whatsoever. PTP also does not cost any resources when not in use. > >> @@ -182,6 +190,7 @@ static int ax88179a_suspend(struct usb_interface *intf, pm_message_t message) >> } >> >> ax88179_set_pm_mode(dev, false); >> + >> return 0; > > Stray white space change. Oops, will fix. Birger ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH net-next 3/3] net: usb: ax88179: Enable PTP 2026-11-29 17:24 ` Birger Koblitz @ 2026-11-29 21:09 ` Andrew Lunn 0 siblings, 0 replies; 13+ messages in thread From: Andrew Lunn @ 2026-11-29 21:09 UTC (permalink / raw) To: Birger Koblitz Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran, Russell King, netdev, linux-usb, linux-kernel On Sun, Nov 29, 2026 at 06:24:39PM +0100, Birger Koblitz wrote: > Thanks Andrew for reviewing this! > > On 29/11/2026 4:48 pm, Andrew Lunn wrote: > > > +static bool ptp_enable; > > > +module_param(ptp_enable, bool, 0); > > > +MODULE_PARM_DESC(ptp_enable, "Enable PTP hardware timestamping"); > > > + > > > > Module parameters are not liked. Is this needed? Why not always enable > > it? > I was not aware of that. It might not be written down anywhere, one of those things which gets passed down from generation to generation via the mailing list. module parameters are hard to use, especially when it is builtin, or hot pluggable. It is messy to specify which of the 42 different instances of the device you want the module parameter to apply to, etc. We much prefer ethtool, or similar. Andrew ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH net-next 3/3] net: usb: ax88179: Enable PTP 2026-11-29 9:29 ` [PATCH net-next 3/3] net: usb: ax88179: Enable PTP Birger Koblitz 2026-11-29 15:48 ` Andrew Lunn @ 2026-11-29 21:18 ` kernel test robot 2026-11-30 9:30 ` sashiko-bot 2 siblings, 0 replies; 13+ messages in thread From: kernel test robot @ 2026-11-29 21:18 UTC (permalink / raw) To: Birger Koblitz, Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran, Russell King Cc: llvm, oe-kbuild-all, netdev, linux-usb, linux-kernel, Birger Koblitz Hi Birger, kernel test robot noticed the following build warnings: [auto build test WARNING on eb6d6c80d35449c27471b1874f13066c09a8964d] url: https://github.com/intel-lab-lkp/linux/commits/Birger-Koblitz/net-usb-ax88179-Add-PTP-data-structures-and-definitions/20261129-102950 base: eb6d6c80d35449c27471b1874f13066c09a8964d patch link: https://lore.kernel.org/r/20261129-ptp-v1-3-8148c402beaf%40birger-koblitz.de patch subject: [PATCH net-next 3/3] net: usb: ax88179: Enable PTP config: hexagon-allmodconfig (https://download.01.org/0day-ci/archive/20261130/20261130050726.LLJ9oe3l-lkp@intel.com/config) compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 04597ff5c4eda9d66da39a536aa26a7bab2fb2dd) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261130/20261130050726.LLJ9oe3l-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/20261130050726.LLJ9oe3l-lkp@intel.com/ All warnings (new ones prefixed by >>): >> drivers/net/usb/ax88179a_devices.c:1182:18: warning: shift count >= width of type [-Wshift-count-overflow] 1182 | if (pkt_desc & AX179A_RX_PD_PTP) { | ^~~~~~~~~~~~~~~~ drivers/net/usb/ax88179_lib.h:272:27: note: expanded from macro 'AX179A_RX_PD_PTP' 272 | #define AX179A_RX_PD_PTP BIT(56) | ^~~~~~~ include/vdso/bits.h:7:26: note: expanded from macro 'BIT' 7 | #define BIT(nr) (UL(1) << (nr)) | ^ ~~~~ 1 warning generated. vim +1182 drivers/net/usb/ax88179a_devices.c 1078 1079 static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 1080 { 1081 struct ax88179_data *ax179_data = dev->driver_priv; 1082 struct sk_buff *ax_skb; 1083 u32 hdr_off, pkt_end; 1084 __le64 *pkt_desc_ptr; 1085 u16 vlan_tag; 1086 u16 pkt_cnt; 1087 u64 rx_hdr; 1088 1089 /* SKB contents for AX179A-based chips: 1090 * <packet 1> 1091 * ... 1092 * <packet N> 1093 * <per-packet metadata entry 1> 1094 * ... 1095 * <per-packet metadata entry N> 1096 * <rx_hdr> 1097 * 1098 * where: 1099 * <packet N> contains pkt_len data bytes and padding: 1100 * 2 bytes of IP alignment (optional, depends on AX_RX_CTL_IPE flag) 1101 * packet data received 1102 * optional padding to 8-bytes boundary 1103 * <per-packet metadata entry N> contains 8 bytes: 1104 * pkt_len and fields AX_RXHDR_* 1105 * <rx-hdr> contains 8 bytes: 1106 * pkt_cnt and hdr_off (offset of <per-packet metadata entry 1>) 1107 * 1108 * pkt_cnt is number of entries in the per-packet metadata array. 1109 */ 1110 1111 if (!skb || skb->len < sizeof(rx_hdr)) 1112 goto err; 1113 1114 /* RX Descriptor Header */ 1115 skb_trim(skb, skb->len - sizeof(rx_hdr)); 1116 rx_hdr = get_unaligned_le64((__le64 *)skb_tail_pointer(skb)); 1117 1118 /* Check these packets */ 1119 hdr_off = FIELD_GET(AX179A_RX_DH_DESC_OFFSET, rx_hdr); 1120 pkt_cnt = rx_hdr & AX179A_RX_DH_PKT_CNT_MASK; 1121 1122 /* Consistency check header position, frames with PTP timestamps 1123 * have longer headers than normal frames, so check for minimum size 1124 */ 1125 if (hdr_off > skb->len - (pkt_cnt * sizeof(rx_hdr))) 1126 goto err; 1127 1128 /* Make sure that the bounds of the metadata array are inside the SKB 1129 * (and in front of the counter at the end). 1130 */ 1131 if (pkt_cnt * 8 + hdr_off > skb->len) 1132 goto err; 1133 1134 /* Packets must not overlap the metadata array */ 1135 skb_trim(skb, hdr_off); 1136 1137 if (!pkt_cnt) 1138 goto err; 1139 1140 /* Get the first RX packet descriptor */ 1141 pkt_desc_ptr = (__le64 *)(skb->data + hdr_off); 1142 1143 pkt_end = 0; 1144 while (pkt_cnt--) { 1145 u64 pkt_desc = get_unaligned_le64(pkt_desc_ptr); 1146 u32 pkt_len_plus_padd; 1147 u32 pkt_len; 1148 1149 pkt_len = FIELD_GET(AX179A_RX_PD_LEN, pkt_desc); 1150 pkt_len -= (ax179_data->ip_align ? 2 : 0); 1151 pkt_len_plus_padd = ((pkt_len + 7 + (ax179_data->ip_align ? 2 : 0)) & 0x7FFF8); 1152 1153 pkt_end += pkt_len_plus_padd; 1154 if (pkt_end > hdr_off || (pkt_cnt == 0 && pkt_end != hdr_off)) 1155 goto err; 1156 1157 if (pkt_desc & AX179A_RX_PD_DROP || !(pkt_desc & AX179A_RX_PD_RX_OK) || 1158 pkt_len > (dev->hard_mtu + AX179A_RX_HW_PAD) || pkt_len < 2 + ETH_HLEN) { 1159 skb_pull(skb, pkt_len_plus_padd); 1160 1161 /* Next RX Packet Descriptor */ 1162 pkt_desc_ptr++; 1163 continue; 1164 } 1165 1166 ax_skb = netdev_alloc_skb_ip_align(dev->net, pkt_len); 1167 if (!ax_skb) 1168 goto err; 1169 1170 skb_put(ax_skb, pkt_len); 1171 memcpy(ax_skb->data, skb->data + (ax179_data->ip_align ? AX179A_RX_HW_PAD : 0), 1172 pkt_len); 1173 1174 if (ax179_data->rx_checksum) 1175 ax88179a_rx_checksum(ax_skb, pkt_desc); 1176 1177 if (pkt_desc & AX179A_RX_PD_VLAN) { 1178 vlan_tag = pkt_desc >> AX179A_RX_PD_VLAN_SHIFT; 1179 __vlan_hwaccel_put_tag(ax_skb, htons(ETH_P_8021Q), vlan_tag); 1180 } 1181 > 1182 if (pkt_desc & AX179A_RX_PD_PTP) { 1183 u64 ts_l = get_unaligned_le64(++pkt_desc_ptr); 1184 u64 ts_h = get_unaligned_le64(++pkt_desc_ptr); 1185 1186 ax88179a_process_rx_ts(ax_skb, ts_l, ts_h); 1187 } 1188 1189 usbnet_skb_return(dev, ax_skb); 1190 skb_pull(skb, pkt_len_plus_padd); 1191 1192 /* Next RX Packet Header */ 1193 pkt_desc_ptr++; 1194 } 1195 1196 return 1; 1197 1198 err: 1199 return 0; 1200 } 1201 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH net-next 3/3] net: usb: ax88179: Enable PTP 2026-11-29 9:29 ` [PATCH net-next 3/3] net: usb: ax88179: Enable PTP Birger Koblitz 2026-11-29 15:48 ` Andrew Lunn 2026-11-29 21:18 ` kernel test robot @ 2026-11-30 9:30 ` sashiko-bot 2 siblings, 0 replies; 13+ messages in thread From: sashiko-bot @ 2026-11-30 9:30 UTC (permalink / raw) To: Birger Koblitz; +Cc: linux-usb Thank you for your contribution! Sashiko AI review found 18 potential issues to consider: - [High] net: usb: ax88179: NULL deref on skb_clone() failure in ax88179a_tx_fixup() - [High] net: usb: ax88179: unlocked tx_skbs access and leak in ax88279_process_tx_ts() - [High] net: usb: ax88179: uninitialized ptp_hdr deref on non-PTP TX timestamp SKBs - [High] net: usb: ax88179: out-of-bounds PTP timestamp read in ax88179a_rx_fixup() - [High] net: usb: ax88179: dropped PTP packet desynchronizes ax88179a_rx_fixup() - [High] net: usb: ax88179: 32-bit shift overflow in AX179A_RX_PD_PTP macro - [High] net: usb: ax88179: UAF and NULL ptp_config() deref in ax88179a_bind() - [High] net: usb: ax88179: active PTP URB double submission on resume and link up - [High] net: usb: ax88179: urb leak and UAF race in ax88179a_unbind() - [High] net: usb: ax88179: uncanceled PTP work and phylink race in ax88179a_suspend() - [High] net: usb: ax88179: data races on ep4_buf and ptp_info_buf in ax88179a_ptp_read_ts() - [High] net: usb: ax88179: wrong array index in ax88179a_ptp_read_ts() and ax88279_read_ts_complete() - [High] net: usb: ax88179: misplaced PTP_1588_CLOCK_OPTIONAL dependency in Kconfig - [Medium] net: usb: ax88179: skb_copy_expand() drops TX timestamp in ax88179a_tx_fixup() - [Medium] net: usb: ax88179: wrong speed constants passed in ax88179a_mac_link_up() - [Medium] net: usb: ax88179: capability mismatch in ax88179a_get_ts_info() - [Medium] net: usb: ax88179: resource leaks and dangling ptp in ax88179a_ptp_register() - [Medium] net: usb: ax88179: missing cpu_to_le32 conversion on 32-bit PTP register writes -- commit 43874aad48bf8ec849f6baccb1d0f436a2536594 Author: Birger Koblitz <mail@birger-koblitz.de> net: usb: ax88179: Enable PTP This commit adds a ptp_enable module parameter and hooks up PTP hardware timestamping support across bind, unbind, suspend, resume, link changes, ethtool, and the RX/TX paths for AX88179A and AX88279 devices. > diff --git a/drivers/net/usb/ax88179a_devices.c b/drivers/net/usb/ax88179a_devices.c > index 69095110e7043..994e749ffaa2b 100644 > --- a/drivers/net/usb/ax88179a_devices.c > +++ b/drivers/net/usb/ax88179a_devices.c [ ... ] > @@ -137,6 +142,9 @@ static int ax88179a_suspend(struct usb_interface *intf, pm_message_t message) > int ret; > > priv = dev->driver_priv; > + if (netif_running(dev->net)) > + ax88179a_ptp_kill_urbs(dev); > + > ax88179_set_pm_mode(dev, true); > > if (netif_running(dev->net)) { [Severity: High] Can pending phylink or PTP work run during and after ax88179a_suspend()? ax88179a_ptp_kill_urbs() runs before phylink_suspend() and usbnet_suspend(), so phylink resolve work can call ax88179a_mac_link_up() and resubmit ptp->urb in between. On AX88179A, ax88179a_ptp_kill_urbs() is a no-op, and when priv->wolopts is set phylink_suspend() does not call ax88179a_mac_link_down(). Because ax88179a_suspend() never cancels ptp->req_tx_timestamp or ptp->process_ptp_info_buf, ax88179a_ptp_read_ts_work() can run after ax88179a_suspend() clears in_pm, issuing non-PM USB control reads on the suspended device and calling netif_device_detach() on failure. [ ... ] > @@ -232,6 +242,11 @@ static int ax88179a_resume(struct usb_interface *intf) > > ax88179_set_pm_mode(dev, false); > > + if (netif_running(dev->net) && > + ax179_data->chip_version == AX_VERSION_AX88279 && ax179_data->ptp) { > + ax179_data->ptp->ptp_read_ts(dev); > + } > + > return usbnet_resume(intf); > } [Severity: High] Does calling ptp_read_ts() in both ax88179a_resume() and ax88179a_mac_link_up() submit ptp->urb twice on AX88279? ax88279_ptp_read_ts() reinitializes and submits the single pre-allocated ptp->urb. When ax88179a_resume() calls ptp_read_ts() and phylink also calls ax88179a_mac_link_up() (either synchronously inside phylink_resume() when WoL was active, or when link negotiation completes), ptp->urb is reinitialized and submitted while already active, triggering WARN_ONCE() in usb_submit_urb(). Also, ax88279_read_ts_complete() calls ptp->ptp_read_ts(dev) from URB completion context, which passes GFP_KERNEL to usb_submit_urb() in atomic context. And if ptp->urb is submitted here while the link is down and the interface is then stopped before link-up, ax88179a_stop() never calls ax88179a_ptp_kill_urbs(dev), leaving ptp->urb active after ndo_stop. > @@ -353,6 +368,42 @@ static int ax88179a_set_eee(struct net_device *net, struct ethtool_keee *edata) > return phylink_ethtool_set_eee(ax179_data->phylink, edata); > } > > +static int ax88179a_get_ts_info(struct net_device *net, > + struct kernel_ethtool_ts_info *info) > +{ > + struct ax88179_data *ax179_data = netdev2data(net); > + > + if (!ax179_data->ptp) > + return ethtool_op_get_ts_info(net, info); > + > + info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | > + SOF_TIMESTAMPING_RX_SOFTWARE | > + SOF_TIMESTAMPING_SOFTWARE | > + SOF_TIMESTAMPING_TX_HARDWARE | > + SOF_TIMESTAMPING_RX_HARDWARE | > + SOF_TIMESTAMPING_RAW_HARDWARE; > + > + info->phc_index = ax179_data->ptp->phc_index; > + > + info->tx_types = BIT(HWTSTAMP_TX_OFF) | > + BIT(HWTSTAMP_TX_ON) | > + BIT(HWTSTAMP_TX_ONESTEP_SYNC) | > + BIT(HWTSTAMP_TX_ONESTEP_P2P); > + > + info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | > + BIT(HWTSTAMP_FILTER_PTP_V1_L4_EVENT) | > + BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) | > + BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) | > + BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | > + BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) | > + BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) | > + BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | > + BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) | > + BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ); > + > + return 0; > +} [Severity: Medium] Do info->tx_types and info->rx_filters match what ax88179a_hwtstamp_set() actually supports? ax88179a_hwtstamp_set() only accepts HWTSTAMP_TX_OFF and HWTSTAMP_TX_ON and returns -ERANGE for HWTSTAMP_TX_ONESTEP_SYNC and HWTSTAMP_TX_ONESTEP_P2P. It also forces any non-NONE filter to HWTSTAMP_FILTER_ALL, which is missing from info->rx_filters. [ ... ] > @@ -606,6 +672,11 @@ static void ax88179a_mac_link_up(struct phylink_config *config, > > ax88179_write_u8(dev, AX_ACCESS_MAC, AX88179A_MAC_PATH, 1, > AX_MAC_RX_PATH_READY | AX_MAC_TX_PATH_READY); > + > + if (ax179_data->ptp) { > + ax179_data->ptp->ptp_config(dev, speed); > + ax179_data->ptp->ptp_read_ts(dev); > + } > } [Severity: Medium] Does ax88179a_ptp_config() expect SPEED_* values here instead of ETHER_LINK_*? ax88179a_mac_link_up() passes phylink's speed (SPEED_10, SPEED_100, SPEED_1000), which matches ax88279_ptp_config(), but ax88179a_ptp_config() switches on ETHER_LINK_10 (1), ETHER_LINK_100 (2), and ETHER_LINK_1000 (3): drivers/net/usb/ax88179a_ptp.c:ax88179a_ptp_config() { ... switch (speed) { case ETHER_LINK_10: ... case ETHER_LINK_100: ... } As a result, 10M and 100M links on AX88179A always fall through to the default 1000M TX/RX PHY delay calibration values. [Severity: High] Can ptp_read_ts() in ax88179a_mac_link_up() race with ax88179a_ptp_read_ts_work() and ax88279_process_tx_ts()? On AX88179A, phylink resolve work calls ax88179a_ptp_read_ts(dev) here while ptp->req_tx_timestamp work also calls ax88179a_ptp_read_ts(dev) concurrently, writing into the shared ptp->ep4_buf and updating ptp->ptp_info_buf and ptp->ptp_info_buf_head without any lock: CPU0 (phylink resolve) CPU1 (req_tx_timestamp / process_ptp) ax88179a_mac_link_up() ax88179a_ptp_read_ts() ax88179_read_cmd(..., ep4_buf) ax88179a_ptp_read_ts_work() ax88179a_ptp_read_ts() ax88179_read_cmd(..., ep4_buf) ptp->ptp_info_buf_head++ ax88279_process_tx_ts() match_ptp_ts() ptp_info_buf[ptp_info_buf_tail] <-- data race / out-of-bounds head ptp->ptp_info_buf_head %= ... On both AX88179A and AX88279, the producer updates ptp_info_buf_head with non-atomic ++ followed by %= AX88179A_PTP_QUEUE_SIZE without locking or barriers against match_ptp_ts() running in ptp->process_ptp_info_buf work. [Severity: High] Do ax88179a_ptp_read_ts() and ax88279_read_ts_complete() read from ptp_info[0] instead of ptp_info[i]? Both functions loop over i = 0..AX88179A_PTP_QUEUE_SIZE - 1 and check if (ptp_info[i].status), but copy the fields from ptp_info->... instead of ptp_info[i]: drivers/net/usb/ax88179a_ptp.c:ax88179a_ptp_read_ts() { ... if (ptp_info[i].status) { int j; j = ptp->ptp_info_buf_head; ptp->ptp_info_buf[j].msg_type = ptp_info->msg_type; ptp->ptp_info_buf[j].sequence_id = ptp_info->sequence_id; ptp->ptp_info_buf[j].nsec = le32_to_cpu(ptp_info->nsec); ptp->ptp_info_buf[j].sec = le16_to_cpu(ptp_info->sec_h); ptp->ptp_info_buf[j].sec <<= 32; ptp->ptp_info_buf[j].sec |= le32_to_cpu(ptp_info->sec_l); ... } Any TX timestamp delivered in queue slots 1..4 is lost and replaced with slot 0. [ ... ] > @@ -927,6 +1003,30 @@ static int ax88179a_bind(struct usbnet *dev, struct usb_interface *intf) > */ > pm_runtime_get_noresume(&intf->dev); > > + if (!ptp_enable) > + return 0; > + > + ret = ax88179a_ptp_register(dev); > + if (ret < 0) { > + netdev_warn(dev->net, "Failed to register PHC device. (%d)\n", ret); > + netdev_warn(dev->net, "PTP disabled\n"); > + /* Disable PTP functionality again and cycle PHY to take effect */ > + ax88179_write_cmd(dev, AX_FW_MODE, AX_FW_MODE_179A, 0, 0, NULL); > + ax88179_write_u8(dev, AX88179A_PHY_POWER, 0, 0, 0); > + msleep(50); > + return 0; > + } > + > + if (ax179_data->ptp && ax179_data->chip_version == AX_VERSION_AX88179A) { > + ax179_data->ptp->ptp_config(dev, SPEED_UNKNOWN); > + > + /* Power cycle PHY to make PTP settings take effect, > + * reset() will power on the PHY again > + */ > + ax88179_write_u8(dev, AX88179A_PHY_POWER, 0, 0, 0); > + msleep(50); > + } > + > return 0; [Severity: High] Can ax88179a_ptp_register() leave ax179_data->ptp dangling or with NULL callbacks when registration fails or the hardware is unsupported? In ax88179a_ptp_register(), ax179_data->ptp = ax88179a_ptp is assigned on entry: drivers/net/usb/ax88179a_ptp.c:ax88179a_ptp_register() { ... ax179_data->ptp = ax88179a_ptp; ... case AX_VERSION_AX88179A: ... if (ax88179_read_cmd(dev, AX88179A_ACCESS_BL, AX88179A_HW_EC_VERSION, 1, 1, &ptp_ver) < 0) return 0; if (ptp_ver < 3) { netdev_warn(dev->net, "PTP on AX88179A version %d < 3 unsupported\n", ptp_ver); return 0; } ... no_ctrlreq: kfree(ax88179a_ptp->urb); err: kfree(ax88179a_ptp); return ret; } On AX88179A with ptp_ver < 3 (including AX88772D) or an EC version read failure, ax88179a_ptp_register() returns 0 before setting ptp_config, pps_ctrl, and ptp_read_ts, so ax179_data->ptp->ptp_config() in ax88179a_bind() immediately dereferences a NULL function pointer. When ax88179a_ptp_register() returns a negative error, it frees ax88179a_ptp without clearing ax179_data->ptp, and ax88179a_bind() returns 0, causing a use-after-free in every later if (ax179_data->ptp) check and a double free in ax88179a_ptp_unregister(). [Severity: Medium] Does ax88179a_ptp_register() also leak urb and ctrlreq and call kfree() on a struct urb in its error paths? In the snippet above, jumping to err after ptp_clock_register() fails (or returning early on ptp_ver < 3) leaks ax88179a_ptp->urb and ax88179a_ptp->ctrlreq, and the no_ctrlreq label frees ax88179a_ptp->urb with kfree() instead of usb_free_urb(). [Severity: High] Does linking ax88179a_ptp_register() into CONFIG_USB_NET_AX88179 fail when CONFIG_USB_NET_AX88179=y and CONFIG_PTP_1588_CLOCK=m? In commit "net: usb: ax88179: Add PTP support", depends on PTP_1588_CLOCK_OPTIONAL was added to config USB_NET_AX8817X instead of config USB_NET_AX88179 in drivers/net/usb/Kconfig, so a built-in ax88179 driver fails to link against modular ptp_clock_register(), ptp_clock_index(), and ptp_clock_unregister(). [Severity: Medium] Do the 32-bit register writes in ax88179a_ptp_config(), ax88179a_ptp_pps_ctrl(), ax88279_ptp_pps_ctrl(), and ax88279_ptp_pbus_write_u32() miss cpu_to_le32() conversion on big-endian hosts? ax88179_write_cmd() only byte-swaps 2-byte values (size == 2), unlike ax88179_read_cmd() and ax88179_write_u32(). Passing a CPU-endian u32 with size 4 to ax88179_write_cmd() in those helpers sends big-endian register values over USB on big-endian systems. > @@ -946,6 +1046,8 @@ static void ax88179a_unbind(struct usbnet *dev, struct usb_interface *intf) > > /* Configure RX control register => stop operation */ > ax88179_write_u16(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, AX_RX_CTL_STOP); > + ax88179a_ptp_kill_urbs(dev); > + ax88179a_ptp_unregister(dev); > > rtnl_lock(); > phylink_disconnect_phy(ax179_data->phylink); [Severity: High] Can freeing ax179_data->ptp before phylink_disconnect_phy() cause a use-after-free in ax88179a_unbind(), and does ax88179a_ptp_unregister() leak urb on AX88179A? ax88179a_ptp_unregister() frees ax179_data->ptp without clearing ax179_data->ptp = NULL or cancelling ax88179a_ptp->req_tx_timestamp, and it runs before phylink_disconnect_phy() flushes phylink resolve work: CPU0 CPU1 (phylink resolve / req_tx_timestamp) ax88179a_unbind() ax88179a_ptp_unregister() kfree(ax179_data->ptp) ax88179a_mac_link_down() if (ax179_data->ptp) ax179_data->ptp->pps_ctrl(...) <-- use-after-free phylink_disconnect_phy() flush_work(&pl->resolve) In addition, ax88179a_ptp_register() allocates ax88179a_ptp->urb on both AX88279 and AX88179A, but ax88179a_ptp_unregister() only calls usb_free_urb(ax88179a_ptp->urb) on AX88279, leaking the URB on every AX88179A unbind. > @@ -1017,8 +1119,10 @@ static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) > hdr_off = FIELD_GET(AX179A_RX_DH_DESC_OFFSET, rx_hdr); > pkt_cnt = rx_hdr & AX179A_RX_DH_PKT_CNT_MASK; > > - /* Consistency check header position */ > - if (hdr_off != skb->len - (pkt_cnt * sizeof(rx_hdr))) > + /* Consistency check header position, frames with PTP timestamps > + * have longer headers than normal frames, so check for minimum size > + */ > + if (hdr_off > skb->len - (pkt_cnt * sizeof(rx_hdr))) > goto err; [Severity: High] Can AX179A_RX_PD_PTP descriptors causepkt_desc_ptr to read past the end of the SKB in ax88179a_rx_fixup()? The pre-loop check only validates 8 bytes per packet (pkt_cnt * 8 + hdr_off <= skb->len), and skb_trim(skb, hdr_off) then discards the original metadata end offset. When AX179A_RX_PD_PTP is set on a descriptor, the loop consumes two additional 64-bit words (++pkt_desc_ptr twice) without checking that 16 extra bytes remain in the metadata area before rx_hdr. (Also, skb->len - (pkt_cnt * sizeof(rx_hdr)) can wrap around as an unsigned subtraction when pkt_cnt * 8 > skb->len.) > @@ -1075,6 +1179,13 @@ static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) > __vlan_hwaccel_put_tag(ax_skb, htons(ETH_P_8021Q), vlan_tag); > } > > + if (pkt_desc & AX179A_RX_PD_PTP) { > + u64 ts_l = get_unaligned_le64(++pkt_desc_ptr); > + u64 ts_h = get_unaligned_le64(++pkt_desc_ptr); > + > + ax88179a_process_rx_ts(ax_skb, ts_l, ts_h); > + } > + > usbnet_skb_return(dev, ax_skb); > skb_pull(skb, pkt_len_plus_padd); [Severity: High] What happens if a packet with AX179A_RX_PD_PTP set hits the drop/error check earlier in the ax88179a_rx_fixup() loop? When a frame has AX179A_RX_PD_DROP set, lacks AX179A_RX_PD_RX_OK, or has an invalid length, the continue branch only increments pkt_desc_ptr by one u64 instead of skipping the two 64-bit timestamp words as well, so the next iteration misinterprets ts_l as the next packet's RX descriptor. [Severity: High] Does AX179A_RX_PD_PTP overflow on 32-bit architectures? In commit "net: usb: ax88179: Add PTP support", drivers/net/usb/ax88179_lib.h defines AX179A_RX_PD_PTP as BIT(56) instead of BIT_ULL(56). On 32-bit builds, 1UL << 56 triggers -Wshift-count-overflow (breaking the build with CONFIG_WERROR=y) and evaluates to 0. > @@ -1090,6 +1201,7 @@ static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) > > static struct sk_buff *ax88179a_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) > { > + struct ax88179_data *ax179_data = dev->driver_priv; > u64 tx_desc = skb->len & AX179A_TX_DESC_LEN_MASK; > int frame_size = dev->maxpacket; > struct sk_buff *ax_skb; > @@ -1132,6 +1244,24 @@ static struct sk_buff *ax88179a_tx_fixup(struct usbnet *dev, struct sk_buff *skb > } > if (padding_size != 0) > skb_put_zero(skb, padding_size); > + > + if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && ax179_data->ptp) { > + struct sk_buff *ptp_skb; > + > + /* Keep the original SKB owned by the socket in our > + * queue with PTP SKBs, while continuing TX with a > + * clone, so that skb_tstamp_tx() can later return > + * the TX timestamp via the socket's error channel. > + */ > + skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; > + ptp_skb = skb_clone(skb, GFP_ATOMIC); > + skb_queue_tail(&ax179_data->ptp->tx_skbs, skb); > + skb = ptp_skb; > + // Get the TX timestamp > + if (ax179_data->chip_version == AX_VERSION_AX88179A) > + schedule_work(&ax179_data->ptp->req_tx_timestamp); > + } > + > /* Copy TX header */ > tx_desc_ptr = skb_push(skb, sizeof(tx_desc)); [Severity: High] If skb_clone() fails in ax88179a_tx_fixup(), won't skb_push() immediately dereference a NULL skb pointer? ptp_skb is not checked for NULL, so on allocation failure the original skb is queued onto ax179_data->ptp->tx_skbs, skb becomes NULL, and skb_push() crashes. [Severity: Medium] Does skb_copy_expand() earlier in ax88179a_tx_fixup() drop the socket and SKBTX_HW_TSTAMP flag before this check runs? When the outgoing skb is cloned or needs headroom or tailroom (for example, when padding_size is 8..15 bytes and exceeds needed_tailroom of 8), ax88179a_tx_fixup() replaces skb with skb_copy_expand() and frees the original socket-owned skb. Because skb_copy_expand() does not copy skb->sk or skb_shinfo(skb)->tx_flags, SKBTX_HW_TSTAMP is already cleared by the time execution reaches this check and the TX timestamp is silently lost. Even when skb_copy_expand() is not taken, skb_put_zero() mutates the length of the original skb before skb_clone(), skb_push() writes the TX descriptor into the shared head buffer of a cloned skb, and on AX88179A schedule_work(&ax179_data->ptp->req_tx_timestamp) runs before usbnet_start_xmit() has submitted the TX URB. [Severity: High] Can queuing non-PTP packets with SKBTX_HW_TSTAMP here dereference an uninitialized ptp_hdr pointer in ax88279_process_tx_ts()? ax88179a_tx_fixup() queues any SKB with SKBTX_HW_TSTAMP onto ptp->tx_skbs without checking ax179_data->ptp->tx_tstamp or classifying the packet. In ax88279_process_tx_ts(), ptp_hdr is uninitialized on the stack and is only assigned when type != PTP_CLASS_NONE: drivers/net/usb/ax88179a_ptp.c:ax88279_process_tx_ts() { struct ptp_header *ptp_hdr; ... skb_queue_walk_safe(&ptp->tx_skbs, skb, tmp) { type = ptp_classify_raw(skb); if (type != PTP_CLASS_NONE) { ptp_hdr = ptp_parse_header(skb, type); if (!ptp_hdr) goto frame_err; } match_ptp_ts(ptp, ptp_hdr, skb, type); } When a user sends a non-PTP packet on a socket with SOF_TIMESTAMPING_TX_HARDWARE, type is PTP_CLASS_NONE and match_ptp_ts() immediately dereferences the uninitialized (or stale) ptp_hdr pointer at ntohs(ptp_hdr->sequence_id). [Severity: High] Does ax88279_process_tx_ts() walk and unlink from ptp->tx_skbs without holding ptp->tx_skbs.lock? ax88179a_tx_fixup() enqueues with skb_queue_tail(), which takes tx_skbs.lock, while ax88279_process_tx_ts() walks ptp->tx_skbs with skb_queue_walk_safe() and unlinks entries with unlocked __skb_unlink(): CPU0 (TX path) CPU1 (process_ptp_info_buf work) ax88179a_tx_fixup() ax88279_process_tx_ts() skb_queue_tail(&ptp->tx_skbs) skb_queue_walk_safe(&ptp->tx_skbs, ...) spin_lock_irqsave(&lock) __skb_queue_tail(...) __skb_unlink(skb, &ptp->tx_skbs) <-- list corruption spin_unlock_irqrestore(&lock) Also, neither ax88179a_mac_link_down() nor ax88179a_stop() cancels ptp->process_ptp_info_buf or purges ptp->tx_skbs, leaving any queued SKBs pinned until unbind. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261129-ptp-v1-0-8148c402beaf@birger-koblitz.de?part=3 ^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2026-11-30 9:30 UTC | newest] Thread overview: 13+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-29 9:29 [PATCH net-next 0/3] net: usb: ax88179: Add support for PTP Birger Koblitz 2026-11-29 9:29 ` [PATCH net-next 1/3] net: usb: ax88179: Add PTP data structures and definitions Birger Koblitz 2026-11-30 9:30 ` sashiko-bot 2026-11-29 9:29 ` [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation Birger Koblitz 2026-11-29 18:31 ` Manuel Ebner 2026-11-30 5:18 ` Birger Koblitz 2026-11-30 9:30 ` sashiko-bot 2026-11-29 9:29 ` [PATCH net-next 3/3] net: usb: ax88179: Enable PTP Birger Koblitz 2026-11-29 15:48 ` Andrew Lunn 2026-11-29 17:24 ` Birger Koblitz 2026-11-29 21:09 ` Andrew Lunn 2026-11-29 21:18 ` kernel test robot 2026-11-30 9:30 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox