mbox series

[net-next,v2,0/7] ptp: support virtual clocks for multiple

Message ID 20210521043619.44694-1-yangbo.lu@nxp.com
Headers show
Series ptp: support virtual clocks for multiple | expand

Message

Y.b. Lu May 21, 2021, 4:36 a.m. UTC
Current PTP driver exposes one PTP device to user which binds network
interface/interfaces to provide timestamping. Actually we have a way
utilizing timecounter/cyclecounter to virtualize any number of PTP
clocks based on a same free running physical clock for using.
The purpose of having multiple PTP virtual clocks is for user space
to directly/easily use them for multiple domains synchronization.

               +------------------------------+
MAC driver:    |      rx/tx PTP packet        |
               +------------------------------+
                               ^
                               | Match vclock via domain value
                               | Convert HW timestamp to domain time
                               v
         +--------------+--------------+--------------+
vclock:  | ptp1 domain1 | ptp2 domain2 | ptpN domainN |
         +--------------+--------------+--------------+
pclock:  |             ptp0 free running              |
         +--------------------------------------------+

The block diagram may explain how it works. Besides the PTP virtual
clocks, the PTP packet HW timestamp converting to domain time is also
done in kernel.

An example to use it:

  Run two ptp4l jobs on each of two boards for domain 1 and 2 synchronziation.

  Board1:
    # echo 2 > /sys/class/ptp/ptp0/num_vclocks
    [  282.230432] ptp ptp0: new virtual clock ptp2
    [  282.235211] ptp ptp0: new virtual clock ptp3
    [  282.240354] ptp ptp0: guarantee physical clock free running
    # echo 1 > /sys/class/ptp/ptp2/domain
    # echo 2 > /sys/class/ptp/ptp3/domain
    #
    # ptp4l -i eno0 -p/dev/ptp2 -m --domainNumber=1 --priority1=127 > domain1-master.log 2>&1 &
    # ptp4l -i eno0 -p/dev/ptp3 -m --domainNumber=2 --priority1=128 > domain2-slave.log 2>&1 &

  Board2:
    # echo 2 > /sys/class/ptp/ptp0/num_vclocks
    [  259.619382] ptp ptp0: new virtual clock ptp2
    [  259.624140] ptp ptp0: new virtual clock ptp3
    [  259.629315] ptp ptp0: guarantee physical clock free running
    # echo 1 > /sys/class/ptp/ptp2/domain
    # echo 2 > /sys/class/ptp/ptp3/domain
    #
    # ptp4l -i eno0 -p/dev/ptp2 -m --domainNumber=1 --priority1=128 > domain1-slave.log 2>&1 &
    # ptp4l -i eno0 -p/dev/ptp3 -m --domainNumber=2 --priority1=127 > domain2-master.log 2>&1 &

  Physical clock is guaranteed to stay free running when virtual clocks
  are created. To back use physical clock,

    # echo 0 > /sys/class/ptp/ptp0/num_vclocks
    [  531.249944] ptp ptp0: delete virtual clock ptp3
    [  531.254971] ptp ptp0: delete virtual clock ptp2
    [  531.259976] ptp ptp0: only physical clock in use now

How the patch-set affect current ptp drivers/function:

  - For current drivers, no impact on all existing function.

  - For drivers adapted to use it in the future, no impact on all
    existing function unless configuring value to num_vclocks in
    sysfs to convert to using virtual clocks.

  - For drivers adapted to use it with virtual clocks in using
    - Manual configurations are required for domain value in sysfs.
    - Manual configurations are required for application to specify
      right ptp device for domain on which the application will run.
    Note,
    - Physical clock is guaranteed to stay free running. Operation on it
      returns error.
    - If domain value of application has no PTP clock matched, the
      original HW timestamp will be used.

How to adapt drivers to use the feature:

  - During normally registering PTP clock for physical clock with
    ptp_clock_info, just fill the new member vclock_cc which contains
    cyclecounter info for virtual clock.

  - Timestamp conversion to domain time need to be done in MAC driver.
    When get HW timestamp, parse PTP packet domain value and convert
    timestamp to domain time by calling API ptp_clock_domain_tstamp(),
    before submitting to upper stack.

TODO:

  - PTP physical clock and its timestamping capabilities is still
    binding to network interface/interfaces through ethtool_ts_info.
    New way is needed to support querying all PTP virtual clocks
    and their timestamping capabilities. Making application to use
    PTP virtual clocks dynamically is a direction.

  - PTP packet parsing for domain value and one-step sync (not related
    to this patch-set) is common used. Make such functions common.

Changes for v2:
	- Converted to num_vclocks for creating virtual clocks.
	- Guranteed physical clock free running when using virtual
	  clocks.
	- Fixed build warning.
	- Updated copyright.

Yangbo Lu (7):
  ptp: add ptp virtual clock driver framework
  ptp: support ptp physical/virtual clocks conversion
  ptp: support domains and timestamp conversion
  ptp_qoriq: export ptp clock reading function for cyclecounter
  enetc_ptp: support ptp virtual clock
  enetc: store ptp device pointer
  enetc: support PTP domain timestamp conversion

 Documentation/ABI/testing/sysfs-ptp           |  25 +++
 MAINTAINERS                                   |   6 +
 drivers/net/ethernet/freescale/enetc/enetc.c  |  39 +++-
 drivers/net/ethernet/freescale/enetc/enetc.h  |   3 +-
 .../net/ethernet/freescale/enetc/enetc_pf.c   |  14 +-
 .../net/ethernet/freescale/enetc/enetc_ptp.c  |  13 +-
 .../net/ethernet/freescale/enetc/enetc_vf.c   |  14 +-
 drivers/ptp/Makefile                          |   2 +-
 drivers/ptp/ptp_clock.c                       |  12 ++
 drivers/ptp/ptp_private.h                     |  42 +++++
 drivers/ptp/ptp_qoriq.c                       |  16 ++
 drivers/ptp/ptp_sysfs.c                       | 134 +++++++++++++
 drivers/ptp/ptp_vclock.c                      | 176 ++++++++++++++++++
 include/linux/fsl/ptp_qoriq.h                 |   3 +-
 include/linux/ptp_clock_kernel.h              |  57 +++++-
 15 files changed, 545 insertions(+), 11 deletions(-)
 create mode 100644 drivers/ptp/ptp_vclock.c


base-commit: 86544c3de6a2185409c5a3d02f674ea223a14217