Home | History | Annotate | Line # | Download | only in freebsd
freebsd_timex.h revision 1.3.98.1
      1  1.3.98.1      mjf /*	$NetBSD: freebsd_timex.h,v 1.3.98.1 2007/12/08 18:18:36 mjf Exp $	*/
      2       1.2    perry 
      3       1.1  mycroft /******************************************************************************
      4       1.1  mycroft  *                                                                            *
      5       1.1  mycroft  * Copyright (c) David L. Mills 1993, 1994                                    *
      6       1.1  mycroft  *                                                                            *
      7       1.1  mycroft  * Permission to use, copy, modify, and distribute this software and its      *
      8       1.1  mycroft  * documentation for any purpose and without fee is hereby granted, provided  *
      9       1.1  mycroft  * that the above copyright notice appears in all copies and that both the    *
     10       1.1  mycroft  * copyright notice and this permission notice appear in supporting           *
     11       1.1  mycroft  * documentation, and that the name University of Delaware not be used in     *
     12       1.1  mycroft  * advertising or publicity pertaining to distribution of the software        *
     13       1.1  mycroft  * without specific, written prior permission.  The University of Delaware    *
     14       1.1  mycroft  * makes no representations about the suitability this software for any       *
     15       1.1  mycroft  * purpose.  It is provided "as is" without express or implied warranty.      *
     16       1.1  mycroft  *                                                                            *
     17       1.1  mycroft  ******************************************************************************/
     18       1.1  mycroft 
     19       1.1  mycroft /*
     20       1.1  mycroft  * Modification history timex.h
     21       1.1  mycroft  *
     22       1.1  mycroft  * 19 Mar 94	David L. Mills
     23       1.1  mycroft  *	Moved defines from kernel routines to header file and added new
     24       1.1  mycroft  *	defines for PPS phase-lock loop.
     25       1.1  mycroft  *
     26       1.1  mycroft  * 20 Feb 94	David L. Mills
     27       1.1  mycroft  *	Revised status codes and structures for external clock and PPS
     28       1.1  mycroft  *	signal discipline.
     29       1.1  mycroft  *
     30       1.1  mycroft  * 28 Nov 93	David L. Mills
     31       1.1  mycroft  *	Adjusted parameters to improve stability and increase poll
     32       1.1  mycroft  *	interval.
     33       1.1  mycroft  *
     34       1.1  mycroft  * 17 Sep 93    David L. Mills
     35       1.1  mycroft  *      Created file
     36       1.1  mycroft  */
     37       1.1  mycroft /*
     38       1.1  mycroft  * This header file defines the Network Time Protocol (NTP) interfaces
     39       1.1  mycroft  * for user and daemon application programs. These are implemented using
     40       1.1  mycroft  * private syscalls and data structures and require specific kernel
     41       1.1  mycroft  * support.
     42       1.1  mycroft  *
     43       1.1  mycroft  * NAME
     44       1.1  mycroft  *	ntp_gettime - NTP user application interface
     45       1.1  mycroft  *
     46       1.1  mycroft  * SYNOPSIS
     47       1.1  mycroft  *	#include <sys/timex.h>
     48       1.1  mycroft  *
     49       1.1  mycroft  *	int syscall(SYS_ntp_gettime, tptr)
     50       1.1  mycroft  *
     51       1.1  mycroft  *	int SYS_ntp_gettime		defined in syscall.h header file
     52       1.1  mycroft  *	struct ntptimeval *tptr		pointer to ntptimeval structure
     53       1.1  mycroft  *
     54       1.1  mycroft  * NAME
     55       1.1  mycroft  *	ntp_adjtime - NTP daemon application interface
     56       1.1  mycroft  *
     57       1.1  mycroft  * SYNOPSIS
     58       1.1  mycroft  *	#include <sys/timex.h>
     59       1.1  mycroft  *
     60       1.1  mycroft  *	int syscall(SYS_ntp_adjtime, mode, tptr)
     61       1.1  mycroft  *
     62       1.1  mycroft  *	int SYS_ntp_adjtime		defined in syscall.h header file
     63       1.1  mycroft  *	struct timex *tptr		pointer to timex structure
     64       1.1  mycroft  *
     65       1.1  mycroft  */
     66       1.1  mycroft #ifndef _FREEBSD_TIMEX_H
     67       1.1  mycroft #define _FREEBSD_TIMEX_H 1
     68       1.1  mycroft 
     69       1.1  mycroft #ifndef MSDOS			/* Microsoft specific */
     70       1.1  mycroft #include <sys/syscall.h>
     71       1.1  mycroft #endif /* MSDOS */
     72       1.1  mycroft 
     73       1.1  mycroft /*
     74       1.1  mycroft  * The following defines establish the engineering parameters of the
     75       1.1  mycroft  * phase-lock loop (PLL) model used in the kernel implementation. These
     76       1.1  mycroft  * parameters have been carefully chosen by analysis for good stability
     77       1.1  mycroft  * and wide dynamic range.
     78       1.1  mycroft  *
     79       1.1  mycroft  * The hz variable is defined in the kernel build environment. It
     80       1.1  mycroft  * establishes the timer interrupt frequency, 100 Hz for the SunOS
     81       1.1  mycroft  * kernel, 256 Hz for the Ultrix kernel and 1024 Hz for the OSF/1
     82       1.1  mycroft  * kernel. SHIFT_HZ expresses the same value as the nearest power of two
     83       1.1  mycroft  * in order to avoid hardware multiply operations.
     84       1.1  mycroft  *
     85       1.1  mycroft  * SHIFT_KG and SHIFT_KF establish the damping of the PLL and are chosen
     86       1.1  mycroft  * for a slightly underdamped convergence characteristic.
     87       1.1  mycroft  *
     88       1.1  mycroft  * MAXTC establishes the maximum time constant of the PLL. With the
     89       1.1  mycroft  * SHIFT_KG and SHIFT_KF values given and a time constant range from
     90       1.1  mycroft  * zero to MAXTC, the PLL will converge in 15 minutes to 16 hours,
     91       1.1  mycroft  * respectively.
     92       1.1  mycroft  */
     93       1.1  mycroft #define SHIFT_HZ 7		/* log2(hz) */
     94       1.1  mycroft #define SHIFT_KG 6		/* phase factor (shift) */
     95       1.1  mycroft #define SHIFT_KF 16		/* frequency factor (shift) */
     96       1.1  mycroft #define MAXTC 6			/* maximum time constant (shift) */
     97       1.1  mycroft 
     98       1.1  mycroft /*
     99       1.1  mycroft  * The following defines establish the scaling of the various variables
    100       1.1  mycroft  * used by the PLL. They are chosen to allow the greatest precision
    101       1.1  mycroft  * possible without overflow of a 32-bit word.
    102       1.1  mycroft  *
    103       1.1  mycroft  * SHIFT_SCALE defines the scaling (shift) of the time_phase variable,
    104       1.1  mycroft  * which serves as a an extension to the low-order bits of the system
    105       1.1  mycroft  * clock variable time.tv_usec.
    106       1.1  mycroft  *
    107       1.1  mycroft  * SHIFT_UPDATE defines the scaling (shift) of the time_offset variable,
    108       1.1  mycroft  * which represents the current time offset with respect to standard
    109       1.1  mycroft  * time.
    110       1.1  mycroft  *
    111       1.1  mycroft  * SHIFT_USEC defines the scaling (shift) of the time_freq and
    112       1.1  mycroft  * time_tolerance variables, which represent the current frequency
    113       1.1  mycroft  * offset and maximum frequency tolerance.
    114       1.1  mycroft  *
    115       1.1  mycroft  * FINEUSEC is 1 us in SHIFT_UPDATE units of the time_phase variable.
    116       1.1  mycroft  */
    117       1.1  mycroft #define SHIFT_SCALE 23		/* phase scale (shift) */
    118       1.1  mycroft #define SHIFT_UPDATE (SHIFT_KG + MAXTC) /* time offset scale (shift) */
    119       1.1  mycroft #define SHIFT_USEC 16		/* frequency offset scale (shift) */
    120       1.1  mycroft #define FINEUSEC (1L << SHIFT_SCALE) /* 1 us in phase units */
    121       1.1  mycroft 
    122       1.1  mycroft /*
    123       1.1  mycroft  * The following defines establish the performance envelope of the PLL.
    124       1.1  mycroft  * They insure it operates within predefined limits, in order to satisfy
    125       1.1  mycroft  * correctness assertions. An excursion which exceeds these bounds is
    126       1.1  mycroft  * clamped to the bound and operation proceeds accordingly. In practice,
    127       1.1  mycroft  * this can occur only if something has failed or is operating out of
    128       1.1  mycroft  * tolerance, but otherwise the PLL continues to operate in a stable
    129       1.1  mycroft  * mode.
    130       1.1  mycroft  *
    131       1.1  mycroft  * MAXPHASE must be set greater than or equal to CLOCK.MAX (128 ms), as
    132       1.1  mycroft  * defined in the NTP specification. CLOCK.MAX establishes the maximum
    133       1.1  mycroft  * time offset allowed before the system time is reset, rather than
    134       1.1  mycroft  * incrementally adjusted. Here, the maximum offset is clamped to
    135       1.1  mycroft  * MAXPHASE only in order to prevent overflow errors due to defective
    136       1.1  mycroft  * protocol implementations.
    137       1.1  mycroft  *
    138       1.1  mycroft  * MAXFREQ is the maximum frequency tolerance of the CPU clock
    139       1.1  mycroft  * oscillator plus the maximum slew rate allowed by the protocol. It
    140       1.1  mycroft  * should be set to at least the frequency tolerance of the oscillator
    141       1.1  mycroft  * plus 100 ppm for vernier frequency adjustments. If the kernel
    142       1.1  mycroft  * PPS discipline code is configured (PPS_SYNC), the oscillator time and
    143       1.1  mycroft  * frequency are disciplined to an external source, presumably with
    144       1.1  mycroft  * negligible time and frequency error relative to UTC, and MAXFREQ can
    145       1.1  mycroft  * be reduced.
    146       1.1  mycroft  *
    147       1.1  mycroft  * MAXTIME is the maximum jitter tolerance of the PPS signal if the
    148       1.1  mycroft  * kernel PPS discipline code is configured (PPS_SYNC).
    149       1.1  mycroft  *
    150       1.1  mycroft  * MINSEC and MAXSEC define the lower and upper bounds on the interval
    151       1.1  mycroft  * between protocol updates.
    152       1.1  mycroft  */
    153       1.1  mycroft #define MAXPHASE 128000L	/* max phase error (us) */
    154       1.1  mycroft #ifdef PPS_SYNC
    155       1.1  mycroft #define MAXFREQ (100L << SHIFT_USEC) /* max freq error (100 ppm) */
    156       1.1  mycroft #define MAXTIME (200L << PPS_AVG) /* max PPS error (jitter) (200 us) */
    157       1.1  mycroft #else
    158       1.1  mycroft #define MAXFREQ (200L << SHIFT_USEC) /* max freq error (200 ppm) */
    159       1.1  mycroft #endif /* PPS_SYNC */
    160       1.1  mycroft #define MINSEC 16L		/* min interval between updates (s) */
    161       1.1  mycroft #define MAXSEC 1200L		/* max interval between updates (s) */
    162       1.1  mycroft 
    163       1.1  mycroft #ifdef PPS_SYNC
    164       1.1  mycroft /*
    165       1.1  mycroft  * The following defines are used only if a pulse-per-second (PPS)
    166       1.1  mycroft  * signal is available and connected via a modem control lead, such as
    167       1.1  mycroft  * produced by the optional ppsclock feature incorporated in the Sun
    168       1.1  mycroft  * asynch driver. They establish the design parameters of the frequency-
    169       1.1  mycroft  * lock loop used to discipline the CPU clock oscillator to the PPS
    170       1.1  mycroft  * signal.
    171       1.1  mycroft  *
    172       1.1  mycroft  * PPS_AVG is the averaging factor for the frequency loop, as well as
    173       1.1  mycroft  * the time and frequency dispersion.
    174       1.1  mycroft  *
    175       1.1  mycroft  * PPS_SHIFT and PPS_SHIFTMAX specify the minimum and maximum
    176       1.1  mycroft  * calibration intervals, respectively, in seconds as a power of two.
    177       1.1  mycroft  *
    178       1.1  mycroft  * PPS_VALID is the maximum interval before the PPS signal is considered
    179       1.1  mycroft  * invalid and protocol updates used directly instead.
    180       1.1  mycroft  *
    181       1.1  mycroft  * MAXGLITCH is the maximum interval before a time offset of more than
    182       1.1  mycroft  * MAXTIME is believed.
    183       1.1  mycroft  */
    184       1.1  mycroft #define PPS_AVG 2		/* pps averaging constant (shift) */
    185       1.1  mycroft #define PPS_SHIFT 2		/* min interval duration (s) (shift) */
    186       1.1  mycroft #define PPS_SHIFTMAX 8		/* max interval duration (s) (shift) */
    187       1.1  mycroft #define PPS_VALID 120		/* pps signal watchdog max (s) */
    188       1.1  mycroft #define MAXGLITCH 30		/* pps signal glitch max (s) */
    189       1.1  mycroft #endif /* PPS_SYNC */
    190       1.1  mycroft 
    191       1.1  mycroft /*
    192       1.1  mycroft  * The following defines and structures define the user interface for
    193       1.1  mycroft  * the ntp_gettime() and ntp_adjtime() system calls.
    194       1.1  mycroft  *
    195       1.1  mycroft  * Control mode codes (timex.modes)
    196       1.1  mycroft  */
    197       1.1  mycroft #define MOD_OFFSET	0x0001	/* set time offset */
    198       1.1  mycroft #define MOD_FREQUENCY	0x0002	/* set frequency offset */
    199       1.1  mycroft #define MOD_MAXERROR	0x0004	/* set maximum time error */
    200       1.1  mycroft #define MOD_ESTERROR	0x0008	/* set estimated time error */
    201       1.1  mycroft #define MOD_STATUS	0x0010	/* set clock status bits */
    202       1.1  mycroft #define MOD_TIMECONST	0x0020	/* set pll time constant */
    203       1.1  mycroft #define MOD_CLKB	0x4000	/* set clock B */
    204       1.1  mycroft #define MOD_CLKA	0x8000	/* set clock A */
    205       1.1  mycroft 
    206       1.1  mycroft /*
    207       1.1  mycroft  * Status codes (timex.status)
    208       1.1  mycroft  */
    209       1.1  mycroft #define STA_PLL		0x0001	/* enable PLL updates (rw) */
    210       1.1  mycroft #define STA_PPSFREQ	0x0002	/* enable PPS freq discipline (rw) */
    211       1.1  mycroft #define STA_PPSTIME	0x0004	/* enable PPS time discipline (rw) */
    212       1.1  mycroft 
    213       1.1  mycroft #define STA_INS		0x0010	/* insert leap (rw) */
    214       1.1  mycroft #define STA_DEL		0x0020	/* delete leap (rw) */
    215       1.1  mycroft #define STA_UNSYNC	0x0040	/* clock unsynchronized (rw) */
    216       1.1  mycroft 
    217       1.1  mycroft #define STA_PPSSIGNAL	0x0100	/* PPS signal present (ro) */
    218       1.1  mycroft #define STA_PPSJITTER	0x0200	/* PPS signal jitter exceeded (ro) */
    219       1.1  mycroft #define STA_PPSWANDER	0x0400	/* PPS signal wander exceeded (ro) */
    220       1.1  mycroft #define STA_PPSERROR	0x0800	/* PPS signal calibration error (ro) */
    221       1.1  mycroft 
    222       1.1  mycroft #define STA_CLOCKERR	0x1000	/* clock hardware fault (ro) */
    223       1.1  mycroft 
    224       1.1  mycroft #define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
    225       1.1  mycroft     STA_PPSERROR | STA_CLOCKERR) /* read-only bits */
    226       1.1  mycroft 
    227       1.1  mycroft /*
    228       1.1  mycroft  * Clock states (time_state)
    229       1.1  mycroft  */
    230       1.1  mycroft #define TIME_OK		0	/* no leap second warning */
    231       1.1  mycroft #define TIME_INS	1	/* insert leap second warning */
    232       1.1  mycroft #define TIME_DEL	2	/* delete leap second warning */
    233       1.1  mycroft #define TIME_OOP	3	/* leap second in progress */
    234       1.3      wiz #define TIME_WAIT	4	/* leap second has occurred */
    235       1.1  mycroft #define TIME_ERROR	5	/* clock not synchronized */
    236       1.1  mycroft 
    237       1.1  mycroft /*
    238       1.1  mycroft  * NTP user interface (ntp_gettime()) - used to read kernel clock values
    239       1.1  mycroft  *
    240       1.1  mycroft  * Note: maximum error = NTP synch distance = dispersion + delay / 2;
    241       1.1  mycroft  * estimated error = NTP dispersion.
    242       1.1  mycroft  */
    243       1.1  mycroft struct freebsd_ntptimeval {
    244       1.1  mycroft 	struct timeval time;	/* current time (ro) */
    245       1.1  mycroft 	long maxerror;		/* maximum error (us) (ro) */
    246       1.1  mycroft 	long esterror;		/* estimated error (us) (ro) */
    247       1.1  mycroft 	int time_state;		/* what ntp_gettime returns */
    248       1.1  mycroft };
    249       1.1  mycroft 
    250       1.1  mycroft /*
    251       1.1  mycroft  * NTP daemon interface - (ntp_adjtime()) used to discipline CPU clock
    252       1.1  mycroft  * oscillator
    253       1.1  mycroft  */
    254       1.1  mycroft struct freebsd_timex {
    255       1.1  mycroft 	unsigned int modes;	/* clock mode bits (wo) */
    256       1.1  mycroft 	long offset;		/* time offset (us) (rw) */
    257       1.1  mycroft 	long freq;		/* frequency offset (scaled ppm) (rw) */
    258       1.1  mycroft 	long maxerror;		/* maximum error (us) (rw) */
    259       1.1  mycroft 	long esterror;		/* estimated error (us) (rw) */
    260       1.1  mycroft 	int status;		/* clock status bits (rw) */
    261       1.1  mycroft 	long constant;		/* pll time constant (rw) */
    262       1.1  mycroft 	long precision;		/* clock precision (us) (ro) */
    263       1.1  mycroft 	long tolerance;		/* clock frequency tolerance (scaled
    264       1.1  mycroft 				 * ppm) (ro) */
    265       1.1  mycroft 	/*
    266       1.1  mycroft 	 * The following read-only structure members are implemented
    267       1.1  mycroft 	 * only if the PPS signal discipline is configured in the
    268       1.1  mycroft 	 * kernel.
    269       1.1  mycroft 	 */
    270       1.1  mycroft 	long ppsfreq;		/* pps frequency (scaled ppm) (ro) */
    271       1.1  mycroft 	long jitter;		/* pps jitter (us) (ro) */
    272       1.1  mycroft 	int shift;		/* interval duration (s) (shift) (ro) */
    273       1.1  mycroft 	long stabil;		/* pps stability (scaled ppm) (ro) */
    274       1.1  mycroft 	long jitcnt;		/* jitter limit exceeded (ro) */
    275       1.1  mycroft 	long calcnt;		/* calibration intervals (ro) */
    276       1.1  mycroft 	long errcnt;		/* calibration errors (ro) */
    277       1.1  mycroft 	long stbcnt;		/* stability limit exceeded (ro) */
    278       1.1  mycroft 
    279       1.1  mycroft };
    280       1.1  mycroft #ifdef __FreeBSD__
    281       1.1  mycroft 
    282       1.1  mycroft /*
    283       1.1  mycroft  * sysctl identifiers underneath kern.ntp_pll
    284       1.1  mycroft  */
    285       1.1  mycroft #define NTP_PLL_GETTIME	1	/* used by ntp_gettime() */
    286       1.1  mycroft #define NTP_PLL_MAXID	2	/* number of valid ids */
    287       1.1  mycroft 
    288       1.1  mycroft #define NTP_PLL_NAMES { \
    289       1.1  mycroft 			  { 0, 0 }, \
    290       1.1  mycroft 			  { "gettime", CTLTYPE_STRUCT }, \
    291       1.1  mycroft 		      }
    292       1.1  mycroft 
    293       1.1  mycroft #ifndef _KERNEL
    294       1.1  mycroft #include <sys/cdefs.h>
    295       1.1  mycroft 
    296       1.1  mycroft __BEGIN_DECLS
    297  1.3.98.1      mjf extern int ntp_gettime(struct ntptimeval *);
    298  1.3.98.1      mjf extern int ntp_adjtime(struct timex *);
    299       1.1  mycroft __END_DECLS
    300       1.1  mycroft 
    301       1.1  mycroft #endif /* not _KERNEL */
    302       1.1  mycroft 
    303       1.1  mycroft #endif /* __FreeBSD__ */
    304       1.1  mycroft #endif /* _FREEBSD_TIMEX_H */
    305