From patchwork Tue Mar 22 21:07:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerhard Engleder X-Patchwork-Id: 12789034 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 133BBC433F5 for ; Tue, 22 Mar 2022 21:07:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235091AbiCVVJP (ORCPT ); Tue, 22 Mar 2022 17:09:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234894AbiCVVJO (ORCPT ); Tue, 22 Mar 2022 17:09:14 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3540BDFE1 for ; Tue, 22 Mar 2022 14:07:42 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id m30so15311088wrb.1 for ; Tue, 22 Mar 2022 14:07:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=engleder-embedded-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jN9fN+K5eR2qq2+bppJVp+JVT+ABg49IcCA/S7DXA0E=; b=8OB/veL5qCHCPUSqSO7ikQAqfix8VnPYKomsTpFuybOrn2cDA9x+SUOg0zR24AyQaI Q38ChW22mYayXgozIXueTP7MiLxL3ln3ghjnbDjnc6AkZ7iJ26T57q6J7qfaYSTya79f UMCKHm8WvzcBN/1w+k6zxGqVTSGowBZFyh10CssdaBDUXmvEKjLa0oY3cLi5uFNfwR/6 RrYc+UcKHBLUD6lm4RWgEvsnw3jQ567opT0Yq8SosCql713qr9zofHdzX8cXX5a61JqV 0U3hYCOMgrBUsSfG7g3pQ4nyQ3gG+dS5J4EwShTfMmN+k8hjflgbtwaOa0rtlz8V8vRo w4UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jN9fN+K5eR2qq2+bppJVp+JVT+ABg49IcCA/S7DXA0E=; b=aIzQjmnx1+S4vBIvslLOCT0Aol8jr/U++efjyWSgm87sqobao9Fclmn+OK0xGANSa2 KYwvH1rdKYMHWma44XCcJhYIEDMeoTH44v5+dRYDI4SxgmXDEhMOtVKZ+Kh4NBXpN89J Xdr0SCb59eSeMcMXrG33TtXk/jq/L2QIfFXKmgLBYEwyTFixH+BqYmBze3pNVWBJD8/T kVrjN7iiNQp1D/H50DLaHfv0OdAzlGyqlwrfbyOid3TRjFIUSMsdr0fgXymUrHuuNK6X BRT9DX3BvVCpKDo5Aqy3V7+jWe6Pr8S/wSEMpisCvnSFYBvnR8KtVe4rfcNm91CnSIt0 RtNw== X-Gm-Message-State: AOAM5328ON8Q6Wxa80bZt0+CA6NRK7j67wlXdyfntCF2OV2+h39jM2RP pKvjwESis0tGQYjsy5StHhQdAw== X-Google-Smtp-Source: ABdhPJxUWUQrxqUkEZu2vEtfguOXhg7+MpKjzOxHXMGkQtbWXyoxWI9nlJRoxDwiClwsH5QR1go6dw== X-Received: by 2002:a5d:588b:0:b0:204:1c1a:965d with SMTP id n11-20020a5d588b000000b002041c1a965dmr7641401wrf.669.1647983260673; Tue, 22 Mar 2022 14:07:40 -0700 (PDT) Received: from hornet.engleder.at ([2001:871:23a:8366:6e3b:e5ff:fe2c:34c1]) by smtp.gmail.com with ESMTPSA id c7-20020a5d4f07000000b00203db8f13c6sm16281805wru.75.2022.03.22.14.07.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Mar 2022 14:07:40 -0700 (PDT) From: Gerhard Engleder To: richardcochran@gmail.com, yangbo.lu@nxp.com, davem@davemloft.net, kuba@kernel.org Cc: mlichvar@redhat.com, vinicius.gomes@intel.com, netdev@vger.kernel.org, Gerhard Engleder Subject: [PATCH net-next v1 1/6] ptp: Add cycles support for virtual clocks Date: Tue, 22 Mar 2022 22:07:17 +0100 Message-Id: <20220322210722.6405-2-gerhard@engleder-embedded.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20220322210722.6405-1-gerhard@engleder-embedded.com> References: <20220322210722.6405-1-gerhard@engleder-embedded.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org ptp vclocks require a free running time for their timecounter. Currently only a physical clock forced to free running is supported. If vclocks are used, then the physical clock cannot be synchronized anymore. The synchronized time is not available in hardware in this case. As a result, timed transmission with TAPRIO hardware support is not possible anymore. If hardware would support a free running time additionally to the physical clock, then the physical clock does not need to be forced to free running. Thus, the physical clocks can still be synchronized while vclocks are in use. The physical clock could be used to synchronize the time domain of the TSN network and trigger TAPRIO. In parallel vclocks can be used to synchronize other time domains. Introduce support for a free running time called cycles to physical clocks. Rework ptp vclocks to use cycles. Default implementation of cycles is based on time of physical clock. Thus, behavior of ptp vclocks based on physical clocks without cycles is identical to previous behavior. Signed-off-by: Gerhard Engleder --- drivers/ptp/ptp_clock.c | 31 +++++++++++++++++++++++++++---- drivers/ptp/ptp_private.h | 10 ++++++++++ drivers/ptp/ptp_sysfs.c | 10 ++++++---- drivers/ptp/ptp_vclock.c | 13 +++++-------- include/linux/ptp_clock_kernel.h | 30 ++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 16 deletions(-) diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c index b6f2cfd15dd2..54b9f54ac0b2 100644 --- a/drivers/ptp/ptp_clock.c +++ b/drivers/ptp/ptp_clock.c @@ -77,8 +77,8 @@ static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp { struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); - if (ptp_vclock_in_use(ptp)) { - pr_err("ptp: virtual clock in use\n"); + if (ptp_clock_freerun(ptp)) { + pr_err("ptp: physical clock is free running\n"); return -EBUSY; } @@ -103,8 +103,8 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) struct ptp_clock_info *ops; int err = -EOPNOTSUPP; - if (ptp_vclock_in_use(ptp)) { - pr_err("ptp: virtual clock in use\n"); + if (ptp_clock_freerun(ptp)) { + pr_err("ptp: physical clock is free running\n"); return -EBUSY; } @@ -178,6 +178,14 @@ static void ptp_clock_release(struct device *dev) kfree(ptp); } +static int ptp_getcycles64(struct ptp_clock_info *info, struct timespec64 *ts) +{ + if (info->getcyclesx64) + return info->getcyclesx64(info, ts, NULL); + else + return info->gettime64(info, ts); +} + static void ptp_aux_kworker(struct kthread_work *work) { struct ptp_clock *ptp = container_of(work, struct ptp_clock, @@ -225,6 +233,21 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, mutex_init(&ptp->n_vclocks_mux); init_waitqueue_head(&ptp->tsev_wq); + if (!ptp->info->getcycles64 && !ptp->info->getcyclesx64) { + /* Cycles are based on time per default. */ + ptp->info->getcycles64 = ptp_getcycles64; + + if (ptp->info->gettimex64) + ptp->info->getcyclesx64 = ptp->info->gettimex64; + + if (ptp->info->getcrosststamp) + ptp->info->getcrosscycles = ptp->info->getcrosststamp; + } else { + ptp->cycles = true; + if (!ptp->info->getcycles64 && ptp->info->getcyclesx64) + ptp->info->getcycles64 = ptp_getcycles64; + } + if (ptp->info->do_aux_work) { kthread_init_delayed_work(&ptp->aux_work, ptp_aux_kworker); ptp->kworker = kthread_create_worker(0, "ptp%d", ptp->index); diff --git a/drivers/ptp/ptp_private.h b/drivers/ptp/ptp_private.h index dba6be477067..ae66376def84 100644 --- a/drivers/ptp/ptp_private.h +++ b/drivers/ptp/ptp_private.h @@ -52,6 +52,7 @@ struct ptp_clock { int *vclock_index; struct mutex n_vclocks_mux; /* protect concurrent n_vclocks access */ bool is_virtual_clock; + bool cycles; }; #define info_to_vclock(d) container_of((d), struct ptp_vclock, info) @@ -96,6 +97,15 @@ static inline bool ptp_vclock_in_use(struct ptp_clock *ptp) return in_use; } +/* Check if ptp clock shall be free running */ +static inline bool ptp_clock_freerun(struct ptp_clock *ptp) +{ + if (ptp->cycles) + return false; + + return ptp_vclock_in_use(ptp); +} + extern struct class *ptp_class; /* diff --git a/drivers/ptp/ptp_sysfs.c b/drivers/ptp/ptp_sysfs.c index 9233bfedeb17..8da3d374f78e 100644 --- a/drivers/ptp/ptp_sysfs.c +++ b/drivers/ptp/ptp_sysfs.c @@ -231,10 +231,12 @@ static ssize_t n_vclocks_store(struct device *dev, *(ptp->vclock_index + ptp->n_vclocks - i) = -1; } - if (num == 0) - dev_info(dev, "only physical clock in use now\n"); - else - dev_info(dev, "guarantee physical clock free running\n"); + if (!ptp->cycles) { + if (num == 0) + dev_info(dev, "only physical clock in use now\n"); + else + dev_info(dev, "guarantee physical clock free running\n"); + } ptp->n_vclocks = num; mutex_unlock(&ptp->n_vclocks_mux); diff --git a/drivers/ptp/ptp_vclock.c b/drivers/ptp/ptp_vclock.c index cb179a3ea508..3a095eab9cc5 100644 --- a/drivers/ptp/ptp_vclock.c +++ b/drivers/ptp/ptp_vclock.c @@ -68,7 +68,7 @@ static int ptp_vclock_gettimex(struct ptp_clock_info *ptp, int err; u64 ns; - err = pptp->info->gettimex64(pptp->info, &pts, sts); + err = pptp->info->getcyclesx64(pptp->info, &pts, sts); if (err) return err; @@ -104,7 +104,7 @@ static int ptp_vclock_getcrosststamp(struct ptp_clock_info *ptp, int err; u64 ns; - err = pptp->info->getcrosststamp(pptp->info, xtstamp); + err = pptp->info->getcrosscycles(pptp->info, xtstamp); if (err) return err; @@ -143,10 +143,7 @@ static u64 ptp_vclock_read(const struct cyclecounter *cc) struct ptp_clock *ptp = vclock->pclock; struct timespec64 ts = {}; - if (ptp->info->gettimex64) - ptp->info->gettimex64(ptp->info, &ts, NULL); - else - ptp->info->gettime64(ptp->info, &ts); + ptp->info->getcycles64(ptp->info, &ts); return timespec64_to_ns(&ts); } @@ -168,11 +165,11 @@ struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock) vclock->pclock = pclock; vclock->info = ptp_vclock_info; - if (pclock->info->gettimex64) + if (pclock->info->getcyclesx64) vclock->info.gettimex64 = ptp_vclock_gettimex; else vclock->info.gettime64 = ptp_vclock_gettime; - if (pclock->info->getcrosststamp) + if (pclock->info->getcrosscycles) vclock->info.getcrosststamp = ptp_vclock_getcrosststamp; vclock->cc = ptp_vclock_cc; diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h index 554454cb8693..acdcaa4a1ec2 100644 --- a/include/linux/ptp_clock_kernel.h +++ b/include/linux/ptp_clock_kernel.h @@ -108,6 +108,31 @@ struct ptp_system_timestamp { * @settime64: Set the current time on the hardware clock. * parameter ts: Time value to set. * + * @getcycles64: Reads the current free running time from the hardware clock. + * If @getcycles64 and @getcyclesx64 are not supported, then + * @gettime64 or @gettimex64 will be used as default + * implementation. + * parameter ts: Holds the result. + * + * @getcyclesx64: Reads the current free running time from the hardware clock + * and optionally also the system clock. + * If @getcycles64 and @getcyclesx64 are not supported, then + * @gettimex64 will be used as default implementation if + * available. + * parameter ts: Holds the PHC timestamp. + * parameter sts: If not NULL, it holds a pair of timestamps + * from the system clock. The first reading is made right before + * reading the lowest bits of the PHC timestamp and the second + * reading immediately follows that. + * + * @getcrosscycles: Reads the current free running time from the hardware clock + * and system clock simultaneously. + * If @getcycles64 and @getcyclesx64 are not supported, then + * @getcrosststamp will be used as default implementation if + * available. + * parameter cts: Contains timestamp (device,system) pair, + * where system time is realtime and monotonic. + * * @enable: Request driver to enable or disable an ancillary feature. * parameter request: Desired resource to enable or disable. * parameter on: Caller passes one to enable or zero to disable. @@ -155,6 +180,11 @@ struct ptp_clock_info { int (*getcrosststamp)(struct ptp_clock_info *ptp, struct system_device_crosststamp *cts); int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts); + int (*getcycles64)(struct ptp_clock_info *ptp, struct timespec64 *ts); + int (*getcyclesx64)(struct ptp_clock_info *ptp, struct timespec64 *ts, + struct ptp_system_timestamp *sts); + int (*getcrosscycles)(struct ptp_clock_info *ptp, + struct system_device_crosststamp *cts); int (*enable)(struct ptp_clock_info *ptp, struct ptp_clock_request *request, int on); int (*verify)(struct ptp_clock_info *ptp, unsigned int pin, From patchwork Tue Mar 22 21:07:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerhard Engleder X-Patchwork-Id: 12789033 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55F54C433EF for ; Tue, 22 Mar 2022 21:07:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235032AbiCVVJO (ORCPT ); Tue, 22 Mar 2022 17:09:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230015AbiCVVJN (ORCPT ); Tue, 22 Mar 2022 17:09:13 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D42C16151 for ; Tue, 22 Mar 2022 14:07:43 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id l7-20020a05600c1d0700b0038c99618859so4103726wms.2 for ; Tue, 22 Mar 2022 14:07:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=engleder-embedded-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dOjiq8Aqyi8KntJHZ/EWB00zgN02oNcNdxQFPgwQW6w=; b=J8LYC5pkKNpKe6OeijsMM3+GJo69vUpHBF6B+YmEgYZwKj+P2IVAIwh11IL/InqTrm FqlYlYMawYfYzRPBTmy1hoiCJACJpw0G7bCF4lkVXnnH2DeA7P5QFk/oFirbPVHh7oeV XXT9CTJEwqx/2ZxPGnVYxl0g0YMWvneV89PDuyv82xRfTpPQPPfZSvyCwDdG2VPgjYJ5 5btcR7J+UpnPLWx961Ikj9EdD74FmmpRkQvhkmzeltMNT+/yGjaswxeqGztJfMnvJzVt yUgopuYu/XbQa2wVd+Xx7DmRDEUVvkOVIo6XqfTey9XxlS3pwG6+ujQvDFF45Kbs6vNy T26w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dOjiq8Aqyi8KntJHZ/EWB00zgN02oNcNdxQFPgwQW6w=; b=xHDHHjP1bdHuOAHUQ0OHXzEODqVGAYPZuH56nY1h+hNaJ2QjDPim/BBMjF73esdo2y QvIcYrX4JRE80LpJBFOjNzDvZ8VzTmUbQZp2EZ3RNd6Tm3TpNg8Bub6XRX5oMbb+haK4 FcIvUh0zK0pj97/yKkmTvqBDk0Cf5Td3ZKZaG+dp0Ut+fUkLa0lDG1jx6nNuhxYFzfxB 44BYQFCH/zrOa8McMZDbxrBonQIKjs7JfGnrAFAUTnC9N/kGXRnIfyiRWbirqEW/5ktR MFxVW+RyDbgsaipIcc+eArKp2GLuHdzVGNcyIglXVtdnC6RBwHPjBZ1dCkJv4ODvn/Yw 4ROA== X-Gm-Message-State: AOAM533yTvidnMvSvlG0J5VAwUYFxtHR2niPAMK7H+hta/0F+nJDSMal POK/ujASjmiYnwKCxUL/as5xCQ== X-Google-Smtp-Source: ABdhPJzenh++JnlXb5IWu7bsEZ6UrSl/D0L/fwAWeYNr26lfBnCM0vd2Xx+ZjAGBI3Z3KPJ6VA/QOw== X-Received: by 2002:a05:600c:4f92:b0:38a:1d7:c01c with SMTP id n18-20020a05600c4f9200b0038a01d7c01cmr5764372wmq.164.1647983262489; Tue, 22 Mar 2022 14:07:42 -0700 (PDT) Received: from hornet.engleder.at ([2001:871:23a:8366:6e3b:e5ff:fe2c:34c1]) by smtp.gmail.com with ESMTPSA id c7-20020a5d4f07000000b00203db8f13c6sm16281805wru.75.2022.03.22.14.07.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Mar 2022 14:07:42 -0700 (PDT) From: Gerhard Engleder To: richardcochran@gmail.com, yangbo.lu@nxp.com, davem@davemloft.net, kuba@kernel.org Cc: mlichvar@redhat.com, vinicius.gomes@intel.com, netdev@vger.kernel.org, Gerhard Engleder Subject: [PATCH net-next v1 2/6] ptp: Request cycles for TX timestamp Date: Tue, 22 Mar 2022 22:07:18 +0100 Message-Id: <20220322210722.6405-3-gerhard@engleder-embedded.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20220322210722.6405-1-gerhard@engleder-embedded.com> References: <20220322210722.6405-1-gerhard@engleder-embedded.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org The free running time of physical clocks called cycles shall be used for hardware timestamps to enable synchronisation. Introduce new flag SKBTX_HW_TSTAMP_USE_CYCLES, which signals driver to provide a TX timestamp based on cycles if cycles are supported. Signed-off-by: Gerhard Engleder --- include/linux/skbuff.h | 3 +++ net/core/skbuff.c | 2 ++ net/socket.c | 10 +++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 26538ceb4b01..f494ddbfc826 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -578,6 +578,9 @@ enum { /* device driver is going to provide hardware time stamp */ SKBTX_IN_PROGRESS = 1 << 2, + /* generate hardware time stamp based on cycles if supported */ + SKBTX_HW_TSTAMP_USE_CYCLES = 1 << 3, + /* generate wifi status information (where possible) */ SKBTX_WIFI_STATUS = 1 << 4, diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 10bde7c6db44..c0f8f1341c3f 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -4847,6 +4847,8 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & SKBTX_ANY_TSTAMP; skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; + } else { + skb_shinfo(skb)->tx_flags &= ~SKBTX_HW_TSTAMP_USE_CYCLES; } if (hwtstamps) diff --git a/net/socket.c b/net/socket.c index 982eecad464c..1acebcb19e8f 100644 --- a/net/socket.c +++ b/net/socket.c @@ -683,9 +683,17 @@ void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags) { u8 flags = *tx_flags; - if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE) + if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE) { flags |= SKBTX_HW_TSTAMP; + /* PTP hardware clocks can provide a free running time called + * cycles as base for virtual clocks. Tell driver to use cycles + * for timestamp if socket is bound to virtual clock. + */ + if (tsflags & SOF_TIMESTAMPING_BIND_PHC) + flags |= SKBTX_HW_TSTAMP_USE_CYCLES; + } + if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE) flags |= SKBTX_SW_TSTAMP; From patchwork Tue Mar 22 21:07:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerhard Engleder X-Patchwork-Id: 12789035 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91DA0C433EF for ; Tue, 22 Mar 2022 21:07:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235100AbiCVVJQ (ORCPT ); Tue, 22 Mar 2022 17:09:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235021AbiCVVJO (ORCPT ); Tue, 22 Mar 2022 17:09:14 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94B013EAA5 for ; Tue, 22 Mar 2022 14:07:45 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id v130-20020a1cac88000000b00389d0a5c511so4085701wme.5 for ; Tue, 22 Mar 2022 14:07:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=engleder-embedded-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lIYEEzHUYKzkWkxt1nwU2xaGYq4yK0UBpk6s66e1KD4=; b=c7JZOjdlVB/hNXmn+I2Vk1+M2KyKT1MtJn0t2XLD3/Up4911XVoTUIf7TWD4EGlYFu +0FgCxM634JO08vusv4DfGM34JUZvGVuAj44eUyhNU8Vzw8W9NnXQVbw1xZ8FdtPOnz+ Wrsq05UoJmTs4taZi1Ad2KArn0/bQWb+Mr05X9QMRB1U5KVlYOymF37IDXUA0/DASHgn P5o9vkdKS6B3u4v1A5UwwcLAU06gE0I4dgU3fJOmU8yF6IEzYnO+d41dC4eca9twtFhg HyQ9xWj3Wavu8jK4T+mYE9L8ignnXe4olnayGohbDZR78jATxZ+NuM5aHjuPMSiuecdy qUKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lIYEEzHUYKzkWkxt1nwU2xaGYq4yK0UBpk6s66e1KD4=; b=dVp31fSf4x21skjUQ8kvhOw0cHexDaE0lr0iAWg+5suj+DMs1i40lc1njOyB6JAkrS OGmG4dix/elYClVS4QxDSkd+KcIgUHcHAHVb8vcPjNOHPRZCPE7EKnGQPiQqkqZtJSgs JcIz2RQhrQ579xOE7oaZz26zYFEVHszEQNBlw4ivSBCCTfLm5mQbBG7wWVAmvr2/jriX xjvVwtZREttiPrS/PuRDGR2zvDRiWWgld0hTzpLPPmEp7TnJ6dYYgc6GJiuTIXnqF5LE T3y+SKv+lcH/i+x4wQ44sJHKhskMAFTDd1qNHHycoD64Jn3ZlxW4VYIe+4iIeOx1QGSp s0dg== X-Gm-Message-State: AOAM5329lAo/Dy0JqrzoOdkI2U0dTKkO1p17EbUU+ZaGvQJgUbHLobKQ UkBX6vjCwGKyiKuaQ26O+x0Xag== X-Google-Smtp-Source: ABdhPJzC4CkdJU8Mjb9hZGgZTUueN21KJNVRaff5uqR89ZD64UBuJx7nM45hvERhXu6qBlfJU3oWCg== X-Received: by 2002:a1c:c904:0:b0:38c:8dc1:87a3 with SMTP id f4-20020a1cc904000000b0038c8dc187a3mr5884410wmb.101.1647983264184; Tue, 22 Mar 2022 14:07:44 -0700 (PDT) Received: from hornet.engleder.at ([2001:871:23a:8366:6e3b:e5ff:fe2c:34c1]) by smtp.gmail.com with ESMTPSA id c7-20020a5d4f07000000b00203db8f13c6sm16281805wru.75.2022.03.22.14.07.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Mar 2022 14:07:43 -0700 (PDT) From: Gerhard Engleder To: richardcochran@gmail.com, yangbo.lu@nxp.com, davem@davemloft.net, kuba@kernel.org Cc: mlichvar@redhat.com, vinicius.gomes@intel.com, netdev@vger.kernel.org, Gerhard Engleder Subject: [PATCH net-next v1 3/6] ptp: Pass hwtstamp to ptp_convert_timestamp() Date: Tue, 22 Mar 2022 22:07:19 +0100 Message-Id: <20220322210722.6405-4-gerhard@engleder-embedded.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20220322210722.6405-1-gerhard@engleder-embedded.com> References: <20220322210722.6405-1-gerhard@engleder-embedded.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org ptp_convert_timestamp() converts only the timestamp hwtstamp, which is a field of the argument with the type struct skb_shared_hwtstamps *. So a pointer to the hwtstamp field of this structure is sufficient. Rework ptp_convert_timestamp() to use an argument of type ktime_t *. This allows to add additional timestamp manipulation stages before the call of ptp_convert_timestamp(). Signed-off-by: Gerhard Engleder Acked-by: Richard Cochran --- drivers/ptp/ptp_vclock.c | 5 ++--- include/linux/ptp_clock_kernel.h | 7 +++---- net/socket.c | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/ptp/ptp_vclock.c b/drivers/ptp/ptp_vclock.c index 3a095eab9cc5..c30bcce2bb43 100644 --- a/drivers/ptp/ptp_vclock.c +++ b/drivers/ptp/ptp_vclock.c @@ -232,8 +232,7 @@ int ptp_get_vclocks_index(int pclock_index, int **vclock_index) } EXPORT_SYMBOL(ptp_get_vclocks_index); -ktime_t ptp_convert_timestamp(const struct skb_shared_hwtstamps *hwtstamps, - int vclock_index) +ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index) { char name[PTP_CLOCK_NAME_LEN] = ""; struct ptp_vclock *vclock; @@ -255,7 +254,7 @@ ktime_t ptp_convert_timestamp(const struct skb_shared_hwtstamps *hwtstamps, vclock = info_to_vclock(ptp->info); - ns = ktime_to_ns(hwtstamps->hwtstamp); + ns = ktime_to_ns(*hwtstamp); spin_lock_irqsave(&vclock->lock, flags); ns = timecounter_cyc2time(&vclock->tc, ns); diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h index acdcaa4a1ec2..cc6a7b2e267d 100644 --- a/include/linux/ptp_clock_kernel.h +++ b/include/linux/ptp_clock_kernel.h @@ -379,17 +379,16 @@ int ptp_get_vclocks_index(int pclock_index, int **vclock_index); /** * ptp_convert_timestamp() - convert timestamp to a ptp vclock time * - * @hwtstamps: skb_shared_hwtstamps structure pointer + * @hwtstamp: timestamp * @vclock_index: phc index of ptp vclock. * * Returns converted timestamp, or 0 on error. */ -ktime_t ptp_convert_timestamp(const struct skb_shared_hwtstamps *hwtstamps, - int vclock_index); +ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index); #else static inline int ptp_get_vclocks_index(int pclock_index, int **vclock_index) { return 0; } -static inline ktime_t ptp_convert_timestamp(const struct skb_shared_hwtstamps *hwtstamps, +static inline ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index) { return 0; } diff --git a/net/socket.c b/net/socket.c index 1acebcb19e8f..2e932c058002 100644 --- a/net/socket.c +++ b/net/socket.c @@ -887,7 +887,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) && !skb_is_swtx_tstamp(skb, false_tstamp)) { if (sk->sk_tsflags & SOF_TIMESTAMPING_BIND_PHC) - hwtstamp = ptp_convert_timestamp(shhwtstamps, + hwtstamp = ptp_convert_timestamp(&shhwtstamps->hwtstamp, sk->sk_bind_phc); else hwtstamp = shhwtstamps->hwtstamp; From patchwork Tue Mar 22 21:07:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerhard Engleder X-Patchwork-Id: 12789036 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22B12C433FE for ; Tue, 22 Mar 2022 21:07:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234755AbiCVVJR (ORCPT ); Tue, 22 Mar 2022 17:09:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235043AbiCVVJP (ORCPT ); Tue, 22 Mar 2022 17:09:15 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB3216151 for ; Tue, 22 Mar 2022 14:07:46 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id i186so2694405wma.3 for ; Tue, 22 Mar 2022 14:07:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=engleder-embedded-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4gY9w4G273mp/X5m017v5e4gJ/MFTlUqy6e7fxJy9R4=; b=stPV/SWYUBBHr4/BQ+3zUfFIXkLZnFB8nPEp1MDspMzENhFCUzTyDPyV39TwMzN/Cf WXiaE6hWDNA+eF4XXkt9IKVSrpAZkK9yjTCT4JTSxrxZBYQLkUqW3cqx3fUg29eZvIcX PMCI24KI389rAAg4JI4Iexa6FFsRI7yC29iQIkL93ZN7G2jQCJfUikXIX9Hk5WhB2KT8 HN0wvfKuZYicFkVlPxmSxtgzbq+SMW8VapFzfoqSdd14TQy3TWdoe/BjP353CJNq+ptr 1zlQhk3gRxMomct46ktNfrXvdN/gDnKLzVzenwt2ke24+irL0cGF/d2d0nIiWCMvlnba SiBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4gY9w4G273mp/X5m017v5e4gJ/MFTlUqy6e7fxJy9R4=; b=c7cIXilN24OqvwfVJrcdOk6R4iXQyYIKCiImLKUxn58DYVlPOHzpXXYp5y+MLKBO+h InMsIt3+J3Pr0MRdAEFlEq9KzL+E8hQOA8HzqPNJcdkAjxbHU7MJtbJYZMWQ2FCbKVaX ghqIK0rpqjb8Dcl5Khdu+jUMoEb3GXLiq/Toqw3e35OXZwOhvR6P/4D4Bsgcm0ZuqHdC d1M6FlVwka7lLoxhbraO/MifIRnnns8pS5ULXOIDZA1jJZeqSzU4ajLAwj1Wi4HXgmAd ipLg1FKTtRqrVUJtwSLjReBZVEl7omsgO8ovFjBpMEUCVqU0zcuwIBqqILVKi1JfleCB wPKw== X-Gm-Message-State: AOAM533bzzdhd1oFlZt0gT8Tn5h41+RVBtbiHKUMkexAUidy1M3eDxmG UkvxhYOPlMthNkUlXET3z9sWIw== X-Google-Smtp-Source: ABdhPJzDvtSHfVPQjjTyq/iofflRrNfz2n0/pdyapCldVDh86rT38MN5EgQlxvh4uGEltgkaAkYg4A== X-Received: by 2002:a7b:c5d1:0:b0:37f:a8a3:9e17 with SMTP id n17-20020a7bc5d1000000b0037fa8a39e17mr5648420wmk.109.1647983265543; Tue, 22 Mar 2022 14:07:45 -0700 (PDT) Received: from hornet.engleder.at ([2001:871:23a:8366:6e3b:e5ff:fe2c:34c1]) by smtp.gmail.com with ESMTPSA id c7-20020a5d4f07000000b00203db8f13c6sm16281805wru.75.2022.03.22.14.07.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Mar 2022 14:07:45 -0700 (PDT) From: Gerhard Engleder To: richardcochran@gmail.com, yangbo.lu@nxp.com, davem@davemloft.net, kuba@kernel.org Cc: mlichvar@redhat.com, vinicius.gomes@intel.com, netdev@vger.kernel.org, Gerhard Engleder Subject: [PATCH net-next v1 4/6] ethtool: Add kernel API for PHC index Date: Tue, 22 Mar 2022 22:07:20 +0100 Message-Id: <20220322210722.6405-5-gerhard@engleder-embedded.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20220322210722.6405-1-gerhard@engleder-embedded.com> References: <20220322210722.6405-1-gerhard@engleder-embedded.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Add a new function, which returns the physical clock index of a networking device. This function will be used to get the physical clock of a device for timestamp manipulation in the receive path. Signed-off-by: Gerhard Engleder Acked-by: Richard Cochran --- include/linux/ethtool.h | 8 ++++++++ net/ethtool/common.c | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h index 4af58459a1e7..e107069f37a4 100644 --- a/include/linux/ethtool.h +++ b/include/linux/ethtool.h @@ -820,6 +820,14 @@ void ethtool_params_from_link_mode(struct ethtool_link_ksettings *link_ksettings, enum ethtool_link_mode_bit_indices link_mode); +/** + * ethtool_get_phc - Get phc index + * @dev: pointer to net_device structure + * + * Return index of phc + */ +int ethtool_get_phc(struct net_device *dev); + /** * ethtool_get_phc_vclocks - Derive phc vclocks information, and caller * is responsible to free memory of vclock_index diff --git a/net/ethtool/common.c b/net/ethtool/common.c index 0c5210015911..8218e3b3e98a 100644 --- a/net/ethtool/common.c +++ b/net/ethtool/common.c @@ -557,6 +557,19 @@ int __ethtool_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info) return 0; } +int ethtool_get_phc(struct net_device *dev) +{ + struct ethtool_ts_info info; + int ret; + + ret = __ethtool_get_ts_info(dev, &info); + if (ret) + return ret; + + return info.phc_index; +} +EXPORT_SYMBOL(ethtool_get_phc); + int ethtool_get_phc_vclocks(struct net_device *dev, int **vclock_index) { struct ethtool_ts_info info = { }; From patchwork Tue Mar 22 21:07:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerhard Engleder X-Patchwork-Id: 12789038 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4A31C433FE for ; Tue, 22 Mar 2022 21:08:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235129AbiCVVJ1 (ORCPT ); Tue, 22 Mar 2022 17:09:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235125AbiCVVJR (ORCPT ); Tue, 22 Mar 2022 17:09:17 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B1613EB98 for ; Tue, 22 Mar 2022 14:07:48 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id m30so15311362wrb.1 for ; Tue, 22 Mar 2022 14:07:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=engleder-embedded-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EOSi+suv0J1h2ZIB8fjA7MCFCHrs1fEJm+48J01la2U=; b=ngq5GXkC/g/YurIKAjejy9jmPJYT8Lz4UDF1+Vu44XOtlQgzIYDOfGOyTcxkd6onhs zw4TVNlXP+1dDjJqKeSk8W0ES957YXcFuw3zEbzz7bbM8RnUSM+11ly/BZJNj7MXMl7P bV+pn7FtrnJjMl/ccoJtEkniPX4C+oQXCfAmMNIe2JOZrJHNslC19jtHXpUTBadylDtb 54fM1KeCHUiIiUBJqTqUwXpx/To3zD1sMkwjpWpU/VNVd5gboO9iU+U2F2y3mlFwOCmm fNwaK5sfPHRn9oHn3zKtO2lSr2tmXTlBBhel4Er9QEx7iH8lVFfFMo0eNRuDBzIHebgA J/UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EOSi+suv0J1h2ZIB8fjA7MCFCHrs1fEJm+48J01la2U=; b=Y9CUIGz6of1R1iFXRwcsqvJ8Sw0f+HczhkxXsbDtkAXBgutgj4QHrhFSGAfkuelUQR I/yjLLRE4bMDxKyiB1C3lGKq9M5yeyoE7yJ7edQ1fb7rZDRrV7BPNKb4SYELDf2MOidF yUUUMkPYMCLZ0RmepZbTWPWPAxfdBdvjEt5d+mI2whPp78K/MIIFsO28DC9EwkXf3Man wTDZm6f7RNmuYxpUvJrCgxIZPjz49ARTVZDpy9NyB/s0sJyFUi08rzK3xwe31ARFKk3O u6jVrmIhMvu80TqxRUMWsXZFwSl6KJkqOVyLH7AnaYNBqP0NojcqLjpwc53uTYF8hHLp sfNg== X-Gm-Message-State: AOAM532ZxrksQ1QbaEGCpho30bcisMxqgDQlk60L97sVTRtbX9MoXq/x Oc9a8p79uwTiJz2IKxInHIyC+w== X-Google-Smtp-Source: ABdhPJweIEmKrZ95k6rhi0nKGbT00X+ACh03cD/gY8IxmwoiLt2Xadz6l+e1CubF0sUiMbWDFI32mg== X-Received: by 2002:a05:6000:1a43:b0:203:fc82:22d9 with SMTP id t3-20020a0560001a4300b00203fc8222d9mr16933491wry.517.1647983266830; Tue, 22 Mar 2022 14:07:46 -0700 (PDT) Received: from hornet.engleder.at ([2001:871:23a:8366:6e3b:e5ff:fe2c:34c1]) by smtp.gmail.com with ESMTPSA id c7-20020a5d4f07000000b00203db8f13c6sm16281805wru.75.2022.03.22.14.07.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Mar 2022 14:07:46 -0700 (PDT) From: Gerhard Engleder To: richardcochran@gmail.com, yangbo.lu@nxp.com, davem@davemloft.net, kuba@kernel.org Cc: mlichvar@redhat.com, vinicius.gomes@intel.com, netdev@vger.kernel.org, Gerhard Engleder Subject: [PATCH net-next v1 5/6] ptp: Support late timestamp determination Date: Tue, 22 Mar 2022 22:07:21 +0100 Message-Id: <20220322210722.6405-6-gerhard@engleder-embedded.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20220322210722.6405-1-gerhard@engleder-embedded.com> References: <20220322210722.6405-1-gerhard@engleder-embedded.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org If a physical clock supports a free running time called cycles, then timestamps shall be based on this time too. For TX it is known in advance before the transmission if a timestamp based on cycles is needed. For RX it is impossible to know which timestamp is needed before the packet is received and assigned to a socket. Support late timestamp determination by a physical clock. Therefore, an address/cookie is stored within the new phc_data field of struct skb_shared_hwtstamps. This address/cookie is provided to a new physical clock method called gettstamp(), which returns a timestamp based on the normal/adjustable time or based on cycles. The previously introduced flag SKBTX_HW_TSTAMP_USE_CYCLES is reused with an additional alias to request the late timestamp determination by the physical clock. That is possible, because SKBTX_HW_TSTAMP_USE_CYCLES is not used in the receive path. Signed-off-by: Gerhard Engleder Reported-by: kernel test robot Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/ptp/ptp_clock.c | 27 ++++++++++++++++++++++++ include/linux/ptp_clock_kernel.h | 30 +++++++++++++++++++++++++++ include/linux/skbuff.h | 8 +++++++- net/socket.c | 35 ++++++++++++++++++++++---------- 4 files changed, 88 insertions(+), 12 deletions(-) diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c index 54b9f54ac0b2..b7a8cf27c349 100644 --- a/drivers/ptp/ptp_clock.c +++ b/drivers/ptp/ptp_clock.c @@ -450,6 +450,33 @@ void ptp_cancel_worker_sync(struct ptp_clock *ptp) } EXPORT_SYMBOL(ptp_cancel_worker_sync); +ktime_t ptp_get_timestamp(int index, + const struct skb_shared_hwtstamps *hwtstamps, + bool cycles) +{ + char name[PTP_CLOCK_NAME_LEN] = ""; + struct ptp_clock *ptp; + struct device *dev; + ktime_t ts; + + snprintf(name, PTP_CLOCK_NAME_LEN, "ptp%d", index); + dev = class_find_device_by_name(ptp_class, name); + if (!dev) + return 0; + + ptp = dev_get_drvdata(dev); + + if (ptp->info->gettstamp) + ts = ptp->info->gettstamp(ptp->info, hwtstamps, cycles); + else + ts = hwtstamps->hwtstamp; + + put_device(dev); + + return ts; +} +EXPORT_SYMBOL(ptp_get_timestamp); + /* module operations */ static void __exit ptp_exit(void) diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h index cc6a7b2e267d..f4f0d8a880c6 100644 --- a/include/linux/ptp_clock_kernel.h +++ b/include/linux/ptp_clock_kernel.h @@ -133,6 +133,16 @@ struct ptp_system_timestamp { * parameter cts: Contains timestamp (device,system) pair, * where system time is realtime and monotonic. * + * @gettstamp: Get hardware timestamp based on normal/adjustable time or free + * running time. If @getcycles64 or @getcyclesx64 are supported, + * then this method is required to provide timestamps based on the + * free running time. This method will be called if + * SKBTX_HW_TSTAMP_PHC is set by the driver. + * parameter hwtstamps: skb_shared_hwtstamps structure pointer. + * parameter cycles: If false, then hardware timestamp based on + * normal/adjustable time is requested. If true, then hardware + * timestamp based on free running time is requested. + * * @enable: Request driver to enable or disable an ancillary feature. * parameter request: Desired resource to enable or disable. * parameter on: Caller passes one to enable or zero to disable. @@ -185,6 +195,9 @@ struct ptp_clock_info { struct ptp_system_timestamp *sts); int (*getcrosscycles)(struct ptp_clock_info *ptp, struct system_device_crosststamp *cts); + ktime_t (*gettstamp)(struct ptp_clock_info *ptp, + const struct skb_shared_hwtstamps *hwtstamps, + bool cycles); int (*enable)(struct ptp_clock_info *ptp, struct ptp_clock_request *request, int on); int (*verify)(struct ptp_clock_info *ptp, unsigned int pin, @@ -364,6 +377,19 @@ static inline void ptp_cancel_worker_sync(struct ptp_clock *ptp) * a loadable module. */ +/** + * ptp_get_timestamp() - get timestamp of ptp clock + * + * @index: phc index of ptp pclock. + * @hwtstamps: skb_shared_hwtstamps structure pointer. + * @cycles: true for timestamp based on cycles. + * + * Returns timestamp, or 0 on error. + */ +ktime_t ptp_get_timestamp(int index, + const struct skb_shared_hwtstamps *hwtstamps, + bool cycles); + /** * ptp_get_vclocks_index() - get all vclocks index on pclock, and * caller is responsible to free memory @@ -386,6 +412,10 @@ int ptp_get_vclocks_index(int pclock_index, int **vclock_index); */ ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index); #else +static inline ktime_t ptp_get_timestamp(int index, + const struct skb_shared_hwtstamps *hwtstamps, + bool cycles); +{ return 0; } static inline int ptp_get_vclocks_index(int pclock_index, int **vclock_index) { return 0; } static inline ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index f494ddbfc826..38929c113953 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -564,7 +564,10 @@ static inline bool skb_frag_must_loop(struct page *p) * &skb_shared_info. Use skb_hwtstamps() to get a pointer. */ struct skb_shared_hwtstamps { - ktime_t hwtstamp; + union { + ktime_t hwtstamp; + void *phc_data; + }; }; /* Definitions for tx_flags in struct skb_shared_info */ @@ -581,6 +584,9 @@ enum { /* generate hardware time stamp based on cycles if supported */ SKBTX_HW_TSTAMP_USE_CYCLES = 1 << 3, + /* call PHC to get actual hardware time stamp */ + SKBTX_HW_TSTAMP_PHC = 1 << 3, + /* generate wifi status information (where possible) */ SKBTX_WIFI_STATUS = 1 << 4, diff --git a/net/socket.c b/net/socket.c index 2e932c058002..fe765d559086 100644 --- a/net/socket.c +++ b/net/socket.c @@ -804,21 +804,17 @@ static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp) return skb->tstamp && !false_tstamp && skb_is_err_queue(skb); } -static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb) +static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb, + int if_index) { struct scm_ts_pktinfo ts_pktinfo; - struct net_device *orig_dev; if (!skb_mac_header_was_set(skb)) return; memset(&ts_pktinfo, 0, sizeof(ts_pktinfo)); - rcu_read_lock(); - orig_dev = dev_get_by_napi_id(skb_napi_id(skb)); - if (orig_dev) - ts_pktinfo.if_index = orig_dev->ifindex; - rcu_read_unlock(); + ts_pktinfo.if_index = if_index; ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb); put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO, @@ -838,6 +834,9 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, int empty = 1, false_tstamp = 0; struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); + struct net_device *orig_dev; + int if_index = 0; + int phc_index = -1; ktime_t hwtstamp; /* Race occurred between timestamp enabling and packet @@ -886,18 +885,32 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, if (shhwtstamps && (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) && !skb_is_swtx_tstamp(skb, false_tstamp)) { - if (sk->sk_tsflags & SOF_TIMESTAMPING_BIND_PHC) - hwtstamp = ptp_convert_timestamp(&shhwtstamps->hwtstamp, - sk->sk_bind_phc); + rcu_read_lock(); + orig_dev = dev_get_by_napi_id(skb_napi_id(skb)); + if (orig_dev) { + if_index = orig_dev->ifindex; + if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_PHC) + phc_index = ethtool_get_phc(orig_dev); + } + rcu_read_unlock(); + + if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_PHC) && + (phc_index != -1)) + hwtstamp = ptp_get_timestamp(phc_index, shhwtstamps, + sk->sk_tsflags & SOF_TIMESTAMPING_BIND_PHC); else hwtstamp = shhwtstamps->hwtstamp; + if (sk->sk_tsflags & SOF_TIMESTAMPING_BIND_PHC) + hwtstamp = ptp_convert_timestamp(&hwtstamp, + sk->sk_bind_phc); + if (ktime_to_timespec64_cond(hwtstamp, tss.ts + 2)) { empty = 0; if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) && !skb_is_err_queue(skb)) - put_ts_pktinfo(msg, skb); + put_ts_pktinfo(msg, skb, if_index); } } if (!empty) { From patchwork Tue Mar 22 21:07:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerhard Engleder X-Patchwork-Id: 12789037 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55F76C433F5 for ; Tue, 22 Mar 2022 21:08:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235152AbiCVVJ0 (ORCPT ); Tue, 22 Mar 2022 17:09:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235087AbiCVVJS (ORCPT ); Tue, 22 Mar 2022 17:09:18 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8F2D3E5F9 for ; Tue, 22 Mar 2022 14:07:49 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id k124-20020a1ca182000000b0038c9cf6e2a6so2702759wme.0 for ; Tue, 22 Mar 2022 14:07:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=engleder-embedded-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=r4mWBVMcX+Ym/GkYMe5nyJzvD+eaNrKowzj3i13n1h4=; b=VE/Bpo/a49xcsDbf/70gNF1Fc8f9+DSghlyytRzZwO9ga+f3qb91IcloszLjXjM40F 8Q/zYHzRuZduB+jNQ4TEkfY7oU+hY5IZdx4fWgXjdoIxeAnbO2srlk/OVv3+SmpeHT/b 6lgYa9UHnxyk9g6YweAqH5rNH+P9cMNVbPYKsPJcn2/soE/GBnsgqsLH47rHhyyTtYJq hWiN6MeM/l8SYCkhk0Fys11jNjlFySRwVNG5mhhod2gJ+pJOv5zlQ/0um4JYkLQupuRJ I40t7VirA6ytKRTphQhl6Jg+vbNz7foPvdACyAelNI+PfCo491ngs+8AXLvj+kjNtODK qcgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=r4mWBVMcX+Ym/GkYMe5nyJzvD+eaNrKowzj3i13n1h4=; b=sBDXmWUeO2ggMRcbaHUqM6dVZ7GU1HRk7j22KCHz/nhQJPyqrisfGN2OxAWn5WCD/S m8zEjHqTFPdYdBwtGhD3ICWJwhtMjZUGSOpC7z8d+Q/V40Qd3YzKETC33psr0OsWAoXX ulJQorq+evY/VIRNYOwWwr5VuMrpWOuz/OQon1dWIqt7c3HTTPahQHDcQxABVNw6k0yd QIbuLQlStYpalDMYwMgiuiTIiwgcdvdo3orFGfcdLWP1B0yDyWnD0hyB5fnaC44PfHvX abVQ5M7gNfm1R9V/M08oGKDBCw1o6pA4awHGlEIkk5kUsluXOVvtG9gTRj2v96Uh6PYP USKQ== X-Gm-Message-State: AOAM531+Cl4hWelngk2Op0Ek8LOMEqKpJxbiGTg7m6tsk0vNBr7hOyUH uUfvyOzohCMbBHEg6LrbVosvYA== X-Google-Smtp-Source: ABdhPJzCfG2mjYPHIZ+KXzxsIShToVyJS9flGedZGp8KeIue8PwJ6AzA0D0eW7fJyAItqw6tXwUJ2A== X-Received: by 2002:a1c:cc01:0:b0:37b:dcc8:7dfd with SMTP id h1-20020a1ccc01000000b0037bdcc87dfdmr5724873wmb.134.1647983268421; Tue, 22 Mar 2022 14:07:48 -0700 (PDT) Received: from hornet.engleder.at ([2001:871:23a:8366:6e3b:e5ff:fe2c:34c1]) by smtp.gmail.com with ESMTPSA id c7-20020a5d4f07000000b00203db8f13c6sm16281805wru.75.2022.03.22.14.07.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Mar 2022 14:07:48 -0700 (PDT) From: Gerhard Engleder To: richardcochran@gmail.com, yangbo.lu@nxp.com, davem@davemloft.net, kuba@kernel.org Cc: mlichvar@redhat.com, vinicius.gomes@intel.com, netdev@vger.kernel.org, Gerhard Engleder Subject: [PATCH net-next v1 6/6] tsnep: Add physical clock cycles support Date: Tue, 22 Mar 2022 22:07:22 +0100 Message-Id: <20220322210722.6405-7-gerhard@engleder-embedded.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20220322210722.6405-1-gerhard@engleder-embedded.com> References: <20220322210722.6405-1-gerhard@engleder-embedded.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org The TSN endpoint Ethernet MAC supports a free running counter additionally to its clock. This free running counter can be read and hardware timestamps are supported. As the name implies, this counter cannot be set and its frequency cannot be adjusted. Add cycles support based on free running counter to physical clock. This also requires hardware time stamps based on that free running counter. Signed-off-by: Gerhard Engleder --- drivers/net/ethernet/engleder/tsnep_hw.h | 9 ++++- drivers/net/ethernet/engleder/tsnep_main.c | 27 ++++++++----- drivers/net/ethernet/engleder/tsnep_ptp.c | 44 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/drivers/net/ethernet/engleder/tsnep_hw.h b/drivers/net/ethernet/engleder/tsnep_hw.h index 71cc8577d640..916ceac3ada2 100644 --- a/drivers/net/ethernet/engleder/tsnep_hw.h +++ b/drivers/net/ethernet/engleder/tsnep_hw.h @@ -43,6 +43,10 @@ #define ECM_RESET_CHANNEL 0x00000100 #define ECM_RESET_TXRX 0x00010000 +/* counter */ +#define ECM_COUNTER_LOW 0x0028 +#define ECM_COUNTER_HIGH 0x002C + /* control and status */ #define ECM_STATUS 0x0080 #define ECM_LINK_MODE_OFF 0x01000000 @@ -190,7 +194,8 @@ struct tsnep_tx_desc { /* tsnep TX descriptor writeback */ struct tsnep_tx_desc_wb { __le32 properties; - __le32 reserved1[3]; + __le32 reserved1; + __le64 counter; __le64 timestamp; __le32 dma_delay; __le32 reserved2; @@ -221,7 +226,7 @@ struct tsnep_rx_desc_wb { /* tsnep RX inline meta */ struct tsnep_rx_inline { - __le64 reserved; + __le64 counter; __le64 timestamp; }; diff --git a/drivers/net/ethernet/engleder/tsnep_main.c b/drivers/net/ethernet/engleder/tsnep_main.c index 904f3304727e..599776c6bd5e 100644 --- a/drivers/net/ethernet/engleder/tsnep_main.c +++ b/drivers/net/ethernet/engleder/tsnep_main.c @@ -441,6 +441,7 @@ static bool tsnep_tx_poll(struct tsnep_tx *tx, int napi_budget) unsigned long flags; int budget = 128; struct tsnep_tx_entry *entry; + struct skb_shared_info *shinfo; int count; spin_lock_irqsave(&tx->lock, flags); @@ -460,18 +461,26 @@ static bool tsnep_tx_poll(struct tsnep_tx *tx, int napi_budget) */ dma_rmb(); + shinfo = skb_shinfo(entry->skb); + count = 1; - if (skb_shinfo(entry->skb)->nr_frags > 0) - count += skb_shinfo(entry->skb)->nr_frags; + if (shinfo->nr_frags > 0) + count += shinfo->nr_frags; tsnep_tx_unmap(tx, count); - if ((skb_shinfo(entry->skb)->tx_flags & SKBTX_IN_PROGRESS) && + if ((shinfo->tx_flags & SKBTX_IN_PROGRESS) && (__le32_to_cpu(entry->desc_wb->properties) & TSNEP_DESC_EXTENDED_WRITEBACK_FLAG)) { - struct skb_shared_hwtstamps hwtstamps; - u64 timestamp = - __le64_to_cpu(entry->desc_wb->timestamp); + struct skb_shared_hwtstamps hwtstamps = {}; + u64 timestamp; + + if (shinfo->tx_flags & SKBTX_HW_TSTAMP_USE_CYCLES) + timestamp = + __le64_to_cpu(entry->desc_wb->counter); + else + timestamp = + __le64_to_cpu(entry->desc_wb->timestamp); memset(&hwtstamps, 0, sizeof(hwtstamps)); hwtstamps.hwtstamp = ns_to_ktime(timestamp); @@ -704,11 +713,11 @@ static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi, skb_hwtstamps(skb); struct tsnep_rx_inline *rx_inline = (struct tsnep_rx_inline *)skb->data; - u64 timestamp = - __le64_to_cpu(rx_inline->timestamp); + skb_shinfo(skb)->tx_flags |= + SKBTX_HW_TSTAMP_PHC; memset(hwtstamps, 0, sizeof(*hwtstamps)); - hwtstamps->hwtstamp = ns_to_ktime(timestamp); + hwtstamps->phc_data = rx_inline; } skb_pull(skb, TSNEP_RX_INLINE_METADATA_SIZE); skb->protocol = eth_type_trans(skb, diff --git a/drivers/net/ethernet/engleder/tsnep_ptp.c b/drivers/net/ethernet/engleder/tsnep_ptp.c index eaad453d487e..eb66dfa98242 100644 --- a/drivers/net/ethernet/engleder/tsnep_ptp.c +++ b/drivers/net/ethernet/engleder/tsnep_ptp.c @@ -175,6 +175,48 @@ static int tsnep_ptp_settime64(struct ptp_clock_info *ptp, return 0; } +static int tsnep_ptp_getcyclesx64(struct ptp_clock_info *ptp, + struct timespec64 *ts, + struct ptp_system_timestamp *sts) +{ + struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter, + ptp_clock_info); + u32 high_before; + u32 low; + u32 high; + u64 counter; + + /* read high dword twice to detect overrun */ + high = ioread32(adapter->addr + ECM_COUNTER_HIGH); + do { + ptp_read_system_prets(sts); + low = ioread32(adapter->addr + ECM_COUNTER_LOW); + ptp_read_system_postts(sts); + high_before = high; + high = ioread32(adapter->addr + ECM_COUNTER_HIGH); + } while (high != high_before); + counter = (((u64)high) << 32) | ((u64)low); + + *ts = ns_to_timespec64(counter); + + return 0; +} + +static ktime_t tsnep_ptp_gettstamp(struct ptp_clock_info *ptp, + const struct skb_shared_hwtstamps *hwtstamps, + bool cycles) +{ + struct tsnep_rx_inline *rx_inline = hwtstamps->phc_data; + u64 timestamp; + + if (cycles) + timestamp = __le64_to_cpu(rx_inline->counter); + else + timestamp = __le64_to_cpu(rx_inline->timestamp); + + return ns_to_ktime(timestamp); +} + int tsnep_ptp_init(struct tsnep_adapter *adapter) { int retval = 0; @@ -192,6 +234,8 @@ int tsnep_ptp_init(struct tsnep_adapter *adapter) adapter->ptp_clock_info.adjtime = tsnep_ptp_adjtime; adapter->ptp_clock_info.gettimex64 = tsnep_ptp_gettimex64; adapter->ptp_clock_info.settime64 = tsnep_ptp_settime64; + adapter->ptp_clock_info.getcyclesx64 = tsnep_ptp_getcyclesx64; + adapter->ptp_clock_info.gettstamp = tsnep_ptp_gettstamp; spin_lock_init(&adapter->ptp_lock);