wr-api.h 5.63 KB
Newer Older
1
/*
2 3
 * Copyright (C) 2011 CERN (www.cern.ch)
 * Author: Aurelio Colosimo
4
 * Based on ptp-noposix project (see AUTHORS for details)
5 6
 *
 * Released according to the GNU LGPL, version 2.1 or any later version.
7 8
 */

9 10 11
#ifndef __WREXT_WR_API_H__
#define __WREXT_WR_API_H__

12
#include <ppsi/lib.h>
13
#include "wr-constants.h"
14

15 16
#define WRS_PPSI_SHMEM_VERSION 5 /* added fields tx_count and rx_count to
				  * pp_instance */
17

18 19 20 21 22
/*
 * This structure is used as extension-specific data in the DSPort
 * (see wrspec.v2-06-07-2011, page 17)
 */
struct wr_dsport {
23
	struct wr_operations *ops; /* hardware-dependent, see below */
24 25 26
	Enumeration8 wrConfig;
	Enumeration8 wrMode;
	Boolean wrModeOn;
27
	Boolean ppsOutputOn;
28
	Enumeration8  wrPortState; /* used for sub-states during calibration */
29 30 31 32 33 34
	/* FIXME check doc: knownDeltaTx, knownDeltaRx, deltasKnown?) */
	Boolean calibrated;
	FixedDelta deltaTx;
	FixedDelta deltaRx;
	UInteger32 wrStateTimeout;
	UInteger8 wrStateRetry;
35
	UInteger32 calPeriod;		/* microseconsds, never changed */
36 37 38 39 40 41 42 43 44 45
	UInteger8 calRetry;
	Enumeration8 parentWrConfig;
	Boolean parentIsWRnode; /* FIXME Not in the doc */
	/* FIXME check doc: (parentWrMode?) */
	Enumeration16 msgTmpWrMessageID; /* FIXME Not in the doc */
	Boolean parentWrModeOn;
	Boolean parentCalibrated;

	/* FIXME: are they in the doc? */
	UInteger16 otherNodeCalSendPattern;
46
	UInteger32 otherNodeCalPeriod;/* microseconsds, never changed */
47 48 49 50 51 52 53 54 55 56 57 58 59
	UInteger8 otherNodeCalRetry;
	FixedDelta otherNodeDeltaTx;
	FixedDelta otherNodeDeltaRx;
	Boolean doRestart;
	Boolean linkUP;
};

/* This uppercase name matches "DSPOR(ppi)" used by standard protocol */
static inline struct wr_dsport *WR_DSPOR(struct pp_instance *ppi)
{
	return ppi->portDS->ext_dsport;
}

60 61
static inline Integer32 phase_to_cf_units(Integer32 phase)
{
62 63 64 65 66 67 68 69 70 71
	uint64_t ph64;
	if (phase >= 0) {
		ph64 = phase * 65536LL;
		__div64_32(&ph64, 1000);
		return ph64;
	} else {
		ph64 = -phase * 65536LL;
		__div64_32(&ph64, 1000);
		return -ph64;
	}
72 73
}

74
/* Pack/Unkpack White rabbit message in the suffix of PTP announce message */
75
void msg_pack_announce_wr_tlv(struct pp_instance *ppi);
76 77
void msg_unpack_announce_wr_tlv(void *buf, MsgAnnounce *ann);

78
/* Pack/Unkpack/Issue White rabbit message signaling msg */
79 80 81
int msg_pack_wrsig(struct pp_instance *ppi, Enumeration16 wr_msg_id);
void msg_unpack_wrsig(struct pp_instance *ppi, void *buf,
		      MsgSignaling *wrsig_msg, Enumeration16 *wr_msg_id);
82
int msg_issue_wrsig(struct pp_instance *ppi, Enumeration16 wr_msg_id);
83 84 85 86 87 88 89 90 91 92

