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 <ma…@birger-koblitz.de> --- drivers/net/usb/ax88179a_devices.c | 173 ++++++++++++++++++++++++++++++++++--- 1 file changed, 163 insertions(+), 10 deletions(-) diff --git a/drivers/net/usb/ax88179a_devices.c b/drivers/net/usb/ax88179a_devices.c index 69095110e70435806a99274c50d79e86016ec9a0..b8cd0a87d92e34c6dfff27d04e49957ec6a13428 100644 --- a/drivers/net/usb/ax88179a_devices.c +++ b/drivers/net/usb/ax88179a_devices.c @@ -4,6 +4,7 @@ #include <linux/phylink.h> #include <linux/if_vlan.h> #include "ax88179_lib.h" +#include "ax88179a_ptp.h" static int ax88179a_reset(struct usbnet *dev); @@ -13,7 +14,7 @@ static int ax88179a_reset(struct usbnet *dev); 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 +138,14 @@ static int ax88179a_suspend(struct usb_interface *intf, pm_message_t message) int ret; priv = dev->driver_priv; + + if (priv->ptp) { + if (priv->chip_version == AX_VERSION_AX88179A) + cancel_work_sync(&priv->ptp->req_tx_timestamp); + cancel_work_sync(&priv->ptp->process_ptp_info_buf); + ax88179a_ptp_kill_urbs(dev); + } + ax88179_set_pm_mode(dev, true); if (netif_running(dev->net)) { @@ -145,6 +154,9 @@ static int ax88179a_suspend(struct usb_interface *intf, pm_message_t message) rtnl_unlock(); } + if (netif_running(dev->net)) + ax88179a_ptp_kill_urbs(dev); + ret = usbnet_suspend(intf, message); if (ret && netif_running(dev->net)) { rtnl_lock(); @@ -213,7 +225,8 @@ static int ax88179a_resume(struct usb_interface *intf) ax88179_write_cmd(dev, AX88179A_PHY_POWER, 0, 0, 1, &reg8); 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 +245,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 +371,43 @@ 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_ALL) | + 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 +425,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 +551,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 +578,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 +676,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 +872,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 +973,10 @@ 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 PTP functionality + */ + ax88179_write_cmd(dev, AX_FW_MODE, AX_FW_MODE_179A, AX_USB_EP4_EN, 0, NULL); ax88179_write_cmd(dev, AX_RELOAD_EEPROM_EFUSE, 0, 0, 0, NULL); /* Read MAC address from DTB or ASIX chip */ @@ -927,6 +1006,27 @@ static int ax88179a_bind(struct usbnet *dev, struct usb_interface *intf) */ pm_runtime_get_noresume(&intf->dev); + 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); @@ -980,6 +1082,7 @@ static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) struct sk_buff *ax_skb; u32 hdr_off, pkt_end; __le64 *pkt_desc_ptr; + u64 *metadata_end; u16 vlan_tag; u16 pkt_cnt; u64 rx_hdr; @@ -1012,13 +1115,16 @@ static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) /* RX Descriptor Header */ skb_trim(skb, skb->len - sizeof(rx_hdr)); rx_hdr = get_unaligned_le64((__le64 *)skb_tail_pointer(skb)); + metadata_end = (u64 *)skb_tail_pointer(skb); /* Check these packets */ 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 @@ -1055,8 +1161,9 @@ static int ax88179a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) skb_pull(skb, pkt_len_plus_padd); /* Next RX Packet Descriptor */ - pkt_desc_ptr++; - continue; + if (pkt_desc & AX179A_RX_PD_PTP) + pkt_desc_ptr += 2; + goto next_pkt; } ax_skb = netdev_alloc_skb_ip_align(dev->net, pkt_len); @@ -1075,11 +1182,25 @@ 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, ts_h; + + if (pkt_desc_ptr + 2 >= metadata_end) + goto err; + ts_l = get_unaligned_le64(++pkt_desc_ptr); + 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); +next_pkt: /* Next RX Packet Header */ pkt_desc_ptr++; + if (pkt_desc_ptr > metadata_end) + goto err; } return 1; @@ -1090,6 +1211,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; @@ -1123,6 +1245,33 @@ static struct sk_buff *ax88179a_tx_fixup(struct usbnet *dev, struct sk_buff *skb headroom = skb_headroom(skb); tailroom = skb_tailroom(skb); + if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && ax179_data->ptp) { + struct sk_buff *ptp_skb; + + ax88179a_ptp_age_out_skbs(ax179_data->ptp); + if (skb_queue_len_lockless(&ax179_data->ptp->tx_skbs) > AX88179A_PTP_MAX_SKBS) { + ptp_skb = skb_dequeue(&ax179_data->ptp->tx_skbs); + if (ptp_skb) + dev_kfree_skb_any(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); + if (!ptp_skb) + return NULL; + skb_queue_tail(&ax179_data->ptp->tx_skbs, skb); + skb = ptp_skb; + *(unsigned long *)skb->cb = jiffies; + // Get the TX timestamp + if (ax179_data->chip_version == AX_VERSION_AX88179A) + schedule_work(&ax179_data->ptp->req_tx_timestamp); + } + if (skb_cloned(skb) || !(headroom >= sizeof(tx_desc) && tailroom >= padding_size)) { ax_skb = skb_copy_expand(skb, sizeof(tx_desc), padding_size, flags); dev_kfree_skb_any(skb); @@ -1132,6 +1281,7 @@ static struct sk_buff *ax88179a_tx_fixup(struct usbnet *dev, struct sk_buff *skb } if (padding_size != 0) skb_put_zero(skb, padding_size); + /* Copy TX header */ tx_desc_ptr = skb_push(skb, sizeof(tx_desc)); put_unaligned_le64(tx_desc, tx_desc_ptr); @@ -1261,6 +1411,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, &reg16);
-- 2.47.3