From patchwork Fri May 21 04:36:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Y.b. Lu" X-Patchwork-Id: 446216 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBF2CC433ED for ; Fri, 21 May 2021 04:27:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8839660FE5 for ; Fri, 21 May 2021 04:27:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239129AbhEUE2q (ORCPT ); Fri, 21 May 2021 00:28:46 -0400 Received: from inva021.nxp.com ([92.121.34.21]:52428 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239050AbhEUE20 (ORCPT ); Fri, 21 May 2021 00:28:26 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 15DB4202EEE; Fri, 21 May 2021 06:26:29 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva021.eu-rdc02.nxp.com 15DB4202EEE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=nselector4; t=1621571189; bh=vTs5ZY6eg7HBoI/z05ojsDHNzx+U9X1CiogcZAGAv+4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ACeWwX+/2xLmHYhnxUc0o0cfG24tib7MSAvzVdCrSHsGRgcHVcyLxNOCjKfu+8oSi /IFEtCk5dJ81kF2DCh7h8DwBDD1aMSG9CVauYQYGYbRwqlb9YCHv/4tTGEFDKXkSy+ ZJ60Cu4TC5S47JJSL86v2kQij+HAYPI7jn5iIoIngklLmImWnVrSDzdeyXmLiqvHrt RGqWVBQo3Le5ej3vFxwU3s7Kw/YYBPXY8S4pEKfQMJp9hipi2cIzyiXM2Cwq23WE+R DnJy7QaEK4bGbrkTiR4Qo5Wh4EBjMgBoxDlFYEMn3QTF8MeBj4+3YrrG0Nfq+gfEen BpUZHVDlK9sAQ== Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 3717720067E; Fri, 21 May 2021 06:26:26 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva021.eu-rdc02.nxp.com 3717720067E Received: from localhost.localdomain (mega.ap.freescale.net [10.192.208.232]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id E3E774024D; Fri, 21 May 2021 12:26:22 +0800 (+08) From: Yangbo Lu To: netdev@vger.kernel.org Cc: Yangbo Lu , "David S . Miller" , Richard Cochran , Claudiu Manoil , Jakub Kicinski Subject: [net-next, v2, 1/7] ptp: add ptp virtual clock driver framework Date: Fri, 21 May 2021 12:36:13 +0800 Message-Id: <20210521043619.44694-2-yangbo.lu@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210521043619.44694-1-yangbo.lu@nxp.com> References: <20210521043619.44694-1-yangbo.lu@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This patch is to add ptp virtual clock driver framework which just exports essential APIs. A new member is added for ptp_clock_info structure. Device driver can provide initial cyclecounter info for ptp virtual clock via this member, before normally registering ptp clock. - struct ptp_vclock_cc *vclock_cc; PTP vclock register/unregister APIs are private for PTP driver. They can be called after normal ptp clock registering. - ptp_vclock_register() - ptp_vclock_unregister() And below API added is for device driver to get ptp_clock_info of registered physical clock through cyclecounter pointer of ptp virtual clock. This is needed for cyclecounter .read callback to read physical clock cycles. - ptp_get_pclock_info() Signed-off-by: Yangbo Lu --- Changes for v2: - Split from v1 patch #1. - Fixed build warning. - Updated copyright. --- MAINTAINERS | 6 ++ drivers/ptp/Makefile | 2 +- drivers/ptp/ptp_private.h | 23 +++++ drivers/ptp/ptp_vclock.c | 142 +++++++++++++++++++++++++++++++ include/linux/ptp_clock_kernel.h | 43 +++++++++- 5 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 drivers/ptp/ptp_vclock.c diff --git a/MAINTAINERS b/MAINTAINERS index bd7aff0c120f..13ef366e4ab4 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14818,6 +14818,12 @@ F: drivers/net/phy/dp83640* F: drivers/ptp/* F: include/linux/ptp_cl* +PTP VIRTUAL CLOCK SUPPORT +M: Yangbo Lu +L: netdev@vger.kernel.org +S: Maintained +F: drivers/ptp/ptp_vclock.c + PTRACE SUPPORT M: Oleg Nesterov S: Maintained diff --git a/drivers/ptp/Makefile b/drivers/ptp/Makefile index 8673d1743faa..3c6a905760e2 100644 --- a/drivers/ptp/Makefile +++ b/drivers/ptp/Makefile @@ -3,7 +3,7 @@ # Makefile for PTP 1588 clock support. # -ptp-y := ptp_clock.o ptp_chardev.o ptp_sysfs.o +ptp-y := ptp_clock.o ptp_vclock.o ptp_chardev.o ptp_sysfs.o ptp_kvm-$(CONFIG_X86) := ptp_kvm_x86.o ptp_kvm_common.o ptp_kvm-$(CONFIG_HAVE_ARM_SMCCC) := ptp_kvm_arm.o ptp_kvm_common.o obj-$(CONFIG_PTP_1588_CLOCK) += ptp.o diff --git a/drivers/ptp/ptp_private.h b/drivers/ptp/ptp_private.h index 6b97155148f1..870e54506781 100644 --- a/drivers/ptp/ptp_private.h +++ b/drivers/ptp/ptp_private.h @@ -3,6 +3,7 @@ * PTP 1588 clock support - private declarations for the core module. * * Copyright (C) 2010 OMICRON electronics GmbH + * Copyright 2021 NXP */ #ifndef _PTP_PRIVATE_H_ #define _PTP_PRIVATE_H_ @@ -48,6 +49,26 @@ struct ptp_clock { struct kthread_delayed_work aux_work; }; +#define info_to_vclock(d) container_of((d), struct ptp_vclock, info) +#define cc_to_vclock(d) container_of((d), struct ptp_vclock, cc) +#define dw_to_vclock(d) container_of((d), struct ptp_vclock, refresh_work) + +struct ptp_vclock { + struct ptp_clock *pclock; + struct ptp_clock_info info; + struct ptp_clock *clock; + + /* timecounter/cyclecounter definitions */ + struct cyclecounter cc; + struct timecounter tc; + spinlock_t lock; /* protects tc/cc */ + struct delayed_work refresh_work; + unsigned long refresh_interval; + u32 mult; + u32 mult_factor; + u32 div_factor; +}; + /* * The function queue_cnt() is safe for readers to call without * holding q->lock. Readers use this function to verify that the queue @@ -89,4 +110,6 @@ extern const struct attribute_group *ptp_groups[]; int ptp_populate_pin_groups(struct ptp_clock *ptp); void ptp_cleanup_pin_groups(struct ptp_clock *ptp); +struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock); +void ptp_vclock_unregister(struct ptp_vclock *vclock); #endif diff --git a/drivers/ptp/ptp_vclock.c b/drivers/ptp/ptp_vclock.c new file mode 100644 index 000000000000..70aae8696003 --- /dev/null +++ b/drivers/ptp/ptp_vclock.c @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * PTP virtual clock driver + * + * Copyright 2021 NXP + */ +#include +#include "ptp_private.h" + +static int ptp_vclock_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) +{ + struct ptp_vclock *vclock = info_to_vclock(ptp); + unsigned long flags; + s64 adj; + + adj = (s64)scaled_ppm * vclock->mult_factor; + adj = div_s64(adj, vclock->div_factor); + + spin_lock_irqsave(&vclock->lock, flags); + timecounter_read(&vclock->tc); + vclock->cc.mult = vclock->mult + adj; + spin_unlock_irqrestore(&vclock->lock, flags); + + return 0; +} + +static int ptp_vclock_adjtime(struct ptp_clock_info *ptp, s64 delta) +{ + struct ptp_vclock *vclock = info_to_vclock(ptp); + unsigned long flags; + + spin_lock_irqsave(&vclock->lock, flags); + timecounter_adjtime(&vclock->tc, delta); + spin_unlock_irqrestore(&vclock->lock, flags); + + return 0; +} + +static int ptp_vclock_gettime(struct ptp_clock_info *ptp, + struct timespec64 *ts) +{ + struct ptp_vclock *vclock = info_to_vclock(ptp); + unsigned long flags; + u64 ns; + + spin_lock_irqsave(&vclock->lock, flags); + ns = timecounter_read(&vclock->tc); + spin_unlock_irqrestore(&vclock->lock, flags); + *ts = ns_to_timespec64(ns); + + return 0; +} + +static int ptp_vclock_settime(struct ptp_clock_info *ptp, + const struct timespec64 *ts) +{ + struct ptp_vclock *vclock = info_to_vclock(ptp); + u64 ns = timespec64_to_ns(ts); + unsigned long flags; + + spin_lock_irqsave(&vclock->lock, flags); + timecounter_init(&vclock->tc, &vclock->cc, ns); + spin_unlock_irqrestore(&vclock->lock, flags); + + return 0; +} + +static const struct ptp_clock_info ptp_vclock_info = { + .owner = THIS_MODULE, + .name = "ptp virtual clock", + /* The maximum ppb value that long scaled_ppm can support */ + .max_adj = 32767999, + .adjfine = ptp_vclock_adjfine, + .adjtime = ptp_vclock_adjtime, + .gettime64 = ptp_vclock_gettime, + .settime64 = ptp_vclock_settime, +}; + +static void ptp_vclock_refresh(struct work_struct *work) +{ + struct delayed_work *dw = to_delayed_work(work); + struct ptp_vclock *vclock = dw_to_vclock(dw); + struct timespec64 ts; + + ptp_vclock_gettime(&vclock->info, &ts); + schedule_delayed_work(&vclock->refresh_work, vclock->refresh_interval); +} + +struct ptp_clock_info *ptp_get_pclock_info(const struct cyclecounter *cc) +{ + struct ptp_vclock *vclock = cc_to_vclock(cc); + + return vclock->pclock->info; +} +EXPORT_SYMBOL(ptp_get_pclock_info); + +struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock) +{ + struct ptp_vclock_cc *vclock_cc = pclock->info->vclock_cc; + struct ptp_vclock *vclock; + + vclock = kzalloc(sizeof(*vclock), GFP_KERNEL); + if (!vclock) + return NULL; + + vclock->pclock = pclock; + + vclock->info = ptp_vclock_info; + vclock->info.vclock_cc = vclock_cc; + snprintf(vclock->info.name, PTP_CLOCK_NAME_LEN, + "virtual clock on ptp%d", pclock->index); + + /* Copy members initial values of ptp_vclock_cc to ptp_vclock */ + vclock->cc = vclock_cc->cc; + vclock->mult = vclock_cc->cc.mult; + vclock->refresh_interval = vclock_cc->refresh_interval; + vclock->mult_factor = vclock_cc->mult_factor; + vclock->div_factor = vclock_cc->div_factor; + + spin_lock_init(&vclock->lock); + + vclock->clock = ptp_clock_register(&vclock->info, pclock->dev.parent); + if (IS_ERR_OR_NULL(vclock->clock)) { + kfree(vclock); + return NULL; + } + + timecounter_init(&vclock->tc, &vclock->cc, + ktime_to_ns(ktime_get_real())); + + INIT_DELAYED_WORK(&vclock->refresh_work, ptp_vclock_refresh); + schedule_delayed_work(&vclock->refresh_work, vclock->refresh_interval); + + return vclock; +} + +void ptp_vclock_unregister(struct ptp_vclock *vclock) +{ + cancel_delayed_work_sync(&vclock->refresh_work); + ptp_clock_unregister(vclock->clock); + kfree(vclock); +} diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h index a311bddd9e85..e4c1c6411e7d 100644 --- a/include/linux/ptp_clock_kernel.h +++ b/include/linux/ptp_clock_kernel.h @@ -3,6 +3,7 @@ * PTP 1588 clock support * * Copyright (C) 2010 OMICRON electronics GmbH + * Copyright 2021 NXP */ #ifndef _PTP_CLOCK_KERNEL_H_ @@ -11,7 +12,9 @@ #include #include #include +#include +#define PTP_CLOCK_NAME_LEN 32 /** * struct ptp_clock_request - request PTP clock event * @@ -48,6 +51,32 @@ struct ptp_system_timestamp { struct timespec64 post_ts; }; +/** + * struct ptp_vclock_cc - ptp virtual clock cycle counter info + * + * @cc: cyclecounter structure + * @refresh_interval: time interval to refresh time counter, to avoid 64-bit + * overflow during delta conversion. For example, with + * cc.mult value 2^28, there are 36 bits left of cycle + * counter. With 1 ns counter resolution, the overflow time + * is 2^36 ns which is 68.7 s. The refresh_interval may be + * (60 * HZ) less than 68.7 s. + * @mult_factor: parameter for cc.mult adjustment calculation, see below + * @div_factor: parameter for cc.mult adjustment calculation, see below + * + * scaled_ppm to adjustment of cc.mult + * + * adj = mult * (ppb / 10^9) + * = mult * (scaled_ppm * 1000 / 2^16) / 10^9 + * = scaled_ppm * mult_factor / div_factor + */ +struct ptp_vclock_cc { + struct cyclecounter cc; + unsigned long refresh_interval; + u32 mult_factor; + u32 div_factor; +}; + /** * struct ptp_clock_info - describes a PTP hardware clock * @@ -64,6 +93,8 @@ struct ptp_system_timestamp { * @pin_config: Array of length 'n_pins'. If the number of * programmable pins is nonzero, then drivers must * allocate and initialize this array. + * @vclock_cc: ptp_vclock_cc structure pointer. Provide initial cyclecounter + * info for ptp virtual clock. This is optional. * * clock operations * @@ -134,7 +165,7 @@ struct ptp_system_timestamp { struct ptp_clock_info { struct module *owner; - char name[16]; + char name[PTP_CLOCK_NAME_LEN]; s32 max_adj; int n_alarm; int n_ext_ts; @@ -142,6 +173,7 @@ struct ptp_clock_info { int n_pins; int pps; struct ptp_pin_desc *pin_config; + struct ptp_vclock_cc *vclock_cc; int (*adjfine)(struct ptp_clock_info *ptp, long scaled_ppm); int (*adjfreq)(struct ptp_clock_info *ptp, s32 delta); int (*adjphase)(struct ptp_clock_info *ptp, s32 phase); @@ -304,6 +336,12 @@ int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay); */ void ptp_cancel_worker_sync(struct ptp_clock *ptp); +/** + * ptp_get_pclock_info() - get ptp_clock_info pointer of physical clock + * + * @cc: cyclecounter pointer of ptp virtual clock. + */ +struct ptp_clock_info *ptp_get_pclock_info(const struct cyclecounter *cc); #else static inline struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, struct device *parent) @@ -324,6 +362,9 @@ static inline int ptp_schedule_worker(struct ptp_clock *ptp, static inline void ptp_cancel_worker_sync(struct ptp_clock *ptp) { } +static inline struct ptp_clock_info *ptp_get_pclock_info( + const struct cyclecounter *cc) +{ return NULL; } #endif static inline void ptp_read_system_prets(struct ptp_system_timestamp *sts) From patchwork Fri May 21 04:36:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Y.b. Lu" X-Patchwork-Id: 446218 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E2E4C433B4 for ; Fri, 21 May 2021 04:27:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 072A861261 for ; Fri, 21 May 2021 04:27:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239078AbhEUE2k (ORCPT ); Fri, 21 May 2021 00:28:40 -0400 Received: from inva021.nxp.com ([92.121.34.21]:52448 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239054AbhEUE20 (ORCPT ); Fri, 21 May 2021 00:28:26 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id CB426202EF3; Fri, 21 May 2021 06:26:29 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva021.eu-rdc02.nxp.com CB426202EF3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=nselector4; t=1621571189; bh=hoUYGy9nnJnFlkjcdb6m6crgv00+bIpL9Gcfu+ehN+A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SNHCiArtWYXEfh/D5qAs+f6T+oe9XbUT72e1VWWlcWIEzPQVtJP8UGsStnrAl6Nyz vJNGMwDJCzL43P3ESBdsACRWBVy84Syvvf908Lt/HYP5N5S4c1xGB30uNv5F6bx5jU 3vr6a5rws/zV1EZ7mKXPfc4fKLpZ2UJ0Jdqv4x1DOOKCV26miU+/AKMdW8aAi1eF0H E/Vn/TEeRNNCKR6HLSTPOvnBB2H7SChW0lkh14APoyp9BssgVdhjSg5IZRph/KAbCe MtgcDzcaLsWRKgo6/VoCZCN4pV58B/iTInXfnxBIUTX816n6ecamSir1FuLLTmEO0X pnubtSPZ/zvqQ== Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id ECAF42006B6; Fri, 21 May 2021 06:26:26 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva021.eu-rdc02.nxp.com ECAF42006B6 Received: from localhost.localdomain (mega.ap.freescale.net [10.192.208.232]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id A672640280; Fri, 21 May 2021 12:26:23 +0800 (+08) From: Yangbo Lu To: netdev@vger.kernel.org Cc: Yangbo Lu , "David S . Miller" , Richard Cochran , Claudiu Manoil , Jakub Kicinski Subject: [net-next, v2, 2/7] ptp: support ptp physical/virtual clocks conversion Date: Fri, 21 May 2021 12:36:14 +0800 Message-Id: <20210521043619.44694-3-yangbo.lu@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210521043619.44694-1-yangbo.lu@nxp.com> References: <20210521043619.44694-1-yangbo.lu@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Support ptp physical/virtual clocks conversion via sysfs. There will be a new attribute num_vclocks under ptp physical clock sysfs. - In default, the value is 0 meaning only ptp physical clock is in use. - Setting the value can create corresponding number of ptp virtual clocks to use. But current physical clock is guaranteed to stay free running. - Setting the value back to 0 can delete virtual clocks and back use physical clock again. Signed-off-by: Yangbo Lu --- Changes for v2: - Split from v1 patch #2. - Converted to num_vclocks for creating virtual clocks. - Guranteed physical clock free running when using virtual clocks. - Fixed build warning. - Updated copyright. --- Documentation/ABI/testing/sysfs-ptp | 13 +++++ drivers/ptp/ptp_clock.c | 11 ++++ drivers/ptp/ptp_private.h | 13 +++++ drivers/ptp/ptp_sysfs.c | 79 +++++++++++++++++++++++++++++ drivers/ptp/ptp_vclock.c | 1 + include/linux/ptp_clock_kernel.h | 1 + 6 files changed, 118 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-ptp b/Documentation/ABI/testing/sysfs-ptp index 2363ad810ddb..6403e746eeb4 100644 --- a/Documentation/ABI/testing/sysfs-ptp +++ b/Documentation/ABI/testing/sysfs-ptp @@ -61,6 +61,19 @@ Description: This file contains the number of programmable pins offered by the PTP hardware clock. +What: /sys/class/ptp/ptpN/num_vclocks +Date: May 2021 +Contact: Yangbo Lu +Description: + This file contains the ptp virtual clocks number in use, + based on current ptp physical clock. In default, the + value is 0 meaning only ptp physical clock is in use. + Setting the value can create corresponding number of ptp + virtual clocks to use. But current ptp physical clock is + guaranteed to stay free running. Setting the value back + to 0 can delete ptp virtual clocks and back use ptp + physical clock again. + What: /sys/class/ptp/ptpN/pins Date: March 2014 Contact: Richard Cochran diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c index a780435331c8..9b8ab1e6625f 100644 --- a/drivers/ptp/ptp_clock.c +++ b/drivers/ptp/ptp_clock.c @@ -3,6 +3,7 @@ * PTP 1588 clock support * * Copyright (C) 2010 OMICRON electronics GmbH + * Copyright 2021 NXP */ #include #include @@ -76,6 +77,11 @@ 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_guarantee_pclock(ptp)) { + pr_err("ptp: virtual clock in use, guarantee physical clock free running\n"); + return -EBUSY; + } + return ptp->info->settime64(ptp->info, tp); } @@ -97,6 +103,11 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) struct ptp_clock_info *ops; int err = -EOPNOTSUPP; + if (ptp_guarantee_pclock(ptp)) { + pr_err("ptp: virtual clock in use, guarantee physical clock free running\n"); + return -EBUSY; + } + ops = ptp->info; if (tx->modes & ADJ_SETOFFSET) { diff --git a/drivers/ptp/ptp_private.h b/drivers/ptp/ptp_private.h index 870e54506781..da24d0c83799 100644 --- a/drivers/ptp/ptp_private.h +++ b/drivers/ptp/ptp_private.h @@ -47,6 +47,7 @@ struct ptp_clock { const struct attribute_group *pin_attr_groups[2]; struct kthread_worker *kworker; struct kthread_delayed_work aux_work; + u8 num_vclocks; }; #define info_to_vclock(d) container_of((d), struct ptp_vclock, info) @@ -82,6 +83,18 @@ static inline int queue_cnt(struct timestamp_event_queue *q) return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt; } +/* + * Guarantee physical clock to stay free running, if ptp virtual clocks + * on it are in use. + */ +static inline bool ptp_guarantee_pclock(struct ptp_clock *ptp) +{ + if (!ptp->info->vclock_flag && ptp->num_vclocks) + return true; + + return false; +} + /* * see ptp_chardev.c */ diff --git a/drivers/ptp/ptp_sysfs.c b/drivers/ptp/ptp_sysfs.c index be076a91e20e..5e1b5947dbff 100644 --- a/drivers/ptp/ptp_sysfs.c +++ b/drivers/ptp/ptp_sysfs.c @@ -3,6 +3,7 @@ * PTP 1588 clock support - sysfs interface. * * Copyright (C) 2010 OMICRON electronics GmbH + * Copyright 2021 NXP */ #include #include @@ -148,6 +149,80 @@ static ssize_t pps_enable_store(struct device *dev, } static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store); +static int unregister_vclock(struct device *dev, void *data) +{ + struct ptp_clock *ptp = dev_get_drvdata(dev); + struct ptp_clock_info *info = ptp->info; + struct ptp_vclock *vclock; + u8 *num = data; + + if (info->vclock_flag) { + vclock = info_to_vclock(info); + dev_info(&vclock->pclock->dev, "delete virtual clock ptp%d\n", + vclock->clock->index); + ptp_vclock_unregister(vclock); + (*num)--; + } + + /* For break. Not error. */ + if (*num == 0) + return -EINVAL; + + return 0; +} + +static ssize_t num_vclocks_show(struct device *dev, + struct device_attribute *attr, char *page) +{ + struct ptp_clock *ptp = dev_get_drvdata(dev); + + return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->num_vclocks); +} + +static ssize_t num_vclocks_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct ptp_clock *ptp = dev_get_drvdata(dev); + struct ptp_vclock *vclock; + int err = -EINVAL; + u8 num, i; + + if (kstrtou8(buf, 0, &num)) + goto out; + + /* Need to create more vclocks */ + if (num > ptp->num_vclocks) { + for (i = 0; i < num - ptp->num_vclocks; i++) { + vclock = ptp_vclock_register(ptp); + if (!vclock) + goto out; + + dev_info(dev, "new virtual clock ptp%d\n", + vclock->clock->index); + } + } + + /* Need to delete vclocks */ + if (num < ptp->num_vclocks) { + i = ptp->num_vclocks - num; + device_for_each_child_reverse(dev->parent, &i, + unregister_vclock); + } + + if (num == 0) + dev_info(dev, "only physical clock in use now\n"); + else + dev_info(dev, "guarantee physical clock free running\n"); + + ptp->num_vclocks = num; + + return count; +out: + return err; +} +static DEVICE_ATTR_RW(num_vclocks); + static struct attribute *ptp_attrs[] = { &dev_attr_clock_name.attr, @@ -162,6 +237,7 @@ static struct attribute *ptp_attrs[] = { &dev_attr_fifo.attr, &dev_attr_period.attr, &dev_attr_pps_enable.attr, + &dev_attr_num_vclocks.attr, NULL }; @@ -183,6 +259,9 @@ static umode_t ptp_is_attribute_visible(struct kobject *kobj, } else if (attr == &dev_attr_pps_enable.attr) { if (!info->pps) mode = 0; + } else if (attr == &dev_attr_num_vclocks.attr) { + if (info->vclock_flag || !info->vclock_cc) + mode = 0; } return mode; diff --git a/drivers/ptp/ptp_vclock.c b/drivers/ptp/ptp_vclock.c index 70aae8696003..fce09f10ae69 100644 --- a/drivers/ptp/ptp_vclock.c +++ b/drivers/ptp/ptp_vclock.c @@ -68,6 +68,7 @@ static int ptp_vclock_settime(struct ptp_clock_info *ptp, static const struct ptp_clock_info ptp_vclock_info = { .owner = THIS_MODULE, .name = "ptp virtual clock", + .vclock_flag = true, /* The maximum ppb value that long scaled_ppm can support */ .max_adj = 32767999, .adjfine = ptp_vclock_adjfine, diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h index e4c1c6411e7d..8f34f192f2cf 100644 --- a/include/linux/ptp_clock_kernel.h +++ b/include/linux/ptp_clock_kernel.h @@ -174,6 +174,7 @@ struct ptp_clock_info { int pps; struct ptp_pin_desc *pin_config; struct ptp_vclock_cc *vclock_cc; + bool vclock_flag; int (*adjfine)(struct ptp_clock_info *ptp, long scaled_ppm); int (*adjfreq)(struct ptp_clock_info *ptp, s32 delta); int (*adjphase)(struct ptp_clock_info *ptp, s32 phase); From patchwork Fri May 21 04:36:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Y.b. Lu" X-Patchwork-Id: 446217 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E1B9C433B4 for ; Fri, 21 May 2021 04:27:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8488961353 for ; Fri, 21 May 2021 04:27:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239089AbhEUE2o (ORCPT ); Fri, 21 May 2021 00:28:44 -0400 Received: from inva021.nxp.com ([92.121.34.21]:52466 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239056AbhEUE20 (ORCPT ); Fri, 21 May 2021 00:28:26 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 90E22202EF4; Fri, 21 May 2021 06:26:30 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva021.eu-rdc02.nxp.com 90E22202EF4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=nselector4; t=1621571190; bh=JXllx/O7GZHB4piX09sK6Og9wRNio96Vg2ZN46xlKNA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FR2bTUTks7aTNCVyibTrYJgBOtUaRZJ+1+ZUj2Qw7wWD5TEb4zy0hHwW+oMS6wZjK aDboBqBl0mydTwxSYT6K1kUO1Ppkhrp0rle6bOD5KaZxREPRo/3pjgBEl5Zm9Y0doU 6FU9a+qjmGZmvG8rTf0cjCY30Kifoijwqw+sA7EsdCMHUgKpryjF1LiTLr/ImZGRWr IeqvDigNs/is7lajTdrIxHuJNryUA9BYNpeJetvHPdOWO2mzaM/g84Aa+KcCiOi79r H9HwOTIpUa4rfUarpU6d3ZlfLdftlVK9S972xPu0qKI9WKkUI/pkAOPtRRwEOEPjuv lQMzf6/YJTmKg== Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id B1CE9202EEF; Fri, 21 May 2021 06:26:27 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva021.eu-rdc02.nxp.com B1CE9202EEF Received: from localhost.localdomain (mega.ap.freescale.net [10.192.208.232]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id 69CCA40299; Fri, 21 May 2021 12:26:24 +0800 (+08) From: Yangbo Lu To: netdev@vger.kernel.org Cc: Yangbo Lu , "David S . Miller" , Richard Cochran , Claudiu Manoil , Jakub Kicinski Subject: [net-next, v2, 3/7] ptp: support domains and timestamp conversion Date: Fri, 21 May 2021 12:36:15 +0800 Message-Id: <20210521043619.44694-4-yangbo.lu@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210521043619.44694-1-yangbo.lu@nxp.com> References: <20210521043619.44694-1-yangbo.lu@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org PTP virtual clocks support is actually for multiple domains synchronization. This patch is to support configuring domain value for PTP virtual clock via sysfs and to support hardware timestamp conversion to domain time. The idea is driver identifying PTP message domain number, matching with PTP virtual clock which has same domain number configured, and then converting to the PTP virtual clock time. The identifying could be in MAC driver, the matching and converting is through the API this patch added which can be called by MAC driver, - ptp_clock_domain_tstamp() Different domain values should be configured for multiple ptp virtual clocks based on same one free running ptp physical clock. If ptp message domain value has no PTP clock matched, the original hardware timestamp will be used. Signed-off-by: Yangbo Lu --- Changes for v2: - Split from v1 patch #1 and #2. - Fixed build warning. --- Documentation/ABI/testing/sysfs-ptp | 12 +++++++ drivers/ptp/ptp_clock.c | 1 + drivers/ptp/ptp_private.h | 6 ++++ drivers/ptp/ptp_sysfs.c | 55 +++++++++++++++++++++++++++++ drivers/ptp/ptp_vclock.c | 33 +++++++++++++++++ include/linux/ptp_clock_kernel.h | 13 +++++++ 6 files changed, 120 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-ptp b/Documentation/ABI/testing/sysfs-ptp index 6403e746eeb4..7a518185cb11 100644 --- a/Documentation/ABI/testing/sysfs-ptp +++ b/Documentation/ABI/testing/sysfs-ptp @@ -25,6 +25,18 @@ Description: MAC based ones. The string does not necessarily have to be any kind of unique id. +What: /sys/class/ptp/ptpN/domain +Date: May 2021 +Contact: Yangbo Lu +Description: + This file contains the domain value that the PTP virtual + clock serves. PTP virtual clock will provide timestamp + to PTP messages which have same domain matched. + Write value 0 ~ 255 into this file to change the domain. + Writing -1 means serving no domain. If PTP message domain + value does not have a PTP virtual clock matched, the + original hardware time stamp will be used. + What: /sys/class/ptp/ptpN/max_adjustment Date: September 2010 Contact: Richard Cochran diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c index 9b8ab1e6625f..4ad20b2aae57 100644 --- a/drivers/ptp/ptp_clock.c +++ b/drivers/ptp/ptp_clock.c @@ -216,6 +216,7 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, ptp->info = info; ptp->devid = MKDEV(major, index); ptp->index = index; + ptp->domain = -1; /* No domain set */ spin_lock_init(&ptp->tsevq.lock); mutex_init(&ptp->tsevq_mux); mutex_init(&ptp->pincfg_mux); diff --git a/drivers/ptp/ptp_private.h b/drivers/ptp/ptp_private.h index da24d0c83799..fe8f976b7b75 100644 --- a/drivers/ptp/ptp_private.h +++ b/drivers/ptp/ptp_private.h @@ -48,6 +48,7 @@ struct ptp_clock { struct kthread_worker *kworker; struct kthread_delayed_work aux_work; u8 num_vclocks; + int16_t domain; }; #define info_to_vclock(d) container_of((d), struct ptp_vclock, info) @@ -70,6 +71,11 @@ struct ptp_vclock { u32 div_factor; }; +struct domain_tstamp { + u64 tstamp; + u8 domain; +}; + /* * The function queue_cnt() is safe for readers to call without * holding q->lock. Readers use this function to verify that the queue diff --git a/drivers/ptp/ptp_sysfs.c b/drivers/ptp/ptp_sysfs.c index 5e1b5947dbff..b854661aadeb 100644 --- a/drivers/ptp/ptp_sysfs.c +++ b/drivers/ptp/ptp_sysfs.c @@ -223,6 +223,57 @@ static ssize_t num_vclocks_store(struct device *dev, } static DEVICE_ATTR_RW(num_vclocks); +static int check_domain_avail(struct device *dev, void *data) +{ + struct ptp_clock *ptp = dev_get_drvdata(dev); + int16_t *domain = data; + + if (ptp->domain == *domain) + return -EINVAL; + + return 0; +} + +static ssize_t domain_show(struct device *dev, + struct device_attribute *attr, char *page) +{ + struct ptp_clock *ptp = dev_get_drvdata(dev); + + return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->domain); +} + +static ssize_t domain_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct ptp_clock *ptp = dev_get_drvdata(dev); + int err = -EINVAL; + int16_t domain; + + if (kstrtos16(buf, 0, &domain)) + goto out; + + if (domain > 255 || domain < -1) + goto out; + + if (domain == -1) { + ptp->domain = -1; + return count; + } + + if (device_for_each_child(dev->parent, &domain, check_domain_avail)) { + dev_err(dev, "the domain value already in used\n"); + goto out; + } + + ptp->domain = domain; + + return count; +out: + return err; +} +static DEVICE_ATTR_RW(domain); + static struct attribute *ptp_attrs[] = { &dev_attr_clock_name.attr, @@ -238,6 +289,7 @@ static struct attribute *ptp_attrs[] = { &dev_attr_period.attr, &dev_attr_pps_enable.attr, &dev_attr_num_vclocks.attr, + &dev_attr_domain.attr, NULL }; @@ -262,6 +314,9 @@ static umode_t ptp_is_attribute_visible(struct kobject *kobj, } else if (attr == &dev_attr_num_vclocks.attr) { if (info->vclock_flag || !info->vclock_cc) mode = 0; + } else if (attr == &dev_attr_domain.attr) { + if (!info->vclock_flag) + mode = 0; } return mode; diff --git a/drivers/ptp/ptp_vclock.c b/drivers/ptp/ptp_vclock.c index fce09f10ae69..6e7d1403d024 100644 --- a/drivers/ptp/ptp_vclock.c +++ b/drivers/ptp/ptp_vclock.c @@ -87,6 +87,39 @@ static void ptp_vclock_refresh(struct work_struct *work) schedule_delayed_work(&vclock->refresh_work, vclock->refresh_interval); } +static int ptp_convert_domain_tstamp(struct device *dev, void *data) +{ + struct ptp_clock *ptp = dev_get_drvdata(dev); + struct ptp_clock_info *info = ptp->info; + struct domain_tstamp *domain_ts = data; + struct ptp_vclock *vclock; + unsigned long flags; + + /* Convert to domain tstamp if there is a domain matched */ + if (ptp->domain == domain_ts->domain) { + vclock = info_to_vclock(info); + spin_lock_irqsave(&vclock->lock, flags); + domain_ts->tstamp = timecounter_cyc2time(&vclock->tc, + domain_ts->tstamp); + spin_unlock_irqrestore(&vclock->lock, flags); + return -EINVAL; /* For break. Not error. */ + } + + return 0; +} + +void ptp_clock_domain_tstamp(struct device *ptp_dev, u64 *tstamp, u8 domain) +{ + struct domain_tstamp domain_ts; + + domain_ts.tstamp = *tstamp; + domain_ts.domain = domain; + + device_for_each_child(ptp_dev, &domain_ts, ptp_convert_domain_tstamp); + *tstamp = domain_ts.tstamp; +} +EXPORT_SYMBOL(ptp_clock_domain_tstamp); + struct ptp_clock_info *ptp_get_pclock_info(const struct cyclecounter *cc) { struct ptp_vclock *vclock = cc_to_vclock(cc); diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h index 8f34f192f2cf..edc816cdfb2c 100644 --- a/include/linux/ptp_clock_kernel.h +++ b/include/linux/ptp_clock_kernel.h @@ -343,6 +343,15 @@ void ptp_cancel_worker_sync(struct ptp_clock *ptp); * @cc: cyclecounter pointer of ptp virtual clock. */ struct ptp_clock_info *ptp_get_pclock_info(const struct cyclecounter *cc); + +/** + * ptp_clock_domain_tstamp() - convert to domain timestamp + * + * @ptp_dev: device pointer of current ptp clock. + * @tstamp: time stamp pointer to hardware time stamp + * @domain: domain number to convert + */ +void ptp_clock_domain_tstamp(struct device *ptp_dev, u64 *tstamp, u8 domain); #else static inline struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, struct device *parent) @@ -366,6 +375,10 @@ static inline void ptp_cancel_worker_sync(struct ptp_clock *ptp) static inline struct ptp_clock_info *ptp_get_pclock_info( const struct cyclecounter *cc) { return NULL; } + +static inline void ptp_clock_domain_tstamp(struct device *dev, u64 *tstamp, + u8 domain) +{ } #endif static inline void ptp_read_system_prets(struct ptp_system_timestamp *sts) From patchwork Fri May 21 04:36:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Y.b. Lu" X-Patchwork-Id: 445145 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA5AFC43460 for ; Fri, 21 May 2021 04:27:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B02DD61261 for ; Fri, 21 May 2021 04:27:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239098AbhEUE2p (ORCPT ); Fri, 21 May 2021 00:28:45 -0400 Received: from inva020.nxp.com ([92.121.34.13]:47116 "EHLO inva020.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239057AbhEUE20 (ORCPT ); Fri, 21 May 2021 00:28:26 -0400 Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 279681A042E; Fri, 21 May 2021 06:26:31 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva020.eu-rdc02.nxp.com 279681A042E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=nselector3; t=1621571191; bh=z3u4vjghb1aCcF5nu/LY7fkYl80SW2eGPBiznkYvWSc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WfMOan5zBO21MecgpAgSox8Djo/AgdT9zWJ2A1/KiPDwQ3avWu75Fxvv8LiUBIgTJ LoBV6E14+C017JlBWuGhPf2QdAaoEeE87Fx7m78zXm1qnRTTmGIQLbdRF4dQKu2N9J 48Ah4oDVSlbxm0W/QWeZbINHqb++zOsiSi1gZE/zAaHb41wSRFx2WTgLObhlHu51Jx 1z6BUauyE093Kn8fg+jNDRAJFTWsOCWrg+fbvgeI7U8Jh3137DIUOfIoDEi8HbKD2j 3ttXMUB0LVSz+h1XaNGsPUmP62qIaualGQN0w0IEtIK8m97+ZNWv+wxVwRZA/xYz9w 7/0zFoCk9pJZw== Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 73F811A010C; Fri, 21 May 2021 06:26:28 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva020.eu-rdc02.nxp.com 73F811A010C Received: from localhost.localdomain (mega.ap.freescale.net [10.192.208.232]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id 2D4C34024E; Fri, 21 May 2021 12:26:25 +0800 (+08) From: Yangbo Lu To: netdev@vger.kernel.org Cc: Yangbo Lu , "David S . Miller" , Richard Cochran , Claudiu Manoil , Jakub Kicinski Subject: [net-next, v2, 4/7] ptp_qoriq: export ptp clock reading function for cyclecounter Date: Fri, 21 May 2021 12:36:16 +0800 Message-Id: <20210521043619.44694-5-yangbo.lu@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210521043619.44694-1-yangbo.lu@nxp.com> References: <20210521043619.44694-1-yangbo.lu@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Export ptp clock reading function for cyclecounter to read cycles. Signed-off-by: Yangbo Lu --- Changes for v2: - Updated copyright. --- drivers/ptp/ptp_qoriq.c | 16 ++++++++++++++++ include/linux/fsl/ptp_qoriq.h | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/drivers/ptp/ptp_qoriq.c b/drivers/ptp/ptp_qoriq.c index 08f4cf0ad9e3..2bcbe9da2a29 100644 --- a/drivers/ptp/ptp_qoriq.c +++ b/drivers/ptp/ptp_qoriq.c @@ -3,6 +3,7 @@ * PTP 1588 clock for Freescale QorIQ 1588 timer * * Copyright (C) 2010 OMICRON electronics GmbH + * Copyright 2021 NXP */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt @@ -311,6 +312,21 @@ int ptp_qoriq_enable(struct ptp_clock_info *ptp, } EXPORT_SYMBOL_GPL(ptp_qoriq_enable); +u64 ptp_qoriq_clock_read(const struct cyclecounter *cc) +{ + struct ptp_clock_info *ptp = ptp_get_pclock_info(cc); + struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps); + unsigned long flags; + u64 ns; + + spin_lock_irqsave(&ptp_qoriq->lock, flags); + ns = tmr_cnt_read(ptp_qoriq); + spin_unlock_irqrestore(&ptp_qoriq->lock, flags); + + return ns; +} +EXPORT_SYMBOL_GPL(ptp_qoriq_clock_read); + static const struct ptp_clock_info ptp_qoriq_caps = { .owner = THIS_MODULE, .name = "qoriq ptp clock", diff --git a/include/linux/fsl/ptp_qoriq.h b/include/linux/fsl/ptp_qoriq.h index 01acebe37fab..4590e70f1afb 100644 --- a/include/linux/fsl/ptp_qoriq.h +++ b/include/linux/fsl/ptp_qoriq.h @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2010 OMICRON electronics GmbH - * Copyright 2018 NXP + * Copyright 2018-2021 NXP */ #ifndef __PTP_QORIQ_H__ #define __PTP_QORIQ_H__ @@ -193,6 +193,7 @@ int ptp_qoriq_settime(struct ptp_clock_info *ptp, const struct timespec64 *ts); int ptp_qoriq_enable(struct ptp_clock_info *ptp, struct ptp_clock_request *rq, int on); +u64 ptp_qoriq_clock_read(const struct cyclecounter *cc); int extts_clean_up(struct ptp_qoriq *ptp_qoriq, int index, bool update_event); #ifdef CONFIG_DEBUG_FS void ptp_qoriq_create_debugfs(struct ptp_qoriq *ptp_qoriq); From patchwork Fri May 21 04:36:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Y.b. Lu" X-Patchwork-Id: 446219 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B69BEC433ED for ; Fri, 21 May 2021 04:27:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8F93760FE5 for ; Fri, 21 May 2021 04:27:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239075AbhEUE2h (ORCPT ); Fri, 21 May 2021 00:28:37 -0400 Received: from inva021.nxp.com ([92.121.34.21]:52486 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239062AbhEUE2Z (ORCPT ); Fri, 21 May 2021 00:28:25 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 451292006B6; Fri, 21 May 2021 06:26:32 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva021.eu-rdc02.nxp.com 451292006B6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=nselector4; t=1621571192; bh=vSH6qe4RRrXv5rg1WC3/BTKNnR2jOaq7vgUi0iNwVKY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Rlqi52AiJNzKyTNEkrYMZhoG7T6/Kg6OEHi+YtWABE7L1XCnmUiMJotXQGyjJURlw vW3hjyCJGMuTFBV83Oy+lC2OiiQFMpKAr/bxdPojZA11eKLSCyg4/49/urYna7sGvh e20hH5N21lGcBVE335+Lchl5JZDB6l0wsN2g5yrW3e64EDtsBsx5C8HtpFbDX7RGDu hGZVPXnkfGbCIlzK9eryqqYS+fKFTugLDapGWJSf2pdRyjFpRc0guBnESsTT0g54MX hhbGL4JVIrCWIcjjVMCQ7mssgA82k8bUcCjY+By1E92rFum2w65PgojYKFOuA/r5BE EGXTDfg9aLpJA== Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 8CAFD20067E; Fri, 21 May 2021 06:26:29 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva021.eu-rdc02.nxp.com 8CAFD20067E Received: from localhost.localdomain (mega.ap.freescale.net [10.192.208.232]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id E4E3940243; Fri, 21 May 2021 12:26:25 +0800 (+08) From: Yangbo Lu To: netdev@vger.kernel.org Cc: Yangbo Lu , "David S . Miller" , Richard Cochran , Claudiu Manoil , Jakub Kicinski Subject: [net-next, v2, 5/7] enetc_ptp: support ptp virtual clock Date: Fri, 21 May 2021 12:36:17 +0800 Message-Id: <20210521043619.44694-6-yangbo.lu@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210521043619.44694-1-yangbo.lu@nxp.com> References: <20210521043619.44694-1-yangbo.lu@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add support for ptp virtual clock. Signed-off-by: Yangbo Lu --- Changes for v2: - Made ptp_qoriq_vclock_cc static. - Updated copyright. --- drivers/net/ethernet/freescale/enetc/enetc_ptp.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/net/ethernet/freescale/enetc/enetc_ptp.c b/drivers/net/ethernet/freescale/enetc/enetc_ptp.c index bc594892507a..dabcdaf972a9 100644 --- a/drivers/net/ethernet/freescale/enetc/enetc_ptp.c +++ b/drivers/net/ethernet/freescale/enetc/enetc_ptp.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -/* Copyright 2019 NXP */ +/* Copyright 2019-2021 NXP */ #include #include @@ -10,6 +10,16 @@ int enetc_phc_index = -1; EXPORT_SYMBOL(enetc_phc_index); +static struct ptp_vclock_cc ptp_qoriq_vclock_cc = { + .cc.read = ptp_qoriq_clock_read, + .cc.mask = CYCLECOUNTER_MASK(64), + .cc.shift = 28, + .cc.mult = (1 << 28), + .refresh_interval = (HZ * 60), + .mult_factor = (1 << 6), + .div_factor = 15625, +}; + static struct ptp_clock_info enetc_ptp_caps = { .owner = THIS_MODULE, .name = "ENETC PTP clock", @@ -24,6 +34,7 @@ static struct ptp_clock_info enetc_ptp_caps = { .gettime64 = ptp_qoriq_gettime, .settime64 = ptp_qoriq_settime, .enable = ptp_qoriq_enable, + .vclock_cc = &ptp_qoriq_vclock_cc, }; static int enetc_ptp_probe(struct pci_dev *pdev, From patchwork Fri May 21 04:36:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Y.b. Lu" X-Patchwork-Id: 445147 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9084EC43461 for ; Fri, 21 May 2021 04:27:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7080660FE5 for ; Fri, 21 May 2021 04:27:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239073AbhEUE2l (ORCPT ); Fri, 21 May 2021 00:28:41 -0400 Received: from inva020.nxp.com ([92.121.34.13]:47152 "EHLO inva020.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239063AbhEUE20 (ORCPT ); Fri, 21 May 2021 00:28:26 -0400 Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 2C6BD1A010C; Fri, 21 May 2021 06:26:33 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva020.eu-rdc02.nxp.com 2C6BD1A010C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=nselector3; t=1621571193; bh=Ob6/fAWbo5qLxnUfVt5H5XofuGBwTloUXc68drPWfQM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mq3raBtGXqXpdfWka9TdtFPnuLA8djtlgUXplYBCCc7Fvg17fL4E5Ioy5dHgwpEMy WzDUcF1fsU4vKr/rlPJI+P+Gn/9QKNBYqBba9oYXYXui0d7rChU4zm94ggLN8lDwOG MizguzaemDaScAFntMjeUZDch+zvj5602dijh5k974puqQIg1fv2w9v12v6XkZ5oCY DWNPHczOMD6gUIIJsARh3vJ+mmIKLAmSMOOWGb/7JqsHKpKl9ZS2UVmV9uAI0m0e2Y KidDDpsPMcSRhtEnkquKFxaOJyq9fLtfdU/afR2yXELoQCfIz90+mLOf+WTpl+wcGd 38Fp0inDjCN2w== Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 4F38E1A0401; Fri, 21 May 2021 06:26:30 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva020.eu-rdc02.nxp.com 4F38E1A0401 Received: from localhost.localdomain (mega.ap.freescale.net [10.192.208.232]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id A75C3402A7; Fri, 21 May 2021 12:26:26 +0800 (+08) From: Yangbo Lu To: netdev@vger.kernel.org Cc: Yangbo Lu , "David S . Miller" , Richard Cochran , Claudiu Manoil , Jakub Kicinski Subject: [net-next, v2, 6/7] enetc: store ptp device pointer Date: Fri, 21 May 2021 12:36:18 +0800 Message-Id: <20210521043619.44694-7-yangbo.lu@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210521043619.44694-1-yangbo.lu@nxp.com> References: <20210521043619.44694-1-yangbo.lu@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Store ptp device pointer which will be used for ptp domain timestamp conversion. Signed-off-by: Yangbo Lu --- Changes for v2: - Made sure ptp device was got. - Update copyright. --- drivers/net/ethernet/freescale/enetc/enetc.h | 3 ++- drivers/net/ethernet/freescale/enetc/enetc_pf.c | 14 +++++++++++++- drivers/net/ethernet/freescale/enetc/enetc_vf.c | 14 +++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/drivers/net/ethernet/freescale/enetc/enetc.h b/drivers/net/ethernet/freescale/enetc/enetc.h index 08b283347d9c..925f2a96e375 100644 --- a/drivers/net/ethernet/freescale/enetc/enetc.h +++ b/drivers/net/ethernet/freescale/enetc/enetc.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */ -/* Copyright 2017-2019 NXP */ +/* Copyright 2017-2021 NXP */ #include #include @@ -351,6 +351,7 @@ struct enetc_ndev_priv { struct work_struct tx_onestep_tstamp; struct sk_buff_head tx_skbs; + struct device *ptp_dev; }; /* Messaging */ diff --git a/drivers/net/ethernet/freescale/enetc/enetc_pf.c b/drivers/net/ethernet/freescale/enetc/enetc_pf.c index 31274325159a..994a4d3b715e 100644 --- a/drivers/net/ethernet/freescale/enetc/enetc_pf.c +++ b/drivers/net/ethernet/freescale/enetc/enetc_pf.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -/* Copyright 2017-2019 NXP */ +/* Copyright 2017-2021 NXP */ #include #include @@ -1201,6 +1201,7 @@ static int enetc_pf_probe(struct pci_dev *pdev, { struct device_node *node = pdev->dev.of_node; struct enetc_ndev_priv *priv; + struct pci_dev *ptp_pdev; struct net_device *ndev; struct enetc_si *si; struct enetc_pf *pf; @@ -1293,6 +1294,16 @@ static int enetc_pf_probe(struct pci_dev *pdev, goto err_alloc_msix; } + ptp_pdev = pci_get_device(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_PTP, + NULL); + if (!ptp_pdev) { + dev_err(&pdev->dev, "no PTP device found\n"); + err = -ENODEV; + goto err_get_ptp; + } + + priv->ptp_dev = &ptp_pdev->dev; + if (!of_get_phy_mode(node, &pf->if_mode)) { err = enetc_mdiobus_create(pf, node); if (err) @@ -1310,6 +1321,7 @@ static int enetc_pf_probe(struct pci_dev *pdev, return 0; err_reg_netdev: +err_get_ptp: enetc_phylink_destroy(priv); err_phylink_create: enetc_mdiobus_destroy(pf); diff --git a/drivers/net/ethernet/freescale/enetc/enetc_vf.c b/drivers/net/ethernet/freescale/enetc/enetc_vf.c index 03090ba7e226..ca1deb7d4dcb 100644 --- a/drivers/net/ethernet/freescale/enetc/enetc_vf.c +++ b/drivers/net/ethernet/freescale/enetc/enetc_vf.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -/* Copyright 2017-2019 NXP */ +/* Copyright 2017-2021 NXP */ #include #include "enetc.h" @@ -138,6 +138,7 @@ static int enetc_vf_probe(struct pci_dev *pdev, const struct pci_device_id *ent) { struct enetc_ndev_priv *priv; + struct pci_dev *ptp_pdev; struct net_device *ndev; struct enetc_si *si; int err; @@ -188,6 +189,16 @@ static int enetc_vf_probe(struct pci_dev *pdev, goto err_alloc_msix; } + ptp_pdev = pci_get_device(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_PTP, + NULL); + if (!ptp_pdev) { + dev_err(&pdev->dev, "no PTP device found\n"); + err = -ENODEV; + goto err_get_ptp; + } + + priv->ptp_dev = &ptp_pdev->dev; + err = register_netdev(ndev); if (err) goto err_reg_netdev; @@ -197,6 +208,7 @@ static int enetc_vf_probe(struct pci_dev *pdev, return 0; err_reg_netdev: +err_get_ptp: enetc_free_msix(priv); err_config_si: err_alloc_msix: From patchwork Fri May 21 04:36:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Y.b. Lu" X-Patchwork-Id: 445144 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74861C433B4 for ; Fri, 21 May 2021 04:27:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5827C60FE5 for ; Fri, 21 May 2021 04:27:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239140AbhEUE2r (ORCPT ); Fri, 21 May 2021 00:28:47 -0400 Received: from inva020.nxp.com ([92.121.34.13]:47194 "EHLO inva020.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239065AbhEUE21 (ORCPT ); Fri, 21 May 2021 00:28:27 -0400 Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id C00781A044F; Fri, 21 May 2021 06:26:33 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva020.eu-rdc02.nxp.com C00781A044F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=nselector3; t=1621571193; bh=wmrQZJnvsK/YpHBbN6kF1obxIzNuyuELS5e2tkyqutM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IRzCgWWtdd3sK+iReBajEXRlFiXw8++subHGYe3Kt9nZRJLbvQJpgpjY1eNt+k1sM Q0keUlyv98f94R9k74AeiwBZlkLoAmNve8zlaX4WcqxBc5TO4RGV0CnvPwVay64+0C XfmJrHBYnMo5mEuccTdTXYRJRwec1INQ6KNlDf61lAWVydjmxFoflBmufLNAK+XSzm sash822EXdP9OMQkZ+cQE6V8dUL1lhMWktXwgzSGPo4Fy+TE4nmdLMR3rz5B7lT4QN /gXxNPF0J5dSuHvD6KZTrK/u5tBz0B+ZrLs7ZmTR16sJbjxH/wajJcESzNyoQ1hvxo ZsNEygqUFUmVw== Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 115281A0425; Fri, 21 May 2021 06:26:31 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 inva020.eu-rdc02.nxp.com 115281A0425 Received: from localhost.localdomain (mega.ap.freescale.net [10.192.208.232]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id 6A538402B0; Fri, 21 May 2021 12:26:27 +0800 (+08) From: Yangbo Lu To: netdev@vger.kernel.org Cc: Yangbo Lu , "David S . Miller" , Richard Cochran , Claudiu Manoil , Jakub Kicinski Subject: [net-next, v2, 7/7] enetc: support PTP domain timestamp conversion Date: Fri, 21 May 2021 12:36:19 +0800 Message-Id: <20210521043619.44694-8-yangbo.lu@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210521043619.44694-1-yangbo.lu@nxp.com> References: <20210521043619.44694-1-yangbo.lu@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Support timestamp conversion to specified PTP domain in PTP packet. Signed-off-by: Yangbo Lu --- Changes for v2: - Fixed build waring. - Updated copyright. --- drivers/net/ethernet/freescale/enetc/enetc.c | 39 ++++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/drivers/net/ethernet/freescale/enetc/enetc.c b/drivers/net/ethernet/freescale/enetc/enetc.c index 3ca93adb9662..cd0429c73999 100644 --- a/drivers/net/ethernet/freescale/enetc/enetc.c +++ b/drivers/net/ethernet/freescale/enetc/enetc.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -/* Copyright 2017-2019 NXP */ +/* Copyright 2017-2021 NXP */ #include "enetc.h" #include @@ -7,6 +7,7 @@ #include #include #include +#include #include static int enetc_num_stack_tx_queues(struct enetc_ndev_priv *priv) @@ -472,13 +473,36 @@ static void enetc_get_tx_tstamp(struct enetc_hw *hw, union enetc_tx_bd *txbd, *tstamp = (u64)hi << 32 | tstamp_lo; } -static void enetc_tstamp_tx(struct sk_buff *skb, u64 tstamp) +static int enetc_ptp_parse_domain(struct sk_buff *skb, u8 *domain) +{ + unsigned int ptp_class; + struct ptp_header *hdr; + + ptp_class = ptp_classify_raw(skb); + if (ptp_class == PTP_CLASS_NONE) + return -EINVAL; + + hdr = ptp_parse_header(skb, ptp_class); + if (!hdr) + return -EINVAL; + + *domain = hdr->domain_number; + return 0; +} + +static void enetc_tstamp_tx(struct enetc_ndev_priv *priv, struct sk_buff *skb, + u64 tstamp) { struct skb_shared_hwtstamps shhwtstamps; + u64 ts = tstamp; + u8 domain; if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) { + if (!enetc_ptp_parse_domain(skb, &domain)) + ptp_clock_domain_tstamp(priv->ptp_dev, &ts, domain); + memset(&shhwtstamps, 0, sizeof(shhwtstamps)); - shhwtstamps.hwtstamp = ns_to_ktime(tstamp); + shhwtstamps.hwtstamp = ns_to_ktime(ts); skb_txtime_consumed(skb); skb_tstamp_tx(skb, &shhwtstamps); } @@ -575,7 +599,7 @@ static bool enetc_clean_tx_ring(struct enetc_bdr *tx_ring, int napi_budget) */ schedule_work(&priv->tx_onestep_tstamp); } else if (unlikely(do_twostep_tstamp)) { - enetc_tstamp_tx(skb, tstamp); + enetc_tstamp_tx(priv, skb, tstamp); do_twostep_tstamp = false; } napi_consume_skb(skb, napi_budget); @@ -698,6 +722,7 @@ static void enetc_get_rx_tstamp(struct net_device *ndev, struct enetc_hw *hw = &priv->si->hw; u32 lo, hi, tstamp_lo; u64 tstamp; + u8 domain; if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TSTMP) { lo = enetc_rd_reg_hot(hw->reg + ENETC_SICTR0); @@ -708,6 +733,12 @@ static void enetc_get_rx_tstamp(struct net_device *ndev, hi -= 1; tstamp = (u64)hi << 32 | tstamp_lo; + + skb_reset_mac_header(skb); + + if (!enetc_ptp_parse_domain(skb, &domain)) + ptp_clock_domain_tstamp(priv->ptp_dev, &tstamp, domain); + memset(shhwtstamps, 0, sizeof(*shhwtstamps)); shhwtstamps->hwtstamp = ns_to_ktime(tstamp); }