/* White rabbit state functions */
int wr_present(struct pp_instance *ppi, unsigned char *pkt, int plen);
int wr_m_lock(struct pp_instance *ppi, unsigned char *pkt, int plen);
int wr_s_lock(struct pp_instance *ppi, unsigned char *pkt, int plen);
int wr_locked(struct pp_instance *ppi, unsigned char *pkt, int plen);
int wr_calibration(struct pp_instance *ppi, unsigned char *pkt, int plen);
int wr_calibrated(struct pp_instance *ppi, unsigned char *pkt, int plen);
int wr_resp_calib_req(struct pp_instance *ppi, unsigned char *pkt, int plen);
int wr_link_on(struct pp_instance *ppi, unsigned char *pkt, int plen);
93

94 95 96 97 98
/* Common functions, used by various states and hooks */
void wr_handshake_init(struct pp_instance *ppi, int mode);
void wr_handshake_fail(struct pp_instance *ppi); /* goto non-wr */
int wr_handshake_retry(struct pp_instance *ppi); /* 1 == retry; 0 == failed */

99 100 101
/* White Rabbit hw-dependent functions (code in arch-wrpc and arch-wrs) */
struct wr_operations {
	int (*locking_enable)(struct pp_instance *ppi);
102
	int (*locking_poll)(struct pp_instance *ppi, int grandmaster);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	int (*locking_disable)(struct pp_instance *ppi);
	int (*enable_ptracker)(struct pp_instance *ppi);

	int (*adjust_in_progress)(void);
	int (*adjust_counters)(int64_t adjust_sec, int32_t adjust_nsec);
	int (*adjust_phase)(int32_t phase_ps);

	int (*read_calib_data)(struct pp_instance *ppi,
			      uint32_t *deltaTx, uint32_t *deltaRx,
			      int32_t *fix_alpha, int32_t *clock_period);
	int (*calib_disable)(struct pp_instance *ppi, int txrx);
	int (*calib_enable)(struct pp_instance *ppi, int txrx);
	int (*calib_poll)(struct pp_instance *ppi, int txrx, uint32_t *delta);
	int (*calib_pattern_enable)(struct pp_instance *ppi,
				    unsigned int calibrationPeriod,
				    unsigned int calibrationPattern,
				    unsigned int calibrationPatternLen);
	int (*calib_pattern_disable)(struct pp_instance *ppi);
121
	int (*enable_timing_output)(struct pp_instance *ppi, int enable);
122
};
123 124 125 126


/* wr_servo interface */
int wr_servo_init(struct pp_instance *ppi);
127
void wr_servo_reset(void);
128
int wr_servo_man_adjust_phase(int phase);
129
void wr_servo_enable_tracking(int enable);
130 131 132 133 134
int wr_servo_got_sync(struct pp_instance *ppi, TimeInternal *t1,
		      TimeInternal *t2);
int wr_servo_got_delay(struct pp_instance *ppi, Integer32 cf);
int wr_servo_update(struct pp_instance *ppi);

135
struct wr_servo_state {
136
	char if_name[16]; /* Informative, for wr_mon through shmem */
137 138 139
	unsigned long flags;

#define WR_FLAG_VALID	1
140
#define WR_FLAG_WAIT_HW	2
141

142
	int state;
143 144

	/* These fields are used by servo code, after asetting at init time */
145 146 147 148 149 150
	int32_t delta_tx_m;
	int32_t delta_rx_m;
	int32_t delta_tx_s;
	int32_t delta_rx_s;
	int32_t fiber_fix_alpha;
	int32_t clock_period_ps;
151 152 153 154

	/* These fields are used by servo code, across iterations */
	TimeInternal t1, t2, t3, t4;
	int64_t delta_ms_prev;
155
	int missed_iters;
156

157 158 159 160 161
	/* Following fields are for monitoring/diagnostics (use w/ shmem) */
	TimeInternal mu;
	int64_t picos_mu;
	int32_t cur_setpoint;
	int64_t delta_ms;
162 163 164 165 166
	uint32_t update_count;
	int tracking_enabled;
	char servo_state_name[32];
	int64_t skew;
	int64_t offset;
167 168 169
};

/* All data used as extension ppsi-wr must be put here */
170 171
struct wr_data {
	struct wr_servo_state servo_state;
172
};
173

174
#endif /* __WREXT_WR_API_H__ */